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