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