Fix two bugs: one where a condition was mistakenly swapped, and another
[oota-llvm.git] / lib / Transforms / Scalar / InstructionCombining.cpp
1 //===- InstructionCombining.cpp - Combine multiple instructions -----------===//
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 // InstructionCombining - Combine instructions to form fewer, simple
11 // instructions.  This pass does not modify the CFG This pass is where algebraic
12 // simplification happens.
13 //
14 // This pass combines things like:
15 //    %Y = add int %X, 1
16 //    %Z = add int %Y, 1
17 // into:
18 //    %Z = add int %X, 2
19 //
20 // This is a simple worklist driven algorithm.
21 //
22 // This pass guarantees that the following canonicalizations are performed on
23 // the program:
24 //    1. If a binary operator has a constant operand, it is moved to the RHS
25 //    2. Bitwise operators with constant operands are always grouped so that
26 //       shifts are performed first, then or's, then and's, then xor's.
27 //    3. SetCC instructions are converted from <,>,<=,>= to ==,!= if possible
28 //    4. All SetCC instructions on boolean values are replaced with logical ops
29 //    5. add X, X is represented as (X*2) => (X << 1)
30 //    6. Multiplies with a power-of-two constant argument are transformed into
31 //       shifts.
32 //    N. This list is incomplete
33 //
34 //===----------------------------------------------------------------------===//
35
36 #define DEBUG_TYPE "instcombine"
37 #include "llvm/Transforms/Scalar.h"
38 #include "llvm/Instructions.h"
39 #include "llvm/Intrinsics.h"
40 #include "llvm/Pass.h"
41 #include "llvm/Constants.h"
42 #include "llvm/DerivedTypes.h"
43 #include "llvm/GlobalVariable.h"
44 #include "llvm/Target/TargetData.h"
45 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
46 #include "llvm/Transforms/Utils/Local.h"
47 #include "llvm/Support/CallSite.h"
48 #include "llvm/Support/GetElementPtrTypeIterator.h"
49 #include "llvm/Support/InstIterator.h"
50 #include "llvm/Support/InstVisitor.h"
51 #include "llvm/Support/PatternMatch.h"
52 #include "llvm/Support/Debug.h"
53 #include "llvm/ADT/Statistic.h"
54 #include <algorithm>
55 using namespace llvm;
56 using namespace llvm::PatternMatch;
57
58 namespace {
59   Statistic<> NumCombined ("instcombine", "Number of insts combined");
60   Statistic<> NumConstProp("instcombine", "Number of constant folds");
61   Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
62
63   class InstCombiner : public FunctionPass,
64                        public InstVisitor<InstCombiner, Instruction*> {
65     // Worklist of all of the instructions that need to be simplified.
66     std::vector<Instruction*> WorkList;
67     TargetData *TD;
68
69     /// AddUsersToWorkList - When an instruction is simplified, add all users of
70     /// the instruction to the work lists because they might get more simplified
71     /// now.
72     ///
73     void AddUsersToWorkList(Instruction &I) {
74       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
75            UI != UE; ++UI)
76         WorkList.push_back(cast<Instruction>(*UI));
77     }
78
79     /// AddUsesToWorkList - When an instruction is simplified, add operands to
80     /// the work lists because they might get more simplified now.
81     ///
82     void AddUsesToWorkList(Instruction &I) {
83       for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
84         if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i)))
85           WorkList.push_back(Op);
86     }
87
88     // removeFromWorkList - remove all instances of I from the worklist.
89     void removeFromWorkList(Instruction *I);
90   public:
91     virtual bool runOnFunction(Function &F);
92
93     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
94       AU.addRequired<TargetData>();
95       AU.setPreservesCFG();
96     }
97
98     TargetData &getTargetData() const { return *TD; }
99
100     // Visitation implementation - Implement instruction combining for different
101     // instruction types.  The semantics are as follows:
102     // Return Value:
103     //    null        - No change was made
104     //     I          - Change was made, I is still valid, I may be dead though
105     //   otherwise    - Change was made, replace I with returned instruction
106     //   
107     Instruction *visitAdd(BinaryOperator &I);
108     Instruction *visitSub(BinaryOperator &I);
109     Instruction *visitMul(BinaryOperator &I);
110     Instruction *visitDiv(BinaryOperator &I);
111     Instruction *visitRem(BinaryOperator &I);
112     Instruction *visitAnd(BinaryOperator &I);
113     Instruction *visitOr (BinaryOperator &I);
114     Instruction *visitXor(BinaryOperator &I);
115     Instruction *visitSetCondInst(BinaryOperator &I);
116     Instruction *visitShiftInst(ShiftInst &I);
117     Instruction *visitCastInst(CastInst &CI);
118     Instruction *visitSelectInst(SelectInst &CI);
119     Instruction *visitCallInst(CallInst &CI);
120     Instruction *visitInvokeInst(InvokeInst &II);
121     Instruction *visitPHINode(PHINode &PN);
122     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
123     Instruction *visitAllocationInst(AllocationInst &AI);
124     Instruction *visitFreeInst(FreeInst &FI);
125     Instruction *visitLoadInst(LoadInst &LI);
126     Instruction *visitBranchInst(BranchInst &BI);
127     Instruction *visitSwitchInst(SwitchInst &SI);
128
129     // visitInstruction - Specify what to return for unhandled instructions...
130     Instruction *visitInstruction(Instruction &I) { return 0; }
131
132   private:
133     Instruction *visitCallSite(CallSite CS);
134     bool transformConstExprCastCall(CallSite CS);
135
136   public:
137     // InsertNewInstBefore - insert an instruction New before instruction Old
138     // in the program.  Add the new instruction to the worklist.
139     //
140     Value *InsertNewInstBefore(Instruction *New, Instruction &Old) {
141       assert(New && New->getParent() == 0 &&
142              "New instruction already inserted into a basic block!");
143       BasicBlock *BB = Old.getParent();
144       BB->getInstList().insert(&Old, New);  // Insert inst
145       WorkList.push_back(New);              // Add to worklist
146       return New;
147     }
148
149     /// InsertCastBefore - Insert a cast of V to TY before the instruction POS.
150     /// This also adds the cast to the worklist.  Finally, this returns the
151     /// cast.
152     Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) {
153       if (V->getType() == Ty) return V;
154       
155       Instruction *C = new CastInst(V, Ty, V->getName(), &Pos);
156       WorkList.push_back(C);
157       return C;
158     }
159
160     // ReplaceInstUsesWith - This method is to be used when an instruction is
161     // found to be dead, replacable with another preexisting expression.  Here
162     // we add all uses of I to the worklist, replace all uses of I with the new
163     // value, then return I, so that the inst combiner will know that I was
164     // modified.
165     //
166     Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
167       AddUsersToWorkList(I);         // Add all modified instrs to worklist
168       if (&I != V) {
169         I.replaceAllUsesWith(V);
170         return &I;
171       } else {
172         // If we are replacing the instruction with itself, this must be in a
173         // segment of unreachable code, so just clobber the instruction.
174         I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
175         return &I;
176       }
177     }
178
179     // EraseInstFromFunction - When dealing with an instruction that has side
180     // effects or produces a void value, we can't rely on DCE to delete the
181     // instruction.  Instead, visit methods should return the value returned by
182     // this function.
183     Instruction *EraseInstFromFunction(Instruction &I) {
184       assert(I.use_empty() && "Cannot erase instruction that is used!");
185       AddUsesToWorkList(I);
186       removeFromWorkList(&I);
187       I.getParent()->getInstList().erase(&I);
188       return 0;  // Don't do anything with FI
189     }
190
191
192   private:
193     /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
194     /// InsertBefore instruction.  This is specialized a bit to avoid inserting
195     /// casts that are known to not do anything...
196     ///
197     Value *InsertOperandCastBefore(Value *V, const Type *DestTy,
198                                    Instruction *InsertBefore);
199
200     // SimplifyCommutative - This performs a few simplifications for commutative
201     // operators...
202     bool SimplifyCommutative(BinaryOperator &I);
203
204     Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS,
205                           ConstantIntegral *AndRHS, BinaryOperator &TheAnd);
206   };
207
208   RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
209 }
210
211 // getComplexity:  Assign a complexity or rank value to LLVM Values...
212 //   0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst
213 static unsigned getComplexity(Value *V) {
214   if (isa<Instruction>(V)) {
215     if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
216       return 2;
217     return 3;
218   }
219   if (isa<Argument>(V)) return 2;
220   return isa<Constant>(V) ? 0 : 1;
221 }
222
223 // isOnlyUse - Return true if this instruction will be deleted if we stop using
224 // it.
225 static bool isOnlyUse(Value *V) {
226   return V->hasOneUse() || isa<Constant>(V);
227 }
228
229 // getPromotedType - Return the specified type promoted as it would be to pass
230 // though a va_arg area...
231 static const Type *getPromotedType(const Type *Ty) {
232   switch (Ty->getTypeID()) {
233   case Type::SByteTyID:
234   case Type::ShortTyID:  return Type::IntTy;
235   case Type::UByteTyID:
236   case Type::UShortTyID: return Type::UIntTy;
237   case Type::FloatTyID:  return Type::DoubleTy;
238   default:               return Ty;
239   }
240 }
241
242 // SimplifyCommutative - This performs a few simplifications for commutative
243 // operators:
244 //
245 //  1. Order operands such that they are listed from right (least complex) to
246 //     left (most complex).  This puts constants before unary operators before
247 //     binary operators.
248 //
249 //  2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
250 //  3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
251 //
252 bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
253   bool Changed = false;
254   if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
255     Changed = !I.swapOperands();
256   
257   if (!I.isAssociative()) return Changed;
258   Instruction::BinaryOps Opcode = I.getOpcode();
259   if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
260     if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
261       if (isa<Constant>(I.getOperand(1))) {
262         Constant *Folded = ConstantExpr::get(I.getOpcode(),
263                                              cast<Constant>(I.getOperand(1)),
264                                              cast<Constant>(Op->getOperand(1)));
265         I.setOperand(0, Op->getOperand(0));
266         I.setOperand(1, Folded);
267         return true;
268       } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
269         if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
270             isOnlyUse(Op) && isOnlyUse(Op1)) {
271           Constant *C1 = cast<Constant>(Op->getOperand(1));
272           Constant *C2 = cast<Constant>(Op1->getOperand(1));
273
274           // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
275           Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
276           Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
277                                                     Op1->getOperand(0),
278                                                     Op1->getName(), &I);
279           WorkList.push_back(New);
280           I.setOperand(0, New);
281           I.setOperand(1, Folded);
282           return true;
283         }      
284     }
285   return Changed;
286 }
287
288 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
289 // if the LHS is a constant zero (which is the 'negate' form).
290 //
291 static inline Value *dyn_castNegVal(Value *V) {
292   if (BinaryOperator::isNeg(V))
293     return BinaryOperator::getNegArgument(cast<BinaryOperator>(V));
294
295   // Constants can be considered to be negated values if they can be folded...
296   if (Constant *C = dyn_cast<Constant>(V))
297     return ConstantExpr::getNeg(C);
298   return 0;
299 }
300
301 static inline Value *dyn_castNotVal(Value *V) {
302   if (BinaryOperator::isNot(V))
303     return BinaryOperator::getNotArgument(cast<BinaryOperator>(V));
304
305   // Constants can be considered to be not'ed values...
306   if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V))
307     return ConstantExpr::getNot(C);
308   return 0;
309 }
310
311 // dyn_castFoldableMul - If this value is a multiply that can be folded into
312 // other computations (because it has a constant operand), return the
313 // non-constant operand of the multiply.
314 //
315 static inline Value *dyn_castFoldableMul(Value *V) {
316   if (V->hasOneUse() && V->getType()->isInteger())
317     if (Instruction *I = dyn_cast<Instruction>(V))
318       if (I->getOpcode() == Instruction::Mul)
319         if (isa<Constant>(I->getOperand(1)))
320           return I->getOperand(0);
321   return 0;
322 }
323
324 // Log2 - Calculate the log base 2 for the specified value if it is exactly a
325 // power of 2.
326 static unsigned Log2(uint64_t Val) {
327   assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!");
328   unsigned Count = 0;
329   while (Val != 1) {
330     if (Val & 1) return 0;    // Multiple bits set?
331     Val >>= 1;
332     ++Count;
333   }
334   return Count;
335 }
336
337
338 /// AssociativeOpt - Perform an optimization on an associative operator.  This
339 /// function is designed to check a chain of associative operators for a
340 /// potential to apply a certain optimization.  Since the optimization may be
341 /// applicable if the expression was reassociated, this checks the chain, then
342 /// reassociates the expression as necessary to expose the optimization
343 /// opportunity.  This makes use of a special Functor, which must define
344 /// 'shouldApply' and 'apply' methods.
345 ///
346 template<typename Functor>
347 Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
348   unsigned Opcode = Root.getOpcode();
349   Value *LHS = Root.getOperand(0);
350
351   // Quick check, see if the immediate LHS matches...
352   if (F.shouldApply(LHS))
353     return F.apply(Root);
354
355   // Otherwise, if the LHS is not of the same opcode as the root, return.
356   Instruction *LHSI = dyn_cast<Instruction>(LHS);
357   while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) {
358     // Should we apply this transform to the RHS?
359     bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
360
361     // If not to the RHS, check to see if we should apply to the LHS...
362     if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
363       cast<BinaryOperator>(LHSI)->swapOperands();   // Make the LHS the RHS
364       ShouldApply = true;
365     }
366
367     // If the functor wants to apply the optimization to the RHS of LHSI,
368     // reassociate the expression from ((? op A) op B) to (? op (A op B))
369     if (ShouldApply) {
370       BasicBlock *BB = Root.getParent();
371       
372       // Now all of the instructions are in the current basic block, go ahead
373       // and perform the reassociation.
374       Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
375
376       // First move the selected RHS to the LHS of the root...
377       Root.setOperand(0, LHSI->getOperand(1));
378
379       // Make what used to be the LHS of the root be the user of the root...
380       Value *ExtraOperand = TmpLHSI->getOperand(1);
381       if (&Root == TmpLHSI) {
382         Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
383         return 0;
384       }
385       Root.replaceAllUsesWith(TmpLHSI);          // Users now use TmpLHSI
386       TmpLHSI->setOperand(1, &Root);             // TmpLHSI now uses the root
387       TmpLHSI->getParent()->getInstList().remove(TmpLHSI);
388       BasicBlock::iterator ARI = &Root; ++ARI;
389       BB->getInstList().insert(ARI, TmpLHSI);    // Move TmpLHSI to after Root
390       ARI = Root;
391
392       // Now propagate the ExtraOperand down the chain of instructions until we
393       // get to LHSI.
394       while (TmpLHSI != LHSI) {
395         Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
396         // Move the instruction to immediately before the chain we are
397         // constructing to avoid breaking dominance properties.
398         NextLHSI->getParent()->getInstList().remove(NextLHSI);
399         BB->getInstList().insert(ARI, NextLHSI);
400         ARI = NextLHSI;
401
402         Value *NextOp = NextLHSI->getOperand(1);
403         NextLHSI->setOperand(1, ExtraOperand);
404         TmpLHSI = NextLHSI;
405         ExtraOperand = NextOp;
406       }
407       
408       // Now that the instructions are reassociated, have the functor perform
409       // the transformation...
410       return F.apply(Root);
411     }
412     
413     LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
414   }
415   return 0;
416 }
417
418
419 // AddRHS - Implements: X + X --> X << 1
420 struct AddRHS {
421   Value *RHS;
422   AddRHS(Value *rhs) : RHS(rhs) {}
423   bool shouldApply(Value *LHS) const { return LHS == RHS; }
424   Instruction *apply(BinaryOperator &Add) const {
425     return new ShiftInst(Instruction::Shl, Add.getOperand(0),
426                          ConstantInt::get(Type::UByteTy, 1));
427   }
428 };
429
430 // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
431 //                 iff C1&C2 == 0
432 struct AddMaskingAnd {
433   Constant *C2;
434   AddMaskingAnd(Constant *c) : C2(c) {}
435   bool shouldApply(Value *LHS) const {
436     ConstantInt *C1;
437     return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && 
438            ConstantExpr::getAnd(C1, C2)->isNullValue();
439   }
440   Instruction *apply(BinaryOperator &Add) const {
441     return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1));
442   }
443 };
444
445 static Value *FoldOperationIntoSelectOperand(Instruction &BI, Value *SO,
446                                              InstCombiner *IC) {
447   // Figure out if the constant is the left or the right argument.
448   bool ConstIsRHS = isa<Constant>(BI.getOperand(1));
449   Constant *ConstOperand = cast<Constant>(BI.getOperand(ConstIsRHS));
450
451   if (Constant *SOC = dyn_cast<Constant>(SO)) {
452     if (ConstIsRHS)
453       return ConstantExpr::get(BI.getOpcode(), SOC, ConstOperand);
454     return ConstantExpr::get(BI.getOpcode(), ConstOperand, SOC);
455   }
456
457   Value *Op0 = SO, *Op1 = ConstOperand;
458   if (!ConstIsRHS)
459     std::swap(Op0, Op1);
460   Instruction *New;
461   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&BI))
462     New = BinaryOperator::create(BO->getOpcode(), Op0, Op1);
463   else if (ShiftInst *SI = dyn_cast<ShiftInst>(&BI))
464     New = new ShiftInst(SI->getOpcode(), Op0, Op1);
465   else {
466     assert(0 && "Unknown binary instruction type!");
467     abort();
468   }
469   return IC->InsertNewInstBefore(New, BI);
470 }
471
472 // FoldBinOpIntoSelect - Given an instruction with a select as one operand and a
473 // constant as the other operand, try to fold the binary operator into the
474 // select arguments.
475 static Instruction *FoldBinOpIntoSelect(Instruction &BI, SelectInst *SI,
476                                         InstCombiner *IC) {
477   // Don't modify shared select instructions
478   if (!SI->hasOneUse()) return 0;
479   Value *TV = SI->getOperand(1);
480   Value *FV = SI->getOperand(2);
481
482   if (isa<Constant>(TV) || isa<Constant>(FV)) {
483     Value *SelectTrueVal = FoldOperationIntoSelectOperand(BI, TV, IC);
484     Value *SelectFalseVal = FoldOperationIntoSelectOperand(BI, FV, IC);
485
486     return new SelectInst(SI->getCondition(), SelectTrueVal,
487                           SelectFalseVal);
488   }
489   return 0;
490 }
491
492 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
493   bool Changed = SimplifyCommutative(I);
494   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
495
496   if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
497     // X + 0 --> X
498     if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop
499         RHSC->isNullValue())
500       return ReplaceInstUsesWith(I, LHS);
501     
502     // X + (signbit) --> X ^ signbit
503     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
504       unsigned NumBits = CI->getType()->getPrimitiveSize()*8;
505       uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1;
506       if (Val == (1ULL << NumBits-1))
507         return BinaryOperator::createXor(LHS, RHS);
508     }
509   }
510
511   // X + X --> X << 1
512   if (I.getType()->isInteger()) {
513     if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;
514   }
515
516   // -A + B  -->  B - A
517   if (Value *V = dyn_castNegVal(LHS))
518     return BinaryOperator::createSub(RHS, V);
519
520   // A + -B  -->  A - B
521   if (!isa<Constant>(RHS))
522     if (Value *V = dyn_castNegVal(RHS))
523       return BinaryOperator::createSub(LHS, V);
524
525   // X*C + X --> X * (C+1)
526   if (dyn_castFoldableMul(LHS) == RHS) {
527     Constant *CP1 =
528       ConstantExpr::getAdd(
529                         cast<Constant>(cast<Instruction>(LHS)->getOperand(1)),
530                         ConstantInt::get(I.getType(), 1));
531     return BinaryOperator::createMul(RHS, CP1);
532   }
533
534   // X + X*C --> X * (C+1)
535   if (dyn_castFoldableMul(RHS) == LHS) {
536     Constant *CP1 =
537       ConstantExpr::getAdd(
538                         cast<Constant>(cast<Instruction>(RHS)->getOperand(1)),
539                         ConstantInt::get(I.getType(), 1));
540     return BinaryOperator::createMul(LHS, CP1);
541   }
542
543   // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
544   ConstantInt *C2;
545   if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
546     if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R;
547
548   if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
549     Value *X;
550     if (match(LHS, m_Not(m_Value(X)))) {   // ~X + C --> (C-1) - X
551       Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1));
552       return BinaryOperator::createSub(C, X);
553     }
554
555     // Try to fold constant add into select arguments.
556     if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
557       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
558         return R;
559   }
560
561   return Changed ? &I : 0;
562 }
563
564 // isSignBit - Return true if the value represented by the constant only has the
565 // highest order bit set.
566 static bool isSignBit(ConstantInt *CI) {
567   unsigned NumBits = CI->getType()->getPrimitiveSize()*8;
568   return (CI->getRawValue() & ~(-1LL << NumBits)) == (1ULL << (NumBits-1));
569 }
570
571 static unsigned getTypeSizeInBits(const Type *Ty) {
572   return Ty == Type::BoolTy ? 1 : Ty->getPrimitiveSize()*8;
573 }
574
575 /// RemoveNoopCast - Strip off nonconverting casts from the value.
576 ///
577 static Value *RemoveNoopCast(Value *V) {
578   if (CastInst *CI = dyn_cast<CastInst>(V)) {
579     const Type *CTy = CI->getType();
580     const Type *OpTy = CI->getOperand(0)->getType();
581     if (CTy->isInteger() && OpTy->isInteger()) {
582       if (CTy->getPrimitiveSize() == OpTy->getPrimitiveSize())
583         return RemoveNoopCast(CI->getOperand(0));
584     } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy))
585       return RemoveNoopCast(CI->getOperand(0));
586   }
587   return V;
588 }
589
590 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
591   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
592
593   if (Op0 == Op1)         // sub X, X  -> 0
594     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
595
596   // If this is a 'B = x-(-A)', change to B = x+A...
597   if (Value *V = dyn_castNegVal(Op1))
598     return BinaryOperator::createAdd(Op0, V);
599
600   if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
601     // Replace (-1 - A) with (~A)...
602     if (C->isAllOnesValue())
603       return BinaryOperator::createNot(Op1);
604
605     // C - ~X == X + (1+C)
606     Value *X;
607     if (match(Op1, m_Not(m_Value(X))))
608       return BinaryOperator::createAdd(X,
609                     ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1)));
610     // -((uint)X >> 31) -> ((int)X >> 31)
611     // -((int)X >> 31) -> ((uint)X >> 31)
612     if (C->isNullValue()) {
613       Value *NoopCastedRHS = RemoveNoopCast(Op1);
614       if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS))
615         if (SI->getOpcode() == Instruction::Shr)
616           if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) {
617             const Type *NewTy;
618             if (SI->getType()->isSigned())
619               NewTy = SI->getType()->getUnsignedVersion();
620             else
621               NewTy = SI->getType()->getSignedVersion();
622             // Check to see if we are shifting out everything but the sign bit.
623             if (CU->getValue() == SI->getType()->getPrimitiveSize()*8-1) {
624               // Ok, the transformation is safe.  Insert a cast of the incoming
625               // value, then the new shift, then the new cast.
626               Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy,
627                                                  SI->getOperand(0)->getName());
628               Value *InV = InsertNewInstBefore(FirstCast, I);
629               Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast,
630                                                     CU, SI->getName());
631               if (NewShift->getType() == I.getType())
632                 return NewShift;
633               else {
634                 InV = InsertNewInstBefore(NewShift, I);
635                 return new CastInst(NewShift, I.getType());
636               }
637             }
638           }
639     }
640
641     // Try to fold constant sub into select arguments.
642     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
643       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
644         return R;
645   }
646
647   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
648     if (Op1I->hasOneUse()) {
649       // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
650       // is not used by anyone else...
651       //
652       if (Op1I->getOpcode() == Instruction::Sub &&
653           !Op1I->getType()->isFloatingPoint()) {
654         // Swap the two operands of the subexpr...
655         Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
656         Op1I->setOperand(0, IIOp1);
657         Op1I->setOperand(1, IIOp0);
658         
659         // Create the new top level add instruction...
660         return BinaryOperator::createAdd(Op0, Op1);
661       }
662
663       // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
664       //
665       if (Op1I->getOpcode() == Instruction::And &&
666           (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
667         Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
668
669         Value *NewNot =
670           InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I);
671         return BinaryOperator::createAnd(Op0, NewNot);
672       }
673
674       // X - X*C --> X * (1-C)
675       if (dyn_castFoldableMul(Op1I) == Op0) {
676         Constant *CP1 =
677           ConstantExpr::getSub(ConstantInt::get(I.getType(), 1),
678                          cast<Constant>(cast<Instruction>(Op1)->getOperand(1)));
679         assert(CP1 && "Couldn't constant fold 1-C?");
680         return BinaryOperator::createMul(Op0, CP1);
681       }
682     }
683
684   // X*C - X --> X * (C-1)
685   if (dyn_castFoldableMul(Op0) == Op1) {
686     Constant *CP1 =
687      ConstantExpr::getSub(cast<Constant>(cast<Instruction>(Op0)->getOperand(1)),
688                         ConstantInt::get(I.getType(), 1));
689     assert(CP1 && "Couldn't constant fold C - 1?");
690     return BinaryOperator::createMul(Op1, CP1);
691   }
692
693   return 0;
694 }
695
696 /// isSignBitCheck - Given an exploded setcc instruction, return true if it is
697 /// really just returns true if the most significant (sign) bit is set.
698 static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) {
699   if (RHS->getType()->isSigned()) {
700     // True if source is LHS < 0 or LHS <= -1
701     return Opcode == Instruction::SetLT && RHS->isNullValue() ||
702            Opcode == Instruction::SetLE && RHS->isAllOnesValue();
703   } else {
704     ConstantUInt *RHSC = cast<ConstantUInt>(RHS);
705     // True if source is LHS > 127 or LHS >= 128, where the constants depend on
706     // the size of the integer type.
707     if (Opcode == Instruction::SetGE)
708       return RHSC->getValue() == 1ULL<<(RHS->getType()->getPrimitiveSize()*8-1);
709     if (Opcode == Instruction::SetGT)
710       return RHSC->getValue() ==
711         (1ULL << (RHS->getType()->getPrimitiveSize()*8-1))-1;
712   }
713   return false;
714 }
715
716 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
717   bool Changed = SimplifyCommutative(I);
718   Value *Op0 = I.getOperand(0);
719
720   // Simplify mul instructions with a constant RHS...
721   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
722     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
723
724       // ((X << C1)*C2) == (X * (C2 << C1))
725       if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0))
726         if (SI->getOpcode() == Instruction::Shl)
727           if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
728             return BinaryOperator::createMul(SI->getOperand(0),
729                                              ConstantExpr::getShl(CI, ShOp));
730       
731       if (CI->isNullValue())
732         return ReplaceInstUsesWith(I, Op1);  // X * 0  == 0
733       if (CI->equalsInt(1))                  // X * 1  == X
734         return ReplaceInstUsesWith(I, Op0);
735       if (CI->isAllOnesValue())              // X * -1 == 0 - X
736         return BinaryOperator::createNeg(Op0, I.getName());
737
738       int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue();
739       if (uint64_t C = Log2(Val))            // Replace X*(2^C) with X << C
740         return new ShiftInst(Instruction::Shl, Op0,
741                              ConstantUInt::get(Type::UByteTy, C));
742     } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
743       if (Op1F->isNullValue())
744         return ReplaceInstUsesWith(I, Op1);
745
746       // "In IEEE floating point, x*1 is not equivalent to x for nans.  However,
747       // ANSI says we can drop signals, so we can do this anyway." (from GCC)
748       if (Op1F->getValue() == 1.0)
749         return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul double %X, 1.0'
750     }
751
752     // Try to fold constant mul into select arguments.
753     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
754       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
755         return R;
756   }
757
758   if (Value *Op0v = dyn_castNegVal(Op0))     // -X * -Y = X*Y
759     if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
760       return BinaryOperator::createMul(Op0v, Op1v);
761
762   // If one of the operands of the multiply is a cast from a boolean value, then
763   // we know the bool is either zero or one, so this is a 'masking' multiply.
764   // See if we can simplify things based on how the boolean was originally
765   // formed.
766   CastInst *BoolCast = 0;
767   if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0)))
768     if (CI->getOperand(0)->getType() == Type::BoolTy)
769       BoolCast = CI;
770   if (!BoolCast)
771     if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1)))
772       if (CI->getOperand(0)->getType() == Type::BoolTy)
773         BoolCast = CI;
774   if (BoolCast) {
775     if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) {
776       Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1);
777       const Type *SCOpTy = SCIOp0->getType();
778
779       // If the setcc is true iff the sign bit of X is set, then convert this
780       // multiply into a shift/and combination.
781       if (isa<ConstantInt>(SCIOp1) &&
782           isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) {
783         // Shift the X value right to turn it into "all signbits".
784         Constant *Amt = ConstantUInt::get(Type::UByteTy,
785                                           SCOpTy->getPrimitiveSize()*8-1);
786         if (SCIOp0->getType()->isUnsigned()) {
787           const Type *NewTy = SCIOp0->getType()->getSignedVersion();
788           SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy,
789                                                     SCIOp0->getName()), I);
790         }
791
792         Value *V =
793           InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt,
794                                             BoolCast->getOperand(0)->getName()+
795                                             ".mask"), I);
796
797         // If the multiply type is not the same as the source type, sign extend
798         // or truncate to the multiply type.
799         if (I.getType() != V->getType())
800           V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I);
801         
802         Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
803         return BinaryOperator::createAnd(V, OtherOp);
804       }
805     }
806   }
807
808   return Changed ? &I : 0;
809 }
810
811 Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
812   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
813     // div X, 1 == X
814     if (RHS->equalsInt(1))
815       return ReplaceInstUsesWith(I, I.getOperand(0));
816
817     // div X, -1 == -X
818     if (RHS->isAllOnesValue())
819       return BinaryOperator::createNeg(I.getOperand(0));
820
821     // Check to see if this is an unsigned division with an exact power of 2,
822     // if so, convert to a right shift.
823     if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
824       if (uint64_t Val = C->getValue())    // Don't break X / 0
825         if (uint64_t C = Log2(Val))
826           return new ShiftInst(Instruction::Shr, I.getOperand(0),
827                                ConstantUInt::get(Type::UByteTy, C));
828   }
829
830   // 0 / X == 0, we don't need to preserve faults!
831   if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
832     if (LHS->equalsInt(0))
833       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
834
835   return 0;
836 }
837
838
839 Instruction *InstCombiner::visitRem(BinaryOperator &I) {
840   if (I.getType()->isSigned())
841     if (Value *RHSNeg = dyn_castNegVal(I.getOperand(1)))
842       if (!isa<ConstantSInt>(RHSNeg) ||
843           cast<ConstantSInt>(RHSNeg)->getValue() > 0) {
844         // X % -Y -> X % Y
845         AddUsesToWorkList(I);
846         I.setOperand(1, RHSNeg);
847         return &I;
848       }
849
850   if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
851     if (RHS->equalsInt(1))  // X % 1 == 0
852       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
853
854     // Check to see if this is an unsigned remainder with an exact power of 2,
855     // if so, convert to a bitwise and.
856     if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
857       if (uint64_t Val = C->getValue())    // Don't break X % 0 (divide by zero)
858         if (!(Val & (Val-1)))              // Power of 2
859           return BinaryOperator::createAnd(I.getOperand(0),
860                                         ConstantUInt::get(I.getType(), Val-1));
861   }
862
863   // 0 % X == 0, we don't need to preserve faults!
864   if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
865     if (LHS->equalsInt(0))
866       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
867
868   return 0;
869 }
870
871 // isMaxValueMinusOne - return true if this is Max-1
872 static bool isMaxValueMinusOne(const ConstantInt *C) {
873   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
874     // Calculate -1 casted to the right type...
875     unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
876     uint64_t Val = ~0ULL;                // All ones
877     Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
878     return CU->getValue() == Val-1;
879   }
880
881   const ConstantSInt *CS = cast<ConstantSInt>(C);
882   
883   // Calculate 0111111111..11111
884   unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
885   int64_t Val = INT64_MAX;             // All ones
886   Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
887   return CS->getValue() == Val-1;
888 }
889
890 // isMinValuePlusOne - return true if this is Min+1
891 static bool isMinValuePlusOne(const ConstantInt *C) {
892   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
893     return CU->getValue() == 1;
894
895   const ConstantSInt *CS = cast<ConstantSInt>(C);
896   
897   // Calculate 1111111111000000000000 
898   unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
899   int64_t Val = -1;                    // All ones
900   Val <<= TypeBits-1;                  // Shift over to the right spot
901   return CS->getValue() == Val+1;
902 }
903
904 // isOneBitSet - Return true if there is exactly one bit set in the specified
905 // constant.
906 static bool isOneBitSet(const ConstantInt *CI) {
907   uint64_t V = CI->getRawValue();
908   return V && (V & (V-1)) == 0;
909 }
910
911 #if 0   // Currently unused
912 // isLowOnes - Return true if the constant is of the form 0+1+.
913 static bool isLowOnes(const ConstantInt *CI) {
914   uint64_t V = CI->getRawValue();
915
916   // There won't be bits set in parts that the type doesn't contain.
917   V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue();
918
919   uint64_t U = V+1;  // If it is low ones, this should be a power of two.
920   return U && V && (U & V) == 0;
921 }
922 #endif
923
924 // isHighOnes - Return true if the constant is of the form 1+0+.
925 // This is the same as lowones(~X).
926 static bool isHighOnes(const ConstantInt *CI) {
927   uint64_t V = ~CI->getRawValue();
928
929   // There won't be bits set in parts that the type doesn't contain.
930   V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue();
931
932   uint64_t U = V+1;  // If it is low ones, this should be a power of two.
933   return U && V && (U & V) == 0;
934 }
935
936
937 /// getSetCondCode - Encode a setcc opcode into a three bit mask.  These bits
938 /// are carefully arranged to allow folding of expressions such as:
939 ///
940 ///      (A < B) | (A > B) --> (A != B)
941 ///
942 /// Bit value '4' represents that the comparison is true if A > B, bit value '2'
943 /// represents that the comparison is true if A == B, and bit value '1' is true
944 /// if A < B.
945 ///
946 static unsigned getSetCondCode(const SetCondInst *SCI) {
947   switch (SCI->getOpcode()) {
948     // False -> 0
949   case Instruction::SetGT: return 1;
950   case Instruction::SetEQ: return 2;
951   case Instruction::SetGE: return 3;
952   case Instruction::SetLT: return 4;
953   case Instruction::SetNE: return 5;
954   case Instruction::SetLE: return 6;
955     // True -> 7
956   default:
957     assert(0 && "Invalid SetCC opcode!");
958     return 0;
959   }
960 }
961
962 /// getSetCCValue - This is the complement of getSetCondCode, which turns an
963 /// opcode and two operands into either a constant true or false, or a brand new
964 /// SetCC instruction.
965 static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) {
966   switch (Opcode) {
967   case 0: return ConstantBool::False;
968   case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS);
969   case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS);
970   case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS);
971   case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS);
972   case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS);
973   case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS);
974   case 7: return ConstantBool::True;
975   default: assert(0 && "Illegal SetCCCode!"); return 0;
976   }
977 }
978
979 // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B)
980 struct FoldSetCCLogical {
981   InstCombiner &IC;
982   Value *LHS, *RHS;
983   FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI)
984     : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {}
985   bool shouldApply(Value *V) const {
986     if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
987       return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS ||
988               SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS);
989     return false;
990   }
991   Instruction *apply(BinaryOperator &Log) const {
992     SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0));
993     if (SCI->getOperand(0) != LHS) {
994       assert(SCI->getOperand(1) == LHS);
995       SCI->swapOperands();  // Swap the LHS and RHS of the SetCC
996     }
997
998     unsigned LHSCode = getSetCondCode(SCI);
999     unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1)));
1000     unsigned Code;
1001     switch (Log.getOpcode()) {
1002     case Instruction::And: Code = LHSCode & RHSCode; break;
1003     case Instruction::Or:  Code = LHSCode | RHSCode; break;
1004     case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
1005     default: assert(0 && "Illegal logical opcode!"); return 0;
1006     }
1007
1008     Value *RV = getSetCCValue(Code, LHS, RHS);
1009     if (Instruction *I = dyn_cast<Instruction>(RV))
1010       return I;
1011     // Otherwise, it's a constant boolean value...
1012     return IC.ReplaceInstUsesWith(Log, RV);
1013   }
1014 };
1015
1016
1017 // OptAndOp - This handles expressions of the form ((val OP C1) & C2).  Where
1018 // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is
1019 // guaranteed to be either a shift instruction or a binary operator.
1020 Instruction *InstCombiner::OptAndOp(Instruction *Op,
1021                                     ConstantIntegral *OpRHS,
1022                                     ConstantIntegral *AndRHS,
1023                                     BinaryOperator &TheAnd) {
1024   Value *X = Op->getOperand(0);
1025   Constant *Together = 0;
1026   if (!isa<ShiftInst>(Op))
1027     Together = ConstantExpr::getAnd(AndRHS, OpRHS);
1028
1029   switch (Op->getOpcode()) {
1030   case Instruction::Xor:
1031     if (Together->isNullValue()) {
1032       // (X ^ C1) & C2 --> (X & C2) iff (C1&C2) == 0
1033       return BinaryOperator::createAnd(X, AndRHS);
1034     } else if (Op->hasOneUse()) {
1035       // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
1036       std::string OpName = Op->getName(); Op->setName("");
1037       Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName);
1038       InsertNewInstBefore(And, TheAnd);
1039       return BinaryOperator::createXor(And, Together);
1040     }
1041     break;
1042   case Instruction::Or:
1043     // (X | C1) & C2 --> X & C2 iff C1 & C1 == 0
1044     if (Together->isNullValue())
1045       return BinaryOperator::createAnd(X, AndRHS);
1046     else {
1047       if (Together == AndRHS) // (X | C) & C --> C
1048         return ReplaceInstUsesWith(TheAnd, AndRHS);
1049       
1050       if (Op->hasOneUse() && Together != OpRHS) {
1051         // (X | C1) & C2 --> (X | (C1&C2)) & C2
1052         std::string Op0Name = Op->getName(); Op->setName("");
1053         Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name);
1054         InsertNewInstBefore(Or, TheAnd);
1055         return BinaryOperator::createAnd(Or, AndRHS);
1056       }
1057     }
1058     break;
1059   case Instruction::Add:
1060     if (Op->hasOneUse()) {
1061       // Adding a one to a single bit bit-field should be turned into an XOR
1062       // of the bit.  First thing to check is to see if this AND is with a
1063       // single bit constant.
1064       uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue();
1065
1066       // Clear bits that are not part of the constant.
1067       AndRHSV &= (1ULL << AndRHS->getType()->getPrimitiveSize()*8)-1;
1068
1069       // If there is only one bit set...
1070       if (isOneBitSet(cast<ConstantInt>(AndRHS))) {
1071         // Ok, at this point, we know that we are masking the result of the
1072         // ADD down to exactly one bit.  If the constant we are adding has
1073         // no bits set below this bit, then we can eliminate the ADD.
1074         uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue();
1075             
1076         // Check to see if any bits below the one bit set in AndRHSV are set.
1077         if ((AddRHS & (AndRHSV-1)) == 0) {
1078           // If not, the only thing that can effect the output of the AND is
1079           // the bit specified by AndRHSV.  If that bit is set, the effect of
1080           // the XOR is to toggle the bit.  If it is clear, then the ADD has
1081           // no effect.
1082           if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
1083             TheAnd.setOperand(0, X);
1084             return &TheAnd;
1085           } else {
1086             std::string Name = Op->getName(); Op->setName("");
1087             // Pull the XOR out of the AND.
1088             Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name);
1089             InsertNewInstBefore(NewAnd, TheAnd);
1090             return BinaryOperator::createXor(NewAnd, AndRHS);
1091           }
1092         }
1093       }
1094     }
1095     break;
1096
1097   case Instruction::Shl: {
1098     // We know that the AND will not produce any of the bits shifted in, so if
1099     // the anded constant includes them, clear them now!
1100     //
1101     Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
1102     Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS);
1103     Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask);
1104                                         
1105     if (CI == ShlMask) {   // Masking out bits that the shift already masks
1106       return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and.
1107     } else if (CI != AndRHS) {                  // Reducing bits set in and.
1108       TheAnd.setOperand(1, CI);
1109       return &TheAnd;
1110     }
1111     break;
1112   } 
1113   case Instruction::Shr:
1114     // We know that the AND will not produce any of the bits shifted in, so if
1115     // the anded constant includes them, clear them now!  This only applies to
1116     // unsigned shifts, because a signed shr may bring in set bits!
1117     //
1118     if (AndRHS->getType()->isUnsigned()) {
1119       Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
1120       Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS);
1121       Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask);
1122
1123       if (CI == ShrMask) {   // Masking out bits that the shift already masks.
1124         return ReplaceInstUsesWith(TheAnd, Op);
1125       } else if (CI != AndRHS) {
1126         TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
1127         return &TheAnd;
1128       }
1129     } else {   // Signed shr.
1130       // See if this is shifting in some sign extension, then masking it out
1131       // with an and.
1132       if (Op->hasOneUse()) {
1133         Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
1134         Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS);
1135         Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask);
1136         if (CI == ShrMask) {          // Masking out bits shifted in.
1137           // Make the argument unsigned.
1138           Value *ShVal = Op->getOperand(0);
1139           ShVal = InsertCastBefore(ShVal,
1140                                    ShVal->getType()->getUnsignedVersion(),
1141                                    TheAnd);
1142           ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal,
1143                                                     OpRHS, Op->getName()),
1144                                       TheAnd);
1145           return new CastInst(ShVal, Op->getType());
1146         }
1147       }
1148     }
1149     break;
1150   }
1151   return 0;
1152 }
1153
1154
1155 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
1156   bool Changed = SimplifyCommutative(I);
1157   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1158
1159   // and X, X = X   and X, 0 == 0
1160   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
1161     return ReplaceInstUsesWith(I, Op1);
1162
1163   // and X, -1 == X
1164   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
1165     if (RHS->isAllOnesValue())
1166       return ReplaceInstUsesWith(I, Op0);
1167
1168     // Optimize a variety of ((val OP C1) & C2) combinations...
1169     if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) {
1170       Instruction *Op0I = cast<Instruction>(Op0);
1171       Value *X = Op0I->getOperand(0);
1172       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1173         if (Instruction *Res = OptAndOp(Op0I, Op0CI, RHS, I))
1174           return Res;
1175     }
1176
1177     // Try to fold constant and into select arguments.
1178     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1179       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
1180         return R;
1181   }
1182
1183   Value *Op0NotVal = dyn_castNotVal(Op0);
1184   Value *Op1NotVal = dyn_castNotVal(Op1);
1185
1186   if (Op0NotVal == Op1 || Op1NotVal == Op0)  // A & ~A  == ~A & A == 0
1187     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1188
1189   // (~A & ~B) == (~(A | B)) - De Morgan's Law
1190   if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
1191     Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal,
1192                                                I.getName()+".demorgan");
1193     InsertNewInstBefore(Or, I);
1194     return BinaryOperator::createNot(Or);
1195   }
1196
1197   // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B)
1198   if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1)))
1199     if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
1200       return R;
1201
1202   return Changed ? &I : 0;
1203 }
1204
1205
1206
1207 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
1208   bool Changed = SimplifyCommutative(I);
1209   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1210
1211   // or X, X = X   or X, 0 == X
1212   if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
1213     return ReplaceInstUsesWith(I, Op0);
1214
1215   // or X, -1 == -1
1216   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
1217     if (RHS->isAllOnesValue())
1218       return ReplaceInstUsesWith(I, Op1);
1219
1220     ConstantInt *C1; Value *X;
1221     // (X & C1) | C2 --> (X | C2) & (C1|C2)
1222     if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
1223       std::string Op0Name = Op0->getName(); Op0->setName("");
1224       Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name);
1225       InsertNewInstBefore(Or, I);
1226       return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1));
1227     }
1228
1229     // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
1230     if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
1231       std::string Op0Name = Op0->getName(); Op0->setName("");
1232       Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name);
1233       InsertNewInstBefore(Or, I);
1234       return BinaryOperator::createXor(Or,
1235                  ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS)));
1236     }
1237
1238     // Try to fold constant and into select arguments.
1239     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1240       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
1241         return R;
1242   }
1243
1244   // (A & C1)|(A & C2) == A & (C1|C2)
1245   Value *A, *B; ConstantInt *C1, *C2;
1246   if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) &&
1247       match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && A == B)
1248     return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2));
1249
1250   if (match(Op0, m_Not(m_Value(A)))) {   // ~A | Op1
1251     if (A == Op1)   // ~A | A == -1
1252       return ReplaceInstUsesWith(I, 
1253                                 ConstantIntegral::getAllOnesValue(I.getType()));
1254   } else {
1255     A = 0;
1256   }
1257
1258   if (match(Op1, m_Not(m_Value(B)))) {   // Op0 | ~B
1259     if (Op0 == B)
1260       return ReplaceInstUsesWith(I, 
1261                                 ConstantIntegral::getAllOnesValue(I.getType()));
1262
1263     // (~A | ~B) == (~(A & B)) - De Morgan's Law
1264     if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
1265       Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B,
1266                                               I.getName()+".demorgan"), I);
1267       return BinaryOperator::createNot(And);
1268     }
1269   }
1270
1271   // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B)
1272   if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1)))
1273     if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
1274       return R;
1275
1276   return Changed ? &I : 0;
1277 }
1278
1279 // XorSelf - Implements: X ^ X --> 0
1280 struct XorSelf {
1281   Value *RHS;
1282   XorSelf(Value *rhs) : RHS(rhs) {}
1283   bool shouldApply(Value *LHS) const { return LHS == RHS; }
1284   Instruction *apply(BinaryOperator &Xor) const {
1285     return &Xor;
1286   }
1287 };
1288
1289
1290 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
1291   bool Changed = SimplifyCommutative(I);
1292   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1293
1294   // xor X, X = 0, even if X is nested in a sequence of Xor's.
1295   if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
1296     assert(Result == &I && "AssociativeOpt didn't work?");
1297     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1298   }
1299
1300   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
1301     // xor X, 0 == X
1302     if (RHS->isNullValue())
1303       return ReplaceInstUsesWith(I, Op0);
1304
1305     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1306       // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
1307       if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I))
1308         if (RHS == ConstantBool::True && SCI->hasOneUse())
1309           return new SetCondInst(SCI->getInverseCondition(),
1310                                  SCI->getOperand(0), SCI->getOperand(1));
1311
1312       // ~(c-X) == X-c-1 == X+(-c-1)
1313       if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
1314         if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
1315           Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
1316           Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
1317                                               ConstantInt::get(I.getType(), 1));
1318           return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS);
1319         }
1320
1321       // ~(~X & Y) --> (X | ~Y)
1322       if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) {
1323         if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
1324         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
1325           Instruction *NotY =
1326             BinaryOperator::createNot(Op0I->getOperand(1), 
1327                                       Op0I->getOperand(1)->getName()+".not");
1328           InsertNewInstBefore(NotY, I);
1329           return BinaryOperator::createOr(Op0NotVal, NotY);
1330         }
1331       }
1332           
1333       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1334         switch (Op0I->getOpcode()) {
1335         case Instruction::Add:
1336           // ~(X-c) --> (-c-1)-X
1337           if (RHS->isAllOnesValue()) {
1338             Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
1339             return BinaryOperator::createSub(
1340                            ConstantExpr::getSub(NegOp0CI,
1341                                              ConstantInt::get(I.getType(), 1)),
1342                                           Op0I->getOperand(0));
1343           }
1344           break;
1345         case Instruction::And:
1346           // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0
1347           if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue())
1348             return BinaryOperator::createOr(Op0, RHS);
1349           break;
1350         case Instruction::Or:
1351           // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
1352           if (ConstantExpr::getAnd(RHS, Op0CI) == RHS)
1353             return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS));
1354           break;
1355         default: break;
1356         }
1357     }
1358
1359     // Try to fold constant and into select arguments.
1360     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1361       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
1362         return R;
1363   }
1364
1365   if (Value *X = dyn_castNotVal(Op0))   // ~A ^ A == -1
1366     if (X == Op1)
1367       return ReplaceInstUsesWith(I,
1368                                 ConstantIntegral::getAllOnesValue(I.getType()));
1369
1370   if (Value *X = dyn_castNotVal(Op1))   // A ^ ~A == -1
1371     if (X == Op0)
1372       return ReplaceInstUsesWith(I,
1373                                 ConstantIntegral::getAllOnesValue(I.getType()));
1374
1375   if (Instruction *Op1I = dyn_cast<Instruction>(Op1))
1376     if (Op1I->getOpcode() == Instruction::Or) {
1377       if (Op1I->getOperand(0) == Op0) {              // B^(B|A) == (A|B)^B
1378         cast<BinaryOperator>(Op1I)->swapOperands();
1379         I.swapOperands();
1380         std::swap(Op0, Op1);
1381       } else if (Op1I->getOperand(1) == Op0) {       // B^(A|B) == (A|B)^B
1382         I.swapOperands();
1383         std::swap(Op0, Op1);
1384       }      
1385     } else if (Op1I->getOpcode() == Instruction::Xor) {
1386       if (Op0 == Op1I->getOperand(0))                        // A^(A^B) == B
1387         return ReplaceInstUsesWith(I, Op1I->getOperand(1));
1388       else if (Op0 == Op1I->getOperand(1))                   // A^(B^A) == B
1389         return ReplaceInstUsesWith(I, Op1I->getOperand(0));
1390     }
1391
1392   if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
1393     if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) {
1394       if (Op0I->getOperand(0) == Op1)                // (B|A)^B == (A|B)^B
1395         cast<BinaryOperator>(Op0I)->swapOperands();
1396       if (Op0I->getOperand(1) == Op1) {              // (A|B)^B == A & ~B
1397         Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1,
1398                                                      Op1->getName()+".not"), I);
1399         return BinaryOperator::createAnd(Op0I->getOperand(0), NotB);
1400       }
1401     } else if (Op0I->getOpcode() == Instruction::Xor) {
1402       if (Op1 == Op0I->getOperand(0))                        // (A^B)^A == B
1403         return ReplaceInstUsesWith(I, Op0I->getOperand(1));
1404       else if (Op1 == Op0I->getOperand(1))                   // (B^A)^A == B
1405         return ReplaceInstUsesWith(I, Op0I->getOperand(0));
1406     }
1407
1408   // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
1409   Value *A, *B; ConstantInt *C1, *C2;
1410   if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) &&
1411       match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) &&
1412       ConstantExpr::getAnd(C1, C2)->isNullValue())
1413     return BinaryOperator::createOr(Op0, Op1);
1414
1415   // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B)
1416   if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1)))
1417     if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
1418       return R;
1419
1420   return Changed ? &I : 0;
1421 }
1422
1423 // AddOne, SubOne - Add or subtract a constant one from an integer constant...
1424 static Constant *AddOne(ConstantInt *C) {
1425   return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
1426 }
1427 static Constant *SubOne(ConstantInt *C) {
1428   return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
1429 }
1430
1431 // isTrueWhenEqual - Return true if the specified setcondinst instruction is
1432 // true when both operands are equal...
1433 //
1434 static bool isTrueWhenEqual(Instruction &I) {
1435   return I.getOpcode() == Instruction::SetEQ ||
1436          I.getOpcode() == Instruction::SetGE ||
1437          I.getOpcode() == Instruction::SetLE;
1438 }
1439
1440 Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
1441   bool Changed = SimplifyCommutative(I);
1442   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1443   const Type *Ty = Op0->getType();
1444
1445   // setcc X, X
1446   if (Op0 == Op1)
1447     return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
1448
1449   // setcc <global/alloca*>, 0 - Global/Stack value addresses are never null!
1450   if (isa<ConstantPointerNull>(Op1) && 
1451       (isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0)))
1452     return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
1453
1454
1455   // setcc's with boolean values can always be turned into bitwise operations
1456   if (Ty == Type::BoolTy) {
1457     switch (I.getOpcode()) {
1458     default: assert(0 && "Invalid setcc instruction!");
1459     case Instruction::SetEQ: {     //  seteq bool %A, %B -> ~(A^B)
1460       Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp");
1461       InsertNewInstBefore(Xor, I);
1462       return BinaryOperator::createNot(Xor);
1463     }
1464     case Instruction::SetNE:
1465       return BinaryOperator::createXor(Op0, Op1);
1466
1467     case Instruction::SetGT:
1468       std::swap(Op0, Op1);                   // Change setgt -> setlt
1469       // FALL THROUGH
1470     case Instruction::SetLT: {               // setlt bool A, B -> ~X & Y
1471       Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
1472       InsertNewInstBefore(Not, I);
1473       return BinaryOperator::createAnd(Not, Op1);
1474     }
1475     case Instruction::SetGE:
1476       std::swap(Op0, Op1);                   // Change setge -> setle
1477       // FALL THROUGH
1478     case Instruction::SetLE: {     //  setle bool %A, %B -> ~A | B
1479       Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
1480       InsertNewInstBefore(Not, I);
1481       return BinaryOperator::createOr(Not, Op1);
1482     }
1483     }
1484   }
1485
1486   // See if we are doing a comparison between a constant and an instruction that
1487   // can be folded into the comparison.
1488   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1489     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
1490       switch (LHSI->getOpcode()) {
1491       case Instruction::And:
1492         if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
1493             LHSI->getOperand(0)->hasOneUse()) {
1494           // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
1495           // could exist), turn it into (X & (C2 << C1)) != (C3 << C1).  This
1496           // happens a LOT in code produced by the C front-end, for bitfield
1497           // access.
1498           ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0));
1499           ConstantUInt *ShAmt;
1500           ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0;
1501           ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
1502           const Type *Ty = LHSI->getType();
1503           
1504           // We can fold this as long as we can't shift unknown bits
1505           // into the mask.  This can only happen with signed shift
1506           // rights, as they sign-extend.
1507           if (ShAmt) {
1508             bool CanFold = Shift->getOpcode() != Instruction::Shr ||
1509                           Shift->getType()->isUnsigned();
1510             if (!CanFold) {
1511               // To test for the bad case of the signed shr, see if any
1512               // of the bits shifted in could be tested after the mask.
1513               Constant *OShAmt = ConstantUInt::get(Type::UByteTy, 
1514                                    Ty->getPrimitiveSize()*8-ShAmt->getValue());
1515               Constant *ShVal = 
1516                 ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt);
1517               if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue())
1518                 CanFold = true;
1519             }
1520             
1521             if (CanFold) {
1522               unsigned ShiftOp = Shift->getOpcode() == Instruction::Shl
1523                 ? Instruction::Shr : Instruction::Shl;
1524               Constant *NewCst = ConstantExpr::get(ShiftOp, CI, ShAmt);
1525
1526               // Check to see if we are shifting out any of the bits being
1527               // compared.
1528               if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){
1529                 // If we shifted bits out, the fold is not going to work out.
1530                 // As a special case, check to see if this means that the
1531                 // result is always true or false now.
1532                 if (I.getOpcode() == Instruction::SetEQ)
1533                   return ReplaceInstUsesWith(I, ConstantBool::False);
1534                 if (I.getOpcode() == Instruction::SetNE)
1535                   return ReplaceInstUsesWith(I, ConstantBool::True);
1536               } else {
1537                 I.setOperand(1, NewCst);
1538                 LHSI->setOperand(1, ConstantExpr::get(ShiftOp, AndCST,ShAmt));
1539                 LHSI->setOperand(0, Shift->getOperand(0));
1540                 WorkList.push_back(Shift); // Shift is dead.
1541                 AddUsesToWorkList(I);
1542                 return &I;
1543               }
1544             }
1545           }
1546         }
1547         break;
1548
1549       case Instruction::Shr:         // (setcc (shr X, ShAmt), CI)
1550         if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) {
1551           unsigned ShAmtVal = ShAmt->getValue();
1552           
1553           switch (I.getOpcode()) {
1554           default: break;
1555           case Instruction::SetEQ:
1556           case Instruction::SetNE: {
1557             // If we are comparing against bits always shifted out, the
1558             // comparison cannot succeed.
1559             Constant *Comp = 
1560               ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt);
1561             
1562             if (Comp != CI) {// Comparing against a bit that we know is zero.
1563               bool IsSetNE = I.getOpcode() == Instruction::SetNE;
1564               Constant *Cst = ConstantBool::get(IsSetNE);
1565               return ReplaceInstUsesWith(I, Cst);
1566             }
1567               
1568             if (LHSI->hasOneUse() || CI->isNullValue()) {
1569               // Otherwise strength reduce the shift into an and.
1570               uint64_t Val = ~0ULL;          // All ones.
1571               Val <<= ShAmtVal;              // Shift over to the right spot.
1572
1573               Constant *Mask;
1574               if (CI->getType()->isUnsigned()) {
1575                 unsigned TypeBits = CI->getType()->getPrimitiveSize()*8;
1576                 Val &= (1ULL << TypeBits)-1;
1577                 Mask = ConstantUInt::get(CI->getType(), Val);
1578               } else {
1579                 Mask = ConstantSInt::get(CI->getType(), Val);
1580               }
1581               
1582               Instruction *AndI =
1583                 BinaryOperator::createAnd(LHSI->getOperand(0),
1584                                           Mask, LHSI->getName()+".mask");
1585               Value *And = InsertNewInstBefore(AndI, I);
1586               return new SetCondInst(I.getOpcode(), And,
1587                                      ConstantExpr::getShl(CI, ShAmt));
1588             }
1589             break;
1590           }
1591           }
1592         }
1593         break;
1594
1595       case Instruction::Div:
1596         if (0 && isa<ConstantInt>(LHSI->getOperand(1))) {
1597           std::cerr << "COULD FOLD: " << *LHSI;
1598           std::cerr << "COULD FOLD: " << I << "\n";
1599         }
1600         break;
1601       case Instruction::Select:
1602         // If either operand of the select is a constant, we can fold the
1603         // comparison into the select arms, which will cause one to be
1604         // constant folded and the select turned into a bitwise or.
1605         Value *Op1 = 0, *Op2 = 0;
1606         if (LHSI->hasOneUse()) {
1607           if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
1608             // Fold the known value into the constant operand.
1609             Op1 = ConstantExpr::get(I.getOpcode(), C, CI);
1610             // Insert a new SetCC of the other select operand.
1611             Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
1612                                                       LHSI->getOperand(2), CI,
1613                                                       I.getName()), I);
1614           } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
1615             // Fold the known value into the constant operand.
1616             Op2 = ConstantExpr::get(I.getOpcode(), C, CI);
1617             // Insert a new SetCC of the other select operand.
1618             Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
1619                                                       LHSI->getOperand(1), CI,
1620                                                       I.getName()), I);
1621           }
1622         }
1623         
1624         if (Op1)
1625           return new SelectInst(LHSI->getOperand(0), Op1, Op2);
1626         break;
1627       }
1628     
1629     // Simplify seteq and setne instructions...
1630     if (I.getOpcode() == Instruction::SetEQ ||
1631         I.getOpcode() == Instruction::SetNE) {
1632       bool isSetNE = I.getOpcode() == Instruction::SetNE;
1633
1634       // If the first operand is (and|or|xor) with a constant, and the second
1635       // operand is a constant, simplify a bit.
1636       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) {
1637         switch (BO->getOpcode()) {
1638         case Instruction::Rem:
1639           // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
1640           if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) &&
1641               BO->hasOneUse() &&
1642               cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1)
1643             if (unsigned L2 =
1644                 Log2(cast<ConstantSInt>(BO->getOperand(1))->getValue())) {
1645               const Type *UTy = BO->getType()->getUnsignedVersion();
1646               Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0),
1647                                                              UTy, "tmp"), I);
1648               Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2);
1649               Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX,
1650                                                     RHSCst, BO->getName()), I);
1651               return BinaryOperator::create(I.getOpcode(), NewRem,
1652                                             Constant::getNullValue(UTy));
1653             }
1654           break;          
1655
1656         case Instruction::Add:
1657           // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
1658           if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1659             if (BO->hasOneUse())
1660               return new SetCondInst(I.getOpcode(), BO->getOperand(0),
1661                                      ConstantExpr::getSub(CI, BOp1C));
1662           } else if (CI->isNullValue()) {
1663             // Replace ((add A, B) != 0) with (A != -B) if A or B is
1664             // efficiently invertible, or if the add has just this one use.
1665             Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
1666             
1667             if (Value *NegVal = dyn_castNegVal(BOp1))
1668               return new SetCondInst(I.getOpcode(), BOp0, NegVal);
1669             else if (Value *NegVal = dyn_castNegVal(BOp0))
1670               return new SetCondInst(I.getOpcode(), NegVal, BOp1);
1671             else if (BO->hasOneUse()) {
1672               Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName());
1673               BO->setName("");
1674               InsertNewInstBefore(Neg, I);
1675               return new SetCondInst(I.getOpcode(), BOp0, Neg);
1676             }
1677           }
1678           break;
1679         case Instruction::Xor:
1680           // For the xor case, we can xor two constants together, eliminating
1681           // the explicit xor.
1682           if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
1683             return BinaryOperator::create(I.getOpcode(), BO->getOperand(0),
1684                                   ConstantExpr::getXor(CI, BOC));
1685
1686           // FALLTHROUGH
1687         case Instruction::Sub:
1688           // Replace (([sub|xor] A, B) != 0) with (A != B)
1689           if (CI->isNullValue())
1690             return new SetCondInst(I.getOpcode(), BO->getOperand(0),
1691                                    BO->getOperand(1));
1692           break;
1693
1694         case Instruction::Or:
1695           // If bits are being or'd in that are not present in the constant we
1696           // are comparing against, then the comparison could never succeed!
1697           if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
1698             Constant *NotCI = ConstantExpr::getNot(CI);
1699             if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
1700               return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE));
1701           }
1702           break;
1703
1704         case Instruction::And:
1705           if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1706             // If bits are being compared against that are and'd out, then the
1707             // comparison can never succeed!
1708             if (!ConstantExpr::getAnd(CI,
1709                                       ConstantExpr::getNot(BOC))->isNullValue())
1710               return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE));
1711
1712             // If we have ((X & C) == C), turn it into ((X & C) != 0).
1713             if (CI == BOC && isOneBitSet(CI))
1714               return new SetCondInst(isSetNE ? Instruction::SetEQ :
1715                                      Instruction::SetNE, Op0,
1716                                      Constant::getNullValue(CI->getType()));
1717
1718             // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X
1719             // to be a signed value as appropriate.
1720             if (isSignBit(BOC)) {
1721               Value *X = BO->getOperand(0);
1722               // If 'X' is not signed, insert a cast now...
1723               if (!BOC->getType()->isSigned()) {
1724                 const Type *DestTy = BOC->getType()->getSignedVersion();
1725                 X = InsertCastBefore(X, DestTy, I);
1726               }
1727               return new SetCondInst(isSetNE ? Instruction::SetLT :
1728                                          Instruction::SetGE, X,
1729                                      Constant::getNullValue(X->getType()));
1730             }
1731             
1732             // ((X & ~7) == 0) --> X < 8
1733             if (CI->isNullValue() && isHighOnes(BOC)) {
1734               Value *X = BO->getOperand(0);
1735               Constant *NegX = ConstantExpr::getNeg(BOC);
1736
1737               // If 'X' is signed, insert a cast now.
1738               if (NegX->getType()->isSigned()) {
1739                 const Type *DestTy = NegX->getType()->getUnsignedVersion();
1740                 X = InsertCastBefore(X, DestTy, I);
1741                 NegX = ConstantExpr::getCast(NegX, DestTy);
1742               }
1743
1744               return new SetCondInst(isSetNE ? Instruction::SetGE :
1745                                      Instruction::SetLT, X, NegX);
1746             }
1747
1748           }
1749         default: break;
1750         }
1751       }
1752     } else {  // Not a SetEQ/SetNE
1753       // If the LHS is a cast from an integral value of the same size, 
1754       if (CastInst *Cast = dyn_cast<CastInst>(Op0)) {
1755         Value *CastOp = Cast->getOperand(0);
1756         const Type *SrcTy = CastOp->getType();
1757         unsigned SrcTySize = SrcTy->getPrimitiveSize();
1758         if (SrcTy != Cast->getType() && SrcTy->isInteger() &&
1759             SrcTySize == Cast->getType()->getPrimitiveSize()) {
1760           assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && 
1761                  "Source and destination signednesses should differ!");
1762           if (Cast->getType()->isSigned()) {
1763             // If this is a signed comparison, check for comparisons in the
1764             // vicinity of zero.
1765             if (I.getOpcode() == Instruction::SetLT && CI->isNullValue())
1766               // X < 0  => x > 127
1767               return BinaryOperator::createSetGT(CastOp,
1768                          ConstantUInt::get(SrcTy, (1ULL << (SrcTySize*8-1))-1));
1769             else if (I.getOpcode() == Instruction::SetGT &&
1770                      cast<ConstantSInt>(CI)->getValue() == -1)
1771               // X > -1  => x < 128
1772               return BinaryOperator::createSetLT(CastOp,
1773                          ConstantUInt::get(SrcTy, 1ULL << (SrcTySize*8-1)));
1774           } else {
1775             ConstantUInt *CUI = cast<ConstantUInt>(CI);
1776             if (I.getOpcode() == Instruction::SetLT &&
1777                 CUI->getValue() == 1ULL << (SrcTySize*8-1))
1778               // X < 128 => X > -1
1779               return BinaryOperator::createSetGT(CastOp,
1780                                                  ConstantSInt::get(SrcTy, -1));
1781             else if (I.getOpcode() == Instruction::SetGT &&
1782                      CUI->getValue() == (1ULL << (SrcTySize*8-1))-1)
1783               // X > 127 => X < 0
1784               return BinaryOperator::createSetLT(CastOp,
1785                                                  Constant::getNullValue(SrcTy));
1786           }
1787         }
1788       }
1789     }
1790
1791     // Check to see if we are comparing against the minimum or maximum value...
1792     if (CI->isMinValue()) {
1793       if (I.getOpcode() == Instruction::SetLT)       // A < MIN -> FALSE
1794         return ReplaceInstUsesWith(I, ConstantBool::False);
1795       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN -> TRUE
1796         return ReplaceInstUsesWith(I, ConstantBool::True);
1797       if (I.getOpcode() == Instruction::SetLE)       // A <= MIN -> A == MIN
1798         return BinaryOperator::createSetEQ(Op0, Op1);
1799       if (I.getOpcode() == Instruction::SetGT)       // A > MIN -> A != MIN
1800         return BinaryOperator::createSetNE(Op0, Op1);
1801
1802     } else if (CI->isMaxValue()) {
1803       if (I.getOpcode() == Instruction::SetGT)       // A > MAX -> FALSE
1804         return ReplaceInstUsesWith(I, ConstantBool::False);
1805       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX -> TRUE
1806         return ReplaceInstUsesWith(I, ConstantBool::True);
1807       if (I.getOpcode() == Instruction::SetGE)       // A >= MAX -> A == MAX
1808         return BinaryOperator::createSetEQ(Op0, Op1);
1809       if (I.getOpcode() == Instruction::SetLT)       // A < MAX -> A != MAX
1810         return BinaryOperator::createSetNE(Op0, Op1);
1811
1812       // Comparing against a value really close to min or max?
1813     } else if (isMinValuePlusOne(CI)) {
1814       if (I.getOpcode() == Instruction::SetLT)       // A < MIN+1 -> A == MIN
1815         return BinaryOperator::createSetEQ(Op0, SubOne(CI));
1816       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN-1 -> A != MIN
1817         return BinaryOperator::createSetNE(Op0, SubOne(CI));
1818
1819     } else if (isMaxValueMinusOne(CI)) {
1820       if (I.getOpcode() == Instruction::SetGT)       // A > MAX-1 -> A == MAX
1821         return BinaryOperator::createSetEQ(Op0, AddOne(CI));
1822       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX-1 -> A != MAX
1823         return BinaryOperator::createSetNE(Op0, AddOne(CI));
1824     }
1825
1826     // If we still have a setle or setge instruction, turn it into the
1827     // appropriate setlt or setgt instruction.  Since the border cases have
1828     // already been handled above, this requires little checking.
1829     //
1830     if (I.getOpcode() == Instruction::SetLE)
1831       return BinaryOperator::createSetLT(Op0, AddOne(CI));
1832     if (I.getOpcode() == Instruction::SetGE)
1833       return BinaryOperator::createSetGT(Op0, SubOne(CI));
1834   }
1835
1836   // Test to see if the operands of the setcc are casted versions of other
1837   // values.  If the cast can be stripped off both arguments, we do so now.
1838   if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
1839     Value *CastOp0 = CI->getOperand(0);
1840     if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) &&
1841         (isa<Constant>(Op1) || isa<CastInst>(Op1)) &&
1842         (I.getOpcode() == Instruction::SetEQ ||
1843          I.getOpcode() == Instruction::SetNE)) {
1844       // We keep moving the cast from the left operand over to the right
1845       // operand, where it can often be eliminated completely.
1846       Op0 = CastOp0;
1847       
1848       // If operand #1 is a cast instruction, see if we can eliminate it as
1849       // well.
1850       if (CastInst *CI2 = dyn_cast<CastInst>(Op1))
1851         if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo(
1852                                                                Op0->getType()))
1853           Op1 = CI2->getOperand(0);
1854       
1855       // If Op1 is a constant, we can fold the cast into the constant.
1856       if (Op1->getType() != Op0->getType())
1857         if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
1858           Op1 = ConstantExpr::getCast(Op1C, Op0->getType());
1859         } else {
1860           // Otherwise, cast the RHS right before the setcc
1861           Op1 = new CastInst(Op1, Op0->getType(), Op1->getName());
1862           InsertNewInstBefore(cast<Instruction>(Op1), I);
1863         }
1864       return BinaryOperator::create(I.getOpcode(), Op0, Op1);
1865     }
1866
1867     // Handle the special case of: setcc (cast bool to X), <cst>
1868     // This comes up when you have code like
1869     //   int X = A < B;
1870     //   if (X) ...
1871     // For generality, we handle any zero-extension of any operand comparison
1872     // with a constant.
1873     if (ConstantInt *ConstantRHS = dyn_cast<ConstantInt>(Op1)) {
1874       const Type *SrcTy = CastOp0->getType();
1875       const Type *DestTy = Op0->getType();
1876       if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() &&
1877           (SrcTy->isUnsigned() || SrcTy == Type::BoolTy)) {
1878         // Ok, we have an expansion of operand 0 into a new type.  Get the
1879         // constant value, masink off bits which are not set in the RHS.  These
1880         // could be set if the destination value is signed.
1881         uint64_t ConstVal = ConstantRHS->getRawValue();
1882         ConstVal &= (1ULL << DestTy->getPrimitiveSize()*8)-1;
1883
1884         // If the constant we are comparing it with has high bits set, which
1885         // don't exist in the original value, the values could never be equal,
1886         // because the source would be zero extended.
1887         unsigned SrcBits =
1888           SrcTy == Type::BoolTy ? 1 : SrcTy->getPrimitiveSize()*8;
1889         bool HasSignBit = ConstVal & (1ULL << (DestTy->getPrimitiveSize()*8-1));
1890         if (ConstVal & ~((1ULL << SrcBits)-1)) {
1891           switch (I.getOpcode()) {
1892           default: assert(0 && "Unknown comparison type!");
1893           case Instruction::SetEQ:
1894             return ReplaceInstUsesWith(I, ConstantBool::False);
1895           case Instruction::SetNE:
1896             return ReplaceInstUsesWith(I, ConstantBool::True);
1897           case Instruction::SetLT:
1898           case Instruction::SetLE:
1899             if (DestTy->isSigned() && HasSignBit)
1900               return ReplaceInstUsesWith(I, ConstantBool::False);
1901             return ReplaceInstUsesWith(I, ConstantBool::True);
1902           case Instruction::SetGT:
1903           case Instruction::SetGE:
1904             if (DestTy->isSigned() && HasSignBit)
1905               return ReplaceInstUsesWith(I, ConstantBool::True);
1906             return ReplaceInstUsesWith(I, ConstantBool::False);
1907           }
1908         }
1909         
1910         // Otherwise, we can replace the setcc with a setcc of the smaller
1911         // operand value.
1912         Op1 = ConstantExpr::getCast(cast<Constant>(Op1), SrcTy);
1913         return BinaryOperator::create(I.getOpcode(), CastOp0, Op1);
1914       }
1915     }
1916   }
1917   return Changed ? &I : 0;
1918 }
1919
1920
1921
1922 Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
1923   assert(I.getOperand(1)->getType() == Type::UByteTy);
1924   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1925   bool isLeftShift = I.getOpcode() == Instruction::Shl;
1926
1927   // shl X, 0 == X and shr X, 0 == X
1928   // shl 0, X == 0 and shr 0, X == 0
1929   if (Op1 == Constant::getNullValue(Type::UByteTy) ||
1930       Op0 == Constant::getNullValue(Op0->getType()))
1931     return ReplaceInstUsesWith(I, Op0);
1932
1933   // shr int -1, X = -1   (for any arithmetic shift rights of ~0)
1934   if (!isLeftShift)
1935     if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
1936       if (CSI->isAllOnesValue())
1937         return ReplaceInstUsesWith(I, CSI);
1938
1939   // Try to fold constant and into select arguments.
1940   if (isa<Constant>(Op0))
1941     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1942       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
1943         return R;
1944
1945   if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
1946     // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr
1947     // of a signed value.
1948     //
1949     unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
1950     if (CUI->getValue() >= TypeBits) {
1951       if (!Op0->getType()->isSigned() || isLeftShift)
1952         return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
1953       else {
1954         I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1));
1955         return &I;
1956       }
1957     }
1958
1959     // ((X*C1) << C2) == (X * (C1 << C2))
1960     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
1961       if (BO->getOpcode() == Instruction::Mul && isLeftShift)
1962         if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
1963           return BinaryOperator::createMul(BO->getOperand(0),
1964                                            ConstantExpr::getShl(BOOp, CUI));
1965     
1966     // Try to fold constant and into select arguments.
1967     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1968       if (Instruction *R = FoldBinOpIntoSelect(I, SI, this))
1969         return R;
1970
1971     // If the operand is an bitwise operator with a constant RHS, and the
1972     // shift is the only use, we can pull it out of the shift.
1973     if (Op0->hasOneUse())
1974       if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0))
1975         if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
1976           bool isValid = true;     // Valid only for And, Or, Xor
1977           bool highBitSet = false; // Transform if high bit of constant set?
1978
1979           switch (Op0BO->getOpcode()) {
1980           default: isValid = false; break;   // Do not perform transform!
1981           case Instruction::Or:
1982           case Instruction::Xor:
1983             highBitSet = false;
1984             break;
1985           case Instruction::And:
1986             highBitSet = true;
1987             break;
1988           }
1989
1990           // If this is a signed shift right, and the high bit is modified
1991           // by the logical operation, do not perform the transformation.
1992           // The highBitSet boolean indicates the value of the high bit of
1993           // the constant which would cause it to be modified for this
1994           // operation.
1995           //
1996           if (isValid && !isLeftShift && !I.getType()->isUnsigned()) {
1997             uint64_t Val = Op0C->getRawValue();
1998             isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet;
1999           }
2000
2001           if (isValid) {
2002             Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI);
2003
2004             Instruction *NewShift =
2005               new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI,
2006                             Op0BO->getName());
2007             Op0BO->setName("");
2008             InsertNewInstBefore(NewShift, I);
2009
2010             return BinaryOperator::create(Op0BO->getOpcode(), NewShift,
2011                                           NewRHS);
2012           }
2013         }
2014
2015     // If this is a shift of a shift, see if we can fold the two together...
2016     if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0))
2017       if (ConstantUInt *ShiftAmt1C =
2018                                  dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) {
2019         unsigned ShiftAmt1 = ShiftAmt1C->getValue();
2020         unsigned ShiftAmt2 = CUI->getValue();
2021         
2022         // Check for (A << c1) << c2   and   (A >> c1) >> c2
2023         if (I.getOpcode() == Op0SI->getOpcode()) {
2024           unsigned Amt = ShiftAmt1+ShiftAmt2;   // Fold into one big shift...
2025           if (Op0->getType()->getPrimitiveSize()*8 < Amt)
2026             Amt = Op0->getType()->getPrimitiveSize()*8;
2027           return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0),
2028                                ConstantUInt::get(Type::UByteTy, Amt));
2029         }
2030         
2031         // Check for (A << c1) >> c2 or visaversa.  If we are dealing with
2032         // signed types, we can only support the (A >> c1) << c2 configuration,
2033         // because it can not turn an arbitrary bit of A into a sign bit.
2034         if (I.getType()->isUnsigned() || isLeftShift) {
2035           // Calculate bitmask for what gets shifted off the edge...
2036           Constant *C = ConstantIntegral::getAllOnesValue(I.getType());
2037           if (isLeftShift)
2038             C = ConstantExpr::getShl(C, ShiftAmt1C);
2039           else
2040             C = ConstantExpr::getShr(C, ShiftAmt1C);
2041           
2042           Instruction *Mask =
2043             BinaryOperator::createAnd(Op0SI->getOperand(0), C,
2044                                       Op0SI->getOperand(0)->getName()+".mask");
2045           InsertNewInstBefore(Mask, I);
2046           
2047           // Figure out what flavor of shift we should use...
2048           if (ShiftAmt1 == ShiftAmt2)
2049             return ReplaceInstUsesWith(I, Mask);  // (A << c) >> c  === A & c2
2050           else if (ShiftAmt1 < ShiftAmt2) {
2051             return new ShiftInst(I.getOpcode(), Mask,
2052                          ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1));
2053           } else {
2054             return new ShiftInst(Op0SI->getOpcode(), Mask,
2055                          ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
2056           }
2057         }
2058       }
2059   }
2060
2061   return 0;
2062 }
2063
2064 enum CastType {
2065   Noop     = 0,
2066   Truncate = 1,
2067   Signext  = 2,
2068   Zeroext  = 3
2069 };
2070
2071 /// getCastType - In the future, we will split the cast instruction into these
2072 /// various types.  Until then, we have to do the analysis here.
2073 static CastType getCastType(const Type *Src, const Type *Dest) {
2074   assert(Src->isIntegral() && Dest->isIntegral() &&
2075          "Only works on integral types!");
2076   unsigned SrcSize = Src->getPrimitiveSize()*8;
2077   if (Src == Type::BoolTy) SrcSize = 1;
2078   unsigned DestSize = Dest->getPrimitiveSize()*8;
2079   if (Dest == Type::BoolTy) DestSize = 1;
2080
2081   if (SrcSize == DestSize) return Noop;
2082   if (SrcSize > DestSize)  return Truncate;
2083   if (Src->isSigned()) return Signext;
2084   return Zeroext;
2085 }
2086
2087
2088 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
2089 // instruction.
2090 //
2091 static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy,
2092                                           const Type *DstTy, TargetData *TD) {
2093
2094   // It is legal to eliminate the instruction if casting A->B->A if the sizes
2095   // are identical and the bits don't get reinterpreted (for example 
2096   // int->float->int would not be allowed).
2097   if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy))
2098     return true;
2099
2100   // If we are casting between pointer and integer types, treat pointers as
2101   // integers of the appropriate size for the code below.
2102   if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType();
2103   if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType();
2104   if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType();
2105
2106   // Allow free casting and conversion of sizes as long as the sign doesn't
2107   // change...
2108   if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
2109     CastType FirstCast = getCastType(SrcTy, MidTy);
2110     CastType SecondCast = getCastType(MidTy, DstTy);
2111
2112     // Capture the effect of these two casts.  If the result is a legal cast,
2113     // the CastType is stored here, otherwise a special code is used.
2114     static const unsigned CastResult[] = {
2115       // First cast is noop
2116       0, 1, 2, 3,
2117       // First cast is a truncate
2118       1, 1, 4, 4,         // trunc->extend is not safe to eliminate
2119       // First cast is a sign ext
2120       2, 5, 2, 4,         // signext->zeroext never ok
2121       // First cast is a zero ext
2122       3, 5, 3, 3,
2123     };
2124
2125     unsigned Result = CastResult[FirstCast*4+SecondCast];
2126     switch (Result) {
2127     default: assert(0 && "Illegal table value!");
2128     case 0:
2129     case 1:
2130     case 2:
2131     case 3:
2132       // FIXME: in the future, when LLVM has explicit sign/zeroextends and
2133       // truncates, we could eliminate more casts.
2134       return (unsigned)getCastType(SrcTy, DstTy) == Result;
2135     case 4:
2136       return false;  // Not possible to eliminate this here.
2137     case 5:
2138       // Sign or zero extend followed by truncate is always ok if the result
2139       // is a truncate or noop.
2140       CastType ResultCast = getCastType(SrcTy, DstTy);
2141       if (ResultCast == Noop || ResultCast == Truncate)
2142         return true;
2143       // Otherwise we are still growing the value, we are only safe if the 
2144       // result will match the sign/zeroextendness of the result.
2145       return ResultCast == FirstCast;
2146     }
2147   }
2148   return false;
2149 }
2150
2151 static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) {
2152   if (V->getType() == Ty || isa<Constant>(V)) return false;
2153   if (const CastInst *CI = dyn_cast<CastInst>(V))
2154     if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty,
2155                                TD))
2156       return false;
2157   return true;
2158 }
2159
2160 /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
2161 /// InsertBefore instruction.  This is specialized a bit to avoid inserting
2162 /// casts that are known to not do anything...
2163 ///
2164 Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy,
2165                                              Instruction *InsertBefore) {
2166   if (V->getType() == DestTy) return V;
2167   if (Constant *C = dyn_cast<Constant>(V))
2168     return ConstantExpr::getCast(C, DestTy);
2169
2170   CastInst *CI = new CastInst(V, DestTy, V->getName());
2171   InsertNewInstBefore(CI, *InsertBefore);
2172   return CI;
2173 }
2174
2175 // CastInst simplification
2176 //
2177 Instruction *InstCombiner::visitCastInst(CastInst &CI) {
2178   Value *Src = CI.getOperand(0);
2179
2180   // If the user is casting a value to the same type, eliminate this cast
2181   // instruction...
2182   if (CI.getType() == Src->getType())
2183     return ReplaceInstUsesWith(CI, Src);
2184
2185   // If casting the result of another cast instruction, try to eliminate this
2186   // one!
2187   //
2188   if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {
2189     if (isEliminableCastOfCast(CSrc->getOperand(0)->getType(),
2190                                CSrc->getType(), CI.getType(), TD)) {
2191       // This instruction now refers directly to the cast's src operand.  This
2192       // has a good chance of making CSrc dead.
2193       CI.setOperand(0, CSrc->getOperand(0));
2194       return &CI;
2195     }
2196
2197     // If this is an A->B->A cast, and we are dealing with integral types, try
2198     // to convert this into a logical 'and' instruction.
2199     //
2200     if (CSrc->getOperand(0)->getType() == CI.getType() &&
2201         CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
2202         CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
2203         CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
2204       assert(CSrc->getType() != Type::ULongTy &&
2205              "Cannot have type bigger than ulong!");
2206       uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1;
2207       Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
2208       return BinaryOperator::createAnd(CSrc->getOperand(0), AndOp);
2209     }
2210   }
2211
2212   // If this is a cast to bool, turn it into the appropriate setne instruction.
2213   if (CI.getType() == Type::BoolTy)
2214     return BinaryOperator::createSetNE(CI.getOperand(0),
2215                        Constant::getNullValue(CI.getOperand(0)->getType()));
2216
2217   // If casting the result of a getelementptr instruction with no offset, turn
2218   // this into a cast of the original pointer!
2219   //
2220   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
2221     bool AllZeroOperands = true;
2222     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
2223       if (!isa<Constant>(GEP->getOperand(i)) ||
2224           !cast<Constant>(GEP->getOperand(i))->isNullValue()) {
2225         AllZeroOperands = false;
2226         break;
2227       }
2228     if (AllZeroOperands) {
2229       CI.setOperand(0, GEP->getOperand(0));
2230       return &CI;
2231     }
2232   }
2233
2234   // If we are casting a malloc or alloca to a pointer to a type of the same
2235   // size, rewrite the allocation instruction to allocate the "right" type.
2236   //
2237   if (AllocationInst *AI = dyn_cast<AllocationInst>(Src))
2238     if (AI->hasOneUse() && !AI->isArrayAllocation())
2239       if (const PointerType *PTy = dyn_cast<PointerType>(CI.getType())) {
2240         // Get the type really allocated and the type casted to...
2241         const Type *AllocElTy = AI->getAllocatedType();
2242         const Type *CastElTy = PTy->getElementType();
2243         if (AllocElTy->isSized() && CastElTy->isSized()) {
2244           unsigned AllocElTySize = TD->getTypeSize(AllocElTy);
2245           unsigned CastElTySize = TD->getTypeSize(CastElTy);
2246
2247           // If the allocation is for an even multiple of the cast type size
2248           if (CastElTySize && (AllocElTySize % CastElTySize == 0)) {
2249             Value *Amt = ConstantUInt::get(Type::UIntTy, 
2250                                          AllocElTySize/CastElTySize);
2251             std::string Name = AI->getName(); AI->setName("");
2252             AllocationInst *New;
2253             if (isa<MallocInst>(AI))
2254               New = new MallocInst(CastElTy, Amt, Name);
2255             else
2256               New = new AllocaInst(CastElTy, Amt, Name);
2257             InsertNewInstBefore(New, *AI);
2258             return ReplaceInstUsesWith(CI, New);
2259           }
2260         }
2261       }
2262
2263   // If the source value is an instruction with only this use, we can attempt to
2264   // propagate the cast into the instruction.  Also, only handle integral types
2265   // for now.
2266   if (Instruction *SrcI = dyn_cast<Instruction>(Src))
2267     if (SrcI->hasOneUse() && Src->getType()->isIntegral() &&
2268         CI.getType()->isInteger()) {  // Don't mess with casts to bool here
2269       const Type *DestTy = CI.getType();
2270       unsigned SrcBitSize = getTypeSizeInBits(Src->getType());
2271       unsigned DestBitSize = getTypeSizeInBits(DestTy);
2272
2273       Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0;
2274       Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0;
2275
2276       switch (SrcI->getOpcode()) {
2277       case Instruction::Add:
2278       case Instruction::Mul:
2279       case Instruction::And:
2280       case Instruction::Or:
2281       case Instruction::Xor:
2282         // If we are discarding information, or just changing the sign, rewrite.
2283         if (DestBitSize <= SrcBitSize && DestBitSize != 1) {
2284           // Don't insert two casts if they cannot be eliminated.  We allow two
2285           // casts to be inserted if the sizes are the same.  This could only be
2286           // converting signedness, which is a noop.
2287           if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) ||
2288               !ValueRequiresCast(Op0, DestTy, TD)) {
2289             Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI);
2290             Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI);
2291             return BinaryOperator::create(cast<BinaryOperator>(SrcI)
2292                              ->getOpcode(), Op0c, Op1c);
2293           }
2294         }
2295         break;
2296       case Instruction::Shl:
2297         // Allow changing the sign of the source operand.  Do not allow changing
2298         // the size of the shift, UNLESS the shift amount is a constant.  We
2299         // mush not change variable sized shifts to a smaller size, because it
2300         // is undefined to shift more bits out than exist in the value.
2301         if (DestBitSize == SrcBitSize ||
2302             (DestBitSize < SrcBitSize && isa<Constant>(Op1))) {
2303           Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI);
2304           return new ShiftInst(Instruction::Shl, Op0c, Op1);
2305         }
2306         break;
2307       }
2308     }
2309   
2310   return 0;
2311 }
2312
2313 /// GetSelectFoldableOperands - We want to turn code that looks like this:
2314 ///   %C = or %A, %B
2315 ///   %D = select %cond, %C, %A
2316 /// into:
2317 ///   %C = select %cond, %B, 0
2318 ///   %D = or %A, %C
2319 ///
2320 /// Assuming that the specified instruction is an operand to the select, return
2321 /// a bitmask indicating which operands of this instruction are foldable if they
2322 /// equal the other incoming value of the select.
2323 ///
2324 static unsigned GetSelectFoldableOperands(Instruction *I) {
2325   switch (I->getOpcode()) {
2326   case Instruction::Add:
2327   case Instruction::Mul:
2328   case Instruction::And:
2329   case Instruction::Or:
2330   case Instruction::Xor:
2331     return 3;              // Can fold through either operand.
2332   case Instruction::Sub:   // Can only fold on the amount subtracted.
2333   case Instruction::Shl:   // Can only fold on the shift amount.
2334   case Instruction::Shr:
2335     return 1;           
2336   default:
2337     return 0;              // Cannot fold
2338   }
2339 }
2340
2341 /// GetSelectFoldableConstant - For the same transformation as the previous
2342 /// function, return the identity constant that goes into the select.
2343 static Constant *GetSelectFoldableConstant(Instruction *I) {
2344   switch (I->getOpcode()) {
2345   default: assert(0 && "This cannot happen!"); abort();
2346   case Instruction::Add:
2347   case Instruction::Sub:
2348   case Instruction::Or:
2349   case Instruction::Xor:
2350     return Constant::getNullValue(I->getType());
2351   case Instruction::Shl:
2352   case Instruction::Shr:
2353     return Constant::getNullValue(Type::UByteTy);
2354   case Instruction::And:
2355     return ConstantInt::getAllOnesValue(I->getType());
2356   case Instruction::Mul:
2357     return ConstantInt::get(I->getType(), 1);
2358   }
2359 }
2360
2361 Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
2362   Value *CondVal = SI.getCondition();
2363   Value *TrueVal = SI.getTrueValue();
2364   Value *FalseVal = SI.getFalseValue();
2365
2366   // select true, X, Y  -> X
2367   // select false, X, Y -> Y
2368   if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal))
2369     if (C == ConstantBool::True)
2370       return ReplaceInstUsesWith(SI, TrueVal);
2371     else {
2372       assert(C == ConstantBool::False);
2373       return ReplaceInstUsesWith(SI, FalseVal);
2374     }
2375
2376   // select C, X, X -> X
2377   if (TrueVal == FalseVal)
2378     return ReplaceInstUsesWith(SI, TrueVal);
2379
2380   if (SI.getType() == Type::BoolTy)
2381     if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) {
2382       if (C == ConstantBool::True) {
2383         // Change: A = select B, true, C --> A = or B, C
2384         return BinaryOperator::createOr(CondVal, FalseVal);
2385       } else {
2386         // Change: A = select B, false, C --> A = and !B, C
2387         Value *NotCond =
2388           InsertNewInstBefore(BinaryOperator::createNot(CondVal,
2389                                              "not."+CondVal->getName()), SI);
2390         return BinaryOperator::createAnd(NotCond, FalseVal);
2391       }
2392     } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) {
2393       if (C == ConstantBool::False) {
2394         // Change: A = select B, C, false --> A = and B, C
2395         return BinaryOperator::createAnd(CondVal, TrueVal);
2396       } else {
2397         // Change: A = select B, C, true --> A = or !B, C
2398         Value *NotCond =
2399           InsertNewInstBefore(BinaryOperator::createNot(CondVal,
2400                                              "not."+CondVal->getName()), SI);
2401         return BinaryOperator::createOr(NotCond, TrueVal);
2402       }
2403     }
2404
2405   // Selecting between two integer constants?
2406   if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
2407     if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
2408       // select C, 1, 0 -> cast C to int
2409       if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) {
2410         return new CastInst(CondVal, SI.getType());
2411       } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) {
2412         // select C, 0, 1 -> cast !C to int
2413         Value *NotCond =
2414           InsertNewInstBefore(BinaryOperator::createNot(CondVal,
2415                                                "not."+CondVal->getName()), SI);
2416         return new CastInst(NotCond, SI.getType());
2417       }
2418
2419       // If one of the constants is zero (we know they can't both be) and we
2420       // have a setcc instruction with zero, and we have an 'and' with the
2421       // non-constant value, eliminate this whole mess.  This corresponds to
2422       // cases like this: ((X & 27) ? 27 : 0)
2423       if (TrueValC->isNullValue() || FalseValC->isNullValue())
2424         if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition()))
2425           if ((IC->getOpcode() == Instruction::SetEQ ||
2426                IC->getOpcode() == Instruction::SetNE) &&
2427               isa<ConstantInt>(IC->getOperand(1)) &&
2428               cast<Constant>(IC->getOperand(1))->isNullValue())
2429             if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0)))
2430               if (ICA->getOpcode() == Instruction::And &&
2431                   isa<ConstantInt>(ICA->getOperand(1)) && 
2432                   (ICA->getOperand(1) == TrueValC || 
2433                    ICA->getOperand(1) == FalseValC) && 
2434                   isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) {
2435                 // Okay, now we know that everything is set up, we just don't
2436                 // know whether we have a setne or seteq and whether the true or
2437                 // false val is the zero.
2438                 bool ShouldNotVal = !TrueValC->isNullValue();
2439                 ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE;
2440                 Value *V = ICA;
2441                 if (ShouldNotVal)
2442                   V = InsertNewInstBefore(BinaryOperator::create(
2443                                   Instruction::Xor, V, ICA->getOperand(1)), SI);
2444                 return ReplaceInstUsesWith(SI, V);
2445               }
2446     }
2447
2448   // See if we are selecting two values based on a comparison of the two values.
2449   if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) {
2450     if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) {
2451       // Transform (X == Y) ? X : Y  -> Y
2452       if (SCI->getOpcode() == Instruction::SetEQ)
2453         return ReplaceInstUsesWith(SI, FalseVal);
2454       // Transform (X != Y) ? X : Y  -> X
2455       if (SCI->getOpcode() == Instruction::SetNE)
2456         return ReplaceInstUsesWith(SI, TrueVal);
2457       // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
2458
2459     } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){
2460       // Transform (X == Y) ? Y : X  -> X
2461       if (SCI->getOpcode() == Instruction::SetEQ)
2462         return ReplaceInstUsesWith(SI, FalseVal);
2463       // Transform (X != Y) ? Y : X  -> Y
2464       if (SCI->getOpcode() == Instruction::SetNE)
2465         return ReplaceInstUsesWith(SI, TrueVal);
2466       // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
2467     }
2468   }
2469   
2470   // See if we can fold the select into one of our operands.
2471   if (SI.getType()->isInteger()) {
2472     // See the comment above GetSelectFoldableOperands for a description of the
2473     // transformation we are doing here.
2474     if (Instruction *TVI = dyn_cast<Instruction>(TrueVal))
2475       if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
2476           !isa<Constant>(FalseVal))
2477         if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
2478           unsigned OpToFold = 0;
2479           if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
2480             OpToFold = 1;
2481           } else  if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
2482             OpToFold = 2;
2483           }
2484
2485           if (OpToFold) {
2486             Constant *C = GetSelectFoldableConstant(TVI);
2487             std::string Name = TVI->getName(); TVI->setName("");
2488             Instruction *NewSel =
2489               new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C,
2490                              Name);
2491             InsertNewInstBefore(NewSel, SI);
2492             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
2493               return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel);
2494             else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI))
2495               return new ShiftInst(SI->getOpcode(), FalseVal, NewSel);
2496             else {
2497               assert(0 && "Unknown instruction!!");
2498             }
2499           }
2500         }
2501     
2502     if (Instruction *FVI = dyn_cast<Instruction>(FalseVal))
2503       if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
2504           !isa<Constant>(TrueVal))
2505         if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
2506           unsigned OpToFold = 0;
2507           if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
2508             OpToFold = 1;
2509           } else  if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
2510             OpToFold = 2;
2511           }
2512
2513           if (OpToFold) {
2514             Constant *C = GetSelectFoldableConstant(FVI);
2515             std::string Name = FVI->getName(); FVI->setName("");
2516             Instruction *NewSel =
2517               new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold),
2518                              Name);
2519             InsertNewInstBefore(NewSel, SI);
2520             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
2521               return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel);
2522             else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI))
2523               return new ShiftInst(SI->getOpcode(), TrueVal, NewSel);
2524             else {
2525               assert(0 && "Unknown instruction!!");
2526             }
2527           }
2528         }
2529   }
2530   return 0;
2531 }
2532
2533
2534 // CallInst simplification
2535 //
2536 Instruction *InstCombiner::visitCallInst(CallInst &CI) {
2537   // Intrinsics cannot occur in an invoke, so handle them here instead of in
2538   // visitCallSite.
2539   if (Function *F = CI.getCalledFunction())
2540     switch (F->getIntrinsicID()) {
2541     case Intrinsic::memmove:
2542     case Intrinsic::memcpy:
2543     case Intrinsic::memset:
2544       // memmove/cpy/set of zero bytes is a noop.
2545       if (Constant *NumBytes = dyn_cast<Constant>(CI.getOperand(3))) {
2546         if (NumBytes->isNullValue())
2547           return EraseInstFromFunction(CI);
2548       }
2549       break;
2550     default:
2551       break;
2552     }
2553
2554   return visitCallSite(&CI);
2555 }
2556
2557 // InvokeInst simplification
2558 //
2559 Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
2560   return visitCallSite(&II);
2561 }
2562
2563 // visitCallSite - Improvements for call and invoke instructions.
2564 //
2565 Instruction *InstCombiner::visitCallSite(CallSite CS) {
2566   bool Changed = false;
2567
2568   // If the callee is a constexpr cast of a function, attempt to move the cast
2569   // to the arguments of the call/invoke.
2570   if (transformConstExprCastCall(CS)) return 0;
2571
2572   Value *Callee = CS.getCalledValue();
2573   const PointerType *PTy = cast<PointerType>(Callee->getType());
2574   const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
2575   if (FTy->isVarArg()) {
2576     // See if we can optimize any arguments passed through the varargs area of
2577     // the call.
2578     for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
2579            E = CS.arg_end(); I != E; ++I)
2580       if (CastInst *CI = dyn_cast<CastInst>(*I)) {
2581         // If this cast does not effect the value passed through the varargs
2582         // area, we can eliminate the use of the cast.
2583         Value *Op = CI->getOperand(0);
2584         if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) {
2585           *I = Op;
2586           Changed = true;
2587         }
2588       }
2589   }
2590   
2591   return Changed ? CS.getInstruction() : 0;
2592 }
2593
2594 // transformConstExprCastCall - If the callee is a constexpr cast of a function,
2595 // attempt to move the cast to the arguments of the call/invoke.
2596 //
2597 bool InstCombiner::transformConstExprCastCall(CallSite CS) {
2598   if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
2599   ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
2600   if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0)))
2601     return false;
2602   Function *Callee = cast<Function>(CE->getOperand(0));
2603   Instruction *Caller = CS.getInstruction();
2604
2605   // Okay, this is a cast from a function to a different type.  Unless doing so
2606   // would cause a type conversion of one of our arguments, change this call to
2607   // be a direct call with arguments casted to the appropriate types.
2608   //
2609   const FunctionType *FT = Callee->getFunctionType();
2610   const Type *OldRetTy = Caller->getType();
2611
2612   // Check to see if we are changing the return type...
2613   if (OldRetTy != FT->getReturnType()) {
2614     if (Callee->isExternal() &&
2615         !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) &&
2616         !Caller->use_empty())
2617       return false;   // Cannot transform this return value...
2618
2619     // If the callsite is an invoke instruction, and the return value is used by
2620     // a PHI node in a successor, we cannot change the return type of the call
2621     // because there is no place to put the cast instruction (without breaking
2622     // the critical edge).  Bail out in this case.
2623     if (!Caller->use_empty())
2624       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
2625         for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
2626              UI != E; ++UI)
2627           if (PHINode *PN = dyn_cast<PHINode>(*UI))
2628             if (PN->getParent() == II->getNormalDest() ||
2629                 PN->getParent() == II->getUnwindDest())
2630               return false;
2631   }
2632
2633   unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
2634   unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
2635                                     
2636   CallSite::arg_iterator AI = CS.arg_begin();
2637   for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
2638     const Type *ParamTy = FT->getParamType(i);
2639     bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy);
2640     if (Callee->isExternal() && !isConvertible) return false;    
2641   }
2642
2643   if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
2644       Callee->isExternal())
2645     return false;   // Do not delete arguments unless we have a function body...
2646
2647   // Okay, we decided that this is a safe thing to do: go ahead and start
2648   // inserting cast instructions as necessary...
2649   std::vector<Value*> Args;
2650   Args.reserve(NumActualArgs);
2651
2652   AI = CS.arg_begin();
2653   for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
2654     const Type *ParamTy = FT->getParamType(i);
2655     if ((*AI)->getType() == ParamTy) {
2656       Args.push_back(*AI);
2657     } else {
2658       Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"),
2659                                          *Caller));
2660     }
2661   }
2662
2663   // If the function takes more arguments than the call was taking, add them
2664   // now...
2665   for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
2666     Args.push_back(Constant::getNullValue(FT->getParamType(i)));
2667
2668   // If we are removing arguments to the function, emit an obnoxious warning...
2669   if (FT->getNumParams() < NumActualArgs)
2670     if (!FT->isVarArg()) {
2671       std::cerr << "WARNING: While resolving call to function '"
2672                 << Callee->getName() << "' arguments were dropped!\n";
2673     } else {
2674       // Add all of the arguments in their promoted form to the arg list...
2675       for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
2676         const Type *PTy = getPromotedType((*AI)->getType());
2677         if (PTy != (*AI)->getType()) {
2678           // Must promote to pass through va_arg area!
2679           Instruction *Cast = new CastInst(*AI, PTy, "tmp");
2680           InsertNewInstBefore(Cast, *Caller);
2681           Args.push_back(Cast);
2682         } else {
2683           Args.push_back(*AI);
2684         }
2685       }
2686     }
2687
2688   if (FT->getReturnType() == Type::VoidTy)
2689     Caller->setName("");   // Void type should not have a name...
2690
2691   Instruction *NC;
2692   if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
2693     NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(),
2694                         Args, Caller->getName(), Caller);
2695   } else {
2696     NC = new CallInst(Callee, Args, Caller->getName(), Caller);
2697   }
2698
2699   // Insert a cast of the return type as necessary...
2700   Value *NV = NC;
2701   if (Caller->getType() != NV->getType() && !Caller->use_empty()) {
2702     if (NV->getType() != Type::VoidTy) {
2703       NV = NC = new CastInst(NC, Caller->getType(), "tmp");
2704
2705       // If this is an invoke instruction, we should insert it after the first
2706       // non-phi, instruction in the normal successor block.
2707       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
2708         BasicBlock::iterator I = II->getNormalDest()->begin();
2709         while (isa<PHINode>(I)) ++I;
2710         InsertNewInstBefore(NC, *I);
2711       } else {
2712         // Otherwise, it's a call, just insert cast right after the call instr
2713         InsertNewInstBefore(NC, *Caller);
2714       }
2715       AddUsersToWorkList(*Caller);
2716     } else {
2717       NV = Constant::getNullValue(Caller->getType());
2718     }
2719   }
2720
2721   if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
2722     Caller->replaceAllUsesWith(NV);
2723   Caller->getParent()->getInstList().erase(Caller);
2724   removeFromWorkList(Caller);
2725   return true;
2726 }
2727
2728
2729
2730 // PHINode simplification
2731 //
2732 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
2733   if (Value *V = hasConstantValue(&PN))
2734     return ReplaceInstUsesWith(PN, V);
2735
2736   // If the only user of this instruction is a cast instruction, and all of the
2737   // incoming values are constants, change this PHI to merge together the casted
2738   // constants.
2739   if (PN.hasOneUse())
2740     if (CastInst *CI = dyn_cast<CastInst>(PN.use_back()))
2741       if (CI->getType() != PN.getType()) {  // noop casts will be folded
2742         bool AllConstant = true;
2743         for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2744           if (!isa<Constant>(PN.getIncomingValue(i))) {
2745             AllConstant = false;
2746             break;
2747           }
2748         if (AllConstant) {
2749           // Make a new PHI with all casted values.
2750           PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN);
2751           for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2752             Constant *OldArg = cast<Constant>(PN.getIncomingValue(i));
2753             New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()),
2754                              PN.getIncomingBlock(i));
2755           }
2756
2757           // Update the cast instruction.
2758           CI->setOperand(0, New);
2759           WorkList.push_back(CI);    // revisit the cast instruction to fold.
2760           WorkList.push_back(New);   // Make sure to revisit the new Phi
2761           return &PN;                // PN is now dead!
2762         }
2763       }
2764   return 0;
2765 }
2766
2767 static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy,
2768                                       Instruction *InsertPoint,
2769                                       InstCombiner *IC) {
2770   unsigned PS = IC->getTargetData().getPointerSize();
2771   const Type *VTy = V->getType();
2772   Instruction *Cast;
2773   if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS)
2774     // We must insert a cast to ensure we sign-extend.
2775     V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(),
2776                                              V->getName()), *InsertPoint);
2777   return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()),
2778                                  *InsertPoint);
2779 }
2780
2781
2782 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
2783   Value *PtrOp = GEP.getOperand(0);
2784   // Is it 'getelementptr %P, long 0'  or 'getelementptr %P'
2785   // If so, eliminate the noop.
2786   if (GEP.getNumOperands() == 1)
2787     return ReplaceInstUsesWith(GEP, PtrOp);
2788
2789   bool HasZeroPointerIndex = false;
2790   if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
2791     HasZeroPointerIndex = C->isNullValue();
2792
2793   if (GEP.getNumOperands() == 2 && HasZeroPointerIndex)
2794     return ReplaceInstUsesWith(GEP, PtrOp);
2795
2796   // Eliminate unneeded casts for indices.
2797   bool MadeChange = false;
2798   gep_type_iterator GTI = gep_type_begin(GEP);
2799   for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI)
2800     if (isa<SequentialType>(*GTI)) {
2801       if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) {
2802         Value *Src = CI->getOperand(0);
2803         const Type *SrcTy = Src->getType();
2804         const Type *DestTy = CI->getType();
2805         if (Src->getType()->isInteger()) {
2806           if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) {
2807             // We can always eliminate a cast from ulong or long to the other.
2808             // We can always eliminate a cast from uint to int or the other on
2809             // 32-bit pointer platforms.
2810             if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) {
2811               MadeChange = true;
2812               GEP.setOperand(i, Src);
2813             }
2814           } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() &&
2815                      SrcTy->getPrimitiveSize() == 4) {
2816             // We can always eliminate a cast from int to [u]long.  We can
2817             // eliminate a cast from uint to [u]long iff the target is a 32-bit
2818             // pointer target.
2819             if (SrcTy->isSigned() || 
2820                 SrcTy->getPrimitiveSize() >= TD->getPointerSize()) {
2821               MadeChange = true;
2822               GEP.setOperand(i, Src);
2823             }
2824           }
2825         }
2826       }
2827       // If we are using a wider index than needed for this platform, shrink it
2828       // to what we need.  If the incoming value needs a cast instruction,
2829       // insert it.  This explicit cast can make subsequent optimizations more
2830       // obvious.
2831       Value *Op = GEP.getOperand(i);
2832       if (Op->getType()->getPrimitiveSize() > TD->getPointerSize())
2833         if (Constant *C = dyn_cast<Constant>(Op)) {
2834           GEP.setOperand(i, ConstantExpr::getCast(C,
2835                                      TD->getIntPtrType()->getSignedVersion()));
2836           MadeChange = true;
2837         } else {
2838           Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(),
2839                                                 Op->getName()), GEP);
2840           GEP.setOperand(i, Op);
2841           MadeChange = true;
2842         }
2843
2844       // If this is a constant idx, make sure to canonicalize it to be a signed
2845       // operand, otherwise CSE and other optimizations are pessimized.
2846       if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) {
2847         GEP.setOperand(i, ConstantExpr::getCast(CUI,
2848                                           CUI->getType()->getSignedVersion()));
2849         MadeChange = true;
2850       }
2851     }
2852   if (MadeChange) return &GEP;
2853
2854   // Combine Indices - If the source pointer to this getelementptr instruction
2855   // is a getelementptr instruction, combine the indices of the two
2856   // getelementptr instructions into a single instruction.
2857   //
2858   std::vector<Value*> SrcGEPOperands;
2859   if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(PtrOp)) {
2860     SrcGEPOperands.assign(Src->op_begin(), Src->op_end());
2861   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) {
2862     if (CE->getOpcode() == Instruction::GetElementPtr)
2863       SrcGEPOperands.assign(CE->op_begin(), CE->op_end());
2864   }
2865
2866   if (!SrcGEPOperands.empty()) {
2867     // Note that if our source is a gep chain itself that we wait for that
2868     // chain to be resolved before we perform this transformation.  This
2869     // avoids us creating a TON of code in some cases.
2870     //
2871     if (isa<GetElementPtrInst>(SrcGEPOperands[0]) &&
2872         cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2)
2873       return 0;   // Wait until our source is folded to completion.
2874
2875     std::vector<Value *> Indices;
2876
2877     // Find out whether the last index in the source GEP is a sequential idx.
2878     bool EndsWithSequential = false;
2879     for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)),
2880            E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I)
2881       EndsWithSequential = !isa<StructType>(*I);
2882   
2883     // Can we combine the two pointer arithmetics offsets?
2884     if (EndsWithSequential) {
2885       // Replace: gep (gep %P, long B), long A, ...
2886       // With:    T = long A+B; gep %P, T, ...
2887       //
2888       Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1);
2889       if (SO1 == Constant::getNullValue(SO1->getType())) {
2890         Sum = GO1;
2891       } else if (GO1 == Constant::getNullValue(GO1->getType())) {
2892         Sum = SO1;
2893       } else {
2894         // If they aren't the same type, convert both to an integer of the
2895         // target's pointer size.
2896         if (SO1->getType() != GO1->getType()) {
2897           if (Constant *SO1C = dyn_cast<Constant>(SO1)) {
2898             SO1 = ConstantExpr::getCast(SO1C, GO1->getType());
2899           } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
2900             GO1 = ConstantExpr::getCast(GO1C, SO1->getType());
2901           } else {
2902             unsigned PS = TD->getPointerSize();
2903             Instruction *Cast;
2904             if (SO1->getType()->getPrimitiveSize() == PS) {
2905               // Convert GO1 to SO1's type.
2906               GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this);
2907
2908             } else if (GO1->getType()->getPrimitiveSize() == PS) {
2909               // Convert SO1 to GO1's type.
2910               SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this);
2911             } else {
2912               const Type *PT = TD->getIntPtrType();
2913               SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this);
2914               GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this);
2915             }
2916           }
2917         }
2918         if (isa<Constant>(SO1) && isa<Constant>(GO1))
2919           Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1));
2920         else {
2921           Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum");
2922           InsertNewInstBefore(cast<Instruction>(Sum), GEP);
2923         }
2924       }
2925
2926       // Recycle the GEP we already have if possible.
2927       if (SrcGEPOperands.size() == 2) {
2928         GEP.setOperand(0, SrcGEPOperands[0]);
2929         GEP.setOperand(1, Sum);
2930         return &GEP;
2931       } else {
2932         Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
2933                        SrcGEPOperands.end()-1);
2934         Indices.push_back(Sum);
2935         Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end());
2936       }
2937     } else if (isa<Constant>(*GEP.idx_begin()) && 
2938                cast<Constant>(*GEP.idx_begin())->isNullValue() &&
2939                SrcGEPOperands.size() != 1) { 
2940       // Otherwise we can do the fold if the first index of the GEP is a zero
2941       Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
2942                      SrcGEPOperands.end());
2943       Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
2944     }
2945
2946     if (!Indices.empty())
2947       return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName());
2948
2949   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) {
2950     // GEP of global variable.  If all of the indices for this GEP are
2951     // constants, we can promote this to a constexpr instead of an instruction.
2952
2953     // Scan for nonconstants...
2954     std::vector<Constant*> Indices;
2955     User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
2956     for (; I != E && isa<Constant>(*I); ++I)
2957       Indices.push_back(cast<Constant>(*I));
2958
2959     if (I == E) {  // If they are all constants...
2960       Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices);
2961
2962       // Replace all uses of the GEP with the new constexpr...
2963       return ReplaceInstUsesWith(GEP, CE);
2964     }
2965   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) {
2966     if (CE->getOpcode() == Instruction::Cast) {
2967       if (HasZeroPointerIndex) {
2968         // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ...
2969         // into     : GEP [10 x ubyte]* X, long 0, ...
2970         //
2971         // This occurs when the program declares an array extern like "int X[];"
2972         //
2973         Constant *X = CE->getOperand(0);
2974         const PointerType *CPTy = cast<PointerType>(CE->getType());
2975         if (const PointerType *XTy = dyn_cast<PointerType>(X->getType()))
2976           if (const ArrayType *XATy =
2977               dyn_cast<ArrayType>(XTy->getElementType()))
2978             if (const ArrayType *CATy =
2979                 dyn_cast<ArrayType>(CPTy->getElementType()))
2980               if (CATy->getElementType() == XATy->getElementType()) {
2981                 // At this point, we know that the cast source type is a pointer
2982                 // to an array of the same type as the destination pointer
2983                 // array.  Because the array type is never stepped over (there
2984                 // is a leading zero) we can fold the cast into this GEP.
2985                 GEP.setOperand(0, X);
2986                 return &GEP;
2987               }
2988       }
2989     }
2990   }
2991
2992   return 0;
2993 }
2994
2995 Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
2996   // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
2997   if (AI.isArrayAllocation())    // Check C != 1
2998     if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
2999       const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
3000       AllocationInst *New = 0;
3001
3002       // Create and insert the replacement instruction...
3003       if (isa<MallocInst>(AI))
3004         New = new MallocInst(NewTy, 0, AI.getName());
3005       else {
3006         assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
3007         New = new AllocaInst(NewTy, 0, AI.getName());
3008       }
3009
3010       InsertNewInstBefore(New, AI);
3011       
3012       // Scan to the end of the allocation instructions, to skip over a block of
3013       // allocas if possible...
3014       //
3015       BasicBlock::iterator It = New;
3016       while (isa<AllocationInst>(*It)) ++It;
3017
3018       // Now that I is pointing to the first non-allocation-inst in the block,
3019       // insert our getelementptr instruction...
3020       //
3021       std::vector<Value*> Idx(2, Constant::getNullValue(Type::IntTy));
3022       Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
3023
3024       // Now make everything use the getelementptr instead of the original
3025       // allocation.
3026       return ReplaceInstUsesWith(AI, V);
3027     }
3028
3029   // If alloca'ing a zero byte object, replace the alloca with a null pointer.
3030   // Note that we only do this for alloca's, because malloc should allocate and
3031   // return a unique pointer, even for a zero byte allocation.
3032   if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && 
3033       TD->getTypeSize(AI.getAllocatedType()) == 0)
3034     return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
3035
3036   return 0;
3037 }
3038
3039 Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
3040   Value *Op = FI.getOperand(0);
3041
3042   // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X
3043   if (CastInst *CI = dyn_cast<CastInst>(Op))
3044     if (isa<PointerType>(CI->getOperand(0)->getType())) {
3045       FI.setOperand(0, CI->getOperand(0));
3046       return &FI;
3047     }
3048
3049   // If we have 'free null' delete the instruction.  This can happen in stl code
3050   // when lots of inlining happens.
3051   if (isa<ConstantPointerNull>(Op))
3052     return EraseInstFromFunction(FI);
3053
3054   return 0;
3055 }
3056
3057
3058 /// GetGEPGlobalInitializer - Given a constant, and a getelementptr
3059 /// constantexpr, return the constant value being addressed by the constant
3060 /// expression, or null if something is funny.
3061 ///
3062 static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) {
3063   if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType()))
3064     return 0;  // Do not allow stepping over the value!
3065
3066   // Loop over all of the operands, tracking down which value we are
3067   // addressing...
3068   gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
3069   for (++I; I != E; ++I)
3070     if (const StructType *STy = dyn_cast<StructType>(*I)) {
3071       ConstantUInt *CU = cast<ConstantUInt>(I.getOperand());
3072       assert(CU->getValue() < STy->getNumElements() &&
3073              "Struct index out of range!");
3074       if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
3075         C = CS->getOperand(CU->getValue());
3076       } else if (isa<ConstantAggregateZero>(C)) {
3077         C = Constant::getNullValue(STy->getElementType(CU->getValue()));
3078       } else {
3079         return 0;
3080       }
3081     } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) {
3082       const ArrayType *ATy = cast<ArrayType>(*I);
3083       if ((uint64_t)CI->getRawValue() >= ATy->getNumElements()) return 0;
3084       if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
3085         C = CA->getOperand(CI->getRawValue());
3086       else if (isa<ConstantAggregateZero>(C))
3087         C = Constant::getNullValue(ATy->getElementType());
3088       else
3089         return 0;
3090     } else {
3091       return 0;
3092     }
3093   return C;
3094 }
3095
3096 static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) {
3097   User *CI = cast<User>(LI.getOperand(0));
3098
3099   const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
3100   if (const PointerType *SrcTy =
3101       dyn_cast<PointerType>(CI->getOperand(0)->getType())) {
3102     const Type *SrcPTy = SrcTy->getElementType();
3103     if (SrcPTy->isSized() && DestPTy->isSized() &&
3104         IC.getTargetData().getTypeSize(SrcPTy) == 
3105             IC.getTargetData().getTypeSize(DestPTy) &&
3106         (SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) &&
3107         (DestPTy->isInteger() || isa<PointerType>(DestPTy))) {
3108       // Okay, we are casting from one integer or pointer type to another of
3109       // the same size.  Instead of casting the pointer before the load, cast
3110       // the result of the loaded value.
3111       Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CI->getOperand(0),
3112                                                            CI->getName(),
3113                                                            LI.isVolatile()),LI);
3114       // Now cast the result of the load.
3115       return new CastInst(NewLoad, LI.getType());
3116     }
3117   }
3118   return 0;
3119 }
3120
3121 /// isSafeToLoadUnconditionally - Return true if we know that executing a load
3122 /// from this value cannot trap.  If it is not obviously safe to load from the
3123 /// specified pointer, we do a quick local scan of the basic block containing
3124 /// ScanFrom, to determine if the address is already accessed.
3125 static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) {
3126   // If it is an alloca or global variable, it is always safe to load from.
3127   if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true;
3128
3129   // Otherwise, be a little bit agressive by scanning the local block where we
3130   // want to check to see if the pointer is already being loaded or stored
3131   // from/to.  If so, the previous load or store would have already trapped,
3132   // so there is no harm doing an extra load (also, CSE will later eliminate
3133   // the load entirely).
3134   BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
3135
3136   while (BBI != E) {
3137     --BBI;
3138
3139     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
3140       if (LI->getOperand(0) == V) return true;
3141     } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
3142       if (SI->getOperand(1) == V) return true;
3143     
3144   }
3145   return false;
3146 }
3147
3148 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
3149   Value *Op = LI.getOperand(0);
3150
3151   if (Constant *C = dyn_cast<Constant>(Op))
3152     if (C->isNullValue() && !LI.isVolatile())  // load null -> 0
3153       return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
3154
3155   // Instcombine load (constant global) into the value loaded...
3156   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
3157     if (GV->isConstant() && !GV->isExternal())
3158       return ReplaceInstUsesWith(LI, GV->getInitializer());
3159
3160   // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded...
3161   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
3162     if (CE->getOpcode() == Instruction::GetElementPtr) {
3163       if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
3164         if (GV->isConstant() && !GV->isExternal())
3165           if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE))
3166             return ReplaceInstUsesWith(LI, V);
3167     } else if (CE->getOpcode() == Instruction::Cast) {
3168       if (Instruction *Res = InstCombineLoadCast(*this, LI))
3169         return Res;
3170     }
3171
3172   // load (cast X) --> cast (load X) iff safe
3173   if (CastInst *CI = dyn_cast<CastInst>(Op))
3174     if (Instruction *Res = InstCombineLoadCast(*this, LI))
3175       return Res;
3176
3177   if (!LI.isVolatile() && Op->hasOneUse()) {
3178     // Change select and PHI nodes to select values instead of addresses: this
3179     // helps alias analysis out a lot, allows many others simplifications, and
3180     // exposes redundancy in the code.
3181     //
3182     // Note that we cannot do the transformation unless we know that the
3183     // introduced loads cannot trap!  Something like this is valid as long as
3184     // the condition is always false: load (select bool %C, int* null, int* %G),
3185     // but it would not be valid if we transformed it to load from null
3186     // unconditionally.
3187     //
3188     if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
3189       // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2).
3190       if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) &&
3191           isSafeToLoadUnconditionally(SI->getOperand(2), SI)) {
3192         Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1),
3193                                      SI->getOperand(1)->getName()+".val"), LI);
3194         Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2),
3195                                      SI->getOperand(2)->getName()+".val"), LI);
3196         return new SelectInst(SI->getCondition(), V1, V2);
3197       }
3198
3199       // load (select (cond, null, P)) -> load P
3200       if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
3201         if (C->isNullValue()) {
3202           LI.setOperand(0, SI->getOperand(2));
3203           return &LI;
3204         }
3205
3206       // load (select (cond, P, null)) -> load P
3207       if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
3208         if (C->isNullValue()) {
3209           LI.setOperand(0, SI->getOperand(1));
3210           return &LI;
3211         }
3212
3213     } else if (PHINode *PN = dyn_cast<PHINode>(Op)) {
3214       // load (phi (&V1, &V2, &V3))  --> phi(load &V1, load &V2, load &V3)
3215       bool Safe = PN->getParent() == LI.getParent();
3216
3217       // Scan all of the instructions between the PHI and the load to make
3218       // sure there are no instructions that might possibly alter the value
3219       // loaded from the PHI.
3220       if (Safe) {
3221         BasicBlock::iterator I = &LI;
3222         for (--I; !isa<PHINode>(I); --I)
3223           if (isa<StoreInst>(I) || isa<CallInst>(I)) {
3224             Safe = false;
3225             break;
3226           }
3227       }
3228
3229       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i)
3230         if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i),
3231                                     PN->getIncomingBlock(i)->getTerminator()))
3232           Safe = false;
3233
3234       if (Safe) {
3235         // Create the PHI.
3236         PHINode *NewPN = new PHINode(LI.getType(), PN->getName());
3237         InsertNewInstBefore(NewPN, *PN);
3238         std::map<BasicBlock*,Value*> LoadMap;  // Don't insert duplicate loads
3239
3240         for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
3241           BasicBlock *BB = PN->getIncomingBlock(i);
3242           Value *&TheLoad = LoadMap[BB];
3243           if (TheLoad == 0) {
3244             Value *InVal = PN->getIncomingValue(i);
3245             TheLoad = InsertNewInstBefore(new LoadInst(InVal,
3246                                                        InVal->getName()+".val"),
3247                                           *BB->getTerminator());
3248           }
3249           NewPN->addIncoming(TheLoad, BB);
3250         }
3251         return ReplaceInstUsesWith(LI, NewPN);
3252       }
3253     }
3254   }
3255   return 0;
3256 }
3257
3258
3259 Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
3260   // Change br (not X), label True, label False to: br X, label False, True
3261   Value *X;
3262   BasicBlock *TrueDest;
3263   BasicBlock *FalseDest;
3264   if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
3265       !isa<Constant>(X)) {
3266     // Swap Destinations and condition...
3267     BI.setCondition(X);
3268     BI.setSuccessor(0, FalseDest);
3269     BI.setSuccessor(1, TrueDest);
3270     return &BI;
3271   }
3272
3273   // Cannonicalize setne -> seteq
3274   Instruction::BinaryOps Op; Value *Y;
3275   if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)),
3276                       TrueDest, FalseDest)))
3277     if ((Op == Instruction::SetNE || Op == Instruction::SetLE ||
3278          Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) {
3279       SetCondInst *I = cast<SetCondInst>(BI.getCondition());
3280       std::string Name = I->getName(); I->setName("");
3281       Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op);
3282       Value *NewSCC =  BinaryOperator::create(NewOpcode, X, Y, Name, I);
3283       // Swap Destinations and condition...
3284       BI.setCondition(NewSCC);
3285       BI.setSuccessor(0, FalseDest);
3286       BI.setSuccessor(1, TrueDest);
3287       removeFromWorkList(I);
3288       I->getParent()->getInstList().erase(I);
3289       WorkList.push_back(cast<Instruction>(NewSCC));
3290       return &BI;
3291     }
3292   
3293   return 0;
3294 }
3295
3296 Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
3297   Value *Cond = SI.getCondition();
3298   if (Instruction *I = dyn_cast<Instruction>(Cond)) {
3299     if (I->getOpcode() == Instruction::Add)
3300       if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
3301         // change 'switch (X+4) case 1:' into 'switch (X) case -3'
3302         for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
3303           SI.setOperand(i, ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
3304                                                 AddRHS));
3305         SI.setOperand(0, I->getOperand(0));
3306         WorkList.push_back(I);
3307         return &SI;
3308       }
3309   }
3310   return 0;
3311 }
3312
3313
3314 void InstCombiner::removeFromWorkList(Instruction *I) {
3315   WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
3316                  WorkList.end());
3317 }
3318
3319 bool InstCombiner::runOnFunction(Function &F) {
3320   bool Changed = false;
3321   TD = &getAnalysis<TargetData>();
3322
3323   for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
3324     WorkList.push_back(&*i);
3325
3326
3327   while (!WorkList.empty()) {
3328     Instruction *I = WorkList.back();  // Get an instruction from the worklist
3329     WorkList.pop_back();
3330
3331     // Check to see if we can DCE or ConstantPropagate the instruction...
3332     // Check to see if we can DIE the instruction...
3333     if (isInstructionTriviallyDead(I)) {
3334       // Add operands to the worklist...
3335       if (I->getNumOperands() < 4)
3336         AddUsesToWorkList(*I);
3337       ++NumDeadInst;
3338
3339       I->getParent()->getInstList().erase(I);
3340       removeFromWorkList(I);
3341       continue;
3342     }
3343
3344     // Instruction isn't dead, see if we can constant propagate it...
3345     if (Constant *C = ConstantFoldInstruction(I)) {
3346       // Add operands to the worklist...
3347       AddUsesToWorkList(*I);
3348       ReplaceInstUsesWith(*I, C);
3349
3350       ++NumConstProp;
3351       I->getParent()->getInstList().erase(I);
3352       removeFromWorkList(I);
3353       continue;
3354     }
3355
3356     // Now that we have an instruction, try combining it to simplify it...
3357     if (Instruction *Result = visit(*I)) {
3358       ++NumCombined;
3359       // Should we replace the old instruction with a new one?
3360       if (Result != I) {
3361         DEBUG(std::cerr << "IC: Old = " << *I
3362                         << "    New = " << *Result);
3363
3364         // Everything uses the new instruction now.
3365         I->replaceAllUsesWith(Result);
3366
3367         // Push the new instruction and any users onto the worklist.
3368         WorkList.push_back(Result);
3369         AddUsersToWorkList(*Result);
3370
3371         // Move the name to the new instruction first...
3372         std::string OldName = I->getName(); I->setName("");
3373         Result->setName(OldName);
3374
3375         // Insert the new instruction into the basic block...
3376         BasicBlock *InstParent = I->getParent();
3377         InstParent->getInstList().insert(I, Result);
3378
3379         // Make sure that we reprocess all operands now that we reduced their
3380         // use counts.
3381         for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
3382           if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i)))
3383             WorkList.push_back(OpI);
3384
3385         // Instructions can end up on the worklist more than once.  Make sure
3386         // we do not process an instruction that has been deleted.
3387         removeFromWorkList(I);
3388
3389         // Erase the old instruction.
3390         InstParent->getInstList().erase(I);
3391       } else {
3392         DEBUG(std::cerr << "IC: MOD = " << *I);
3393
3394         // If the instruction was modified, it's possible that it is now dead.
3395         // if so, remove it.
3396         if (isInstructionTriviallyDead(I)) {
3397           // Make sure we process all operands now that we are reducing their
3398           // use counts.
3399           for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
3400             if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i)))
3401               WorkList.push_back(OpI);
3402           
3403           // Instructions may end up in the worklist more than once.  Erase all
3404           // occurrances of this instruction.
3405           removeFromWorkList(I);
3406           I->getParent()->getInstList().erase(I);
3407         } else {
3408           WorkList.push_back(Result);
3409           AddUsersToWorkList(*Result);
3410         }
3411       }
3412       Changed = true;
3413     }
3414   }
3415
3416   return Changed;
3417 }
3418
3419 FunctionPass *llvm::createInstructionCombiningPass() {
3420   return new InstCombiner();
3421 }
3422