LLVM  6.0.0svn
BasicBlockUtils.cpp
Go to the documentation of this file.
1 //===- BasicBlockUtils.cpp - BasicBlock Utilities --------------------------==//
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 // This family of functions perform manipulations on basic blocks, and
11 // instructions contained within basic blocks.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Analysis/CFG.h"
21 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/CFG.h"
25 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/InstrTypes.h"
30 #include "llvm/IR/Instruction.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/IR/User.h"
36 #include "llvm/IR/Value.h"
37 #include "llvm/IR/ValueHandle.h"
38 #include "llvm/Support/Casting.h"
40 #include <cassert>
41 #include <cstdint>
42 #include <string>
43 #include <utility>
44 #include <vector>
45 
46 using namespace llvm;
47 
49  assert((pred_begin(BB) == pred_end(BB) ||
50  // Can delete self loop.
51  BB->getSinglePredecessor() == BB) && "Block is not dead!");
52  TerminatorInst *BBTerm = BB->getTerminator();
53 
54  // Loop through all of our successors and make sure they know that one
55  // of their predecessors is going away.
56  for (BasicBlock *Succ : BBTerm->successors())
57  Succ->removePredecessor(BB);
58 
59  // Zap all the instructions in the block.
60  while (!BB->empty()) {
61  Instruction &I = BB->back();
62  // If this instruction is used, replace uses with an arbitrary value.
63  // Because control flow can't get here, we don't care what we replace the
64  // value with. Note that since this block is unreachable, and all values
65  // contained within it must dominate their uses, that all uses will
66  // eventually be removed (they are themselves dead).
67  if (!I.use_empty())
69  BB->getInstList().pop_back();
70  }
71 
72  // Zap the block!
73  BB->eraseFromParent();
74 }
75 
77  MemoryDependenceResults *MemDep) {
78  if (!isa<PHINode>(BB->begin())) return;
79 
80  while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
81  if (PN->getIncomingValue(0) != PN)
82  PN->replaceAllUsesWith(PN->getIncomingValue(0));
83  else
84  PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
85 
86  if (MemDep)
87  MemDep->removeInstruction(PN); // Memdep updates AA itself.
88 
89  PN->eraseFromParent();
90  }
91 }
92 
94  // Recursively deleting a PHI may cause multiple PHIs to be deleted
95  // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.
97  for (BasicBlock::iterator I = BB->begin();
98  PHINode *PN = dyn_cast<PHINode>(I); ++I)
99  PHIs.push_back(PN);
100 
101  bool Changed = false;
102  for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
103  if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
104  Changed |= RecursivelyDeleteDeadPHINode(PN, TLI);
105 
106  return Changed;
107 }
108 
110  LoopInfo *LI,
111  MemoryDependenceResults *MemDep) {
112  // Don't merge away blocks who have their address taken.
113  if (BB->hasAddressTaken()) return false;
114 
115  // Can't merge if there are multiple predecessors, or no predecessors.
116  BasicBlock *PredBB = BB->getUniquePredecessor();
117  if (!PredBB) return false;
118 
119  // Don't break self-loops.
120  if (PredBB == BB) return false;
121  // Don't break unwinding instructions.
122  if (PredBB->getTerminator()->isExceptional())
123  return false;
124 
125  succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB));
126  BasicBlock *OnlySucc = BB;
127  for (; SI != SE; ++SI)
128  if (*SI != OnlySucc) {
129  OnlySucc = nullptr; // There are multiple distinct successors!
130  break;
131  }
132 
133  // Can't merge if there are multiple successors.
134  if (!OnlySucc) return false;
135 
136  // Can't merge if there is PHI loop.
137  for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) {
138  if (PHINode *PN = dyn_cast<PHINode>(BI)) {
139  for (Value *IncValue : PN->incoming_values())
140  if (IncValue == PN)
141  return false;
142  } else
143  break;
144  }
145 
146  // Begin by getting rid of unneeded PHIs.
147  SmallVector<Value *, 4> IncomingValues;
148  if (isa<PHINode>(BB->front())) {
149  for (auto &I : *BB)
150  if (PHINode *PN = dyn_cast<PHINode>(&I)) {
151  if (PN->getIncomingValue(0) != PN)
152  IncomingValues.push_back(PN->getIncomingValue(0));
153  } else
154  break;
155  FoldSingleEntryPHINodes(BB, MemDep);
156  }
157 
158  // Delete the unconditional branch from the predecessor...
159  PredBB->getInstList().pop_back();
160 
161  // Make all PHI nodes that referred to BB now refer to Pred as their
162  // source...
163  BB->replaceAllUsesWith(PredBB);
164 
165  // Move all definitions in the successor to the predecessor...
166  PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
167 
168  // Eliminate duplicate dbg.values describing the entry PHI node post-splice.
169  for (auto *Incoming : IncomingValues) {
170  if (isa<Instruction>(Incoming)) {
173  DbgValueSet;
174  llvm::findDbgValues(DbgValues, Incoming);
175  for (auto &DVI : DbgValues) {
176  auto R = DbgValueSet.insert({DVI->getVariable(), DVI->getExpression()});
177  if (!R.second)
178  DVI->eraseFromParent();
179  }
180  }
181  }
182 
183  // Inherit predecessors name if it exists.
184  if (!PredBB->hasName())
185  PredBB->takeName(BB);
186 
187  // Finally, erase the old block and update dominator info.
188  if (DT)
189  if (DomTreeNode *DTN = DT->getNode(BB)) {
190  DomTreeNode *PredDTN = DT->getNode(PredBB);
191  SmallVector<DomTreeNode *, 8> Children(DTN->begin(), DTN->end());
192  for (DomTreeNode *DI : Children)
193  DT->changeImmediateDominator(DI, PredDTN);
194 
195  DT->eraseNode(BB);
196  }
197 
198  if (LI)
199  LI->removeBlock(BB);
200 
201  if (MemDep)
203 
204  BB->eraseFromParent();
205  return true;
206 }
207 
209  BasicBlock::iterator &BI, Value *V) {
210  Instruction &I = *BI;
211  // Replaces all of the uses of the instruction with uses of the value
212  I.replaceAllUsesWith(V);
213 
214  // Make sure to propagate a name if there is one already.
215  if (I.hasName() && !V->hasName())
216  V->takeName(&I);
217 
218  // Delete the unnecessary instruction now...
219  BI = BIL.erase(BI);
220 }
221 
224  assert(I->getParent() == nullptr &&
225  "ReplaceInstWithInst: Instruction already inserted into basic block!");
226 
227  // Copy debug location to newly added instruction, if it wasn't already set
228  // by the caller.
229  if (!I->getDebugLoc())
230  I->setDebugLoc(BI->getDebugLoc());
231 
232  // Insert the new instruction into the basic block...
233  BasicBlock::iterator New = BIL.insert(BI, I);
234 
235  // Replace all uses of the old instruction, and delete it.
236  ReplaceInstWithValue(BIL, BI, I);
237 
238  // Move BI back to point to the newly inserted instruction
239  BI = New;
240 }
241 
243  BasicBlock::iterator BI(From);
244  ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
245 }
246 
248  LoopInfo *LI) {
249  unsigned SuccNum = GetSuccessorNumber(BB, Succ);
250 
251  // If this is a critical edge, let SplitCriticalEdge do it.
252  TerminatorInst *LatchTerm = BB->getTerminator();
253  if (SplitCriticalEdge(LatchTerm, SuccNum, CriticalEdgeSplittingOptions(DT, LI)
254  .setPreserveLCSSA()))
255  return LatchTerm->getSuccessor(SuccNum);
256 
257  // If the edge isn't critical, then BB has a single successor or Succ has a
258  // single pred. Split the block.
259  if (BasicBlock *SP = Succ->getSinglePredecessor()) {
260  // If the successor only has a single pred, split the top of the successor
261  // block.
262  assert(SP == BB && "CFG broken");
263  SP = nullptr;
264  return SplitBlock(Succ, &Succ->front(), DT, LI);
265  }
266 
267  // Otherwise, if BB has a single successor, split it at the bottom of the
268  // block.
269  assert(BB->getTerminator()->getNumSuccessors() == 1 &&
270  "Should have a single succ!");
271  return SplitBlock(BB, BB->getTerminator(), DT, LI);
272 }
273 
274 unsigned
276  const CriticalEdgeSplittingOptions &Options) {
277  unsigned NumBroken = 0;
278  for (BasicBlock &BB : F) {
279  TerminatorInst *TI = BB.getTerminator();
280  if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
281  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
282  if (SplitCriticalEdge(TI, i, Options))
283  ++NumBroken;
284  }
285  return NumBroken;
286 }
287 
289  DominatorTree *DT, LoopInfo *LI) {
290  BasicBlock::iterator SplitIt = SplitPt->getIterator();
291  while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
292  ++SplitIt;
293  BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
294 
295  // The new block lives in whichever loop the old one did. This preserves
296  // LCSSA as well, because we force the split point to be after any PHI nodes.
297  if (LI)
298  if (Loop *L = LI->getLoopFor(Old))
299  L->addBasicBlockToLoop(New, *LI);
300 
301  if (DT)
302  // Old dominates New. New node dominates all other nodes dominated by Old.
303  if (DomTreeNode *OldNode = DT->getNode(Old)) {
304  std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
305 
306  DomTreeNode *NewNode = DT->addNewBlock(New, Old);
307  for (DomTreeNode *I : Children)
308  DT->changeImmediateDominator(I, NewNode);
309  }
310 
311  return New;
312 }
313 
314 /// Update DominatorTree, LoopInfo, and LCCSA analysis information.
317  DominatorTree *DT, LoopInfo *LI,
318  bool PreserveLCSSA, bool &HasLoopExit) {
319  // Update dominator tree if available.
320  if (DT)
321  DT->splitBlock(NewBB);
322 
323  // The rest of the logic is only relevant for updating the loop structures.
324  if (!LI)
325  return;
326 
327  Loop *L = LI->getLoopFor(OldBB);
328 
329  // If we need to preserve loop analyses, collect some information about how
330  // this split will affect loops.
331  bool IsLoopEntry = !!L;
332  bool SplitMakesNewLoopHeader = false;
333  for (BasicBlock *Pred : Preds) {
334  // If we need to preserve LCSSA, determine if any of the preds is a loop
335  // exit.
336  if (PreserveLCSSA)
337  if (Loop *PL = LI->getLoopFor(Pred))
338  if (!PL->contains(OldBB))
339  HasLoopExit = true;
340 
341  // If we need to preserve LoopInfo, note whether any of the preds crosses
342  // an interesting loop boundary.
343  if (!L)
344  continue;
345  if (L->contains(Pred))
346  IsLoopEntry = false;
347  else
348  SplitMakesNewLoopHeader = true;
349  }
350 
351  // Unless we have a loop for OldBB, nothing else to do here.
352  if (!L)
353  return;
354 
355  if (IsLoopEntry) {
356  // Add the new block to the nearest enclosing loop (and not an adjacent
357  // loop). To find this, examine each of the predecessors and determine which
358  // loops enclose them, and select the most-nested loop which contains the
359  // loop containing the block being split.
360  Loop *InnermostPredLoop = nullptr;
361  for (BasicBlock *Pred : Preds) {
362  if (Loop *PredLoop = LI->getLoopFor(Pred)) {
363  // Seek a loop which actually contains the block being split (to avoid
364  // adjacent loops).
365  while (PredLoop && !PredLoop->contains(OldBB))
366  PredLoop = PredLoop->getParentLoop();
367 
368  // Select the most-nested of these loops which contains the block.
369  if (PredLoop && PredLoop->contains(OldBB) &&
370  (!InnermostPredLoop ||
371  InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
372  InnermostPredLoop = PredLoop;
373  }
374  }
375 
376  if (InnermostPredLoop)
377  InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
378  } else {
379  L->addBasicBlockToLoop(NewBB, *LI);
380  if (SplitMakesNewLoopHeader)
381  L->moveToHeader(NewBB);
382  }
383 }
384 
385 /// Update the PHI nodes in OrigBB to include the values coming from NewBB.
386 /// This also updates AliasAnalysis, if available.
387 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
389  bool HasLoopExit) {
390  // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
391  SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
392  for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
393  PHINode *PN = cast<PHINode>(I++);
394 
395  // Check to see if all of the values coming in are the same. If so, we
396  // don't need to create a new PHI node, unless it's needed for LCSSA.
397  Value *InVal = nullptr;
398  if (!HasLoopExit) {
399  InVal = PN->getIncomingValueForBlock(Preds[0]);
400  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
401  if (!PredSet.count(PN->getIncomingBlock(i)))
402  continue;
403  if (!InVal)
404  InVal = PN->getIncomingValue(i);
405  else if (InVal != PN->getIncomingValue(i)) {
406  InVal = nullptr;
407  break;
408  }
409  }
410  }
411 
412  if (InVal) {
413  // If all incoming values for the new PHI would be the same, just don't
414  // make a new PHI. Instead, just remove the incoming values from the old
415  // PHI.
416 
417  // NOTE! This loop walks backwards for a reason! First off, this minimizes
418  // the cost of removal if we end up removing a large number of values, and
419  // second off, this ensures that the indices for the incoming values
420  // aren't invalidated when we remove one.
421  for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
422  if (PredSet.count(PN->getIncomingBlock(i)))
423  PN->removeIncomingValue(i, false);
424 
425  // Add an incoming value to the PHI node in the loop for the preheader
426  // edge.
427  PN->addIncoming(InVal, NewBB);
428  continue;
429  }
430 
431  // If the values coming into the block are not the same, we need a new
432  // PHI.
433  // Create the new PHI node, insert it into NewBB at the end of the block
434  PHINode *NewPHI =
435  PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
436 
437  // NOTE! This loop walks backwards for a reason! First off, this minimizes
438  // the cost of removal if we end up removing a large number of values, and
439  // second off, this ensures that the indices for the incoming values aren't
440  // invalidated when we remove one.
441  for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
442  BasicBlock *IncomingBB = PN->getIncomingBlock(i);
443  if (PredSet.count(IncomingBB)) {
444  Value *V = PN->removeIncomingValue(i, false);
445  NewPHI->addIncoming(V, IncomingBB);
446  }
447  }
448 
449  PN->addIncoming(NewPHI, NewBB);
450  }
451 }
452 
455  const char *Suffix, DominatorTree *DT,
456  LoopInfo *LI, bool PreserveLCSSA) {
457  // Do not attempt to split that which cannot be split.
458  if (!BB->canSplitPredecessors())
459  return nullptr;
460 
461  // For the landingpads we need to act a bit differently.
462  // Delegate this work to the SplitLandingPadPredecessors.
463  if (BB->isLandingPad()) {
465  std::string NewName = std::string(Suffix) + ".split-lp";
466 
467  SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs, DT,
468  LI, PreserveLCSSA);
469  return NewBBs[0];
470  }
471 
472  // Create new basic block, insert right before the original block.
474  BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
475 
476  // The new block unconditionally branches to the old block.
477  BranchInst *BI = BranchInst::Create(BB, NewBB);
478  BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
479 
480  // Move the edges from Preds to point to NewBB instead of BB.
481  for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
482  // This is slightly more strict than necessary; the minimum requirement
483  // is that there be no more than one indirectbr branching to BB. And
484  // all BlockAddress uses would need to be updated.
485  assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
486  "Cannot split an edge from an IndirectBrInst");
487  Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
488  }
489 
490  // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
491  // node becomes an incoming value for BB's phi node. However, if the Preds
492  // list is empty, we need to insert dummy entries into the PHI nodes in BB to
493  // account for the newly created predecessor.
494  if (Preds.empty()) {
495  // Insert dummy values as the incoming value.
496  for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
497  cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
498  return NewBB;
499  }
500 
501  // Update DominatorTree, LoopInfo, and LCCSA analysis information.
502  bool HasLoopExit = false;
503  UpdateAnalysisInformation(BB, NewBB, Preds, DT, LI, PreserveLCSSA,
504  HasLoopExit);
505 
506  // Update the PHI nodes in BB with the values coming from NewBB.
507  UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
508  return NewBB;
509 }
510 
513  const char *Suffix1, const char *Suffix2,
515  DominatorTree *DT, LoopInfo *LI,
516  bool PreserveLCSSA) {
517  assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
518 
519  // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
520  // it right before the original block.
521  BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
522  OrigBB->getName() + Suffix1,
523  OrigBB->getParent(), OrigBB);
524  NewBBs.push_back(NewBB1);
525 
526  // The new block unconditionally branches to the old block.
527  BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
528  BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
529 
530  // Move the edges from Preds to point to NewBB1 instead of OrigBB.
531  for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
532  // This is slightly more strict than necessary; the minimum requirement
533  // is that there be no more than one indirectbr branching to BB. And
534  // all BlockAddress uses would need to be updated.
535  assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
536  "Cannot split an edge from an IndirectBrInst");
537  Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
538  }
539 
540  bool HasLoopExit = false;
541  UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DT, LI, PreserveLCSSA,
542  HasLoopExit);
543 
544  // Update the PHI nodes in OrigBB with the values coming from NewBB1.
545  UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
546 
547  // Move the remaining edges from OrigBB to point to NewBB2.
548  SmallVector<BasicBlock*, 8> NewBB2Preds;
549  for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
550  i != e; ) {
551  BasicBlock *Pred = *i++;
552  if (Pred == NewBB1) continue;
553  assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
554  "Cannot split an edge from an IndirectBrInst");
555  NewBB2Preds.push_back(Pred);
556  e = pred_end(OrigBB);
557  }
558 
559  BasicBlock *NewBB2 = nullptr;
560  if (!NewBB2Preds.empty()) {
561  // Create another basic block for the rest of OrigBB's predecessors.
562  NewBB2 = BasicBlock::Create(OrigBB->getContext(),
563  OrigBB->getName() + Suffix2,
564  OrigBB->getParent(), OrigBB);
565  NewBBs.push_back(NewBB2);
566 
567  // The new block unconditionally branches to the old block.
568  BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
569  BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
570 
571  // Move the remaining edges from OrigBB to point to NewBB2.
572  for (BasicBlock *NewBB2Pred : NewBB2Preds)
573  NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
574 
575  // Update DominatorTree, LoopInfo, and LCCSA analysis information.
576  HasLoopExit = false;
577  UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DT, LI,
578  PreserveLCSSA, HasLoopExit);
579 
580  // Update the PHI nodes in OrigBB with the values coming from NewBB2.
581  UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
582  }
583 
584  LandingPadInst *LPad = OrigBB->getLandingPadInst();
585  Instruction *Clone1 = LPad->clone();
586  Clone1->setName(Twine("lpad") + Suffix1);
587  NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
588 
589  if (NewBB2) {
590  Instruction *Clone2 = LPad->clone();
591  Clone2->setName(Twine("lpad") + Suffix2);
592  NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
593 
594  // Create a PHI node for the two cloned landingpad instructions only
595  // if the original landingpad instruction has some uses.
596  if (!LPad->use_empty()) {
597  assert(!LPad->getType()->isTokenTy() &&
598  "Split cannot be applied if LPad is token type. Otherwise an "
599  "invalid PHINode of token type would be created.");
600  PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
601  PN->addIncoming(Clone1, NewBB1);
602  PN->addIncoming(Clone2, NewBB2);
603  LPad->replaceAllUsesWith(PN);
604  }
605  LPad->eraseFromParent();
606  } else {
607  // There is no second clone. Just replace the landing pad with the first
608  // clone.
609  LPad->replaceAllUsesWith(Clone1);
610  LPad->eraseFromParent();
611  }
612 }
613 
615  BasicBlock *Pred) {
616  Instruction *UncondBranch = Pred->getTerminator();
617  // Clone the return and add it to the end of the predecessor.
618  Instruction *NewRet = RI->clone();
619  Pred->getInstList().push_back(NewRet);
620 
621  // If the return instruction returns a value, and if the value was a
622  // PHI node in "BB", propagate the right value into the return.
623  for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
624  i != e; ++i) {
625  Value *V = *i;
626  Instruction *NewBC = nullptr;
627  if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
628  // Return value might be bitcasted. Clone and insert it before the
629  // return instruction.
630  V = BCI->getOperand(0);
631  NewBC = BCI->clone();
632  Pred->getInstList().insert(NewRet->getIterator(), NewBC);
633  *i = NewBC;
634  }
635  if (PHINode *PN = dyn_cast<PHINode>(V)) {
636  if (PN->getParent() == BB) {
637  if (NewBC)
638  NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
639  else
640  *i = PN->getIncomingValueForBlock(Pred);
641  }
642  }
643  }
644 
645  // Update any PHI nodes in the returning block to realize that we no
646  // longer branch to them.
647  BB->removePredecessor(Pred);
648  UncondBranch->eraseFromParent();
649  return cast<ReturnInst>(NewRet);
650 }
651 
654  bool Unreachable, MDNode *BranchWeights,
655  DominatorTree *DT, LoopInfo *LI) {
656  BasicBlock *Head = SplitBefore->getParent();
657  BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
658  TerminatorInst *HeadOldTerm = Head->getTerminator();
659  LLVMContext &C = Head->getContext();
660  BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
661  TerminatorInst *CheckTerm;
662  if (Unreachable)
663  CheckTerm = new UnreachableInst(C, ThenBlock);
664  else
665  CheckTerm = BranchInst::Create(Tail, ThenBlock);
666  CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
667  BranchInst *HeadNewTerm =
668  BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond);
669  HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
670  ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
671 
672  if (DT) {
673  if (DomTreeNode *OldNode = DT->getNode(Head)) {
674  std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
675 
676  DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
677  for (DomTreeNode *Child : Children)
678  DT->changeImmediateDominator(Child, NewNode);
679 
680  // Head dominates ThenBlock.
681  DT->addNewBlock(ThenBlock, Head);
682  }
683  }
684 
685  if (LI) {
686  if (Loop *L = LI->getLoopFor(Head)) {
687  L->addBasicBlockToLoop(ThenBlock, *LI);
688  L->addBasicBlockToLoop(Tail, *LI);
689  }
690  }
691 
692  return CheckTerm;
693 }
694 
696  TerminatorInst **ThenTerm,
697  TerminatorInst **ElseTerm,
698  MDNode *BranchWeights) {
699  BasicBlock *Head = SplitBefore->getParent();
700  BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
701  TerminatorInst *HeadOldTerm = Head->getTerminator();
702  LLVMContext &C = Head->getContext();
703  BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
704  BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
705  *ThenTerm = BranchInst::Create(Tail, ThenBlock);
706  (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
707  *ElseTerm = BranchInst::Create(Tail, ElseBlock);
708  (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
709  BranchInst *HeadNewTerm =
710  BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
711  HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
712  ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
713 }
714 
716  BasicBlock *&IfFalse) {
717  PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
718  BasicBlock *Pred1 = nullptr;
719  BasicBlock *Pred2 = nullptr;
720 
721  if (SomePHI) {
722  if (SomePHI->getNumIncomingValues() != 2)
723  return nullptr;
724  Pred1 = SomePHI->getIncomingBlock(0);
725  Pred2 = SomePHI->getIncomingBlock(1);
726  } else {
727  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
728  if (PI == PE) // No predecessor
729  return nullptr;
730  Pred1 = *PI++;
731  if (PI == PE) // Only one predecessor
732  return nullptr;
733  Pred2 = *PI++;
734  if (PI != PE) // More than two predecessors
735  return nullptr;
736  }
737 
738  // We can only handle branches. Other control flow will be lowered to
739  // branches if possible anyway.
740  BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
741  BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
742  if (!Pred1Br || !Pred2Br)
743  return nullptr;
744 
745  // Eliminate code duplication by ensuring that Pred1Br is conditional if
746  // either are.
747  if (Pred2Br->isConditional()) {
748  // If both branches are conditional, we don't have an "if statement". In
749  // reality, we could transform this case, but since the condition will be
750  // required anyway, we stand no chance of eliminating it, so the xform is
751  // probably not profitable.
752  if (Pred1Br->isConditional())
753  return nullptr;
754 
755  std::swap(Pred1, Pred2);
756  std::swap(Pred1Br, Pred2Br);
757  }
758 
759  if (Pred1Br->isConditional()) {
760  // The only thing we have to watch out for here is to make sure that Pred2
761  // doesn't have incoming edges from other blocks. If it does, the condition
762  // doesn't dominate BB.
763  if (!Pred2->getSinglePredecessor())
764  return nullptr;
765 
766  // If we found a conditional branch predecessor, make sure that it branches
767  // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
768  if (Pred1Br->getSuccessor(0) == BB &&
769  Pred1Br->getSuccessor(1) == Pred2) {
770  IfTrue = Pred1;
771  IfFalse = Pred2;
772  } else if (Pred1Br->getSuccessor(0) == Pred2 &&
773  Pred1Br->getSuccessor(1) == BB) {
774  IfTrue = Pred2;
775  IfFalse = Pred1;
776  } else {
777  // We know that one arm of the conditional goes to BB, so the other must
778  // go somewhere unrelated, and this must not be an "if statement".
779  return nullptr;
780  }
781 
782  return Pred1Br->getCondition();
783  }
784 
785  // Ok, if we got here, both predecessors end with an unconditional branch to
786  // BB. Don't panic! If both blocks only have a single (identical)
787  // predecessor, and THAT is a conditional branch, then we're all ok!
788  BasicBlock *CommonPred = Pred1->getSinglePredecessor();
789  if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
790  return nullptr;
791 
792  // Otherwise, if this is a conditional branch, then we can use it!
793  BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
794  if (!BI) return nullptr;
795 
796  assert(BI->isConditional() && "Two successors but not conditional?");
797  if (BI->getSuccessor(0) == Pred1) {
798  IfTrue = Pred1;
799  IfFalse = Pred2;
800  } else {
801  IfTrue = Pred2;
802  IfFalse = Pred1;
803  }
804  return BI->getCondition();
805 }
uint64_t CallInst * C
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Return a value (possibly void), from a function.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:69
bool canSplitPredecessors() const
Definition: BasicBlock.cpp:346
void invalidateCachedPredecessors()
Clears the PredIteratorCache info.
void ReplaceInstWithInst(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
Definition: BasicBlock.cpp:276
Provides a lazy, caching interface for making common memory aliasing information queries, backed by LLVM&#39;s alias analysis passes.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, TerminatorInst **ThenTerm, TerminatorInst **ElseTerm, MDNode *BranchWeights=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
iterator erase(iterator where)
Definition: ilist.h:280
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
iterator begin() const
Definition: ArrayRef.h:137
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
unsigned getLoopDepth() const
Return the nesting level of this loop.
Definition: LoopInfo.h:92
void DeleteDeadBlock(BasicBlock *BB)
Delete the specified block, which must have no predecessors.
void moveToHeader(BlockT *BB)
This method is used to move BB (which must be part of this loop) to be the loop header of the loop (t...
Definition: LoopInfo.h:359
BasicBlock * getSuccessor(unsigned i) const
Metadata node.
Definition: Metadata.h:862
F(f)
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
Loop over all of the edges in the CFG, breaking critical edges as they are found. ...
Value * getCondition() const
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:33
op_iterator op_begin()
Definition: User.h:214
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:252
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
Option class for critical edge splitting.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:678
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:286
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:103
bool empty() const
Definition: BasicBlock.h:263
Instruction * clone() const
Create a copy of &#39;this&#39; instruction that is identical in all ways except the following: ...
void findDbgValues(SmallVectorImpl< DbgValueInst *> &DbgValues, Value *V)
Finds the llvm.dbg.value intrinsics describing a value.
Definition: Local.cpp:1288
static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, ArrayRef< BasicBlock *> Preds, BranchInst *BI, bool HasLoopExit)
Update the PHI nodes in OrigBB to include the values coming from NewBB.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
Definition: LoopInfoImpl.h:183
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:230
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Examine each PHI in the given block and delete it if it is dead.
This class represents a no-op cast from one type to another.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:430
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock *> Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock *> &NewBBs, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
void ReplaceInstWithValue(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Value *V)
Replace all uses of an instruction (specified by BI) with a value, then remove and delete the origina...
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:292
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:140
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:106
static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, ArrayRef< BasicBlock *> Preds, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA, bool &HasLoopExit)
Update DominatorTree, LoopInfo, and LCCSA analysis information.
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
If this edge is a critical edge, insert a new node to split the critical edge.
succ_range successors()
Definition: InstrTypes.h:267
The landingpad instruction holds all of the information necessary to generate correct exception handl...
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:171
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:54
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:200
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:281
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:217
bool hasName() const
Definition: Value.h:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
Conditional or Unconditional Branch instruction.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node&#39;s...
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
This function has undefined behavior.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
Definition: BasicBlock.h:264
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:113
op_iterator op_end()
Definition: User.h:216
void splice(iterator where, iplist_impl &L2)
Definition: ilist.h:342
const Instruction & back() const
Definition: BasicBlock.h:266
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:116
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock *> Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:101
self_iterator getIterator()
Definition: ilist_node.h:82
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1320
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1214
bool isLandingPad() const
Return true if this basic block is a landing pad.
Definition: BasicBlock.cpp:442
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
Definition: BasicBlock.h:376
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:317
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:110
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
iterator end()
Definition: BasicBlock.h:254
bool isExceptional() const
Definition: InstrTypes.h:84
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
Provides information about what library functions are available for the current target.
iterator end() const
Definition: ArrayRef.h:138
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
Definition: Local.cpp:458
Value * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
Check whether BB is the merge point of a if-region.
TerminatorInst * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
bool isConditional() const
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...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void setOperand(unsigned i, Value *Val)
Definition: User.h:159
Implements a dense probed hash-table based set with some number of buckets stored inline...
Definition: DenseSet.h:239
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
void push_back(pointer val)
Definition: ilist.h:326
void FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
LoopT * getParentLoop() const
Definition: LoopInfo.h:101
unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
Definition: CFG.cpp:72
iterator insert(iterator where, pointer New)
Definition: ilist.h:241
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:284
bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemoryDependenceResults *MemDep=nullptr)
Attempts to merge a block into its predecessor, if possible.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:439
bool isTokenTy() const
Return true if this is &#39;token&#39;.
Definition: Type.h:194
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink &#39;this&#39; from the containing function and delete it.
Definition: BasicBlock.cpp:97
#define I(x, y, z)
Definition: MD5.cpp:58
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
LLVM_NODISCARD 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:323
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred)
This method duplicates the specified return instruction into a predecessor which ends in an unconditi...
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:382
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
LLVM Value Representation.
Definition: Value.h:73
void removeInstruction(Instruction *InstToRemove)
Removes an instruction from the dependence analysis, updating the dependence of instructions that pre...
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the edge connecting specified block.
const LandingPadInst * getLandingPadInst() const
Return the landingpad instruction associated with the landing pad.
Definition: BasicBlock.cpp:447
const Instruction * getFirstNonPHIOrDbg() const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
Definition: BasicBlock.cpp:178
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
void pop_back()
Definition: ilist.h:331
bool use_empty() const
Definition: Value.h:328
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
Definition: LoopInfo.h:737
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
const BasicBlock * getParent() const
Definition: Instruction.h:66