Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVMTargetMachine class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Target/TargetMachine.h" |
| 15 | #include "llvm/PassManager.h" |
| 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Assembly/PrintModulePass.h" |
| 18 | #include "llvm/Analysis/LoopPass.h" |
| 19 | #include "llvm/CodeGen/Passes.h" |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/Collector.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetOptions.h" |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetAsmInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, |
| 28 | cl::desc("Print LLVM IR produced by the loop-reduce pass")); |
| 29 | static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, |
| 30 | cl::desc("Print LLVM IR input to isel pass")); |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 31 | static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden, |
| 32 | cl::desc("Dump emitter generated instructions as assembly")); |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 33 | static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, |
| 34 | cl::desc("Dump garbage collector data")); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 3988226 | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 36 | // Hidden options to help debugging |
| 37 | static cl::opt<bool> |
| 38 | EnableSinking("enable-sinking", cl::init(false), cl::Hidden, |
| 39 | cl::desc("Perform sinking on machine code")); |
Bill Wendling | 4aab7ae | 2008-01-04 08:11:03 +0000 | [diff] [blame] | 40 | static cl::opt<bool> |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 41 | EnableStackColoring("stack-coloring", |
Evan Cheng | 557d799 | 2008-06-04 18:09:20 +0000 | [diff] [blame] | 42 | cl::init(false), cl::Hidden, |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 43 | cl::desc("Perform stack slot coloring")); |
Evan Cheng | 7e29ba0 | 2008-02-28 23:29:57 +0000 | [diff] [blame] | 44 | static cl::opt<bool> |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 45 | EnableLICM("machine-licm", |
| 46 | cl::init(false), cl::Hidden, |
| 47 | cl::desc("Perform loop-invariant code motion on machine code")); |
Chris Lattner | 3988226 | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 48 | |
Chris Lattner | e06d8eb | 2008-01-14 19:00:06 +0000 | [diff] [blame] | 49 | // When this works it will be on by default. |
| 50 | static cl::opt<bool> |
| 51 | DisablePostRAScheduler("disable-post-RA-scheduler", |
| 52 | cl::desc("Disable scheduling after register allocation"), |
| 53 | cl::init(true)); |
| 54 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | FileModel::Model |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 56 | LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | std::ostream &Out, |
| 58 | CodeGenFileType FileType, |
| 59 | bool Fast) { |
| 60 | // Standard LLVM-Level Passes. |
| 61 | |
| 62 | // Run loop strength reduction before anything else. |
| 63 | if (!Fast) { |
| 64 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 65 | if (PrintLSR) |
Dan Gohman | 35a5415 | 2008-03-25 21:38:12 +0000 | [diff] [blame] | 66 | PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 69 | PM.add(createGCLoweringPass()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 71 | if (!getTargetAsmInfo()->doesSupportExceptionHandling()) |
Dale Johannesen | 748a85c | 2008-04-01 20:00:57 +0000 | [diff] [blame] | 72 | PM.add(createLowerInvokePass(getTargetLowering())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | |
| 74 | // Make sure that no unreachable blocks are instruction selected. |
| 75 | PM.add(createUnreachableBlockEliminationPass()); |
| 76 | |
| 77 | if (!Fast) |
| 78 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 79 | |
| 80 | if (PrintISelInput) |
Dan Gohman | 35a5415 | 2008-03-25 21:38:12 +0000 | [diff] [blame] | 81 | PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | &cerr)); |
| 83 | |
| 84 | // Ask the target for an isel. |
| 85 | if (addInstSelector(PM, Fast)) |
| 86 | return FileModel::Error; |
| 87 | |
| 88 | // Print the instruction selected machine code... |
| 89 | if (PrintMachineCode) |
| 90 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | b958b0d | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 91 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 92 | if (EnableLICM) |
Bill Wendling | 4aab7ae | 2008-01-04 08:11:03 +0000 | [diff] [blame] | 93 | PM.add(createMachineLICMPass()); |
Chris Lattner | 3988226 | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 94 | |
| 95 | if (EnableSinking) |
| 96 | PM.add(createMachineSinkingPass()); |
Bill Wendling | b958b0d | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 97 | |
Anton Korobeynikov | 339d245 | 2008-04-23 18:22:28 +0000 | [diff] [blame] | 98 | // Run pre-ra passes. |
| 99 | if (addPreRegAlloc(PM, Fast) && PrintMachineCode) |
| 100 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 101 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 102 | // Perform register allocation to convert to a concrete x86 representation |
| 103 | PM.add(createRegisterAllocator()); |
| 104 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 105 | // Perform stack slot coloring. |
| 106 | if (EnableStackColoring) |
| 107 | PM.add(createStackSlotColoringPass()); |
| 108 | |
| 109 | if (PrintMachineCode) // Print the register-allocated code |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 111 | |
| 112 | // Run post-ra passes. |
| 113 | if (addPostRegAlloc(PM, Fast) && PrintMachineCode) |
| 114 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 115 | |
Christopher Lamb | ed37973 | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 116 | PM.add(createLowerSubregsPass()); |
| 117 | |
| 118 | if (PrintMachineCode) // Print the subreg lowered code |
| 119 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 121 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 122 | PM.add(createPrologEpilogCodeInserter()); |
| 123 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 124 | if (PrintMachineCode) |
| 125 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 126 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | // Second pass scheduler. |
Chris Lattner | e06d8eb | 2008-01-14 19:00:06 +0000 | [diff] [blame] | 128 | if (!Fast && !DisablePostRAScheduler) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | PM.add(createPostRAScheduler()); |
| 130 | |
| 131 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
| 132 | if (!Fast) |
| 133 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Bill Wendling | b958b0d | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 134 | |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 135 | PM.add(createGCMachineCodeAnalysisPass()); |
| 136 | if (PrintMachineCode) |
| 137 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 138 | |
| 139 | if (PrintGCInfo) |
| 140 | PM.add(createCollectorMetadataPrinter(*cerr)); |
| 141 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | // Fold redundant debug labels. |
| 143 | PM.add(createDebugLabelFoldingPass()); |
| 144 | |
| 145 | if (PrintMachineCode) // Print the register-allocated code |
| 146 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 147 | |
| 148 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
| 149 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 150 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 151 | if (!Fast && !OptimizeForSize) |
Evan Cheng | 7e29ba0 | 2008-02-28 23:29:57 +0000 | [diff] [blame] | 152 | PM.add(createLoopAlignerPass()); |
| 153 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 154 | switch (FileType) { |
| 155 | default: |
| 156 | break; |
| 157 | case TargetMachine::AssemblyFile: |
| 158 | if (addAssemblyEmitter(PM, Fast, Out)) |
| 159 | return FileModel::Error; |
| 160 | return FileModel::AsmFile; |
| 161 | case TargetMachine::ObjectFile: |
| 162 | if (getMachOWriterInfo()) |
| 163 | return FileModel::MachOFile; |
| 164 | else if (getELFWriterInfo()) |
| 165 | return FileModel::ElfFile; |
| 166 | } |
| 167 | |
| 168 | return FileModel::Error; |
| 169 | } |
| 170 | |
| 171 | /// addPassesToEmitFileFinish - If the passes to emit the specified file had to |
| 172 | /// be split up (e.g., to add an object writer pass), this method can be used to |
| 173 | /// finish up adding passes to emit the file, if necessary. |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 174 | bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | MachineCodeEmitter *MCE, |
| 176 | bool Fast) { |
| 177 | if (MCE) |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 178 | addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE); |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 179 | |
| 180 | PM.add(createCollectorMetadataDeleter()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 181 | |
| 182 | // Delete machine code for this function |
| 183 | PM.add(createMachineCodeDeleter()); |
| 184 | |
| 185 | return false; // success! |
| 186 | } |
| 187 | |
| 188 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to |
| 189 | /// get machine code emitted. This uses a MachineCodeEmitter object to handle |
| 190 | /// actually outputting the machine code and resolving things like the address |
| 191 | /// of functions. This method should returns true if machine code emission is |
| 192 | /// not supported. |
| 193 | /// |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 194 | bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 195 | MachineCodeEmitter &MCE, |
| 196 | bool Fast) { |
| 197 | // Standard LLVM-Level Passes. |
| 198 | |
| 199 | // Run loop strength reduction before anything else. |
| 200 | if (!Fast) { |
| 201 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 202 | if (PrintLSR) |
Dan Gohman | 35a5415 | 2008-03-25 21:38:12 +0000 | [diff] [blame] | 203 | PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 206 | PM.add(createGCLoweringPass()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 207 | |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 208 | if (!getTargetAsmInfo()->doesSupportExceptionHandling()) |
Dale Johannesen | 748a85c | 2008-04-01 20:00:57 +0000 | [diff] [blame] | 209 | PM.add(createLowerInvokePass(getTargetLowering())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | |
| 211 | // Make sure that no unreachable blocks are instruction selected. |
| 212 | PM.add(createUnreachableBlockEliminationPass()); |
| 213 | |
| 214 | if (!Fast) |
| 215 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 216 | |
| 217 | if (PrintISelInput) |
Dan Gohman | 35a5415 | 2008-03-25 21:38:12 +0000 | [diff] [blame] | 218 | PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 219 | &cerr)); |
| 220 | |
| 221 | // Ask the target for an isel. |
| 222 | if (addInstSelector(PM, Fast)) |
| 223 | return true; |
| 224 | |
| 225 | // Print the instruction selected machine code... |
| 226 | if (PrintMachineCode) |
| 227 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | b958b0d | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 228 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 229 | if (EnableLICM) |
Bill Wendling | 4aab7ae | 2008-01-04 08:11:03 +0000 | [diff] [blame] | 230 | PM.add(createMachineLICMPass()); |
Chris Lattner | a132dd4 | 2008-01-05 06:14:16 +0000 | [diff] [blame] | 231 | |
| 232 | if (EnableSinking) |
| 233 | PM.add(createMachineSinkingPass()); |
Bill Wendling | b958b0d | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 234 | |
Anton Korobeynikov | 9cba34c | 2008-04-23 18:26:03 +0000 | [diff] [blame] | 235 | // Run pre-ra passes. |
| 236 | if (addPreRegAlloc(PM, Fast) && PrintMachineCode) |
| 237 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 238 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 239 | // Perform register allocation. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 240 | PM.add(createRegisterAllocator()); |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 241 | |
| 242 | // Perform stack slot coloring. |
| 243 | if (EnableStackColoring) |
| 244 | PM.add(createStackSlotColoringPass()); |
| 245 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | if (PrintMachineCode) |
| 247 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Christopher Lamb | ed37973 | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 248 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 249 | // Run post-ra passes. |
| 250 | if (addPostRegAlloc(PM, Fast) && PrintMachineCode) |
| 251 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 252 | |
| 253 | if (PrintMachineCode) // Print the register-allocated code |
| 254 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 255 | |
Christopher Lamb | ed37973 | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 256 | PM.add(createLowerSubregsPass()); |
| 257 | |
| 258 | if (PrintMachineCode) // Print the subreg lowered code |
| 259 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 261 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 262 | PM.add(createPrologEpilogCodeInserter()); |
| 263 | |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 264 | if (PrintMachineCode) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 265 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 266 | |
| 267 | // Second pass scheduler. |
| 268 | if (!Fast) |
| 269 | PM.add(createPostRAScheduler()); |
| 270 | |
| 271 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
| 272 | if (!Fast) |
| 273 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Bill Wendling | b958b0d | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 274 | |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 275 | PM.add(createGCMachineCodeAnalysisPass()); |
Evan Cheng | 14f8a50 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 276 | |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 277 | if (PrintMachineCode) |
| 278 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 279 | |
| 280 | if (PrintGCInfo) |
| 281 | PM.add(createCollectorMetadataPrinter(*cerr)); |
| 282 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 283 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
| 284 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 285 | |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 286 | addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 287 | |
Gordon Henriksen | 3646477 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 288 | PM.add(createCollectorMetadataDeleter()); |
| 289 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 290 | // Delete machine code for this function |
| 291 | PM.add(createMachineCodeDeleter()); |
| 292 | |
| 293 | return false; // success! |
| 294 | } |