1 //===-- SIAnnotateControlFlow.cpp - ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Annotates the control flow with hardware specific intrinsics.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/DepthFirstIterator.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
23 #include "llvm/Transforms/Utils/SSAUpdater.h"
29 // Complex types used in this pass
30 typedef std::pair<BasicBlock *, Value *> StackEntry;
31 typedef SmallVector<StackEntry, 16> StackVector;
33 // Intrinsic names the control flow is annotated with
34 static const char *const IfIntrinsic = "llvm.SI.if";
35 static const char *const ElseIntrinsic = "llvm.SI.else";
36 static const char *const BreakIntrinsic = "llvm.SI.break";
37 static const char *const IfBreakIntrinsic = "llvm.SI.if.break";
38 static const char *const ElseBreakIntrinsic = "llvm.SI.else.break";
39 static const char *const LoopIntrinsic = "llvm.SI.loop";
40 static const char *const EndCfIntrinsic = "llvm.SI.end.cf";
42 class SIAnnotateControlFlow : public FunctionPass {
51 ConstantInt *BoolTrue;
52 ConstantInt *BoolFalse;
53 UndefValue *BoolUndef;
66 SSAUpdater PhiInserter;
68 bool isTopOfStack(BasicBlock *BB);
72 void push(BasicBlock *BB, Value *Saved);
74 bool isElse(PHINode *Phi);
76 void eraseIfUnused(PHINode *Phi);
78 void openIf(BranchInst *Term);
80 void insertElse(BranchInst *Term);
82 void handleLoopCondition(Value *Cond);
84 void handleLoop(BranchInst *Term);
86 void closeControlFlow(BasicBlock *BB);
89 SIAnnotateControlFlow():
92 virtual bool doInitialization(Module &M);
94 virtual bool runOnFunction(Function &F);
96 virtual const char *getPassName() const {
97 return "SI annotate control flow";
100 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
101 AU.addRequired<DominatorTreeWrapperPass>();
102 AU.addPreserved<DominatorTreeWrapperPass>();
103 FunctionPass::getAnalysisUsage(AU);
108 } // end anonymous namespace
110 char SIAnnotateControlFlow::ID = 0;
112 /// \brief Initialize all the types and constants used in the pass
113 bool SIAnnotateControlFlow::doInitialization(Module &M) {
114 LLVMContext &Context = M.getContext();
116 Void = Type::getVoidTy(Context);
117 Boolean = Type::getInt1Ty(Context);
118 Int64 = Type::getInt64Ty(Context);
119 ReturnStruct = StructType::get(Boolean, Int64, (Type *)0);
121 BoolTrue = ConstantInt::getTrue(Context);
122 BoolFalse = ConstantInt::getFalse(Context);
123 BoolUndef = UndefValue::get(Boolean);
124 Int64Zero = ConstantInt::get(Int64, 0);
126 If = M.getOrInsertFunction(
127 IfIntrinsic, ReturnStruct, Boolean, (Type *)0);
129 Else = M.getOrInsertFunction(
130 ElseIntrinsic, ReturnStruct, Int64, (Type *)0);
132 Break = M.getOrInsertFunction(
133 BreakIntrinsic, Int64, Int64, (Type *)0);
135 IfBreak = M.getOrInsertFunction(
136 IfBreakIntrinsic, Int64, Boolean, Int64, (Type *)0);
138 ElseBreak = M.getOrInsertFunction(
139 ElseBreakIntrinsic, Int64, Int64, Int64, (Type *)0);
141 Loop = M.getOrInsertFunction(
142 LoopIntrinsic, Boolean, Int64, (Type *)0);
144 EndCf = M.getOrInsertFunction(
145 EndCfIntrinsic, Void, Int64, (Type *)0);
150 /// \brief Is BB the last block saved on the stack ?
151 bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
152 return !Stack.empty() && Stack.back().first == BB;
155 /// \brief Pop the last saved value from the control flow stack
156 Value *SIAnnotateControlFlow::popSaved() {
157 return Stack.pop_back_val().second;
160 /// \brief Push a BB and saved value to the control flow stack
161 void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
162 Stack.push_back(std::make_pair(BB, Saved));
165 /// \brief Can the condition represented by this PHI node treated like
167 bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
168 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
169 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
170 if (Phi->getIncomingBlock(i) == IDom) {
172 if (Phi->getIncomingValue(i) != BoolTrue)
176 if (Phi->getIncomingValue(i) != BoolFalse)
184 // \brief Erase "Phi" if it is not used any more
185 void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
186 if (!Phi->hasNUsesOrMore(1))
187 Phi->eraseFromParent();
190 /// \brief Open a new "If" block
191 void SIAnnotateControlFlow::openIf(BranchInst *Term) {
192 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
193 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
194 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
197 /// \brief Close the last "If" block and open a new "Else" block
198 void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
199 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
200 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
201 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
204 /// \brief Recursively handle the condition leading to a loop
205 void SIAnnotateControlFlow::handleLoopCondition(Value *Cond) {
206 if (PHINode *Phi = dyn_cast<PHINode>(Cond)) {
208 // Handle all non-constant incoming values first
209 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
210 Value *Incoming = Phi->getIncomingValue(i);
211 if (isa<ConstantInt>(Incoming))
214 Phi->setIncomingValue(i, BoolFalse);
215 handleLoopCondition(Incoming);
218 BasicBlock *Parent = Phi->getParent();
219 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
221 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
223 Value *Incoming = Phi->getIncomingValue(i);
224 if (Incoming != BoolTrue)
227 BasicBlock *From = Phi->getIncomingBlock(i);
229 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
230 if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
232 OldEnd->getArgOperand(0),
233 PhiInserter.GetValueAtEndOfBlock(Parent)
235 Value *Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
236 PhiInserter.AddAvailableValue(Parent, Ret);
241 TerminatorInst *Insert = From->getTerminator();
242 Value *Arg = PhiInserter.GetValueAtEndOfBlock(From);
243 Value *Ret = CallInst::Create(Break, Arg, "", Insert);
244 PhiInserter.AddAvailableValue(From, Ret);
248 } else if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
249 BasicBlock *Parent = Inst->getParent();
250 TerminatorInst *Insert = Parent->getTerminator();
251 Value *Args[] = { Cond, PhiInserter.GetValueAtEndOfBlock(Parent) };
252 Value *Ret = CallInst::Create(IfBreak, Args, "", Insert);
253 PhiInserter.AddAvailableValue(Parent, Ret);
256 llvm_unreachable("Unhandled loop condition!");
260 /// \brief Handle a back edge (loop)
261 void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
262 BasicBlock *Target = Term->getSuccessor(1);
263 PHINode *Broken = PHINode::Create(Int64, 0, "", &Target->front());
265 PhiInserter.Initialize(Int64, "");
266 PhiInserter.AddAvailableValue(Target, Broken);
268 Value *Cond = Term->getCondition();
269 Term->setCondition(BoolTrue);
270 handleLoopCondition(Cond);
272 BasicBlock *BB = Term->getParent();
273 Value *Arg = PhiInserter.GetValueAtEndOfBlock(BB);
274 for (pred_iterator PI = pred_begin(Target), PE = pred_end(Target);
277 Broken->addIncoming(*PI == BB ? Arg : Int64Zero, *PI);
280 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
281 push(Term->getSuccessor(0), Arg);
284 /// \brief Close the last opened control flow
285 void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
286 CallInst::Create(EndCf, popSaved(), "", BB->getFirstInsertionPt());
289 /// \brief Annotate the control flow with intrinsics so the backend can
290 /// recognize if/then/else and loops.
291 bool SIAnnotateControlFlow::runOnFunction(Function &F) {
292 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
294 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
295 E = df_end(&F.getEntryBlock()); I != E; ++I) {
297 BranchInst *Term = dyn_cast<BranchInst>((*I)->getTerminator());
299 if (!Term || Term->isUnconditional()) {
300 if (isTopOfStack(*I))
301 closeControlFlow(*I);
305 if (I.nodeVisited(Term->getSuccessor(1))) {
306 if (isTopOfStack(*I))
307 closeControlFlow(*I);
312 if (isTopOfStack(*I)) {
313 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
314 if (Phi && Phi->getParent() == *I && isElse(Phi)) {
319 closeControlFlow(*I);
324 assert(Stack.empty());
328 /// \brief Create the annotation pass
329 FunctionPass *llvm::createSIAnnotateControlFlowPass() {
330 return new SIAnnotateControlFlow();