LLVM  8.0.1
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"
25 #include "llvm/IR/BasicBlock.h"
26 #include "llvm/IR/CFG.h"
27 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DomTreeUpdater.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/IR/User.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/IR/ValueHandle.h"
41 #include "llvm/Support/Casting.h"
43 #include <cassert>
44 #include <cstdint>
45 #include <string>
46 #include <utility>
47 #include <vector>
48 
49 using namespace llvm;
50 
53  DeleteDeadBlocks(BBs, DTU);
54 }
55 
57  DomTreeUpdater *DTU) {
58 #ifndef NDEBUG
59  // Make sure that all predecessors of each dead block is also dead.
61  assert(Dead.size() == BBs.size() && "Duplicating blocks?");
62  for (auto *BB : Dead)
63  for (BasicBlock *Pred : predecessors(BB))
64  assert(Dead.count(Pred) && "All predecessors must be dead!");
65 #endif
66 
68  for (auto *BB : BBs) {
69  // Loop through all of our successors and make sure they know that one
70  // of their predecessors is going away.
71  for (BasicBlock *Succ : successors(BB)) {
72  Succ->removePredecessor(BB);
73  if (DTU)
74  Updates.push_back({DominatorTree::Delete, BB, Succ});
75  }
76 
77  // Zap all the instructions in the block.
78  while (!BB->empty()) {
79  Instruction &I = BB->back();
80  // If this instruction is used, replace uses with an arbitrary value.
81  // Because control flow can't get here, we don't care what we replace the
82  // value with. Note that since this block is unreachable, and all values
83  // contained within it must dominate their uses, that all uses will
84  // eventually be removed (they are themselves dead).
85  if (!I.use_empty())
87  BB->getInstList().pop_back();
88  }
89  new UnreachableInst(BB->getContext(), BB);
90  assert(BB->getInstList().size() == 1 &&
91  isa<UnreachableInst>(BB->getTerminator()) &&
92  "The successor list of BB isn't empty before "
93  "applying corresponding DTU updates.");
94  }
95  if (DTU)
96  DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true);
97 
98  for (BasicBlock *BB : BBs)
99  if (DTU)
100  DTU->deleteBB(BB);
101  else
102  BB->eraseFromParent();
103 }
104 
106  MemoryDependenceResults *MemDep) {
107  if (!isa<PHINode>(BB->begin())) return;
108 
109  while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
110  if (PN->getIncomingValue(0) != PN)
111  PN->replaceAllUsesWith(PN->getIncomingValue(0));
112  else
113  PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
114 
115  if (MemDep)
116  MemDep->removeInstruction(PN); // Memdep updates AA itself.
117 
118  PN->eraseFromParent();
119  }
120 }
121 
123  // Recursively deleting a PHI may cause multiple PHIs to be deleted
124  // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.
126  for (PHINode &PN : BB->phis())
127  PHIs.push_back(&PN);
128 
129  bool Changed = false;
130  for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
131  if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
132  Changed |= RecursivelyDeleteDeadPHINode(PN, TLI);
133 
134  return Changed;
135 }
136 
138  LoopInfo *LI, MemorySSAUpdater *MSSAU,
139  MemoryDependenceResults *MemDep) {
140  if (BB->hasAddressTaken())
141  return false;
142 
143  // Can't merge if there are multiple predecessors, or no predecessors.
144  BasicBlock *PredBB = BB->getUniquePredecessor();
145  if (!PredBB) return false;
146 
147  // Don't break self-loops.
148  if (PredBB == BB) return false;
149  // Don't break unwinding instructions.
150  if (PredBB->getTerminator()->isExceptionalTerminator())
151  return false;
152 
153  // Can't merge if there are multiple distinct successors.
154  if (PredBB->getUniqueSuccessor() != BB)
155  return false;
156 
157  // Can't merge if there is PHI loop.
158  for (PHINode &PN : BB->phis())
159  for (Value *IncValue : PN.incoming_values())
160  if (IncValue == &PN)
161  return false;
162 
163  // Begin by getting rid of unneeded PHIs.
164  SmallVector<AssertingVH<Value>, 4> IncomingValues;
165  if (isa<PHINode>(BB->front())) {
166  for (PHINode &PN : BB->phis())
167  if (!isa<PHINode>(PN.getIncomingValue(0)) ||
168  cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB)
169  IncomingValues.push_back(PN.getIncomingValue(0));
170  FoldSingleEntryPHINodes(BB, MemDep);
171  }
172 
173  // DTU update: Collect all the edges that exit BB.
174  // These dominator edges will be redirected from Pred.
175  std::vector<DominatorTree::UpdateType> Updates;
176  if (DTU) {
177  Updates.reserve(1 + (2 * succ_size(BB)));
178  Updates.push_back({DominatorTree::Delete, PredBB, BB});
179  for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
180  Updates.push_back({DominatorTree::Delete, BB, *I});
181  Updates.push_back({DominatorTree::Insert, PredBB, *I});
182  }
183  }
184 
185  if (MSSAU)
186  MSSAU->moveAllAfterMergeBlocks(BB, PredBB, &*(BB->begin()));
187 
188  // Delete the unconditional branch from the predecessor...
189  PredBB->getInstList().pop_back();
190 
191  // Make all PHI nodes that referred to BB now refer to Pred as their
192  // source...
193  BB->replaceAllUsesWith(PredBB);
194 
195  // Move all definitions in the successor to the predecessor...
196  PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
197  new UnreachableInst(BB->getContext(), BB);
198 
199  // Eliminate duplicate dbg.values describing the entry PHI node post-splice.
200  for (auto Incoming : IncomingValues) {
201  if (isa<Instruction>(*Incoming)) {
204  DbgValueSet;
205  llvm::findDbgValues(DbgValues, Incoming);
206  for (auto &DVI : DbgValues) {
207  auto R = DbgValueSet.insert({DVI->getVariable(), DVI->getExpression()});
208  if (!R.second)
209  DVI->eraseFromParent();
210  }
211  }
212  }
213 
214  // Inherit predecessors name if it exists.
215  if (!PredBB->hasName())
216  PredBB->takeName(BB);
217 
218  if (LI)
219  LI->removeBlock(BB);
220 
221  if (MemDep)
223 
224  // Finally, erase the old block and update dominator info.
225  if (DTU) {
226  assert(BB->getInstList().size() == 1 &&
227  isa<UnreachableInst>(BB->getTerminator()) &&
228  "The successor list of BB isn't empty before "
229  "applying corresponding DTU updates.");
230  DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true);
231  DTU->deleteBB(BB);
232  }
233 
234  else {
235  BB->eraseFromParent(); // Nuke BB if DTU is nullptr.
236  }
237  return true;
238 }
239 
241  BasicBlock::iterator &BI, Value *V) {
242  Instruction &I = *BI;
243  // Replaces all of the uses of the instruction with uses of the value
244  I.replaceAllUsesWith(V);
245 
246  // Make sure to propagate a name if there is one already.
247  if (I.hasName() && !V->hasName())
248  V->takeName(&I);
249 
250  // Delete the unnecessary instruction now...
251  BI = BIL.erase(BI);
252 }
253 
256  assert(I->getParent() == nullptr &&
257  "ReplaceInstWithInst: Instruction already inserted into basic block!");
258 
259  // Copy debug location to newly added instruction, if it wasn't already set
260  // by the caller.
261  if (!I->getDebugLoc())
262  I->setDebugLoc(BI->getDebugLoc());
263 
264  // Insert the new instruction into the basic block...
265  BasicBlock::iterator New = BIL.insert(BI, I);
266 
267  // Replace all uses of the old instruction, and delete it.
268  ReplaceInstWithValue(BIL, BI, I);
269 
270  // Move BI back to point to the newly inserted instruction
271  BI = New;
272 }
273 
275  BasicBlock::iterator BI(From);
276  ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
277 }
278 
280  LoopInfo *LI, MemorySSAUpdater *MSSAU) {
281  unsigned SuccNum = GetSuccessorNumber(BB, Succ);
282 
283  // If this is a critical edge, let SplitCriticalEdge do it.
284  Instruction *LatchTerm = BB->getTerminator();
285  if (SplitCriticalEdge(
286  LatchTerm, SuccNum,
287  CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA()))
288  return LatchTerm->getSuccessor(SuccNum);
289 
290  // If the edge isn't critical, then BB has a single successor or Succ has a
291  // single pred. Split the block.
292  if (BasicBlock *SP = Succ->getSinglePredecessor()) {
293  // If the successor only has a single pred, split the top of the successor
294  // block.
295  assert(SP == BB && "CFG broken");
296  SP = nullptr;
297  return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU);
298  }
299 
300  // Otherwise, if BB has a single successor, split it at the bottom of the
301  // block.
302  assert(BB->getTerminator()->getNumSuccessors() == 1 &&
303  "Should have a single succ!");
304  return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU);
305 }
306 
307 unsigned
309  const CriticalEdgeSplittingOptions &Options) {
310  unsigned NumBroken = 0;
311  for (BasicBlock &BB : F) {
312  Instruction *TI = BB.getTerminator();
313  if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
314  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
315  if (SplitCriticalEdge(TI, i, Options))
316  ++NumBroken;
317  }
318  return NumBroken;
319 }
320 
322  DominatorTree *DT, LoopInfo *LI,
323  MemorySSAUpdater *MSSAU) {
324  BasicBlock::iterator SplitIt = SplitPt->getIterator();
325  while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
326  ++SplitIt;
327  BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
328 
329  // The new block lives in whichever loop the old one did. This preserves
330  // LCSSA as well, because we force the split point to be after any PHI nodes.
331  if (LI)
332  if (Loop *L = LI->getLoopFor(Old))
333  L->addBasicBlockToLoop(New, *LI);
334 
335  if (DT)
336  // Old dominates New. New node dominates all other nodes dominated by Old.
337  if (DomTreeNode *OldNode = DT->getNode(Old)) {
338  std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
339 
340  DomTreeNode *NewNode = DT->addNewBlock(New, Old);
341  for (DomTreeNode *I : Children)
342  DT->changeImmediateDominator(I, NewNode);
343  }
344 
345  // Move MemoryAccesses still tracked in Old, but part of New now.
346  // Update accesses in successor blocks accordingly.
347  if (MSSAU)
348  MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));
349 
350  return New;
351 }
352 
353 /// Update DominatorTree, LoopInfo, and LCCSA analysis information.
356  DominatorTree *DT, LoopInfo *LI,
357  MemorySSAUpdater *MSSAU,
358  bool PreserveLCSSA, bool &HasLoopExit) {
359  // Update dominator tree if available.
360  if (DT) {
361  if (OldBB == DT->getRootNode()->getBlock()) {
362  assert(NewBB == &NewBB->getParent()->getEntryBlock());
363  DT->setNewRoot(NewBB);
364  } else {
365  // Split block expects NewBB to have a non-empty set of predecessors.
366  DT->splitBlock(NewBB);
367  }
368  }
369 
370  // Update MemoryPhis after split if MemorySSA is available
371  if (MSSAU)
372  MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);
373 
374  // The rest of the logic is only relevant for updating the loop structures.
375  if (!LI)
376  return;
377 
378  assert(DT && "DT should be available to update LoopInfo!");
379  Loop *L = LI->getLoopFor(OldBB);
380 
381  // If we need to preserve loop analyses, collect some information about how
382  // this split will affect loops.
383  bool IsLoopEntry = !!L;
384  bool SplitMakesNewLoopHeader = false;
385  for (BasicBlock *Pred : Preds) {
386  // Preds that are not reachable from entry should not be used to identify if
387  // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks
388  // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader
389  // as true and make the NewBB the header of some loop. This breaks LI.
390  if (!DT->isReachableFromEntry(Pred))
391  continue;
392  // If we need to preserve LCSSA, determine if any of the preds is a loop
393  // exit.
394  if (PreserveLCSSA)
395  if (Loop *PL = LI->getLoopFor(Pred))
396  if (!PL->contains(OldBB))
397  HasLoopExit = true;
398 
399  // If we need to preserve LoopInfo, note whether any of the preds crosses
400  // an interesting loop boundary.
401  if (!L)
402  continue;
403  if (L->contains(Pred))
404  IsLoopEntry = false;
405  else
406  SplitMakesNewLoopHeader = true;
407  }
408 
409  // Unless we have a loop for OldBB, nothing else to do here.
410  if (!L)
411  return;
412 
413  if (IsLoopEntry) {
414  // Add the new block to the nearest enclosing loop (and not an adjacent
415  // loop). To find this, examine each of the predecessors and determine which
416  // loops enclose them, and select the most-nested loop which contains the
417  // loop containing the block being split.
418  Loop *InnermostPredLoop = nullptr;
419  for (BasicBlock *Pred : Preds) {
420  if (Loop *PredLoop = LI->getLoopFor(Pred)) {
421  // Seek a loop which actually contains the block being split (to avoid
422  // adjacent loops).
423  while (PredLoop && !PredLoop->contains(OldBB))
424  PredLoop = PredLoop->getParentLoop();
425 
426  // Select the most-nested of these loops which contains the block.
427  if (PredLoop && PredLoop->contains(OldBB) &&
428  (!InnermostPredLoop ||
429  InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
430  InnermostPredLoop = PredLoop;
431  }
432  }
433 
434  if (InnermostPredLoop)
435  InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
436  } else {
437  L->addBasicBlockToLoop(NewBB, *LI);
438  if (SplitMakesNewLoopHeader)
439  L->moveToHeader(NewBB);
440  }
441 }
442 
443 /// Update the PHI nodes in OrigBB to include the values coming from NewBB.
444 /// This also updates AliasAnalysis, if available.
445 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
447  bool HasLoopExit) {
448  // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
449  SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
450  for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
451  PHINode *PN = cast<PHINode>(I++);
452 
453  // Check to see if all of the values coming in are the same. If so, we
454  // don't need to create a new PHI node, unless it's needed for LCSSA.
455  Value *InVal = nullptr;
456  if (!HasLoopExit) {
457  InVal = PN->getIncomingValueForBlock(Preds[0]);
458  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
459  if (!PredSet.count(PN->getIncomingBlock(i)))
460  continue;
461  if (!InVal)
462  InVal = PN->getIncomingValue(i);
463  else if (InVal != PN->getIncomingValue(i)) {
464  InVal = nullptr;
465  break;
466  }
467  }
468  }
469 
470  if (InVal) {
471  // If all incoming values for the new PHI would be the same, just don't
472  // make a new PHI. Instead, just remove the incoming values from the old
473  // PHI.
474 
475  // NOTE! This loop walks backwards for a reason! First off, this minimizes
476  // the cost of removal if we end up removing a large number of values, and
477  // second off, this ensures that the indices for the incoming values
478  // aren't invalidated when we remove one.
479  for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
480  if (PredSet.count(PN->getIncomingBlock(i)))
481  PN->removeIncomingValue(i, false);
482 
483  // Add an incoming value to the PHI node in the loop for the preheader
484  // edge.
485  PN->addIncoming(InVal, NewBB);
486  continue;
487  }
488 
489  // If the values coming into the block are not the same, we need a new
490  // PHI.
491  // Create the new PHI node, insert it into NewBB at the end of the block
492  PHINode *NewPHI =
493  PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
494 
495  // NOTE! This loop walks backwards for a reason! First off, this minimizes
496  // the cost of removal if we end up removing a large number of values, and
497  // second off, this ensures that the indices for the incoming values aren't
498  // invalidated when we remove one.
499  for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
500  BasicBlock *IncomingBB = PN->getIncomingBlock(i);
501  if (PredSet.count(IncomingBB)) {
502  Value *V = PN->removeIncomingValue(i, false);
503  NewPHI->addIncoming(V, IncomingBB);
504  }
505  }
506 
507  PN->addIncoming(NewPHI, NewBB);
508  }
509 }
510 
513  const char *Suffix, DominatorTree *DT,
514  LoopInfo *LI, MemorySSAUpdater *MSSAU,
515  bool PreserveLCSSA) {
516  // Do not attempt to split that which cannot be split.
517  if (!BB->canSplitPredecessors())
518  return nullptr;
519 
520  // For the landingpads we need to act a bit differently.
521  // Delegate this work to the SplitLandingPadPredecessors.
522  if (BB->isLandingPad()) {
524  std::string NewName = std::string(Suffix) + ".split-lp";
525 
526  SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs, DT,
527  LI, MSSAU, PreserveLCSSA);
528  return NewBBs[0];
529  }
530 
531  // Create new basic block, insert right before the original block.
533  BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
534 
535  // The new block unconditionally branches to the old block.
536  BranchInst *BI = BranchInst::Create(BB, NewBB);
537  BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
538 
539  // Move the edges from Preds to point to NewBB instead of BB.
540  for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
541  // This is slightly more strict than necessary; the minimum requirement
542  // is that there be no more than one indirectbr branching to BB. And
543  // all BlockAddress uses would need to be updated.
544  assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
545  "Cannot split an edge from an IndirectBrInst");
546  Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
547  }
548 
549  // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
550  // node becomes an incoming value for BB's phi node. However, if the Preds
551  // list is empty, we need to insert dummy entries into the PHI nodes in BB to
552  // account for the newly created predecessor.
553  if (Preds.empty()) {
554  // Insert dummy values as the incoming value.
555  for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
556  cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
557  }
558 
559  // Update DominatorTree, LoopInfo, and LCCSA analysis information.
560  bool HasLoopExit = false;
561  UpdateAnalysisInformation(BB, NewBB, Preds, DT, LI, MSSAU, PreserveLCSSA,
562  HasLoopExit);
563 
564  if (!Preds.empty()) {
565  // Update the PHI nodes in BB with the values coming from NewBB.
566  UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
567  }
568 
569  return NewBB;
570 }
571 
574  const char *Suffix1, const char *Suffix2,
576  DominatorTree *DT, LoopInfo *LI,
577  MemorySSAUpdater *MSSAU,
578  bool PreserveLCSSA) {
579  assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
580 
581  // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
582  // it right before the original block.
583  BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
584  OrigBB->getName() + Suffix1,
585  OrigBB->getParent(), OrigBB);
586  NewBBs.push_back(NewBB1);
587 
588  // The new block unconditionally branches to the old block.
589  BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
590  BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
591 
592  // Move the edges from Preds to point to NewBB1 instead of OrigBB.
593  for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
594  // This is slightly more strict than necessary; the minimum requirement
595  // is that there be no more than one indirectbr branching to BB. And
596  // all BlockAddress uses would need to be updated.
597  assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
598  "Cannot split an edge from an IndirectBrInst");
599  Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
600  }
601 
602  bool HasLoopExit = false;
603  UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DT, LI, MSSAU, PreserveLCSSA,
604  HasLoopExit);
605 
606  // Update the PHI nodes in OrigBB with the values coming from NewBB1.
607  UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
608 
609  // Move the remaining edges from OrigBB to point to NewBB2.
610  SmallVector<BasicBlock*, 8> NewBB2Preds;
611  for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
612  i != e; ) {
613  BasicBlock *Pred = *i++;
614  if (Pred == NewBB1) continue;
615  assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
616  "Cannot split an edge from an IndirectBrInst");
617  NewBB2Preds.push_back(Pred);
618  e = pred_end(OrigBB);
619  }
620 
621  BasicBlock *NewBB2 = nullptr;
622  if (!NewBB2Preds.empty()) {
623  // Create another basic block for the rest of OrigBB's predecessors.
624  NewBB2 = BasicBlock::Create(OrigBB->getContext(),
625  OrigBB->getName() + Suffix2,
626  OrigBB->getParent(), OrigBB);
627  NewBBs.push_back(NewBB2);
628 
629  // The new block unconditionally branches to the old block.
630  BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
631  BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
632 
633  // Move the remaining edges from OrigBB to point to NewBB2.
634  for (BasicBlock *NewBB2Pred : NewBB2Preds)
635  NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
636 
637  // Update DominatorTree, LoopInfo, and LCCSA analysis information.
638  HasLoopExit = false;
639  UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DT, LI, MSSAU,
640  PreserveLCSSA, HasLoopExit);
641 
642  // Update the PHI nodes in OrigBB with the values coming from NewBB2.
643  UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
644  }
645 
646  LandingPadInst *LPad = OrigBB->getLandingPadInst();
647  Instruction *Clone1 = LPad->clone();
648  Clone1->setName(Twine("lpad") + Suffix1);
649  NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
650 
651  if (NewBB2) {
652  Instruction *Clone2 = LPad->clone();
653  Clone2->setName(Twine("lpad") + Suffix2);
654  NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
655 
656  // Create a PHI node for the two cloned landingpad instructions only
657  // if the original landingpad instruction has some uses.
658  if (!LPad->use_empty()) {
659  assert(!LPad->getType()->isTokenTy() &&
660  "Split cannot be applied if LPad is token type. Otherwise an "
661  "invalid PHINode of token type would be created.");
662  PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
663  PN->addIncoming(Clone1, NewBB1);
664  PN->addIncoming(Clone2, NewBB2);
665  LPad->replaceAllUsesWith(PN);
666  }
667  LPad->eraseFromParent();
668  } else {
669  // There is no second clone. Just replace the landing pad with the first
670  // clone.
671  LPad->replaceAllUsesWith(Clone1);
672  LPad->eraseFromParent();
673  }
674 }
675 
677  BasicBlock *Pred,
678  DomTreeUpdater *DTU) {
679  Instruction *UncondBranch = Pred->getTerminator();
680  // Clone the return and add it to the end of the predecessor.
681  Instruction *NewRet = RI->clone();
682  Pred->getInstList().push_back(NewRet);
683 
684  // If the return instruction returns a value, and if the value was a
685  // PHI node in "BB", propagate the right value into the return.
686  for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
687  i != e; ++i) {
688  Value *V = *i;
689  Instruction *NewBC = nullptr;
690  if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
691  // Return value might be bitcasted. Clone and insert it before the
692  // return instruction.
693  V = BCI->getOperand(0);
694  NewBC = BCI->clone();
695  Pred->getInstList().insert(NewRet->getIterator(), NewBC);
696  *i = NewBC;
697  }
698  if (PHINode *PN = dyn_cast<PHINode>(V)) {
699  if (PN->getParent() == BB) {
700  if (NewBC)
701  NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
702  else
703  *i = PN->getIncomingValueForBlock(Pred);
704  }
705  }
706  }
707 
708  // Update any PHI nodes in the returning block to realize that we no
709  // longer branch to them.
710  BB->removePredecessor(Pred);
711  UncondBranch->eraseFromParent();
712 
713  if (DTU)
714  DTU->deleteEdge(Pred, BB);
715 
716  return cast<ReturnInst>(NewRet);
717 }
718 
720  Instruction *SplitBefore,
721  bool Unreachable,
722  MDNode *BranchWeights,
723  DominatorTree *DT, LoopInfo *LI) {
724  BasicBlock *Head = SplitBefore->getParent();
725  BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
726  Instruction *HeadOldTerm = Head->getTerminator();
727  LLVMContext &C = Head->getContext();
728  BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
729  Instruction *CheckTerm;
730  if (Unreachable)
731  CheckTerm = new UnreachableInst(C, ThenBlock);
732  else
733  CheckTerm = BranchInst::Create(Tail, ThenBlock);
734  CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
735  BranchInst *HeadNewTerm =
736  BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond);
737  HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
738  ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
739 
740  if (DT) {
741  if (DomTreeNode *OldNode = DT->getNode(Head)) {
742  std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
743 
744  DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
745  for (DomTreeNode *Child : Children)
746  DT->changeImmediateDominator(Child, NewNode);
747 
748  // Head dominates ThenBlock.
749  DT->addNewBlock(ThenBlock, Head);
750  }
751  }
752 
753  if (LI) {
754  if (Loop *L = LI->getLoopFor(Head)) {
755  L->addBasicBlockToLoop(ThenBlock, *LI);
756  L->addBasicBlockToLoop(Tail, *LI);
757  }
758  }
759 
760  return CheckTerm;
761 }
762 
764  Instruction **ThenTerm,
765  Instruction **ElseTerm,
766  MDNode *BranchWeights) {
767  BasicBlock *Head = SplitBefore->getParent();
768  BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
769  Instruction *HeadOldTerm = Head->getTerminator();
770  LLVMContext &C = Head->getContext();
771  BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
772  BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
773  *ThenTerm = BranchInst::Create(Tail, ThenBlock);
774  (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
775  *ElseTerm = BranchInst::Create(Tail, ElseBlock);
776  (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
777  BranchInst *HeadNewTerm =
778  BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
779  HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
780  ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
781 }
782 
784  BasicBlock *&IfFalse) {
785  PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
786  BasicBlock *Pred1 = nullptr;
787  BasicBlock *Pred2 = nullptr;
788 
789  if (SomePHI) {
790  if (SomePHI->getNumIncomingValues() != 2)
791  return nullptr;
792  Pred1 = SomePHI->getIncomingBlock(0);
793  Pred2 = SomePHI->getIncomingBlock(1);
794  } else {
795  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
796  if (PI == PE) // No predecessor
797  return nullptr;
798  Pred1 = *PI++;
799  if (PI == PE) // Only one predecessor
800  return nullptr;
801  Pred2 = *PI++;
802  if (PI != PE) // More than two predecessors
803  return nullptr;
804  }
805 
806  // We can only handle branches. Other control flow will be lowered to
807  // branches if possible anyway.
808  BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
809  BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
810  if (!Pred1Br || !Pred2Br)
811  return nullptr;
812 
813  // Eliminate code duplication by ensuring that Pred1Br is conditional if
814  // either are.
815  if (Pred2Br->isConditional()) {
816  // If both branches are conditional, we don't have an "if statement". In
817  // reality, we could transform this case, but since the condition will be
818  // required anyway, we stand no chance of eliminating it, so the xform is
819  // probably not profitable.
820  if (Pred1Br->isConditional())
821  return nullptr;
822 
823  std::swap(Pred1, Pred2);
824  std::swap(Pred1Br, Pred2Br);
825  }
826 
827  if (Pred1Br->isConditional()) {
828  // The only thing we have to watch out for here is to make sure that Pred2
829  // doesn't have incoming edges from other blocks. If it does, the condition
830  // doesn't dominate BB.
831  if (!Pred2->getSinglePredecessor())
832  return nullptr;
833 
834  // If we found a conditional branch predecessor, make sure that it branches
835  // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
836  if (Pred1Br->getSuccessor(0) == BB &&
837  Pred1Br->getSuccessor(1) == Pred2) {
838  IfTrue = Pred1;
839  IfFalse = Pred2;
840  } else if (Pred1Br->getSuccessor(0) == Pred2 &&
841  Pred1Br->getSuccessor(1) == BB) {
842  IfTrue = Pred2;
843  IfFalse = Pred1;
844  } else {
845  // We know that one arm of the conditional goes to BB, so the other must
846  // go somewhere unrelated, and this must not be an "if statement".
847  return nullptr;
848  }
849 
850  return Pred1Br->getCondition();
851  }
852 
853  // Ok, if we got here, both predecessors end with an unconditional branch to
854  // BB. Don't panic! If both blocks only have a single (identical)
855  // predecessor, and THAT is a conditional branch, then we're all ok!
856  BasicBlock *CommonPred = Pred1->getSinglePredecessor();
857  if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
858  return nullptr;
859 
860  // Otherwise, if this is a conditional branch, then we can use it!
861  BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
862  if (!BI) return nullptr;
863 
864  assert(BI->isConditional() && "Two successors but not conditional?");
865  if (BI->getSuccessor(0) == Pred1) {
866  IfTrue = Pred1;
867  IfFalse = Pred2;
868  } else {
869  IfTrue = Pred2;
870  IfFalse = Pred1;
871  }
872  return BI->getCondition();
873 }
uint64_t CallInst * C
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:68
bool canSplitPredecessors() const
Definition: BasicBlock.cpp:372
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:302
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 splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
iterator erase(iterator where)
Definition: ilist.h:267
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr)
Attempts to merge a block into its predecessor, if possible.
iterator begin() const
Definition: ArrayRef.h:137
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
unsigned getLoopDepth() const
Return the nesting level of this loop.
Definition: LoopInfo.h:92
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:379
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
Delete the specified block, which must have no predecessors.
BasicBlock * getSuccessor(unsigned i) const
Metadata node.
Definition: Metadata.h:864
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
const Instruction * 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:138
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:33
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:300
op_iterator op_begin()
Definition: User.h:230
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:269
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Split the edge connecting specified block.
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:690
void DeleteDeadBlocks(SmallVectorImpl< BasicBlock *> &BBs, DomTreeUpdater *DTU=nullptr)
Delete the specified blocks from BB.
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:285
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
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred, DomTreeUpdater *DTU=nullptr)
This method duplicates the specified return instruction into a predecessor which ends in an unconditi...
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:1495
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:251
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:247
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
BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
If this edge is a critical edge, insert a new node to split the critical edge.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
DomTreeNodeBase< NodeT > * setNewRoot(NodeT *BB)
Add a new node to the forward dominator tree and make it a new root.
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:291
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:106
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock *> Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock *> &NewBBs, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
const BasicBlock & getEntryBlock() const
Definition: Function.h:640
NodeT * getBlock() const
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock *> Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
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:190
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:217
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:308
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:234
bool hasName() const
Definition: Value.h:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
void deleteBB(BasicBlock *DelBB)
Delete DelBB.
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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
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:281
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
void moveAllAfterSpliceBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was spliced into From and To.
op_iterator op_end()
Definition: User.h:232
void splice(iterator where, iplist_impl &L2)
Definition: ilist.h:329
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:116
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
self_iterator getIterator()
Definition: ilist_node.h:82
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1415
bool isExceptionalTerminator() const
Definition: Instruction.h:136
size_t size() const
Definition: SmallVector.h:53
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
void deleteEdge(BasicBlock *From, BasicBlock *To)
Notify all available trees on an edge deletion.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1226
bool isLandingPad() const
Return true if this basic block is a landing pad.
Definition: BasicBlock.cpp:468
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
Definition: BasicBlock.h:392
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:334
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
BlockVerifier::State From
iterator end()
Definition: BasicBlock.h:271
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
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:513
Value * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
Check whether BB is the merge point of a if-region.
void wireOldPredecessorsToNewImmediatePredecessor(BasicBlock *Old, BasicBlock *New, ArrayRef< BasicBlock *> Preds, bool IdenticalEdgesWereMerged=true)
A new empty BasicBlock (New) now branches directly to Old.
void applyUpdates(ArrayRef< DominatorTree::UpdateType > Updates, bool ForceRemoveDuplicates=false)
Apply updates on all available trees.
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...
pred_range predecessors(BasicBlock *BB)
Definition: CFG.h:125
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void setOperand(unsigned i, Value *Val)
Definition: User.h:175
Implements a dense probed hash-table based set with some number of buckets stored inline...
Definition: DenseSet.h:268
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
void push_back(pointer val)
Definition: ilist.h:313
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:90
void FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
Instruction * 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 ...
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
LoopT * getParentLoop() const
Definition: LoopInfo.h:101
unsigned succ_size(const Instruction *I)
Definition: CFG.h:261
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
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:228
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:311
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:465
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:214
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:107
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink &#39;this&#39; from the containing function and delete it.
Definition: BasicBlock.cpp:115
#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
void moveAllAfterMergeBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was merged into To.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:325
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:408
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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...
succ_range successors(Instruction *I)
Definition: CFG.h:264
static const Function * getParent(const Value *V)
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
const LandingPadInst * getLandingPadInst() const
Return the landingpad instruction associated with the landing pad.
Definition: BasicBlock.cpp:473
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:197
void pop_back()
Definition: ilist.h:318
bool use_empty() const
Definition: Value.h:323
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:749
static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, ArrayRef< BasicBlock *> Preds, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA, bool &HasLoopExit)
Update DominatorTree, LoopInfo, and LCCSA analysis information.
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
const BasicBlock * getParent() const
Definition: Instruction.h:67
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
Definition: BasicBlock.cpp:277