| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===-- NVPTXTargetMachine.cpp - Define TargetMachine for NVPTX -----------===// | 
|  | 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 NVPTX target. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
|  | 14 | #include "NVPTXTargetMachine.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/NVPTXMCAsmInfo.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "NVPTX.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 17 | #include "NVPTXAllocaHoisting.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "NVPTXLowerAggrCopies.h" | 
| Aditya Nandakumar | a271932 | 2014-11-13 09:26:31 +0000 | [diff] [blame] | 19 | #include "NVPTXTargetObjectFile.h" | 
| Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 20 | #include "NVPTXTargetTransformInfo.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Passes.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/AsmPrinter.h" | 
|  | 23 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" | 
|  | 24 | #include "llvm/CodeGen/MachineModuleInfo.h" | 
|  | 25 | #include "llvm/CodeGen/Passes.h" | 
| Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame^] | 26 | #include "llvm/CodeGen/TargetPassConfig.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" | 
| Chandler Carruth | b8ddc70 | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 28 | #include "llvm/IR/IRPrintingPasses.h" | 
| Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 29 | #include "llvm/IR/LegacyPassManager.h" | 
| Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Verifier.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCAsmInfo.h" | 
|  | 32 | #include "llvm/MC/MCInstrInfo.h" | 
|  | 33 | #include "llvm/MC/MCStreamer.h" | 
|  | 34 | #include "llvm/MC/MCSubtargetInfo.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" | 
|  | 36 | #include "llvm/Support/Debug.h" | 
|  | 37 | #include "llvm/Support/FormattedStream.h" | 
|  | 38 | #include "llvm/Support/TargetRegistry.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetInstrInfo.h" | 
|  | 41 | #include "llvm/Target/TargetLowering.h" | 
|  | 42 | #include "llvm/Target/TargetLoweringObjectFile.h" | 
|  | 43 | #include "llvm/Target/TargetMachine.h" | 
|  | 44 | #include "llvm/Target/TargetOptions.h" | 
|  | 45 | #include "llvm/Target/TargetRegisterInfo.h" | 
|  | 46 | #include "llvm/Target/TargetSubtargetInfo.h" | 
|  | 47 | #include "llvm/Transforms/Scalar.h" | 
| Chandler Carruth | 89c45a1 | 2016-03-11 08:50:55 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Scalar/GVN.h" | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 49 |  | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 50 | using namespace llvm; | 
|  | 51 |  | 
| Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 52 | static cl::opt<bool> UseInferAddressSpaces( | 
|  | 53 | "nvptx-use-infer-addrspace", cl::init(false), cl::Hidden, | 
|  | 54 | cl::desc("Optimize address spaces using NVPTXInferAddressSpaces instead of " | 
|  | 55 | "NVPTXFavorNonGenericAddrSpaces")); | 
|  | 56 |  | 
| Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 57 | namespace llvm { | 
|  | 58 | void initializeNVVMReflectPass(PassRegistry&); | 
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 59 | void initializeGenericToNVVMPass(PassRegistry&); | 
| Benjamin Kramer | 414c096 | 2015-03-10 19:20:52 +0000 | [diff] [blame] | 60 | void initializeNVPTXAllocaHoistingPass(PassRegistry &); | 
| Eli Bendersky | 264cd46 | 2014-03-31 15:56:26 +0000 | [diff] [blame] | 61 | void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry&); | 
| Eli Bendersky | bbef172 | 2014-04-03 21:18:25 +0000 | [diff] [blame] | 62 | void initializeNVPTXFavorNonGenericAddrSpacesPass(PassRegistry &); | 
| Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 63 | void initializeNVPTXInferAddressSpacesPass(PassRegistry &); | 
| Eli Bendersky | f14af16 | 2015-07-16 16:27:19 +0000 | [diff] [blame] | 64 | void initializeNVPTXLowerAggrCopiesPass(PassRegistry &); | 
| Jingyue Wu | a2f6027 | 2015-06-04 21:28:26 +0000 | [diff] [blame] | 65 | void initializeNVPTXLowerKernelArgsPass(PassRegistry &); | 
| Jingyue Wu | cd3afea | 2015-06-17 22:31:02 +0000 | [diff] [blame] | 66 | void initializeNVPTXLowerAllocaPass(PassRegistry &); | 
| Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 69 | extern "C" void LLVMInitializeNVPTXTarget() { | 
|  | 70 | // Register the target. | 
|  | 71 | RegisterTargetMachine<NVPTXTargetMachine32> X(TheNVPTXTarget32); | 
|  | 72 | RegisterTargetMachine<NVPTXTargetMachine64> Y(TheNVPTXTarget64); | 
|  | 73 |  | 
| Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 74 | // FIXME: This pass is really intended to be invoked during IR optimization, | 
|  | 75 | // but it's very NVPTX-specific. | 
| Eli Bendersky | f14af16 | 2015-07-16 16:27:19 +0000 | [diff] [blame] | 76 | PassRegistry &PR = *PassRegistry::getPassRegistry(); | 
|  | 77 | initializeNVVMReflectPass(PR); | 
|  | 78 | initializeGenericToNVVMPass(PR); | 
|  | 79 | initializeNVPTXAllocaHoistingPass(PR); | 
|  | 80 | initializeNVPTXAssignValidGlobalNamesPass(PR); | 
|  | 81 | initializeNVPTXFavorNonGenericAddrSpacesPass(PR); | 
| Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 82 | initializeNVPTXInferAddressSpacesPass(PR); | 
| Eli Bendersky | f14af16 | 2015-07-16 16:27:19 +0000 | [diff] [blame] | 83 | initializeNVPTXLowerKernelArgsPass(PR); | 
|  | 84 | initializeNVPTXLowerAllocaPass(PR); | 
|  | 85 | initializeNVPTXLowerAggrCopiesPass(PR); | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 88 | static std::string computeDataLayout(bool is64Bit) { | 
|  | 89 | std::string Ret = "e"; | 
|  | 90 |  | 
|  | 91 | if (!is64Bit) | 
|  | 92 | Ret += "-p:32:32"; | 
|  | 93 |  | 
|  | 94 | Ret += "-i64:64-v16:16-v32:32-n16:32:64"; | 
|  | 95 |  | 
|  | 96 | return Ret; | 
|  | 97 | } | 
|  | 98 |  | 
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 99 | NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT, | 
| Eric Christopher | a186946 | 2014-06-27 01:27:06 +0000 | [diff] [blame] | 100 | StringRef CPU, StringRef FS, | 
|  | 101 | const TargetOptions &Options, | 
|  | 102 | Reloc::Model RM, CodeModel::Model CM, | 
|  | 103 | CodeGenOpt::Level OL, bool is64bit) | 
| Mehdi Amini | 93e1ea1 | 2015-03-12 00:07:24 +0000 | [diff] [blame] | 104 | : LLVMTargetMachine(T, computeDataLayout(is64bit), TT, CPU, FS, Options, RM, | 
|  | 105 | CM, OL), | 
|  | 106 | is64bit(is64bit), TLOF(make_unique<NVPTXTargetObjectFile>()), | 
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 107 | Subtarget(TT, CPU, FS, *this) { | 
|  | 108 | if (TT.getOS() == Triple::NVCL) | 
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 109 | drvInterface = NVPTX::NVCL; | 
|  | 110 | else | 
|  | 111 | drvInterface = NVPTX::CUDA; | 
| Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 112 | initAsmInfo(); | 
|  | 113 | } | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 114 |  | 
| Reid Kleckner | 357600e | 2014-11-20 23:37:18 +0000 | [diff] [blame] | 115 | NVPTXTargetMachine::~NVPTXTargetMachine() {} | 
|  | 116 |  | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 117 | void NVPTXTargetMachine32::anchor() {} | 
|  | 118 |  | 
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 119 | NVPTXTargetMachine32::NVPTXTargetMachine32(const Target &T, const Triple &TT, | 
|  | 120 | StringRef CPU, StringRef FS, | 
|  | 121 | const TargetOptions &Options, | 
|  | 122 | Reloc::Model RM, CodeModel::Model CM, | 
|  | 123 | CodeGenOpt::Level OL) | 
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 124 | : NVPTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {} | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 125 |  | 
|  | 126 | void NVPTXTargetMachine64::anchor() {} | 
|  | 127 |  | 
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 128 | NVPTXTargetMachine64::NVPTXTargetMachine64(const Target &T, const Triple &TT, | 
|  | 129 | StringRef CPU, StringRef FS, | 
|  | 130 | const TargetOptions &Options, | 
|  | 131 | Reloc::Model RM, CodeModel::Model CM, | 
|  | 132 | CodeGenOpt::Level OL) | 
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 133 | : NVPTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {} | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 134 |  | 
| Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 135 | namespace { | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 136 | class NVPTXPassConfig : public TargetPassConfig { | 
|  | 137 | public: | 
|  | 138 | NVPTXPassConfig(NVPTXTargetMachine *TM, PassManagerBase &PM) | 
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 139 | : TargetPassConfig(TM, PM) {} | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 140 |  | 
|  | 141 | NVPTXTargetMachine &getNVPTXTargetMachine() const { | 
|  | 142 | return getTM<NVPTXTargetMachine>(); | 
|  | 143 | } | 
|  | 144 |  | 
| Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 145 | void addIRPasses() override; | 
|  | 146 | bool addInstSelector() override; | 
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 147 | void addPostRegAlloc() override; | 
| Justin Holewinski | 6dca839 | 2014-06-27 18:35:14 +0000 | [diff] [blame] | 148 | void addMachineSSAOptimization() override; | 
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 149 |  | 
| Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 150 | FunctionPass *createTargetRegisterAllocator(bool) override; | 
|  | 151 | void addFastRegAlloc(FunctionPass *RegAllocPass) override; | 
|  | 152 | void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override; | 
| Jingyue Wu | 6a3fdec | 2015-07-23 04:59:07 +0000 | [diff] [blame] | 153 |  | 
|  | 154 | private: | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 155 | // If the opt level is aggressive, add GVN; otherwise, add EarlyCSE. This | 
|  | 156 | // function is only called in opt mode. | 
| Jingyue Wu | 6a3fdec | 2015-07-23 04:59:07 +0000 | [diff] [blame] | 157 | void addEarlyCSEOrGVNPass(); | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 158 |  | 
|  | 159 | // Add passes that propagate special memory spaces. | 
| Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 160 | void addAddressSpaceInferencePasses(); | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 161 |  | 
|  | 162 | // Add passes that perform straight-line scalar optimizations. | 
|  | 163 | void addStraightLineScalarOptimizationPasses(); | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 164 | }; | 
| Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 165 | } // end anonymous namespace | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 166 |  | 
|  | 167 | TargetPassConfig *NVPTXTargetMachine::createPassConfig(PassManagerBase &PM) { | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 168 | return new NVPTXPassConfig(this, PM); | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Justin Lebar | 7cdbce5 | 2016-04-27 19:13:37 +0000 | [diff] [blame] | 171 | void NVPTXTargetMachine::addEarlyAsPossiblePasses(PassManagerBase &PM) { | 
|  | 172 | PM.add(createNVVMReflectPass()); | 
|  | 173 | } | 
|  | 174 |  | 
| Chandler Carruth | 8b04c0d | 2015-02-01 13:20:00 +0000 | [diff] [blame] | 175 | TargetIRAnalysis NVPTXTargetMachine::getTargetIRAnalysis() { | 
| Eric Christopher | a4e5d3c | 2015-09-16 23:38:13 +0000 | [diff] [blame] | 176 | return TargetIRAnalysis([this](const Function &F) { | 
| Mehdi Amini | 5010ebf | 2015-07-09 02:08:42 +0000 | [diff] [blame] | 177 | return TargetTransformInfo(NVPTXTTIImpl(this, F)); | 
|  | 178 | }); | 
| Jingyue Wu | 0c981bd | 2014-11-10 18:38:25 +0000 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
| Jingyue Wu | 6a3fdec | 2015-07-23 04:59:07 +0000 | [diff] [blame] | 181 | void NVPTXPassConfig::addEarlyCSEOrGVNPass() { | 
|  | 182 | if (getOptLevel() == CodeGenOpt::Aggressive) | 
|  | 183 | addPass(createGVNPass()); | 
|  | 184 | else | 
|  | 185 | addPass(createEarlyCSEPass()); | 
|  | 186 | } | 
|  | 187 |  | 
| Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 188 | void NVPTXPassConfig::addAddressSpaceInferencePasses() { | 
| Jingyue Wu | 2e4d1dd | 2015-06-09 00:05:56 +0000 | [diff] [blame] | 189 | // NVPTXLowerKernelArgs emits alloca for byval parameters which can often | 
| Jingyue Wu | cd3afea | 2015-06-17 22:31:02 +0000 | [diff] [blame] | 190 | // be eliminated by SROA. | 
| Jingyue Wu | 2e4d1dd | 2015-06-09 00:05:56 +0000 | [diff] [blame] | 191 | addPass(createSROAPass()); | 
| Jingyue Wu | cd3afea | 2015-06-17 22:31:02 +0000 | [diff] [blame] | 192 | addPass(createNVPTXLowerAllocaPass()); | 
| Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 193 | if (UseInferAddressSpaces) { | 
|  | 194 | addPass(createNVPTXInferAddressSpacesPass()); | 
|  | 195 | } else { | 
|  | 196 | addPass(createNVPTXFavorNonGenericAddrSpacesPass()); | 
|  | 197 | // FavorNonGenericAddrSpaces shortcuts unnecessary addrspacecasts, and leave | 
|  | 198 | // them unused. We could remove dead code in an ad-hoc manner, but that | 
|  | 199 | // requires manual work and might be error-prone. | 
|  | 200 | addPass(createDeadCodeEliminationPass()); | 
|  | 201 | } | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 202 | } | 
| Jingyue Wu | 6a3fdec | 2015-07-23 04:59:07 +0000 | [diff] [blame] | 203 |  | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 204 | void NVPTXPassConfig::addStraightLineScalarOptimizationPasses() { | 
| Eli Bendersky | a108a65 | 2014-05-01 18:38:36 +0000 | [diff] [blame] | 205 | addPass(createSeparateConstOffsetFromGEPPass()); | 
| Jingyue Wu | e7981ce | 2015-07-16 20:13:48 +0000 | [diff] [blame] | 206 | addPass(createSpeculativeExecutionPass()); | 
| Jingyue Wu | 3286ec1 | 2015-04-23 20:00:04 +0000 | [diff] [blame] | 207 | // ReassociateGEPs exposes more opportunites for SLSR. See | 
|  | 208 | // the example in reassociate-geps-and-slsr.ll. | 
|  | 209 | addPass(createStraightLineStrengthReducePass()); | 
|  | 210 | // SeparateConstOffsetFromGEP and SLSR creates common expressions which GVN or | 
|  | 211 | // EarlyCSE can reuse. GVN generates significantly better code than EarlyCSE | 
|  | 212 | // for some of our benchmarks. | 
| Jingyue Wu | 6a3fdec | 2015-07-23 04:59:07 +0000 | [diff] [blame] | 213 | addEarlyCSEOrGVNPass(); | 
| Jingyue Wu | 72fca6c | 2015-04-24 04:22:39 +0000 | [diff] [blame] | 214 | // Run NaryReassociate after EarlyCSE/GVN to be more effective. | 
|  | 215 | addPass(createNaryReassociatePass()); | 
| Jingyue Wu | c2a0146 | 2015-05-28 04:56:52 +0000 | [diff] [blame] | 216 | // NaryReassociate on GEPs creates redundant common expressions, so run | 
|  | 217 | // EarlyCSE after it. | 
|  | 218 | addPass(createEarlyCSEPass()); | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
|  | 221 | void NVPTXPassConfig::addIRPasses() { | 
|  | 222 | // The following passes are known to not play well with virtual regs hanging | 
|  | 223 | // around after register allocation (which in our case, is *all* registers). | 
|  | 224 | // We explicitly disable them here.  We do, however, need some functionality | 
|  | 225 | // of the PrologEpilogCodeInserter pass, so we emulate that behavior in the | 
|  | 226 | // NVPTXPrologEpilog pass (see NVPTXPrologEpilogPass.cpp). | 
|  | 227 | disablePass(&PrologEpilogCodeInserterID); | 
|  | 228 | disablePass(&MachineCopyPropagationID); | 
|  | 229 | disablePass(&TailDuplicateID); | 
| Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 230 | disablePass(&StackMapLivenessID); | 
|  | 231 | disablePass(&LiveDebugValuesID); | 
|  | 232 | disablePass(&PostRASchedulerID); | 
|  | 233 | disablePass(&FuncletLayoutID); | 
| Sanjoy Das | fe71ec7 | 2016-04-19 06:24:58 +0000 | [diff] [blame] | 234 | disablePass(&PatchableFunctionID); | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 235 |  | 
| Justin Lebar | 7cdbce5 | 2016-04-27 19:13:37 +0000 | [diff] [blame] | 236 | // NVVMReflectPass is added in addEarlyAsPossiblePasses, so hopefully running | 
|  | 237 | // it here does nothing.  But since we need it for correctness when lowering | 
|  | 238 | // to NVPTX, run it here too, in case whoever built our pass pipeline didn't | 
|  | 239 | // call addEarlyAsPossiblePasses. | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 240 | addPass(createNVVMReflectPass()); | 
| Justin Lebar | 7cdbce5 | 2016-04-27 19:13:37 +0000 | [diff] [blame] | 241 |  | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 242 | if (getOptLevel() != CodeGenOpt::None) | 
|  | 243 | addPass(createNVPTXImageOptimizerPass()); | 
|  | 244 | addPass(createNVPTXAssignValidGlobalNamesPass()); | 
|  | 245 | addPass(createGenericToNVVMPass()); | 
|  | 246 |  | 
| Jingyue Wu | c1b9d47 | 2016-04-26 22:59:25 +0000 | [diff] [blame] | 247 | // NVPTXLowerKernelArgs is required for correctness and should be run right | 
|  | 248 | // before the address space inference passes. | 
|  | 249 | addPass(createNVPTXLowerKernelArgsPass(&getNVPTXTargetMachine())); | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 250 | if (getOptLevel() != CodeGenOpt::None) { | 
| Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 251 | addAddressSpaceInferencePasses(); | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 252 | addStraightLineScalarOptimizationPasses(); | 
|  | 253 | } | 
| Jingyue Wu | 6a3fdec | 2015-07-23 04:59:07 +0000 | [diff] [blame] | 254 |  | 
|  | 255 | // === LSR and other generic IR passes === | 
|  | 256 | TargetPassConfig::addIRPasses(); | 
|  | 257 | // EarlyCSE is not always strong enough to clean up what LSR produces. For | 
|  | 258 | // example, GVN can combine | 
|  | 259 | // | 
|  | 260 | //   %0 = add %a, %b | 
|  | 261 | //   %1 = add %b, %a | 
|  | 262 | // | 
|  | 263 | // and | 
|  | 264 | // | 
|  | 265 | //   %0 = shl nsw %a, 2 | 
|  | 266 | //   %1 = shl %a, 2 | 
|  | 267 | // | 
|  | 268 | // but EarlyCSE can do neither of them. | 
| Jingyue Wu | f650441 | 2016-02-04 04:15:36 +0000 | [diff] [blame] | 269 | if (getOptLevel() != CodeGenOpt::None) | 
|  | 270 | addEarlyCSEOrGVNPass(); | 
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 271 | } | 
|  | 272 |  | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 273 | bool NVPTXPassConfig::addInstSelector() { | 
| Eric Christopher | 5c3dffc | 2015-03-21 03:13:03 +0000 | [diff] [blame] | 274 | const NVPTXSubtarget &ST = *getTM<NVPTXTargetMachine>().getSubtargetImpl(); | 
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 275 |  | 
| Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 276 | addPass(createLowerAggrCopies()); | 
| Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 277 | addPass(createAllocaHoisting()); | 
|  | 278 | addPass(createNVPTXISelDag(getNVPTXTargetMachine(), getOptLevel())); | 
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 279 |  | 
|  | 280 | if (!ST.hasImageHandles()) | 
|  | 281 | addPass(createNVPTXReplaceImageHandlesPass()); | 
|  | 282 |  | 
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 283 | return false; | 
|  | 284 | } | 
|  | 285 |  | 
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 286 | void NVPTXPassConfig::addPostRegAlloc() { | 
|  | 287 | addPass(createNVPTXPrologEpilogPass(), false); | 
| Jingyue Wu | c1b9d47 | 2016-04-26 22:59:25 +0000 | [diff] [blame] | 288 | if (getOptLevel() != CodeGenOpt::None) { | 
|  | 289 | // NVPTXPrologEpilogPass calculates frame object offset and replace frame | 
|  | 290 | // index with VRFrame register. NVPTXPeephole need to be run after that and | 
|  | 291 | // will replace VRFrame with VRFrameLocal when possible. | 
|  | 292 | addPass(createNVPTXPeephole()); | 
|  | 293 | } | 
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 294 | } | 
|  | 295 |  | 
| Benjamin Kramer | fae7ff1 | 2013-05-31 19:21:58 +0000 | [diff] [blame] | 296 | FunctionPass *NVPTXPassConfig::createTargetRegisterAllocator(bool) { | 
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 297 | return nullptr; // No reg alloc | 
| Benjamin Kramer | fae7ff1 | 2013-05-31 19:21:58 +0000 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 300 | void NVPTXPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) { | 
| Benjamin Kramer | fae7ff1 | 2013-05-31 19:21:58 +0000 | [diff] [blame] | 301 | assert(!RegAllocPass && "NVPTX uses no regalloc!"); | 
| Justin Holewinski | a51418c | 2013-10-11 12:39:39 +0000 | [diff] [blame] | 302 | addPass(&PHIEliminationID); | 
|  | 303 | addPass(&TwoAddressInstructionPassID); | 
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
|  | 306 | void NVPTXPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) { | 
| Benjamin Kramer | fae7ff1 | 2013-05-31 19:21:58 +0000 | [diff] [blame] | 307 | assert(!RegAllocPass && "NVPTX uses no regalloc!"); | 
| Justin Holewinski | a51418c | 2013-10-11 12:39:39 +0000 | [diff] [blame] | 308 |  | 
|  | 309 | addPass(&ProcessImplicitDefsID); | 
|  | 310 | addPass(&LiveVariablesID); | 
|  | 311 | addPass(&MachineLoopInfoID); | 
|  | 312 | addPass(&PHIEliminationID); | 
|  | 313 |  | 
|  | 314 | addPass(&TwoAddressInstructionPassID); | 
|  | 315 | addPass(&RegisterCoalescerID); | 
|  | 316 |  | 
|  | 317 | // PreRA instruction scheduling. | 
|  | 318 | if (addPass(&MachineSchedulerID)) | 
|  | 319 | printAndVerify("After Machine Scheduling"); | 
|  | 320 |  | 
|  | 321 |  | 
|  | 322 | addPass(&StackSlotColoringID); | 
|  | 323 |  | 
|  | 324 | // FIXME: Needs physical registers | 
|  | 325 | //addPass(&PostRAMachineLICMID); | 
|  | 326 |  | 
|  | 327 | printAndVerify("After StackSlotColoring"); | 
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 328 | } | 
| Justin Holewinski | 6dca839 | 2014-06-27 18:35:14 +0000 | [diff] [blame] | 329 |  | 
|  | 330 | void NVPTXPassConfig::addMachineSSAOptimization() { | 
|  | 331 | // Pre-ra tail duplication. | 
|  | 332 | if (addPass(&EarlyTailDuplicateID)) | 
|  | 333 | printAndVerify("After Pre-RegAlloc TailDuplicate"); | 
|  | 334 |  | 
|  | 335 | // Optimize PHIs before DCE: removing dead PHI cycles may make more | 
|  | 336 | // instructions dead. | 
|  | 337 | addPass(&OptimizePHIsID); | 
|  | 338 |  | 
|  | 339 | // This pass merges large allocas. StackSlotColoring is a different pass | 
|  | 340 | // which merges spill slots. | 
|  | 341 | addPass(&StackColoringID); | 
|  | 342 |  | 
|  | 343 | // If the target requests it, assign local variables to stack slots relative | 
|  | 344 | // to one another and simplify frame index references where possible. | 
|  | 345 | addPass(&LocalStackSlotAllocationID); | 
|  | 346 |  | 
|  | 347 | // With optimization, dead code should already be eliminated. However | 
|  | 348 | // there is one known exception: lowered code for arguments that are only | 
|  | 349 | // used by tail calls, where the tail calls reuse the incoming stack | 
|  | 350 | // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll). | 
|  | 351 | addPass(&DeadMachineInstructionElimID); | 
|  | 352 | printAndVerify("After codegen DCE pass"); | 
|  | 353 |  | 
|  | 354 | // Allow targets to insert passes that improve instruction level parallelism, | 
|  | 355 | // like if-conversion. Such passes will typically need dominator trees and | 
|  | 356 | // loop info, just like LICM and CSE below. | 
|  | 357 | if (addILPOpts()) | 
|  | 358 | printAndVerify("After ILP optimizations"); | 
|  | 359 |  | 
|  | 360 | addPass(&MachineLICMID); | 
|  | 361 | addPass(&MachineCSEID); | 
|  | 362 |  | 
|  | 363 | addPass(&MachineSinkingID); | 
|  | 364 | printAndVerify("After Machine LICM, CSE and Sinking passes"); | 
|  | 365 |  | 
|  | 366 | addPass(&PeepholeOptimizerID); | 
|  | 367 | printAndVerify("After codegen peephole optimization pass"); | 
|  | 368 | } |