]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Transforms/Utils/Local.cpp
Update LLVM to r96341.
[FreeBSD/FreeBSD.git] / lib / Transforms / Utils / Local.cpp
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"
35 using 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.
46 static 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.
77 bool 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 //
150 bool 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 ///
311 bool 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.
335 bool 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.
373 bool
374 llvm::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.
405 bool 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.
440 void 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 ///
477 void 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   // Zap anything that took the address of DestBB.  Not doing this will give the
495   // address an invalid value.
496   if (DestBB->hasAddressTaken()) {
497     BlockAddress *BA = BlockAddress::get(DestBB);
498     Constant *Replacement =
499       ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1);
500     BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
501                                                      BA->getType()));
502     BA->destroyConstant();
503   }
504   
505   // Anything that branched to PredBB now branches to DestBB.
506   PredBB->replaceAllUsesWith(DestBB);
507   
508   if (P) {
509     ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
510     if (PI) {
511       PI->replaceAllUses(PredBB, DestBB);
512       PI->removeEdge(ProfileInfo::getEdge(PredBB, DestBB));
513     }
514   }
515   // Nuke BB.
516   PredBB->eraseFromParent();
517 }
518
519 /// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
520 /// almost-empty BB ending in an unconditional branch to Succ, into succ.
521 ///
522 /// Assumption: Succ is the single successor for BB.
523 ///
524 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
525   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
526
527   DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " 
528         << Succ->getName() << "\n");
529   // Shortcut, if there is only a single predecessor it must be BB and merging
530   // is always safe
531   if (Succ->getSinglePredecessor()) return true;
532
533   // Make a list of the predecessors of BB
534   typedef SmallPtrSet<BasicBlock*, 16> BlockSet;
535   BlockSet BBPreds(pred_begin(BB), pred_end(BB));
536
537   // Use that list to make another list of common predecessors of BB and Succ
538   BlockSet CommonPreds;
539   for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
540         PI != PE; ++PI)
541     if (BBPreds.count(*PI))
542       CommonPreds.insert(*PI);
543
544   // Shortcut, if there are no common predecessors, merging is always safe
545   if (CommonPreds.empty())
546     return true;
547   
548   // Look at all the phi nodes in Succ, to see if they present a conflict when
549   // merging these blocks
550   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
551     PHINode *PN = cast<PHINode>(I);
552
553     // If the incoming value from BB is again a PHINode in
554     // BB which has the same incoming value for *PI as PN does, we can
555     // merge the phi nodes and then the blocks can still be merged
556     PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
557     if (BBPN && BBPN->getParent() == BB) {
558       for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
559             PI != PE; PI++) {
560         if (BBPN->getIncomingValueForBlock(*PI) 
561               != PN->getIncomingValueForBlock(*PI)) {
562           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 
563                 << Succ->getName() << " is conflicting with " 
564                 << BBPN->getName() << " with regard to common predecessor "
565                 << (*PI)->getName() << "\n");
566           return false;
567         }
568       }
569     } else {
570       Value* Val = PN->getIncomingValueForBlock(BB);
571       for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
572             PI != PE; PI++) {
573         // See if the incoming value for the common predecessor is equal to the
574         // one for BB, in which case this phi node will not prevent the merging
575         // of the block.
576         if (Val != PN->getIncomingValueForBlock(*PI)) {
577           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 
578                 << Succ->getName() << " is conflicting with regard to common "
579                 << "predecessor " << (*PI)->getName() << "\n");
580           return false;
581         }
582       }
583     }
584   }
585
586   return true;
587 }
588
589 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
590 /// unconditional branch, and contains no instructions other than PHI nodes,
591 /// potential debug intrinsics and the branch.  If possible, eliminate BB by
592 /// rewriting all the predecessors to branch to the successor block and return
593 /// true.  If we can't transform, return false.
594 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
595   // We can't eliminate infinite loops.
596   BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
597   if (BB == Succ) return false;
598   
599   // Check to see if merging these blocks would cause conflicts for any of the
600   // phi nodes in BB or Succ. If not, we can safely merge.
601   if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
602
603   // Check for cases where Succ has multiple predecessors and a PHI node in BB
604   // has uses which will not disappear when the PHI nodes are merged.  It is
605   // possible to handle such cases, but difficult: it requires checking whether
606   // BB dominates Succ, which is non-trivial to calculate in the case where
607   // Succ has multiple predecessors.  Also, it requires checking whether
608   // constructing the necessary self-referential PHI node doesn't intoduce any
609   // conflicts; this isn't too difficult, but the previous code for doing this
610   // was incorrect.
611   //
612   // Note that if this check finds a live use, BB dominates Succ, so BB is
613   // something like a loop pre-header (or rarely, a part of an irreducible CFG);
614   // folding the branch isn't profitable in that case anyway.
615   if (!Succ->getSinglePredecessor()) {
616     BasicBlock::iterator BBI = BB->begin();
617     while (isa<PHINode>(*BBI)) {
618       for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
619            UI != E; ++UI) {
620         if (PHINode* PN = dyn_cast<PHINode>(*UI)) {
621           if (PN->getIncomingBlock(UI) != BB)
622             return false;
623         } else {
624           return false;
625         }
626       }
627       ++BBI;
628     }
629   }
630
631   DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
632   
633   if (isa<PHINode>(Succ->begin())) {
634     // If there is more than one pred of succ, and there are PHI nodes in
635     // the successor, then we need to add incoming edges for the PHI nodes
636     //
637     const SmallVector<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
638     
639     // Loop over all of the PHI nodes in the successor of BB.
640     for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
641       PHINode *PN = cast<PHINode>(I);
642       Value *OldVal = PN->removeIncomingValue(BB, false);
643       assert(OldVal && "No entry in PHI for Pred BB!");
644       
645       // If this incoming value is one of the PHI nodes in BB, the new entries
646       // in the PHI node are the entries from the old PHI.
647       if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
648         PHINode *OldValPN = cast<PHINode>(OldVal);
649         for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
650           // Note that, since we are merging phi nodes and BB and Succ might
651           // have common predecessors, we could end up with a phi node with
652           // identical incoming branches. This will be cleaned up later (and
653           // will trigger asserts if we try to clean it up now, without also
654           // simplifying the corresponding conditional branch).
655           PN->addIncoming(OldValPN->getIncomingValue(i),
656                           OldValPN->getIncomingBlock(i));
657       } else {
658         // Add an incoming value for each of the new incoming values.
659         for (unsigned i = 0, e = BBPreds.size(); i != e; ++i)
660           PN->addIncoming(OldVal, BBPreds[i]);
661       }
662     }
663   }
664   
665   while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
666     if (Succ->getSinglePredecessor()) {
667       // BB is the only predecessor of Succ, so Succ will end up with exactly
668       // the same predecessors BB had.
669       Succ->getInstList().splice(Succ->begin(),
670                                  BB->getInstList(), BB->begin());
671     } else {
672       // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
673       assert(PN->use_empty() && "There shouldn't be any uses here!");
674       PN->eraseFromParent();
675     }
676   }
677     
678   // Everything that jumped to BB now goes to Succ.
679   BB->replaceAllUsesWith(Succ);
680   if (!Succ->hasName()) Succ->takeName(BB);
681   BB->eraseFromParent();              // Delete the old basic block.
682   return true;
683 }
684
685 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
686 /// nodes in this block. This doesn't try to be clever about PHI nodes
687 /// which differ only in the order of the incoming values, but instcombine
688 /// orders them so it usually won't matter.
689 ///
690 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
691   bool Changed = false;
692
693   // This implementation doesn't currently consider undef operands
694   // specially. Theroetically, two phis which are identical except for
695   // one having an undef where the other doesn't could be collapsed.
696
697   // Map from PHI hash values to PHI nodes. If multiple PHIs have
698   // the same hash value, the element is the first PHI in the
699   // linked list in CollisionMap.
700   DenseMap<uintptr_t, PHINode *> HashMap;
701
702   // Maintain linked lists of PHI nodes with common hash values.
703   DenseMap<PHINode *, PHINode *> CollisionMap;
704
705   // Examine each PHI.
706   for (BasicBlock::iterator I = BB->begin();
707        PHINode *PN = dyn_cast<PHINode>(I++); ) {
708     // Compute a hash value on the operands. Instcombine will likely have sorted
709     // them, which helps expose duplicates, but we have to check all the
710     // operands to be safe in case instcombine hasn't run.
711     uintptr_t Hash = 0;
712     for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
713       // This hash algorithm is quite weak as hash functions go, but it seems
714       // to do a good enough job for this particular purpose, and is very quick.
715       Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
716       Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
717     }
718     // If we've never seen this hash value before, it's a unique PHI.
719     std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair =
720       HashMap.insert(std::make_pair(Hash, PN));
721     if (Pair.second) continue;
722     // Otherwise it's either a duplicate or a hash collision.
723     for (PHINode *OtherPN = Pair.first->second; ; ) {
724       if (OtherPN->isIdenticalTo(PN)) {
725         // A duplicate. Replace this PHI with its duplicate.
726         PN->replaceAllUsesWith(OtherPN);
727         PN->eraseFromParent();
728         Changed = true;
729         break;
730       }
731       // A non-duplicate hash collision.
732       DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN);
733       if (I == CollisionMap.end()) {
734         // Set this PHI to be the head of the linked list of colliding PHIs.
735         PHINode *Old = Pair.first->second;
736         Pair.first->second = PN;
737         CollisionMap[PN] = Old;
738         break;
739       }
740       // Procede to the next PHI in the list.
741       OtherPN = I->second;
742     }
743   }
744
745   return Changed;
746 }