Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 1 | //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +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" |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/PrintModulePass.h" |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Passes.h" |
Gordon Henriksen | 5a29c9e | 2008-08-17 12:56:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/GCStrategy.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetOptions.h" |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetAsmInfo.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Dan Gohman | 2c4bf11 | 2008-09-25 01:14:49 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | bool EnableFastISel; |
| 30 | } |
| 31 | |
Chris Lattner | 85ef254 | 2007-06-19 05:47:49 +0000 | [diff] [blame] | 32 | static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, |
| 33 | cl::desc("Print LLVM IR produced by the loop-reduce pass")); |
| 34 | static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, |
| 35 | cl::desc("Print LLVM IR input to isel pass")); |
Evan Cheng | 8bd6035 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 36 | static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden, |
| 37 | cl::desc("Dump emitter generated instructions as assembly")); |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 38 | static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, |
| 39 | cl::desc("Dump garbage collector data")); |
Chris Lattner | 85ef254 | 2007-06-19 05:47:49 +0000 | [diff] [blame] | 40 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 41 | // Hidden options to help debugging |
| 42 | static cl::opt<bool> |
| 43 | EnableSinking("enable-sinking", cl::init(false), cl::Hidden, |
| 44 | cl::desc("Perform sinking on machine code")); |
Bill Wendling | cc8f603 | 2008-01-04 08:11:03 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 46 | EnableLICM("machine-licm", |
| 47 | cl::init(false), cl::Hidden, |
| 48 | cl::desc("Perform loop-invariant code motion on machine code")); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 459525d | 2008-01-14 19:00:06 +0000 | [diff] [blame] | 50 | // When this works it will be on by default. |
| 51 | static cl::opt<bool> |
| 52 | DisablePostRAScheduler("disable-post-RA-scheduler", |
| 53 | cl::desc("Disable scheduling after register allocation"), |
| 54 | cl::init(true)); |
| 55 | |
Dan Gohman | 2c4bf11 | 2008-09-25 01:14:49 +0000 | [diff] [blame] | 56 | static cl::opt<bool, true> |
| 57 | FastISelOption("fast-isel", cl::Hidden, |
| 58 | cl::desc("Enable the experimental \"fast\" instruction selector"), |
| 59 | cl::location(EnableFastISel)); |
| 60 | |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 61 | FileModel::Model |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 62 | LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 63 | raw_ostream &Out, |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 64 | CodeGenFileType FileType, |
| 65 | bool Fast) { |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 66 | // Add common CodeGen passes. |
| 67 | if (addCommonCodeGenPasses(PM, Fast)) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 68 | return FileModel::Error; |
| 69 | |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 70 | // Fold redundant debug labels. |
| 71 | PM.add(createDebugLabelFoldingPass()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 72 | |
| 73 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 74 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 75 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 76 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 77 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 78 | |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 79 | if (!Fast && !OptimizeForSize) |
Evan Cheng | d703ed6 | 2008-02-28 23:29:57 +0000 | [diff] [blame] | 80 | PM.add(createLoopAlignerPass()); |
| 81 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 82 | switch (FileType) { |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 83 | default: |
| 84 | break; |
| 85 | case TargetMachine::AssemblyFile: |
| 86 | if (addAssemblyEmitter(PM, Fast, Out)) |
| 87 | return FileModel::Error; |
| 88 | return FileModel::AsmFile; |
| 89 | case TargetMachine::ObjectFile: |
| 90 | if (getMachOWriterInfo()) |
| 91 | return FileModel::MachOFile; |
| 92 | else if (getELFWriterInfo()) |
| 93 | return FileModel::ElfFile; |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 94 | } |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 95 | |
| 96 | return FileModel::Error; |
| 97 | } |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 98 | |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 99 | /// addPassesToEmitFileFinish - If the passes to emit the specified file had to |
| 100 | /// be split up (e.g., to add an object writer pass), this method can be used to |
| 101 | /// finish up adding passes to emit the file, if necessary. |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 102 | bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 103 | MachineCodeEmitter *MCE, |
| 104 | bool Fast) { |
| 105 | if (MCE) |
Evan Cheng | 8bd6035 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 106 | addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 107 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 108 | PM.add(createGCInfoDeleter()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 110 | // Delete machine code for this function |
| 111 | PM.add(createMachineCodeDeleter()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 113 | return false; // success! |
| 114 | } |
| 115 | |
| 116 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to |
| 117 | /// get machine code emitted. This uses a MachineCodeEmitter object to handle |
| 118 | /// actually outputting the machine code and resolving things like the address |
| 119 | /// of functions. This method should returns true if machine code emission is |
| 120 | /// not supported. |
| 121 | /// |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 122 | bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM, |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 123 | MachineCodeEmitter &MCE, |
| 124 | bool Fast) { |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 125 | // Add common CodeGen passes. |
| 126 | if (addCommonCodeGenPasses(PM, Fast)) |
| 127 | return true; |
| 128 | |
| 129 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
| 130 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 131 | |
| 132 | addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE); |
| 133 | |
| 134 | PM.add(createGCInfoDeleter()); |
| 135 | |
| 136 | // Delete machine code for this function |
| 137 | PM.add(createMachineCodeDeleter()); |
| 138 | |
| 139 | return false; // success! |
| 140 | } |
| 141 | |
| 142 | /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for |
| 143 | /// both emitting to assembly files or machine code output. |
| 144 | /// |
| 145 | bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) { |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 146 | // Standard LLVM-Level Passes. |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 148 | // Run loop strength reduction before anything else. |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 149 | if (!Fast) { |
| 150 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 151 | if (PrintLSR) |
Dan Gohman | 62c7b8c | 2008-03-25 21:38:12 +0000 | [diff] [blame] | 152 | PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr)); |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 153 | } |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 154 | |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 155 | PM.add(createGCLoweringPass()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 156 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 157 | if (!getTargetAsmInfo()->doesSupportExceptionHandling()) |
Dale Johannesen | b6d5b14 | 2008-04-01 20:00:57 +0000 | [diff] [blame] | 158 | PM.add(createLowerInvokePass(getTargetLowering())); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 160 | // Make sure that no unreachable blocks are instruction selected. |
| 161 | PM.add(createUnreachableBlockEliminationPass()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 162 | |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 163 | if (!Fast) |
| 164 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 165 | |
| 166 | if (PrintISelInput) |
Dan Gohman | 62c7b8c | 2008-03-25 21:38:12 +0000 | [diff] [blame] | 167 | PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n", |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 168 | &cerr)); |
| 169 | |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 170 | // Standard Lower-Level Passes. |
| 171 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 172 | // Ask the target for an isel. |
| 173 | if (addInstSelector(PM, Fast)) |
| 174 | return true; |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 176 | // Print the instruction selected machine code... |
| 177 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 178 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 179 | |
Dan Gohman | 2c4bf11 | 2008-09-25 01:14:49 +0000 | [diff] [blame] | 180 | // If we're using Fast-ISel, clean up the mess. |
| 181 | if (EnableFastISel) |
| 182 | PM.add(createDeadMachineInstructionElimPass()); |
| 183 | |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 184 | if (EnableLICM) |
Bill Wendling | cc8f603 | 2008-01-04 08:11:03 +0000 | [diff] [blame] | 185 | PM.add(createMachineLICMPass()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 3c42f12 | 2008-01-05 06:14:16 +0000 | [diff] [blame] | 187 | if (EnableSinking) |
| 188 | PM.add(createMachineSinkingPass()); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 189 | |
Anton Korobeynikov | b013f50 | 2008-04-23 18:26:03 +0000 | [diff] [blame] | 190 | // Run pre-ra passes. |
| 191 | if (addPreRegAlloc(PM, Fast) && PrintMachineCode) |
| 192 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 193 | |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 194 | // Perform register allocation. |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 195 | PM.add(createRegisterAllocator()); |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 196 | |
| 197 | // Perform stack slot coloring. |
Evan Cheng | 9ef4c53 | 2008-06-30 22:33:16 +0000 | [diff] [blame] | 198 | if (!Fast) |
| 199 | PM.add(createStackSlotColoringPass()); |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 200 | |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 201 | if (PrintMachineCode) // Print the register-allocated code |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 202 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 203 | |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 204 | // Run post-ra passes. |
| 205 | if (addPostRegAlloc(PM, Fast) && PrintMachineCode) |
| 206 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 207 | |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 208 | if (PrintMachineCode) |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 209 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 210 | |
Christopher Lamb | ada779f | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 211 | PM.add(createLowerSubregsPass()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 212 | |
Christopher Lamb | ada779f | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 213 | if (PrintMachineCode) // Print the subreg lowered code |
| 214 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 216 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 217 | PM.add(createPrologEpilogCodeInserter()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 218 | |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 219 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 220 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 221 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 222 | // Second pass scheduler. |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 223 | if (!Fast && !DisablePostRAScheduler) |
Dale Johannesen | 72f1596 | 2007-07-13 17:31:29 +0000 | [diff] [blame] | 224 | PM.add(createPostRAScheduler()); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 225 | |
Chris Lattner | e01eaa0 | 2006-11-16 01:00:07 +0000 | [diff] [blame] | 226 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
| 227 | if (!Fast) |
Dale Johannesen | e6e4354 | 2007-05-22 18:31:04 +0000 | [diff] [blame] | 228 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 229 | |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 230 | PM.add(createGCMachineCodeAnalysisPass()); |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 231 | |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 232 | if (PrintMachineCode) |
| 233 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 234 | |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 235 | if (PrintGCInfo) |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 236 | PM.add(createGCInfoPrinter(*cerr)); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 237 | |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 238 | return false; |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 239 | } |