home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / FAQSYS18.ZIP / FAQS.DAT / OPT32.TXT < prev    next >
Text File  |  1996-06-02  |  77KB  |  1,886 lines

  1. AP-500
  2.  
  3. Application
  4. Note
  5.  
  6.  
  7. Optimizations for Intel's 
  8. 32-Bit Processors
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. February 1994
  36.  
  37.  
  38.  
  39.  
  40.  
  41. The following are trademarks of Intel Corporation and may only be used to identify Intel products:
  42. Intel, Intel386, Intel486, i486, and Pentium
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. Intel Corporation makes no warranty for the use of its products and assumes no responsibility for any errors which may appear in this document nor does it make a commitment to update the information contained herein.
  75.  
  76. Intel retains the right to make changes to these specifications at any time, without notice.
  77.  
  78. Contact your local Intel sales office or your distributor to obtain the latest specifications before placing your product order.
  79.  
  80. MDS is an ordering code only and is not used as a product name or trademark of Intel Corporation.
  81.  
  82. Intel Corporation and Intel's FASTPATH are not affiliated with Kinetics, a division of Excelan, Inc. or it FASTPATH trademark or products.
  83.  
  84. *  Other brands and names are the property of their respective owners.
  85.  
  86. Additional copies of this document or other Intel literature may be obtained from:
  87.  
  88.         Intel Corporation
  89.         Literature Sales
  90.         P.O. Box 7641
  91.         Mt. Prospect, IL  60056-7641
  92.  
  93.         or call 1-800-879-4683
  94.  
  95.  
  96.  
  97. ╞ INTEL CORPORATION, 1993
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. Table of Contents
  106.  
  107.  
  108. 1.  Introduction     1
  109.  
  110. 2.  Overview of Intel386(tm), Intel486(tm), Pentium(tm) Processors    1
  111. 2.1. The Intel386 Processor    1
  112. 2.1.1.  Instruction Prefetcher    1
  113. 2.1.2.  Instruction Decoder    1
  114. 2.1.3.  Excution Core    1
  115. 2.2. The Intel486 processor    1
  116. 2.2.1.  Integer Pipeline    2
  117. 2.2.2.  On-Chip Cache    2
  118. 2.2.3.  On-Chip Floating-Point Unit    2
  119. 2.3. The Pentium processor    2
  120. 2.3.1.  Integer Pipelines    3
  121. 2.3.2.  Caches    3
  122. 2.3.3.  Instruction Prefetcher    3
  123. 2.3.4.  Branch Target Buffer    3
  124. 2.3.5.  Pipelined Floating-Point Unit    3
  125. 3.  Integer Examples    4
  126. 3.1. Code Sequence 1, Intel486 Processor    5
  127. 3.2. Code Sequence 1, Pentium Processor    6
  128. 3.3. Code Sequence 2, Intel486 Processor    6
  129. 3.4. Code Sequence 2, Pentium Processor    7
  130. 3.5. Code Sequence 3, Intel486 Processor    7
  131. 3.6. Code Sequence 3, Pentium Processor    7
  132. 4.  Code Generation Strategy    9
  133.  
  134. 5.  Blended Code Generation Consideration    10
  135. 5.1.  Choice of Index Versus Base Register    10
  136. 5.2.  Addressing Modes and Register Usage    10
  137. 5.3.  Prefetch Bandwidth    11
  138. 5.4.  Alignment    12
  139. 5.4.1.  Code    12
  140. 5.4.2.  Data    12
  141. 5.4.3.  2-Byte Data    12
  142. 5.4.4.  4-Byte Data    12
  143. 5.4.5.  8-Byte Data    12
  144. 5.5.  Prefixed Opcodes    12
  145. 5.6.  Integer Instruction Scheduling    13
  146. 5.6.1.  Pairing    13
  147. 5.6.2.  Instruction Set Pairability    13
  148.     5.6.2.1.  Unpairable Instructions (NP)    13
  149.     5.6.2.2.  Pairable Instructions Issued to U or V  pipes (UV)....................14
  150.     5.6.2.3.  Pairable Instructions Issued ot U pipe (PU)    14
  151.     5.6.2.4.  Pairable Instructions Issued to V  pipe (PV)    14
  152. 5.6.3.  Unpairability Due to Registers    14
  153. 5.6.4.  Special Pairs    15
  154. 5.6.5.  Restrictions on Pair Execution    16
  155. 5.7.  Integer Instruction Selection    16
  156.  
  157.  
  158.  
  159. 6.  Processor Specific Optimizations    20
  160. 6.1.  Pentium Processor Floating Point    20
  161. 6.1.1.  Floating-Point Example    20
  162. 6.1.2.  FXCH Rules and Regulations    22
  163. 6.1.3.  Memory Operands    23
  164. 6.1.4.  Floating-Point Stalls    23
  165. 7.  Summary    26
  166.  
  167. Appendix A - Instruction Pairing Summary............................................................................A-1
  168.     
  169.  
  170. 1. Introduction
  171.  
  172. The Intel386 Architecture Family represents a series of compatible processors including the
  173. Intel386, Intel486, and the Pentium processors.  The newer members of the family are capable of executing any binaries created for members of previous generations. For example, any existing 8086/8088, 80286, Intel386 (DX or SX), and Intel486 applications will be able to execute on the Pentium processor without any modification or recompilation. However, there are certain code optimization
  174. techniques which will make applications execute faster on a specific member of the family with
  175. little or no impact on the performance of other members. Most of these optimizations deal with
  176. instruction sequence selection and instruction reordering to complement the processor
  177. micro architecture .
  178.  
  179. The intent of this document is to describe the implementation differences of the processor members
  180. and the optimization strategy that gives the best performance for all members of the family.
  181.  
  182. 2. Overview of Intel386, Intel486, and Pentium Processors
  183.  
  184. 2.1 The Intel386 Processor
  185.  
  186. The Intel386 processor is the first implementation of the 32-bit Intel386 architecture. It includes full
  187. 32-bit data paths, rich 32-bit addressing modes and on-chip memory management.
  188.  
  189. 2.1.1. Instruction Prefetcher
  190.  
  191. The instruction prefetcher prefetches the instruction stream from external memory and the prefetched instructions are kept in its four-deep, four-byte-wide prefetch buffers. The instruction decoder operates on the code stream fed to it through the prefetch buffers.
  192.  
  193. 2.1.2. Instruction Decoder
  194.  
  195. The instruction decoder places the decoded information in a three-deep FIFO. It is decoupled from both the prefetcher and the execution core and has separate protocols with each.
  196.  
  197. 2.1.3. Execution Core
  198.  
  199. The core engine executes the incoming instructions one at a time, but for certain cases, it allows overlapping the last execution cycle of the current instruction with the effective address calculation of the next instruction's memory reference.
  200.  
  201. From the compiler writer's point of view, the decoupled prefetch/decode/execution stages and the sequential nature of the core engine has placed few requirements for instruction scheduling.  Avoiding the use of an index in the effective address will save one extra clock. A careful choice of instructions to minimize the execution clock counts is the best optimization approach.
  202.  
  203. 2.2 The Intel486 Processor
  204.  
  205. The Intel486 processor has a full blown integer pipeline delivering a peak throughput of one instruction per clock. It has integrated the first level cache and the floating-point unit on chip, and it has the same on-chip memory management capabilities as the Intel386 processor.
  206.  
  207.  
  208.  
  209. 2.2.1. Integer Pipeline
  210.  
  211. The Intel486 has a five-stage integer pipeline capable of processing one instruction per clock.
  212. The five pipeline stages are:
  213.  
  214. a. Prefetch, (PF) - where instructions are fetched from the cache and placed in one of two 16-byte buffers.
  215.  
  216. b. Decode (D1) - where incoming code stream is being decoded. Prefixed instructions stay in D1 for two clocks.
  217.  
  218. c. Address Generation (D2) - where effective address and linear address are calculated in parallel. The address generation can usually be completed in one cycle except in cases where the indexed addressing mode is used. If the index is used, the instruction stays in D2 for two clocks.
  219.  
  220. d. Execution (E) - where the machine operations are performed. Simple instructions (those with one machine operation) take one cycle to execute giving a maximum throughput of one instruction per clock. The more complex instructions take multiple execution cycles.
  221.  
  222. e. Writeback (WB) - where the needed register update occurs.
  223.  
  224. A taken branch breaks the pipeline stream and causes a two-clock penalty, whereas the pipeline stream is unaffected by a not-taken branch.
  225.  
  226. 2.2.2. On-Chip Cache
  227.  
  228. The on-chip cache is a combined instruction and data cache. It is 8-Kbytes in size, four-way set associative with a 16-byte line size and pseudo-LRU replacement algorithm. All data references have priority access to the cache over instruction prefetch cycles.
  229.  
  230. 2.2.3. On-Chip Floating-Point Unit
  231.  
  232. The on-chip floating-point unit utilizes the integer pipeline for early data access. The bus structure allows 64-bit data to be transferred between the cache and the floating point hardware in one clock. The floating-point design also allows overlapping the floating-point operations with integer operations.
  233.  
  234. The execution core of the Intel486 processor has been architected to maximize the throughput of a class of "frequently used" instructions. Hence, careful selection of an instruction sequence to perform a given task results in faster execution time. Also, code scheduling to avoid pipeline stalls helps to boost application performance. Most of the optimizations targeted to the Intel486 processor do not have negative effects on Intel386 processor.
  235.  
  236. 2.3 The Pentium Processor
  237.  
  238. The Pentium processor is an advanced superscalar processor.  It is built around two general purpose integer pipelines and a pipelined floating-point unit. The Pentium processor can execute two integer instructions simultaneously. A software-transparent dynamic branch-prediction mechanism minimizes pipeline stalls due to branches.
  239.  
  240.  
  241.  
  242.  
  243.  
  244. 2.3.1. Integer Pipelines
  245.  
  246. The Pentium processor has two parallel integer pipelines, the main pipe (U) which is an enhanced Intel486 processor pipe and the secondary pipe (V) which is similar to the main one but has some limitations on the instructions it can execute. The limitations will be described in more
  247. detail in later sections.
  248.  
  249. The Pentium processor can issue two instructions every cycle. During execution, the next two instructions are checked, and if possible, they are issued such that the first one executes in the U pipe, and the second in the V  pipe. (If it is not possible to issue two instructions, then the next instruction is issued to the U pipe and no instruction is issued to the V  pipe.)
  250.  
  251. When instructions execute in the two pipes, their behavior is exactly the same as if they were executed sequentially. When a stall occurs, successive instructions are not allowed to pass the stalled instruction in either pipe. In the Pentium processor's pipelines, the D2 stage can perform a multiway add, so there is not a one-clock index penalty as with the Intel486 pipeline.
  252.  
  253. 2.3.2. Caches
  254.  
  255. The on-chip cache subsystem consists of two (instruction and data) 8-Kbyte, two-way set associative caches with a cache line length of 32 bytes. There is a 64-bit wide external data bus interface. The caches employ a write back mechanism and an LRU replacement algorithm. The data cache consists of eight banks interleaved on four-byte boundaries.  The data cache can be accessed simultaneously from both pipes, as long as the references are to different banks. The minimum delay for a cache miss is 3 clocks.
  256.  
  257. 2.3.3.  Instruction Prefetcher
  258.  
  259. The instruction prefetcher has four buffers, each of which is 32 bytes long. It can fetch an instruction which is split among two cache lines with no penalty. Because the instruction and data caches are separate, instruction prefetches no longer conflict with data references for access to the cache (as in the case of Intel486 processor).
  260.  
  261. 2.3.4.  Branch Target Buffer
  262.  
  263. The Pentium processor employs a dynamic branch prediction scheme with a 256-entry Branch Target Buffer (BTB).  If the prediction is correct, there is no penalty when executing a branch instruction.  There is a 3-cycle penalty if the conditional branch was executed in the U pipe or a 4-cycle penalty if it was executed in the V  pipe. Mispredicted calls and unconditional jump instructions have a 3-clock penalty in either pipe. On the Intel486 processor, taken branches have a two-clock penalty.
  264.  
  265. 2.3.5. Pipelined Floating-Point Unit                             
  266.  
  267. The majority of the frequently used instructions are pipelined so that the pipelines can accept a new pair of operands every cycle. Therefore a good code generator can achieve a throughput of almost 1 instruction per cycle (of course this assumes a program with a modest amount of natural parallelism!). The fxch instruction can be executed in parallel with the commonly used FP instructions, which lets the code generator or programmer treat the floating-point stack as a regular register set without any performance degradation.
  268.  
  269. With the superscalar implementation, it is important to schedule the instruction stream to maximize the usage of the two integer pipelines. Since each of the Pentium processor's integer pipelines is enhanced from the pipeline of the Intel486 processor, the instruction scheduling criteria for the Pentium processor is a superset of the Intel486 processor requirements.
  270.  
  271. 3. Integer Examples
  272.  
  273. With the overview of the Intel386, Intel486 and Pentium processors in the previous section, the examples given in this section further illustrate the execution clock cycles among various instruction sequences.
  274.  
  275. All examples assume a 100% cache hit rate and non-conflicting memory accesses. A 32-bit flat address model is also assumed.
  276.  
  277. There is a cycle count next to each instruction. A cycle count appears without an instruction when there is a pipe stall. These examples also assume that the branch prediction is correct.
  278.  
  279. Going through these examples should give you an intuitive feel for how pairing works on the Pentium processor and additional insight about some of the common delays on both the Intel486 and Pentium processors.
  280.  
  281. C source:
  282.  
  283. static int a[10], b[10];
  284. int i;
  285. for (i=0; i<10; i++) {
  286.     a[i] = a[i] + 1;
  287.     b[i] = b[i] + 1;
  288. }
  289.  
  290. There are various instruction sequences which will produce a correct program. Their individual performance, however, may vary considerably.
  291.  
  292. Here are three examples:
  293.  
  294. Sequence 1    Sequence 2    Sequence 3
  295.  
  296.  xor eax, eax    xor eax, eax    mov eax, -40
  297.  
  298. TopOfLoop:     TopOfLoop:    TopOfLoop:
  299.      mov  edx, eax        inc  dword ptr [eax*4+a]    mov  edx, [eax+40+a]
  300.      shl  edx, 2        inc  dword ptr [eax*4+b]    mov  ecx, [eax+40+b]
  301.      inc  dword ptr [edx+a]        inc  eax        inc  edx
  302.      mov  edx, eax        cmp  eax, 10        inc  ecx
  303.      shl  edx, 2        jl   TopOfLoop        mov  [eax+40+a], edx
  304.      inc  dword ptr [edx+b]        mov  [eax+40+b], ecx
  305.      inc  eax         add  eax, 4
  306.      cmp  eax, 10        jnz  TopOfLoop
  307.      j1   TopOfLoop
  308.  
  309. Code Sequence 1 could benefit from common subexpression elimination. It is not unoptimized code, it is just not thoroughly optimized. Code that is unoptimized would not keep "i" in a register.
  310.  
  311. Code Sequence 2 is the most straight-forward style code.
  312.  
  313. Code Sequence 3 uses a load/store model. It also incorporates some induction variable elimination optimizations with test replacement. The loop counter in eax counts up to zero.  When it becomes zero, the jnz is not taken. This code avoids the compare instruction.
  314.  
  315. The performance of each of these code sequences is examined on both the Intel486 and Pentium 
  316. processors.
  317.  
  318. 3.1.  Code Sequence 1, Intel486 Processor
  319.  
  320. The shl instruction takes two cycles on an Intel486 processor. ALU operations (e.g. add) with
  321. memory results take 3 clocks: 1 to load, 1 to add, and 1 to store.
  322.  
  323.     mov  edx, eax     ; 1
  324.     shl  edx, 2    ; 2   2 clock instruction
  325.       (sh1)    ; 3
  326.     inc  dword ptr [edx+a]    ; 4   3 clocks with memory operand
  327.       (inc)    ; 5     plus 1 clock for edx AGI
  328.       (inc)    ; 6
  329.       (inc)    ; 7
  330.     mov  edx, eax    ; 8
  331.     shl  edx, 2    ; 9   2 clock instruction
  332.       (shl)    ; 10
  333.     inc  dword ptr [edx+b]    ; 11  3 clocks with memory operand
  334.       (inc)    ; 12    plus 1 clock for edx AGI
  335.       (inc)    ; 13
  336.       (inc)    ; 14
  337.     inc  eax    ; 15
  338.     cmp  eax, 10    ; 16
  339.     jl   TopOfLoop     ; 17  2 clocks because jl is prefixed
  340.         ;18
  341.         ; 19  branch taken penalty
  342.         ; 20
  343.     mov  edx, eax    ; 21  next iteration
  344.  
  345. Total: 20 cycles
  346.  
  347. Cycles 4 and 11 had an Address Generation Interlock (AGI) Delay. Register edx was written in cycle 3 and used as a base register in cycle 5. When a register is used in an effective address calculation in the cycle after the register is written, there is a one-clock penalty. This happens because the effective address calculation is performed in the D2 stage of the pipeline.
  348.  
  349. With the assembler used for this code sequence, the jl instruction was a "jump near," not a "jump short." "Jump near" is a 0f prefixed instruction. Prefixed instructions take an extra cycle on the Intel486 processor in the D1 stage.
  350.  
  351. The Intel486 processor does not have any branch prediction mechanism. Whenever jumps are taken,
  352. there is a 2-clock penalty (Cycles 19 and 20).
  353.  
  354.  
  355. 3.2.  Code Sequence 1, Pentium Processor
  356.  
  357. U pipe                V  pipe        
  358.  
  359. mov  edx, eax        ; 1
  360. shl  edx, 2         ; 2
  361. inc  dword ptr [edx+a]        ; 3   3 clocks with mem. op
  362.   (inc)        ; 4     plus 1 for edx AGI
  363.   (inc)        ; 5
  364.   (inc)    mov  edx, eax    ; 6   Pairs with last U cycle
  365. shl  edx, 2        ; 7
  366. inc  dword ptr [edx+b]        ; 8   3 clocks with mem. op
  367.   (inc)        ; 9     plus l for edx AGI
  368.   (inc)        ; 10
  369.   (inc)    inc  eax    ; 11  Pairs with last U cycle
  370. cmp  eax, 10    jl   TopOfLoop    ; 12
  371. mov  edx, eax        ; 13  Next iteration
  372.  
  373. Total :12 cycles
  374.  
  375. Note that the "shift" instruction takes two clocks on the Intel486 processor and only one on the Pentium processor. The Pentium processor has special hardware to avoid the 0f prefix delay on jcc "near" instructions. It also can pair the compare and jump, even though cmp writes a condition flag and jl reads it. The branch prediction hardware, when it predicts the branch to be taken, can execute the target instruction in the cycle following the jump. When a multiple cycle instruction in the U pipe pairs with another instruction, the last memory operation of the U pipe instruction pairs with the first operation of the V  pipe instruction (cycles 6 and 11).
  376.  
  377. 3.3.  Code Sequence 2, Intel486 Processor
  378.  
  379. inc  dword ptr [eax*4+a]     ; 1   3 clocks with memory operand
  380.   (inc)    ; 2     plus 1 for indexing
  381.   (inc)    ; 3
  382.   (inc)    ; 4
  383. inc  dword ptr [eax*4+b]    ; 5   3 clocks with memory operand
  384.   (inc)    ; 6     plus 1 for indexing
  385.   (inc)    ; 7
  386.   (inc)    ; 8
  387. inc  eax    ; 9
  388. cmp  eax, 10    ; 10
  389. jl   TopOfLoop    ; 11  2 clocks because jl is prefixed
  390.     ; 12
  391.     ; 13  Branch taken penalty
  392.     ; 14
  393. inc  dword ptr [eax*4+a]    ; 15  Next iteration
  394.  
  395. Total :14 cycles
  396.  
  397. On the Intel486 processor, whenever an index register is used in an effective address calculation,
  398. there is a one-clock penalty in the D2 stage (Cycles 1 and 5). This does not apply to base
  399. registers.
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407. 3.4.  Code sequence 2, Pentium Processor
  408.  
  409. U pipe                  V  pipe 
  410.  
  411. inc   dword ptr [eax*4+a]     ; 1 3 clocks with mem. op.
  412.   (inc)        ; 2
  413.   (inc)    inc  dword ptr [eax*4+b]    ; 3 1st V pairs with last U
  414.        (inc)    ; 4
  415.       (inc)    ; 5
  416. inc    eax        ; 6
  417. cmp    eax, 10    jl  TopOfLoop    ; 7
  418. inc    dword ptr [eax*4+a]    ; 8 Next iteration
  419.  
  420. Total: 7 cycles
  421.  
  422. The inc eax instruction at cycle 6 did not pair with the cmp instruction because of a register dependence. Other than in a few special cases (such as cmp-jmp), a register cannot be accessed until the cycle after it is written.
  423.  
  424. 3.5.  Code Sequence 3, Intel486 Processor
  425.  
  426.     mov  edx, [eax+40+a]    ; 1
  427.         ; 2   Fill prefetch buffer
  428.     mov  ecx, [eax+40+b]    ; 3
  429.     inc  edx    ; 4
  430.     inc  ecx    ; 5
  431.     mov  [eax+40+a], edx    ; 6
  432.     mov  [eax+40+b], ecx     ; 7
  433.     add  eax, 4    ; 8
  434.     jnz  TopOfLoop    ; 9   Prefix on jnz
  435.         ; 10
  436.         ; 11  Branch penalty
  437.             ; 12
  438.     mov  edx, [eax+40+a]    ; 13  Next iteration
  439.  
  440. Total : 12 cycles
  441.  
  442. The delay at clock 2 is caused by a miss in the Intel486 processor's prefetch buffer. The previous two examples had a similar penalty, but it was hidden by the 2-clocks used for the shl instruction in code sequence 1, and by the index penalty in code sequence 2.
  443.  
  444. 3.6.  Code Sequence 3, Pentium Processor
  445.  
  446. U pipe       V  pipe
  447.  
  448. mov  edx, [eax+40+a]    mov  ecx, [eax+40+b]    ; 1
  449. inc  edx    inc  ecx    ; 2
  450. mov  [eax+40+a], edx    mov  [eax+40+b], ecx    ; 3
  451. add  eax, 4    jnz  TopOfLoop    ; 4
  452. mov  edx, [eax+40+a]    mov  ecx, [eax+40+b]    ; 5 AGI on eax with
  453.   (mov)                           (mov)    ; 6 next iteration
  454.  
  455. Total: 5 cycles
  456.  
  457. The prefetch buffer delay on the Intel486 processor is no longer relevant. The Pentium processor has more prefetch buffers and different alignment capabilities. In the example above, the loop control is determined by the add eax, 4 instruction setting the zero condition code as it counts up to zero.
  458.  
  459. There is an AGI on eax because the add in cycle 4 writes to eax and both mov' s in cycle 5 reference it, even though there is a branch in between. On the Intel486 processor, AGl's only happen between adjacent instructions. On the Pentium processor, there can be two instructions in between and still be an AGI; for example, between a U pipe add in cycle n and a V  pipe mov that uses the result of the add as a base in cycle n+1. The general rule is than an AGI will occur when any instruction in cycle n writes to a register that is used in an effective address calculation in any instruction in cycle n+1. This is because the effective address calculation is performed in D2.
  460.  
  461. In a compiler's intermediate representation of the program before instruction scheduling (reordering), one might expect the order for sequence 3 to be:
  462.  
  463. load [a+eax+40]            load [b+eax+40]
  464.        |                             |
  465.        |                             |
  466.        add 1                      add 1
  467.         |                             |
  468.         |                             |
  469.      store [a+eax+40]      store [b+eax+40]
  470.                 |                 |            
  471.                 |                 |            
  472.                    add  eax, 1
  473.                          |
  474.                          |
  475.                      cmp  10
  476.                          |
  477.                          |
  478.                        jump
  479.  
  480. Reordering this intermediate code to obtain the assembly code shown earlier involves moving the load from "b" in front of the store into "a." Instruction reordering requires knowing that memory operands are independent. In this case, it can be easily proven that elements of "a" do not overlap in memory with elements of "b."
  481.  
  482. Comments              
  483.  
  484. Using a load/store paradigm works well on the Pentium processor because it exposes more opportunities for pairing instructions when the instructions are scheduled.  It does not however, increase the number of clocks, even without scheduling, though this ignores possible secondary effects such as larger code size.   This can lead to instruction cache misses.  Another secondary effect is the use of more registers, which are a limited resource on these processors. Compiler writers may want to pay more attention to register allocation.
  485.  
  486. In this document, we will refer to this as "load/store" style code generation.
  487.  
  488.  
  489. 4. Code Generation Strategy
  490.  
  491. Even though each member of the Intel386 processor family has a different micro architecture due to
  492. technology versus implementation tradeoffs, the differences induced few conflicts in the overall code optimization strategy. In fact, there is a set of "blended" optimizations that will create an optimal binary across the entire family. The "blended" optimizations include:
  493.  
  494. 1. Optimizations that benefit all members.
  495.  
  496. 2. Optimizations that benefit one or more members but do not hurt the remaining members.
  497.  
  498. 3. Optimizations that benefit one or more members a lot but only hurt the remaining members
  499.     a little.
  500.  
  501. For those optimizations that benefit only certain members but cause noticeable degradation to others, it is recommended that they be implemented under switches and left to the user to decide whether maximizing the performance of a specific processor is desirable.
  502.  
  503.  
  504. 5. Blended Code Generation Consideration
  505.  
  506. 5.1 Choice of Index Versus Base Register
  507.  
  508. The Intel386 and the Intel486 processors need an additional clock cycle to generate an effective address when an index register is used. Therefore, if only one indexing component is used (i.e., not both a base register and an index register) and scaling is not necessary, then it is faster to use the register as a base rather than an index. For example:
  509.  
  510. mov  eax, [esi]  ; use esi as base
  511. mov  eax, [esi*] ; use esi as index, 1 clock penalty
  512.  
  513. It takes the Pentium processor one clock to calculate the effective address even when an index register is used.  Hence, Pentium processor is neutral to the choice of index versus base register.
  514.  
  515. 5.2. Addressing Modes and Register Usage
  516.  
  517. 1. For the Intel486 processor, when a register is used as the base component, an additional clock cycle is used if that register is the destination of the immediately preceding instruction (assuming all instructions are already in the prefetch queue). For example:
  518.  
  519. add  esi, eax    ; esi is destination register
  520. mov  eax, [esi]  ; esi is base, 1 clock penalty
  521.  
  522. Since the Pentium processor has two integer pipelines and each pipeline has a similar organization on the Intel486 processor, a register used as the base or index component of an effective address calculation (in either pipe) causes an additional clock cycle if that register is the destination of either instruction from the immediately preceding cycle (Address Generation Interlock, (AGI)). To avoid the AGI, the instructions should be separated by at least one cycle by placing other instructions between them.
  523.  
  524. 2. Note that some instructions have implicit reads/writes to registers. Instructions that generate addresses implicitly through esp (push,pop/ret/call) also suffer from the AGI penalty.
  525.  
  526. Examples:
  527.  
  528. sub  esp, 24
  529.         ;  1 cycle stall
  530. push ebx
  531.  
  532.  
  533. mov  esp, ebp
  534.         ;  1 cycle stall
  535. pop  ebp
  536.  
  537. Push and pop also implicitly write to esp. This, however, does not cause an AGI when the next instruction addresses through esp.
  538.  
  539. Example:
  540.  
  541. push edi          ; no stall
  542. mov  ebx, [esp]
  543.  
  544.  
  545.  
  546. 3. On the Intel486 processor there is a 1-clock penalty for decoding an instruction with either an
  547. index or an immediate-displacement combination. On the Pentium processor, the immediate-displacement combination is not pairable. When it is necessary to use constants, it would still be more efficient to use immediate data instead of loading the constant into a register first.  However, if the same immediate data is used more than once, it would be faster to load the constant in a register and then use the register multiple times.
  548.  
  549. mov  result, 555     ; 555 is immediate, result is displacement
  550. mov  dword ptr [esp+4], 1    ; 1  is immediate,      4 is displacement
  551.  
  552. 4. The Intel486 processor has a 1-clock penalty when using a register immediately after its sub-
  553. register was written.  The Pentium processor is neutral in this respect.
  554.  
  555. Example (Pentium Processor):
  556.  
  557. mov  al,       0     ; 1
  558. mov  [ebp], eax     ; 2 - No delay on the Pentium processor
  559.  
  560. Example (Intel486 processor):
  561.  
  562. mov  al,       0     ; 1
  563.     ; 2
  564. mov  [ebp], eax    ; 3
  565.  
  566. 5.3 Prefetch Bandwidth
  567.  
  568. The Intel486 processor prefetch unit will access the on-chip cache to fill the prefetch queue whenever the cache is idle and there is enough room in the queue for another cache line (16 bytes). If the prefetch queue becomes empty, it can take up to three additional clocks to start the next instruction. The prefetch queue is 32 bytes in size (2 cache lines).
  569.  
  570. Because data accesses always have priority over prefetch requests, keeping the cache busy with data accesses can lock out the prefetch unit. As a result, optimized code should avoid four consecutive memory instructions.
  571.  
  572. It is important to arrange instructions so that the memory bus is not used continuously by a series of memory-reference instructions. The instructions should be rearranged so that there is a non-memory referencing instruction (such as a register instruction) at least two clocks before the prefetch queue becomes exhausted. This will allow the prefetch unit to transfer a cache line into the queue.
  573.  
  574. Such arrangement of the instructions will not affect the performance of the Intel386 and Pentium
  575. processors.
  576.  
  577. In general, it is difficult for a compiler to model the Intel486 prefetch buffer behavior. A sequence of four consecutive memory instructions without stalls (i.e., index penalty) will probably stall because of the prefetch buffers being exhausted.
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586. 5.4 Alignment
  587.  
  588. 5.4.1 Code                   
  589.        
  590. The Intel486 processor has a cache line size of 16 bytes and the Pentium processor has a cache line size of 32 bytes. Since the Intel486 processor has only two prefetch buffers (16 bytes each), code alignment has a direct impact on Intel486 processor performance as a result of the prefetch buffer efficiency. Code alignment has little effect on the Pentium processor performance because of its ability to prefetch across a cache line boundary with no penalty.  The Intel386 processor with no on-chip cache and a decoupled prefetch unit is not sensitive to code alignment. For optimal performance across the family, it is recommended that labels be aligned to the next 0MOD16 when it is less than 8 bytes away from that boundary.
  591.  
  592. 5.4.2. Data
  593.  
  594. A misaligned access in the data cache costs at least an extra 3 cycles on both the Intel486 and Pentium processor.
  595.  
  596. 5.4.3.  2-byte Data: A 2-byte object should be fully contained within an aligned 4-byte word (i.e., its binary address should be xxxx00, xxxx01, xxxx10, but not xxxx11).
  597.  
  598. 5.4.4.  4-byte Data: The alignment of 4-byte object should be on a 4-byte boundary.
  599.  
  600. 5.4.5.  8-byte Data:  An 8-byte datum (64-bit, e.g., double-precision reals) should be aligned on an 8-byte boundary.
  601.  
  602. 5.5 Prefixed Opcodes
  603.  
  604. On the Intel386 processor and the Intel486 processor, all prefix opcodes require an additional clock to decode. On the Pentium processor, an instruction with a prefix is pairable in the U pipe (PU) if the instruction (without the prefix) is pairable in both pipes (UV) or in the U pipe (PU). This is a special case of pairing. The prefixes are issued to the U pipe and get decoded in one cycle for each prefix and then the instruction is issued to the U pipe and may be paired.
  605.  
  606. All these prefixes: lock, segment override, address size, second opcode map (Of), and operand size belong to this group. Note that this includes all the 16 bit instructions when executing in 32-bit mode because an operand size prefix is required (e.g., mov word ptr [..], add word ptr [..], ...)
  607.  
  608. The near jcc prefix behaves differently; it does not take an extra cycle to decode and belongs to PV group.  Other 0f opcodes behave as normal prefixed instructions. For optimized code, prefixed opcodes should be avoided.
  609.  
  610. When prefixed opcodes have to be used, there are two cases in which overlap can be achieved between the extra clock it takes to decode a prefix and a cycle used by the previous instruction executing in the same pipe:
  611.  
  612. 1. The one-cycle penalty from using the result register of a previous instruction as a base or index (AGI).
  613.  
  614. 2. The last cycle of a preceding multi-cycle instruction.
  615.  
  616.  
  617.  
  618.  
  619.  
  620. 5.6 Integer Instruction Scheduling
  621.  
  622. Instruction scheduling is the process of reordering the instructions in a program to avoid stalls
  623. and delays while maintaining the semantics of the generated code.
  624.  
  625. Scheduling of integer instructions has two purposes:
  626.  
  627. 1.  Eliminate stalls in the Intel486 pipeline and each pipe of the Pentium processor.
  628.  
  629. There are some conditions where pipe stalls are encountered. The general guideline is to find instructions that can be inserted between the instructions that cause a stall. Since most of the commonly used integer instructions take only one clock, there is not much need to hide latencies. The most common delays which can be avoided through scheduling are AGI 's.
  630.  
  631. 2.  Create pairs for maximum throughput from the Pentium processor's dual pipe architecture:
  632.  
  633. The Pentium processor can issue two instructions for execution simultaneously. This is called pairing. There are limitations on which two instructions can be paired and some pairs, even when issued, will not execute in parallel. Pairing details are described in following sections.  More information about instruction pairability can be found in Appendix A.
  634.  
  635. Reordering instructions should be done in order to increase the possibility of issuing two instructions simultaneously.  Dependent instructions should be separated by at least one other instruction. Scheduling for the Pentium processor's dual pipe is overkill for the Intel486 processor but has otherwise little effect on its performance.
  636.  
  637. The following subsections are Pentium processor specific optimizations. These optimizations do not adversely impact the Intel386 and Intel486 processors.
  638.  
  639. 5.6.1. Pairing
  640.  
  641. Pairing cannot be performed when the following conditions occur:
  642.  
  643. 1. The next two instructions are not pairable instructions (See Appendix A for pairing characteristics of individual instructions.  In general, most simple ALU instructions are pairable.
  644.  
  645. 2. The next two instructions have some type of register contention (implicit or explicit). There are some special exceptions to this rule where register contention can occur with pairing. These are described later.
  646.  
  647. 3. The instructions are not both in the instruction cache.  An exception to this which permits pairing is if the first instruction is a one byte instruction.
  648.  
  649. 5.6.2.  Instruction Set Pairability
  650.  
  651. 5.6.2.1  Unpairable instructions (NP)
  652.  
  653. 1. shift/rotate with the shift count in cl
  654.  
  655. 2. Long-Arithmetic instructions, for example,  mul, div
  656.  
  657. 3. Extended instructions, for example, ret, enter, pusha, movs, rep stos, loopnz
  658.  
  659. 4. Some Floating-Point Instructions, for example, fscale, fldcw, fst
  660.  
  661. 5. Inter-segment instructions, for example, push sreg, call far
  662.  
  663. 5.6.2.2   Pairable instructions issued to U or V  pipes (UV)
  664.  
  665. 1. Most 8/32-bit ALU operations, for example, add, inc, xor,
  666.  
  667. 2. All 8/32-bit compare instructions, for example cmp, test
  668.  
  669. 3. All 8/32-bit stack operations using registers, for example, push reg, pop reg
  670.  
  671.  5.6.2.3  Pairable instructions issued to U pipe (PU)
  672.  
  673. These instructions must be issued to the U pipe and can pair with a suitable instruction in the V
  674. Pipe. These instructions never execute in the V pipe.
  675.  
  676. 1. Carry and borrow instructions, for example, adc, sbb
  677.  
  678. 2. Prefixed instructions (see next section)
  679.  
  680. 3. Shift with immediate
  681.  
  682. 4. Some Floating-Point Operations, for example, fadd, fmul, fld
  683.  
  684. 5.6.2.4  Pairable instructions issued to V  pipe (PV)
  685.  
  686. These instructions can execute in either the U pipe or the V pipe, but they are only paired when they are in the V  pipe. Since these instructions change the instruction pointer (eip), they cannot pair in the U pipe because the next instruction may not be adjacent. Even when a branch in the U pipe is predicted "not taken", it will not pair with the following instruction.
  687.  
  688. 1. Simple control transfer instructions, for example - call near, jmp near, jcc. This includes both the jcc short and the jcc near (which has a 0f prefix) versions of the conditional jump instructions.
  689.  
  690. 2. fxch
  691.  
  692. 5.6.3.  Unpairability Due to Registers
  693.  
  694. The pairability of an instruction is also affected by its operands. The following are the combinations that are not pairable due to register contention. Exceptions to these rules are given in the next section.
  695.  
  696. 1. The first instruction writes to a register that the second one reads from (flow-dependence).
  697.  
  698. Example:
  699.  
  700.      mov  eax, 8
  701.      mov  [ebp], eax
  702.  
  703.  
  704. 2. Both instructions write to the same register (output-dependence).
  705.  
  706. Example:
  707.  
  708.      mov  eax, 8
  709.      mov  eax, [ebp]
  710.  
  711. This limitation does not apply to a pair of instructions which write to the eflags register (e.g. two ALU operations that change the condition codes). The condition code after the paired instructions execute will have the condition from the V pipe instruction.
  712.  
  713. Note that a pair of instructions in which the first reads a register and the second writes to it (anti- dependence) is pairable.
  714.  
  715. Example:
  716.  
  717.      mov  eax, ebx       mov  ebx, [ebp]
  718.  
  719. For purposes of determining register contention, a reference to a byte or word register is treated
  720. as a reference to the containing 32-bit register. Hence,
  721.  
  722.      mov  al, 1
  723.      mov  ah, 0
  724.  
  725. do not pair due to apparent output dependencies on eax.
  726.  
  727. 5.6.4 Special Pairs
  728.  
  729. There are some instructions that can be paired although the general rule prohibits this. These special pairs overcome register dependencies. Most of these exceptions involve implicit reads/writes to the esp register or implicit writes to the condition codes:
  730.  
  731. Stack Pointer:
  732.  
  733. 1. push reg/imm; push reg/imm
  734.  
  735. 2. push reg/imm; call
  736.  
  737. 3. pop reg     ; pop reg
  738.  
  739. Condition Codes:
  740.  
  741. 1. cmp         ; jcc
  742.  
  743. 2. add         ; jne
  744.  
  745. Note that the special pairs that consist of push/pop instructions may have only immediate or register operands.
  746.  
  747.  
  748. 5.6.5 Restrictions on Pair Execution
  749.  
  750. There are some pairs that may be issued simultaneously but will not execute in parallel:
  751.  
  752. 1. If both instructions access the same data-cache memory bank then the second request (V  pipe) must wait for the first request to complete. A bank conflict occurs when bits 2-4 are the same in the two physical addresses. This is because the cache is organized as 8 banks of 32-bit wide data entries. A bank conflict incurs a one clock penalty on the V pipe instruction .
  753.  
  754. 2. Inter-pipe concurrency in execution preserves memory-access ordering. A multi-cycle instruction in the U pipe will execute alone until its last memory access.
  755.  
  756.     add  eax, meml    add  ebx, mem2     ; 1
  757.       (add)       (add)    ; 2  2-cycle
  758.  
  759. The instructions above add the contents of the register and the value at the memory location, then put the result in the register. An add with a memory operand takes two clocks to execute. The first clock loads the value from cache, and the second clock performs the addition. Since there is only one memory access in the U pipe instruction, the add in the V  pipe can start in the same cycle.
  760.  
  761.     add  meml, eax         ; 1
  762.         (add)         ; 2
  763.          (add)    add  mem2, ebx    ; 3
  764.            (add)    ; 4
  765.            (add)    ; 5
  766.  
  767. The above instructions add the contents of the register to the memory location and store the result at the memory location. An add with a memory result takes 3 clocks to execute.  The first clock loads the value, the second performs the addition, and the third stores the result. When paired, the last cycle of the U pipe instruction overlaps with the first cycle of the V pipe instruction execution.
  768.  
  769. No other instructions may begin execution until the instructions already executing have
  770. completed.
  771.  
  772. To expose the opportunities for scheduling and pairing, it is better to issue a sequence of simple instructions rather than a complex instruction that takes the same number of cycles. The simple instruction sequence can take advantage of more issue slots.  Compiler writers/programmers can also choose to reconstruct the complex form if the pairing opportunity does not materialize. The load/store style code generation requires more registers and increases code size. This impacts Intel486 processor performance, although only as a second order effect. To compensate for the extra registers needed, extra effort should be put into the register allocator and instruction scheduler so that extra registers are only used when parallelism increases.
  773.  
  774. 5.7 Integer Instruction Selection
  775.  
  776. The following highlights some instruction sequences to avoid and some sequences to use when generating optimal assembly code.
  777.  
  778. The lea instruction can be advantageous:
  779.  
  780. a. Lea may be used sometimes as a three/four operand addition instruction
  781.             e.g.,  lea  ecx, [eax+ebx+4+a]
  782.  
  783. b. In many cases an lea instruction or a sequence of lea, add, sub and the shift instructions may be used to replace constant multiply instructions.
  784.  
  785. c. This can also be used to avoid copying a register when both operands to an add are still needed  after the add, since lea need not overwrite its operands.
  786.  
  787. The disadvantage of the lea instruction is that it increases the possibility of an AGI stall with previous instructions. Lea is useful for shifts of 2,4,8 because the shift instructions take 2 clocks on Intel486 processor whereas lea only takes one.  On the Pentium processor, lea can execute in either U or V pipes, but the shift instructions can only execute in the U pipe. 
  788.  
  789. Complex instructions
  790.  
  791. Avoid using complex instructions (for example, enter, leave, loop).  Use sequences of simple
  792. instructions instead.
  793.  
  794. Zero-Extension of Short
  795.  
  796. The movzx instruction has a prefix and takes 3 cycles to execute, totaling 4 cycles. As with the Intel486 processor, it is recommended the following sequence be used instead:
  797.  
  798. xor  eax, eax
  799. mov  al, mem
  800.  
  801. If this occurs within a loop, it may be possible to pull the xor out of the loop if the only assignment to eax is the mov al, mem. This has greater importance for the Pentium processor since the movzx is not pairable and the new sequence may be paired with adjacent instructions.
  802.  
  803. Push mem
  804.  
  805. The push mem instruction takes four cycles for the Intel486 processor. It is recommended to use the following sequence because it takes only two cycles for the Intel486 processor and increases pairing opportunity for the Pentium processor.
  806.  
  807. mov  mem, reg
  808. push reg
  809.  
  810. Short Opcodes
  811.  
  812. Use one-byte long instructions as much as possible. This will reduce code size and help increase instruction density in the instruction cache. The most common example is using inc and dec rather than adding or subtracting the constant 1 with add or sub.
  813.  
  814. 8/16 bit operands
  815.  
  816. With 8-bit operands, try to use the byte opcodes, rather than using 32-bit operations on sign and zero extended bytes. Prefixes for operand size override apply to 16-bit operands, not to 8-bit operands.
  817.  
  818. Sign Extension is usually quite expensive. Often, the semantics can be maintained by zero extending 16-bit operands. Specifically, the C code in the following example does not need sign extension nor does it need prefixes for operand size overrides.
  819.  
  820. static short int a, b;
  821. if (a==b) {
  822.    . . .
  823. }
  824.  
  825. Code for comparing these 16-bit operands might be:
  826.  
  827.                           U pipe                V  pipe        
  828.  
  829.         xor  eax, eax     xor  ebx, ebx          ; 1
  830.         movw    ax, [a]                    ; 2 (prefix) + 1
  831.         movw    bx, [b]                    ; 4 (prefix) +1                              cmp  eax, ebx                            ; 6
  832.  
  833. The straight-forward method may be slower:
  834.  
  835. movsw     eax, a    ; 1  1 prefix + 3
  836. movsw     ebx, b    ; 5
  837. cmp        ebx, eax     ; 9
  838.  
  839. Of course, this can only be done under certain circumstances, but the circumstances tend to be quite common. This would not work if the compare was for greater than, less than, greater than or equal, and so on, or if the values in eax or ebx were to be used in another operation where sign extension was required.
  840.  
  841. Compares
  842.  
  843. Use test when comparing a value in a register with 0. Test essentially "ands" the operands together without writing to a destination register. If you "and" a value with itself and the result sets the zero condition flag, the value was zero. Test is preferred over and because the and writes the result register which may subsequently cause an AGI.  Test is better than cmp  .., 0 because the instruction size is smaller.
  844.  
  845. Use test when comparing the result of a Boolean "and" with an immediate constant for
  846. equality or inequality if the register is eax. (if (avar & 8) { }).
  847.  
  848. Test is a one-cycle pairable instruction when the form is eax, imm or reg, reg. Other
  849. forms of test take two cycles and do not pair.
  850.  
  851. Address Calculations
  852.  
  853. Pull address calculations into load and store instructions. Internally, memory reference instructions can have 4 operands: a relocatable load-time constant, an immediate constant, a base register, and a scaled index register. (In the segmented model, a segment register may constitute an additional operand in the linear address calculation.) In many cases, several integer instructions can be eliminated by fully using the operands of memory references.
  854.  
  855. When there is a choice to use either a base or index register, always choose the base because there is a 1-clock penalty on the Intel486 processor for using an index.
  856.  
  857. Clearing a Register
  858.  
  859. The preferred sequence to move zero to a register is xor reg, reg. This saves code space but sets the condition codes. In contexts where the condition codes must be preserved, use mov reg, 0.
  860.  
  861. Integer Divide
  862.  
  863. Typically, an integer divide is preceded by a cdq instruction (Divide instructions use edx: eax as the dividend and cdq sets up edx). It is better to copy eax into edx, then right shift edx 31 places to sign extend. The copy/shift takes the same number of clocks as cdq on both the Pentium and Intel486 processors, but the copy/shift  scheme allows two other instructions to execute at the same time on the Pentium processor.  If you know the value is positive, use xor edx, edx.
  864.  
  865. Prolog Sequences
  866.  
  867. Be careful to avoid AGl's in the prolog due to register esp. Since push can pair with other push instructions, saving callee-saved registers on entry to functions should use these instructions. If possible, load parameters before decrementing esp.
  868.  
  869. In  routines that do not call other routines (leaf routines), use esp as the base register to free up ebp. If you are not using the 32-bit flat model,  remember that ebp cannot be used as a general purpose base register because it references the stack segment.
  870.  
  871. Avoid Compares with Immediate Zero
  872.  
  873. Often when a value is compared with zero, the operation producing the value sets condition codes which can be tested directly by a jcc instruction. The most notable exceptions are mov and lea. In these cases, use test.
  874.  
  875. Epilog Sequence
  876.  
  877. If only 4 bytes were allocated in the stack frame for the current function, instead of incrementing the stack pointer by 4, use pop instructions. This avoids AGls and helps both Intel486 and Pentium processor.  For Pentium processor use 2 pops for eight bytes.
  878.  
  879. Integer Multiply by Constant 
  880.  
  881. The integer multiply by an immediate can usually be replaced by a faster series of shifts, adds, subs, and leas.
  882.  
  883. a. Binary Method
  884.  
  885. In general, if there are 8 or fewer bits set in the binary representation of the constant, it is better not to do the integer multiply. On an Intel486 processor, the break even point is lower: it is profitable if 6 bits or less are in the constant. Basically, shift and add for each bit set.
  886.  
  887. b. Factorization Method
  888.  
  889. This is done by factoring the constant by powers of two plus or minus one, and the constant plus or minus one by powers of two. If the number can be factored by powers of two, then the multiplication can be performed by a series of shifts. If powers of two plus or minus one are included, a shift of the previous
  890. result and an add or subtract of the previous result can be generated.  If the given number plus or minus one can be factored by a power of two, a shift of the previous result and an add or subtract of the original operand can be generated.   An iterative for checking powers of two from 31 to 1 can be done.  The shift amount needed and an ordinal to specify an add or subtract is saved for each factor. This information can be used in reverse order to generate the needed instructions.
  891.  
  892. For example:
  893.  
  894. imul  eax, 217      ; 10 clocks, no pairing
  895.  
  896. In checking powers of two in decreasing order  it is found that 217 will divide by 31.
  897.       217/31 = 7.       31 = (2^^5)-1
  898.  
  899. save shift = 5 and ordinal = sub_previous_result
  900.  
  901. After a check of 217/31 or 7, it is found that 7+1 is divisible by 8.
  902.  
  903. save shift = 3 and ordinal = sub_operand
  904.  
  905. After factoring, the instructions can be generated in reverse.
  906.  
  907.               mov  ecx, eax       ; 1
  908.        shl  eax, 3         ; 2           
  909.        sub  eax, ecx       ; 3
  910.        mov  ecx, eax       ; 4
  911.        shl  eax, 5         ; 5
  912.        sub  eax, ecx       ; 6                    
  913.  
  914. This code sequence allows scheduling of other instructions in the Pentium processor's V  pipe.
  915.  
  916. 6. Processor Specific Optimizations
  917.  
  918. 6.1 Pentium Processor Floating-Point Optimizations
  919.  
  920. The Pentium processor is the first generation of the Intel386 family that implements a pipelined floating-point unit.  However, in order to achieve maximum throughput from the Pentium processor floating-point unit, specific optimizations must be done.
  921.  
  922. 6.1.1 Floating-Point Example
  923.  
  924. FORTRAN source:
  925.  
  926.     subroutine da(x,y,z,n)
  927.     dimension x(n),y(n)
  928.  
  929.     do 10 i=l,n
  930. 10        x(i) = x(i) + y(i) * z
  931.  
  932.     return
  933.     end
  934.  
  935.  
  936. Assembly code:                            
  937.  
  938.                                       Pentium/Intel486 processor
  939.     TopOfLoop:
  940.         fld   dword ptr [esp+8]         ; 1    /    1
  941.         fmul  dword ptr [ebx+eax*4]     ; 2    /    5
  942.         fadd  dword ptr [ecx+eax*4]     ; 5    /   16
  943.         fstp  dword ptr [ecx+eax*4]        ; 9    /   26
  944.         inc   eax                         ; 11   /   33
  945.         cmp   eax, ebp                    ; 12   /   34
  946.         jle      TopOfLoop                   ; 12   /   36+2 for branch
  947.  
  948. Total :12 cycles per iteration
  949.  
  950. On the Intel486 processor, the time it takes to add and multiply varies depending on the values. In this example, 11 was used for multiply and 10 for add. The load takes 3 clocks; the store requires 7 clocks. The extra cycle before the fmul is an index penalty for the fmul. The fadd and fstp do not show an index penalty because the penalty overlapped with the execution of the previous floating-point instruction. These overlaps do not occur with fld or fxch.
  951.  
  952. On the Pentium processor, the results of fadd and fmul can be used three cycles after they start, except when the use is fst. When an fst instruction uses the result of another floating-point operation, an extra cycle is needed. The fst instruction executes for two cycles and nothing can execute in parallel.
  953.  
  954. There is an enormous improvement due to decreasing the clock counts for the common floating-point instructions; however, this example does not overlap any floating-point instructions. A further improvement can be achieved by overlapping the execution of the floating-point instructions as explained in the next section.
  955.  
  956. To expose more parallelism, loop unrolling can be used if the iterations are independent.
  957. Following is the assembly code after unrolling:
  958.  
  959.                                      Pentium processor     Intel486 CPU
  960.    TopOfLoop:
  961.         fld   dword ptr [esp+8]     ;  1                   1
  962.         fmul  dword ptr [ebx+eax*4]    ;  2                   5
  963.         fadd  dword ptr [ecx+eax*4]    ;  5                  16
  964.         fstp  dword ptr [ecx+eax*4]     ;  9                  26
  965.         fld   dword ptr [esp+8]    ;  11                 33
  966.         fmul  dword ptr [ebx+eax*4+4]    ;  12                 37
  967.         fadd  dword ptr [ecx+eax*4+4]    ;  15                 48
  968.         fstp  dword ptr [ecx+eax*4+4]    ;  19                 58
  969.         fld   dword ptr [esp+8]    ;  21                 65
  970.         fmul  dword ptr [ebx+eax*4+8]    ;  22                 69
  971.         fadd  dword ptr [ecx+eax*4+8]    ;  25                 80
  972.         fstp  dword ptr [ecx+eax*4+8]    ;  29                 90
  973.         add   eax, 3    ;  31                 97
  974.         cmp   eax, ebp    ;  32                 98
  975.         jle   TopOfLoop    ;  32            100+2 (br taken)
  976.  
  977. Total: 32 cycles (10.7/ iteration)
  978.  
  979. The clock count improvements gained through loop unrolling was due to eliminating some of the loop control overhead.  To get more improvement, we need to get the floating-point operations overlapped in order to hide their latencies.
  980.  
  981. Most floating-point operations require that one operand and the result use the top of stack. This makes each instruction dependent on the previous instruction and inhibits overlapping the instructions .
  982.  
  983. One obvious way to get around this is to change the architecture and have floating-point registers, rather than a stack. Unfortunately, upward and downward compatibility would be lost.   Instead, the fxch instruction was made "fast". This provides us another way to avoid the top of stack dependencies.  The fxch instructions can be paired with the common floating-point operations, so there is no penalty on the Pentium processor.  On the Intel486 processor, each fxch takes 4 clocks.
  984.  
  985. To take advantage of the exposed parallelism from loop unrolling, the instructions should be scheduled.
  986.  
  987.  
  988. Assembly code after unrolling and scheduling:
  989.  
  990.                                                         After Instruction
  991.                                     Intel486  Pentium   ST(O)     ST(l)    ST(2)
  992.                                        CPU      CPU     --------  -------- -----
  993. TopOfLoop: 
  994.     fld       dword ptr [esp+8]          1       1      z
  995.     fmul      dword ptr [ebx+eax*4]      5       2      y0*z
  996.     fld       dword ptr [esp+8]         16       3      z         y0*z
  997.     fmul      dword ptr [ebx+eax*4+4]   20       4      yl*z      y0*z
  998.     fxch      st(l)                     31       4      y0*z      yl*z
  999.     fadd      dword ptr [ecx+eax*4]     36       5      x0+y0*z   yl*z
  1000.     fld       dword ptr [esp+8]         46       6      z         x0+y0*z   yl*z
  1001.     fmul      dword ptr [ebx+eax*4+8]   50       7      y2*z      x0+y0*z   yl*z
  1002.     fxch      st(2)                     61       7      yl*z      x0+y0*z   y2*z
  1003.     fadd      dword ptr [ecx+eax*4+4]   66       8      xl+yl*z   x0+y0*z   y2*z
  1004.     fxch      st(l)                     76       8      x0+y0*z   xl+yl*z   y2*z
  1005.     fstp      dword ptr [ecx+eax*4]     81       9      xl+yl*z   y2*z
  1006.     fxch      st(l)                     88      11      y2*z      xl+yl*z
  1007.     fadd      dword ptr [ecx+eax*4+8]   93      12      x2+y2*z   xl+yl*z
  1008.     fxch      st(l)                    103      12      xl+yl*z   x2+y2*z
  1009.     fstp      dword ptr [ecx+eax*4+4]  108      13      x2+y2*z
  1010.     fstp      dword ptr [ecx+eax*4+8]  116      16
  1011.     add       eax, 3                   123      18
  1012.     cmp       eax, ebp                 124      19
  1013.     jle       TopOfLoop                126+2    19
  1014.  
  1015.                                            |
  1016.  
  1017.                                        (jle taken)
  1018.  
  1019. Total :1 9 cycles (6.3/iteration)
  1020.  
  1021. On the Intel486 processor, the index penalty and the added cost of fxch are apparent. The index penalty does not overlap with the fxch instruction.
  1022.  
  1023. On the Pentium processor, the fxch instructions pair with preceding fadd and fmul instructions and execute in parallel with them (cycles 7,8,12). The fxch instructions move an operand into position for the next floating point instruction. There is a cycle lost at clock 15 due to the store waiting 3 clocks after the instruction defining its operand.  The fxch instruction does not pair with fst and takes one clock as a separate instruction (cycles 9-11)
  1024.  
  1025. 6.1.2 FXCH Rules and Regulations
  1026.  
  1027. The fxch instruction can be executed for "free" when all of the following conditions occur:
  1028.  
  1029.  An FP instruction follows the fxch instruction.
  1030.  
  1031. An FP instruction belonging to the following list immediately precedes the fxch instruction: fadd, fsub, fmul, fld, fcom, fucom, fchs, ftst, fabs, fdiv.
  1032.  
  1033. This fxch instruction has already been executed. This is because the instruction boundaries in the cache are marked the first time the instruction is executed, so pairing only happens the second time this instruction is executed from the cache.
  1034.  
  1035. This means that this instruction is almost "free" and can be used to access elements in the deeper levels of the FP stack instead of storing them and then loading them again.
  1036.  
  1037.  
  1038. 6.1.3 Memory Operands
  1039.  
  1040. Performing a floating-point operation on a memory operand instead of on a stack register costs no cycles. In the integer part of the Pentium processor, it was better to avoid memory operands. In the floating-point part, you are encouraged to use memory operands.
  1041.  
  1042. 6.1.4 Floating-Point Stalls
  1043.  
  1044. There are cases where a delay occurs between two operations. Instructions should be inserted between the pair that cause the pipe stall. These instructions could be integer instructions or floating-point instructions that will not cause a new stall themselves. The number of instructions that should be inserted depends on the delay length.
  1045.  
  1046. One example of this is when a floating-point instruction depends on the result of the immediately preceding instruction which is also a floating-point instruction. In this case, it would be advantageous to move integer instructions between the two fp instructions, even if the integer instructions perform loop control. The following example restructures a loop in this manner:
  1047.  
  1048. for (i=0; i<Size; i++)
  1049.     array1 [i] += array2 [i];
  1050.  
  1051.  
  1052.            Pentium Processor         Intel486 Processor
  1053.            Clocks                     Clocks
  1054. TopOfLoop:
  1055.   flds   [eax + array2]    2 - AGI        3
  1056.   fadds  [eax + array1]    1        3
  1057.   fstps  [eax + array1]        5 - Wait for fadds    14 - Wait for fadds
  1058.   add    eax, 4        1        1
  1059.   jnz    TopOfLoop        0 - Pairs with add    3
  1060.                 ------                    ------
  1061.         9            24
  1062.  
  1063.  
  1064.  
  1065.                          Pentium Processor         Intel486 Processor
  1066.                               Clocks                     Clocks
  1067. TopOfLoop:
  1068.   fstps [eax + array1]    4 - Wait for fadds,AGI    10 - Wait for fadds
  1069. LoopEntryPoint:
  1070.   flds  [eax + array2]    1        3
  1071.   fadds [eax + array1]    1        3
  1072.   add eax, 4        1        1
  1073.   jnz TopOfLoop        0 - Pairs with add    3
  1074.         -------    -------
  1075.         7        20
  1076.  
  1077.  
  1078. By moving the integer instructions between the fadds and fstps, both processors can execute the integer instructions while the fadds is completing in the floating-point unit and before the fstps begins execution. Note that this new loop structure requires a separate entry point for the first iteration because the loop needs to begin with the flds. Also, there needs to be an additional fstps after the conditional jump to finish the final loop iteration.
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084. 1. Floating-Point Stores
  1085.  
  1086. A floating-point store must wait an extra cycle for its floating-point operand. After an fld, an fst must wait one clock. After the common arithmetic operations, fmul and fadd, which normally have a latency of two, fst waits an extra cycle for a total of three1.
  1087.  
  1088.     fld   meml    ; 1 fld takes 1 clock
  1089.         ; 2 fst waits, schedule something here
  1090.     fst   mem2    ; 3,4 fst takes 2 clocks
  1091.  
  1092.     fadd  meml    ; 1 add takes 3 clocks
  1093.         ; 2 add, schedule something here
  1094.         ; 3 add, schedule something here
  1095.         ; 4 fst waits, schedule something here
  1096.     fst   mem2    ; 5,2 fst takes 2 clocks
  1097.  
  1098. In the next example, the store is not dependent on the previous load:
  1099.  
  1100.     fld   meml          : 1
  1101.     fld   mem2          ; 2
  1102.     fxch  st(l)         ; 2
  1103.     fst   mem3          ; 3 stores values loaded from meml
  1104.  
  1105. 2. A register may be used immediately after it has been loaded (with fld).
  1106.  
  1107.     fld   mem1          ; l
  1108.     fadd  mem2          ; 2,3,4
  1109.  
  1110. 3. Use of a register by a floating-point operation immediately after it has been written by another fadd, fsub, or fmul causes a 2 cycle delay. If instructions are inserted between these two, then latency and a potential stall can be hidden.
  1111.  
  1112. 4. There are multi-cycle floating-point instructions (fdiv and fsqrt) that execute in the floating-point unit pipe. While executing these instructions in the floating-point unit pipe, integer instructions can be executed in parallel. Emitting a number of integer instructions after such an instruction will keep the integer execution units busy (the exact number of instructions depends on the floating-point instruction's cycle count).
  1113.  
  1114. 5. The integer multiply operations, mul and imul, are executed in the floating-point unit so these instructions cannot be executed in parallel with a floating-point instruction.
  1115.  
  1116. 6. A floating-point multiply instruction (fmul) delays for one cycle if the immediately preceding cycle executed an fmul or an fmul / fxch pair. The multiplier can only accept a new pair of operands every other cycle.
  1117.  
  1118. 7. Transcendental operations execute in the U pipe and nothing can be overlapped with them, so an integer instruction following such an instruction will wait until that instruction completes.
  1119.  
  1120. 8. Floating-point operations that take integer operands (fiadd or fisub ..) should be avoided. These instructions should be split into two instructions: fild and a floating-point operation. The number of cycles before another instruction can be issued (throughput) for fiadd is 4, while for fild and simple floating-point op it is 1.
  1121.  
  1122.  
  1123. Example:
  1124.  
  1125. Complex Instructions          Better for Potential Overlap
  1126.  
  1127. fiadd  [ebp] ; 4              fild   [ebp]  ; 1
  1128.                               faddp  st(l)  ; 2
  1129.  
  1130. Using the fild - faddp instructions yields 2 free cycles for executing other instructions.
  1131.  
  1132. 9. The fstsw instruction that usually appears after a floating-point comparison instruction (fcom, fcomp, fcompp) delays for 3 cycles. Other instructions may be inserted after the comparison instruction in order to hide the latency.
  1133.  
  1134. 10. Moving a floating-point memory/immediate to memory should be done by integer moves (if precision conversion is not needed) instead of doing fld - fstp.
  1135.  
  1136. Examples for floating-point moves:
  1137.  
  1138. double precision: 4 vs. 2 cycles
  1139.  
  1140.         fld  [ebp]     ; 1           mov  eax, [ebp]        ; 1
  1141.                        ; 2           mov  edx, [ebp+4]      ; 1
  1142.         fstp [edi]     ; 3,4         mov  [edi], eax        ; 2
  1143.                                      mov  [edi+4], edx      ; 2
  1144.  
  1145.     single precision: 4 vs. 2 cycles
  1146.  
  1147.             fld  [ebp]     ; 1           mov  eax, [ebp]        ; 1
  1148.                        ; 2           mov  [edi], eax        ; 2
  1149.         fstp [edi]     ; 3,4
  1150.  
  1151. This optimization also applies to the Intel486 processor.
  1152.  
  1153. 11. Transcendental operations execute on the Pentium processor much faster than on the Intel486 processor.  It may be worthwhile inlining some of these math library calls because of the fact that the call and prologue/epilogue overhead involved with the library calls is no longer negligible.  Emulating these operations in software will not be faster than the hardware unless accuracy is sacrificed.
  1154.  
  1155. 12. Integer instructions generally overlap with the floating-point operations except when the last
  1156. floating-point operation was fxch. In this case there is a one cycle delay.
  1157.  
  1158.     U pipe                   V  pipe        
  1159.  
  1160.     fadd                  fxch                           ; 1
  1161.                                                             ; 2 fxch delay
  1162.     mov   eax, 1          inc       edx                  ; 3
  1163.  
  1164.  
  1165. 7. Summary
  1166.  
  1167. The following tables summarize the micro architecture differences among Intel386, Intel486 and Pentium processors and the corresponding code generation consideration. It is possible to derive a set of code generation strategies that provide the optimal performance across the various members of the Intel386 processor family except for the use of FXCH to maximize the Pentium processor floating-point throughput  which can be implemented under a user-directed option.
  1168.  
  1169.  
  1170. Intel386TM Processor
  1171. Intel486TM  Processor
  1172. PentiumTM  Processor
  1173. Cache
  1174. None
  1175. 8K Combined
  1176. 8K Code, 8K Data
  1177. Prefetch
  1178. 4x4b filled by external memory access
  1179. 2x6b shared bus to cache
  1180. 4x32b private bus to cache
  1181. Decoder
  1182. 3 deep decoded FIFO
  1183. part of core pipeline
  1184. part of core pipeline
  1185. Core
  1186. some instruction overlap
  1187. 5 stages pipeline
  1188. 5 stages pipeline & superscalar
  1189. Math
  1190. Co-processor
  1191. On-Chip
  1192. On-Chip & pipelined
  1193.  
  1194.  
  1195. Processor Characteristics
  1196. Optimizations
  1197. Intel386 Processor
  1198. Intel486 Processor
  1199. Pentium Processor
  1200. Cache
  1201. Interleave mem with non-mem
  1202. don't care
  1203. Interleave if 4 consecutive
  1204. don't care
  1205. Prefetcher
  1206. Alignment
  1207. 0-MOD-4
  1208. 0-MOD-16
  1209. don't care
  1210. Pipelined Execution core
  1211. Base Vs index
  1212. don't care
  1213. use base
  1214. don't care
  1215.  
  1216. Avoid AGI
  1217. don't care
  1218. next instr
  1219. next 3 instr
  1220.  
  1221. Instruction selection
  1222. 1 clk penalty
  1223. short instr
  1224. short instr
  1225. Superscalar
  1226. Pairing
  1227. don't care
  1228. don't care
  1229. pair
  1230. Pipelined FPU with FXCH
  1231. more scheduling
  1232. 18 clk penalty
  1233. 4 clk penalty
  1234. schedule
  1235.  
  1236.  
  1237. Recommendations for Blended:
  1238.  
  1239. 1. Interleave mem with non-mem: do nothing
  1240.  
  1241. 2. Code alignment: 0-mod-16 on loop
  1242.  
  1243. 3. Base Vs index: use base
  1244.  
  1245. 4. Avoid AGI: next 3 instructions
  1246.  
  1247. 5. Instruction selection: short instructions sequence
  1248.  
  1249. 6. Pairing: pair
  1250.  
  1251. 7. FP scheduling: avoid FXCH
  1252.  
  1253. APPENDIX A
  1254. INSTRUCTION PAIRING SUMMARY
  1255.  
  1256.  
  1257. The following abbreviations are used in the Pairing column of the integer table in this Appendix:
  1258. NP    - Not pairable, executes in U pipe
  1259. PU    - Pairable if issued to U pipe
  1260. PV    - Pairable if issued to V pipe
  1261. UV    - Pairable in either pipe
  1262.  
  1263.  
  1264. In the floating-point table in this Appendix:
  1265. FX    - Pairs with FXCH
  1266. NP    - No pairing.
  1267.  
  1268. The I/O instructions are not pairable. 
  1269. Integer Instruction Pairing
  1270. INSTRUCTION    FORMAT
  1271. Pairing
  1272. AAA - ASCII Adjust after Addition
  1273. NP
  1274. AAD - ASCII Adjust AX before Division
  1275. NP
  1276. AAM - ASCII Adjust AX after Multiply
  1277. NP
  1278. AAS - ASCII Adjust AL after Subtraction
  1279. NP
  1280. ADC - ADD with Carry
  1281. PU
  1282. ADD - Add
  1283. UV
  1284. AND - Logical AND
  1285. UV
  1286. ARPL - Adjust RPL Field of Selector
  1287. NP
  1288. BOUND - Check Array Against Bounds
  1289. NP
  1290. BSF - Bit Scan Forward
  1291. NP
  1292. BSR - Bit Scan Reverse
  1293. NP
  1294. BSWAP - Byte Swap
  1295. NP
  1296. BT - Bit Test
  1297. NP
  1298. BTC - Bit Test and Complement
  1299. NP
  1300. BTR - Bit Test and Reset
  1301. NP
  1302. BTS - Bit Test and Set
  1303. NP
  1304. CALL - Call Procedure (in same segment)
  1305.  
  1306.    direct    1110 1000 : full displacement
  1307. PV
  1308.    register indirect    1111 1111 : 11 010 reg
  1309. NP
  1310.    memory indirect    1111 1111 : mod 010 r/m
  1311. NP
  1312. CALL - Call Procedure (in other segment)
  1313. NP
  1314. CBW - Convert Byte to Word 
  1315. CWDE - Convert Word to Doubleword
  1316. NP
  1317. CLC - Clear Carry Flag
  1318. NP
  1319. CLD - Clear Direction Flag
  1320. NP
  1321. CLI - Clear Interrupt Flag
  1322. NP
  1323. CLTS - Clear Task-Switched Flag in CR0
  1324. NP
  1325. CMC - Complement Carry Flag
  1326. NP
  1327. CMP - Compare Two Operands
  1328. UV
  1329. CMPS/CMPSB/CMPSW/CMPSD - Compare String Operands
  1330. NP
  1331. CMPXCHG - Compare and Exchange 
  1332. NP
  1333. CMPXCHG8B - Compare and Exchange 8 Bytes 
  1334. NP
  1335. CWD - Convert Word to Dword 
  1336. CDQ - Convert Dword to Qword
  1337. NP
  1338. DAA - Decimal Adjust AL after Addition
  1339. NP
  1340. DAS - Decimal Adjust AL after Subtraction
  1341. NP
  1342. DEC - Decrement by 1
  1343. UV
  1344. DIV - Unsigned Divide
  1345. NP
  1346. ENTER - Make Stack Frame for Procedure Parameters
  1347. NP
  1348. HLT - Halt
  1349.  
  1350.  
  1351. Integer Instruction Pairing (Continued)
  1352. INSTRUCTION    FORMAT
  1353. Pairing
  1354. IDIV - Signed Divide
  1355. NP
  1356. IMUL - Signed Multiply
  1357. NP
  1358. INC - Increment by 1
  1359. UV
  1360. INT n - Interrupt Type n 
  1361. NP
  1362. INT - Single-Step Interrupt 3
  1363. NP
  1364. INTO - Interrupt 4 on Overflow
  1365. NP
  1366. INVD - Invalidate Cache
  1367. NP
  1368. INVLPG - Invalidate TLB Entry
  1369. NP
  1370. IRET/IRETD - Interrupt Return
  1371. NP
  1372. Jcc - Jump if Condition is Met
  1373. PV
  1374. JCXZ/JECXZ - Jump on CX/ECX Zero
  1375. NP
  1376. JMP - Unconditional Jump (to same segment)
  1377.  
  1378.    short    1110 1011 : 8-bit displacement
  1379. PV
  1380.    direct    1110 1001 : full displacement
  1381. PV
  1382.    register indirect    1111 1111 : 11 100 reg
  1383. NP
  1384.    memory indirect    1111 1111 : mod 100 r/m
  1385. NP
  1386. JMP - Unconditional Jump (to other segment)
  1387. NP
  1388. LAHF - Load Flags into AH Register
  1389. NP
  1390. LAR - Load Access Rights Byte
  1391. NP
  1392. LDS - Load Pointer to DS
  1393. NP
  1394. LEA - Load Effective Address
  1395. UV
  1396. LEAVE - High Level Procedure Exit
  1397. NP
  1398. LES - Load Pointer to ES 
  1399. NP
  1400. LFS - Load Pointer to FS 
  1401. NP
  1402. LGDT - Load Global Descriptor Table Register
  1403. NP
  1404. LGS - Load Pointer to GS 
  1405. NP
  1406. LIDT - Load Interrupt Descriptor Table Register
  1407. NP
  1408. LLDT - Load Local Descriptor Table Register
  1409. NP
  1410. LMSW - Load Machine Status Word
  1411. NP
  1412. LOCK - Assert LOCK# Signal Prefix
  1413.  
  1414. LODS/LODSB/LODSW/LODSD - Load String Operand
  1415. NP
  1416. LOOP - Loop Count 
  1417. NP
  1418. LOOPZ/LOOPE - Loop Count while Zero/Equal
  1419. NP
  1420. LOOPNZ/LOOPNE - Loop Count while not Zero/Equal
  1421. NP
  1422. LSL - Load Segment Limit
  1423. NP
  1424. LSS - Load Pointer to SS    0000 1111 : 1011 0010 : mod reg r/m
  1425. NP
  1426. LTR - Load Task Register
  1427. NP
  1428. MOV - Move Data
  1429. UV
  1430. MOV - Move to/from Control Registers
  1431. NP
  1432. MOV - Move to/from Debug Registers
  1433. NP
  1434.  
  1435. Integer Instruction Pairing (Continued)
  1436. INSTRUCTION    FORMAT
  1437. Pairing
  1438. MOV - Move to/from Segment Registers
  1439. NP
  1440. MOVS/MOVSB/MOVSW/MOVSD - Move Data from String to String
  1441. NP
  1442. MOVSX - Move with Sign-Extend
  1443. NP
  1444. MOVZX - Move with Zero-Extend
  1445. NP
  1446. MUL - Unsigned Multiplication of AL, AX or EAX
  1447. NP
  1448. NEG - Two's Complement Negation
  1449. NP
  1450. NOP - No Operation    1001 0000
  1451. UV
  1452. NOT - One's Complement Negation
  1453. NP
  1454. OR - Logical Inclusive OR
  1455. UV
  1456. POP - Pop a Word from the Stack
  1457.  
  1458.    reg    1000 1111 : 11 000 reg
  1459. UV
  1460.       or    0101 1 reg
  1461. UV
  1462.    memory    1000 1111 : mod 000 r/m
  1463. NP
  1464. POP - Pop a Segment Register from the Stack
  1465. NP
  1466. POPA/POPAD - Pop All General Registers
  1467. NP
  1468. POPF/POPFD - Pop Stack into FLAGS or EFLAGS Register
  1469. NP
  1470. PUSH - Push Operand onto the Stack
  1471.  
  1472.    reg    1111 1111 : 11 110 reg
  1473. UV
  1474.       or    0101 0 reg
  1475. UV
  1476.    memory    1111 1111 : mod 110 r/m
  1477. NP
  1478.    immediate    0110 10s0 : immediate data
  1479. UV
  1480. PUSH - Push Segment Register onto the Stack
  1481. NP
  1482. PUSHA/PUSHAD - Push All General Registers
  1483. NP
  1484. PUSHF/PUSHFD - Push Flags Register onto the Stack
  1485. NP
  1486. RCL - Rotate thru Carry Left
  1487.  
  1488.    reg by 1    1101 000w : 11 010 reg
  1489. PU
  1490.    memory by 1    1101 000w : mod 010 r/m
  1491. PU
  1492.    reg by CL    1101 001w : 11 010 reg
  1493. NP
  1494.    memory by CL    1101 001w : mod 010 r/m
  1495. NP
  1496.    reg by immediate count    1100 000w : 11 010 reg : imm8 data
  1497. PU
  1498.    memory by immediate count    1100 000w : mod 010 r/m : imm8 data
  1499. PU
  1500. RCR - Rotate thru Carry Right
  1501.  
  1502.    reg by 1    1101 000w : 11 011 reg
  1503. PU
  1504.    memory by 1    1101 000w : mod 011 r/m
  1505. PU
  1506.    reg by CL    1101 001w : 11 011 reg
  1507. NP
  1508.    memory by CL    1101 001w : mod 011 r/m
  1509. NP
  1510.    reg by immediate count    1100 000w : 11 011 reg : imm8 data
  1511. PU
  1512.    memory by immediate count    1100 000w : mod 011 r/m : imm8 data
  1513. PU
  1514. RDMSR - Read from Model-Specific Register
  1515. NP
  1516. REP LODS - Load String
  1517. NP
  1518.  
  1519. Integer Instruction Pairing (Continued)
  1520. INSTRUCTION    FORMAT
  1521. Pairing
  1522. REP MOVS - Move String
  1523. NP
  1524. REP STOS - Store String
  1525. NP
  1526. REPE CMPS - Compare String (Find Non-Match)
  1527. NP
  1528. REPE SCAS - Scan String (Find Non-AL/AX/EAX)
  1529. NP
  1530. REPNE CMPS - Compare String (Find Match)
  1531. NP
  1532. REPNE SCAS - Scan String (Find AL/AX/EAX)
  1533. NP
  1534. RET - Return from Procedure (to same segment)
  1535. NP
  1536. RET - Return from Procedure (to other segment)
  1537. NP
  1538. ROL - Rotate (not thru Carry) Left
  1539.  
  1540.    reg by 1    1101 000w : 11 000 reg
  1541. PU
  1542.    memory by 1    1101 000w : mod 000 r/m
  1543. PU
  1544.    reg by CL    1101 001w : 11 000 reg
  1545. NP
  1546.    memory by CL    1101 001w : mod 000 r/m
  1547. NP
  1548.    reg by immediate count    1100 000w : 11 000 reg : imm8 data
  1549. PU
  1550.    memory by immediate count    1100 000w : mod 000 r/m : imm8 data
  1551. PU
  1552. ROR - Rotate (not thru Carry) Right
  1553.  
  1554.    reg by 1    1101 000w : 11 001 reg
  1555. PU
  1556.    memory by 1    1101 000w : mod 001 r/m
  1557. PU
  1558.    reg by CL    1101 001w : 11 001 reg
  1559. NP
  1560.    memory by CL    1101 001w : mod 001 r/m
  1561. NP
  1562.    reg by immediate count    1100 000w : 11 001 reg : imm8 data
  1563. PU
  1564.    memory by immediate count    1100 000w : mod 001 r/m : imm8 data
  1565. PU
  1566. RSM - Resume from System  Management Mode
  1567. NP
  1568. SAHF - Store AH into Flags
  1569. NP
  1570. SAL - Shift Arithmetic Left    same instruction as SHL
  1571.  
  1572. SAR - Shift Arithmetic Right
  1573.  
  1574.    reg by 1    1101 000w : 11 111 reg
  1575. PU
  1576.    memory by 1    1101 000w : mod 111 r/m
  1577. PU
  1578.    reg by CL    1101 001w : 11 111 reg
  1579. NP
  1580.    memory by CL    1101 001w : mod 111 r/m
  1581. NP
  1582.    reg by immediate count    1100 000w : 11 111 reg : imm8 data
  1583. PU
  1584.    memory by immediate count    1100 000w : mod 111 r/m : imm8 data
  1585. PU
  1586. SBB - Integer Subtraction with Borrow
  1587. PU
  1588. SCAS/SCASB/SCASW/SCASD - Scan String
  1589. NP
  1590. SETcc - Byte Set on Condition
  1591. NP
  1592. SGDT - Store Global Descriptor Table Register
  1593. NP
  1594.  
  1595. Integer Instruction Pairing (Continued)
  1596. INSTRUCTION    FORMAT
  1597. Pairing
  1598. SHL - Shift Left
  1599.  
  1600.    reg by 1    1101 000w : 11 100 reg
  1601. PU
  1602.    memory by 1    1101 000w : mod 100 r/m
  1603. PU
  1604.    reg by CL    1101 001w : 11 100 reg
  1605. NP
  1606.    memory by CL    1101 001w : mod 100 r/m
  1607. NP
  1608.    reg by immediate count    1100 000w : 11 100 reg : imm8 data
  1609. PU
  1610.    memory by immediate count    1100 000w : mod 100 r/m : imm8 data
  1611. PU
  1612. SHLD - Double Precision Shift Left
  1613.  
  1614.    register by immediate count    0000 1111 : 1010 0100 : 11 reg2 reg1 : imm8
  1615. NP
  1616.    memory by immediate count    0000 1111 : 1010 0100 : mod reg r/m : imm8
  1617. NP
  1618.    register by CL    0000 1111 : 1010 0101 : 11 reg2 reg1
  1619. NP
  1620.    memory by CL    0000 1111 : 1010 0101 : mod reg r/m
  1621. NP
  1622. SHR - Shift Right
  1623.  
  1624.    reg by 1    1101 000w : 11 101 reg
  1625. PU
  1626.    memory by 1    1101 000w : mod 101 r/m
  1627. PU
  1628.    reg by CL    1101 001w : 11 101 reg
  1629. NP
  1630.    memory by CL    1101 001w : mod 101 r/m
  1631. NP
  1632.    reg by immediate count    1100 000w : 11 101 reg : imm8 data
  1633. PU
  1634.    memory by immediate count    1100 000w : mod 101 r/m : imm8 data
  1635. PU
  1636. SHRD - Double Precision Shift Right
  1637.  
  1638.    register by immediate count    0000 1111 : 1010 1100 : 11 reg2 reg1 : imm8
  1639. NP
  1640.    memory by immediate count    0000 1111 : 1010 1100 : mod reg r/m : imm8
  1641. NP
  1642.    register by CL    0000 1111 : 1010 1101 : 11 reg2 reg1
  1643. NP
  1644.    memory by CL    0000 1111 : 1010 1101 : mod reg r/m
  1645. NP
  1646. SIDT - Store Interrupt Descriptor Table Register
  1647. NP
  1648. SLDT - Store Local Descriptor Table Register
  1649. NP
  1650. SMSW - Store Machine Status Word
  1651. NP
  1652. STC - Set Carry Flag
  1653. NP
  1654. STD - Set Direction Flag
  1655. NP
  1656. STI - Set Interrupt Flag
  1657.  
  1658. STOS/STOSB/STOSW/STOSD - Store String Data
  1659. NP
  1660. STR - Store Task Register
  1661. NP
  1662. SUB - Integer Subtraction
  1663. UV
  1664. TEST - Logical Compare
  1665.  
  1666.    reg1 and reg2    1000 010w : 11 reg1 reg2
  1667. UV
  1668.    memory and register    1000 010w : mod reg r/m
  1669. UV
  1670.    immediate and register    1111 011w : 11 000 reg : immediate data
  1671. NP
  1672.    immediate and accumulator    1010 100w : immediate data
  1673. UV
  1674.    immediate and memory    1111 011w : mod 000 r/m : immediate data
  1675. NP
  1676.  
  1677. Integer Instruction Pairing (Continued)
  1678. INSTRUCTION    FORMAT
  1679. Pairing
  1680. VERR - Verify a Segment for Reading 
  1681. NP
  1682. VERW - Verify a Segment for Writing
  1683. NP
  1684. WAIT - Wait    1001 1011
  1685. NP
  1686. WBINVD - Write-Back and Invalidate Data Cache
  1687. NP
  1688. WRMSR - Write to Model-Specific Register
  1689. NP
  1690. XADD - Exchange and Add
  1691. NP
  1692. XCHG - Exchange Register/Memory with Register
  1693. NP
  1694. XLAT/XLATB - Table Look-up Translation
  1695. NP
  1696. XOR - Logical Exclusive OR
  1697. UV
  1698.  
  1699. Floating-Point Instruction Pairing
  1700. INSTRUCTION    FORMAT
  1701. Pairing
  1702. F2XM1 - Compute 2ST(0) - 1
  1703. NP
  1704. FABS - Absolute Value
  1705. FX
  1706. FADD - Add
  1707. FX
  1708. FADDP - Add and Pop
  1709. FX
  1710. FBLD - Load Binary Coded Decimal
  1711. NP
  1712. FBSTP - Store Binary Coded Decimal and Pop
  1713. NP
  1714. FCHS - Change Sign
  1715. FX
  1716. FCLEX - Clear Exceptions
  1717. NP
  1718. FCOM - Compare Real
  1719. FX
  1720. FCOMP - Compare Real and Pop
  1721. FX
  1722. FCOMPP - Compare Real and Pop Twice
  1723.  
  1724. FCOS - Cosine of ST(0)
  1725. NP
  1726. FDECSTP - Decrement Stack-Top Pointer
  1727. NP
  1728. FDIV - Divide
  1729. FX
  1730. FDIVP - Divide and Pop
  1731. FX
  1732. FDIVR - Reverse Divide
  1733. FX
  1734. FDIVRP - Reverse Divide and Pop
  1735. FX
  1736. FFREE - Free ST(i) Register
  1737. NP
  1738. FIADD - Add Integer
  1739. NP
  1740. FICOM - Compare Integer
  1741. NP
  1742. FICOMP - Compare Integer and Pop
  1743. NP
  1744. FIDIV
  1745. NP
  1746. FIDIVR
  1747. NP
  1748. FILD - Load Integer
  1749. NP
  1750. FIMUL
  1751. NP
  1752. FINCSTP - Increment Stack Pointer
  1753. NP
  1754. FINIT - Initialize Floating-Point Unit
  1755. NP
  1756. FIST - Store Integer
  1757. NP
  1758. FISTP - Store Integer and Pop
  1759. NP
  1760. FISUB
  1761. NP
  1762. FISUBR
  1763. NP
  1764. FLD - Load Real
  1765.  
  1766.    32-bit memory    11011 001 : mod 000 r/m
  1767. FX
  1768.    64-bit memory    11011 101 : mod 000 r/m
  1769. FX
  1770.    80-bit memory    11011 011 : mod 101 r/m
  1771. NP
  1772.    ST(i)    11011 001 : 11 000 ST(i)
  1773. FX
  1774. FLD1 - Load +1.0 into ST(0)
  1775. NP
  1776. FLDCW - Load Control Word
  1777. NP
  1778. FLDENV - Load FPU Environment
  1779. NP
  1780. FLDL2E - Load log2() into ST(0)
  1781. NP
  1782. FLDL2T - Load log2(10) into ST(0)
  1783. NP
  1784.  
  1785. Floating-Point Instruction Pairing (Continued)
  1786. INSTRUCTION    FORMAT
  1787. Pairing
  1788. FLDLG2 - Load log10(2) into ST(0)
  1789. NP
  1790. FLDLN2 - Load log(2) into ST(0)
  1791. NP
  1792. FLDPI - Load  into ST(0)
  1793. NP
  1794. FLDZ - Load +0.0 into ST(0)
  1795. NP
  1796. FMUL - Multiply
  1797. FX
  1798. FMULP - Multiply
  1799. FX
  1800. FNOP - No Operation
  1801. NP
  1802. FPATAN - Partial Arctangent
  1803. NP
  1804. FPREM - Partial Remainder
  1805. NP
  1806. FPREM1 - Partial Remainder (IEEE)
  1807. NP
  1808. FPTAN - Partial Tangent
  1809. NP
  1810. FRNDINT - Round to Integer
  1811.  
  1812. FRSTOR - Restore FPU State
  1813. NP
  1814. FSAVE - Store FPU State
  1815. NP
  1816. FSCALE - Scale
  1817. NP
  1818. FSIN - Sine
  1819. NP
  1820. FSINCOS - Sine and Cosine
  1821. NP
  1822. FSQRT - Square Root
  1823. NP
  1824. FST - Store Real
  1825. NP
  1826. FSTCW - Store Control Word
  1827. NP
  1828. FSTENV - Store FPU Environment
  1829. NP
  1830. FSTP - Store Real and Pop
  1831. NP
  1832. FSTSW - Store Status Word into AX
  1833. NP
  1834. FSTSW - Store Status Word into Memory
  1835. NP
  1836. FSUB - Subtract
  1837. FX
  1838. FSUBP - Subtract and Pop
  1839. FX
  1840. FSUBR - Reverse Subtract
  1841. FX
  1842. FSUBRP - Reverse Subtract and Pop
  1843. FX
  1844. FTST - Test
  1845. FX
  1846. FUCOM - Unordered Compare Real)
  1847. FX
  1848. FUCOMP - Unordered Compare and Pop
  1849. FX
  1850. FUCOMPP - Unordered Compare  and Pop Twice
  1851. FX
  1852. FXAM - Examine
  1853. NP
  1854. FXCH - Exchange ST(0) and ST(i)
  1855.  
  1856. FXTRACT - Extract Exponent  and Significand
  1857. NP
  1858. FYL2X - ST(1)  log2(ST(0))
  1859. NP
  1860. FYL2XP1 - ST(1)  log2(ST(0) + 1.0)
  1861. NP
  1862. FWAIT - Wait until FPU Ready
  1863.  
  1864.  
  1865.  
  1866. 1  This set includes also the faddp, fsubrp, ... instructions
  1867.  
  1868.     
  1869.  
  1870. Optimizations for Intel's 32-Bit Processors                                                               Version 2.0
  1871.  
  1872.                                                                             Page  26
  1873.  
  1874. Intel Confidential        Page 3
  1875.  
  1876.     Instruction Pairing Summary    
  1877.  
  1878.  
  1879.  
  1880.  
  1881.     A-9
  1882.  
  1883.     A-1
  1884.  
  1885.  
  1886.