1 //===-- AMDGPUCodeGenPrepare.cpp ------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// This pass does misc. AMDGPU optimizations on IR before instruction
12 /// selection.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "AMDGPU.h"
17 #include "AMDGPUSubtarget.h"
18 
19 #include "llvm/Analysis/DivergenceAnalysis.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/IR/InstVisitor.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 
26 #define DEBUG_TYPE "amdgpu-codegenprepare"
27 
28 using namespace llvm;
29 
30 namespace {
31 
32 class AMDGPUCodeGenPrepare : public FunctionPass,
33                              public InstVisitor<AMDGPUCodeGenPrepare> {
34   DivergenceAnalysis *DA;
35   const TargetMachine *TM;
36 
37 public:
38   static char ID;
AMDGPUCodeGenPrepare(const TargetMachine * TM=nullptr)39   AMDGPUCodeGenPrepare(const TargetMachine *TM = nullptr) :
40     FunctionPass(ID),
41     TM(TM) { }
42 
43   bool doInitialization(Module &M) override;
44   bool runOnFunction(Function &F) override;
45 
getPassName() const46   const char *getPassName() const override {
47     return "AMDGPU IR optimizations";
48   }
49 
getAnalysisUsage(AnalysisUsage & AU) const50   void getAnalysisUsage(AnalysisUsage &AU) const override {
51     AU.addRequired<DivergenceAnalysis>();
52     AU.setPreservesAll();
53  }
54 };
55 
56 } // End anonymous namespace
57 
doInitialization(Module & M)58 bool AMDGPUCodeGenPrepare::doInitialization(Module &M) {
59   return false;
60 }
61 
runOnFunction(Function & F)62 bool AMDGPUCodeGenPrepare::runOnFunction(Function &F) {
63   if (!TM || skipFunction(F))
64     return false;
65 
66   DA = &getAnalysis<DivergenceAnalysis>();
67   visit(F);
68 
69   return true;
70 }
71 
72 INITIALIZE_TM_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE,
73                       "AMDGPU IR optimizations", false, false)
74 INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
75 INITIALIZE_TM_PASS_END(AMDGPUCodeGenPrepare, DEBUG_TYPE,
76                        "AMDGPU IR optimizations", false, false)
77 
78 char AMDGPUCodeGenPrepare::ID = 0;
79 
createAMDGPUCodeGenPreparePass(const TargetMachine * TM)80 FunctionPass *llvm::createAMDGPUCodeGenPreparePass(const TargetMachine *TM) {
81   return new AMDGPUCodeGenPrepare(TM);
82 }
83