Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1 | //===-- AMDGPUTargetMachine.cpp - TargetMachine for hw codegen targets-----===// |
| 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 | /// \file |
| 11 | /// \brief The AMDGPU target machine contains all of the hardware specific |
| 12 | /// information needed to emit code for R600 and SI GPUs. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "AMDGPUTargetMachine.h" |
| 17 | #include "AMDGPU.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 18 | #include "AMDGPUAliasAnalysis.h" |
Matt Arsenault | eb9025d | 2016-06-28 17:42:09 +0000 | [diff] [blame] | 19 | #include "AMDGPUCallLowering.h" |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 20 | #include "AMDGPUInstructionSelector.h" |
| 21 | #include "AMDGPULegalizerInfo.h" |
Matt Arsenault | 9aa45f0 | 2017-07-06 20:57:05 +0000 | [diff] [blame] | 22 | #include "AMDGPUMacroFusion.h" |
Matt Arsenault | eb9025d | 2016-06-28 17:42:09 +0000 | [diff] [blame] | 23 | #include "AMDGPUTargetObjectFile.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 24 | #include "AMDGPUTargetTransformInfo.h" |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 25 | #include "GCNIterativeScheduler.h" |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 26 | #include "GCNSchedStrategy.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 27 | #include "R600MachineScheduler.h" |
Matt Arsenault | 2ffe8fd | 2016-08-11 19:18:50 +0000 | [diff] [blame] | 28 | #include "SIMachineScheduler.h" |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/GlobalISel/IRTranslator.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/GlobalISel/Legalizer.h" |
| 32 | #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/TargetLoweringObjectFile.h" |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/TargetPassConfig.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Attributes.h" |
| 37 | #include "llvm/IR/Function.h" |
Stanislav Mekhanoshin | 50ea93a | 2016-12-08 19:46:04 +0000 | [diff] [blame] | 38 | #include "llvm/IR/LegacyPassManager.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
| 40 | #include "llvm/Support/CommandLine.h" |
| 41 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 42 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/IPO.h" |
| 44 | #include "llvm/Transforms/IPO/AlwaysInliner.h" |
| 45 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 46 | #include "llvm/Transforms/Scalar.h" |
| 47 | #include "llvm/Transforms/Scalar/GVN.h" |
| 48 | #include "llvm/Transforms/Vectorize.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 49 | #include <memory> |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace llvm; |
| 52 | |
Matt Arsenault | c581611 | 2016-06-24 06:30:22 +0000 | [diff] [blame] | 53 | static cl::opt<bool> EnableR600StructurizeCFG( |
| 54 | "r600-ir-structurize", |
| 55 | cl::desc("Use StructurizeCFG IR pass"), |
| 56 | cl::init(true)); |
| 57 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 58 | static cl::opt<bool> EnableSROA( |
| 59 | "amdgpu-sroa", |
| 60 | cl::desc("Run SROA after promote alloca pass"), |
| 61 | cl::ReallyHidden, |
| 62 | cl::init(true)); |
| 63 | |
Matt Arsenault | 9f5e0ef | 2017-01-25 04:25:02 +0000 | [diff] [blame] | 64 | static cl::opt<bool> |
| 65 | EnableEarlyIfConversion("amdgpu-early-ifcvt", cl::Hidden, |
| 66 | cl::desc("Run early if-conversion"), |
| 67 | cl::init(false)); |
| 68 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 69 | static cl::opt<bool> EnableR600IfConvert( |
| 70 | "r600-if-convert", |
| 71 | cl::desc("Use if conversion pass"), |
| 72 | cl::ReallyHidden, |
| 73 | cl::init(true)); |
| 74 | |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 75 | // Option to disable vectorizer for tests. |
| 76 | static cl::opt<bool> EnableLoadStoreVectorizer( |
| 77 | "amdgpu-load-store-vectorizer", |
| 78 | cl::desc("Enable load store vectorizer"), |
Matt Arsenault | 0efdd06 | 2016-09-09 22:29:28 +0000 | [diff] [blame] | 79 | cl::init(true), |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 80 | cl::Hidden); |
| 81 | |
Alexander Timofeev | 1800956 | 2016-12-08 17:28:47 +0000 | [diff] [blame] | 82 | // Option to to control global loads scalarization |
| 83 | static cl::opt<bool> ScalarizeGlobal( |
| 84 | "amdgpu-scalarize-global-loads", |
| 85 | cl::desc("Enable global load scalarization"), |
Alexander Timofeev | 982aee6 | 2017-07-04 17:32:00 +0000 | [diff] [blame] | 86 | cl::init(true), |
Alexander Timofeev | 1800956 | 2016-12-08 17:28:47 +0000 | [diff] [blame] | 87 | cl::Hidden); |
| 88 | |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 89 | // Option to run internalize pass. |
| 90 | static cl::opt<bool> InternalizeSymbols( |
| 91 | "amdgpu-internalize-symbols", |
| 92 | cl::desc("Enable elimination of non-kernel functions and unused globals"), |
| 93 | cl::init(false), |
| 94 | cl::Hidden); |
| 95 | |
Stanislav Mekhanoshin | 9053f22 | 2017-03-28 18:23:24 +0000 | [diff] [blame] | 96 | // Option to inline all early. |
| 97 | static cl::opt<bool> EarlyInlineAll( |
| 98 | "amdgpu-early-inline-all", |
| 99 | cl::desc("Inline all functions early"), |
| 100 | cl::init(false), |
| 101 | cl::Hidden); |
| 102 | |
Sam Kolton | f60ad58 | 2017-03-21 12:51:34 +0000 | [diff] [blame] | 103 | static cl::opt<bool> EnableSDWAPeephole( |
| 104 | "amdgpu-sdwa-peephole", |
| 105 | cl::desc("Enable SDWA peepholer"), |
Sam Kolton | 9fa1696 | 2017-04-06 15:03:28 +0000 | [diff] [blame] | 106 | cl::init(true)); |
Sam Kolton | f60ad58 | 2017-03-21 12:51:34 +0000 | [diff] [blame] | 107 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 108 | // Enable address space based alias analysis |
| 109 | static cl::opt<bool> EnableAMDGPUAliasAnalysis("enable-amdgpu-aa", cl::Hidden, |
| 110 | cl::desc("Enable AMDGPU Alias Analysis"), |
| 111 | cl::init(true)); |
| 112 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 113 | // Option to enable new waitcnt insertion pass. |
| 114 | static cl::opt<bool> EnableSIInsertWaitcntsPass( |
| 115 | "enable-si-insert-waitcnts", |
| 116 | cl::desc("Use new waitcnt insertion pass"), |
Mark Searles | 70359ac | 2017-06-02 14:19:25 +0000 | [diff] [blame] | 117 | cl::init(true)); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 118 | |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 119 | // Option to run late CFG structurizer |
Matt Arsenault | cc85223 | 2017-10-10 20:22:07 +0000 | [diff] [blame] | 120 | static cl::opt<bool, true> LateCFGStructurize( |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 121 | "amdgpu-late-structurize", |
| 122 | cl::desc("Enable late CFG structurization"), |
Matt Arsenault | cc85223 | 2017-10-10 20:22:07 +0000 | [diff] [blame] | 123 | cl::location(AMDGPUTargetMachine::EnableLateStructurizeCFG), |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 124 | cl::Hidden); |
| 125 | |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 126 | static cl::opt<bool> EnableAMDGPUFunctionCalls( |
| 127 | "amdgpu-function-calls", |
| 128 | cl::Hidden, |
| 129 | cl::desc("Enable AMDGPU function call support"), |
| 130 | cl::init(false)); |
| 131 | |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 132 | // Enable lib calls simplifications |
| 133 | static cl::opt<bool> EnableLibCallSimplify( |
| 134 | "amdgpu-simplify-libcall", |
| 135 | cl::desc("Enable mdgpu library simplifications"), |
| 136 | cl::init(true), |
| 137 | cl::Hidden); |
| 138 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 139 | extern "C" void LLVMInitializeAMDGPUTarget() { |
| 140 | // Register the target |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 141 | RegisterTargetMachine<R600TargetMachine> X(getTheAMDGPUTarget()); |
| 142 | RegisterTargetMachine<GCNTargetMachine> Y(getTheGCNTarget()); |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 143 | |
| 144 | PassRegistry *PR = PassRegistry::getPassRegistry(); |
Tom Stellard | a2f57be | 2017-08-02 22:19:45 +0000 | [diff] [blame] | 145 | initializeR600ClauseMergePassPass(*PR); |
| 146 | initializeR600ControlFlowFinalizerPass(*PR); |
| 147 | initializeR600PacketizerPass(*PR); |
| 148 | initializeR600ExpandSpecialInstrsPassPass(*PR); |
| 149 | initializeR600VectorRegMergerPass(*PR); |
Matt Arsenault | 7016f13 | 2017-08-03 22:30:46 +0000 | [diff] [blame] | 150 | initializeAMDGPUDAGToDAGISelPass(*PR); |
Matt Arsenault | 8c0ef8b | 2015-10-12 17:43:59 +0000 | [diff] [blame] | 151 | initializeSILowerI1CopiesPass(*PR); |
Matt Arsenault | 782c03b | 2015-11-03 22:30:13 +0000 | [diff] [blame] | 152 | initializeSIFixSGPRCopiesPass(*PR); |
Stanislav Mekhanoshin | 22a56f2 | 2017-01-24 17:46:17 +0000 | [diff] [blame] | 153 | initializeSIFixVGPRCopiesPass(*PR); |
Matt Arsenault | 8c0ef8b | 2015-10-12 17:43:59 +0000 | [diff] [blame] | 154 | initializeSIFoldOperandsPass(*PR); |
Sam Kolton | f60ad58 | 2017-03-21 12:51:34 +0000 | [diff] [blame] | 155 | initializeSIPeepholeSDWAPass(*PR); |
Matt Arsenault | c3a01ec | 2016-06-09 23:18:47 +0000 | [diff] [blame] | 156 | initializeSIShrinkInstructionsPass(*PR); |
Stanislav Mekhanoshin | 37e7f95 | 2017-08-01 23:14:32 +0000 | [diff] [blame] | 157 | initializeSIOptimizeExecMaskingPreRAPass(*PR); |
Matt Arsenault | 187276f | 2015-10-07 00:42:53 +0000 | [diff] [blame] | 158 | initializeSILoadStoreOptimizerPass(*PR); |
Matt Arsenault | 746e065 | 2017-06-02 18:02:42 +0000 | [diff] [blame] | 159 | initializeAMDGPUAlwaysInlinePass(*PR); |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 160 | initializeAMDGPUAnnotateKernelFeaturesPass(*PR); |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 161 | initializeAMDGPUAnnotateUniformValuesPass(*PR); |
Matt Arsenault | 7016f13 | 2017-08-03 22:30:46 +0000 | [diff] [blame] | 162 | initializeAMDGPUArgumentUsageInfoPass(*PR); |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 163 | initializeAMDGPULowerIntrinsicsPass(*PR); |
Yaxun Liu | de4b88d | 2017-10-10 19:39:48 +0000 | [diff] [blame] | 164 | initializeAMDGPUOpenCLEnqueuedBlockLoweringPass(*PR); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 165 | initializeAMDGPUPromoteAllocaPass(*PR); |
Matt Arsenault | 86de486 | 2016-06-24 07:07:55 +0000 | [diff] [blame] | 166 | initializeAMDGPUCodeGenPreparePass(*PR); |
Matt Arsenault | c06574f | 2017-07-28 18:40:05 +0000 | [diff] [blame] | 167 | initializeAMDGPURewriteOutArgumentsPass(*PR); |
Stanislav Mekhanoshin | 50ea93a | 2016-12-08 19:46:04 +0000 | [diff] [blame] | 168 | initializeAMDGPUUnifyMetadataPass(*PR); |
Tom Stellard | 77a1777 | 2016-01-20 15:48:27 +0000 | [diff] [blame] | 169 | initializeSIAnnotateControlFlowPass(*PR); |
Tom Stellard | 6e1967e | 2016-02-05 17:42:38 +0000 | [diff] [blame] | 170 | initializeSIInsertWaitsPass(*PR); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 171 | initializeSIInsertWaitcntsPass(*PR); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 172 | initializeSIWholeQuadModePass(*PR); |
Matt Arsenault | 55d49cf | 2016-02-12 02:16:10 +0000 | [diff] [blame] | 173 | initializeSILowerControlFlowPass(*PR); |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 174 | initializeSIInsertSkipsPass(*PR); |
Konstantin Zhuravlyov | e9a5a77 | 2017-07-21 21:19:23 +0000 | [diff] [blame] | 175 | initializeSIMemoryLegalizerPass(*PR); |
Matt Arsenault | d3e4c64 | 2016-06-02 00:04:22 +0000 | [diff] [blame] | 176 | initializeSIDebuggerInsertNopsPass(*PR); |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 177 | initializeSIOptimizeExecMaskingPass(*PR); |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 178 | initializeSIFixWWMLivenessPass(*PR); |
Matt Arsenault | b8f8dbc | 2017-03-24 19:52:05 +0000 | [diff] [blame] | 179 | initializeAMDGPUUnifyDivergentExitNodesPass(*PR); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 180 | initializeAMDGPUAAWrapperPassPass(*PR); |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 181 | initializeAMDGPUUseNativeCallsPass(*PR); |
| 182 | initializeAMDGPUSimplifyLibCallsPass(*PR); |
Stanislav Mekhanoshin | 5670e6d | 2017-09-20 04:25:58 +0000 | [diff] [blame] | 183 | initializeAMDGPUInlinerPass(*PR); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Tom Stellard | e135ffd | 2015-09-25 21:41:28 +0000 | [diff] [blame] | 186 | static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 187 | return llvm::make_unique<AMDGPUTargetObjectFile>(); |
Tom Stellard | e135ffd | 2015-09-25 21:41:28 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 190 | static ScheduleDAGInstrs *createR600MachineScheduler(MachineSchedContext *C) { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 191 | return new ScheduleDAGMILive(C, llvm::make_unique<R600SchedStrategy>()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Matt Arsenault | 2ffe8fd | 2016-08-11 19:18:50 +0000 | [diff] [blame] | 194 | static ScheduleDAGInstrs *createSIMachineScheduler(MachineSchedContext *C) { |
| 195 | return new SIScheduleDAGMI(C); |
| 196 | } |
| 197 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 198 | static ScheduleDAGInstrs * |
| 199 | createGCNMaxOccupancyMachineScheduler(MachineSchedContext *C) { |
| 200 | ScheduleDAGMILive *DAG = |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 201 | new GCNScheduleDAGMILive(C, make_unique<GCNMaxOccupancySchedStrategy>(C)); |
Matthias Braun | 115efcd | 2016-11-28 20:11:54 +0000 | [diff] [blame] | 202 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 203 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
Matt Arsenault | 9aa45f0 | 2017-07-06 20:57:05 +0000 | [diff] [blame] | 204 | DAG->addMutation(createAMDGPUMacroFusionDAGMutation()); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 205 | return DAG; |
| 206 | } |
| 207 | |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 208 | static ScheduleDAGInstrs * |
| 209 | createIterativeGCNMaxOccupancyMachineScheduler(MachineSchedContext *C) { |
| 210 | auto DAG = new GCNIterativeScheduler(C, |
| 211 | GCNIterativeScheduler::SCHEDULE_LEGACYMAXOCCUPANCY); |
| 212 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 213 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 214 | return DAG; |
| 215 | } |
| 216 | |
| 217 | static ScheduleDAGInstrs *createMinRegScheduler(MachineSchedContext *C) { |
| 218 | return new GCNIterativeScheduler(C, |
| 219 | GCNIterativeScheduler::SCHEDULE_MINREGFORCED); |
| 220 | } |
| 221 | |
Valery Pykhtin | f2fe972 | 2017-11-20 14:35:53 +0000 | [diff] [blame] | 222 | static ScheduleDAGInstrs * |
| 223 | createIterativeILPMachineScheduler(MachineSchedContext *C) { |
| 224 | auto DAG = new GCNIterativeScheduler(C, |
| 225 | GCNIterativeScheduler::SCHEDULE_ILP); |
| 226 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 227 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 228 | DAG->addMutation(createAMDGPUMacroFusionDAGMutation()); |
| 229 | return DAG; |
| 230 | } |
| 231 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 232 | static MachineSchedRegistry |
Nicolai Haehnle | 02c3291 | 2016-01-13 16:10:10 +0000 | [diff] [blame] | 233 | R600SchedRegistry("r600", "Run R600's custom scheduler", |
| 234 | createR600MachineScheduler); |
| 235 | |
| 236 | static MachineSchedRegistry |
| 237 | SISchedRegistry("si", "Run SI's custom scheduler", |
| 238 | createSIMachineScheduler); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 239 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 240 | static MachineSchedRegistry |
| 241 | GCNMaxOccupancySchedRegistry("gcn-max-occupancy", |
| 242 | "Run GCN scheduler to maximize occupancy", |
| 243 | createGCNMaxOccupancyMachineScheduler); |
| 244 | |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 245 | static MachineSchedRegistry |
| 246 | IterativeGCNMaxOccupancySchedRegistry("gcn-max-occupancy-experimental", |
| 247 | "Run GCN scheduler to maximize occupancy (experimental)", |
| 248 | createIterativeGCNMaxOccupancyMachineScheduler); |
| 249 | |
| 250 | static MachineSchedRegistry |
| 251 | GCNMinRegSchedRegistry("gcn-minreg", |
| 252 | "Run GCN iterative scheduler for minimal register usage (experimental)", |
| 253 | createMinRegScheduler); |
| 254 | |
Valery Pykhtin | f2fe972 | 2017-11-20 14:35:53 +0000 | [diff] [blame] | 255 | static MachineSchedRegistry |
| 256 | GCNILPSchedRegistry("gcn-ilp", |
| 257 | "Run GCN iterative scheduler for ILP scheduling (experimental)", |
| 258 | createIterativeILPMachineScheduler); |
| 259 | |
Matt Arsenault | ec30eb5 | 2016-05-31 16:57:45 +0000 | [diff] [blame] | 260 | static StringRef computeDataLayout(const Triple &TT) { |
| 261 | if (TT.getArch() == Triple::r600) { |
| 262 | // 32-bit pointers. |
Yaxun Liu | cc56a8b | 2017-11-06 14:32:33 +0000 | [diff] [blame] | 263 | if (TT.getEnvironmentName() == "amdgiz" || |
| 264 | TT.getEnvironmentName() == "amdgizcl") |
| 265 | return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128" |
| 266 | "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-A5"; |
Matt Arsenault | ec30eb5 | 2016-05-31 16:57:45 +0000 | [diff] [blame] | 267 | return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128" |
| 268 | "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64"; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Matt Arsenault | ec30eb5 | 2016-05-31 16:57:45 +0000 | [diff] [blame] | 271 | // 32-bit private, local, and region pointers. 64-bit global, constant and |
| 272 | // flat. |
Yaxun Liu | 14834c3 | 2017-03-25 02:05:44 +0000 | [diff] [blame] | 273 | if (TT.getEnvironmentName() == "amdgiz" || |
| 274 | TT.getEnvironmentName() == "amdgizcl") |
Yaxun Liu | 76ae47c | 2017-04-06 19:17:32 +0000 | [diff] [blame] | 275 | return "e-p:64:64-p1:64:64-p2:64:64-p3:32:32-p4:32:32-p5:32:32" |
Matt Arsenault | ec30eb5 | 2016-05-31 16:57:45 +0000 | [diff] [blame] | 276 | "-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128" |
Yaxun Liu | e95df71 | 2017-04-11 17:18:13 +0000 | [diff] [blame] | 277 | "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-A5"; |
Yaxun Liu | 14834c3 | 2017-03-25 02:05:44 +0000 | [diff] [blame] | 278 | return "e-p:32:32-p1:64:64-p2:64:64-p3:32:32-p4:64:64-p5:32:32" |
| 279 | "-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128" |
| 280 | "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64"; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 283 | LLVM_READNONE |
| 284 | static StringRef getGPUOrDefault(const Triple &TT, StringRef GPU) { |
| 285 | if (!GPU.empty()) |
| 286 | return GPU; |
| 287 | |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 288 | if (TT.getArch() == Triple::amdgcn) |
Matt Arsenault | 8728c5f | 2017-08-07 14:58:04 +0000 | [diff] [blame] | 289 | return "generic"; |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 290 | |
Matt Arsenault | 8e00194 | 2016-06-02 18:37:16 +0000 | [diff] [blame] | 291 | return "r600"; |
Matt Arsenault | b22828f | 2016-01-27 02:17:49 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 294 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 295 | // The AMDGPU toolchain only supports generating shared objects, so we |
| 296 | // must always use PIC. |
| 297 | return Reloc::PIC_; |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 300 | static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) { |
| 301 | if (CM) |
| 302 | return *CM; |
| 303 | return CodeModel::Small; |
| 304 | } |
| 305 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 306 | AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT, |
| 307 | StringRef CPU, StringRef FS, |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 308 | TargetOptions Options, |
| 309 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 310 | Optional<CodeModel::Model> CM, |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 311 | CodeGenOpt::Level OptLevel) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 312 | : LLVMTargetMachine(T, computeDataLayout(TT), TT, getGPUOrDefault(TT, CPU), |
| 313 | FS, Options, getEffectiveRelocModel(RM), |
| 314 | getEffectiveCodeModel(CM), OptLevel), |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 315 | TLOF(createTLOF(getTargetTriple())) { |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 316 | AS = AMDGPU::getAMDGPUAS(TT); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 317 | initAsmInfo(); |
| 318 | } |
| 319 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 320 | AMDGPUTargetMachine::~AMDGPUTargetMachine() = default; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 321 | |
Matt Arsenault | cc85223 | 2017-10-10 20:22:07 +0000 | [diff] [blame] | 322 | bool AMDGPUTargetMachine::EnableLateStructurizeCFG = false; |
| 323 | |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 324 | StringRef AMDGPUTargetMachine::getGPUName(const Function &F) const { |
| 325 | Attribute GPUAttr = F.getFnAttribute("target-cpu"); |
| 326 | return GPUAttr.hasAttribute(Attribute::None) ? |
| 327 | getTargetCPU() : GPUAttr.getValueAsString(); |
| 328 | } |
| 329 | |
| 330 | StringRef AMDGPUTargetMachine::getFeatureString(const Function &F) const { |
| 331 | Attribute FSAttr = F.getFnAttribute("target-features"); |
| 332 | |
| 333 | return FSAttr.hasAttribute(Attribute::None) ? |
| 334 | getTargetFeatureString() : |
| 335 | FSAttr.getValueAsString(); |
| 336 | } |
| 337 | |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 338 | static ImmutablePass *createAMDGPUExternalAAWrapperPass() { |
| 339 | return createExternalAAWrapperPass([](Pass &P, Function &, AAResults &AAR) { |
| 340 | if (auto *WrapperPass = P.getAnalysisIfAvailable<AMDGPUAAWrapperPass>()) |
| 341 | AAR.addAAResult(WrapperPass->getResult()); |
| 342 | }); |
| 343 | } |
| 344 | |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 345 | /// Predicate for Internalize pass. |
Benjamin Kramer | f9ab3dd | 2017-10-31 23:21:30 +0000 | [diff] [blame] | 346 | static bool mustPreserveGV(const GlobalValue &GV) { |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 347 | if (const Function *F = dyn_cast<Function>(&GV)) |
| 348 | return F->isDeclaration() || AMDGPU::isEntryFunctionCC(F->getCallingConv()); |
| 349 | |
| 350 | return !GV.use_empty(); |
| 351 | } |
| 352 | |
Stanislav Mekhanoshin | 8159811 | 2017-01-26 16:49:08 +0000 | [diff] [blame] | 353 | void AMDGPUTargetMachine::adjustPassManager(PassManagerBuilder &Builder) { |
Stanislav Mekhanoshin | ee2dd78 | 2017-03-17 17:13:41 +0000 | [diff] [blame] | 354 | Builder.DivergentTarget = true; |
| 355 | |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 356 | bool EnableOpt = getOptLevel() > CodeGenOpt::None; |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 357 | bool Internalize = InternalizeSymbols; |
Stanislav Mekhanoshin | 5670e6d | 2017-09-20 04:25:58 +0000 | [diff] [blame] | 358 | bool EarlyInline = EarlyInlineAll && EnableOpt && !EnableAMDGPUFunctionCalls; |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 359 | bool AMDGPUAA = EnableAMDGPUAliasAnalysis && EnableOpt; |
| 360 | bool LibCallSimplify = EnableLibCallSimplify && EnableOpt; |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 361 | |
Stanislav Mekhanoshin | 2e3bf37 | 2017-09-20 06:34:28 +0000 | [diff] [blame] | 362 | if (EnableAMDGPUFunctionCalls) { |
| 363 | delete Builder.Inliner; |
Stanislav Mekhanoshin | 5641820 | 2017-09-20 06:10:15 +0000 | [diff] [blame] | 364 | Builder.Inliner = createAMDGPUFunctionInliningPass(); |
Stanislav Mekhanoshin | 2e3bf37 | 2017-09-20 06:34:28 +0000 | [diff] [blame] | 365 | } |
Stanislav Mekhanoshin | 5670e6d | 2017-09-20 04:25:58 +0000 | [diff] [blame] | 366 | |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 367 | if (Internalize) { |
| 368 | // If we're generating code, we always have the whole program available. The |
| 369 | // relocations expected for externally visible functions aren't supported, |
| 370 | // so make sure every non-entry function is hidden. |
| 371 | Builder.addExtension( |
| 372 | PassManagerBuilder::EP_EnabledOnOptLevel0, |
| 373 | [](const PassManagerBuilder &, legacy::PassManagerBase &PM) { |
| 374 | PM.add(createInternalizePass(mustPreserveGV)); |
| 375 | }); |
| 376 | } |
| 377 | |
Stanislav Mekhanoshin | 8159811 | 2017-01-26 16:49:08 +0000 | [diff] [blame] | 378 | Builder.addExtension( |
Stanislav Mekhanoshin | f6c1feb | 2017-01-27 16:38:10 +0000 | [diff] [blame] | 379 | PassManagerBuilder::EP_ModuleOptimizerEarly, |
Stanislav Mekhanoshin | 9053f22 | 2017-03-28 18:23:24 +0000 | [diff] [blame] | 380 | [Internalize, EarlyInline, AMDGPUAA](const PassManagerBuilder &, |
| 381 | legacy::PassManagerBase &PM) { |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 382 | if (AMDGPUAA) { |
| 383 | PM.add(createAMDGPUAAWrapperPass()); |
| 384 | PM.add(createAMDGPUExternalAAWrapperPass()); |
| 385 | } |
Stanislav Mekhanoshin | 8159811 | 2017-01-26 16:49:08 +0000 | [diff] [blame] | 386 | PM.add(createAMDGPUUnifyMetadataPass()); |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 387 | if (Internalize) { |
Matt Arsenault | e745d99 | 2017-09-19 07:40:11 +0000 | [diff] [blame] | 388 | PM.add(createInternalizePass(mustPreserveGV)); |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 389 | PM.add(createGlobalDCEPass()); |
| 390 | } |
Stanislav Mekhanoshin | 9053f22 | 2017-03-28 18:23:24 +0000 | [diff] [blame] | 391 | if (EarlyInline) |
Stanislav Mekhanoshin | 89653df | 2017-03-30 20:16:02 +0000 | [diff] [blame] | 392 | PM.add(createAMDGPUAlwaysInlinePass(false)); |
Stanislav Mekhanoshin | a3b7279 | 2017-01-30 21:05:18 +0000 | [diff] [blame] | 393 | }); |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 394 | |
Stanislav Mekhanoshin | 1d8cf2b | 2017-09-29 23:40:19 +0000 | [diff] [blame] | 395 | const auto &Opt = Options; |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 396 | Builder.addExtension( |
| 397 | PassManagerBuilder::EP_EarlyAsPossible, |
Stanislav Mekhanoshin | 1d8cf2b | 2017-09-29 23:40:19 +0000 | [diff] [blame] | 398 | [AMDGPUAA, LibCallSimplify, &Opt](const PassManagerBuilder &, |
| 399 | legacy::PassManagerBase &PM) { |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 400 | if (AMDGPUAA) { |
| 401 | PM.add(createAMDGPUAAWrapperPass()); |
| 402 | PM.add(createAMDGPUExternalAAWrapperPass()); |
| 403 | } |
Stanislav Mekhanoshin | 7f37794 | 2017-08-11 16:42:09 +0000 | [diff] [blame] | 404 | PM.add(llvm::createAMDGPUUseNativeCallsPass()); |
| 405 | if (LibCallSimplify) |
Stanislav Mekhanoshin | 1d8cf2b | 2017-09-29 23:40:19 +0000 | [diff] [blame] | 406 | PM.add(llvm::createAMDGPUSimplifyLibCallsPass(Opt)); |
Stanislav Mekhanoshin | a27b2ca | 2017-03-24 18:01:14 +0000 | [diff] [blame] | 407 | }); |
Stanislav Mekhanoshin | 50c2f25 | 2017-06-19 23:17:36 +0000 | [diff] [blame] | 408 | |
| 409 | Builder.addExtension( |
| 410 | PassManagerBuilder::EP_CGSCCOptimizerLate, |
| 411 | [](const PassManagerBuilder &, legacy::PassManagerBase &PM) { |
| 412 | // Add infer address spaces pass to the opt pipeline after inlining |
| 413 | // but before SROA to increase SROA opportunities. |
| 414 | PM.add(createInferAddressSpacesPass()); |
| 415 | }); |
Stanislav Mekhanoshin | 50ea93a | 2016-12-08 19:46:04 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 418 | //===----------------------------------------------------------------------===// |
| 419 | // R600 Target Machine (R600 -> Cayman) |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | |
| 422 | R600TargetMachine::R600TargetMachine(const Target &T, const Triple &TT, |
Tom Stellard | 5dde1d2 | 2016-02-05 18:29:17 +0000 | [diff] [blame] | 423 | StringRef CPU, StringRef FS, |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 424 | TargetOptions Options, |
| 425 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 426 | Optional<CodeModel::Model> CM, |
| 427 | CodeGenOpt::Level OL, bool JIT) |
| 428 | : AMDGPUTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) { |
Matt Arsenault | ad55ee5 | 2016-12-06 01:02:51 +0000 | [diff] [blame] | 429 | setRequiresStructuredCFG(true); |
| 430 | } |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 431 | |
| 432 | const R600Subtarget *R600TargetMachine::getSubtargetImpl( |
| 433 | const Function &F) const { |
| 434 | StringRef GPU = getGPUName(F); |
| 435 | StringRef FS = getFeatureString(F); |
| 436 | |
| 437 | SmallString<128> SubtargetKey(GPU); |
| 438 | SubtargetKey.append(FS); |
| 439 | |
| 440 | auto &I = SubtargetMap[SubtargetKey]; |
| 441 | if (!I) { |
| 442 | // This needs to be done before we create a new subtarget since any |
| 443 | // creation will depend on the TM and the code generation flags on the |
| 444 | // function that reside in TargetOptions. |
| 445 | resetTargetOptions(F); |
| 446 | I = llvm::make_unique<R600Subtarget>(TargetTriple, GPU, FS, *this); |
| 447 | } |
| 448 | |
| 449 | return I.get(); |
| 450 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 451 | |
| 452 | //===----------------------------------------------------------------------===// |
| 453 | // GCN Target Machine (SI+) |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
| 456 | GCNTargetMachine::GCNTargetMachine(const Target &T, const Triple &TT, |
Tom Stellard | 5dde1d2 | 2016-02-05 18:29:17 +0000 | [diff] [blame] | 457 | StringRef CPU, StringRef FS, |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 458 | TargetOptions Options, |
| 459 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 460 | Optional<CodeModel::Model> CM, |
| 461 | CodeGenOpt::Level OL, bool JIT) |
| 462 | : AMDGPUTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {} |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 463 | |
| 464 | const SISubtarget *GCNTargetMachine::getSubtargetImpl(const Function &F) const { |
| 465 | StringRef GPU = getGPUName(F); |
| 466 | StringRef FS = getFeatureString(F); |
| 467 | |
| 468 | SmallString<128> SubtargetKey(GPU); |
| 469 | SubtargetKey.append(FS); |
| 470 | |
| 471 | auto &I = SubtargetMap[SubtargetKey]; |
| 472 | if (!I) { |
| 473 | // This needs to be done before we create a new subtarget since any |
| 474 | // creation will depend on the TM and the code generation flags on the |
| 475 | // function that reside in TargetOptions. |
| 476 | resetTargetOptions(F); |
| 477 | I = llvm::make_unique<SISubtarget>(TargetTriple, GPU, FS, *this); |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Alexander Timofeev | 1800956 | 2016-12-08 17:28:47 +0000 | [diff] [blame] | 480 | I->setScalarizeGlobalBehavior(ScalarizeGlobal); |
| 481 | |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 482 | return I.get(); |
| 483 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 484 | |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | // AMDGPU Pass Setup |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
| 489 | namespace { |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 490 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 491 | class AMDGPUPassConfig : public TargetPassConfig { |
| 492 | public: |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 493 | AMDGPUPassConfig(LLVMTargetMachine &TM, PassManagerBase &PM) |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 494 | : TargetPassConfig(TM, PM) { |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 495 | // Exceptions and StackMaps are not supported, so these passes will never do |
| 496 | // anything. |
| 497 | disablePass(&StackMapLivenessID); |
| 498 | disablePass(&FuncletLayoutID); |
| 499 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 500 | |
| 501 | AMDGPUTargetMachine &getAMDGPUTargetMachine() const { |
| 502 | return getTM<AMDGPUTargetMachine>(); |
| 503 | } |
| 504 | |
Matthias Braun | 115efcd | 2016-11-28 20:11:54 +0000 | [diff] [blame] | 505 | ScheduleDAGInstrs * |
| 506 | createMachineScheduler(MachineSchedContext *C) const override { |
| 507 | ScheduleDAGMILive *DAG = createGenericSchedLive(C); |
| 508 | DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 509 | DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); |
| 510 | return DAG; |
| 511 | } |
| 512 | |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 513 | void addEarlyCSEOrGVNPass(); |
| 514 | void addStraightLineScalarOptimizationPasses(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 515 | void addIRPasses() override; |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 516 | void addCodeGenPrepare() override; |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 517 | bool addPreISel() override; |
| 518 | bool addInstSelector() override; |
| 519 | bool addGCPasses() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 520 | }; |
| 521 | |
Matt Arsenault | 6b6a2c3 | 2016-03-11 08:00:27 +0000 | [diff] [blame] | 522 | class R600PassConfig final : public AMDGPUPassConfig { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 523 | public: |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 524 | R600PassConfig(LLVMTargetMachine &TM, PassManagerBase &PM) |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 525 | : AMDGPUPassConfig(TM, PM) {} |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 526 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 527 | ScheduleDAGInstrs *createMachineScheduler( |
| 528 | MachineSchedContext *C) const override { |
| 529 | return createR600MachineScheduler(C); |
| 530 | } |
| 531 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 532 | bool addPreISel() override; |
Tom Stellard | 2028769 | 2017-08-08 04:57:55 +0000 | [diff] [blame] | 533 | bool addInstSelector() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 534 | void addPreRegAlloc() override; |
| 535 | void addPreSched2() override; |
| 536 | void addPreEmitPass() override; |
| 537 | }; |
| 538 | |
Matt Arsenault | 6b6a2c3 | 2016-03-11 08:00:27 +0000 | [diff] [blame] | 539 | class GCNPassConfig final : public AMDGPUPassConfig { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 540 | public: |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 541 | GCNPassConfig(LLVMTargetMachine &TM, PassManagerBase &PM) |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 542 | : AMDGPUPassConfig(TM, PM) { |
Matt Arsenault | a202538 | 2017-08-03 23:24:05 +0000 | [diff] [blame] | 543 | // It is necessary to know the register usage of the entire call graph. We |
| 544 | // allow calls without EnableAMDGPUFunctionCalls if they are marked |
| 545 | // noinline, so this is always required. |
| 546 | setRequiresCodeGenSCCOrder(true); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 547 | } |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 548 | |
| 549 | GCNTargetMachine &getGCNTargetMachine() const { |
| 550 | return getTM<GCNTargetMachine>(); |
| 551 | } |
| 552 | |
| 553 | ScheduleDAGInstrs * |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 554 | createMachineScheduler(MachineSchedContext *C) const override; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 555 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 556 | bool addPreISel() override; |
Matt Arsenault | 3d1c1de | 2016-04-14 21:58:24 +0000 | [diff] [blame] | 557 | void addMachineSSAOptimization() override; |
Matt Arsenault | 9f5e0ef | 2017-01-25 04:25:02 +0000 | [diff] [blame] | 558 | bool addILPOpts() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 559 | bool addInstSelector() override; |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 560 | bool addIRTranslator() override; |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 561 | bool addLegalizeMachineIR() override; |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 562 | bool addRegBankSelect() override; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 563 | bool addGlobalInstructionSelect() override; |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 564 | void addFastRegAlloc(FunctionPass *RegAllocPass) override; |
| 565 | void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 566 | void addPreRegAlloc() override; |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 567 | void addPostRegAlloc() override; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 568 | void addPreSched2() override; |
| 569 | void addPreEmitPass() override; |
| 570 | }; |
| 571 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 572 | } // end anonymous namespace |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 573 | |
| 574 | TargetIRAnalysis AMDGPUTargetMachine::getTargetIRAnalysis() { |
Eric Christopher | a4e5d3c | 2015-09-16 23:38:13 +0000 | [diff] [blame] | 575 | return TargetIRAnalysis([this](const Function &F) { |
Matt Arsenault | 59c0ffa | 2016-06-27 20:48:03 +0000 | [diff] [blame] | 576 | return TargetTransformInfo(AMDGPUTTIImpl(this, F)); |
Mehdi Amini | 5010ebf | 2015-07-09 02:08:42 +0000 | [diff] [blame] | 577 | }); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 580 | void AMDGPUPassConfig::addEarlyCSEOrGVNPass() { |
| 581 | if (getOptLevel() == CodeGenOpt::Aggressive) |
| 582 | addPass(createGVNPass()); |
| 583 | else |
| 584 | addPass(createEarlyCSEPass()); |
| 585 | } |
| 586 | |
| 587 | void AMDGPUPassConfig::addStraightLineScalarOptimizationPasses() { |
| 588 | addPass(createSeparateConstOffsetFromGEPPass()); |
| 589 | addPass(createSpeculativeExecutionPass()); |
| 590 | // ReassociateGEPs exposes more opportunites for SLSR. See |
| 591 | // the example in reassociate-geps-and-slsr.ll. |
| 592 | addPass(createStraightLineStrengthReducePass()); |
| 593 | // SeparateConstOffsetFromGEP and SLSR creates common expressions which GVN or |
| 594 | // EarlyCSE can reuse. |
| 595 | addEarlyCSEOrGVNPass(); |
| 596 | // Run NaryReassociate after EarlyCSE/GVN to be more effective. |
| 597 | addPass(createNaryReassociatePass()); |
| 598 | // NaryReassociate on GEPs creates redundant common expressions, so run |
| 599 | // EarlyCSE after it. |
| 600 | addPass(createEarlyCSEPass()); |
| 601 | } |
| 602 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 603 | void AMDGPUPassConfig::addIRPasses() { |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 604 | const AMDGPUTargetMachine &TM = getAMDGPUTargetMachine(); |
| 605 | |
Matt Arsenault | bde8034 | 2016-05-18 15:41:07 +0000 | [diff] [blame] | 606 | // There is no reason to run these. |
| 607 | disablePass(&StackMapLivenessID); |
| 608 | disablePass(&FuncletLayoutID); |
| 609 | disablePass(&PatchableFunctionID); |
| 610 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 611 | addPass(createAMDGPULowerIntrinsicsPass()); |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 612 | |
Matt Arsenault | a202538 | 2017-08-03 23:24:05 +0000 | [diff] [blame] | 613 | if (TM.getTargetTriple().getArch() == Triple::r600 || |
| 614 | !EnableAMDGPUFunctionCalls) { |
| 615 | // Function calls are not supported, so make sure we inline everything. |
| 616 | addPass(createAMDGPUAlwaysInlinePass()); |
| 617 | addPass(createAlwaysInlinerLegacyPass()); |
| 618 | // We need to add the barrier noop pass, otherwise adding the function |
| 619 | // inlining pass will cause all of the PassConfigs passes to be run |
| 620 | // one function at a time, which means if we have a nodule with two |
| 621 | // functions, then we will generate code for the first function |
| 622 | // without ever running any passes on the second. |
| 623 | addPass(createBarrierNoopPass()); |
| 624 | } |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 625 | |
Matt Arsenault | 0c32938 | 2017-01-30 18:40:29 +0000 | [diff] [blame] | 626 | if (TM.getTargetTriple().getArch() == Triple::amdgcn) { |
| 627 | // TODO: May want to move later or split into an early and late one. |
| 628 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 629 | addPass(createAMDGPUCodeGenPreparePass()); |
Matt Arsenault | 0c32938 | 2017-01-30 18:40:29 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Tom Stellard | fd25395 | 2015-08-07 23:19:30 +0000 | [diff] [blame] | 632 | // Handle uses of OpenCL image2d_t, image3d_t and sampler_t arguments. |
| 633 | addPass(createAMDGPUOpenCLImageTypeLoweringPass()); |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 634 | |
Yaxun Liu | de4b88d | 2017-10-10 19:39:48 +0000 | [diff] [blame] | 635 | // Replace OpenCL enqueued block function pointers with global variables. |
| 636 | addPass(createAMDGPUOpenCLEnqueuedBlockLoweringPass()); |
| 637 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 638 | if (TM.getOptLevel() > CodeGenOpt::None) { |
Matt Arsenault | 417e007 | 2017-02-08 06:16:04 +0000 | [diff] [blame] | 639 | addPass(createInferAddressSpacesPass()); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 640 | addPass(createAMDGPUPromoteAlloca()); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 641 | |
| 642 | if (EnableSROA) |
| 643 | addPass(createSROAPass()); |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 644 | |
Konstantin Zhuravlyov | 4658e5f | 2016-09-30 16:39:24 +0000 | [diff] [blame] | 645 | addStraightLineScalarOptimizationPasses(); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 646 | |
| 647 | if (EnableAMDGPUAliasAnalysis) { |
| 648 | addPass(createAMDGPUAAWrapperPass()); |
| 649 | addPass(createExternalAAWrapperPass([](Pass &P, Function &, |
| 650 | AAResults &AAR) { |
| 651 | if (auto *WrapperPass = P.getAnalysisIfAvailable<AMDGPUAAWrapperPass>()) |
| 652 | AAR.addAAResult(WrapperPass->getResult()); |
| 653 | })); |
| 654 | } |
Konstantin Zhuravlyov | 4658e5f | 2016-09-30 16:39:24 +0000 | [diff] [blame] | 655 | } |
Matt Arsenault | f42c692 | 2016-06-15 00:11:01 +0000 | [diff] [blame] | 656 | |
| 657 | TargetPassConfig::addIRPasses(); |
| 658 | |
| 659 | // EarlyCSE is not always strong enough to clean up what LSR produces. For |
| 660 | // example, GVN can combine |
| 661 | // |
| 662 | // %0 = add %a, %b |
| 663 | // %1 = add %b, %a |
| 664 | // |
| 665 | // and |
| 666 | // |
| 667 | // %0 = shl nsw %a, 2 |
| 668 | // %1 = shl %a, 2 |
| 669 | // |
| 670 | // but EarlyCSE can do neither of them. |
| 671 | if (getOptLevel() != CodeGenOpt::None) |
| 672 | addEarlyCSEOrGVNPass(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Matt Arsenault | 908b9e2 | 2016-07-01 03:33:52 +0000 | [diff] [blame] | 675 | void AMDGPUPassConfig::addCodeGenPrepare() { |
| 676 | TargetPassConfig::addCodeGenPrepare(); |
| 677 | |
| 678 | if (EnableLoadStoreVectorizer) |
| 679 | addPass(createLoadStoreVectorizerPass()); |
| 680 | } |
| 681 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 682 | bool AMDGPUPassConfig::addPreISel() { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 683 | addPass(createFlattenCFGPass()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 684 | return false; |
| 685 | } |
| 686 | |
| 687 | bool AMDGPUPassConfig::addInstSelector() { |
Matt Arsenault | 7016f13 | 2017-08-03 22:30:46 +0000 | [diff] [blame] | 688 | addPass(createAMDGPUISelDag(&getAMDGPUTargetMachine(), getOptLevel())); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 689 | return false; |
| 690 | } |
| 691 | |
Matt Arsenault | 0a10900 | 2015-09-25 17:41:20 +0000 | [diff] [blame] | 692 | bool AMDGPUPassConfig::addGCPasses() { |
| 693 | // Do nothing. GC is not supported. |
| 694 | return false; |
| 695 | } |
| 696 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 697 | //===----------------------------------------------------------------------===// |
| 698 | // R600 Pass Setup |
| 699 | //===----------------------------------------------------------------------===// |
| 700 | |
| 701 | bool R600PassConfig::addPreISel() { |
| 702 | AMDGPUPassConfig::addPreISel(); |
Matt Arsenault | c581611 | 2016-06-24 06:30:22 +0000 | [diff] [blame] | 703 | |
| 704 | if (EnableR600StructurizeCFG) |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 705 | addPass(createStructurizeCFGPass()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | |
Tom Stellard | 2028769 | 2017-08-08 04:57:55 +0000 | [diff] [blame] | 709 | bool R600PassConfig::addInstSelector() { |
| 710 | addPass(createR600ISelDag(&getAMDGPUTargetMachine(), getOptLevel())); |
| 711 | return false; |
| 712 | } |
| 713 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 714 | void R600PassConfig::addPreRegAlloc() { |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 715 | addPass(createR600VectorRegMerger()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | void R600PassConfig::addPreSched2() { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 719 | addPass(createR600EmitClauseMarkers(), false); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 720 | if (EnableR600IfConvert) |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 721 | addPass(&IfConverterID, false); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 722 | addPass(createR600ClauseMergePass(), false); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void R600PassConfig::addPreEmitPass() { |
| 726 | addPass(createAMDGPUCFGStructurizerPass(), false); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 727 | addPass(createR600ExpandSpecialInstrsPass(), false); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 728 | addPass(&FinalizeMachineBundlesID, false); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 729 | addPass(createR600Packetizer(), false); |
| 730 | addPass(createR600ControlFlowFinalizer(), false); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | TargetPassConfig *R600TargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 734 | return new R600PassConfig(*this, PM); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | //===----------------------------------------------------------------------===// |
| 738 | // GCN Pass Setup |
| 739 | //===----------------------------------------------------------------------===// |
| 740 | |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 741 | ScheduleDAGInstrs *GCNPassConfig::createMachineScheduler( |
| 742 | MachineSchedContext *C) const { |
| 743 | const SISubtarget &ST = C->MF->getSubtarget<SISubtarget>(); |
| 744 | if (ST.enableSIScheduler()) |
| 745 | return createSIMachineScheduler(C); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 746 | return createGCNMaxOccupancyMachineScheduler(C); |
Matt Arsenault | 03d8584 | 2016-06-27 20:32:13 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 749 | bool GCNPassConfig::addPreISel() { |
| 750 | AMDGPUPassConfig::addPreISel(); |
Matt Arsenault | 3931948 | 2015-11-06 18:01:57 +0000 | [diff] [blame] | 751 | |
| 752 | // FIXME: We need to run a pass to propagate the attributes when calls are |
| 753 | // supported. |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 754 | addPass(createAMDGPUAnnotateKernelFeaturesPass()); |
Matt Arsenault | b8f8dbc | 2017-03-24 19:52:05 +0000 | [diff] [blame] | 755 | |
| 756 | // Merge divergent exit nodes. StructurizeCFG won't recognize the multi-exit |
| 757 | // regions formed by them. |
| 758 | addPass(&AMDGPUUnifyDivergentExitNodesID); |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 759 | if (!LateCFGStructurize) { |
| 760 | addPass(createStructurizeCFGPass(true)); // true -> SkipUniformRegions |
| 761 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 762 | addPass(createSinkingPass()); |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 763 | addPass(createAMDGPUAnnotateUniformValues()); |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 764 | if (!LateCFGStructurize) { |
| 765 | addPass(createSIAnnotateControlFlowPass()); |
| 766 | } |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 767 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 768 | return false; |
| 769 | } |
| 770 | |
Matt Arsenault | 3d1c1de | 2016-04-14 21:58:24 +0000 | [diff] [blame] | 771 | void GCNPassConfig::addMachineSSAOptimization() { |
| 772 | TargetPassConfig::addMachineSSAOptimization(); |
| 773 | |
| 774 | // We want to fold operands after PeepholeOptimizer has run (or as part of |
| 775 | // it), because it will eliminate extra copies making it easier to fold the |
| 776 | // real source operand. We want to eliminate dead instructions after, so that |
| 777 | // we see fewer uses of the copies. We then need to clean up the dead |
| 778 | // instructions leftover after the operands are folded as well. |
| 779 | // |
| 780 | // XXX - Can we get away without running DeadMachineInstructionElim again? |
| 781 | addPass(&SIFoldOperandsID); |
| 782 | addPass(&DeadMachineInstructionElimID); |
Tom Stellard | c2ff0eb | 2016-08-29 19:15:22 +0000 | [diff] [blame] | 783 | addPass(&SILoadStoreOptimizerID); |
Sam Kolton | 6e79529 | 2017-04-07 10:53:12 +0000 | [diff] [blame] | 784 | if (EnableSDWAPeephole) { |
| 785 | addPass(&SIPeepholeSDWAID); |
Stanislav Mekhanoshin | 56ea488 | 2017-05-30 16:49:24 +0000 | [diff] [blame] | 786 | addPass(&MachineLICMID); |
| 787 | addPass(&MachineCSEID); |
| 788 | addPass(&SIFoldOperandsID); |
Sam Kolton | 6e79529 | 2017-04-07 10:53:12 +0000 | [diff] [blame] | 789 | addPass(&DeadMachineInstructionElimID); |
| 790 | } |
Stanislav Mekhanoshin | 0330660 | 2017-06-03 17:39:47 +0000 | [diff] [blame] | 791 | addPass(createSIShrinkInstructionsPass()); |
Matt Arsenault | 3d1c1de | 2016-04-14 21:58:24 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Matt Arsenault | 9f5e0ef | 2017-01-25 04:25:02 +0000 | [diff] [blame] | 794 | bool GCNPassConfig::addILPOpts() { |
| 795 | if (EnableEarlyIfConversion) |
| 796 | addPass(&EarlyIfConverterID); |
| 797 | |
| 798 | TargetPassConfig::addILPOpts(); |
| 799 | return false; |
| 800 | } |
| 801 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 802 | bool GCNPassConfig::addInstSelector() { |
| 803 | AMDGPUPassConfig::addInstSelector(); |
| 804 | addPass(createSILowerI1CopiesPass()); |
Matt Arsenault | 782c03b | 2015-11-03 22:30:13 +0000 | [diff] [blame] | 805 | addPass(&SIFixSGPRCopiesID); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 806 | return false; |
| 807 | } |
| 808 | |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 809 | bool GCNPassConfig::addIRTranslator() { |
| 810 | addPass(new IRTranslator()); |
| 811 | return false; |
| 812 | } |
| 813 | |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 814 | bool GCNPassConfig::addLegalizeMachineIR() { |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 815 | addPass(new Legalizer()); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 816 | return false; |
| 817 | } |
| 818 | |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 819 | bool GCNPassConfig::addRegBankSelect() { |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 820 | addPass(new RegBankSelect()); |
Tom Stellard | 000c5af | 2016-04-14 19:09:28 +0000 | [diff] [blame] | 821 | return false; |
| 822 | } |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 823 | |
| 824 | bool GCNPassConfig::addGlobalInstructionSelect() { |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 825 | addPass(new InstructionSelect()); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 826 | return false; |
| 827 | } |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 828 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 829 | void GCNPassConfig::addPreRegAlloc() { |
Jan Sjodin | a06bfe0 | 2017-05-15 20:18:37 +0000 | [diff] [blame] | 830 | if (LateCFGStructurize) { |
| 831 | addPass(createAMDGPUMachineCFGStructurizerPass()); |
| 832 | } |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 833 | addPass(createSIWholeQuadModePass()); |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | void GCNPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) { |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 837 | // FIXME: We have to disable the verifier here because of PHIElimination + |
| 838 | // TwoAddressInstructions disabling it. |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 839 | |
| 840 | // This must be run immediately after phi elimination and before |
| 841 | // TwoAddressInstructions, otherwise the processing of the tied operand of |
| 842 | // SI_ELSE will introduce a copy of the tied operand source after the else. |
| 843 | insertPass(&PHIEliminationID, &SILowerControlFlowID, false); |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 844 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 845 | // This must be run after SILowerControlFlow, since it needs to use the |
| 846 | // machine-level CFG, but before register allocation. |
| 847 | insertPass(&SILowerControlFlowID, &SIFixWWMLivenessID, false); |
| 848 | |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 849 | TargetPassConfig::addFastRegAlloc(RegAllocPass); |
| 850 | } |
| 851 | |
| 852 | void GCNPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) { |
Matt Arsenault | 9d288e6 | 2017-08-07 18:12:48 +0000 | [diff] [blame] | 853 | insertPass(&MachineSchedulerID, &SIOptimizeExecMaskingPreRAID); |
Stanislav Mekhanoshin | 37e7f95 | 2017-08-01 23:14:32 +0000 | [diff] [blame] | 854 | |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 855 | // This must be run immediately after phi elimination and before |
| 856 | // TwoAddressInstructions, otherwise the processing of the tied operand of |
| 857 | // SI_ELSE will introduce a copy of the tied operand source after the else. |
| 858 | insertPass(&PHIEliminationID, &SILowerControlFlowID, false); |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 859 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 860 | // This must be run after SILowerControlFlow, since it needs to use the |
| 861 | // machine-level CFG, but before register allocation. |
| 862 | insertPass(&SILowerControlFlowID, &SIFixWWMLivenessID, false); |
| 863 | |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 864 | TargetPassConfig::addOptimizedRegAlloc(RegAllocPass); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 867 | void GCNPassConfig::addPostRegAlloc() { |
Stanislav Mekhanoshin | 22a56f2 | 2017-01-24 17:46:17 +0000 | [diff] [blame] | 868 | addPass(&SIFixVGPRCopiesID); |
Matt Arsenault | e674075 | 2016-09-29 01:44:16 +0000 | [diff] [blame] | 869 | addPass(&SIOptimizeExecMaskingID); |
| 870 | TargetPassConfig::addPostRegAlloc(); |
| 871 | } |
| 872 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 873 | void GCNPassConfig::addPreSched2() { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | void GCNPassConfig::addPreEmitPass() { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 877 | // The hazard recognizer that runs as part of the post-ra scheduler does not |
Matt Arsenault | 254a645 | 2016-06-28 16:59:53 +0000 | [diff] [blame] | 878 | // guarantee to be able handle all hazards correctly. This is because if there |
| 879 | // are multiple scheduling regions in a basic block, the regions are scheduled |
| 880 | // bottom up, so when we begin to schedule a region we don't know what |
| 881 | // instructions were emitted directly before it. |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 882 | // |
Matt Arsenault | 254a645 | 2016-06-28 16:59:53 +0000 | [diff] [blame] | 883 | // Here we add a stand-alone hazard recognizer pass which can handle all |
| 884 | // cases. |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 885 | addPass(&PostRAHazardRecognizerID); |
| 886 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 887 | if (EnableSIInsertWaitcntsPass) |
| 888 | addPass(createSIInsertWaitcntsPass()); |
| 889 | else |
| 890 | addPass(createSIInsertWaitsPass()); |
Matt Arsenault | cf2744f | 2016-04-29 20:23:42 +0000 | [diff] [blame] | 891 | addPass(createSIShrinkInstructionsPass()); |
Matt Arsenault | 78fc9da | 2016-08-22 19:33:16 +0000 | [diff] [blame] | 892 | addPass(&SIInsertSkipsPassID); |
Konstantin Zhuravlyov | e9a5a77 | 2017-07-21 21:19:23 +0000 | [diff] [blame] | 893 | addPass(createSIMemoryLegalizerPass()); |
Matt Arsenault | 9babdf4 | 2016-06-22 20:15:28 +0000 | [diff] [blame] | 894 | addPass(createSIDebuggerInsertNopsPass()); |
Matt Arsenault | 6bc43d8 | 2016-10-06 16:20:41 +0000 | [diff] [blame] | 895 | addPass(&BranchRelaxationPassID); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | TargetPassConfig *GCNTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 899 | return new GCNPassConfig(*this, PM); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 900 | } |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 901 | |