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