• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- BasicBlockUtils.cpp - BasicBlock Utilities --------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This family of functions perform manipulations on basic blocks, and
10 // instructions contained within basic blocks.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Analysis/CFG.h"
20 #include "llvm/Analysis/DomTreeUpdater.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
23 #include "llvm/Analysis/MemorySSAUpdater.h"
24 #include "llvm/Analysis/PostDominators.h"
25 #include "llvm/IR/BasicBlock.h"
26 #include "llvm/IR/CFG.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DebugInfoMetadata.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/InstrTypes.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/IR/User.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/IR/ValueHandle.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Transforms/Utils/Local.h"
44 #include <cassert>
45 #include <cstdint>
46 #include <string>
47 #include <utility>
48 #include <vector>
49 
50 using namespace llvm;
51 
52 #define DEBUG_TYPE "basicblock-utils"
53 
DetatchDeadBlocks(ArrayRef<BasicBlock * > BBs,SmallVectorImpl<DominatorTree::UpdateType> * Updates,bool KeepOneInputPHIs)54 void llvm::DetatchDeadBlocks(
55     ArrayRef<BasicBlock *> BBs,
56     SmallVectorImpl<DominatorTree::UpdateType> *Updates,
57     bool KeepOneInputPHIs) {
58   for (auto *BB : BBs) {
59     // Loop through all of our successors and make sure they know that one
60     // of their predecessors is going away.
61     SmallPtrSet<BasicBlock *, 4> UniqueSuccessors;
62     for (BasicBlock *Succ : successors(BB)) {
63       Succ->removePredecessor(BB, KeepOneInputPHIs);
64       if (Updates && UniqueSuccessors.insert(Succ).second)
65         Updates->push_back({DominatorTree::Delete, BB, Succ});
66     }
67 
68     // Zap all the instructions in the block.
69     while (!BB->empty()) {
70       Instruction &I = BB->back();
71       // If this instruction is used, replace uses with an arbitrary value.
72       // Because control flow can't get here, we don't care what we replace the
73       // value with.  Note that since this block is unreachable, and all values
74       // contained within it must dominate their uses, that all uses will
75       // eventually be removed (they are themselves dead).
76       if (!I.use_empty())
77         I.replaceAllUsesWith(UndefValue::get(I.getType()));
78       BB->getInstList().pop_back();
79     }
80     new UnreachableInst(BB->getContext(), BB);
81     assert(BB->getInstList().size() == 1 &&
82            isa<UnreachableInst>(BB->getTerminator()) &&
83            "The successor list of BB isn't empty before "
84            "applying corresponding DTU updates.");
85   }
86 }
87 
DeleteDeadBlock(BasicBlock * BB,DomTreeUpdater * DTU,bool KeepOneInputPHIs)88 void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU,
89                            bool KeepOneInputPHIs) {
90   DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs);
91 }
92 
DeleteDeadBlocks(ArrayRef<BasicBlock * > BBs,DomTreeUpdater * DTU,bool KeepOneInputPHIs)93 void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU,
94                             bool KeepOneInputPHIs) {
95 #ifndef NDEBUG
96   // Make sure that all predecessors of each dead block is also dead.
97   SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end());
98   assert(Dead.size() == BBs.size() && "Duplicating blocks?");
99   for (auto *BB : Dead)
100     for (BasicBlock *Pred : predecessors(BB))
101       assert(Dead.count(Pred) && "All predecessors must be dead!");
102 #endif
103 
104   SmallVector<DominatorTree::UpdateType, 4> Updates;
105   DetatchDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs);
106 
107   if (DTU)
108     DTU->applyUpdatesPermissive(Updates);
109 
110   for (BasicBlock *BB : BBs)
111     if (DTU)
112       DTU->deleteBB(BB);
113     else
114       BB->eraseFromParent();
115 }
116 
EliminateUnreachableBlocks(Function & F,DomTreeUpdater * DTU,bool KeepOneInputPHIs)117 bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
118                                       bool KeepOneInputPHIs) {
119   df_iterator_default_set<BasicBlock*> Reachable;
120 
121   // Mark all reachable blocks.
122   for (BasicBlock *BB : depth_first_ext(&F, Reachable))
123     (void)BB/* Mark all reachable blocks */;
124 
125   // Collect all dead blocks.
126   std::vector<BasicBlock*> DeadBlocks;
127   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
128     if (!Reachable.count(&*I)) {
129       BasicBlock *BB = &*I;
130       DeadBlocks.push_back(BB);
131     }
132 
133   // Delete the dead blocks.
134   DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs);
135 
136   return !DeadBlocks.empty();
137 }
138 
FoldSingleEntryPHINodes(BasicBlock * BB,MemoryDependenceResults * MemDep)139 void llvm::FoldSingleEntryPHINodes(BasicBlock *BB,
140                                    MemoryDependenceResults *MemDep) {
141   if (!isa<PHINode>(BB->begin())) return;
142 
143   while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
144     if (PN->getIncomingValue(0) != PN)
145       PN->replaceAllUsesWith(PN->getIncomingValue(0));
146     else
147       PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
148 
149     if (MemDep)
150       MemDep->removeInstruction(PN);  // Memdep updates AA itself.
151 
152     PN->eraseFromParent();
153   }
154 }
155 
DeleteDeadPHIs(BasicBlock * BB,const TargetLibraryInfo * TLI,MemorySSAUpdater * MSSAU)156 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI,
157                           MemorySSAUpdater *MSSAU) {
158   // Recursively deleting a PHI may cause multiple PHIs to be deleted
159   // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.
160   SmallVector<WeakTrackingVH, 8> PHIs;
161   for (PHINode &PN : BB->phis())
162     PHIs.push_back(&PN);
163 
164   bool Changed = false;
165   for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
166     if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
167       Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU);
168 
169   return Changed;
170 }
171 
MergeBlockIntoPredecessor(BasicBlock * BB,DomTreeUpdater * DTU,LoopInfo * LI,MemorySSAUpdater * MSSAU,MemoryDependenceResults * MemDep,bool PredecessorWithTwoSuccessors)172 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
173                                      LoopInfo *LI, MemorySSAUpdater *MSSAU,
174                                      MemoryDependenceResults *MemDep,
175                                      bool PredecessorWithTwoSuccessors) {
176   if (BB->hasAddressTaken())
177     return false;
178 
179   // Can't merge if there are multiple predecessors, or no predecessors.
180   BasicBlock *PredBB = BB->getUniquePredecessor();
181   if (!PredBB) return false;
182 
183   // Don't break self-loops.
184   if (PredBB == BB) return false;
185   // Don't break unwinding instructions.
186   if (PredBB->getTerminator()->isExceptionalTerminator())
187     return false;
188 
189   // Can't merge if there are multiple distinct successors.
190   if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB)
191     return false;
192 
193   // Currently only allow PredBB to have two predecessors, one being BB.
194   // Update BI to branch to BB's only successor instead of BB.
195   BranchInst *PredBB_BI;
196   BasicBlock *NewSucc = nullptr;
197   unsigned FallThruPath;
198   if (PredecessorWithTwoSuccessors) {
199     if (!(PredBB_BI = dyn_cast<BranchInst>(PredBB->getTerminator())))
200       return false;
201     BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator());
202     if (!BB_JmpI || !BB_JmpI->isUnconditional())
203       return false;
204     NewSucc = BB_JmpI->getSuccessor(0);
205     FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1;
206   }
207 
208   // Can't merge if there is PHI loop.
209   for (PHINode &PN : BB->phis())
210     for (Value *IncValue : PN.incoming_values())
211       if (IncValue == &PN)
212         return false;
213 
214   LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "
215                     << PredBB->getName() << "\n");
216 
217   // Begin by getting rid of unneeded PHIs.
218   SmallVector<AssertingVH<Value>, 4> IncomingValues;
219   if (isa<PHINode>(BB->front())) {
220     for (PHINode &PN : BB->phis())
221       if (!isa<PHINode>(PN.getIncomingValue(0)) ||
222           cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB)
223         IncomingValues.push_back(PN.getIncomingValue(0));
224     FoldSingleEntryPHINodes(BB, MemDep);
225   }
226 
227   // DTU update: Collect all the edges that exit BB.
228   // These dominator edges will be redirected from Pred.
229   std::vector<DominatorTree::UpdateType> Updates;
230   if (DTU) {
231     Updates.reserve(1 + (2 * succ_size(BB)));
232     // Add insert edges first. Experimentally, for the particular case of two
233     // blocks that can be merged, with a single successor and single predecessor
234     // respectively, it is beneficial to have all insert updates first. Deleting
235     // edges first may lead to unreachable blocks, followed by inserting edges
236     // making the blocks reachable again. Such DT updates lead to high compile
237     // times. We add inserts before deletes here to reduce compile time.
238     for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
239       // This successor of BB may already have PredBB as a predecessor.
240       if (!llvm::is_contained(successors(PredBB), *I))
241         Updates.push_back({DominatorTree::Insert, PredBB, *I});
242     for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
243       Updates.push_back({DominatorTree::Delete, BB, *I});
244     Updates.push_back({DominatorTree::Delete, PredBB, BB});
245   }
246 
247   Instruction *PTI = PredBB->getTerminator();
248   Instruction *STI = BB->getTerminator();
249   Instruction *Start = &*BB->begin();
250   // If there's nothing to move, mark the starting instruction as the last
251   // instruction in the block. Terminator instruction is handled separately.
252   if (Start == STI)
253     Start = PTI;
254 
255   // Move all definitions in the successor to the predecessor...
256   PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(),
257                                BB->begin(), STI->getIterator());
258 
259   if (MSSAU)
260     MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start);
261 
262   // Make all PHI nodes that referred to BB now refer to Pred as their
263   // source...
264   BB->replaceAllUsesWith(PredBB);
265 
266   if (PredecessorWithTwoSuccessors) {
267     // Delete the unconditional branch from BB.
268     BB->getInstList().pop_back();
269 
270     // Update branch in the predecessor.
271     PredBB_BI->setSuccessor(FallThruPath, NewSucc);
272   } else {
273     // Delete the unconditional branch from the predecessor.
274     PredBB->getInstList().pop_back();
275 
276     // Move terminator instruction.
277     PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
278 
279     // Terminator may be a memory accessing instruction too.
280     if (MSSAU)
281       if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>(
282               MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator())))
283         MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End);
284   }
285   // Add unreachable to now empty BB.
286   new UnreachableInst(BB->getContext(), BB);
287 
288   // Inherit predecessors name if it exists.
289   if (!PredBB->hasName())
290     PredBB->takeName(BB);
291 
292   if (LI)
293     LI->removeBlock(BB);
294 
295   if (MemDep)
296     MemDep->invalidateCachedPredecessors();
297 
298   // Finally, erase the old block and update dominator info.
299   if (DTU) {
300     assert(BB->getInstList().size() == 1 &&
301            isa<UnreachableInst>(BB->getTerminator()) &&
302            "The successor list of BB isn't empty before "
303            "applying corresponding DTU updates.");
304     DTU->applyUpdatesPermissive(Updates);
305     DTU->deleteBB(BB);
306   } else {
307     BB->eraseFromParent(); // Nuke BB if DTU is nullptr.
308   }
309 
310   return true;
311 }
312 
MergeBlockSuccessorsIntoGivenBlocks(SmallPtrSetImpl<BasicBlock * > & MergeBlocks,Loop * L,DomTreeUpdater * DTU,LoopInfo * LI)313 bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
314     SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU,
315     LoopInfo *LI) {
316   assert(!MergeBlocks.empty() && "MergeBlocks should not be empty");
317 
318   bool BlocksHaveBeenMerged = false;
319   while (!MergeBlocks.empty()) {
320     BasicBlock *BB = *MergeBlocks.begin();
321     BasicBlock *Dest = BB->getSingleSuccessor();
322     if (Dest && (!L || L->contains(Dest))) {
323       BasicBlock *Fold = Dest->getUniquePredecessor();
324       (void)Fold;
325       if (MergeBlockIntoPredecessor(Dest, DTU, LI)) {
326         assert(Fold == BB &&
327                "Expecting BB to be unique predecessor of the Dest block");
328         MergeBlocks.erase(Dest);
329         BlocksHaveBeenMerged = true;
330       } else
331         MergeBlocks.erase(BB);
332     } else
333       MergeBlocks.erase(BB);
334   }
335   return BlocksHaveBeenMerged;
336 }
337 
338 /// Remove redundant instructions within sequences of consecutive dbg.value
339 /// instructions. This is done using a backward scan to keep the last dbg.value
340 /// describing a specific variable/fragment.
341 ///
342 /// BackwardScan strategy:
343 /// ----------------------
344 /// Given a sequence of consecutive DbgValueInst like this
345 ///
346 ///   dbg.value ..., "x", FragmentX1  (*)
347 ///   dbg.value ..., "y", FragmentY1
348 ///   dbg.value ..., "x", FragmentX2
349 ///   dbg.value ..., "x", FragmentX1  (**)
350 ///
351 /// then the instruction marked with (*) can be removed (it is guaranteed to be
352 /// obsoleted by the instruction marked with (**) as the latter instruction is
353 /// describing the same variable using the same fragment info).
354 ///
355 /// Possible improvements:
356 /// - Check fully overlapping fragments and not only identical fragments.
357 /// - Support dbg.addr, dbg.declare. dbg.label, and possibly other meta
358 ///   instructions being part of the sequence of consecutive instructions.
removeRedundantDbgInstrsUsingBackwardScan(BasicBlock * BB)359 static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
360   SmallVector<DbgValueInst *, 8> ToBeRemoved;
361   SmallDenseSet<DebugVariable> VariableSet;
362   for (auto &I : reverse(*BB)) {
363     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
364       DebugVariable Key(DVI->getVariable(),
365                         DVI->getExpression(),
366                         DVI->getDebugLoc()->getInlinedAt());
367       auto R = VariableSet.insert(Key);
368       // If the same variable fragment is described more than once it is enough
369       // to keep the last one (i.e. the first found since we for reverse
370       // iteration).
371       if (!R.second)
372         ToBeRemoved.push_back(DVI);
373       continue;
374     }
375     // Sequence with consecutive dbg.value instrs ended. Clear the map to
376     // restart identifying redundant instructions if case we find another
377     // dbg.value sequence.
378     VariableSet.clear();
379   }
380 
381   for (auto &Instr : ToBeRemoved)
382     Instr->eraseFromParent();
383 
384   return !ToBeRemoved.empty();
385 }
386 
387 /// Remove redundant dbg.value instructions using a forward scan. This can
388 /// remove a dbg.value instruction that is redundant due to indicating that a
389 /// variable has the same value as already being indicated by an earlier
390 /// dbg.value.
391 ///
392 /// ForwardScan strategy:
393 /// ---------------------
394 /// Given two identical dbg.value instructions, separated by a block of
395 /// instructions that isn't describing the same variable, like this
396 ///
397 ///   dbg.value X1, "x", FragmentX1  (**)
398 ///   <block of instructions, none being "dbg.value ..., "x", ...">
399 ///   dbg.value X1, "x", FragmentX1  (*)
400 ///
401 /// then the instruction marked with (*) can be removed. Variable "x" is already
402 /// described as being mapped to the SSA value X1.
403 ///
404 /// Possible improvements:
405 /// - Keep track of non-overlapping fragments.
removeRedundantDbgInstrsUsingForwardScan(BasicBlock * BB)406 static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
407   SmallVector<DbgValueInst *, 8> ToBeRemoved;
408   DenseMap<DebugVariable, std::pair<Value *, DIExpression *> > VariableMap;
409   for (auto &I : *BB) {
410     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
411       DebugVariable Key(DVI->getVariable(),
412                         NoneType(),
413                         DVI->getDebugLoc()->getInlinedAt());
414       auto VMI = VariableMap.find(Key);
415       // Update the map if we found a new value/expression describing the
416       // variable, or if the variable wasn't mapped already.
417       if (VMI == VariableMap.end() ||
418           VMI->second.first != DVI->getValue() ||
419           VMI->second.second != DVI->getExpression()) {
420         VariableMap[Key] = { DVI->getValue(), DVI->getExpression() };
421         continue;
422       }
423       // Found an identical mapping. Remember the instruction for later removal.
424       ToBeRemoved.push_back(DVI);
425     }
426   }
427 
428   for (auto &Instr : ToBeRemoved)
429     Instr->eraseFromParent();
430 
431   return !ToBeRemoved.empty();
432 }
433 
RemoveRedundantDbgInstrs(BasicBlock * BB)434 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) {
435   bool MadeChanges = false;
436   // By using the "backward scan" strategy before the "forward scan" strategy we
437   // can remove both dbg.value (2) and (3) in a situation like this:
438   //
439   //   (1) dbg.value V1, "x", DIExpression()
440   //       ...
441   //   (2) dbg.value V2, "x", DIExpression()
442   //   (3) dbg.value V1, "x", DIExpression()
443   //
444   // The backward scan will remove (2), it is made obsolete by (3). After
445   // getting (2) out of the way, the foward scan will remove (3) since "x"
446   // already is described as having the value V1 at (1).
447   MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB);
448   MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB);
449 
450   if (MadeChanges)
451     LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: "
452                       << BB->getName() << "\n");
453   return MadeChanges;
454 }
455 
ReplaceInstWithValue(BasicBlock::InstListType & BIL,BasicBlock::iterator & BI,Value * V)456 void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
457                                 BasicBlock::iterator &BI, Value *V) {
458   Instruction &I = *BI;
459   // Replaces all of the uses of the instruction with uses of the value
460   I.replaceAllUsesWith(V);
461 
462   // Make sure to propagate a name if there is one already.
463   if (I.hasName() && !V->hasName())
464     V->takeName(&I);
465 
466   // Delete the unnecessary instruction now...
467   BI = BIL.erase(BI);
468 }
469 
ReplaceInstWithInst(BasicBlock::InstListType & BIL,BasicBlock::iterator & BI,Instruction * I)470 void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
471                                BasicBlock::iterator &BI, Instruction *I) {
472   assert(I->getParent() == nullptr &&
473          "ReplaceInstWithInst: Instruction already inserted into basic block!");
474 
475   // Copy debug location to newly added instruction, if it wasn't already set
476   // by the caller.
477   if (!I->getDebugLoc())
478     I->setDebugLoc(BI->getDebugLoc());
479 
480   // Insert the new instruction into the basic block...
481   BasicBlock::iterator New = BIL.insert(BI, I);
482 
483   // Replace all uses of the old instruction, and delete it.
484   ReplaceInstWithValue(BIL, BI, I);
485 
486   // Move BI back to point to the newly inserted instruction
487   BI = New;
488 }
489 
ReplaceInstWithInst(Instruction * From,Instruction * To)490 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
491   BasicBlock::iterator BI(From);
492   ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
493 }
494 
SplitEdge(BasicBlock * BB,BasicBlock * Succ,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU)495 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
496                             LoopInfo *LI, MemorySSAUpdater *MSSAU) {
497   unsigned SuccNum = GetSuccessorNumber(BB, Succ);
498 
499   // If this is a critical edge, let SplitCriticalEdge do it.
500   Instruction *LatchTerm = BB->getTerminator();
501   if (SplitCriticalEdge(
502           LatchTerm, SuccNum,
503           CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA()))
504     return LatchTerm->getSuccessor(SuccNum);
505 
506   // If the edge isn't critical, then BB has a single successor or Succ has a
507   // single pred.  Split the block.
508   if (BasicBlock *SP = Succ->getSinglePredecessor()) {
509     // If the successor only has a single pred, split the top of the successor
510     // block.
511     assert(SP == BB && "CFG broken");
512     SP = nullptr;
513     return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU);
514   }
515 
516   // Otherwise, if BB has a single successor, split it at the bottom of the
517   // block.
518   assert(BB->getTerminator()->getNumSuccessors() == 1 &&
519          "Should have a single succ!");
520   return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU);
521 }
522 
523 unsigned
SplitAllCriticalEdges(Function & F,const CriticalEdgeSplittingOptions & Options)524 llvm::SplitAllCriticalEdges(Function &F,
525                             const CriticalEdgeSplittingOptions &Options) {
526   unsigned NumBroken = 0;
527   for (BasicBlock &BB : F) {
528     Instruction *TI = BB.getTerminator();
529     if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI) &&
530         !isa<CallBrInst>(TI))
531       for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
532         if (SplitCriticalEdge(TI, i, Options))
533           ++NumBroken;
534   }
535   return NumBroken;
536 }
537 
SplitBlock(BasicBlock * Old,Instruction * SplitPt,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU,const Twine & BBName)538 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
539                              DominatorTree *DT, LoopInfo *LI,
540                              MemorySSAUpdater *MSSAU, const Twine &BBName) {
541   BasicBlock::iterator SplitIt = SplitPt->getIterator();
542   while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
543     ++SplitIt;
544   std::string Name = BBName.str();
545   BasicBlock *New = Old->splitBasicBlock(
546       SplitIt, Name.empty() ? Old->getName() + ".split" : Name);
547 
548   // The new block lives in whichever loop the old one did. This preserves
549   // LCSSA as well, because we force the split point to be after any PHI nodes.
550   if (LI)
551     if (Loop *L = LI->getLoopFor(Old))
552       L->addBasicBlockToLoop(New, *LI);
553 
554   if (DT)
555     // Old dominates New. New node dominates all other nodes dominated by Old.
556     if (DomTreeNode *OldNode = DT->getNode(Old)) {
557       std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
558 
559       DomTreeNode *NewNode = DT->addNewBlock(New, Old);
560       for (DomTreeNode *I : Children)
561         DT->changeImmediateDominator(I, NewNode);
562     }
563 
564   // Move MemoryAccesses still tracked in Old, but part of New now.
565   // Update accesses in successor blocks accordingly.
566   if (MSSAU)
567     MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));
568 
569   return New;
570 }
571 
572 /// Update DominatorTree, LoopInfo, and LCCSA analysis information.
UpdateAnalysisInformation(BasicBlock * OldBB,BasicBlock * NewBB,ArrayRef<BasicBlock * > Preds,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU,bool PreserveLCSSA,bool & HasLoopExit)573 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
574                                       ArrayRef<BasicBlock *> Preds,
575                                       DominatorTree *DT, LoopInfo *LI,
576                                       MemorySSAUpdater *MSSAU,
577                                       bool PreserveLCSSA, bool &HasLoopExit) {
578   // Update dominator tree if available.
579   if (DT) {
580     if (OldBB == DT->getRootNode()->getBlock()) {
581       assert(NewBB == &NewBB->getParent()->getEntryBlock());
582       DT->setNewRoot(NewBB);
583     } else {
584       // Split block expects NewBB to have a non-empty set of predecessors.
585       DT->splitBlock(NewBB);
586     }
587   }
588 
589   // Update MemoryPhis after split if MemorySSA is available
590   if (MSSAU)
591     MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);
592 
593   // The rest of the logic is only relevant for updating the loop structures.
594   if (!LI)
595     return;
596 
597   assert(DT && "DT should be available to update LoopInfo!");
598   Loop *L = LI->getLoopFor(OldBB);
599 
600   // If we need to preserve loop analyses, collect some information about how
601   // this split will affect loops.
602   bool IsLoopEntry = !!L;
603   bool SplitMakesNewLoopHeader = false;
604   for (BasicBlock *Pred : Preds) {
605     // Preds that are not reachable from entry should not be used to identify if
606     // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks
607     // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader
608     // as true and make the NewBB the header of some loop. This breaks LI.
609     if (!DT->isReachableFromEntry(Pred))
610       continue;
611     // If we need to preserve LCSSA, determine if any of the preds is a loop
612     // exit.
613     if (PreserveLCSSA)
614       if (Loop *PL = LI->getLoopFor(Pred))
615         if (!PL->contains(OldBB))
616           HasLoopExit = true;
617 
618     // If we need to preserve LoopInfo, note whether any of the preds crosses
619     // an interesting loop boundary.
620     if (!L)
621       continue;
622     if (L->contains(Pred))
623       IsLoopEntry = false;
624     else
625       SplitMakesNewLoopHeader = true;
626   }
627 
628   // Unless we have a loop for OldBB, nothing else to do here.
629   if (!L)
630     return;
631 
632   if (IsLoopEntry) {
633     // Add the new block to the nearest enclosing loop (and not an adjacent
634     // loop). To find this, examine each of the predecessors and determine which
635     // loops enclose them, and select the most-nested loop which contains the
636     // loop containing the block being split.
637     Loop *InnermostPredLoop = nullptr;
638     for (BasicBlock *Pred : Preds) {
639       if (Loop *PredLoop = LI->getLoopFor(Pred)) {
640         // Seek a loop which actually contains the block being split (to avoid
641         // adjacent loops).
642         while (PredLoop && !PredLoop->contains(OldBB))
643           PredLoop = PredLoop->getParentLoop();
644 
645         // Select the most-nested of these loops which contains the block.
646         if (PredLoop && PredLoop->contains(OldBB) &&
647             (!InnermostPredLoop ||
648              InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
649           InnermostPredLoop = PredLoop;
650       }
651     }
652 
653     if (InnermostPredLoop)
654       InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
655   } else {
656     L->addBasicBlockToLoop(NewBB, *LI);
657     if (SplitMakesNewLoopHeader)
658       L->moveToHeader(NewBB);
659   }
660 }
661 
662 /// Update the PHI nodes in OrigBB to include the values coming from NewBB.
663 /// This also updates AliasAnalysis, if available.
UpdatePHINodes(BasicBlock * OrigBB,BasicBlock * NewBB,ArrayRef<BasicBlock * > Preds,BranchInst * BI,bool HasLoopExit)664 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
665                            ArrayRef<BasicBlock *> Preds, BranchInst *BI,
666                            bool HasLoopExit) {
667   // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
668   SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
669   for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
670     PHINode *PN = cast<PHINode>(I++);
671 
672     // Check to see if all of the values coming in are the same.  If so, we
673     // don't need to create a new PHI node, unless it's needed for LCSSA.
674     Value *InVal = nullptr;
675     if (!HasLoopExit) {
676       InVal = PN->getIncomingValueForBlock(Preds[0]);
677       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
678         if (!PredSet.count(PN->getIncomingBlock(i)))
679           continue;
680         if (!InVal)
681           InVal = PN->getIncomingValue(i);
682         else if (InVal != PN->getIncomingValue(i)) {
683           InVal = nullptr;
684           break;
685         }
686       }
687     }
688 
689     if (InVal) {
690       // If all incoming values for the new PHI would be the same, just don't
691       // make a new PHI.  Instead, just remove the incoming values from the old
692       // PHI.
693 
694       // NOTE! This loop walks backwards for a reason! First off, this minimizes
695       // the cost of removal if we end up removing a large number of values, and
696       // second off, this ensures that the indices for the incoming values
697       // aren't invalidated when we remove one.
698       for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
699         if (PredSet.count(PN->getIncomingBlock(i)))
700           PN->removeIncomingValue(i, false);
701 
702       // Add an incoming value to the PHI node in the loop for the preheader
703       // edge.
704       PN->addIncoming(InVal, NewBB);
705       continue;
706     }
707 
708     // If the values coming into the block are not the same, we need a new
709     // PHI.
710     // Create the new PHI node, insert it into NewBB at the end of the block
711     PHINode *NewPHI =
712         PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
713 
714     // NOTE! This loop walks backwards for a reason! First off, this minimizes
715     // the cost of removal if we end up removing a large number of values, and
716     // second off, this ensures that the indices for the incoming values aren't
717     // invalidated when we remove one.
718     for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
719       BasicBlock *IncomingBB = PN->getIncomingBlock(i);
720       if (PredSet.count(IncomingBB)) {
721         Value *V = PN->removeIncomingValue(i, false);
722         NewPHI->addIncoming(V, IncomingBB);
723       }
724     }
725 
726     PN->addIncoming(NewPHI, NewBB);
727   }
728 }
729 
SplitBlockPredecessors(BasicBlock * BB,ArrayRef<BasicBlock * > Preds,const char * Suffix,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU,bool PreserveLCSSA)730 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
731                                          ArrayRef<BasicBlock *> Preds,
732                                          const char *Suffix, DominatorTree *DT,
733                                          LoopInfo *LI, MemorySSAUpdater *MSSAU,
734                                          bool PreserveLCSSA) {
735   // Do not attempt to split that which cannot be split.
736   if (!BB->canSplitPredecessors())
737     return nullptr;
738 
739   // For the landingpads we need to act a bit differently.
740   // Delegate this work to the SplitLandingPadPredecessors.
741   if (BB->isLandingPad()) {
742     SmallVector<BasicBlock*, 2> NewBBs;
743     std::string NewName = std::string(Suffix) + ".split-lp";
744 
745     SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs, DT,
746                                 LI, MSSAU, PreserveLCSSA);
747     return NewBBs[0];
748   }
749 
750   // Create new basic block, insert right before the original block.
751   BasicBlock *NewBB = BasicBlock::Create(
752       BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
753 
754   // The new block unconditionally branches to the old block.
755   BranchInst *BI = BranchInst::Create(BB, NewBB);
756 
757   Loop *L = nullptr;
758   BasicBlock *OldLatch = nullptr;
759   // Splitting the predecessors of a loop header creates a preheader block.
760   if (LI && LI->isLoopHeader(BB)) {
761     L = LI->getLoopFor(BB);
762     // Using the loop start line number prevents debuggers stepping into the
763     // loop body for this instruction.
764     BI->setDebugLoc(L->getStartLoc());
765 
766     // If BB is the header of the Loop, it is possible that the loop is
767     // modified, such that the current latch does not remain the latch of the
768     // loop. If that is the case, the loop metadata from the current latch needs
769     // to be applied to the new latch.
770     OldLatch = L->getLoopLatch();
771   } else
772     BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
773 
774   // Move the edges from Preds to point to NewBB instead of BB.
775   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
776     // This is slightly more strict than necessary; the minimum requirement
777     // is that there be no more than one indirectbr branching to BB. And
778     // all BlockAddress uses would need to be updated.
779     assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
780            "Cannot split an edge from an IndirectBrInst");
781     assert(!isa<CallBrInst>(Preds[i]->getTerminator()) &&
782            "Cannot split an edge from a CallBrInst");
783     Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
784   }
785 
786   // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
787   // node becomes an incoming value for BB's phi node.  However, if the Preds
788   // list is empty, we need to insert dummy entries into the PHI nodes in BB to
789   // account for the newly created predecessor.
790   if (Preds.empty()) {
791     // Insert dummy values as the incoming value.
792     for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
793       cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
794   }
795 
796   // Update DominatorTree, LoopInfo, and LCCSA analysis information.
797   bool HasLoopExit = false;
798   UpdateAnalysisInformation(BB, NewBB, Preds, DT, LI, MSSAU, PreserveLCSSA,
799                             HasLoopExit);
800 
801   if (!Preds.empty()) {
802     // Update the PHI nodes in BB with the values coming from NewBB.
803     UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
804   }
805 
806   if (OldLatch) {
807     BasicBlock *NewLatch = L->getLoopLatch();
808     if (NewLatch != OldLatch) {
809       MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop");
810       NewLatch->getTerminator()->setMetadata("llvm.loop", MD);
811       OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr);
812     }
813   }
814 
815   return NewBB;
816 }
817 
SplitLandingPadPredecessors(BasicBlock * OrigBB,ArrayRef<BasicBlock * > Preds,const char * Suffix1,const char * Suffix2,SmallVectorImpl<BasicBlock * > & NewBBs,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU,bool PreserveLCSSA)818 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
819                                        ArrayRef<BasicBlock *> Preds,
820                                        const char *Suffix1, const char *Suffix2,
821                                        SmallVectorImpl<BasicBlock *> &NewBBs,
822                                        DominatorTree *DT, LoopInfo *LI,
823                                        MemorySSAUpdater *MSSAU,
824                                        bool PreserveLCSSA) {
825   assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
826 
827   // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
828   // it right before the original block.
829   BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
830                                           OrigBB->getName() + Suffix1,
831                                           OrigBB->getParent(), OrigBB);
832   NewBBs.push_back(NewBB1);
833 
834   // The new block unconditionally branches to the old block.
835   BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
836   BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
837 
838   // Move the edges from Preds to point to NewBB1 instead of OrigBB.
839   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
840     // This is slightly more strict than necessary; the minimum requirement
841     // is that there be no more than one indirectbr branching to BB. And
842     // all BlockAddress uses would need to be updated.
843     assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
844            "Cannot split an edge from an IndirectBrInst");
845     Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
846   }
847 
848   bool HasLoopExit = false;
849   UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DT, LI, MSSAU, PreserveLCSSA,
850                             HasLoopExit);
851 
852   // Update the PHI nodes in OrigBB with the values coming from NewBB1.
853   UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
854 
855   // Move the remaining edges from OrigBB to point to NewBB2.
856   SmallVector<BasicBlock*, 8> NewBB2Preds;
857   for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
858        i != e; ) {
859     BasicBlock *Pred = *i++;
860     if (Pred == NewBB1) continue;
861     assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
862            "Cannot split an edge from an IndirectBrInst");
863     NewBB2Preds.push_back(Pred);
864     e = pred_end(OrigBB);
865   }
866 
867   BasicBlock *NewBB2 = nullptr;
868   if (!NewBB2Preds.empty()) {
869     // Create another basic block for the rest of OrigBB's predecessors.
870     NewBB2 = BasicBlock::Create(OrigBB->getContext(),
871                                 OrigBB->getName() + Suffix2,
872                                 OrigBB->getParent(), OrigBB);
873     NewBBs.push_back(NewBB2);
874 
875     // The new block unconditionally branches to the old block.
876     BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
877     BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
878 
879     // Move the remaining edges from OrigBB to point to NewBB2.
880     for (BasicBlock *NewBB2Pred : NewBB2Preds)
881       NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
882 
883     // Update DominatorTree, LoopInfo, and LCCSA analysis information.
884     HasLoopExit = false;
885     UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DT, LI, MSSAU,
886                               PreserveLCSSA, HasLoopExit);
887 
888     // Update the PHI nodes in OrigBB with the values coming from NewBB2.
889     UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
890   }
891 
892   LandingPadInst *LPad = OrigBB->getLandingPadInst();
893   Instruction *Clone1 = LPad->clone();
894   Clone1->setName(Twine("lpad") + Suffix1);
895   NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
896 
897   if (NewBB2) {
898     Instruction *Clone2 = LPad->clone();
899     Clone2->setName(Twine("lpad") + Suffix2);
900     NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
901 
902     // Create a PHI node for the two cloned landingpad instructions only
903     // if the original landingpad instruction has some uses.
904     if (!LPad->use_empty()) {
905       assert(!LPad->getType()->isTokenTy() &&
906              "Split cannot be applied if LPad is token type. Otherwise an "
907              "invalid PHINode of token type would be created.");
908       PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
909       PN->addIncoming(Clone1, NewBB1);
910       PN->addIncoming(Clone2, NewBB2);
911       LPad->replaceAllUsesWith(PN);
912     }
913     LPad->eraseFromParent();
914   } else {
915     // There is no second clone. Just replace the landing pad with the first
916     // clone.
917     LPad->replaceAllUsesWith(Clone1);
918     LPad->eraseFromParent();
919   }
920 }
921 
FoldReturnIntoUncondBranch(ReturnInst * RI,BasicBlock * BB,BasicBlock * Pred,DomTreeUpdater * DTU)922 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
923                                              BasicBlock *Pred,
924                                              DomTreeUpdater *DTU) {
925   Instruction *UncondBranch = Pred->getTerminator();
926   // Clone the return and add it to the end of the predecessor.
927   Instruction *NewRet = RI->clone();
928   Pred->getInstList().push_back(NewRet);
929 
930   // If the return instruction returns a value, and if the value was a
931   // PHI node in "BB", propagate the right value into the return.
932   for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
933        i != e; ++i) {
934     Value *V = *i;
935     Instruction *NewBC = nullptr;
936     if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
937       // Return value might be bitcasted. Clone and insert it before the
938       // return instruction.
939       V = BCI->getOperand(0);
940       NewBC = BCI->clone();
941       Pred->getInstList().insert(NewRet->getIterator(), NewBC);
942       *i = NewBC;
943     }
944 
945     Instruction *NewEV = nullptr;
946     if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
947       V = EVI->getOperand(0);
948       NewEV = EVI->clone();
949       if (NewBC) {
950         NewBC->setOperand(0, NewEV);
951         Pred->getInstList().insert(NewBC->getIterator(), NewEV);
952       } else {
953         Pred->getInstList().insert(NewRet->getIterator(), NewEV);
954         *i = NewEV;
955       }
956     }
957 
958     if (PHINode *PN = dyn_cast<PHINode>(V)) {
959       if (PN->getParent() == BB) {
960         if (NewEV) {
961           NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred));
962         } else if (NewBC)
963           NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
964         else
965           *i = PN->getIncomingValueForBlock(Pred);
966       }
967     }
968   }
969 
970   // Update any PHI nodes in the returning block to realize that we no
971   // longer branch to them.
972   BB->removePredecessor(Pred);
973   UncondBranch->eraseFromParent();
974 
975   if (DTU)
976     DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}});
977 
978   return cast<ReturnInst>(NewRet);
979 }
980 
SplitBlockAndInsertIfThen(Value * Cond,Instruction * SplitBefore,bool Unreachable,MDNode * BranchWeights,DominatorTree * DT,LoopInfo * LI,BasicBlock * ThenBlock)981 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
982                                              Instruction *SplitBefore,
983                                              bool Unreachable,
984                                              MDNode *BranchWeights,
985                                              DominatorTree *DT, LoopInfo *LI,
986                                              BasicBlock *ThenBlock) {
987   BasicBlock *Head = SplitBefore->getParent();
988   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
989   Instruction *HeadOldTerm = Head->getTerminator();
990   LLVMContext &C = Head->getContext();
991   Instruction *CheckTerm;
992   bool CreateThenBlock = (ThenBlock == nullptr);
993   if (CreateThenBlock) {
994     ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
995     if (Unreachable)
996       CheckTerm = new UnreachableInst(C, ThenBlock);
997     else
998       CheckTerm = BranchInst::Create(Tail, ThenBlock);
999     CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
1000   } else
1001     CheckTerm = ThenBlock->getTerminator();
1002   BranchInst *HeadNewTerm =
1003     BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond);
1004   HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1005   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1006 
1007   if (DT) {
1008     if (DomTreeNode *OldNode = DT->getNode(Head)) {
1009       std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
1010 
1011       DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
1012       for (DomTreeNode *Child : Children)
1013         DT->changeImmediateDominator(Child, NewNode);
1014 
1015       // Head dominates ThenBlock.
1016       if (CreateThenBlock)
1017         DT->addNewBlock(ThenBlock, Head);
1018       else
1019         DT->changeImmediateDominator(ThenBlock, Head);
1020     }
1021   }
1022 
1023   if (LI) {
1024     if (Loop *L = LI->getLoopFor(Head)) {
1025       L->addBasicBlockToLoop(ThenBlock, *LI);
1026       L->addBasicBlockToLoop(Tail, *LI);
1027     }
1028   }
1029 
1030   return CheckTerm;
1031 }
1032 
SplitBlockAndInsertIfThenElse(Value * Cond,Instruction * SplitBefore,Instruction ** ThenTerm,Instruction ** ElseTerm,MDNode * BranchWeights)1033 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
1034                                          Instruction **ThenTerm,
1035                                          Instruction **ElseTerm,
1036                                          MDNode *BranchWeights) {
1037   BasicBlock *Head = SplitBefore->getParent();
1038   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
1039   Instruction *HeadOldTerm = Head->getTerminator();
1040   LLVMContext &C = Head->getContext();
1041   BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1042   BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1043   *ThenTerm = BranchInst::Create(Tail, ThenBlock);
1044   (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
1045   *ElseTerm = BranchInst::Create(Tail, ElseBlock);
1046   (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
1047   BranchInst *HeadNewTerm =
1048     BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
1049   HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1050   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1051 }
1052 
GetIfCondition(BasicBlock * BB,BasicBlock * & IfTrue,BasicBlock * & IfFalse)1053 Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
1054                              BasicBlock *&IfFalse) {
1055   PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
1056   BasicBlock *Pred1 = nullptr;
1057   BasicBlock *Pred2 = nullptr;
1058 
1059   if (SomePHI) {
1060     if (SomePHI->getNumIncomingValues() != 2)
1061       return nullptr;
1062     Pred1 = SomePHI->getIncomingBlock(0);
1063     Pred2 = SomePHI->getIncomingBlock(1);
1064   } else {
1065     pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1066     if (PI == PE) // No predecessor
1067       return nullptr;
1068     Pred1 = *PI++;
1069     if (PI == PE) // Only one predecessor
1070       return nullptr;
1071     Pred2 = *PI++;
1072     if (PI != PE) // More than two predecessors
1073       return nullptr;
1074   }
1075 
1076   // We can only handle branches.  Other control flow will be lowered to
1077   // branches if possible anyway.
1078   BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
1079   BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
1080   if (!Pred1Br || !Pred2Br)
1081     return nullptr;
1082 
1083   // Eliminate code duplication by ensuring that Pred1Br is conditional if
1084   // either are.
1085   if (Pred2Br->isConditional()) {
1086     // If both branches are conditional, we don't have an "if statement".  In
1087     // reality, we could transform this case, but since the condition will be
1088     // required anyway, we stand no chance of eliminating it, so the xform is
1089     // probably not profitable.
1090     if (Pred1Br->isConditional())
1091       return nullptr;
1092 
1093     std::swap(Pred1, Pred2);
1094     std::swap(Pred1Br, Pred2Br);
1095   }
1096 
1097   if (Pred1Br->isConditional()) {
1098     // The only thing we have to watch out for here is to make sure that Pred2
1099     // doesn't have incoming edges from other blocks.  If it does, the condition
1100     // doesn't dominate BB.
1101     if (!Pred2->getSinglePredecessor())
1102       return nullptr;
1103 
1104     // If we found a conditional branch predecessor, make sure that it branches
1105     // to BB and Pred2Br.  If it doesn't, this isn't an "if statement".
1106     if (Pred1Br->getSuccessor(0) == BB &&
1107         Pred1Br->getSuccessor(1) == Pred2) {
1108       IfTrue = Pred1;
1109       IfFalse = Pred2;
1110     } else if (Pred1Br->getSuccessor(0) == Pred2 &&
1111                Pred1Br->getSuccessor(1) == BB) {
1112       IfTrue = Pred2;
1113       IfFalse = Pred1;
1114     } else {
1115       // We know that one arm of the conditional goes to BB, so the other must
1116       // go somewhere unrelated, and this must not be an "if statement".
1117       return nullptr;
1118     }
1119 
1120     return Pred1Br->getCondition();
1121   }
1122 
1123   // Ok, if we got here, both predecessors end with an unconditional branch to
1124   // BB.  Don't panic!  If both blocks only have a single (identical)
1125   // predecessor, and THAT is a conditional branch, then we're all ok!
1126   BasicBlock *CommonPred = Pred1->getSinglePredecessor();
1127   if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
1128     return nullptr;
1129 
1130   // Otherwise, if this is a conditional branch, then we can use it!
1131   BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
1132   if (!BI) return nullptr;
1133 
1134   assert(BI->isConditional() && "Two successors but not conditional?");
1135   if (BI->getSuccessor(0) == Pred1) {
1136     IfTrue = Pred1;
1137     IfFalse = Pred2;
1138   } else {
1139     IfTrue = Pred2;
1140     IfFalse = Pred1;
1141   }
1142   return BI->getCondition();
1143 }
1144 
1145 // After creating a control flow hub, the operands of PHINodes in an outgoing
1146 // block Out no longer match the predecessors of that block. Predecessors of Out
1147 // that are incoming blocks to the hub are now replaced by just one edge from
1148 // the hub. To match this new control flow, the corresponding values from each
1149 // PHINode must now be moved a new PHINode in the first guard block of the hub.
1150 //
1151 // This operation cannot be performed with SSAUpdater, because it involves one
1152 // new use: If the block Out is in the list of Incoming blocks, then the newly
1153 // created PHI in the Hub will use itself along that edge from Out to Hub.
reconnectPhis(BasicBlock * Out,BasicBlock * GuardBlock,const SetVector<BasicBlock * > & Incoming,BasicBlock * FirstGuardBlock)1154 static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
1155                           const SetVector<BasicBlock *> &Incoming,
1156                           BasicBlock *FirstGuardBlock) {
1157   auto I = Out->begin();
1158   while (I != Out->end() && isa<PHINode>(I)) {
1159     auto Phi = cast<PHINode>(I);
1160     auto NewPhi =
1161         PHINode::Create(Phi->getType(), Incoming.size(),
1162                         Phi->getName() + ".moved", &FirstGuardBlock->back());
1163     for (auto In : Incoming) {
1164       Value *V = UndefValue::get(Phi->getType());
1165       if (In == Out) {
1166         V = NewPhi;
1167       } else if (Phi->getBasicBlockIndex(In) != -1) {
1168         V = Phi->removeIncomingValue(In, false);
1169       }
1170       NewPhi->addIncoming(V, In);
1171     }
1172     assert(NewPhi->getNumIncomingValues() == Incoming.size());
1173     if (Phi->getNumOperands() == 0) {
1174       Phi->replaceAllUsesWith(NewPhi);
1175       I = Phi->eraseFromParent();
1176       continue;
1177     }
1178     Phi->addIncoming(NewPhi, GuardBlock);
1179     ++I;
1180   }
1181 }
1182 
1183 using BBPredicates = DenseMap<BasicBlock *, PHINode *>;
1184 using BBSetVector = SetVector<BasicBlock *>;
1185 
1186 // Redirects the terminator of the incoming block to the first guard
1187 // block in the hub. The condition of the original terminator (if it
1188 // was conditional) and its original successors are returned as a
1189 // tuple <condition, succ0, succ1>. The function additionally filters
1190 // out successors that are not in the set of outgoing blocks.
1191 //
1192 // - condition is non-null iff the branch is conditional.
1193 // - Succ1 is non-null iff the sole/taken target is an outgoing block.
1194 // - Succ2 is non-null iff condition is non-null and the fallthrough
1195 //         target is an outgoing block.
1196 static std::tuple<Value *, BasicBlock *, BasicBlock *>
redirectToHub(BasicBlock * BB,BasicBlock * FirstGuardBlock,const BBSetVector & Outgoing)1197 redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock,
1198               const BBSetVector &Outgoing) {
1199   auto Branch = cast<BranchInst>(BB->getTerminator());
1200   auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr;
1201 
1202   BasicBlock *Succ0 = Branch->getSuccessor(0);
1203   BasicBlock *Succ1 = nullptr;
1204   Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;
1205 
1206   if (Branch->isUnconditional()) {
1207     Branch->setSuccessor(0, FirstGuardBlock);
1208     assert(Succ0);
1209   } else {
1210     Succ1 = Branch->getSuccessor(1);
1211     Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;
1212     assert(Succ0 || Succ1);
1213     if (Succ0 && !Succ1) {
1214       Branch->setSuccessor(0, FirstGuardBlock);
1215     } else if (Succ1 && !Succ0) {
1216       Branch->setSuccessor(1, FirstGuardBlock);
1217     } else {
1218       Branch->eraseFromParent();
1219       BranchInst::Create(FirstGuardBlock, BB);
1220     }
1221   }
1222 
1223   assert(Succ0 || Succ1);
1224   return std::make_tuple(Condition, Succ0, Succ1);
1225 }
1226 
1227 // Capture the existing control flow as guard predicates, and redirect
1228 // control flow from every incoming block to the first guard block in
1229 // the hub.
1230 //
1231 // There is one guard predicate for each outgoing block OutBB. The
1232 // predicate is a PHINode with one input for each InBB which
1233 // represents whether the hub should transfer control flow to OutBB if
1234 // it arrived from InBB. These predicates are NOT ORTHOGONAL. The Hub
1235 // evaluates them in the same order as the Outgoing set-vector, and
1236 // control branches to the first outgoing block whose predicate
1237 // evaluates to true.
convertToGuardPredicates(BasicBlock * FirstGuardBlock,BBPredicates & GuardPredicates,SmallVectorImpl<WeakVH> & DeletionCandidates,const BBSetVector & Incoming,const BBSetVector & Outgoing)1238 static void convertToGuardPredicates(
1239     BasicBlock *FirstGuardBlock, BBPredicates &GuardPredicates,
1240     SmallVectorImpl<WeakVH> &DeletionCandidates, const BBSetVector &Incoming,
1241     const BBSetVector &Outgoing) {
1242   auto &Context = Incoming.front()->getContext();
1243   auto BoolTrue = ConstantInt::getTrue(Context);
1244   auto BoolFalse = ConstantInt::getFalse(Context);
1245 
1246   // The predicate for the last outgoing is trivially true, and so we
1247   // process only the first N-1 successors.
1248   for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1249     auto Out = Outgoing[i];
1250     LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n");
1251     auto Phi =
1252         PHINode::Create(Type::getInt1Ty(Context), Incoming.size(),
1253                         StringRef("Guard.") + Out->getName(), FirstGuardBlock);
1254     GuardPredicates[Out] = Phi;
1255   }
1256 
1257   for (auto In : Incoming) {
1258     Value *Condition;
1259     BasicBlock *Succ0;
1260     BasicBlock *Succ1;
1261     std::tie(Condition, Succ0, Succ1) =
1262         redirectToHub(In, FirstGuardBlock, Outgoing);
1263 
1264     // Optimization: Consider an incoming block A with both successors
1265     // Succ0 and Succ1 in the set of outgoing blocks. The predicates
1266     // for Succ0 and Succ1 complement each other. If Succ0 is visited
1267     // first in the loop below, control will branch to Succ0 using the
1268     // corresponding predicate. But if that branch is not taken, then
1269     // control must reach Succ1, which means that the predicate for
1270     // Succ1 is always true.
1271     bool OneSuccessorDone = false;
1272     for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1273       auto Out = Outgoing[i];
1274       auto Phi = GuardPredicates[Out];
1275       if (Out != Succ0 && Out != Succ1) {
1276         Phi->addIncoming(BoolFalse, In);
1277         continue;
1278       }
1279       // Optimization: When only one successor is an outgoing block,
1280       // the predicate is always true.
1281       if (!Succ0 || !Succ1 || OneSuccessorDone) {
1282         Phi->addIncoming(BoolTrue, In);
1283         continue;
1284       }
1285       assert(Succ0 && Succ1);
1286       OneSuccessorDone = true;
1287       if (Out == Succ0) {
1288         Phi->addIncoming(Condition, In);
1289         continue;
1290       }
1291       auto Inverted = invertCondition(Condition);
1292       DeletionCandidates.push_back(Condition);
1293       Phi->addIncoming(Inverted, In);
1294     }
1295   }
1296 }
1297 
1298 // For each outgoing block OutBB, create a guard block in the Hub. The
1299 // first guard block was already created outside, and available as the
1300 // first element in the vector of guard blocks.
1301 //
1302 // Each guard block terminates in a conditional branch that transfers
1303 // control to the corresponding outgoing block or the next guard
1304 // block. The last guard block has two outgoing blocks as successors
1305 // since the condition for the final outgoing block is trivially
1306 // true. So we create one less block (including the first guard block)
1307 // than the number of outgoing blocks.
createGuardBlocks(SmallVectorImpl<BasicBlock * > & GuardBlocks,Function * F,const BBSetVector & Outgoing,BBPredicates & GuardPredicates,StringRef Prefix)1308 static void createGuardBlocks(SmallVectorImpl<BasicBlock *> &GuardBlocks,
1309                               Function *F, const BBSetVector &Outgoing,
1310                               BBPredicates &GuardPredicates, StringRef Prefix) {
1311   for (int i = 0, e = Outgoing.size() - 2; i != e; ++i) {
1312     GuardBlocks.push_back(
1313         BasicBlock::Create(F->getContext(), Prefix + ".guard", F));
1314   }
1315   assert(GuardBlocks.size() == GuardPredicates.size());
1316 
1317   // To help keep the loop simple, temporarily append the last
1318   // outgoing block to the list of guard blocks.
1319   GuardBlocks.push_back(Outgoing.back());
1320 
1321   for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) {
1322     auto Out = Outgoing[i];
1323     assert(GuardPredicates.count(Out));
1324     BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out],
1325                        GuardBlocks[i]);
1326   }
1327 
1328   // Remove the last block from the guard list.
1329   GuardBlocks.pop_back();
1330 }
1331 
CreateControlFlowHub(DomTreeUpdater * DTU,SmallVectorImpl<BasicBlock * > & GuardBlocks,const BBSetVector & Incoming,const BBSetVector & Outgoing,const StringRef Prefix)1332 BasicBlock *llvm::CreateControlFlowHub(
1333     DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
1334     const BBSetVector &Incoming, const BBSetVector &Outgoing,
1335     const StringRef Prefix) {
1336   auto F = Incoming.front()->getParent();
1337   auto FirstGuardBlock =
1338       BasicBlock::Create(F->getContext(), Prefix + ".guard", F);
1339 
1340   SmallVector<DominatorTree::UpdateType, 16> Updates;
1341   if (DTU) {
1342     for (auto In : Incoming) {
1343       for (auto Succ : successors(In)) {
1344         if (Outgoing.count(Succ))
1345           Updates.push_back({DominatorTree::Delete, In, Succ});
1346       }
1347       Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock});
1348     }
1349   }
1350 
1351   BBPredicates GuardPredicates;
1352   SmallVector<WeakVH, 8> DeletionCandidates;
1353   convertToGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates,
1354                            Incoming, Outgoing);
1355 
1356   GuardBlocks.push_back(FirstGuardBlock);
1357   createGuardBlocks(GuardBlocks, F, Outgoing, GuardPredicates, Prefix);
1358 
1359   // Update the PHINodes in each outgoing block to match the new control flow.
1360   for (int i = 0, e = GuardBlocks.size(); i != e; ++i) {
1361     reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock);
1362   }
1363   reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock);
1364 
1365   if (DTU) {
1366     int NumGuards = GuardBlocks.size();
1367     assert((int)Outgoing.size() == NumGuards + 1);
1368     for (int i = 0; i != NumGuards - 1; ++i) {
1369       Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]});
1370       Updates.push_back(
1371           {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]});
1372     }
1373     Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
1374                        Outgoing[NumGuards - 1]});
1375     Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
1376                        Outgoing[NumGuards]});
1377     DTU->applyUpdates(Updates);
1378   }
1379 
1380   for (auto I : DeletionCandidates) {
1381     if (I->use_empty())
1382       if (auto Inst = dyn_cast_or_null<Instruction>(I))
1383         Inst->eraseFromParent();
1384   }
1385 
1386   return FirstGuardBlock;
1387 }
1388