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