Local.cpp revision 49db68fba01722ca032dc5170f8248a9d25f0199
1//===-- Local.cpp - Functions to perform local transformations ------------===//
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 various local transformations to the
11// program.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/Local.h"
16#include "llvm/Constants.h"
17#include "llvm/GlobalAlias.h"
18#include "llvm/GlobalVariable.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
21#include "llvm/Intrinsics.h"
22#include "llvm/IntrinsicInst.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/Analysis/ConstantFolding.h"
26#include "llvm/Analysis/InstructionSimplify.h"
27#include "llvm/Analysis/ProfileInfo.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Support/CFG.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/GetElementPtrTypeIterator.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/ValueHandle.h"
34#include "llvm/Support/raw_ostream.h"
35using namespace llvm;
36
37//===----------------------------------------------------------------------===//
38//  Local analysis.
39//
40
41/// getUnderlyingObjectWithOffset - Strip off up to MaxLookup GEPs and
42/// bitcasts to get back to the underlying object being addressed, keeping
43/// track of the offset in bytes from the GEPs relative to the result.
44/// This is closely related to Value::getUnderlyingObject but is located
45/// here to avoid making VMCore depend on TargetData.
46static Value *getUnderlyingObjectWithOffset(Value *V, const TargetData *TD,
47                                            uint64_t &ByteOffset,
48                                            unsigned MaxLookup = 6) {
49  if (!isa<PointerType>(V->getType()))
50    return V;
51  for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
52    if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
53      if (!GEP->hasAllConstantIndices())
54        return V;
55      SmallVector<Value*, 8> Indices(GEP->op_begin() + 1, GEP->op_end());
56      ByteOffset += TD->getIndexedOffset(GEP->getPointerOperandType(),
57                                         &Indices[0], Indices.size());
58      V = GEP->getPointerOperand();
59    } else if (Operator::getOpcode(V) == Instruction::BitCast) {
60      V = cast<Operator>(V)->getOperand(0);
61    } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
62      if (GA->mayBeOverridden())
63        return V;
64      V = GA->getAliasee();
65    } else {
66      return V;
67    }
68    assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
69  }
70  return V;
71}
72
73/// isSafeToLoadUnconditionally - Return true if we know that executing a load
74/// from this value cannot trap.  If it is not obviously safe to load from the
75/// specified pointer, we do a quick local scan of the basic block containing
76/// ScanFrom, to determine if the address is already accessed.
77bool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom,
78                                       unsigned Align, const TargetData *TD) {
79  uint64_t ByteOffset = 0;
80  Value *Base = V;
81  if (TD)
82    Base = getUnderlyingObjectWithOffset(V, TD, ByteOffset);
83
84  const Type *BaseType = 0;
85  unsigned BaseAlign = 0;
86  if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
87    // An alloca is safe to load from as load as it is suitably aligned.
88    BaseType = AI->getAllocatedType();
89    BaseAlign = AI->getAlignment();
90  } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(Base)) {
91    // Global variables are safe to load from but their size cannot be
92    // guaranteed if they are overridden.
93    if (!isa<GlobalAlias>(GV) && !GV->mayBeOverridden()) {
94      BaseType = GV->getType()->getElementType();
95      BaseAlign = GV->getAlignment();
96    }
97  }
98  if (TD && BaseType && BaseAlign == 0)
99    BaseAlign = TD->getPrefTypeAlignment(BaseType);
100
101  if (BaseType && Align <= BaseAlign) {
102    if (!TD)
103      return true; // Loading directly from an alloca or global is OK.
104    if (BaseType->isSized()) {
105      // Check if the load is within the bounds of the underlying object.
106      const PointerType *AddrTy = cast<PointerType>(V->getType());
107      uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType());
108      if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) &&
109          (Align == 0 || (ByteOffset % Align) == 0))
110        return true;
111    }
112  }
113
114  // Otherwise, be a little bit aggressive by scanning the local block where we
115  // want to check to see if the pointer is already being loaded or stored
116  // from/to.  If so, the previous load or store would have already trapped,
117  // so there is no harm doing an extra load (also, CSE will later eliminate
118  // the load entirely).
119  BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
120
121  while (BBI != E) {
122    --BBI;
123
124    // If we see a free or a call which may write to memory (i.e. which might do
125    // a free) the pointer could be marked invalid.
126    if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
127        !isa<DbgInfoIntrinsic>(BBI))
128      return false;
129
130    if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
131      if (LI->getOperand(0) == V) return true;
132    } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
133      if (SI->getOperand(1) == V) return true;
134    }
135  }
136  return false;
137}
138
139
140//===----------------------------------------------------------------------===//
141//  Local constant propagation.
142//
143
144// ConstantFoldTerminator - If a terminator instruction is predicated on a
145// constant value, convert it into an unconditional branch to the constant
146// destination.
147//
148bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
149  TerminatorInst *T = BB->getTerminator();
150
151  // Branch - See if we are conditional jumping on constant
152  if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
153    if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
154    BasicBlock *Dest1 = BI->getSuccessor(0);
155    BasicBlock *Dest2 = BI->getSuccessor(1);
156
157    if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
158      // Are we branching on constant?
159      // YES.  Change to unconditional branch...
160      BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
161      BasicBlock *OldDest     = Cond->getZExtValue() ? Dest2 : Dest1;
162
163      //cerr << "Function: " << T->getParent()->getParent()
164      //     << "\nRemoving branch from " << T->getParent()
165      //     << "\n\nTo: " << OldDest << endl;
166
167      // Let the basic block know that we are letting go of it.  Based on this,
168      // it will adjust it's PHI nodes.
169      assert(BI->getParent() && "Terminator not inserted in block!");
170      OldDest->removePredecessor(BI->getParent());
171
172      // Set the unconditional destination, and change the insn to be an
173      // unconditional branch.
174      BI->setUnconditionalDest(Destination);
175      return true;
176    }
177
178    if (Dest2 == Dest1) {       // Conditional branch to same location?
179      // This branch matches something like this:
180      //     br bool %cond, label %Dest, label %Dest
181      // and changes it into:  br label %Dest
182
183      // Let the basic block know that we are letting go of one copy of it.
184      assert(BI->getParent() && "Terminator not inserted in block!");
185      Dest1->removePredecessor(BI->getParent());
186
187      // Change a conditional branch to unconditional.
188      BI->setUnconditionalDest(Dest1);
189      return true;
190    }
191    return false;
192  }
193
194  if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
195    // If we are switching on a constant, we can convert the switch into a
196    // single branch instruction!
197    ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
198    BasicBlock *TheOnlyDest = SI->getSuccessor(0);  // The default dest
199    BasicBlock *DefaultDest = TheOnlyDest;
200    assert(TheOnlyDest == SI->getDefaultDest() &&
201           "Default destination is not successor #0?");
202
203    // Figure out which case it goes to.
204    for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
205      // Found case matching a constant operand?
206      if (SI->getSuccessorValue(i) == CI) {
207        TheOnlyDest = SI->getSuccessor(i);
208        break;
209      }
210
211      // Check to see if this branch is going to the same place as the default
212      // dest.  If so, eliminate it as an explicit compare.
213      if (SI->getSuccessor(i) == DefaultDest) {
214        // Remove this entry.
215        DefaultDest->removePredecessor(SI->getParent());
216        SI->removeCase(i);
217        --i; --e;  // Don't skip an entry...
218        continue;
219      }
220
221      // Otherwise, check to see if the switch only branches to one destination.
222      // We do this by reseting "TheOnlyDest" to null when we find two non-equal
223      // destinations.
224      if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
225    }
226
227    if (CI && !TheOnlyDest) {
228      // Branching on a constant, but not any of the cases, go to the default
229      // successor.
230      TheOnlyDest = SI->getDefaultDest();
231    }
232
233    // If we found a single destination that we can fold the switch into, do so
234    // now.
235    if (TheOnlyDest) {
236      // Insert the new branch.
237      BranchInst::Create(TheOnlyDest, SI);
238      BasicBlock *BB = SI->getParent();
239
240      // Remove entries from PHI nodes which we no longer branch to...
241      for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
242        // Found case matching a constant operand?
243        BasicBlock *Succ = SI->getSuccessor(i);
244        if (Succ == TheOnlyDest)
245          TheOnlyDest = 0;  // Don't modify the first branch to TheOnlyDest
246        else
247          Succ->removePredecessor(BB);
248      }
249
250      // Delete the old switch.
251      BB->getInstList().erase(SI);
252      return true;
253    }
254
255    if (SI->getNumSuccessors() == 2) {
256      // Otherwise, we can fold this switch into a conditional branch
257      // instruction if it has only one non-default destination.
258      Value *Cond = new ICmpInst(SI, ICmpInst::ICMP_EQ, SI->getCondition(),
259                                 SI->getSuccessorValue(1), "cond");
260      // Insert the new branch.
261      BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
262
263      // Delete the old switch.
264      SI->eraseFromParent();
265      return true;
266    }
267    return false;
268  }
269
270  if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) {
271    // indirectbr blockaddress(@F, @BB) -> br label @BB
272    if (BlockAddress *BA =
273          dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
274      BasicBlock *TheOnlyDest = BA->getBasicBlock();
275      // Insert the new branch.
276      BranchInst::Create(TheOnlyDest, IBI);
277
278      for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
279        if (IBI->getDestination(i) == TheOnlyDest)
280          TheOnlyDest = 0;
281        else
282          IBI->getDestination(i)->removePredecessor(IBI->getParent());
283      }
284      IBI->eraseFromParent();
285
286      // If we didn't find our destination in the IBI successor list, then we
287      // have undefined behavior.  Replace the unconditional branch with an
288      // 'unreachable' instruction.
289      if (TheOnlyDest) {
290        BB->getTerminator()->eraseFromParent();
291        new UnreachableInst(BB->getContext(), BB);
292      }
293
294      return true;
295    }
296  }
297
298  return false;
299}
300
301
302//===----------------------------------------------------------------------===//
303//  Local dead code elimination.
304//
305
306/// isInstructionTriviallyDead - Return true if the result produced by the
307/// instruction is not used, and the instruction has no side effects.
308///
309bool llvm::isInstructionTriviallyDead(Instruction *I) {
310  if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
311
312  // We don't want debug info removed by anything this general.
313  if (isa<DbgInfoIntrinsic>(I)) return false;
314
315  // Likewise for memory use markers.
316  if (isa<MemoryUseIntrinsic>(I)) return false;
317
318  if (!I->mayHaveSideEffects()) return true;
319
320  // Special case intrinsics that "may have side effects" but can be deleted
321  // when dead.
322  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
323    // Safe to delete llvm.stacksave if dead.
324    if (II->getIntrinsicID() == Intrinsic::stacksave)
325      return true;
326  return false;
327}
328
329/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
330/// trivially dead instruction, delete it.  If that makes any of its operands
331/// trivially dead, delete them too, recursively.  Return true if any
332/// instructions were deleted.
333bool llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) {
334  Instruction *I = dyn_cast<Instruction>(V);
335  if (!I || !I->use_empty() || !isInstructionTriviallyDead(I))
336    return false;
337
338  SmallVector<Instruction*, 16> DeadInsts;
339  DeadInsts.push_back(I);
340
341  do {
342    I = DeadInsts.pop_back_val();
343
344    // Null out all of the instruction's operands to see if any operand becomes
345    // dead as we go.
346    for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
347      Value *OpV = I->getOperand(i);
348      I->setOperand(i, 0);
349
350      if (!OpV->use_empty()) continue;
351
352      // If the operand is an instruction that became dead as we nulled out the
353      // operand, and if it is 'trivially' dead, delete it in a future loop
354      // iteration.
355      if (Instruction *OpI = dyn_cast<Instruction>(OpV))
356        if (isInstructionTriviallyDead(OpI))
357          DeadInsts.push_back(OpI);
358    }
359
360    I->eraseFromParent();
361  } while (!DeadInsts.empty());
362
363  return true;
364}
365
366/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
367/// dead PHI node, due to being a def-use chain of single-use nodes that
368/// either forms a cycle or is terminated by a trivially dead instruction,
369/// delete it.  If that makes any of its operands trivially dead, delete them
370/// too, recursively.  Return true if the PHI node is actually deleted.
371bool
372llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
373  // We can remove a PHI if it is on a cycle in the def-use graph
374  // where each node in the cycle has degree one, i.e. only one use,
375  // and is an instruction with no side effects.
376  if (!PN->hasOneUse())
377    return false;
378
379  bool Changed = false;
380  SmallPtrSet<PHINode *, 4> PHIs;
381  PHIs.insert(PN);
382  for (Instruction *J = cast<Instruction>(*PN->use_begin());
383       J->hasOneUse() && !J->mayHaveSideEffects();
384       J = cast<Instruction>(*J->use_begin()))
385    // If we find a PHI more than once, we're on a cycle that
386    // won't prove fruitful.
387    if (PHINode *JP = dyn_cast<PHINode>(J))
388      if (!PHIs.insert(cast<PHINode>(JP))) {
389        // Break the cycle and delete the PHI and its operands.
390        JP->replaceAllUsesWith(UndefValue::get(JP->getType()));
391        (void)RecursivelyDeleteTriviallyDeadInstructions(JP);
392        Changed = true;
393        break;
394      }
395  return Changed;
396}
397
398/// SimplifyInstructionsInBlock - Scan the specified basic block and try to
399/// simplify any instructions in it and recursively delete dead instructions.
400///
401/// This returns true if it changed the code, note that it can delete
402/// instructions in other blocks as well in this block.
403bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD) {
404  bool MadeChange = false;
405  for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
406    Instruction *Inst = BI++;
407
408    if (Value *V = SimplifyInstruction(Inst, TD)) {
409      WeakVH BIHandle(BI);
410      ReplaceAndSimplifyAllUses(Inst, V, TD);
411      MadeChange = true;
412      if (BIHandle == 0)
413        BI = BB->begin();
414      continue;
415    }
416
417    MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst);
418  }
419  return MadeChange;
420}
421
422//===----------------------------------------------------------------------===//
423//  Control Flow Graph Restructuring.
424//
425
426
427/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
428/// method is called when we're about to delete Pred as a predecessor of BB.  If
429/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
430///
431/// Unlike the removePredecessor method, this attempts to simplify uses of PHI
432/// nodes that collapse into identity values.  For example, if we have:
433///   x = phi(1, 0, 0, 0)
434///   y = and x, z
435///
436/// .. and delete the predecessor corresponding to the '1', this will attempt to
437/// recursively fold the and to 0.
438void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
439                                        TargetData *TD) {
440  // This only adjusts blocks with PHI nodes.
441  if (!isa<PHINode>(BB->begin()))
442    return;
443
444  // Remove the entries for Pred from the PHI nodes in BB, but do not simplify
445  // them down.  This will leave us with single entry phi nodes and other phis
446  // that can be removed.
447  BB->removePredecessor(Pred, true);
448
449  WeakVH PhiIt = &BB->front();
450  while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) {
451    PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt));
452
453    Value *PNV = PN->hasConstantValue();
454    if (PNV == 0) continue;
455
456    // If we're able to simplify the phi to a single value, substitute the new
457    // value into all of its uses.
458    assert(PNV != PN && "hasConstantValue broken");
459
460    ReplaceAndSimplifyAllUses(PN, PNV, TD);
461
462    // If recursive simplification ended up deleting the next PHI node we would
463    // iterate to, then our iterator is invalid, restart scanning from the top
464    // of the block.
465    if (PhiIt == 0) PhiIt = &BB->front();
466  }
467}
468
469
470/// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
471/// predecessor is known to have one successor (DestBB!).  Eliminate the edge
472/// between them, moving the instructions in the predecessor into DestBB and
473/// deleting the predecessor block.
474///
475void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
476  // If BB has single-entry PHI nodes, fold them.
477  while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
478    Value *NewVal = PN->getIncomingValue(0);
479    // Replace self referencing PHI with undef, it must be dead.
480    if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
481    PN->replaceAllUsesWith(NewVal);
482    PN->eraseFromParent();
483  }
484
485  BasicBlock *PredBB = DestBB->getSinglePredecessor();
486  assert(PredBB && "Block doesn't have a single predecessor!");
487
488  // Splice all the instructions from PredBB to DestBB.
489  PredBB->getTerminator()->eraseFromParent();
490  DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
491
492  // Anything that branched to PredBB now branches to DestBB.
493  PredBB->replaceAllUsesWith(DestBB);
494
495  if (P) {
496    ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
497    if (PI) {
498      PI->replaceAllUses(PredBB, DestBB);
499      PI->removeEdge(ProfileInfo::getEdge(PredBB, DestBB));
500    }
501  }
502  // Nuke BB.
503  PredBB->eraseFromParent();
504}
505
506/// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
507/// almost-empty BB ending in an unconditional branch to Succ, into succ.
508///
509/// Assumption: Succ is the single successor for BB.
510///
511static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
512  assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
513
514  DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
515        << Succ->getName() << "\n");
516  // Shortcut, if there is only a single predecessor it must be BB and merging
517  // is always safe
518  if (Succ->getSinglePredecessor()) return true;
519
520  // Make a list of the predecessors of BB
521  typedef SmallPtrSet<BasicBlock*, 16> BlockSet;
522  BlockSet BBPreds(pred_begin(BB), pred_end(BB));
523
524  // Use that list to make another list of common predecessors of BB and Succ
525  BlockSet CommonPreds;
526  for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
527        PI != PE; ++PI)
528    if (BBPreds.count(*PI))
529      CommonPreds.insert(*PI);
530
531  // Shortcut, if there are no common predecessors, merging is always safe
532  if (CommonPreds.empty())
533    return true;
534
535  // Look at all the phi nodes in Succ, to see if they present a conflict when
536  // merging these blocks
537  for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
538    PHINode *PN = cast<PHINode>(I);
539
540    // If the incoming value from BB is again a PHINode in
541    // BB which has the same incoming value for *PI as PN does, we can
542    // merge the phi nodes and then the blocks can still be merged
543    PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
544    if (BBPN && BBPN->getParent() == BB) {
545      for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
546            PI != PE; PI++) {
547        if (BBPN->getIncomingValueForBlock(*PI)
548              != PN->getIncomingValueForBlock(*PI)) {
549          DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
550                << Succ->getName() << " is conflicting with "
551                << BBPN->getName() << " with regard to common predecessor "
552                << (*PI)->getName() << "\n");
553          return false;
554        }
555      }
556    } else {
557      Value* Val = PN->getIncomingValueForBlock(BB);
558      for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
559            PI != PE; PI++) {
560        // See if the incoming value for the common predecessor is equal to the
561        // one for BB, in which case this phi node will not prevent the merging
562        // of the block.
563        if (Val != PN->getIncomingValueForBlock(*PI)) {
564          DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
565                << Succ->getName() << " is conflicting with regard to common "
566                << "predecessor " << (*PI)->getName() << "\n");
567          return false;
568        }
569      }
570    }
571  }
572
573  return true;
574}
575
576/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
577/// unconditional branch, and contains no instructions other than PHI nodes,
578/// potential debug intrinsics and the branch.  If possible, eliminate BB by
579/// rewriting all the predecessors to branch to the successor block and return
580/// true.  If we can't transform, return false.
581bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
582  // We can't eliminate infinite loops.
583  BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
584  if (BB == Succ) return false;
585
586  // Check to see if merging these blocks would cause conflicts for any of the
587  // phi nodes in BB or Succ. If not, we can safely merge.
588  if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
589
590  // Check for cases where Succ has multiple predecessors and a PHI node in BB
591  // has uses which will not disappear when the PHI nodes are merged.  It is
592  // possible to handle such cases, but difficult: it requires checking whether
593  // BB dominates Succ, which is non-trivial to calculate in the case where
594  // Succ has multiple predecessors.  Also, it requires checking whether
595  // constructing the necessary self-referential PHI node doesn't intoduce any
596  // conflicts; this isn't too difficult, but the previous code for doing this
597  // was incorrect.
598  //
599  // Note that if this check finds a live use, BB dominates Succ, so BB is
600  // something like a loop pre-header (or rarely, a part of an irreducible CFG);
601  // folding the branch isn't profitable in that case anyway.
602  if (!Succ->getSinglePredecessor()) {
603    BasicBlock::iterator BBI = BB->begin();
604    while (isa<PHINode>(*BBI)) {
605      for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
606           UI != E; ++UI) {
607        if (PHINode* PN = dyn_cast<PHINode>(*UI)) {
608          if (PN->getIncomingBlock(UI) != BB)
609            return false;
610        } else {
611          return false;
612        }
613      }
614      ++BBI;
615    }
616  }
617
618  DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
619
620  if (isa<PHINode>(Succ->begin())) {
621    // If there is more than one pred of succ, and there are PHI nodes in
622    // the successor, then we need to add incoming edges for the PHI nodes
623    //
624    const SmallVector<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
625
626    // Loop over all of the PHI nodes in the successor of BB.
627    for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
628      PHINode *PN = cast<PHINode>(I);
629      Value *OldVal = PN->removeIncomingValue(BB, false);
630      assert(OldVal && "No entry in PHI for Pred BB!");
631
632      // If this incoming value is one of the PHI nodes in BB, the new entries
633      // in the PHI node are the entries from the old PHI.
634      if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
635        PHINode *OldValPN = cast<PHINode>(OldVal);
636        for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
637          // Note that, since we are merging phi nodes and BB and Succ might
638          // have common predecessors, we could end up with a phi node with
639          // identical incoming branches. This will be cleaned up later (and
640          // will trigger asserts if we try to clean it up now, without also
641          // simplifying the corresponding conditional branch).
642          PN->addIncoming(OldValPN->getIncomingValue(i),
643                          OldValPN->getIncomingBlock(i));
644      } else {
645        // Add an incoming value for each of the new incoming values.
646        for (unsigned i = 0, e = BBPreds.size(); i != e; ++i)
647          PN->addIncoming(OldVal, BBPreds[i]);
648      }
649    }
650  }
651
652  while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
653    if (Succ->getSinglePredecessor()) {
654      // BB is the only predecessor of Succ, so Succ will end up with exactly
655      // the same predecessors BB had.
656      Succ->getInstList().splice(Succ->begin(),
657                                 BB->getInstList(), BB->begin());
658    } else {
659      // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
660      assert(PN->use_empty() && "There shouldn't be any uses here!");
661      PN->eraseFromParent();
662    }
663  }
664
665  // Everything that jumped to BB now goes to Succ.
666  BB->replaceAllUsesWith(Succ);
667  if (!Succ->hasName()) Succ->takeName(BB);
668  BB->eraseFromParent();              // Delete the old basic block.
669  return true;
670}
671
672/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
673/// nodes in this block. This doesn't try to be clever about PHI nodes
674/// which differ only in the order of the incoming values, but instcombine
675/// orders them so it usually won't matter.
676///
677bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
678  bool Changed = false;
679
680  // This implementation doesn't currently consider undef operands
681  // specially. Theroetically, two phis which are identical except for
682  // one having an undef where the other doesn't could be collapsed.
683
684  // Map from PHI hash values to PHI nodes. If multiple PHIs have
685  // the same hash value, the element is the first PHI in the
686  // linked list in CollisionMap.
687  DenseMap<uintptr_t, PHINode *> HashMap;
688
689  // Maintain linked lists of PHI nodes with common hash values.
690  DenseMap<PHINode *, PHINode *> CollisionMap;
691
692  // Examine each PHI.
693  for (BasicBlock::iterator I = BB->begin();
694       PHINode *PN = dyn_cast<PHINode>(I++); ) {
695    // Compute a hash value on the operands. Instcombine will likely have sorted
696    // them, which helps expose duplicates, but we have to check all the
697    // operands to be safe in case instcombine hasn't run.
698    uintptr_t Hash = 0;
699    for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
700      // This hash algorithm is quite weak as hash functions go, but it seems
701      // to do a good enough job for this particular purpose, and is very quick.
702      Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
703      Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
704    }
705    // If we've never seen this hash value before, it's a unique PHI.
706    std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair =
707      HashMap.insert(std::make_pair(Hash, PN));
708    if (Pair.second) continue;
709    // Otherwise it's either a duplicate or a hash collision.
710    for (PHINode *OtherPN = Pair.first->second; ; ) {
711      if (OtherPN->isIdenticalTo(PN)) {
712        // A duplicate. Replace this PHI with its duplicate.
713        PN->replaceAllUsesWith(OtherPN);
714        PN->eraseFromParent();
715        Changed = true;
716        break;
717      }
718      // A non-duplicate hash collision.
719      DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN);
720      if (I == CollisionMap.end()) {
721        // Set this PHI to be the head of the linked list of colliding PHIs.
722        PHINode *Old = Pair.first->second;
723        Pair.first->second = PN;
724        CollisionMap[PN] = Old;
725        break;
726      }
727      // Procede to the next PHI in the list.
728      OtherPN = I->second;
729    }
730  }
731
732  return Changed;
733}
734