xref: /aosp_15_r20/external/llvm/lib/Analysis/MemDepPrinter.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker //
11*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
12*9880d681SAndroid Build Coastguard Worker 
13*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/Passes.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/MemoryDependenceAnalysis.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker namespace {
24*9880d681SAndroid Build Coastguard Worker   struct MemDepPrinter : public FunctionPass {
25*9880d681SAndroid Build Coastguard Worker     const Function *F;
26*9880d681SAndroid Build Coastguard Worker 
27*9880d681SAndroid Build Coastguard Worker     enum DepType {
28*9880d681SAndroid Build Coastguard Worker       Clobber = 0,
29*9880d681SAndroid Build Coastguard Worker       Def,
30*9880d681SAndroid Build Coastguard Worker       NonFuncLocal,
31*9880d681SAndroid Build Coastguard Worker       Unknown
32*9880d681SAndroid Build Coastguard Worker     };
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker     static const char *const DepTypeStr[];
35*9880d681SAndroid Build Coastguard Worker 
36*9880d681SAndroid Build Coastguard Worker     typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
37*9880d681SAndroid Build Coastguard Worker     typedef std::pair<InstTypePair, const BasicBlock *> Dep;
38*9880d681SAndroid Build Coastguard Worker     typedef SmallSetVector<Dep, 4> DepSet;
39*9880d681SAndroid Build Coastguard Worker     typedef DenseMap<const Instruction *, DepSet> DepSetMap;
40*9880d681SAndroid Build Coastguard Worker     DepSetMap Deps;
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker     static char ID; // Pass identifcation, replacement for typeid
MemDepPrinter__anon25c9f3370111::MemDepPrinter43*9880d681SAndroid Build Coastguard Worker     MemDepPrinter() : FunctionPass(ID) {
44*9880d681SAndroid Build Coastguard Worker       initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
45*9880d681SAndroid Build Coastguard Worker     }
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker     bool runOnFunction(Function &F) override;
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker     void print(raw_ostream &OS, const Module * = nullptr) const override;
50*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage__anon25c9f3370111::MemDepPrinter51*9880d681SAndroid Build Coastguard Worker     void getAnalysisUsage(AnalysisUsage &AU) const override {
52*9880d681SAndroid Build Coastguard Worker       AU.addRequiredTransitive<AAResultsWrapperPass>();
53*9880d681SAndroid Build Coastguard Worker       AU.addRequiredTransitive<MemoryDependenceWrapperPass>();
54*9880d681SAndroid Build Coastguard Worker       AU.setPreservesAll();
55*9880d681SAndroid Build Coastguard Worker     }
56*9880d681SAndroid Build Coastguard Worker 
releaseMemory__anon25c9f3370111::MemDepPrinter57*9880d681SAndroid Build Coastguard Worker     void releaseMemory() override {
58*9880d681SAndroid Build Coastguard Worker       Deps.clear();
59*9880d681SAndroid Build Coastguard Worker       F = nullptr;
60*9880d681SAndroid Build Coastguard Worker     }
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker   private:
getInstTypePair__anon25c9f3370111::MemDepPrinter63*9880d681SAndroid Build Coastguard Worker     static InstTypePair getInstTypePair(MemDepResult dep) {
64*9880d681SAndroid Build Coastguard Worker       if (dep.isClobber())
65*9880d681SAndroid Build Coastguard Worker         return InstTypePair(dep.getInst(), Clobber);
66*9880d681SAndroid Build Coastguard Worker       if (dep.isDef())
67*9880d681SAndroid Build Coastguard Worker         return InstTypePair(dep.getInst(), Def);
68*9880d681SAndroid Build Coastguard Worker       if (dep.isNonFuncLocal())
69*9880d681SAndroid Build Coastguard Worker         return InstTypePair(dep.getInst(), NonFuncLocal);
70*9880d681SAndroid Build Coastguard Worker       assert(dep.isUnknown() && "unexpected dependence type");
71*9880d681SAndroid Build Coastguard Worker       return InstTypePair(dep.getInst(), Unknown);
72*9880d681SAndroid Build Coastguard Worker     }
getInstTypePair__anon25c9f3370111::MemDepPrinter73*9880d681SAndroid Build Coastguard Worker     static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
74*9880d681SAndroid Build Coastguard Worker       return InstTypePair(inst, type);
75*9880d681SAndroid Build Coastguard Worker     }
76*9880d681SAndroid Build Coastguard Worker   };
77*9880d681SAndroid Build Coastguard Worker }
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker char MemDepPrinter::ID = 0;
80*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
81*9880d681SAndroid Build Coastguard Worker                       "Print MemDeps of function", false, true)
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)82*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
83*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
84*9880d681SAndroid Build Coastguard Worker                       "Print MemDeps of function", false, true)
85*9880d681SAndroid Build Coastguard Worker 
86*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createMemDepPrinter() {
87*9880d681SAndroid Build Coastguard Worker   return new MemDepPrinter();
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker const char *const MemDepPrinter::DepTypeStr[]
91*9880d681SAndroid Build Coastguard Worker   = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
92*9880d681SAndroid Build Coastguard Worker 
runOnFunction(Function & F)93*9880d681SAndroid Build Coastguard Worker bool MemDepPrinter::runOnFunction(Function &F) {
94*9880d681SAndroid Build Coastguard Worker   this->F = &F;
95*9880d681SAndroid Build Coastguard Worker   MemoryDependenceResults &MDA = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
96*9880d681SAndroid Build Coastguard Worker 
97*9880d681SAndroid Build Coastguard Worker   // All this code uses non-const interfaces because MemDep is not
98*9880d681SAndroid Build Coastguard Worker   // const-friendly, though nothing is actually modified.
99*9880d681SAndroid Build Coastguard Worker   for (auto &I : instructions(F)) {
100*9880d681SAndroid Build Coastguard Worker     Instruction *Inst = &I;
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker     if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
103*9880d681SAndroid Build Coastguard Worker       continue;
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker     MemDepResult Res = MDA.getDependency(Inst);
106*9880d681SAndroid Build Coastguard Worker     if (!Res.isNonLocal()) {
107*9880d681SAndroid Build Coastguard Worker       Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
108*9880d681SAndroid Build Coastguard Worker                                        static_cast<BasicBlock *>(nullptr)));
109*9880d681SAndroid Build Coastguard Worker     } else if (auto CS = CallSite(Inst)) {
110*9880d681SAndroid Build Coastguard Worker       const MemoryDependenceResults::NonLocalDepInfo &NLDI =
111*9880d681SAndroid Build Coastguard Worker         MDA.getNonLocalCallDependency(CS);
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker       DepSet &InstDeps = Deps[Inst];
114*9880d681SAndroid Build Coastguard Worker       for (const NonLocalDepEntry &I : NLDI) {
115*9880d681SAndroid Build Coastguard Worker         const MemDepResult &Res = I.getResult();
116*9880d681SAndroid Build Coastguard Worker         InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
117*9880d681SAndroid Build Coastguard Worker       }
118*9880d681SAndroid Build Coastguard Worker     } else {
119*9880d681SAndroid Build Coastguard Worker       SmallVector<NonLocalDepResult, 4> NLDI;
120*9880d681SAndroid Build Coastguard Worker       assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
121*9880d681SAndroid Build Coastguard Worker                isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
122*9880d681SAndroid Build Coastguard Worker       MDA.getNonLocalPointerDependency(Inst, NLDI);
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker       DepSet &InstDeps = Deps[Inst];
125*9880d681SAndroid Build Coastguard Worker       for (const NonLocalDepResult &I : NLDI) {
126*9880d681SAndroid Build Coastguard Worker         const MemDepResult &Res = I.getResult();
127*9880d681SAndroid Build Coastguard Worker         InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
128*9880d681SAndroid Build Coastguard Worker       }
129*9880d681SAndroid Build Coastguard Worker     }
130*9880d681SAndroid Build Coastguard Worker   }
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   return false;
133*9880d681SAndroid Build Coastguard Worker }
134*9880d681SAndroid Build Coastguard Worker 
print(raw_ostream & OS,const Module * M) const135*9880d681SAndroid Build Coastguard Worker void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
136*9880d681SAndroid Build Coastguard Worker   for (const auto &I : instructions(*F)) {
137*9880d681SAndroid Build Coastguard Worker     const Instruction *Inst = &I;
138*9880d681SAndroid Build Coastguard Worker 
139*9880d681SAndroid Build Coastguard Worker     DepSetMap::const_iterator DI = Deps.find(Inst);
140*9880d681SAndroid Build Coastguard Worker     if (DI == Deps.end())
141*9880d681SAndroid Build Coastguard Worker       continue;
142*9880d681SAndroid Build Coastguard Worker 
143*9880d681SAndroid Build Coastguard Worker     const DepSet &InstDeps = DI->second;
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker     for (const auto &I : InstDeps) {
146*9880d681SAndroid Build Coastguard Worker       const Instruction *DepInst = I.first.getPointer();
147*9880d681SAndroid Build Coastguard Worker       DepType type = I.first.getInt();
148*9880d681SAndroid Build Coastguard Worker       const BasicBlock *DepBB = I.second;
149*9880d681SAndroid Build Coastguard Worker 
150*9880d681SAndroid Build Coastguard Worker       OS << "    ";
151*9880d681SAndroid Build Coastguard Worker       OS << DepTypeStr[type];
152*9880d681SAndroid Build Coastguard Worker       if (DepBB) {
153*9880d681SAndroid Build Coastguard Worker         OS << " in block ";
154*9880d681SAndroid Build Coastguard Worker         DepBB->printAsOperand(OS, /*PrintType=*/false, M);
155*9880d681SAndroid Build Coastguard Worker       }
156*9880d681SAndroid Build Coastguard Worker       if (DepInst) {
157*9880d681SAndroid Build Coastguard Worker         OS << " from: ";
158*9880d681SAndroid Build Coastguard Worker         DepInst->print(OS);
159*9880d681SAndroid Build Coastguard Worker       }
160*9880d681SAndroid Build Coastguard Worker       OS << "\n";
161*9880d681SAndroid Build Coastguard Worker     }
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker     Inst->print(OS);
164*9880d681SAndroid Build Coastguard Worker     OS << "\n\n";
165*9880d681SAndroid Build Coastguard Worker   }
166*9880d681SAndroid Build Coastguard Worker }
167