Revert "Include optional subclass flags, such as inbounds, nsw, etc., ...", this
[oota-llvm.git] / lib / Transforms / Scalar / InstructionCombining.cpp
1 //===- InstructionCombining.cpp - Combine multiple instructions -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // InstructionCombining - Combine instructions to form fewer, simple
11 // instructions.  This pass does not modify the CFG.  This pass is where
12 // algebraic simplification happens.
13 //
14 // This pass combines things like:
15 //    %Y = add i32 %X, 1
16 //    %Z = add i32 %Y, 1
17 // into:
18 //    %Z = add i32 %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. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
28 //    4. All cmp 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 //   ... etc.
33 //
34 //===----------------------------------------------------------------------===//
35
36 #define DEBUG_TYPE "instcombine"
37 #include "llvm/Transforms/Scalar.h"
38 #include "llvm/IntrinsicInst.h"
39 #include "llvm/LLVMContext.h"
40 #include "llvm/Pass.h"
41 #include "llvm/DerivedTypes.h"
42 #include "llvm/GlobalVariable.h"
43 #include "llvm/Operator.h"
44 #include "llvm/Analysis/ConstantFolding.h"
45 #include "llvm/Analysis/ValueTracking.h"
46 #include "llvm/Target/TargetData.h"
47 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
48 #include "llvm/Transforms/Utils/Local.h"
49 #include "llvm/Support/CallSite.h"
50 #include "llvm/Support/ConstantRange.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/GetElementPtrTypeIterator.h"
54 #include "llvm/Support/InstVisitor.h"
55 #include "llvm/Support/IRBuilder.h"
56 #include "llvm/Support/MathExtras.h"
57 #include "llvm/Support/PatternMatch.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/ADT/DenseMap.h"
60 #include "llvm/ADT/SmallVector.h"
61 #include "llvm/ADT/SmallPtrSet.h"
62 #include "llvm/ADT/Statistic.h"
63 #include "llvm/ADT/STLExtras.h"
64 #include <algorithm>
65 #include <climits>
66 using namespace llvm;
67 using namespace llvm::PatternMatch;
68
69 STATISTIC(NumCombined , "Number of insts combined");
70 STATISTIC(NumConstProp, "Number of constant folds");
71 STATISTIC(NumDeadInst , "Number of dead inst eliminated");
72 STATISTIC(NumDeadStore, "Number of dead stores eliminated");
73 STATISTIC(NumSunkInst , "Number of instructions sunk");
74
75 namespace {
76   /// InstCombineWorklist - This is the worklist management logic for
77   /// InstCombine.
78   class InstCombineWorklist {
79     SmallVector<Instruction*, 256> Worklist;
80     DenseMap<Instruction*, unsigned> WorklistMap;
81     
82     void operator=(const InstCombineWorklist&RHS);   // DO NOT IMPLEMENT
83     InstCombineWorklist(const InstCombineWorklist&); // DO NOT IMPLEMENT
84   public:
85     InstCombineWorklist() {}
86     
87     bool isEmpty() const { return Worklist.empty(); }
88     
89     /// Add - Add the specified instruction to the worklist if it isn't already
90     /// in it.
91     void Add(Instruction *I) {
92       if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second)
93         Worklist.push_back(I);
94     }
95     
96     void AddValue(Value *V) {
97       if (Instruction *I = dyn_cast<Instruction>(V))
98         Add(I);
99     }
100     
101     // Remove - remove I from the worklist if it exists.
102     void Remove(Instruction *I) {
103       DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
104       if (It == WorklistMap.end()) return; // Not in worklist.
105       
106       // Don't bother moving everything down, just null out the slot.
107       Worklist[It->second] = 0;
108       
109       WorklistMap.erase(It);
110     }
111     
112     Instruction *RemoveOne() {
113       Instruction *I = Worklist.back();
114       Worklist.pop_back();
115       WorklistMap.erase(I);
116       return I;
117     }
118
119     /// AddUsersToWorkList - When an instruction is simplified, add all users of
120     /// the instruction to the work lists because they might get more simplified
121     /// now.
122     ///
123     void AddUsersToWorkList(Instruction &I) {
124       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
125            UI != UE; ++UI)
126         Add(cast<Instruction>(*UI));
127     }
128     
129     
130     /// Zap - check that the worklist is empty and nuke the backing store for
131     /// the map if it is large.
132     void Zap() {
133       assert(WorklistMap.empty() && "Worklist empty, but map not?");
134       
135       // Do an explicit clear, this shrinks the map if needed.
136       WorklistMap.clear();
137     }
138   };
139 } // end anonymous namespace.
140
141
142 namespace {
143   /// InstCombineIRInserter - This is an IRBuilder insertion helper that works
144   /// just like the normal insertion helper, but also adds any new instructions
145   /// to the instcombine worklist.
146   class InstCombineIRInserter : public IRBuilderDefaultInserter<true> {
147     InstCombineWorklist &Worklist;
148   public:
149     InstCombineIRInserter(InstCombineWorklist &WL) : Worklist(WL) {}
150     
151     void InsertHelper(Instruction *I, const Twine &Name,
152                       BasicBlock *BB, BasicBlock::iterator InsertPt) const {
153       IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
154       Worklist.Add(I);
155     }
156   };
157 } // end anonymous namespace
158
159
160 namespace {
161   class InstCombiner : public FunctionPass,
162                        public InstVisitor<InstCombiner, Instruction*> {
163     TargetData *TD;
164     bool MustPreserveLCSSA;
165     bool MadeIRChange;
166   public:
167     /// Worklist - All of the instructions that need to be simplified.
168     InstCombineWorklist Worklist;
169
170     /// Builder - This is an IRBuilder that automatically inserts new
171     /// instructions into the worklist when they are created.
172     typedef IRBuilder<true, ConstantFolder, InstCombineIRInserter> BuilderTy;
173     BuilderTy *Builder;
174         
175     static char ID; // Pass identification, replacement for typeid
176     InstCombiner() : FunctionPass(&ID), TD(0), Builder(0) {}
177
178     LLVMContext *Context;
179     LLVMContext *getContext() const { return Context; }
180
181   public:
182     virtual bool runOnFunction(Function &F);
183     
184     bool DoOneIteration(Function &F, unsigned ItNum);
185
186     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
187       AU.addPreservedID(LCSSAID);
188       AU.setPreservesCFG();
189     }
190
191     TargetData *getTargetData() const { return TD; }
192
193     // Visitation implementation - Implement instruction combining for different
194     // instruction types.  The semantics are as follows:
195     // Return Value:
196     //    null        - No change was made
197     //     I          - Change was made, I is still valid, I may be dead though
198     //   otherwise    - Change was made, replace I with returned instruction
199     //
200     Instruction *visitAdd(BinaryOperator &I);
201     Instruction *visitFAdd(BinaryOperator &I);
202     Instruction *visitSub(BinaryOperator &I);
203     Instruction *visitFSub(BinaryOperator &I);
204     Instruction *visitMul(BinaryOperator &I);
205     Instruction *visitFMul(BinaryOperator &I);
206     Instruction *visitURem(BinaryOperator &I);
207     Instruction *visitSRem(BinaryOperator &I);
208     Instruction *visitFRem(BinaryOperator &I);
209     bool SimplifyDivRemOfSelect(BinaryOperator &I);
210     Instruction *commonRemTransforms(BinaryOperator &I);
211     Instruction *commonIRemTransforms(BinaryOperator &I);
212     Instruction *commonDivTransforms(BinaryOperator &I);
213     Instruction *commonIDivTransforms(BinaryOperator &I);
214     Instruction *visitUDiv(BinaryOperator &I);
215     Instruction *visitSDiv(BinaryOperator &I);
216     Instruction *visitFDiv(BinaryOperator &I);
217     Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
218     Instruction *FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS);
219     Instruction *visitAnd(BinaryOperator &I);
220     Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
221     Instruction *FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS);
222     Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op,
223                                      Value *A, Value *B, Value *C);
224     Instruction *visitOr (BinaryOperator &I);
225     Instruction *visitXor(BinaryOperator &I);
226     Instruction *visitShl(BinaryOperator &I);
227     Instruction *visitAShr(BinaryOperator &I);
228     Instruction *visitLShr(BinaryOperator &I);
229     Instruction *commonShiftTransforms(BinaryOperator &I);
230     Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
231                                       Constant *RHSC);
232     Instruction *visitFCmpInst(FCmpInst &I);
233     Instruction *visitICmpInst(ICmpInst &I);
234     Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
235     Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
236                                                 Instruction *LHS,
237                                                 ConstantInt *RHS);
238     Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
239                                 ConstantInt *DivRHS);
240
241     Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
242                              ICmpInst::Predicate Cond, Instruction &I);
243     Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
244                                      BinaryOperator &I);
245     Instruction *commonCastTransforms(CastInst &CI);
246     Instruction *commonIntCastTransforms(CastInst &CI);
247     Instruction *commonPointerCastTransforms(CastInst &CI);
248     Instruction *visitTrunc(TruncInst &CI);
249     Instruction *visitZExt(ZExtInst &CI);
250     Instruction *visitSExt(SExtInst &CI);
251     Instruction *visitFPTrunc(FPTruncInst &CI);
252     Instruction *visitFPExt(CastInst &CI);
253     Instruction *visitFPToUI(FPToUIInst &FI);
254     Instruction *visitFPToSI(FPToSIInst &FI);
255     Instruction *visitUIToFP(CastInst &CI);
256     Instruction *visitSIToFP(CastInst &CI);
257     Instruction *visitPtrToInt(PtrToIntInst &CI);
258     Instruction *visitIntToPtr(IntToPtrInst &CI);
259     Instruction *visitBitCast(BitCastInst &CI);
260     Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
261                                 Instruction *FI);
262     Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*);
263     Instruction *visitSelectInst(SelectInst &SI);
264     Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
265     Instruction *visitCallInst(CallInst &CI);
266     Instruction *visitInvokeInst(InvokeInst &II);
267     Instruction *visitPHINode(PHINode &PN);
268     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
269     Instruction *visitAllocationInst(AllocationInst &AI);
270     Instruction *visitFreeInst(FreeInst &FI);
271     Instruction *visitLoadInst(LoadInst &LI);
272     Instruction *visitStoreInst(StoreInst &SI);
273     Instruction *visitBranchInst(BranchInst &BI);
274     Instruction *visitSwitchInst(SwitchInst &SI);
275     Instruction *visitInsertElementInst(InsertElementInst &IE);
276     Instruction *visitExtractElementInst(ExtractElementInst &EI);
277     Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
278     Instruction *visitExtractValueInst(ExtractValueInst &EV);
279
280     // visitInstruction - Specify what to return for unhandled instructions...
281     Instruction *visitInstruction(Instruction &I) { return 0; }
282
283   private:
284     Instruction *visitCallSite(CallSite CS);
285     bool transformConstExprCastCall(CallSite CS);
286     Instruction *transformCallThroughTrampoline(CallSite CS);
287     Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
288                                    bool DoXform = true);
289     bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
290     DbgDeclareInst *hasOneUsePlusDeclare(Value *V);
291
292
293   public:
294     // InsertNewInstBefore - insert an instruction New before instruction Old
295     // in the program.  Add the new instruction to the worklist.
296     //
297     Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
298       assert(New && New->getParent() == 0 &&
299              "New instruction already inserted into a basic block!");
300       BasicBlock *BB = Old.getParent();
301       BB->getInstList().insert(&Old, New);  // Insert inst
302       Worklist.Add(New);
303       return New;
304     }
305         
306     // ReplaceInstUsesWith - This method is to be used when an instruction is
307     // found to be dead, replacable with another preexisting expression.  Here
308     // we add all uses of I to the worklist, replace all uses of I with the new
309     // value, then return I, so that the inst combiner will know that I was
310     // modified.
311     //
312     Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
313       Worklist.AddUsersToWorkList(I);   // Add all modified instrs to worklist.
314       
315       // If we are replacing the instruction with itself, this must be in a
316       // segment of unreachable code, so just clobber the instruction.
317       if (&I == V) 
318         V = UndefValue::get(I.getType());
319         
320       I.replaceAllUsesWith(V);
321       return &I;
322     }
323
324     // EraseInstFromFunction - When dealing with an instruction that has side
325     // effects or produces a void value, we can't rely on DCE to delete the
326     // instruction.  Instead, visit methods should return the value returned by
327     // this function.
328     Instruction *EraseInstFromFunction(Instruction &I) {
329       DEBUG(errs() << "IC: erase " << I);
330
331       assert(I.use_empty() && "Cannot erase instruction that is used!");
332       // Make sure that we reprocess all operands now that we reduced their
333       // use counts.
334       if (I.getNumOperands() < 8) {
335         for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
336           if (Instruction *Op = dyn_cast<Instruction>(*i))
337             Worklist.Add(Op);
338       }
339       Worklist.Remove(&I);
340       I.eraseFromParent();
341       MadeIRChange = true;
342       return 0;  // Don't do anything with FI
343     }
344         
345     void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero,
346                            APInt &KnownOne, unsigned Depth = 0) const {
347       return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
348     }
349     
350     bool MaskedValueIsZero(Value *V, const APInt &Mask, 
351                            unsigned Depth = 0) const {
352       return llvm::MaskedValueIsZero(V, Mask, TD, Depth);
353     }
354     unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const {
355       return llvm::ComputeNumSignBits(Op, TD, Depth);
356     }
357
358   private:
359
360     /// SimplifyCommutative - This performs a few simplifications for 
361     /// commutative operators.
362     bool SimplifyCommutative(BinaryOperator &I);
363
364     /// SimplifyCompare - This reorders the operands of a CmpInst to get them in
365     /// most-complex to least-complex order.
366     bool SimplifyCompare(CmpInst &I);
367
368     /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
369     /// based on the demanded bits.
370     Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, 
371                                    APInt& KnownZero, APInt& KnownOne,
372                                    unsigned Depth);
373     bool SimplifyDemandedBits(Use &U, APInt DemandedMask, 
374                               APInt& KnownZero, APInt& KnownOne,
375                               unsigned Depth=0);
376         
377     /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
378     /// SimplifyDemandedBits knows about.  See if the instruction has any
379     /// properties that allow us to simplify its operands.
380     bool SimplifyDemandedInstructionBits(Instruction &Inst);
381         
382     Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
383                                       APInt& UndefElts, unsigned Depth = 0);
384       
385     // FoldOpIntoPhi - Given a binary operator or cast instruction which has a
386     // PHI node as operand #0, see if we can fold the instruction into the PHI
387     // (which is only possible if all operands to the PHI are constants).
388     Instruction *FoldOpIntoPhi(Instruction &I);
389
390     // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
391     // operator and they all are only used by the PHI, PHI together their
392     // inputs, and do the operation once, to the result of the PHI.
393     Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
394     Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
395     Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
396
397     
398     Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
399                           ConstantInt *AndRHS, BinaryOperator &TheAnd);
400     
401     Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
402                               bool isSub, Instruction &I);
403     Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
404                                  bool isSigned, bool Inside, Instruction &IB);
405     Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI);
406     Instruction *MatchBSwap(BinaryOperator &I);
407     bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
408     Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
409     Instruction *SimplifyMemSet(MemSetInst *MI);
410
411
412     Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned);
413
414     bool CanEvaluateInDifferentType(Value *V, const Type *Ty,
415                                     unsigned CastOpc, int &NumCastsRemoved);
416     unsigned GetOrEnforceKnownAlignment(Value *V,
417                                         unsigned PrefAlign = 0);
418
419   };
420 } // end anonymous namespace
421
422 char InstCombiner::ID = 0;
423 static RegisterPass<InstCombiner>
424 X("instcombine", "Combine redundant instructions");
425
426 // getComplexity:  Assign a complexity or rank value to LLVM Values...
427 //   0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
428 static unsigned getComplexity(Value *V) {
429   if (isa<Instruction>(V)) {
430     if (BinaryOperator::isNeg(V) ||
431         BinaryOperator::isFNeg(V) ||
432         BinaryOperator::isNot(V))
433       return 3;
434     return 4;
435   }
436   if (isa<Argument>(V)) return 3;
437   return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
438 }
439
440 // isOnlyUse - Return true if this instruction will be deleted if we stop using
441 // it.
442 static bool isOnlyUse(Value *V) {
443   return V->hasOneUse() || isa<Constant>(V);
444 }
445
446 // getPromotedType - Return the specified type promoted as it would be to pass
447 // though a va_arg area...
448 static const Type *getPromotedType(const Type *Ty) {
449   if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
450     if (ITy->getBitWidth() < 32)
451       return Type::getInt32Ty(Ty->getContext());
452   }
453   return Ty;
454 }
455
456 /// getBitCastOperand - If the specified operand is a CastInst, a constant
457 /// expression bitcast, or a GetElementPtrInst with all zero indices, return the
458 /// operand value, otherwise return null.
459 static Value *getBitCastOperand(Value *V) {
460   if (Operator *O = dyn_cast<Operator>(V)) {
461     if (O->getOpcode() == Instruction::BitCast)
462       return O->getOperand(0);
463     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
464       if (GEP->hasAllZeroIndices())
465         return GEP->getPointerOperand();
466   }
467   return 0;
468 }
469
470 /// This function is a wrapper around CastInst::isEliminableCastPair. It
471 /// simply extracts arguments and returns what that function returns.
472 static Instruction::CastOps 
473 isEliminableCastPair(
474   const CastInst *CI, ///< The first cast instruction
475   unsigned opcode,       ///< The opcode of the second cast instruction
476   const Type *DstTy,     ///< The target type for the second cast instruction
477   TargetData *TD         ///< The target data for pointer size
478 ) {
479
480   const Type *SrcTy = CI->getOperand(0)->getType();   // A from above
481   const Type *MidTy = CI->getType();                  // B from above
482
483   // Get the opcodes of the two Cast instructions
484   Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
485   Instruction::CastOps secondOp = Instruction::CastOps(opcode);
486
487   unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
488                                                 DstTy,
489                                   TD ? TD->getIntPtrType(CI->getContext()) : 0);
490   
491   // We don't want to form an inttoptr or ptrtoint that converts to an integer
492   // type that differs from the pointer size.
493   if ((Res == Instruction::IntToPtr &&
494           (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) ||
495       (Res == Instruction::PtrToInt &&
496           (!TD || DstTy != TD->getIntPtrType(CI->getContext()))))
497     Res = 0;
498   
499   return Instruction::CastOps(Res);
500 }
501
502 /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
503 /// in any code being generated.  It does not require codegen if V is simple
504 /// enough or if the cast can be folded into other casts.
505 static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, 
506                               const Type *Ty, TargetData *TD) {
507   if (V->getType() == Ty || isa<Constant>(V)) return false;
508   
509   // If this is another cast that can be eliminated, it isn't codegen either.
510   if (const CastInst *CI = dyn_cast<CastInst>(V))
511     if (isEliminableCastPair(CI, opcode, Ty, TD))
512       return false;
513   return true;
514 }
515
516 // SimplifyCommutative - This performs a few simplifications for commutative
517 // operators:
518 //
519 //  1. Order operands such that they are listed from right (least complex) to
520 //     left (most complex).  This puts constants before unary operators before
521 //     binary operators.
522 //
523 //  2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
524 //  3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
525 //
526 bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
527   bool Changed = false;
528   if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
529     Changed = !I.swapOperands();
530
531   if (!I.isAssociative()) return Changed;
532   Instruction::BinaryOps Opcode = I.getOpcode();
533   if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
534     if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
535       if (isa<Constant>(I.getOperand(1))) {
536         Constant *Folded = ConstantExpr::get(I.getOpcode(),
537                                              cast<Constant>(I.getOperand(1)),
538                                              cast<Constant>(Op->getOperand(1)));
539         I.setOperand(0, Op->getOperand(0));
540         I.setOperand(1, Folded);
541         return true;
542       } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
543         if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
544             isOnlyUse(Op) && isOnlyUse(Op1)) {
545           Constant *C1 = cast<Constant>(Op->getOperand(1));
546           Constant *C2 = cast<Constant>(Op1->getOperand(1));
547
548           // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
549           Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
550           Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0),
551                                                     Op1->getOperand(0),
552                                                     Op1->getName(), &I);
553           Worklist.Add(New);
554           I.setOperand(0, New);
555           I.setOperand(1, Folded);
556           return true;
557         }
558     }
559   return Changed;
560 }
561
562 /// SimplifyCompare - For a CmpInst this function just orders the operands
563 /// so that theyare listed from right (least complex) to left (most complex).
564 /// This puts constants before unary operators before binary operators.
565 bool InstCombiner::SimplifyCompare(CmpInst &I) {
566   if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1)))
567     return false;
568   I.swapOperands();
569   // Compare instructions are not associative so there's nothing else we can do.
570   return true;
571 }
572
573 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
574 // if the LHS is a constant zero (which is the 'negate' form).
575 //
576 static inline Value *dyn_castNegVal(Value *V) {
577   if (BinaryOperator::isNeg(V))
578     return BinaryOperator::getNegArgument(V);
579
580   // Constants can be considered to be negated values if they can be folded.
581   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
582     return ConstantExpr::getNeg(C);
583
584   if (ConstantVector *C = dyn_cast<ConstantVector>(V))
585     if (C->getType()->getElementType()->isInteger())
586       return ConstantExpr::getNeg(C);
587
588   return 0;
589 }
590
591 // dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the
592 // instruction if the LHS is a constant negative zero (which is the 'negate'
593 // form).
594 //
595 static inline Value *dyn_castFNegVal(Value *V) {
596   if (BinaryOperator::isFNeg(V))
597     return BinaryOperator::getFNegArgument(V);
598
599   // Constants can be considered to be negated values if they can be folded.
600   if (ConstantFP *C = dyn_cast<ConstantFP>(V))
601     return ConstantExpr::getFNeg(C);
602
603   if (ConstantVector *C = dyn_cast<ConstantVector>(V))
604     if (C->getType()->getElementType()->isFloatingPoint())
605       return ConstantExpr::getFNeg(C);
606
607   return 0;
608 }
609
610 static inline Value *dyn_castNotVal(Value *V) {
611   if (BinaryOperator::isNot(V))
612     return BinaryOperator::getNotArgument(V);
613
614   // Constants can be considered to be not'ed values...
615   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
616     return ConstantInt::get(C->getType(), ~C->getValue());
617   return 0;
618 }
619
620 // dyn_castFoldableMul - If this value is a multiply that can be folded into
621 // other computations (because it has a constant operand), return the
622 // non-constant operand of the multiply, and set CST to point to the multiplier.
623 // Otherwise, return null.
624 //
625 static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
626   if (V->hasOneUse() && V->getType()->isInteger())
627     if (Instruction *I = dyn_cast<Instruction>(V)) {
628       if (I->getOpcode() == Instruction::Mul)
629         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
630           return I->getOperand(0);
631       if (I->getOpcode() == Instruction::Shl)
632         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
633           // The multiplier is really 1 << CST.
634           uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
635           uint32_t CSTVal = CST->getLimitedValue(BitWidth);
636           CST = ConstantInt::get(V->getType()->getContext(),
637                                  APInt(BitWidth, 1).shl(CSTVal));
638           return I->getOperand(0);
639         }
640     }
641   return 0;
642 }
643
644 /// AddOne - Add one to a ConstantInt
645 static Constant *AddOne(Constant *C) {
646   return ConstantExpr::getAdd(C, 
647     ConstantInt::get(C->getType(), 1));
648 }
649 /// SubOne - Subtract one from a ConstantInt
650 static Constant *SubOne(ConstantInt *C) {
651   return ConstantExpr::getSub(C, 
652     ConstantInt::get(C->getType(), 1));
653 }
654 /// MultiplyOverflows - True if the multiply can not be expressed in an int
655 /// this size.
656 static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
657   uint32_t W = C1->getBitWidth();
658   APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
659   if (sign) {
660     LHSExt.sext(W * 2);
661     RHSExt.sext(W * 2);
662   } else {
663     LHSExt.zext(W * 2);
664     RHSExt.zext(W * 2);
665   }
666
667   APInt MulExt = LHSExt * RHSExt;
668
669   if (sign) {
670     APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
671     APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
672     return MulExt.slt(Min) || MulExt.sgt(Max);
673   } else 
674     return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
675 }
676
677
678 /// ShrinkDemandedConstant - Check to see if the specified operand of the 
679 /// specified instruction is a constant integer.  If so, check to see if there
680 /// are any bits set in the constant that are not demanded.  If so, shrink the
681 /// constant and return true.
682 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, 
683                                    APInt Demanded) {
684   assert(I && "No instruction?");
685   assert(OpNo < I->getNumOperands() && "Operand index too large");
686
687   // If the operand is not a constant integer, nothing to do.
688   ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
689   if (!OpC) return false;
690
691   // If there are no bits set that aren't demanded, nothing to do.
692   Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
693   if ((~Demanded & OpC->getValue()) == 0)
694     return false;
695
696   // This instruction is producing bits that are not demanded. Shrink the RHS.
697   Demanded &= OpC->getValue();
698   I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
699   return true;
700 }
701
702 // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a 
703 // set of known zero and one bits, compute the maximum and minimum values that
704 // could have the specified known zero and known one bits, returning them in
705 // min/max.
706 static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero,
707                                                    const APInt& KnownOne,
708                                                    APInt& Min, APInt& Max) {
709   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
710          KnownZero.getBitWidth() == Min.getBitWidth() &&
711          KnownZero.getBitWidth() == Max.getBitWidth() &&
712          "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
713   APInt UnknownBits = ~(KnownZero|KnownOne);
714
715   // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
716   // bit if it is unknown.
717   Min = KnownOne;
718   Max = KnownOne|UnknownBits;
719   
720   if (UnknownBits.isNegative()) { // Sign bit is unknown
721     Min.set(Min.getBitWidth()-1);
722     Max.clear(Max.getBitWidth()-1);
723   }
724 }
725
726 // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
727 // a set of known zero and one bits, compute the maximum and minimum values that
728 // could have the specified known zero and known one bits, returning them in
729 // min/max.
730 static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
731                                                      const APInt &KnownOne,
732                                                      APInt &Min, APInt &Max) {
733   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
734          KnownZero.getBitWidth() == Min.getBitWidth() &&
735          KnownZero.getBitWidth() == Max.getBitWidth() &&
736          "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
737   APInt UnknownBits = ~(KnownZero|KnownOne);
738   
739   // The minimum value is when the unknown bits are all zeros.
740   Min = KnownOne;
741   // The maximum value is when the unknown bits are all ones.
742   Max = KnownOne|UnknownBits;
743 }
744
745 /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
746 /// SimplifyDemandedBits knows about.  See if the instruction has any
747 /// properties that allow us to simplify its operands.
748 bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
749   unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
750   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
751   APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
752   
753   Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, 
754                                      KnownZero, KnownOne, 0);
755   if (V == 0) return false;
756   if (V == &Inst) return true;
757   ReplaceInstUsesWith(Inst, V);
758   return true;
759 }
760
761 /// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
762 /// specified instruction operand if possible, updating it in place.  It returns
763 /// true if it made any change and false otherwise.
764 bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask, 
765                                         APInt &KnownZero, APInt &KnownOne,
766                                         unsigned Depth) {
767   Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
768                                           KnownZero, KnownOne, Depth);
769   if (NewVal == 0) return false;
770   U.set(NewVal);
771   return true;
772 }
773
774
775 /// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
776 /// value based on the demanded bits.  When this function is called, it is known
777 /// that only the bits set in DemandedMask of the result of V are ever used
778 /// downstream. Consequently, depending on the mask and V, it may be possible
779 /// to replace V with a constant or one of its operands. In such cases, this
780 /// function does the replacement and returns true. In all other cases, it
781 /// returns false after analyzing the expression and setting KnownOne and known
782 /// to be one in the expression.  KnownZero contains all the bits that are known
783 /// to be zero in the expression. These are provided to potentially allow the
784 /// caller (which might recursively be SimplifyDemandedBits itself) to simplify
785 /// the expression. KnownOne and KnownZero always follow the invariant that 
786 /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
787 /// the bits in KnownOne and KnownZero may only be accurate for those bits set
788 /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
789 /// and KnownOne must all be the same.
790 ///
791 /// This returns null if it did not change anything and it permits no
792 /// simplification.  This returns V itself if it did some simplification of V's
793 /// operands based on the information about what bits are demanded. This returns
794 /// some other non-null value if it found out that V is equal to another value
795 /// in the context where the specified bits are demanded, but not for all users.
796 Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
797                                              APInt &KnownZero, APInt &KnownOne,
798                                              unsigned Depth) {
799   assert(V != 0 && "Null pointer of Value???");
800   assert(Depth <= 6 && "Limit Search Depth");
801   uint32_t BitWidth = DemandedMask.getBitWidth();
802   const Type *VTy = V->getType();
803   assert((TD || !isa<PointerType>(VTy)) &&
804          "SimplifyDemandedBits needs to know bit widths!");
805   assert((!TD || TD->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
806          (!VTy->isIntOrIntVector() ||
807           VTy->getScalarSizeInBits() == BitWidth) &&
808          KnownZero.getBitWidth() == BitWidth &&
809          KnownOne.getBitWidth() == BitWidth &&
810          "Value *V, DemandedMask, KnownZero and KnownOne "
811          "must have same BitWidth");
812   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
813     // We know all of the bits for a constant!
814     KnownOne = CI->getValue() & DemandedMask;
815     KnownZero = ~KnownOne & DemandedMask;
816     return 0;
817   }
818   if (isa<ConstantPointerNull>(V)) {
819     // We know all of the bits for a constant!
820     KnownOne.clear();
821     KnownZero = DemandedMask;
822     return 0;
823   }
824
825   KnownZero.clear();
826   KnownOne.clear();
827   if (DemandedMask == 0) {   // Not demanding any bits from V.
828     if (isa<UndefValue>(V))
829       return 0;
830     return UndefValue::get(VTy);
831   }
832   
833   if (Depth == 6)        // Limit search depth.
834     return 0;
835   
836   APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
837   APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne;
838
839   Instruction *I = dyn_cast<Instruction>(V);
840   if (!I) {
841     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
842     return 0;        // Only analyze instructions.
843   }
844
845   // If there are multiple uses of this value and we aren't at the root, then
846   // we can't do any simplifications of the operands, because DemandedMask
847   // only reflects the bits demanded by *one* of the users.
848   if (Depth != 0 && !I->hasOneUse()) {
849     // Despite the fact that we can't simplify this instruction in all User's
850     // context, we can at least compute the knownzero/knownone bits, and we can
851     // do simplifications that apply to *just* the one user if we know that
852     // this instruction has a simpler value in that context.
853     if (I->getOpcode() == Instruction::And) {
854       // If either the LHS or the RHS are Zero, the result is zero.
855       ComputeMaskedBits(I->getOperand(1), DemandedMask,
856                         RHSKnownZero, RHSKnownOne, Depth+1);
857       ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero,
858                         LHSKnownZero, LHSKnownOne, Depth+1);
859       
860       // If all of the demanded bits are known 1 on one side, return the other.
861       // These bits cannot contribute to the result of the 'and' in this
862       // context.
863       if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == 
864           (DemandedMask & ~LHSKnownZero))
865         return I->getOperand(0);
866       if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == 
867           (DemandedMask & ~RHSKnownZero))
868         return I->getOperand(1);
869       
870       // If all of the demanded bits in the inputs are known zeros, return zero.
871       if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
872         return Constant::getNullValue(VTy);
873       
874     } else if (I->getOpcode() == Instruction::Or) {
875       // We can simplify (X|Y) -> X or Y in the user's context if we know that
876       // only bits from X or Y are demanded.
877       
878       // If either the LHS or the RHS are One, the result is One.
879       ComputeMaskedBits(I->getOperand(1), DemandedMask, 
880                         RHSKnownZero, RHSKnownOne, Depth+1);
881       ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, 
882                         LHSKnownZero, LHSKnownOne, Depth+1);
883       
884       // If all of the demanded bits are known zero on one side, return the
885       // other.  These bits cannot contribute to the result of the 'or' in this
886       // context.
887       if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == 
888           (DemandedMask & ~LHSKnownOne))
889         return I->getOperand(0);
890       if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == 
891           (DemandedMask & ~RHSKnownOne))
892         return I->getOperand(1);
893       
894       // If all of the potentially set bits on one side are known to be set on
895       // the other side, just use the 'other' side.
896       if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == 
897           (DemandedMask & (~RHSKnownZero)))
898         return I->getOperand(0);
899       if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == 
900           (DemandedMask & (~LHSKnownZero)))
901         return I->getOperand(1);
902     }
903     
904     // Compute the KnownZero/KnownOne bits to simplify things downstream.
905     ComputeMaskedBits(I, DemandedMask, KnownZero, KnownOne, Depth);
906     return 0;
907   }
908   
909   // If this is the root being simplified, allow it to have multiple uses,
910   // just set the DemandedMask to all bits so that we can try to simplify the
911   // operands.  This allows visitTruncInst (for example) to simplify the
912   // operand of a trunc without duplicating all the logic below.
913   if (Depth == 0 && !V->hasOneUse())
914     DemandedMask = APInt::getAllOnesValue(BitWidth);
915   
916   switch (I->getOpcode()) {
917   default:
918     ComputeMaskedBits(I, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
919     break;
920   case Instruction::And:
921     // If either the LHS or the RHS are Zero, the result is zero.
922     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
923                              RHSKnownZero, RHSKnownOne, Depth+1) ||
924         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
925                              LHSKnownZero, LHSKnownOne, Depth+1))
926       return I;
927     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
928     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
929
930     // If all of the demanded bits are known 1 on one side, return the other.
931     // These bits cannot contribute to the result of the 'and'.
932     if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == 
933         (DemandedMask & ~LHSKnownZero))
934       return I->getOperand(0);
935     if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == 
936         (DemandedMask & ~RHSKnownZero))
937       return I->getOperand(1);
938     
939     // If all of the demanded bits in the inputs are known zeros, return zero.
940     if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
941       return Constant::getNullValue(VTy);
942       
943     // If the RHS is a constant, see if we can simplify it.
944     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
945       return I;
946       
947     // Output known-1 bits are only known if set in both the LHS & RHS.
948     RHSKnownOne &= LHSKnownOne;
949     // Output known-0 are known to be clear if zero in either the LHS | RHS.
950     RHSKnownZero |= LHSKnownZero;
951     break;
952   case Instruction::Or:
953     // If either the LHS or the RHS are One, the result is One.
954     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, 
955                              RHSKnownZero, RHSKnownOne, Depth+1) ||
956         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne, 
957                              LHSKnownZero, LHSKnownOne, Depth+1))
958       return I;
959     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
960     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
961     
962     // If all of the demanded bits are known zero on one side, return the other.
963     // These bits cannot contribute to the result of the 'or'.
964     if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == 
965         (DemandedMask & ~LHSKnownOne))
966       return I->getOperand(0);
967     if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == 
968         (DemandedMask & ~RHSKnownOne))
969       return I->getOperand(1);
970
971     // If all of the potentially set bits on one side are known to be set on
972     // the other side, just use the 'other' side.
973     if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == 
974         (DemandedMask & (~RHSKnownZero)))
975       return I->getOperand(0);
976     if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == 
977         (DemandedMask & (~LHSKnownZero)))
978       return I->getOperand(1);
979         
980     // If the RHS is a constant, see if we can simplify it.
981     if (ShrinkDemandedConstant(I, 1, DemandedMask))
982       return I;
983           
984     // Output known-0 bits are only known if clear in both the LHS & RHS.
985     RHSKnownZero &= LHSKnownZero;
986     // Output known-1 are known to be set if set in either the LHS | RHS.
987     RHSKnownOne |= LHSKnownOne;
988     break;
989   case Instruction::Xor: {
990     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
991                              RHSKnownZero, RHSKnownOne, Depth+1) ||
992         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, 
993                              LHSKnownZero, LHSKnownOne, Depth+1))
994       return I;
995     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
996     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
997     
998     // If all of the demanded bits are known zero on one side, return the other.
999     // These bits cannot contribute to the result of the 'xor'.
1000     if ((DemandedMask & RHSKnownZero) == DemandedMask)
1001       return I->getOperand(0);
1002     if ((DemandedMask & LHSKnownZero) == DemandedMask)
1003       return I->getOperand(1);
1004     
1005     // Output known-0 bits are known if clear or set in both the LHS & RHS.
1006     APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | 
1007                          (RHSKnownOne & LHSKnownOne);
1008     // Output known-1 are known to be set if set in only one of the LHS, RHS.
1009     APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | 
1010                         (RHSKnownOne & LHSKnownZero);
1011     
1012     // If all of the demanded bits are known to be zero on one side or the
1013     // other, turn this into an *inclusive* or.
1014     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
1015     if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
1016       Instruction *Or = 
1017         BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
1018                                  I->getName());
1019       return InsertNewInstBefore(Or, *I);
1020     }
1021     
1022     // If all of the demanded bits on one side are known, and all of the set
1023     // bits on that side are also known to be set on the other side, turn this
1024     // into an AND, as we know the bits will be cleared.
1025     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
1026     if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { 
1027       // all known
1028       if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
1029         Constant *AndC = Constant::getIntegerValue(VTy,
1030                                                    ~RHSKnownOne & DemandedMask);
1031         Instruction *And = 
1032           BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
1033         return InsertNewInstBefore(And, *I);
1034       }
1035     }
1036     
1037     // If the RHS is a constant, see if we can simplify it.
1038     // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
1039     if (ShrinkDemandedConstant(I, 1, DemandedMask))
1040       return I;
1041     
1042     RHSKnownZero = KnownZeroOut;
1043     RHSKnownOne  = KnownOneOut;
1044     break;
1045   }
1046   case Instruction::Select:
1047     if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
1048                              RHSKnownZero, RHSKnownOne, Depth+1) ||
1049         SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, 
1050                              LHSKnownZero, LHSKnownOne, Depth+1))
1051       return I;
1052     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
1053     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
1054     
1055     // If the operands are constants, see if we can simplify them.
1056     if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
1057         ShrinkDemandedConstant(I, 2, DemandedMask))
1058       return I;
1059     
1060     // Only known if known in both the LHS and RHS.
1061     RHSKnownOne &= LHSKnownOne;
1062     RHSKnownZero &= LHSKnownZero;
1063     break;
1064   case Instruction::Trunc: {
1065     unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
1066     DemandedMask.zext(truncBf);
1067     RHSKnownZero.zext(truncBf);
1068     RHSKnownOne.zext(truncBf);
1069     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, 
1070                              RHSKnownZero, RHSKnownOne, Depth+1))
1071       return I;
1072     DemandedMask.trunc(BitWidth);
1073     RHSKnownZero.trunc(BitWidth);
1074     RHSKnownOne.trunc(BitWidth);
1075     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
1076     break;
1077   }
1078   case Instruction::BitCast:
1079     if (!I->getOperand(0)->getType()->isIntOrIntVector())
1080       return false;  // vector->int or fp->int?
1081
1082     if (const VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
1083       if (const VectorType *SrcVTy =
1084             dyn_cast<VectorType>(I->getOperand(0)->getType())) {
1085         if (DstVTy->getNumElements() != SrcVTy->getNumElements())
1086           // Don't touch a bitcast between vectors of different element counts.
1087           return false;
1088       } else
1089         // Don't touch a scalar-to-vector bitcast.
1090         return false;
1091     } else if (isa<VectorType>(I->getOperand(0)->getType()))
1092       // Don't touch a vector-to-scalar bitcast.
1093       return false;
1094
1095     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
1096                              RHSKnownZero, RHSKnownOne, Depth+1))
1097       return I;
1098     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
1099     break;
1100   case Instruction::ZExt: {
1101     // Compute the bits in the result that are not present in the input.
1102     unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
1103     
1104     DemandedMask.trunc(SrcBitWidth);
1105     RHSKnownZero.trunc(SrcBitWidth);
1106     RHSKnownOne.trunc(SrcBitWidth);
1107     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
1108                              RHSKnownZero, RHSKnownOne, Depth+1))
1109       return I;
1110     DemandedMask.zext(BitWidth);
1111     RHSKnownZero.zext(BitWidth);
1112     RHSKnownOne.zext(BitWidth);
1113     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
1114     // The top bits are known to be zero.
1115     RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1116     break;
1117   }
1118   case Instruction::SExt: {
1119     // Compute the bits in the result that are not present in the input.
1120     unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
1121     
1122     APInt InputDemandedBits = DemandedMask & 
1123                               APInt::getLowBitsSet(BitWidth, SrcBitWidth);
1124
1125     APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
1126     // If any of the sign extended bits are demanded, we know that the sign
1127     // bit is demanded.
1128     if ((NewBits & DemandedMask) != 0)
1129       InputDemandedBits.set(SrcBitWidth-1);
1130       
1131     InputDemandedBits.trunc(SrcBitWidth);
1132     RHSKnownZero.trunc(SrcBitWidth);
1133     RHSKnownOne.trunc(SrcBitWidth);
1134     if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
1135                              RHSKnownZero, RHSKnownOne, Depth+1))
1136       return I;
1137     InputDemandedBits.zext(BitWidth);
1138     RHSKnownZero.zext(BitWidth);
1139     RHSKnownOne.zext(BitWidth);
1140     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
1141       
1142     // If the sign bit of the input is known set or clear, then we know the
1143     // top bits of the result.
1144
1145     // If the input sign bit is known zero, or if the NewBits are not demanded
1146     // convert this into a zero extension.
1147     if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
1148       // Convert to ZExt cast
1149       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
1150       return InsertNewInstBefore(NewCast, *I);
1151     } else if (RHSKnownOne[SrcBitWidth-1]) {    // Input sign bit known set
1152       RHSKnownOne |= NewBits;
1153     }
1154     break;
1155   }
1156   case Instruction::Add: {
1157     // Figure out what the input bits are.  If the top bits of the and result
1158     // are not demanded, then the add doesn't demand them from its input
1159     // either.
1160     unsigned NLZ = DemandedMask.countLeadingZeros();
1161       
1162     // If there is a constant on the RHS, there are a variety of xformations
1163     // we can do.
1164     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
1165       // If null, this should be simplified elsewhere.  Some of the xforms here
1166       // won't work if the RHS is zero.
1167       if (RHS->isZero())
1168         break;
1169       
1170       // If the top bit of the output is demanded, demand everything from the
1171       // input.  Otherwise, we demand all the input bits except NLZ top bits.
1172       APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
1173
1174       // Find information about known zero/one bits in the input.
1175       if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits, 
1176                                LHSKnownZero, LHSKnownOne, Depth+1))
1177         return I;
1178
1179       // If the RHS of the add has bits set that can't affect the input, reduce
1180       // the constant.
1181       if (ShrinkDemandedConstant(I, 1, InDemandedBits))
1182         return I;
1183       
1184       // Avoid excess work.
1185       if (LHSKnownZero == 0 && LHSKnownOne == 0)
1186         break;
1187       
1188       // Turn it into OR if input bits are zero.
1189       if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
1190         Instruction *Or =
1191           BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
1192                                    I->getName());
1193         return InsertNewInstBefore(Or, *I);
1194       }
1195       
1196       // We can say something about the output known-zero and known-one bits,
1197       // depending on potential carries from the input constant and the
1198       // unknowns.  For example if the LHS is known to have at most the 0x0F0F0
1199       // bits set and the RHS constant is 0x01001, then we know we have a known
1200       // one mask of 0x00001 and a known zero mask of 0xE0F0E.
1201       
1202       // To compute this, we first compute the potential carry bits.  These are
1203       // the bits which may be modified.  I'm not aware of a better way to do
1204       // this scan.
1205       const APInt &RHSVal = RHS->getValue();
1206       APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
1207       
1208       // Now that we know which bits have carries, compute the known-1/0 sets.
1209       
1210       // Bits are known one if they are known zero in one operand and one in the
1211       // other, and there is no input carry.
1212       RHSKnownOne = ((LHSKnownZero & RHSVal) | 
1213                      (LHSKnownOne & ~RHSVal)) & ~CarryBits;
1214       
1215       // Bits are known zero if they are known zero in both operands and there
1216       // is no input carry.
1217       RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
1218     } else {
1219       // If the high-bits of this ADD are not demanded, then it does not demand
1220       // the high bits of its LHS or RHS.
1221       if (DemandedMask[BitWidth-1] == 0) {
1222         // Right fill the mask of bits for this ADD to demand the most
1223         // significant bit and all those below it.
1224         APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
1225         if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
1226                                  LHSKnownZero, LHSKnownOne, Depth+1) ||
1227             SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
1228                                  LHSKnownZero, LHSKnownOne, Depth+1))
1229           return I;
1230       }
1231     }
1232     break;
1233   }
1234   case Instruction::Sub:
1235     // If the high-bits of this SUB are not demanded, then it does not demand
1236     // the high bits of its LHS or RHS.
1237     if (DemandedMask[BitWidth-1] == 0) {
1238       // Right fill the mask of bits for this SUB to demand the most
1239       // significant bit and all those below it.
1240       uint32_t NLZ = DemandedMask.countLeadingZeros();
1241       APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
1242       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
1243                                LHSKnownZero, LHSKnownOne, Depth+1) ||
1244           SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
1245                                LHSKnownZero, LHSKnownOne, Depth+1))
1246         return I;
1247     }
1248     // Otherwise just hand the sub off to ComputeMaskedBits to fill in
1249     // the known zeros and ones.
1250     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
1251     break;
1252   case Instruction::Shl:
1253     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1254       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
1255       APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
1256       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, 
1257                                RHSKnownZero, RHSKnownOne, Depth+1))
1258         return I;
1259       assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
1260       RHSKnownZero <<= ShiftAmt;
1261       RHSKnownOne  <<= ShiftAmt;
1262       // low bits known zero.
1263       if (ShiftAmt)
1264         RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
1265     }
1266     break;
1267   case Instruction::LShr:
1268     // For a logical shift right
1269     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1270       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
1271       
1272       // Unsigned shift right.
1273       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
1274       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
1275                                RHSKnownZero, RHSKnownOne, Depth+1))
1276         return I;
1277       assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
1278       RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
1279       RHSKnownOne  = APIntOps::lshr(RHSKnownOne, ShiftAmt);
1280       if (ShiftAmt) {
1281         // Compute the new bits that are at the top now.
1282         APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
1283         RHSKnownZero |= HighBits;  // high bits known zero.
1284       }
1285     }
1286     break;
1287   case Instruction::AShr:
1288     // If this is an arithmetic shift right and only the low-bit is set, we can
1289     // always convert this into a logical shr, even if the shift amount is
1290     // variable.  The low bit of the shift cannot be an input sign bit unless
1291     // the shift amount is >= the size of the datatype, which is undefined.
1292     if (DemandedMask == 1) {
1293       // Perform the logical shift right.
1294       Instruction *NewVal = BinaryOperator::CreateLShr(
1295                         I->getOperand(0), I->getOperand(1), I->getName());
1296       return InsertNewInstBefore(NewVal, *I);
1297     }    
1298
1299     // If the sign bit is the only bit demanded by this ashr, then there is no
1300     // need to do it, the shift doesn't change the high bit.
1301     if (DemandedMask.isSignBit())
1302       return I->getOperand(0);
1303     
1304     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1305       uint32_t ShiftAmt = SA->getLimitedValue(BitWidth);
1306       
1307       // Signed shift right.
1308       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
1309       // If any of the "high bits" are demanded, we should set the sign bit as
1310       // demanded.
1311       if (DemandedMask.countLeadingZeros() <= ShiftAmt)
1312         DemandedMaskIn.set(BitWidth-1);
1313       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
1314                                RHSKnownZero, RHSKnownOne, Depth+1))
1315         return I;
1316       assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
1317       // Compute the new bits that are at the top now.
1318       APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
1319       RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
1320       RHSKnownOne  = APIntOps::lshr(RHSKnownOne, ShiftAmt);
1321         
1322       // Handle the sign bits.
1323       APInt SignBit(APInt::getSignBit(BitWidth));
1324       // Adjust to where it is now in the mask.
1325       SignBit = APIntOps::lshr(SignBit, ShiftAmt);  
1326         
1327       // If the input sign bit is known to be zero, or if none of the top bits
1328       // are demanded, turn this into an unsigned shift right.
1329       if (BitWidth <= ShiftAmt || RHSKnownZero[BitWidth-ShiftAmt-1] || 
1330           (HighBits & ~DemandedMask) == HighBits) {
1331         // Perform the logical shift right.
1332         Instruction *NewVal = BinaryOperator::CreateLShr(
1333                           I->getOperand(0), SA, I->getName());
1334         return InsertNewInstBefore(NewVal, *I);
1335       } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one.
1336         RHSKnownOne |= HighBits;
1337       }
1338     }
1339     break;
1340   case Instruction::SRem:
1341     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1342       APInt RA = Rem->getValue().abs();
1343       if (RA.isPowerOf2()) {
1344         if (DemandedMask.ult(RA))    // srem won't affect demanded bits
1345           return I->getOperand(0);
1346
1347         APInt LowBits = RA - 1;
1348         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1349         if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
1350                                  LHSKnownZero, LHSKnownOne, Depth+1))
1351           return I;
1352
1353         if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
1354           LHSKnownZero |= ~LowBits;
1355
1356         KnownZero |= LHSKnownZero & DemandedMask;
1357
1358         assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?"); 
1359       }
1360     }
1361     break;
1362   case Instruction::URem: {
1363     APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
1364     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1365     if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
1366                              KnownZero2, KnownOne2, Depth+1) ||
1367         SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
1368                              KnownZero2, KnownOne2, Depth+1))
1369       return I;
1370
1371     unsigned Leaders = KnownZero2.countLeadingOnes();
1372     Leaders = std::max(Leaders,
1373                        KnownZero2.countLeadingOnes());
1374     KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
1375     break;
1376   }
1377   case Instruction::Call:
1378     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1379       switch (II->getIntrinsicID()) {
1380       default: break;
1381       case Intrinsic::bswap: {
1382         // If the only bits demanded come from one byte of the bswap result,
1383         // just shift the input byte into position to eliminate the bswap.
1384         unsigned NLZ = DemandedMask.countLeadingZeros();
1385         unsigned NTZ = DemandedMask.countTrailingZeros();
1386           
1387         // Round NTZ down to the next byte.  If we have 11 trailing zeros, then
1388         // we need all the bits down to bit 8.  Likewise, round NLZ.  If we
1389         // have 14 leading zeros, round to 8.
1390         NLZ &= ~7;
1391         NTZ &= ~7;
1392         // If we need exactly one byte, we can do this transformation.
1393         if (BitWidth-NLZ-NTZ == 8) {
1394           unsigned ResultBit = NTZ;
1395           unsigned InputBit = BitWidth-NTZ-8;
1396           
1397           // Replace this with either a left or right shift to get the byte into
1398           // the right place.
1399           Instruction *NewVal;
1400           if (InputBit > ResultBit)
1401             NewVal = BinaryOperator::CreateLShr(I->getOperand(1),
1402                     ConstantInt::get(I->getType(), InputBit-ResultBit));
1403           else
1404             NewVal = BinaryOperator::CreateShl(I->getOperand(1),
1405                     ConstantInt::get(I->getType(), ResultBit-InputBit));
1406           NewVal->takeName(I);
1407           return InsertNewInstBefore(NewVal, *I);
1408         }
1409           
1410         // TODO: Could compute known zero/one bits based on the input.
1411         break;
1412       }
1413       }
1414     }
1415     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
1416     break;
1417   }
1418   
1419   // If the client is only demanding bits that we know, return the known
1420   // constant.
1421   if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask)
1422     return Constant::getIntegerValue(VTy, RHSKnownOne);
1423   return false;
1424 }
1425
1426
1427 /// SimplifyDemandedVectorElts - The specified value produces a vector with
1428 /// any number of elements. DemandedElts contains the set of elements that are
1429 /// actually used by the caller.  This method analyzes which elements of the
1430 /// operand are undef and returns that information in UndefElts.
1431 ///
1432 /// If the information about demanded elements can be used to simplify the
1433 /// operation, the operation is simplified, then the resultant value is
1434 /// returned.  This returns null if no change was made.
1435 Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
1436                                                 APInt& UndefElts,
1437                                                 unsigned Depth) {
1438   unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
1439   APInt EltMask(APInt::getAllOnesValue(VWidth));
1440   assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
1441
1442   if (isa<UndefValue>(V)) {
1443     // If the entire vector is undefined, just return this info.
1444     UndefElts = EltMask;
1445     return 0;
1446   } else if (DemandedElts == 0) { // If nothing is demanded, provide undef.
1447     UndefElts = EltMask;
1448     return UndefValue::get(V->getType());
1449   }
1450
1451   UndefElts = 0;
1452   if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) {
1453     const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
1454     Constant *Undef = UndefValue::get(EltTy);
1455
1456     std::vector<Constant*> Elts;
1457     for (unsigned i = 0; i != VWidth; ++i)
1458       if (!DemandedElts[i]) {   // If not demanded, set to undef.
1459         Elts.push_back(Undef);
1460         UndefElts.set(i);
1461       } else if (isa<UndefValue>(CP->getOperand(i))) {   // Already undef.
1462         Elts.push_back(Undef);
1463         UndefElts.set(i);
1464       } else {                               // Otherwise, defined.
1465         Elts.push_back(CP->getOperand(i));
1466       }
1467
1468     // If we changed the constant, return it.
1469     Constant *NewCP = ConstantVector::get(Elts);
1470     return NewCP != CP ? NewCP : 0;
1471   } else if (isa<ConstantAggregateZero>(V)) {
1472     // Simplify the CAZ to a ConstantVector where the non-demanded elements are
1473     // set to undef.
1474     
1475     // Check if this is identity. If so, return 0 since we are not simplifying
1476     // anything.
1477     if (DemandedElts == ((1ULL << VWidth) -1))
1478       return 0;
1479     
1480     const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
1481     Constant *Zero = Constant::getNullValue(EltTy);
1482     Constant *Undef = UndefValue::get(EltTy);
1483     std::vector<Constant*> Elts;
1484     for (unsigned i = 0; i != VWidth; ++i) {
1485       Constant *Elt = DemandedElts[i] ? Zero : Undef;
1486       Elts.push_back(Elt);
1487     }
1488     UndefElts = DemandedElts ^ EltMask;
1489     return ConstantVector::get(Elts);
1490   }
1491   
1492   // Limit search depth.
1493   if (Depth == 10)
1494     return 0;
1495
1496   // If multiple users are using the root value, procede with
1497   // simplification conservatively assuming that all elements
1498   // are needed.
1499   if (!V->hasOneUse()) {
1500     // Quit if we find multiple users of a non-root value though.
1501     // They'll be handled when it's their turn to be visited by
1502     // the main instcombine process.
1503     if (Depth != 0)
1504       // TODO: Just compute the UndefElts information recursively.
1505       return 0;
1506
1507     // Conservatively assume that all elements are needed.
1508     DemandedElts = EltMask;
1509   }
1510   
1511   Instruction *I = dyn_cast<Instruction>(V);
1512   if (!I) return 0;        // Only analyze instructions.
1513   
1514   bool MadeChange = false;
1515   APInt UndefElts2(VWidth, 0);
1516   Value *TmpV;
1517   switch (I->getOpcode()) {
1518   default: break;
1519     
1520   case Instruction::InsertElement: {
1521     // If this is a variable index, we don't know which element it overwrites.
1522     // demand exactly the same input as we produce.
1523     ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
1524     if (Idx == 0) {
1525       // Note that we can't propagate undef elt info, because we don't know
1526       // which elt is getting updated.
1527       TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1528                                         UndefElts2, Depth+1);
1529       if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1530       break;
1531     }
1532     
1533     // If this is inserting an element that isn't demanded, remove this
1534     // insertelement.
1535     unsigned IdxNo = Idx->getZExtValue();
1536     if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1537       Worklist.Add(I);
1538       return I->getOperand(0);
1539     }
1540     
1541     // Otherwise, the element inserted overwrites whatever was there, so the
1542     // input demanded set is simpler than the output set.
1543     APInt DemandedElts2 = DemandedElts;
1544     DemandedElts2.clear(IdxNo);
1545     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
1546                                       UndefElts, Depth+1);
1547     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1548
1549     // The inserted element is defined.
1550     UndefElts.clear(IdxNo);
1551     break;
1552   }
1553   case Instruction::ShuffleVector: {
1554     ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1555     uint64_t LHSVWidth =
1556       cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1557     APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1558     for (unsigned i = 0; i < VWidth; i++) {
1559       if (DemandedElts[i]) {
1560         unsigned MaskVal = Shuffle->getMaskValue(i);
1561         if (MaskVal != -1u) {
1562           assert(MaskVal < LHSVWidth * 2 &&
1563                  "shufflevector mask index out of range!");
1564           if (MaskVal < LHSVWidth)
1565             LeftDemanded.set(MaskVal);
1566           else
1567             RightDemanded.set(MaskVal - LHSVWidth);
1568         }
1569       }
1570     }
1571
1572     APInt UndefElts4(LHSVWidth, 0);
1573     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
1574                                       UndefElts4, Depth+1);
1575     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1576
1577     APInt UndefElts3(LHSVWidth, 0);
1578     TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
1579                                       UndefElts3, Depth+1);
1580     if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1581
1582     bool NewUndefElts = false;
1583     for (unsigned i = 0; i < VWidth; i++) {
1584       unsigned MaskVal = Shuffle->getMaskValue(i);
1585       if (MaskVal == -1u) {
1586         UndefElts.set(i);
1587       } else if (MaskVal < LHSVWidth) {
1588         if (UndefElts4[MaskVal]) {
1589           NewUndefElts = true;
1590           UndefElts.set(i);
1591         }
1592       } else {
1593         if (UndefElts3[MaskVal - LHSVWidth]) {
1594           NewUndefElts = true;
1595           UndefElts.set(i);
1596         }
1597       }
1598     }
1599
1600     if (NewUndefElts) {
1601       // Add additional discovered undefs.
1602       std::vector<Constant*> Elts;
1603       for (unsigned i = 0; i < VWidth; ++i) {
1604         if (UndefElts[i])
1605           Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
1606         else
1607           Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context),
1608                                           Shuffle->getMaskValue(i)));
1609       }
1610       I->setOperand(2, ConstantVector::get(Elts));
1611       MadeChange = true;
1612     }
1613     break;
1614   }
1615   case Instruction::BitCast: {
1616     // Vector->vector casts only.
1617     const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
1618     if (!VTy) break;
1619     unsigned InVWidth = VTy->getNumElements();
1620     APInt InputDemandedElts(InVWidth, 0);
1621     unsigned Ratio;
1622
1623     if (VWidth == InVWidth) {
1624       // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1625       // elements as are demanded of us.
1626       Ratio = 1;
1627       InputDemandedElts = DemandedElts;
1628     } else if (VWidth > InVWidth) {
1629       // Untested so far.
1630       break;
1631       
1632       // If there are more elements in the result than there are in the source,
1633       // then an input element is live if any of the corresponding output
1634       // elements are live.
1635       Ratio = VWidth/InVWidth;
1636       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1637         if (DemandedElts[OutIdx])
1638           InputDemandedElts.set(OutIdx/Ratio);
1639       }
1640     } else {
1641       // Untested so far.
1642       break;
1643       
1644       // If there are more elements in the source than there are in the result,
1645       // then an input element is live if the corresponding output element is
1646       // live.
1647       Ratio = InVWidth/VWidth;
1648       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1649         if (DemandedElts[InIdx/Ratio])
1650           InputDemandedElts.set(InIdx);
1651     }
1652     
1653     // div/rem demand all inputs, because they don't want divide by zero.
1654     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1655                                       UndefElts2, Depth+1);
1656     if (TmpV) {
1657       I->setOperand(0, TmpV);
1658       MadeChange = true;
1659     }
1660     
1661     UndefElts = UndefElts2;
1662     if (VWidth > InVWidth) {
1663       llvm_unreachable("Unimp");
1664       // If there are more elements in the result than there are in the source,
1665       // then an output element is undef if the corresponding input element is
1666       // undef.
1667       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1668         if (UndefElts2[OutIdx/Ratio])
1669           UndefElts.set(OutIdx);
1670     } else if (VWidth < InVWidth) {
1671       llvm_unreachable("Unimp");
1672       // If there are more elements in the source than there are in the result,
1673       // then a result element is undef if all of the corresponding input
1674       // elements are undef.
1675       UndefElts = ~0ULL >> (64-VWidth);  // Start out all undef.
1676       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1677         if (!UndefElts2[InIdx])            // Not undef?
1678           UndefElts.clear(InIdx/Ratio);    // Clear undef bit.
1679     }
1680     break;
1681   }
1682   case Instruction::And:
1683   case Instruction::Or:
1684   case Instruction::Xor:
1685   case Instruction::Add:
1686   case Instruction::Sub:
1687   case Instruction::Mul:
1688     // div/rem demand all inputs, because they don't want divide by zero.
1689     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1690                                       UndefElts, Depth+1);
1691     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1692     TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1693                                       UndefElts2, Depth+1);
1694     if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1695       
1696     // Output elements are undefined if both are undefined.  Consider things
1697     // like undef&0.  The result is known zero, not undef.
1698     UndefElts &= UndefElts2;
1699     break;
1700     
1701   case Instruction::Call: {
1702     IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1703     if (!II) break;
1704     switch (II->getIntrinsicID()) {
1705     default: break;
1706       
1707     // Binary vector operations that work column-wise.  A dest element is a
1708     // function of the corresponding input elements from the two inputs.
1709     case Intrinsic::x86_sse_sub_ss:
1710     case Intrinsic::x86_sse_mul_ss:
1711     case Intrinsic::x86_sse_min_ss:
1712     case Intrinsic::x86_sse_max_ss:
1713     case Intrinsic::x86_sse2_sub_sd:
1714     case Intrinsic::x86_sse2_mul_sd:
1715     case Intrinsic::x86_sse2_min_sd:
1716     case Intrinsic::x86_sse2_max_sd:
1717       TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
1718                                         UndefElts, Depth+1);
1719       if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; }
1720       TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts,
1721                                         UndefElts2, Depth+1);
1722       if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; }
1723
1724       // If only the low elt is demanded and this is a scalarizable intrinsic,
1725       // scalarize it now.
1726       if (DemandedElts == 1) {
1727         switch (II->getIntrinsicID()) {
1728         default: break;
1729         case Intrinsic::x86_sse_sub_ss:
1730         case Intrinsic::x86_sse_mul_ss:
1731         case Intrinsic::x86_sse2_sub_sd:
1732         case Intrinsic::x86_sse2_mul_sd:
1733           // TODO: Lower MIN/MAX/ABS/etc
1734           Value *LHS = II->getOperand(1);
1735           Value *RHS = II->getOperand(2);
1736           // Extract the element as scalars.
1737           LHS = InsertNewInstBefore(ExtractElementInst::Create(LHS, 
1738             ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), "tmp"), *II);
1739           RHS = InsertNewInstBefore(ExtractElementInst::Create(RHS,
1740             ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), "tmp"), *II);
1741           
1742           switch (II->getIntrinsicID()) {
1743           default: llvm_unreachable("Case stmts out of sync!");
1744           case Intrinsic::x86_sse_sub_ss:
1745           case Intrinsic::x86_sse2_sub_sd:
1746             TmpV = InsertNewInstBefore(BinaryOperator::CreateFSub(LHS, RHS,
1747                                                         II->getName()), *II);
1748             break;
1749           case Intrinsic::x86_sse_mul_ss:
1750           case Intrinsic::x86_sse2_mul_sd:
1751             TmpV = InsertNewInstBefore(BinaryOperator::CreateFMul(LHS, RHS,
1752                                                          II->getName()), *II);
1753             break;
1754           }
1755           
1756           Instruction *New =
1757             InsertElementInst::Create(
1758               UndefValue::get(II->getType()), TmpV,
1759               ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), II->getName());
1760           InsertNewInstBefore(New, *II);
1761           return New;
1762         }            
1763       }
1764         
1765       // Output elements are undefined if both are undefined.  Consider things
1766       // like undef&0.  The result is known zero, not undef.
1767       UndefElts &= UndefElts2;
1768       break;
1769     }
1770     break;
1771   }
1772   }
1773   return MadeChange ? I : 0;
1774 }
1775
1776
1777 /// AssociativeOpt - Perform an optimization on an associative operator.  This
1778 /// function is designed to check a chain of associative operators for a
1779 /// potential to apply a certain optimization.  Since the optimization may be
1780 /// applicable if the expression was reassociated, this checks the chain, then
1781 /// reassociates the expression as necessary to expose the optimization
1782 /// opportunity.  This makes use of a special Functor, which must define
1783 /// 'shouldApply' and 'apply' methods.
1784 ///
1785 template<typename Functor>
1786 static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
1787   unsigned Opcode = Root.getOpcode();
1788   Value *LHS = Root.getOperand(0);
1789
1790   // Quick check, see if the immediate LHS matches...
1791   if (F.shouldApply(LHS))
1792     return F.apply(Root);
1793
1794   // Otherwise, if the LHS is not of the same opcode as the root, return.
1795   Instruction *LHSI = dyn_cast<Instruction>(LHS);
1796   while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) {
1797     // Should we apply this transform to the RHS?
1798     bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
1799
1800     // If not to the RHS, check to see if we should apply to the LHS...
1801     if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
1802       cast<BinaryOperator>(LHSI)->swapOperands();   // Make the LHS the RHS
1803       ShouldApply = true;
1804     }
1805
1806     // If the functor wants to apply the optimization to the RHS of LHSI,
1807     // reassociate the expression from ((? op A) op B) to (? op (A op B))
1808     if (ShouldApply) {
1809       // Now all of the instructions are in the current basic block, go ahead
1810       // and perform the reassociation.
1811       Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
1812
1813       // First move the selected RHS to the LHS of the root...
1814       Root.setOperand(0, LHSI->getOperand(1));
1815
1816       // Make what used to be the LHS of the root be the user of the root...
1817       Value *ExtraOperand = TmpLHSI->getOperand(1);
1818       if (&Root == TmpLHSI) {
1819         Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
1820         return 0;
1821       }
1822       Root.replaceAllUsesWith(TmpLHSI);          // Users now use TmpLHSI
1823       TmpLHSI->setOperand(1, &Root);             // TmpLHSI now uses the root
1824       BasicBlock::iterator ARI = &Root; ++ARI;
1825       TmpLHSI->moveBefore(ARI);                  // Move TmpLHSI to after Root
1826       ARI = Root;
1827
1828       // Now propagate the ExtraOperand down the chain of instructions until we
1829       // get to LHSI.
1830       while (TmpLHSI != LHSI) {
1831         Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
1832         // Move the instruction to immediately before the chain we are
1833         // constructing to avoid breaking dominance properties.
1834         NextLHSI->moveBefore(ARI);
1835         ARI = NextLHSI;
1836
1837         Value *NextOp = NextLHSI->getOperand(1);
1838         NextLHSI->setOperand(1, ExtraOperand);
1839         TmpLHSI = NextLHSI;
1840         ExtraOperand = NextOp;
1841       }
1842
1843       // Now that the instructions are reassociated, have the functor perform
1844       // the transformation...
1845       return F.apply(Root);
1846     }
1847
1848     LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
1849   }
1850   return 0;
1851 }
1852
1853 namespace {
1854
1855 // AddRHS - Implements: X + X --> X << 1
1856 struct AddRHS {
1857   Value *RHS;
1858   explicit AddRHS(Value *rhs) : RHS(rhs) {}
1859   bool shouldApply(Value *LHS) const { return LHS == RHS; }
1860   Instruction *apply(BinaryOperator &Add) const {
1861     return BinaryOperator::CreateShl(Add.getOperand(0),
1862                                      ConstantInt::get(Add.getType(), 1));
1863   }
1864 };
1865
1866 // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
1867 //                 iff C1&C2 == 0
1868 struct AddMaskingAnd {
1869   Constant *C2;
1870   explicit AddMaskingAnd(Constant *c) : C2(c) {}
1871   bool shouldApply(Value *LHS) const {
1872     ConstantInt *C1;
1873     return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
1874            ConstantExpr::getAnd(C1, C2)->isNullValue();
1875   }
1876   Instruction *apply(BinaryOperator &Add) const {
1877     return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1));
1878   }
1879 };
1880
1881 }
1882
1883 static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
1884                                              InstCombiner *IC) {
1885   if (CastInst *CI = dyn_cast<CastInst>(&I))
1886     return IC->Builder->CreateCast(CI->getOpcode(), SO, I.getType());
1887
1888   // Figure out if the constant is the left or the right argument.
1889   bool ConstIsRHS = isa<Constant>(I.getOperand(1));
1890   Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
1891
1892   if (Constant *SOC = dyn_cast<Constant>(SO)) {
1893     if (ConstIsRHS)
1894       return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
1895     return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
1896   }
1897
1898   Value *Op0 = SO, *Op1 = ConstOperand;
1899   if (!ConstIsRHS)
1900     std::swap(Op0, Op1);
1901   
1902   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
1903     return IC->Builder->CreateBinOp(BO->getOpcode(), Op0, Op1,
1904                                     SO->getName()+".op");
1905   if (ICmpInst *CI = dyn_cast<ICmpInst>(&I))
1906     return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
1907                                    SO->getName()+".cmp");
1908   if (FCmpInst *CI = dyn_cast<FCmpInst>(&I))
1909     return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
1910                                    SO->getName()+".cmp");
1911   llvm_unreachable("Unknown binary instruction type!");
1912 }
1913
1914 // FoldOpIntoSelect - Given an instruction with a select as one operand and a
1915 // constant as the other operand, try to fold the binary operator into the
1916 // select arguments.  This also works for Cast instructions, which obviously do
1917 // not have a second operand.
1918 static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
1919                                      InstCombiner *IC) {
1920   // Don't modify shared select instructions
1921   if (!SI->hasOneUse()) return 0;
1922   Value *TV = SI->getOperand(1);
1923   Value *FV = SI->getOperand(2);
1924
1925   if (isa<Constant>(TV) || isa<Constant>(FV)) {
1926     // Bool selects with constant operands can be folded to logical ops.
1927     if (SI->getType() == Type::getInt1Ty(*IC->getContext())) return 0;
1928
1929     Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC);
1930     Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC);
1931
1932     return SelectInst::Create(SI->getCondition(), SelectTrueVal,
1933                               SelectFalseVal);
1934   }
1935   return 0;
1936 }
1937
1938
1939 /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI
1940 /// node as operand #0, see if we can fold the instruction into the PHI (which
1941 /// is only possible if all operands to the PHI are constants).
1942 Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
1943   PHINode *PN = cast<PHINode>(I.getOperand(0));
1944   unsigned NumPHIValues = PN->getNumIncomingValues();
1945   if (!PN->hasOneUse() || NumPHIValues == 0) return 0;
1946
1947   // Check to see if all of the operands of the PHI are constants.  If there is
1948   // one non-constant value, remember the BB it is.  If there is more than one
1949   // or if *it* is a PHI, bail out.
1950   BasicBlock *NonConstBB = 0;
1951   for (unsigned i = 0; i != NumPHIValues; ++i)
1952     if (!isa<Constant>(PN->getIncomingValue(i))) {
1953       if (NonConstBB) return 0;  // More than one non-const value.
1954       if (isa<PHINode>(PN->getIncomingValue(i))) return 0;  // Itself a phi.
1955       NonConstBB = PN->getIncomingBlock(i);
1956       
1957       // If the incoming non-constant value is in I's block, we have an infinite
1958       // loop.
1959       if (NonConstBB == I.getParent())
1960         return 0;
1961     }
1962   
1963   // If there is exactly one non-constant value, we can insert a copy of the
1964   // operation in that block.  However, if this is a critical edge, we would be
1965   // inserting the computation one some other paths (e.g. inside a loop).  Only
1966   // do this if the pred block is unconditionally branching into the phi block.
1967   if (NonConstBB) {
1968     BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
1969     if (!BI || !BI->isUnconditional()) return 0;
1970   }
1971
1972   // Okay, we can do the transformation: create the new PHI node.
1973   PHINode *NewPN = PHINode::Create(I.getType(), "");
1974   NewPN->reserveOperandSpace(PN->getNumOperands()/2);
1975   InsertNewInstBefore(NewPN, *PN);
1976   NewPN->takeName(PN);
1977
1978   // Next, add all of the operands to the PHI.
1979   if (I.getNumOperands() == 2) {
1980     Constant *C = cast<Constant>(I.getOperand(1));
1981     for (unsigned i = 0; i != NumPHIValues; ++i) {
1982       Value *InV = 0;
1983       if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
1984         if (CmpInst *CI = dyn_cast<CmpInst>(&I))
1985           InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
1986         else
1987           InV = ConstantExpr::get(I.getOpcode(), InC, C);
1988       } else {
1989         assert(PN->getIncomingBlock(i) == NonConstBB);
1990         if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) 
1991           InV = BinaryOperator::Create(BO->getOpcode(),
1992                                        PN->getIncomingValue(i), C, "phitmp",
1993                                        NonConstBB->getTerminator());
1994         else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
1995           InV = CmpInst::Create(CI->getOpcode(),
1996                                 CI->getPredicate(),
1997                                 PN->getIncomingValue(i), C, "phitmp",
1998                                 NonConstBB->getTerminator());
1999         else
2000           llvm_unreachable("Unknown binop!");
2001         
2002         Worklist.Add(cast<Instruction>(InV));
2003       }
2004       NewPN->addIncoming(InV, PN->getIncomingBlock(i));
2005     }
2006   } else { 
2007     CastInst *CI = cast<CastInst>(&I);
2008     const Type *RetTy = CI->getType();
2009     for (unsigned i = 0; i != NumPHIValues; ++i) {
2010       Value *InV;
2011       if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
2012         InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
2013       } else {
2014         assert(PN->getIncomingBlock(i) == NonConstBB);
2015         InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), 
2016                                I.getType(), "phitmp", 
2017                                NonConstBB->getTerminator());
2018         Worklist.Add(cast<Instruction>(InV));
2019       }
2020       NewPN->addIncoming(InV, PN->getIncomingBlock(i));
2021     }
2022   }
2023   return ReplaceInstUsesWith(I, NewPN);
2024 }
2025
2026
2027 /// WillNotOverflowSignedAdd - Return true if we can prove that:
2028 ///    (sext (add LHS, RHS))  === (add (sext LHS), (sext RHS))
2029 /// This basically requires proving that the add in the original type would not
2030 /// overflow to change the sign bit or have a carry out.
2031 bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) {
2032   // There are different heuristics we can use for this.  Here are some simple
2033   // ones.
2034   
2035   // Add has the property that adding any two 2's complement numbers can only 
2036   // have one carry bit which can change a sign.  As such, if LHS and RHS each
2037   // have at least two sign bits, we know that the addition of the two values will
2038   // sign extend fine.
2039   if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1)
2040     return true;
2041   
2042   
2043   // If one of the operands only has one non-zero bit, and if the other operand
2044   // has a known-zero bit in a more significant place than it (not including the
2045   // sign bit) the ripple may go up to and fill the zero, but won't change the
2046   // sign.  For example, (X & ~4) + 1.
2047   
2048   // TODO: Implement.
2049   
2050   return false;
2051 }
2052
2053
2054 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
2055   bool Changed = SimplifyCommutative(I);
2056   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
2057
2058   if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
2059     // X + undef -> undef
2060     if (isa<UndefValue>(RHS))
2061       return ReplaceInstUsesWith(I, RHS);
2062
2063     // X + 0 --> X
2064     if (RHSC->isNullValue())
2065       return ReplaceInstUsesWith(I, LHS);
2066
2067     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
2068       // X + (signbit) --> X ^ signbit
2069       const APInt& Val = CI->getValue();
2070       uint32_t BitWidth = Val.getBitWidth();
2071       if (Val == APInt::getSignBit(BitWidth))
2072         return BinaryOperator::CreateXor(LHS, RHS);
2073       
2074       // See if SimplifyDemandedBits can simplify this.  This handles stuff like
2075       // (X & 254)+1 -> (X&254)|1
2076       if (SimplifyDemandedInstructionBits(I))
2077         return &I;
2078
2079       // zext(bool) + C -> bool ? C + 1 : C
2080       if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
2081         if (ZI->getSrcTy() == Type::getInt1Ty(*Context))
2082           return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
2083     }
2084
2085     if (isa<PHINode>(LHS))
2086       if (Instruction *NV = FoldOpIntoPhi(I))
2087         return NV;
2088     
2089     ConstantInt *XorRHS = 0;
2090     Value *XorLHS = 0;
2091     if (isa<ConstantInt>(RHSC) &&
2092         match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
2093       uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
2094       const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue();
2095       
2096       uint32_t Size = TySizeBits / 2;
2097       APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1));
2098       APInt CFF80Val(-C0080Val);
2099       do {
2100         if (TySizeBits > Size) {
2101           // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
2102           // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
2103           if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) ||
2104               (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) {
2105             // This is a sign extend if the top bits are known zero.
2106             if (!MaskedValueIsZero(XorLHS, 
2107                    APInt::getHighBitsSet(TySizeBits, TySizeBits - Size)))
2108               Size = 0;  // Not a sign ext, but can't be any others either.
2109             break;
2110           }
2111         }
2112         Size >>= 1;
2113         C0080Val = APIntOps::lshr(C0080Val, Size);
2114         CFF80Val = APIntOps::ashr(CFF80Val, Size);
2115       } while (Size >= 1);
2116       
2117       // FIXME: This shouldn't be necessary. When the backends can handle types
2118       // with funny bit widths then this switch statement should be removed. It
2119       // is just here to get the size of the "middle" type back up to something
2120       // that the back ends can handle.
2121       const Type *MiddleType = 0;
2122       switch (Size) {
2123         default: break;
2124         case 32: MiddleType = Type::getInt32Ty(*Context); break;
2125         case 16: MiddleType = Type::getInt16Ty(*Context); break;
2126         case  8: MiddleType = Type::getInt8Ty(*Context); break;
2127       }
2128       if (MiddleType) {
2129         Value *NewTrunc = Builder->CreateTrunc(XorLHS, MiddleType, "sext");
2130         return new SExtInst(NewTrunc, I.getType(), I.getName());
2131       }
2132     }
2133   }
2134
2135   if (I.getType() == Type::getInt1Ty(*Context))
2136     return BinaryOperator::CreateXor(LHS, RHS);
2137
2138   // X + X --> X << 1
2139   if (I.getType()->isInteger()) {
2140     if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS)))
2141       return Result;
2142
2143     if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
2144       if (RHSI->getOpcode() == Instruction::Sub)
2145         if (LHS == RHSI->getOperand(1))                   // A + (B - A) --> B
2146           return ReplaceInstUsesWith(I, RHSI->getOperand(0));
2147     }
2148     if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
2149       if (LHSI->getOpcode() == Instruction::Sub)
2150         if (RHS == LHSI->getOperand(1))                   // (B - A) + A --> B
2151           return ReplaceInstUsesWith(I, LHSI->getOperand(0));
2152     }
2153   }
2154
2155   // -A + B  -->  B - A
2156   // -A + -B  -->  -(A + B)
2157   if (Value *LHSV = dyn_castNegVal(LHS)) {
2158     if (LHS->getType()->isIntOrIntVector()) {
2159       if (Value *RHSV = dyn_castNegVal(RHS)) {
2160         Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
2161         return BinaryOperator::CreateNeg(NewAdd);
2162       }
2163     }
2164     
2165     return BinaryOperator::CreateSub(RHS, LHSV);
2166   }
2167
2168   // A + -B  -->  A - B
2169   if (!isa<Constant>(RHS))
2170     if (Value *V = dyn_castNegVal(RHS))
2171       return BinaryOperator::CreateSub(LHS, V);
2172
2173
2174   ConstantInt *C2;
2175   if (Value *X = dyn_castFoldableMul(LHS, C2)) {
2176     if (X == RHS)   // X*C + X --> X * (C+1)
2177       return BinaryOperator::CreateMul(RHS, AddOne(C2));
2178
2179     // X*C1 + X*C2 --> X * (C1+C2)
2180     ConstantInt *C1;
2181     if (X == dyn_castFoldableMul(RHS, C1))
2182       return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2));
2183   }
2184
2185   // X + X*C --> X * (C+1)
2186   if (dyn_castFoldableMul(RHS, C2) == LHS)
2187     return BinaryOperator::CreateMul(LHS, AddOne(C2));
2188
2189   // X + ~X --> -1   since   ~X = -X-1
2190   if (dyn_castNotVal(LHS) == RHS ||
2191       dyn_castNotVal(RHS) == LHS)
2192     return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
2193   
2194
2195   // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
2196   if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
2197     if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
2198       return R;
2199   
2200   // A+B --> A|B iff A and B have no bits set in common.
2201   if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
2202     APInt Mask = APInt::getAllOnesValue(IT->getBitWidth());
2203     APInt LHSKnownOne(IT->getBitWidth(), 0);
2204     APInt LHSKnownZero(IT->getBitWidth(), 0);
2205     ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
2206     if (LHSKnownZero != 0) {
2207       APInt RHSKnownOne(IT->getBitWidth(), 0);
2208       APInt RHSKnownZero(IT->getBitWidth(), 0);
2209       ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
2210       
2211       // No bits in common -> bitwise or.
2212       if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
2213         return BinaryOperator::CreateOr(LHS, RHS);
2214     }
2215   }
2216
2217   // W*X + Y*Z --> W * (X+Z)  iff W == Y
2218   if (I.getType()->isIntOrIntVector()) {
2219     Value *W, *X, *Y, *Z;
2220     if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
2221         match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
2222       if (W != Y) {
2223         if (W == Z) {
2224           std::swap(Y, Z);
2225         } else if (Y == X) {
2226           std::swap(W, X);
2227         } else if (X == Z) {
2228           std::swap(Y, Z);
2229           std::swap(W, X);
2230         }
2231       }
2232
2233       if (W == Y) {
2234         Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName());
2235         return BinaryOperator::CreateMul(W, NewAdd);
2236       }
2237     }
2238   }
2239
2240   if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
2241     Value *X = 0;
2242     if (match(LHS, m_Not(m_Value(X))))    // ~X + C --> (C-1) - X
2243       return BinaryOperator::CreateSub(SubOne(CRHS), X);
2244
2245     // (X & FF00) + xx00  -> (X+xx00) & FF00
2246     if (LHS->hasOneUse() &&
2247         match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
2248       Constant *Anded = ConstantExpr::getAnd(CRHS, C2);
2249       if (Anded == CRHS) {
2250         // See if all bits from the first bit set in the Add RHS up are included
2251         // in the mask.  First, get the rightmost bit.
2252         const APInt& AddRHSV = CRHS->getValue();
2253
2254         // Form a mask of all bits from the lowest bit added through the top.
2255         APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
2256
2257         // See if the and mask includes all of these bits.
2258         APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
2259
2260         if (AddRHSHighBits == AddRHSHighBitsAnd) {
2261           // Okay, the xform is safe.  Insert the new add pronto.
2262           Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName());
2263           return BinaryOperator::CreateAnd(NewAdd, C2);
2264         }
2265       }
2266     }
2267
2268     // Try to fold constant add into select arguments.
2269     if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
2270       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
2271         return R;
2272   }
2273
2274   // add (select X 0 (sub n A)) A  -->  select X A n
2275   {
2276     SelectInst *SI = dyn_cast<SelectInst>(LHS);
2277     Value *A = RHS;
2278     if (!SI) {
2279       SI = dyn_cast<SelectInst>(RHS);
2280       A = LHS;
2281     }
2282     if (SI && SI->hasOneUse()) {
2283       Value *TV = SI->getTrueValue();
2284       Value *FV = SI->getFalseValue();
2285       Value *N;
2286
2287       // Can we fold the add into the argument of the select?
2288       // We check both true and false select arguments for a matching subtract.
2289       if (match(FV, m_Zero()) &&
2290           match(TV, m_Sub(m_Value(N), m_Specific(A))))
2291         // Fold the add into the true select value.
2292         return SelectInst::Create(SI->getCondition(), N, A);
2293       if (match(TV, m_Zero()) &&
2294           match(FV, m_Sub(m_Value(N), m_Specific(A))))
2295         // Fold the add into the false select value.
2296         return SelectInst::Create(SI->getCondition(), A, N);
2297     }
2298   }
2299
2300   // Check for (add (sext x), y), see if we can merge this into an
2301   // integer add followed by a sext.
2302   if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
2303     // (add (sext x), cst) --> (sext (add x, cst'))
2304     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
2305       Constant *CI = 
2306         ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
2307       if (LHSConv->hasOneUse() &&
2308           ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
2309           WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
2310         // Insert the new, smaller add.
2311         Value *NewAdd = Builder->CreateAdd(LHSConv->getOperand(0), 
2312                                            CI, "addconv");
2313         return new SExtInst(NewAdd, I.getType());
2314       }
2315     }
2316     
2317     // (add (sext x), (sext y)) --> (sext (add int x, y))
2318     if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
2319       // Only do this if x/y have the same type, if at last one of them has a
2320       // single use (so we don't increase the number of sexts), and if the
2321       // integer add will not overflow.
2322       if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
2323           (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
2324           WillNotOverflowSignedAdd(LHSConv->getOperand(0),
2325                                    RHSConv->getOperand(0))) {
2326         // Insert the new integer add.
2327         Value *NewAdd = Builder->CreateAdd(LHSConv->getOperand(0), 
2328                                            RHSConv->getOperand(0), "addconv");
2329         return new SExtInst(NewAdd, I.getType());
2330       }
2331     }
2332   }
2333
2334   return Changed ? &I : 0;
2335 }
2336
2337 Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
2338   bool Changed = SimplifyCommutative(I);
2339   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
2340
2341   if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
2342     // X + 0 --> X
2343     if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
2344       if (CFP->isExactlyValue(ConstantFP::getNegativeZero
2345                               (I.getType())->getValueAPF()))
2346         return ReplaceInstUsesWith(I, LHS);
2347     }
2348
2349     if (isa<PHINode>(LHS))
2350       if (Instruction *NV = FoldOpIntoPhi(I))
2351         return NV;
2352   }
2353
2354   // -A + B  -->  B - A
2355   // -A + -B  -->  -(A + B)
2356   if (Value *LHSV = dyn_castFNegVal(LHS))
2357     return BinaryOperator::CreateFSub(RHS, LHSV);
2358
2359   // A + -B  -->  A - B
2360   if (!isa<Constant>(RHS))
2361     if (Value *V = dyn_castFNegVal(RHS))
2362       return BinaryOperator::CreateFSub(LHS, V);
2363
2364   // Check for X+0.0.  Simplify it to X if we know X is not -0.0.
2365   if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
2366     if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
2367       return ReplaceInstUsesWith(I, LHS);
2368
2369   // Check for (add double (sitofp x), y), see if we can merge this into an
2370   // integer add followed by a promotion.
2371   if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
2372     // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
2373     // ... if the constant fits in the integer value.  This is useful for things
2374     // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
2375     // requires a constant pool load, and generally allows the add to be better
2376     // instcombined.
2377     if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
2378       Constant *CI = 
2379       ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
2380       if (LHSConv->hasOneUse() &&
2381           ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
2382           WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
2383         // Insert the new integer add.
2384         Value *NewAdd = Builder->CreateAdd(LHSConv->getOperand(0),
2385                                            CI, "addconv");
2386         return new SIToFPInst(NewAdd, I.getType());
2387       }
2388     }
2389     
2390     // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
2391     if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
2392       // Only do this if x/y have the same type, if at last one of them has a
2393       // single use (so we don't increase the number of int->fp conversions),
2394       // and if the integer add will not overflow.
2395       if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
2396           (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
2397           WillNotOverflowSignedAdd(LHSConv->getOperand(0),
2398                                    RHSConv->getOperand(0))) {
2399         // Insert the new integer add.
2400         Value *NewAdd = Builder->CreateAdd(LHSConv->getOperand(0), 
2401                                            RHSConv->getOperand(0), "addconv");
2402         return new SIToFPInst(NewAdd, I.getType());
2403       }
2404     }
2405   }
2406   
2407   return Changed ? &I : 0;
2408 }
2409
2410 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
2411   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2412
2413   if (Op0 == Op1)                        // sub X, X  -> 0
2414     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
2415
2416   // If this is a 'B = x-(-A)', change to B = x+A...
2417   if (Value *V = dyn_castNegVal(Op1))
2418     return BinaryOperator::CreateAdd(Op0, V);
2419
2420   if (isa<UndefValue>(Op0))
2421     return ReplaceInstUsesWith(I, Op0);    // undef - X -> undef
2422   if (isa<UndefValue>(Op1))
2423     return ReplaceInstUsesWith(I, Op1);    // X - undef -> undef
2424
2425   if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
2426     // Replace (-1 - A) with (~A)...
2427     if (C->isAllOnesValue())
2428       return BinaryOperator::CreateNot(Op1);
2429
2430     // C - ~X == X + (1+C)
2431     Value *X = 0;
2432     if (match(Op1, m_Not(m_Value(X))))
2433       return BinaryOperator::CreateAdd(X, AddOne(C));
2434
2435     // -(X >>u 31) -> (X >>s 31)
2436     // -(X >>s 31) -> (X >>u 31)
2437     if (C->isZero()) {
2438       if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) {
2439         if (SI->getOpcode() == Instruction::LShr) {
2440           if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
2441             // Check to see if we are shifting out everything but the sign bit.
2442             if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
2443                 SI->getType()->getPrimitiveSizeInBits()-1) {
2444               // Ok, the transformation is safe.  Insert AShr.
2445               return BinaryOperator::Create(Instruction::AShr, 
2446                                           SI->getOperand(0), CU, SI->getName());
2447             }
2448           }
2449         }
2450         else if (SI->getOpcode() == Instruction::AShr) {
2451           if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
2452             // Check to see if we are shifting out everything but the sign bit.
2453             if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
2454                 SI->getType()->getPrimitiveSizeInBits()-1) {
2455               // Ok, the transformation is safe.  Insert LShr. 
2456               return BinaryOperator::CreateLShr(
2457                                           SI->getOperand(0), CU, SI->getName());
2458             }
2459           }
2460         }
2461       }
2462     }
2463
2464     // Try to fold constant sub into select arguments.
2465     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
2466       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
2467         return R;
2468
2469     // C - zext(bool) -> bool ? C - 1 : C
2470     if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1))
2471       if (ZI->getSrcTy() == Type::getInt1Ty(*Context))
2472         return SelectInst::Create(ZI->getOperand(0), SubOne(C), C);
2473   }
2474
2475   if (I.getType() == Type::getInt1Ty(*Context))
2476     return BinaryOperator::CreateXor(Op0, Op1);
2477
2478   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
2479     if (Op1I->getOpcode() == Instruction::Add) {
2480       if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y
2481         return BinaryOperator::CreateNeg(Op1I->getOperand(1),
2482                                          I.getName());
2483       else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y
2484         return BinaryOperator::CreateNeg(Op1I->getOperand(0),
2485                                          I.getName());
2486       else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
2487         if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
2488           // C1-(X+C2) --> (C1-C2)-X
2489           return BinaryOperator::CreateSub(
2490             ConstantExpr::getSub(CI1, CI2), Op1I->getOperand(0));
2491       }
2492     }
2493
2494     if (Op1I->hasOneUse()) {
2495       // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
2496       // is not used by anyone else...
2497       //
2498       if (Op1I->getOpcode() == Instruction::Sub) {
2499         // Swap the two operands of the subexpr...
2500         Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
2501         Op1I->setOperand(0, IIOp1);
2502         Op1I->setOperand(1, IIOp0);
2503
2504         // Create the new top level add instruction...
2505         return BinaryOperator::CreateAdd(Op0, Op1);
2506       }
2507
2508       // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
2509       //
2510       if (Op1I->getOpcode() == Instruction::And &&
2511           (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
2512         Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
2513
2514         Value *NewNot = Builder->CreateNot(OtherOp, "B.not");
2515         return BinaryOperator::CreateAnd(Op0, NewNot);
2516       }
2517
2518       // 0 - (X sdiv C)  -> (X sdiv -C)
2519       if (Op1I->getOpcode() == Instruction::SDiv)
2520         if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
2521           if (CSI->isZero())
2522             if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1)))
2523               return BinaryOperator::CreateSDiv(Op1I->getOperand(0),
2524                                           ConstantExpr::getNeg(DivRHS));
2525
2526       // X - X*C --> X * (1-C)
2527       ConstantInt *C2 = 0;
2528       if (dyn_castFoldableMul(Op1I, C2) == Op0) {
2529         Constant *CP1 = 
2530           ConstantExpr::getSub(ConstantInt::get(I.getType(), 1),
2531                                              C2);
2532         return BinaryOperator::CreateMul(Op0, CP1);
2533       }
2534     }
2535   }
2536
2537   if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2538     if (Op0I->getOpcode() == Instruction::Add) {
2539       if (Op0I->getOperand(0) == Op1)             // (Y+X)-Y == X
2540         return ReplaceInstUsesWith(I, Op0I->getOperand(1));
2541       else if (Op0I->getOperand(1) == Op1)        // (X+Y)-Y == X
2542         return ReplaceInstUsesWith(I, Op0I->getOperand(0));
2543     } else if (Op0I->getOpcode() == Instruction::Sub) {
2544       if (Op0I->getOperand(0) == Op1)             // (X-Y)-X == -Y
2545         return BinaryOperator::CreateNeg(Op0I->getOperand(1),
2546                                          I.getName());
2547     }
2548   }
2549
2550   ConstantInt *C1;
2551   if (Value *X = dyn_castFoldableMul(Op0, C1)) {
2552     if (X == Op1)  // X*C - X --> X * (C-1)
2553       return BinaryOperator::CreateMul(Op1, SubOne(C1));
2554
2555     ConstantInt *C2;   // X*C1 - X*C2 -> X * (C1-C2)
2556     if (X == dyn_castFoldableMul(Op1, C2))
2557       return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2));
2558   }
2559   return 0;
2560 }
2561
2562 Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
2563   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2564
2565   // If this is a 'B = x-(-A)', change to B = x+A...
2566   if (Value *V = dyn_castFNegVal(Op1))
2567     return BinaryOperator::CreateFAdd(Op0, V);
2568
2569   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
2570     if (Op1I->getOpcode() == Instruction::FAdd) {
2571       if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y
2572         return BinaryOperator::CreateFNeg(Op1I->getOperand(1),
2573                                           I.getName());
2574       else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y
2575         return BinaryOperator::CreateFNeg(Op1I->getOperand(0),
2576                                           I.getName());
2577     }
2578   }
2579
2580   return 0;
2581 }
2582
2583 /// isSignBitCheck - Given an exploded icmp instruction, return true if the
2584 /// comparison only checks the sign bit.  If it only checks the sign bit, set
2585 /// TrueIfSigned if the result of the comparison is true when the input value is
2586 /// signed.
2587 static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
2588                            bool &TrueIfSigned) {
2589   switch (pred) {
2590   case ICmpInst::ICMP_SLT:   // True if LHS s< 0
2591     TrueIfSigned = true;
2592     return RHS->isZero();
2593   case ICmpInst::ICMP_SLE:   // True if LHS s<= RHS and RHS == -1
2594     TrueIfSigned = true;
2595     return RHS->isAllOnesValue();
2596   case ICmpInst::ICMP_SGT:   // True if LHS s> -1
2597     TrueIfSigned = false;
2598     return RHS->isAllOnesValue();
2599   case ICmpInst::ICMP_UGT:
2600     // True if LHS u> RHS and RHS == high-bit-mask - 1
2601     TrueIfSigned = true;
2602     return RHS->getValue() ==
2603       APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits());
2604   case ICmpInst::ICMP_UGE: 
2605     // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
2606     TrueIfSigned = true;
2607     return RHS->getValue().isSignBit();
2608   default:
2609     return false;
2610   }
2611 }
2612
2613 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
2614   bool Changed = SimplifyCommutative(I);
2615   Value *Op0 = I.getOperand(0);
2616
2617   if (isa<UndefValue>(I.getOperand(1)))              // undef * X -> 0
2618     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
2619
2620   // Simplify mul instructions with a constant RHS...
2621   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
2622     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2623
2624       // ((X << C1)*C2) == (X * (C2 << C1))
2625       if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0))
2626         if (SI->getOpcode() == Instruction::Shl)
2627           if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
2628             return BinaryOperator::CreateMul(SI->getOperand(0),
2629                                         ConstantExpr::getShl(CI, ShOp));
2630
2631       if (CI->isZero())
2632         return ReplaceInstUsesWith(I, Op1);  // X * 0  == 0
2633       if (CI->equalsInt(1))                  // X * 1  == X
2634         return ReplaceInstUsesWith(I, Op0);
2635       if (CI->isAllOnesValue())              // X * -1 == 0 - X
2636         return BinaryOperator::CreateNeg(Op0, I.getName());
2637
2638       const APInt& Val = cast<ConstantInt>(CI)->getValue();
2639       if (Val.isPowerOf2()) {          // Replace X*(2^C) with X << C
2640         return BinaryOperator::CreateShl(Op0,
2641                  ConstantInt::get(Op0->getType(), Val.logBase2()));
2642       }
2643     } else if (isa<VectorType>(Op1->getType())) {
2644       if (Op1->isNullValue())
2645         return ReplaceInstUsesWith(I, Op1);
2646
2647       if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
2648         if (Op1V->isAllOnesValue())              // X * -1 == 0 - X
2649           return BinaryOperator::CreateNeg(Op0, I.getName());
2650
2651         // As above, vector X*splat(1.0) -> X in all defined cases.
2652         if (Constant *Splat = Op1V->getSplatValue()) {
2653           if (ConstantInt *CI = dyn_cast<ConstantInt>(Splat))
2654             if (CI->equalsInt(1))
2655               return ReplaceInstUsesWith(I, Op0);
2656         }
2657       }
2658     }
2659     
2660     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
2661       if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() &&
2662           isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1)) {
2663         // Canonicalize (X+C1)*C2 -> X*C2+C1*C2.
2664         Value *Add = Builder->CreateMul(Op0I->getOperand(0), Op1, "tmp");
2665         Value *C1C2 = Builder->CreateMul(Op1, Op0I->getOperand(1));
2666         return BinaryOperator::CreateAdd(Add, C1C2);
2667         
2668       }
2669
2670     // Try to fold constant mul into select arguments.
2671     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2672       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
2673         return R;
2674
2675     if (isa<PHINode>(Op0))
2676       if (Instruction *NV = FoldOpIntoPhi(I))
2677         return NV;
2678   }
2679
2680   if (Value *Op0v = dyn_castNegVal(Op0))     // -X * -Y = X*Y
2681     if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
2682       return BinaryOperator::CreateMul(Op0v, Op1v);
2683
2684   // (X / Y) *  Y = X - (X % Y)
2685   // (X / Y) * -Y = (X % Y) - X
2686   {
2687     Value *Op1 = I.getOperand(1);
2688     BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0);
2689     if (!BO ||
2690         (BO->getOpcode() != Instruction::UDiv && 
2691          BO->getOpcode() != Instruction::SDiv)) {
2692       Op1 = Op0;
2693       BO = dyn_cast<BinaryOperator>(I.getOperand(1));
2694     }
2695     Value *Neg = dyn_castNegVal(Op1);
2696     if (BO && BO->hasOneUse() &&
2697         (BO->getOperand(1) == Op1 || BO->getOperand(1) == Neg) &&
2698         (BO->getOpcode() == Instruction::UDiv ||
2699          BO->getOpcode() == Instruction::SDiv)) {
2700       Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
2701
2702       // If the division is exact, X % Y is zero.
2703       if (SDivOperator *SDiv = dyn_cast<SDivOperator>(BO))
2704         if (SDiv->isExact()) {
2705           if (Op1BO == Op1)
2706             return ReplaceInstUsesWith(I, Op0BO);
2707           else
2708             return BinaryOperator::CreateNeg(Op0BO);
2709         }
2710
2711       Value *Rem;
2712       if (BO->getOpcode() == Instruction::UDiv)
2713         Rem = Builder->CreateURem(Op0BO, Op1BO);
2714       else
2715         Rem = Builder->CreateSRem(Op0BO, Op1BO);
2716       Rem->takeName(BO);
2717
2718       if (Op1BO == Op1)
2719         return BinaryOperator::CreateSub(Op0BO, Rem);
2720       return BinaryOperator::CreateSub(Rem, Op0BO);
2721     }
2722   }
2723
2724   if (I.getType() == Type::getInt1Ty(*Context))
2725     return BinaryOperator::CreateAnd(Op0, I.getOperand(1));
2726
2727   // If one of the operands of the multiply is a cast from a boolean value, then
2728   // we know the bool is either zero or one, so this is a 'masking' multiply.
2729   // See if we can simplify things based on how the boolean was originally
2730   // formed.
2731   CastInst *BoolCast = 0;
2732   if (ZExtInst *CI = dyn_cast<ZExtInst>(Op0))
2733     if (CI->getOperand(0)->getType() == Type::getInt1Ty(*Context))
2734       BoolCast = CI;
2735   if (!BoolCast)
2736     if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1)))
2737       if (CI->getOperand(0)->getType() == Type::getInt1Ty(*Context))
2738         BoolCast = CI;
2739   if (BoolCast) {
2740     if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) {
2741       Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1);
2742       const Type *SCOpTy = SCIOp0->getType();
2743       bool TIS = false;
2744       
2745       // If the icmp is true iff the sign bit of X is set, then convert this
2746       // multiply into a shift/and combination.
2747       if (isa<ConstantInt>(SCIOp1) &&
2748           isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) &&
2749           TIS) {
2750         // Shift the X value right to turn it into "all signbits".
2751         Constant *Amt = ConstantInt::get(SCIOp0->getType(),
2752                                           SCOpTy->getPrimitiveSizeInBits()-1);
2753         Value *V = Builder->CreateAShr(SCIOp0, Amt,
2754                                     BoolCast->getOperand(0)->getName()+".mask");
2755
2756         // If the multiply type is not the same as the source type, sign extend
2757         // or truncate to the multiply type.
2758         if (I.getType() != V->getType())
2759           V = Builder->CreateIntCast(V, I.getType(), true);
2760
2761         Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
2762         return BinaryOperator::CreateAnd(V, OtherOp);
2763       }
2764     }
2765   }
2766
2767   return Changed ? &I : 0;
2768 }
2769
2770 Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
2771   bool Changed = SimplifyCommutative(I);
2772   Value *Op0 = I.getOperand(0);
2773
2774   // Simplify mul instructions with a constant RHS...
2775   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
2776     if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
2777       // "In IEEE floating point, x*1 is not equivalent to x for nans.  However,
2778       // ANSI says we can drop signals, so we can do this anyway." (from GCC)
2779       if (Op1F->isExactlyValue(1.0))
2780         return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul double %X, 1.0'
2781     } else if (isa<VectorType>(Op1->getType())) {
2782       if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
2783         // As above, vector X*splat(1.0) -> X in all defined cases.
2784         if (Constant *Splat = Op1V->getSplatValue()) {
2785           if (ConstantFP *F = dyn_cast<ConstantFP>(Splat))
2786             if (F->isExactlyValue(1.0))
2787               return ReplaceInstUsesWith(I, Op0);
2788         }
2789       }
2790     }
2791
2792     // Try to fold constant mul into select arguments.
2793     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2794       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
2795         return R;
2796
2797     if (isa<PHINode>(Op0))
2798       if (Instruction *NV = FoldOpIntoPhi(I))
2799         return NV;
2800   }
2801
2802   if (Value *Op0v = dyn_castFNegVal(Op0))     // -X * -Y = X*Y
2803     if (Value *Op1v = dyn_castFNegVal(I.getOperand(1)))
2804       return BinaryOperator::CreateFMul(Op0v, Op1v);
2805
2806   return Changed ? &I : 0;
2807 }
2808
2809 /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select
2810 /// instruction.
2811 bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
2812   SelectInst *SI = cast<SelectInst>(I.getOperand(1));
2813   
2814   // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
2815   int NonNullOperand = -1;
2816   if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
2817     if (ST->isNullValue())
2818       NonNullOperand = 2;
2819   // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
2820   if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
2821     if (ST->isNullValue())
2822       NonNullOperand = 1;
2823   
2824   if (NonNullOperand == -1)
2825     return false;
2826   
2827   Value *SelectCond = SI->getOperand(0);
2828   
2829   // Change the div/rem to use 'Y' instead of the select.
2830   I.setOperand(1, SI->getOperand(NonNullOperand));
2831   
2832   // Okay, we know we replace the operand of the div/rem with 'Y' with no
2833   // problem.  However, the select, or the condition of the select may have
2834   // multiple uses.  Based on our knowledge that the operand must be non-zero,
2835   // propagate the known value for the select into other uses of it, and
2836   // propagate a known value of the condition into its other users.
2837   
2838   // If the select and condition only have a single use, don't bother with this,
2839   // early exit.
2840   if (SI->use_empty() && SelectCond->hasOneUse())
2841     return true;
2842   
2843   // Scan the current block backward, looking for other uses of SI.
2844   BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin();
2845   
2846   while (BBI != BBFront) {
2847     --BBI;
2848     // If we found a call to a function, we can't assume it will return, so
2849     // information from below it cannot be propagated above it.
2850     if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
2851       break;
2852     
2853     // Replace uses of the select or its condition with the known values.
2854     for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
2855          I != E; ++I) {
2856       if (*I == SI) {
2857         *I = SI->getOperand(NonNullOperand);
2858         Worklist.Add(BBI);
2859       } else if (*I == SelectCond) {
2860         *I = NonNullOperand == 1 ? ConstantInt::getTrue(*Context) :
2861                                    ConstantInt::getFalse(*Context);
2862         Worklist.Add(BBI);
2863       }
2864     }
2865     
2866     // If we past the instruction, quit looking for it.
2867     if (&*BBI == SI)
2868       SI = 0;
2869     if (&*BBI == SelectCond)
2870       SelectCond = 0;
2871     
2872     // If we ran out of things to eliminate, break out of the loop.
2873     if (SelectCond == 0 && SI == 0)
2874       break;
2875     
2876   }
2877   return true;
2878 }
2879
2880
2881 /// This function implements the transforms on div instructions that work
2882 /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is
2883 /// used by the visitors to those instructions.
2884 /// @brief Transforms common to all three div instructions
2885 Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) {
2886   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2887
2888   // undef / X -> 0        for integer.
2889   // undef / X -> undef    for FP (the undef could be a snan).
2890   if (isa<UndefValue>(Op0)) {
2891     if (Op0->getType()->isFPOrFPVector())
2892       return ReplaceInstUsesWith(I, Op0);
2893     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
2894   }
2895
2896   // X / undef -> undef
2897   if (isa<UndefValue>(Op1))
2898     return ReplaceInstUsesWith(I, Op1);
2899
2900   return 0;
2901 }
2902
2903 /// This function implements the transforms common to both integer division
2904 /// instructions (udiv and sdiv). It is called by the visitors to those integer
2905 /// division instructions.
2906 /// @brief Common integer divide transforms
2907 Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
2908   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2909
2910   // (sdiv X, X) --> 1     (udiv X, X) --> 1
2911   if (Op0 == Op1) {
2912     if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) {
2913       Constant *CI = ConstantInt::get(Ty->getElementType(), 1);
2914       std::vector<Constant*> Elts(Ty->getNumElements(), CI);
2915       return ReplaceInstUsesWith(I, ConstantVector::get(Elts));
2916     }
2917
2918     Constant *CI = ConstantInt::get(I.getType(), 1);
2919     return ReplaceInstUsesWith(I, CI);
2920   }
2921   
2922   if (Instruction *Common = commonDivTransforms(I))
2923     return Common;
2924   
2925   // Handle cases involving: [su]div X, (select Cond, Y, Z)
2926   // This does not apply for fdiv.
2927   if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
2928     return &I;
2929
2930   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2931     // div X, 1 == X
2932     if (RHS->equalsInt(1))
2933       return ReplaceInstUsesWith(I, Op0);
2934
2935     // (X / C1) / C2  -> X / (C1*C2)
2936     if (Instruction *LHS = dyn_cast<Instruction>(Op0))
2937       if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
2938         if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
2939           if (MultiplyOverflows(RHS, LHSRHS,
2940                                 I.getOpcode()==Instruction::SDiv))
2941             return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
2942           else 
2943             return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
2944                                       ConstantExpr::getMul(RHS, LHSRHS));
2945         }
2946
2947     if (!RHS->isZero()) { // avoid X udiv 0
2948       if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2949         if (Instruction *R = FoldOpIntoSelect(I, SI, this))
2950           return R;
2951       if (isa<PHINode>(Op0))
2952         if (Instruction *NV = FoldOpIntoPhi(I))
2953           return NV;
2954     }
2955   }
2956
2957   // 0 / X == 0, we don't need to preserve faults!
2958   if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0))
2959     if (LHS->equalsInt(0))
2960       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
2961
2962   // It can't be division by zero, hence it must be division by one.
2963   if (I.getType() == Type::getInt1Ty(*Context))
2964     return ReplaceInstUsesWith(I, Op0);
2965
2966   if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
2967     if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue()))
2968       // div X, 1 == X
2969       if (X->isOne())
2970         return ReplaceInstUsesWith(I, Op0);
2971   }
2972
2973   return 0;
2974 }
2975
2976 Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
2977   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2978
2979   // Handle the integer div common cases
2980   if (Instruction *Common = commonIDivTransforms(I))
2981     return Common;
2982
2983   if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) {
2984     // X udiv C^2 -> X >> C
2985     // Check to see if this is an unsigned division with an exact power of 2,
2986     // if so, convert to a right shift.
2987     if (C->getValue().isPowerOf2())  // 0 not included in isPowerOf2
2988       return BinaryOperator::CreateLShr(Op0, 
2989             ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
2990
2991     // X udiv C, where C >= signbit
2992     if (C->getValue().isNegative()) {
2993       Value *IC = Builder->CreateICmpULT( Op0, C);
2994       return SelectInst::Create(IC, Constant::getNullValue(I.getType()),
2995                                 ConstantInt::get(I.getType(), 1));
2996     }
2997   }
2998
2999   // X udiv (C1 << N), where C1 is "1<<C2"  -->  X >> (N+C2)
3000   if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) {
3001     if (RHSI->getOpcode() == Instruction::Shl &&
3002         isa<ConstantInt>(RHSI->getOperand(0))) {
3003       const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue();
3004       if (C1.isPowerOf2()) {
3005         Value *N = RHSI->getOperand(1);
3006         const Type *NTy = N->getType();
3007         if (uint32_t C2 = C1.logBase2())
3008           N = Builder->CreateAdd(N, ConstantInt::get(NTy, C2), "tmp");
3009         return BinaryOperator::CreateLShr(Op0, N);
3010       }
3011     }
3012   }
3013   
3014   // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2)
3015   // where C1&C2 are powers of two.
3016   if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) 
3017     if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
3018       if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2)))  {
3019         const APInt &TVA = STO->getValue(), &FVA = SFO->getValue();
3020         if (TVA.isPowerOf2() && FVA.isPowerOf2()) {
3021           // Compute the shift amounts
3022           uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
3023           // Construct the "on true" case of the select
3024           Constant *TC = ConstantInt::get(Op0->getType(), TSA);
3025           Value *TSI = Builder->CreateLShr(Op0, TC, SI->getName()+".t");
3026   
3027           // Construct the "on false" case of the select
3028           Constant *FC = ConstantInt::get(Op0->getType(), FSA); 
3029           Value *FSI = Builder->CreateLShr(Op0, FC, SI->getName()+".f");
3030
3031           // construct the select instruction and return it.
3032           return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName());
3033         }
3034       }
3035   return 0;
3036 }
3037
3038 Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
3039   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3040
3041   // Handle the integer div common cases
3042   if (Instruction *Common = commonIDivTransforms(I))
3043     return Common;
3044
3045   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3046     // sdiv X, -1 == -X
3047     if (RHS->isAllOnesValue())
3048       return BinaryOperator::CreateNeg(Op0);
3049
3050     // sdiv X, C  -->  ashr X, log2(C)
3051     if (cast<SDivOperator>(&I)->isExact() &&
3052         RHS->getValue().isNonNegative() &&
3053         RHS->getValue().isPowerOf2()) {
3054       Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
3055                                             RHS->getValue().exactLogBase2());
3056       return BinaryOperator::CreateAShr(Op0, ShAmt, I.getName());
3057     }
3058
3059     // -X/C  -->  X/-C  provided the negation doesn't overflow.
3060     if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
3061       if (isa<Constant>(Sub->getOperand(0)) &&
3062           cast<Constant>(Sub->getOperand(0))->isNullValue() &&
3063           Sub->hasNoSignedWrap())
3064         return BinaryOperator::CreateSDiv(Sub->getOperand(1),
3065                                           ConstantExpr::getNeg(RHS));
3066   }
3067
3068   // If the sign bits of both operands are zero (i.e. we can prove they are
3069   // unsigned inputs), turn this into a udiv.
3070   if (I.getType()->isInteger()) {
3071     APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
3072     if (MaskedValueIsZero(Op0, Mask)) {
3073       if (MaskedValueIsZero(Op1, Mask)) {
3074         // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
3075         return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
3076       }
3077       ConstantInt *ShiftedInt;
3078       if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value())) &&
3079           ShiftedInt->getValue().isPowerOf2()) {
3080         // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
3081         // Safe because the only negative value (1 << Y) can take on is
3082         // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
3083         // the sign bit set.
3084         return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
3085       }
3086     }
3087   }
3088   
3089   return 0;
3090 }
3091
3092 Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
3093   return commonDivTransforms(I);
3094 }
3095
3096 /// This function implements the transforms on rem instructions that work
3097 /// regardless of the kind of rem instruction it is (urem, srem, or frem). It 
3098 /// is used by the visitors to those instructions.
3099 /// @brief Transforms common to all three rem instructions
3100 Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) {
3101   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3102
3103   if (isa<UndefValue>(Op0)) {             // undef % X -> 0
3104     if (I.getType()->isFPOrFPVector())
3105       return ReplaceInstUsesWith(I, Op0);  // X % undef -> undef (could be SNaN)
3106     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3107   }
3108   if (isa<UndefValue>(Op1))
3109     return ReplaceInstUsesWith(I, Op1);  // X % undef -> undef
3110
3111   // Handle cases involving: rem X, (select Cond, Y, Z)
3112   if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
3113     return &I;
3114
3115   return 0;
3116 }
3117
3118 /// This function implements the transforms common to both integer remainder
3119 /// instructions (urem and srem). It is called by the visitors to those integer
3120 /// remainder instructions.
3121 /// @brief Common integer remainder transforms
3122 Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
3123   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3124
3125   if (Instruction *common = commonRemTransforms(I))
3126     return common;
3127
3128   // 0 % X == 0 for integer, we don't need to preserve faults!
3129   if (Constant *LHS = dyn_cast<Constant>(Op0))
3130     if (LHS->isNullValue())
3131       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3132
3133   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3134     // X % 0 == undef, we don't need to preserve faults!
3135     if (RHS->equalsInt(0))
3136       return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
3137     
3138     if (RHS->equalsInt(1))  // X % 1 == 0
3139       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3140
3141     if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
3142       if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
3143         if (Instruction *R = FoldOpIntoSelect(I, SI, this))
3144           return R;
3145       } else if (isa<PHINode>(Op0I)) {
3146         if (Instruction *NV = FoldOpIntoPhi(I))
3147           return NV;
3148       }
3149
3150       // See if we can fold away this rem instruction.
3151       if (SimplifyDemandedInstructionBits(I))
3152         return &I;
3153     }
3154   }
3155
3156   return 0;
3157 }
3158
3159 Instruction *InstCombiner::visitURem(BinaryOperator &I) {
3160   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3161
3162   if (Instruction *common = commonIRemTransforms(I))
3163     return common;
3164   
3165   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
3166     // X urem C^2 -> X and C
3167     // Check to see if this is an unsigned remainder with an exact power of 2,
3168     // if so, convert to a bitwise and.
3169     if (ConstantInt *C = dyn_cast<ConstantInt>(RHS))
3170       if (C->getValue().isPowerOf2())
3171         return BinaryOperator::CreateAnd(Op0, SubOne(C));
3172   }
3173
3174   if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
3175     // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1)  
3176     if (RHSI->getOpcode() == Instruction::Shl &&
3177         isa<ConstantInt>(RHSI->getOperand(0))) {
3178       if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) {
3179         Constant *N1 = Constant::getAllOnesValue(I.getType());
3180         Value *Add = Builder->CreateAdd(RHSI, N1, "tmp");
3181         return BinaryOperator::CreateAnd(Op0, Add);
3182       }
3183     }
3184   }
3185
3186   // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2)
3187   // where C1&C2 are powers of two.
3188   if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
3189     if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
3190       if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) {
3191         // STO == 0 and SFO == 0 handled above.
3192         if ((STO->getValue().isPowerOf2()) && 
3193             (SFO->getValue().isPowerOf2())) {
3194           Value *TrueAnd = Builder->CreateAnd(Op0, SubOne(STO),
3195                                               SI->getName()+".t");
3196           Value *FalseAnd = Builder->CreateAnd(Op0, SubOne(SFO),
3197                                                SI->getName()+".f");
3198           return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
3199         }
3200       }
3201   }
3202   
3203   return 0;
3204 }
3205
3206 Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
3207   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3208
3209   // Handle the integer rem common cases
3210   if (Instruction *Common = commonIRemTransforms(I))
3211     return Common;
3212   
3213   if (Value *RHSNeg = dyn_castNegVal(Op1))
3214     if (!isa<Constant>(RHSNeg) ||
3215         (isa<ConstantInt>(RHSNeg) &&
3216          cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
3217       // X % -Y -> X % Y
3218       Worklist.AddValue(I.getOperand(1));
3219       I.setOperand(1, RHSNeg);
3220       return &I;
3221     }
3222
3223   // If the sign bits of both operands are zero (i.e. we can prove they are
3224   // unsigned inputs), turn this into a urem.
3225   if (I.getType()->isInteger()) {
3226     APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
3227     if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
3228       // X srem Y -> X urem Y, iff X and Y don't have sign bit set
3229       return BinaryOperator::CreateURem(Op0, Op1, I.getName());
3230     }
3231   }
3232
3233   // If it's a constant vector, flip any negative values positive.
3234   if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) {
3235     unsigned VWidth = RHSV->getNumOperands();
3236
3237     bool hasNegative = false;
3238     for (unsigned i = 0; !hasNegative && i != VWidth; ++i)
3239       if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i)))
3240         if (RHS->getValue().isNegative())
3241           hasNegative = true;
3242
3243     if (hasNegative) {
3244       std::vector<Constant *> Elts(VWidth);
3245       for (unsigned i = 0; i != VWidth; ++i) {
3246         if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) {
3247           if (RHS->getValue().isNegative())
3248             Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
3249           else
3250             Elts[i] = RHS;
3251         }
3252       }
3253
3254       Constant *NewRHSV = ConstantVector::get(Elts);
3255       if (NewRHSV != RHSV) {
3256         Worklist.AddValue(I.getOperand(1));
3257         I.setOperand(1, NewRHSV);
3258         return &I;
3259       }
3260     }
3261   }
3262
3263   return 0;
3264 }
3265
3266 Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
3267   return commonRemTransforms(I);
3268 }
3269
3270 // isOneBitSet - Return true if there is exactly one bit set in the specified
3271 // constant.
3272 static bool isOneBitSet(const ConstantInt *CI) {
3273   return CI->getValue().isPowerOf2();
3274 }
3275
3276 // isHighOnes - Return true if the constant is of the form 1+0+.
3277 // This is the same as lowones(~X).
3278 static bool isHighOnes(const ConstantInt *CI) {
3279   return (~CI->getValue() + 1).isPowerOf2();
3280 }
3281
3282 /// getICmpCode - Encode a icmp predicate into a three bit mask.  These bits
3283 /// are carefully arranged to allow folding of expressions such as:
3284 ///
3285 ///      (A < B) | (A > B) --> (A != B)
3286 ///
3287 /// Note that this is only valid if the first and second predicates have the
3288 /// same sign. Is illegal to do: (A u< B) | (A s> B) 
3289 ///
3290 /// Three bits are used to represent the condition, as follows:
3291 ///   0  A > B
3292 ///   1  A == B
3293 ///   2  A < B
3294 ///
3295 /// <=>  Value  Definition
3296 /// 000     0   Always false
3297 /// 001     1   A >  B
3298 /// 010     2   A == B
3299 /// 011     3   A >= B
3300 /// 100     4   A <  B
3301 /// 101     5   A != B
3302 /// 110     6   A <= B
3303 /// 111     7   Always true
3304 ///  
3305 static unsigned getICmpCode(const ICmpInst *ICI) {
3306   switch (ICI->getPredicate()) {
3307     // False -> 0
3308   case ICmpInst::ICMP_UGT: return 1;  // 001
3309   case ICmpInst::ICMP_SGT: return 1;  // 001
3310   case ICmpInst::ICMP_EQ:  return 2;  // 010
3311   case ICmpInst::ICMP_UGE: return 3;  // 011
3312   case ICmpInst::ICMP_SGE: return 3;  // 011
3313   case ICmpInst::ICMP_ULT: return 4;  // 100
3314   case ICmpInst::ICMP_SLT: return 4;  // 100
3315   case ICmpInst::ICMP_NE:  return 5;  // 101
3316   case ICmpInst::ICMP_ULE: return 6;  // 110
3317   case ICmpInst::ICMP_SLE: return 6;  // 110
3318     // True -> 7
3319   default:
3320     llvm_unreachable("Invalid ICmp predicate!");
3321     return 0;
3322   }
3323 }
3324
3325 /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp
3326 /// predicate into a three bit mask. It also returns whether it is an ordered
3327 /// predicate by reference.
3328 static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) {
3329   isOrdered = false;
3330   switch (CC) {
3331   case FCmpInst::FCMP_ORD: isOrdered = true; return 0;  // 000
3332   case FCmpInst::FCMP_UNO:                   return 0;  // 000
3333   case FCmpInst::FCMP_OGT: isOrdered = true; return 1;  // 001
3334   case FCmpInst::FCMP_UGT:                   return 1;  // 001
3335   case FCmpInst::FCMP_OEQ: isOrdered = true; return 2;  // 010
3336   case FCmpInst::FCMP_UEQ:                   return 2;  // 010
3337   case FCmpInst::FCMP_OGE: isOrdered = true; return 3;  // 011
3338   case FCmpInst::FCMP_UGE:                   return 3;  // 011
3339   case FCmpInst::FCMP_OLT: isOrdered = true; return 4;  // 100
3340   case FCmpInst::FCMP_ULT:                   return 4;  // 100
3341   case FCmpInst::FCMP_ONE: isOrdered = true; return 5;  // 101
3342   case FCmpInst::FCMP_UNE:                   return 5;  // 101
3343   case FCmpInst::FCMP_OLE: isOrdered = true; return 6;  // 110
3344   case FCmpInst::FCMP_ULE:                   return 6;  // 110
3345     // True -> 7
3346   default:
3347     // Not expecting FCMP_FALSE and FCMP_TRUE;
3348     llvm_unreachable("Unexpected FCmp predicate!");
3349     return 0;
3350   }
3351 }
3352
3353 /// getICmpValue - This is the complement of getICmpCode, which turns an
3354 /// opcode and two operands into either a constant true or false, or a brand 
3355 /// new ICmp instruction. The sign is passed in to determine which kind
3356 /// of predicate to use in the new icmp instruction.
3357 static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS,
3358                            LLVMContext *Context) {
3359   switch (code) {
3360   default: llvm_unreachable("Illegal ICmp code!");
3361   case  0: return ConstantInt::getFalse(*Context);
3362   case  1: 
3363     if (sign)
3364       return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS);
3365     else
3366       return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS);
3367   case  2: return new ICmpInst(ICmpInst::ICMP_EQ,  LHS, RHS);
3368   case  3: 
3369     if (sign)
3370       return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS);
3371     else
3372       return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS);
3373   case  4: 
3374     if (sign)
3375       return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS);
3376     else
3377       return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS);
3378   case  5: return new ICmpInst(ICmpInst::ICMP_NE,  LHS, RHS);
3379   case  6: 
3380     if (sign)
3381       return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS);
3382     else
3383       return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS);
3384   case  7: return ConstantInt::getTrue(*Context);
3385   }
3386 }
3387
3388 /// getFCmpValue - This is the complement of getFCmpCode, which turns an
3389 /// opcode and two operands into either a FCmp instruction. isordered is passed
3390 /// in to determine which kind of predicate to use in the new fcmp instruction.
3391 static Value *getFCmpValue(bool isordered, unsigned code,
3392                            Value *LHS, Value *RHS, LLVMContext *Context) {
3393   switch (code) {
3394   default: llvm_unreachable("Illegal FCmp code!");
3395   case  0:
3396     if (isordered)
3397       return new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS);
3398     else
3399       return new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS);
3400   case  1: 
3401     if (isordered)
3402       return new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS);
3403     else
3404       return new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS);
3405   case  2: 
3406     if (isordered)
3407       return new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS);
3408     else
3409       return new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS);
3410   case  3: 
3411     if (isordered)
3412       return new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS);
3413     else
3414       return new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS);
3415   case  4: 
3416     if (isordered)
3417       return new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS);
3418     else
3419       return new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS);
3420   case  5: 
3421     if (isordered)
3422       return new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS);
3423     else
3424       return new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS);
3425   case  6: 
3426     if (isordered)
3427       return new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS);
3428     else
3429       return new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS);
3430   case  7: return ConstantInt::getTrue(*Context);
3431   }
3432 }
3433
3434 /// PredicatesFoldable - Return true if both predicates match sign or if at
3435 /// least one of them is an equality comparison (which is signless).
3436 static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
3437   return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) ||
3438          (ICmpInst::isSignedPredicate(p1) && ICmpInst::isEquality(p2)) ||
3439          (ICmpInst::isSignedPredicate(p2) && ICmpInst::isEquality(p1));
3440 }
3441
3442 namespace { 
3443 // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
3444 struct FoldICmpLogical {
3445   InstCombiner &IC;
3446   Value *LHS, *RHS;
3447   ICmpInst::Predicate pred;
3448   FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI)
3449     : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)),
3450       pred(ICI->getPredicate()) {}
3451   bool shouldApply(Value *V) const {
3452     if (ICmpInst *ICI = dyn_cast<ICmpInst>(V))
3453       if (PredicatesFoldable(pred, ICI->getPredicate()))
3454         return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) ||
3455                 (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS));
3456     return false;
3457   }
3458   Instruction *apply(Instruction &Log) const {
3459     ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0));
3460     if (ICI->getOperand(0) != LHS) {
3461       assert(ICI->getOperand(1) == LHS);
3462       ICI->swapOperands();  // Swap the LHS and RHS of the ICmp
3463     }
3464
3465     ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1));
3466     unsigned LHSCode = getICmpCode(ICI);
3467     unsigned RHSCode = getICmpCode(RHSICI);
3468     unsigned Code;
3469     switch (Log.getOpcode()) {
3470     case Instruction::And: Code = LHSCode & RHSCode; break;
3471     case Instruction::Or:  Code = LHSCode | RHSCode; break;
3472     case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
3473     default: llvm_unreachable("Illegal logical opcode!"); return 0;
3474     }
3475
3476     bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || 
3477                     ICmpInst::isSignedPredicate(ICI->getPredicate());
3478       
3479     Value *RV = getICmpValue(isSigned, Code, LHS, RHS, IC.getContext());
3480     if (Instruction *I = dyn_cast<Instruction>(RV))
3481       return I;
3482     // Otherwise, it's a constant boolean value...
3483     return IC.ReplaceInstUsesWith(Log, RV);
3484   }
3485 };
3486 } // end anonymous namespace
3487
3488 // OptAndOp - This handles expressions of the form ((val OP C1) & C2).  Where
3489 // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is
3490 // guaranteed to be a binary operator.
3491 Instruction *InstCombiner::OptAndOp(Instruction *Op,
3492                                     ConstantInt *OpRHS,
3493                                     ConstantInt *AndRHS,
3494                                     BinaryOperator &TheAnd) {
3495   Value *X = Op->getOperand(0);
3496   Constant *Together = 0;
3497   if (!Op->isShift())
3498     Together = ConstantExpr::getAnd(AndRHS, OpRHS);
3499
3500   switch (Op->getOpcode()) {
3501   case Instruction::Xor:
3502     if (Op->hasOneUse()) {
3503       // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
3504       Value *And = Builder->CreateAnd(X, AndRHS);
3505       And->takeName(Op);
3506       return BinaryOperator::CreateXor(And, Together);
3507     }
3508     break;
3509   case Instruction::Or:
3510     if (Together == AndRHS) // (X | C) & C --> C
3511       return ReplaceInstUsesWith(TheAnd, AndRHS);
3512
3513     if (Op->hasOneUse() && Together != OpRHS) {
3514       // (X | C1) & C2 --> (X | (C1&C2)) & C2
3515       Value *Or = Builder->CreateOr(X, Together);
3516       Or->takeName(Op);
3517       return BinaryOperator::CreateAnd(Or, AndRHS);
3518     }
3519     break;
3520   case Instruction::Add:
3521     if (Op->hasOneUse()) {
3522       // Adding a one to a single bit bit-field should be turned into an XOR
3523       // of the bit.  First thing to check is to see if this AND is with a
3524       // single bit constant.
3525       const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
3526
3527       // If there is only one bit set...
3528       if (isOneBitSet(cast<ConstantInt>(AndRHS))) {
3529         // Ok, at this point, we know that we are masking the result of the
3530         // ADD down to exactly one bit.  If the constant we are adding has
3531         // no bits set below this bit, then we can eliminate the ADD.
3532         const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
3533
3534         // Check to see if any bits below the one bit set in AndRHSV are set.
3535         if ((AddRHS & (AndRHSV-1)) == 0) {
3536           // If not, the only thing that can effect the output of the AND is
3537           // the bit specified by AndRHSV.  If that bit is set, the effect of
3538           // the XOR is to toggle the bit.  If it is clear, then the ADD has
3539           // no effect.
3540           if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
3541             TheAnd.setOperand(0, X);
3542             return &TheAnd;
3543           } else {
3544             // Pull the XOR out of the AND.
3545             Value *NewAnd = Builder->CreateAnd(X, AndRHS);
3546             NewAnd->takeName(Op);
3547             return BinaryOperator::CreateXor(NewAnd, AndRHS);
3548           }
3549         }
3550       }
3551     }
3552     break;
3553
3554   case Instruction::Shl: {
3555     // We know that the AND will not produce any of the bits shifted in, so if
3556     // the anded constant includes them, clear them now!
3557     //
3558     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
3559     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
3560     APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
3561     ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShlMask);
3562
3563     if (CI->getValue() == ShlMask) { 
3564     // Masking out bits that the shift already masks
3565       return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and.
3566     } else if (CI != AndRHS) {                  // Reducing bits set in and.
3567       TheAnd.setOperand(1, CI);
3568       return &TheAnd;
3569     }
3570     break;
3571   }
3572   case Instruction::LShr:
3573   {
3574     // We know that the AND will not produce any of the bits shifted in, so if
3575     // the anded constant includes them, clear them now!  This only applies to
3576     // unsigned shifts, because a signed shr may bring in set bits!
3577     //
3578     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
3579     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
3580     APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
3581     ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
3582
3583     if (CI->getValue() == ShrMask) {   
3584     // Masking out bits that the shift already masks.
3585       return ReplaceInstUsesWith(TheAnd, Op);
3586     } else if (CI != AndRHS) {
3587       TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
3588       return &TheAnd;
3589     }
3590     break;
3591   }
3592   case Instruction::AShr:
3593     // Signed shr.
3594     // See if this is shifting in some sign extension, then masking it out
3595     // with an and.
3596     if (Op->hasOneUse()) {
3597       uint32_t BitWidth = AndRHS->getType()->getBitWidth();
3598       uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
3599       APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
3600       Constant *C = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
3601       if (C == AndRHS) {          // Masking out bits shifted in.
3602         // (Val ashr C1) & C2 -> (Val lshr C1) & C2
3603         // Make the argument unsigned.
3604         Value *ShVal = Op->getOperand(0);
3605         ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
3606         return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
3607       }
3608     }
3609     break;
3610   }
3611   return 0;
3612 }
3613
3614
3615 /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is
3616 /// true, otherwise (V < Lo || V >= Hi).  In pratice, we emit the more efficient
3617 /// (V-Lo) <u Hi-Lo.  This method expects that Lo <= Hi. isSigned indicates
3618 /// whether to treat the V, Lo and HI as signed or not. IB is the location to
3619 /// insert new instructions.
3620 Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
3621                                            bool isSigned, bool Inside, 
3622                                            Instruction &IB) {
3623   assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? 
3624             ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
3625          "Lo is not <= Hi in range emission code!");
3626     
3627   if (Inside) {
3628     if (Lo == Hi)  // Trivially false.
3629       return new ICmpInst(ICmpInst::ICMP_NE, V, V);
3630
3631     // V >= Min && V < Hi --> V < Hi
3632     if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
3633       ICmpInst::Predicate pred = (isSigned ? 
3634         ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
3635       return new ICmpInst(pred, V, Hi);
3636     }
3637
3638     // Emit V-Lo <u Hi-Lo
3639     Constant *NegLo = ConstantExpr::getNeg(Lo);
3640     Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
3641     Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
3642     return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
3643   }
3644
3645   if (Lo == Hi)  // Trivially true.
3646     return new ICmpInst(ICmpInst::ICMP_EQ, V, V);
3647
3648   // V < Min || V >= Hi -> V > Hi-1
3649   Hi = SubOne(cast<ConstantInt>(Hi));
3650   if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
3651     ICmpInst::Predicate pred = (isSigned ? 
3652         ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
3653     return new ICmpInst(pred, V, Hi);
3654   }
3655
3656   // Emit V-Lo >u Hi-1-Lo
3657   // Note that Hi has already had one subtracted from it, above.
3658   ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
3659   Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
3660   Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
3661   return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
3662 }
3663
3664 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
3665 // any number of 0s on either side.  The 1s are allowed to wrap from LSB to
3666 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
3667 // not, since all 1s are not contiguous.
3668 static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
3669   const APInt& V = Val->getValue();
3670   uint32_t BitWidth = Val->getType()->getBitWidth();
3671   if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
3672
3673   // look for the first zero bit after the run of ones
3674   MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
3675   // look for the first non-zero bit
3676   ME = V.getActiveBits(); 
3677   return true;
3678 }
3679
3680 /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
3681 /// where isSub determines whether the operator is a sub.  If we can fold one of
3682 /// the following xforms:
3683 /// 
3684 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
3685 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
3686 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
3687 ///
3688 /// return (A +/- B).
3689 ///
3690 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
3691                                         ConstantInt *Mask, bool isSub,
3692                                         Instruction &I) {
3693   Instruction *LHSI = dyn_cast<Instruction>(LHS);
3694   if (!LHSI || LHSI->getNumOperands() != 2 ||
3695       !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
3696
3697   ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
3698
3699   switch (LHSI->getOpcode()) {
3700   default: return 0;
3701   case Instruction::And:
3702     if (ConstantExpr::getAnd(N, Mask) == Mask) {
3703       // If the AndRHS is a power of two minus one (0+1+), this is simple.
3704       if ((Mask->getValue().countLeadingZeros() + 
3705            Mask->getValue().countPopulation()) == 
3706           Mask->getValue().getBitWidth())
3707         break;
3708
3709       // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
3710       // part, we don't need any explicit masks to take them out of A.  If that
3711       // is all N is, ignore it.
3712       uint32_t MB = 0, ME = 0;
3713       if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
3714         uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
3715         APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
3716         if (MaskedValueIsZero(RHS, Mask))
3717           break;
3718       }
3719     }
3720     return 0;
3721   case Instruction::Or:
3722   case Instruction::Xor:
3723     // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
3724     if ((Mask->getValue().countLeadingZeros() + 
3725          Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
3726         && ConstantExpr::getAnd(N, Mask)->isNullValue())
3727       break;
3728     return 0;
3729   }
3730   
3731   if (isSub)
3732     return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
3733   return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
3734 }
3735
3736 /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
3737 Instruction *InstCombiner::FoldAndOfICmps(Instruction &I,
3738                                           ICmpInst *LHS, ICmpInst *RHS) {
3739   Value *Val, *Val2;
3740   ConstantInt *LHSCst, *RHSCst;
3741   ICmpInst::Predicate LHSCC, RHSCC;
3742   
3743   // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
3744   if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
3745                          m_ConstantInt(LHSCst))) ||
3746       !match(RHS, m_ICmp(RHSCC, m_Value(Val2),
3747                          m_ConstantInt(RHSCst))))
3748     return 0;
3749   
3750   // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
3751   // where C is a power of 2
3752   if (LHSCst == RHSCst && LHSCC == RHSCC && LHSCC == ICmpInst::ICMP_ULT &&
3753       LHSCst->getValue().isPowerOf2()) {
3754     Value *NewOr = Builder->CreateOr(Val, Val2);
3755     return new ICmpInst(LHSCC, NewOr, LHSCst);
3756   }
3757   
3758   // From here on, we only handle:
3759   //    (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
3760   if (Val != Val2) return 0;
3761   
3762   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
3763   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
3764       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
3765       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
3766       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
3767     return 0;
3768   
3769   // We can't fold (ugt x, C) & (sgt x, C2).
3770   if (!PredicatesFoldable(LHSCC, RHSCC))
3771     return 0;
3772     
3773   // Ensure that the larger constant is on the RHS.
3774   bool ShouldSwap;
3775   if (ICmpInst::isSignedPredicate(LHSCC) ||
3776       (ICmpInst::isEquality(LHSCC) && 
3777        ICmpInst::isSignedPredicate(RHSCC)))
3778     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
3779   else
3780     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
3781     
3782   if (ShouldSwap) {
3783     std::swap(LHS, RHS);
3784     std::swap(LHSCst, RHSCst);
3785     std::swap(LHSCC, RHSCC);
3786   }
3787
3788   // At this point, we know we have have two icmp instructions
3789   // comparing a value against two constants and and'ing the result
3790   // together.  Because of the above check, we know that we only have
3791   // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know 
3792   // (from the FoldICmpLogical check above), that the two constants 
3793   // are not equal and that the larger constant is on the RHS
3794   assert(LHSCst != RHSCst && "Compares not folded above?");
3795
3796   switch (LHSCC) {
3797   default: llvm_unreachable("Unknown integer condition code!");
3798   case ICmpInst::ICMP_EQ:
3799     switch (RHSCC) {
3800     default: llvm_unreachable("Unknown integer condition code!");
3801     case ICmpInst::ICMP_EQ:         // (X == 13 & X == 15) -> false
3802     case ICmpInst::ICMP_UGT:        // (X == 13 & X >  15) -> false
3803     case ICmpInst::ICMP_SGT:        // (X == 13 & X >  15) -> false
3804       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
3805     case ICmpInst::ICMP_NE:         // (X == 13 & X != 15) -> X == 13
3806     case ICmpInst::ICMP_ULT:        // (X == 13 & X <  15) -> X == 13
3807     case ICmpInst::ICMP_SLT:        // (X == 13 & X <  15) -> X == 13
3808       return ReplaceInstUsesWith(I, LHS);
3809     }
3810   case ICmpInst::ICMP_NE:
3811     switch (RHSCC) {
3812     default: llvm_unreachable("Unknown integer condition code!");
3813     case ICmpInst::ICMP_ULT:
3814       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
3815         return new ICmpInst(ICmpInst::ICMP_ULT, Val, LHSCst);
3816       break;                        // (X != 13 & X u< 15) -> no change
3817     case ICmpInst::ICMP_SLT:
3818       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
3819         return new ICmpInst(ICmpInst::ICMP_SLT, Val, LHSCst);
3820       break;                        // (X != 13 & X s< 15) -> no change
3821     case ICmpInst::ICMP_EQ:         // (X != 13 & X == 15) -> X == 15
3822     case ICmpInst::ICMP_UGT:        // (X != 13 & X u> 15) -> X u> 15
3823     case ICmpInst::ICMP_SGT:        // (X != 13 & X s> 15) -> X s> 15
3824       return ReplaceInstUsesWith(I, RHS);
3825     case ICmpInst::ICMP_NE:
3826       if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
3827         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
3828         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
3829         return new ICmpInst(ICmpInst::ICMP_UGT, Add,
3830                             ConstantInt::get(Add->getType(), 1));
3831       }
3832       break;                        // (X != 13 & X != 15) -> no change
3833     }
3834     break;
3835   case ICmpInst::ICMP_ULT:
3836     switch (RHSCC) {
3837     default: llvm_unreachable("Unknown integer condition code!");
3838     case ICmpInst::ICMP_EQ:         // (X u< 13 & X == 15) -> false
3839     case ICmpInst::ICMP_UGT:        // (X u< 13 & X u> 15) -> false
3840       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
3841     case ICmpInst::ICMP_SGT:        // (X u< 13 & X s> 15) -> no change
3842       break;
3843     case ICmpInst::ICMP_NE:         // (X u< 13 & X != 15) -> X u< 13
3844     case ICmpInst::ICMP_ULT:        // (X u< 13 & X u< 15) -> X u< 13
3845       return ReplaceInstUsesWith(I, LHS);
3846     case ICmpInst::ICMP_SLT:        // (X u< 13 & X s< 15) -> no change
3847       break;
3848     }
3849     break;
3850   case ICmpInst::ICMP_SLT:
3851     switch (RHSCC) {
3852     default: llvm_unreachable("Unknown integer condition code!");
3853     case ICmpInst::ICMP_EQ:         // (X s< 13 & X == 15) -> false
3854     case ICmpInst::ICMP_SGT:        // (X s< 13 & X s> 15) -> false
3855       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
3856     case ICmpInst::ICMP_UGT:        // (X s< 13 & X u> 15) -> no change
3857       break;
3858     case ICmpInst::ICMP_NE:         // (X s< 13 & X != 15) -> X < 13
3859     case ICmpInst::ICMP_SLT:        // (X s< 13 & X s< 15) -> X < 13
3860       return ReplaceInstUsesWith(I, LHS);
3861     case ICmpInst::ICMP_ULT:        // (X s< 13 & X u< 15) -> no change
3862       break;
3863     }
3864     break;
3865   case ICmpInst::ICMP_UGT:
3866     switch (RHSCC) {
3867     default: llvm_unreachable("Unknown integer condition code!");
3868     case ICmpInst::ICMP_EQ:         // (X u> 13 & X == 15) -> X == 15
3869     case ICmpInst::ICMP_UGT:        // (X u> 13 & X u> 15) -> X u> 15
3870       return ReplaceInstUsesWith(I, RHS);
3871     case ICmpInst::ICMP_SGT:        // (X u> 13 & X s> 15) -> no change
3872       break;
3873     case ICmpInst::ICMP_NE:
3874       if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
3875         return new ICmpInst(LHSCC, Val, RHSCst);
3876       break;                        // (X u> 13 & X != 15) -> no change
3877     case ICmpInst::ICMP_ULT:        // (X u> 13 & X u< 15) -> (X-14) <u 1
3878       return InsertRangeTest(Val, AddOne(LHSCst),
3879                              RHSCst, false, true, I);
3880     case ICmpInst::ICMP_SLT:        // (X u> 13 & X s< 15) -> no change
3881       break;
3882     }
3883     break;
3884   case ICmpInst::ICMP_SGT:
3885     switch (RHSCC) {
3886     default: llvm_unreachable("Unknown integer condition code!");
3887     case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X == 15
3888     case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15
3889       return ReplaceInstUsesWith(I, RHS);
3890     case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change
3891       break;
3892     case ICmpInst::ICMP_NE:
3893       if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
3894         return new ICmpInst(LHSCC, Val, RHSCst);
3895       break;                        // (X s> 13 & X != 15) -> no change
3896     case ICmpInst::ICMP_SLT:        // (X s> 13 & X s< 15) -> (X-14) s< 1
3897       return InsertRangeTest(Val, AddOne(LHSCst),
3898                              RHSCst, true, true, I);
3899     case ICmpInst::ICMP_ULT:        // (X s> 13 & X u< 15) -> no change
3900       break;
3901     }
3902     break;
3903   }
3904  
3905   return 0;
3906 }
3907
3908 Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS,
3909                                           FCmpInst *RHS) {
3910   
3911   if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
3912       RHS->getPredicate() == FCmpInst::FCMP_ORD) {
3913     // (fcmp ord x, c) & (fcmp ord y, c)  -> (fcmp ord x, y)
3914     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
3915       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
3916         // If either of the constants are nans, then the whole thing returns
3917         // false.
3918         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
3919           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
3920         return new FCmpInst(FCmpInst::FCMP_ORD,
3921                             LHS->getOperand(0), RHS->getOperand(0));
3922       }
3923     
3924     // Handle vector zeros.  This occurs because the canonical form of
3925     // "fcmp ord x,x" is "fcmp ord x, 0".
3926     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
3927         isa<ConstantAggregateZero>(RHS->getOperand(1)))
3928       return new FCmpInst(FCmpInst::FCMP_ORD,
3929                           LHS->getOperand(0), RHS->getOperand(0));
3930     return 0;
3931   }
3932   
3933   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
3934   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
3935   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
3936   
3937   
3938   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
3939     // Swap RHS operands to match LHS.
3940     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
3941     std::swap(Op1LHS, Op1RHS);
3942   }
3943   
3944   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
3945     // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
3946     if (Op0CC == Op1CC)
3947       return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
3948     
3949     if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
3950       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
3951     if (Op0CC == FCmpInst::FCMP_TRUE)
3952       return ReplaceInstUsesWith(I, RHS);
3953     if (Op1CC == FCmpInst::FCMP_TRUE)
3954       return ReplaceInstUsesWith(I, LHS);
3955     
3956     bool Op0Ordered;
3957     bool Op1Ordered;
3958     unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
3959     unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
3960     if (Op1Pred == 0) {
3961       std::swap(LHS, RHS);
3962       std::swap(Op0Pred, Op1Pred);
3963       std::swap(Op0Ordered, Op1Ordered);
3964     }
3965     if (Op0Pred == 0) {
3966       // uno && ueq -> uno && (uno || eq) -> ueq
3967       // ord && olt -> ord && (ord && lt) -> olt
3968       if (Op0Ordered == Op1Ordered)
3969         return ReplaceInstUsesWith(I, RHS);
3970       
3971       // uno && oeq -> uno && (ord && eq) -> false
3972       // uno && ord -> false
3973       if (!Op0Ordered)
3974         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
3975       // ord && ueq -> ord && (uno || eq) -> oeq
3976       return cast<Instruction>(getFCmpValue(true, Op1Pred,
3977                                             Op0LHS, Op0RHS, Context));
3978     }
3979   }
3980
3981   return 0;
3982 }
3983
3984
3985 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
3986   bool Changed = SimplifyCommutative(I);
3987   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3988
3989   if (isa<UndefValue>(Op1))                         // X & undef -> 0
3990     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
3991
3992   // and X, X = X
3993   if (Op0 == Op1)
3994     return ReplaceInstUsesWith(I, Op1);
3995
3996   // See if we can simplify any instructions used by the instruction whose sole 
3997   // purpose is to compute bits we don't care about.
3998   if (SimplifyDemandedInstructionBits(I))
3999     return &I;
4000   if (isa<VectorType>(I.getType())) {
4001     if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
4002       if (CP->isAllOnesValue())            // X & <-1,-1> -> X
4003         return ReplaceInstUsesWith(I, I.getOperand(0));
4004     } else if (isa<ConstantAggregateZero>(Op1)) {
4005       return ReplaceInstUsesWith(I, Op1);  // X & <0,0> -> <0,0>
4006     }
4007   }
4008
4009   if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
4010     const APInt& AndRHSMask = AndRHS->getValue();
4011     APInt NotAndRHS(~AndRHSMask);
4012
4013     // Optimize a variety of ((val OP C1) & C2) combinations...
4014     if (isa<BinaryOperator>(Op0)) {
4015       Instruction *Op0I = cast<Instruction>(Op0);
4016       Value *Op0LHS = Op0I->getOperand(0);
4017       Value *Op0RHS = Op0I->getOperand(1);
4018       switch (Op0I->getOpcode()) {
4019       case Instruction::Xor:
4020       case Instruction::Or:
4021         // If the mask is only needed on one incoming arm, push it up.
4022         if (Op0I->hasOneUse()) {
4023           if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
4024             // Not masking anything out for the LHS, move to RHS.
4025             Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
4026                                                Op0RHS->getName()+".masked");
4027             return BinaryOperator::Create(
4028                        cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS);
4029           }
4030           if (!isa<Constant>(Op0RHS) &&
4031               MaskedValueIsZero(Op0RHS, NotAndRHS)) {
4032             // Not masking anything out for the RHS, move to LHS.
4033             Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
4034                                                Op0LHS->getName()+".masked");
4035             return BinaryOperator::Create(
4036                        cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS);
4037           }
4038         }
4039
4040         break;
4041       case Instruction::Add:
4042         // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
4043         // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
4044         // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
4045         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
4046           return BinaryOperator::CreateAnd(V, AndRHS);
4047         if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
4048           return BinaryOperator::CreateAnd(V, AndRHS);  // Add commutes
4049         break;
4050
4051       case Instruction::Sub:
4052         // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
4053         // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
4054         // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
4055         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
4056           return BinaryOperator::CreateAnd(V, AndRHS);
4057
4058         // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
4059         // has 1's for all bits that the subtraction with A might affect.
4060         if (Op0I->hasOneUse()) {
4061           uint32_t BitWidth = AndRHSMask.getBitWidth();
4062           uint32_t Zeros = AndRHSMask.countLeadingZeros();
4063           APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
4064
4065           ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
4066           if (!(A && A->isZero()) &&               // avoid infinite recursion.
4067               MaskedValueIsZero(Op0LHS, Mask)) {
4068             Value *NewNeg = Builder->CreateNeg(Op0RHS);
4069             return BinaryOperator::CreateAnd(NewNeg, AndRHS);
4070           }
4071         }
4072         break;
4073
4074       case Instruction::Shl:
4075       case Instruction::LShr:
4076         // (1 << x) & 1 --> zext(x == 0)
4077         // (1 >> x) & 1 --> zext(x == 0)
4078         if (AndRHSMask == 1 && Op0LHS == AndRHS) {
4079           Value *NewICmp =
4080             Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
4081           return new ZExtInst(NewICmp, I.getType());
4082         }
4083         break;
4084       }
4085
4086       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
4087         if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
4088           return Res;
4089     } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
4090       // If this is an integer truncation or change from signed-to-unsigned, and
4091       // if the source is an and/or with immediate, transform it.  This
4092       // frequently occurs for bitfield accesses.
4093       if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
4094         if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) &&
4095             CastOp->getNumOperands() == 2)
4096           if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) {
4097             if (CastOp->getOpcode() == Instruction::And) {
4098               // Change: and (cast (and X, C1) to T), C2
4099               // into  : and (cast X to T), trunc_or_bitcast(C1)&C2
4100               // This will fold the two constants together, which may allow 
4101               // other simplifications.
4102               Value *NewCast = Builder->CreateTruncOrBitCast(
4103                 CastOp->getOperand(0), I.getType(), 
4104                 CastOp->getName()+".shrunk");
4105               // trunc_or_bitcast(C1)&C2
4106               Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
4107               C3 = ConstantExpr::getAnd(C3, AndRHS);
4108               return BinaryOperator::CreateAnd(NewCast, C3);
4109             } else if (CastOp->getOpcode() == Instruction::Or) {
4110               // Change: and (cast (or X, C1) to T), C2
4111               // into  : trunc(C1)&C2 iff trunc(C1)&C2 == C2
4112               Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
4113               if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS)
4114                 // trunc(C1)&C2
4115                 return ReplaceInstUsesWith(I, AndRHS);
4116             }
4117           }
4118       }
4119     }
4120
4121     // Try to fold constant and into select arguments.
4122     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
4123       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
4124         return R;
4125     if (isa<PHINode>(Op0))
4126       if (Instruction *NV = FoldOpIntoPhi(I))
4127         return NV;
4128   }
4129
4130   Value *Op0NotVal = dyn_castNotVal(Op0);
4131   Value *Op1NotVal = dyn_castNotVal(Op1);
4132
4133   if (Op0NotVal == Op1 || Op1NotVal == Op0)  // A & ~A  == ~A & A == 0
4134     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
4135
4136   // (~A & ~B) == (~(A | B)) - De Morgan's Law
4137   if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
4138     Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
4139                                   I.getName()+".demorgan");
4140     return BinaryOperator::CreateNot(Or);
4141   }
4142   
4143   {
4144     Value *A = 0, *B = 0, *C = 0, *D = 0;
4145     if (match(Op0, m_Or(m_Value(A), m_Value(B)))) {
4146       if (A == Op1 || B == Op1)    // (A | ?) & A  --> A
4147         return ReplaceInstUsesWith(I, Op1);
4148     
4149       // (A|B) & ~(A&B) -> A^B
4150       if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) {
4151         if ((A == C && B == D) || (A == D && B == C))
4152           return BinaryOperator::CreateXor(A, B);
4153       }
4154     }
4155     
4156     if (match(Op1, m_Or(m_Value(A), m_Value(B)))) {
4157       if (A == Op0 || B == Op0)    // A & (A | ?)  --> A
4158         return ReplaceInstUsesWith(I, Op0);
4159
4160       // ~(A&B) & (A|B) -> A^B
4161       if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) {
4162         if ((A == C && B == D) || (A == D && B == C))
4163           return BinaryOperator::CreateXor(A, B);
4164       }
4165     }
4166     
4167     if (Op0->hasOneUse() &&
4168         match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
4169       if (A == Op1) {                                // (A^B)&A -> A&(A^B)
4170         I.swapOperands();     // Simplify below
4171         std::swap(Op0, Op1);
4172       } else if (B == Op1) {                         // (A^B)&B -> B&(B^A)
4173         cast<BinaryOperator>(Op0)->swapOperands();
4174         I.swapOperands();     // Simplify below
4175         std::swap(Op0, Op1);
4176       }
4177     }
4178
4179     if (Op1->hasOneUse() &&
4180         match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
4181       if (B == Op0) {                                // B&(A^B) -> B&(B^A)
4182         cast<BinaryOperator>(Op1)->swapOperands();
4183         std::swap(A, B);
4184       }
4185       if (A == Op0)                                // A&(A^B) -> A & ~B
4186         return BinaryOperator::CreateAnd(A, Builder->CreateNot(B, "tmp"));
4187     }
4188
4189     // (A&((~A)|B)) -> A&B
4190     if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
4191         match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
4192       return BinaryOperator::CreateAnd(A, Op1);
4193     if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
4194         match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
4195       return BinaryOperator::CreateAnd(A, Op0);
4196   }
4197   
4198   if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
4199     // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
4200     if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
4201       return R;
4202
4203     if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
4204       if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS))
4205         return Res;
4206   }
4207
4208   // fold (and (cast A), (cast B)) -> (cast (and A, B))
4209   if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
4210     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
4211       if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ?
4212         const Type *SrcTy = Op0C->getOperand(0)->getType();
4213         if (SrcTy == Op1C->getOperand(0)->getType() &&
4214             SrcTy->isIntOrIntVector() &&
4215             // Only do this if the casts both really cause code to be generated.
4216             ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), 
4217                               I.getType(), TD) &&
4218             ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), 
4219                               I.getType(), TD)) {
4220           Value *NewOp = Builder->CreateAnd(Op0C->getOperand(0),
4221                                             Op1C->getOperand(0), I.getName());
4222           return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
4223         }
4224       }
4225     
4226   // (X >> Z) & (Y >> Z)  -> (X&Y) >> Z  for all shifts.
4227   if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
4228     if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
4229       if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && 
4230           SI0->getOperand(1) == SI1->getOperand(1) &&
4231           (SI0->hasOneUse() || SI1->hasOneUse())) {
4232         Value *NewOp =
4233           Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
4234                              SI0->getName());
4235         return BinaryOperator::Create(SI1->getOpcode(), NewOp, 
4236                                       SI1->getOperand(1));
4237       }
4238   }
4239
4240   // If and'ing two fcmp, try combine them into one.
4241   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
4242     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
4243       if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS))
4244         return Res;
4245   }
4246
4247   return Changed ? &I : 0;
4248 }
4249
4250 /// CollectBSwapParts - Analyze the specified subexpression and see if it is
4251 /// capable of providing pieces of a bswap.  The subexpression provides pieces
4252 /// of a bswap if it is proven that each of the non-zero bytes in the output of
4253 /// the expression came from the corresponding "byte swapped" byte in some other
4254 /// value.  For example, if the current subexpression is "(shl i32 %X, 24)" then
4255 /// we know that the expression deposits the low byte of %X into the high byte
4256 /// of the bswap result and that all other bytes are zero.  This expression is
4257 /// accepted, the high byte of ByteValues is set to X to indicate a correct
4258 /// match.
4259 ///
4260 /// This function returns true if the match was unsuccessful and false if so.
4261 /// On entry to the function the "OverallLeftShift" is a signed integer value
4262 /// indicating the number of bytes that the subexpression is later shifted.  For
4263 /// example, if the expression is later right shifted by 16 bits, the
4264 /// OverallLeftShift value would be -2 on entry.  This is used to specify which
4265 /// byte of ByteValues is actually being set.
4266 ///
4267 /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
4268 /// byte is masked to zero by a user.  For example, in (X & 255), X will be
4269 /// processed with a bytemask of 1.  Because bytemask is 32-bits, this limits
4270 /// this function to working on up to 32-byte (256 bit) values.  ByteMask is
4271 /// always in the local (OverallLeftShift) coordinate space.
4272 ///
4273 static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
4274                               SmallVector<Value*, 8> &ByteValues) {
4275   if (Instruction *I = dyn_cast<Instruction>(V)) {
4276     // If this is an or instruction, it may be an inner node of the bswap.
4277     if (I->getOpcode() == Instruction::Or) {
4278       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
4279                                ByteValues) ||
4280              CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
4281                                ByteValues);
4282     }
4283   
4284     // If this is a logical shift by a constant multiple of 8, recurse with
4285     // OverallLeftShift and ByteMask adjusted.
4286     if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
4287       unsigned ShAmt = 
4288         cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
4289       // Ensure the shift amount is defined and of a byte value.
4290       if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
4291         return true;
4292
4293       unsigned ByteShift = ShAmt >> 3;
4294       if (I->getOpcode() == Instruction::Shl) {
4295         // X << 2 -> collect(X, +2)
4296         OverallLeftShift += ByteShift;
4297         ByteMask >>= ByteShift;
4298       } else {
4299         // X >>u 2 -> collect(X, -2)
4300         OverallLeftShift -= ByteShift;
4301         ByteMask <<= ByteShift;
4302         ByteMask &= (~0U >> (32-ByteValues.size()));
4303       }
4304
4305       if (OverallLeftShift >= (int)ByteValues.size()) return true;
4306       if (OverallLeftShift <= -(int)ByteValues.size()) return true;
4307
4308       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, 
4309                                ByteValues);
4310     }
4311
4312     // If this is a logical 'and' with a mask that clears bytes, clear the
4313     // corresponding bytes in ByteMask.
4314     if (I->getOpcode() == Instruction::And &&
4315         isa<ConstantInt>(I->getOperand(1))) {
4316       // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
4317       unsigned NumBytes = ByteValues.size();
4318       APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
4319       const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
4320       
4321       for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
4322         // If this byte is masked out by a later operation, we don't care what
4323         // the and mask is.
4324         if ((ByteMask & (1 << i)) == 0)
4325           continue;
4326         
4327         // If the AndMask is all zeros for this byte, clear the bit.
4328         APInt MaskB = AndMask & Byte;
4329         if (MaskB == 0) {
4330           ByteMask &= ~(1U << i);
4331           continue;
4332         }
4333         
4334         // If the AndMask is not all ones for this byte, it's not a bytezap.
4335         if (MaskB != Byte)
4336           return true;
4337
4338         // Otherwise, this byte is kept.
4339       }
4340
4341       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, 
4342                                ByteValues);
4343     }
4344   }
4345   
4346   // Okay, we got to something that isn't a shift, 'or' or 'and'.  This must be
4347   // the input value to the bswap.  Some observations: 1) if more than one byte
4348   // is demanded from this input, then it could not be successfully assembled
4349   // into a byteswap.  At least one of the two bytes would not be aligned with
4350   // their ultimate destination.
4351   if (!isPowerOf2_32(ByteMask)) return true;
4352   unsigned InputByteNo = CountTrailingZeros_32(ByteMask);
4353   
4354   // 2) The input and ultimate destinations must line up: if byte 3 of an i32
4355   // is demanded, it needs to go into byte 0 of the result.  This means that the
4356   // byte needs to be shifted until it lands in the right byte bucket.  The
4357   // shift amount depends on the position: if the byte is coming from the high
4358   // part of the value (e.g. byte 3) then it must be shifted right.  If from the
4359   // low part, it must be shifted left.
4360   unsigned DestByteNo = InputByteNo + OverallLeftShift;
4361   if (InputByteNo < ByteValues.size()/2) {
4362     if (ByteValues.size()-1-DestByteNo != InputByteNo)
4363       return true;
4364   } else {
4365     if (ByteValues.size()-1-DestByteNo != InputByteNo)
4366       return true;
4367   }
4368   
4369   // If the destination byte value is already defined, the values are or'd
4370   // together, which isn't a bswap (unless it's an or of the same bits).
4371   if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
4372     return true;
4373   ByteValues[DestByteNo] = V;
4374   return false;
4375 }
4376
4377 /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
4378 /// If so, insert the new bswap intrinsic and return it.
4379 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
4380   const IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
4381   if (!ITy || ITy->getBitWidth() % 16 || 
4382       // ByteMask only allows up to 32-byte values.
4383       ITy->getBitWidth() > 32*8) 
4384     return 0;   // Can only bswap pairs of bytes.  Can't do vectors.
4385   
4386   /// ByteValues - For each byte of the result, we keep track of which value
4387   /// defines each byte.
4388   SmallVector<Value*, 8> ByteValues;
4389   ByteValues.resize(ITy->getBitWidth()/8);
4390     
4391   // Try to find all the pieces corresponding to the bswap.
4392   uint32_t ByteMask = ~0U >> (32-ByteValues.size());
4393   if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
4394     return 0;
4395   
4396   // Check to see if all of the bytes come from the same value.
4397   Value *V = ByteValues[0];
4398   if (V == 0) return 0;  // Didn't find a byte?  Must be zero.
4399   
4400   // Check to make sure that all of the bytes come from the same value.
4401   for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
4402     if (ByteValues[i] != V)
4403       return 0;
4404   const Type *Tys[] = { ITy };
4405   Module *M = I.getParent()->getParent()->getParent();
4406   Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
4407   return CallInst::Create(F, V);
4408 }
4409
4410 /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D).  Check
4411 /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
4412 /// we can simplify this expression to "cond ? C : D or B".
4413 static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
4414                                          Value *C, Value *D,
4415                                          LLVMContext *Context) {
4416   // If A is not a select of -1/0, this cannot match.
4417   Value *Cond = 0;
4418   if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond))))
4419     return 0;
4420
4421   // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
4422   if (match(D, m_SelectCst<0, -1>(m_Specific(Cond))))
4423     return SelectInst::Create(Cond, C, B);
4424   if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
4425     return SelectInst::Create(Cond, C, B);
4426   // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
4427   if (match(B, m_SelectCst<0, -1>(m_Specific(Cond))))
4428     return SelectInst::Create(Cond, C, D);
4429   if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
4430     return SelectInst::Create(Cond, C, D);
4431   return 0;
4432 }
4433
4434 /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
4435 Instruction *InstCombiner::FoldOrOfICmps(Instruction &I,
4436                                          ICmpInst *LHS, ICmpInst *RHS) {
4437   Value *Val, *Val2;
4438   ConstantInt *LHSCst, *RHSCst;
4439   ICmpInst::Predicate LHSCC, RHSCC;
4440   
4441   // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
4442   if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
4443              m_ConstantInt(LHSCst))) ||
4444       !match(RHS, m_ICmp(RHSCC, m_Value(Val2),
4445              m_ConstantInt(RHSCst))))
4446     return 0;
4447   
4448   // From here on, we only handle:
4449   //    (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
4450   if (Val != Val2) return 0;
4451   
4452   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
4453   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
4454       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
4455       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
4456       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
4457     return 0;
4458   
4459   // We can't fold (ugt x, C) | (sgt x, C2).
4460   if (!PredicatesFoldable(LHSCC, RHSCC))
4461     return 0;
4462   
4463   // Ensure that the larger constant is on the RHS.
4464   bool ShouldSwap;
4465   if (ICmpInst::isSignedPredicate(LHSCC) ||
4466       (ICmpInst::isEquality(LHSCC) && 
4467        ICmpInst::isSignedPredicate(RHSCC)))
4468     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
4469   else
4470     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
4471   
4472   if (ShouldSwap) {
4473     std::swap(LHS, RHS);
4474     std::swap(LHSCst, RHSCst);
4475     std::swap(LHSCC, RHSCC);
4476   }
4477   
4478   // At this point, we know we have have two icmp instructions
4479   // comparing a value against two constants and or'ing the result
4480   // together.  Because of the above check, we know that we only have
4481   // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
4482   // FoldICmpLogical check above), that the two constants are not
4483   // equal.
4484   assert(LHSCst != RHSCst && "Compares not folded above?");
4485
4486   switch (LHSCC) {
4487   default: llvm_unreachable("Unknown integer condition code!");
4488   case ICmpInst::ICMP_EQ:
4489     switch (RHSCC) {
4490     default: llvm_unreachable("Unknown integer condition code!");
4491     case ICmpInst::ICMP_EQ:
4492       if (LHSCst == SubOne(RHSCst)) {
4493         // (X == 13 | X == 14) -> X-13 <u 2
4494         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
4495         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
4496         AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
4497         return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST);
4498       }
4499       break;                         // (X == 13 | X == 15) -> no change
4500     case ICmpInst::ICMP_UGT:         // (X == 13 | X u> 14) -> no change
4501     case ICmpInst::ICMP_SGT:         // (X == 13 | X s> 14) -> no change
4502       break;
4503     case ICmpInst::ICMP_NE:          // (X == 13 | X != 15) -> X != 15
4504     case ICmpInst::ICMP_ULT:         // (X == 13 | X u< 15) -> X u< 15
4505     case ICmpInst::ICMP_SLT:         // (X == 13 | X s< 15) -> X s< 15
4506       return ReplaceInstUsesWith(I, RHS);
4507     }
4508     break;
4509   case ICmpInst::ICMP_NE:
4510     switch (RHSCC) {
4511     default: llvm_unreachable("Unknown integer condition code!");
4512     case ICmpInst::ICMP_EQ:          // (X != 13 | X == 15) -> X != 13
4513     case ICmpInst::ICMP_UGT:         // (X != 13 | X u> 15) -> X != 13
4514     case ICmpInst::ICMP_SGT:         // (X != 13 | X s> 15) -> X != 13
4515       return ReplaceInstUsesWith(I, LHS);
4516     case ICmpInst::ICMP_NE:          // (X != 13 | X != 15) -> true
4517     case ICmpInst::ICMP_ULT:         // (X != 13 | X u< 15) -> true
4518     case ICmpInst::ICMP_SLT:         // (X != 13 | X s< 15) -> true
4519       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
4520     }
4521     break;
4522   case ICmpInst::ICMP_ULT:
4523     switch (RHSCC) {
4524     default: llvm_unreachable("Unknown integer condition code!");
4525     case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change
4526       break;
4527     case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) -> (X-13) u> 2
4528       // If RHSCst is [us]MAXINT, it is always false.  Not handling
4529       // this can cause overflow.
4530       if (RHSCst->isMaxValue(false))
4531         return ReplaceInstUsesWith(I, LHS);
4532       return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
4533                              false, false, I);
4534     case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change
4535       break;
4536     case ICmpInst::ICMP_NE:         // (X u< 13 | X != 15) -> X != 15
4537     case ICmpInst::ICMP_ULT:        // (X u< 13 | X u< 15) -> X u< 15
4538       return ReplaceInstUsesWith(I, RHS);
4539     case ICmpInst::ICMP_SLT:        // (X u< 13 | X s< 15) -> no change
4540       break;
4541     }
4542     break;
4543   case ICmpInst::ICMP_SLT:
4544     switch (RHSCC) {
4545     default: llvm_unreachable("Unknown integer condition code!");
4546     case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change
4547       break;
4548     case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) -> (X-13) s> 2
4549       // If RHSCst is [us]MAXINT, it is always false.  Not handling
4550       // this can cause overflow.
4551       if (RHSCst->isMaxValue(true))
4552         return ReplaceInstUsesWith(I, LHS);
4553       return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
4554                              true, false, I);
4555     case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change
4556       break;
4557     case ICmpInst::ICMP_NE:         // (X s< 13 | X != 15) -> X != 15
4558     case ICmpInst::ICMP_SLT:        // (X s< 13 | X s< 15) -> X s< 15
4559       return ReplaceInstUsesWith(I, RHS);
4560     case ICmpInst::ICMP_ULT:        // (X s< 13 | X u< 15) -> no change
4561       break;
4562     }
4563     break;
4564   case ICmpInst::ICMP_UGT:
4565     switch (RHSCC) {
4566     default: llvm_unreachable("Unknown integer condition code!");
4567     case ICmpInst::ICMP_EQ:         // (X u> 13 | X == 15) -> X u> 13
4568     case ICmpInst::ICMP_UGT:        // (X u> 13 | X u> 15) -> X u> 13
4569       return ReplaceInstUsesWith(I, LHS);
4570     case ICmpInst::ICMP_SGT:        // (X u> 13 | X s> 15) -> no change
4571       break;
4572     case ICmpInst::ICMP_NE:         // (X u> 13 | X != 15) -> true
4573     case ICmpInst::ICMP_ULT:        // (X u> 13 | X u< 15) -> true
4574       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
4575     case ICmpInst::ICMP_SLT:        // (X u> 13 | X s< 15) -> no change
4576       break;
4577     }
4578     break;
4579   case ICmpInst::ICMP_SGT:
4580     switch (RHSCC) {
4581     default: llvm_unreachable("Unknown integer condition code!");
4582     case ICmpInst::ICMP_EQ:         // (X s> 13 | X == 15) -> X > 13
4583     case ICmpInst::ICMP_SGT:        // (X s> 13 | X s> 15) -> X > 13
4584       return ReplaceInstUsesWith(I, LHS);
4585     case ICmpInst::ICMP_UGT:        // (X s> 13 | X u> 15) -> no change
4586       break;
4587     case ICmpInst::ICMP_NE:         // (X s> 13 | X != 15) -> true
4588     case ICmpInst::ICMP_SLT:        // (X s> 13 | X s< 15) -> true
4589       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
4590     case ICmpInst::ICMP_ULT:        // (X s> 13 | X u< 15) -> no change
4591       break;
4592     }
4593     break;
4594   }
4595   return 0;
4596 }
4597
4598 Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS,
4599                                          FCmpInst *RHS) {
4600   if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
4601       RHS->getPredicate() == FCmpInst::FCMP_UNO && 
4602       LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
4603     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
4604       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
4605         // If either of the constants are nans, then the whole thing returns
4606         // true.
4607         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
4608           return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
4609         
4610         // Otherwise, no need to compare the two constants, compare the
4611         // rest.
4612         return new FCmpInst(FCmpInst::FCMP_UNO,
4613                             LHS->getOperand(0), RHS->getOperand(0));
4614       }
4615     
4616     // Handle vector zeros.  This occurs because the canonical form of
4617     // "fcmp uno x,x" is "fcmp uno x, 0".
4618     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
4619         isa<ConstantAggregateZero>(RHS->getOperand(1)))
4620       return new FCmpInst(FCmpInst::FCMP_UNO,
4621                           LHS->getOperand(0), RHS->getOperand(0));
4622     
4623     return 0;
4624   }
4625   
4626   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
4627   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
4628   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
4629   
4630   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
4631     // Swap RHS operands to match LHS.
4632     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
4633     std::swap(Op1LHS, Op1RHS);
4634   }
4635   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
4636     // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
4637     if (Op0CC == Op1CC)
4638       return new FCmpInst((FCmpInst::Predicate)Op0CC,
4639                           Op0LHS, Op0RHS);
4640     if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
4641       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
4642     if (Op0CC == FCmpInst::FCMP_FALSE)
4643       return ReplaceInstUsesWith(I, RHS);
4644     if (Op1CC == FCmpInst::FCMP_FALSE)
4645       return ReplaceInstUsesWith(I, LHS);
4646     bool Op0Ordered;
4647     bool Op1Ordered;
4648     unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
4649     unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
4650     if (Op0Ordered == Op1Ordered) {
4651       // If both are ordered or unordered, return a new fcmp with
4652       // or'ed predicates.
4653       Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred,
4654                                Op0LHS, Op0RHS, Context);
4655       if (Instruction *I = dyn_cast<Instruction>(RV))
4656         return I;
4657       // Otherwise, it's a constant boolean value...
4658       return ReplaceInstUsesWith(I, RV);
4659     }
4660   }
4661   return 0;
4662 }
4663
4664 /// FoldOrWithConstants - This helper function folds:
4665 ///
4666 ///     ((A | B) & C1) | (B & C2)
4667 ///
4668 /// into:
4669 /// 
4670 ///     (A & C1) | B
4671 ///
4672 /// when the XOR of the two constants is "all ones" (-1).
4673 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
4674                                                Value *A, Value *B, Value *C) {
4675   ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
4676   if (!CI1) return 0;
4677
4678   Value *V1 = 0;
4679   ConstantInt *CI2 = 0;
4680   if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
4681
4682   APInt Xor = CI1->getValue() ^ CI2->getValue();
4683   if (!Xor.isAllOnesValue()) return 0;
4684
4685   if (V1 == A || V1 == B) {
4686     Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
4687     return BinaryOperator::CreateOr(NewOp, V1);
4688   }
4689
4690   return 0;
4691 }
4692
4693 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
4694   bool Changed = SimplifyCommutative(I);
4695   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4696
4697   if (isa<UndefValue>(Op1))                       // X | undef -> -1
4698     return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
4699
4700   // or X, X = X
4701   if (Op0 == Op1)
4702     return ReplaceInstUsesWith(I, Op0);
4703
4704   // See if we can simplify any instructions used by the instruction whose sole 
4705   // purpose is to compute bits we don't care about.
4706   if (SimplifyDemandedInstructionBits(I))
4707     return &I;
4708   if (isa<VectorType>(I.getType())) {
4709     if (isa<ConstantAggregateZero>(Op1)) {
4710       return ReplaceInstUsesWith(I, Op0);  // X | <0,0> -> X
4711     } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
4712       if (CP->isAllOnesValue())            // X | <-1,-1> -> <-1,-1>
4713         return ReplaceInstUsesWith(I, I.getOperand(1));
4714     }
4715   }
4716
4717   // or X, -1 == -1
4718   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
4719     ConstantInt *C1 = 0; Value *X = 0;
4720     // (X & C1) | C2 --> (X | C2) & (C1|C2)
4721     if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
4722         isOnlyUse(Op0)) {
4723       Value *Or = Builder->CreateOr(X, RHS);
4724       Or->takeName(Op0);
4725       return BinaryOperator::CreateAnd(Or, 
4726                ConstantInt::get(*Context, RHS->getValue() | C1->getValue()));
4727     }
4728
4729     // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
4730     if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
4731         isOnlyUse(Op0)) {
4732       Value *Or = Builder->CreateOr(X, RHS);
4733       Or->takeName(Op0);
4734       return BinaryOperator::CreateXor(Or,
4735                  ConstantInt::get(*Context, C1->getValue() & ~RHS->getValue()));
4736     }
4737
4738     // Try to fold constant and into select arguments.
4739     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
4740       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
4741         return R;
4742     if (isa<PHINode>(Op0))
4743       if (Instruction *NV = FoldOpIntoPhi(I))
4744         return NV;
4745   }
4746
4747   Value *A = 0, *B = 0;
4748   ConstantInt *C1 = 0, *C2 = 0;
4749
4750   if (match(Op0, m_And(m_Value(A), m_Value(B))))
4751     if (A == Op1 || B == Op1)    // (A & ?) | A  --> A
4752       return ReplaceInstUsesWith(I, Op1);
4753   if (match(Op1, m_And(m_Value(A), m_Value(B))))
4754     if (A == Op0 || B == Op0)    // A | (A & ?)  --> A
4755       return ReplaceInstUsesWith(I, Op0);
4756
4757   // (A | B) | C  and  A | (B | C)                  -> bswap if possible.
4758   // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible.
4759   if (match(Op0, m_Or(m_Value(), m_Value())) ||
4760       match(Op1, m_Or(m_Value(), m_Value())) ||
4761       (match(Op0, m_Shift(m_Value(), m_Value())) &&
4762        match(Op1, m_Shift(m_Value(), m_Value())))) {
4763     if (Instruction *BSwap = MatchBSwap(I))
4764       return BSwap;
4765   }
4766   
4767   // (X^C)|Y -> (X|Y)^C iff Y&C == 0
4768   if (Op0->hasOneUse() &&
4769       match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
4770       MaskedValueIsZero(Op1, C1->getValue())) {
4771     Value *NOr = Builder->CreateOr(A, Op1);
4772     NOr->takeName(Op0);
4773     return BinaryOperator::CreateXor(NOr, C1);
4774   }
4775
4776   // Y|(X^C) -> (X|Y)^C iff Y&C == 0
4777   if (Op1->hasOneUse() &&
4778       match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
4779       MaskedValueIsZero(Op0, C1->getValue())) {
4780     Value *NOr = Builder->CreateOr(A, Op0);
4781     NOr->takeName(Op0);
4782     return BinaryOperator::CreateXor(NOr, C1);
4783   }
4784
4785   // (A & C)|(B & D)
4786   Value *C = 0, *D = 0;
4787   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
4788       match(Op1, m_And(m_Value(B), m_Value(D)))) {
4789     Value *V1 = 0, *V2 = 0, *V3 = 0;
4790     C1 = dyn_cast<ConstantInt>(C);
4791     C2 = dyn_cast<ConstantInt>(D);
4792     if (C1 && C2) {  // (A & C1)|(B & C2)
4793       // If we have: ((V + N) & C1) | (V & C2)
4794       // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
4795       // replace with V+N.
4796       if (C1->getValue() == ~C2->getValue()) {
4797         if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
4798             match(A, m_Add(m_Value(V1), m_Value(V2)))) {
4799           // Add commutes, try both ways.
4800           if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
4801             return ReplaceInstUsesWith(I, A);
4802           if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
4803             return ReplaceInstUsesWith(I, A);
4804         }
4805         // Or commutes, try both ways.
4806         if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
4807             match(B, m_Add(m_Value(V1), m_Value(V2)))) {
4808           // Add commutes, try both ways.
4809           if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
4810             return ReplaceInstUsesWith(I, B);
4811           if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
4812             return ReplaceInstUsesWith(I, B);
4813         }
4814       }
4815       V1 = 0; V2 = 0; V3 = 0;
4816     }
4817     
4818     // Check to see if we have any common things being and'ed.  If so, find the
4819     // terms for V1 & (V2|V3).
4820     if (isOnlyUse(Op0) || isOnlyUse(Op1)) {
4821       if (A == B)      // (A & C)|(A & D) == A & (C|D)
4822         V1 = A, V2 = C, V3 = D;
4823       else if (A == D) // (A & C)|(B & A) == A & (B|C)
4824         V1 = A, V2 = B, V3 = C;
4825       else if (C == B) // (A & C)|(C & D) == C & (A|D)
4826         V1 = C, V2 = A, V3 = D;
4827       else if (C == D) // (A & C)|(B & C) == C & (A|B)
4828         V1 = C, V2 = A, V3 = B;
4829       
4830       if (V1) {
4831         Value *Or = Builder->CreateOr(V2, V3, "tmp");
4832         return BinaryOperator::CreateAnd(V1, Or);
4833       }
4834     }
4835
4836     // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) ->  C0 ? A : B, and commuted variants
4837     if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D, Context))
4838       return Match;
4839     if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C, Context))
4840       return Match;
4841     if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D, Context))
4842       return Match;
4843     if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C, Context))
4844       return Match;
4845
4846     // ((A&~B)|(~A&B)) -> A^B
4847     if ((match(C, m_Not(m_Specific(D))) &&
4848          match(B, m_Not(m_Specific(A)))))
4849       return BinaryOperator::CreateXor(A, D);
4850     // ((~B&A)|(~A&B)) -> A^B
4851     if ((match(A, m_Not(m_Specific(D))) &&
4852          match(B, m_Not(m_Specific(C)))))
4853       return BinaryOperator::CreateXor(C, D);
4854     // ((A&~B)|(B&~A)) -> A^B
4855     if ((match(C, m_Not(m_Specific(B))) &&
4856          match(D, m_Not(m_Specific(A)))))
4857       return BinaryOperator::CreateXor(A, B);
4858     // ((~B&A)|(B&~A)) -> A^B
4859     if ((match(A, m_Not(m_Specific(B))) &&
4860          match(D, m_Not(m_Specific(C)))))
4861       return BinaryOperator::CreateXor(C, B);
4862   }
4863   
4864   // (X >> Z) | (Y >> Z)  -> (X|Y) >> Z  for all shifts.
4865   if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
4866     if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
4867       if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && 
4868           SI0->getOperand(1) == SI1->getOperand(1) &&
4869           (SI0->hasOneUse() || SI1->hasOneUse())) {
4870         Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
4871                                          SI0->getName());
4872         return BinaryOperator::Create(SI1->getOpcode(), NewOp, 
4873                                       SI1->getOperand(1));
4874       }
4875   }
4876
4877   // ((A|B)&1)|(B&-2) -> (A&1) | B
4878   if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
4879       match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
4880     Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C);
4881     if (Ret) return Ret;
4882   }
4883   // (B&-2)|((A|B)&1) -> (A&1) | B
4884   if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
4885       match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
4886     Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C);
4887     if (Ret) return Ret;
4888   }
4889
4890   if (match(Op0, m_Not(m_Value(A)))) {   // ~A | Op1
4891     if (A == Op1)   // ~A | A == -1
4892       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
4893   } else {
4894     A = 0;
4895   }
4896   // Note, A is still live here!
4897   if (match(Op1, m_Not(m_Value(B)))) {   // Op0 | ~B
4898     if (Op0 == B)
4899       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
4900
4901     // (~A | ~B) == (~(A & B)) - De Morgan's Law
4902     if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
4903       Value *And = Builder->CreateAnd(A, B, I.getName()+".demorgan");
4904       return BinaryOperator::CreateNot(And);
4905     }
4906   }
4907
4908   // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
4909   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) {
4910     if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
4911       return R;
4912
4913     if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
4914       if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS))
4915         return Res;
4916   }
4917     
4918   // fold (or (cast A), (cast B)) -> (cast (or A, B))
4919   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
4920     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
4921       if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
4922         if (!isa<ICmpInst>(Op0C->getOperand(0)) ||
4923             !isa<ICmpInst>(Op1C->getOperand(0))) {
4924           const Type *SrcTy = Op0C->getOperand(0)->getType();
4925           if (SrcTy == Op1C->getOperand(0)->getType() &&
4926               SrcTy->isIntOrIntVector() &&
4927               // Only do this if the casts both really cause code to be
4928               // generated.
4929               ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), 
4930                                 I.getType(), TD) &&
4931               ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), 
4932                                 I.getType(), TD)) {
4933             Value *NewOp = Builder->CreateOr(Op0C->getOperand(0),
4934                                              Op1C->getOperand(0), I.getName());
4935             return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
4936           }
4937         }
4938       }
4939   }
4940   
4941     
4942   // (fcmp uno x, c) | (fcmp uno y, c)  -> (fcmp uno x, y)
4943   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
4944     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
4945       if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS))
4946         return Res;
4947   }
4948
4949   return Changed ? &I : 0;
4950 }
4951
4952 namespace {
4953
4954 // XorSelf - Implements: X ^ X --> 0
4955 struct XorSelf {
4956   Value *RHS;
4957   XorSelf(Value *rhs) : RHS(rhs) {}
4958   bool shouldApply(Value *LHS) const { return LHS == RHS; }
4959   Instruction *apply(BinaryOperator &Xor) const {
4960     return &Xor;
4961   }
4962 };
4963
4964 }
4965
4966 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
4967   bool Changed = SimplifyCommutative(I);
4968   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4969
4970   if (isa<UndefValue>(Op1)) {
4971     if (isa<UndefValue>(Op0))
4972       // Handle undef ^ undef -> 0 special case. This is a common
4973       // idiom (misuse).
4974       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
4975     return ReplaceInstUsesWith(I, Op1);  // X ^ undef -> undef
4976   }
4977
4978   // xor X, X = 0, even if X is nested in a sequence of Xor's.
4979   if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
4980     assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
4981     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
4982   }
4983   
4984   // See if we can simplify any instructions used by the instruction whose sole 
4985   // purpose is to compute bits we don't care about.
4986   if (SimplifyDemandedInstructionBits(I))
4987     return &I;
4988   if (isa<VectorType>(I.getType()))
4989     if (isa<ConstantAggregateZero>(Op1))
4990       return ReplaceInstUsesWith(I, Op0);  // X ^ <0,0> -> X
4991
4992   // Is this a ~ operation?
4993   if (Value *NotOp = dyn_castNotVal(&I)) {
4994     // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
4995     // ~(~X | Y) === (X & ~Y) - De Morgan's Law
4996     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
4997       if (Op0I->getOpcode() == Instruction::And || 
4998           Op0I->getOpcode() == Instruction::Or) {
4999         if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
5000         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
5001           Value *NotY =
5002             Builder->CreateNot(Op0I->getOperand(1),
5003                                Op0I->getOperand(1)->getName()+".not");
5004           if (Op0I->getOpcode() == Instruction::And)
5005             return BinaryOperator::CreateOr(Op0NotVal, NotY);
5006           return BinaryOperator::CreateAnd(Op0NotVal, NotY);
5007         }
5008       }
5009     }
5010   }
5011   
5012   
5013   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
5014     if (RHS == ConstantInt::getTrue(*Context) && Op0->hasOneUse()) {
5015       // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
5016       if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
5017         return new ICmpInst(ICI->getInversePredicate(),
5018                             ICI->getOperand(0), ICI->getOperand(1));
5019
5020       if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
5021         return new FCmpInst(FCI->getInversePredicate(),
5022                             FCI->getOperand(0), FCI->getOperand(1));
5023     }
5024
5025     // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
5026     if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
5027       if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
5028         if (CI->hasOneUse() && Op0C->hasOneUse()) {
5029           Instruction::CastOps Opcode = Op0C->getOpcode();
5030           if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
5031               (RHS == ConstantExpr::getCast(Opcode, 
5032                                             ConstantInt::getTrue(*Context),
5033                                             Op0C->getDestTy()))) {
5034             CI->setPredicate(CI->getInversePredicate());
5035             return CastInst::Create(Opcode, CI, Op0C->getType());
5036           }
5037         }
5038       }
5039     }
5040
5041     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
5042       // ~(c-X) == X-c-1 == X+(-c-1)
5043       if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
5044         if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
5045           Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
5046           Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
5047                                       ConstantInt::get(I.getType(), 1));
5048           return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
5049         }
5050           
5051       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
5052         if (Op0I->getOpcode() == Instruction::Add) {
5053           // ~(X-c) --> (-c-1)-X
5054           if (RHS->isAllOnesValue()) {
5055             Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
5056             return BinaryOperator::CreateSub(
5057                            ConstantExpr::getSub(NegOp0CI,
5058                                       ConstantInt::get(I.getType(), 1)),
5059                                       Op0I->getOperand(0));
5060           } else if (RHS->getValue().isSignBit()) {
5061             // (X + C) ^ signbit -> (X + C + signbit)
5062             Constant *C = ConstantInt::get(*Context,
5063                                            RHS->getValue() + Op0CI->getValue());
5064             return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
5065
5066           }
5067         } else if (Op0I->getOpcode() == Instruction::Or) {
5068           // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
5069           if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
5070             Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
5071             // Anything in both C1 and C2 is known to be zero, remove it from
5072             // NewRHS.
5073             Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
5074             NewRHS = ConstantExpr::getAnd(NewRHS, 
5075                                        ConstantExpr::getNot(CommonBits));
5076             Worklist.Add(Op0I);
5077             I.setOperand(0, Op0I->getOperand(0));
5078             I.setOperand(1, NewRHS);
5079             return &I;
5080           }
5081         }
5082       }
5083     }
5084
5085     // Try to fold constant and into select arguments.
5086     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
5087       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
5088         return R;
5089     if (isa<PHINode>(Op0))
5090       if (Instruction *NV = FoldOpIntoPhi(I))
5091         return NV;
5092   }
5093
5094   if (Value *X = dyn_castNotVal(Op0))   // ~A ^ A == -1
5095     if (X == Op1)
5096       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
5097
5098   if (Value *X = dyn_castNotVal(Op1))   // A ^ ~A == -1
5099     if (X == Op0)
5100       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
5101
5102   
5103   BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
5104   if (Op1I) {
5105     Value *A, *B;
5106     if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
5107       if (A == Op0) {              // B^(B|A) == (A|B)^B
5108         Op1I->swapOperands();
5109         I.swapOperands();
5110         std::swap(Op0, Op1);
5111       } else if (B == Op0) {       // B^(A|B) == (A|B)^B
5112         I.swapOperands();     // Simplified below.
5113         std::swap(Op0, Op1);
5114       }
5115     } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) {
5116       return ReplaceInstUsesWith(I, B);                      // A^(A^B) == B
5117     } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) {
5118       return ReplaceInstUsesWith(I, A);                      // A^(B^A) == B
5119     } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && 
5120                Op1I->hasOneUse()){
5121       if (A == Op0) {                                      // A^(A&B) -> A^(B&A)
5122         Op1I->swapOperands();
5123         std::swap(A, B);
5124       }
5125       if (B == Op0) {                                      // A^(B&A) -> (B&A)^A
5126         I.swapOperands();     // Simplified below.
5127         std::swap(Op0, Op1);
5128       }
5129     }
5130   }
5131   
5132   BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
5133   if (Op0I) {
5134     Value *A, *B;
5135     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
5136         Op0I->hasOneUse()) {
5137       if (A == Op1)                                  // (B|A)^B == (A|B)^B
5138         std::swap(A, B);
5139       if (B == Op1)                                  // (A|B)^B == A & ~B
5140         return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1, "tmp"));
5141     } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) {
5142       return ReplaceInstUsesWith(I, B);                      // (A^B)^A == B
5143     } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) {
5144       return ReplaceInstUsesWith(I, A);                      // (B^A)^A == B
5145     } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && 
5146                Op0I->hasOneUse()){
5147       if (A == Op1)                                        // (A&B)^A -> (B&A)^A
5148         std::swap(A, B);
5149       if (B == Op1 &&                                      // (B&A)^A == ~B & A
5150           !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C
5151         return BinaryOperator::CreateAnd(Builder->CreateNot(A, "tmp"), Op1);
5152       }
5153     }
5154   }
5155   
5156   // (X >> Z) ^ (Y >> Z)  -> (X^Y) >> Z  for all shifts.
5157   if (Op0I && Op1I && Op0I->isShift() && 
5158       Op0I->getOpcode() == Op1I->getOpcode() && 
5159       Op0I->getOperand(1) == Op1I->getOperand(1) &&
5160       (Op1I->hasOneUse() || Op1I->hasOneUse())) {
5161     Value *NewOp =
5162       Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0),
5163                          Op0I->getName());
5164     return BinaryOperator::Create(Op1I->getOpcode(), NewOp, 
5165                                   Op1I->getOperand(1));
5166   }
5167     
5168   if (Op0I && Op1I) {
5169     Value *A, *B, *C, *D;
5170     // (A & B)^(A | B) -> A ^ B
5171     if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
5172         match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
5173       if ((A == C && B == D) || (A == D && B == C)) 
5174         return BinaryOperator::CreateXor(A, B);
5175     }
5176     // (A | B)^(A & B) -> A ^ B
5177     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
5178         match(Op1I, m_And(m_Value(C), m_Value(D)))) {
5179       if ((A == C && B == D) || (A == D && B == C)) 
5180         return BinaryOperator::CreateXor(A, B);
5181     }
5182     
5183     // (A & B)^(C & D)
5184     if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
5185         match(Op0I, m_And(m_Value(A), m_Value(B))) &&
5186         match(Op1I, m_And(m_Value(C), m_Value(D)))) {
5187       // (X & Y)^(X & Y) -> (Y^Z) & X
5188       Value *X = 0, *Y = 0, *Z = 0;
5189       if (A == C)
5190         X = A, Y = B, Z = D;
5191       else if (A == D)
5192         X = A, Y = B, Z = C;
5193       else if (B == C)
5194         X = B, Y = A, Z = D;
5195       else if (B == D)
5196         X = B, Y = A, Z = C;
5197       
5198       if (X) {
5199         Value *NewOp = Builder->CreateXor(Y, Z, Op0->getName());
5200         return BinaryOperator::CreateAnd(NewOp, X);
5201       }
5202     }
5203   }
5204     
5205   // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
5206   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
5207     if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
5208       return R;
5209
5210   // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
5211   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
5212     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
5213       if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
5214         const Type *SrcTy = Op0C->getOperand(0)->getType();
5215         if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
5216             // Only do this if the casts both really cause code to be generated.
5217             ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), 
5218                               I.getType(), TD) &&
5219             ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), 
5220                               I.getType(), TD)) {
5221           Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
5222                                             Op1C->getOperand(0), I.getName());
5223           return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
5224         }
5225       }
5226   }
5227
5228   return Changed ? &I : 0;
5229 }
5230
5231 static ConstantInt *ExtractElement(Constant *V, Constant *Idx,
5232                                    LLVMContext *Context) {
5233   return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
5234 }
5235
5236 static bool HasAddOverflow(ConstantInt *Result,
5237                            ConstantInt *In1, ConstantInt *In2,
5238                            bool IsSigned) {
5239   if (IsSigned)
5240     if (In2->getValue().isNegative())
5241       return Result->getValue().sgt(In1->getValue());
5242     else
5243       return Result->getValue().slt(In1->getValue());
5244   else
5245     return Result->getValue().ult(In1->getValue());
5246 }
5247
5248 /// AddWithOverflow - Compute Result = In1+In2, returning true if the result
5249 /// overflowed for this type.
5250 static bool AddWithOverflow(Constant *&Result, Constant *In1,
5251                             Constant *In2, LLVMContext *Context,
5252                             bool IsSigned = false) {
5253   Result = ConstantExpr::getAdd(In1, In2);
5254
5255   if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
5256     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
5257       Constant *Idx = ConstantInt::get(Type::getInt32Ty(*Context), i);
5258       if (HasAddOverflow(ExtractElement(Result, Idx, Context),
5259                          ExtractElement(In1, Idx, Context),
5260                          ExtractElement(In2, Idx, Context),
5261                          IsSigned))
5262         return true;
5263     }
5264     return false;
5265   }
5266
5267   return HasAddOverflow(cast<ConstantInt>(Result),
5268                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
5269                         IsSigned);
5270 }
5271
5272 static bool HasSubOverflow(ConstantInt *Result,
5273                            ConstantInt *In1, ConstantInt *In2,
5274                            bool IsSigned) {
5275   if (IsSigned)
5276     if (In2->getValue().isNegative())
5277       return Result->getValue().slt(In1->getValue());
5278     else
5279       return Result->getValue().sgt(In1->getValue());
5280   else
5281     return Result->getValue().ugt(In1->getValue());
5282 }
5283
5284 /// SubWithOverflow - Compute Result = In1-In2, returning true if the result
5285 /// overflowed for this type.
5286 static bool SubWithOverflow(Constant *&Result, Constant *In1,
5287                             Constant *In2, LLVMContext *Context,
5288                             bool IsSigned = false) {
5289   Result = ConstantExpr::getSub(In1, In2);
5290
5291   if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
5292     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
5293       Constant *Idx = ConstantInt::get(Type::getInt32Ty(*Context), i);
5294       if (HasSubOverflow(ExtractElement(Result, Idx, Context),
5295                          ExtractElement(In1, Idx, Context),
5296                          ExtractElement(In2, Idx, Context),
5297                          IsSigned))
5298         return true;
5299     }
5300     return false;
5301   }
5302
5303   return HasSubOverflow(cast<ConstantInt>(Result),
5304                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
5305                         IsSigned);
5306 }
5307
5308 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
5309 /// code necessary to compute the offset from the base pointer (without adding
5310 /// in the base pointer).  Return the result as a signed integer of intptr size.
5311 static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
5312   TargetData &TD = *IC.getTargetData();
5313   gep_type_iterator GTI = gep_type_begin(GEP);
5314   const Type *IntPtrTy = TD.getIntPtrType(I.getContext());
5315   Value *Result = Constant::getNullValue(IntPtrTy);
5316
5317   // Build a mask for high order bits.
5318   unsigned IntPtrWidth = TD.getPointerSizeInBits();
5319   uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
5320
5321   for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
5322        ++i, ++GTI) {
5323     Value *Op = *i;
5324     uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
5325     if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
5326       if (OpC->isZero()) continue;
5327       
5328       // Handle a struct index, which adds its field offset to the pointer.
5329       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
5330         Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
5331         
5332         Result = IC.Builder->CreateAdd(Result,
5333                                        ConstantInt::get(IntPtrTy, Size),
5334                                        GEP->getName()+".offs");
5335         continue;
5336       }
5337       
5338       Constant *Scale = ConstantInt::get(IntPtrTy, Size);
5339       Constant *OC =
5340               ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
5341       Scale = ConstantExpr::getMul(OC, Scale);
5342       // Emit an add instruction.
5343       Result = IC.Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
5344       continue;
5345     }
5346     // Convert to correct type.
5347     if (Op->getType() != IntPtrTy)
5348       Op = IC.Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
5349     if (Size != 1) {
5350       Constant *Scale = ConstantInt::get(IntPtrTy, Size);
5351       // We'll let instcombine(mul) convert this to a shl if possible.
5352       Op = IC.Builder->CreateMul(Op, Scale, GEP->getName()+".idx");
5353     }
5354
5355     // Emit an add instruction.
5356     Result = IC.Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
5357   }
5358   return Result;
5359 }
5360
5361
5362 /// EvaluateGEPOffsetExpression - Return a value that can be used to compare
5363 /// the *offset* implied by a GEP to zero.  For example, if we have &A[i], we
5364 /// want to return 'i' for "icmp ne i, 0".  Note that, in general, indices can
5365 /// be complex, and scales are involved.  The above expression would also be
5366 /// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32).
5367 /// This later form is less amenable to optimization though, and we are allowed
5368 /// to generate the first by knowing that pointer arithmetic doesn't overflow.
5369 ///
5370 /// If we can't emit an optimized form for this expression, this returns null.
5371 /// 
5372 static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I,
5373                                           InstCombiner &IC) {
5374   TargetData &TD = *IC.getTargetData();
5375   gep_type_iterator GTI = gep_type_begin(GEP);
5376
5377   // Check to see if this gep only has a single variable index.  If so, and if
5378   // any constant indices are a multiple of its scale, then we can compute this
5379   // in terms of the scale of the variable index.  For example, if the GEP
5380   // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
5381   // because the expression will cross zero at the same point.
5382   unsigned i, e = GEP->getNumOperands();
5383   int64_t Offset = 0;
5384   for (i = 1; i != e; ++i, ++GTI) {
5385     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
5386       // Compute the aggregate offset of constant indices.
5387       if (CI->isZero()) continue;
5388
5389       // Handle a struct index, which adds its field offset to the pointer.
5390       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
5391         Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
5392       } else {
5393         uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
5394         Offset += Size*CI->getSExtValue();
5395       }
5396     } else {
5397       // Found our variable index.
5398       break;
5399     }
5400   }
5401   
5402   // If there are no variable indices, we must have a constant offset, just
5403   // evaluate it the general way.
5404   if (i == e) return 0;
5405   
5406   Value *VariableIdx = GEP->getOperand(i);
5407   // Determine the scale factor of the variable element.  For example, this is
5408   // 4 if the variable index is into an array of i32.
5409   uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType());
5410   
5411   // Verify that there are no other variable indices.  If so, emit the hard way.
5412   for (++i, ++GTI; i != e; ++i, ++GTI) {
5413     ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
5414     if (!CI) return 0;
5415    
5416     // Compute the aggregate offset of constant indices.
5417     if (CI->isZero()) continue;
5418     
5419     // Handle a struct index, which adds its field offset to the pointer.
5420     if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
5421       Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
5422     } else {
5423       uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
5424       Offset += Size*CI->getSExtValue();
5425     }
5426   }
5427   
5428   // Okay, we know we have a single variable index, which must be a
5429   // pointer/array/vector index.  If there is no offset, life is simple, return
5430   // the index.
5431   unsigned IntPtrWidth = TD.getPointerSizeInBits();
5432   if (Offset == 0) {
5433     // Cast to intptrty in case a truncation occurs.  If an extension is needed,
5434     // we don't need to bother extending: the extension won't affect where the
5435     // computation crosses zero.
5436     if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth)
5437       VariableIdx = new TruncInst(VariableIdx, 
5438                                   TD.getIntPtrType(VariableIdx->getContext()),
5439                                   VariableIdx->getName(), &I);
5440     return VariableIdx;
5441   }
5442   
5443   // Otherwise, there is an index.  The computation we will do will be modulo
5444   // the pointer size, so get it.
5445   uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
5446   
5447   Offset &= PtrSizeMask;
5448   VariableScale &= PtrSizeMask;
5449
5450   // To do this transformation, any constant index must be a multiple of the
5451   // variable scale factor.  For example, we can evaluate "12 + 4*i" as "3 + i",
5452   // but we can't evaluate "10 + 3*i" in terms of i.  Check that the offset is a
5453   // multiple of the variable scale.
5454   int64_t NewOffs = Offset / (int64_t)VariableScale;
5455   if (Offset != NewOffs*(int64_t)VariableScale)
5456     return 0;
5457
5458   // Okay, we can do this evaluation.  Start by converting the index to intptr.
5459   const Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
5460   if (VariableIdx->getType() != IntPtrTy)
5461     VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
5462                                               true /*SExt*/, 
5463                                               VariableIdx->getName(), &I);
5464   Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
5465   return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I);
5466 }
5467
5468
5469 /// FoldGEPICmp - Fold comparisons between a GEP instruction and something
5470 /// else.  At this point we know that the GEP is on the LHS of the comparison.
5471 Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
5472                                        ICmpInst::Predicate Cond,
5473                                        Instruction &I) {
5474   // Look through bitcasts.
5475   if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS))
5476     RHS = BCI->getOperand(0);
5477
5478   Value *PtrBase = GEPLHS->getOperand(0);
5479   if (TD && PtrBase == RHS && GEPLHS->isInBounds()) {
5480     // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0).
5481     // This transformation (ignoring the base and scales) is valid because we
5482     // know pointers can't overflow since the gep is inbounds.  See if we can
5483     // output an optimized form.
5484     Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this);
5485     
5486     // If not, synthesize the offset the hard way.
5487     if (Offset == 0)
5488       Offset = EmitGEPOffset(GEPLHS, I, *this);
5489     return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
5490                         Constant::getNullValue(Offset->getType()));
5491   } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
5492     // If the base pointers are different, but the indices are the same, just
5493     // compare the base pointer.
5494     if (PtrBase != GEPRHS->getOperand(0)) {
5495       bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
5496       IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
5497                         GEPRHS->getOperand(0)->getType();
5498       if (IndicesTheSame)
5499         for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
5500           if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
5501             IndicesTheSame = false;
5502             break;
5503           }
5504
5505       // If all indices are the same, just compare the base pointers.
5506       if (IndicesTheSame)
5507         return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
5508                             GEPLHS->getOperand(0), GEPRHS->getOperand(0));
5509
5510       // Otherwise, the base pointers are different and the indices are
5511       // different, bail out.
5512       return 0;
5513     }
5514
5515     // If one of the GEPs has all zero indices, recurse.
5516     bool AllZeros = true;
5517     for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
5518       if (!isa<Constant>(GEPLHS->getOperand(i)) ||
5519           !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
5520         AllZeros = false;
5521         break;
5522       }
5523     if (AllZeros)
5524       return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
5525                           ICmpInst::getSwappedPredicate(Cond), I);
5526
5527     // If the other GEP has all zero indices, recurse.
5528     AllZeros = true;
5529     for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
5530       if (!isa<Constant>(GEPRHS->getOperand(i)) ||
5531           !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
5532         AllZeros = false;
5533         break;
5534       }
5535     if (AllZeros)
5536       return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
5537
5538     if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
5539       // If the GEPs only differ by one index, compare it.
5540       unsigned NumDifferences = 0;  // Keep track of # differences.
5541       unsigned DiffOperand = 0;     // The operand that differs.
5542       for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
5543         if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
5544           if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
5545                    GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
5546             // Irreconcilable differences.
5547             NumDifferences = 2;
5548             break;
5549           } else {
5550             if (NumDifferences++) break;
5551             DiffOperand = i;
5552           }
5553         }
5554
5555       if (NumDifferences == 0)   // SAME GEP?
5556         return ReplaceInstUsesWith(I, // No comparison is needed here.
5557                                    ConstantInt::get(Type::getInt1Ty(*Context),
5558                                              ICmpInst::isTrueWhenEqual(Cond)));
5559
5560       else if (NumDifferences == 1) {
5561         Value *LHSV = GEPLHS->getOperand(DiffOperand);
5562         Value *RHSV = GEPRHS->getOperand(DiffOperand);
5563         // Make sure we do a signed comparison here.
5564         return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
5565       }
5566     }
5567
5568     // Only lower this if the icmp is the only user of the GEP or if we expect
5569     // the result to fold to a constant!
5570     if (TD &&
5571         (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
5572         (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
5573       // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)  --->  (OFFSET1 cmp OFFSET2)
5574       Value *L = EmitGEPOffset(GEPLHS, I, *this);
5575       Value *R = EmitGEPOffset(GEPRHS, I, *this);
5576       return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
5577     }
5578   }
5579   return 0;
5580 }
5581
5582 /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.
5583 ///
5584 Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
5585                                                 Instruction *LHSI,
5586                                                 Constant *RHSC) {
5587   if (!isa<ConstantFP>(RHSC)) return 0;
5588   const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
5589   
5590   // Get the width of the mantissa.  We don't want to hack on conversions that
5591   // might lose information from the integer, e.g. "i64 -> float"
5592   int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
5593   if (MantissaWidth == -1) return 0;  // Unknown.
5594   
5595   // Check to see that the input is converted from an integer type that is small
5596   // enough that preserves all bits.  TODO: check here for "known" sign bits.
5597   // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
5598   unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits();
5599   
5600   // If this is a uitofp instruction, we need an extra bit to hold the sign.
5601   bool LHSUnsigned = isa<UIToFPInst>(LHSI);
5602   if (LHSUnsigned)
5603     ++InputSize;
5604   
5605   // If the conversion would lose info, don't hack on this.
5606   if ((int)InputSize > MantissaWidth)
5607     return 0;
5608   
5609   // Otherwise, we can potentially simplify the comparison.  We know that it
5610   // will always come through as an integer value and we know the constant is
5611   // not a NAN (it would have been previously simplified).
5612   assert(!RHS.isNaN() && "NaN comparison not already folded!");
5613   
5614   ICmpInst::Predicate Pred;
5615   switch (I.getPredicate()) {
5616   default: llvm_unreachable("Unexpected predicate!");
5617   case FCmpInst::FCMP_UEQ:
5618   case FCmpInst::FCMP_OEQ:
5619     Pred = ICmpInst::ICMP_EQ;
5620     break;
5621   case FCmpInst::FCMP_UGT:
5622   case FCmpInst::FCMP_OGT:
5623     Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
5624     break;
5625   case FCmpInst::FCMP_UGE:
5626   case FCmpInst::FCMP_OGE:
5627     Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
5628     break;
5629   case FCmpInst::FCMP_ULT:
5630   case FCmpInst::FCMP_OLT:
5631     Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
5632     break;
5633   case FCmpInst::FCMP_ULE:
5634   case FCmpInst::FCMP_OLE:
5635     Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
5636     break;
5637   case FCmpInst::FCMP_UNE:
5638   case FCmpInst::FCMP_ONE:
5639     Pred = ICmpInst::ICMP_NE;
5640     break;
5641   case FCmpInst::FCMP_ORD:
5642     return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5643   case FCmpInst::FCMP_UNO:
5644     return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5645   }
5646   
5647   const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
5648   
5649   // Now we know that the APFloat is a normal number, zero or inf.
5650   
5651   // See if the FP constant is too large for the integer.  For example,
5652   // comparing an i8 to 300.0.
5653   unsigned IntWidth = IntTy->getScalarSizeInBits();
5654   
5655   if (!LHSUnsigned) {
5656     // If the RHS value is > SignedMax, fold the comparison.  This handles +INF
5657     // and large values.
5658     APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false);
5659     SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
5660                           APFloat::rmNearestTiesToEven);
5661     if (SMax.compare(RHS) == APFloat::cmpLessThan) {  // smax < 13123.0
5662       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_SLT ||
5663           Pred == ICmpInst::ICMP_SLE)
5664         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5665       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5666     }
5667   } else {
5668     // If the RHS value is > UnsignedMax, fold the comparison. This handles
5669     // +INF and large values.
5670     APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false);
5671     UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
5672                           APFloat::rmNearestTiesToEven);
5673     if (UMax.compare(RHS) == APFloat::cmpLessThan) {  // umax < 13123.0
5674       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_ULT ||
5675           Pred == ICmpInst::ICMP_ULE)
5676         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5677       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5678     }
5679   }
5680   
5681   if (!LHSUnsigned) {
5682     // See if the RHS value is < SignedMin.
5683     APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false);
5684     SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
5685                           APFloat::rmNearestTiesToEven);
5686     if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
5687       if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
5688           Pred == ICmpInst::ICMP_SGE)
5689         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5690       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5691     }
5692   }
5693
5694   // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
5695   // [0, UMAX], but it may still be fractional.  See if it is fractional by
5696   // casting the FP value to the integer value and back, checking for equality.
5697   // Don't do this for zero, because -0.0 is not fractional.
5698   Constant *RHSInt = LHSUnsigned
5699     ? ConstantExpr::getFPToUI(RHSC, IntTy)
5700     : ConstantExpr::getFPToSI(RHSC, IntTy);
5701   if (!RHS.isZero()) {
5702     bool Equal = LHSUnsigned
5703       ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
5704       : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
5705     if (!Equal) {
5706       // If we had a comparison against a fractional value, we have to adjust
5707       // the compare predicate and sometimes the value.  RHSC is rounded towards
5708       // zero at this point.
5709       switch (Pred) {
5710       default: llvm_unreachable("Unexpected integer comparison!");
5711       case ICmpInst::ICMP_NE:  // (float)int != 4.4   --> true
5712         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5713       case ICmpInst::ICMP_EQ:  // (float)int == 4.4   --> false
5714         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5715       case ICmpInst::ICMP_ULE:
5716         // (float)int <= 4.4   --> int <= 4
5717         // (float)int <= -4.4  --> false
5718         if (RHS.isNegative())
5719           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5720         break;
5721       case ICmpInst::ICMP_SLE:
5722         // (float)int <= 4.4   --> int <= 4
5723         // (float)int <= -4.4  --> int < -4
5724         if (RHS.isNegative())
5725           Pred = ICmpInst::ICMP_SLT;
5726         break;
5727       case ICmpInst::ICMP_ULT:
5728         // (float)int < -4.4   --> false
5729         // (float)int < 4.4    --> int <= 4
5730         if (RHS.isNegative())
5731           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5732         Pred = ICmpInst::ICMP_ULE;
5733         break;
5734       case ICmpInst::ICMP_SLT:
5735         // (float)int < -4.4   --> int < -4
5736         // (float)int < 4.4    --> int <= 4
5737         if (!RHS.isNegative())
5738           Pred = ICmpInst::ICMP_SLE;
5739         break;
5740       case ICmpInst::ICMP_UGT:
5741         // (float)int > 4.4    --> int > 4
5742         // (float)int > -4.4   --> true
5743         if (RHS.isNegative())
5744           return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5745         break;
5746       case ICmpInst::ICMP_SGT:
5747         // (float)int > 4.4    --> int > 4
5748         // (float)int > -4.4   --> int >= -4
5749         if (RHS.isNegative())
5750           Pred = ICmpInst::ICMP_SGE;
5751         break;
5752       case ICmpInst::ICMP_UGE:
5753         // (float)int >= -4.4   --> true
5754         // (float)int >= 4.4    --> int > 4
5755         if (!RHS.isNegative())
5756           return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5757         Pred = ICmpInst::ICMP_UGT;
5758         break;
5759       case ICmpInst::ICMP_SGE:
5760         // (float)int >= -4.4   --> int >= -4
5761         // (float)int >= 4.4    --> int > 4
5762         if (!RHS.isNegative())
5763           Pred = ICmpInst::ICMP_SGT;
5764         break;
5765       }
5766     }
5767   }
5768
5769   // Lower this FP comparison into an appropriate integer version of the
5770   // comparison.
5771   return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
5772 }
5773
5774 Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
5775   bool Changed = SimplifyCompare(I);
5776   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5777
5778   // Fold trivial predicates.
5779   if (I.getPredicate() == FCmpInst::FCMP_FALSE)
5780     return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 0));
5781   if (I.getPredicate() == FCmpInst::FCMP_TRUE)
5782     return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 1));
5783   
5784   // Simplify 'fcmp pred X, X'
5785   if (Op0 == Op1) {
5786     switch (I.getPredicate()) {
5787     default: llvm_unreachable("Unknown predicate!");
5788     case FCmpInst::FCMP_UEQ:    // True if unordered or equal
5789     case FCmpInst::FCMP_UGE:    // True if unordered, greater than, or equal
5790     case FCmpInst::FCMP_ULE:    // True if unordered, less than, or equal
5791       return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 1));
5792     case FCmpInst::FCMP_OGT:    // True if ordered and greater than
5793     case FCmpInst::FCMP_OLT:    // True if ordered and less than
5794     case FCmpInst::FCMP_ONE:    // True if ordered and operands are unequal
5795       return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 0));
5796       
5797     case FCmpInst::FCMP_UNO:    // True if unordered: isnan(X) | isnan(Y)
5798     case FCmpInst::FCMP_ULT:    // True if unordered or less than
5799     case FCmpInst::FCMP_UGT:    // True if unordered or greater than
5800     case FCmpInst::FCMP_UNE:    // True if unordered or not equal
5801       // Canonicalize these to be 'fcmp uno %X, 0.0'.
5802       I.setPredicate(FCmpInst::FCMP_UNO);
5803       I.setOperand(1, Constant::getNullValue(Op0->getType()));
5804       return &I;
5805       
5806     case FCmpInst::FCMP_ORD:    // True if ordered (no nans)
5807     case FCmpInst::FCMP_OEQ:    // True if ordered and equal
5808     case FCmpInst::FCMP_OGE:    // True if ordered and greater than or equal
5809     case FCmpInst::FCMP_OLE:    // True if ordered and less than or equal
5810       // Canonicalize these to be 'fcmp ord %X, 0.0'.
5811       I.setPredicate(FCmpInst::FCMP_ORD);
5812       I.setOperand(1, Constant::getNullValue(Op0->getType()));
5813       return &I;
5814     }
5815   }
5816     
5817   if (isa<UndefValue>(Op1))                  // fcmp pred X, undef -> undef
5818     return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
5819
5820   // Handle fcmp with constant RHS
5821   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
5822     // If the constant is a nan, see if we can fold the comparison based on it.
5823     if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
5824       if (CFP->getValueAPF().isNaN()) {
5825         if (FCmpInst::isOrdered(I.getPredicate()))   // True if ordered and...
5826           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
5827         assert(FCmpInst::isUnordered(I.getPredicate()) &&
5828                "Comparison must be either ordered or unordered!");
5829         // True if unordered.
5830         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5831       }
5832     }
5833     
5834     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
5835       switch (LHSI->getOpcode()) {
5836       case Instruction::PHI:
5837         // Only fold fcmp into the PHI if the phi and fcmp are in the same
5838         // block.  If in the same block, we're encouraging jump threading.  If
5839         // not, we are just pessimizing the code by making an i1 phi.
5840         if (LHSI->getParent() == I.getParent())
5841           if (Instruction *NV = FoldOpIntoPhi(I))
5842             return NV;
5843         break;
5844       case Instruction::SIToFP:
5845       case Instruction::UIToFP:
5846         if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC))
5847           return NV;
5848         break;
5849       case Instruction::Select:
5850         // If either operand of the select is a constant, we can fold the
5851         // comparison into the select arms, which will cause one to be
5852         // constant folded and the select turned into a bitwise or.
5853         Value *Op1 = 0, *Op2 = 0;
5854         if (LHSI->hasOneUse()) {
5855           if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
5856             // Fold the known value into the constant operand.
5857             Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
5858             // Insert a new FCmp of the other select operand.
5859             Op2 = Builder->CreateFCmp(I.getPredicate(),
5860                                       LHSI->getOperand(2), RHSC, I.getName());
5861           } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
5862             // Fold the known value into the constant operand.
5863             Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
5864             // Insert a new FCmp of the other select operand.
5865             Op1 = Builder->CreateFCmp(I.getPredicate(), LHSI->getOperand(1),
5866                                       RHSC, I.getName());
5867           }
5868         }
5869
5870         if (Op1)
5871           return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
5872         break;
5873       }
5874   }
5875
5876   return Changed ? &I : 0;
5877 }
5878
5879 Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
5880   bool Changed = SimplifyCompare(I);
5881   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5882   const Type *Ty = Op0->getType();
5883
5884   // icmp X, X
5885   if (Op0 == Op1)
5886     return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(),
5887                                                    I.isTrueWhenEqual()));
5888
5889   if (isa<UndefValue>(Op1))                  // X icmp undef -> undef
5890     return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
5891   
5892   // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
5893   // addresses never equal each other!  We already know that Op0 != Op1.
5894   if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) ||
5895        isa<ConstantPointerNull>(Op0)) &&
5896       (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
5897        isa<ConstantPointerNull>(Op1)))
5898     return ReplaceInstUsesWith(I, ConstantInt::get(Type::getInt1Ty(*Context), 
5899                                                    !I.isTrueWhenEqual()));
5900
5901   // icmp's with boolean values can always be turned into bitwise operations
5902   if (Ty == Type::getInt1Ty(*Context)) {
5903     switch (I.getPredicate()) {
5904     default: llvm_unreachable("Invalid icmp instruction!");
5905     case ICmpInst::ICMP_EQ: {               // icmp eq i1 A, B -> ~(A^B)
5906       Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp");
5907       return BinaryOperator::CreateNot(Xor);
5908     }
5909     case ICmpInst::ICMP_NE:                  // icmp eq i1 A, B -> A^B
5910       return BinaryOperator::CreateXor(Op0, Op1);
5911
5912     case ICmpInst::ICMP_UGT:
5913       std::swap(Op0, Op1);                   // Change icmp ugt -> icmp ult
5914       // FALL THROUGH
5915     case ICmpInst::ICMP_ULT:{               // icmp ult i1 A, B -> ~A & B
5916       Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
5917       return BinaryOperator::CreateAnd(Not, Op1);
5918     }
5919     case ICmpInst::ICMP_SGT:
5920       std::swap(Op0, Op1);                   // Change icmp sgt -> icmp slt
5921       // FALL THROUGH
5922     case ICmpInst::ICMP_SLT: {               // icmp slt i1 A, B -> A & ~B
5923       Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
5924       return BinaryOperator::CreateAnd(Not, Op0);
5925     }
5926     case ICmpInst::ICMP_UGE:
5927       std::swap(Op0, Op1);                   // Change icmp uge -> icmp ule
5928       // FALL THROUGH
5929     case ICmpInst::ICMP_ULE: {               //  icmp ule i1 A, B -> ~A | B
5930       Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
5931       return BinaryOperator::CreateOr(Not, Op1);
5932     }
5933     case ICmpInst::ICMP_SGE:
5934       std::swap(Op0, Op1);                   // Change icmp sge -> icmp sle
5935       // FALL THROUGH
5936     case ICmpInst::ICMP_SLE: {               //  icmp sle i1 A, B -> A | ~B
5937       Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
5938       return BinaryOperator::CreateOr(Not, Op0);
5939     }
5940     }
5941   }
5942
5943   unsigned BitWidth = 0;
5944   if (TD)
5945     BitWidth = TD->getTypeSizeInBits(Ty->getScalarType());
5946   else if (Ty->isIntOrIntVector())
5947     BitWidth = Ty->getScalarSizeInBits();
5948
5949   bool isSignBit = false;
5950
5951   // See if we are doing a comparison with a constant.
5952   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
5953     Value *A = 0, *B = 0;
5954     
5955     // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
5956     if (I.isEquality() && CI->isNullValue() &&
5957         match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
5958       // (icmp cond A B) if cond is equality
5959       return new ICmpInst(I.getPredicate(), A, B);
5960     }
5961     
5962     // If we have an icmp le or icmp ge instruction, turn it into the
5963     // appropriate icmp lt or icmp gt instruction.  This allows us to rely on
5964     // them being folded in the code below.
5965     switch (I.getPredicate()) {
5966     default: break;
5967     case ICmpInst::ICMP_ULE:
5968       if (CI->isMaxValue(false))                 // A <=u MAX -> TRUE
5969         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5970       return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
5971                           AddOne(CI));
5972     case ICmpInst::ICMP_SLE:
5973       if (CI->isMaxValue(true))                  // A <=s MAX -> TRUE
5974         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5975       return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
5976                           AddOne(CI));
5977     case ICmpInst::ICMP_UGE:
5978       if (CI->isMinValue(false))                 // A >=u MIN -> TRUE
5979         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5980       return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
5981                           SubOne(CI));
5982     case ICmpInst::ICMP_SGE:
5983       if (CI->isMinValue(true))                  // A >=s MIN -> TRUE
5984         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
5985       return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
5986                           SubOne(CI));
5987     }
5988     
5989     // If this comparison is a normal comparison, it demands all
5990     // bits, if it is a sign bit comparison, it only demands the sign bit.
5991     bool UnusedBit;
5992     isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
5993   }
5994
5995   // See if we can fold the comparison based on range information we can get
5996   // by checking whether bits are known to be zero or one in the input.
5997   if (BitWidth != 0) {
5998     APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
5999     APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
6000
6001     if (SimplifyDemandedBits(I.getOperandUse(0),
6002                              isSignBit ? APInt::getSignBit(BitWidth)
6003                                        : APInt::getAllOnesValue(BitWidth),
6004                              Op0KnownZero, Op0KnownOne, 0))
6005       return &I;
6006     if (SimplifyDemandedBits(I.getOperandUse(1),
6007                              APInt::getAllOnesValue(BitWidth),
6008                              Op1KnownZero, Op1KnownOne, 0))
6009       return &I;
6010
6011     // Given the known and unknown bits, compute a range that the LHS could be
6012     // in.  Compute the Min, Max and RHS values based on the known bits. For the
6013     // EQ and NE we use unsigned values.
6014     APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6015     APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6016     if (ICmpInst::isSignedPredicate(I.getPredicate())) {
6017       ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
6018                                              Op0Min, Op0Max);
6019       ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
6020                                              Op1Min, Op1Max);
6021     } else {
6022       ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
6023                                                Op0Min, Op0Max);
6024       ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
6025                                                Op1Min, Op1Max);
6026     }
6027
6028     // If Min and Max are known to be the same, then SimplifyDemandedBits
6029     // figured out that the LHS is a constant.  Just constant fold this now so
6030     // that code below can assume that Min != Max.
6031     if (!isa<Constant>(Op0) && Op0Min == Op0Max)
6032       return new ICmpInst(I.getPredicate(),
6033                           ConstantInt::get(*Context, Op0Min), Op1);
6034     if (!isa<Constant>(Op1) && Op1Min == Op1Max)
6035       return new ICmpInst(I.getPredicate(), Op0,
6036                           ConstantInt::get(*Context, Op1Min));
6037
6038     // Based on the range information we know about the LHS, see if we can
6039     // simplify this comparison.  For example, (x&4) < 8  is always true.
6040     switch (I.getPredicate()) {
6041     default: llvm_unreachable("Unknown icmp opcode!");
6042     case ICmpInst::ICMP_EQ:
6043       if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
6044         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6045       break;
6046     case ICmpInst::ICMP_NE:
6047       if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
6048         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6049       break;
6050     case ICmpInst::ICMP_ULT:
6051       if (Op0Max.ult(Op1Min))          // A <u B -> true if max(A) < min(B)
6052         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6053       if (Op0Min.uge(Op1Max))          // A <u B -> false if min(A) >= max(B)
6054         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6055       if (Op1Min == Op0Max)            // A <u B -> A != B if max(A) == min(B)
6056         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6057       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
6058         if (Op1Max == Op0Min+1)        // A <u C -> A == C-1 if min(A)+1 == C
6059           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6060                               SubOne(CI));
6061
6062         // (x <u 2147483648) -> (x >s -1)  -> true if sign bit clear
6063         if (CI->isMinValue(true))
6064           return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
6065                            Constant::getAllOnesValue(Op0->getType()));
6066       }
6067       break;
6068     case ICmpInst::ICMP_UGT:
6069       if (Op0Min.ugt(Op1Max))          // A >u B -> true if min(A) > max(B)
6070         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6071       if (Op0Max.ule(Op1Min))          // A >u B -> false if max(A) <= max(B)
6072         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6073
6074       if (Op1Max == Op0Min)            // A >u B -> A != B if min(A) == max(B)
6075         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6076       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
6077         if (Op1Min == Op0Max-1)        // A >u C -> A == C+1 if max(a)-1 == C
6078           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6079                               AddOne(CI));
6080
6081         // (x >u 2147483647) -> (x <s 0)  -> true if sign bit set
6082         if (CI->isMaxValue(true))
6083           return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
6084                               Constant::getNullValue(Op0->getType()));
6085       }
6086       break;
6087     case ICmpInst::ICMP_SLT:
6088       if (Op0Max.slt(Op1Min))          // A <s B -> true if max(A) < min(C)
6089         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6090       if (Op0Min.sge(Op1Max))          // A <s B -> false if min(A) >= max(C)
6091         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6092       if (Op1Min == Op0Max)            // A <s B -> A != B if max(A) == min(B)
6093         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6094       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
6095         if (Op1Max == Op0Min+1)        // A <s C -> A == C-1 if min(A)+1 == C
6096           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6097                               SubOne(CI));
6098       }
6099       break;
6100     case ICmpInst::ICMP_SGT:
6101       if (Op0Min.sgt(Op1Max))          // A >s B -> true if min(A) > max(B)
6102         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6103       if (Op0Max.sle(Op1Min))          // A >s B -> false if max(A) <= min(B)
6104         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6105
6106       if (Op1Max == Op0Min)            // A >s B -> A != B if min(A) == max(B)
6107         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6108       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
6109         if (Op1Min == Op0Max-1)        // A >s C -> A == C+1 if max(A)-1 == C
6110           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6111                               AddOne(CI));
6112       }
6113       break;
6114     case ICmpInst::ICMP_SGE:
6115       assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
6116       if (Op0Min.sge(Op1Max))          // A >=s B -> true if min(A) >= max(B)
6117         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6118       if (Op0Max.slt(Op1Min))          // A >=s B -> false if max(A) < min(B)
6119         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6120       break;
6121     case ICmpInst::ICMP_SLE:
6122       assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
6123       if (Op0Max.sle(Op1Min))          // A <=s B -> true if max(A) <= min(B)
6124         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6125       if (Op0Min.sgt(Op1Max))          // A <=s B -> false if min(A) > max(B)
6126         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6127       break;
6128     case ICmpInst::ICMP_UGE:
6129       assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
6130       if (Op0Min.uge(Op1Max))          // A >=u B -> true if min(A) >= max(B)
6131         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6132       if (Op0Max.ult(Op1Min))          // A >=u B -> false if max(A) < min(B)
6133         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6134       break;
6135     case ICmpInst::ICMP_ULE:
6136       assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
6137       if (Op0Max.ule(Op1Min))          // A <=u B -> true if max(A) <= min(B)
6138         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
6139       if (Op0Min.ugt(Op1Max))          // A <=u B -> false if min(A) > max(B)
6140         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
6141       break;
6142     }
6143
6144     // Turn a signed comparison into an unsigned one if both operands
6145     // are known to have the same sign.
6146     if (I.isSignedPredicate() &&
6147         ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
6148          (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
6149       return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
6150   }
6151
6152   // Test if the ICmpInst instruction is used exclusively by a select as
6153   // part of a minimum or maximum operation. If so, refrain from doing
6154   // any other folding. This helps out other analyses which understand
6155   // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
6156   // and CodeGen. And in this case, at least one of the comparison
6157   // operands has at least one user besides the compare (the select),
6158   // which would often largely negate the benefit of folding anyway.
6159   if (I.hasOneUse())
6160     if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin()))
6161       if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
6162           (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
6163         return 0;
6164
6165   // See if we are doing a comparison between a constant and an instruction that
6166   // can be folded into the comparison.
6167   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
6168     // Since the RHS is a ConstantInt (CI), if the left hand side is an 
6169     // instruction, see if that instruction also has constants so that the 
6170     // instruction can be folded into the icmp 
6171     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
6172       if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
6173         return Res;
6174   }
6175
6176   // Handle icmp with constant (but not simple integer constant) RHS
6177   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
6178     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
6179       switch (LHSI->getOpcode()) {
6180       case Instruction::GetElementPtr:
6181         if (RHSC->isNullValue()) {
6182           // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
6183           bool isAllZeros = true;
6184           for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i)
6185             if (!isa<Constant>(LHSI->getOperand(i)) ||
6186                 !cast<Constant>(LHSI->getOperand(i))->isNullValue()) {
6187               isAllZeros = false;
6188               break;
6189             }
6190           if (isAllZeros)
6191             return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
6192                     Constant::getNullValue(LHSI->getOperand(0)->getType()));
6193         }
6194         break;
6195
6196       case Instruction::PHI:
6197         // Only fold icmp into the PHI if the phi and fcmp are in the same
6198         // block.  If in the same block, we're encouraging jump threading.  If
6199         // not, we are just pessimizing the code by making an i1 phi.
6200         if (LHSI->getParent() == I.getParent())
6201           if (Instruction *NV = FoldOpIntoPhi(I))
6202             return NV;
6203         break;
6204       case Instruction::Select: {
6205         // If either operand of the select is a constant, we can fold the
6206         // comparison into the select arms, which will cause one to be
6207         // constant folded and the select turned into a bitwise or.
6208         Value *Op1 = 0, *Op2 = 0;
6209         if (LHSI->hasOneUse()) {
6210           if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
6211             // Fold the known value into the constant operand.
6212             Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
6213             // Insert a new ICmp of the other select operand.
6214             Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2),
6215                                       RHSC, I.getName());
6216           } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
6217             // Fold the known value into the constant operand.
6218             Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
6219             // Insert a new ICmp of the other select operand.
6220             Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1),
6221                                       RHSC, I.getName());
6222           }
6223         }
6224
6225         if (Op1)
6226           return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
6227         break;
6228       }
6229       case Instruction::Malloc:
6230         // If we have (malloc != null), and if the malloc has a single use, we
6231         // can assume it is successful and remove the malloc.
6232         if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) {
6233           Worklist.Add(LHSI);
6234           return ReplaceInstUsesWith(I, ConstantInt::get(Type::getInt1Ty(*Context),
6235                                                          !I.isTrueWhenEqual()));
6236         }
6237         break;
6238       }
6239   }
6240
6241   // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
6242   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
6243     if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
6244       return NI;
6245   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
6246     if (Instruction *NI = FoldGEPICmp(GEP, Op0,
6247                            ICmpInst::getSwappedPredicate(I.getPredicate()), I))
6248       return NI;
6249
6250   // Test to see if the operands of the icmp are casted versions of other
6251   // values.  If the ptr->ptr cast can be stripped off both arguments, we do so
6252   // now.
6253   if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
6254     if (isa<PointerType>(Op0->getType()) && 
6255         (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { 
6256       // We keep moving the cast from the left operand over to the right
6257       // operand, where it can often be eliminated completely.
6258       Op0 = CI->getOperand(0);
6259
6260       // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
6261       // so eliminate it as well.
6262       if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
6263         Op1 = CI2->getOperand(0);
6264
6265       // If Op1 is a constant, we can fold the cast into the constant.
6266       if (Op0->getType() != Op1->getType()) {
6267         if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
6268           Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
6269         } else {
6270           // Otherwise, cast the RHS right before the icmp
6271           Op1 = Builder->CreateBitCast(Op1, Op0->getType());
6272         }
6273       }
6274       return new ICmpInst(I.getPredicate(), Op0, Op1);
6275     }
6276   }
6277   
6278   if (isa<CastInst>(Op0)) {
6279     // Handle the special case of: icmp (cast bool to X), <cst>
6280     // This comes up when you have code like
6281     //   int X = A < B;
6282     //   if (X) ...
6283     // For generality, we handle any zero-extension of any operand comparison
6284     // with a constant or another cast from the same type.
6285     if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1))
6286       if (Instruction *R = visitICmpInstWithCastAndCast(I))
6287         return R;
6288   }
6289   
6290   // See if it's the same type of instruction on the left and right.
6291   if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
6292     if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
6293       if (Op0I->getOpcode() == Op1I->getOpcode() && Op0I->hasOneUse() &&
6294           Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1)) {
6295         switch (Op0I->getOpcode()) {
6296         default: break;
6297         case Instruction::Add:
6298         case Instruction::Sub:
6299         case Instruction::Xor:
6300           if (I.isEquality())    // a+x icmp eq/ne b+x --> a icmp b
6301             return new ICmpInst(I.getPredicate(), Op0I->getOperand(0),
6302                                 Op1I->getOperand(0));
6303           // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
6304           if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
6305             if (CI->getValue().isSignBit()) {
6306               ICmpInst::Predicate Pred = I.isSignedPredicate()
6307                                              ? I.getUnsignedPredicate()
6308                                              : I.getSignedPredicate();
6309               return new ICmpInst(Pred, Op0I->getOperand(0),
6310                                   Op1I->getOperand(0));
6311             }
6312             
6313             if (CI->getValue().isMaxSignedValue()) {
6314               ICmpInst::Predicate Pred = I.isSignedPredicate()
6315                                              ? I.getUnsignedPredicate()
6316                                              : I.getSignedPredicate();
6317               Pred = I.getSwappedPredicate(Pred);
6318               return new ICmpInst(Pred, Op0I->getOperand(0),
6319                                   Op1I->getOperand(0));
6320             }
6321           }
6322           break;
6323         case Instruction::Mul:
6324           if (!I.isEquality())
6325             break;
6326
6327           if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
6328             // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
6329             // Mask = -1 >> count-trailing-zeros(Cst).
6330             if (!CI->isZero() && !CI->isOne()) {
6331               const APInt &AP = CI->getValue();
6332               ConstantInt *Mask = ConstantInt::get(*Context, 
6333                                       APInt::getLowBitsSet(AP.getBitWidth(),
6334                                                            AP.getBitWidth() -
6335                                                       AP.countTrailingZeros()));
6336               Value *And1 = Builder->CreateAnd(Op0I->getOperand(0), Mask);
6337               Value *And2 = Builder->CreateAnd(Op1I->getOperand(0), Mask);
6338               return new ICmpInst(I.getPredicate(), And1, And2);
6339             }
6340           }
6341           break;
6342         }
6343       }
6344     }
6345   }
6346   
6347   // ~x < ~y --> y < x
6348   { Value *A, *B;
6349     if (match(Op0, m_Not(m_Value(A))) &&
6350         match(Op1, m_Not(m_Value(B))))
6351       return new ICmpInst(I.getPredicate(), B, A);
6352   }
6353   
6354   if (I.isEquality()) {
6355     Value *A, *B, *C, *D;
6356     
6357     // -x == -y --> x == y
6358     if (match(Op0, m_Neg(m_Value(A))) &&
6359         match(Op1, m_Neg(m_Value(B))))
6360       return new ICmpInst(I.getPredicate(), A, B);
6361     
6362     if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
6363       if (A == Op1 || B == Op1) {    // (A^B) == A  ->  B == 0
6364         Value *OtherVal = A == Op1 ? B : A;
6365         return new ICmpInst(I.getPredicate(), OtherVal,
6366                             Constant::getNullValue(A->getType()));
6367       }
6368
6369       if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
6370         // A^c1 == C^c2 --> A == C^(c1^c2)
6371         ConstantInt *C1, *C2;
6372         if (match(B, m_ConstantInt(C1)) &&
6373             match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
6374           Constant *NC = 
6375                    ConstantInt::get(*Context, C1->getValue() ^ C2->getValue());
6376           Value *Xor = Builder->CreateXor(C, NC, "tmp");
6377           return new ICmpInst(I.getPredicate(), A, Xor);
6378         }
6379         
6380         // A^B == A^D -> B == D
6381         if (A == C) return new ICmpInst(I.getPredicate(), B, D);
6382         if (A == D) return new ICmpInst(I.getPredicate(), B, C);
6383         if (B == C) return new ICmpInst(I.getPredicate(), A, D);
6384         if (B == D) return new ICmpInst(I.getPredicate(), A, C);
6385       }
6386     }
6387     
6388     if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
6389         (A == Op0 || B == Op0)) {
6390       // A == (A^B)  ->  B == 0
6391       Value *OtherVal = A == Op0 ? B : A;
6392       return new ICmpInst(I.getPredicate(), OtherVal,
6393                           Constant::getNullValue(A->getType()));
6394     }
6395
6396     // (A-B) == A  ->  B == 0
6397     if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B))))
6398       return new ICmpInst(I.getPredicate(), B, 
6399                           Constant::getNullValue(B->getType()));
6400
6401     // A == (A-B)  ->  B == 0
6402     if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B))))
6403       return new ICmpInst(I.getPredicate(), B,
6404                           Constant::getNullValue(B->getType()));
6405     
6406     // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
6407     if (Op0->hasOneUse() && Op1->hasOneUse() &&
6408         match(Op0, m_And(m_Value(A), m_Value(B))) && 
6409         match(Op1, m_And(m_Value(C), m_Value(D)))) {
6410       Value *X = 0, *Y = 0, *Z = 0;
6411       
6412       if (A == C) {
6413         X = B; Y = D; Z = A;
6414       } else if (A == D) {
6415         X = B; Y = C; Z = A;
6416       } else if (B == C) {
6417         X = A; Y = D; Z = B;
6418       } else if (B == D) {
6419         X = A; Y = C; Z = B;
6420       }
6421       
6422       if (X) {   // Build (X^Y) & Z
6423         Op1 = Builder->CreateXor(X, Y, "tmp");
6424         Op1 = Builder->CreateAnd(Op1, Z, "tmp");
6425         I.setOperand(0, Op1);
6426         I.setOperand(1, Constant::getNullValue(Op1->getType()));
6427         return &I;
6428       }
6429     }
6430   }
6431   return Changed ? &I : 0;
6432 }
6433
6434
6435 /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
6436 /// and CmpRHS are both known to be integer constants.
6437 Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
6438                                           ConstantInt *DivRHS) {
6439   ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
6440   const APInt &CmpRHSV = CmpRHS->getValue();
6441   
6442   // FIXME: If the operand types don't match the type of the divide 
6443   // then don't attempt this transform. The code below doesn't have the
6444   // logic to deal with a signed divide and an unsigned compare (and
6445   // vice versa). This is because (x /s C1) <s C2  produces different 
6446   // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
6447   // (x /u C1) <u C2.  Simply casting the operands and result won't 
6448   // work. :(  The if statement below tests that condition and bails 
6449   // if it finds it. 
6450   bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
6451   if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate())
6452     return 0;
6453   if (DivRHS->isZero())
6454     return 0; // The ProdOV computation fails on divide by zero.
6455   if (DivIsSigned && DivRHS->isAllOnesValue())
6456     return 0; // The overflow computation also screws up here
6457   if (DivRHS->isOne())
6458     return 0; // Not worth bothering, and eliminates some funny cases
6459               // with INT_MIN.
6460
6461   // Compute Prod = CI * DivRHS. We are essentially solving an equation
6462   // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and 
6463   // C2 (CI). By solving for X we can turn this into a range check 
6464   // instead of computing a divide. 
6465   Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
6466
6467   // Determine if the product overflows by seeing if the product is
6468   // not equal to the divide. Make sure we do the same kind of divide
6469   // as in the LHS instruction that we're folding. 
6470   bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
6471                  ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
6472
6473   // Get the ICmp opcode
6474   ICmpInst::Predicate Pred = ICI.getPredicate();
6475
6476   // Figure out the interval that is being checked.  For example, a comparison
6477   // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). 
6478   // Compute this interval based on the constants involved and the signedness of
6479   // the compare/divide.  This computes a half-open interval, keeping track of
6480   // whether either value in the interval overflows.  After analysis each
6481   // overflow variable is set to 0 if it's corresponding bound variable is valid
6482   // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
6483   int LoOverflow = 0, HiOverflow = 0;
6484   Constant *LoBound = 0, *HiBound = 0;
6485   
6486   if (!DivIsSigned) {  // udiv
6487     // e.g. X/5 op 3  --> [15, 20)
6488     LoBound = Prod;
6489     HiOverflow = LoOverflow = ProdOV;
6490     if (!HiOverflow)
6491       HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, Context, false);
6492   } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
6493     if (CmpRHSV == 0) {       // (X / pos) op 0
6494       // Can't overflow.  e.g.  X/2 op 0 --> [-1, 2)
6495       LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
6496       HiBound = DivRHS;
6497     } else if (CmpRHSV.isStrictlyPositive()) {   // (X / pos) op pos
6498       LoBound = Prod;     // e.g.   X/5 op 3 --> [15, 20)
6499       HiOverflow = LoOverflow = ProdOV;
6500       if (!HiOverflow)
6501         HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, Context, true);
6502     } else {                       // (X / pos) op neg
6503       // e.g. X/5 op -3  --> [-15-4, -15+1) --> [-19, -14)
6504       HiBound = AddOne(Prod);
6505       LoOverflow = HiOverflow = ProdOV ? -1 : 0;
6506       if (!LoOverflow) {
6507         ConstantInt* DivNeg =
6508                          cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
6509         LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, Context,
6510                                      true) ? -1 : 0;
6511        }
6512     }
6513   } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0.
6514     if (CmpRHSV == 0) {       // (X / neg) op 0
6515       // e.g. X/-5 op 0  --> [-4, 5)
6516       LoBound = AddOne(DivRHS);
6517       HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
6518       if (HiBound == DivRHS) {     // -INTMIN = INTMIN
6519         HiOverflow = 1;            // [INTMIN+1, overflow)
6520         HiBound = 0;               // e.g. X/INTMIN = 0 --> X > INTMIN
6521       }
6522     } else if (CmpRHSV.isStrictlyPositive()) {   // (X / neg) op pos
6523       // e.g. X/-5 op 3  --> [-19, -14)
6524       HiBound = AddOne(Prod);
6525       HiOverflow = LoOverflow = ProdOV ? -1 : 0;
6526       if (!LoOverflow)
6527         LoOverflow = AddWithOverflow(LoBound, HiBound,
6528                                      DivRHS, Context, true) ? -1 : 0;
6529     } else {                       // (X / neg) op neg
6530       LoBound = Prod;       // e.g. X/-5 op -3  --> [15, 20)
6531       LoOverflow = HiOverflow = ProdOV;
6532       if (!HiOverflow)
6533         HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, Context, true);
6534     }
6535     
6536     // Dividing by a negative swaps the condition.  LT <-> GT
6537     Pred = ICmpInst::getSwappedPredicate(Pred);
6538   }
6539
6540   Value *X = DivI->getOperand(0);
6541   switch (Pred) {
6542   default: llvm_unreachable("Unhandled icmp opcode!");
6543   case ICmpInst::ICMP_EQ:
6544     if (LoOverflow && HiOverflow)
6545       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
6546     else if (HiOverflow)
6547       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
6548                           ICmpInst::ICMP_UGE, X, LoBound);
6549     else if (LoOverflow)
6550       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
6551                           ICmpInst::ICMP_ULT, X, HiBound);
6552     else
6553       return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI);
6554   case ICmpInst::ICMP_NE:
6555     if (LoOverflow && HiOverflow)
6556       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
6557     else if (HiOverflow)
6558       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
6559                           ICmpInst::ICMP_ULT, X, LoBound);
6560     else if (LoOverflow)
6561       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
6562                           ICmpInst::ICMP_UGE, X, HiBound);
6563     else
6564       return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI);
6565   case ICmpInst::ICMP_ULT:
6566   case ICmpInst::ICMP_SLT:
6567     if (LoOverflow == +1)   // Low bound is greater than input range.
6568       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
6569     if (LoOverflow == -1)   // Low bound is less than input range.
6570       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
6571     return new ICmpInst(Pred, X, LoBound);
6572   case ICmpInst::ICMP_UGT:
6573   case ICmpInst::ICMP_SGT:
6574     if (HiOverflow == +1)       // High bound greater than input range.
6575       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
6576     else if (HiOverflow == -1)  // High bound less than input range.
6577       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
6578     if (Pred == ICmpInst::ICMP_UGT)
6579       return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
6580     else
6581       return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
6582   }
6583 }
6584
6585
6586 /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
6587 ///
6588 Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
6589                                                           Instruction *LHSI,
6590                                                           ConstantInt *RHS) {
6591   const APInt &RHSV = RHS->getValue();
6592   
6593   switch (LHSI->getOpcode()) {
6594   case Instruction::Trunc:
6595     if (ICI.isEquality() && LHSI->hasOneUse()) {
6596       // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
6597       // of the high bits truncated out of x are known.
6598       unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
6599              SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
6600       APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits));
6601       APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
6602       ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne);
6603       
6604       // If all the high bits are known, we can do this xform.
6605       if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
6606         // Pull in the high bits from known-ones set.
6607         APInt NewRHS(RHS->getValue());
6608         NewRHS.zext(SrcBits);
6609         NewRHS |= KnownOne;
6610         return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
6611                             ConstantInt::get(*Context, NewRHS));
6612       }
6613     }
6614     break;
6615       
6616   case Instruction::Xor:         // (icmp pred (xor X, XorCST), CI)
6617     if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
6618       // If this is a comparison that tests the signbit (X < 0) or (x > -1),
6619       // fold the xor.
6620       if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
6621           (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
6622         Value *CompareVal = LHSI->getOperand(0);
6623         
6624         // If the sign bit of the XorCST is not set, there is no change to
6625         // the operation, just stop using the Xor.
6626         if (!XorCST->getValue().isNegative()) {
6627           ICI.setOperand(0, CompareVal);
6628           Worklist.Add(LHSI);
6629           return &ICI;
6630         }
6631         
6632         // Was the old condition true if the operand is positive?
6633         bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
6634         
6635         // If so, the new one isn't.
6636         isTrueIfPositive ^= true;
6637         
6638         if (isTrueIfPositive)
6639           return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal,
6640                               SubOne(RHS));
6641         else
6642           return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal,
6643                               AddOne(RHS));
6644       }
6645
6646       if (LHSI->hasOneUse()) {
6647         // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
6648         if (!ICI.isEquality() && XorCST->getValue().isSignBit()) {
6649           const APInt &SignBit = XorCST->getValue();
6650           ICmpInst::Predicate Pred = ICI.isSignedPredicate()
6651                                          ? ICI.getUnsignedPredicate()
6652                                          : ICI.getSignedPredicate();
6653           return new ICmpInst(Pred, LHSI->getOperand(0),
6654                               ConstantInt::get(*Context, RHSV ^ SignBit));
6655         }
6656
6657         // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
6658         if (!ICI.isEquality() && XorCST->getValue().isMaxSignedValue()) {
6659           const APInt &NotSignBit = XorCST->getValue();
6660           ICmpInst::Predicate Pred = ICI.isSignedPredicate()
6661                                          ? ICI.getUnsignedPredicate()
6662                                          : ICI.getSignedPredicate();
6663           Pred = ICI.getSwappedPredicate(Pred);
6664           return new ICmpInst(Pred, LHSI->getOperand(0),
6665                               ConstantInt::get(*Context, RHSV ^ NotSignBit));
6666         }
6667       }
6668     }
6669     break;
6670   case Instruction::And:         // (icmp pred (and X, AndCST), RHS)
6671     if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
6672         LHSI->getOperand(0)->hasOneUse()) {
6673       ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
6674       
6675       // If the LHS is an AND of a truncating cast, we can widen the
6676       // and/compare to be the input width without changing the value
6677       // produced, eliminating a cast.
6678       if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
6679         // We can do this transformation if either the AND constant does not
6680         // have its sign bit set or if it is an equality comparison. 
6681         // Extending a relational comparison when we're checking the sign
6682         // bit would not work.
6683         if (Cast->hasOneUse() &&
6684             (ICI.isEquality() ||
6685              (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) {
6686           uint32_t BitWidth = 
6687             cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth();
6688           APInt NewCST = AndCST->getValue();
6689           NewCST.zext(BitWidth);
6690           APInt NewCI = RHSV;
6691           NewCI.zext(BitWidth);
6692           Value *NewAnd = 
6693             Builder->CreateAnd(Cast->getOperand(0),
6694                            ConstantInt::get(*Context, NewCST), LHSI->getName());
6695           return new ICmpInst(ICI.getPredicate(), NewAnd,
6696                               ConstantInt::get(*Context, NewCI));
6697         }
6698       }
6699       
6700       // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
6701       // could exist), turn it into (X & (C2 << C1)) != (C3 << C1).  This
6702       // happens a LOT in code produced by the C front-end, for bitfield
6703       // access.
6704       BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
6705       if (Shift && !Shift->isShift())
6706         Shift = 0;
6707       
6708       ConstantInt *ShAmt;
6709       ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0;
6710       const Type *Ty = Shift ? Shift->getType() : 0;  // Type of the shift.
6711       const Type *AndTy = AndCST->getType();          // Type of the and.
6712       
6713       // We can fold this as long as we can't shift unknown bits
6714       // into the mask.  This can only happen with signed shift
6715       // rights, as they sign-extend.
6716       if (ShAmt) {
6717         bool CanFold = Shift->isLogicalShift();
6718         if (!CanFold) {
6719           // To test for the bad case of the signed shr, see if any
6720           // of the bits shifted in could be tested after the mask.
6721           uint32_t TyBits = Ty->getPrimitiveSizeInBits();
6722           int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits);
6723           
6724           uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
6725           if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & 
6726                AndCST->getValue()) == 0)
6727             CanFold = true;
6728         }
6729         
6730         if (CanFold) {
6731           Constant *NewCst;
6732           if (Shift->getOpcode() == Instruction::Shl)
6733             NewCst = ConstantExpr::getLShr(RHS, ShAmt);
6734           else
6735             NewCst = ConstantExpr::getShl(RHS, ShAmt);
6736           
6737           // Check to see if we are shifting out any of the bits being
6738           // compared.
6739           if (ConstantExpr::get(Shift->getOpcode(),
6740                                        NewCst, ShAmt) != RHS) {
6741             // If we shifted bits out, the fold is not going to work out.
6742             // As a special case, check to see if this means that the
6743             // result is always true or false now.
6744             if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
6745               return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
6746             if (ICI.getPredicate() == ICmpInst::ICMP_NE)
6747               return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
6748           } else {
6749             ICI.setOperand(1, NewCst);
6750             Constant *NewAndCST;
6751             if (Shift->getOpcode() == Instruction::Shl)
6752               NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
6753             else
6754               NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
6755             LHSI->setOperand(1, NewAndCST);
6756             LHSI->setOperand(0, Shift->getOperand(0));
6757             Worklist.Add(Shift); // Shift is dead.
6758             return &ICI;
6759           }
6760         }
6761       }
6762       
6763       // Turn ((X >> Y) & C) == 0  into  (X & (C << Y)) == 0.  The later is
6764       // preferable because it allows the C<<Y expression to be hoisted out
6765       // of a loop if Y is invariant and X is not.
6766       if (Shift && Shift->hasOneUse() && RHSV == 0 &&
6767           ICI.isEquality() && !Shift->isArithmeticShift() &&
6768           !isa<Constant>(Shift->getOperand(0))) {
6769         // Compute C << Y.
6770         Value *NS;
6771         if (Shift->getOpcode() == Instruction::LShr) {
6772           NS = Builder->CreateShl(AndCST, Shift->getOperand(1), "tmp");
6773         } else {
6774           // Insert a logical shift.
6775           NS = Builder->CreateLShr(AndCST, Shift->getOperand(1), "tmp");
6776         }
6777         
6778         // Compute X & (C << Y).
6779         Value *NewAnd = 
6780           Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
6781         
6782         ICI.setOperand(0, NewAnd);
6783         return &ICI;
6784       }
6785     }
6786     break;
6787     
6788   case Instruction::Shl: {       // (icmp pred (shl X, ShAmt), CI)
6789     ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
6790     if (!ShAmt) break;
6791     
6792     uint32_t TypeBits = RHSV.getBitWidth();
6793     
6794     // Check that the shift amount is in range.  If not, don't perform
6795     // undefined shifts.  When the shift is visited it will be
6796     // simplified.
6797     if (ShAmt->uge(TypeBits))
6798       break;
6799     
6800     if (ICI.isEquality()) {
6801       // If we are comparing against bits always shifted out, the
6802       // comparison cannot succeed.
6803       Constant *Comp =
6804         ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt),
6805                                                                  ShAmt);
6806       if (Comp != RHS) {// Comparing against a bit that we know is zero.
6807         bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6808         Constant *Cst = ConstantInt::get(Type::getInt1Ty(*Context), IsICMP_NE);
6809         return ReplaceInstUsesWith(ICI, Cst);
6810       }
6811       
6812       if (LHSI->hasOneUse()) {
6813         // Otherwise strength reduce the shift into an and.
6814         uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
6815         Constant *Mask =
6816           ConstantInt::get(*Context, APInt::getLowBitsSet(TypeBits, 
6817                                                        TypeBits-ShAmtVal));
6818         
6819         Value *And =
6820           Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask");
6821         return new ICmpInst(ICI.getPredicate(), And,
6822                             ConstantInt::get(*Context, RHSV.lshr(ShAmtVal)));
6823       }
6824     }
6825     
6826     // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
6827     bool TrueIfSigned = false;
6828     if (LHSI->hasOneUse() &&
6829         isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
6830       // (X << 31) <s 0  --> (X&1) != 0
6831       Constant *Mask = ConstantInt::get(*Context, APInt(TypeBits, 1) <<
6832                                            (TypeBits-ShAmt->getZExtValue()-1));
6833       Value *And =
6834         Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask");
6835       return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
6836                           And, Constant::getNullValue(And->getType()));
6837     }
6838     break;
6839   }
6840     
6841   case Instruction::LShr:         // (icmp pred (shr X, ShAmt), CI)
6842   case Instruction::AShr: {
6843     // Only handle equality comparisons of shift-by-constant.
6844     ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
6845     if (!ShAmt || !ICI.isEquality()) break;
6846
6847     // Check that the shift amount is in range.  If not, don't perform
6848     // undefined shifts.  When the shift is visited it will be
6849     // simplified.
6850     uint32_t TypeBits = RHSV.getBitWidth();
6851     if (ShAmt->uge(TypeBits))
6852       break;
6853     
6854     uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
6855       
6856     // If we are comparing against bits always shifted out, the
6857     // comparison cannot succeed.
6858     APInt Comp = RHSV << ShAmtVal;
6859     if (LHSI->getOpcode() == Instruction::LShr)
6860       Comp = Comp.lshr(ShAmtVal);
6861     else
6862       Comp = Comp.ashr(ShAmtVal);
6863     
6864     if (Comp != RHSV) { // Comparing against a bit that we know is zero.
6865       bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6866       Constant *Cst = ConstantInt::get(Type::getInt1Ty(*Context), IsICMP_NE);
6867       return ReplaceInstUsesWith(ICI, Cst);
6868     }
6869     
6870     // Otherwise, check to see if the bits shifted out are known to be zero.
6871     // If so, we can compare against the unshifted value:
6872     //  (X & 4) >> 1 == 2  --> (X & 4) == 4.
6873     if (LHSI->hasOneUse() &&
6874         MaskedValueIsZero(LHSI->getOperand(0), 
6875                           APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) {
6876       return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
6877                           ConstantExpr::getShl(RHS, ShAmt));
6878     }
6879       
6880     if (LHSI->hasOneUse()) {
6881       // Otherwise strength reduce the shift into an and.
6882       APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
6883       Constant *Mask = ConstantInt::get(*Context, Val);
6884       
6885       Value *And = Builder->CreateAnd(LHSI->getOperand(0),
6886                                       Mask, LHSI->getName()+".mask");
6887       return new ICmpInst(ICI.getPredicate(), And,
6888                           ConstantExpr::getShl(RHS, ShAmt));
6889     }
6890     break;
6891   }
6892     
6893   case Instruction::SDiv:
6894   case Instruction::UDiv:
6895     // Fold: icmp pred ([us]div X, C1), C2 -> range test
6896     // Fold this div into the comparison, producing a range check. 
6897     // Determine, based on the divide type, what the range is being 
6898     // checked.  If there is an overflow on the low or high side, remember 
6899     // it, otherwise compute the range [low, hi) bounding the new value.
6900     // See: InsertRangeTest above for the kinds of replacements possible.
6901     if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
6902       if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
6903                                           DivRHS))
6904         return R;
6905     break;
6906
6907   case Instruction::Add:
6908     // Fold: icmp pred (add, X, C1), C2
6909
6910     if (!ICI.isEquality()) {
6911       ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
6912       if (!LHSC) break;
6913       const APInt &LHSV = LHSC->getValue();
6914
6915       ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
6916                             .subtract(LHSV);
6917
6918       if (ICI.isSignedPredicate()) {
6919         if (CR.getLower().isSignBit()) {
6920           return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
6921                               ConstantInt::get(*Context, CR.getUpper()));
6922         } else if (CR.getUpper().isSignBit()) {
6923           return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
6924                               ConstantInt::get(*Context, CR.getLower()));
6925         }
6926       } else {
6927         if (CR.getLower().isMinValue()) {
6928           return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
6929                               ConstantInt::get(*Context, CR.getUpper()));
6930         } else if (CR.getUpper().isMinValue()) {
6931           return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
6932                               ConstantInt::get(*Context, CR.getLower()));
6933         }
6934       }
6935     }
6936     break;
6937   }
6938   
6939   // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
6940   if (ICI.isEquality()) {
6941     bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6942     
6943     // If the first operand is (add|sub|and|or|xor|rem) with a constant, and 
6944     // the second operand is a constant, simplify a bit.
6945     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
6946       switch (BO->getOpcode()) {
6947       case Instruction::SRem:
6948         // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
6949         if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
6950           const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
6951           if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) {
6952             Value *NewRem =
6953               Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
6954                                   BO->getName());
6955             return new ICmpInst(ICI.getPredicate(), NewRem,
6956                                 Constant::getNullValue(BO->getType()));
6957           }
6958         }
6959         break;
6960       case Instruction::Add:
6961         // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
6962         if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6963           if (BO->hasOneUse())
6964             return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
6965                                 ConstantExpr::getSub(RHS, BOp1C));
6966         } else if (RHSV == 0) {
6967           // Replace ((add A, B) != 0) with (A != -B) if A or B is
6968           // efficiently invertible, or if the add has just this one use.
6969           Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
6970           
6971           if (Value *NegVal = dyn_castNegVal(BOp1))
6972             return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
6973           else if (Value *NegVal = dyn_castNegVal(BOp0))
6974             return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
6975           else if (BO->hasOneUse()) {
6976             Value *Neg = Builder->CreateNeg(BOp1);
6977             Neg->takeName(BO);
6978             return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
6979           }
6980         }
6981         break;
6982       case Instruction::Xor:
6983         // For the xor case, we can xor two constants together, eliminating
6984         // the explicit xor.
6985         if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
6986           return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), 
6987                               ConstantExpr::getXor(RHS, BOC));
6988         
6989         // FALLTHROUGH
6990       case Instruction::Sub:
6991         // Replace (([sub|xor] A, B) != 0) with (A != B)
6992         if (RHSV == 0)
6993           return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
6994                               BO->getOperand(1));
6995         break;
6996         
6997       case Instruction::Or:
6998         // If bits are being or'd in that are not present in the constant we
6999         // are comparing against, then the comparison could never succeed!
7000         if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
7001           Constant *NotCI = ConstantExpr::getNot(RHS);
7002           if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
7003             return ReplaceInstUsesWith(ICI,
7004                                        ConstantInt::get(Type::getInt1Ty(*Context), 
7005                                        isICMP_NE));
7006         }
7007         break;
7008         
7009       case Instruction::And:
7010         if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
7011           // If bits are being compared against that are and'd out, then the
7012           // comparison can never succeed!
7013           if ((RHSV & ~BOC->getValue()) != 0)
7014             return ReplaceInstUsesWith(ICI,
7015                                        ConstantInt::get(Type::getInt1Ty(*Context),
7016                                        isICMP_NE));
7017           
7018           // If we have ((X & C) == C), turn it into ((X & C) != 0).
7019           if (RHS == BOC && RHSV.isPowerOf2())
7020             return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
7021                                 ICmpInst::ICMP_NE, LHSI,
7022                                 Constant::getNullValue(RHS->getType()));
7023           
7024           // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
7025           if (BOC->getValue().isSignBit()) {
7026             Value *X = BO->getOperand(0);
7027             Constant *Zero = Constant::getNullValue(X->getType());
7028             ICmpInst::Predicate pred = isICMP_NE ? 
7029               ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
7030             return new ICmpInst(pred, X, Zero);
7031           }
7032           
7033           // ((X & ~7) == 0) --> X < 8
7034           if (RHSV == 0 && isHighOnes(BOC)) {
7035             Value *X = BO->getOperand(0);
7036             Constant *NegX = ConstantExpr::getNeg(BOC);
7037             ICmpInst::Predicate pred = isICMP_NE ? 
7038               ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
7039             return new ICmpInst(pred, X, NegX);
7040           }
7041         }
7042       default: break;
7043       }
7044     } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
7045       // Handle icmp {eq|ne} <intrinsic>, intcst.
7046       if (II->getIntrinsicID() == Intrinsic::bswap) {
7047         Worklist.Add(II);
7048         ICI.setOperand(0, II->getOperand(1));
7049         ICI.setOperand(1, ConstantInt::get(*Context, RHSV.byteSwap()));
7050         return &ICI;
7051       }
7052     }
7053   }
7054   return 0;
7055 }
7056
7057 /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
7058 /// We only handle extending casts so far.
7059 ///
7060 Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
7061   const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
7062   Value *LHSCIOp        = LHSCI->getOperand(0);
7063   const Type *SrcTy     = LHSCIOp->getType();
7064   const Type *DestTy    = LHSCI->getType();
7065   Value *RHSCIOp;
7066
7067   // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the 
7068   // integer type is the same size as the pointer type.
7069   if (TD && LHSCI->getOpcode() == Instruction::PtrToInt &&
7070       TD->getPointerSizeInBits() ==
7071          cast<IntegerType>(DestTy)->getBitWidth()) {
7072     Value *RHSOp = 0;
7073     if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
7074       RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
7075     } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
7076       RHSOp = RHSC->getOperand(0);
7077       // If the pointer types don't match, insert a bitcast.
7078       if (LHSCIOp->getType() != RHSOp->getType())
7079         RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
7080     }
7081
7082     if (RHSOp)
7083       return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
7084   }
7085   
7086   // The code below only handles extension cast instructions, so far.
7087   // Enforce this.
7088   if (LHSCI->getOpcode() != Instruction::ZExt &&
7089       LHSCI->getOpcode() != Instruction::SExt)
7090     return 0;
7091
7092   bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
7093   bool isSignedCmp = ICI.isSignedPredicate();
7094
7095   if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
7096     // Not an extension from the same type?
7097     RHSCIOp = CI->getOperand(0);
7098     if (RHSCIOp->getType() != LHSCIOp->getType()) 
7099       return 0;
7100     
7101     // If the signedness of the two casts doesn't agree (i.e. one is a sext
7102     // and the other is a zext), then we can't handle this.
7103     if (CI->getOpcode() != LHSCI->getOpcode())
7104       return 0;
7105
7106     // Deal with equality cases early.
7107     if (ICI.isEquality())
7108       return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
7109
7110     // A signed comparison of sign extended values simplifies into a
7111     // signed comparison.
7112     if (isSignedCmp && isSignedExt)
7113       return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
7114
7115     // The other three cases all fold into an unsigned comparison.
7116     return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
7117   }
7118
7119   // If we aren't dealing with a constant on the RHS, exit early
7120   ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
7121   if (!CI)
7122     return 0;
7123
7124   // Compute the constant that would happen if we truncated to SrcTy then
7125   // reextended to DestTy.
7126   Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
7127   Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(),
7128                                                 Res1, DestTy);
7129
7130   // If the re-extended constant didn't change...
7131   if (Res2 == CI) {
7132     // Make sure that sign of the Cmp and the sign of the Cast are the same.
7133     // For example, we might have:
7134     //    %A = sext i16 %X to i32
7135     //    %B = icmp ugt i32 %A, 1330
7136     // It is incorrect to transform this into 
7137     //    %B = icmp ugt i16 %X, 1330
7138     // because %A may have negative value. 
7139     //
7140     // However, we allow this when the compare is EQ/NE, because they are
7141     // signless.
7142     if (isSignedExt == isSignedCmp || ICI.isEquality())
7143       return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
7144     return 0;
7145   }
7146
7147   // The re-extended constant changed so the constant cannot be represented 
7148   // in the shorter type. Consequently, we cannot emit a simple comparison.
7149
7150   // First, handle some easy cases. We know the result cannot be equal at this
7151   // point so handle the ICI.isEquality() cases
7152   if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
7153     return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
7154   if (ICI.getPredicate() == ICmpInst::ICMP_NE)
7155     return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
7156
7157   // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
7158   // should have been folded away previously and not enter in here.
7159   Value *Result;
7160   if (isSignedCmp) {
7161     // We're performing a signed comparison.
7162     if (cast<ConstantInt>(CI)->getValue().isNegative())
7163       Result = ConstantInt::getFalse(*Context);          // X < (small) --> false
7164     else
7165       Result = ConstantInt::getTrue(*Context);           // X < (large) --> true
7166   } else {
7167     // We're performing an unsigned comparison.
7168     if (isSignedExt) {
7169       // We're performing an unsigned comp with a sign extended value.
7170       // This is true if the input is >= 0. [aka >s -1]
7171       Constant *NegOne = Constant::getAllOnesValue(SrcTy);
7172       Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName());
7173     } else {
7174       // Unsigned extend & unsigned compare -> always true.
7175       Result = ConstantInt::getTrue(*Context);
7176     }
7177   }
7178
7179   // Finally, return the value computed.
7180   if (ICI.getPredicate() == ICmpInst::ICMP_ULT ||
7181       ICI.getPredicate() == ICmpInst::ICMP_SLT)
7182     return ReplaceInstUsesWith(ICI, Result);
7183
7184   assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || 
7185           ICI.getPredicate()==ICmpInst::ICMP_SGT) &&
7186          "ICmp should be folded!");
7187   if (Constant *CI = dyn_cast<Constant>(Result))
7188     return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
7189   return BinaryOperator::CreateNot(Result);
7190 }
7191
7192 Instruction *InstCombiner::visitShl(BinaryOperator &I) {
7193   return commonShiftTransforms(I);
7194 }
7195
7196 Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
7197   return commonShiftTransforms(I);
7198 }
7199
7200 Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
7201   if (Instruction *R = commonShiftTransforms(I))
7202     return R;
7203   
7204   Value *Op0 = I.getOperand(0);
7205   
7206   // ashr int -1, X = -1   (for any arithmetic shift rights of ~0)
7207   if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
7208     if (CSI->isAllOnesValue())
7209       return ReplaceInstUsesWith(I, CSI);
7210
7211   // See if we can turn a signed shr into an unsigned shr.
7212   if (MaskedValueIsZero(Op0,
7213                         APInt::getSignBit(I.getType()->getScalarSizeInBits())))
7214     return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
7215
7216   // Arithmetic shifting an all-sign-bit value is a no-op.
7217   unsigned NumSignBits = ComputeNumSignBits(Op0);
7218   if (NumSignBits == Op0->getType()->getScalarSizeInBits())
7219     return ReplaceInstUsesWith(I, Op0);
7220
7221   return 0;
7222 }
7223
7224 Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
7225   assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
7226   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7227
7228   // shl X, 0 == X and shr X, 0 == X
7229   // shl 0, X == 0 and shr 0, X == 0
7230   if (Op1 == Constant::getNullValue(Op1->getType()) ||
7231       Op0 == Constant::getNullValue(Op0->getType()))
7232     return ReplaceInstUsesWith(I, Op0);
7233   
7234   if (isa<UndefValue>(Op0)) {            
7235     if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
7236       return ReplaceInstUsesWith(I, Op0);
7237     else                                    // undef << X -> 0, undef >>u X -> 0
7238       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
7239   }
7240   if (isa<UndefValue>(Op1)) {
7241     if (I.getOpcode() == Instruction::AShr)  // X >>s undef -> X
7242       return ReplaceInstUsesWith(I, Op0);          
7243     else                                     // X << undef, X >>u undef -> 0
7244       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
7245   }
7246
7247   // See if we can fold away this shift.
7248   if (SimplifyDemandedInstructionBits(I))
7249     return &I;
7250
7251   // Try to fold constant and into select arguments.
7252   if (isa<Constant>(Op0))
7253     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
7254       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
7255         return R;
7256
7257   if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
7258     if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
7259       return Res;
7260   return 0;
7261 }
7262
7263 Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
7264                                                BinaryOperator &I) {
7265   bool isLeftShift = I.getOpcode() == Instruction::Shl;
7266
7267   // See if we can simplify any instructions used by the instruction whose sole 
7268   // purpose is to compute bits we don't care about.
7269   uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
7270   
7271   // shl i32 X, 32 = 0 and srl i8 Y, 9 = 0, ... just don't eliminate
7272   // a signed shift.
7273   //
7274   if (Op1->uge(TypeBits)) {
7275     if (I.getOpcode() != Instruction::AShr)
7276       return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
7277     else {
7278       I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
7279       return &I;
7280     }
7281   }
7282   
7283   // ((X*C1) << C2) == (X * (C1 << C2))
7284   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
7285     if (BO->getOpcode() == Instruction::Mul && isLeftShift)
7286       if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
7287         return BinaryOperator::CreateMul(BO->getOperand(0),
7288                                         ConstantExpr::getShl(BOOp, Op1));
7289   
7290   // Try to fold constant and into select arguments.
7291   if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
7292     if (Instruction *R = FoldOpIntoSelect(I, SI, this))
7293       return R;
7294   if (isa<PHINode>(Op0))
7295     if (Instruction *NV = FoldOpIntoPhi(I))
7296       return NV;
7297   
7298   // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
7299   if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
7300     Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
7301     // If 'shift2' is an ashr, we would have to get the sign bit into a funny
7302     // place.  Don't try to do this transformation in this case.  Also, we
7303     // require that the input operand is a shift-by-constant so that we have
7304     // confidence that the shifts will get folded together.  We could do this
7305     // xform in more cases, but it is unlikely to be profitable.
7306     if (TrOp && I.isLogicalShift() && TrOp->isShift() && 
7307         isa<ConstantInt>(TrOp->getOperand(1))) {
7308       // Okay, we'll do this xform.  Make the shift of shift.
7309       Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
7310       // (shift2 (shift1 & 0x00FF), c2)
7311       Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
7312
7313       // For logical shifts, the truncation has the effect of making the high
7314       // part of the register be zeros.  Emulate this by inserting an AND to
7315       // clear the top bits as needed.  This 'and' will usually be zapped by
7316       // other xforms later if dead.
7317       unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
7318       unsigned DstSize = TI->getType()->getScalarSizeInBits();
7319       APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
7320       
7321       // The mask we constructed says what the trunc would do if occurring
7322       // between the shifts.  We want to know the effect *after* the second
7323       // shift.  We know that it is a logical shift by a constant, so adjust the
7324       // mask as appropriate.
7325       if (I.getOpcode() == Instruction::Shl)
7326         MaskV <<= Op1->getZExtValue();
7327       else {
7328         assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
7329         MaskV = MaskV.lshr(Op1->getZExtValue());
7330       }
7331
7332       // shift1 & 0x00FF
7333       Value *And = Builder->CreateAnd(NSh, ConstantInt::get(*Context, MaskV),
7334                                       TI->getName());
7335
7336       // Return the value truncated to the interesting size.
7337       return new TruncInst(And, I.getType());
7338     }
7339   }
7340   
7341   if (Op0->hasOneUse()) {
7342     if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
7343       // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
7344       Value *V1, *V2;
7345       ConstantInt *CC;
7346       switch (Op0BO->getOpcode()) {
7347         default: break;
7348         case Instruction::Add:
7349         case Instruction::And:
7350         case Instruction::Or:
7351         case Instruction::Xor: {
7352           // These operators commute.
7353           // Turn (Y + (X >> C)) << C  ->  (X + (Y << C)) & (~0 << C)
7354           if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
7355               match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
7356                     m_Specific(Op1)))) {
7357             Value *YS =         // (Y << C)
7358               Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
7359             // (X + (Y << C))
7360             Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
7361                                             Op0BO->getOperand(1)->getName());
7362             uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
7363             return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
7364                        APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
7365           }
7366           
7367           // Turn (Y + ((X >> C) & CC)) << C  ->  ((X & (CC << C)) + (Y << C))
7368           Value *Op0BOOp1 = Op0BO->getOperand(1);
7369           if (isLeftShift && Op0BOOp1->hasOneUse() &&
7370               match(Op0BOOp1, 
7371                     m_And(m_Shr(m_Value(V1), m_Specific(Op1)),
7372                           m_ConstantInt(CC))) &&
7373               cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) {
7374             Value *YS =   // (Y << C)
7375               Builder->CreateShl(Op0BO->getOperand(0), Op1,
7376                                            Op0BO->getName());
7377             // X & (CC << C)
7378             Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
7379                                            V1->getName()+".mask");
7380             return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
7381           }
7382         }
7383           
7384         // FALL THROUGH.
7385         case Instruction::Sub: {
7386           // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
7387           if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
7388               match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
7389                     m_Specific(Op1)))) {
7390             Value *YS =  // (Y << C)
7391               Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
7392             // (X + (Y << C))
7393             Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
7394                                             Op0BO->getOperand(0)->getName());
7395             uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
7396             return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
7397                        APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
7398           }
7399           
7400           // Turn (((X >> C)&CC) + Y) << C  ->  (X + (Y << C)) & (CC << C)
7401           if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
7402               match(Op0BO->getOperand(0),
7403                     m_And(m_Shr(m_Value(V1), m_Value(V2)),
7404                           m_ConstantInt(CC))) && V2 == Op1 &&
7405               cast<BinaryOperator>(Op0BO->getOperand(0))
7406                   ->getOperand(0)->hasOneUse()) {
7407             Value *YS = // (Y << C)
7408               Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
7409             // X & (CC << C)
7410             Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
7411                                            V1->getName()+".mask");
7412             
7413             return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
7414           }
7415           
7416           break;
7417         }
7418       }
7419       
7420       
7421       // If the operand is an bitwise operator with a constant RHS, and the
7422       // shift is the only use, we can pull it out of the shift.
7423       if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
7424         bool isValid = true;     // Valid only for And, Or, Xor
7425         bool highBitSet = false; // Transform if high bit of constant set?
7426         
7427         switch (Op0BO->getOpcode()) {
7428           default: isValid = false; break;   // Do not perform transform!
7429           case Instruction::Add:
7430             isValid = isLeftShift;
7431             break;
7432           case Instruction::Or:
7433           case Instruction::Xor:
7434             highBitSet = false;
7435             break;
7436           case Instruction::And:
7437             highBitSet = true;
7438             break;
7439         }
7440         
7441         // If this is a signed shift right, and the high bit is modified
7442         // by the logical operation, do not perform the transformation.
7443         // The highBitSet boolean indicates the value of the high bit of
7444         // the constant which would cause it to be modified for this
7445         // operation.
7446         //
7447         if (isValid && I.getOpcode() == Instruction::AShr)
7448           isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
7449         
7450         if (isValid) {
7451           Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
7452           
7453           Value *NewShift =
7454             Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
7455           NewShift->takeName(Op0BO);
7456           
7457           return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
7458                                         NewRHS);
7459         }
7460       }
7461     }
7462   }
7463   
7464   // Find out if this is a shift of a shift by a constant.
7465   BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
7466   if (ShiftOp && !ShiftOp->isShift())
7467     ShiftOp = 0;
7468   
7469   if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
7470     ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
7471     uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
7472     uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits);
7473     assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
7474     if (ShiftAmt1 == 0) return 0;  // Will be simplified in the future.
7475     Value *X = ShiftOp->getOperand(0);
7476     
7477     uint32_t AmtSum = ShiftAmt1+ShiftAmt2;   // Fold into one big shift.
7478     
7479     const IntegerType *Ty = cast<IntegerType>(I.getType());
7480     
7481     // Check for (X << c1) << c2  and  (X >> c1) >> c2
7482     if (I.getOpcode() == ShiftOp->getOpcode()) {
7483       // If this is oversized composite shift, then unsigned shifts get 0, ashr
7484       // saturates.
7485       if (AmtSum >= TypeBits) {
7486         if (I.getOpcode() != Instruction::AShr)
7487           return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
7488         AmtSum = TypeBits-1;  // Saturate to 31 for i32 ashr.
7489       }
7490       
7491       return BinaryOperator::Create(I.getOpcode(), X,
7492                                     ConstantInt::get(Ty, AmtSum));
7493     }
7494     
7495     if (ShiftOp->getOpcode() == Instruction::LShr &&
7496         I.getOpcode() == Instruction::AShr) {
7497       if (AmtSum >= TypeBits)
7498         return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
7499       
7500       // ((X >>u C1) >>s C2) -> (X >>u (C1+C2))  since C1 != 0.
7501       return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
7502     }
7503     
7504     if (ShiftOp->getOpcode() == Instruction::AShr &&
7505         I.getOpcode() == Instruction::LShr) {
7506       // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
7507       if (AmtSum >= TypeBits)
7508         AmtSum = TypeBits-1;
7509       
7510       Value *Shift = Builder->CreateAShr(X, ConstantInt::get(Ty, AmtSum));
7511
7512       APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
7513       return BinaryOperator::CreateAnd(Shift, ConstantInt::get(*Context, Mask));
7514     }
7515     
7516     // Okay, if we get here, one shift must be left, and the other shift must be
7517     // right.  See if the amounts are equal.
7518     if (ShiftAmt1 == ShiftAmt2) {
7519       // If we have ((X >>? C) << C), turn this into X & (-1 << C).
7520       if (I.getOpcode() == Instruction::Shl) {
7521         APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
7522         return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
7523       }
7524       // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
7525       if (I.getOpcode() == Instruction::LShr) {
7526         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
7527         return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
7528       }
7529       // We can simplify ((X << C) >>s C) into a trunc + sext.
7530       // NOTE: we could do this for any C, but that would make 'unusual' integer
7531       // types.  For now, just stick to ones well-supported by the code
7532       // generators.
7533       const Type *SExtType = 0;
7534       switch (Ty->getBitWidth() - ShiftAmt1) {
7535       case 1  :
7536       case 8  :
7537       case 16 :
7538       case 32 :
7539       case 64 :
7540       case 128:
7541         SExtType = IntegerType::get(*Context, Ty->getBitWidth() - ShiftAmt1);
7542         break;
7543       default: break;
7544       }
7545       if (SExtType)
7546         return new SExtInst(Builder->CreateTrunc(X, SExtType, "sext"), Ty);
7547       // Otherwise, we can't handle it yet.
7548     } else if (ShiftAmt1 < ShiftAmt2) {
7549       uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
7550       
7551       // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2)
7552       if (I.getOpcode() == Instruction::Shl) {
7553         assert(ShiftOp->getOpcode() == Instruction::LShr ||
7554                ShiftOp->getOpcode() == Instruction::AShr);
7555         Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
7556         
7557         APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
7558         return BinaryOperator::CreateAnd(Shift,
7559                                          ConstantInt::get(*Context, Mask));
7560       }
7561       
7562       // (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2)
7563       if (I.getOpcode() == Instruction::LShr) {
7564         assert(ShiftOp->getOpcode() == Instruction::Shl);
7565         Value *Shift = Builder->CreateLShr(X, ConstantInt::get(Ty, ShiftDiff));
7566         
7567         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
7568         return BinaryOperator::CreateAnd(Shift,
7569                                          ConstantInt::get(*Context, Mask));
7570       }
7571       
7572       // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
7573     } else {
7574       assert(ShiftAmt2 < ShiftAmt1);
7575       uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
7576
7577       // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2)
7578       if (I.getOpcode() == Instruction::Shl) {
7579         assert(ShiftOp->getOpcode() == Instruction::LShr ||
7580                ShiftOp->getOpcode() == Instruction::AShr);
7581         Value *Shift = Builder->CreateBinOp(ShiftOp->getOpcode(), X,
7582                                             ConstantInt::get(Ty, ShiftDiff));
7583         
7584         APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
7585         return BinaryOperator::CreateAnd(Shift,
7586                                          ConstantInt::get(*Context, Mask));
7587       }
7588       
7589       // (X << C1) >>u C2  --> X << (C1-C2) & (-1 >> C2)
7590       if (I.getOpcode() == Instruction::LShr) {
7591         assert(ShiftOp->getOpcode() == Instruction::Shl);
7592         Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
7593         
7594         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
7595         return BinaryOperator::CreateAnd(Shift,
7596                                          ConstantInt::get(*Context, Mask));
7597       }
7598       
7599       // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
7600     }
7601   }
7602   return 0;
7603 }
7604
7605
7606 /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear
7607 /// expression.  If so, decompose it, returning some value X, such that Val is
7608 /// X*Scale+Offset.
7609 ///
7610 static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
7611                                         int &Offset, LLVMContext *Context) {
7612   assert(Val->getType() == Type::getInt32Ty(*Context) && 
7613          "Unexpected allocation size type!");
7614   if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
7615     Offset = CI->getZExtValue();
7616     Scale  = 0;
7617     return ConstantInt::get(Type::getInt32Ty(*Context), 0);
7618   } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
7619     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
7620       if (I->getOpcode() == Instruction::Shl) {
7621         // This is a value scaled by '1 << the shift amt'.
7622         Scale = 1U << RHS->getZExtValue();
7623         Offset = 0;
7624         return I->getOperand(0);
7625       } else if (I->getOpcode() == Instruction::Mul) {
7626         // This value is scaled by 'RHS'.
7627         Scale = RHS->getZExtValue();
7628         Offset = 0;
7629         return I->getOperand(0);
7630       } else if (I->getOpcode() == Instruction::Add) {
7631         // We have X+C.  Check to see if we really have (X*C2)+C1, 
7632         // where C1 is divisible by C2.
7633         unsigned SubScale;
7634         Value *SubVal = 
7635           DecomposeSimpleLinearExpr(I->getOperand(0), SubScale,
7636                                     Offset, Context);
7637         Offset += RHS->getZExtValue();
7638         Scale = SubScale;
7639         return SubVal;
7640       }
7641     }
7642   }
7643
7644   // Otherwise, we can't look past this.
7645   Scale = 1;
7646   Offset = 0;
7647   return Val;
7648 }
7649
7650
7651 /// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
7652 /// try to eliminate the cast by moving the type information into the alloc.
7653 Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
7654                                                    AllocationInst &AI) {
7655   const PointerType *PTy = cast<PointerType>(CI.getType());
7656   
7657   BuilderTy AllocaBuilder(*Builder);
7658   AllocaBuilder.SetInsertPoint(AI.getParent(), &AI);
7659   
7660   // Remove any uses of AI that are dead.
7661   assert(!CI.use_empty() && "Dead instructions should be removed earlier!");
7662   
7663   for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) {
7664     Instruction *User = cast<Instruction>(*UI++);
7665     if (isInstructionTriviallyDead(User)) {
7666       while (UI != E && *UI == User)
7667         ++UI; // If this instruction uses AI more than once, don't break UI.
7668       
7669       ++NumDeadInst;
7670       DEBUG(errs() << "IC: DCE: " << *User << '\n');
7671       EraseInstFromFunction(*User);
7672     }
7673   }
7674
7675   // This requires TargetData to get the alloca alignment and size information.
7676   if (!TD) return 0;
7677
7678   // Get the type really allocated and the type casted to.
7679   const Type *AllocElTy = AI.getAllocatedType();
7680   const Type *CastElTy = PTy->getElementType();
7681   if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
7682
7683   unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy);
7684   unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy);
7685   if (CastElTyAlign < AllocElTyAlign) return 0;
7686
7687   // If the allocation has multiple uses, only promote it if we are strictly
7688   // increasing the alignment of the resultant allocation.  If we keep it the
7689   // same, we open the door to infinite loops of various kinds.  (A reference
7690   // from a dbg.declare doesn't count as a use for this purpose.)
7691   if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) &&
7692       CastElTyAlign == AllocElTyAlign) return 0;
7693
7694   uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy);
7695   uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy);
7696   if (CastElTySize == 0 || AllocElTySize == 0) return 0;
7697
7698   // See if we can satisfy the modulus by pulling a scale out of the array
7699   // size argument.
7700   unsigned ArraySizeScale;
7701   int ArrayOffset;
7702   Value *NumElements = // See if the array size is a decomposable linear expr.
7703     DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale,
7704                               ArrayOffset, Context);
7705  
7706   // If we can now satisfy the modulus, by using a non-1 scale, we really can
7707   // do the xform.
7708   if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
7709       (AllocElTySize*ArrayOffset   ) % CastElTySize != 0) return 0;
7710
7711   unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
7712   Value *Amt = 0;
7713   if (Scale == 1) {
7714     Amt = NumElements;
7715   } else {
7716     Amt = ConstantInt::get(Type::getInt32Ty(*Context), Scale);
7717     // Insert before the alloca, not before the cast.
7718     Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp");
7719   }
7720   
7721   if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
7722     Value *Off = ConstantInt::get(Type::getInt32Ty(*Context), Offset, true);
7723     Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp");
7724   }
7725   
7726   AllocationInst *New;
7727   if (isa<MallocInst>(AI))
7728     New = AllocaBuilder.CreateMalloc(CastElTy, Amt);
7729   else
7730     New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
7731   New->setAlignment(AI.getAlignment());
7732   New->takeName(&AI);
7733   
7734   // If the allocation has one real use plus a dbg.declare, just remove the
7735   // declare.
7736   if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) {
7737     EraseInstFromFunction(*DI);
7738   }
7739   // If the allocation has multiple real uses, insert a cast and change all
7740   // things that used it to use the new cast.  This will also hack on CI, but it
7741   // will die soon.
7742   else if (!AI.hasOneUse()) {
7743     // New is the allocation instruction, pointer typed. AI is the original
7744     // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
7745     Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast");
7746     AI.replaceAllUsesWith(NewCast);
7747   }
7748   return ReplaceInstUsesWith(CI, New);
7749 }
7750
7751 /// CanEvaluateInDifferentType - Return true if we can take the specified value
7752 /// and return it as type Ty without inserting any new casts and without
7753 /// changing the computed value.  This is used by code that tries to decide
7754 /// whether promoting or shrinking integer operations to wider or smaller types
7755 /// will allow us to eliminate a truncate or extend.
7756 ///
7757 /// This is a truncation operation if Ty is smaller than V->getType(), or an
7758 /// extension operation if Ty is larger.
7759 ///
7760 /// If CastOpc is a truncation, then Ty will be a type smaller than V.  We
7761 /// should return true if trunc(V) can be computed by computing V in the smaller
7762 /// type.  If V is an instruction, then trunc(inst(x,y)) can be computed as
7763 /// inst(trunc(x),trunc(y)), which only makes sense if x and y can be
7764 /// efficiently truncated.
7765 ///
7766 /// If CastOpc is a sext or zext, we are asking if the low bits of the value can
7767 /// bit computed in a larger type, which is then and'd or sext_in_reg'd to get
7768 /// the final result.
7769 bool InstCombiner::CanEvaluateInDifferentType(Value *V, const Type *Ty,
7770                                               unsigned CastOpc,
7771                                               int &NumCastsRemoved){
7772   // We can always evaluate constants in another type.
7773   if (isa<Constant>(V))
7774     return true;
7775   
7776   Instruction *I = dyn_cast<Instruction>(V);
7777   if (!I) return false;
7778   
7779   const Type *OrigTy = V->getType();
7780   
7781   // If this is an extension or truncate, we can often eliminate it.
7782   if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) {
7783     // If this is a cast from the destination type, we can trivially eliminate
7784     // it, and this will remove a cast overall.
7785     if (I->getOperand(0)->getType() == Ty) {
7786       // If the first operand is itself a cast, and is eliminable, do not count
7787       // this as an eliminable cast.  We would prefer to eliminate those two
7788       // casts first.
7789       if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse())
7790         ++NumCastsRemoved;
7791       return true;
7792     }
7793   }
7794
7795   // We can't extend or shrink something that has multiple uses: doing so would
7796   // require duplicating the instruction in general, which isn't profitable.
7797   if (!I->hasOneUse()) return false;
7798
7799   unsigned Opc = I->getOpcode();
7800   switch (Opc) {
7801   case Instruction::Add:
7802   case Instruction::Sub:
7803   case Instruction::Mul:
7804   case Instruction::And:
7805   case Instruction::Or:
7806   case Instruction::Xor:
7807     // These operators can all arbitrarily be extended or truncated.
7808     return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7809                                       NumCastsRemoved) &&
7810            CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
7811                                       NumCastsRemoved);
7812
7813   case Instruction::UDiv:
7814   case Instruction::URem: {
7815     // UDiv and URem can be truncated if all the truncated bits are zero.
7816     uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
7817     uint32_t BitWidth = Ty->getScalarSizeInBits();
7818     if (BitWidth < OrigBitWidth) {
7819       APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth);
7820       if (MaskedValueIsZero(I->getOperand(0), Mask) &&
7821           MaskedValueIsZero(I->getOperand(1), Mask)) {
7822         return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7823                                           NumCastsRemoved) &&
7824                CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
7825                                           NumCastsRemoved);
7826       }
7827     }
7828     break;
7829   }
7830   case Instruction::Shl:
7831     // If we are truncating the result of this SHL, and if it's a shift of a
7832     // constant amount, we can always perform a SHL in a smaller type.
7833     if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
7834       uint32_t BitWidth = Ty->getScalarSizeInBits();
7835       if (BitWidth < OrigTy->getScalarSizeInBits() &&
7836           CI->getLimitedValue(BitWidth) < BitWidth)
7837         return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7838                                           NumCastsRemoved);
7839     }
7840     break;
7841   case Instruction::LShr:
7842     // If this is a truncate of a logical shr, we can truncate it to a smaller
7843     // lshr iff we know that the bits we would otherwise be shifting in are
7844     // already zeros.
7845     if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
7846       uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
7847       uint32_t BitWidth = Ty->getScalarSizeInBits();
7848       if (BitWidth < OrigBitWidth &&
7849           MaskedValueIsZero(I->getOperand(0),
7850             APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
7851           CI->getLimitedValue(BitWidth) < BitWidth) {
7852         return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
7853                                           NumCastsRemoved);
7854       }
7855     }
7856     break;
7857   case Instruction::ZExt:
7858   case Instruction::SExt:
7859   case Instruction::Trunc:
7860     // If this is the same kind of case as our original (e.g. zext+zext), we
7861     // can safely replace it.  Note that replacing it does not reduce the number
7862     // of casts in the input.
7863     if (Opc == CastOpc)
7864       return true;
7865
7866     // sext (zext ty1), ty2 -> zext ty2
7867     if (CastOpc == Instruction::SExt && Opc == Instruction::ZExt)
7868       return true;
7869     break;
7870   case Instruction::Select: {
7871     SelectInst *SI = cast<SelectInst>(I);
7872     return CanEvaluateInDifferentType(SI->getTrueValue(), Ty, CastOpc,
7873                                       NumCastsRemoved) &&
7874            CanEvaluateInDifferentType(SI->getFalseValue(), Ty, CastOpc,
7875                                       NumCastsRemoved);
7876   }
7877   case Instruction::PHI: {
7878     // We can change a phi if we can change all operands.
7879     PHINode *PN = cast<PHINode>(I);
7880     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
7881       if (!CanEvaluateInDifferentType(PN->getIncomingValue(i), Ty, CastOpc,
7882                                       NumCastsRemoved))
7883         return false;
7884     return true;
7885   }
7886   default:
7887     // TODO: Can handle more cases here.
7888     break;
7889   }
7890   
7891   return false;
7892 }
7893
7894 /// EvaluateInDifferentType - Given an expression that 
7895 /// CanEvaluateInDifferentType returns true for, actually insert the code to
7896 /// evaluate the expression.
7897 Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, 
7898                                              bool isSigned) {
7899   if (Constant *C = dyn_cast<Constant>(V))
7900     return ConstantExpr::getIntegerCast(C, Ty,
7901                                                isSigned /*Sext or ZExt*/);
7902
7903   // Otherwise, it must be an instruction.
7904   Instruction *I = cast<Instruction>(V);
7905   Instruction *Res = 0;
7906   unsigned Opc = I->getOpcode();
7907   switch (Opc) {
7908   case Instruction::Add:
7909   case Instruction::Sub:
7910   case Instruction::Mul:
7911   case Instruction::And:
7912   case Instruction::Or:
7913   case Instruction::Xor:
7914   case Instruction::AShr:
7915   case Instruction::LShr:
7916   case Instruction::Shl:
7917   case Instruction::UDiv:
7918   case Instruction::URem: {
7919     Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
7920     Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
7921     Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
7922     break;
7923   }    
7924   case Instruction::Trunc:
7925   case Instruction::ZExt:
7926   case Instruction::SExt:
7927     // If the source type of the cast is the type we're trying for then we can
7928     // just return the source.  There's no need to insert it because it is not
7929     // new.
7930     if (I->getOperand(0)->getType() == Ty)
7931       return I->getOperand(0);
7932     
7933     // Otherwise, must be the same type of cast, so just reinsert a new one.
7934     Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0),
7935                            Ty);
7936     break;
7937   case Instruction::Select: {
7938     Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
7939     Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
7940     Res = SelectInst::Create(I->getOperand(0), True, False);
7941     break;
7942   }
7943   case Instruction::PHI: {
7944     PHINode *OPN = cast<PHINode>(I);
7945     PHINode *NPN = PHINode::Create(Ty);
7946     for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
7947       Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
7948       NPN->addIncoming(V, OPN->getIncomingBlock(i));
7949     }
7950     Res = NPN;
7951     break;
7952   }
7953   default: 
7954     // TODO: Can handle more cases here.
7955     llvm_unreachable("Unreachable!");
7956     break;
7957   }
7958   
7959   Res->takeName(I);
7960   return InsertNewInstBefore(Res, *I);
7961 }
7962
7963 /// @brief Implement the transforms common to all CastInst visitors.
7964 Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
7965   Value *Src = CI.getOperand(0);
7966
7967   // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
7968   // eliminate it now.
7969   if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {   // A->B->C cast
7970     if (Instruction::CastOps opc = 
7971         isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
7972       // The first cast (CSrc) is eliminable so we need to fix up or replace
7973       // the second cast (CI). CSrc will then have a good chance of being dead.
7974       return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
7975     }
7976   }
7977
7978   // If we are casting a select then fold the cast into the select
7979   if (SelectInst *SI = dyn_cast<SelectInst>(Src))
7980     if (Instruction *NV = FoldOpIntoSelect(CI, SI, this))
7981       return NV;
7982
7983   // If we are casting a PHI then fold the cast into the PHI
7984   if (isa<PHINode>(Src))
7985     if (Instruction *NV = FoldOpIntoPhi(CI))
7986       return NV;
7987   
7988   return 0;
7989 }
7990
7991 /// FindElementAtOffset - Given a type and a constant offset, determine whether
7992 /// or not there is a sequence of GEP indices into the type that will land us at
7993 /// the specified offset.  If so, fill them into NewIndices and return the
7994 /// resultant element type, otherwise return null.
7995 static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset, 
7996                                        SmallVectorImpl<Value*> &NewIndices,
7997                                        const TargetData *TD,
7998                                        LLVMContext *Context) {
7999   if (!TD) return 0;
8000   if (!Ty->isSized()) return 0;
8001   
8002   // Start with the index over the outer type.  Note that the type size
8003   // might be zero (even if the offset isn't zero) if the indexed type
8004   // is something like [0 x {int, int}]
8005   const Type *IntPtrTy = TD->getIntPtrType(*Context);
8006   int64_t FirstIdx = 0;
8007   if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
8008     FirstIdx = Offset/TySize;
8009     Offset -= FirstIdx*TySize;
8010     
8011     // Handle hosts where % returns negative instead of values [0..TySize).
8012     if (Offset < 0) {
8013       --FirstIdx;
8014       Offset += TySize;
8015       assert(Offset >= 0);
8016     }
8017     assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
8018   }
8019   
8020   NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
8021     
8022   // Index into the types.  If we fail, set OrigBase to null.
8023   while (Offset) {
8024     // Indexing into tail padding between struct/array elements.
8025     if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty))
8026       return 0;
8027     
8028     if (const StructType *STy = dyn_cast<StructType>(Ty)) {
8029       const StructLayout *SL = TD->getStructLayout(STy);
8030       assert(Offset < (int64_t)SL->getSizeInBytes() &&
8031              "Offset must stay within the indexed type");
8032       
8033       unsigned Elt = SL->getElementContainingOffset(Offset);
8034       NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(*Context), Elt));
8035       
8036       Offset -= SL->getElementOffset(Elt);
8037       Ty = STy->getElementType(Elt);
8038     } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
8039       uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType());
8040       assert(EltSize && "Cannot index into a zero-sized array");
8041       NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
8042       Offset %= EltSize;
8043       Ty = AT->getElementType();
8044     } else {
8045       // Otherwise, we can't index into the middle of this atomic type, bail.
8046       return 0;
8047     }
8048   }
8049   
8050   return Ty;
8051 }
8052
8053 /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
8054 Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
8055   Value *Src = CI.getOperand(0);
8056   
8057   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
8058     // If casting the result of a getelementptr instruction with no offset, turn
8059     // this into a cast of the original pointer!
8060     if (GEP->hasAllZeroIndices()) {
8061       // Changing the cast operand is usually not a good idea but it is safe
8062       // here because the pointer operand is being replaced with another 
8063       // pointer operand so the opcode doesn't need to change.
8064       Worklist.Add(GEP);
8065       CI.setOperand(0, GEP->getOperand(0));
8066       return &CI;
8067     }
8068     
8069     // If the GEP has a single use, and the base pointer is a bitcast, and the
8070     // GEP computes a constant offset, see if we can convert these three
8071     // instructions into fewer.  This typically happens with unions and other
8072     // non-type-safe code.
8073     if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) {
8074       if (GEP->hasAllConstantIndices()) {
8075         // We are guaranteed to get a constant from EmitGEPOffset.
8076         ConstantInt *OffsetV =
8077                       cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this));
8078         int64_t Offset = OffsetV->getSExtValue();
8079         
8080         // Get the base pointer input of the bitcast, and the type it points to.
8081         Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0);
8082         const Type *GEPIdxTy =
8083           cast<PointerType>(OrigBase->getType())->getElementType();
8084         SmallVector<Value*, 8> NewIndices;
8085         if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices, TD, Context)) {
8086           // If we were able to index down into an element, create the GEP
8087           // and bitcast the result.  This eliminates one bitcast, potentially
8088           // two.
8089           Value *NGEP = Builder->CreateGEP(OrigBase, NewIndices.begin(),
8090                                            NewIndices.end());
8091           NGEP->takeName(GEP);
8092           if (isa<Instruction>(NGEP) && cast<GEPOperator>(GEP)->isInBounds())
8093             cast<GEPOperator>(NGEP)->setIsInBounds(true);
8094           
8095           if (isa<BitCastInst>(CI))
8096             return new BitCastInst(NGEP, CI.getType());
8097           assert(isa<PtrToIntInst>(CI));
8098           return new PtrToIntInst(NGEP, CI.getType());
8099         }
8100       }      
8101     }
8102   }
8103     
8104   return commonCastTransforms(CI);
8105 }
8106
8107 /// isSafeIntegerType - Return true if this is a basic integer type, not a crazy
8108 /// type like i42.  We don't want to introduce operations on random non-legal
8109 /// integer types where they don't already exist in the code.  In the future,
8110 /// we should consider making this based off target-data, so that 32-bit targets
8111 /// won't get i64 operations etc.
8112 static bool isSafeIntegerType(const Type *Ty) {
8113   switch (Ty->getPrimitiveSizeInBits()) {
8114   case 8:
8115   case 16:
8116   case 32:
8117   case 64:
8118     return true;
8119   default: 
8120     return false;
8121   }
8122 }
8123
8124 /// commonIntCastTransforms - This function implements the common transforms
8125 /// for trunc, zext, and sext.
8126 Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) {
8127   if (Instruction *Result = commonCastTransforms(CI))
8128     return Result;
8129
8130   Value *Src = CI.getOperand(0);
8131   const Type *SrcTy = Src->getType();
8132   const Type *DestTy = CI.getType();
8133   uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
8134   uint32_t DestBitSize = DestTy->getScalarSizeInBits();
8135
8136   // See if we can simplify any instructions used by the LHS whose sole 
8137   // purpose is to compute bits we don't care about.
8138   if (SimplifyDemandedInstructionBits(CI))
8139     return &CI;
8140
8141   // If the source isn't an instruction or has more than one use then we
8142   // can't do anything more. 
8143   Instruction *SrcI = dyn_cast<Instruction>(Src);
8144   if (!SrcI || !Src->hasOneUse())
8145     return 0;
8146
8147   // Attempt to propagate the cast into the instruction for int->int casts.
8148   int NumCastsRemoved = 0;
8149   // Only do this if the dest type is a simple type, don't convert the
8150   // expression tree to something weird like i93 unless the source is also
8151   // strange.
8152   if ((isSafeIntegerType(DestTy->getScalarType()) ||
8153        !isSafeIntegerType(SrcI->getType()->getScalarType())) &&
8154       CanEvaluateInDifferentType(SrcI, DestTy,
8155                                  CI.getOpcode(), NumCastsRemoved)) {
8156     // If this cast is a truncate, evaluting in a different type always
8157     // eliminates the cast, so it is always a win.  If this is a zero-extension,
8158     // we need to do an AND to maintain the clear top-part of the computation,
8159     // so we require that the input have eliminated at least one cast.  If this
8160     // is a sign extension, we insert two new casts (to do the extension) so we
8161     // require that two casts have been eliminated.
8162     bool DoXForm = false;
8163     bool JustReplace = false;
8164     switch (CI.getOpcode()) {
8165     default:
8166       // All the others use floating point so we shouldn't actually 
8167       // get here because of the check above.
8168       llvm_unreachable("Unknown cast type");
8169     case Instruction::Trunc:
8170       DoXForm = true;
8171       break;
8172     case Instruction::ZExt: {
8173       DoXForm = NumCastsRemoved >= 1;
8174       if (!DoXForm && 0) {
8175         // If it's unnecessary to issue an AND to clear the high bits, it's
8176         // always profitable to do this xform.
8177         Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, false);
8178         APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize));
8179         if (MaskedValueIsZero(TryRes, Mask))
8180           return ReplaceInstUsesWith(CI, TryRes);
8181         
8182         if (Instruction *TryI = dyn_cast<Instruction>(TryRes))
8183           if (TryI->use_empty())
8184             EraseInstFromFunction(*TryI);
8185       }
8186       break;
8187     }
8188     case Instruction::SExt: {
8189       DoXForm = NumCastsRemoved >= 2;
8190       if (!DoXForm && !isa<TruncInst>(SrcI) && 0) {
8191         // If we do not have to emit the truncate + sext pair, then it's always
8192         // profitable to do this xform.
8193         //
8194         // It's not safe to eliminate the trunc + sext pair if one of the
8195         // eliminated cast is a truncate. e.g.
8196         // t2 = trunc i32 t1 to i16
8197         // t3 = sext i16 t2 to i32
8198         // !=
8199         // i32 t1
8200         Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, true);
8201         unsigned NumSignBits = ComputeNumSignBits(TryRes);
8202         if (NumSignBits > (DestBitSize - SrcBitSize))
8203           return ReplaceInstUsesWith(CI, TryRes);
8204         
8205         if (Instruction *TryI = dyn_cast<Instruction>(TryRes))
8206           if (TryI->use_empty())
8207             EraseInstFromFunction(*TryI);
8208       }
8209       break;
8210     }
8211     }
8212     
8213     if (DoXForm) {
8214       DEBUG(errs() << "ICE: EvaluateInDifferentType converting expression type"
8215             " to avoid cast: " << CI);
8216       Value *Res = EvaluateInDifferentType(SrcI, DestTy, 
8217                                            CI.getOpcode() == Instruction::SExt);
8218       if (JustReplace)
8219         // Just replace this cast with the result.
8220         return ReplaceInstUsesWith(CI, Res);
8221
8222       assert(Res->getType() == DestTy);
8223       switch (CI.getOpcode()) {
8224       default: llvm_unreachable("Unknown cast type!");
8225       case Instruction::Trunc:
8226         // Just replace this cast with the result.
8227         return ReplaceInstUsesWith(CI, Res);
8228       case Instruction::ZExt: {
8229         assert(SrcBitSize < DestBitSize && "Not a zext?");
8230
8231         // If the high bits are already zero, just replace this cast with the
8232         // result.
8233         APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize));
8234         if (MaskedValueIsZero(Res, Mask))
8235           return ReplaceInstUsesWith(CI, Res);
8236
8237         // We need to emit an AND to clear the high bits.
8238         Constant *C = ConstantInt::get(*Context, 
8239                                  APInt::getLowBitsSet(DestBitSize, SrcBitSize));
8240         return BinaryOperator::CreateAnd(Res, C);
8241       }
8242       case Instruction::SExt: {
8243         // If the high bits are already filled with sign bit, just replace this
8244         // cast with the result.
8245         unsigned NumSignBits = ComputeNumSignBits(Res);
8246         if (NumSignBits > (DestBitSize - SrcBitSize))
8247           return ReplaceInstUsesWith(CI, Res);
8248
8249         // We need to emit a cast to truncate, then a cast to sext.
8250         return new SExtInst(Builder->CreateTrunc(Res, Src->getType()), DestTy);
8251       }
8252       }
8253     }
8254   }
8255   
8256   Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0;
8257   Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0;
8258
8259   switch (SrcI->getOpcode()) {
8260   case Instruction::Add:
8261   case Instruction::Mul:
8262   case Instruction::And:
8263   case Instruction::Or:
8264   case Instruction::Xor:
8265     // If we are discarding information, rewrite.
8266     if (DestBitSize < SrcBitSize && DestBitSize != 1) {
8267       // Don't insert two casts unless at least one can be eliminated.
8268       if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) ||
8269           !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) {
8270         Value *Op0c = Builder->CreateTrunc(Op0, DestTy, Op0->getName());
8271         Value *Op1c = Builder->CreateTrunc(Op1, DestTy, Op1->getName());
8272         return BinaryOperator::Create(
8273             cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c);
8274       }
8275     }
8276
8277     // cast (xor bool X, true) to int  --> xor (cast bool X to int), 1
8278     if (isa<ZExtInst>(CI) && SrcBitSize == 1 && 
8279         SrcI->getOpcode() == Instruction::Xor &&
8280         Op1 == ConstantInt::getTrue(*Context) &&
8281         (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) {
8282       Value *New = Builder->CreateZExt(Op0, DestTy, Op0->getName());
8283       return BinaryOperator::CreateXor(New,
8284                                       ConstantInt::get(CI.getType(), 1));
8285     }
8286     break;
8287
8288   case Instruction::Shl: {
8289     // Canonicalize trunc inside shl, if we can.
8290     ConstantInt *CI = dyn_cast<ConstantInt>(Op1);
8291     if (CI && DestBitSize < SrcBitSize &&
8292         CI->getLimitedValue(DestBitSize) < DestBitSize) {
8293       Value *Op0c = Builder->CreateTrunc(Op0, DestTy, Op0->getName());
8294       Value *Op1c = Builder->CreateTrunc(Op1, DestTy, Op1->getName());
8295       return BinaryOperator::CreateShl(Op0c, Op1c);
8296     }
8297     break;
8298   }
8299   }
8300   return 0;
8301 }
8302
8303 Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
8304   if (Instruction *Result = commonIntCastTransforms(CI))
8305     return Result;
8306   
8307   Value *Src = CI.getOperand(0);
8308   const Type *Ty = CI.getType();
8309   uint32_t DestBitWidth = Ty->getScalarSizeInBits();
8310   uint32_t SrcBitWidth = Src->getType()->getScalarSizeInBits();
8311
8312   // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0)
8313   if (DestBitWidth == 1) {
8314     Constant *One = ConstantInt::get(Src->getType(), 1);
8315     Src = Builder->CreateAnd(Src, One, "tmp");
8316     Value *Zero = Constant::getNullValue(Src->getType());
8317     return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
8318   }
8319
8320   // Optimize trunc(lshr(), c) to pull the shift through the truncate.
8321   ConstantInt *ShAmtV = 0;
8322   Value *ShiftOp = 0;
8323   if (Src->hasOneUse() &&
8324       match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)))) {
8325     uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
8326     
8327     // Get a mask for the bits shifting in.
8328     APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
8329     if (MaskedValueIsZero(ShiftOp, Mask)) {
8330       if (ShAmt >= DestBitWidth)        // All zeros.
8331         return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
8332       
8333       // Okay, we can shrink this.  Truncate the input, then return a new
8334       // shift.
8335       Value *V1 = Builder->CreateTrunc(ShiftOp, Ty, ShiftOp->getName());
8336       Value *V2 = ConstantExpr::getTrunc(ShAmtV, Ty);
8337       return BinaryOperator::CreateLShr(V1, V2);
8338     }
8339   }
8340   
8341   return 0;
8342 }
8343
8344 /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
8345 /// in order to eliminate the icmp.
8346 Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
8347                                              bool DoXform) {
8348   // If we are just checking for a icmp eq of a single bit and zext'ing it
8349   // to an integer, then shift the bit to the appropriate place and then
8350   // cast to integer to avoid the comparison.
8351   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
8352     const APInt &Op1CV = Op1C->getValue();
8353       
8354     // zext (x <s  0) to i32 --> x>>u31      true if signbit set.
8355     // zext (x >s -1) to i32 --> (x>>u31)^1  true if signbit clear.
8356     if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
8357         (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
8358       if (!DoXform) return ICI;
8359
8360       Value *In = ICI->getOperand(0);
8361       Value *Sh = ConstantInt::get(In->getType(),
8362                                    In->getType()->getScalarSizeInBits()-1);
8363       In = Builder->CreateLShr(In, Sh, In->getName()+".lobit");
8364       if (In->getType() != CI.getType())
8365         In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/, "tmp");
8366
8367       if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
8368         Constant *One = ConstantInt::get(In->getType(), 1);
8369         In = Builder->CreateXor(In, One, In->getName()+".not");
8370       }
8371
8372       return ReplaceInstUsesWith(CI, In);
8373     }
8374       
8375       
8376       
8377     // zext (X == 0) to i32 --> X^1      iff X has only the low bit set.
8378     // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
8379     // zext (X == 1) to i32 --> X        iff X has only the low bit set.
8380     // zext (X == 2) to i32 --> X>>1     iff X has only the 2nd bit set.
8381     // zext (X != 0) to i32 --> X        iff X has only the low bit set.
8382     // zext (X != 0) to i32 --> X>>1     iff X has only the 2nd bit set.
8383     // zext (X != 1) to i32 --> X^1      iff X has only the low bit set.
8384     // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
8385     if ((Op1CV == 0 || Op1CV.isPowerOf2()) && 
8386         // This only works for EQ and NE
8387         ICI->isEquality()) {
8388       // If Op1C some other power of two, convert:
8389       uint32_t BitWidth = Op1C->getType()->getBitWidth();
8390       APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
8391       APInt TypeMask(APInt::getAllOnesValue(BitWidth));
8392       ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne);
8393         
8394       APInt KnownZeroMask(~KnownZero);
8395       if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
8396         if (!DoXform) return ICI;
8397
8398         bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
8399         if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
8400           // (X&4) == 2 --> false
8401           // (X&4) != 2 --> true
8402           Constant *Res = ConstantInt::get(Type::getInt1Ty(*Context), isNE);
8403           Res = ConstantExpr::getZExt(Res, CI.getType());
8404           return ReplaceInstUsesWith(CI, Res);
8405         }
8406           
8407         uint32_t ShiftAmt = KnownZeroMask.logBase2();
8408         Value *In = ICI->getOperand(0);
8409         if (ShiftAmt) {
8410           // Perform a logical shr by shiftamt.
8411           // Insert the shift to put the result in the low bit.
8412           In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt),
8413                                    In->getName()+".lobit");
8414         }
8415           
8416         if ((Op1CV != 0) == isNE) { // Toggle the low bit.
8417           Constant *One = ConstantInt::get(In->getType(), 1);
8418           In = Builder->CreateXor(In, One, "tmp");
8419         }
8420           
8421         if (CI.getType() == In->getType())
8422           return ReplaceInstUsesWith(CI, In);
8423         else
8424           return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
8425       }
8426     }
8427   }
8428
8429   return 0;
8430 }
8431
8432 Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
8433   // If one of the common conversion will work ..
8434   if (Instruction *Result = commonIntCastTransforms(CI))
8435     return Result;
8436
8437   Value *Src = CI.getOperand(0);
8438
8439   // If this is a TRUNC followed by a ZEXT then we are dealing with integral
8440   // types and if the sizes are just right we can convert this into a logical
8441   // 'and' which will be much cheaper than the pair of casts.
8442   if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) {   // A->B->C cast
8443     // Get the sizes of the types involved.  We know that the intermediate type
8444     // will be smaller than A or C, but don't know the relation between A and C.
8445     Value *A = CSrc->getOperand(0);
8446     unsigned SrcSize = A->getType()->getScalarSizeInBits();
8447     unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
8448     unsigned DstSize = CI.getType()->getScalarSizeInBits();
8449     // If we're actually extending zero bits, then if
8450     // SrcSize <  DstSize: zext(a & mask)
8451     // SrcSize == DstSize: a & mask
8452     // SrcSize  > DstSize: trunc(a) & mask
8453     if (SrcSize < DstSize) {
8454       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
8455       Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
8456       Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask");
8457       return new ZExtInst(And, CI.getType());
8458     }
8459     
8460     if (SrcSize == DstSize) {
8461       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
8462       return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
8463                                                            AndValue));
8464     }
8465     if (SrcSize > DstSize) {
8466       Value *Trunc = Builder->CreateTrunc(A, CI.getType(), "tmp");
8467       APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
8468       return BinaryOperator::CreateAnd(Trunc, 
8469                                        ConstantInt::get(Trunc->getType(),
8470                                                                AndValue));
8471     }
8472   }
8473
8474   if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
8475     return transformZExtICmp(ICI, CI);
8476
8477   BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
8478   if (SrcI && SrcI->getOpcode() == Instruction::Or) {
8479     // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
8480     // of the (zext icmp) will be transformed.
8481     ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
8482     ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
8483     if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
8484         (transformZExtICmp(LHS, CI, false) ||
8485          transformZExtICmp(RHS, CI, false))) {
8486       Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName());
8487       Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName());
8488       return BinaryOperator::Create(Instruction::Or, LCast, RCast);
8489     }
8490   }
8491
8492   // zext(trunc(t) & C) -> (t & zext(C)).
8493   if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse())
8494     if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
8495       if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) {
8496         Value *TI0 = TI->getOperand(0);
8497         if (TI0->getType() == CI.getType())
8498           return
8499             BinaryOperator::CreateAnd(TI0,
8500                                 ConstantExpr::getZExt(C, CI.getType()));
8501       }
8502
8503   // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)).
8504   if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse())
8505     if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
8506       if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0)))
8507         if (And->getOpcode() == Instruction::And && And->hasOneUse() &&
8508             And->getOperand(1) == C)
8509           if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) {
8510             Value *TI0 = TI->getOperand(0);
8511             if (TI0->getType() == CI.getType()) {
8512               Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
8513               Value *NewAnd = Builder->CreateAnd(TI0, ZC, "tmp");
8514               return BinaryOperator::CreateXor(NewAnd, ZC);
8515             }
8516           }
8517
8518   return 0;
8519 }
8520
8521 Instruction *InstCombiner::visitSExt(SExtInst &CI) {
8522   if (Instruction *I = commonIntCastTransforms(CI))
8523     return I;
8524   
8525   Value *Src = CI.getOperand(0);
8526   
8527   // Canonicalize sign-extend from i1 to a select.
8528   if (Src->getType() == Type::getInt1Ty(*Context))
8529     return SelectInst::Create(Src,
8530                               Constant::getAllOnesValue(CI.getType()),
8531                               Constant::getNullValue(CI.getType()));
8532
8533   // See if the value being truncated is already sign extended.  If so, just
8534   // eliminate the trunc/sext pair.
8535   if (Operator::getOpcode(Src) == Instruction::Trunc) {
8536     Value *Op = cast<User>(Src)->getOperand(0);
8537     unsigned OpBits   = Op->getType()->getScalarSizeInBits();
8538     unsigned MidBits  = Src->getType()->getScalarSizeInBits();
8539     unsigned DestBits = CI.getType()->getScalarSizeInBits();
8540     unsigned NumSignBits = ComputeNumSignBits(Op);
8541
8542     if (OpBits == DestBits) {
8543       // Op is i32, Mid is i8, and Dest is i32.  If Op has more than 24 sign
8544       // bits, it is already ready.
8545       if (NumSignBits > DestBits-MidBits)
8546         return ReplaceInstUsesWith(CI, Op);
8547     } else if (OpBits < DestBits) {
8548       // Op is i32, Mid is i8, and Dest is i64.  If Op has more than 24 sign
8549       // bits, just sext from i32.
8550       if (NumSignBits > OpBits-MidBits)
8551         return new SExtInst(Op, CI.getType(), "tmp");
8552     } else {
8553       // Op is i64, Mid is i8, and Dest is i32.  If Op has more than 56 sign
8554       // bits, just truncate to i32.
8555       if (NumSignBits > OpBits-MidBits)
8556         return new TruncInst(Op, CI.getType(), "tmp");
8557     }
8558   }
8559
8560   // If the input is a shl/ashr pair of a same constant, then this is a sign
8561   // extension from a smaller value.  If we could trust arbitrary bitwidth
8562   // integers, we could turn this into a truncate to the smaller bit and then
8563   // use a sext for the whole extension.  Since we don't, look deeper and check
8564   // for a truncate.  If the source and dest are the same type, eliminate the
8565   // trunc and extend and just do shifts.  For example, turn:
8566   //   %a = trunc i32 %i to i8
8567   //   %b = shl i8 %a, 6
8568   //   %c = ashr i8 %b, 6
8569   //   %d = sext i8 %c to i32
8570   // into:
8571   //   %a = shl i32 %i, 30
8572   //   %d = ashr i32 %a, 30
8573   Value *A = 0;
8574   ConstantInt *BA = 0, *CA = 0;
8575   if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)),
8576                         m_ConstantInt(CA))) &&
8577       BA == CA && isa<TruncInst>(A)) {
8578     Value *I = cast<TruncInst>(A)->getOperand(0);
8579     if (I->getType() == CI.getType()) {
8580       unsigned MidSize = Src->getType()->getScalarSizeInBits();
8581       unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
8582       unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
8583       Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
8584       I = Builder->CreateShl(I, ShAmtV, CI.getName());
8585       return BinaryOperator::CreateAShr(I, ShAmtV);
8586     }
8587   }
8588   
8589   return 0;
8590 }
8591
8592 /// FitsInFPType - Return a Constant* for the specified FP constant if it fits
8593 /// in the specified FP type without changing its value.
8594 static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem,
8595                               LLVMContext *Context) {
8596   bool losesInfo;
8597   APFloat F = CFP->getValueAPF();
8598   (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
8599   if (!losesInfo)
8600     return ConstantFP::get(*Context, F);
8601   return 0;
8602 }
8603
8604 /// LookThroughFPExtensions - If this is an fp extension instruction, look
8605 /// through it until we get the source value.
8606 static Value *LookThroughFPExtensions(Value *V, LLVMContext *Context) {
8607   if (Instruction *I = dyn_cast<Instruction>(V))
8608     if (I->getOpcode() == Instruction::FPExt)
8609       return LookThroughFPExtensions(I->getOperand(0), Context);
8610   
8611   // If this value is a constant, return the constant in the smallest FP type
8612   // that can accurately represent it.  This allows us to turn
8613   // (float)((double)X+2.0) into x+2.0f.
8614   if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
8615     if (CFP->getType() == Type::getPPC_FP128Ty(*Context))
8616       return V;  // No constant folding of this.
8617     // See if the value can be truncated to float and then reextended.
8618     if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle, Context))
8619       return V;
8620     if (CFP->getType() == Type::getDoubleTy(*Context))
8621       return V;  // Won't shrink.
8622     if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble, Context))
8623       return V;
8624     // Don't try to shrink to various long double types.
8625   }
8626   
8627   return V;
8628 }
8629
8630 Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
8631   if (Instruction *I = commonCastTransforms(CI))
8632     return I;
8633   
8634   // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are
8635   // smaller than the destination type, we can eliminate the truncate by doing
8636   // the add as the smaller type.  This applies to fadd/fsub/fmul/fdiv as well as
8637   // many builtins (sqrt, etc).
8638   BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
8639   if (OpI && OpI->hasOneUse()) {
8640     switch (OpI->getOpcode()) {
8641     default: break;
8642     case Instruction::FAdd:
8643     case Instruction::FSub:
8644     case Instruction::FMul:
8645     case Instruction::FDiv:
8646     case Instruction::FRem:
8647       const Type *SrcTy = OpI->getType();
8648       Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0), Context);
8649       Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1), Context);
8650       if (LHSTrunc->getType() != SrcTy && 
8651           RHSTrunc->getType() != SrcTy) {
8652         unsigned DstSize = CI.getType()->getScalarSizeInBits();
8653         // If the source types were both smaller than the destination type of
8654         // the cast, do this xform.
8655         if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize &&
8656             RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) {
8657           LHSTrunc = Builder->CreateFPExt(LHSTrunc, CI.getType());
8658           RHSTrunc = Builder->CreateFPExt(RHSTrunc, CI.getType());
8659           return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
8660         }
8661       }
8662       break;  
8663     }
8664   }
8665   return 0;
8666 }
8667
8668 Instruction *InstCombiner::visitFPExt(CastInst &CI) {
8669   return commonCastTransforms(CI);
8670 }
8671
8672 Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
8673   Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
8674   if (OpI == 0)
8675     return commonCastTransforms(FI);
8676
8677   // fptoui(uitofp(X)) --> X
8678   // fptoui(sitofp(X)) --> X
8679   // This is safe if the intermediate type has enough bits in its mantissa to
8680   // accurately represent all values of X.  For example, do not do this with
8681   // i64->float->i64.  This is also safe for sitofp case, because any negative
8682   // 'X' value would cause an undefined result for the fptoui. 
8683   if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
8684       OpI->getOperand(0)->getType() == FI.getType() &&
8685       (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
8686                     OpI->getType()->getFPMantissaWidth())
8687     return ReplaceInstUsesWith(FI, OpI->getOperand(0));
8688
8689   return commonCastTransforms(FI);
8690 }
8691
8692 Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
8693   Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
8694   if (OpI == 0)
8695     return commonCastTransforms(FI);
8696   
8697   // fptosi(sitofp(X)) --> X
8698   // fptosi(uitofp(X)) --> X
8699   // This is safe if the intermediate type has enough bits in its mantissa to
8700   // accurately represent all values of X.  For example, do not do this with
8701   // i64->float->i64.  This is also safe for sitofp case, because any negative
8702   // 'X' value would cause an undefined result for the fptoui. 
8703   if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
8704       OpI->getOperand(0)->getType() == FI.getType() &&
8705       (int)FI.getType()->getScalarSizeInBits() <=
8706                     OpI->getType()->getFPMantissaWidth())
8707     return ReplaceInstUsesWith(FI, OpI->getOperand(0));
8708   
8709   return commonCastTransforms(FI);
8710 }
8711
8712 Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
8713   return commonCastTransforms(CI);
8714 }
8715
8716 Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
8717   return commonCastTransforms(CI);
8718 }
8719
8720 Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) {
8721   // If the destination integer type is smaller than the intptr_t type for
8722   // this target, do a ptrtoint to intptr_t then do a trunc.  This allows the
8723   // trunc to be exposed to other transforms.  Don't do this for extending
8724   // ptrtoint's, because we don't know if the target sign or zero extends its
8725   // pointers.
8726   if (TD &&
8727       CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
8728     Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
8729                                        TD->getIntPtrType(CI.getContext()),
8730                                        "tmp");
8731     return new TruncInst(P, CI.getType());
8732   }
8733   
8734   return commonPointerCastTransforms(CI);
8735 }
8736
8737 Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
8738   // If the source integer type is larger than the intptr_t type for
8739   // this target, do a trunc to the intptr_t type, then inttoptr of it.  This
8740   // allows the trunc to be exposed to other transforms.  Don't do this for
8741   // extending inttoptr's, because we don't know if the target sign or zero
8742   // extends to pointers.
8743   if (TD && CI.getOperand(0)->getType()->getScalarSizeInBits() >
8744       TD->getPointerSizeInBits()) {
8745     Value *P = Builder->CreateTrunc(CI.getOperand(0),
8746                                     TD->getIntPtrType(CI.getContext()), "tmp");
8747     return new IntToPtrInst(P, CI.getType());
8748   }
8749   
8750   if (Instruction *I = commonCastTransforms(CI))
8751     return I;
8752
8753   return 0;
8754 }
8755
8756 Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
8757   // If the operands are integer typed then apply the integer transforms,
8758   // otherwise just apply the common ones.
8759   Value *Src = CI.getOperand(0);
8760   const Type *SrcTy = Src->getType();
8761   const Type *DestTy = CI.getType();
8762
8763   if (isa<PointerType>(SrcTy)) {
8764     if (Instruction *I = commonPointerCastTransforms(CI))
8765       return I;
8766   } else {
8767     if (Instruction *Result = commonCastTransforms(CI))
8768       return Result;
8769   }
8770
8771
8772   // Get rid of casts from one type to the same type. These are useless and can
8773   // be replaced by the operand.
8774   if (DestTy == Src->getType())
8775     return ReplaceInstUsesWith(CI, Src);
8776
8777   if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
8778     const PointerType *SrcPTy = cast<PointerType>(SrcTy);
8779     const Type *DstElTy = DstPTy->getElementType();
8780     const Type *SrcElTy = SrcPTy->getElementType();
8781     
8782     // If the address spaces don't match, don't eliminate the bitcast, which is
8783     // required for changing types.
8784     if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
8785       return 0;
8786     
8787     // If we are casting a malloc or alloca to a pointer to a type of the same
8788     // size, rewrite the allocation instruction to allocate the "right" type.
8789     if (AllocationInst *AI = dyn_cast<AllocationInst>(Src))
8790       if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
8791         return V;
8792     
8793     // If the source and destination are pointers, and this cast is equivalent
8794     // to a getelementptr X, 0, 0, 0...  turn it into the appropriate gep.
8795     // This can enhance SROA and other transforms that want type-safe pointers.
8796     Constant *ZeroUInt = Constant::getNullValue(Type::getInt32Ty(*Context));
8797     unsigned NumZeros = 0;
8798     while (SrcElTy != DstElTy && 
8799            isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) &&
8800            SrcElTy->getNumContainedTypes() /* not "{}" */) {
8801       SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
8802       ++NumZeros;
8803     }
8804
8805     // If we found a path from the src to dest, create the getelementptr now.
8806     if (SrcElTy == DstElTy) {
8807       SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
8808       Instruction *GEP = GetElementPtrInst::Create(Src,
8809                                                    Idxs.begin(), Idxs.end(), "",
8810                                                    ((Instruction*) NULL));
8811       cast<GEPOperator>(GEP)->setIsInBounds(true);
8812       return GEP;
8813     }
8814   }
8815
8816   if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
8817     if (DestVTy->getNumElements() == 1) {
8818       if (!isa<VectorType>(SrcTy)) {
8819         Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType());
8820         return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
8821                             Constant::getNullValue(Type::getInt32Ty(*Context)));
8822       }
8823       // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
8824     }
8825   }
8826
8827   if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
8828     if (SrcVTy->getNumElements() == 1) {
8829       if (!isa<VectorType>(DestTy)) {
8830         Value *Elem = 
8831           Builder->CreateExtractElement(Src,
8832                             Constant::getNullValue(Type::getInt32Ty(*Context)));
8833         return CastInst::Create(Instruction::BitCast, Elem, DestTy);
8834       }
8835     }
8836   }
8837
8838   if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
8839     if (SVI->hasOneUse()) {
8840       // Okay, we have (bitconvert (shuffle ..)).  Check to see if this is
8841       // a bitconvert to a vector with the same # elts.
8842       if (isa<VectorType>(DestTy) && 
8843           cast<VectorType>(DestTy)->getNumElements() ==
8844                 SVI->getType()->getNumElements() &&
8845           SVI->getType()->getNumElements() ==
8846             cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) {
8847         CastInst *Tmp;
8848         // If either of the operands is a cast from CI.getType(), then
8849         // evaluating the shuffle in the casted destination's type will allow
8850         // us to eliminate at least one cast.
8851         if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && 
8852              Tmp->getOperand(0)->getType() == DestTy) ||
8853             ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && 
8854              Tmp->getOperand(0)->getType() == DestTy)) {
8855           Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
8856           Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
8857           // Return a new shuffle vector.  Use the same element ID's, as we
8858           // know the vector types match #elts.
8859           return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
8860         }
8861       }
8862     }
8863   }
8864   return 0;
8865 }
8866
8867 /// GetSelectFoldableOperands - We want to turn code that looks like this:
8868 ///   %C = or %A, %B
8869 ///   %D = select %cond, %C, %A
8870 /// into:
8871 ///   %C = select %cond, %B, 0
8872 ///   %D = or %A, %C
8873 ///
8874 /// Assuming that the specified instruction is an operand to the select, return
8875 /// a bitmask indicating which operands of this instruction are foldable if they
8876 /// equal the other incoming value of the select.
8877 ///
8878 static unsigned GetSelectFoldableOperands(Instruction *I) {
8879   switch (I->getOpcode()) {
8880   case Instruction::Add:
8881   case Instruction::Mul:
8882   case Instruction::And:
8883   case Instruction::Or:
8884   case Instruction::Xor:
8885     return 3;              // Can fold through either operand.
8886   case Instruction::Sub:   // Can only fold on the amount subtracted.
8887   case Instruction::Shl:   // Can only fold on the shift amount.
8888   case Instruction::LShr:
8889   case Instruction::AShr:
8890     return 1;
8891   default:
8892     return 0;              // Cannot fold
8893   }
8894 }
8895
8896 /// GetSelectFoldableConstant - For the same transformation as the previous
8897 /// function, return the identity constant that goes into the select.
8898 static Constant *GetSelectFoldableConstant(Instruction *I,
8899                                            LLVMContext *Context) {
8900   switch (I->getOpcode()) {
8901   default: llvm_unreachable("This cannot happen!");
8902   case Instruction::Add:
8903   case Instruction::Sub:
8904   case Instruction::Or:
8905   case Instruction::Xor:
8906   case Instruction::Shl:
8907   case Instruction::LShr:
8908   case Instruction::AShr:
8909     return Constant::getNullValue(I->getType());
8910   case Instruction::And:
8911     return Constant::getAllOnesValue(I->getType());
8912   case Instruction::Mul:
8913     return ConstantInt::get(I->getType(), 1);
8914   }
8915 }
8916
8917 /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
8918 /// have the same opcode and only one use each.  Try to simplify this.
8919 Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
8920                                           Instruction *FI) {
8921   if (TI->getNumOperands() == 1) {
8922     // If this is a non-volatile load or a cast from the same type,
8923     // merge.
8924     if (TI->isCast()) {
8925       if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType())
8926         return 0;
8927     } else {
8928       return 0;  // unknown unary op.
8929     }
8930
8931     // Fold this by inserting a select from the input values.
8932     SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0),
8933                                           FI->getOperand(0), SI.getName()+".v");
8934     InsertNewInstBefore(NewSI, SI);
8935     return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, 
8936                             TI->getType());
8937   }
8938
8939   // Only handle binary operators here.
8940   if (!isa<BinaryOperator>(TI))
8941     return 0;
8942
8943   // Figure out if the operations have any operands in common.
8944   Value *MatchOp, *OtherOpT, *OtherOpF;
8945   bool MatchIsOpZero;
8946   if (TI->getOperand(0) == FI->getOperand(0)) {
8947     MatchOp  = TI->getOperand(0);
8948     OtherOpT = TI->getOperand(1);
8949     OtherOpF = FI->getOperand(1);
8950     MatchIsOpZero = true;
8951   } else if (TI->getOperand(1) == FI->getOperand(1)) {
8952     MatchOp  = TI->getOperand(1);
8953     OtherOpT = TI->getOperand(0);
8954     OtherOpF = FI->getOperand(0);
8955     MatchIsOpZero = false;
8956   } else if (!TI->isCommutative()) {
8957     return 0;
8958   } else if (TI->getOperand(0) == FI->getOperand(1)) {
8959     MatchOp  = TI->getOperand(0);
8960     OtherOpT = TI->getOperand(1);
8961     OtherOpF = FI->getOperand(0);
8962     MatchIsOpZero = true;
8963   } else if (TI->getOperand(1) == FI->getOperand(0)) {
8964     MatchOp  = TI->getOperand(1);
8965     OtherOpT = TI->getOperand(0);
8966     OtherOpF = FI->getOperand(1);
8967     MatchIsOpZero = true;
8968   } else {
8969     return 0;
8970   }
8971
8972   // If we reach here, they do have operations in common.
8973   SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT,
8974                                          OtherOpF, SI.getName()+".v");
8975   InsertNewInstBefore(NewSI, SI);
8976
8977   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
8978     if (MatchIsOpZero)
8979       return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI);
8980     else
8981       return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp);
8982   }
8983   llvm_unreachable("Shouldn't get here");
8984   return 0;
8985 }
8986
8987 static bool isSelect01(Constant *C1, Constant *C2) {
8988   ConstantInt *C1I = dyn_cast<ConstantInt>(C1);
8989   if (!C1I)
8990     return false;
8991   ConstantInt *C2I = dyn_cast<ConstantInt>(C2);
8992   if (!C2I)
8993     return false;
8994   return (C1I->isZero() || C1I->isOne()) && (C2I->isZero() || C2I->isOne());
8995 }
8996
8997 /// FoldSelectIntoOp - Try fold the select into one of the operands to
8998 /// facilitate further optimization.
8999 Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
9000                                             Value *FalseVal) {
9001   // See the comment above GetSelectFoldableOperands for a description of the
9002   // transformation we are doing here.
9003   if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
9004     if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
9005         !isa<Constant>(FalseVal)) {
9006       if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
9007         unsigned OpToFold = 0;
9008         if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
9009           OpToFold = 1;
9010         } else  if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
9011           OpToFold = 2;
9012         }
9013
9014         if (OpToFold) {
9015           Constant *C = GetSelectFoldableConstant(TVI, Context);
9016           Value *OOp = TVI->getOperand(2-OpToFold);
9017           // Avoid creating select between 2 constants unless it's selecting
9018           // between 0 and 1.
9019           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
9020             Instruction *NewSel = SelectInst::Create(SI.getCondition(), OOp, C);
9021             InsertNewInstBefore(NewSel, SI);
9022             NewSel->takeName(TVI);
9023             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
9024               return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel);
9025             llvm_unreachable("Unknown instruction!!");
9026           }
9027         }
9028       }
9029     }
9030   }
9031
9032   if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
9033     if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
9034         !isa<Constant>(TrueVal)) {
9035       if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
9036         unsigned OpToFold = 0;
9037         if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
9038           OpToFold = 1;
9039         } else  if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
9040           OpToFold = 2;
9041         }
9042
9043         if (OpToFold) {
9044           Constant *C = GetSelectFoldableConstant(FVI, Context);
9045           Value *OOp = FVI->getOperand(2-OpToFold);
9046           // Avoid creating select between 2 constants unless it's selecting
9047           // between 0 and 1.
9048           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
9049             Instruction *NewSel = SelectInst::Create(SI.getCondition(), C, OOp);
9050             InsertNewInstBefore(NewSel, SI);
9051             NewSel->takeName(FVI);
9052             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
9053               return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel);
9054             llvm_unreachable("Unknown instruction!!");
9055           }
9056         }
9057       }
9058     }
9059   }
9060
9061   return 0;
9062 }
9063
9064 /// visitSelectInstWithICmp - Visit a SelectInst that has an
9065 /// ICmpInst as its first operand.
9066 ///
9067 Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
9068                                                    ICmpInst *ICI) {
9069   bool Changed = false;
9070   ICmpInst::Predicate Pred = ICI->getPredicate();
9071   Value *CmpLHS = ICI->getOperand(0);
9072   Value *CmpRHS = ICI->getOperand(1);
9073   Value *TrueVal = SI.getTrueValue();
9074   Value *FalseVal = SI.getFalseValue();
9075
9076   // Check cases where the comparison is with a constant that
9077   // can be adjusted to fit the min/max idiom. We may edit ICI in
9078   // place here, so make sure the select is the only user.
9079   if (ICI->hasOneUse())
9080     if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
9081       switch (Pred) {
9082       default: break;
9083       case ICmpInst::ICMP_ULT:
9084       case ICmpInst::ICMP_SLT: {
9085         // X < MIN ? T : F  -->  F
9086         if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
9087           return ReplaceInstUsesWith(SI, FalseVal);
9088         // X < C ? X : C-1  -->  X > C-1 ? C-1 : X
9089         Constant *AdjustedRHS = SubOne(CI);
9090         if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
9091             (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
9092           Pred = ICmpInst::getSwappedPredicate(Pred);
9093           CmpRHS = AdjustedRHS;
9094           std::swap(FalseVal, TrueVal);
9095           ICI->setPredicate(Pred);
9096           ICI->setOperand(1, CmpRHS);
9097           SI.setOperand(1, TrueVal);
9098           SI.setOperand(2, FalseVal);
9099           Changed = true;
9100         }
9101         break;
9102       }
9103       case ICmpInst::ICMP_UGT:
9104       case ICmpInst::ICMP_SGT: {
9105         // X > MAX ? T : F  -->  F
9106         if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
9107           return ReplaceInstUsesWith(SI, FalseVal);
9108         // X > C ? X : C+1  -->  X < C+1 ? C+1 : X
9109         Constant *AdjustedRHS = AddOne(CI);
9110         if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
9111             (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
9112           Pred = ICmpInst::getSwappedPredicate(Pred);
9113           CmpRHS = AdjustedRHS;
9114           std::swap(FalseVal, TrueVal);
9115           ICI->setPredicate(Pred);
9116           ICI->setOperand(1, CmpRHS);
9117           SI.setOperand(1, TrueVal);
9118           SI.setOperand(2, FalseVal);
9119           Changed = true;
9120         }
9121         break;
9122       }
9123       }
9124
9125       // (x <s 0) ? -1 : 0 -> ashr x, 31   -> all ones if signed
9126       // (x >s -1) ? -1 : 0 -> ashr x, 31  -> all ones if not signed
9127       CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
9128       if (match(TrueVal, m_ConstantInt<-1>()) &&
9129           match(FalseVal, m_ConstantInt<0>()))
9130         Pred = ICI->getPredicate();
9131       else if (match(TrueVal, m_ConstantInt<0>()) &&
9132                match(FalseVal, m_ConstantInt<-1>()))
9133         Pred = CmpInst::getInversePredicate(ICI->getPredicate());
9134       
9135       if (Pred != CmpInst::BAD_ICMP_PREDICATE) {
9136         // If we are just checking for a icmp eq of a single bit and zext'ing it
9137         // to an integer, then shift the bit to the appropriate place and then
9138         // cast to integer to avoid the comparison.
9139         const APInt &Op1CV = CI->getValue();
9140     
9141         // sext (x <s  0) to i32 --> x>>s31      true if signbit set.
9142         // sext (x >s -1) to i32 --> (x>>s31)^-1  true if signbit clear.
9143         if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) ||
9144             (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) {
9145           Value *In = ICI->getOperand(0);
9146           Value *Sh = ConstantInt::get(In->getType(),
9147                                        In->getType()->getScalarSizeInBits()-1);
9148           In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh,
9149                                                         In->getName()+".lobit"),
9150                                    *ICI);
9151           if (In->getType() != SI.getType())
9152             In = CastInst::CreateIntegerCast(In, SI.getType(),
9153                                              true/*SExt*/, "tmp", ICI);
9154     
9155           if (Pred == ICmpInst::ICMP_SGT)
9156             In = InsertNewInstBefore(BinaryOperator::CreateNot(In,
9157                                        In->getName()+".not"), *ICI);
9158     
9159           return ReplaceInstUsesWith(SI, In);
9160         }
9161       }
9162     }
9163
9164   if (CmpLHS == TrueVal && CmpRHS == FalseVal) {
9165     // Transform (X == Y) ? X : Y  -> Y
9166     if (Pred == ICmpInst::ICMP_EQ)
9167       return ReplaceInstUsesWith(SI, FalseVal);
9168     // Transform (X != Y) ? X : Y  -> X
9169     if (Pred == ICmpInst::ICMP_NE)
9170       return ReplaceInstUsesWith(SI, TrueVal);
9171     /// NOTE: if we wanted to, this is where to detect integer MIN/MAX
9172
9173   } else if (CmpLHS == FalseVal && CmpRHS == TrueVal) {
9174     // Transform (X == Y) ? Y : X  -> X
9175     if (Pred == ICmpInst::ICMP_EQ)
9176       return ReplaceInstUsesWith(SI, FalseVal);
9177     // Transform (X != Y) ? Y : X  -> Y
9178     if (Pred == ICmpInst::ICMP_NE)
9179       return ReplaceInstUsesWith(SI, TrueVal);
9180     /// NOTE: if we wanted to, this is where to detect integer MIN/MAX
9181   }
9182
9183   /// NOTE: if we wanted to, this is where to detect integer ABS
9184
9185   return Changed ? &SI : 0;
9186 }
9187
9188 Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
9189   Value *CondVal = SI.getCondition();
9190   Value *TrueVal = SI.getTrueValue();
9191   Value *FalseVal = SI.getFalseValue();
9192
9193   // select true, X, Y  -> X
9194   // select false, X, Y -> Y
9195   if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal))
9196     return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal);
9197
9198   // select C, X, X -> X
9199   if (TrueVal == FalseVal)
9200     return ReplaceInstUsesWith(SI, TrueVal);
9201
9202   if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X
9203     return ReplaceInstUsesWith(SI, FalseVal);
9204   if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X
9205     return ReplaceInstUsesWith(SI, TrueVal);
9206   if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y
9207     if (isa<Constant>(TrueVal))
9208       return ReplaceInstUsesWith(SI, TrueVal);
9209     else
9210       return ReplaceInstUsesWith(SI, FalseVal);
9211   }
9212
9213   if (SI.getType() == Type::getInt1Ty(*Context)) {
9214     if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
9215       if (C->getZExtValue()) {
9216         // Change: A = select B, true, C --> A = or B, C
9217         return BinaryOperator::CreateOr(CondVal, FalseVal);
9218       } else {
9219         // Change: A = select B, false, C --> A = and !B, C
9220         Value *NotCond =
9221           InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
9222                                              "not."+CondVal->getName()), SI);
9223         return BinaryOperator::CreateAnd(NotCond, FalseVal);
9224       }
9225     } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
9226       if (C->getZExtValue() == false) {
9227         // Change: A = select B, C, false --> A = and B, C
9228         return BinaryOperator::CreateAnd(CondVal, TrueVal);
9229       } else {
9230         // Change: A = select B, C, true --> A = or !B, C
9231         Value *NotCond =
9232           InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
9233                                              "not."+CondVal->getName()), SI);
9234         return BinaryOperator::CreateOr(NotCond, TrueVal);
9235       }
9236     }
9237     
9238     // select a, b, a  -> a&b
9239     // select a, a, b  -> a|b
9240     if (CondVal == TrueVal)
9241       return BinaryOperator::CreateOr(CondVal, FalseVal);
9242     else if (CondVal == FalseVal)
9243       return BinaryOperator::CreateAnd(CondVal, TrueVal);
9244   }
9245
9246   // Selecting between two integer constants?
9247   if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
9248     if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
9249       // select C, 1, 0 -> zext C to int
9250       if (FalseValC->isZero() && TrueValC->getValue() == 1) {
9251         return CastInst::Create(Instruction::ZExt, CondVal, SI.getType());
9252       } else if (TrueValC->isZero() && FalseValC->getValue() == 1) {
9253         // select C, 0, 1 -> zext !C to int
9254         Value *NotCond =
9255           InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
9256                                                "not."+CondVal->getName()), SI);
9257         return CastInst::Create(Instruction::ZExt, NotCond, SI.getType());
9258       }
9259
9260       if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) {
9261         // If one of the constants is zero (we know they can't both be) and we
9262         // have an icmp instruction with zero, and we have an 'and' with the
9263         // non-constant value, eliminate this whole mess.  This corresponds to
9264         // cases like this: ((X & 27) ? 27 : 0)
9265         if (TrueValC->isZero() || FalseValC->isZero())
9266           if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) &&
9267               cast<Constant>(IC->getOperand(1))->isNullValue())
9268             if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0)))
9269               if (ICA->getOpcode() == Instruction::And &&
9270                   isa<ConstantInt>(ICA->getOperand(1)) &&
9271                   (ICA->getOperand(1) == TrueValC ||
9272                    ICA->getOperand(1) == FalseValC) &&
9273                   isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) {
9274                 // Okay, now we know that everything is set up, we just don't
9275                 // know whether we have a icmp_ne or icmp_eq and whether the 
9276                 // true or false val is the zero.
9277                 bool ShouldNotVal = !TrueValC->isZero();
9278                 ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
9279                 Value *V = ICA;
9280                 if (ShouldNotVal)
9281                   V = InsertNewInstBefore(BinaryOperator::Create(
9282                                   Instruction::Xor, V, ICA->getOperand(1)), SI);
9283                 return ReplaceInstUsesWith(SI, V);
9284               }
9285       }
9286     }
9287
9288   // See if we are selecting two values based on a comparison of the two values.
9289   if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
9290     if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
9291       // Transform (X == Y) ? X : Y  -> Y
9292       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
9293         // This is not safe in general for floating point:  
9294         // consider X== -0, Y== +0.
9295         // It becomes safe if either operand is a nonzero constant.
9296         ConstantFP *CFPt, *CFPf;
9297         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
9298               !CFPt->getValueAPF().isZero()) ||
9299             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
9300              !CFPf->getValueAPF().isZero()))
9301         return ReplaceInstUsesWith(SI, FalseVal);
9302       }
9303       // Transform (X != Y) ? X : Y  -> X
9304       if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
9305         return ReplaceInstUsesWith(SI, TrueVal);
9306       // NOTE: if we wanted to, this is where to detect MIN/MAX
9307
9308     } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
9309       // Transform (X == Y) ? Y : X  -> X
9310       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
9311         // This is not safe in general for floating point:  
9312         // consider X== -0, Y== +0.
9313         // It becomes safe if either operand is a nonzero constant.
9314         ConstantFP *CFPt, *CFPf;
9315         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
9316               !CFPt->getValueAPF().isZero()) ||
9317             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
9318              !CFPf->getValueAPF().isZero()))
9319           return ReplaceInstUsesWith(SI, FalseVal);
9320       }
9321       // Transform (X != Y) ? Y : X  -> Y
9322       if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
9323         return ReplaceInstUsesWith(SI, TrueVal);
9324       // NOTE: if we wanted to, this is where to detect MIN/MAX
9325     }
9326     // NOTE: if we wanted to, this is where to detect ABS
9327   }
9328
9329   // See if we are selecting two values based on a comparison of the two values.
9330   if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
9331     if (Instruction *Result = visitSelectInstWithICmp(SI, ICI))
9332       return Result;
9333
9334   if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
9335     if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
9336       if (TI->hasOneUse() && FI->hasOneUse()) {
9337         Instruction *AddOp = 0, *SubOp = 0;
9338
9339         // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
9340         if (TI->getOpcode() == FI->getOpcode())
9341           if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
9342             return IV;
9343
9344         // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))).  This is
9345         // even legal for FP.
9346         if ((TI->getOpcode() == Instruction::Sub &&
9347              FI->getOpcode() == Instruction::Add) ||
9348             (TI->getOpcode() == Instruction::FSub &&
9349              FI->getOpcode() == Instruction::FAdd)) {
9350           AddOp = FI; SubOp = TI;
9351         } else if ((FI->getOpcode() == Instruction::Sub &&
9352                     TI->getOpcode() == Instruction::Add) ||
9353                    (FI->getOpcode() == Instruction::FSub &&
9354                     TI->getOpcode() == Instruction::FAdd)) {
9355           AddOp = TI; SubOp = FI;
9356         }
9357
9358         if (AddOp) {
9359           Value *OtherAddOp = 0;
9360           if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
9361             OtherAddOp = AddOp->getOperand(1);
9362           } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
9363             OtherAddOp = AddOp->getOperand(0);
9364           }
9365
9366           if (OtherAddOp) {
9367             // So at this point we know we have (Y -> OtherAddOp):
9368             //        select C, (add X, Y), (sub X, Z)
9369             Value *NegVal;  // Compute -Z
9370             if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) {
9371               NegVal = ConstantExpr::getNeg(C);
9372             } else {
9373               NegVal = InsertNewInstBefore(
9374                     BinaryOperator::CreateNeg(SubOp->getOperand(1),
9375                                               "tmp"), SI);
9376             }
9377
9378             Value *NewTrueOp = OtherAddOp;
9379             Value *NewFalseOp = NegVal;
9380             if (AddOp != TI)
9381               std::swap(NewTrueOp, NewFalseOp);
9382             Instruction *NewSel =
9383               SelectInst::Create(CondVal, NewTrueOp,
9384                                  NewFalseOp, SI.getName() + ".p");
9385
9386             NewSel = InsertNewInstBefore(NewSel, SI);
9387             return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
9388           }
9389         }
9390       }
9391
9392   // See if we can fold the select into one of our operands.
9393   if (SI.getType()->isInteger()) {
9394     Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal);
9395     if (FoldI)
9396       return FoldI;
9397   }
9398
9399   if (BinaryOperator::isNot(CondVal)) {
9400     SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
9401     SI.setOperand(1, FalseVal);
9402     SI.setOperand(2, TrueVal);
9403     return &SI;
9404   }
9405
9406   return 0;
9407 }
9408
9409 /// EnforceKnownAlignment - If the specified pointer points to an object that
9410 /// we control, modify the object's alignment to PrefAlign. This isn't
9411 /// often possible though. If alignment is important, a more reliable approach
9412 /// is to simply align all global variables and allocation instructions to
9413 /// their preferred alignment from the beginning.
9414 ///
9415 static unsigned EnforceKnownAlignment(Value *V,
9416                                       unsigned Align, unsigned PrefAlign) {
9417
9418   User *U = dyn_cast<User>(V);
9419   if (!U) return Align;
9420
9421   switch (Operator::getOpcode(U)) {
9422   default: break;
9423   case Instruction::BitCast:
9424     return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
9425   case Instruction::GetElementPtr: {
9426     // If all indexes are zero, it is just the alignment of the base pointer.
9427     bool AllZeroOperands = true;
9428     for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
9429       if (!isa<Constant>(*i) ||
9430           !cast<Constant>(*i)->isNullValue()) {
9431         AllZeroOperands = false;
9432         break;
9433       }
9434
9435     if (AllZeroOperands) {
9436       // Treat this like a bitcast.
9437       return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
9438     }
9439     break;
9440   }
9441   }
9442
9443   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
9444     // If there is a large requested alignment and we can, bump up the alignment
9445     // of the global.
9446     if (!GV->isDeclaration()) {
9447       if (GV->getAlignment() >= PrefAlign)
9448         Align = GV->getAlignment();
9449       else {
9450         GV->setAlignment(PrefAlign);
9451         Align = PrefAlign;
9452       }
9453     }
9454   } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
9455     // If there is a requested alignment and if this is an alloca, round up.  We
9456     // don't do this for malloc, because some systems can't respect the request.
9457     if (isa<AllocaInst>(AI)) {
9458       if (AI->getAlignment() >= PrefAlign)
9459         Align = AI->getAlignment();
9460       else {
9461         AI->setAlignment(PrefAlign);
9462         Align = PrefAlign;
9463       }
9464     }
9465   }
9466
9467   return Align;
9468 }
9469
9470 /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that
9471 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
9472 /// and it is more than the alignment of the ultimate object, see if we can
9473 /// increase the alignment of the ultimate object, making this check succeed.
9474 unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V,
9475                                                   unsigned PrefAlign) {
9476   unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) :
9477                       sizeof(PrefAlign) * CHAR_BIT;
9478   APInt Mask = APInt::getAllOnesValue(BitWidth);
9479   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
9480   ComputeMaskedBits(V, Mask, KnownZero, KnownOne);
9481   unsigned TrailZ = KnownZero.countTrailingOnes();
9482   unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
9483
9484   if (PrefAlign > Align)
9485     Align = EnforceKnownAlignment(V, Align, PrefAlign);
9486   
9487     // We don't need to make any adjustment.
9488   return Align;
9489 }
9490
9491 Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
9492   unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
9493   unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
9494   unsigned MinAlign = std::min(DstAlign, SrcAlign);
9495   unsigned CopyAlign = MI->getAlignment();
9496
9497   if (CopyAlign < MinAlign) {
9498     MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), 
9499                                              MinAlign, false));
9500     return MI;
9501   }
9502   
9503   // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
9504   // load/store.
9505   ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3));
9506   if (MemOpLength == 0) return 0;
9507   
9508   // Source and destination pointer types are always "i8*" for intrinsic.  See
9509   // if the size is something we can handle with a single primitive load/store.
9510   // A single load+store correctly handles overlapping memory in the memmove
9511   // case.
9512   unsigned Size = MemOpLength->getZExtValue();
9513   if (Size == 0) return MI;  // Delete this mem transfer.
9514   
9515   if (Size > 8 || (Size&(Size-1)))
9516     return 0;  // If not 1/2/4/8 bytes, exit.
9517   
9518   // Use an integer load+store unless we can find something better.
9519   Type *NewPtrTy =
9520                 PointerType::getUnqual(IntegerType::get(*Context, Size<<3));
9521   
9522   // Memcpy forces the use of i8* for the source and destination.  That means
9523   // that if you're using memcpy to move one double around, you'll get a cast
9524   // from double* to i8*.  We'd much rather use a double load+store rather than
9525   // an i64 load+store, here because this improves the odds that the source or
9526   // dest address will be promotable.  See if we can find a better type than the
9527   // integer datatype.
9528   if (Value *Op = getBitCastOperand(MI->getOperand(1))) {
9529     const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType();
9530     if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
9531       // The SrcETy might be something like {{{double}}} or [1 x double].  Rip
9532       // down through these levels if so.
9533       while (!SrcETy->isSingleValueType()) {
9534         if (const StructType *STy = dyn_cast<StructType>(SrcETy)) {
9535           if (STy->getNumElements() == 1)
9536             SrcETy = STy->getElementType(0);
9537           else
9538             break;
9539         } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
9540           if (ATy->getNumElements() == 1)
9541             SrcETy = ATy->getElementType();
9542           else
9543             break;
9544         } else
9545           break;
9546       }
9547       
9548       if (SrcETy->isSingleValueType())
9549         NewPtrTy = PointerType::getUnqual(SrcETy);
9550     }
9551   }
9552   
9553   
9554   // If the memcpy/memmove provides better alignment info than we can
9555   // infer, use it.
9556   SrcAlign = std::max(SrcAlign, CopyAlign);
9557   DstAlign = std::max(DstAlign, CopyAlign);
9558   
9559   Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewPtrTy);
9560   Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewPtrTy);
9561   Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign);
9562   InsertNewInstBefore(L, *MI);
9563   InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
9564
9565   // Set the size of the copy to 0, it will be deleted on the next iteration.
9566   MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
9567   return MI;
9568 }
9569
9570 Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
9571   unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
9572   if (MI->getAlignment() < Alignment) {
9573     MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
9574                                              Alignment, false));
9575     return MI;
9576   }
9577   
9578   // Extract the length and alignment and fill if they are constant.
9579   ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
9580   ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
9581   if (!LenC || !FillC || FillC->getType() != Type::getInt8Ty(*Context))
9582     return 0;
9583   uint64_t Len = LenC->getZExtValue();
9584   Alignment = MI->getAlignment();
9585   
9586   // If the length is zero, this is a no-op
9587   if (Len == 0) return MI; // memset(d,c,0,a) -> noop
9588   
9589   // memset(s,c,n) -> store s, c (for n=1,2,4,8)
9590   if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
9591     const Type *ITy = IntegerType::get(*Context, Len*8);  // n=1 -> i8.
9592     
9593     Value *Dest = MI->getDest();
9594     Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy));
9595
9596     // Alignment 0 is identity for alignment 1 for memset, but not store.
9597     if (Alignment == 0) Alignment = 1;
9598     
9599     // Extract the fill value and store.
9600     uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
9601     InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill),
9602                                       Dest, false, Alignment), *MI);
9603     
9604     // Set the size of the copy to 0, it will be deleted on the next iteration.
9605     MI->setLength(Constant::getNullValue(LenC->getType()));
9606     return MI;
9607   }
9608
9609   return 0;
9610 }
9611
9612
9613 /// visitCallInst - CallInst simplification.  This mostly only handles folding 
9614 /// of intrinsic instructions.  For normal calls, it allows visitCallSite to do
9615 /// the heavy lifting.
9616 ///
9617 Instruction *InstCombiner::visitCallInst(CallInst &CI) {
9618   // If the caller function is nounwind, mark the call as nounwind, even if the
9619   // callee isn't.
9620   if (CI.getParent()->getParent()->doesNotThrow() &&
9621       !CI.doesNotThrow()) {
9622     CI.setDoesNotThrow();
9623     return &CI;
9624   }
9625   
9626   IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
9627   if (!II) return visitCallSite(&CI);
9628   
9629   // Intrinsics cannot occur in an invoke, so handle them here instead of in
9630   // visitCallSite.
9631   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
9632     bool Changed = false;
9633
9634     // memmove/cpy/set of zero bytes is a noop.
9635     if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
9636       if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
9637
9638       if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
9639         if (CI->getZExtValue() == 1) {
9640           // Replace the instruction with just byte operations.  We would
9641           // transform other cases to loads/stores, but we don't know if
9642           // alignment is sufficient.
9643         }
9644     }
9645
9646     // If we have a memmove and the source operation is a constant global,
9647     // then the source and dest pointers can't alias, so we can change this
9648     // into a call to memcpy.
9649     if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
9650       if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
9651         if (GVSrc->isConstant()) {
9652           Module *M = CI.getParent()->getParent()->getParent();
9653           Intrinsic::ID MemCpyID = Intrinsic::memcpy;
9654           const Type *Tys[1];
9655           Tys[0] = CI.getOperand(3)->getType();
9656           CI.setOperand(0, 
9657                         Intrinsic::getDeclaration(M, MemCpyID, Tys, 1));
9658           Changed = true;
9659         }
9660
9661       // memmove(x,x,size) -> noop.
9662       if (MMI->getSource() == MMI->getDest())
9663         return EraseInstFromFunction(CI);
9664     }
9665
9666     // If we can determine a pointer alignment that is bigger than currently
9667     // set, update the alignment.
9668     if (isa<MemTransferInst>(MI)) {
9669       if (Instruction *I = SimplifyMemTransfer(MI))
9670         return I;
9671     } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
9672       if (Instruction *I = SimplifyMemSet(MSI))
9673         return I;
9674     }
9675           
9676     if (Changed) return II;
9677   }
9678   
9679   switch (II->getIntrinsicID()) {
9680   default: break;
9681   case Intrinsic::bswap:
9682     // bswap(bswap(x)) -> x
9683     if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1)))
9684       if (Operand->getIntrinsicID() == Intrinsic::bswap)
9685         return ReplaceInstUsesWith(CI, Operand->getOperand(1));
9686     break;
9687   case Intrinsic::ppc_altivec_lvx:
9688   case Intrinsic::ppc_altivec_lvxl:
9689   case Intrinsic::x86_sse_loadu_ps:
9690   case Intrinsic::x86_sse2_loadu_pd:
9691   case Intrinsic::x86_sse2_loadu_dq:
9692     // Turn PPC lvx     -> load if the pointer is known aligned.
9693     // Turn X86 loadups -> load if the pointer is known aligned.
9694     if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
9695       Value *Ptr = Builder->CreateBitCast(II->getOperand(1),
9696                                          PointerType::getUnqual(II->getType()));
9697       return new LoadInst(Ptr);
9698     }
9699     break;
9700   case Intrinsic::ppc_altivec_stvx:
9701   case Intrinsic::ppc_altivec_stvxl:
9702     // Turn stvx -> store if the pointer is known aligned.
9703     if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
9704       const Type *OpPtrTy = 
9705         PointerType::getUnqual(II->getOperand(1)->getType());
9706       Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy);
9707       return new StoreInst(II->getOperand(1), Ptr);
9708     }
9709     break;
9710   case Intrinsic::x86_sse_storeu_ps:
9711   case Intrinsic::x86_sse2_storeu_pd:
9712   case Intrinsic::x86_sse2_storeu_dq:
9713     // Turn X86 storeu -> store if the pointer is known aligned.
9714     if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
9715       const Type *OpPtrTy = 
9716         PointerType::getUnqual(II->getOperand(2)->getType());
9717       Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
9718       return new StoreInst(II->getOperand(2), Ptr);
9719     }
9720     break;
9721     
9722   case Intrinsic::x86_sse_cvttss2si: {
9723     // These intrinsics only demands the 0th element of its input vector.  If
9724     // we can simplify the input based on that, do so now.
9725     unsigned VWidth =
9726       cast<VectorType>(II->getOperand(1)->getType())->getNumElements();
9727     APInt DemandedElts(VWidth, 1);
9728     APInt UndefElts(VWidth, 0);
9729     if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
9730                                               UndefElts)) {
9731       II->setOperand(1, V);
9732       return II;
9733     }
9734     break;
9735   }
9736     
9737   case Intrinsic::ppc_altivec_vperm:
9738     // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
9739     if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) {
9740       assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
9741       
9742       // Check that all of the elements are integer constants or undefs.
9743       bool AllEltsOk = true;
9744       for (unsigned i = 0; i != 16; ++i) {
9745         if (!isa<ConstantInt>(Mask->getOperand(i)) && 
9746             !isa<UndefValue>(Mask->getOperand(i))) {
9747           AllEltsOk = false;
9748           break;
9749         }
9750       }
9751       
9752       if (AllEltsOk) {
9753         // Cast the input vectors to byte vectors.
9754         Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
9755         Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType());
9756         Value *Result = UndefValue::get(Op0->getType());
9757         
9758         // Only extract each element once.
9759         Value *ExtractedElts[32];
9760         memset(ExtractedElts, 0, sizeof(ExtractedElts));
9761         
9762         for (unsigned i = 0; i != 16; ++i) {
9763           if (isa<UndefValue>(Mask->getOperand(i)))
9764             continue;
9765           unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
9766           Idx &= 31;  // Match the hardware behavior.
9767           
9768           if (ExtractedElts[Idx] == 0) {
9769             ExtractedElts[Idx] = 
9770               Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1, 
9771                   ConstantInt::get(Type::getInt32Ty(*Context), Idx&15, false),
9772                                             "tmp");
9773           }
9774         
9775           // Insert this value into the result vector.
9776           Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
9777                          ConstantInt::get(Type::getInt32Ty(*Context), i, false),
9778                                                 "tmp");
9779         }
9780         return CastInst::Create(Instruction::BitCast, Result, CI.getType());
9781       }
9782     }
9783     break;
9784
9785   case Intrinsic::stackrestore: {
9786     // If the save is right next to the restore, remove the restore.  This can
9787     // happen when variable allocas are DCE'd.
9788     if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
9789       if (SS->getIntrinsicID() == Intrinsic::stacksave) {
9790         BasicBlock::iterator BI = SS;
9791         if (&*++BI == II)
9792           return EraseInstFromFunction(CI);
9793       }
9794     }
9795     
9796     // Scan down this block to see if there is another stack restore in the
9797     // same block without an intervening call/alloca.
9798     BasicBlock::iterator BI = II;
9799     TerminatorInst *TI = II->getParent()->getTerminator();
9800     bool CannotRemove = false;
9801     for (++BI; &*BI != TI; ++BI) {
9802       if (isa<AllocaInst>(BI)) {
9803         CannotRemove = true;
9804         break;
9805       }
9806       if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
9807         if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
9808           // If there is a stackrestore below this one, remove this one.
9809           if (II->getIntrinsicID() == Intrinsic::stackrestore)
9810             return EraseInstFromFunction(CI);
9811           // Otherwise, ignore the intrinsic.
9812         } else {
9813           // If we found a non-intrinsic call, we can't remove the stack
9814           // restore.
9815           CannotRemove = true;
9816           break;
9817         }
9818       }
9819     }
9820     
9821     // If the stack restore is in a return/unwind block and if there are no
9822     // allocas or calls between the restore and the return, nuke the restore.
9823     if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
9824       return EraseInstFromFunction(CI);
9825     break;
9826   }
9827   }
9828
9829   return visitCallSite(II);
9830 }
9831
9832 // InvokeInst simplification
9833 //
9834 Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
9835   return visitCallSite(&II);
9836 }
9837
9838 /// isSafeToEliminateVarargsCast - If this cast does not affect the value 
9839 /// passed through the varargs area, we can eliminate the use of the cast.
9840 static bool isSafeToEliminateVarargsCast(const CallSite CS,
9841                                          const CastInst * const CI,
9842                                          const TargetData * const TD,
9843                                          const int ix) {
9844   if (!CI->isLosslessCast())
9845     return false;
9846
9847   // The size of ByVal arguments is derived from the type, so we
9848   // can't change to a type with a different size.  If the size were
9849   // passed explicitly we could avoid this check.
9850   if (!CS.paramHasAttr(ix, Attribute::ByVal))
9851     return true;
9852
9853   const Type* SrcTy = 
9854             cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
9855   const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
9856   if (!SrcTy->isSized() || !DstTy->isSized())
9857     return false;
9858   if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
9859     return false;
9860   return true;
9861 }
9862
9863 // visitCallSite - Improvements for call and invoke instructions.
9864 //
9865 Instruction *InstCombiner::visitCallSite(CallSite CS) {
9866   bool Changed = false;
9867
9868   // If the callee is a constexpr cast of a function, attempt to move the cast
9869   // to the arguments of the call/invoke.
9870   if (transformConstExprCastCall(CS)) return 0;
9871
9872   Value *Callee = CS.getCalledValue();
9873
9874   if (Function *CalleeF = dyn_cast<Function>(Callee))
9875     if (CalleeF->getCallingConv() != CS.getCallingConv()) {
9876       Instruction *OldCall = CS.getInstruction();
9877       // If the call and callee calling conventions don't match, this call must
9878       // be unreachable, as the call is undefined.
9879       new StoreInst(ConstantInt::getTrue(*Context),
9880                 UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))), 
9881                                   OldCall);
9882       if (!OldCall->use_empty())
9883         OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
9884       if (isa<CallInst>(OldCall))   // Not worth removing an invoke here.
9885         return EraseInstFromFunction(*OldCall);
9886       return 0;
9887     }
9888
9889   if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
9890     // This instruction is not reachable, just remove it.  We insert a store to
9891     // undef so that we know that this code is not reachable, despite the fact
9892     // that we can't modify the CFG here.
9893     new StoreInst(ConstantInt::getTrue(*Context),
9894                UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))),
9895                   CS.getInstruction());
9896
9897     if (!CS.getInstruction()->use_empty())
9898       CS.getInstruction()->
9899         replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
9900
9901     if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
9902       // Don't break the CFG, insert a dummy cond branch.
9903       BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
9904                          ConstantInt::getTrue(*Context), II);
9905     }
9906     return EraseInstFromFunction(*CS.getInstruction());
9907   }
9908
9909   if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
9910     if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
9911       if (In->getIntrinsicID() == Intrinsic::init_trampoline)
9912         return transformCallThroughTrampoline(CS);
9913
9914   const PointerType *PTy = cast<PointerType>(Callee->getType());
9915   const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
9916   if (FTy->isVarArg()) {
9917     int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
9918     // See if we can optimize any arguments passed through the varargs area of
9919     // the call.
9920     for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
9921            E = CS.arg_end(); I != E; ++I, ++ix) {
9922       CastInst *CI = dyn_cast<CastInst>(*I);
9923       if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
9924         *I = CI->getOperand(0);
9925         Changed = true;
9926       }
9927     }
9928   }
9929
9930   if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
9931     // Inline asm calls cannot throw - mark them 'nounwind'.
9932     CS.setDoesNotThrow();
9933     Changed = true;
9934   }
9935
9936   return Changed ? CS.getInstruction() : 0;
9937 }
9938
9939 // transformConstExprCastCall - If the callee is a constexpr cast of a function,
9940 // attempt to move the cast to the arguments of the call/invoke.
9941 //
9942 bool InstCombiner::transformConstExprCastCall(CallSite CS) {
9943   if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
9944   ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
9945   if (CE->getOpcode() != Instruction::BitCast || 
9946       !isa<Function>(CE->getOperand(0)))
9947     return false;
9948   Function *Callee = cast<Function>(CE->getOperand(0));
9949   Instruction *Caller = CS.getInstruction();
9950   const AttrListPtr &CallerPAL = CS.getAttributes();
9951
9952   // Okay, this is a cast from a function to a different type.  Unless doing so
9953   // would cause a type conversion of one of our arguments, change this call to
9954   // be a direct call with arguments casted to the appropriate types.
9955   //
9956   const FunctionType *FT = Callee->getFunctionType();
9957   const Type *OldRetTy = Caller->getType();
9958   const Type *NewRetTy = FT->getReturnType();
9959
9960   if (isa<StructType>(NewRetTy))
9961     return false; // TODO: Handle multiple return values.
9962
9963   // Check to see if we are changing the return type...
9964   if (OldRetTy != NewRetTy) {
9965     if (Callee->isDeclaration() &&
9966         // Conversion is ok if changing from one pointer type to another or from
9967         // a pointer to an integer of the same size.
9968         !((isa<PointerType>(OldRetTy) || !TD ||
9969            OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
9970           (isa<PointerType>(NewRetTy) || !TD ||
9971            NewRetTy == TD->getIntPtrType(Caller->getContext()))))
9972       return false;   // Cannot transform this return value.
9973
9974     if (!Caller->use_empty() &&
9975         // void -> non-void is handled specially
9976         NewRetTy != Type::getVoidTy(*Context) && !CastInst::isCastable(NewRetTy, OldRetTy))
9977       return false;   // Cannot transform this return value.
9978
9979     if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
9980       Attributes RAttrs = CallerPAL.getRetAttributes();
9981       if (RAttrs & Attribute::typeIncompatible(NewRetTy))
9982         return false;   // Attribute not compatible with transformed value.
9983     }
9984
9985     // If the callsite is an invoke instruction, and the return value is used by
9986     // a PHI node in a successor, we cannot change the return type of the call
9987     // because there is no place to put the cast instruction (without breaking
9988     // the critical edge).  Bail out in this case.
9989     if (!Caller->use_empty())
9990       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
9991         for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
9992              UI != E; ++UI)
9993           if (PHINode *PN = dyn_cast<PHINode>(*UI))
9994             if (PN->getParent() == II->getNormalDest() ||
9995                 PN->getParent() == II->getUnwindDest())
9996               return false;
9997   }
9998
9999   unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
10000   unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
10001
10002   CallSite::arg_iterator AI = CS.arg_begin();
10003   for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
10004     const Type *ParamTy = FT->getParamType(i);
10005     const Type *ActTy = (*AI)->getType();
10006
10007     if (!CastInst::isCastable(ActTy, ParamTy))
10008       return false;   // Cannot transform this parameter value.
10009
10010     if (CallerPAL.getParamAttributes(i + 1) 
10011         & Attribute::typeIncompatible(ParamTy))
10012       return false;   // Attribute not compatible with transformed value.
10013
10014     // Converting from one pointer type to another or between a pointer and an
10015     // integer of the same size is safe even if we do not have a body.
10016     bool isConvertible = ActTy == ParamTy ||
10017       (TD && ((isa<PointerType>(ParamTy) ||
10018       ParamTy == TD->getIntPtrType(Caller->getContext())) &&
10019               (isa<PointerType>(ActTy) ||
10020               ActTy == TD->getIntPtrType(Caller->getContext()))));
10021     if (Callee->isDeclaration() && !isConvertible) return false;
10022   }
10023
10024   if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
10025       Callee->isDeclaration())
10026     return false;   // Do not delete arguments unless we have a function body.
10027
10028   if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
10029       !CallerPAL.isEmpty())
10030     // In this case we have more arguments than the new function type, but we
10031     // won't be dropping them.  Check that these extra arguments have attributes
10032     // that are compatible with being a vararg call argument.
10033     for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
10034       if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
10035         break;
10036       Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
10037       if (PAttrs & Attribute::VarArgsIncompatible)
10038         return false;
10039     }
10040
10041   // Okay, we decided that this is a safe thing to do: go ahead and start
10042   // inserting cast instructions as necessary...
10043   std::vector<Value*> Args;
10044   Args.reserve(NumActualArgs);
10045   SmallVector<AttributeWithIndex, 8> attrVec;
10046   attrVec.reserve(NumCommonArgs);
10047
10048   // Get any return attributes.
10049   Attributes RAttrs = CallerPAL.getRetAttributes();
10050
10051   // If the return value is not being used, the type may not be compatible
10052   // with the existing attributes.  Wipe out any problematic attributes.
10053   RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
10054
10055   // Add the new return attributes.
10056   if (RAttrs)
10057     attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
10058
10059   AI = CS.arg_begin();
10060   for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
10061     const Type *ParamTy = FT->getParamType(i);
10062     if ((*AI)->getType() == ParamTy) {
10063       Args.push_back(*AI);
10064     } else {
10065       Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
10066           false, ParamTy, false);
10067       Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp"));
10068     }
10069
10070     // Add any parameter attributes.
10071     if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
10072       attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
10073   }
10074
10075   // If the function takes more arguments than the call was taking, add them
10076   // now.
10077   for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
10078     Args.push_back(Constant::getNullValue(FT->getParamType(i)));
10079
10080   // If we are removing arguments to the function, emit an obnoxious warning.
10081   if (FT->getNumParams() < NumActualArgs) {
10082     if (!FT->isVarArg()) {
10083       errs() << "WARNING: While resolving call to function '"
10084              << Callee->getName() << "' arguments were dropped!\n";
10085     } else {
10086       // Add all of the arguments in their promoted form to the arg list.
10087       for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
10088         const Type *PTy = getPromotedType((*AI)->getType());
10089         if (PTy != (*AI)->getType()) {
10090           // Must promote to pass through va_arg area!
10091           Instruction::CastOps opcode =
10092             CastInst::getCastOpcode(*AI, false, PTy, false);
10093           Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp"));
10094         } else {
10095           Args.push_back(*AI);
10096         }
10097
10098         // Add any parameter attributes.
10099         if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
10100           attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
10101       }
10102     }
10103   }
10104
10105   if (Attributes FnAttrs =  CallerPAL.getFnAttributes())
10106     attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
10107
10108   if (NewRetTy == Type::getVoidTy(*Context))
10109     Caller->setName("");   // Void type should not have a name.
10110
10111   const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
10112                                                      attrVec.end());
10113
10114   Instruction *NC;
10115   if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
10116     NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
10117                             Args.begin(), Args.end(),
10118                             Caller->getName(), Caller);
10119     cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
10120     cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
10121   } else {
10122     NC = CallInst::Create(Callee, Args.begin(), Args.end(),
10123                           Caller->getName(), Caller);
10124     CallInst *CI = cast<CallInst>(Caller);
10125     if (CI->isTailCall())
10126       cast<CallInst>(NC)->setTailCall();
10127     cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
10128     cast<CallInst>(NC)->setAttributes(NewCallerPAL);
10129   }
10130
10131   // Insert a cast of the return type as necessary.
10132   Value *NV = NC;
10133   if (OldRetTy != NV->getType() && !Caller->use_empty()) {
10134     if (NV->getType() != Type::getVoidTy(*Context)) {
10135       Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, 
10136                                                             OldRetTy, false);
10137       NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
10138
10139       // If this is an invoke instruction, we should insert it after the first
10140       // non-phi, instruction in the normal successor block.
10141       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
10142         BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI();
10143         InsertNewInstBefore(NC, *I);
10144       } else {
10145         // Otherwise, it's a call, just insert cast right after the call instr
10146         InsertNewInstBefore(NC, *Caller);
10147       }
10148       Worklist.AddUsersToWorkList(*Caller);
10149     } else {
10150       NV = UndefValue::get(Caller->getType());
10151     }
10152   }
10153
10154   
10155   if (!Caller->use_empty())
10156     Caller->replaceAllUsesWith(NV);
10157   
10158   EraseInstFromFunction(*Caller);
10159   return true;
10160 }
10161
10162 // transformCallThroughTrampoline - Turn a call to a function created by the
10163 // init_trampoline intrinsic into a direct call to the underlying function.
10164 //
10165 Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
10166   Value *Callee = CS.getCalledValue();
10167   const PointerType *PTy = cast<PointerType>(Callee->getType());
10168   const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
10169   const AttrListPtr &Attrs = CS.getAttributes();
10170
10171   // If the call already has the 'nest' attribute somewhere then give up -
10172   // otherwise 'nest' would occur twice after splicing in the chain.
10173   if (Attrs.hasAttrSomewhere(Attribute::Nest))
10174     return 0;
10175
10176   IntrinsicInst *Tramp =
10177     cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
10178
10179   Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts());
10180   const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
10181   const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
10182
10183   const AttrListPtr &NestAttrs = NestF->getAttributes();
10184   if (!NestAttrs.isEmpty()) {
10185     unsigned NestIdx = 1;
10186     const Type *NestTy = 0;
10187     Attributes NestAttr = Attribute::None;
10188
10189     // Look for a parameter marked with the 'nest' attribute.
10190     for (FunctionType::param_iterator I = NestFTy->param_begin(),
10191          E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
10192       if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
10193         // Record the parameter type and any other attributes.
10194         NestTy = *I;
10195         NestAttr = NestAttrs.getParamAttributes(NestIdx);
10196         break;
10197       }
10198
10199     if (NestTy) {
10200       Instruction *Caller = CS.getInstruction();
10201       std::vector<Value*> NewArgs;
10202       NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
10203
10204       SmallVector<AttributeWithIndex, 8> NewAttrs;
10205       NewAttrs.reserve(Attrs.getNumSlots() + 1);
10206
10207       // Insert the nest argument into the call argument list, which may
10208       // mean appending it.  Likewise for attributes.
10209
10210       // Add any result attributes.
10211       if (Attributes Attr = Attrs.getRetAttributes())
10212         NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
10213
10214       {
10215         unsigned Idx = 1;
10216         CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
10217         do {
10218           if (Idx == NestIdx) {
10219             // Add the chain argument and attributes.
10220             Value *NestVal = Tramp->getOperand(3);
10221             if (NestVal->getType() != NestTy)
10222               NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
10223             NewArgs.push_back(NestVal);
10224             NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
10225           }
10226
10227           if (I == E)
10228             break;
10229
10230           // Add the original argument and attributes.
10231           NewArgs.push_back(*I);
10232           if (Attributes Attr = Attrs.getParamAttributes(Idx))
10233             NewAttrs.push_back
10234               (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
10235
10236           ++Idx, ++I;
10237         } while (1);
10238       }
10239
10240       // Add any function attributes.
10241       if (Attributes Attr = Attrs.getFnAttributes())
10242         NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
10243
10244       // The trampoline may have been bitcast to a bogus type (FTy).
10245       // Handle this by synthesizing a new function type, equal to FTy
10246       // with the chain parameter inserted.
10247
10248       std::vector<const Type*> NewTypes;
10249       NewTypes.reserve(FTy->getNumParams()+1);
10250
10251       // Insert the chain's type into the list of parameter types, which may
10252       // mean appending it.
10253       {
10254         unsigned Idx = 1;
10255         FunctionType::param_iterator I = FTy->param_begin(),
10256           E = FTy->param_end();
10257
10258         do {
10259           if (Idx == NestIdx)
10260             // Add the chain's type.
10261             NewTypes.push_back(NestTy);
10262
10263           if (I == E)
10264             break;
10265
10266           // Add the original type.
10267           NewTypes.push_back(*I);
10268
10269           ++Idx, ++I;
10270         } while (1);
10271       }
10272
10273       // Replace the trampoline call with a direct call.  Let the generic
10274       // code sort out any function type mismatches.
10275       FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, 
10276                                                 FTy->isVarArg());
10277       Constant *NewCallee =
10278         NestF->getType() == PointerType::getUnqual(NewFTy) ?
10279         NestF : ConstantExpr::getBitCast(NestF, 
10280                                          PointerType::getUnqual(NewFTy));
10281       const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
10282                                                    NewAttrs.end());
10283
10284       Instruction *NewCaller;
10285       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
10286         NewCaller = InvokeInst::Create(NewCallee,
10287                                        II->getNormalDest(), II->getUnwindDest(),
10288                                        NewArgs.begin(), NewArgs.end(),
10289                                        Caller->getName(), Caller);
10290         cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
10291         cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
10292       } else {
10293         NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
10294                                      Caller->getName(), Caller);
10295         if (cast<CallInst>(Caller)->isTailCall())
10296           cast<CallInst>(NewCaller)->setTailCall();
10297         cast<CallInst>(NewCaller)->
10298           setCallingConv(cast<CallInst>(Caller)->getCallingConv());
10299         cast<CallInst>(NewCaller)->setAttributes(NewPAL);
10300       }
10301       if (Caller->getType() != Type::getVoidTy(*Context) && !Caller->use_empty())
10302         Caller->replaceAllUsesWith(NewCaller);
10303       Caller->eraseFromParent();
10304       Worklist.Remove(Caller);
10305       return 0;
10306     }
10307   }
10308
10309   // Replace the trampoline call with a direct call.  Since there is no 'nest'
10310   // parameter, there is no need to adjust the argument list.  Let the generic
10311   // code sort out any function type mismatches.
10312   Constant *NewCallee =
10313     NestF->getType() == PTy ? NestF : 
10314                               ConstantExpr::getBitCast(NestF, PTy);
10315   CS.setCalledFunction(NewCallee);
10316   return CS.getInstruction();
10317 }
10318
10319 /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)]
10320 /// and if a/b/c/d and the add's all have a single use, turn this into two phi's
10321 /// and a single binop.
10322 Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) {
10323   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
10324   assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
10325   unsigned Opc = FirstInst->getOpcode();
10326   Value *LHSVal = FirstInst->getOperand(0);
10327   Value *RHSVal = FirstInst->getOperand(1);
10328     
10329   const Type *LHSType = LHSVal->getType();
10330   const Type *RHSType = RHSVal->getType();
10331   
10332   // Scan to see if all operands are the same opcode, all have one use, and all
10333   // kill their operands (i.e. the operands have one use).
10334   for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
10335     Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
10336     if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
10337         // Verify type of the LHS matches so we don't fold cmp's of different
10338         // types or GEP's with different index types.
10339         I->getOperand(0)->getType() != LHSType ||
10340         I->getOperand(1)->getType() != RHSType)
10341       return 0;
10342
10343     // If they are CmpInst instructions, check their predicates
10344     if (Opc == Instruction::ICmp || Opc == Instruction::FCmp)
10345       if (cast<CmpInst>(I)->getPredicate() !=
10346           cast<CmpInst>(FirstInst)->getPredicate())
10347         return 0;
10348     
10349     // Keep track of which operand needs a phi node.
10350     if (I->getOperand(0) != LHSVal) LHSVal = 0;
10351     if (I->getOperand(1) != RHSVal) RHSVal = 0;
10352   }
10353   
10354   // Otherwise, this is safe to transform!
10355   
10356   Value *InLHS = FirstInst->getOperand(0);
10357   Value *InRHS = FirstInst->getOperand(1);
10358   PHINode *NewLHS = 0, *NewRHS = 0;
10359   if (LHSVal == 0) {
10360     NewLHS = PHINode::Create(LHSType,
10361                              FirstInst->getOperand(0)->getName() + ".pn");
10362     NewLHS->reserveOperandSpace(PN.getNumOperands()/2);
10363     NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
10364     InsertNewInstBefore(NewLHS, PN);
10365     LHSVal = NewLHS;
10366   }
10367   
10368   if (RHSVal == 0) {
10369     NewRHS = PHINode::Create(RHSType,
10370                              FirstInst->getOperand(1)->getName() + ".pn");
10371     NewRHS->reserveOperandSpace(PN.getNumOperands()/2);
10372     NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
10373     InsertNewInstBefore(NewRHS, PN);
10374     RHSVal = NewRHS;
10375   }
10376   
10377   // Add all operands to the new PHIs.
10378   if (NewLHS || NewRHS) {
10379     for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10380       Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i));
10381       if (NewLHS) {
10382         Value *NewInLHS = InInst->getOperand(0);
10383         NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i));
10384       }
10385       if (NewRHS) {
10386         Value *NewInRHS = InInst->getOperand(1);
10387         NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i));
10388       }
10389     }
10390   }
10391     
10392   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
10393     return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
10394   CmpInst *CIOp = cast<CmpInst>(FirstInst);
10395   return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
10396                          LHSVal, RHSVal);
10397 }
10398
10399 Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) {
10400   GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0));
10401   
10402   SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(), 
10403                                         FirstInst->op_end());
10404   // This is true if all GEP bases are allocas and if all indices into them are
10405   // constants.
10406   bool AllBasePointersAreAllocas = true;
10407   
10408   // Scan to see if all operands are the same opcode, all have one use, and all
10409   // kill their operands (i.e. the operands have one use).
10410   for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
10411     GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i));
10412     if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() ||
10413       GEP->getNumOperands() != FirstInst->getNumOperands())
10414       return 0;
10415
10416     // Keep track of whether or not all GEPs are of alloca pointers.
10417     if (AllBasePointersAreAllocas &&
10418         (!isa<AllocaInst>(GEP->getOperand(0)) ||
10419          !GEP->hasAllConstantIndices()))
10420       AllBasePointersAreAllocas = false;
10421     
10422     // Compare the operand lists.
10423     for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) {
10424       if (FirstInst->getOperand(op) == GEP->getOperand(op))
10425         continue;
10426       
10427       // Don't merge two GEPs when two operands differ (introducing phi nodes)
10428       // if one of the PHIs has a constant for the index.  The index may be
10429       // substantially cheaper to compute for the constants, so making it a
10430       // variable index could pessimize the path.  This also handles the case
10431       // for struct indices, which must always be constant.
10432       if (isa<ConstantInt>(FirstInst->getOperand(op)) ||
10433           isa<ConstantInt>(GEP->getOperand(op)))
10434         return 0;
10435       
10436       if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType())
10437         return 0;
10438       FixedOperands[op] = 0;  // Needs a PHI.
10439     }
10440   }
10441   
10442   // If all of the base pointers of the PHI'd GEPs are from allocas, don't
10443   // bother doing this transformation.  At best, this will just save a bit of
10444   // offset calculation, but all the predecessors will have to materialize the
10445   // stack address into a register anyway.  We'd actually rather *clone* the
10446   // load up into the predecessors so that we have a load of a gep of an alloca,
10447   // which can usually all be folded into the load.
10448   if (AllBasePointersAreAllocas)
10449     return 0;
10450   
10451   // Otherwise, this is safe to transform.  Insert PHI nodes for each operand
10452   // that is variable.
10453   SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size());
10454   
10455   bool HasAnyPHIs = false;
10456   for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) {
10457     if (FixedOperands[i]) continue;  // operand doesn't need a phi.
10458     Value *FirstOp = FirstInst->getOperand(i);
10459     PHINode *NewPN = PHINode::Create(FirstOp->getType(),
10460                                      FirstOp->getName()+".pn");
10461     InsertNewInstBefore(NewPN, PN);
10462     
10463     NewPN->reserveOperandSpace(e);
10464     NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0));
10465     OperandPhis[i] = NewPN;
10466     FixedOperands[i] = NewPN;
10467     HasAnyPHIs = true;
10468   }
10469
10470   
10471   // Add all operands to the new PHIs.
10472   if (HasAnyPHIs) {
10473     for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10474       GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i));
10475       BasicBlock *InBB = PN.getIncomingBlock(i);
10476       
10477       for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op)
10478         if (PHINode *OpPhi = OperandPhis[op])
10479           OpPhi->addIncoming(InGEP->getOperand(op), InBB);
10480     }
10481   }
10482   
10483   Value *Base = FixedOperands[0];
10484   GetElementPtrInst *GEP =
10485     GetElementPtrInst::Create(Base, FixedOperands.begin()+1,
10486                               FixedOperands.end());
10487   if (cast<GEPOperator>(FirstInst)->isInBounds())
10488     cast<GEPOperator>(GEP)->setIsInBounds(true);
10489   return GEP;
10490 }
10491
10492
10493 /// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to
10494 /// sink the load out of the block that defines it.  This means that it must be
10495 /// obvious the value of the load is not changed from the point of the load to
10496 /// the end of the block it is in.
10497 ///
10498 /// Finally, it is safe, but not profitable, to sink a load targetting a
10499 /// non-address-taken alloca.  Doing so will cause us to not promote the alloca
10500 /// to a register.
10501 static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
10502   BasicBlock::iterator BBI = L, E = L->getParent()->end();
10503   
10504   for (++BBI; BBI != E; ++BBI)
10505     if (BBI->mayWriteToMemory())
10506       return false;
10507   
10508   // Check for non-address taken alloca.  If not address-taken already, it isn't
10509   // profitable to do this xform.
10510   if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) {
10511     bool isAddressTaken = false;
10512     for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
10513          UI != E; ++UI) {
10514       if (isa<LoadInst>(UI)) continue;
10515       if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
10516         // If storing TO the alloca, then the address isn't taken.
10517         if (SI->getOperand(1) == AI) continue;
10518       }
10519       isAddressTaken = true;
10520       break;
10521     }
10522     
10523     if (!isAddressTaken && AI->isStaticAlloca())
10524       return false;
10525   }
10526   
10527   // If this load is a load from a GEP with a constant offset from an alloca,
10528   // then we don't want to sink it.  In its present form, it will be
10529   // load [constant stack offset].  Sinking it will cause us to have to
10530   // materialize the stack addresses in each predecessor in a register only to
10531   // do a shared load from register in the successor.
10532   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0)))
10533     if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0)))
10534       if (AI->isStaticAlloca() && GEP->hasAllConstantIndices())
10535         return false;
10536   
10537   return true;
10538 }
10539
10540
10541 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
10542 // operator and they all are only used by the PHI, PHI together their
10543 // inputs, and do the operation once, to the result of the PHI.
10544 Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
10545   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
10546
10547   // Scan the instruction, looking for input operations that can be folded away.
10548   // If all input operands to the phi are the same instruction (e.g. a cast from
10549   // the same type or "+42") we can pull the operation through the PHI, reducing
10550   // code size and simplifying code.
10551   Constant *ConstantOp = 0;
10552   const Type *CastSrcTy = 0;
10553   bool isVolatile = false;
10554   if (isa<CastInst>(FirstInst)) {
10555     CastSrcTy = FirstInst->getOperand(0)->getType();
10556   } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
10557     // Can fold binop, compare or shift here if the RHS is a constant, 
10558     // otherwise call FoldPHIArgBinOpIntoPHI.
10559     ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
10560     if (ConstantOp == 0)
10561       return FoldPHIArgBinOpIntoPHI(PN);
10562   } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) {
10563     isVolatile = LI->isVolatile();
10564     // We can't sink the load if the loaded value could be modified between the
10565     // load and the PHI.
10566     if (LI->getParent() != PN.getIncomingBlock(0) ||
10567         !isSafeAndProfitableToSinkLoad(LI))
10568       return 0;
10569     
10570     // If the PHI is of volatile loads and the load block has multiple
10571     // successors, sinking it would remove a load of the volatile value from
10572     // the path through the other successor.
10573     if (isVolatile &&
10574         LI->getParent()->getTerminator()->getNumSuccessors() != 1)
10575       return 0;
10576     
10577   } else if (isa<GetElementPtrInst>(FirstInst)) {
10578     return FoldPHIArgGEPIntoPHI(PN);
10579   } else {
10580     return 0;  // Cannot fold this operation.
10581   }
10582
10583   // Check to see if all arguments are the same operation.
10584   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10585     if (!isa<Instruction>(PN.getIncomingValue(i))) return 0;
10586     Instruction *I = cast<Instruction>(PN.getIncomingValue(i));
10587     if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst))
10588       return 0;
10589     if (CastSrcTy) {
10590       if (I->getOperand(0)->getType() != CastSrcTy)
10591         return 0;  // Cast operation must match.
10592     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
10593       // We can't sink the load if the loaded value could be modified between 
10594       // the load and the PHI.
10595       if (LI->isVolatile() != isVolatile ||
10596           LI->getParent() != PN.getIncomingBlock(i) ||
10597           !isSafeAndProfitableToSinkLoad(LI))
10598         return 0;
10599       
10600       // If the PHI is of volatile loads and the load block has multiple
10601       // successors, sinking it would remove a load of the volatile value from
10602       // the path through the other successor.
10603       if (isVolatile &&
10604           LI->getParent()->getTerminator()->getNumSuccessors() != 1)
10605         return 0;
10606       
10607     } else if (I->getOperand(1) != ConstantOp) {
10608       return 0;
10609     }
10610   }
10611
10612   // Okay, they are all the same operation.  Create a new PHI node of the
10613   // correct type, and PHI together all of the LHS's of the instructions.
10614   PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
10615                                    PN.getName()+".in");
10616   NewPN->reserveOperandSpace(PN.getNumOperands()/2);
10617
10618   Value *InVal = FirstInst->getOperand(0);
10619   NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
10620
10621   // Add all operands to the new PHI.
10622   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10623     Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
10624     if (NewInVal != InVal)
10625       InVal = 0;
10626     NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
10627   }
10628
10629   Value *PhiVal;
10630   if (InVal) {
10631     // The new PHI unions all of the same values together.  This is really
10632     // common, so we handle it intelligently here for compile-time speed.
10633     PhiVal = InVal;
10634     delete NewPN;
10635   } else {
10636     InsertNewInstBefore(NewPN, PN);
10637     PhiVal = NewPN;
10638   }
10639
10640   // Insert and return the new operation.
10641   if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst))
10642     return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType());
10643   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
10644     return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
10645   if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
10646     return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
10647                            PhiVal, ConstantOp);
10648   assert(isa<LoadInst>(FirstInst) && "Unknown operation");
10649   
10650   // If this was a volatile load that we are merging, make sure to loop through
10651   // and mark all the input loads as non-volatile.  If we don't do this, we will
10652   // insert a new volatile load and the old ones will not be deletable.
10653   if (isVolatile)
10654     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
10655       cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false);
10656   
10657   return new LoadInst(PhiVal, "", isVolatile);
10658 }
10659
10660 /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle
10661 /// that is dead.
10662 static bool DeadPHICycle(PHINode *PN,
10663                          SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) {
10664   if (PN->use_empty()) return true;
10665   if (!PN->hasOneUse()) return false;
10666
10667   // Remember this node, and if we find the cycle, return.
10668   if (!PotentiallyDeadPHIs.insert(PN))
10669     return true;
10670   
10671   // Don't scan crazily complex things.
10672   if (PotentiallyDeadPHIs.size() == 16)
10673     return false;
10674
10675   if (PHINode *PU = dyn_cast<PHINode>(PN->use_back()))
10676     return DeadPHICycle(PU, PotentiallyDeadPHIs);
10677
10678   return false;
10679 }
10680
10681 /// PHIsEqualValue - Return true if this phi node is always equal to
10682 /// NonPhiInVal.  This happens with mutually cyclic phi nodes like:
10683 ///   z = some value; x = phi (y, z); y = phi (x, z)
10684 static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, 
10685                            SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) {
10686   // See if we already saw this PHI node.
10687   if (!ValueEqualPHIs.insert(PN))
10688     return true;
10689   
10690   // Don't scan crazily complex things.
10691   if (ValueEqualPHIs.size() == 16)
10692     return false;
10693  
10694   // Scan the operands to see if they are either phi nodes or are equal to
10695   // the value.
10696   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
10697     Value *Op = PN->getIncomingValue(i);
10698     if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
10699       if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
10700         return false;
10701     } else if (Op != NonPhiInVal)
10702       return false;
10703   }
10704   
10705   return true;
10706 }
10707
10708
10709 // PHINode simplification
10710 //
10711 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
10712   // If LCSSA is around, don't mess with Phi nodes
10713   if (MustPreserveLCSSA) return 0;
10714   
10715   if (Value *V = PN.hasConstantValue())
10716     return ReplaceInstUsesWith(PN, V);
10717
10718   // If all PHI operands are the same operation, pull them through the PHI,
10719   // reducing code size.
10720   if (isa<Instruction>(PN.getIncomingValue(0)) &&
10721       isa<Instruction>(PN.getIncomingValue(1)) &&
10722       cast<Instruction>(PN.getIncomingValue(0))->getOpcode() ==
10723       cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
10724       // FIXME: The hasOneUse check will fail for PHIs that use the value more
10725       // than themselves more than once.
10726       PN.getIncomingValue(0)->hasOneUse())
10727     if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
10728       return Result;
10729
10730   // If this is a trivial cycle in the PHI node graph, remove it.  Basically, if
10731   // this PHI only has a single use (a PHI), and if that PHI only has one use (a
10732   // PHI)... break the cycle.
10733   if (PN.hasOneUse()) {
10734     Instruction *PHIUser = cast<Instruction>(PN.use_back());
10735     if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
10736       SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
10737       PotentiallyDeadPHIs.insert(&PN);
10738       if (DeadPHICycle(PU, PotentiallyDeadPHIs))
10739         return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
10740     }
10741    
10742     // If this phi has a single use, and if that use just computes a value for
10743     // the next iteration of a loop, delete the phi.  This occurs with unused
10744     // induction variables, e.g. "for (int j = 0; ; ++j);".  Detecting this
10745     // common case here is good because the only other things that catch this
10746     // are induction variable analysis (sometimes) and ADCE, which is only run
10747     // late.
10748     if (PHIUser->hasOneUse() &&
10749         (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
10750         PHIUser->use_back() == &PN) {
10751       return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
10752     }
10753   }
10754
10755   // We sometimes end up with phi cycles that non-obviously end up being the
10756   // same value, for example:
10757   //   z = some value; x = phi (y, z); y = phi (x, z)
10758   // where the phi nodes don't necessarily need to be in the same block.  Do a
10759   // quick check to see if the PHI node only contains a single non-phi value, if
10760   // so, scan to see if the phi cycle is actually equal to that value.
10761   {
10762     unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues();
10763     // Scan for the first non-phi operand.
10764     while (InValNo != NumOperandVals && 
10765            isa<PHINode>(PN.getIncomingValue(InValNo)))
10766       ++InValNo;
10767
10768     if (InValNo != NumOperandVals) {
10769       Value *NonPhiInVal = PN.getOperand(InValNo);
10770       
10771       // Scan the rest of the operands to see if there are any conflicts, if so
10772       // there is no need to recursively scan other phis.
10773       for (++InValNo; InValNo != NumOperandVals; ++InValNo) {
10774         Value *OpVal = PN.getIncomingValue(InValNo);
10775         if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
10776           break;
10777       }
10778       
10779       // If we scanned over all operands, then we have one unique value plus
10780       // phi values.  Scan PHI nodes to see if they all merge in each other or
10781       // the value.
10782       if (InValNo == NumOperandVals) {
10783         SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
10784         if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
10785           return ReplaceInstUsesWith(PN, NonPhiInVal);
10786       }
10787     }
10788   }
10789   return 0;
10790 }
10791
10792 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
10793   Value *PtrOp = GEP.getOperand(0);
10794   // Eliminate 'getelementptr %P, i32 0' and 'getelementptr %P', they are noops.
10795   if (GEP.getNumOperands() == 1)
10796     return ReplaceInstUsesWith(GEP, PtrOp);
10797
10798   if (isa<UndefValue>(GEP.getOperand(0)))
10799     return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
10800
10801   bool HasZeroPointerIndex = false;
10802   if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
10803     HasZeroPointerIndex = C->isNullValue();
10804
10805   if (GEP.getNumOperands() == 2 && HasZeroPointerIndex)
10806     return ReplaceInstUsesWith(GEP, PtrOp);
10807
10808   // Eliminate unneeded casts for indices.
10809   if (TD) {
10810     bool MadeChange = false;
10811     unsigned PtrSize = TD->getPointerSizeInBits();
10812     
10813     gep_type_iterator GTI = gep_type_begin(GEP);
10814     for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end();
10815          I != E; ++I, ++GTI) {
10816       if (!isa<SequentialType>(*GTI)) continue;
10817       
10818       // If we are using a wider index than needed for this platform, shrink it
10819       // to what we need.  If narrower, sign-extend it to what we need.  This
10820       // explicit cast can make subsequent optimizations more obvious.
10821       unsigned OpBits = cast<IntegerType>((*I)->getType())->getBitWidth();
10822       if (OpBits == PtrSize)
10823         continue;
10824       
10825       *I = Builder->CreateIntCast(*I, TD->getIntPtrType(GEP.getContext()),true);
10826       MadeChange = true;
10827     }
10828     if (MadeChange) return &GEP;
10829   }
10830
10831   // Combine Indices - If the source pointer to this getelementptr instruction
10832   // is a getelementptr instruction, combine the indices of the two
10833   // getelementptr instructions into a single instruction.
10834   //
10835   if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
10836     // Note that if our source is a gep chain itself that we wait for that
10837     // chain to be resolved before we perform this transformation.  This
10838     // avoids us creating a TON of code in some cases.
10839     //
10840     if (GetElementPtrInst *SrcGEP =
10841           dyn_cast<GetElementPtrInst>(Src->getOperand(0)))
10842       if (SrcGEP->getNumOperands() == 2)
10843         return 0;   // Wait until our source is folded to completion.
10844
10845     SmallVector<Value*, 8> Indices;
10846
10847     // Find out whether the last index in the source GEP is a sequential idx.
10848     bool EndsWithSequential = false;
10849     for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
10850          I != E; ++I)
10851       EndsWithSequential = !isa<StructType>(*I);
10852
10853     // Can we combine the two pointer arithmetics offsets?
10854     if (EndsWithSequential) {
10855       // Replace: gep (gep %P, long B), long A, ...
10856       // With:    T = long A+B; gep %P, T, ...
10857       //
10858       Value *Sum;
10859       Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
10860       Value *GO1 = GEP.getOperand(1);
10861       if (SO1 == Constant::getNullValue(SO1->getType())) {
10862         Sum = GO1;
10863       } else if (GO1 == Constant::getNullValue(GO1->getType())) {
10864         Sum = SO1;
10865       } else {
10866         // If they aren't the same type, then the input hasn't been processed
10867         // by the loop above yet (which canonicalizes sequential index types to
10868         // intptr_t).  Just avoid transforming this until the input has been
10869         // normalized.
10870         if (SO1->getType() != GO1->getType())
10871           return 0;
10872         Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
10873       }
10874
10875       // Update the GEP in place if possible.
10876       if (Src->getNumOperands() == 2) {
10877         GEP.setOperand(0, Src->getOperand(0));
10878         GEP.setOperand(1, Sum);
10879         return &GEP;
10880       }
10881       Indices.append(Src->op_begin()+1, Src->op_end()-1);
10882       Indices.push_back(Sum);
10883       Indices.append(GEP.op_begin()+2, GEP.op_end());
10884     } else if (isa<Constant>(*GEP.idx_begin()) &&
10885                cast<Constant>(*GEP.idx_begin())->isNullValue() &&
10886                Src->getNumOperands() != 1) {
10887       // Otherwise we can do the fold if the first index of the GEP is a zero
10888       Indices.append(Src->op_begin()+1, Src->op_end());
10889       Indices.append(GEP.idx_begin()+1, GEP.idx_end());
10890     }
10891
10892     if (!Indices.empty()) {
10893       GetElementPtrInst *NewGEP =
10894         GetElementPtrInst::Create(Src->getOperand(0), Indices.begin(),
10895                                   Indices.end(), GEP.getName());
10896       if (cast<GEPOperator>(&GEP)->isInBounds() && Src->isInBounds())
10897         cast<GEPOperator>(NewGEP)->setIsInBounds(true);
10898       return NewGEP;
10899     }
10900   }
10901   
10902   // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
10903   if (Value *X = getBitCastOperand(PtrOp)) {
10904     assert(isa<PointerType>(X->getType()) && "Must be cast from pointer");
10905
10906     // If the input bitcast is actually "bitcast(bitcast(x))", then we don't 
10907     // want to change the gep until the bitcasts are eliminated.
10908     if (getBitCastOperand(X)) {
10909       Worklist.AddValue(PtrOp);
10910       return 0;
10911     }
10912     
10913     // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
10914     // into     : GEP [10 x i8]* X, i32 0, ...
10915     //
10916     // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
10917     //           into     : GEP i8* X, ...
10918     // 
10919     // This occurs when the program declares an array extern like "int X[];"
10920     if (HasZeroPointerIndex) {
10921       const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
10922       const PointerType *XTy = cast<PointerType>(X->getType());
10923       if (const ArrayType *CATy =
10924           dyn_cast<ArrayType>(CPTy->getElementType())) {
10925         // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
10926         if (CATy->getElementType() == XTy->getElementType()) {
10927           // -> GEP i8* X, ...
10928           SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end());
10929           GetElementPtrInst *NewGEP =
10930             GetElementPtrInst::Create(X, Indices.begin(), Indices.end(),
10931                                       GEP.getName());
10932           if (cast<GEPOperator>(&GEP)->isInBounds())
10933             cast<GEPOperator>(NewGEP)->setIsInBounds(true);
10934           return NewGEP;
10935         }
10936         
10937         if (const ArrayType *XATy = dyn_cast<ArrayType>(XTy->getElementType())){
10938           // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
10939           if (CATy->getElementType() == XATy->getElementType()) {
10940             // -> GEP [10 x i8]* X, i32 0, ...
10941             // At this point, we know that the cast source type is a pointer
10942             // to an array of the same type as the destination pointer
10943             // array.  Because the array type is never stepped over (there
10944             // is a leading zero) we can fold the cast into this GEP.
10945             GEP.setOperand(0, X);
10946             return &GEP;
10947           }
10948         }
10949       }
10950     } else if (GEP.getNumOperands() == 2) {
10951       // Transform things like:
10952       // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
10953       // into:  %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
10954       const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
10955       const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
10956       if (TD && isa<ArrayType>(SrcElTy) &&
10957           TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
10958           TD->getTypeAllocSize(ResElTy)) {
10959         Value *Idx[2];
10960         Idx[0] = Constant::getNullValue(Type::getInt32Ty(*Context));
10961         Idx[1] = GEP.getOperand(1);
10962         Value *NewGEP =
10963           Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName());
10964         if (cast<GEPOperator>(&GEP)->isInBounds())
10965           cast<GEPOperator>(NewGEP)->setIsInBounds(true);
10966         // V and GEP are both pointer types --> BitCast
10967         return new BitCastInst(NewGEP, GEP.getType());
10968       }
10969       
10970       // Transform things like:
10971       // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
10972       //   (where tmp = 8*tmp2) into:
10973       // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
10974       
10975       if (TD && isa<ArrayType>(SrcElTy) && ResElTy == Type::getInt8Ty(*Context)) {
10976         uint64_t ArrayEltSize =
10977             TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
10978         
10979         // Check to see if "tmp" is a scale by a multiple of ArrayEltSize.  We
10980         // allow either a mul, shift, or constant here.
10981         Value *NewIdx = 0;
10982         ConstantInt *Scale = 0;
10983         if (ArrayEltSize == 1) {
10984           NewIdx = GEP.getOperand(1);
10985           Scale = ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1);
10986         } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
10987           NewIdx = ConstantInt::get(CI->getType(), 1);
10988           Scale = CI;
10989         } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
10990           if (Inst->getOpcode() == Instruction::Shl &&
10991               isa<ConstantInt>(Inst->getOperand(1))) {
10992             ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
10993             uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
10994             Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()),
10995                                      1ULL << ShAmtVal);
10996             NewIdx = Inst->getOperand(0);
10997           } else if (Inst->getOpcode() == Instruction::Mul &&
10998                      isa<ConstantInt>(Inst->getOperand(1))) {
10999             Scale = cast<ConstantInt>(Inst->getOperand(1));
11000             NewIdx = Inst->getOperand(0);
11001           }
11002         }
11003         
11004         // If the index will be to exactly the right offset with the scale taken
11005         // out, perform the transformation. Note, we don't know whether Scale is
11006         // signed or not. We'll use unsigned version of division/modulo
11007         // operation after making sure Scale doesn't have the sign bit set.
11008         if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
11009             Scale->getZExtValue() % ArrayEltSize == 0) {
11010           Scale = ConstantInt::get(Scale->getType(),
11011                                    Scale->getZExtValue() / ArrayEltSize);
11012           if (Scale->getZExtValue() != 1) {
11013             Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
11014                                                        false /*ZExt*/);
11015             NewIdx = Builder->CreateMul(NewIdx, C, "idxscale");
11016           }
11017
11018           // Insert the new GEP instruction.
11019           Value *Idx[2];
11020           Idx[0] = Constant::getNullValue(Type::getInt32Ty(*Context));
11021           Idx[1] = NewIdx;
11022           Value *NewGEP = Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName());
11023           if (cast<GEPOperator>(&GEP)->isInBounds())
11024             cast<GEPOperator>(NewGEP)->setIsInBounds(true);
11025           // The NewGEP must be pointer typed, so must the old one -> BitCast
11026           return new BitCastInst(NewGEP, GEP.getType());
11027         }
11028       }
11029     }
11030   }
11031   
11032   /// See if we can simplify:
11033   ///   X = bitcast A* to B*
11034   ///   Y = gep X, <...constant indices...>
11035   /// into a gep of the original struct.  This is important for SROA and alias
11036   /// analysis of unions.  If "A" is also a bitcast, wait for A/X to be merged.
11037   if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
11038     if (TD &&
11039         !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) {
11040       // Determine how much the GEP moves the pointer.  We are guaranteed to get
11041       // a constant back from EmitGEPOffset.
11042       ConstantInt *OffsetV =
11043                     cast<ConstantInt>(EmitGEPOffset(&GEP, GEP, *this));
11044       int64_t Offset = OffsetV->getSExtValue();
11045       
11046       // If this GEP instruction doesn't move the pointer, just replace the GEP
11047       // with a bitcast of the real input to the dest type.
11048       if (Offset == 0) {
11049         // If the bitcast is of an allocation, and the allocation will be
11050         // converted to match the type of the cast, don't touch this.
11051         if (isa<AllocationInst>(BCI->getOperand(0))) {
11052           // See if the bitcast simplifies, if so, don't nuke this GEP yet.
11053           if (Instruction *I = visitBitCast(*BCI)) {
11054             if (I != BCI) {
11055               I->takeName(BCI);
11056               BCI->getParent()->getInstList().insert(BCI, I);
11057               ReplaceInstUsesWith(*BCI, I);
11058             }
11059             return &GEP;
11060           }
11061         }
11062         return new BitCastInst(BCI->getOperand(0), GEP.getType());
11063       }
11064       
11065       // Otherwise, if the offset is non-zero, we need to find out if there is a
11066       // field at Offset in 'A's type.  If so, we can pull the cast through the
11067       // GEP.
11068       SmallVector<Value*, 8> NewIndices;
11069       const Type *InTy =
11070         cast<PointerType>(BCI->getOperand(0)->getType())->getElementType();
11071       if (FindElementAtOffset(InTy, Offset, NewIndices, TD, Context)) {
11072         Value *NGEP = Builder->CreateGEP(BCI->getOperand(0), NewIndices.begin(),
11073                                          NewIndices.end());
11074         if (cast<GEPOperator>(&GEP)->isInBounds())
11075           cast<GEPOperator>(NGEP)->setIsInBounds(true);
11076         
11077         if (NGEP->getType() == GEP.getType())
11078           return ReplaceInstUsesWith(GEP, NGEP);
11079         NGEP->takeName(&GEP);
11080         return new BitCastInst(NGEP, GEP.getType());
11081       }
11082     }
11083   }    
11084     
11085   return 0;
11086 }
11087
11088 Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
11089   // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
11090   if (AI.isArrayAllocation()) {  // Check C != 1
11091     if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
11092       const Type *NewTy = 
11093         ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
11094       AllocationInst *New = 0;
11095
11096       // Create and insert the replacement instruction...
11097       if (isa<MallocInst>(AI))
11098         New = Builder->CreateMalloc(NewTy, 0, AI.getName());
11099       else {
11100         assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
11101         New = Builder->CreateAlloca(NewTy, 0, AI.getName());
11102       }
11103       New->setAlignment(AI.getAlignment());
11104
11105       // Scan to the end of the allocation instructions, to skip over a block of
11106       // allocas if possible...also skip interleaved debug info
11107       //
11108       BasicBlock::iterator It = New;
11109       while (isa<AllocationInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
11110
11111       // Now that I is pointing to the first non-allocation-inst in the block,
11112       // insert our getelementptr instruction...
11113       //
11114       Value *NullIdx = Constant::getNullValue(Type::getInt32Ty(*Context));
11115       Value *Idx[2];
11116       Idx[0] = NullIdx;
11117       Idx[1] = NullIdx;
11118       Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2,
11119                                            New->getName()+".sub", It);
11120       cast<GEPOperator>(V)->setIsInBounds(true);
11121
11122       // Now make everything use the getelementptr instead of the original
11123       // allocation.
11124       return ReplaceInstUsesWith(AI, V);
11125     } else if (isa<UndefValue>(AI.getArraySize())) {
11126       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
11127     }
11128   }
11129
11130   if (TD && isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) {
11131     // If alloca'ing a zero byte object, replace the alloca with a null pointer.
11132     // Note that we only do this for alloca's, because malloc should allocate
11133     // and return a unique pointer, even for a zero byte allocation.
11134     if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0)
11135       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
11136
11137     // If the alignment is 0 (unspecified), assign it the preferred alignment.
11138     if (AI.getAlignment() == 0)
11139       AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType()));
11140   }
11141
11142   return 0;
11143 }
11144
11145 Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
11146   Value *Op = FI.getOperand(0);
11147
11148   // free undef -> unreachable.
11149   if (isa<UndefValue>(Op)) {
11150     // Insert a new store to null because we cannot modify the CFG here.
11151     new StoreInst(ConstantInt::getTrue(*Context),
11152            UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))), &FI);
11153     return EraseInstFromFunction(FI);
11154   }
11155   
11156   // If we have 'free null' delete the instruction.  This can happen in stl code
11157   // when lots of inlining happens.
11158   if (isa<ConstantPointerNull>(Op))
11159     return EraseInstFromFunction(FI);
11160   
11161   // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X
11162   if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) {
11163     FI.setOperand(0, CI->getOperand(0));
11164     return &FI;
11165   }
11166   
11167   // Change free (gep X, 0,0,0,0) into free(X)
11168   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
11169     if (GEPI->hasAllZeroIndices()) {
11170       Worklist.Add(GEPI);
11171       FI.setOperand(0, GEPI->getOperand(0));
11172       return &FI;
11173     }
11174   }
11175   
11176   // Change free(malloc) into nothing, if the malloc has a single use.
11177   if (MallocInst *MI = dyn_cast<MallocInst>(Op))
11178     if (MI->hasOneUse()) {
11179       EraseInstFromFunction(FI);
11180       return EraseInstFromFunction(*MI);
11181     }
11182
11183   return 0;
11184 }
11185
11186
11187 /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
11188 static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
11189                                         const TargetData *TD) {
11190   User *CI = cast<User>(LI.getOperand(0));
11191   Value *CastOp = CI->getOperand(0);
11192   LLVMContext *Context = IC.getContext();
11193
11194   if (TD) {
11195     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
11196       // Instead of loading constant c string, use corresponding integer value
11197       // directly if string length is small enough.
11198       std::string Str;
11199       if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) {
11200         unsigned len = Str.length();
11201         const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
11202         unsigned numBits = Ty->getPrimitiveSizeInBits();
11203         // Replace LI with immediate integer store.
11204         if ((numBits >> 3) == len + 1) {
11205           APInt StrVal(numBits, 0);
11206           APInt SingleChar(numBits, 0);
11207           if (TD->isLittleEndian()) {
11208             for (signed i = len-1; i >= 0; i--) {
11209               SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
11210               StrVal = (StrVal << 8) | SingleChar;
11211             }
11212           } else {
11213             for (unsigned i = 0; i < len; i++) {
11214               SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
11215               StrVal = (StrVal << 8) | SingleChar;
11216             }
11217             // Append NULL at the end.
11218             SingleChar = 0;
11219             StrVal = (StrVal << 8) | SingleChar;
11220           }
11221           Value *NL = ConstantInt::get(*Context, StrVal);
11222           return IC.ReplaceInstUsesWith(LI, NL);
11223         }
11224       }
11225     }
11226   }
11227
11228   const PointerType *DestTy = cast<PointerType>(CI->getType());
11229   const Type *DestPTy = DestTy->getElementType();
11230   if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
11231
11232     // If the address spaces don't match, don't eliminate the cast.
11233     if (DestTy->getAddressSpace() != SrcTy->getAddressSpace())
11234       return 0;
11235
11236     const Type *SrcPTy = SrcTy->getElementType();
11237
11238     if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || 
11239          isa<VectorType>(DestPTy)) {
11240       // If the source is an array, the code below will not succeed.  Check to
11241       // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
11242       // constants.
11243       if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
11244         if (Constant *CSrc = dyn_cast<Constant>(CastOp))
11245           if (ASrcTy->getNumElements() != 0) {
11246             Value *Idxs[2];
11247             Idxs[0] = Idxs[1] = Constant::getNullValue(Type::getInt32Ty(*Context));
11248             CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
11249             SrcTy = cast<PointerType>(CastOp->getType());
11250             SrcPTy = SrcTy->getElementType();
11251           }
11252
11253       if (IC.getTargetData() &&
11254           (SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || 
11255             isa<VectorType>(SrcPTy)) &&
11256           // Do not allow turning this into a load of an integer, which is then
11257           // casted to a pointer, this pessimizes pointer analysis a lot.
11258           (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) &&
11259           IC.getTargetData()->getTypeSizeInBits(SrcPTy) ==
11260                IC.getTargetData()->getTypeSizeInBits(DestPTy)) {
11261
11262         // Okay, we are casting from one integer or pointer type to another of
11263         // the same size.  Instead of casting the pointer before the load, cast
11264         // the result of the loaded value.
11265         Value *NewLoad = 
11266           IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName());
11267         // Now cast the result of the load.
11268         return new BitCastInst(NewLoad, LI.getType());
11269       }
11270     }
11271   }
11272   return 0;
11273 }
11274
11275 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
11276   Value *Op = LI.getOperand(0);
11277
11278   // Attempt to improve the alignment.
11279   if (TD) {
11280     unsigned KnownAlign =
11281       GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()));
11282     if (KnownAlign >
11283         (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) :
11284                                   LI.getAlignment()))
11285       LI.setAlignment(KnownAlign);
11286   }
11287
11288   // load (cast X) --> cast (load X) iff safe.
11289   if (isa<CastInst>(Op))
11290     if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
11291       return Res;
11292
11293   // None of the following transforms are legal for volatile loads.
11294   if (LI.isVolatile()) return 0;
11295   
11296   // Do really simple store-to-load forwarding and load CSE, to catch cases
11297   // where there are several consequtive memory accesses to the same location,
11298   // separated by a few arithmetic operations.
11299   BasicBlock::iterator BBI = &LI;
11300   if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
11301     return ReplaceInstUsesWith(LI, AvailableVal);
11302
11303   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
11304     const Value *GEPI0 = GEPI->getOperand(0);
11305     // TODO: Consider a target hook for valid address spaces for this xform.
11306     if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
11307       // Insert a new store to null instruction before the load to indicate
11308       // that this code is not reachable.  We do this instead of inserting
11309       // an unreachable instruction directly because we cannot modify the
11310       // CFG.
11311       new StoreInst(UndefValue::get(LI.getType()),
11312                     Constant::getNullValue(Op->getType()), &LI);
11313       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11314     }
11315   } 
11316
11317   if (Constant *C = dyn_cast<Constant>(Op)) {
11318     // load null/undef -> undef
11319     // TODO: Consider a target hook for valid address spaces for this xform.
11320     if (isa<UndefValue>(C) ||
11321         (C->isNullValue() && LI.getPointerAddressSpace() == 0)) {
11322       // Insert a new store to null instruction before the load to indicate that
11323       // this code is not reachable.  We do this instead of inserting an
11324       // unreachable instruction directly because we cannot modify the CFG.
11325       new StoreInst(UndefValue::get(LI.getType()),
11326                     Constant::getNullValue(Op->getType()), &LI);
11327       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11328     }
11329
11330     // Instcombine load (constant global) into the value loaded.
11331     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
11332       if (GV->isConstant() && GV->hasDefinitiveInitializer())
11333         return ReplaceInstUsesWith(LI, GV->getInitializer());
11334
11335     // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded.
11336     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) {
11337       if (CE->getOpcode() == Instruction::GetElementPtr) {
11338         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
11339           if (GV->isConstant() && GV->hasDefinitiveInitializer())
11340             if (Constant *V = 
11341                ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE, 
11342                                                       *Context))
11343               return ReplaceInstUsesWith(LI, V);
11344         if (CE->getOperand(0)->isNullValue()) {
11345           // Insert a new store to null instruction before the load to indicate
11346           // that this code is not reachable.  We do this instead of inserting
11347           // an unreachable instruction directly because we cannot modify the
11348           // CFG.
11349           new StoreInst(UndefValue::get(LI.getType()),
11350                         Constant::getNullValue(Op->getType()), &LI);
11351           return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11352         }
11353
11354       } else if (CE->isCast()) {
11355         if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
11356           return Res;
11357       }
11358     }
11359   }
11360     
11361   // If this load comes from anywhere in a constant global, and if the global
11362   // is all undef or zero, we know what it loads.
11363   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){
11364     if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
11365       if (GV->getInitializer()->isNullValue())
11366         return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
11367       else if (isa<UndefValue>(GV->getInitializer()))
11368         return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11369     }
11370   }
11371
11372   if (Op->hasOneUse()) {
11373     // Change select and PHI nodes to select values instead of addresses: this
11374     // helps alias analysis out a lot, allows many others simplifications, and
11375     // exposes redundancy in the code.
11376     //
11377     // Note that we cannot do the transformation unless we know that the
11378     // introduced loads cannot trap!  Something like this is valid as long as
11379     // the condition is always false: load (select bool %C, int* null, int* %G),
11380     // but it would not be valid if we transformed it to load from null
11381     // unconditionally.
11382     //
11383     if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
11384       // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2).
11385       if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) &&
11386           isSafeToLoadUnconditionally(SI->getOperand(2), SI)) {
11387         Value *V1 = Builder->CreateLoad(SI->getOperand(1),
11388                                         SI->getOperand(1)->getName()+".val");
11389         Value *V2 = Builder->CreateLoad(SI->getOperand(2),
11390                                         SI->getOperand(2)->getName()+".val");
11391         return SelectInst::Create(SI->getCondition(), V1, V2);
11392       }
11393
11394       // load (select (cond, null, P)) -> load P
11395       if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
11396         if (C->isNullValue()) {
11397           LI.setOperand(0, SI->getOperand(2));
11398           return &LI;
11399         }
11400
11401       // load (select (cond, P, null)) -> load P
11402       if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
11403         if (C->isNullValue()) {
11404           LI.setOperand(0, SI->getOperand(1));
11405           return &LI;
11406         }
11407     }
11408   }
11409   return 0;
11410 }
11411
11412 /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
11413 /// when possible.  This makes it generally easy to do alias analysis and/or
11414 /// SROA/mem2reg of the memory object.
11415 static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
11416   User *CI = cast<User>(SI.getOperand(1));
11417   Value *CastOp = CI->getOperand(0);
11418
11419   const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
11420   const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
11421   if (SrcTy == 0) return 0;
11422   
11423   const Type *SrcPTy = SrcTy->getElementType();
11424
11425   if (!DestPTy->isInteger() && !isa<PointerType>(DestPTy))
11426     return 0;
11427   
11428   /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
11429   /// to its first element.  This allows us to handle things like:
11430   ///   store i32 xxx, (bitcast {foo*, float}* %P to i32*)
11431   /// on 32-bit hosts.
11432   SmallVector<Value*, 4> NewGEPIndices;
11433   
11434   // If the source is an array, the code below will not succeed.  Check to
11435   // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
11436   // constants.
11437   if (isa<ArrayType>(SrcPTy) || isa<StructType>(SrcPTy)) {
11438     // Index through pointer.
11439     Constant *Zero = Constant::getNullValue(Type::getInt32Ty(*IC.getContext()));
11440     NewGEPIndices.push_back(Zero);
11441     
11442     while (1) {
11443       if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) {
11444         if (!STy->getNumElements()) /* Struct can be empty {} */
11445           break;
11446         NewGEPIndices.push_back(Zero);
11447         SrcPTy = STy->getElementType(0);
11448       } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
11449         NewGEPIndices.push_back(Zero);
11450         SrcPTy = ATy->getElementType();
11451       } else {
11452         break;
11453       }
11454     }
11455     
11456     SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
11457   }
11458
11459   if (!SrcPTy->isInteger() && !isa<PointerType>(SrcPTy))
11460     return 0;
11461   
11462   // If the pointers point into different address spaces or if they point to
11463   // values with different sizes, we can't do the transformation.
11464   if (!IC.getTargetData() ||
11465       SrcTy->getAddressSpace() != 
11466         cast<PointerType>(CI->getType())->getAddressSpace() ||
11467       IC.getTargetData()->getTypeSizeInBits(SrcPTy) !=
11468       IC.getTargetData()->getTypeSizeInBits(DestPTy))
11469     return 0;
11470
11471   // Okay, we are casting from one integer or pointer type to another of
11472   // the same size.  Instead of casting the pointer before 
11473   // the store, cast the value to be stored.
11474   Value *NewCast;
11475   Value *SIOp0 = SI.getOperand(0);
11476   Instruction::CastOps opcode = Instruction::BitCast;
11477   const Type* CastSrcTy = SIOp0->getType();
11478   const Type* CastDstTy = SrcPTy;
11479   if (isa<PointerType>(CastDstTy)) {
11480     if (CastSrcTy->isInteger())
11481       opcode = Instruction::IntToPtr;
11482   } else if (isa<IntegerType>(CastDstTy)) {
11483     if (isa<PointerType>(SIOp0->getType()))
11484       opcode = Instruction::PtrToInt;
11485   }
11486   
11487   // SIOp0 is a pointer to aggregate and this is a store to the first field,
11488   // emit a GEP to index into its first field.
11489   if (!NewGEPIndices.empty()) {
11490     CastOp = IC.Builder->CreateGEP(CastOp, NewGEPIndices.begin(),
11491                                    NewGEPIndices.end());
11492     cast<GEPOperator>(CastOp)->setIsInBounds(true);
11493   }
11494   
11495   NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy,
11496                                    SIOp0->getName()+".c");
11497   return new StoreInst(NewCast, CastOp);
11498 }
11499
11500 /// equivalentAddressValues - Test if A and B will obviously have the same
11501 /// value. This includes recognizing that %t0 and %t1 will have the same
11502 /// value in code like this:
11503 ///   %t0 = getelementptr \@a, 0, 3
11504 ///   store i32 0, i32* %t0
11505 ///   %t1 = getelementptr \@a, 0, 3
11506 ///   %t2 = load i32* %t1
11507 ///
11508 static bool equivalentAddressValues(Value *A, Value *B) {
11509   // Test if the values are trivially equivalent.
11510   if (A == B) return true;
11511   
11512   // Test if the values come form identical arithmetic instructions.
11513   // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
11514   // its only used to compare two uses within the same basic block, which
11515   // means that they'll always either have the same value or one of them
11516   // will have an undefined value.
11517   if (isa<BinaryOperator>(A) ||
11518       isa<CastInst>(A) ||
11519       isa<PHINode>(A) ||
11520       isa<GetElementPtrInst>(A))
11521     if (Instruction *BI = dyn_cast<Instruction>(B))
11522       if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
11523         return true;
11524   
11525   // Otherwise they may not be equivalent.
11526   return false;
11527 }
11528
11529 // If this instruction has two uses, one of which is a llvm.dbg.declare,
11530 // return the llvm.dbg.declare.
11531 DbgDeclareInst *InstCombiner::hasOneUsePlusDeclare(Value *V) {
11532   if (!V->hasNUses(2))
11533     return 0;
11534   for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
11535        UI != E; ++UI) {
11536     if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI))
11537       return DI;
11538     if (isa<BitCastInst>(UI) && UI->hasOneUse()) {
11539       if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI->use_begin()))
11540         return DI;
11541       }
11542   }
11543   return 0;
11544 }
11545
11546 Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
11547   Value *Val = SI.getOperand(0);
11548   Value *Ptr = SI.getOperand(1);
11549
11550   if (isa<UndefValue>(Ptr)) {     // store X, undef -> noop (even if volatile)
11551     EraseInstFromFunction(SI);
11552     ++NumCombined;
11553     return 0;
11554   }
11555   
11556   // If the RHS is an alloca with a single use, zapify the store, making the
11557   // alloca dead.
11558   // If the RHS is an alloca with a two uses, the other one being a 
11559   // llvm.dbg.declare, zapify the store and the declare, making the
11560   // alloca dead.  We must do this to prevent declare's from affecting
11561   // codegen.
11562   if (!SI.isVolatile()) {
11563     if (Ptr->hasOneUse()) {
11564       if (isa<AllocaInst>(Ptr)) {
11565         EraseInstFromFunction(SI);
11566         ++NumCombined;
11567         return 0;
11568       }
11569       if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
11570         if (isa<AllocaInst>(GEP->getOperand(0))) {
11571           if (GEP->getOperand(0)->hasOneUse()) {
11572             EraseInstFromFunction(SI);
11573             ++NumCombined;
11574             return 0;
11575           }
11576           if (DbgDeclareInst *DI = hasOneUsePlusDeclare(GEP->getOperand(0))) {
11577             EraseInstFromFunction(*DI);
11578             EraseInstFromFunction(SI);
11579             ++NumCombined;
11580             return 0;
11581           }
11582         }
11583       }
11584     }
11585     if (DbgDeclareInst *DI = hasOneUsePlusDeclare(Ptr)) {
11586       EraseInstFromFunction(*DI);
11587       EraseInstFromFunction(SI);
11588       ++NumCombined;
11589       return 0;
11590     }
11591   }
11592
11593   // Attempt to improve the alignment.
11594   if (TD) {
11595     unsigned KnownAlign =
11596       GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType()));
11597     if (KnownAlign >
11598         (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) :
11599                                   SI.getAlignment()))
11600       SI.setAlignment(KnownAlign);
11601   }
11602
11603   // Do really simple DSE, to catch cases where there are several consecutive
11604   // stores to the same location, separated by a few arithmetic operations. This
11605   // situation often occurs with bitfield accesses.
11606   BasicBlock::iterator BBI = &SI;
11607   for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
11608        --ScanInsts) {
11609     --BBI;
11610     // Don't count debug info directives, lest they affect codegen,
11611     // and we skip pointer-to-pointer bitcasts, which are NOPs.
11612     // It is necessary for correctness to skip those that feed into a
11613     // llvm.dbg.declare, as these are not present when debugging is off.
11614     if (isa<DbgInfoIntrinsic>(BBI) ||
11615         (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) {
11616       ScanInsts++;
11617       continue;
11618     }    
11619     
11620     if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
11621       // Prev store isn't volatile, and stores to the same location?
11622       if (!PrevSI->isVolatile() &&equivalentAddressValues(PrevSI->getOperand(1),
11623                                                           SI.getOperand(1))) {
11624         ++NumDeadStore;
11625         ++BBI;
11626         EraseInstFromFunction(*PrevSI);
11627         continue;
11628       }
11629       break;
11630     }
11631     
11632     // If this is a load, we have to stop.  However, if the loaded value is from
11633     // the pointer we're loading and is producing the pointer we're storing,
11634     // then *this* store is dead (X = load P; store X -> P).
11635     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
11636       if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
11637           !SI.isVolatile()) {
11638         EraseInstFromFunction(SI);
11639         ++NumCombined;
11640         return 0;
11641       }
11642       // Otherwise, this is a load from some other location.  Stores before it
11643       // may not be dead.
11644       break;
11645     }
11646     
11647     // Don't skip over loads or things that can modify memory.
11648     if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
11649       break;
11650   }
11651   
11652   
11653   if (SI.isVolatile()) return 0;  // Don't hack volatile stores.
11654
11655   // store X, null    -> turns into 'unreachable' in SimplifyCFG
11656   if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
11657     if (!isa<UndefValue>(Val)) {
11658       SI.setOperand(0, UndefValue::get(Val->getType()));
11659       if (Instruction *U = dyn_cast<Instruction>(Val))
11660         Worklist.Add(U);  // Dropped a use.
11661       ++NumCombined;
11662     }
11663     return 0;  // Do not modify these!
11664   }
11665
11666   // store undef, Ptr -> noop
11667   if (isa<UndefValue>(Val)) {
11668     EraseInstFromFunction(SI);
11669     ++NumCombined;
11670     return 0;
11671   }
11672
11673   // If the pointer destination is a cast, see if we can fold the cast into the
11674   // source instead.
11675   if (isa<CastInst>(Ptr))
11676     if (Instruction *Res = InstCombineStoreToCast(*this, SI))
11677       return Res;
11678   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
11679     if (CE->isCast())
11680       if (Instruction *Res = InstCombineStoreToCast(*this, SI))
11681         return Res;
11682
11683   
11684   // If this store is the last instruction in the basic block (possibly
11685   // excepting debug info instructions and the pointer bitcasts that feed
11686   // into them), and if the block ends with an unconditional branch, try
11687   // to move it to the successor block.
11688   BBI = &SI; 
11689   do {
11690     ++BBI;
11691   } while (isa<DbgInfoIntrinsic>(BBI) ||
11692            (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType())));
11693   if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
11694     if (BI->isUnconditional())
11695       if (SimplifyStoreAtEndOfBlock(SI))
11696         return 0;  // xform done!
11697   
11698   return 0;
11699 }
11700
11701 /// SimplifyStoreAtEndOfBlock - Turn things like:
11702 ///   if () { *P = v1; } else { *P = v2 }
11703 /// into a phi node with a store in the successor.
11704 ///
11705 /// Simplify things like:
11706 ///   *P = v1; if () { *P = v2; }
11707 /// into a phi node with a store in the successor.
11708 ///
11709 bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
11710   BasicBlock *StoreBB = SI.getParent();
11711   
11712   // Check to see if the successor block has exactly two incoming edges.  If
11713   // so, see if the other predecessor contains a store to the same location.
11714   // if so, insert a PHI node (if needed) and move the stores down.
11715   BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
11716   
11717   // Determine whether Dest has exactly two predecessors and, if so, compute
11718   // the other predecessor.
11719   pred_iterator PI = pred_begin(DestBB);
11720   BasicBlock *OtherBB = 0;
11721   if (*PI != StoreBB)
11722     OtherBB = *PI;
11723   ++PI;
11724   if (PI == pred_end(DestBB))
11725     return false;
11726   
11727   if (*PI != StoreBB) {
11728     if (OtherBB)
11729       return false;
11730     OtherBB = *PI;
11731   }
11732   if (++PI != pred_end(DestBB))
11733     return false;
11734
11735   // Bail out if all the relevant blocks aren't distinct (this can happen,
11736   // for example, if SI is in an infinite loop)
11737   if (StoreBB == DestBB || OtherBB == DestBB)
11738     return false;
11739
11740   // Verify that the other block ends in a branch and is not otherwise empty.
11741   BasicBlock::iterator BBI = OtherBB->getTerminator();
11742   BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
11743   if (!OtherBr || BBI == OtherBB->begin())
11744     return false;
11745   
11746   // If the other block ends in an unconditional branch, check for the 'if then
11747   // else' case.  there is an instruction before the branch.
11748   StoreInst *OtherStore = 0;
11749   if (OtherBr->isUnconditional()) {
11750     --BBI;
11751     // Skip over debugging info.
11752     while (isa<DbgInfoIntrinsic>(BBI) ||
11753            (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) {
11754       if (BBI==OtherBB->begin())
11755         return false;
11756       --BBI;
11757     }
11758     // If this isn't a store, or isn't a store to the same location, bail out.
11759     OtherStore = dyn_cast<StoreInst>(BBI);
11760     if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1))
11761       return false;
11762   } else {
11763     // Otherwise, the other block ended with a conditional branch. If one of the
11764     // destinations is StoreBB, then we have the if/then case.
11765     if (OtherBr->getSuccessor(0) != StoreBB && 
11766         OtherBr->getSuccessor(1) != StoreBB)
11767       return false;
11768     
11769     // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
11770     // if/then triangle.  See if there is a store to the same ptr as SI that
11771     // lives in OtherBB.
11772     for (;; --BBI) {
11773       // Check to see if we find the matching store.
11774       if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
11775         if (OtherStore->getOperand(1) != SI.getOperand(1))
11776           return false;
11777         break;
11778       }
11779       // If we find something that may be using or overwriting the stored
11780       // value, or if we run out of instructions, we can't do the xform.
11781       if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
11782           BBI == OtherBB->begin())
11783         return false;
11784     }
11785     
11786     // In order to eliminate the store in OtherBr, we have to
11787     // make sure nothing reads or overwrites the stored value in
11788     // StoreBB.
11789     for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
11790       // FIXME: This should really be AA driven.
11791       if (I->mayReadFromMemory() || I->mayWriteToMemory())
11792         return false;
11793     }
11794   }
11795   
11796   // Insert a PHI node now if we need it.
11797   Value *MergedVal = OtherStore->getOperand(0);
11798   if (MergedVal != SI.getOperand(0)) {
11799     PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge");
11800     PN->reserveOperandSpace(2);
11801     PN->addIncoming(SI.getOperand(0), SI.getParent());
11802     PN->addIncoming(OtherStore->getOperand(0), OtherBB);
11803     MergedVal = InsertNewInstBefore(PN, DestBB->front());
11804   }
11805   
11806   // Advance to a place where it is safe to insert the new store and
11807   // insert it.
11808   BBI = DestBB->getFirstNonPHI();
11809   InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
11810                                     OtherStore->isVolatile()), *BBI);
11811   
11812   // Nuke the old stores.
11813   EraseInstFromFunction(SI);
11814   EraseInstFromFunction(*OtherStore);
11815   ++NumCombined;
11816   return true;
11817 }
11818
11819
11820 Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
11821   // Change br (not X), label True, label False to: br X, label False, True
11822   Value *X = 0;
11823   BasicBlock *TrueDest;
11824   BasicBlock *FalseDest;
11825   if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
11826       !isa<Constant>(X)) {
11827     // Swap Destinations and condition...
11828     BI.setCondition(X);
11829     BI.setSuccessor(0, FalseDest);
11830     BI.setSuccessor(1, TrueDest);
11831     return &BI;
11832   }
11833
11834   // Cannonicalize fcmp_one -> fcmp_oeq
11835   FCmpInst::Predicate FPred; Value *Y;
11836   if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), 
11837                              TrueDest, FalseDest)) &&
11838       BI.getCondition()->hasOneUse())
11839     if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
11840         FPred == FCmpInst::FCMP_OGE) {
11841       FCmpInst *Cond = cast<FCmpInst>(BI.getCondition());
11842       Cond->setPredicate(FCmpInst::getInversePredicate(FPred));
11843       
11844       // Swap Destinations and condition.
11845       BI.setSuccessor(0, FalseDest);
11846       BI.setSuccessor(1, TrueDest);
11847       Worklist.Add(Cond);
11848       return &BI;
11849     }
11850
11851   // Cannonicalize icmp_ne -> icmp_eq
11852   ICmpInst::Predicate IPred;
11853   if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
11854                       TrueDest, FalseDest)) &&
11855       BI.getCondition()->hasOneUse())
11856     if (IPred == ICmpInst::ICMP_NE  || IPred == ICmpInst::ICMP_ULE ||
11857         IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
11858         IPred == ICmpInst::ICMP_SGE) {
11859       ICmpInst *Cond = cast<ICmpInst>(BI.getCondition());
11860       Cond->setPredicate(ICmpInst::getInversePredicate(IPred));
11861       // Swap Destinations and condition.
11862       BI.setSuccessor(0, FalseDest);
11863       BI.setSuccessor(1, TrueDest);
11864       Worklist.Add(Cond);
11865       return &BI;
11866     }
11867
11868   return 0;
11869 }
11870
11871 Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
11872   Value *Cond = SI.getCondition();
11873   if (Instruction *I = dyn_cast<Instruction>(Cond)) {
11874     if (I->getOpcode() == Instruction::Add)
11875       if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
11876         // change 'switch (X+4) case 1:' into 'switch (X) case -3'
11877         for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
11878           SI.setOperand(i,
11879                    ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
11880                                                 AddRHS));
11881         SI.setOperand(0, I->getOperand(0));
11882         Worklist.Add(I);
11883         return &SI;
11884       }
11885   }
11886   return 0;
11887 }
11888
11889 Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) {
11890   Value *Agg = EV.getAggregateOperand();
11891
11892   if (!EV.hasIndices())
11893     return ReplaceInstUsesWith(EV, Agg);
11894
11895   if (Constant *C = dyn_cast<Constant>(Agg)) {
11896     if (isa<UndefValue>(C))
11897       return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
11898       
11899     if (isa<ConstantAggregateZero>(C))
11900       return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
11901
11902     if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
11903       // Extract the element indexed by the first index out of the constant
11904       Value *V = C->getOperand(*EV.idx_begin());
11905       if (EV.getNumIndices() > 1)
11906         // Extract the remaining indices out of the constant indexed by the
11907         // first index
11908         return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end());
11909       else
11910         return ReplaceInstUsesWith(EV, V);
11911     }
11912     return 0; // Can't handle other constants
11913   } 
11914   if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
11915     // We're extracting from an insertvalue instruction, compare the indices
11916     const unsigned *exti, *exte, *insi, *inse;
11917     for (exti = EV.idx_begin(), insi = IV->idx_begin(),
11918          exte = EV.idx_end(), inse = IV->idx_end();
11919          exti != exte && insi != inse;
11920          ++exti, ++insi) {
11921       if (*insi != *exti)
11922         // The insert and extract both reference distinctly different elements.
11923         // This means the extract is not influenced by the insert, and we can
11924         // replace the aggregate operand of the extract with the aggregate
11925         // operand of the insert. i.e., replace
11926         // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
11927         // %E = extractvalue { i32, { i32 } } %I, 0
11928         // with
11929         // %E = extractvalue { i32, { i32 } } %A, 0
11930         return ExtractValueInst::Create(IV->getAggregateOperand(),
11931                                         EV.idx_begin(), EV.idx_end());
11932     }
11933     if (exti == exte && insi == inse)
11934       // Both iterators are at the end: Index lists are identical. Replace
11935       // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
11936       // %C = extractvalue { i32, { i32 } } %B, 1, 0
11937       // with "i32 42"
11938       return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand());
11939     if (exti == exte) {
11940       // The extract list is a prefix of the insert list. i.e. replace
11941       // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
11942       // %E = extractvalue { i32, { i32 } } %I, 1
11943       // with
11944       // %X = extractvalue { i32, { i32 } } %A, 1
11945       // %E = insertvalue { i32 } %X, i32 42, 0
11946       // by switching the order of the insert and extract (though the
11947       // insertvalue should be left in, since it may have other uses).
11948       Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
11949                                                  EV.idx_begin(), EV.idx_end());
11950       return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
11951                                      insi, inse);
11952     }
11953     if (insi == inse)
11954       // The insert list is a prefix of the extract list
11955       // We can simply remove the common indices from the extract and make it
11956       // operate on the inserted value instead of the insertvalue result.
11957       // i.e., replace
11958       // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
11959       // %E = extractvalue { i32, { i32 } } %I, 1, 0
11960       // with
11961       // %E extractvalue { i32 } { i32 42 }, 0
11962       return ExtractValueInst::Create(IV->getInsertedValueOperand(), 
11963                                       exti, exte);
11964   }
11965   // Can't simplify extracts from other values. Note that nested extracts are
11966   // already simplified implicitely by the above (extract ( extract (insert) )
11967   // will be translated into extract ( insert ( extract ) ) first and then just
11968   // the value inserted, if appropriate).
11969   return 0;
11970 }
11971
11972 /// CheapToScalarize - Return true if the value is cheaper to scalarize than it
11973 /// is to leave as a vector operation.
11974 static bool CheapToScalarize(Value *V, bool isConstant) {
11975   if (isa<ConstantAggregateZero>(V)) 
11976     return true;
11977   if (ConstantVector *C = dyn_cast<ConstantVector>(V)) {
11978     if (isConstant) return true;
11979     // If all elts are the same, we can extract.
11980     Constant *Op0 = C->getOperand(0);
11981     for (unsigned i = 1; i < C->getNumOperands(); ++i)
11982       if (C->getOperand(i) != Op0)
11983         return false;
11984     return true;
11985   }
11986   Instruction *I = dyn_cast<Instruction>(V);
11987   if (!I) return false;
11988   
11989   // Insert element gets simplified to the inserted element or is deleted if
11990   // this is constant idx extract element and its a constant idx insertelt.
11991   if (I->getOpcode() == Instruction::InsertElement && isConstant &&
11992       isa<ConstantInt>(I->getOperand(2)))
11993     return true;
11994   if (I->getOpcode() == Instruction::Load && I->hasOneUse())
11995     return true;
11996   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
11997     if (BO->hasOneUse() &&
11998         (CheapToScalarize(BO->getOperand(0), isConstant) ||
11999          CheapToScalarize(BO->getOperand(1), isConstant)))
12000       return true;
12001   if (CmpInst *CI = dyn_cast<CmpInst>(I))
12002     if (CI->hasOneUse() &&
12003         (CheapToScalarize(CI->getOperand(0), isConstant) ||
12004          CheapToScalarize(CI->getOperand(1), isConstant)))
12005       return true;
12006   
12007   return false;
12008 }
12009
12010 /// Read and decode a shufflevector mask.
12011 ///
12012 /// It turns undef elements into values that are larger than the number of
12013 /// elements in the input.
12014 static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) {
12015   unsigned NElts = SVI->getType()->getNumElements();
12016   if (isa<ConstantAggregateZero>(SVI->getOperand(2)))
12017     return std::vector<unsigned>(NElts, 0);
12018   if (isa<UndefValue>(SVI->getOperand(2)))
12019     return std::vector<unsigned>(NElts, 2*NElts);
12020
12021   std::vector<unsigned> Result;
12022   const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2));
12023   for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i)
12024     if (isa<UndefValue>(*i))
12025       Result.push_back(NElts*2);  // undef -> 8
12026     else
12027       Result.push_back(cast<ConstantInt>(*i)->getZExtValue());
12028   return Result;
12029 }
12030
12031 /// FindScalarElement - Given a vector and an element number, see if the scalar
12032 /// value is already around as a register, for example if it were inserted then
12033 /// extracted from the vector.
12034 static Value *FindScalarElement(Value *V, unsigned EltNo,
12035                                 LLVMContext *Context) {
12036   assert(isa<VectorType>(V->getType()) && "Not looking at a vector?");
12037   const VectorType *PTy = cast<VectorType>(V->getType());
12038   unsigned Width = PTy->getNumElements();
12039   if (EltNo >= Width)  // Out of range access.
12040     return UndefValue::get(PTy->getElementType());
12041   
12042   if (isa<UndefValue>(V))
12043     return UndefValue::get(PTy->getElementType());
12044   else if (isa<ConstantAggregateZero>(V))
12045     return Constant::getNullValue(PTy->getElementType());
12046   else if (ConstantVector *CP = dyn_cast<ConstantVector>(V))
12047     return CP->getOperand(EltNo);
12048   else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
12049     // If this is an insert to a variable element, we don't know what it is.
12050     if (!isa<ConstantInt>(III->getOperand(2))) 
12051       return 0;
12052     unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
12053     
12054     // If this is an insert to the element we are looking for, return the
12055     // inserted value.
12056     if (EltNo == IIElt) 
12057       return III->getOperand(1);
12058     
12059     // Otherwise, the insertelement doesn't modify the value, recurse on its
12060     // vector input.
12061     return FindScalarElement(III->getOperand(0), EltNo, Context);
12062   } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
12063     unsigned LHSWidth =
12064       cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
12065     unsigned InEl = getShuffleMask(SVI)[EltNo];
12066     if (InEl < LHSWidth)
12067       return FindScalarElement(SVI->getOperand(0), InEl, Context);
12068     else if (InEl < LHSWidth*2)
12069       return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth, Context);
12070     else
12071       return UndefValue::get(PTy->getElementType());
12072   }
12073   
12074   // Otherwise, we don't know.
12075   return 0;
12076 }
12077
12078 Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
12079   // If vector val is undef, replace extract with scalar undef.
12080   if (isa<UndefValue>(EI.getOperand(0)))
12081     return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
12082
12083   // If vector val is constant 0, replace extract with scalar 0.
12084   if (isa<ConstantAggregateZero>(EI.getOperand(0)))
12085     return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
12086   
12087   if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) {
12088     // If vector val is constant with all elements the same, replace EI with
12089     // that element. When the elements are not identical, we cannot replace yet
12090     // (we do that below, but only when the index is constant).
12091     Constant *op0 = C->getOperand(0);
12092     for (unsigned i = 1; i < C->getNumOperands(); ++i)
12093       if (C->getOperand(i) != op0) {
12094         op0 = 0; 
12095         break;
12096       }
12097     if (op0)
12098       return ReplaceInstUsesWith(EI, op0);
12099   }
12100   
12101   // If extracting a specified index from the vector, see if we can recursively
12102   // find a previously computed scalar that was inserted into the vector.
12103   if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
12104     unsigned IndexVal = IdxC->getZExtValue();
12105     unsigned VectorWidth = 
12106       cast<VectorType>(EI.getOperand(0)->getType())->getNumElements();
12107       
12108     // If this is extracting an invalid index, turn this into undef, to avoid
12109     // crashing the code below.
12110     if (IndexVal >= VectorWidth)
12111       return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
12112     
12113     // This instruction only demands the single element from the input vector.
12114     // If the input vector has a single use, simplify it based on this use
12115     // property.
12116     if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
12117       APInt UndefElts(VectorWidth, 0);
12118       APInt DemandedMask(VectorWidth, 1 << IndexVal);
12119       if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
12120                                                 DemandedMask, UndefElts)) {
12121         EI.setOperand(0, V);
12122         return &EI;
12123       }
12124     }
12125     
12126     if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal, Context))
12127       return ReplaceInstUsesWith(EI, Elt);
12128     
12129     // If the this extractelement is directly using a bitcast from a vector of
12130     // the same number of elements, see if we can find the source element from
12131     // it.  In this case, we will end up needing to bitcast the scalars.
12132     if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
12133       if (const VectorType *VT = 
12134               dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
12135         if (VT->getNumElements() == VectorWidth)
12136           if (Value *Elt = FindScalarElement(BCI->getOperand(0),
12137                                              IndexVal, Context))
12138             return new BitCastInst(Elt, EI.getType());
12139     }
12140   }
12141   
12142   if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
12143     if (I->hasOneUse()) {
12144       // Push extractelement into predecessor operation if legal and
12145       // profitable to do so
12146       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
12147         bool isConstantElt = isa<ConstantInt>(EI.getOperand(1));
12148         if (CheapToScalarize(BO, isConstantElt)) {
12149           Value *newEI0 =
12150             Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
12151                                           EI.getName()+".lhs");
12152           Value *newEI1 =
12153             Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
12154                                           EI.getName()+".rhs");
12155           return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
12156         }
12157       } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
12158         unsigned AS = LI->getPointerAddressSpace();
12159         Value *Ptr = Builder->CreateBitCast(I->getOperand(0),
12160                                             PointerType::get(EI.getType(), AS),
12161                                             I->getOperand(0)->getName());
12162         Value *GEP =
12163           Builder->CreateGEP(Ptr, EI.getOperand(1), I->getName()+".gep");
12164         cast<GEPOperator>(GEP)->setIsInBounds(true);
12165         
12166         LoadInst *Load = Builder->CreateLoad(GEP, "tmp");
12167
12168         // Make sure the Load goes before the load instruction in the source,
12169         // not wherever the extract happens to be.
12170         if (Instruction *P = dyn_cast<Instruction>(Ptr))
12171           P->moveBefore(I);
12172         if (Instruction *G = dyn_cast<Instruction>(GEP))
12173           G->moveBefore(I);
12174         Load->moveBefore(I);
12175         
12176         return ReplaceInstUsesWith(EI, Load);
12177       }
12178     }
12179     if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
12180       // Extracting the inserted element?
12181       if (IE->getOperand(2) == EI.getOperand(1))
12182         return ReplaceInstUsesWith(EI, IE->getOperand(1));
12183       // If the inserted and extracted elements are constants, they must not
12184       // be the same value, extract from the pre-inserted value instead.
12185       if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
12186         Worklist.AddValue(EI.getOperand(0));
12187         EI.setOperand(0, IE->getOperand(0));
12188         return &EI;
12189       }
12190     } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
12191       // If this is extracting an element from a shufflevector, figure out where
12192       // it came from and extract from the appropriate input element instead.
12193       if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
12194         unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()];
12195         Value *Src;
12196         unsigned LHSWidth =
12197           cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
12198
12199         if (SrcIdx < LHSWidth)
12200           Src = SVI->getOperand(0);
12201         else if (SrcIdx < LHSWidth*2) {
12202           SrcIdx -= LHSWidth;
12203           Src = SVI->getOperand(1);
12204         } else {
12205           return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
12206         }
12207         return ExtractElementInst::Create(Src,
12208                          ConstantInt::get(Type::getInt32Ty(*Context), SrcIdx,
12209                                           false));
12210       }
12211     }
12212     // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
12213   }
12214   return 0;
12215 }
12216
12217 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
12218 /// elements from either LHS or RHS, return the shuffle mask and true. 
12219 /// Otherwise, return false.
12220 static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
12221                                          std::vector<Constant*> &Mask,
12222                                          LLVMContext *Context) {
12223   assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
12224          "Invalid CollectSingleShuffleElements");
12225   unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
12226
12227   if (isa<UndefValue>(V)) {
12228     Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(*Context)));
12229     return true;
12230   } else if (V == LHS) {
12231     for (unsigned i = 0; i != NumElts; ++i)
12232       Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i));
12233     return true;
12234   } else if (V == RHS) {
12235     for (unsigned i = 0; i != NumElts; ++i)
12236       Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i+NumElts));
12237     return true;
12238   } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
12239     // If this is an insert of an extract from some other vector, include it.
12240     Value *VecOp    = IEI->getOperand(0);
12241     Value *ScalarOp = IEI->getOperand(1);
12242     Value *IdxOp    = IEI->getOperand(2);
12243     
12244     if (!isa<ConstantInt>(IdxOp))
12245       return false;
12246     unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
12247     
12248     if (isa<UndefValue>(ScalarOp)) {  // inserting undef into vector.
12249       // Okay, we can handle this if the vector we are insertinting into is
12250       // transitively ok.
12251       if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) {
12252         // If so, update the mask to reflect the inserted undef.
12253         Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(*Context));
12254         return true;
12255       }      
12256     } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
12257       if (isa<ConstantInt>(EI->getOperand(1)) &&
12258           EI->getOperand(0)->getType() == V->getType()) {
12259         unsigned ExtractedIdx =
12260           cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
12261         
12262         // This must be extracting from either LHS or RHS.
12263         if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
12264           // Okay, we can handle this if the vector we are insertinting into is
12265           // transitively ok.
12266           if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) {
12267             // If so, update the mask to reflect the inserted value.
12268             if (EI->getOperand(0) == LHS) {
12269               Mask[InsertedIdx % NumElts] = 
12270                  ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx);
12271             } else {
12272               assert(EI->getOperand(0) == RHS);
12273               Mask[InsertedIdx % NumElts] = 
12274                 ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx+NumElts);
12275               
12276             }
12277             return true;
12278           }
12279         }
12280       }
12281     }
12282   }
12283   // TODO: Handle shufflevector here!
12284   
12285   return false;
12286 }
12287
12288 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the
12289 /// RHS of the shuffle instruction, if it is not null.  Return a shuffle mask
12290 /// that computes V and the LHS value of the shuffle.
12291 static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
12292                                      Value *&RHS, LLVMContext *Context) {
12293   assert(isa<VectorType>(V->getType()) && 
12294          (RHS == 0 || V->getType() == RHS->getType()) &&
12295          "Invalid shuffle!");
12296   unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
12297
12298   if (isa<UndefValue>(V)) {
12299     Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(*Context)));
12300     return V;
12301   } else if (isa<ConstantAggregateZero>(V)) {
12302     Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(*Context), 0));
12303     return V;
12304   } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
12305     // If this is an insert of an extract from some other vector, include it.
12306     Value *VecOp    = IEI->getOperand(0);
12307     Value *ScalarOp = IEI->getOperand(1);
12308     Value *IdxOp    = IEI->getOperand(2);
12309     
12310     if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
12311       if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
12312           EI->getOperand(0)->getType() == V->getType()) {
12313         unsigned ExtractedIdx =
12314           cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
12315         unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
12316         
12317         // Either the extracted from or inserted into vector must be RHSVec,
12318         // otherwise we'd end up with a shuffle of three inputs.
12319         if (EI->getOperand(0) == RHS || RHS == 0) {
12320           RHS = EI->getOperand(0);
12321           Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context);
12322           Mask[InsertedIdx % NumElts] = 
12323             ConstantInt::get(Type::getInt32Ty(*Context), NumElts+ExtractedIdx);
12324           return V;
12325         }
12326         
12327         if (VecOp == RHS) {
12328           Value *V = CollectShuffleElements(EI->getOperand(0), Mask,
12329                                             RHS, Context);
12330           // Everything but the extracted element is replaced with the RHS.
12331           for (unsigned i = 0; i != NumElts; ++i) {
12332             if (i != InsertedIdx)
12333               Mask[i] = ConstantInt::get(Type::getInt32Ty(*Context), NumElts+i);
12334           }
12335           return V;
12336         }
12337         
12338         // If this insertelement is a chain that comes from exactly these two
12339         // vectors, return the vector and the effective shuffle.
12340         if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask,
12341                                          Context))
12342           return EI->getOperand(0);
12343         
12344       }
12345     }
12346   }
12347   // TODO: Handle shufflevector here!
12348   
12349   // Otherwise, can't do anything fancy.  Return an identity vector.
12350   for (unsigned i = 0; i != NumElts; ++i)
12351     Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i));
12352   return V;
12353 }
12354
12355 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
12356   Value *VecOp    = IE.getOperand(0);
12357   Value *ScalarOp = IE.getOperand(1);
12358   Value *IdxOp    = IE.getOperand(2);
12359   
12360   // Inserting an undef or into an undefined place, remove this.
12361   if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
12362     ReplaceInstUsesWith(IE, VecOp);
12363   
12364   // If the inserted element was extracted from some other vector, and if the 
12365   // indexes are constant, try to turn this into a shufflevector operation.
12366   if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
12367     if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
12368         EI->getOperand(0)->getType() == IE.getType()) {
12369       unsigned NumVectorElts = IE.getType()->getNumElements();
12370       unsigned ExtractedIdx =
12371         cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
12372       unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
12373       
12374       if (ExtractedIdx >= NumVectorElts) // Out of range extract.
12375         return ReplaceInstUsesWith(IE, VecOp);
12376       
12377       if (InsertedIdx >= NumVectorElts)  // Out of range insert.
12378         return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
12379       
12380       // If we are extracting a value from a vector, then inserting it right
12381       // back into the same place, just use the input vector.
12382       if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
12383         return ReplaceInstUsesWith(IE, VecOp);      
12384       
12385       // We could theoretically do this for ANY input.  However, doing so could
12386       // turn chains of insertelement instructions into a chain of shufflevector
12387       // instructions, and right now we do not merge shufflevectors.  As such,
12388       // only do this in a situation where it is clear that there is benefit.
12389       if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) {
12390         // Turn this into shuffle(EIOp0, VecOp, Mask).  The result has all of
12391         // the values of VecOp, except then one read from EIOp0.
12392         // Build a new shuffle mask.
12393         std::vector<Constant*> Mask;
12394         if (isa<UndefValue>(VecOp))
12395           Mask.assign(NumVectorElts, UndefValue::get(Type::getInt32Ty(*Context)));
12396         else {
12397           assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
12398           Mask.assign(NumVectorElts, ConstantInt::get(Type::getInt32Ty(*Context),
12399                                                        NumVectorElts));
12400         } 
12401         Mask[InsertedIdx] = 
12402                            ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx);
12403         return new ShuffleVectorInst(EI->getOperand(0), VecOp,
12404                                      ConstantVector::get(Mask));
12405       }
12406       
12407       // If this insertelement isn't used by some other insertelement, turn it
12408       // (and any insertelements it points to), into one big shuffle.
12409       if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
12410         std::vector<Constant*> Mask;
12411         Value *RHS = 0;
12412         Value *LHS = CollectShuffleElements(&IE, Mask, RHS, Context);
12413         if (RHS == 0) RHS = UndefValue::get(LHS->getType());
12414         // We now have a shuffle of LHS, RHS, Mask.
12415         return new ShuffleVectorInst(LHS, RHS,
12416                                      ConstantVector::get(Mask));
12417       }
12418     }
12419   }
12420
12421   unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
12422   APInt UndefElts(VWidth, 0);
12423   APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
12424   if (SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts))
12425     return &IE;
12426
12427   return 0;
12428 }
12429
12430
12431 Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
12432   Value *LHS = SVI.getOperand(0);
12433   Value *RHS = SVI.getOperand(1);
12434   std::vector<unsigned> Mask = getShuffleMask(&SVI);
12435
12436   bool MadeChange = false;
12437
12438   // Undefined shuffle mask -> undefined value.
12439   if (isa<UndefValue>(SVI.getOperand(2)))
12440     return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
12441
12442   unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
12443
12444   if (VWidth != cast<VectorType>(LHS->getType())->getNumElements())
12445     return 0;
12446
12447   APInt UndefElts(VWidth, 0);
12448   APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
12449   if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
12450     LHS = SVI.getOperand(0);
12451     RHS = SVI.getOperand(1);
12452     MadeChange = true;
12453   }
12454   
12455   // Canonicalize shuffle(x    ,x,mask) -> shuffle(x, undef,mask')
12456   // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
12457   if (LHS == RHS || isa<UndefValue>(LHS)) {
12458     if (isa<UndefValue>(LHS) && LHS == RHS) {
12459       // shuffle(undef,undef,mask) -> undef.
12460       return ReplaceInstUsesWith(SVI, LHS);
12461     }
12462     
12463     // Remap any references to RHS to use LHS.
12464     std::vector<Constant*> Elts;
12465     for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
12466       if (Mask[i] >= 2*e)
12467         Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
12468       else {
12469         if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
12470             (Mask[i] <  e && isa<UndefValue>(LHS))) {
12471           Mask[i] = 2*e;     // Turn into undef.
12472           Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
12473         } else {
12474           Mask[i] = Mask[i] % e;  // Force to LHS.
12475           Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context), Mask[i]));
12476         }
12477       }
12478     }
12479     SVI.setOperand(0, SVI.getOperand(1));
12480     SVI.setOperand(1, UndefValue::get(RHS->getType()));
12481     SVI.setOperand(2, ConstantVector::get(Elts));
12482     LHS = SVI.getOperand(0);
12483     RHS = SVI.getOperand(1);
12484     MadeChange = true;
12485   }
12486   
12487   // Analyze the shuffle, are the LHS or RHS and identity shuffles?
12488   bool isLHSID = true, isRHSID = true;
12489     
12490   for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
12491     if (Mask[i] >= e*2) continue;  // Ignore undef values.
12492     // Is this an identity shuffle of the LHS value?
12493     isLHSID &= (Mask[i] == i);
12494       
12495     // Is this an identity shuffle of the RHS value?
12496     isRHSID &= (Mask[i]-e == i);
12497   }
12498
12499   // Eliminate identity shuffles.
12500   if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
12501   if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
12502   
12503   // If the LHS is a shufflevector itself, see if we can combine it with this
12504   // one without producing an unusual shuffle.  Here we are really conservative:
12505   // we are absolutely afraid of producing a shuffle mask not in the input
12506   // program, because the code gen may not be smart enough to turn a merged
12507   // shuffle into two specific shuffles: it may produce worse code.  As such,
12508   // we only merge two shuffles if the result is one of the two input shuffle
12509   // masks.  In this case, merging the shuffles just removes one instruction,
12510   // which we know is safe.  This is good for things like turning:
12511   // (splat(splat)) -> splat.
12512   if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
12513     if (isa<UndefValue>(RHS)) {
12514       std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI);
12515
12516       std::vector<unsigned> NewMask;
12517       for (unsigned i = 0, e = Mask.size(); i != e; ++i)
12518         if (Mask[i] >= 2*e)
12519           NewMask.push_back(2*e);
12520         else
12521           NewMask.push_back(LHSMask[Mask[i]]);
12522       
12523       // If the result mask is equal to the src shuffle or this shuffle mask, do
12524       // the replacement.
12525       if (NewMask == LHSMask || NewMask == Mask) {
12526         unsigned LHSInNElts =
12527           cast<VectorType>(LHSSVI->getOperand(0)->getType())->getNumElements();
12528         std::vector<Constant*> Elts;
12529         for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
12530           if (NewMask[i] >= LHSInNElts*2) {
12531             Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
12532           } else {
12533             Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context), NewMask[i]));
12534           }
12535         }
12536         return new ShuffleVectorInst(LHSSVI->getOperand(0),
12537                                      LHSSVI->getOperand(1),
12538                                      ConstantVector::get(Elts));
12539       }
12540     }
12541   }
12542
12543   return MadeChange ? &SVI : 0;
12544 }
12545
12546
12547
12548
12549 /// TryToSinkInstruction - Try to move the specified instruction from its
12550 /// current block into the beginning of DestBlock, which can only happen if it's
12551 /// safe to move the instruction past all of the instructions between it and the
12552 /// end of its block.
12553 static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
12554   assert(I->hasOneUse() && "Invariants didn't hold!");
12555
12556   // Cannot move control-flow-involving, volatile loads, vaarg, etc.
12557   if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I))
12558     return false;
12559
12560   // Do not sink alloca instructions out of the entry block.
12561   if (isa<AllocaInst>(I) && I->getParent() ==
12562         &DestBlock->getParent()->getEntryBlock())
12563     return false;
12564
12565   // We can only sink load instructions if there is nothing between the load and
12566   // the end of block that could change the value.
12567   if (I->mayReadFromMemory()) {
12568     for (BasicBlock::iterator Scan = I, E = I->getParent()->end();
12569          Scan != E; ++Scan)
12570       if (Scan->mayWriteToMemory())
12571         return false;
12572   }
12573
12574   BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
12575
12576   CopyPrecedingStopPoint(I, InsertPos);
12577   I->moveBefore(InsertPos);
12578   ++NumSunkInst;
12579   return true;
12580 }
12581
12582
12583 /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
12584 /// all reachable code to the worklist.
12585 ///
12586 /// This has a couple of tricks to make the code faster and more powerful.  In
12587 /// particular, we constant fold and DCE instructions as we go, to avoid adding
12588 /// them to the worklist (this significantly speeds up instcombine on code where
12589 /// many instructions are dead or constant).  Additionally, if we find a branch
12590 /// whose condition is a known constant, we only visit the reachable successors.
12591 ///
12592 static void AddReachableCodeToWorklist(BasicBlock *BB, 
12593                                        SmallPtrSet<BasicBlock*, 64> &Visited,
12594                                        InstCombiner &IC,
12595                                        const TargetData *TD) {
12596   SmallVector<BasicBlock*, 256> Worklist;
12597   Worklist.push_back(BB);
12598
12599   while (!Worklist.empty()) {
12600     BB = Worklist.back();
12601     Worklist.pop_back();
12602     
12603     // We have now visited this block!  If we've already been here, ignore it.
12604     if (!Visited.insert(BB)) continue;
12605
12606     DbgInfoIntrinsic *DBI_Prev = NULL;
12607     for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
12608       Instruction *Inst = BBI++;
12609       
12610       // DCE instruction if trivially dead.
12611       if (isInstructionTriviallyDead(Inst)) {
12612         ++NumDeadInst;
12613         DEBUG(errs() << "IC: DCE: " << *Inst << '\n');
12614         Inst->eraseFromParent();
12615         continue;
12616       }
12617       
12618       // ConstantProp instruction if trivially constant.
12619       if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) {
12620         DEBUG(errs() << "IC: ConstFold to: " << *C << " from: "
12621                      << *Inst << '\n');
12622         Inst->replaceAllUsesWith(C);
12623         ++NumConstProp;
12624         Inst->eraseFromParent();
12625         continue;
12626       }
12627      
12628       // If there are two consecutive llvm.dbg.stoppoint calls then
12629       // it is likely that the optimizer deleted code in between these
12630       // two intrinsics. 
12631       DbgInfoIntrinsic *DBI_Next = dyn_cast<DbgInfoIntrinsic>(Inst);
12632       if (DBI_Next) {
12633         if (DBI_Prev
12634             && DBI_Prev->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint
12635             && DBI_Next->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint) {
12636           IC.Worklist.Remove(DBI_Prev);
12637           DBI_Prev->eraseFromParent();
12638         }
12639         DBI_Prev = DBI_Next;
12640       } else {
12641         DBI_Prev = 0;
12642       }
12643
12644       IC.Worklist.Add(Inst);
12645     }
12646
12647     // Recursively visit successors.  If this is a branch or switch on a
12648     // constant, only visit the reachable successor.
12649     TerminatorInst *TI = BB->getTerminator();
12650     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
12651       if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
12652         bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
12653         BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
12654         Worklist.push_back(ReachableBB);
12655         continue;
12656       }
12657     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
12658       if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
12659         // See if this is an explicit destination.
12660         for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
12661           if (SI->getCaseValue(i) == Cond) {
12662             BasicBlock *ReachableBB = SI->getSuccessor(i);
12663             Worklist.push_back(ReachableBB);
12664             continue;
12665           }
12666         
12667         // Otherwise it is the default destination.
12668         Worklist.push_back(SI->getSuccessor(0));
12669         continue;
12670       }
12671     }
12672     
12673     for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
12674       Worklist.push_back(TI->getSuccessor(i));
12675   }
12676 }
12677
12678 bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
12679   MadeIRChange = false;
12680   TD = getAnalysisIfAvailable<TargetData>();
12681   
12682   DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
12683         << F.getNameStr() << "\n");
12684
12685   {
12686     // Do a depth-first traversal of the function, populate the worklist with
12687     // the reachable instructions.  Ignore blocks that are not reachable.  Keep
12688     // track of which blocks we visit.
12689     SmallPtrSet<BasicBlock*, 64> Visited;
12690     AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
12691
12692     // Do a quick scan over the function.  If we find any blocks that are
12693     // unreachable, remove any instructions inside of them.  This prevents
12694     // the instcombine code from having to deal with some bad special cases.
12695     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
12696       if (!Visited.count(BB)) {
12697         Instruction *Term = BB->getTerminator();
12698         while (Term != BB->begin()) {   // Remove instrs bottom-up
12699           BasicBlock::iterator I = Term; --I;
12700
12701           DEBUG(errs() << "IC: DCE: " << *I << '\n');
12702           // A debug intrinsic shouldn't force another iteration if we weren't
12703           // going to do one without it.
12704           if (!isa<DbgInfoIntrinsic>(I)) {
12705             ++NumDeadInst;
12706             MadeIRChange = true;
12707           }
12708           if (!I->use_empty())
12709             I->replaceAllUsesWith(UndefValue::get(I->getType()));
12710           I->eraseFromParent();
12711         }
12712       }
12713   }
12714
12715   while (!Worklist.isEmpty()) {
12716     Instruction *I = Worklist.RemoveOne();
12717     if (I == 0) continue;  // skip null values.
12718
12719     // Check to see if we can DCE the instruction.
12720     if (isInstructionTriviallyDead(I)) {
12721       DEBUG(errs() << "IC: DCE: " << *I << '\n');
12722       EraseInstFromFunction(*I);
12723       ++NumDeadInst;
12724       MadeIRChange = true;
12725       continue;
12726     }
12727
12728     // Instruction isn't dead, see if we can constant propagate it.
12729     if (Constant *C = ConstantFoldInstruction(I, F.getContext(), TD)) {
12730       DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
12731
12732       // Add operands to the worklist.
12733       ReplaceInstUsesWith(*I, C);
12734       ++NumConstProp;
12735       EraseInstFromFunction(*I);
12736       MadeIRChange = true;
12737       continue;
12738     }
12739
12740     if (TD) {
12741       // See if we can constant fold its operands.
12742       for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
12743         if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i))
12744           if (Constant *NewC = ConstantFoldConstantExpression(CE,   
12745                                   F.getContext(), TD))
12746             if (NewC != CE) {
12747               i->set(NewC);
12748               MadeIRChange = true;
12749             }
12750     }
12751
12752     // See if we can trivially sink this instruction to a successor basic block.
12753     if (I->hasOneUse()) {
12754       BasicBlock *BB = I->getParent();
12755       BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent();
12756       if (UserParent != BB) {
12757         bool UserIsSuccessor = false;
12758         // See if the user is one of our successors.
12759         for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
12760           if (*SI == UserParent) {
12761             UserIsSuccessor = true;
12762             break;
12763           }
12764
12765         // If the user is one of our immediate successors, and if that successor
12766         // only has us as a predecessors (we'd have to split the critical edge
12767         // otherwise), we can keep going.
12768         if (UserIsSuccessor && !isa<PHINode>(I->use_back()) &&
12769             next(pred_begin(UserParent)) == pred_end(UserParent))
12770           // Okay, the CFG is simple enough, try to sink this instruction.
12771           MadeIRChange |= TryToSinkInstruction(I, UserParent);
12772       }
12773     }
12774
12775     // Now that we have an instruction, try combining it to simplify it.
12776     Builder->SetInsertPoint(I->getParent(), I);
12777     
12778 #ifndef NDEBUG
12779     std::string OrigI;
12780 #endif
12781     DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
12782     
12783     if (Instruction *Result = visit(*I)) {
12784       ++NumCombined;
12785       // Should we replace the old instruction with a new one?
12786       if (Result != I) {
12787         DEBUG(errs() << "IC: Old = " << *I << '\n'
12788                      << "    New = " << *Result << '\n');
12789
12790         // Everything uses the new instruction now.
12791         I->replaceAllUsesWith(Result);
12792
12793         // Push the new instruction and any users onto the worklist.
12794         Worklist.Add(Result);
12795         Worklist.AddUsersToWorkList(*Result);
12796
12797         // Move the name to the new instruction first.
12798         Result->takeName(I);
12799
12800         // Insert the new instruction into the basic block...
12801         BasicBlock *InstParent = I->getParent();
12802         BasicBlock::iterator InsertPos = I;
12803
12804         if (!isa<PHINode>(Result))        // If combining a PHI, don't insert
12805           while (isa<PHINode>(InsertPos)) // middle of a block of PHIs.
12806             ++InsertPos;
12807
12808         InstParent->getInstList().insert(InsertPos, Result);
12809
12810         EraseInstFromFunction(*I);
12811       } else {
12812 #ifndef NDEBUG
12813         DEBUG(errs() << "IC: Mod = " << OrigI << '\n'
12814                      << "    New = " << *I << '\n');
12815 #endif
12816
12817         // If the instruction was modified, it's possible that it is now dead.
12818         // if so, remove it.
12819         if (isInstructionTriviallyDead(I)) {
12820           EraseInstFromFunction(*I);
12821         } else {
12822           Worklist.Add(I);
12823           Worklist.AddUsersToWorkList(*I);
12824         }
12825       }
12826       MadeIRChange = true;
12827     }
12828   }
12829
12830   Worklist.Zap();
12831   return MadeIRChange;
12832 }
12833
12834
12835 bool InstCombiner::runOnFunction(Function &F) {
12836   MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
12837   Context = &F.getContext();
12838   
12839   
12840   /// Builder - This is an IRBuilder that automatically inserts new
12841   /// instructions into the worklist when they are created.
12842   IRBuilder<true, ConstantFolder, InstCombineIRInserter> 
12843     TheBuilder(F.getContext(), ConstantFolder(F.getContext()),
12844                InstCombineIRInserter(Worklist));
12845   Builder = &TheBuilder;
12846   
12847   bool EverMadeChange = false;
12848
12849   // Iterate while there is work to do.
12850   unsigned Iteration = 0;
12851   while (DoOneIteration(F, Iteration++))
12852     EverMadeChange = true;
12853   
12854   Builder = 0;
12855   return EverMadeChange;
12856 }
12857
12858 FunctionPass *llvm::createInstructionCombiningPass() {
12859   return new InstCombiner();
12860 }