1 //===-- AMDGPUAnnotateUniformValues.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 adds amdgpu.uniform metadata to IR values so this information
12 /// can be used during instruction selection.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "AMDGPU.h"
17 #include "AMDGPUIntrinsicInfo.h"
18 #include "llvm/Analysis/DivergenceAnalysis.h"
19 #include "llvm/IR/InstVisitor.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 #define DEBUG_TYPE "amdgpu-annotate-uniform"
25 
26 using namespace llvm;
27 
28 namespace {
29 
30 class AMDGPUAnnotateUniformValues : public FunctionPass,
31                        public InstVisitor<AMDGPUAnnotateUniformValues> {
32   DivergenceAnalysis *DA;
33 
34 public:
35   static char ID;
AMDGPUAnnotateUniformValues()36   AMDGPUAnnotateUniformValues() :
37     FunctionPass(ID) { }
38   bool doInitialization(Module &M) override;
39   bool runOnFunction(Function &F) override;
getPassName() const40   const char *getPassName() const override { return "AMDGPU Annotate Uniform Values"; }
getAnalysisUsage(AnalysisUsage & AU) const41   void getAnalysisUsage(AnalysisUsage &AU) const override {
42     AU.addRequired<DivergenceAnalysis>();
43     AU.setPreservesAll();
44  }
45 
46   void visitLoadInst(LoadInst &I);
47 
48 };
49 
50 } // End anonymous namespace
51 
52 INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
53                       "Add AMDGPU uniform metadata", false, false)
54 INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
55 INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
56                     "Add AMDGPU uniform metadata", false, false)
57 
58 char AMDGPUAnnotateUniformValues::ID = 0;
59 
visitLoadInst(LoadInst & I)60 void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst &I) {
61   Value *Ptr = I.getPointerOperand();
62   if (!DA->isUniform(Ptr))
63     return;
64 
65   if (Instruction *PtrI = dyn_cast<Instruction>(Ptr))
66     PtrI->setMetadata("amdgpu.uniform", MDNode::get(I.getContext(), {}));
67 
68 }
69 
doInitialization(Module & M)70 bool AMDGPUAnnotateUniformValues::doInitialization(Module &M) {
71   return false;
72 }
73 
runOnFunction(Function & F)74 bool AMDGPUAnnotateUniformValues::runOnFunction(Function &F) {
75   DA = &getAnalysis<DivergenceAnalysis>();
76   visit(F);
77 
78   return true;
79 }
80 
81 FunctionPass *
createAMDGPUAnnotateUniformValues()82 llvm::createAMDGPUAnnotateUniformValues() {
83   return new AMDGPUAnnotateUniformValues();
84 }
85