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