Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 1 | //===-- PTXTargetMachine.cpp - Define TargetMachine for PTX ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Top-level implementation for the PTX target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PTX.h" |
| 15 | #include "PTXTargetMachine.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 16 | #include "llvm/PassManager.h" |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Passes.h" |
| 18 | #include "llvm/Analysis/Verifier.h" |
| 19 | #include "llvm/Assembly/PrintModulePass.h" |
| 20 | #include "llvm/ADT/OwningPtr.h" |
| 21 | #include "llvm/CodeGen/AsmPrinter.h" |
| 22 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 23 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 24 | #include "llvm/CodeGen/Passes.h" |
| 25 | #include "llvm/MC/MCAsmInfo.h" |
| 26 | #include "llvm/MC/MCInstrInfo.h" |
| 27 | #include "llvm/MC/MCStreamer.h" |
| 28 | #include "llvm/MC/MCSubtargetInfo.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetRegistry.h" |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
| 32 | #include "llvm/Target/TargetInstrInfo.h" |
| 33 | #include "llvm/Target/TargetLowering.h" |
| 34 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | #include "llvm/Target/TargetOptions.h" |
| 37 | #include "llvm/Target/TargetRegisterInfo.h" |
| 38 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 39 | #include "llvm/Transforms/Scalar.h" |
| 40 | #include "llvm/Support/Debug.h" |
| 41 | #include "llvm/Support/TargetRegistry.h" |
| 42 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace llvm; |
| 45 | |
Rafael Espindola | a484f2c | 2010-11-28 14:48:34 +0000 | [diff] [blame] | 46 | namespace llvm { |
| 47 | MCStreamer *createPTXAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 48 | bool isVerboseAsm, bool useLoc, |
Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 49 | bool useCFI, bool useDwarfDirectory, |
Rafael Espindola | a484f2c | 2010-11-28 14:48:34 +0000 | [diff] [blame] | 50 | MCInstPrinter *InstPrint, |
| 51 | MCCodeEmitter *CE, |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 52 | MCAsmBackend *MAB, |
Bill Wendling | e266ce6 | 2011-06-17 20:55:01 +0000 | [diff] [blame] | 53 | bool ShowInst); |
Rafael Espindola | a484f2c | 2010-11-28 14:48:34 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 56 | extern "C" void LLVMInitializePTXTarget() { |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 57 | |
| 58 | RegisterTargetMachine<PTX32TargetMachine> X(ThePTX32Target); |
| 59 | RegisterTargetMachine<PTX64TargetMachine> Y(ThePTX64Target); |
| 60 | |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 61 | TargetRegistry::RegisterAsmStreamer(ThePTX32Target, createPTXAsmStreamer); |
| 62 | TargetRegistry::RegisterAsmStreamer(ThePTX64Target, createPTXAsmStreamer); |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 65 | namespace { |
Che-Liang Chiou | 31c488c | 2011-03-02 07:58:46 +0000 | [diff] [blame] | 66 | const char* DataLayout32 = |
| 67 | "e-p:32:32-i64:32:32-f64:32:32-v128:32:128-v64:32:64-n32:64"; |
| 68 | const char* DataLayout64 = |
| 69 | "e-p:64:64-i64:32:32-f64:32:32-v128:32:128-v64:32:64-n32:64"; |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 72 | // DataLayout and FrameLowering are filled with dummy data |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 73 | PTXTargetMachine::PTXTargetMachine(const Target &T, |
Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 74 | StringRef TT, StringRef CPU, StringRef FS, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 75 | const TargetOptions &Options, |
Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 76 | Reloc::Model RM, CodeModel::Model CM, |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 77 | CodeGenOpt::Level OL, |
Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 78 | bool is64Bit) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 79 | : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL), |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 80 | DataLayout(is64Bit ? DataLayout64 : DataLayout32), |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 81 | Subtarget(TT, CPU, FS, is64Bit), |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 82 | FrameLowering(Subtarget), |
Che-Liang Chiou | 31c488c | 2011-03-02 07:58:46 +0000 | [diff] [blame] | 83 | InstrInfo(*this), |
Justin Holewinski | bc97f44 | 2011-09-26 18:57:27 +0000 | [diff] [blame] | 84 | TSInfo(*this), |
Che-Liang Chiou | 31c488c | 2011-03-02 07:58:46 +0000 | [diff] [blame] | 85 | TLInfo(*this) { |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 86 | } |
| 87 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 88 | void PTX32TargetMachine::anchor() { } |
| 89 | |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 90 | PTX32TargetMachine::PTX32TargetMachine(const Target &T, StringRef TT, |
| 91 | StringRef CPU, StringRef FS, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 92 | const TargetOptions &Options, |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 93 | Reloc::Model RM, CodeModel::Model CM, |
| 94 | CodeGenOpt::Level OL) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 95 | : PTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) { |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 96 | } |
| 97 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 98 | void PTX64TargetMachine::anchor() { } |
| 99 | |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 100 | PTX64TargetMachine::PTX64TargetMachine(const Target &T, StringRef TT, |
| 101 | StringRef CPU, StringRef FS, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 102 | const TargetOptions &Options, |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 103 | Reloc::Model RM, CodeModel::Model CM, |
| 104 | CodeGenOpt::Level OL) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 105 | : PTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) { |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 108 | namespace { |
| 109 | /// PTX Code Generator Pass Configuration Options. |
| 110 | class PTXPassConfig : public TargetPassConfig { |
| 111 | public: |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 112 | PTXPassConfig(PTXTargetMachine *TM, PassManagerBase &PM) |
| 113 | : TargetPassConfig(TM, PM) {} |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 114 | |
| 115 | PTXTargetMachine &getPTXTargetMachine() const { |
| 116 | return getTM<PTXTargetMachine>(); |
| 117 | } |
| 118 | |
| 119 | bool addInstSelector(); |
| 120 | bool addPostRegAlloc(); |
| 121 | bool addCodeGenPasses(MCContext *&OutContext); |
| 122 | }; |
| 123 | } // namespace |
| 124 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 125 | TargetPassConfig *PTXTargetMachine::createPassConfig(PassManagerBase &PM) { |
| 126 | return new PTXPassConfig(this, PM); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool PTXPassConfig::addInstSelector() { |
| 130 | PM.add(createPTXISelDag(getPTXTargetMachine(), getOptLevel())); |
Che-Liang Chiou | ad83c1d | 2011-01-01 10:50:37 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 134 | bool PTXPassConfig::addPostRegAlloc() { |
Che-Liang Chiou | ad83c1d | 2011-01-01 10:50:37 +0000 | [diff] [blame] | 135 | // PTXMFInfoExtract must after register allocation! |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 136 | //PM.add(createPTXMFInfoExtract(getPTXTargetMachine())); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 137 | return false; |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 138 | } |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 139 | |
| 140 | bool PTXTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
| 141 | formatted_raw_ostream &Out, |
| 142 | CodeGenFileType FileType, |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 143 | bool DisableVerify) { |
| 144 | // This is mostly based on LLVMTargetMachine::addPassesToEmitFile |
| 145 | |
| 146 | // Add common CodeGen passes. |
| 147 | MCContext *Context = 0; |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 148 | |
| 149 | // FIXME: soon this will be converted to use the exposed TargetPassConfig API. |
| 150 | OwningPtr<PTXPassConfig> PassConfig( |
| 151 | static_cast<PTXPassConfig*>(createPassConfig(PM))); |
| 152 | |
| 153 | PassConfig->setDisableVerify(DisableVerify); |
| 154 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 155 | if (PassConfig->addCodeGenPasses(Context)) |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 156 | return true; |
| 157 | assert(Context != 0 && "Failed to get MCContext"); |
| 158 | |
| 159 | if (hasMCSaveTempLabels()) |
| 160 | Context->setAllowTemporaryLabels(false); |
| 161 | |
| 162 | const MCAsmInfo &MAI = *getMCAsmInfo(); |
| 163 | const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>(); |
| 164 | OwningPtr<MCStreamer> AsmStreamer; |
| 165 | |
| 166 | switch (FileType) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 167 | case CGFT_AssemblyFile: { |
| 168 | MCInstPrinter *InstPrinter = |
| 169 | getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI); |
| 170 | |
| 171 | // Create a code emitter if asked to show the encoding. |
| 172 | MCCodeEmitter *MCE = 0; |
| 173 | MCAsmBackend *MAB = 0; |
| 174 | |
| 175 | MCStreamer *S = getTarget().createAsmStreamer(*Context, Out, |
| 176 | true, /* verbose asm */ |
| 177 | hasMCUseLoc(), |
| 178 | hasMCUseCFI(), |
Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 179 | hasMCUseDwarfDirectory(), |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 180 | InstPrinter, |
| 181 | MCE, MAB, |
| 182 | false /* show MC encoding */); |
| 183 | AsmStreamer.reset(S); |
| 184 | break; |
| 185 | } |
| 186 | case CGFT_ObjectFile: { |
| 187 | llvm_unreachable("Object file emission is not supported with PTX"); |
| 188 | } |
| 189 | case CGFT_Null: |
| 190 | // The Null output is intended for use for performance analysis and testing, |
| 191 | // not real users. |
| 192 | AsmStreamer.reset(createNullStreamer(*Context)); |
| 193 | break; |
| 194 | } |
| 195 | |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 196 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
| 197 | FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer); |
| 198 | if (Printer == 0) |
| 199 | return true; |
| 200 | |
| 201 | // If successful, createAsmPrinter took ownership of AsmStreamer. |
| 202 | AsmStreamer.take(); |
| 203 | |
| 204 | PM.add(Printer); |
| 205 | |
| 206 | PM.add(createGCInfoDeleter()); |
| 207 | return false; |
| 208 | } |
| 209 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 210 | bool PTXPassConfig::addCodeGenPasses(MCContext *&OutContext) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 211 | // Add standard LLVM codegen passes. |
| 212 | // This is derived from LLVMTargetMachine::addCommonCodeGenPasses, with some |
| 213 | // modifications for the PTX target. |
| 214 | |
| 215 | // Standard LLVM-Level Passes. |
| 216 | |
| 217 | // Basic AliasAnalysis support. |
| 218 | // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that |
| 219 | // BasicAliasAnalysis wins if they disagree. This is intended to help |
| 220 | // support "obvious" type-punning idioms. |
| 221 | PM.add(createTypeBasedAliasAnalysisPass()); |
| 222 | PM.add(createBasicAliasAnalysisPass()); |
| 223 | |
| 224 | // Before running any passes, run the verifier to determine if the input |
| 225 | // coming from the front-end and/or optimizer is valid. |
| 226 | if (!DisableVerify) |
| 227 | PM.add(createVerifierPass()); |
| 228 | |
| 229 | // Run loop strength reduction before anything else. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 230 | if (getOptLevel() != CodeGenOpt::None) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 231 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 232 | //PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs())); |
| 233 | } |
| 234 | |
| 235 | PM.add(createGCLoweringPass()); |
| 236 | |
| 237 | // Make sure that no unreachable blocks are instruction selected. |
| 238 | PM.add(createUnreachableBlockEliminationPass()); |
| 239 | |
| 240 | PM.add(createLowerInvokePass(getTargetLowering())); |
| 241 | // The lower invoke pass may create unreachable code. Remove it. |
| 242 | PM.add(createUnreachableBlockEliminationPass()); |
| 243 | |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 244 | if (getOptLevel() != CodeGenOpt::None) |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 245 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 246 | |
| 247 | PM.add(createStackProtectorPass(getTargetLowering())); |
| 248 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 249 | addPreISel(); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 250 | |
| 251 | //PM.add(createPrintFunctionPass("\n\n" |
| 252 | // "*** Final LLVM Code input to ISel ***\n", |
| 253 | // &dbgs())); |
| 254 | |
| 255 | // All passes which modify the LLVM IR are now complete; run the verifier |
| 256 | // to ensure that the IR is valid. |
| 257 | if (!DisableVerify) |
| 258 | PM.add(createVerifierPass()); |
| 259 | |
| 260 | // Standard Lower-Level Passes. |
| 261 | |
| 262 | // Install a MachineModuleInfo class, which is an immutable pass that holds |
| 263 | // all the per-module stuff we're generating, including MCContext. |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 264 | MachineModuleInfo *MMI = new MachineModuleInfo(*TM->getMCAsmInfo(), |
| 265 | *TM->getRegisterInfo(), |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 266 | &getTargetLowering()->getObjFileLowering()); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 267 | PM.add(MMI); |
| 268 | OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref. |
| 269 | |
| 270 | // Set up a MachineFunction for the rest of CodeGen to work on. |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 271 | PM.add(new MachineFunctionAnalysis(*TM)); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 272 | |
| 273 | // Ask the target for an isel. |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 274 | if (addInstSelector()) |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 275 | return true; |
| 276 | |
| 277 | // Print the instruction selected machine code... |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 278 | printAndVerify("After Instruction Selection"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 279 | |
| 280 | // Expand pseudo-instructions emitted by ISel. |
| 281 | PM.add(createExpandISelPseudosPass()); |
| 282 | |
| 283 | // Pre-ra tail duplication. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 284 | if (getOptLevel() != CodeGenOpt::None) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 285 | PM.add(createTailDuplicatePass(true)); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 286 | printAndVerify("After Pre-RegAlloc TailDuplicate"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Optimize PHIs before DCE: removing dead PHI cycles may make more |
| 290 | // instructions dead. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 291 | if (getOptLevel() != CodeGenOpt::None) |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 292 | PM.add(createOptimizePHIsPass()); |
| 293 | |
| 294 | // If the target requests it, assign local variables to stack slots relative |
| 295 | // to one another and simplify frame index references where possible. |
| 296 | PM.add(createLocalStackSlotAllocationPass()); |
| 297 | |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 298 | if (getOptLevel() != CodeGenOpt::None) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 299 | // With optimization, dead code should already be eliminated. However |
| 300 | // there is one known exception: lowered code for arguments that are only |
| 301 | // used by tail calls, where the tail calls reuse the incoming stack |
| 302 | // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll). |
| 303 | PM.add(createDeadMachineInstructionElimPass()); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 304 | printAndVerify("After codegen DCE pass"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 305 | |
| 306 | PM.add(createMachineLICMPass()); |
| 307 | PM.add(createMachineCSEPass()); |
| 308 | PM.add(createMachineSinkingPass()); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 309 | printAndVerify("After Machine LICM, CSE and Sinking passes"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 310 | |
| 311 | PM.add(createPeepholeOptimizerPass()); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 312 | printAndVerify("After codegen peephole optimization pass"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Run pre-ra passes. |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 316 | if (addPreRegAlloc()) |
| 317 | printAndVerify("After PreRegAlloc passes"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 318 | |
| 319 | // Perform register allocation. |
| 320 | PM.add(createPTXRegisterAllocator()); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 321 | printAndVerify("After Register Allocation"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 322 | |
| 323 | // Perform stack slot coloring and post-ra machine LICM. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 324 | if (getOptLevel() != CodeGenOpt::None) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 325 | // FIXME: Re-enable coloring with register when it's capable of adding |
| 326 | // kill markers. |
| 327 | PM.add(createStackSlotColoringPass(false)); |
| 328 | |
| 329 | // FIXME: Post-RA LICM has asserts that fire on virtual registers. |
| 330 | // Run post-ra machine LICM to hoist reloads / remats. |
| 331 | //if (!DisablePostRAMachineLICM) |
| 332 | // PM.add(createMachineLICMPass(false)); |
| 333 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 334 | printAndVerify("After StackSlotColoring and postra Machine LICM"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Run post-ra passes. |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 338 | if (addPostRegAlloc()) |
| 339 | printAndVerify("After PostRegAlloc passes"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 340 | |
Jakob Stoklund Olesen | 74e2d6e | 2011-09-25 16:46:08 +0000 | [diff] [blame] | 341 | PM.add(createExpandPostRAPseudosPass()); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 342 | printAndVerify("After ExpandPostRAPseudos"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 343 | |
| 344 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 345 | PM.add(createPrologEpilogCodeInserter()); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 346 | printAndVerify("After PrologEpilogCodeInserter"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 347 | |
| 348 | // Run pre-sched2 passes. |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 349 | if (addPreSched2()) |
| 350 | printAndVerify("After PreSched2 passes"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 351 | |
| 352 | // Second pass scheduler. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 353 | if (getOptLevel() != CodeGenOpt::None) { |
| 354 | PM.add(createPostRAScheduler(getOptLevel())); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 355 | printAndVerify("After PostRAScheduler"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 359 | if (getOptLevel() != CodeGenOpt::None) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 360 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 361 | printNoVerify("After BranchFolding"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // Tail duplication. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 365 | if (getOptLevel() != CodeGenOpt::None) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 366 | PM.add(createTailDuplicatePass(false)); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 367 | printNoVerify("After TailDuplicate"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | PM.add(createGCMachineCodeAnalysisPass()); |
| 371 | |
| 372 | //if (PrintGCInfo) |
| 373 | // PM.add(createGCInfoPrinter(dbgs())); |
| 374 | |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 375 | if (getOptLevel() != CodeGenOpt::None) { |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 376 | PM.add(createCodePlacementOptPass()); |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 377 | printNoVerify("After CodePlacementOpt"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 380 | if (addPreEmitPass()) |
| 381 | printNoVerify("After PreEmit passes"); |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 382 | |
Andrew Trick | 843ee2e | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 383 | PM.add(createPTXMFInfoExtract(getPTXTargetMachine(), getOptLevel())); |
| 384 | PM.add(createPTXFPRoundingModePass(getPTXTargetMachine(), getOptLevel())); |
Justin Holewinski | 6b8990d | 2011-09-26 16:20:25 +0000 | [diff] [blame] | 385 | |
Justin Holewinski | 40466cc | 2011-09-22 16:45:37 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } |