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" |
Daniel Dunbar | 7894578 | 2009-08-13 23:48:47 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.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" |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 6d823cd | 2009-07-15 23:48:37 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegistry.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Dan Gohman | 2c4bf11 | 2008-09-25 01:14:49 +0000 | [diff] [blame] | 30 | namespace llvm { |
| 31 | bool EnableFastISel; |
| 32 | } |
| 33 | |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 34 | static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden, |
| 35 | cl::desc("Disable Post Regalloc")); |
| 36 | static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden, |
| 37 | cl::desc("Disable branch folding")); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 38 | static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden, |
| 39 | cl::desc("Disable tail duplication")); |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 40 | static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden, |
| 41 | cl::desc("Disable code placement")); |
| 42 | static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden, |
| 43 | cl::desc("Disable Stack Slot Coloring")); |
| 44 | static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden, |
| 45 | cl::desc("Disable Machine LICM")); |
| 46 | static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden, |
| 47 | cl::desc("Disable Machine Sinking")); |
| 48 | static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden, |
| 49 | cl::desc("Disable Loop Strength Reduction Pass")); |
| 50 | static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden, |
| 51 | cl::desc("Disable Codegen Prepare")); |
Chris Lattner | 85ef254 | 2007-06-19 05:47:49 +0000 | [diff] [blame] | 52 | static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, |
| 53 | cl::desc("Print LLVM IR produced by the loop-reduce pass")); |
| 54 | static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, |
| 55 | cl::desc("Print LLVM IR input to isel pass")); |
Evan Cheng | 8bd6035 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 56 | static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden, |
| 57 | cl::desc("Dump emitter generated instructions as assembly")); |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 58 | static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, |
| 59 | cl::desc("Dump garbage collector data")); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 60 | static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden, |
| 61 | cl::desc("Verify generated machine code"), |
| 62 | cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL)); |
Chris Lattner | 85ef254 | 2007-06-19 05:47:49 +0000 | [diff] [blame] | 63 | |
Dan Gohman | dc75685 | 2008-10-01 20:39:19 +0000 | [diff] [blame] | 64 | // Enable or disable FastISel. Both options are needed, because |
| 65 | // FastISel is enabled by default with -fast, and we wish to be |
Dan Gohman | e29fea4 | 2009-08-26 15:57:57 +0000 | [diff] [blame] | 66 | // able to enable or disable fast-isel independently from -O0. |
Dan Gohman | eb0d6ab | 2008-10-07 23:00:56 +0000 | [diff] [blame] | 67 | static cl::opt<cl::boolOrDefault> |
Dan Gohman | dc75685 | 2008-10-01 20:39:19 +0000 | [diff] [blame] | 68 | EnableFastISelOption("fast-isel", cl::Hidden, |
Dan Gohman | e29fea4 | 2009-08-26 15:57:57 +0000 | [diff] [blame] | 69 | cl::desc("Enable the \"fast\" instruction selector")); |
Dan Gohman | 2c4bf11 | 2008-09-25 01:14:49 +0000 | [diff] [blame] | 70 | |
Dan Gohman | 2e7e948 | 2009-11-20 02:03:44 +0000 | [diff] [blame] | 71 | // Enable or disable an experimental optimization to split GEPs |
| 72 | // and run a special GVN pass which does not examine loads, in |
| 73 | // an effort to factor out redundancy implicit in complex GEPs. |
| 74 | static cl::opt<bool> EnableSplitGEPGVN("split-gep-gvn", cl::Hidden, |
| 75 | cl::desc("Split GEPs and run no-load GVN")); |
Chris Lattner | a7ac47c | 2009-08-12 07:22:17 +0000 | [diff] [blame] | 76 | |
| 77 | LLVMTargetMachine::LLVMTargetMachine(const Target &T, |
| 78 | const std::string &TargetTriple) |
| 79 | : TargetMachine(T) { |
| 80 | AsmInfo = T.createAsmInfo(TargetTriple); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 85 | FileModel::Model |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 86 | LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 87 | formatted_raw_ostream &Out, |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 88 | CodeGenFileType FileType, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 89 | CodeGenOpt::Level OptLevel) { |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 90 | // Add common CodeGen passes. |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 91 | if (addCommonCodeGenPasses(PM, OptLevel)) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 92 | return FileModel::Error; |
| 93 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 94 | switch (FileType) { |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 95 | default: |
| 96 | break; |
| 97 | case TargetMachine::AssemblyFile: |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 98 | if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out)) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 99 | return FileModel::Error; |
| 100 | return FileModel::AsmFile; |
| 101 | case TargetMachine::ObjectFile: |
| 102 | if (getMachOWriterInfo()) |
| 103 | return FileModel::MachOFile; |
| 104 | else if (getELFWriterInfo()) |
| 105 | return FileModel::ElfFile; |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 106 | } |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 107 | |
| 108 | return FileModel::Error; |
| 109 | } |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | 5d77cad | 2009-07-15 23:34:19 +0000 | [diff] [blame] | 111 | bool LLVMTargetMachine::addAssemblyEmitter(PassManagerBase &PM, |
| 112 | CodeGenOpt::Level OptLevel, |
| 113 | bool Verbose, |
| 114 | formatted_raw_ostream &Out) { |
Daniel Dunbar | 67d894e | 2009-08-13 19:38:51 +0000 | [diff] [blame] | 115 | FunctionPass *Printer = |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 116 | getTarget().createAsmPrinter(Out, *this, getMCAsmInfo(), Verbose); |
Daniel Dunbar | 5d77cad | 2009-07-15 23:34:19 +0000 | [diff] [blame] | 117 | if (!Printer) |
Daniel Dunbar | 36129db | 2009-07-15 23:54:01 +0000 | [diff] [blame] | 118 | return true; |
| 119 | |
Daniel Dunbar | 5d77cad | 2009-07-15 23:34:19 +0000 | [diff] [blame] | 120 | PM.add(Printer); |
| 121 | return false; |
| 122 | } |
| 123 | |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 124 | /// addPassesToEmitFileFinish - If the passes to emit the specified file had to |
| 125 | /// be split up (e.g., to add an object writer pass), this method can be used to |
| 126 | /// finish up adding passes to emit the file, if necessary. |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 127 | bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 128 | MachineCodeEmitter *MCE, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 129 | CodeGenOpt::Level OptLevel) { |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 130 | if (MCE) |
Daniel Dunbar | cfe9a60 | 2009-07-15 22:33:19 +0000 | [diff] [blame] | 131 | addSimpleCodeEmitter(PM, OptLevel, *MCE); |
| 132 | if (PrintEmittedAsm) |
| 133 | addAssemblyEmitter(PM, OptLevel, true, ferrs()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 134 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 135 | PM.add(createGCInfoDeleter()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 137 | return false; // success! |
| 138 | } |
| 139 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 140 | /// addPassesToEmitFileFinish - If the passes to emit the specified file had to |
| 141 | /// be split up (e.g., to add an object writer pass), this method can be used to |
| 142 | /// finish up adding passes to emit the file, if necessary. |
| 143 | bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, |
| 144 | JITCodeEmitter *JCE, |
| 145 | CodeGenOpt::Level OptLevel) { |
| 146 | if (JCE) |
Daniel Dunbar | cfe9a60 | 2009-07-15 22:33:19 +0000 | [diff] [blame] | 147 | addSimpleCodeEmitter(PM, OptLevel, *JCE); |
| 148 | if (PrintEmittedAsm) |
| 149 | addAssemblyEmitter(PM, OptLevel, true, ferrs()); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 150 | |
| 151 | PM.add(createGCInfoDeleter()); |
| 152 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 153 | return false; // success! |
| 154 | } |
| 155 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 156 | /// addPassesToEmitFileFinish - If the passes to emit the specified file had to |
| 157 | /// be split up (e.g., to add an object writer pass), this method can be used to |
| 158 | /// finish up adding passes to emit the file, if necessary. |
| 159 | bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, |
| 160 | ObjectCodeEmitter *OCE, |
| 161 | CodeGenOpt::Level OptLevel) { |
| 162 | if (OCE) |
Daniel Dunbar | cfe9a60 | 2009-07-15 22:33:19 +0000 | [diff] [blame] | 163 | addSimpleCodeEmitter(PM, OptLevel, *OCE); |
| 164 | if (PrintEmittedAsm) |
| 165 | addAssemblyEmitter(PM, OptLevel, true, ferrs()); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 166 | |
| 167 | PM.add(createGCInfoDeleter()); |
| 168 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 169 | return false; // success! |
| 170 | } |
| 171 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 172 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to |
| 173 | /// get machine code emitted. This uses a MachineCodeEmitter object to handle |
| 174 | /// actually outputting the machine code and resolving things like the address |
| 175 | /// of functions. This method should returns true if machine code emission is |
| 176 | /// not supported. |
| 177 | /// |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 178 | bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM, |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 179 | MachineCodeEmitter &MCE, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 180 | CodeGenOpt::Level OptLevel) { |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 181 | // Add common CodeGen passes. |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 182 | if (addCommonCodeGenPasses(PM, OptLevel)) |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 183 | return true; |
| 184 | |
Daniel Dunbar | cfe9a60 | 2009-07-15 22:33:19 +0000 | [diff] [blame] | 185 | addCodeEmitter(PM, OptLevel, MCE); |
| 186 | if (PrintEmittedAsm) |
| 187 | addAssemblyEmitter(PM, OptLevel, true, ferrs()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 188 | |
| 189 | PM.add(createGCInfoDeleter()); |
| 190 | |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 191 | return false; // success! |
| 192 | } |
| 193 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 194 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to |
| 195 | /// get machine code emitted. This uses a MachineCodeEmitter object to handle |
| 196 | /// actually outputting the machine code and resolving things like the address |
| 197 | /// of functions. This method should returns true if machine code emission is |
| 198 | /// not supported. |
| 199 | /// |
| 200 | bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM, |
| 201 | JITCodeEmitter &JCE, |
| 202 | CodeGenOpt::Level OptLevel) { |
| 203 | // Add common CodeGen passes. |
| 204 | if (addCommonCodeGenPasses(PM, OptLevel)) |
| 205 | return true; |
| 206 | |
Daniel Dunbar | cfe9a60 | 2009-07-15 22:33:19 +0000 | [diff] [blame] | 207 | addCodeEmitter(PM, OptLevel, JCE); |
| 208 | if (PrintEmittedAsm) |
| 209 | addAssemblyEmitter(PM, OptLevel, true, ferrs()); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 210 | |
| 211 | PM.add(createGCInfoDeleter()); |
| 212 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 213 | return false; // success! |
| 214 | } |
| 215 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 216 | static void printAndVerify(PassManagerBase &PM, |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 217 | const char *Banner, |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 218 | bool allowDoubleDefs = false) { |
| 219 | if (PrintMachineCode) |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 220 | PM.add(createMachineFunctionPrinterPass(errs(), Banner)); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 221 | |
| 222 | if (VerifyMachineCode) |
| 223 | PM.add(createMachineVerifierPass(allowDoubleDefs)); |
| 224 | } |
| 225 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 226 | /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both |
| 227 | /// emitting to assembly files or machine code output. |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 228 | /// |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 229 | bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 230 | CodeGenOpt::Level OptLevel) { |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 231 | // Standard LLVM-Level Passes. |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 232 | |
Dan Gohman | 2e7e948 | 2009-11-20 02:03:44 +0000 | [diff] [blame] | 233 | // Optionally, tun split-GEPs and no-load GVN. |
| 234 | if (EnableSplitGEPGVN) { |
| 235 | PM.add(createGEPSplitterPass()); |
| 236 | PM.add(createGVNPass(/*NoPRE=*/false, /*NoLoads=*/true)); |
| 237 | } |
| 238 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 239 | // Run loop strength reduction before anything else. |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 240 | if (OptLevel != CodeGenOpt::None && !DisableLSR) { |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 241 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 242 | if (PrintLSR) |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 243 | PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs())); |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 244 | } |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 245 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 246 | // Turn exception handling constructs into something the code generators can |
| 247 | // handle. |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 248 | switch (getMCAsmInfo()->getExceptionHandlingType()) |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 249 | { |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 250 | case ExceptionHandling::SjLj: |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 251 | // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 252 | PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None)); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 253 | PM.add(createSjLjEHPass(getTargetLowering())); |
| 254 | break; |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 255 | case ExceptionHandling::Dwarf: |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 256 | PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None)); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 257 | break; |
| 258 | case ExceptionHandling::None: |
| 259 | PM.add(createLowerInvokePass(getTargetLowering())); |
| 260 | break; |
| 261 | } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 262 | |
| 263 | PM.add(createGCLoweringPass()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 265 | // Make sure that no unreachable blocks are instruction selected. |
| 266 | PM.add(createUnreachableBlockEliminationPass()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 267 | |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 268 | if (OptLevel != CodeGenOpt::None && !DisableCGP) |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 269 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 270 | |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 271 | PM.add(createStackProtectorPass(getTargetLowering())); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 272 | |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 273 | if (PrintISelInput) |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 274 | PM.add(createPrintFunctionPass("\n\n" |
| 275 | "*** Final LLVM Code input to ISel ***\n", |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 276 | &errs())); |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 277 | |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 278 | // Standard Lower-Level Passes. |
| 279 | |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 280 | // Set up a MachineFunction for the rest of CodeGen to work on. |
| 281 | PM.add(new MachineFunctionAnalysis(*this, OptLevel)); |
| 282 | |
Dan Gohman | dc75685 | 2008-10-01 20:39:19 +0000 | [diff] [blame] | 283 | // Enable FastISel with -fast, but allow that to be overridden. |
Dan Gohman | eb0d6ab | 2008-10-07 23:00:56 +0000 | [diff] [blame] | 284 | if (EnableFastISelOption == cl::BOU_TRUE || |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 285 | (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE)) |
Dan Gohman | dc75685 | 2008-10-01 20:39:19 +0000 | [diff] [blame] | 286 | EnableFastISel = true; |
| 287 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 288 | // Ask the target for an isel. |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 289 | if (addInstSelector(PM, OptLevel)) |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 290 | return true; |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 292 | // Print the instruction selected machine code... |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 293 | printAndVerify(PM, "After Instruction Selection", |
| 294 | /* allowDoubleDefs= */ true); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 295 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 296 | if (OptLevel != CodeGenOpt::None) { |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 297 | if (!DisableMachineLICM) |
| 298 | PM.add(createMachineLICMPass()); |
| 299 | if (!DisableMachineSink) |
| 300 | PM.add(createMachineSinkingPass()); |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 301 | printAndVerify(PM, "After MachineLICM and MachineSinking", |
| 302 | /* allowDoubleDefs= */ true); |
Evan Cheng | 8f0d99e | 2009-02-09 08:45:39 +0000 | [diff] [blame] | 303 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 304 | |
Anton Korobeynikov | b013f50 | 2008-04-23 18:26:03 +0000 | [diff] [blame] | 305 | // Run pre-ra passes. |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 306 | if (addPreRegAlloc(PM, OptLevel)) |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 307 | printAndVerify(PM, "After PreRegAlloc passes", |
| 308 | /* allowDoubleDefs= */ true); |
Anton Korobeynikov | b013f50 | 2008-04-23 18:26:03 +0000 | [diff] [blame] | 309 | |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 310 | // Perform register allocation. |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 311 | PM.add(createRegisterAllocator()); |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 312 | printAndVerify(PM, "After Register Allocation"); |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 313 | |
| 314 | // Perform stack slot coloring. |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 315 | if (OptLevel != CodeGenOpt::None && !DisableSSC) { |
Evan Cheng | 6248fa4 | 2009-08-05 07:26:17 +0000 | [diff] [blame] | 316 | // FIXME: Re-enable coloring with register when it's capable of adding |
| 317 | // kill markers. |
| 318 | PM.add(createStackSlotColoringPass(false)); |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 319 | printAndVerify(PM, "After StackSlotColoring"); |
| 320 | } |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 321 | |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 322 | // Run post-ra passes. |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 323 | if (addPostRegAlloc(PM, OptLevel)) |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 324 | printAndVerify(PM, "After PostRegAlloc passes"); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 325 | |
Christopher Lamb | ada779f | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 326 | PM.add(createLowerSubregsPass()); |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 327 | printAndVerify(PM, "After LowerSubregs"); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 329 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 330 | PM.add(createPrologEpilogCodeInserter()); |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 331 | printAndVerify(PM, "After PrologEpilogCodeInserter"); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 332 | |
Evan Cheng | 629adde | 2009-09-30 08:49:50 +0000 | [diff] [blame] | 333 | // Run pre-sched2 passes. |
| 334 | if (addPreSched2(PM, OptLevel)) |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 335 | printAndVerify(PM, "After PreSched2 passes"); |
Evan Cheng | 629adde | 2009-09-30 08:49:50 +0000 | [diff] [blame] | 336 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 337 | // Second pass scheduler. |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 338 | if (OptLevel != CodeGenOpt::None && !DisablePostRA) { |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 339 | PM.add(createPostRAScheduler(OptLevel)); |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 340 | printAndVerify(PM, "After PostRAScheduler"); |
Dan Gohman | 5ce0973 | 2008-11-20 19:54:21 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Dan Gohman | 23b0d49 | 2008-12-18 01:36:42 +0000 | [diff] [blame] | 343 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 344 | if (OptLevel != CodeGenOpt::None && !DisableBranchFold) { |
Bob Wilson | a597103 | 2009-10-28 20:46:46 +0000 | [diff] [blame] | 345 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 346 | printAndVerify(PM, "After BranchFolding"); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 347 | } |
Dan Gohman | 23b0d49 | 2008-12-18 01:36:42 +0000 | [diff] [blame] | 348 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 349 | // Tail duplication. |
| 350 | if (OptLevel != CodeGenOpt::None && !DisableTailDuplicate) { |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame^] | 351 | PM.add(createTailDuplicatePass()); |
| 352 | printAndVerify(PM, "After TailDuplicate"); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 355 | PM.add(createGCMachineCodeAnalysisPass()); |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 356 | |
Gordon Henriksen | 93f96d0 | 2008-01-07 01:33:09 +0000 | [diff] [blame] | 357 | if (PrintGCInfo) |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 358 | PM.add(createGCInfoPrinter(errs())); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 359 | |
Eric Christopher | 522c01a | 2009-11-04 19:57:50 +0000 | [diff] [blame] | 360 | if (OptLevel != CodeGenOpt::None && !DisableCodePlace) { |
Dan Gohman | 499a937 | 2009-10-31 20:17:39 +0000 | [diff] [blame] | 361 | PM.add(createCodePlacementOptPass()); |
| 362 | printAndVerify(PM, "After CodePlacementOpt"); |
| 363 | } |
| 364 | |
Evan Cheng | 517e255 | 2009-11-05 01:16:59 +0000 | [diff] [blame] | 365 | if (addPreEmitPass(PM, OptLevel)) |
| 366 | printAndVerify(PM, "After PreEmit passes"); |
| 367 | |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 368 | return false; |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 369 | } |