Remove the "isSigned" parameters from ConstantRange. It turns out they
[oota-llvm.git] / lib / Transforms / Scalar / CorrelatedExprs.cpp
1 //===- CorrelatedExprs.cpp - Pass to detect and eliminated c.e.'s ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Correlated Expression Elimination propagates information from conditional
11 // branches to blocks dominated by destinations of the branch.  It propagates
12 // information from the condition check itself into the body of the branch,
13 // allowing transformations like these for example:
14 //
15 //  if (i == 7)
16 //    ... 4*i;  // constant propagation
17 //
18 //  M = i+1; N = j+1;
19 //  if (i == j)
20 //    X = M-N;  // = M-M == 0;
21 //
22 // This is called Correlated Expression Elimination because we eliminate or
23 // simplify expressions that are correlated with the direction of a branch.  In
24 // this way we use static information to give us some information about the
25 // dynamic value of a variable.
26 //
27 //===----------------------------------------------------------------------===//
28
29 #define DEBUG_TYPE "cee"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Constants.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Function.h"
34 #include "llvm/Instructions.h"
35 #include "llvm/Type.h"
36 #include "llvm/DerivedTypes.h"
37 #include "llvm/Analysis/ConstantFolding.h"
38 #include "llvm/Analysis/Dominators.h"
39 #include "llvm/Assembly/Writer.h"
40 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
41 #include "llvm/Support/CFG.h"
42 #include "llvm/Support/Compiler.h"
43 #include "llvm/Support/ConstantRange.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/ADT/PostOrderIterator.h"
46 #include "llvm/ADT/Statistic.h"
47 #include <algorithm>
48 using namespace llvm;
49
50 STATISTIC(NumCmpRemoved, "Number of cmp instruction eliminated");
51 STATISTIC(NumOperandsCann, "Number of operands canonicalized");
52 STATISTIC(BranchRevectors, "Number of branches revectored");
53
54 namespace {
55   class ValueInfo;
56   class VISIBILITY_HIDDEN Relation {
57     Value *Val;          // Relation to what value?
58     unsigned Rel;        // SetCC or ICmp relation, or Add if no information
59   public:
60     Relation(Value *V) : Val(V), Rel(Instruction::Add) {}
61     bool operator<(const Relation &R) const { return Val < R.Val; }
62     Value *getValue() const { return Val; }
63     unsigned getRelation() const { return Rel; }
64
65     // contradicts - Return true if the relationship specified by the operand
66     // contradicts already known information.
67     //
68     bool contradicts(unsigned Rel, const ValueInfo &VI) const;
69
70     // incorporate - Incorporate information in the argument into this relation
71     // entry.  This assumes that the information doesn't contradict itself.  If
72     // any new information is gained, true is returned, otherwise false is
73     // returned to indicate that nothing was updated.
74     //
75     bool incorporate(unsigned Rel, ValueInfo &VI);
76
77     // KnownResult - Whether or not this condition determines the result of a
78     // setcc or icmp in the program.  False & True are intentionally 0 & 1 
79     // so we can convert to bool by casting after checking for unknown.
80     //
81     enum KnownResult { KnownFalse = 0, KnownTrue = 1, Unknown = 2 };
82
83     // getImpliedResult - If this relationship between two values implies that
84     // the specified relationship is true or false, return that.  If we cannot
85     // determine the result required, return Unknown.
86     //
87     KnownResult getImpliedResult(unsigned Rel) const;
88
89     // print - Output this relation to the specified stream
90     void print(std::ostream &OS) const;
91     void dump() const;
92   };
93
94
95   // ValueInfo - One instance of this record exists for every value with
96   // relationships between other values.  It keeps track of all of the
97   // relationships to other values in the program (specified with Relation) that
98   // are known to be valid in a region.
99   //
100   class VISIBILITY_HIDDEN ValueInfo {
101     // RelationShips - this value is know to have the specified relationships to
102     // other values.  There can only be one entry per value, and this list is
103     // kept sorted by the Val field.
104     std::vector<Relation> Relationships;
105
106     // If information about this value is known or propagated from constant
107     // expressions, this range contains the possible values this value may hold.
108     ConstantRange Bounds;
109
110     // If we find that this value is equal to another value that has a lower
111     // rank, this value is used as it's replacement.
112     //
113     Value *Replacement;
114   public:
115     ValueInfo(const Type *Ty)
116       : Bounds(Ty->isInteger() ? cast<IntegerType>(Ty)->getBitWidth()  : 32), 
117                Replacement(0) {}
118
119     // getBounds() - Return the constant bounds of the value...
120     const ConstantRange &getBounds() const { return Bounds; }
121     ConstantRange &getBounds() { return Bounds; }
122
123     const std::vector<Relation> &getRelationships() { return Relationships; }
124
125     // getReplacement - Return the value this value is to be replaced with if it
126     // exists, otherwise return null.
127     //
128     Value *getReplacement() const { return Replacement; }
129
130     // setReplacement - Used by the replacement calculation pass to figure out
131     // what to replace this value with, if anything.
132     //
133     void setReplacement(Value *Repl) { Replacement = Repl; }
134
135     // getRelation - return the relationship entry for the specified value.
136     // This can invalidate references to other Relations, so use it carefully.
137     //
138     Relation &getRelation(Value *V) {
139       // Binary search for V's entry...
140       std::vector<Relation>::iterator I =
141         std::lower_bound(Relationships.begin(), Relationships.end(),
142                          Relation(V));
143
144       // If we found the entry, return it...
145       if (I != Relationships.end() && I->getValue() == V)
146         return *I;
147
148       // Insert and return the new relationship...
149       return *Relationships.insert(I, V);
150     }
151
152     const Relation *requestRelation(Value *V) const {
153       // Binary search for V's entry...
154       std::vector<Relation>::const_iterator I =
155         std::lower_bound(Relationships.begin(), Relationships.end(),
156                          Relation(V));
157       if (I != Relationships.end() && I->getValue() == V)
158         return &*I;
159       return 0;
160     }
161
162     // print - Output information about this value relation...
163     void print(std::ostream &OS, Value *V) const;
164     void dump() const;
165   };
166
167   // RegionInfo - Keeps track of all of the value relationships for a region.  A
168   // region is the are dominated by a basic block.  RegionInfo's keep track of
169   // the RegionInfo for their dominator, because anything known in a dominator
170   // is known to be true in a dominated block as well.
171   //
172   class VISIBILITY_HIDDEN RegionInfo {
173     BasicBlock *BB;
174
175     // ValueMap - Tracks the ValueInformation known for this region
176     typedef std::map<Value*, ValueInfo> ValueMapTy;
177     ValueMapTy ValueMap;
178   public:
179     RegionInfo(BasicBlock *bb) : BB(bb) {}
180
181     // getEntryBlock - Return the block that dominates all of the members of
182     // this region.
183     BasicBlock *getEntryBlock() const { return BB; }
184
185     // empty - return true if this region has no information known about it.
186     bool empty() const { return ValueMap.empty(); }
187
188     const RegionInfo &operator=(const RegionInfo &RI) {
189       ValueMap = RI.ValueMap;
190       return *this;
191     }
192
193     // print - Output information about this region...
194     void print(std::ostream &OS) const;
195     void dump() const;
196
197     // Allow external access.
198     typedef ValueMapTy::iterator iterator;
199     iterator begin() { return ValueMap.begin(); }
200     iterator end() { return ValueMap.end(); }
201
202     ValueInfo &getValueInfo(Value *V) {
203       ValueMapTy::iterator I = ValueMap.lower_bound(V);
204       if (I != ValueMap.end() && I->first == V) return I->second;
205       return ValueMap.insert(I, std::make_pair(V, V->getType()))->second;
206     }
207
208     const ValueInfo *requestValueInfo(Value *V) const {
209       ValueMapTy::const_iterator I = ValueMap.find(V);
210       if (I != ValueMap.end()) return &I->second;
211       return 0;
212     }
213
214     /// removeValueInfo - Remove anything known about V from our records.  This
215     /// works whether or not we know anything about V.
216     ///
217     void removeValueInfo(Value *V) {
218       ValueMap.erase(V);
219     }
220   };
221
222   /// CEE - Correlated Expression Elimination
223   class VISIBILITY_HIDDEN CEE : public FunctionPass {
224     std::map<Value*, unsigned> RankMap;
225     std::map<BasicBlock*, RegionInfo> RegionInfoMap;
226     ETForest *EF;
227     DominatorTree *DT;
228   public:
229     virtual bool runOnFunction(Function &F);
230
231     // We don't modify the program, so we preserve all analyses
232     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
233       AU.addRequired<ETForest>();
234       AU.addRequired<DominatorTree>();
235       AU.addRequiredID(BreakCriticalEdgesID);
236     };
237
238     // print - Implement the standard print form to print out analysis
239     // information.
240     virtual void print(std::ostream &O, const Module *M) const;
241
242   private:
243     RegionInfo &getRegionInfo(BasicBlock *BB) {
244       std::map<BasicBlock*, RegionInfo>::iterator I
245         = RegionInfoMap.lower_bound(BB);
246       if (I != RegionInfoMap.end() && I->first == BB) return I->second;
247       return RegionInfoMap.insert(I, std::make_pair(BB, BB))->second;
248     }
249
250     void BuildRankMap(Function &F);
251     unsigned getRank(Value *V) const {
252       if (isa<Constant>(V)) return 0;
253       std::map<Value*, unsigned>::const_iterator I = RankMap.find(V);
254       if (I != RankMap.end()) return I->second;
255       return 0; // Must be some other global thing
256     }
257
258     bool TransformRegion(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks);
259
260     bool ForwardCorrelatedEdgeDestination(TerminatorInst *TI, unsigned SuccNo,
261                                           RegionInfo &RI);
262
263     void ForwardSuccessorTo(TerminatorInst *TI, unsigned Succ, BasicBlock *D,
264                             RegionInfo &RI);
265     void ReplaceUsesOfValueInRegion(Value *Orig, Value *New,
266                                     BasicBlock *RegionDominator);
267     void CalculateRegionExitBlocks(BasicBlock *BB, BasicBlock *OldSucc,
268                                    std::vector<BasicBlock*> &RegionExitBlocks);
269     void InsertRegionExitMerges(PHINode *NewPHI, Instruction *OldVal,
270                              const std::vector<BasicBlock*> &RegionExitBlocks);
271
272     void PropagateBranchInfo(BranchInst *BI);
273     void PropagateSwitchInfo(SwitchInst *SI);
274     void PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI);
275     void PropagateRelation(unsigned Opcode, Value *Op0,
276                            Value *Op1, RegionInfo &RI);
277     void UpdateUsersOfValue(Value *V, RegionInfo &RI);
278     void IncorporateInstruction(Instruction *Inst, RegionInfo &RI);
279     void ComputeReplacements(RegionInfo &RI);
280
281     // getCmpResult - Given a icmp instruction, determine if the result is
282     // determined by facts we already know about the region under analysis.
283     // Return KnownTrue, KnownFalse, or UnKnown based on what we can determine.
284     Relation::KnownResult getCmpResult(CmpInst *ICI, const RegionInfo &RI);
285
286     bool SimplifyBasicBlock(BasicBlock &BB, const RegionInfo &RI);
287     bool SimplifyInstruction(Instruction *Inst, const RegionInfo &RI);
288   };
289   RegisterPass<CEE> X("cee", "Correlated Expression Elimination");
290 }
291
292 FunctionPass *llvm::createCorrelatedExpressionEliminationPass() {
293   return new CEE();
294 }
295
296
297 bool CEE::runOnFunction(Function &F) {
298   // Build a rank map for the function...
299   BuildRankMap(F);
300
301   // Traverse the dominator tree, computing information for each node in the
302   // tree.  Note that our traversal will not even touch unreachable basic
303   // blocks.
304   EF = &getAnalysis<ETForest>();
305   DT = &getAnalysis<DominatorTree>();
306
307   std::set<BasicBlock*> VisitedBlocks;
308   bool Changed = TransformRegion(&F.getEntryBlock(), VisitedBlocks);
309
310   RegionInfoMap.clear();
311   RankMap.clear();
312   return Changed;
313 }
314
315 // TransformRegion - Transform the region starting with BB according to the
316 // calculated region information for the block.  Transforming the region
317 // involves analyzing any information this block provides to successors,
318 // propagating the information to successors, and finally transforming
319 // successors.
320 //
321 // This method processes the function in depth first order, which guarantees
322 // that we process the immediate dominator of a block before the block itself.
323 // Because we are passing information from immediate dominators down to
324 // dominatees, we obviously have to process the information source before the
325 // information consumer.
326 //
327 bool CEE::TransformRegion(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks){
328   // Prevent infinite recursion...
329   if (VisitedBlocks.count(BB)) return false;
330   VisitedBlocks.insert(BB);
331
332   // Get the computed region information for this block...
333   RegionInfo &RI = getRegionInfo(BB);
334
335   // Compute the replacement information for this block...
336   ComputeReplacements(RI);
337
338   // If debugging, print computed region information...
339   DEBUG(RI.print(*cerr.stream()));
340
341   // Simplify the contents of this block...
342   bool Changed = SimplifyBasicBlock(*BB, RI);
343
344   // Get the terminator of this basic block...
345   TerminatorInst *TI = BB->getTerminator();
346
347   // Loop over all of the blocks that this block is the immediate dominator for.
348   // Because all information known in this region is also known in all of the
349   // blocks that are dominated by this one, we can safely propagate the
350   // information down now.
351   //
352   DominatorTree::Node *BBN = (*DT)[BB];
353   if (!RI.empty())        // Time opt: only propagate if we can change something
354     for (unsigned i = 0, e = BBN->getChildren().size(); i != e; ++i) {
355       BasicBlock *Dominated = BBN->getChildren()[i]->getBlock();
356       assert(RegionInfoMap.find(Dominated) == RegionInfoMap.end() &&
357              "RegionInfo should be calculated in dominanace order!");
358       getRegionInfo(Dominated) = RI;
359     }
360
361   // Now that all of our successors have information if they deserve it,
362   // propagate any information our terminator instruction finds to our
363   // successors.
364   if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
365     if (BI->isConditional())
366       PropagateBranchInfo(BI);
367   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
368     PropagateSwitchInfo(SI);
369   }
370
371   // If this is a branch to a block outside our region that simply performs
372   // another conditional branch, one whose outcome is known inside of this
373   // region, then vector this outgoing edge directly to the known destination.
374   //
375   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
376     while (ForwardCorrelatedEdgeDestination(TI, i, RI)) {
377       ++BranchRevectors;
378       Changed = true;
379     }
380
381   // Now that all of our successors have information, recursively process them.
382   for (unsigned i = 0, e = BBN->getChildren().size(); i != e; ++i)
383     Changed |= TransformRegion(BBN->getChildren()[i]->getBlock(),VisitedBlocks);
384
385   return Changed;
386 }
387
388 // isBlockSimpleEnoughForCheck to see if the block is simple enough for us to
389 // revector the conditional branch in the bottom of the block, do so now.
390 //
391 static bool isBlockSimpleEnough(BasicBlock *BB) {
392   assert(isa<BranchInst>(BB->getTerminator()));
393   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
394   assert(BI->isConditional());
395
396   // Check the common case first: empty block, or block with just a setcc.
397   if (BB->size() == 1 ||
398       (BB->size() == 2 && &BB->front() == BI->getCondition() &&
399        BI->getCondition()->hasOneUse()))
400     return true;
401
402   // Check the more complex case now...
403   BasicBlock::iterator I = BB->begin();
404
405   // FIXME: This should be reenabled once the regression with SIM is fixed!
406 #if 0
407   // PHI Nodes are ok, just skip over them...
408   while (isa<PHINode>(*I)) ++I;
409 #endif
410
411   // Accept the setcc instruction...
412   if (&*I == BI->getCondition())
413     ++I;
414
415   // Nothing else is acceptable here yet.  We must not revector... unless we are
416   // at the terminator instruction.
417   if (&*I == BI)
418     return true;
419
420   return false;
421 }
422
423
424 bool CEE::ForwardCorrelatedEdgeDestination(TerminatorInst *TI, unsigned SuccNo,
425                                            RegionInfo &RI) {
426   // If this successor is a simple block not in the current region, which
427   // contains only a conditional branch, we decide if the outcome of the branch
428   // can be determined from information inside of the region.  Instead of going
429   // to this block, we can instead go to the destination we know is the right
430   // target.
431   //
432
433   // Check to see if we dominate the block. If so, this block will get the
434   // condition turned to a constant anyway.
435   //
436   //if (EF->dominates(RI.getEntryBlock(), BB))
437   // return 0;
438
439   BasicBlock *BB = TI->getParent();
440
441   // Get the destination block of this edge...
442   BasicBlock *OldSucc = TI->getSuccessor(SuccNo);
443
444   // Make sure that the block ends with a conditional branch and is simple
445   // enough for use to be able to revector over.
446   BranchInst *BI = dyn_cast<BranchInst>(OldSucc->getTerminator());
447   if (BI == 0 || !BI->isConditional() || !isBlockSimpleEnough(OldSucc))
448     return false;
449
450   // We can only forward the branch over the block if the block ends with a
451   // cmp we can determine the outcome for.
452   //
453   // FIXME: we can make this more generic.  Code below already handles more
454   // generic case.
455   if (!isa<CmpInst>(BI->getCondition()))
456     return false;
457
458   // Make a new RegionInfo structure so that we can simulate the effect of the
459   // PHI nodes in the block we are skipping over...
460   //
461   RegionInfo NewRI(RI);
462
463   // Remove value information for all of the values we are simulating... to make
464   // sure we don't have any stale information.
465   for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); I!=E; ++I)
466     if (I->getType() != Type::VoidTy)
467       NewRI.removeValueInfo(I);
468
469   // Put the newly discovered information into the RegionInfo...
470   for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); I!=E; ++I)
471     if (PHINode *PN = dyn_cast<PHINode>(I)) {
472       int OpNum = PN->getBasicBlockIndex(BB);
473       assert(OpNum != -1 && "PHI doesn't have incoming edge for predecessor!?");
474       PropagateEquality(PN, PN->getIncomingValue(OpNum), NewRI);
475     } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) {
476       Relation::KnownResult Res = getCmpResult(CI, NewRI);
477       if (Res == Relation::Unknown) return false;
478       PropagateEquality(CI, ConstantInt::get(Type::Int1Ty, Res), NewRI);
479     } else {
480       assert(isa<BranchInst>(*I) && "Unexpected instruction type!");
481     }
482
483   // Compute the facts implied by what we have discovered...
484   ComputeReplacements(NewRI);
485
486   ValueInfo &PredicateVI = NewRI.getValueInfo(BI->getCondition());
487   if (PredicateVI.getReplacement() &&
488       isa<Constant>(PredicateVI.getReplacement()) &&
489       !isa<GlobalValue>(PredicateVI.getReplacement())) {
490     ConstantInt *CB = cast<ConstantInt>(PredicateVI.getReplacement());
491
492     // Forward to the successor that corresponds to the branch we will take.
493     ForwardSuccessorTo(TI, SuccNo, 
494                        BI->getSuccessor(!CB->getZExtValue()), NewRI);
495     return true;
496   }
497
498   return false;
499 }
500
501 static Value *getReplacementOrValue(Value *V, RegionInfo &RI) {
502   if (const ValueInfo *VI = RI.requestValueInfo(V))
503     if (Value *Repl = VI->getReplacement())
504       return Repl;
505   return V;
506 }
507
508 /// ForwardSuccessorTo - We have found that we can forward successor # 'SuccNo'
509 /// of Terminator 'TI' to the 'Dest' BasicBlock.  This method performs the
510 /// mechanics of updating SSA information and revectoring the branch.
511 ///
512 void CEE::ForwardSuccessorTo(TerminatorInst *TI, unsigned SuccNo,
513                              BasicBlock *Dest, RegionInfo &RI) {
514   // If there are any PHI nodes in the Dest BB, we must duplicate the entry
515   // in the PHI node for the old successor to now include an entry from the
516   // current basic block.
517   //
518   BasicBlock *OldSucc = TI->getSuccessor(SuccNo);
519   BasicBlock *BB = TI->getParent();
520
521   DOUT << "Forwarding branch in basic block %" << BB->getName()
522        << " from block %" << OldSucc->getName() << " to block %"
523        << Dest->getName() << "\n"
524        << "Before forwarding: " << *BB->getParent();
525
526   // Because we know that there cannot be critical edges in the flow graph, and
527   // that OldSucc has multiple outgoing edges, this means that Dest cannot have
528   // multiple incoming edges.
529   //
530 #ifndef NDEBUG
531   pred_iterator DPI = pred_begin(Dest); ++DPI;
532   assert(DPI == pred_end(Dest) && "Critical edge found!!");
533 #endif
534
535   // Loop over any PHI nodes in the destination, eliminating them, because they
536   // may only have one input.
537   //
538   while (PHINode *PN = dyn_cast<PHINode>(&Dest->front())) {
539     assert(PN->getNumIncomingValues() == 1 && "Crit edge found!");
540     // Eliminate the PHI node
541     PN->replaceAllUsesWith(PN->getIncomingValue(0));
542     Dest->getInstList().erase(PN);
543   }
544
545   // If there are values defined in the "OldSucc" basic block, we need to insert
546   // PHI nodes in the regions we are dealing with to emulate them.  This can
547   // insert dead phi nodes, but it is more trouble to see if they are used than
548   // to just blindly insert them.
549   //
550   if (EF->dominates(OldSucc, Dest)) {
551     // RegionExitBlocks - Find all of the blocks that are not dominated by Dest,
552     // but have predecessors that are.  Additionally, prune down the set to only
553     // include blocks that are dominated by OldSucc as well.
554     //
555     std::vector<BasicBlock*> RegionExitBlocks;
556     CalculateRegionExitBlocks(Dest, OldSucc, RegionExitBlocks);
557
558     for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end();
559          I != E; ++I)
560       if (I->getType() != Type::VoidTy) {
561         // Create and insert the PHI node into the top of Dest.
562         PHINode *NewPN = new PHINode(I->getType(), I->getName()+".fw_merge",
563                                      Dest->begin());
564         // There is definitely an edge from OldSucc... add the edge now
565         NewPN->addIncoming(I, OldSucc);
566
567         // There is also an edge from BB now, add the edge with the calculated
568         // value from the RI.
569         NewPN->addIncoming(getReplacementOrValue(I, RI), BB);
570
571         // Make everything in the Dest region use the new PHI node now...
572         ReplaceUsesOfValueInRegion(I, NewPN, Dest);
573
574         // Make sure that exits out of the region dominated by NewPN get PHI
575         // nodes that merge the values as appropriate.
576         InsertRegionExitMerges(NewPN, I, RegionExitBlocks);
577       }
578   }
579
580   // If there were PHI nodes in OldSucc, we need to remove the entry for this
581   // edge from the PHI node, and we need to replace any references to the PHI
582   // node with a new value.
583   //
584   for (BasicBlock::iterator I = OldSucc->begin(); isa<PHINode>(I); ) {
585     PHINode *PN = cast<PHINode>(I);
586
587     // Get the value flowing across the old edge and remove the PHI node entry
588     // for this edge: we are about to remove the edge!  Don't remove the PHI
589     // node yet though if this is the last edge into it.
590     Value *EdgeValue = PN->removeIncomingValue(BB, false);
591
592     // Make sure that anything that used to use PN now refers to EdgeValue
593     ReplaceUsesOfValueInRegion(PN, EdgeValue, Dest);
594
595     // If there is only one value left coming into the PHI node, replace the PHI
596     // node itself with the one incoming value left.
597     //
598     if (PN->getNumIncomingValues() == 1) {
599       assert(PN->getNumIncomingValues() == 1);
600       PN->replaceAllUsesWith(PN->getIncomingValue(0));
601       PN->getParent()->getInstList().erase(PN);
602       I = OldSucc->begin();
603     } else if (PN->getNumIncomingValues() == 0) {  // Nuke the PHI
604       // If we removed the last incoming value to this PHI, nuke the PHI node
605       // now.
606       PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
607       PN->getParent()->getInstList().erase(PN);
608       I = OldSucc->begin();
609     } else {
610       ++I;  // Otherwise, move on to the next PHI node
611     }
612   }
613
614   // Actually revector the branch now...
615   TI->setSuccessor(SuccNo, Dest);
616
617   // If we just introduced a critical edge in the flow graph, make sure to break
618   // it right away...
619   SplitCriticalEdge(TI, SuccNo, this);
620
621   // Make sure that we don't introduce critical edges from oldsucc now!
622   for (unsigned i = 0, e = OldSucc->getTerminator()->getNumSuccessors();
623        i != e; ++i)
624     SplitCriticalEdge(OldSucc->getTerminator(), i, this);
625
626   // Since we invalidated the CFG, recalculate the dominator set so that it is
627   // useful for later processing!
628   // FIXME: This is much worse than it really should be!
629   //EF->recalculate();
630
631   DOUT << "After forwarding: " << *BB->getParent();
632 }
633
634 /// ReplaceUsesOfValueInRegion - This method replaces all uses of Orig with uses
635 /// of New.  It only affects instructions that are defined in basic blocks that
636 /// are dominated by Head.
637 ///
638 void CEE::ReplaceUsesOfValueInRegion(Value *Orig, Value *New,
639                                      BasicBlock *RegionDominator) {
640   assert(Orig != New && "Cannot replace value with itself");
641   std::vector<Instruction*> InstsToChange;
642   std::vector<PHINode*>     PHIsToChange;
643   InstsToChange.reserve(Orig->getNumUses());
644
645   // Loop over instructions adding them to InstsToChange vector, this allows us
646   // an easy way to avoid invalidating the use_iterator at a bad time.
647   for (Value::use_iterator I = Orig->use_begin(), E = Orig->use_end();
648        I != E; ++I)
649     if (Instruction *User = dyn_cast<Instruction>(*I))
650       if (EF->dominates(RegionDominator, User->getParent()))
651         InstsToChange.push_back(User);
652       else if (PHINode *PN = dyn_cast<PHINode>(User)) {
653         PHIsToChange.push_back(PN);
654       }
655
656   // PHIsToChange contains PHI nodes that use Orig that do not live in blocks
657   // dominated by orig.  If the block the value flows in from is dominated by
658   // RegionDominator, then we rewrite the PHI
659   for (unsigned i = 0, e = PHIsToChange.size(); i != e; ++i) {
660     PHINode *PN = PHIsToChange[i];
661     for (unsigned j = 0, e = PN->getNumIncomingValues(); j != e; ++j)
662       if (PN->getIncomingValue(j) == Orig &&
663           EF->dominates(RegionDominator, PN->getIncomingBlock(j)))
664         PN->setIncomingValue(j, New);
665   }
666
667   // Loop over the InstsToChange list, replacing all uses of Orig with uses of
668   // New.  This list contains all of the instructions in our region that use
669   // Orig.
670   for (unsigned i = 0, e = InstsToChange.size(); i != e; ++i)
671     if (PHINode *PN = dyn_cast<PHINode>(InstsToChange[i])) {
672       // PHINodes must be handled carefully.  If the PHI node itself is in the
673       // region, we have to make sure to only do the replacement for incoming
674       // values that correspond to basic blocks in the region.
675       for (unsigned j = 0, e = PN->getNumIncomingValues(); j != e; ++j)
676         if (PN->getIncomingValue(j) == Orig &&
677             EF->dominates(RegionDominator, PN->getIncomingBlock(j)))
678           PN->setIncomingValue(j, New);
679
680     } else {
681       InstsToChange[i]->replaceUsesOfWith(Orig, New);
682     }
683 }
684
685 static void CalcRegionExitBlocks(BasicBlock *Header, BasicBlock *BB,
686                                  std::set<BasicBlock*> &Visited,
687                                  ETForest &EF,
688                                  std::vector<BasicBlock*> &RegionExitBlocks) {
689   if (Visited.count(BB)) return;
690   Visited.insert(BB);
691
692   if (EF.dominates(Header, BB)) {  // Block in the region, recursively traverse
693     for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
694       CalcRegionExitBlocks(Header, *I, Visited, EF, RegionExitBlocks);
695   } else {
696     // Header does not dominate this block, but we have a predecessor that does
697     // dominate us.  Add ourself to the list.
698     RegionExitBlocks.push_back(BB);
699   }
700 }
701
702 /// CalculateRegionExitBlocks - Find all of the blocks that are not dominated by
703 /// BB, but have predecessors that are.  Additionally, prune down the set to
704 /// only include blocks that are dominated by OldSucc as well.
705 ///
706 void CEE::CalculateRegionExitBlocks(BasicBlock *BB, BasicBlock *OldSucc,
707                                     std::vector<BasicBlock*> &RegionExitBlocks){
708   std::set<BasicBlock*> Visited;  // Don't infinite loop
709
710   // Recursively calculate blocks we are interested in...
711   CalcRegionExitBlocks(BB, BB, Visited, *EF, RegionExitBlocks);
712
713   // Filter out blocks that are not dominated by OldSucc...
714   for (unsigned i = 0; i != RegionExitBlocks.size(); ) {
715     if (EF->dominates(OldSucc, RegionExitBlocks[i]))
716       ++i;  // Block is ok, keep it.
717     else {
718       // Move to end of list...
719       std::swap(RegionExitBlocks[i], RegionExitBlocks.back());
720       RegionExitBlocks.pop_back();        // Nuke the end
721     }
722   }
723 }
724
725 void CEE::InsertRegionExitMerges(PHINode *BBVal, Instruction *OldVal,
726                              const std::vector<BasicBlock*> &RegionExitBlocks) {
727   assert(BBVal->getType() == OldVal->getType() && "Should be derived values!");
728   BasicBlock *BB = BBVal->getParent();
729
730   // Loop over all of the blocks we have to place PHIs in, doing it.
731   for (unsigned i = 0, e = RegionExitBlocks.size(); i != e; ++i) {
732     BasicBlock *FBlock = RegionExitBlocks[i];  // Block on the frontier
733
734     // Create the new PHI node
735     PHINode *NewPN = new PHINode(BBVal->getType(),
736                                  OldVal->getName()+".fw_frontier",
737                                  FBlock->begin());
738
739     // Add an incoming value for every predecessor of the block...
740     for (pred_iterator PI = pred_begin(FBlock), PE = pred_end(FBlock);
741          PI != PE; ++PI) {
742       // If the incoming edge is from the region dominated by BB, use BBVal,
743       // otherwise use OldVal.
744       NewPN->addIncoming(EF->dominates(BB, *PI) ? BBVal : OldVal, *PI);
745     }
746
747     // Now make everyone dominated by this block use this new value!
748     ReplaceUsesOfValueInRegion(OldVal, NewPN, FBlock);
749   }
750 }
751
752
753
754 // BuildRankMap - This method builds the rank map data structure which gives
755 // each instruction/value in the function a value based on how early it appears
756 // in the function.  We give constants and globals rank 0, arguments are
757 // numbered starting at one, and instructions are numbered in reverse post-order
758 // from where the arguments leave off.  This gives instructions in loops higher
759 // values than instructions not in loops.
760 //
761 void CEE::BuildRankMap(Function &F) {
762   unsigned Rank = 1;  // Skip rank zero.
763
764   // Number the arguments...
765   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
766     RankMap[I] = Rank++;
767
768   // Number the instructions in reverse post order...
769   ReversePostOrderTraversal<Function*> RPOT(&F);
770   for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
771          E = RPOT.end(); I != E; ++I)
772     for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end();
773          BBI != E; ++BBI)
774       if (BBI->getType() != Type::VoidTy)
775         RankMap[BBI] = Rank++;
776 }
777
778
779 // PropagateBranchInfo - When this method is invoked, we need to propagate
780 // information derived from the branch condition into the true and false
781 // branches of BI.  Since we know that there aren't any critical edges in the
782 // flow graph, this can proceed unconditionally.
783 //
784 void CEE::PropagateBranchInfo(BranchInst *BI) {
785   assert(BI->isConditional() && "Must be a conditional branch!");
786
787   // Propagate information into the true block...
788   //
789   PropagateEquality(BI->getCondition(), ConstantInt::getTrue(),
790                     getRegionInfo(BI->getSuccessor(0)));
791
792   // Propagate information into the false block...
793   //
794   PropagateEquality(BI->getCondition(), ConstantInt::getFalse(),
795                     getRegionInfo(BI->getSuccessor(1)));
796 }
797
798
799 // PropagateSwitchInfo - We need to propagate the value tested by the
800 // switch statement through each case block.
801 //
802 void CEE::PropagateSwitchInfo(SwitchInst *SI) {
803   // Propagate information down each of our non-default case labels.  We
804   // don't yet propagate information down the default label, because a
805   // potentially large number of inequality constraints provide less
806   // benefit per unit work than a single equality constraint.
807   //
808   Value *cond = SI->getCondition();
809   for (unsigned i = 1; i < SI->getNumSuccessors(); ++i)
810     PropagateEquality(cond, SI->getSuccessorValue(i),
811                       getRegionInfo(SI->getSuccessor(i)));
812 }
813
814
815 // PropagateEquality - If we discover that two values are equal to each other in
816 // a specified region, propagate this knowledge recursively.
817 //
818 void CEE::PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI) {
819   if (Op0 == Op1) return;  // Gee whiz. Are these really equal each other?
820
821   if (isa<Constant>(Op0))  // Make sure the constant is always Op1
822     std::swap(Op0, Op1);
823
824   // Make sure we don't already know these are equal, to avoid infinite loops...
825   ValueInfo &VI = RI.getValueInfo(Op0);
826
827   // Get information about the known relationship between Op0 & Op1
828   Relation &KnownRelation = VI.getRelation(Op1);
829
830   // If we already know they're equal, don't reprocess...
831   if (KnownRelation.getRelation() == FCmpInst::FCMP_OEQ ||
832       KnownRelation.getRelation() == ICmpInst::ICMP_EQ)
833     return;
834
835   // If this is boolean, check to see if one of the operands is a constant.  If
836   // it's a constant, then see if the other one is one of a setcc instruction,
837   // an AND, OR, or XOR instruction.
838   //
839   ConstantInt *CB = dyn_cast<ConstantInt>(Op1);
840   if (CB && Op1->getType() == Type::Int1Ty) {
841     if (Instruction *Inst = dyn_cast<Instruction>(Op0)) {
842       // If we know that this instruction is an AND instruction, and the 
843       // result is true, this means that both operands to the OR are known 
844       // to be true as well.
845       //
846       if (CB->getZExtValue() && Inst->getOpcode() == Instruction::And) {
847         PropagateEquality(Inst->getOperand(0), CB, RI);
848         PropagateEquality(Inst->getOperand(1), CB, RI);
849       }
850
851       // If we know that this instruction is an OR instruction, and the result
852       // is false, this means that both operands to the OR are know to be 
853       // false as well.
854       //
855       if (!CB->getZExtValue() && Inst->getOpcode() == Instruction::Or) {
856         PropagateEquality(Inst->getOperand(0), CB, RI);
857         PropagateEquality(Inst->getOperand(1), CB, RI);
858       }
859
860       // If we know that this instruction is a NOT instruction, we know that 
861       // the operand is known to be the inverse of whatever the current 
862       // value is.
863       //
864       if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(Inst))
865         if (BinaryOperator::isNot(BOp))
866           PropagateEquality(BinaryOperator::getNotArgument(BOp),
867                             ConstantInt::get(Type::Int1Ty, 
868                                              !CB->getZExtValue()), RI);
869
870       // If we know the value of a FCmp instruction, propagate the information
871       // about the relation into this region as well.
872       //
873       if (FCmpInst *FCI = dyn_cast<FCmpInst>(Inst)) {
874         if (CB->getZExtValue()) {  // If we know the condition is true...
875           // Propagate info about the LHS to the RHS & RHS to LHS
876           PropagateRelation(FCI->getPredicate(), FCI->getOperand(0),
877                             FCI->getOperand(1), RI);
878           PropagateRelation(FCI->getSwappedPredicate(),
879                             FCI->getOperand(1), FCI->getOperand(0), RI);
880
881         } else {               // If we know the condition is false...
882           // We know the opposite of the condition is true...
883           FCmpInst::Predicate C = FCI->getInversePredicate();
884
885           PropagateRelation(C, FCI->getOperand(0), FCI->getOperand(1), RI);
886           PropagateRelation(FCmpInst::getSwappedPredicate(C),
887                             FCI->getOperand(1), FCI->getOperand(0), RI);
888         }
889       }
890     
891       // If we know the value of a ICmp instruction, propagate the information
892       // about the relation into this region as well.
893       //
894       if (ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
895         if (CB->getZExtValue()) { // If we know the condition is true...
896           // Propagate info about the LHS to the RHS & RHS to LHS
897           PropagateRelation(ICI->getPredicate(), ICI->getOperand(0),
898                             ICI->getOperand(1), RI);
899           PropagateRelation(ICI->getSwappedPredicate(), ICI->getOperand(1),
900                             ICI->getOperand(1), RI);
901
902         } else {               // If we know the condition is false ...
903           // We know the opposite of the condition is true...
904           ICmpInst::Predicate C = ICI->getInversePredicate();
905
906           PropagateRelation(C, ICI->getOperand(0), ICI->getOperand(1), RI);
907           PropagateRelation(ICmpInst::getSwappedPredicate(C),
908                             ICI->getOperand(1), ICI->getOperand(0), RI);
909         }
910       }
911     }
912   }
913
914   // Propagate information about Op0 to Op1 & visa versa
915   PropagateRelation(ICmpInst::ICMP_EQ, Op0, Op1, RI);
916   PropagateRelation(ICmpInst::ICMP_EQ, Op1, Op0, RI);
917   PropagateRelation(FCmpInst::FCMP_OEQ, Op0, Op1, RI);
918   PropagateRelation(FCmpInst::FCMP_OEQ, Op1, Op0, RI);
919 }
920
921
922 // PropagateRelation - We know that the specified relation is true in all of the
923 // blocks in the specified region.  Propagate the information about Op0 and
924 // anything derived from it into this region.
925 //
926 void CEE::PropagateRelation(unsigned Opcode, Value *Op0,
927                             Value *Op1, RegionInfo &RI) {
928   assert(Op0->getType() == Op1->getType() && "Equal types expected!");
929
930   // Constants are already pretty well understood.  We will apply information
931   // about the constant to Op1 in another call to PropagateRelation.
932   //
933   if (isa<Constant>(Op0)) return;
934
935   // Get the region information for this block to update...
936   ValueInfo &VI = RI.getValueInfo(Op0);
937
938   // Get information about the known relationship between Op0 & Op1
939   Relation &Op1R = VI.getRelation(Op1);
940
941   // Quick bailout for common case if we are reprocessing an instruction...
942   if (Op1R.getRelation() == Opcode)
943     return;
944
945   // If we already have information that contradicts the current information we
946   // are propagating, ignore this info.  Something bad must have happened!
947   //
948   if (Op1R.contradicts(Opcode, VI)) {
949     Op1R.contradicts(Opcode, VI);
950     cerr << "Contradiction found for opcode: "
951          << ((isa<ICmpInst>(Op0)||isa<ICmpInst>(Op1)) ? 
952                   Instruction::getOpcodeName(Instruction::ICmp) :
953                   Instruction::getOpcodeName(Opcode))
954          << "\n";
955     Op1R.print(*cerr.stream());
956     return;
957   }
958
959   // If the information propagated is new, then we want process the uses of this
960   // instruction to propagate the information down to them.
961   //
962   if (Op1R.incorporate(Opcode, VI))
963     UpdateUsersOfValue(Op0, RI);
964 }
965
966
967 // UpdateUsersOfValue - The information about V in this region has been updated.
968 // Propagate this to all consumers of the value.
969 //
970 void CEE::UpdateUsersOfValue(Value *V, RegionInfo &RI) {
971   for (Value::use_iterator I = V->use_begin(), E = V->use_end();
972        I != E; ++I)
973     if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
974       // If this is an instruction using a value that we know something about,
975       // try to propagate information to the value produced by the
976       // instruction.  We can only do this if it is an instruction we can
977       // propagate information for (a setcc for example), and we only WANT to
978       // do this if the instruction dominates this region.
979       //
980       // If the instruction doesn't dominate this region, then it cannot be
981       // used in this region and we don't care about it.  If the instruction
982       // is IN this region, then we will simplify the instruction before we
983       // get to uses of it anyway, so there is no reason to bother with it
984       // here.  This check is also effectively checking to make sure that Inst
985       // is in the same function as our region (in case V is a global f.e.).
986       //
987       if (EF->properlyDominates(Inst->getParent(), RI.getEntryBlock()))
988         IncorporateInstruction(Inst, RI);
989     }
990 }
991
992 // IncorporateInstruction - We just updated the information about one of the
993 // operands to the specified instruction.  Update the information about the
994 // value produced by this instruction
995 //
996 void CEE::IncorporateInstruction(Instruction *Inst, RegionInfo &RI) {
997   if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
998     // See if we can figure out a result for this instruction...
999     Relation::KnownResult Result = getCmpResult(CI, RI);
1000     if (Result != Relation::Unknown) {
1001       PropagateEquality(CI, ConstantInt::get(Type::Int1Ty, Result != 0), RI);
1002     }
1003   }
1004 }
1005
1006
1007 // ComputeReplacements - Some values are known to be equal to other values in a
1008 // region.  For example if there is a comparison of equality between a variable
1009 // X and a constant C, we can replace all uses of X with C in the region we are
1010 // interested in.  We generalize this replacement to replace variables with
1011 // other variables if they are equal and there is a variable with lower rank
1012 // than the current one.  This offers a canonicalizing property that exposes
1013 // more redundancies for later transformations to take advantage of.
1014 //
1015 void CEE::ComputeReplacements(RegionInfo &RI) {
1016   // Loop over all of the values in the region info map...
1017   for (RegionInfo::iterator I = RI.begin(), E = RI.end(); I != E; ++I) {
1018     ValueInfo &VI = I->second;
1019
1020     // If we know that this value is a particular constant, set Replacement to
1021     // the constant...
1022     Value *Replacement = 0;
1023     const APInt * Rplcmnt = VI.getBounds().getSingleElement();
1024     if (Rplcmnt)
1025       Replacement = ConstantInt::get(*Rplcmnt);
1026
1027     // If this value is not known to be some constant, figure out the lowest
1028     // rank value that it is known to be equal to (if anything).
1029     //
1030     if (Replacement == 0) {
1031       // Find out if there are any equality relationships with values of lower
1032       // rank than VI itself...
1033       unsigned MinRank = getRank(I->first);
1034
1035       // Loop over the relationships known about Op0.
1036       const std::vector<Relation> &Relationships = VI.getRelationships();
1037       for (unsigned i = 0, e = Relationships.size(); i != e; ++i)
1038         if (Relationships[i].getRelation() == FCmpInst::FCMP_OEQ) {
1039           unsigned R = getRank(Relationships[i].getValue());
1040           if (R < MinRank) {
1041             MinRank = R;
1042             Replacement = Relationships[i].getValue();
1043           }
1044         }
1045         else if (Relationships[i].getRelation() == ICmpInst::ICMP_EQ) {
1046           unsigned R = getRank(Relationships[i].getValue());
1047           if (R < MinRank) {
1048             MinRank = R;
1049             Replacement = Relationships[i].getValue();
1050           }
1051         }
1052     }
1053
1054     // If we found something to replace this value with, keep track of it.
1055     if (Replacement)
1056       VI.setReplacement(Replacement);
1057   }
1058 }
1059
1060 // SimplifyBasicBlock - Given information about values in region RI, simplify
1061 // the instructions in the specified basic block.
1062 //
1063 bool CEE::SimplifyBasicBlock(BasicBlock &BB, const RegionInfo &RI) {
1064   bool Changed = false;
1065   for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) {
1066     Instruction *Inst = I++;
1067
1068     // Convert instruction arguments to canonical forms...
1069     Changed |= SimplifyInstruction(Inst, RI);
1070
1071     if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
1072       // Try to simplify a setcc instruction based on inherited information
1073       Relation::KnownResult Result = getCmpResult(CI, RI);
1074       if (Result != Relation::Unknown) {
1075         DEBUG(cerr << "Replacing icmp with " << Result
1076                    << " constant: " << *CI);
1077
1078         CI->replaceAllUsesWith(ConstantInt::get(Type::Int1Ty, (bool)Result));
1079         // The instruction is now dead, remove it from the program.
1080         CI->getParent()->getInstList().erase(CI);
1081         ++NumCmpRemoved;
1082         Changed = true;
1083       }
1084     }
1085   }
1086
1087   return Changed;
1088 }
1089
1090 // SimplifyInstruction - Inspect the operands of the instruction, converting
1091 // them to their canonical form if possible.  This takes care of, for example,
1092 // replacing a value 'X' with a constant 'C' if the instruction in question is
1093 // dominated by a true seteq 'X', 'C'.
1094 //
1095 bool CEE::SimplifyInstruction(Instruction *I, const RegionInfo &RI) {
1096   bool Changed = false;
1097
1098   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1099     if (const ValueInfo *VI = RI.requestValueInfo(I->getOperand(i)))
1100       if (Value *Repl = VI->getReplacement()) {
1101         // If we know if a replacement with lower rank than Op0, make the
1102         // replacement now.
1103         DOUT << "In Inst: " << *I << "  Replacing operand #" << i
1104              << " with " << *Repl << "\n";
1105         I->setOperand(i, Repl);
1106         Changed = true;
1107         ++NumOperandsCann;
1108       }
1109
1110   return Changed;
1111 }
1112
1113 // getCmpResult - Try to simplify a cmp instruction based on information
1114 // inherited from a dominating icmp instruction.  V is one of the operands to
1115 // the icmp instruction, and VI is the set of information known about it.  We
1116 // take two cases into consideration here.  If the comparison is against a
1117 // constant value, we can use the constant range to see if the comparison is
1118 // possible to succeed.  If it is not a comparison against a constant, we check
1119 // to see if there is a known relationship between the two values.  If so, we
1120 // may be able to eliminate the check.
1121 //
1122 Relation::KnownResult CEE::getCmpResult(CmpInst *CI,
1123                                         const RegionInfo &RI) {
1124   Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1125   unsigned short predicate = CI->getPredicate();
1126
1127   if (isa<Constant>(Op0)) {
1128     if (isa<Constant>(Op1)) {
1129       if (Constant *Result = ConstantFoldInstruction(CI)) {
1130         // Wow, this is easy, directly eliminate the ICmpInst.
1131         DEBUG(cerr << "Replacing cmp with constant fold: " << *CI);
1132         return cast<ConstantInt>(Result)->getZExtValue()
1133           ? Relation::KnownTrue : Relation::KnownFalse;
1134       }
1135     } else {
1136       // We want to swap this instruction so that operand #0 is the constant.
1137       std::swap(Op0, Op1);
1138       if (isa<ICmpInst>(CI))
1139         predicate = cast<ICmpInst>(CI)->getSwappedPredicate();
1140       else
1141         predicate = cast<FCmpInst>(CI)->getSwappedPredicate();
1142     }
1143   }
1144
1145   // Try to figure out what the result of this comparison will be...
1146   Relation::KnownResult Result = Relation::Unknown;
1147
1148   // We have to know something about the relationship to prove anything...
1149   if (const ValueInfo *Op0VI = RI.requestValueInfo(Op0)) {
1150
1151     // At this point, we know that if we have a constant argument that it is in
1152     // Op1.  Check to see if we know anything about comparing value with a
1153     // constant, and if we can use this info to fold the icmp.
1154     //
1155     if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) {
1156       // Check to see if we already know the result of this comparison...
1157       ICmpInst::Predicate ipred = ICmpInst::Predicate(predicate);
1158       ConstantRange R = ICmpInst::makeConstantRange(ipred, C->getValue());
1159       ConstantRange Int = R.intersectWith(Op0VI->getBounds());
1160
1161       // If the intersection of the two ranges is empty, then the condition
1162       // could never be true!
1163       //
1164       if (Int.isEmptySet()) {
1165         Result = Relation::KnownFalse;
1166
1167       // Otherwise, if VI.getBounds() (the possible values) is a subset of R
1168       // (the allowed values) then we know that the condition must always be
1169       // true!
1170       //
1171       } else if (Int == Op0VI->getBounds()) {
1172         Result = Relation::KnownTrue;
1173       }
1174     } else {
1175       // If we are here, we know that the second argument is not a constant
1176       // integral.  See if we know anything about Op0 & Op1 that allows us to
1177       // fold this anyway.
1178       //
1179       // Do we have value information about Op0 and a relation to Op1?
1180       if (const Relation *Op2R = Op0VI->requestRelation(Op1))
1181         Result = Op2R->getImpliedResult(predicate);
1182     }
1183   }
1184   return Result;
1185 }
1186
1187 //===----------------------------------------------------------------------===//
1188 //  Relation Implementation
1189 //===----------------------------------------------------------------------===//
1190
1191 // contradicts - Return true if the relationship specified by the operand
1192 // contradicts already known information.
1193 //
1194 bool Relation::contradicts(unsigned Op,
1195                            const ValueInfo &VI) const {
1196   assert (Op != Instruction::Add && "Invalid relation argument!");
1197
1198   // If this is a relationship with a constant, make sure that this relationship
1199   // does not contradict properties known about the bounds of the constant.
1200   //
1201   if (ConstantInt *C = dyn_cast<ConstantInt>(Val))
1202     if (Op >= ICmpInst::FIRST_ICMP_PREDICATE && 
1203         Op <= ICmpInst::LAST_ICMP_PREDICATE) {
1204       ICmpInst::Predicate ipred = ICmpInst::Predicate(Op);
1205       if (ICmpInst::makeConstantRange(ipred, C->getValue())
1206                     .intersectWith(VI.getBounds()).isEmptySet())
1207         return true;
1208     }
1209
1210   switch (Rel) {
1211   default: assert(0 && "Unknown Relationship code!");
1212   case Instruction::Add: return false;  // Nothing known, nothing contradicts
1213   case ICmpInst::ICMP_EQ:
1214     return Op == ICmpInst::ICMP_ULT || Op == ICmpInst::ICMP_SLT ||
1215            Op == ICmpInst::ICMP_UGT || Op == ICmpInst::ICMP_SGT ||
1216            Op == ICmpInst::ICMP_NE;
1217   case ICmpInst::ICMP_NE:  return Op == ICmpInst::ICMP_EQ;
1218   case ICmpInst::ICMP_ULE:
1219   case ICmpInst::ICMP_SLE: return Op == ICmpInst::ICMP_UGT ||
1220                                   Op == ICmpInst::ICMP_SGT;
1221   case ICmpInst::ICMP_UGE:
1222   case ICmpInst::ICMP_SGE: return Op == ICmpInst::ICMP_ULT ||
1223                                   Op == ICmpInst::ICMP_SLT;
1224   case ICmpInst::ICMP_ULT:
1225   case ICmpInst::ICMP_SLT:
1226     return Op == ICmpInst::ICMP_EQ  || Op == ICmpInst::ICMP_UGT ||
1227            Op == ICmpInst::ICMP_SGT || Op == ICmpInst::ICMP_UGE ||
1228            Op == ICmpInst::ICMP_SGE;
1229   case ICmpInst::ICMP_UGT:
1230   case ICmpInst::ICMP_SGT:
1231     return Op == ICmpInst::ICMP_EQ  || Op == ICmpInst::ICMP_ULT ||
1232            Op == ICmpInst::ICMP_SLT || Op == ICmpInst::ICMP_ULE ||
1233            Op == ICmpInst::ICMP_SLE;
1234   case FCmpInst::FCMP_OEQ:
1235     return Op == FCmpInst::FCMP_OLT || Op == FCmpInst::FCMP_OGT ||
1236            Op == FCmpInst::FCMP_ONE;
1237   case FCmpInst::FCMP_ONE: return Op == FCmpInst::FCMP_OEQ;
1238   case FCmpInst::FCMP_OLE: return Op == FCmpInst::FCMP_OGT;
1239   case FCmpInst::FCMP_OGE: return Op == FCmpInst::FCMP_OLT;
1240   case FCmpInst::FCMP_OLT:
1241     return Op == FCmpInst::FCMP_OEQ || Op == FCmpInst::FCMP_OGT ||
1242            Op == FCmpInst::FCMP_OGE;
1243   case FCmpInst::FCMP_OGT:
1244     return Op == FCmpInst::FCMP_OEQ || Op == FCmpInst::FCMP_OLT ||
1245            Op == FCmpInst::FCMP_OLE;
1246   }
1247 }
1248
1249 // incorporate - Incorporate information in the argument into this relation
1250 // entry.  This assumes that the information doesn't contradict itself.  If any
1251 // new information is gained, true is returned, otherwise false is returned to
1252 // indicate that nothing was updated.
1253 //
1254 bool Relation::incorporate(unsigned Op, ValueInfo &VI) {
1255   assert(!contradicts(Op, VI) &&
1256          "Cannot incorporate contradictory information!");
1257
1258   // If this is a relationship with a constant, make sure that we update the
1259   // range that is possible for the value to have...
1260   //
1261   if (ConstantInt *C = dyn_cast<ConstantInt>(Val))
1262     if (Op >= ICmpInst::FIRST_ICMP_PREDICATE && 
1263         Op <= ICmpInst::LAST_ICMP_PREDICATE) {
1264       ICmpInst::Predicate ipred = ICmpInst::Predicate(Op);
1265       VI.getBounds() = 
1266         ICmpInst::makeConstantRange(ipred, C->getValue())
1267                   .intersectWith(VI.getBounds());
1268     }
1269
1270   switch (Rel) {
1271   default: assert(0 && "Unknown prior value!");
1272   case Instruction::Add:   Rel = Op; return true;
1273   case ICmpInst::ICMP_EQ:
1274   case ICmpInst::ICMP_NE:
1275   case ICmpInst::ICMP_ULT:
1276   case ICmpInst::ICMP_SLT:
1277   case ICmpInst::ICMP_UGT:
1278   case ICmpInst::ICMP_SGT: return false;  // Nothing is more precise
1279   case ICmpInst::ICMP_ULE:
1280   case ICmpInst::ICMP_SLE:
1281     if (Op == ICmpInst::ICMP_EQ  || Op == ICmpInst::ICMP_ULT ||
1282         Op == ICmpInst::ICMP_SLT) {
1283       Rel = Op;
1284       return true;
1285     } else if (Op == ICmpInst::ICMP_NE) {
1286       Rel = Rel == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_ULT :
1287             ICmpInst::ICMP_SLT;
1288       return true;
1289     }
1290     return false;
1291   case ICmpInst::ICMP_UGE:
1292   case ICmpInst::ICMP_SGE:
1293     if (Op == ICmpInst::ICMP_EQ  || ICmpInst::ICMP_UGT ||
1294         Op == ICmpInst::ICMP_SGT) {
1295       Rel = Op;
1296       return true;
1297     } else if (Op == ICmpInst::ICMP_NE) {
1298       Rel = Rel == ICmpInst::ICMP_UGE ? ICmpInst::ICMP_UGT :
1299             ICmpInst::ICMP_SGT;
1300       return true;
1301     }
1302     return false;
1303   case FCmpInst::FCMP_OEQ: return false;  // Nothing is more precise
1304   case FCmpInst::FCMP_ONE: return false;  // Nothing is more precise
1305   case FCmpInst::FCMP_OLT: return false;  // Nothing is more precise
1306   case FCmpInst::FCMP_OGT: return false;  // Nothing is more precise
1307   case FCmpInst::FCMP_OLE:
1308     if (Op == FCmpInst::FCMP_OEQ || Op == FCmpInst::FCMP_OLT) {
1309       Rel = Op;
1310       return true;
1311     } else if (Op == FCmpInst::FCMP_ONE) {
1312       Rel = FCmpInst::FCMP_OLT;
1313       return true;
1314     }
1315     return false;
1316   case FCmpInst::FCMP_OGE: 
1317     return Op == FCmpInst::FCMP_OLT;
1318     if (Op == FCmpInst::FCMP_OEQ || Op == FCmpInst::FCMP_OGT) {
1319       Rel = Op;
1320       return true;
1321     } else if (Op == FCmpInst::FCMP_ONE) {
1322       Rel = FCmpInst::FCMP_OGT;
1323       return true;
1324     }
1325     return false;
1326   }
1327 }
1328
1329 // getImpliedResult - If this relationship between two values implies that
1330 // the specified relationship is true or false, return that.  If we cannot
1331 // determine the result required, return Unknown.
1332 //
1333 Relation::KnownResult
1334 Relation::getImpliedResult(unsigned Op) const {
1335   if (Rel == Op) return KnownTrue;
1336   if (Op >= ICmpInst::FIRST_ICMP_PREDICATE && 
1337       Op <= ICmpInst::LAST_ICMP_PREDICATE) {
1338     if (Rel == unsigned(ICmpInst::getInversePredicate(ICmpInst::Predicate(Op))))
1339       return KnownFalse;
1340   } else if (Op <= FCmpInst::LAST_FCMP_PREDICATE) {
1341     if (Rel == unsigned(FCmpInst::getInversePredicate(FCmpInst::Predicate(Op))))
1342     return KnownFalse;
1343   }
1344
1345   switch (Rel) {
1346   default: assert(0 && "Unknown prior value!");
1347   case ICmpInst::ICMP_EQ:
1348     if (Op == ICmpInst::ICMP_ULE || Op == ICmpInst::ICMP_SLE || 
1349         Op == ICmpInst::ICMP_UGE || Op == ICmpInst::ICMP_SGE) return KnownTrue;
1350     if (Op == ICmpInst::ICMP_ULT || Op == ICmpInst::ICMP_SLT || 
1351         Op == ICmpInst::ICMP_UGT || Op == ICmpInst::ICMP_SGT) return KnownFalse;
1352     break;
1353   case ICmpInst::ICMP_ULT:
1354   case ICmpInst::ICMP_SLT:
1355     if (Op == ICmpInst::ICMP_ULE || Op == ICmpInst::ICMP_SLE ||
1356         Op == ICmpInst::ICMP_NE) return KnownTrue;
1357     if (Op == ICmpInst::ICMP_EQ) return KnownFalse;
1358     break;
1359   case ICmpInst::ICMP_UGT:
1360   case ICmpInst::ICMP_SGT:
1361     if (Op == ICmpInst::ICMP_UGE || Op == ICmpInst::ICMP_SGE ||
1362         Op == ICmpInst::ICMP_NE) return KnownTrue;
1363     if (Op == ICmpInst::ICMP_EQ) return KnownFalse;
1364     break;
1365   case FCmpInst::FCMP_OEQ:
1366     if (Op == FCmpInst::FCMP_OLE || Op == FCmpInst::FCMP_OGE) return KnownTrue;
1367     if (Op == FCmpInst::FCMP_OLT || Op == FCmpInst::FCMP_OGT) return KnownFalse;
1368     break;
1369   case FCmpInst::FCMP_OLT:
1370     if (Op == FCmpInst::FCMP_ONE || Op == FCmpInst::FCMP_OLE) return KnownTrue;
1371     if (Op == FCmpInst::FCMP_OEQ) return KnownFalse;
1372     break;
1373   case FCmpInst::FCMP_OGT:
1374     if (Op == FCmpInst::FCMP_ONE || Op == FCmpInst::FCMP_OGE) return KnownTrue;
1375     if (Op == FCmpInst::FCMP_OEQ) return KnownFalse;
1376     break;
1377   case ICmpInst::ICMP_NE:
1378   case ICmpInst::ICMP_SLE:
1379   case ICmpInst::ICMP_ULE:
1380   case ICmpInst::ICMP_UGE:
1381   case ICmpInst::ICMP_SGE:
1382   case FCmpInst::FCMP_ONE:
1383   case FCmpInst::FCMP_OLE:
1384   case FCmpInst::FCMP_OGE:
1385   case FCmpInst::FCMP_FALSE:
1386   case FCmpInst::FCMP_ORD:
1387   case FCmpInst::FCMP_UNO:
1388   case FCmpInst::FCMP_UEQ:
1389   case FCmpInst::FCMP_UGT:
1390   case FCmpInst::FCMP_UGE:
1391   case FCmpInst::FCMP_ULT:
1392   case FCmpInst::FCMP_ULE:
1393   case FCmpInst::FCMP_UNE:
1394   case FCmpInst::FCMP_TRUE:
1395     break;
1396   }
1397   return Unknown;
1398 }
1399
1400
1401 //===----------------------------------------------------------------------===//
1402 // Printing Support...
1403 //===----------------------------------------------------------------------===//
1404
1405 // print - Implement the standard print form to print out analysis information.
1406 void CEE::print(std::ostream &O, const Module *M) const {
1407   O << "\nPrinting Correlated Expression Info:\n";
1408   for (std::map<BasicBlock*, RegionInfo>::const_iterator I =
1409          RegionInfoMap.begin(), E = RegionInfoMap.end(); I != E; ++I)
1410     I->second.print(O);
1411 }
1412
1413 // print - Output information about this region...
1414 void RegionInfo::print(std::ostream &OS) const {
1415   if (ValueMap.empty()) return;
1416
1417   OS << " RegionInfo for basic block: " << BB->getName() << "\n";
1418   for (std::map<Value*, ValueInfo>::const_iterator
1419          I = ValueMap.begin(), E = ValueMap.end(); I != E; ++I)
1420     I->second.print(OS, I->first);
1421   OS << "\n";
1422 }
1423
1424 // print - Output information about this value relation...
1425 void ValueInfo::print(std::ostream &OS, Value *V) const {
1426   if (Relationships.empty()) return;
1427
1428   if (V) {
1429     OS << "  ValueInfo for: ";
1430     WriteAsOperand(OS, V);
1431   }
1432   OS << "\n    Bounds = " << Bounds << "\n";
1433   if (Replacement) {
1434     OS << "    Replacement = ";
1435     WriteAsOperand(OS, Replacement);
1436     OS << "\n";
1437   }
1438   for (unsigned i = 0, e = Relationships.size(); i != e; ++i)
1439     Relationships[i].print(OS);
1440 }
1441
1442 // print - Output this relation to the specified stream
1443 void Relation::print(std::ostream &OS) const {
1444   OS << "    is ";
1445   switch (Rel) {
1446   default:           OS << "*UNKNOWN*"; break;
1447   case ICmpInst::ICMP_EQ:
1448   case FCmpInst::FCMP_ORD:
1449   case FCmpInst::FCMP_UEQ:
1450   case FCmpInst::FCMP_OEQ: OS << "== "; break;
1451   case ICmpInst::ICMP_NE:
1452   case FCmpInst::FCMP_UNO:
1453   case FCmpInst::FCMP_UNE:
1454   case FCmpInst::FCMP_ONE: OS << "!= "; break;
1455   case ICmpInst::ICMP_ULT:
1456   case ICmpInst::ICMP_SLT:
1457   case FCmpInst::FCMP_ULT:
1458   case FCmpInst::FCMP_OLT: OS << "< "; break;
1459   case ICmpInst::ICMP_UGT:
1460   case ICmpInst::ICMP_SGT:
1461   case FCmpInst::FCMP_UGT:
1462   case FCmpInst::FCMP_OGT: OS << "> "; break;
1463   case ICmpInst::ICMP_ULE:
1464   case ICmpInst::ICMP_SLE:
1465   case FCmpInst::FCMP_ULE:
1466   case FCmpInst::FCMP_OLE: OS << "<= "; break;
1467   case ICmpInst::ICMP_UGE:
1468   case ICmpInst::ICMP_SGE:
1469   case FCmpInst::FCMP_UGE:
1470   case FCmpInst::FCMP_OGE: OS << ">= "; break;
1471   }
1472
1473   WriteAsOperand(OS, Val);
1474   OS << "\n";
1475 }
1476
1477 // Don't inline these methods or else we won't be able to call them from GDB!
1478 void Relation::dump() const { print(*cerr.stream()); }
1479 void ValueInfo::dump() const { print(*cerr.stream(), 0); }
1480 void RegionInfo::dump() const { print(*cerr.stream()); }