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