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