LLVM  3.7.0
SIAnnotateControlFlow.cpp
Go to the documentation of this file.
1 //===-- SIAnnotateControlFlow.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 /// Annotates the control flow with hardware specific intrinsics.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AMDGPU.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "si-annotate-control-flow"
29 
30 namespace {
31 
32 // Complex types used in this pass
33 typedef std::pair<BasicBlock *, Value *> StackEntry;
34 typedef SmallVector<StackEntry, 16> StackVector;
35 
36 // Intrinsic names the control flow is annotated with
37 static const char *const IfIntrinsic = "llvm.SI.if";
38 static const char *const ElseIntrinsic = "llvm.SI.else";
39 static const char *const BreakIntrinsic = "llvm.SI.break";
40 static const char *const IfBreakIntrinsic = "llvm.SI.if.break";
41 static const char *const ElseBreakIntrinsic = "llvm.SI.else.break";
42 static const char *const LoopIntrinsic = "llvm.SI.loop";
43 static const char *const EndCfIntrinsic = "llvm.SI.end.cf";
44 
45 class SIAnnotateControlFlow : public FunctionPass {
46 
47  static char ID;
48 
49  Type *Boolean;
50  Type *Void;
51  Type *Int64;
52  Type *ReturnStruct;
53 
54  ConstantInt *BoolTrue;
55  ConstantInt *BoolFalse;
56  UndefValue *BoolUndef;
57  Constant *Int64Zero;
58 
59  Constant *If;
60  Constant *Else;
61  Constant *Break;
62  Constant *IfBreak;
63  Constant *ElseBreak;
64  Constant *Loop;
65  Constant *EndCf;
66 
67  DominatorTree *DT;
68  StackVector Stack;
69 
70  LoopInfo *LI;
71 
72  bool isTopOfStack(BasicBlock *BB);
73 
74  Value *popSaved();
75 
76  void push(BasicBlock *BB, Value *Saved);
77 
78  bool isElse(PHINode *Phi);
79 
80  void eraseIfUnused(PHINode *Phi);
81 
82  void openIf(BranchInst *Term);
83 
84  void insertElse(BranchInst *Term);
85 
86  Value *handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L);
87 
88  void handleLoop(BranchInst *Term);
89 
90  void closeControlFlow(BasicBlock *BB);
91 
92 public:
93  SIAnnotateControlFlow():
94  FunctionPass(ID) { }
95 
96  bool doInitialization(Module &M) override;
97 
98  bool runOnFunction(Function &F) override;
99 
100  const char *getPassName() const override {
101  return "SI annotate control flow";
102  }
103 
104  void getAnalysisUsage(AnalysisUsage &AU) const override {
109  }
110 
111 };
112 
113 } // end anonymous namespace
114 
116 
117 /// \brief Initialize all the types and constants used in the pass
118 bool SIAnnotateControlFlow::doInitialization(Module &M) {
119  LLVMContext &Context = M.getContext();
120 
121  Void = Type::getVoidTy(Context);
122  Boolean = Type::getInt1Ty(Context);
123  Int64 = Type::getInt64Ty(Context);
124  ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr);
125 
126  BoolTrue = ConstantInt::getTrue(Context);
127  BoolFalse = ConstantInt::getFalse(Context);
128  BoolUndef = UndefValue::get(Boolean);
129  Int64Zero = ConstantInt::get(Int64, 0);
130 
132  IfIntrinsic, ReturnStruct, Boolean, (Type *)nullptr);
133 
134  Else = M.getOrInsertFunction(
135  ElseIntrinsic, ReturnStruct, Int64, (Type *)nullptr);
136 
137  Break = M.getOrInsertFunction(
138  BreakIntrinsic, Int64, Int64, (Type *)nullptr);
139 
140  IfBreak = M.getOrInsertFunction(
141  IfBreakIntrinsic, Int64, Boolean, Int64, (Type *)nullptr);
142 
143  ElseBreak = M.getOrInsertFunction(
144  ElseBreakIntrinsic, Int64, Int64, Int64, (Type *)nullptr);
145 
147  LoopIntrinsic, Boolean, Int64, (Type *)nullptr);
148 
149  EndCf = M.getOrInsertFunction(
150  EndCfIntrinsic, Void, Int64, (Type *)nullptr);
151 
152  return false;
153 }
154 
155 /// \brief Is BB the last block saved on the stack ?
156 bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
157  return !Stack.empty() && Stack.back().first == BB;
158 }
159 
160 /// \brief Pop the last saved value from the control flow stack
161 Value *SIAnnotateControlFlow::popSaved() {
162  return Stack.pop_back_val().second;
163 }
164 
165 /// \brief Push a BB and saved value to the control flow stack
166 void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
167  Stack.push_back(std::make_pair(BB, Saved));
168 }
169 
170 /// \brief Can the condition represented by this PHI node treated like
171 /// an "Else" block?
172 bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
173  BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
174  for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
175  if (Phi->getIncomingBlock(i) == IDom) {
176 
177  if (Phi->getIncomingValue(i) != BoolTrue)
178  return false;
179 
180  } else {
181  if (Phi->getIncomingValue(i) != BoolFalse)
182  return false;
183 
184  }
185  }
186  return true;
187 }
188 
189 // \brief Erase "Phi" if it is not used any more
190 void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
191  if (!Phi->hasNUsesOrMore(1))
192  Phi->eraseFromParent();
193 }
194 
195 /// \brief Open a new "If" block
196 void SIAnnotateControlFlow::openIf(BranchInst *Term) {
197  Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
198  Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
199  push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
200 }
201 
202 /// \brief Close the last "If" block and open a new "Else" block
203 void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
204  Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
205  Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
206  push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
207 }
208 
209 /// \brief Recursively handle the condition leading to a loop
210 Value *SIAnnotateControlFlow::handleLoopCondition(Value *Cond, PHINode *Broken,
211  llvm::Loop *L) {
212 
213  // Only search through PHI nodes which are inside the loop. If we try this
214  // with PHI nodes that are outside of the loop, we end up inserting new PHI
215  // nodes outside of the loop which depend on values defined inside the loop.
216  // This will break the module with
217  // 'Instruction does not dominate all users!' errors.
218  PHINode *Phi = nullptr;
219  if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) {
220 
221  BasicBlock *Parent = Phi->getParent();
222  PHINode *NewPhi = PHINode::Create(Int64, 0, "", &Parent->front());
223  Value *Ret = NewPhi;
224 
225  // Handle all non-constant incoming values first
226  for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
227  Value *Incoming = Phi->getIncomingValue(i);
228  BasicBlock *From = Phi->getIncomingBlock(i);
229  if (isa<ConstantInt>(Incoming)) {
230  NewPhi->addIncoming(Broken, From);
231  continue;
232  }
233 
234  Phi->setIncomingValue(i, BoolFalse);
235  Value *PhiArg = handleLoopCondition(Incoming, Broken, L);
236  NewPhi->addIncoming(PhiArg, From);
237  }
238 
239  BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
240 
241  for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
242 
243  Value *Incoming = Phi->getIncomingValue(i);
244  if (Incoming != BoolTrue)
245  continue;
246 
247  BasicBlock *From = Phi->getIncomingBlock(i);
248  if (From == IDom) {
249  CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
250  if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
251  Value *Args[] = { OldEnd->getArgOperand(0), NewPhi };
252  Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
253  continue;
254  }
255  }
256  TerminatorInst *Insert = From->getTerminator();
257  Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
258  NewPhi->setIncomingValue(i, PhiArg);
259  }
260  eraseIfUnused(Phi);
261  return Ret;
262 
263  } else if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
264  BasicBlock *Parent = Inst->getParent();
265  Instruction *Insert;
266  if (L->contains(Inst)) {
267  Insert = Parent->getTerminator();
268  } else {
269  Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
270  }
271  Value *Args[] = { Cond, Broken };
272  return CallInst::Create(IfBreak, Args, "", Insert);
273 
274  } else {
275  llvm_unreachable("Unhandled loop condition!");
276  }
277  return 0;
278 }
279 
280 /// \brief Handle a back edge (loop)
281 void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
282  BasicBlock *BB = Term->getParent();
283  llvm::Loop *L = LI->getLoopFor(BB);
284  BasicBlock *Target = Term->getSuccessor(1);
285  PHINode *Broken = PHINode::Create(Int64, 0, "", &Target->front());
286 
287  Value *Cond = Term->getCondition();
288  Term->setCondition(BoolTrue);
289  Value *Arg = handleLoopCondition(Cond, Broken, L);
290 
291  for (pred_iterator PI = pred_begin(Target), PE = pred_end(Target);
292  PI != PE; ++PI) {
293 
294  Broken->addIncoming(*PI == BB ? Arg : Int64Zero, *PI);
295  }
296 
297  Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
298  push(Term->getSuccessor(0), Arg);
299 }/// \brief Close the last opened control flow
300 void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
301  llvm::Loop *L = LI->getLoopFor(BB);
302 
303  if (L && L->getHeader() == BB) {
304  // We can't insert an EndCF call into a loop header, because it will
305  // get executed on every iteration of the loop, when it should be
306  // executed only once before the loop.
308  L->getLoopLatches(Latches);
309 
310  std::vector<BasicBlock*> Preds;
311  for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
312  if (std::find(Latches.begin(), Latches.end(), *PI) == Latches.end())
313  Preds.push_back(*PI);
314  }
315  BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", nullptr, DT,
316  LI, false);
317  }
318 
319  CallInst::Create(EndCf, popSaved(), "", BB->getFirstInsertionPt());
320 }
321 
322 /// \brief Annotate the control flow with intrinsics so the backend can
323 /// recognize if/then/else and loops.
324 bool SIAnnotateControlFlow::runOnFunction(Function &F) {
325  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
326  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
327 
329  E = df_end(&F.getEntryBlock()); I != E; ++I) {
330 
331  BranchInst *Term = dyn_cast<BranchInst>((*I)->getTerminator());
332 
333  if (!Term || Term->isUnconditional()) {
334  if (isTopOfStack(*I))
335  closeControlFlow(*I);
336  continue;
337  }
338 
339  if (I.nodeVisited(Term->getSuccessor(1))) {
340  if (isTopOfStack(*I))
341  closeControlFlow(*I);
342  handleLoop(Term);
343  continue;
344  }
345 
346  if (isTopOfStack(*I)) {
347  PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
348  if (Phi && Phi->getParent() == *I && isElse(Phi)) {
349  insertElse(Term);
350  eraseIfUnused(Phi);
351  continue;
352  }
353  closeControlFlow(*I);
354  }
355  openIf(Term);
356  }
357 
358  assert(Stack.empty());
359  return true;
360 }
361 
362 /// \brief Create the annotation pass
364  return new SIAnnotateControlFlow();
365 }
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:537
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:236
FunctionPass * createSIAnnotateControlFlowPass()
Create the annotation pass.
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
void getLoopLatches(SmallVectorImpl< BlockT * > &LoopLatches) const
getLoopLatches - Return all loop latch blocks of this loop.
Definition: LoopInfo.h:234
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:78
CallInst - This class represents a function call, abstracting a target machine's calling convention...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
const Instruction & front() const
Definition: BasicBlock.h:243
F(f)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
BlockT * getHeader() const
Definition: LoopInfo.h:96
AnalysisUsage & addRequired()
bool isUnconditional() const
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
UndefValue - 'undef' values are things that do not have specified contents.
Definition: Constants.h:1220
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Number of individual test Apply this number of consecutive mutations to each input If
BasicBlock * getSuccessor(unsigned i) const
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:35
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeSet AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:115
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
BranchInst - Conditional or Unconditional Branch instruction.
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
df_iterator< T > df_end(const T &G)
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:114
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:225
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:105
unsigned char Boolean
Definition: ConvertUTF.h:104
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:117
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
StructType::get - This static method is the primary way to create a literal StructType.
Definition: Type.cpp:404
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
Function * getCalledFunction() const
getCalledFunction - Return the function called, or null if this is an indirect function invocation...
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:530
df_iterator< T > df_begin(const T &G)
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Target - Wrapper for Target specific information.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
Value * getCondition() const
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
Definition: Value.cpp:104
void setCondition(Value *V)
LLVM Value Representation.
Definition: Value.h:69
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:737
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitBlockPredecessors - This method introduces at least one new basic block into the function and mo...
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:194
void setIncomingValue(unsigned i, Value *V)
const BasicBlock * getParent() const
Definition: Instruction.h:72
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:265