31
31
//
32
32
// ===----------------------------------------------------------------------===//
33
33
34
+ #include " llvm/CodeGen/CallBrPrepare.h"
34
35
#include " llvm/ADT/ArrayRef.h"
35
36
#include " llvm/ADT/SmallPtrSet.h"
36
37
#include " llvm/ADT/SmallVector.h"
@@ -53,15 +54,16 @@ using namespace llvm;
53
54
54
55
#define DEBUG_TYPE " callbrprepare"
55
56
57
+ static bool SplitCriticalEdges (ArrayRef<CallBrInst *> CBRs, DominatorTree &DT);
58
+ static bool InsertIntrinsicCalls (ArrayRef<CallBrInst *> CBRs,
59
+ DominatorTree &DT);
60
+ static void UpdateSSA (DominatorTree &DT, CallBrInst *CBR, CallInst *Intrinsic,
61
+ SSAUpdater &SSAUpdate);
62
+ static SmallVector<CallBrInst *, 2 > FindCallBrs (Function &Fn);
63
+
56
64
namespace {
57
65
58
66
class CallBrPrepare : public FunctionPass {
59
- bool SplitCriticalEdges (ArrayRef<CallBrInst *> CBRs, DominatorTree &DT);
60
- bool InsertIntrinsicCalls (ArrayRef<CallBrInst *> CBRs,
61
- DominatorTree &DT) const ;
62
- void UpdateSSA (DominatorTree &DT, CallBrInst *CBR, CallInst *Intrinsic,
63
- SSAUpdater &SSAUpdate) const ;
64
-
65
67
public:
66
68
CallBrPrepare () : FunctionPass(ID) {}
67
69
void getAnalysisUsage (AnalysisUsage &AU) const override ;
@@ -71,6 +73,26 @@ class CallBrPrepare : public FunctionPass {
71
73
72
74
} // end anonymous namespace
73
75
76
+ PreservedAnalyses CallBrPreparePass::run (Function &Fn,
77
+ FunctionAnalysisManager &FAM) {
78
+ bool Changed = false ;
79
+ SmallVector<CallBrInst *, 2 > CBRs = FindCallBrs (Fn);
80
+
81
+ if (CBRs.empty ())
82
+ return PreservedAnalyses::all ();
83
+
84
+ auto &DT = FAM.getResult <DominatorTreeAnalysis>(Fn);
85
+
86
+ Changed |= SplitCriticalEdges (CBRs, DT);
87
+ Changed |= InsertIntrinsicCalls (CBRs, DT);
88
+
89
+ if (!Changed)
90
+ return PreservedAnalyses::all ();
91
+ PreservedAnalyses PA;
92
+ PA.preserve <DominatorTreeAnalysis>();
93
+ return PA;
94
+ }
95
+
74
96
char CallBrPrepare::ID = 0 ;
75
97
INITIALIZE_PASS_BEGIN (CallBrPrepare, DEBUG_TYPE, " Prepare callbr" , false , false )
76
98
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
@@ -82,7 +104,7 @@ void CallBrPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
82
104
AU.addPreserved <DominatorTreeWrapperPass>();
83
105
}
84
106
85
- static SmallVector<CallBrInst *, 2 > FindCallBrs (Function &Fn) {
107
+ SmallVector<CallBrInst *, 2 > FindCallBrs (Function &Fn) {
86
108
SmallVector<CallBrInst *, 2 > CBRs;
87
109
for (BasicBlock &BB : Fn)
88
110
if (auto *CBR = dyn_cast<CallBrInst>(BB.getTerminator ()))
@@ -91,8 +113,7 @@ static SmallVector<CallBrInst *, 2> FindCallBrs(Function &Fn) {
91
113
return CBRs;
92
114
}
93
115
94
- bool CallBrPrepare::SplitCriticalEdges (ArrayRef<CallBrInst *> CBRs,
95
- DominatorTree &DT) {
116
+ bool SplitCriticalEdges (ArrayRef<CallBrInst *> CBRs, DominatorTree &DT) {
96
117
bool Changed = false ;
97
118
CriticalEdgeSplittingOptions Options (&DT);
98
119
Options.setMergeIdenticalEdges ();
@@ -114,8 +135,7 @@ bool CallBrPrepare::SplitCriticalEdges(ArrayRef<CallBrInst *> CBRs,
114
135
return Changed;
115
136
}
116
137
117
- bool CallBrPrepare::InsertIntrinsicCalls (ArrayRef<CallBrInst *> CBRs,
118
- DominatorTree &DT) const {
138
+ bool InsertIntrinsicCalls (ArrayRef<CallBrInst *> CBRs, DominatorTree &DT) {
119
139
bool Changed = false ;
120
140
SmallPtrSet<const BasicBlock *, 4 > Visited;
121
141
IRBuilder<> Builder (CBRs[0 ]->getContext ());
@@ -160,9 +180,8 @@ static void PrintDebugDomInfo(const DominatorTree &DT, const Use &U,
160
180
}
161
181
#endif
162
182
163
- void CallBrPrepare::UpdateSSA (DominatorTree &DT, CallBrInst *CBR,
164
- CallInst *Intrinsic,
165
- SSAUpdater &SSAUpdate) const {
183
+ void UpdateSSA (DominatorTree &DT, CallBrInst *CBR, CallInst *Intrinsic,
184
+ SSAUpdater &SSAUpdate) {
166
185
167
186
SmallPtrSet<Use *, 4 > Visited;
168
187
BasicBlock *DefaultDest = CBR->getDefaultDest ();
0 commit comments