1 //===-- AMDGPUTargetMachine.cpp - TargetMachine for hw codegen targets-----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// The AMDGPU target machine contains all of the hardware specific
11 /// information  needed to emit code for R600 and SI GPUs.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AMDGPUTargetMachine.h"
16 #include "AMDGPU.h"
17 #include "AMDGPUAliasAnalysis.h"
18 #include "AMDGPUCallLowering.h"
19 #include "AMDGPUInstructionSelector.h"
20 #include "AMDGPULegalizerInfo.h"
21 #include "AMDGPUMacroFusion.h"
22 #include "AMDGPUTargetObjectFile.h"
23 #include "AMDGPUTargetTransformInfo.h"
24 #include "GCNIterativeScheduler.h"
25 #include "GCNSchedStrategy.h"
26 #include "R600MachineScheduler.h"
27 #include "SIMachineFunctionInfo.h"
28 #include "SIMachineScheduler.h"
29 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
30 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
31 #include "llvm/CodeGen/GlobalISel/Legalizer.h"
32 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
33 #include "llvm/CodeGen/MIRParser/MIParser.h"
34 #include "llvm/CodeGen/Passes.h"
35 #include "llvm/CodeGen/TargetPassConfig.h"
36 #include "llvm/IR/Attributes.h"
37 #include "llvm/IR/Function.h"
38 #include "llvm/IR/LegacyPassManager.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Target/TargetLoweringObjectFile.h"
44 #include "llvm/Transforms/IPO.h"
45 #include "llvm/Transforms/IPO/AlwaysInliner.h"
46 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
47 #include "llvm/Transforms/Scalar.h"
48 #include "llvm/Transforms/Scalar/GVN.h"
49 #include "llvm/Transforms/Utils.h"
50 #include "llvm/Transforms/Vectorize.h"
51 #include <memory>
52 
53 using namespace llvm;
54 
55 static cl::opt<bool> EnableR600StructurizeCFG(
56   "r600-ir-structurize",
57   cl::desc("Use StructurizeCFG IR pass"),
58   cl::init(true));
59 
60 static cl::opt<bool> EnableSROA(
61   "amdgpu-sroa",
62   cl::desc("Run SROA after promote alloca pass"),
63   cl::ReallyHidden,
64   cl::init(true));
65 
66 static cl::opt<bool>
67 EnableEarlyIfConversion("amdgpu-early-ifcvt", cl::Hidden,
68                         cl::desc("Run early if-conversion"),
69                         cl::init(false));
70 
71 static cl::opt<bool>
72 OptExecMaskPreRA("amdgpu-opt-exec-mask-pre-ra", cl::Hidden,
73             cl::desc("Run pre-RA exec mask optimizations"),
74             cl::init(true));
75 
76 static cl::opt<bool> EnableR600IfConvert(
77   "r600-if-convert",
78   cl::desc("Use if conversion pass"),
79   cl::ReallyHidden,
80   cl::init(true));
81 
82 // Option to disable vectorizer for tests.
83 static cl::opt<bool> EnableLoadStoreVectorizer(
84   "amdgpu-load-store-vectorizer",
85   cl::desc("Enable load store vectorizer"),
86   cl::init(true),
87   cl::Hidden);
88 
89 // Option to control global loads scalarization
90 static cl::opt<bool> ScalarizeGlobal(
91   "amdgpu-scalarize-global-loads",
92   cl::desc("Enable global load scalarization"),
93   cl::init(true),
94   cl::Hidden);
95 
96 // Option to run internalize pass.
97 static cl::opt<bool> InternalizeSymbols(
98   "amdgpu-internalize-symbols",
99   cl::desc("Enable elimination of non-kernel functions and unused globals"),
100   cl::init(false),
101   cl::Hidden);
102 
103 // Option to inline all early.
104 static cl::opt<bool> EarlyInlineAll(
105   "amdgpu-early-inline-all",
106   cl::desc("Inline all functions early"),
107   cl::init(false),
108   cl::Hidden);
109 
110 static cl::opt<bool> EnableSDWAPeephole(
111   "amdgpu-sdwa-peephole",
112   cl::desc("Enable SDWA peepholer"),
113   cl::init(true));
114 
115 static cl::opt<bool> EnableDPPCombine(
116   "amdgpu-dpp-combine",
117   cl::desc("Enable DPP combiner"),
118   cl::init(true));
119 
120 // Enable address space based alias analysis
121 static cl::opt<bool> EnableAMDGPUAliasAnalysis("enable-amdgpu-aa", cl::Hidden,
122   cl::desc("Enable AMDGPU Alias Analysis"),
123   cl::init(true));
124 
125 // Option to run late CFG structurizer
126 static cl::opt<bool, true> LateCFGStructurize(
127   "amdgpu-late-structurize",
128   cl::desc("Enable late CFG structurization"),
129   cl::location(AMDGPUTargetMachine::EnableLateStructurizeCFG),
130   cl::Hidden);
131 
132 static cl::opt<bool, true> EnableAMDGPUFunctionCallsOpt(
133   "amdgpu-function-calls",
134   cl::desc("Enable AMDGPU function call support"),
135   cl::location(AMDGPUTargetMachine::EnableFunctionCalls),
136   cl::init(true),
137   cl::Hidden);
138 
139 // Enable lib calls simplifications
140 static cl::opt<bool> EnableLibCallSimplify(
141   "amdgpu-simplify-libcall",
142   cl::desc("Enable amdgpu library simplifications"),
143   cl::init(true),
144   cl::Hidden);
145 
146 static cl::opt<bool> EnableLowerKernelArguments(
147   "amdgpu-ir-lower-kernel-arguments",
148   cl::desc("Lower kernel argument loads in IR pass"),
149   cl::init(true),
150   cl::Hidden);
151 
152 // Enable atomic optimization
153 static cl::opt<bool> EnableAtomicOptimizations(
154   "amdgpu-atomic-optimizations",
155   cl::desc("Enable atomic optimizations"),
156   cl::init(false),
157   cl::Hidden);
158 
159 // Enable Mode register optimization
160 static cl::opt<bool> EnableSIModeRegisterPass(
161   "amdgpu-mode-register",
162   cl::desc("Enable mode register pass"),
163   cl::init(true),
164   cl::Hidden);
165 
166 // Option is used in lit tests to prevent deadcoding of patterns inspected.
167 static cl::opt<bool>
168 EnableDCEInRA("amdgpu-dce-in-ra",
169     cl::init(true), cl::Hidden,
170     cl::desc("Enable machine DCE inside regalloc"));
171 
172 static cl::opt<bool> EnableScalarIRPasses(
173   "amdgpu-scalar-ir-passes",
174   cl::desc("Enable scalar IR passes"),
175   cl::init(true),
176   cl::Hidden);
177 
178 extern "C" void LLVMInitializeAMDGPUTarget() {
179   // Register the target
180   RegisterTargetMachine<R600TargetMachine> X(getTheAMDGPUTarget());
181   RegisterTargetMachine<GCNTargetMachine> Y(getTheGCNTarget());
182 
183   PassRegistry *PR = PassRegistry::getPassRegistry();
184   initializeR600ClauseMergePassPass(*PR);
185   initializeR600ControlFlowFinalizerPass(*PR);
186   initializeR600PacketizerPass(*PR);
187   initializeR600ExpandSpecialInstrsPassPass(*PR);
188   initializeR600VectorRegMergerPass(*PR);
189   initializeGlobalISel(*PR);
190   initializeAMDGPUDAGToDAGISelPass(*PR);
191   initializeGCNDPPCombinePass(*PR);
192   initializeSILowerI1CopiesPass(*PR);
193   initializeSIFixSGPRCopiesPass(*PR);
194   initializeSIFixVGPRCopiesPass(*PR);
195   initializeSIFixupVectorISelPass(*PR);
196   initializeSIFoldOperandsPass(*PR);
197   initializeSIPeepholeSDWAPass(*PR);
198   initializeSIShrinkInstructionsPass(*PR);
199   initializeSIOptimizeExecMaskingPreRAPass(*PR);
200   initializeSILoadStoreOptimizerPass(*PR);
201   initializeAMDGPUFixFunctionBitcastsPass(*PR);
202   initializeAMDGPUAlwaysInlinePass(*PR);
203   initializeAMDGPUAnnotateKernelFeaturesPass(*PR);
204   initializeAMDGPUAnnotateUniformValuesPass(*PR);
205   initializeAMDGPUArgumentUsageInfoPass(*PR);
206   initializeAMDGPUAtomicOptimizerPass(*PR);
207   initializeAMDGPULowerKernelArgumentsPass(*PR);
208   initializeAMDGPULowerKernelAttributesPass(*PR);
209   initializeAMDGPULowerIntrinsicsPass(*PR);
210   initializeAMDGPUOpenCLEnqueuedBlockLoweringPass(*PR);
211   initializeAMDGPUPromoteAllocaPass(*PR);
212   initializeAMDGPUCodeGenPreparePass(*PR);
213   initializeAMDGPURewriteOutArgumentsPass(*PR);
214   initializeAMDGPUUnifyMetadataPass(*PR);
215   initializeSIAnnotateControlFlowPass(*PR);
216   initializeSIInsertWaitcntsPass(*PR);
217   initializeSIModeRegisterPass(*PR);
218   initializeSIWholeQuadModePass(*PR);
219   initializeSILowerControlFlowPass(*PR);
220   initializeSIInsertSkipsPass(*PR);
221   initializeSIMemoryLegalizerPass(*PR);
222   initializeSIOptimizeExecMaskingPass(*PR);
223   initializeSIPreAllocateWWMRegsPass(*PR);
224   initializeSIFormMemoryClausesPass(*PR);
225   initializeAMDGPUUnifyDivergentExitNodesPass(*PR);
226   initializeAMDGPUAAWrapperPassPass(*PR);
227   initializeAMDGPUExternalAAWrapperPass(*PR);
228   initializeAMDGPUUseNativeCallsPass(*PR);
229   initializeAMDGPUSimplifyLibCallsPass(*PR);
230   initializeAMDGPUInlinerPass(*PR);
231 }
232 
233 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
234   return llvm::make_unique<AMDGPUTargetObjectFile>();
235 }
236 
237 static ScheduleDAGInstrs *createR600MachineScheduler(MachineSchedContext *C) {
238   return new ScheduleDAGMILive(C, llvm::make_unique<R600SchedStrategy>());
239 }
240 
241 static ScheduleDAGInstrs *createSIMachineScheduler(MachineSchedContext *C) {
242   return new SIScheduleDAGMI(C);
243 }
244 
245 static ScheduleDAGInstrs *
246 createGCNMaxOccupancyMachineScheduler(MachineSchedContext *C) {
247   ScheduleDAGMILive *DAG =
248     new GCNScheduleDAGMILive(C, make_unique<GCNMaxOccupancySchedStrategy>(C));
249   DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
250   DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
251   DAG->addMutation(createAMDGPUMacroFusionDAGMutation());
252   return DAG;
253 }
254 
255 static ScheduleDAGInstrs *
256 createIterativeGCNMaxOccupancyMachineScheduler(MachineSchedContext *C) {
257   auto DAG = new GCNIterativeScheduler(C,
258     GCNIterativeScheduler::SCHEDULE_LEGACYMAXOCCUPANCY);
259   DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
260   DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
261   return DAG;
262 }
263 
264 static ScheduleDAGInstrs *createMinRegScheduler(MachineSchedContext *C) {
265   return new GCNIterativeScheduler(C,
266     GCNIterativeScheduler::SCHEDULE_MINREGFORCED);
267 }
268 
269 static ScheduleDAGInstrs *
270 createIterativeILPMachineScheduler(MachineSchedContext *C) {
271   auto DAG = new GCNIterativeScheduler(C,
272     GCNIterativeScheduler::SCHEDULE_ILP);
273   DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
274   DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
275   DAG->addMutation(createAMDGPUMacroFusionDAGMutation());
276   return DAG;
277 }
278 
279 static MachineSchedRegistry
280 R600SchedRegistry("r600", "Run R600's custom scheduler",
281                    createR600MachineScheduler);
282 
283 static MachineSchedRegistry
284 SISchedRegistry("si", "Run SI's custom scheduler",
285                 createSIMachineScheduler);
286 
287 static MachineSchedRegistry
288 GCNMaxOccupancySchedRegistry("gcn-max-occupancy",
289                              "Run GCN scheduler to maximize occupancy",
290                              createGCNMaxOccupancyMachineScheduler);
291 
292 static MachineSchedRegistry
293 IterativeGCNMaxOccupancySchedRegistry("gcn-max-occupancy-experimental",
294   "Run GCN scheduler to maximize occupancy (experimental)",
295   createIterativeGCNMaxOccupancyMachineScheduler);
296 
297 static MachineSchedRegistry
298 GCNMinRegSchedRegistry("gcn-minreg",
299   "Run GCN iterative scheduler for minimal register usage (experimental)",
300   createMinRegScheduler);
301 
302 static MachineSchedRegistry
303 GCNILPSchedRegistry("gcn-ilp",
304   "Run GCN iterative scheduler for ILP scheduling (experimental)",
305   createIterativeILPMachineScheduler);
306 
307 static StringRef computeDataLayout(const Triple &TT) {
308   if (TT.getArch() == Triple::r600) {
309     // 32-bit pointers.
310       return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
311              "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5";
312   }
313 
314   // 32-bit private, local, and region pointers. 64-bit global, constant and
315   // flat, non-integral buffer fat pointers.
316     return "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32"
317          "-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
318          "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5"
319          "-ni:7";
320 }
321 
322 LLVM_READNONE
323 static StringRef getGPUOrDefault(const Triple &TT, StringRef GPU) {
324   if (!GPU.empty())
325     return GPU;
326 
327   // Need to default to a target with flat support for HSA.
328   if (TT.getArch() == Triple::amdgcn)
329     return TT.getOS() == Triple::AMDHSA ? "generic-hsa" : "generic";
330 
331   return "r600";
332 }
333 
334 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
335   // The AMDGPU toolchain only supports generating shared objects, so we
336   // must always use PIC.
337   return Reloc::PIC_;
338 }
339 
340 AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT,
341                                          StringRef CPU, StringRef FS,
342                                          TargetOptions Options,
343                                          Optional<Reloc::Model> RM,
344                                          Optional<CodeModel::Model> CM,
345                                          CodeGenOpt::Level OptLevel)
346     : LLVMTargetMachine(T, computeDataLayout(TT), TT, getGPUOrDefault(TT, CPU),
347                         FS, Options, getEffectiveRelocModel(RM),
348                         getEffectiveCodeModel(CM, CodeModel::Small), OptLevel),
349       TLOF(createTLOF(getTargetTriple())) {
350   initAsmInfo();
351 }
352 
353 bool AMDGPUTargetMachine::EnableLateStructurizeCFG = false;
354 bool AMDGPUTargetMachine::EnableFunctionCalls = false;
355 
356 AMDGPUTargetMachine::~AMDGPUTargetMachine() = default;
357 
358 StringRef AMDGPUTargetMachine::getGPUName(const Function &F) const {
359   Attribute GPUAttr = F.getFnAttribute("target-cpu");
360   return GPUAttr.hasAttribute(Attribute::None) ?
361     getTargetCPU() : GPUAttr.getValueAsString();
362 }
363 
364 StringRef AMDGPUTargetMachine::getFeatureString(const Function &F) const {
365   Attribute FSAttr = F.getFnAttribute("target-features");
366 
367   return FSAttr.hasAttribute(Attribute::None) ?
368     getTargetFeatureString() :
369     FSAttr.getValueAsString();
370 }
371 
372 /// Predicate for Internalize pass.
373 static bool mustPreserveGV(const GlobalValue &GV) {
374   if (const Function *F = dyn_cast<Function>(&GV))
375     return F->isDeclaration() || AMDGPU::isEntryFunctionCC(F->getCallingConv());
376 
377   return !GV.use_empty();
378 }
379 
380 void AMDGPUTargetMachine::adjustPassManager(PassManagerBuilder &Builder) {
381   Builder.DivergentTarget = true;
382 
383   bool EnableOpt = getOptLevel() > CodeGenOpt::None;
384   bool Internalize = InternalizeSymbols;
385   bool EarlyInline = EarlyInlineAll && EnableOpt && !EnableFunctionCalls;
386   bool AMDGPUAA = EnableAMDGPUAliasAnalysis && EnableOpt;
387   bool LibCallSimplify = EnableLibCallSimplify && EnableOpt;
388 
389   if (EnableFunctionCalls) {
390     delete Builder.Inliner;
391     Builder.Inliner = createAMDGPUFunctionInliningPass();
392   }
393 
394   Builder.addExtension(
395     PassManagerBuilder::EP_ModuleOptimizerEarly,
396     [Internalize, EarlyInline, AMDGPUAA](const PassManagerBuilder &,
397                                          legacy::PassManagerBase &PM) {
398       if (AMDGPUAA) {
399         PM.add(createAMDGPUAAWrapperPass());
400         PM.add(createAMDGPUExternalAAWrapperPass());
401       }
402       PM.add(createAMDGPUUnifyMetadataPass());
403       if (Internalize) {
404         PM.add(createInternalizePass(mustPreserveGV));
405         PM.add(createGlobalDCEPass());
406       }
407       if (EarlyInline)
408         PM.add(createAMDGPUAlwaysInlinePass(false));
409   });
410 
411   const auto &Opt = Options;
412   Builder.addExtension(
413     PassManagerBuilder::EP_EarlyAsPossible,
414     [AMDGPUAA, LibCallSimplify, &Opt](const PassManagerBuilder &,
415                                       legacy::PassManagerBase &PM) {
416       if (AMDGPUAA) {
417         PM.add(createAMDGPUAAWrapperPass());
418         PM.add(createAMDGPUExternalAAWrapperPass());
419       }
420       PM.add(llvm::createAMDGPUUseNativeCallsPass());
421       if (LibCallSimplify)
422         PM.add(llvm::createAMDGPUSimplifyLibCallsPass(Opt));
423   });
424 
425   Builder.addExtension(
426     PassManagerBuilder::EP_CGSCCOptimizerLate,
427     [](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
428       // Add infer address spaces pass to the opt pipeline after inlining
429       // but before SROA to increase SROA opportunities.
430       PM.add(createInferAddressSpacesPass());
431 
432       // This should run after inlining to have any chance of doing anything,
433       // and before other cleanup optimizations.
434       PM.add(createAMDGPULowerKernelAttributesPass());
435   });
436 }
437 
438 //===----------------------------------------------------------------------===//
439 // R600 Target Machine (R600 -> Cayman)
440 //===----------------------------------------------------------------------===//
441 
442 R600TargetMachine::R600TargetMachine(const Target &T, const Triple &TT,
443                                      StringRef CPU, StringRef FS,
444                                      TargetOptions Options,
445                                      Optional<Reloc::Model> RM,
446                                      Optional<CodeModel::Model> CM,
447                                      CodeGenOpt::Level OL, bool JIT)
448     : AMDGPUTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
449   setRequiresStructuredCFG(true);
450 
451   // Override the default since calls aren't supported for r600.
452   if (EnableFunctionCalls &&
453       EnableAMDGPUFunctionCallsOpt.getNumOccurrences() == 0)
454     EnableFunctionCalls = false;
455 }
456 
457 const R600Subtarget *R600TargetMachine::getSubtargetImpl(
458   const Function &F) const {
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);
471     I = llvm::make_unique<R600Subtarget>(TargetTriple, GPU, FS, *this);
472   }
473 
474   return I.get();
475 }
476 
477 TargetTransformInfo
478 R600TargetMachine::getTargetTransformInfo(const Function &F) {
479   return TargetTransformInfo(R600TTIImpl(this, F));
480 }
481 
482 //===----------------------------------------------------------------------===//
483 // GCN Target Machine (SI+)
484 //===----------------------------------------------------------------------===//
485 
486 GCNTargetMachine::GCNTargetMachine(const Target &T, const Triple &TT,
487                                    StringRef CPU, StringRef FS,
488                                    TargetOptions Options,
489                                    Optional<Reloc::Model> RM,
490                                    Optional<CodeModel::Model> CM,
491                                    CodeGenOpt::Level OL, bool JIT)
492     : AMDGPUTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {}
493 
494 const GCNSubtarget *GCNTargetMachine::getSubtargetImpl(const Function &F) const {
495   StringRef GPU = getGPUName(F);
496   StringRef FS = getFeatureString(F);
497 
498   SmallString<128> SubtargetKey(GPU);
499   SubtargetKey.append(FS);
500 
501   auto &I = SubtargetMap[SubtargetKey];
502   if (!I) {
503     // This needs to be done before we create a new subtarget since any
504     // creation will depend on the TM and the code generation flags on the
505     // function that reside in TargetOptions.
506     resetTargetOptions(F);
507     I = llvm::make_unique<GCNSubtarget>(TargetTriple, GPU, FS, *this);
508   }
509 
510   I->setScalarizeGlobalBehavior(ScalarizeGlobal);
511 
512   return I.get();
513 }
514 
515 TargetTransformInfo
516 GCNTargetMachine::getTargetTransformInfo(const Function &F) {
517   return TargetTransformInfo(GCNTTIImpl(this, F));
518 }
519 
520 //===----------------------------------------------------------------------===//
521 // AMDGPU Pass Setup
522 //===----------------------------------------------------------------------===//
523 
524 namespace {
525 
526 class AMDGPUPassConfig : public TargetPassConfig {
527 public:
528   AMDGPUPassConfig(LLVMTargetMachine &TM, PassManagerBase &PM)
529     : TargetPassConfig(TM, PM) {
530     // Exceptions and StackMaps are not supported, so these passes will never do
531     // anything.
532     disablePass(&StackMapLivenessID);
533     disablePass(&FuncletLayoutID);
534   }
535 
536   AMDGPUTargetMachine &getAMDGPUTargetMachine() const {
537     return getTM<AMDGPUTargetMachine>();
538   }
539 
540   ScheduleDAGInstrs *
541   createMachineScheduler(MachineSchedContext *C) const override {
542     ScheduleDAGMILive *DAG = createGenericSchedLive(C);
543     DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
544     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
545     return DAG;
546   }
547 
548   void addEarlyCSEOrGVNPass();
549   void addStraightLineScalarOptimizationPasses();
550   void addIRPasses() override;
551   void addCodeGenPrepare() override;
552   bool addPreISel() override;
553   bool addInstSelector() override;
554   bool addGCPasses() override;
555 };
556 
557 class R600PassConfig final : public AMDGPUPassConfig {
558 public:
559   R600PassConfig(LLVMTargetMachine &TM, PassManagerBase &PM)
560     : AMDGPUPassConfig(TM, PM) {}
561 
562   ScheduleDAGInstrs *createMachineScheduler(
563     MachineSchedContext *C) const override {
564     return createR600MachineScheduler(C);
565   }
566 
567   bool addPreISel() override;
568   bool addInstSelector() override;
569   void addPreRegAlloc() override;
570   void addPreSched2() override;
571   void addPreEmitPass() override;
572 };
573 
574 class GCNPassConfig final : public AMDGPUPassConfig {
575 public:
576   GCNPassConfig(LLVMTargetMachine &TM, PassManagerBase &PM)
577     : AMDGPUPassConfig(TM, PM) {
578     // It is necessary to know the register usage of the entire call graph.  We
579     // allow calls without EnableAMDGPUFunctionCalls if they are marked
580     // noinline, so this is always required.
581     setRequiresCodeGenSCCOrder(true);
582   }
583 
584   GCNTargetMachine &getGCNTargetMachine() const {
585     return getTM<GCNTargetMachine>();
586   }
587 
588   ScheduleDAGInstrs *
589   createMachineScheduler(MachineSchedContext *C) const override;
590 
591   bool addPreISel() override;
592   void addMachineSSAOptimization() override;
593   bool addILPOpts() override;
594   bool addInstSelector() override;
595   bool addIRTranslator() override;
596   bool addLegalizeMachineIR() override;
597   bool addRegBankSelect() override;
598   bool addGlobalInstructionSelect() override;
599   void addFastRegAlloc() override;
600   void addOptimizedRegAlloc() override;
601   void addPreRegAlloc() override;
602   void addPostRegAlloc() override;
603   void addPreSched2() override;
604   void addPreEmitPass() override;
605 };
606 
607 } // end anonymous namespace
608 
609 void AMDGPUPassConfig::addEarlyCSEOrGVNPass() {
610   if (getOptLevel() == CodeGenOpt::Aggressive)
611     addPass(createGVNPass());
612   else
613     addPass(createEarlyCSEPass());
614 }
615 
616 void AMDGPUPassConfig::addStraightLineScalarOptimizationPasses() {
617   addPass(createLICMPass());
618   addPass(createSeparateConstOffsetFromGEPPass());
619   addPass(createSpeculativeExecutionPass());
620   // ReassociateGEPs exposes more opportunites for SLSR. See
621   // the example in reassociate-geps-and-slsr.ll.
622   addPass(createStraightLineStrengthReducePass());
623   // SeparateConstOffsetFromGEP and SLSR creates common expressions which GVN or
624   // EarlyCSE can reuse.
625   addEarlyCSEOrGVNPass();
626   // Run NaryReassociate after EarlyCSE/GVN to be more effective.
627   addPass(createNaryReassociatePass());
628   // NaryReassociate on GEPs creates redundant common expressions, so run
629   // EarlyCSE after it.
630   addPass(createEarlyCSEPass());
631 }
632 
633 void AMDGPUPassConfig::addIRPasses() {
634   const AMDGPUTargetMachine &TM = getAMDGPUTargetMachine();
635 
636   // There is no reason to run these.
637   disablePass(&StackMapLivenessID);
638   disablePass(&FuncletLayoutID);
639   disablePass(&PatchableFunctionID);
640 
641   addPass(createAtomicExpandPass());
642 
643   // This must occur before inlining, as the inliner will not look through
644   // bitcast calls.
645   addPass(createAMDGPUFixFunctionBitcastsPass());
646 
647   addPass(createAMDGPULowerIntrinsicsPass());
648 
649   // Function calls are not supported, so make sure we inline everything.
650   addPass(createAMDGPUAlwaysInlinePass());
651   addPass(createAlwaysInlinerLegacyPass());
652   // We need to add the barrier noop pass, otherwise adding the function
653   // inlining pass will cause all of the PassConfigs passes to be run
654   // one function at a time, which means if we have a nodule with two
655   // functions, then we will generate code for the first function
656   // without ever running any passes on the second.
657   addPass(createBarrierNoopPass());
658 
659   if (TM.getTargetTriple().getArch() == Triple::amdgcn) {
660     // TODO: May want to move later or split into an early and late one.
661 
662     addPass(createAMDGPUCodeGenPreparePass());
663   }
664 
665   // Handle uses of OpenCL image2d_t, image3d_t and sampler_t arguments.
666   if (TM.getTargetTriple().getArch() == Triple::r600)
667     addPass(createR600OpenCLImageTypeLoweringPass());
668 
669   // Replace OpenCL enqueued block function pointers with global variables.
670   addPass(createAMDGPUOpenCLEnqueuedBlockLoweringPass());
671 
672   if (TM.getOptLevel() > CodeGenOpt::None) {
673     addPass(createInferAddressSpacesPass());
674     addPass(createAMDGPUPromoteAlloca());
675 
676     if (EnableSROA)
677       addPass(createSROAPass());
678 
679     if (EnableScalarIRPasses)
680       addStraightLineScalarOptimizationPasses();
681 
682     if (EnableAMDGPUAliasAnalysis) {
683       addPass(createAMDGPUAAWrapperPass());
684       addPass(createExternalAAWrapperPass([](Pass &P, Function &,
685                                              AAResults &AAR) {
686         if (auto *WrapperPass = P.getAnalysisIfAvailable<AMDGPUAAWrapperPass>())
687           AAR.addAAResult(WrapperPass->getResult());
688         }));
689     }
690   }
691 
692   TargetPassConfig::addIRPasses();
693 
694   // EarlyCSE is not always strong enough to clean up what LSR produces. For
695   // example, GVN can combine
696   //
697   //   %0 = add %a, %b
698   //   %1 = add %b, %a
699   //
700   // and
701   //
702   //   %0 = shl nsw %a, 2
703   //   %1 = shl %a, 2
704   //
705   // but EarlyCSE can do neither of them.
706   if (getOptLevel() != CodeGenOpt::None && EnableScalarIRPasses)
707     addEarlyCSEOrGVNPass();
708 }
709 
710 void AMDGPUPassConfig::addCodeGenPrepare() {
711   if (TM->getTargetTriple().getArch() == Triple::amdgcn)
712     addPass(createAMDGPUAnnotateKernelFeaturesPass());
713 
714   if (TM->getTargetTriple().getArch() == Triple::amdgcn &&
715       EnableLowerKernelArguments)
716     addPass(createAMDGPULowerKernelArgumentsPass());
717 
718   TargetPassConfig::addCodeGenPrepare();
719 
720   if (EnableLoadStoreVectorizer)
721     addPass(createLoadStoreVectorizerPass());
722 }
723 
724 bool AMDGPUPassConfig::addPreISel() {
725   addPass(createLowerSwitchPass());
726   addPass(createFlattenCFGPass());
727   return false;
728 }
729 
730 bool AMDGPUPassConfig::addInstSelector() {
731   addPass(createAMDGPUISelDag(&getAMDGPUTargetMachine(), getOptLevel()));
732   return false;
733 }
734 
735 bool AMDGPUPassConfig::addGCPasses() {
736   // Do nothing. GC is not supported.
737   return false;
738 }
739 
740 //===----------------------------------------------------------------------===//
741 // R600 Pass Setup
742 //===----------------------------------------------------------------------===//
743 
744 bool R600PassConfig::addPreISel() {
745   AMDGPUPassConfig::addPreISel();
746 
747   if (EnableR600StructurizeCFG)
748     addPass(createStructurizeCFGPass());
749   return false;
750 }
751 
752 bool R600PassConfig::addInstSelector() {
753   addPass(createR600ISelDag(&getAMDGPUTargetMachine(), getOptLevel()));
754   return false;
755 }
756 
757 void R600PassConfig::addPreRegAlloc() {
758   addPass(createR600VectorRegMerger());
759 }
760 
761 void R600PassConfig::addPreSched2() {
762   addPass(createR600EmitClauseMarkers(), false);
763   if (EnableR600IfConvert)
764     addPass(&IfConverterID, false);
765   addPass(createR600ClauseMergePass(), false);
766 }
767 
768 void R600PassConfig::addPreEmitPass() {
769   addPass(createAMDGPUCFGStructurizerPass(), false);
770   addPass(createR600ExpandSpecialInstrsPass(), false);
771   addPass(&FinalizeMachineBundlesID, false);
772   addPass(createR600Packetizer(), false);
773   addPass(createR600ControlFlowFinalizer(), false);
774 }
775 
776 TargetPassConfig *R600TargetMachine::createPassConfig(PassManagerBase &PM) {
777   return new R600PassConfig(*this, PM);
778 }
779 
780 //===----------------------------------------------------------------------===//
781 // GCN Pass Setup
782 //===----------------------------------------------------------------------===//
783 
784 ScheduleDAGInstrs *GCNPassConfig::createMachineScheduler(
785   MachineSchedContext *C) const {
786   const GCNSubtarget &ST = C->MF->getSubtarget<GCNSubtarget>();
787   if (ST.enableSIScheduler())
788     return createSIMachineScheduler(C);
789   return createGCNMaxOccupancyMachineScheduler(C);
790 }
791 
792 bool GCNPassConfig::addPreISel() {
793   AMDGPUPassConfig::addPreISel();
794 
795   if (EnableAtomicOptimizations) {
796     addPass(createAMDGPUAtomicOptimizerPass());
797   }
798 
799   // FIXME: We need to run a pass to propagate the attributes when calls are
800   // supported.
801 
802   // Merge divergent exit nodes. StructurizeCFG won't recognize the multi-exit
803   // regions formed by them.
804   addPass(&AMDGPUUnifyDivergentExitNodesID);
805   if (!LateCFGStructurize) {
806     addPass(createStructurizeCFGPass(true)); // true -> SkipUniformRegions
807   }
808   addPass(createSinkingPass());
809   addPass(createAMDGPUAnnotateUniformValues());
810   if (!LateCFGStructurize) {
811     addPass(createSIAnnotateControlFlowPass());
812   }
813 
814   return false;
815 }
816 
817 void GCNPassConfig::addMachineSSAOptimization() {
818   TargetPassConfig::addMachineSSAOptimization();
819 
820   // We want to fold operands after PeepholeOptimizer has run (or as part of
821   // it), because it will eliminate extra copies making it easier to fold the
822   // real source operand. We want to eliminate dead instructions after, so that
823   // we see fewer uses of the copies. We then need to clean up the dead
824   // instructions leftover after the operands are folded as well.
825   //
826   // XXX - Can we get away without running DeadMachineInstructionElim again?
827   addPass(&SIFoldOperandsID);
828   if (EnableDPPCombine)
829     addPass(&GCNDPPCombineID);
830   addPass(&DeadMachineInstructionElimID);
831   addPass(&SILoadStoreOptimizerID);
832   if (EnableSDWAPeephole) {
833     addPass(&SIPeepholeSDWAID);
834     addPass(&EarlyMachineLICMID);
835     addPass(&MachineCSEID);
836     addPass(&SIFoldOperandsID);
837     addPass(&DeadMachineInstructionElimID);
838   }
839   addPass(createSIShrinkInstructionsPass());
840 }
841 
842 bool GCNPassConfig::addILPOpts() {
843   if (EnableEarlyIfConversion)
844     addPass(&EarlyIfConverterID);
845 
846   TargetPassConfig::addILPOpts();
847   return false;
848 }
849 
850 bool GCNPassConfig::addInstSelector() {
851   AMDGPUPassConfig::addInstSelector();
852   addPass(&SIFixSGPRCopiesID);
853   addPass(createSILowerI1CopiesPass());
854   addPass(createSIFixupVectorISelPass());
855   addPass(createSIAddIMGInitPass());
856   return false;
857 }
858 
859 bool GCNPassConfig::addIRTranslator() {
860   addPass(new IRTranslator());
861   return false;
862 }
863 
864 bool GCNPassConfig::addLegalizeMachineIR() {
865   addPass(new Legalizer());
866   return false;
867 }
868 
869 bool GCNPassConfig::addRegBankSelect() {
870   addPass(new RegBankSelect());
871   return false;
872 }
873 
874 bool GCNPassConfig::addGlobalInstructionSelect() {
875   addPass(new InstructionSelect());
876   return false;
877 }
878 
879 void GCNPassConfig::addPreRegAlloc() {
880   if (LateCFGStructurize) {
881     addPass(createAMDGPUMachineCFGStructurizerPass());
882   }
883   addPass(createSIWholeQuadModePass());
884 }
885 
886 void GCNPassConfig::addFastRegAlloc() {
887   // FIXME: We have to disable the verifier here because of PHIElimination +
888   // TwoAddressInstructions disabling it.
889 
890   // This must be run immediately after phi elimination and before
891   // TwoAddressInstructions, otherwise the processing of the tied operand of
892   // SI_ELSE will introduce a copy of the tied operand source after the else.
893   insertPass(&PHIEliminationID, &SILowerControlFlowID, false);
894 
895   // This must be run just after RegisterCoalescing.
896   insertPass(&RegisterCoalescerID, &SIPreAllocateWWMRegsID, false);
897 
898   TargetPassConfig::addFastRegAlloc();
899 }
900 
901 void GCNPassConfig::addOptimizedRegAlloc() {
902   if (OptExecMaskPreRA) {
903     insertPass(&MachineSchedulerID, &SIOptimizeExecMaskingPreRAID);
904     insertPass(&SIOptimizeExecMaskingPreRAID, &SIFormMemoryClausesID);
905   } else {
906     insertPass(&MachineSchedulerID, &SIFormMemoryClausesID);
907   }
908 
909   // This must be run immediately after phi elimination and before
910   // TwoAddressInstructions, otherwise the processing of the tied operand of
911   // SI_ELSE will introduce a copy of the tied operand source after the else.
912   insertPass(&PHIEliminationID, &SILowerControlFlowID, false);
913 
914   // This must be run just after RegisterCoalescing.
915   insertPass(&RegisterCoalescerID, &SIPreAllocateWWMRegsID, false);
916 
917   if (EnableDCEInRA)
918     insertPass(&RenameIndependentSubregsID, &DeadMachineInstructionElimID);
919 
920   TargetPassConfig::addOptimizedRegAlloc();
921 }
922 
923 void GCNPassConfig::addPostRegAlloc() {
924   addPass(&SIFixVGPRCopiesID);
925   if (getOptLevel() > CodeGenOpt::None)
926     addPass(&SIOptimizeExecMaskingID);
927   TargetPassConfig::addPostRegAlloc();
928 }
929 
930 void GCNPassConfig::addPreSched2() {
931 }
932 
933 void GCNPassConfig::addPreEmitPass() {
934   addPass(createSIMemoryLegalizerPass());
935   addPass(createSIInsertWaitcntsPass());
936   addPass(createSIShrinkInstructionsPass());
937   addPass(createSIModeRegisterPass());
938 
939   // The hazard recognizer that runs as part of the post-ra scheduler does not
940   // guarantee to be able handle all hazards correctly. This is because if there
941   // are multiple scheduling regions in a basic block, the regions are scheduled
942   // bottom up, so when we begin to schedule a region we don't know what
943   // instructions were emitted directly before it.
944   //
945   // Here we add a stand-alone hazard recognizer pass which can handle all
946   // cases.
947   //
948   // FIXME: This stand-alone pass will emit indiv. S_NOP 0, as needed. It would
949   // be better for it to emit S_NOP <N> when possible.
950   addPass(&PostRAHazardRecognizerID);
951 
952   addPass(&SIInsertSkipsPassID);
953   addPass(&BranchRelaxationPassID);
954 }
955 
956 TargetPassConfig *GCNTargetMachine::createPassConfig(PassManagerBase &PM) {
957   return new GCNPassConfig(*this, PM);
958 }
959 
960 yaml::MachineFunctionInfo *GCNTargetMachine::createDefaultFuncInfoYAML() const {
961   return new yaml::SIMachineFunctionInfo();
962 }
963 
964 yaml::MachineFunctionInfo *
965 GCNTargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
966   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
967   return new yaml::SIMachineFunctionInfo(*MFI,
968                                          *MF.getSubtarget().getRegisterInfo());
969 }
970 
971 bool GCNTargetMachine::parseMachineFunctionInfo(
972     const yaml::MachineFunctionInfo &MFI_, PerFunctionMIParsingState &PFS,
973     SMDiagnostic &Error, SMRange &SourceRange) const {
974   const yaml::SIMachineFunctionInfo &YamlMFI =
975       reinterpret_cast<const yaml::SIMachineFunctionInfo &>(MFI_);
976   MachineFunction &MF = PFS.MF;
977   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
978 
979   MFI->initializeBaseYamlFields(YamlMFI);
980 
981   auto parseRegister = [&](const yaml::StringValue &RegName, unsigned &RegVal) {
982     if (parseNamedRegisterReference(PFS, RegVal, RegName.Value, Error)) {
983       SourceRange = RegName.SourceRange;
984       return true;
985     }
986 
987     return false;
988   };
989 
990   auto diagnoseRegisterClass = [&](const yaml::StringValue &RegName) {
991     // Create a diagnostic for a the register string literal.
992     const MemoryBuffer &Buffer =
993         *PFS.SM->getMemoryBuffer(PFS.SM->getMainFileID());
994     Error = SMDiagnostic(*PFS.SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
995                          RegName.Value.size(), SourceMgr::DK_Error,
996                          "incorrect register class for field", RegName.Value,
997                          None, None);
998     SourceRange = RegName.SourceRange;
999     return true;
1000   };
1001 
1002   if (parseRegister(YamlMFI.ScratchRSrcReg, MFI->ScratchRSrcReg) ||
1003       parseRegister(YamlMFI.ScratchWaveOffsetReg, MFI->ScratchWaveOffsetReg) ||
1004       parseRegister(YamlMFI.FrameOffsetReg, MFI->FrameOffsetReg) ||
1005       parseRegister(YamlMFI.StackPtrOffsetReg, MFI->StackPtrOffsetReg))
1006     return true;
1007 
1008   if (MFI->ScratchRSrcReg != AMDGPU::PRIVATE_RSRC_REG &&
1009       !AMDGPU::SReg_128RegClass.contains(MFI->ScratchRSrcReg)) {
1010     return diagnoseRegisterClass(YamlMFI.ScratchRSrcReg);
1011   }
1012 
1013   if (MFI->ScratchWaveOffsetReg != AMDGPU::SCRATCH_WAVE_OFFSET_REG &&
1014       !AMDGPU::SGPR_32RegClass.contains(MFI->ScratchWaveOffsetReg)) {
1015     return diagnoseRegisterClass(YamlMFI.ScratchWaveOffsetReg);
1016   }
1017 
1018   if (MFI->FrameOffsetReg != AMDGPU::FP_REG &&
1019       !AMDGPU::SGPR_32RegClass.contains(MFI->FrameOffsetReg)) {
1020     return diagnoseRegisterClass(YamlMFI.FrameOffsetReg);
1021   }
1022 
1023   if (MFI->StackPtrOffsetReg != AMDGPU::SP_REG &&
1024       !AMDGPU::SGPR_32RegClass.contains(MFI->StackPtrOffsetReg)) {
1025     return diagnoseRegisterClass(YamlMFI.StackPtrOffsetReg);
1026   }
1027 
1028   return false;
1029 }
1030