LLVM  3.7.0
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 
17 #include "llvm/Analysis/CFG.h"
18 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/IR/Constant.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/IR/ValueHandle.h"
29 #include "llvm/Transforms/Scalar.h"
31 #include <algorithm>
32 using namespace llvm;
33 
34 /// DeleteDeadBlock - Delete the specified block, which must have no
35 /// predecessors.
37  assert((pred_begin(BB) == pred_end(BB) ||
38  // Can delete self loop.
39  BB->getSinglePredecessor() == BB) && "Block is not dead!");
40  TerminatorInst *BBTerm = BB->getTerminator();
41 
42  // Loop through all of our successors and make sure they know that one
43  // of their predecessors is going away.
44  for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
45  BBTerm->getSuccessor(i)->removePredecessor(BB);
46 
47  // Zap all the instructions in the block.
48  while (!BB->empty()) {
49  Instruction &I = BB->back();
50  // If this instruction is used, replace uses with an arbitrary value.
51  // Because control flow can't get here, we don't care what we replace the
52  // value with. Note that since this block is unreachable, and all values
53  // contained within it must dominate their uses, that all uses will
54  // eventually be removed (they are themselves dead).
55  if (!I.use_empty())
57  BB->getInstList().pop_back();
58  }
59 
60  // Zap the block!
61  BB->eraseFromParent();
62 }
63 
64 /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
65 /// any single-entry PHI nodes in it, fold them away. This handles the case
66 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
67 /// when the block has exactly one predecessor.
69  MemoryDependenceAnalysis *MemDep) {
70  if (!isa<PHINode>(BB->begin())) return;
71 
72  while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
73  if (PN->getIncomingValue(0) != PN)
74  PN->replaceAllUsesWith(PN->getIncomingValue(0));
75  else
76  PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
77 
78  if (MemDep)
79  MemDep->removeInstruction(PN); // Memdep updates AA itself.
80  else if (AA && isa<PointerType>(PN->getType()))
81  AA->deleteValue(PN);
82 
83  PN->eraseFromParent();
84  }
85 }
86 
87 
88 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
89 /// is dead. Also recursively delete any operands that become dead as
90 /// a result. This includes tracing the def-use list from the PHI to see if
91 /// it is ultimately unused or if it reaches an unused cycle.
93  // Recursively deleting a PHI may cause multiple PHIs to be deleted
94  // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete.
96  for (BasicBlock::iterator I = BB->begin();
97  PHINode *PN = dyn_cast<PHINode>(I); ++I)
98  PHIs.push_back(PN);
99 
100  bool Changed = false;
101  for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
102  if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
103  Changed |= RecursivelyDeleteDeadPHINode(PN, TLI);
104 
105  return Changed;
106 }
107 
108 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
109 /// if possible. The return value indicates success or failure.
111  LoopInfo *LI, AliasAnalysis *AA,
112  MemoryDependenceAnalysis *MemDep) {
113  // Don't merge away blocks who have their address taken.
114  if (BB->hasAddressTaken()) return false;
115 
116  // Can't merge if there are multiple predecessors, or no predecessors.
117  BasicBlock *PredBB = BB->getUniquePredecessor();
118  if (!PredBB) return false;
119 
120  // Don't break self-loops.
121  if (PredBB == BB) return false;
122  // Don't break invokes.
123  if (isa<InvokeInst>(PredBB->getTerminator())) 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  if (isa<PHINode>(BB->front()))
148  FoldSingleEntryPHINodes(BB, AA, MemDep);
149 
150  // Delete the unconditional branch from the predecessor...
151  PredBB->getInstList().pop_back();
152 
153  // Make all PHI nodes that referred to BB now refer to Pred as their
154  // source...
155  BB->replaceAllUsesWith(PredBB);
156 
157  // Move all definitions in the successor to the predecessor...
158  PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
159 
160  // Inherit predecessors name if it exists.
161  if (!PredBB->hasName())
162  PredBB->takeName(BB);
163 
164  // Finally, erase the old block and update dominator info.
165  if (DT)
166  if (DomTreeNode *DTN = DT->getNode(BB)) {
167  DomTreeNode *PredDTN = DT->getNode(PredBB);
168  SmallVector<DomTreeNode *, 8> Children(DTN->begin(), DTN->end());
169  for (SmallVectorImpl<DomTreeNode *>::iterator DI = Children.begin(),
170  DE = Children.end();
171  DI != DE; ++DI)
172  DT->changeImmediateDominator(*DI, PredDTN);
173 
174  DT->eraseNode(BB);
175  }
176 
177  if (LI)
178  LI->removeBlock(BB);
179 
180  if (MemDep)
182 
183  BB->eraseFromParent();
184  return true;
185 }
186 
187 /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
188 /// with a value, then remove and delete the original instruction.
189 ///
191  BasicBlock::iterator &BI, Value *V) {
192  Instruction &I = *BI;
193  // Replaces all of the uses of the instruction with uses of the value
194  I.replaceAllUsesWith(V);
195 
196  // Make sure to propagate a name if there is one already.
197  if (I.hasName() && !V->hasName())
198  V->takeName(&I);
199 
200  // Delete the unnecessary instruction now...
201  BI = BIL.erase(BI);
202 }
203 
204 
205 /// ReplaceInstWithInst - Replace the instruction specified by BI with the
206 /// instruction specified by I. The original instruction is deleted and BI is
207 /// updated to point to the new instruction.
208 ///
211  assert(I->getParent() == nullptr &&
212  "ReplaceInstWithInst: Instruction already inserted into basic block!");
213 
214  // Copy debug location to newly added instruction, if it wasn't already set
215  // by the caller.
216  if (!I->getDebugLoc())
217  I->setDebugLoc(BI->getDebugLoc());
218 
219  // Insert the new instruction into the basic block...
220  BasicBlock::iterator New = BIL.insert(BI, I);
221 
222  // Replace all uses of the old instruction, and delete it.
223  ReplaceInstWithValue(BIL, BI, I);
224 
225  // Move BI back to point to the newly inserted instruction
226  BI = New;
227 }
228 
229 /// ReplaceInstWithInst - Replace the instruction specified by From with the
230 /// instruction specified by To.
231 ///
233  BasicBlock::iterator BI(From);
234  ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
235 }
236 
237 /// SplitEdge - Split the edge connecting specified block. Pass P must
238 /// not be NULL.
240  LoopInfo *LI) {
241  unsigned SuccNum = GetSuccessorNumber(BB, Succ);
242 
243  // If this is a critical edge, let SplitCriticalEdge do it.
244  TerminatorInst *LatchTerm = BB->getTerminator();
245  if (SplitCriticalEdge(LatchTerm, SuccNum, CriticalEdgeSplittingOptions(DT, LI)
246  .setPreserveLCSSA()))
247  return LatchTerm->getSuccessor(SuccNum);
248 
249  // If the edge isn't critical, then BB has a single successor or Succ has a
250  // single pred. Split the block.
251  if (BasicBlock *SP = Succ->getSinglePredecessor()) {
252  // If the successor only has a single pred, split the top of the successor
253  // block.
254  assert(SP == BB && "CFG broken");
255  SP = nullptr;
256  return SplitBlock(Succ, Succ->begin(), DT, LI);
257  }
258 
259  // Otherwise, if BB has a single successor, split it at the bottom of the
260  // block.
261  assert(BB->getTerminator()->getNumSuccessors() == 1 &&
262  "Should have a single succ!");
263  return SplitBlock(BB, BB->getTerminator(), DT, LI);
264 }
265 
266 unsigned
268  const CriticalEdgeSplittingOptions &Options) {
269  unsigned NumBroken = 0;
270  for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
271  TerminatorInst *TI = I->getTerminator();
272  if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
273  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
274  if (SplitCriticalEdge(TI, i, Options))
275  ++NumBroken;
276  }
277  return NumBroken;
278 }
279 
280 /// SplitBlock - Split the specified block at the specified instruction - every
281 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
282 /// to a new block. The two blocks are joined by an unconditional branch and
283 /// the loop info is updated.
284 ///
286  DominatorTree *DT, LoopInfo *LI) {
287  BasicBlock::iterator SplitIt = SplitPt;
288  while (isa<PHINode>(SplitIt) || isa<LandingPadInst>(SplitIt))
289  ++SplitIt;
290  BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
291 
292  // The new block lives in whichever loop the old one did. This preserves
293  // LCSSA as well, because we force the split point to be after any PHI nodes.
294  if (LI)
295  if (Loop *L = LI->getLoopFor(Old))
296  L->addBasicBlockToLoop(New, *LI);
297 
298  if (DT)
299  // Old dominates New. New node dominates all other nodes dominated by Old.
300  if (DomTreeNode *OldNode = DT->getNode(Old)) {
301  std::vector<DomTreeNode *> Children;
302  for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end();
303  I != E; ++I)
304  Children.push_back(*I);
305 
306  DomTreeNode *NewNode = DT->addNewBlock(New, Old);
307  for (std::vector<DomTreeNode *>::iterator I = Children.begin(),
308  E = Children.end(); I != E; ++I)
309  DT->changeImmediateDominator(*I, NewNode);
310  }
311 
312  return New;
313 }
314 
315 /// UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA
316 /// analysis information.
319  DominatorTree *DT, LoopInfo *LI,
320  bool PreserveLCSSA, bool &HasLoopExit) {
321  // Update dominator tree if available.
322  if (DT)
323  DT->splitBlock(NewBB);
324 
325  // The rest of the logic is only relevant for updating the loop structures.
326  if (!LI)
327  return;
328 
329  Loop *L = LI->getLoopFor(OldBB);
330 
331  // If we need to preserve loop analyses, collect some information about how
332  // this split will affect loops.
333  bool IsLoopEntry = !!L;
334  bool SplitMakesNewLoopHeader = false;
335  for (ArrayRef<BasicBlock *>::iterator i = Preds.begin(), e = Preds.end();
336  i != e; ++i) {
337  BasicBlock *Pred = *i;
338 
339  // If we need to preserve LCSSA, determine if any of the preds is a loop
340  // exit.
341  if (PreserveLCSSA)
342  if (Loop *PL = LI->getLoopFor(Pred))
343  if (!PL->contains(OldBB))
344  HasLoopExit = true;
345 
346  // If we need to preserve LoopInfo, note whether any of the preds crosses
347  // an interesting loop boundary.
348  if (!L)
349  continue;
350  if (L->contains(Pred))
351  IsLoopEntry = false;
352  else
353  SplitMakesNewLoopHeader = true;
354  }
355 
356  // Unless we have a loop for OldBB, nothing else to do here.
357  if (!L)
358  return;
359 
360  if (IsLoopEntry) {
361  // Add the new block to the nearest enclosing loop (and not an adjacent
362  // loop). To find this, examine each of the predecessors and determine which
363  // loops enclose them, and select the most-nested loop which contains the
364  // loop containing the block being split.
365  Loop *InnermostPredLoop = nullptr;
367  i = Preds.begin(), e = Preds.end(); i != e; ++i) {
368  BasicBlock *Pred = *i;
369  if (Loop *PredLoop = LI->getLoopFor(Pred)) {
370  // Seek a loop which actually contains the block being split (to avoid
371  // adjacent loops).
372  while (PredLoop && !PredLoop->contains(OldBB))
373  PredLoop = PredLoop->getParentLoop();
374 
375  // Select the most-nested of these loops which contains the block.
376  if (PredLoop && PredLoop->contains(OldBB) &&
377  (!InnermostPredLoop ||
378  InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
379  InnermostPredLoop = PredLoop;
380  }
381  }
382 
383  if (InnermostPredLoop)
384  InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
385  } else {
386  L->addBasicBlockToLoop(NewBB, *LI);
387  if (SplitMakesNewLoopHeader)
388  L->moveToHeader(NewBB);
389  }
390 }
391 
392 /// UpdatePHINodes - Update the PHI nodes in OrigBB to include the values coming
393 /// from NewBB. This also updates AliasAnalysis, if available.
394 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
396  AliasAnalysis *AA, bool HasLoopExit) {
397  // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
398  SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
399  for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
400  PHINode *PN = cast<PHINode>(I++);
401 
402  // Check to see if all of the values coming in are the same. If so, we
403  // don't need to create a new PHI node, unless it's needed for LCSSA.
404  Value *InVal = nullptr;
405  if (!HasLoopExit) {
406  InVal = PN->getIncomingValueForBlock(Preds[0]);
407  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
408  if (!PredSet.count(PN->getIncomingBlock(i)))
409  continue;
410  if (!InVal)
411  InVal = PN->getIncomingValue(i);
412  else if (InVal != PN->getIncomingValue(i)) {
413  InVal = nullptr;
414  break;
415  }
416  }
417  }
418 
419  if (InVal) {
420  // If all incoming values for the new PHI would be the same, just don't
421  // make a new PHI. Instead, just remove the incoming values from the old
422  // PHI.
423 
424  // NOTE! This loop walks backwards for a reason! First off, this minimizes
425  // the cost of removal if we end up removing a large number of values, and
426  // second off, this ensures that the indices for the incoming values
427  // aren't invalidated when we remove one.
428  for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
429  if (PredSet.count(PN->getIncomingBlock(i)))
430  PN->removeIncomingValue(i, false);
431 
432  // Add an incoming value to the PHI node in the loop for the preheader
433  // edge.
434  PN->addIncoming(InVal, NewBB);
435  continue;
436  }
437 
438  // If the values coming into the block are not the same, we need a new
439  // PHI.
440  // Create the new PHI node, insert it into NewBB at the end of the block
441  PHINode *NewPHI =
442  PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
443 
444  // NOTE! This loop walks backwards for a reason! First off, this minimizes
445  // the cost of removal if we end up removing a large number of values, and
446  // second off, this ensures that the indices for the incoming values aren't
447  // invalidated when we remove one.
448  for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
449  BasicBlock *IncomingBB = PN->getIncomingBlock(i);
450  if (PredSet.count(IncomingBB)) {
451  Value *V = PN->removeIncomingValue(i, false);
452  NewPHI->addIncoming(V, IncomingBB);
453  }
454  }
455 
456  PN->addIncoming(NewPHI, NewBB);
457  }
458 }
459 
460 /// SplitBlockPredecessors - This method introduces at least one new basic block
461 /// into the function and moves some of the predecessors of BB to be
462 /// predecessors of the new block. The new predecessors are indicated by the
463 /// Preds array. The new block is given a suffix of 'Suffix'. Returns new basic
464 /// block to which predecessors from Preds are now pointing.
465 ///
466 /// If BB is a landingpad block then additional basicblock might be introduced.
467 /// It will have suffix of 'Suffix'+".split_lp".
468 /// See SplitLandingPadPredecessors for more details on this case.
469 ///
470 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
471 /// LoopInfo, and LCCSA but no other analyses. In particular, it does not
472 /// preserve LoopSimplify (because it's complicated to handle the case where one
473 /// of the edges being split is an exit of a loop with other exits).
474 ///
477  const char *Suffix, AliasAnalysis *AA,
478  DominatorTree *DT, LoopInfo *LI,
479  bool PreserveLCSSA) {
480  // For the landingpads we need to act a bit differently.
481  // Delegate this work to the SplitLandingPadPredecessors.
482  if (BB->isLandingPad()) {
484  std::string NewName = std::string(Suffix) + ".split-lp";
485 
486  SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(),
487  NewBBs, AA, DT, LI, PreserveLCSSA);
488  return NewBBs[0];
489  }
490 
491  // Create new basic block, insert right before the original block.
493  BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
494 
495  // The new block unconditionally branches to the old block.
496  BranchInst *BI = BranchInst::Create(BB, NewBB);
497  BI->setDebugLoc(BB->getFirstNonPHI()->getDebugLoc());
498 
499  // Move the edges from Preds to point to NewBB instead of BB.
500  for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
501  // This is slightly more strict than necessary; the minimum requirement
502  // is that there be no more than one indirectbr branching to BB. And
503  // all BlockAddress uses would need to be updated.
504  assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
505  "Cannot split an edge from an IndirectBrInst");
506  Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
507  }
508 
509  // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
510  // node becomes an incoming value for BB's phi node. However, if the Preds
511  // list is empty, we need to insert dummy entries into the PHI nodes in BB to
512  // account for the newly created predecessor.
513  if (Preds.size() == 0) {
514  // Insert dummy values as the incoming value.
515  for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
516  cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
517  return NewBB;
518  }
519 
520  // Update DominatorTree, LoopInfo, and LCCSA analysis information.
521  bool HasLoopExit = false;
522  UpdateAnalysisInformation(BB, NewBB, Preds, DT, LI, PreserveLCSSA,
523  HasLoopExit);
524 
525  // Update the PHI nodes in BB with the values coming from NewBB.
526  UpdatePHINodes(BB, NewBB, Preds, BI, AA, HasLoopExit);
527  return NewBB;
528 }
529 
530 /// SplitLandingPadPredecessors - This method transforms the landing pad,
531 /// OrigBB, by introducing two new basic blocks into the function. One of those
532 /// new basic blocks gets the predecessors listed in Preds. The other basic
533 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
534 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
535 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
536 ///
537 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
538 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
539 /// it does not preserve LoopSimplify (because it's complicated to handle the
540 /// case where one of the edges being split is an exit of a loop with other
541 /// exits).
542 ///
545  const char *Suffix1, const char *Suffix2,
547  AliasAnalysis *AA, DominatorTree *DT,
548  LoopInfo *LI, bool PreserveLCSSA) {
549  assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
550 
551  // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
552  // it right before the original block.
553  BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
554  OrigBB->getName() + Suffix1,
555  OrigBB->getParent(), OrigBB);
556  NewBBs.push_back(NewBB1);
557 
558  // The new block unconditionally branches to the old block.
559  BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
560  BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
561 
562  // Move the edges from Preds to point to NewBB1 instead of OrigBB.
563  for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
564  // This is slightly more strict than necessary; the minimum requirement
565  // is that there be no more than one indirectbr branching to BB. And
566  // all BlockAddress uses would need to be updated.
567  assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
568  "Cannot split an edge from an IndirectBrInst");
569  Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
570  }
571 
572  bool HasLoopExit = false;
573  UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DT, LI, PreserveLCSSA,
574  HasLoopExit);
575 
576  // Update the PHI nodes in OrigBB with the values coming from NewBB1.
577  UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, AA, HasLoopExit);
578 
579  // Move the remaining edges from OrigBB to point to NewBB2.
580  SmallVector<BasicBlock*, 8> NewBB2Preds;
581  for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
582  i != e; ) {
583  BasicBlock *Pred = *i++;
584  if (Pred == NewBB1) continue;
585  assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
586  "Cannot split an edge from an IndirectBrInst");
587  NewBB2Preds.push_back(Pred);
588  e = pred_end(OrigBB);
589  }
590 
591  BasicBlock *NewBB2 = nullptr;
592  if (!NewBB2Preds.empty()) {
593  // Create another basic block for the rest of OrigBB's predecessors.
594  NewBB2 = BasicBlock::Create(OrigBB->getContext(),
595  OrigBB->getName() + Suffix2,
596  OrigBB->getParent(), OrigBB);
597  NewBBs.push_back(NewBB2);
598 
599  // The new block unconditionally branches to the old block.
600  BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
601  BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
602 
603  // Move the remaining edges from OrigBB to point to NewBB2.
605  i = NewBB2Preds.begin(), e = NewBB2Preds.end(); i != e; ++i)
606  (*i)->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
607 
608  // Update DominatorTree, LoopInfo, and LCCSA analysis information.
609  HasLoopExit = false;
610  UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DT, LI,
611  PreserveLCSSA, HasLoopExit);
612 
613  // Update the PHI nodes in OrigBB with the values coming from NewBB2.
614  UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, AA, HasLoopExit);
615  }
616 
617  LandingPadInst *LPad = OrigBB->getLandingPadInst();
618  Instruction *Clone1 = LPad->clone();
619  Clone1->setName(Twine("lpad") + Suffix1);
620  NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
621 
622  if (NewBB2) {
623  Instruction *Clone2 = LPad->clone();
624  Clone2->setName(Twine("lpad") + Suffix2);
625  NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
626 
627  // Create a PHI node for the two cloned landingpad instructions.
628  PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
629  PN->addIncoming(Clone1, NewBB1);
630  PN->addIncoming(Clone2, NewBB2);
631  LPad->replaceAllUsesWith(PN);
632  LPad->eraseFromParent();
633  } else {
634  // There is no second clone. Just replace the landing pad with the first
635  // clone.
636  LPad->replaceAllUsesWith(Clone1);
637  LPad->eraseFromParent();
638  }
639 }
640 
641 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
642 /// instruction into a predecessor which ends in an unconditional branch. If
643 /// the return instruction returns a value defined by a PHI, propagate the
644 /// right value into the return. It returns the new return instruction in the
645 /// predecessor.
647  BasicBlock *Pred) {
648  Instruction *UncondBranch = Pred->getTerminator();
649  // Clone the return and add it to the end of the predecessor.
650  Instruction *NewRet = RI->clone();
651  Pred->getInstList().push_back(NewRet);
652 
653  // If the return instruction returns a value, and if the value was a
654  // PHI node in "BB", propagate the right value into the return.
655  for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
656  i != e; ++i) {
657  Value *V = *i;
658  Instruction *NewBC = nullptr;
659  if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
660  // Return value might be bitcasted. Clone and insert it before the
661  // return instruction.
662  V = BCI->getOperand(0);
663  NewBC = BCI->clone();
664  Pred->getInstList().insert(NewRet, NewBC);
665  *i = NewBC;
666  }
667  if (PHINode *PN = dyn_cast<PHINode>(V)) {
668  if (PN->getParent() == BB) {
669  if (NewBC)
670  NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
671  else
672  *i = PN->getIncomingValueForBlock(Pred);
673  }
674  }
675  }
676 
677  // Update any PHI nodes in the returning block to realize that we no
678  // longer branch to them.
679  BB->removePredecessor(Pred);
680  UncondBranch->eraseFromParent();
681  return cast<ReturnInst>(NewRet);
682 }
683 
684 /// SplitBlockAndInsertIfThen - Split the containing block at the
685 /// specified instruction - everything before and including SplitBefore stays
686 /// in the old basic block, and everything after SplitBefore is moved to a
687 /// new block. The two blocks are connected by a conditional branch
688 /// (with value of Cmp being the condition).
689 /// Before:
690 /// Head
691 /// SplitBefore
692 /// Tail
693 /// After:
694 /// Head
695 /// if (Cond)
696 /// ThenBlock
697 /// SplitBefore
698 /// Tail
699 ///
700 /// If Unreachable is true, then ThenBlock ends with
701 /// UnreachableInst, otherwise it branches to Tail.
702 /// Returns the NewBasicBlock's terminator.
703 
705  Instruction *SplitBefore,
706  bool Unreachable,
707  MDNode *BranchWeights,
708  DominatorTree *DT) {
709  BasicBlock *Head = SplitBefore->getParent();
710  BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
711  TerminatorInst *HeadOldTerm = Head->getTerminator();
712  LLVMContext &C = Head->getContext();
713  BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
714  TerminatorInst *CheckTerm;
715  if (Unreachable)
716  CheckTerm = new UnreachableInst(C, ThenBlock);
717  else
718  CheckTerm = BranchInst::Create(Tail, ThenBlock);
719  CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
720  BranchInst *HeadNewTerm =
721  BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond);
722  HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
723  ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
724 
725  if (DT) {
726  if (DomTreeNode *OldNode = DT->getNode(Head)) {
727  std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
728 
729  DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
730  for (auto Child : Children)
731  DT->changeImmediateDominator(Child, NewNode);
732 
733  // Head dominates ThenBlock.
734  DT->addNewBlock(ThenBlock, Head);
735  }
736  }
737 
738  return CheckTerm;
739 }
740 
741 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
742 /// but also creates the ElseBlock.
743 /// Before:
744 /// Head
745 /// SplitBefore
746 /// Tail
747 /// After:
748 /// Head
749 /// if (Cond)
750 /// ThenBlock
751 /// else
752 /// ElseBlock
753 /// SplitBefore
754 /// Tail
756  TerminatorInst **ThenTerm,
757  TerminatorInst **ElseTerm,
758  MDNode *BranchWeights) {
759  BasicBlock *Head = SplitBefore->getParent();
760  BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
761  TerminatorInst *HeadOldTerm = Head->getTerminator();
762  LLVMContext &C = Head->getContext();
763  BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
764  BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
765  *ThenTerm = BranchInst::Create(Tail, ThenBlock);
766  (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
767  *ElseTerm = BranchInst::Create(Tail, ElseBlock);
768  (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
769  BranchInst *HeadNewTerm =
770  BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
771  HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
772  ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
773 }
774 
775 
776 /// GetIfCondition - Given a basic block (BB) with two predecessors,
777 /// check to see if the merge at this block is due
778 /// to an "if condition". If so, return the boolean condition that determines
779 /// which entry into BB will be taken. Also, return by references the block
780 /// that will be entered from if the condition is true, and the block that will
781 /// be entered if the condition is false.
782 ///
783 /// This does no checking to see if the true/false blocks have large or unsavory
784 /// instructions in them.
786  BasicBlock *&IfFalse) {
787  PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
788  BasicBlock *Pred1 = nullptr;
789  BasicBlock *Pred2 = nullptr;
790 
791  if (SomePHI) {
792  if (SomePHI->getNumIncomingValues() != 2)
793  return nullptr;
794  Pred1 = SomePHI->getIncomingBlock(0);
795  Pred2 = SomePHI->getIncomingBlock(1);
796  } else {
797  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
798  if (PI == PE) // No predecessor
799  return nullptr;
800  Pred1 = *PI++;
801  if (PI == PE) // Only one predecessor
802  return nullptr;
803  Pred2 = *PI++;
804  if (PI != PE) // More than two predecessors
805  return nullptr;
806  }
807 
808  // We can only handle branches. Other control flow will be lowered to
809  // branches if possible anyway.
810  BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
811  BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
812  if (!Pred1Br || !Pred2Br)
813  return nullptr;
814 
815  // Eliminate code duplication by ensuring that Pred1Br is conditional if
816  // either are.
817  if (Pred2Br->isConditional()) {
818  // If both branches are conditional, we don't have an "if statement". In
819  // reality, we could transform this case, but since the condition will be
820  // required anyway, we stand no chance of eliminating it, so the xform is
821  // probably not profitable.
822  if (Pred1Br->isConditional())
823  return nullptr;
824 
825  std::swap(Pred1, Pred2);
826  std::swap(Pred1Br, Pred2Br);
827  }
828 
829  if (Pred1Br->isConditional()) {
830  // The only thing we have to watch out for here is to make sure that Pred2
831  // doesn't have incoming edges from other blocks. If it does, the condition
832  // doesn't dominate BB.
833  if (!Pred2->getSinglePredecessor())
834  return nullptr;
835 
836  // If we found a conditional branch predecessor, make sure that it branches
837  // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
838  if (Pred1Br->getSuccessor(0) == BB &&
839  Pred1Br->getSuccessor(1) == Pred2) {
840  IfTrue = Pred1;
841  IfFalse = Pred2;
842  } else if (Pred1Br->getSuccessor(0) == Pred2 &&
843  Pred1Br->getSuccessor(1) == BB) {
844  IfTrue = Pred2;
845  IfFalse = Pred1;
846  } else {
847  // We know that one arm of the conditional goes to BB, so the other must
848  // go somewhere unrelated, and this must not be an "if statement".
849  return nullptr;
850  }
851 
852  return Pred1Br->getCondition();
853  }
854 
855  // Ok, if we got here, both predecessors end with an unconditional branch to
856  // BB. Don't panic! If both blocks only have a single (identical)
857  // predecessor, and THAT is a conditional branch, then we're all ok!
858  BasicBlock *CommonPred = Pred1->getSinglePredecessor();
859  if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
860  return nullptr;
861 
862  // Otherwise, if this is a conditional branch, then we can use it!
863  BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
864  if (!BI) return nullptr;
865 
866  assert(BI->isConditional() && "Two successors but not conditional?");
867  if (BI->getSuccessor(0) == Pred1) {
868  IfTrue = Pred1;
869  IfFalse = Pred2;
870  } else {
871  IfTrue = Pred2;
872  IfFalse = Pred1;
873  }
874  return BI->getCondition();
875 }
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
addNewBlock - Add a new node to the dominator tree information.
ReturnInst - Return a value (possibly void), from a function.
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
void ReplaceInstWithInst(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Instruction *I)
ReplaceInstWithInst - Replace the instruction specified by BI with the instruction specified by I...
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:224
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
Definition: BasicBlock.cpp:266
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - 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 removeBlock(BlockT *BB)
removeBlock - This method completely removes BB from all data structures, including all of the Loop o...
Definition: LoopInfo.h:605
const Instruction & back() const
Definition: BasicBlock.h:245
bool hasName() const
Definition: Value.h:228
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitBlock - Split the specified block at the specified instruction - every thing before SplitPt stay...
void pop_back()
Definition: ilist.h:559
iterator end()
Definition: Function.h:459
void DeleteDeadBlock(BasicBlock *BB)
DeleteDeadBlock - Delete the specified block, which must have no predecessors.
static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA, bool &HasLoopExit)
UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA analysis information.
iterator end() const
Definition: ArrayRef.h:123
TerminatorInst * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr)
SplitBlockAndInsertIfThen - Split the containing block at the specified instruction - everything befo...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
Metadata node.
Definition: Metadata.h:740
const Instruction & front() const
Definition: BasicBlock.h:243
F(f)
unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
Definition: CFG.cpp:72
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
Definition: LoopInfo.h:540
op_iterator op_begin()
Definition: User.h:183
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
void push_back(NodeTy *val)
Definition: ilist.h:554
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
removeIncomingValue - 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:79
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:165
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
Definition: BasicBlock.h:306
MemoryDependenceAnalysis - This is an analysis that determines, for a given memory operation...
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:250
Instruction * clone() const
clone() - Create a copy of 'this' instruction that is identical in all ways except the following: ...
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:104
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
addBasicBlockToLoop - This method is used by other analyses to update loop information.
Definition: LoopInfoImpl.h:187
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, AliasAnalysis *AA=nullptr, MemoryDependenceAnalysis *MemDep=nullptr)
MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, if possible.
bool empty() const
Definition: BasicBlock.h:242
BasicBlock * getSuccessor(unsigned i) const
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
DeleteDeadPHIs - 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.
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
void ReplaceInstWithValue(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Value *V)
ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) with a value...
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:256
iterator begin()
Definition: Function.h:457
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
void FoldSingleEntryPHINodes(BasicBlock *BB, AliasAnalysis *AA=nullptr, MemoryDependenceAnalysis *MemDep=nullptr)
FoldSingleEntryPHINodes - We know that BB has one predecessor.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
Definition: InstrTypes.h:57
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
SplitCriticalEdge - If this edge is a critical edge, insert a new node to split the critical edge...
LandingPadInst - The landingpad instruction holds all of the information necessary to generate correc...
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:35
void setDebugLoc(DebugLoc Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:227
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
Definition: InstrTypes.h:62
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
BranchInst - Conditional or Unconditional Branch instruction.
UnreachableInst - This function has undefined behavior.
LandingPadInst * getLandingPadInst()
Return the landingpad instruction associated with the landing pad.
Definition: BasicBlock.cpp:418
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
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:230
op_iterator op_end()
Definition: User.h:185
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
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:252
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
iterator begin() const
Definition: ArrayRef.h:122
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:117
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:103
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
virtual void deleteValue(Value *V)
Methods that clients should call when they transform the program to allow alias analyses to update th...
iterator erase(iterator where)
Definition: ilist.h:465
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1083
bool isConditional() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
iterator end()
Definition: BasicBlock.h:233
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Provides information about what library functions are available for the current target.
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteDeadPHINode - If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use nodes that either forms a cycle or is terminated by a trivially dead instruction, delete it.
Definition: Local.cpp:397
Value * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
GetIfCondition - Check whether BB is the merge point of a if-region.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
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...
void splice(iterator where, iplist &L2)
Definition: ilist.h:570
void setOperand(unsigned i, Value *Val)
Definition: User.h:122
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:576
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitLandingPadPredecessors - This method transforms the landing pad, OrigBB, by introducing two new ...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:211
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
void moveToHeader(BlockT *BB)
moveToHeader - This method is used to move BB (which must be part of this loop) to be the loop header...
Definition: LoopInfo.h:304
void invalidateCachedPredecessors()
invalidateCachedPredecessors - Clear the PredIteratorCache info.
iplist< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
Definition: BasicBlock.cpp:97
#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 isLandingPad() const
Return true if this basic block is a landing pad.
Definition: BasicBlock.cpp:413
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred)
FoldReturnIntoUncondBranch - This method duplicates the specified return instruction into a predecess...
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:348
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
bool use_empty() const
Definition: Value.h:275
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
LLVM Value Representation.
Definition: Value.h:69
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitEdge - Split the edge connecting specified block.
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, BranchInst *BI, AliasAnalysis *AA, bool HasLoopExit)
UpdatePHINodes - Update the PHI nodes in OrigBB to include the values coming from NewBB...
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...
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
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
unsigned getLoopDepth() const
getLoopDepth - Return the nesting level of this loop.
Definition: LoopInfo.h:89
const BasicBlock * getParent() const
Definition: Instruction.h:72
void removeInstruction(Instruction *InstToRemove)
removeInstruction - Remove an instruction from the dependence analysis, updating the dependence of in...
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.