| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | 
|  | 2 | "http://www.w3.org/TR/html4/strict.dtd"> | 
|  | 3 | <html> | 
|  | 4 | <head> | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 5 | <meta http-equiv="content-type" content="text/html; charset=utf-8"> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 6 | <title>The LLVM Target-Independent Code Generator</title> | 
|  | 7 | <link rel="stylesheet" href="llvm.css" type="text/css"> | 
|  | 8 | </head> | 
|  | 9 | <body> | 
|  | 10 |  | 
|  | 11 | <div class="doc_title"> | 
|  | 12 | The LLVM Target-Independent Code Generator | 
|  | 13 | </div> | 
|  | 14 |  | 
|  | 15 | <ol> | 
|  | 16 | <li><a href="#introduction">Introduction</a> | 
|  | 17 | <ul> | 
|  | 18 | <li><a href="#required">Required components in the code generator</a></li> | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 19 | <li><a href="#high-level-design">The high-level design of the code | 
|  | 20 | generator</a></li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 21 | <li><a href="#tablegen">Using TableGen for target description</a></li> | 
|  | 22 | </ul> | 
|  | 23 | </li> | 
|  | 24 | <li><a href="#targetdesc">Target description classes</a> | 
|  | 25 | <ul> | 
|  | 26 | <li><a href="#targetmachine">The <tt>TargetMachine</tt> class</a></li> | 
|  | 27 | <li><a href="#targetdata">The <tt>TargetData</tt> class</a></li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 28 | <li><a href="#targetlowering">The <tt>TargetLowering</tt> class</a></li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 29 | <li><a href="#mregisterinfo">The <tt>MRegisterInfo</tt> class</a></li> | 
|  | 30 | <li><a href="#targetinstrinfo">The <tt>TargetInstrInfo</tt> class</a></li> | 
|  | 31 | <li><a href="#targetframeinfo">The <tt>TargetFrameInfo</tt> class</a></li> | 
| Chris Lattner | 47adebb | 2005-10-16 17:06:07 +0000 | [diff] [blame] | 32 | <li><a href="#targetsubtarget">The <tt>TargetSubtarget</tt> class</a></li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 33 | <li><a href="#targetjitinfo">The <tt>TargetJITInfo</tt> class</a></li> | 
|  | 34 | </ul> | 
|  | 35 | </li> | 
|  | 36 | <li><a href="#codegendesc">Machine code description classes</a> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 37 | <ul> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 38 | <li><a href="#machineinstr">The <tt>MachineInstr</tt> class</a></li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 39 | <li><a href="#machinebasicblock">The <tt>MachineBasicBlock</tt> | 
|  | 40 | class</a></li> | 
|  | 41 | <li><a href="#machinefunction">The <tt>MachineFunction</tt> class</a></li> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 42 | </ul> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 43 | </li> | 
|  | 44 | <li><a href="#codegenalgs">Target-independent code generation algorithms</a> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 45 | <ul> | 
|  | 46 | <li><a href="#instselect">Instruction Selection</a> | 
|  | 47 | <ul> | 
|  | 48 | <li><a href="#selectiondag_intro">Introduction to SelectionDAGs</a></li> | 
|  | 49 | <li><a href="#selectiondag_process">SelectionDAG Code Generation | 
|  | 50 | Process</a></li> | 
|  | 51 | <li><a href="#selectiondag_build">Initial SelectionDAG | 
|  | 52 | Construction</a></li> | 
|  | 53 | <li><a href="#selectiondag_legalize">SelectionDAG Legalize Phase</a></li> | 
|  | 54 | <li><a href="#selectiondag_optimize">SelectionDAG Optimization | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 55 | Phase: the DAG Combiner</a></li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 56 | <li><a href="#selectiondag_select">SelectionDAG Select Phase</a></li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 57 | <li><a href="#selectiondag_sched">SelectionDAG Scheduling and Formation | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 58 | Phase</a></li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 59 | <li><a href="#selectiondag_future">Future directions for the | 
|  | 60 | SelectionDAG</a></li> | 
|  | 61 | </ul></li> | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 62 | <li><a href="#liveintervals">Live Intervals</a> | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 63 | <ul> | 
|  | 64 | <li><a href="#livevariable_analysis">Live Variable Analysis</a></li> | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 65 | <li><a href="#liveintervals_analysis">Live Intervals Analysis</a></li> | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 66 | </ul></li> | 
| Bill Wendling | a396ee8 | 2006-09-01 21:46:00 +0000 | [diff] [blame] | 67 | <li><a href="#regalloc">Register Allocation</a> | 
|  | 68 | <ul> | 
|  | 69 | <li><a href="#regAlloc_represent">How registers are represented in | 
|  | 70 | LLVM</a></li> | 
|  | 71 | <li><a href="#regAlloc_howTo">Mapping virtual registers to physical | 
|  | 72 | registers</a></li> | 
|  | 73 | <li><a href="#regAlloc_twoAddr">Handling two address instructions</a></li> | 
|  | 74 | <li><a href="#regAlloc_ssaDecon">The SSA deconstruction phase</a></li> | 
|  | 75 | <li><a href="#regAlloc_fold">Instruction folding</a></li> | 
|  | 76 | <li><a href="#regAlloc_builtIn">Built in register allocators</a></li> | 
|  | 77 | </ul></li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 78 | <li><a href="#codeemit">Code Emission</a> | 
|  | 79 | <ul> | 
|  | 80 | <li><a href="#codeemit_asm">Generating Assembly Code</a></li> | 
|  | 81 | <li><a href="#codeemit_bin">Generating Binary Machine Code</a></li> | 
|  | 82 | </ul></li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 83 | </ul> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 84 | </li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 85 | <li><a href="#targetimpls">Target-specific Implementation Notes</a> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 86 | <ul> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 87 | <li><a href="#x86">The X86 backend</a></li> | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 88 | <li><a href="#ppc">The PowerPC backend</a> | 
| Jim Laskey | 762b6cb | 2006-12-14 17:19:50 +0000 | [diff] [blame] | 89 | <ul> | 
|  | 90 | <li><a href="#ppc_abi">LLVM PowerPC ABI</a></li> | 
|  | 91 | <li><a href="#ppc_frame">Frame Layout</a></li> | 
|  | 92 | <li><a href="#ppc_prolog">Prolog/Epilog</a></li> | 
|  | 93 | <li><a href="#ppc_dynamic">Dynamic Allocation</a></li> | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 94 | </ul></li> | 
|  | 95 | </ul></li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 96 |  | 
|  | 97 | </ol> | 
|  | 98 |  | 
|  | 99 | <div class="doc_author"> | 
| Bill Wendling | a396ee8 | 2006-09-01 21:46:00 +0000 | [diff] [blame] | 100 | <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>, | 
|  | 101 | <a href="mailto:isanbard@gmail.com">Bill Wendling</a>, and | 
|  | 102 | <a href="mailto:pronesto@gmail.com">Fernando Magno Quintao | 
|  | 103 | Pereira</a></p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 104 | </div> | 
|  | 105 |  | 
| Chris Lattner | 10d6800 | 2004-06-01 17:18:11 +0000 | [diff] [blame] | 106 | <div class="doc_warning"> | 
|  | 107 | <p>Warning: This is a work in progress.</p> | 
|  | 108 | </div> | 
|  | 109 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 110 | <!-- *********************************************************************** --> | 
|  | 111 | <div class="doc_section"> | 
|  | 112 | <a name="introduction">Introduction</a> | 
|  | 113 | </div> | 
|  | 114 | <!-- *********************************************************************** --> | 
|  | 115 |  | 
|  | 116 | <div class="doc_text"> | 
|  | 117 |  | 
|  | 118 | <p>The LLVM target-independent code generator is a framework that provides a | 
|  | 119 | suite of reusable components for translating the LLVM internal representation to | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 120 | the machine code for a specified target—either in assembly form (suitable | 
|  | 121 | for a static compiler) or in binary machine code format (usable for a JIT | 
|  | 122 | compiler). The LLVM target-independent code generator consists of five main | 
|  | 123 | components:</p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 124 |  | 
|  | 125 | <ol> | 
|  | 126 | <li><a href="#targetdesc">Abstract target description</a> interfaces which | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 127 | capture important properties about various aspects of the machine, independently | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 128 | of how they will be used.  These interfaces are defined in | 
|  | 129 | <tt>include/llvm/Target/</tt>.</li> | 
|  | 130 |  | 
|  | 131 | <li>Classes used to represent the <a href="#codegendesc">machine code</a> being | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 132 | generated for a target.  These classes are intended to be abstract enough to | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 133 | represent the machine code for <i>any</i> target machine.  These classes are | 
|  | 134 | defined in <tt>include/llvm/CodeGen/</tt>.</li> | 
|  | 135 |  | 
|  | 136 | <li><a href="#codegenalgs">Target-independent algorithms</a> used to implement | 
|  | 137 | various phases of native code generation (register allocation, scheduling, stack | 
|  | 138 | frame representation, etc).  This code lives in <tt>lib/CodeGen/</tt>.</li> | 
|  | 139 |  | 
|  | 140 | <li><a href="#targetimpls">Implementations of the abstract target description | 
|  | 141 | interfaces</a> for particular targets.  These machine descriptions make use of | 
|  | 142 | the components provided by LLVM, and can optionally provide custom | 
|  | 143 | target-specific passes, to build complete code generators for a specific target. | 
|  | 144 | Target descriptions live in <tt>lib/Target/</tt>.</li> | 
|  | 145 |  | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 146 | <li><a href="#jit">The target-independent JIT components</a>.  The LLVM JIT is | 
|  | 147 | completely target independent (it uses the <tt>TargetJITInfo</tt> structure to | 
|  | 148 | interface for target-specific issues.  The code for the target-independent | 
|  | 149 | JIT lives in <tt>lib/ExecutionEngine/JIT</tt>.</li> | 
|  | 150 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 151 | </ol> | 
|  | 152 |  | 
|  | 153 | <p> | 
|  | 154 | Depending on which part of the code generator you are interested in working on, | 
|  | 155 | different pieces of this will be useful to you.  In any case, you should be | 
|  | 156 | familiar with the <a href="#targetdesc">target description</a> and <a | 
|  | 157 | href="#codegendesc">machine code representation</a> classes.  If you want to add | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 158 | a backend for a new target, you will need to <a href="#targetimpls">implement the | 
|  | 159 | target description</a> classes for your new target and understand the <a | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 160 | href="LangRef.html">LLVM code representation</a>.  If you are interested in | 
|  | 161 | implementing a new <a href="#codegenalgs">code generation algorithm</a>, it | 
|  | 162 | should only depend on the target-description and machine code representation | 
|  | 163 | classes, ensuring that it is portable. | 
|  | 164 | </p> | 
|  | 165 |  | 
|  | 166 | </div> | 
|  | 167 |  | 
|  | 168 | <!-- ======================================================================= --> | 
|  | 169 | <div class="doc_subsection"> | 
|  | 170 | <a name="required">Required components in the code generator</a> | 
|  | 171 | </div> | 
|  | 172 |  | 
|  | 173 | <div class="doc_text"> | 
|  | 174 |  | 
|  | 175 | <p>The two pieces of the LLVM code generator are the high-level interface to the | 
|  | 176 | code generator and the set of reusable components that can be used to build | 
|  | 177 | target-specific backends.  The two most important interfaces (<a | 
|  | 178 | href="#targetmachine"><tt>TargetMachine</tt></a> and <a | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 179 | href="#targetdata"><tt>TargetData</tt></a>) are the only ones that are | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 180 | required to be defined for a backend to fit into the LLVM system, but the others | 
|  | 181 | must be defined if the reusable code generator components are going to be | 
|  | 182 | used.</p> | 
|  | 183 |  | 
|  | 184 | <p>This design has two important implications.  The first is that LLVM can | 
|  | 185 | support completely non-traditional code generation targets.  For example, the C | 
|  | 186 | backend does not require register allocation, instruction selection, or any of | 
|  | 187 | the other standard components provided by the system.  As such, it only | 
|  | 188 | implements these two interfaces, and does its own thing.  Another example of a | 
|  | 189 | code generator like this is a (purely hypothetical) backend that converts LLVM | 
|  | 190 | to the GCC RTL form and uses GCC to emit machine code for a target.</p> | 
|  | 191 |  | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 192 | <p>This design also implies that it is possible to design and | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 193 | implement radically different code generators in the LLVM system that do not | 
|  | 194 | make use of any of the built-in components.  Doing so is not recommended at all, | 
|  | 195 | but could be required for radically different targets that do not fit into the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 196 | LLVM machine description model: FPGAs for example.</p> | 
| Chris Lattner | 900bf8c | 2004-06-02 07:06:06 +0000 | [diff] [blame] | 197 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 198 | </div> | 
|  | 199 |  | 
|  | 200 | <!-- ======================================================================= --> | 
|  | 201 | <div class="doc_subsection"> | 
| Chris Lattner | 10d6800 | 2004-06-01 17:18:11 +0000 | [diff] [blame] | 202 | <a name="high-level-design">The high-level design of the code generator</a> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 203 | </div> | 
|  | 204 |  | 
|  | 205 | <div class="doc_text"> | 
|  | 206 |  | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 207 | <p>The LLVM target-independent code generator is designed to support efficient and | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 208 | quality code generation for standard register-based microprocessors.  Code | 
|  | 209 | generation in this model is divided into the following stages:</p> | 
|  | 210 |  | 
|  | 211 | <ol> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 212 | <li><b><a href="#instselect">Instruction Selection</a></b> - This phase | 
|  | 213 | determines an efficient way to express the input LLVM code in the target | 
|  | 214 | instruction set. | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 215 | This stage produces the initial code for the program in the target instruction | 
|  | 216 | set, then makes use of virtual registers in SSA form and physical registers that | 
|  | 217 | represent any required register assignments due to target constraints or calling | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 218 | conventions.  This step turns the LLVM code into a DAG of target | 
|  | 219 | instructions.</li> | 
|  | 220 |  | 
|  | 221 | <li><b><a href="#selectiondag_sched">Scheduling and Formation</a></b> - This | 
|  | 222 | phase takes the DAG of target instructions produced by the instruction selection | 
|  | 223 | phase, determines an ordering of the instructions, then emits the instructions | 
| Chris Lattner | c38959f | 2005-10-17 03:09:31 +0000 | [diff] [blame] | 224 | as <tt><a href="#machineinstr">MachineInstr</a></tt>s with that ordering.  Note | 
|  | 225 | that we describe this in the <a href="#instselect">instruction selection | 
|  | 226 | section</a> because it operates on a <a | 
|  | 227 | href="#selectiondag_intro">SelectionDAG</a>. | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 228 | </li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 229 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 230 | <li><b><a href="#ssamco">SSA-based Machine Code Optimizations</a></b> - This | 
|  | 231 | optional stage consists of a series of machine-code optimizations that | 
|  | 232 | operate on the SSA-form produced by the instruction selector.  Optimizations | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 233 | like modulo-scheduling or peephole optimization work here. | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 234 | </li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 235 |  | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 236 | <li><b><a href="#regalloc">Register Allocation</a></b> - The | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 237 | target code is transformed from an infinite virtual register file in SSA form | 
|  | 238 | to the concrete register file used by the target.  This phase introduces spill | 
|  | 239 | code and eliminates all virtual register references from the program.</li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 240 |  | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 241 | <li><b><a href="#proepicode">Prolog/Epilog Code Insertion</a></b> - Once the | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 242 | machine code has been generated for the function and the amount of stack space | 
|  | 243 | required is known (used for LLVM alloca's and spill slots), the prolog and | 
|  | 244 | epilog code for the function can be inserted and "abstract stack location | 
|  | 245 | references" can be eliminated.  This stage is responsible for implementing | 
|  | 246 | optimizations like frame-pointer elimination and stack packing.</li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 247 |  | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 248 | <li><b><a href="#latemco">Late Machine Code Optimizations</a></b> - Optimizations | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 249 | that operate on "final" machine code can go here, such as spill code scheduling | 
|  | 250 | and peephole optimizations.</li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 251 |  | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 252 | <li><b><a href="#codeemit">Code Emission</a></b> - The final stage actually | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 253 | puts out the code for the current function, either in the target assembler | 
|  | 254 | format or in machine code.</li> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 255 |  | 
|  | 256 | </ol> | 
|  | 257 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 258 | <p>The code generator is based on the assumption that the instruction selector | 
|  | 259 | will use an optimal pattern matching selector to create high-quality sequences of | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 260 | native instructions.  Alternative code generator designs based on pattern | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 261 | expansion and aggressive iterative peephole optimization are much slower.  This | 
|  | 262 | design permits efficient compilation (important for JIT environments) and | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 263 | aggressive optimization (used when generating code offline) by allowing | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 264 | components of varying levels of sophistication to be used for any step of | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 265 | compilation.</p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 266 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 267 | <p>In addition to these stages, target implementations can insert arbitrary | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 268 | target-specific passes into the flow.  For example, the X86 target uses a | 
|  | 269 | special pass to handle the 80x87 floating point stack architecture.  Other | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 270 | targets with unusual requirements can be supported with custom passes as | 
|  | 271 | needed.</p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 272 |  | 
|  | 273 | </div> | 
|  | 274 |  | 
|  | 275 |  | 
|  | 276 | <!-- ======================================================================= --> | 
|  | 277 | <div class="doc_subsection"> | 
| Chris Lattner | 10d6800 | 2004-06-01 17:18:11 +0000 | [diff] [blame] | 278 | <a name="tablegen">Using TableGen for target description</a> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 279 | </div> | 
|  | 280 |  | 
|  | 281 | <div class="doc_text"> | 
|  | 282 |  | 
| Chris Lattner | 5489e93 | 2004-06-01 18:35:00 +0000 | [diff] [blame] | 283 | <p>The target description classes require a detailed description of the target | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 284 | architecture.  These target descriptions often have a large amount of common | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 285 | information (e.g., an <tt>add</tt> instruction is almost identical to a | 
|  | 286 | <tt>sub</tt> instruction). | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 287 | In order to allow the maximum amount of commonality to be factored out, the LLVM | 
|  | 288 | code generator uses the <a href="TableGenFundamentals.html">TableGen</a> tool to | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 289 | describe big chunks of the target machine, which allows the use of | 
|  | 290 | domain-specific and target-specific abstractions to reduce the amount of | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 291 | repetition.</p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 292 |  | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 293 | <p>As LLVM continues to be developed and refined, we plan to move more and more | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 294 | of the target description to the <tt>.td</tt> form.  Doing so gives us a | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 295 | number of advantages.  The most important is that it makes it easier to port | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 296 | LLVM because it reduces the amount of C++ code that has to be written, and the | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 297 | surface area of the code generator that needs to be understood before someone | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 298 | can get something working.  Second, it makes it easier to change things. In | 
|  | 299 | particular, if tables and other things are all emitted by <tt>tblgen</tt>, we | 
|  | 300 | only need a change in one place (<tt>tblgen</tt>) to update all of the targets | 
|  | 301 | to a new interface.</p> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 302 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 303 | </div> | 
|  | 304 |  | 
|  | 305 | <!-- *********************************************************************** --> | 
|  | 306 | <div class="doc_section"> | 
|  | 307 | <a name="targetdesc">Target description classes</a> | 
|  | 308 | </div> | 
|  | 309 | <!-- *********************************************************************** --> | 
|  | 310 |  | 
|  | 311 | <div class="doc_text"> | 
|  | 312 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 313 | <p>The LLVM target description classes (located in the | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 314 | <tt>include/llvm/Target</tt> directory) provide an abstract description of the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 315 | target machine independent of any particular client.  These classes are | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 316 | designed to capture the <i>abstract</i> properties of the target (such as the | 
|  | 317 | instructions and registers it has), and do not incorporate any particular pieces | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 318 | of code generation algorithms.</p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 319 |  | 
|  | 320 | <p>All of the target description classes (except the <tt><a | 
|  | 321 | href="#targetdata">TargetData</a></tt> class) are designed to be subclassed by | 
|  | 322 | the concrete target implementation, and have virtual methods implemented.  To | 
| Reid Spencer | bdbcb8a | 2004-06-05 14:39:24 +0000 | [diff] [blame] | 323 | get to these implementations, the <tt><a | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 324 | href="#targetmachine">TargetMachine</a></tt> class provides accessors that | 
|  | 325 | should be implemented by the target.</p> | 
|  | 326 |  | 
|  | 327 | </div> | 
|  | 328 |  | 
|  | 329 | <!-- ======================================================================= --> | 
|  | 330 | <div class="doc_subsection"> | 
|  | 331 | <a name="targetmachine">The <tt>TargetMachine</tt> class</a> | 
|  | 332 | </div> | 
|  | 333 |  | 
|  | 334 | <div class="doc_text"> | 
|  | 335 |  | 
|  | 336 | <p>The <tt>TargetMachine</tt> class provides virtual methods that are used to | 
|  | 337 | access the target-specific implementations of the various target description | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 338 | classes via the <tt>get*Info</tt> methods (<tt>getInstrInfo</tt>, | 
|  | 339 | <tt>getRegisterInfo</tt>, <tt>getFrameInfo</tt>, etc.).  This class is | 
|  | 340 | designed to be specialized by | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 341 | a concrete target implementation (e.g., <tt>X86TargetMachine</tt>) which | 
|  | 342 | implements the various virtual methods.  The only required target description | 
|  | 343 | class is the <a href="#targetdata"><tt>TargetData</tt></a> class, but if the | 
|  | 344 | code generator components are to be used, the other interfaces should be | 
|  | 345 | implemented as well.</p> | 
|  | 346 |  | 
|  | 347 | </div> | 
|  | 348 |  | 
|  | 349 |  | 
|  | 350 | <!-- ======================================================================= --> | 
|  | 351 | <div class="doc_subsection"> | 
|  | 352 | <a name="targetdata">The <tt>TargetData</tt> class</a> | 
|  | 353 | </div> | 
|  | 354 |  | 
|  | 355 | <div class="doc_text"> | 
|  | 356 |  | 
|  | 357 | <p>The <tt>TargetData</tt> class is the only required target description class, | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 358 | and it is the only class that is not extensible (you cannot derived  a new | 
|  | 359 | class from it).  <tt>TargetData</tt> specifies information about how the target | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 360 | lays out memory for structures, the alignment requirements for various data | 
|  | 361 | types, the size of pointers in the target, and whether the target is | 
|  | 362 | little-endian or big-endian.</p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 363 |  | 
|  | 364 | </div> | 
|  | 365 |  | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 366 | <!-- ======================================================================= --> | 
|  | 367 | <div class="doc_subsection"> | 
|  | 368 | <a name="targetlowering">The <tt>TargetLowering</tt> class</a> | 
|  | 369 | </div> | 
|  | 370 |  | 
|  | 371 | <div class="doc_text"> | 
|  | 372 |  | 
|  | 373 | <p>The <tt>TargetLowering</tt> class is used by SelectionDAG based instruction | 
|  | 374 | selectors primarily to describe how LLVM code should be lowered to SelectionDAG | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 375 | operations.  Among other things, this class indicates:</p> | 
|  | 376 |  | 
|  | 377 | <ul> | 
|  | 378 | <li>an initial register class to use for various <tt>ValueType</tt>s</li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 379 | <li>which operations are natively supported by the target machine</li> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 380 | <li>the return type of <tt>setcc</tt> operations</li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 381 | <li>the type to use for shift amounts</li> | 
|  | 382 | <li>various high-level characteristics, like whether it is profitable to turn | 
|  | 383 | division by a constant into a multiplication sequence</li> | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 384 | </ul> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 385 |  | 
|  | 386 | </div> | 
|  | 387 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 388 | <!-- ======================================================================= --> | 
|  | 389 | <div class="doc_subsection"> | 
| Chris Lattner | 10d6800 | 2004-06-01 17:18:11 +0000 | [diff] [blame] | 390 | <a name="mregisterinfo">The <tt>MRegisterInfo</tt> class</a> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 391 | </div> | 
|  | 392 |  | 
|  | 393 | <div class="doc_text"> | 
|  | 394 |  | 
|  | 395 | <p>The <tt>MRegisterInfo</tt> class (which will eventually be renamed to | 
|  | 396 | <tt>TargetRegisterInfo</tt>) is used to describe the register file of the | 
|  | 397 | target and any interactions between the registers.</p> | 
|  | 398 |  | 
|  | 399 | <p>Registers in the code generator are represented in the code generator by | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 400 | unsigned integers.  Physical registers (those that actually exist in the target | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 401 | description) are unique small numbers, and virtual registers are generally | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 402 | large.  Note that register #0 is reserved as a flag value.</p> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 403 |  | 
|  | 404 | <p>Each register in the processor description has an associated | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 405 | <tt>TargetRegisterDesc</tt> entry, which provides a textual name for the | 
|  | 406 | register (used for assembly output and debugging dumps) and a set of aliases | 
|  | 407 | (used to indicate whether one register overlaps with another). | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 408 | </p> | 
|  | 409 |  | 
|  | 410 | <p>In addition to the per-register description, the <tt>MRegisterInfo</tt> class | 
|  | 411 | exposes a set of processor specific register classes (instances of the | 
|  | 412 | <tt>TargetRegisterClass</tt> class).  Each register class contains sets of | 
|  | 413 | registers that have the same properties (for example, they are all 32-bit | 
|  | 414 | integer registers).  Each SSA virtual register created by the instruction | 
|  | 415 | selector has an associated register class.  When the register allocator runs, it | 
|  | 416 | replaces virtual registers with a physical register in the set.</p> | 
|  | 417 |  | 
|  | 418 | <p> | 
|  | 419 | The target-specific implementations of these classes is auto-generated from a <a | 
|  | 420 | href="TableGenFundamentals.html">TableGen</a> description of the register file. | 
|  | 421 | </p> | 
|  | 422 |  | 
|  | 423 | </div> | 
|  | 424 |  | 
|  | 425 | <!-- ======================================================================= --> | 
|  | 426 | <div class="doc_subsection"> | 
| Chris Lattner | 10d6800 | 2004-06-01 17:18:11 +0000 | [diff] [blame] | 427 | <a name="targetinstrinfo">The <tt>TargetInstrInfo</tt> class</a> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 428 | </div> | 
|  | 429 |  | 
| Reid Spencer | 627cd00 | 2005-07-19 01:36:35 +0000 | [diff] [blame] | 430 | <div class="doc_text"> | 
|  | 431 | <p>The <tt>TargetInstrInfo</tt> class is used to describe the machine | 
|  | 432 | instructions supported by the target. It is essentially an array of | 
|  | 433 | <tt>TargetInstrDescriptor</tt> objects, each of which describes one | 
|  | 434 | instruction the target supports. Descriptors define things like the mnemonic | 
| Chris Lattner | a307978 | 2005-07-19 03:37:48 +0000 | [diff] [blame] | 435 | for the opcode, the number of operands, the list of implicit register uses | 
|  | 436 | and defs, whether the instruction has certain target-independent properties | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 437 | (accesses memory, is commutable, etc), and holds any target-specific | 
|  | 438 | flags.</p> | 
| Reid Spencer | 627cd00 | 2005-07-19 01:36:35 +0000 | [diff] [blame] | 439 | </div> | 
|  | 440 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 441 | <!-- ======================================================================= --> | 
|  | 442 | <div class="doc_subsection"> | 
| Chris Lattner | 10d6800 | 2004-06-01 17:18:11 +0000 | [diff] [blame] | 443 | <a name="targetframeinfo">The <tt>TargetFrameInfo</tt> class</a> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 444 | </div> | 
|  | 445 |  | 
| Reid Spencer | 627cd00 | 2005-07-19 01:36:35 +0000 | [diff] [blame] | 446 | <div class="doc_text"> | 
|  | 447 | <p>The <tt>TargetFrameInfo</tt> class is used to provide information about the | 
|  | 448 | stack frame layout of the target. It holds the direction of stack growth, | 
|  | 449 | the known stack alignment on entry to each function, and the offset to the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 450 | local area.  The offset to the local area is the offset from the stack | 
| Reid Spencer | 627cd00 | 2005-07-19 01:36:35 +0000 | [diff] [blame] | 451 | pointer on function entry to the first location where function data (local | 
|  | 452 | variables, spill locations) can be stored.</p> | 
| Reid Spencer | 627cd00 | 2005-07-19 01:36:35 +0000 | [diff] [blame] | 453 | </div> | 
| Chris Lattner | 47adebb | 2005-10-16 17:06:07 +0000 | [diff] [blame] | 454 |  | 
|  | 455 | <!-- ======================================================================= --> | 
|  | 456 | <div class="doc_subsection"> | 
|  | 457 | <a name="targetsubtarget">The <tt>TargetSubtarget</tt> class</a> | 
|  | 458 | </div> | 
|  | 459 |  | 
|  | 460 | <div class="doc_text"> | 
| Jim Laskey | 82d61a1 | 2005-10-17 12:19:10 +0000 | [diff] [blame] | 461 | <p>The <tt>TargetSubtarget</tt> class is used to provide information about the | 
|  | 462 | specific chip set being targeted.  A sub-target informs code generation of | 
|  | 463 | which instructions are supported, instruction latencies and instruction | 
|  | 464 | execution itinerary; i.e., which processing units are used, in what order, and | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 465 | for how long.</p> | 
| Chris Lattner | 47adebb | 2005-10-16 17:06:07 +0000 | [diff] [blame] | 466 | </div> | 
|  | 467 |  | 
|  | 468 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 469 | <!-- ======================================================================= --> | 
|  | 470 | <div class="doc_subsection"> | 
| Chris Lattner | 10d6800 | 2004-06-01 17:18:11 +0000 | [diff] [blame] | 471 | <a name="targetjitinfo">The <tt>TargetJITInfo</tt> class</a> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 472 | </div> | 
|  | 473 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 474 | <div class="doc_text"> | 
|  | 475 | <p>The <tt>TargetJITInfo</tt> class exposes an abstract interface used by the | 
|  | 476 | Just-In-Time code generator to perform target-specific activities, such as | 
|  | 477 | emitting stubs.  If a <tt>TargetMachine</tt> supports JIT code generation, it | 
|  | 478 | should provide one of these objects through the <tt>getJITInfo</tt> | 
|  | 479 | method.</p> | 
|  | 480 | </div> | 
|  | 481 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 482 | <!-- *********************************************************************** --> | 
|  | 483 | <div class="doc_section"> | 
|  | 484 | <a name="codegendesc">Machine code description classes</a> | 
|  | 485 | </div> | 
|  | 486 | <!-- *********************************************************************** --> | 
|  | 487 |  | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 488 | <div class="doc_text"> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 489 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 490 | <p>At the high-level, LLVM code is translated to a machine specific | 
|  | 491 | representation formed out of | 
|  | 492 | <a href="#machinefunction"><tt>MachineFunction</tt></a>, | 
|  | 493 | <a href="#machinebasicblock"><tt>MachineBasicBlock</tt></a>, and <a | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 494 | href="#machineinstr"><tt>MachineInstr</tt></a> instances | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 495 | (defined in <tt>include/llvm/CodeGen</tt>).  This representation is completely | 
|  | 496 | target agnostic, representing instructions in their most abstract form: an | 
|  | 497 | opcode and a series of operands.  This representation is designed to support | 
|  | 498 | both an SSA representation for machine code, as well as a register allocated, | 
|  | 499 | non-SSA form.</p> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 500 |  | 
|  | 501 | </div> | 
|  | 502 |  | 
|  | 503 | <!-- ======================================================================= --> | 
|  | 504 | <div class="doc_subsection"> | 
|  | 505 | <a name="machineinstr">The <tt>MachineInstr</tt> class</a> | 
|  | 506 | </div> | 
|  | 507 |  | 
|  | 508 | <div class="doc_text"> | 
|  | 509 |  | 
|  | 510 | <p>Target machine instructions are represented as instances of the | 
|  | 511 | <tt>MachineInstr</tt> class.  This class is an extremely abstract way of | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 512 | representing machine instructions.  In particular, it only keeps track of | 
|  | 513 | an opcode number and a set of operands.</p> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 514 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 515 | <p>The opcode number is a simple unsigned integer that only has meaning to a | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 516 | specific backend.  All of the instructions for a target should be defined in | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 517 | the <tt>*InstrInfo.td</tt> file for the target. The opcode enum values | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 518 | are auto-generated from this description.  The <tt>MachineInstr</tt> class does | 
|  | 519 | not have any information about how to interpret the instruction (i.e., what the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 520 | semantics of the instruction are); for that you must refer to the | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 521 | <tt><a href="#targetinstrinfo">TargetInstrInfo</a></tt> class.</p> | 
|  | 522 |  | 
|  | 523 | <p>The operands of a machine instruction can be of several different types: | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 524 | a register reference, a constant integer, a basic block reference, etc.  In | 
|  | 525 | addition, a machine operand should be marked as a def or a use of the value | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 526 | (though only registers are allowed to be defs).</p> | 
|  | 527 |  | 
|  | 528 | <p>By convention, the LLVM code generator orders instruction operands so that | 
|  | 529 | all register definitions come before the register uses, even on architectures | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 530 | that are normally printed in other orders.  For example, the SPARC add | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 531 | instruction: "<tt>add %i1, %i2, %i3</tt>" adds the "%i1", and "%i2" registers | 
|  | 532 | and stores the result into the "%i3" register.  In the LLVM code generator, | 
|  | 533 | the operands should be stored as "<tt>%i3, %i1, %i2</tt>": with the destination | 
|  | 534 | first.</p> | 
|  | 535 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 536 | <p>Keeping destination (definition) operands at the beginning of the operand | 
|  | 537 | list has several advantages.  In particular, the debugging printer will print | 
|  | 538 | the instruction like this:</p> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 539 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 540 | <div class="doc_code"> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 541 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 542 | %r3 = add %i1, %i2 | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 543 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 544 | </div> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 545 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 546 | <p>Also if the first operand is a def, it is easier to <a | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 547 | href="#buildmi">create instructions</a> whose only def is the first | 
|  | 548 | operand.</p> | 
|  | 549 |  | 
|  | 550 | </div> | 
|  | 551 |  | 
|  | 552 | <!-- _______________________________________________________________________ --> | 
|  | 553 | <div class="doc_subsubsection"> | 
|  | 554 | <a name="buildmi">Using the <tt>MachineInstrBuilder.h</tt> functions</a> | 
|  | 555 | </div> | 
|  | 556 |  | 
|  | 557 | <div class="doc_text"> | 
|  | 558 |  | 
|  | 559 | <p>Machine instructions are created by using the <tt>BuildMI</tt> functions, | 
|  | 560 | located in the <tt>include/llvm/CodeGen/MachineInstrBuilder.h</tt> file.  The | 
|  | 561 | <tt>BuildMI</tt> functions make it easy to build arbitrary machine | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 562 | instructions.  Usage of the <tt>BuildMI</tt> functions look like this:</p> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 563 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 564 | <div class="doc_code"> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 565 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 566 | // Create a 'DestReg = mov 42' (rendered in X86 assembly as 'mov DestReg, 42') | 
|  | 567 | // instruction.  The '1' specifies how many operands will be added. | 
|  | 568 | MachineInstr *MI = BuildMI(X86::MOV32ri, 1, DestReg).addImm(42); | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 569 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 570 | // Create the same instr, but insert it at the end of a basic block. | 
|  | 571 | MachineBasicBlock &MBB = ... | 
|  | 572 | BuildMI(MBB, X86::MOV32ri, 1, DestReg).addImm(42); | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 573 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 574 | // Create the same instr, but insert it before a specified iterator point. | 
|  | 575 | MachineBasicBlock::iterator MBBI = ... | 
|  | 576 | BuildMI(MBB, MBBI, X86::MOV32ri, 1, DestReg).addImm(42); | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 577 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 578 | // Create a 'cmp Reg, 0' instruction, no destination reg. | 
|  | 579 | MI = BuildMI(X86::CMP32ri, 2).addReg(Reg).addImm(0); | 
|  | 580 | // Create an 'sahf' instruction which takes no operands and stores nothing. | 
|  | 581 | MI = BuildMI(X86::SAHF, 0); | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 582 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 583 | // Create a self looping branch instruction. | 
|  | 584 | BuildMI(MBB, X86::JNE, 1).addMBB(&MBB); | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 585 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 586 | </div> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 587 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 588 | <p>The key thing to remember with the <tt>BuildMI</tt> functions is that you | 
|  | 589 | have to specify the number of operands that the machine instruction will take. | 
|  | 590 | This allows for efficient memory allocation.  You also need to specify if | 
|  | 591 | operands default to be uses of values, not definitions.  If you need to add a | 
|  | 592 | definition operand (other than the optional destination register), you must | 
|  | 593 | explicitly mark it as such:</p> | 
|  | 594 |  | 
|  | 595 | <div class="doc_code"> | 
|  | 596 | <pre> | 
|  | 597 | MI.addReg(Reg, MachineOperand::Def); | 
|  | 598 | </pre> | 
|  | 599 | </div> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 600 |  | 
|  | 601 | </div> | 
|  | 602 |  | 
|  | 603 | <!-- _______________________________________________________________________ --> | 
|  | 604 | <div class="doc_subsubsection"> | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 605 | <a name="fixedregs">Fixed (preassigned) registers</a> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 606 | </div> | 
|  | 607 |  | 
|  | 608 | <div class="doc_text"> | 
|  | 609 |  | 
|  | 610 | <p>One important issue that the code generator needs to be aware of is the | 
|  | 611 | presence of fixed registers.  In particular, there are often places in the | 
|  | 612 | instruction stream where the register allocator <em>must</em> arrange for a | 
|  | 613 | particular value to be in a particular register.  This can occur due to | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 614 | limitations of the instruction set (e.g., the X86 can only do a 32-bit divide | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 615 | with the <tt>EAX</tt>/<tt>EDX</tt> registers), or external factors like calling | 
|  | 616 | conventions.  In any case, the instruction selector should emit code that | 
|  | 617 | copies a virtual register into or out of a physical register when needed.</p> | 
|  | 618 |  | 
|  | 619 | <p>For example, consider this simple LLVM example:</p> | 
|  | 620 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 621 | <div class="doc_code"> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 622 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 623 | int %test(int %X, int %Y) { | 
|  | 624 | %Z = div int %X, %Y | 
|  | 625 | ret int %Z | 
|  | 626 | } | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 627 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 628 | </div> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 629 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 630 | <p>The X86 instruction selector produces this machine code for the <tt>div</tt> | 
|  | 631 | and <tt>ret</tt> (use | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 632 | "<tt>llc X.bc -march=x86 -print-machineinstrs</tt>" to get this):</p> | 
|  | 633 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 634 | <div class="doc_code"> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 635 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 636 | ;; Start of div | 
|  | 637 | %EAX = mov %reg1024           ;; Copy X (in reg1024) into EAX | 
|  | 638 | %reg1027 = sar %reg1024, 31 | 
|  | 639 | %EDX = mov %reg1027           ;; Sign extend X into EDX | 
|  | 640 | idiv %reg1025                 ;; Divide by Y (in reg1025) | 
|  | 641 | %reg1026 = mov %EAX           ;; Read the result (Z) out of EAX | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 642 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 643 | ;; Start of ret | 
|  | 644 | %EAX = mov %reg1026           ;; 32-bit return value goes in EAX | 
|  | 645 | ret | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 646 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 647 | </div> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 648 |  | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 649 | <p>By the end of code generation, the register allocator has coalesced | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 650 | the registers and deleted the resultant identity moves producing the | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 651 | following code:</p> | 
|  | 652 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 653 | <div class="doc_code"> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 654 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 655 | ;; X is in EAX, Y is in ECX | 
|  | 656 | mov %EAX, %EDX | 
|  | 657 | sar %EDX, 31 | 
|  | 658 | idiv %ECX | 
|  | 659 | ret | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 660 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 661 | </div> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 662 |  | 
|  | 663 | <p>This approach is extremely general (if it can handle the X86 architecture, | 
|  | 664 | it can handle anything!) and allows all of the target specific | 
|  | 665 | knowledge about the instruction stream to be isolated in the instruction | 
|  | 666 | selector.  Note that physical registers should have a short lifetime for good | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 667 | code generation, and all physical registers are assumed dead on entry to and | 
|  | 668 | exit from basic blocks (before register allocation).  Thus, if you need a value | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 669 | to be live across basic block boundaries, it <em>must</em> live in a virtual | 
|  | 670 | register.</p> | 
|  | 671 |  | 
|  | 672 | </div> | 
|  | 673 |  | 
|  | 674 | <!-- _______________________________________________________________________ --> | 
|  | 675 | <div class="doc_subsubsection"> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 676 | <a name="ssa">Machine code in SSA form</a> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 677 | </div> | 
|  | 678 |  | 
|  | 679 | <div class="doc_text"> | 
|  | 680 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 681 | <p><tt>MachineInstr</tt>'s are initially selected in SSA-form, and | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 682 | are maintained in SSA-form until register allocation happens.  For the most | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 683 | part, this is trivially simple since LLVM is already in SSA form; LLVM PHI nodes | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 684 | become machine code PHI nodes, and virtual registers are only allowed to have a | 
|  | 685 | single definition.</p> | 
|  | 686 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 687 | <p>After register allocation, machine code is no longer in SSA-form because there | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 688 | are no virtual registers left in the code.</p> | 
|  | 689 |  | 
|  | 690 | </div> | 
|  | 691 |  | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 692 | <!-- ======================================================================= --> | 
|  | 693 | <div class="doc_subsection"> | 
|  | 694 | <a name="machinebasicblock">The <tt>MachineBasicBlock</tt> class</a> | 
|  | 695 | </div> | 
|  | 696 |  | 
|  | 697 | <div class="doc_text"> | 
|  | 698 |  | 
|  | 699 | <p>The <tt>MachineBasicBlock</tt> class contains a list of machine instructions | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 700 | (<tt><a href="#machineinstr">MachineInstr</a></tt> instances).  It roughly | 
|  | 701 | corresponds to the LLVM code input to the instruction selector, but there can be | 
|  | 702 | a one-to-many mapping (i.e. one LLVM basic block can map to multiple machine | 
|  | 703 | basic blocks). The <tt>MachineBasicBlock</tt> class has a | 
|  | 704 | "<tt>getBasicBlock</tt>" method, which returns the LLVM basic block that it | 
|  | 705 | comes from.</p> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 706 |  | 
|  | 707 | </div> | 
|  | 708 |  | 
|  | 709 | <!-- ======================================================================= --> | 
|  | 710 | <div class="doc_subsection"> | 
|  | 711 | <a name="machinefunction">The <tt>MachineFunction</tt> class</a> | 
|  | 712 | </div> | 
|  | 713 |  | 
|  | 714 | <div class="doc_text"> | 
|  | 715 |  | 
|  | 716 | <p>The <tt>MachineFunction</tt> class contains a list of machine basic blocks | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 717 | (<tt><a href="#machinebasicblock">MachineBasicBlock</a></tt> instances).  It | 
|  | 718 | corresponds one-to-one with the LLVM function input to the instruction selector. | 
|  | 719 | In addition to a list of basic blocks, the <tt>MachineFunction</tt> contains a | 
|  | 720 | a <tt>MachineConstantPool</tt>, a <tt>MachineFrameInfo</tt>, a | 
|  | 721 | <tt>MachineFunctionInfo</tt>, a <tt>SSARegMap</tt>, and a set of live in and | 
|  | 722 | live out registers for the function.  See | 
|  | 723 | <tt>include/llvm/CodeGen/MachineFunction.h</tt> for more information.</p> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 724 |  | 
|  | 725 | </div> | 
|  | 726 |  | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 727 | <!-- *********************************************************************** --> | 
|  | 728 | <div class="doc_section"> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 729 | <a name="codegenalgs">Target-independent code generation algorithms</a> | 
|  | 730 | </div> | 
|  | 731 | <!-- *********************************************************************** --> | 
|  | 732 |  | 
|  | 733 | <div class="doc_text"> | 
|  | 734 |  | 
|  | 735 | <p>This section documents the phases described in the <a | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 736 | href="#high-level-design">high-level design of the code generator</a>.  It | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 737 | explains how they work and some of the rationale behind their design.</p> | 
|  | 738 |  | 
|  | 739 | </div> | 
|  | 740 |  | 
|  | 741 | <!-- ======================================================================= --> | 
|  | 742 | <div class="doc_subsection"> | 
|  | 743 | <a name="instselect">Instruction Selection</a> | 
|  | 744 | </div> | 
|  | 745 |  | 
|  | 746 | <div class="doc_text"> | 
|  | 747 | <p> | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 748 | Instruction Selection is the process of translating LLVM code presented to the | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 749 | code generator into target-specific machine instructions.  There are several | 
|  | 750 | well-known ways to do this in the literature.  In LLVM there are two main forms: | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 751 | the SelectionDAG based instruction selector framework and an old-style 'simple' | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 752 | instruction selector, which effectively peephole selects each LLVM instruction | 
|  | 753 | into a series of machine instructions.  We recommend that all targets use the | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 754 | SelectionDAG infrastructure. | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 755 | </p> | 
|  | 756 |  | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 757 | <p>Portions of the DAG instruction selector are generated from the target | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 758 | description (<tt>*.td</tt>) files.  Our goal is for the entire instruction | 
|  | 759 | selector to be generated from these <tt>.td</tt> files.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 760 | </div> | 
|  | 761 |  | 
|  | 762 | <!-- _______________________________________________________________________ --> | 
|  | 763 | <div class="doc_subsubsection"> | 
|  | 764 | <a name="selectiondag_intro">Introduction to SelectionDAGs</a> | 
|  | 765 | </div> | 
|  | 766 |  | 
|  | 767 | <div class="doc_text"> | 
|  | 768 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 769 | <p>The SelectionDAG provides an abstraction for code representation in a way | 
|  | 770 | that is amenable to instruction selection using automatic techniques | 
|  | 771 | (e.g. dynamic-programming based optimal pattern matching selectors). It is also | 
|  | 772 | well-suited to other phases of code generation; in particular, | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 773 | instruction scheduling (SelectionDAG's are very close to scheduling DAGs | 
|  | 774 | post-selection).  Additionally, the SelectionDAG provides a host representation | 
|  | 775 | where a large variety of very-low-level (but target-independent) | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 776 | <a href="#selectiondag_optimize">optimizations</a> may be | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 777 | performed; ones which require extensive information about the instructions | 
|  | 778 | efficiently supported by the target.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 779 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 780 | <p>The SelectionDAG is a Directed-Acyclic-Graph whose nodes are instances of the | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 781 | <tt>SDNode</tt> class.  The primary payload of the <tt>SDNode</tt> is its | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 782 | operation code (Opcode) that indicates what operation the node performs and | 
|  | 783 | the operands to the operation. | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 784 | The various operation node types are described at the top of the | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 785 | <tt>include/llvm/CodeGen/SelectionDAGNodes.h</tt> file.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 786 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 787 | <p>Although most operations define a single value, each node in the graph may | 
|  | 788 | define multiple values.  For example, a combined div/rem operation will define | 
|  | 789 | both the dividend and the remainder. Many other situations require multiple | 
|  | 790 | values as well.  Each node also has some number of operands, which are edges | 
|  | 791 | to the node defining the used value.  Because nodes may define multiple values, | 
|  | 792 | edges are represented by instances of the <tt>SDOperand</tt> class, which is | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 793 | a <tt><SDNode, unsigned></tt> pair, indicating the node and result | 
|  | 794 | value being used, respectively.  Each value produced by an <tt>SDNode</tt> has | 
|  | 795 | an associated <tt>MVT::ValueType</tt> indicating what type the value is.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 796 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 797 | <p>SelectionDAGs contain two different kinds of values: those that represent | 
|  | 798 | data flow and those that represent control flow dependencies.  Data values are | 
|  | 799 | simple edges with an integer or floating point value type.  Control edges are | 
|  | 800 | represented as "chain" edges which are of type <tt>MVT::Other</tt>.  These edges | 
|  | 801 | provide an ordering between nodes that have side effects (such as | 
|  | 802 | loads, stores, calls, returns, etc).  All nodes that have side effects should | 
|  | 803 | take a token chain as input and produce a new one as output.  By convention, | 
|  | 804 | token chain inputs are always operand #0, and chain results are always the last | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 805 | value produced by an operation.</p> | 
|  | 806 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 807 | <p>A SelectionDAG has designated "Entry" and "Root" nodes.  The Entry node is | 
|  | 808 | always a marker node with an Opcode of <tt>ISD::EntryToken</tt>.  The Root node | 
|  | 809 | is the final side-effecting node in the token chain. For example, in a single | 
|  | 810 | basic block function it would be the return node.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 811 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 812 | <p>One important concept for SelectionDAGs is the notion of a "legal" vs. | 
|  | 813 | "illegal" DAG.  A legal DAG for a target is one that only uses supported | 
|  | 814 | operations and supported types.  On a 32-bit PowerPC, for example, a DAG with | 
|  | 815 | a value of type i1, i8, i16, or i64 would be illegal, as would a DAG that uses a | 
|  | 816 | SREM or UREM operation.  The | 
|  | 817 | <a href="#selectiondag_legalize">legalize</a> phase is responsible for turning | 
|  | 818 | an illegal DAG into a legal DAG.</p> | 
|  | 819 |  | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 820 | </div> | 
|  | 821 |  | 
|  | 822 | <!-- _______________________________________________________________________ --> | 
|  | 823 | <div class="doc_subsubsection"> | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 824 | <a name="selectiondag_process">SelectionDAG Instruction Selection Process</a> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 825 | </div> | 
|  | 826 |  | 
|  | 827 | <div class="doc_text"> | 
|  | 828 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 829 | <p>SelectionDAG-based instruction selection consists of the following steps:</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 830 |  | 
|  | 831 | <ol> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 832 | <li><a href="#selectiondag_build">Build initial DAG</a> - This stage | 
|  | 833 | performs a simple translation from the input LLVM code to an illegal | 
|  | 834 | SelectionDAG.</li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 835 | <li><a href="#selectiondag_optimize">Optimize SelectionDAG</a> - This stage | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 836 | performs simple optimizations on the SelectionDAG to simplify it, and | 
|  | 837 | recognize meta instructions (like rotates and <tt>div</tt>/<tt>rem</tt> | 
|  | 838 | pairs) for targets that support these meta operations.  This makes the | 
|  | 839 | resultant code more efficient and the <a href="#selectiondag_select">select | 
|  | 840 | instructions from DAG</a> phase (below) simpler.</li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 841 | <li><a href="#selectiondag_legalize">Legalize SelectionDAG</a> - This stage | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 842 | converts the illegal SelectionDAG to a legal SelectionDAG by eliminating | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 843 | unsupported operations and data types.</li> | 
|  | 844 | <li><a href="#selectiondag_optimize">Optimize SelectionDAG (#2)</a> - This | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 845 | second run of the SelectionDAG optimizes the newly legalized DAG to | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 846 | eliminate inefficiencies introduced by legalization.</li> | 
|  | 847 | <li><a href="#selectiondag_select">Select instructions from DAG</a> - Finally, | 
|  | 848 | the target instruction selector matches the DAG operations to target | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 849 | instructions.  This process translates the target-independent input DAG into | 
|  | 850 | another DAG of target instructions.</li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 851 | <li><a href="#selectiondag_sched">SelectionDAG Scheduling and Formation</a> | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 852 | - The last phase assigns a linear order to the instructions in the | 
|  | 853 | target-instruction DAG and emits them into the MachineFunction being | 
|  | 854 | compiled.  This step uses traditional prepass scheduling techniques.</li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 855 | </ol> | 
|  | 856 |  | 
|  | 857 | <p>After all of these steps are complete, the SelectionDAG is destroyed and the | 
|  | 858 | rest of the code generation passes are run.</p> | 
|  | 859 |  | 
| Chris Lattner | df921f0 | 2005-10-17 01:40:33 +0000 | [diff] [blame] | 860 | <p>One great way to visualize what is going on here is to take advantage of a | 
|  | 861 | few LLC command line options.  In particular, the <tt>-view-isel-dags</tt> | 
|  | 862 | option pops up a window with the SelectionDAG input to the Select phase for all | 
|  | 863 | of the code compiled (if you only get errors printed to the console while using | 
|  | 864 | this, you probably <a href="ProgrammersManual.html#ViewGraph">need to configure | 
|  | 865 | your system</a> to add support for it).  The <tt>-view-sched-dags</tt> option | 
|  | 866 | views the SelectionDAG output from the Select phase and input to the Scheduler | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 867 | phase.</p> | 
|  | 868 |  | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 869 | </div> | 
|  | 870 |  | 
|  | 871 | <!-- _______________________________________________________________________ --> | 
|  | 872 | <div class="doc_subsubsection"> | 
|  | 873 | <a name="selectiondag_build">Initial SelectionDAG Construction</a> | 
|  | 874 | </div> | 
|  | 875 |  | 
|  | 876 | <div class="doc_text"> | 
|  | 877 |  | 
| Bill Wendling | 1644877 | 2006-08-28 03:04:05 +0000 | [diff] [blame] | 878 | <p>The initial SelectionDAG is naïvely peephole expanded from the LLVM | 
|  | 879 | input by the <tt>SelectionDAGLowering</tt> class in the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 880 | <tt>lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp</tt> file.  The intent of this | 
|  | 881 | pass is to expose as much low-level, target-specific details to the SelectionDAG | 
|  | 882 | as possible.  This pass is mostly hard-coded (e.g. an LLVM <tt>add</tt> turns | 
|  | 883 | into an <tt>SDNode add</tt> while a <tt>geteelementptr</tt> is expanded into the | 
|  | 884 | obvious arithmetic). This pass requires target-specific hooks to lower calls, | 
|  | 885 | returns, varargs, etc.  For these features, the | 
|  | 886 | <tt><a href="#targetlowering">TargetLowering</a></tt> interface is used.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 887 |  | 
|  | 888 | </div> | 
|  | 889 |  | 
|  | 890 | <!-- _______________________________________________________________________ --> | 
|  | 891 | <div class="doc_subsubsection"> | 
|  | 892 | <a name="selectiondag_legalize">SelectionDAG Legalize Phase</a> | 
|  | 893 | </div> | 
|  | 894 |  | 
|  | 895 | <div class="doc_text"> | 
|  | 896 |  | 
|  | 897 | <p>The Legalize phase is in charge of converting a DAG to only use the types and | 
|  | 898 | operations that are natively supported by the target.  This involves two major | 
|  | 899 | tasks:</p> | 
|  | 900 |  | 
|  | 901 | <ol> | 
|  | 902 | <li><p>Convert values of unsupported types to values of supported types.</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 903 | <p>There are two main ways of doing this: converting small types to | 
|  | 904 | larger types ("promoting"), and breaking up large integer types | 
|  | 905 | into smaller ones ("expanding").  For example, a target might require | 
|  | 906 | that all f32 values are promoted to f64 and that all i1/i8/i16 values | 
|  | 907 | are promoted to i32.  The same target might require that all i64 values | 
|  | 908 | be expanded into i32 values.  These changes can insert sign and zero | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 909 | extensions as needed to make sure that the final code has the same | 
|  | 910 | behavior as the input.</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 911 | <p>A target implementation tells the legalizer which types are supported | 
|  | 912 | (and which register class to use for them) by calling the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 913 | <tt>addRegisterClass</tt> method in its TargetLowering constructor.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 914 | </li> | 
|  | 915 |  | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 916 | <li><p>Eliminate operations that are not supported by the target.</p> | 
|  | 917 | <p>Targets often have weird constraints, such as not supporting every | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 918 | operation on every supported datatype (e.g. X86 does not support byte | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 919 | conditional moves and PowerPC does not support sign-extending loads from | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 920 | a 16-bit memory location).  Legalize takes care of this by open-coding | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 921 | another sequence of operations to emulate the operation ("expansion"), by | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 922 | promoting one type to a larger type that supports the operation | 
|  | 923 | ("promotion"), or by using a target-specific hook to implement the | 
|  | 924 | legalization ("custom").</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 925 | <p>A target implementation tells the legalizer which operations are not | 
|  | 926 | supported (and which of the above three actions to take) by calling the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 927 | <tt>setOperationAction</tt> method in its <tt>TargetLowering</tt> | 
|  | 928 | constructor.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 929 | </li> | 
|  | 930 | </ol> | 
|  | 931 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 932 | <p>Prior to the existance of the Legalize pass, we required that every target | 
|  | 933 | <a href="#selectiondag_optimize">selector</a> supported and handled every | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 934 | operator and type even if they are not natively supported.  The introduction of | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 935 | the Legalize phase allows all of the cannonicalization patterns to be shared | 
|  | 936 | across targets, and makes it very easy to optimize the cannonicalized code | 
|  | 937 | because it is still in the form of a DAG.</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 938 |  | 
|  | 939 | </div> | 
|  | 940 |  | 
|  | 941 | <!-- _______________________________________________________________________ --> | 
|  | 942 | <div class="doc_subsubsection"> | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 943 | <a name="selectiondag_optimize">SelectionDAG Optimization Phase: the DAG | 
|  | 944 | Combiner</a> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 945 | </div> | 
|  | 946 |  | 
|  | 947 | <div class="doc_text"> | 
|  | 948 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 949 | <p>The SelectionDAG optimization phase is run twice for code generation: once | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 950 | immediately after the DAG is built and once after legalization.  The first run | 
|  | 951 | of the pass allows the initial code to be cleaned up (e.g. performing | 
|  | 952 | optimizations that depend on knowing that the operators have restricted type | 
|  | 953 | inputs).  The second run of the pass cleans up the messy code generated by the | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 954 | Legalize pass, which allows Legalize to be very simple (it can focus on making | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 955 | code legal instead of focusing on generating <em>good</em> and legal code).</p> | 
|  | 956 |  | 
|  | 957 | <p>One important class of optimizations performed is optimizing inserted sign | 
|  | 958 | and zero extension instructions.  We currently use ad-hoc techniques, but could | 
|  | 959 | move to more rigorous techniques in the future.  Here are some good papers on | 
|  | 960 | the subject:</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 961 |  | 
|  | 962 | <p> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 963 | "<a href="http://www.eecs.harvard.edu/~nr/pubs/widen-abstract.html">Widening | 
|  | 964 | integer arithmetic</a>"<br> | 
|  | 965 | Kevin Redwine and Norman Ramsey<br> | 
|  | 966 | International Conference on Compiler Construction (CC) 2004 | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 967 | </p> | 
|  | 968 |  | 
|  | 969 |  | 
|  | 970 | <p> | 
|  | 971 | "<a href="http://portal.acm.org/citation.cfm?doid=512529.512552">Effective | 
|  | 972 | sign extension elimination</a>"<br> | 
|  | 973 | Motohiro Kawahito, Hideaki Komatsu, and Toshio Nakatani<br> | 
|  | 974 | Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language Design | 
|  | 975 | and Implementation. | 
|  | 976 | </p> | 
|  | 977 |  | 
|  | 978 | </div> | 
|  | 979 |  | 
|  | 980 | <!-- _______________________________________________________________________ --> | 
|  | 981 | <div class="doc_subsubsection"> | 
|  | 982 | <a name="selectiondag_select">SelectionDAG Select Phase</a> | 
|  | 983 | </div> | 
|  | 984 |  | 
|  | 985 | <div class="doc_text"> | 
|  | 986 |  | 
|  | 987 | <p>The Select phase is the bulk of the target-specific code for instruction | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 988 | selection.  This phase takes a legal SelectionDAG as input, pattern matches the | 
|  | 989 | instructions supported by the target to this DAG, and produces a new DAG of | 
|  | 990 | target code.  For example, consider the following LLVM fragment:</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 991 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 992 | <div class="doc_code"> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 993 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 994 | %t1 = add float %W, %X | 
|  | 995 | %t2 = mul float %t1, %Y | 
|  | 996 | %t3 = add float %t2, %Z | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 997 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 998 | </div> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 999 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1000 | <p>This LLVM code corresponds to a SelectionDAG that looks basically like | 
|  | 1001 | this:</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1002 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1003 | <div class="doc_code"> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1004 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1005 | (fadd:f32 (fmul:f32 (fadd:f32 W, X), Y), Z) | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1006 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1007 | </div> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1008 |  | 
| Chris Lattner | a1ff931 | 2005-10-17 15:19:24 +0000 | [diff] [blame] | 1009 | <p>If a target supports floating point multiply-and-add (FMA) operations, one | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1010 | of the adds can be merged with the multiply.  On the PowerPC, for example, the | 
|  | 1011 | output of the instruction selector might look like this DAG:</p> | 
|  | 1012 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1013 | <div class="doc_code"> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1014 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1015 | (FMADDS (FADDS W, X), Y, Z) | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1016 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1017 | </div> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1018 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1019 | <p>The <tt>FMADDS</tt> instruction is a ternary instruction that multiplies its | 
|  | 1020 | first two operands and adds the third (as single-precision floating-point | 
|  | 1021 | numbers).  The <tt>FADDS</tt> instruction is a simple binary single-precision | 
|  | 1022 | add instruction.  To perform this pattern match, the PowerPC backend includes | 
|  | 1023 | the following instruction definitions:</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1024 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1025 | <div class="doc_code"> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1026 | <pre> | 
|  | 1027 | def FMADDS : AForm_1<59, 29, | 
|  | 1028 | (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRC, F4RC:$FRB), | 
|  | 1029 | "fmadds $FRT, $FRA, $FRC, $FRB", | 
|  | 1030 | [<b>(set F4RC:$FRT, (fadd (fmul F4RC:$FRA, F4RC:$FRC), | 
|  | 1031 | F4RC:$FRB))</b>]>; | 
|  | 1032 | def FADDS : AForm_2<59, 21, | 
|  | 1033 | (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRB), | 
|  | 1034 | "fadds $FRT, $FRA, $FRB", | 
|  | 1035 | [<b>(set F4RC:$FRT, (fadd F4RC:$FRA, F4RC:$FRB))</b>]>; | 
|  | 1036 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1037 | </div> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1038 |  | 
|  | 1039 | <p>The portion of the instruction definition in bold indicates the pattern used | 
|  | 1040 | to match the instruction.  The DAG operators (like <tt>fmul</tt>/<tt>fadd</tt>) | 
|  | 1041 | are defined in the <tt>lib/Target/TargetSelectionDAG.td</tt> file. | 
|  | 1042 | "<tt>F4RC</tt>" is the register class of the input and result values.<p> | 
|  | 1043 |  | 
|  | 1044 | <p>The TableGen DAG instruction selector generator reads the instruction | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1045 | patterns in the <tt>.td</tt> file and automatically builds parts of the pattern | 
|  | 1046 | matching code for your target.  It has the following strengths:</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1047 |  | 
|  | 1048 | <ul> | 
|  | 1049 | <li>At compiler-compiler time, it analyzes your instruction patterns and tells | 
| Chris Lattner | 7d6915c | 2005-10-17 04:18:41 +0000 | [diff] [blame] | 1050 | you if your patterns make sense or not.</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1051 | <li>It can handle arbitrary constraints on operands for the pattern match.  In | 
| Chris Lattner | 7d6915c | 2005-10-17 04:18:41 +0000 | [diff] [blame] | 1052 | particular, it is straight-forward to say things like "match any immediate | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1053 | that is a 13-bit sign-extended value".  For examples, see the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1054 | <tt>immSExt16</tt> and related <tt>tblgen</tt> classes in the PowerPC | 
|  | 1055 | backend.</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1056 | <li>It knows several important identities for the patterns defined.  For | 
|  | 1057 | example, it knows that addition is commutative, so it allows the | 
|  | 1058 | <tt>FMADDS</tt> pattern above to match "<tt>(fadd X, (fmul Y, Z))</tt>" as | 
|  | 1059 | well as "<tt>(fadd (fmul X, Y), Z)</tt>", without the target author having | 
|  | 1060 | to specially handle this case.</li> | 
| Chris Lattner | 7d6915c | 2005-10-17 04:18:41 +0000 | [diff] [blame] | 1061 | <li>It has a full-featured type-inferencing system.  In particular, you should | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1062 | rarely have to explicitly tell the system what type parts of your patterns | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1063 | are.  In the <tt>FMADDS</tt> case above, we didn't have to tell | 
|  | 1064 | <tt>tblgen</tt> that all of the nodes in the pattern are of type 'f32'.  It | 
|  | 1065 | was able to infer and propagate this knowledge from the fact that | 
|  | 1066 | <tt>F4RC</tt> has type 'f32'.</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1067 | <li>Targets can define their own (and rely on built-in) "pattern fragments". | 
|  | 1068 | Pattern fragments are chunks of reusable patterns that get inlined into your | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1069 | patterns during compiler-compiler time.  For example, the integer | 
|  | 1070 | "<tt>(not x)</tt>" operation is actually defined as a pattern fragment that | 
|  | 1071 | expands as "<tt>(xor x, -1)</tt>", since the SelectionDAG does not have a | 
|  | 1072 | native '<tt>not</tt>' operation.  Targets can define their own short-hand | 
|  | 1073 | fragments as they see fit.  See the definition of '<tt>not</tt>' and | 
|  | 1074 | '<tt>ineg</tt>' for examples.</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1075 | <li>In addition to instructions, targets can specify arbitrary patterns that | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1076 | map to one or more instructions using the 'Pat' class.  For example, | 
| Chris Lattner | 7d6915c | 2005-10-17 04:18:41 +0000 | [diff] [blame] | 1077 | the PowerPC has no way to load an arbitrary integer immediate into a | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1078 | register in one instruction. To tell tblgen how to do this, it defines: | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1079 | <br> | 
|  | 1080 | <br> | 
|  | 1081 | <div class="doc_code"> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1082 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1083 | // Arbitrary immediate support.  Implement in terms of LIS/ORI. | 
|  | 1084 | def : Pat<(i32 imm:$imm), | 
|  | 1085 | (ORI (LIS (HI16 imm:$imm)), (LO16 imm:$imm))>; | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1086 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1087 | </div> | 
|  | 1088 | <br> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1089 | If none of the single-instruction patterns for loading an immediate into a | 
|  | 1090 | register match, this will be used.  This rule says "match an arbitrary i32 | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1091 | immediate, turning it into an <tt>ORI</tt> ('or a 16-bit immediate') and an | 
|  | 1092 | <tt>LIS</tt> ('load 16-bit immediate, where the immediate is shifted to the | 
|  | 1093 | left 16 bits') instruction".  To make this work, the | 
|  | 1094 | <tt>LO16</tt>/<tt>HI16</tt> node transformations are used to manipulate the | 
|  | 1095 | input immediate (in this case, take the high or low 16-bits of the | 
|  | 1096 | immediate).</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1097 | <li>While the system does automate a lot, it still allows you to write custom | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1098 | C++ code to match special cases if there is something that is hard to | 
|  | 1099 | express.</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1100 | </ul> | 
|  | 1101 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1102 | <p>While it has many strengths, the system currently has some limitations, | 
|  | 1103 | primarily because it is a work in progress and is not yet finished:</p> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1104 |  | 
|  | 1105 | <ul> | 
|  | 1106 | <li>Overall, there is no way to define or match SelectionDAG nodes that define | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1107 | multiple values (e.g. <tt>ADD_PARTS</tt>, <tt>LOAD</tt>, <tt>CALL</tt>, | 
|  | 1108 | etc).  This is the biggest reason that you currently still <em>have to</em> | 
|  | 1109 | write custom C++ code for your instruction selector.</li> | 
|  | 1110 | <li>There is no great way to support matching complex addressing modes yet.  In | 
|  | 1111 | the future, we will extend pattern fragments to allow them to define | 
|  | 1112 | multiple values (e.g. the four operands of the <a href="#x86_memory">X86 | 
|  | 1113 | addressing mode</a>).  In addition, we'll extend fragments so that a | 
|  | 1114 | fragment can match multiple different patterns.</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1115 | <li>We don't automatically infer flags like isStore/isLoad yet.</li> | 
|  | 1116 | <li>We don't automatically generate the set of supported registers and | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 1117 | operations for the <a href="#selectiondag_legalize">Legalizer</a> yet.</li> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1118 | <li>We don't have a way of tying in custom legalized nodes yet.</li> | 
| Chris Lattner | 7d6915c | 2005-10-17 04:18:41 +0000 | [diff] [blame] | 1119 | </ul> | 
| Chris Lattner | 7a025c8 | 2005-10-16 20:02:19 +0000 | [diff] [blame] | 1120 |  | 
|  | 1121 | <p>Despite these limitations, the instruction selector generator is still quite | 
|  | 1122 | useful for most of the binary and logical operations in typical instruction | 
|  | 1123 | sets.  If you run into any problems or can't figure out how to do something, | 
|  | 1124 | please let Chris know!</p> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 1125 |  | 
|  | 1126 | </div> | 
|  | 1127 |  | 
|  | 1128 | <!-- _______________________________________________________________________ --> | 
|  | 1129 | <div class="doc_subsubsection"> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 1130 | <a name="selectiondag_sched">SelectionDAG Scheduling and Formation Phase</a> | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 1131 | </div> | 
|  | 1132 |  | 
|  | 1133 | <div class="doc_text"> | 
|  | 1134 |  | 
|  | 1135 | <p>The scheduling phase takes the DAG of target instructions from the selection | 
|  | 1136 | phase and assigns an order.  The scheduler can pick an order depending on | 
|  | 1137 | various constraints of the machines (i.e. order for minimal register pressure or | 
|  | 1138 | try to cover instruction latencies).  Once an order is established, the DAG is | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1139 | converted to a list of <tt><a href="#machineinstr">MachineInstr</a></tt>s and | 
|  | 1140 | the SelectionDAG is destroyed.</p> | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 1141 |  | 
| Jeff Cohen | 0b81cda | 2005-10-24 16:54:55 +0000 | [diff] [blame] | 1142 | <p>Note that this phase is logically separate from the instruction selection | 
| Chris Lattner | c38959f | 2005-10-17 03:09:31 +0000 | [diff] [blame] | 1143 | phase, but is tied to it closely in the code because it operates on | 
|  | 1144 | SelectionDAGs.</p> | 
|  | 1145 |  | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 1146 | </div> | 
|  | 1147 |  | 
|  | 1148 | <!-- _______________________________________________________________________ --> | 
|  | 1149 | <div class="doc_subsubsection"> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 1150 | <a name="selectiondag_future">Future directions for the SelectionDAG</a> | 
|  | 1151 | </div> | 
|  | 1152 |  | 
|  | 1153 | <div class="doc_text"> | 
|  | 1154 |  | 
|  | 1155 | <ol> | 
| Chris Lattner | e35d3bb | 2005-10-16 00:36:38 +0000 | [diff] [blame] | 1156 | <li>Optional function-at-a-time selection.</li> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1157 | <li>Auto-generate entire selector from <tt>.td</tt> file.</li> | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 1158 | </ol> | 
|  | 1159 |  | 
|  | 1160 | </div> | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1161 |  | 
|  | 1162 | <!-- ======================================================================= --> | 
|  | 1163 | <div class="doc_subsection"> | 
|  | 1164 | <a name="ssamco">SSA-based Machine Code Optimizations</a> | 
|  | 1165 | </div> | 
|  | 1166 | <div class="doc_text"><p>To Be Written</p></div> | 
| Bill Wendling | a396ee8 | 2006-09-01 21:46:00 +0000 | [diff] [blame] | 1167 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1168 | <!-- ======================================================================= --> | 
|  | 1169 | <div class="doc_subsection"> | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1170 | <a name="liveintervals">Live Intervals</a> | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1171 | </div> | 
|  | 1172 |  | 
|  | 1173 | <div class="doc_text"> | 
|  | 1174 |  | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1175 | <p>Live Intervals are the ranges (intervals) where a variable is <i>live</i>. | 
|  | 1176 | They are used by some <a href="#regalloc">register allocator</a> passes to | 
| Bill Wendling | bd0d7b5 | 2006-09-07 08:36:28 +0000 | [diff] [blame] | 1177 | determine if two or more virtual registers which require the same physical | 
| Bill Wendling | 41b3252 | 2006-09-07 08:39:35 +0000 | [diff] [blame] | 1178 | register are live at the same point in the program (i.e., they conflict).  When | 
| Bill Wendling | bd0d7b5 | 2006-09-07 08:36:28 +0000 | [diff] [blame] | 1179 | this situation occurs, one virtual register must be <i>spilled</i>.</p> | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1180 |  | 
|  | 1181 | </div> | 
|  | 1182 |  | 
|  | 1183 | <!-- _______________________________________________________________________ --> | 
|  | 1184 | <div class="doc_subsubsection"> | 
|  | 1185 | <a name="livevariable_analysis">Live Variable Analysis</a> | 
|  | 1186 | </div> | 
|  | 1187 |  | 
|  | 1188 | <div class="doc_text"> | 
|  | 1189 |  | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1190 | <p>The first step in determining the live intervals of variables is to | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1191 | calculate the set of registers that are immediately dead after the | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1192 | instruction (i.e., the instruction calculates the value, but it is | 
|  | 1193 | never used) and the set of registers that are used by the instruction, | 
|  | 1194 | but are never used after the instruction (i.e., they are killed). Live | 
| Bill Wendling | bd0d7b5 | 2006-09-07 08:36:28 +0000 | [diff] [blame] | 1195 | variable information is computed for each <i>virtual</i> register and | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1196 | <i>register allocatable</i> physical register in the function.  This | 
|  | 1197 | is done in a very efficient manner because it uses SSA to sparsely | 
| Bill Wendling | bd0d7b5 | 2006-09-07 08:36:28 +0000 | [diff] [blame] | 1198 | compute lifetime information for virtual registers (which are in SSA | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1199 | form) and only has to track physical registers within a block.  Before | 
|  | 1200 | register allocation, LLVM can assume that physical registers are only | 
|  | 1201 | live within a single basic block.  This allows it to do a single, | 
|  | 1202 | local analysis to resolve physical register lifetimes within each | 
|  | 1203 | basic block. If a physical register is not register allocatable (e.g., | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1204 | a stack pointer or condition codes), it is not tracked.</p> | 
|  | 1205 |  | 
|  | 1206 | <p>Physical registers may be live in to or out of a function. Live in values | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1207 | are typically arguments in registers. Live out values are typically return | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1208 | values in registers. Live in values are marked as such, and are given a dummy | 
| Bill Wendling | bd0d7b5 | 2006-09-07 08:36:28 +0000 | [diff] [blame] | 1209 | "defining" instruction during live intervals analysis. If the last basic block | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1210 | of a function is a <tt>return</tt>, then it's marked as using all live out | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1211 | values in the function.</p> | 
|  | 1212 |  | 
|  | 1213 | <p><tt>PHI</tt> nodes need to be handled specially, because the calculation | 
|  | 1214 | of the live variable information from a depth first traversal of the CFG of | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1215 | the function won't guarantee that a virtual register used by the <tt>PHI</tt> | 
|  | 1216 | node is defined before it's used. When a <tt>PHI</tt> node is encounted, only | 
|  | 1217 | the definition is handled, because the uses will be handled in other basic | 
|  | 1218 | blocks.</p> | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1219 |  | 
|  | 1220 | <p>For each <tt>PHI</tt> node of the current basic block, we simulate an | 
|  | 1221 | assignment at the end of the current basic block and traverse the successor | 
|  | 1222 | basic blocks. If a successor basic block has a <tt>PHI</tt> node and one of | 
|  | 1223 | the <tt>PHI</tt> node's operands is coming from the current basic block, | 
|  | 1224 | then the variable is marked as <i>alive</i> within the current basic block | 
|  | 1225 | and all of its predecessor basic blocks, until the basic block with the | 
|  | 1226 | defining instruction is encountered.</p> | 
|  | 1227 |  | 
|  | 1228 | </div> | 
|  | 1229 |  | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1230 | <!-- _______________________________________________________________________ --> | 
|  | 1231 | <div class="doc_subsubsection"> | 
|  | 1232 | <a name="liveintervals_analysis">Live Intervals Analysis</a> | 
|  | 1233 | </div> | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1234 |  | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1235 | <div class="doc_text"> | 
| Bill Wendling | 3cd5ca6 | 2006-10-11 06:30:10 +0000 | [diff] [blame] | 1236 |  | 
| Bill Wendling | 82e2eea | 2006-10-11 18:00:22 +0000 | [diff] [blame] | 1237 | <p>We now have the information available to perform the live intervals analysis | 
| Bill Wendling | 3cd5ca6 | 2006-10-11 06:30:10 +0000 | [diff] [blame] | 1238 | and build the live intervals themselves.  We start off by numbering the basic | 
|  | 1239 | blocks and machine instructions.  We then handle the "live-in" values.  These | 
|  | 1240 | are in physical registers, so the physical register is assumed to be killed by | 
|  | 1241 | the end of the basic block.  Live intervals for virtual registers are computed | 
| Bill Wendling | 82e2eea | 2006-10-11 18:00:22 +0000 | [diff] [blame] | 1242 | for some ordering of the machine instructions <tt>[1, N]</tt>.  A live interval | 
|  | 1243 | is an interval <tt>[i, j)</tt>, where <tt>1 <= i <= j < N</tt>, for which a | 
| Bill Wendling | 3cd5ca6 | 2006-10-11 06:30:10 +0000 | [diff] [blame] | 1244 | variable is live.</p> | 
|  | 1245 |  | 
| Bill Wendling | 82e2eea | 2006-10-11 18:00:22 +0000 | [diff] [blame] | 1246 | <p><i><b>More to come...</b></i></p> | 
|  | 1247 |  | 
| Bill Wendling | 3fc488d | 2006-09-06 18:42:41 +0000 | [diff] [blame] | 1248 | </div> | 
| Bill Wendling | 2f87a88 | 2006-09-04 23:35:52 +0000 | [diff] [blame] | 1249 |  | 
|  | 1250 | <!-- ======================================================================= --> | 
|  | 1251 | <div class="doc_subsection"> | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1252 | <a name="regalloc">Register Allocation</a> | 
|  | 1253 | </div> | 
| Bill Wendling | a396ee8 | 2006-09-01 21:46:00 +0000 | [diff] [blame] | 1254 |  | 
|  | 1255 | <div class="doc_text"> | 
|  | 1256 |  | 
| Bill Wendling | 3cd5ca6 | 2006-10-11 06:30:10 +0000 | [diff] [blame] | 1257 | <p>The <i>Register Allocation problem</i> consists in mapping a program | 
|  | 1258 | <i>P<sub>v</sub></i>, that can use an unbounded number of virtual | 
|  | 1259 | registers, to a program <i>P<sub>p</sub></i> that contains a finite | 
|  | 1260 | (possibly small) number of physical registers. Each target architecture has | 
|  | 1261 | a different number of physical registers. If the number of physical | 
|  | 1262 | registers is not enough to accommodate all the virtual registers, some of | 
|  | 1263 | them will have to be mapped into memory. These virtuals are called | 
|  | 1264 | <i>spilled virtuals</i>.</p> | 
| Bill Wendling | a396ee8 | 2006-09-01 21:46:00 +0000 | [diff] [blame] | 1265 |  | 
|  | 1266 | </div> | 
|  | 1267 |  | 
|  | 1268 | <!-- _______________________________________________________________________ --> | 
|  | 1269 |  | 
|  | 1270 | <div class="doc_subsubsection"> | 
|  | 1271 | <a name="regAlloc_represent">How registers are represented in LLVM</a> | 
|  | 1272 | </div> | 
|  | 1273 |  | 
|  | 1274 | <div class="doc_text"> | 
|  | 1275 |  | 
|  | 1276 | <p>In LLVM, physical registers are denoted by integer numbers that | 
|  | 1277 | normally range from 1 to 1023. To see how this numbering is defined | 
|  | 1278 | for a particular architecture, you can read the | 
|  | 1279 | <tt>GenRegisterNames.inc</tt> file for that architecture. For | 
|  | 1280 | instance, by inspecting | 
|  | 1281 | <tt>lib/Target/X86/X86GenRegisterNames.inc</tt> we see that the 32-bit | 
|  | 1282 | register <tt>EAX</tt> is denoted by 15, and the MMX register | 
|  | 1283 | <tt>MM0</tt> is mapped to 48.</p> | 
|  | 1284 |  | 
|  | 1285 | <p>Some architectures contain registers that share the same physical | 
|  | 1286 | location. A notable example is the X86 platform. For instance, in the | 
|  | 1287 | X86 architecture, the registers <tt>EAX</tt>, <tt>AX</tt> and | 
|  | 1288 | <tt>AL</tt> share the first eight bits. These physical registers are | 
|  | 1289 | marked as <i>aliased</i> in LLVM. Given a particular architecture, you | 
|  | 1290 | can check which registers are aliased by inspecting its | 
|  | 1291 | <tt>RegisterInfo.td</tt> file. Moreover, the method | 
|  | 1292 | <tt>MRegisterInfo::getAliasSet(p_reg)</tt> returns an array containing | 
|  | 1293 | all the physical registers aliased to the register <tt>p_reg</tt>.</p> | 
|  | 1294 |  | 
|  | 1295 | <p>Physical registers, in LLVM, are grouped in <i>Register Classes</i>. | 
|  | 1296 | Elements in the same register class are functionally equivalent, and can | 
|  | 1297 | be interchangeably used. Each virtual register can only be mapped to | 
|  | 1298 | physical registers of a particular class. For instance, in the X86 | 
|  | 1299 | architecture, some virtuals can only be allocated to 8 bit registers. | 
|  | 1300 | A register class is described by <tt>TargetRegisterClass</tt> objects. | 
|  | 1301 | To discover if a virtual register is compatible with a given physical, | 
|  | 1302 | this code can be used: | 
|  | 1303 | </p> | 
|  | 1304 |  | 
|  | 1305 | <div class="doc_code"> | 
|  | 1306 | <pre> | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 1307 | bool RegMapping_Fer::compatible_class(MachineFunction &mf, | 
| Bill Wendling | a396ee8 | 2006-09-01 21:46:00 +0000 | [diff] [blame] | 1308 | unsigned v_reg, | 
|  | 1309 | unsigned p_reg) { | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 1310 | assert(MRegisterInfo::isPhysicalRegister(p_reg) && | 
| Bill Wendling | a396ee8 | 2006-09-01 21:46:00 +0000 | [diff] [blame] | 1311 | "Target register must be physical"); | 
|  | 1312 | const TargetRegisterClass *trc = mf.getSSARegMap()->getRegClass(v_reg); | 
|  | 1313 | return trc->contains(p_reg); | 
|  | 1314 | } | 
|  | 1315 | </pre> | 
|  | 1316 | </div> | 
|  | 1317 |  | 
|  | 1318 | <p>Sometimes, mostly for debugging purposes, it is useful to change | 
|  | 1319 | the number of physical registers available in the target | 
|  | 1320 | architecture. This must be done statically, inside the | 
|  | 1321 | <tt>TargetRegsterInfo.td</tt> file. Just <tt>grep</tt> for | 
|  | 1322 | <tt>RegisterClass</tt>, the last parameter of which is a list of | 
|  | 1323 | registers. Just commenting some out is one simple way to avoid them | 
|  | 1324 | being used. A more polite way is to explicitly exclude some registers | 
|  | 1325 | from the <i>allocation order</i>. See the definition of the | 
|  | 1326 | <tt>GR</tt> register class in | 
|  | 1327 | <tt>lib/Target/IA64/IA64RegisterInfo.td</tt> for an example of this | 
|  | 1328 | (e.g., <tt>numReservedRegs</tt> registers are hidden.)</p> | 
|  | 1329 |  | 
|  | 1330 | <p>Virtual registers are also denoted by integer numbers. Contrary to | 
|  | 1331 | physical registers, different virtual registers never share the same | 
|  | 1332 | number. The smallest virtual register is normally assigned the number | 
|  | 1333 | 1024. This may change, so, in order to know which is the first virtual | 
|  | 1334 | register, you should access | 
|  | 1335 | <tt>MRegisterInfo::FirstVirtualRegister</tt>. Any register whose | 
|  | 1336 | number is greater than or equal to | 
|  | 1337 | <tt>MRegisterInfo::FirstVirtualRegister</tt> is considered a virtual | 
|  | 1338 | register. Whereas physical registers are statically defined in a | 
|  | 1339 | <tt>TargetRegisterInfo.td</tt> file and cannot be created by the | 
|  | 1340 | application developer, that is not the case with virtual registers. | 
|  | 1341 | In order to create new virtual registers, use the method | 
|  | 1342 | <tt>SSARegMap::createVirtualRegister()</tt>. This method will return a | 
|  | 1343 | virtual register with the highest code. | 
|  | 1344 | </p> | 
|  | 1345 |  | 
|  | 1346 | <p>Before register allocation, the operands of an instruction are | 
|  | 1347 | mostly virtual registers, although physical registers may also be | 
|  | 1348 | used. In order to check if a given machine operand is a register, use | 
|  | 1349 | the boolean function <tt>MachineOperand::isRegister()</tt>. To obtain | 
|  | 1350 | the integer code of a register, use | 
|  | 1351 | <tt>MachineOperand::getReg()</tt>. An instruction may define or use a | 
|  | 1352 | register. For instance, <tt>ADD reg:1026 := reg:1025 reg:1024</tt> | 
|  | 1353 | defines the registers 1024, and uses registers 1025 and 1026. Given a | 
|  | 1354 | register operand, the method <tt>MachineOperand::isUse()</tt> informs | 
|  | 1355 | if that register is being used by the instruction. The method | 
|  | 1356 | <tt>MachineOperand::isDef()</tt> informs if that registers is being | 
|  | 1357 | defined.</p> | 
|  | 1358 |  | 
|  | 1359 | <p>We will call physical registers present in the LLVM bytecode before | 
|  | 1360 | register allocation <i>pre-colored registers</i>. Pre-colored | 
|  | 1361 | registers are used in many different situations, for instance, to pass | 
|  | 1362 | parameters of functions calls, and to store results of particular | 
|  | 1363 | instructions. There are two types of pre-colored registers: the ones | 
|  | 1364 | <i>implicitly</i> defined, and those <i>explicitly</i> | 
|  | 1365 | defined. Explicitly defined registers are normal operands, and can be | 
|  | 1366 | accessed with <tt>MachineInstr::getOperand(int)::getReg()</tt>.  In | 
|  | 1367 | order to check which registers are implicitly defined by an | 
|  | 1368 | instruction, use the | 
|  | 1369 | <tt>TargetInstrInfo::get(opcode)::ImplicitDefs</tt>, where | 
|  | 1370 | <tt>opcode</tt> is the opcode of the target instruction. One important | 
|  | 1371 | difference between explicit and implicit physical registers is that | 
|  | 1372 | the latter are defined statically for each instruction, whereas the | 
|  | 1373 | former may vary depending on the program being compiled. For example, | 
|  | 1374 | an instruction that represents a function call will always implicitly | 
|  | 1375 | define or use the same set of physical registers. To read the | 
|  | 1376 | registers implicitly used by an instruction, use | 
|  | 1377 | <tt>TargetInstrInfo::get(opcode)::ImplicitUses</tt>. Pre-colored | 
|  | 1378 | registers impose constraints on any register allocation algorithm. The | 
|  | 1379 | register allocator must make sure that none of them is been | 
|  | 1380 | overwritten by the values of virtual registers while still alive.</p> | 
|  | 1381 |  | 
|  | 1382 | </div> | 
|  | 1383 |  | 
|  | 1384 | <!-- _______________________________________________________________________ --> | 
|  | 1385 |  | 
|  | 1386 | <div class="doc_subsubsection"> | 
|  | 1387 | <a name="regAlloc_howTo">Mapping virtual registers to physical registers</a> | 
|  | 1388 | </div> | 
|  | 1389 |  | 
|  | 1390 | <div class="doc_text"> | 
|  | 1391 |  | 
|  | 1392 | <p>There are two ways to map virtual registers to physical registers (or to | 
|  | 1393 | memory slots). The first way, that we will call <i>direct mapping</i>, | 
|  | 1394 | is based on the use of methods of the classes <tt>MRegisterInfo</tt>, | 
|  | 1395 | and <tt>MachineOperand</tt>. The second way, that we will call | 
|  | 1396 | <i>indirect mapping</i>, relies on the <tt>VirtRegMap</tt> class in | 
|  | 1397 | order to insert loads and stores sending and getting values to and from | 
|  | 1398 | memory.</p> | 
|  | 1399 |  | 
|  | 1400 | <p>The direct mapping provides more flexibility to the developer of | 
|  | 1401 | the register allocator; however, it is more error prone, and demands | 
|  | 1402 | more implementation work.  Basically, the programmer will have to | 
|  | 1403 | specify where load and store instructions should be inserted in the | 
|  | 1404 | target function being compiled in order to get and store values in | 
|  | 1405 | memory. To assign a physical register to a virtual register present in | 
|  | 1406 | a given operand, use <tt>MachineOperand::setReg(p_reg)</tt>. To insert | 
|  | 1407 | a store instruction, use | 
|  | 1408 | <tt>MRegisterInfo::storeRegToStackSlot(...)</tt>, and to insert a load | 
|  | 1409 | instruction, use <tt>MRegisterInfo::loadRegFromStackSlot</tt>.</p> | 
|  | 1410 |  | 
|  | 1411 | <p>The indirect mapping shields the application developer from the | 
|  | 1412 | complexities of inserting load and store instructions. In order to map | 
|  | 1413 | a virtual register to a physical one, use | 
|  | 1414 | <tt>VirtRegMap::assignVirt2Phys(vreg, preg)</tt>.  In order to map a | 
|  | 1415 | certain virtual register to memory, use | 
|  | 1416 | <tt>VirtRegMap::assignVirt2StackSlot(vreg)</tt>. This method will | 
|  | 1417 | return the stack slot where <tt>vreg</tt>'s value will be located.  If | 
|  | 1418 | it is necessary to map another virtual register to the same stack | 
|  | 1419 | slot, use <tt>VirtRegMap::assignVirt2StackSlot(vreg, | 
|  | 1420 | stack_location)</tt>. One important point to consider when using the | 
|  | 1421 | indirect mapping, is that even if a virtual register is mapped to | 
|  | 1422 | memory, it still needs to be mapped to a physical register. This | 
|  | 1423 | physical register is the location where the virtual register is | 
|  | 1424 | supposed to be found before being stored or after being reloaded.</p> | 
|  | 1425 |  | 
|  | 1426 | <p>If the indirect strategy is used, after all the virtual registers | 
|  | 1427 | have been mapped to physical registers or stack slots, it is necessary | 
|  | 1428 | to use a spiller object to place load and store instructions in the | 
|  | 1429 | code. Every virtual that has been mapped to a stack slot will be | 
|  | 1430 | stored to memory after been defined and will be loaded before being | 
|  | 1431 | used. The implementation of the spiller tries to recycle load/store | 
|  | 1432 | instructions, avoiding unnecessary instructions. For an example of how | 
|  | 1433 | to invoke the spiller, see | 
|  | 1434 | <tt>RegAllocLinearScan::runOnMachineFunction</tt> in | 
|  | 1435 | <tt>lib/CodeGen/RegAllocLinearScan.cpp</tt>.</p> | 
|  | 1436 |  | 
|  | 1437 | </div> | 
|  | 1438 |  | 
|  | 1439 | <!-- _______________________________________________________________________ --> | 
|  | 1440 | <div class="doc_subsubsection"> | 
|  | 1441 | <a name="regAlloc_twoAddr">Handling two address instructions</a> | 
|  | 1442 | </div> | 
|  | 1443 |  | 
|  | 1444 | <div class="doc_text"> | 
|  | 1445 |  | 
|  | 1446 | <p>With very rare exceptions (e.g., function calls), the LLVM machine | 
|  | 1447 | code instructions are three address instructions. That is, each | 
|  | 1448 | instruction is expected to define at most one register, and to use at | 
|  | 1449 | most two registers.  However, some architectures use two address | 
|  | 1450 | instructions. In this case, the defined register is also one of the | 
|  | 1451 | used register. For instance, an instruction such as <tt>ADD %EAX, | 
|  | 1452 | %EBX</tt>, in X86 is actually equivalent to <tt>%EAX = %EAX + | 
|  | 1453 | %EBX</tt>.</p> | 
|  | 1454 |  | 
|  | 1455 | <p>In order to produce correct code, LLVM must convert three address | 
|  | 1456 | instructions that represent two address instructions into true two | 
|  | 1457 | address instructions. LLVM provides the pass | 
|  | 1458 | <tt>TwoAddressInstructionPass</tt> for this specific purpose. It must | 
|  | 1459 | be run before register allocation takes place. After its execution, | 
|  | 1460 | the resulting code may no longer be in SSA form. This happens, for | 
|  | 1461 | instance, in situations where an instruction such as <tt>%a = ADD %b | 
|  | 1462 | %c</tt> is converted to two instructions such as:</p> | 
|  | 1463 |  | 
|  | 1464 | <div class="doc_code"> | 
|  | 1465 | <pre> | 
|  | 1466 | %a = MOVE %b | 
|  | 1467 | %a = ADD %a %b | 
|  | 1468 | </pre> | 
|  | 1469 | </div> | 
|  | 1470 |  | 
|  | 1471 | <p>Notice that, internally, the second instruction is represented as | 
|  | 1472 | <tt>ADD %a[def/use] %b</tt>. I.e., the register operand <tt>%a</tt> is | 
|  | 1473 | both used and defined by the instruction.</p> | 
|  | 1474 |  | 
|  | 1475 | </div> | 
|  | 1476 |  | 
|  | 1477 | <!-- _______________________________________________________________________ --> | 
|  | 1478 | <div class="doc_subsubsection"> | 
|  | 1479 | <a name="regAlloc_ssaDecon">The SSA deconstruction phase</a> | 
|  | 1480 | </div> | 
|  | 1481 |  | 
|  | 1482 | <div class="doc_text"> | 
|  | 1483 |  | 
|  | 1484 | <p>An important transformation that happens during register allocation is called | 
|  | 1485 | the <i>SSA Deconstruction Phase</i>. The SSA form simplifies many | 
|  | 1486 | analyses that are performed on the control flow graph of | 
|  | 1487 | programs. However, traditional instruction sets do not implement | 
|  | 1488 | PHI instructions. Thus, in order to generate executable code, compilers | 
|  | 1489 | must replace PHI instructions with other instructions that preserve their | 
|  | 1490 | semantics.</p> | 
|  | 1491 |  | 
|  | 1492 | <p>There are many ways in which PHI instructions can safely be removed | 
|  | 1493 | from the target code. The most traditional PHI deconstruction | 
|  | 1494 | algorithm replaces PHI instructions with copy instructions. That is | 
|  | 1495 | the strategy adopted by LLVM. The SSA deconstruction algorithm is | 
|  | 1496 | implemented in n<tt>lib/CodeGen/>PHIElimination.cpp</tt>. In order to | 
|  | 1497 | invoke this pass, the identifier <tt>PHIEliminationID</tt> must be | 
|  | 1498 | marked as required in the code of the register allocator.</p> | 
|  | 1499 |  | 
|  | 1500 | </div> | 
|  | 1501 |  | 
|  | 1502 | <!-- _______________________________________________________________________ --> | 
|  | 1503 | <div class="doc_subsubsection"> | 
|  | 1504 | <a name="regAlloc_fold">Instruction folding</a> | 
|  | 1505 | </div> | 
|  | 1506 |  | 
|  | 1507 | <div class="doc_text"> | 
|  | 1508 |  | 
|  | 1509 | <p><i>Instruction folding</i> is an optimization performed during | 
|  | 1510 | register allocation that removes unnecessary copy instructions. For | 
|  | 1511 | instance, a sequence of instructions such as:</p> | 
|  | 1512 |  | 
|  | 1513 | <div class="doc_code"> | 
|  | 1514 | <pre> | 
|  | 1515 | %EBX = LOAD %mem_address | 
|  | 1516 | %EAX = COPY %EBX | 
|  | 1517 | </pre> | 
|  | 1518 | </div> | 
|  | 1519 |  | 
|  | 1520 | <p>can be safely substituted by the single instruction: | 
|  | 1521 |  | 
|  | 1522 | <div class="doc_code"> | 
|  | 1523 | <pre> | 
|  | 1524 | %EAX = LOAD %mem_address | 
|  | 1525 | </pre> | 
|  | 1526 | </div> | 
|  | 1527 |  | 
|  | 1528 | <p>Instructions can be folded with the | 
|  | 1529 | <tt>MRegisterInfo::foldMemoryOperand(...)</tt> method. Care must be | 
|  | 1530 | taken when folding instructions; a folded instruction can be quite | 
|  | 1531 | different from the original instruction. See | 
|  | 1532 | <tt>LiveIntervals::addIntervalsForSpills</tt> in | 
|  | 1533 | <tt>lib/CodeGen/LiveIntervalAnalysis.cpp</tt> for an example of its use.</p> | 
|  | 1534 |  | 
|  | 1535 | </div> | 
|  | 1536 |  | 
|  | 1537 | <!-- _______________________________________________________________________ --> | 
|  | 1538 |  | 
|  | 1539 | <div class="doc_subsubsection"> | 
|  | 1540 | <a name="regAlloc_builtIn">Built in register allocators</a> | 
|  | 1541 | </div> | 
|  | 1542 |  | 
|  | 1543 | <div class="doc_text"> | 
|  | 1544 |  | 
|  | 1545 | <p>The LLVM infrastructure provides the application developer with | 
|  | 1546 | three different register allocators:</p> | 
|  | 1547 |  | 
|  | 1548 | <ul> | 
|  | 1549 | <li><i>Simple</i> - This is a very simple implementation that does | 
|  | 1550 | not keep values in registers across instructions. This register | 
|  | 1551 | allocator immediately spills every value right after it is | 
|  | 1552 | computed, and reloads all used operands from memory to temporary | 
|  | 1553 | registers before each instruction.</li> | 
|  | 1554 | <li><i>Local</i> - This register allocator is an improvement on the | 
|  | 1555 | <i>Simple</i> implementation. It allocates registers on a basic | 
|  | 1556 | block level, attempting to keep values in registers and reusing | 
|  | 1557 | registers as appropriate.</li> | 
|  | 1558 | <li><i>Linear Scan</i> - <i>The default allocator</i>. This is the | 
|  | 1559 | well-know linear scan register allocator. Whereas the | 
|  | 1560 | <i>Simple</i> and <i>Local</i> algorithms use a direct mapping | 
|  | 1561 | implementation technique, the <i>Linear Scan</i> implementation | 
|  | 1562 | uses a spiller in order to place load and stores.</li> | 
|  | 1563 | </ul> | 
|  | 1564 |  | 
|  | 1565 | <p>The type of register allocator used in <tt>llc</tt> can be chosen with the | 
|  | 1566 | command line option <tt>-regalloc=...</tt>:</p> | 
|  | 1567 |  | 
|  | 1568 | <div class="doc_code"> | 
|  | 1569 | <pre> | 
|  | 1570 | $ llc -f -regalloc=simple file.bc -o sp.s; | 
|  | 1571 | $ llc -f -regalloc=local file.bc -o lc.s; | 
|  | 1572 | $ llc -f -regalloc=linearscan file.bc -o ln.s; | 
|  | 1573 | </pre> | 
|  | 1574 | </div> | 
|  | 1575 |  | 
|  | 1576 | </div> | 
|  | 1577 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1578 | <!-- ======================================================================= --> | 
|  | 1579 | <div class="doc_subsection"> | 
|  | 1580 | <a name="proepicode">Prolog/Epilog Code Insertion</a> | 
|  | 1581 | </div> | 
|  | 1582 | <div class="doc_text"><p>To Be Written</p></div> | 
|  | 1583 | <!-- ======================================================================= --> | 
|  | 1584 | <div class="doc_subsection"> | 
|  | 1585 | <a name="latemco">Late Machine Code Optimizations</a> | 
|  | 1586 | </div> | 
|  | 1587 | <div class="doc_text"><p>To Be Written</p></div> | 
|  | 1588 | <!-- ======================================================================= --> | 
|  | 1589 | <div class="doc_subsection"> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 1590 | <a name="codeemit">Code Emission</a> | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1591 | </div> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1592 | <div class="doc_text"><p>To Be Written</p></div> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 1593 | <!-- _______________________________________________________________________ --> | 
|  | 1594 | <div class="doc_subsubsection"> | 
|  | 1595 | <a name="codeemit_asm">Generating Assembly Code</a> | 
|  | 1596 | </div> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1597 | <div class="doc_text"><p>To Be Written</p></div> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 1598 | <!-- _______________________________________________________________________ --> | 
|  | 1599 | <div class="doc_subsubsection"> | 
|  | 1600 | <a name="codeemit_bin">Generating Binary Machine Code</a> | 
|  | 1601 | </div> | 
|  | 1602 |  | 
|  | 1603 | <div class="doc_text"> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1604 | <p>For the JIT or <tt>.o</tt> file writer</p> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 1605 | </div> | 
|  | 1606 |  | 
|  | 1607 |  | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 1608 | <!-- *********************************************************************** --> | 
|  | 1609 | <div class="doc_section"> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 1610 | <a name="targetimpls">Target-specific Implementation Notes</a> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1611 | </div> | 
|  | 1612 | <!-- *********************************************************************** --> | 
|  | 1613 |  | 
|  | 1614 | <div class="doc_text"> | 
|  | 1615 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1616 | <p>This section of the document explains features or design decisions that | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1617 | are specific to the code generator for a particular target.</p> | 
|  | 1618 |  | 
|  | 1619 | </div> | 
|  | 1620 |  | 
|  | 1621 |  | 
|  | 1622 | <!-- ======================================================================= --> | 
|  | 1623 | <div class="doc_subsection"> | 
|  | 1624 | <a name="x86">The X86 backend</a> | 
|  | 1625 | </div> | 
|  | 1626 |  | 
|  | 1627 | <div class="doc_text"> | 
|  | 1628 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1629 | <p>The X86 code generator lives in the <tt>lib/Target/X86</tt> directory.  This | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1630 | code generator currently targets a generic P6-like processor.  As such, it | 
|  | 1631 | produces a few P6-and-above instructions (like conditional moves), but it does | 
|  | 1632 | not make use of newer features like MMX or SSE.  In the future, the X86 backend | 
| Chris Lattner | aa5bcb5 | 2005-01-28 17:22:53 +0000 | [diff] [blame] | 1633 | will have sub-target support added for specific processor families and | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1634 | implementations.</p> | 
|  | 1635 |  | 
|  | 1636 | </div> | 
|  | 1637 |  | 
|  | 1638 | <!-- _______________________________________________________________________ --> | 
|  | 1639 | <div class="doc_subsubsection"> | 
| Chris Lattner | 9b988be | 2005-07-12 00:20:49 +0000 | [diff] [blame] | 1640 | <a name="x86_tt">X86 Target Triples Supported</a> | 
|  | 1641 | </div> | 
|  | 1642 |  | 
|  | 1643 | <div class="doc_text"> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1644 |  | 
|  | 1645 | <p>The following are the known target triples that are supported by the X86 | 
|  | 1646 | backend.  This is not an exhaustive list, and it would be useful to add those | 
|  | 1647 | that people test.</p> | 
| Chris Lattner | 9b988be | 2005-07-12 00:20:49 +0000 | [diff] [blame] | 1648 |  | 
|  | 1649 | <ul> | 
|  | 1650 | <li><b>i686-pc-linux-gnu</b> - Linux</li> | 
|  | 1651 | <li><b>i386-unknown-freebsd5.3</b> - FreeBSD 5.3</li> | 
|  | 1652 | <li><b>i686-pc-cygwin</b> - Cygwin on Win32</li> | 
|  | 1653 | <li><b>i686-pc-mingw32</b> - MingW on Win32</li> | 
| Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 1654 | <li><b>i386-pc-mingw32msvc</b> - MingW crosscompiler on Linux</li> | 
| Chris Lattner | 32e89f2 | 2005-10-16 18:31:08 +0000 | [diff] [blame] | 1655 | <li><b>i686-apple-darwin*</b> - Apple Darwin on X86</li> | 
| Chris Lattner | 9b988be | 2005-07-12 00:20:49 +0000 | [diff] [blame] | 1656 | </ul> | 
|  | 1657 |  | 
|  | 1658 | </div> | 
|  | 1659 |  | 
|  | 1660 | <!-- _______________________________________________________________________ --> | 
|  | 1661 | <div class="doc_subsubsection"> | 
| Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 1662 | <a name="x86_cc">X86 Calling Conventions supported</a> | 
|  | 1663 | </div> | 
|  | 1664 |  | 
|  | 1665 |  | 
|  | 1666 | <div class="doc_text"> | 
|  | 1667 |  | 
|  | 1668 | <p>The folowing target-specific calling conventions are known to backend:</p> | 
|  | 1669 |  | 
|  | 1670 | <ul> | 
|  | 1671 | <li><b>x86_StdCall</b> - stdcall calling convention seen on Microsoft Windows | 
|  | 1672 | platform (CC ID = 64).</li> | 
|  | 1673 | <li><b>x86_FastCall</b> - fastcall calling convention seen on Microsoft Windows | 
|  | 1674 | platform (CC ID = 65).</li> | 
|  | 1675 | </ul> | 
|  | 1676 |  | 
|  | 1677 | </div> | 
|  | 1678 |  | 
|  | 1679 | <!-- _______________________________________________________________________ --> | 
|  | 1680 | <div class="doc_subsubsection"> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1681 | <a name="x86_memory">Representing X86 addressing modes in MachineInstrs</a> | 
|  | 1682 | </div> | 
|  | 1683 |  | 
|  | 1684 | <div class="doc_text"> | 
|  | 1685 |  | 
| Misha Brukman | 600df45 | 2005-02-17 22:22:24 +0000 | [diff] [blame] | 1686 | <p>The x86 has a very flexible way of accessing memory.  It is capable of | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1687 | forming memory addresses of the following expression directly in integer | 
|  | 1688 | instructions (which use ModR/M addressing):</p> | 
|  | 1689 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1690 | <div class="doc_code"> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1691 | <pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1692 | Base + [1,2,4,8] * IndexReg + Disp32 | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1693 | </pre> | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1694 | </div> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1695 |  | 
| Misha Brukman | 600df45 | 2005-02-17 22:22:24 +0000 | [diff] [blame] | 1696 | <p>In order to represent this, LLVM tracks no less than 4 operands for each | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1697 | memory operand of this form.  This means that the "load" form of '<tt>mov</tt>' | 
|  | 1698 | has the following <tt>MachineOperand</tt>s in this order:</p> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1699 |  | 
|  | 1700 | <pre> | 
|  | 1701 | Index:        0     |    1        2       3           4 | 
|  | 1702 | Meaning:   DestReg, | BaseReg,  Scale, IndexReg, Displacement | 
|  | 1703 | OperandTy: VirtReg, | VirtReg, UnsImm, VirtReg,   SignExtImm | 
|  | 1704 | </pre> | 
|  | 1705 |  | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1706 | <p>Stores, and all other instructions, treat the four memory operands in the | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1707 | same way and in the same order.</p> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1708 |  | 
|  | 1709 | </div> | 
|  | 1710 |  | 
|  | 1711 | <!-- _______________________________________________________________________ --> | 
|  | 1712 | <div class="doc_subsubsection"> | 
|  | 1713 | <a name="x86_names">Instruction naming</a> | 
|  | 1714 | </div> | 
|  | 1715 |  | 
|  | 1716 | <div class="doc_text"> | 
|  | 1717 |  | 
| Bill Wendling | 91e10c4 | 2006-08-28 02:26:32 +0000 | [diff] [blame] | 1718 | <p>An instruction name consists of the base name, a default operand size, and a | 
| Reid Spencer | ad1f0cd | 2005-04-24 20:56:18 +0000 | [diff] [blame] | 1719 | a character per operand with an optional special size. For example:</p> | 
| Chris Lattner | ec94f80 | 2004-06-04 00:16:02 +0000 | [diff] [blame] | 1720 |  | 
|  | 1721 | <p> | 
|  | 1722 | <tt>ADD8rr</tt> -> add, 8-bit register, 8-bit register<br> | 
|  | 1723 | <tt>IMUL16rmi</tt> -> imul, 16-bit register, 16-bit memory, 16-bit immediate<br> | 
|  | 1724 | <tt>IMUL16rmi8</tt> -> imul, 16-bit register, 16-bit memory, 8-bit immediate<br> | 
|  | 1725 | <tt>MOVSX32rm16</tt> -> movsx, 32-bit register, 16-bit memory | 
|  | 1726 | </p> | 
|  | 1727 |  | 
|  | 1728 | </div> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 1729 |  | 
| Jim Laskey | 762b6cb | 2006-12-14 17:19:50 +0000 | [diff] [blame] | 1730 | <!-- ======================================================================= --> | 
|  | 1731 | <div class="doc_subsection"> | 
|  | 1732 | <a name="ppc">The PowerPC backend</a> | 
|  | 1733 | </div> | 
|  | 1734 |  | 
|  | 1735 | <div class="doc_text"> | 
|  | 1736 | <p>The PowerPC code generator lives in the lib/Target/PowerPC directory.  The | 
|  | 1737 | code generation is retargetable to several variations or <i>subtargets</i> of | 
|  | 1738 | the PowerPC ISA; including ppc32, ppc64 and altivec. | 
|  | 1739 | </p> | 
|  | 1740 | </div> | 
|  | 1741 |  | 
|  | 1742 | <!-- _______________________________________________________________________ --> | 
|  | 1743 | <div class="doc_subsubsection"> | 
|  | 1744 | <a name="ppc_abi">LLVM PowerPC ABI</a> | 
|  | 1745 | </div> | 
|  | 1746 |  | 
|  | 1747 | <div class="doc_text"> | 
|  | 1748 | <p>LLVM follows the AIX PowerPC ABI, with two deviations. LLVM uses a PC | 
|  | 1749 | relative (PIC) or static addressing for accessing global values, so no TOC (r2) | 
|  | 1750 | is used. Second, r31 is used as a frame pointer to allow dynamic growth of a | 
|  | 1751 | stack frame.  LLVM takes advantage of having no TOC to provide space to save | 
|  | 1752 | the frame pointer in the PowerPC linkage area of the caller frame.  Other | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 1753 | details of PowerPC ABI can be found at <a href= | 
|  | 1754 | "http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/Articles/32bitPowerPC.html" | 
|  | 1755 | >PowerPC ABI.</a> Note: This link describes the 32 bit ABI.  The | 
|  | 1756 | 64 bit ABI is similar except space for GPRs are 8 bytes wide (not 4) and r13 is | 
|  | 1757 | reserved for system use.</p> | 
| Jim Laskey | 762b6cb | 2006-12-14 17:19:50 +0000 | [diff] [blame] | 1758 | </div> | 
|  | 1759 |  | 
|  | 1760 | <!-- _______________________________________________________________________ --> | 
|  | 1761 | <div class="doc_subsubsection"> | 
|  | 1762 | <a name="ppc_frame">Frame Layout</a> | 
|  | 1763 | </div> | 
|  | 1764 |  | 
|  | 1765 | <div class="doc_text"> | 
|  | 1766 | <p>The size of a PowerPC frame is usually fixed for the duration of a | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 1767 | function’s invocation.  Since the frame is fixed size, all references into | 
| Jim Laskey | 762b6cb | 2006-12-14 17:19:50 +0000 | [diff] [blame] | 1768 | the frame can be accessed via fixed offsets from the stack pointer.  The | 
|  | 1769 | exception to this is when dynamic alloca or variable sized arrays are present, | 
|  | 1770 | then a base pointer (r31) is used as a proxy for the stack pointer and stack | 
|  | 1771 | pointer is free to grow or shrink.  A base pointer is also used if llvm-gcc is | 
|  | 1772 | not passed the -fomit-frame-pointer flag. The stack pointer is always aligned to | 
|  | 1773 | 16 bytes, so that space allocated for altivec vectors will be properly | 
|  | 1774 | aligned.</p> | 
|  | 1775 | <p>An invocation frame is layed out as follows (low memory at top);</p> | 
|  | 1776 | </div> | 
|  | 1777 |  | 
|  | 1778 | <div class="doc_text"> | 
|  | 1779 | <table class="layout"> | 
|  | 1780 | <tr> | 
|  | 1781 | <td>Linkage<br><br></td> | 
|  | 1782 | </tr> | 
|  | 1783 | <tr> | 
|  | 1784 | <td>Parameter area<br><br></td> | 
|  | 1785 | </tr> | 
|  | 1786 | <tr> | 
|  | 1787 | <td>Dynamic area<br><br></td> | 
|  | 1788 | </tr> | 
|  | 1789 | <tr> | 
|  | 1790 | <td>Locals area<br><br></td> | 
|  | 1791 | </tr> | 
|  | 1792 | <tr> | 
|  | 1793 | <td>Saved registers area<br><br></td> | 
|  | 1794 | </tr> | 
|  | 1795 | <tr style="border-style: none hidden none hidden;"> | 
|  | 1796 | <td><br></td> | 
|  | 1797 | </tr> | 
|  | 1798 | <tr> | 
|  | 1799 | <td>Previous Frame<br><br></td> | 
|  | 1800 | </tr> | 
|  | 1801 | </table> | 
|  | 1802 | </div> | 
|  | 1803 |  | 
|  | 1804 | <div class="doc_text"> | 
|  | 1805 | <p>The <i>linkage</i> area is used by a callee to save special registers prior | 
|  | 1806 | to allocating its own frame.  Only three entries are relevant to LLVM. The | 
|  | 1807 | first entry is the previous stack pointer (sp), aka link.  This allows probing | 
|  | 1808 | tools like gdb or exception handlers to quickly scan the frames in the stack.  A | 
|  | 1809 | function epilog can also use the link to pop the frame from the stack.  The | 
|  | 1810 | third entry in the linkage area is used to save the return address from the lr | 
|  | 1811 | register. Finally, as mentioned above, the last entry is used to save the | 
|  | 1812 | previous frame pointer (r31.)  The entries in the linkage area are the size of a | 
|  | 1813 | GPR, thus the linkage area is 24 bytes long in 32 bit mode and 48 bytes in 64 | 
|  | 1814 | bit mode.</p> | 
|  | 1815 | </div> | 
|  | 1816 |  | 
|  | 1817 | <div class="doc_text"> | 
|  | 1818 | <p>32 bit linkage area</p> | 
|  | 1819 | <table class="layout"> | 
|  | 1820 | <tr> | 
|  | 1821 | <td>0</td> | 
|  | 1822 | <td>Saved SP (r1)</td> | 
|  | 1823 | </tr> | 
|  | 1824 | <tr> | 
|  | 1825 | <td>4</td> | 
|  | 1826 | <td>Saved CR</td> | 
|  | 1827 | </tr> | 
|  | 1828 | <tr> | 
|  | 1829 | <td>8</td> | 
|  | 1830 | <td>Saved LR</td> | 
|  | 1831 | </tr> | 
|  | 1832 | <tr> | 
|  | 1833 | <td>12</td> | 
|  | 1834 | <td>Reserved</td> | 
|  | 1835 | </tr> | 
|  | 1836 | <tr> | 
|  | 1837 | <td>16</td> | 
|  | 1838 | <td>Reserved</td> | 
|  | 1839 | </tr> | 
|  | 1840 | <tr> | 
|  | 1841 | <td>20</td> | 
|  | 1842 | <td>Saved FP (r31)</td> | 
|  | 1843 | </tr> | 
|  | 1844 | </table> | 
|  | 1845 | </div> | 
|  | 1846 |  | 
|  | 1847 | <div class="doc_text"> | 
|  | 1848 | <p>64 bit linkage area</p> | 
|  | 1849 | <table class="layout"> | 
|  | 1850 | <tr> | 
|  | 1851 | <td>0</td> | 
|  | 1852 | <td>Saved SP (r1)</td> | 
|  | 1853 | </tr> | 
|  | 1854 | <tr> | 
|  | 1855 | <td>8</td> | 
|  | 1856 | <td>Saved CR</td> | 
|  | 1857 | </tr> | 
|  | 1858 | <tr> | 
|  | 1859 | <td>16</td> | 
|  | 1860 | <td>Saved LR</td> | 
|  | 1861 | </tr> | 
|  | 1862 | <tr> | 
|  | 1863 | <td>24</td> | 
|  | 1864 | <td>Reserved</td> | 
|  | 1865 | </tr> | 
|  | 1866 | <tr> | 
|  | 1867 | <td>32</td> | 
|  | 1868 | <td>Reserved</td> | 
|  | 1869 | </tr> | 
|  | 1870 | <tr> | 
|  | 1871 | <td>40</td> | 
|  | 1872 | <td>Saved FP (r31)</td> | 
|  | 1873 | </tr> | 
|  | 1874 | </table> | 
|  | 1875 | </div> | 
|  | 1876 |  | 
|  | 1877 | <div class="doc_text"> | 
|  | 1878 | <p>The <i>parameter area</i> is used to store arguments being passed to a callee | 
|  | 1879 | function.  Following the PowerPC ABI, the first few arguments are actually | 
|  | 1880 | passed in registers, with the space in the parameter area unused.  However, if | 
|  | 1881 | there are not enough registers or the callee is a thunk or vararg function, | 
|  | 1882 | these register arguments can be spilled into the parameter area.  Thus, the | 
|  | 1883 | parameter area must be large enough to store all the parameters for the largest | 
|  | 1884 | call sequence made by the caller.  The size must also be mimimally large enough | 
|  | 1885 | to spill registers r3-r10.  This allows callees blind to the call signature, | 
|  | 1886 | such as thunks and vararg functions, enough space to cache the argument | 
|  | 1887 | registers.  Therefore, the parameter area is minimally 32 bytes (64 bytes in 64 | 
|  | 1888 | bit mode.)  Also note that since the parameter area is a fixed offset from the | 
|  | 1889 | top of the frame, that a callee can access its spilt arguments using fixed | 
|  | 1890 | offsets from the stack pointer (or base pointer.)</p> | 
|  | 1891 | </div> | 
|  | 1892 |  | 
|  | 1893 | <div class="doc_text"> | 
|  | 1894 | <p>Combining the information about the linkage, parameter areas and alignment. A | 
|  | 1895 | stack frame is minimally 64 bytes in 32 bit mode and 128 bytes in 64 bit | 
|  | 1896 | mode.</p> | 
|  | 1897 | </div> | 
|  | 1898 |  | 
|  | 1899 | <div class="doc_text"> | 
|  | 1900 | <p>The <i>dynamic area</i> starts out as size zero.  If a function uses dynamic | 
|  | 1901 | alloca then space is added to the stack, the linkage and parameter areas are | 
|  | 1902 | shifted to top of stack, and the new space is available immediately below the | 
|  | 1903 | linkage and parameter areas.  The cost of shifting the linkage and parameter | 
|  | 1904 | areas is minor since only the link value needs to be copied.  The link value can | 
|  | 1905 | be easily fetched by adding the original frame size to the base pointer.  Note | 
|  | 1906 | that allocations in the dynamic space need to observe 16 byte aligment.</p> | 
|  | 1907 | </div> | 
|  | 1908 |  | 
|  | 1909 | <div class="doc_text"> | 
|  | 1910 | <p>The <i>locals area</i> is where the llvm compiler reserves space for local | 
|  | 1911 | variables.</p> | 
|  | 1912 | </div> | 
|  | 1913 |  | 
|  | 1914 | <div class="doc_text"> | 
|  | 1915 | <p>The <i>saved registers area</i> is where the llvm compiler spills callee saved | 
|  | 1916 | registers on entry to the callee.</p> | 
|  | 1917 | </div> | 
|  | 1918 |  | 
|  | 1919 | <!-- _______________________________________________________________________ --> | 
|  | 1920 | <div class="doc_subsubsection"> | 
|  | 1921 | <a name="ppc_prolog">Prolog/Epilog</a> | 
|  | 1922 | </div> | 
|  | 1923 |  | 
|  | 1924 | <div class="doc_text"> | 
|  | 1925 | <p>The llvm prolog and epilog are the same as described in the PowerPC ABI, with | 
|  | 1926 | the following exceptions.  Callee saved registers are spilled after the frame is | 
|  | 1927 | created.  This allows the llvm epilog/prolog support to be common with other | 
|  | 1928 | targets.  The base pointer callee saved register r31 is saved in the TOC slot of | 
|  | 1929 | linkage area.  This simplifies allocation of space for the base pointer and | 
|  | 1930 | makes it convenient to locate programatically and during debugging.</p> | 
|  | 1931 | </div> | 
|  | 1932 |  | 
|  | 1933 | <!-- _______________________________________________________________________ --> | 
|  | 1934 | <div class="doc_subsubsection"> | 
|  | 1935 | <a name="ppc_dynamic">Dynamic Allocation</a> | 
|  | 1936 | </div> | 
|  | 1937 |  | 
|  | 1938 | <div class="doc_text"> | 
|  | 1939 | <p></p> | 
|  | 1940 | </div> | 
|  | 1941 |  | 
| Jim Laskey | b744c25 | 2006-12-15 10:40:48 +0000 | [diff] [blame] | 1942 | <div class="doc_text"> | 
|  | 1943 | <p><i>TODO - More to come.</i></p> | 
|  | 1944 | </div> | 
| Jim Laskey | 762b6cb | 2006-12-14 17:19:50 +0000 | [diff] [blame] | 1945 |  | 
|  | 1946 |  | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 1947 | <!-- *********************************************************************** --> | 
|  | 1948 | <hr> | 
|  | 1949 | <address> | 
|  | 1950 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img | 
|  | 1951 | src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a> | 
|  | 1952 | <a href="http://validator.w3.org/check/referer"><img | 
|  | 1953 | src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a> | 
|  | 1954 |  | 
|  | 1955 | <a href="mailto:sabre@nondot.org">Chris Lattner</a><br> | 
| Reid Spencer | 05fe4b0 | 2006-03-14 05:39:39 +0000 | [diff] [blame] | 1956 | <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br> | 
| Chris Lattner | ce52b7e | 2004-06-01 06:48:00 +0000 | [diff] [blame] | 1957 | Last modified: $Date$ | 
|  | 1958 | </address> | 
|  | 1959 |  | 
|  | 1960 | </body> | 
|  | 1961 | </html> |