LLVM API Documentation

InstructionCombining.cpp

Go to the documentation of this file.
00001 //===- InstructionCombining.cpp - Combine multiple instructions -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // InstructionCombining - Combine instructions to form fewer, simple
00011 // instructions.  This pass does not modify the CFG.  This pass is where
00012 // algebraic simplification happens.
00013 //
00014 // This pass combines things like:
00015 //    %Y = add i32 %X, 1
00016 //    %Z = add i32 %Y, 1
00017 // into:
00018 //    %Z = add i32 %X, 2
00019 //
00020 // This is a simple worklist driven algorithm.
00021 //
00022 // This pass guarantees that the following canonicalizations are performed on
00023 // the program:
00024 //    1. If a binary operator has a constant operand, it is moved to the RHS
00025 //    2. Bitwise operators with constant operands are always grouped so that
00026 //       shifts are performed first, then or's, then and's, then xor's.
00027 //    3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
00028 //    4. All cmp instructions on boolean values are replaced with logical ops
00029 //    5. add X, X is represented as (X*2) => (X << 1)
00030 //    6. Multiplies with a power-of-two constant argument are transformed into
00031 //       shifts.
00032 //   ... etc.
00033 //
00034 //===----------------------------------------------------------------------===//
00035 
00036 #define DEBUG_TYPE "instcombine"
00037 #include "llvm/Transforms/Scalar.h"
00038 #include "llvm/IntrinsicInst.h"
00039 #include "llvm/LLVMContext.h"
00040 #include "llvm/Pass.h"
00041 #include "llvm/DerivedTypes.h"
00042 #include "llvm/GlobalVariable.h"
00043 #include "llvm/Operator.h"
00044 #include "llvm/Analysis/ConstantFolding.h"
00045 #include "llvm/Analysis/ValueTracking.h"
00046 #include "llvm/Target/TargetData.h"
00047 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00048 #include "llvm/Transforms/Utils/Local.h"
00049 #include "llvm/Support/CallSite.h"
00050 #include "llvm/Support/ConstantRange.h"
00051 #include "llvm/Support/Debug.h"
00052 #include "llvm/Support/ErrorHandling.h"
00053 #include "llvm/Support/GetElementPtrTypeIterator.h"
00054 #include "llvm/Support/InstVisitor.h"
00055 #include "llvm/Support/MathExtras.h"
00056 #include "llvm/Support/PatternMatch.h"
00057 #include "llvm/Support/Compiler.h"
00058 #include "llvm/Support/raw_ostream.h"
00059 #include "llvm/ADT/DenseMap.h"
00060 #include "llvm/ADT/SmallVector.h"
00061 #include "llvm/ADT/SmallPtrSet.h"
00062 #include "llvm/ADT/Statistic.h"
00063 #include "llvm/ADT/STLExtras.h"
00064 #include <algorithm>
00065 #include <climits>
00066 #include <sstream>
00067 using namespace llvm;
00068 using namespace llvm::PatternMatch;
00069 
00070 STATISTIC(NumCombined , "Number of insts combined");
00071 STATISTIC(NumConstProp, "Number of constant folds");
00072 STATISTIC(NumDeadInst , "Number of dead inst eliminated");
00073 STATISTIC(NumDeadStore, "Number of dead stores eliminated");
00074 STATISTIC(NumSunkInst , "Number of instructions sunk");
00075 
00076 namespace {
00077   class VISIBILITY_HIDDEN InstCombiner
00078     : public FunctionPass,
00079       public InstVisitor<InstCombiner, Instruction*> {
00080     // Worklist of all of the instructions that need to be simplified.
00081     SmallVector<Instruction*, 256> Worklist;
00082     DenseMap<Instruction*, unsigned> WorklistMap;
00083     TargetData *TD;
00084     bool MustPreserveLCSSA;
00085   public:
00086     static char ID; // Pass identification, replacement for typeid
00087     InstCombiner() : FunctionPass(&ID) {}
00088 
00089     LLVMContext *Context;
00090     LLVMContext *getContext() const { return Context; }
00091 
00092     /// AddToWorkList - Add the specified instruction to the worklist if it
00093     /// isn't already in it.
00094     void AddToWorkList(Instruction *I) {
00095       if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second)
00096         Worklist.push_back(I);
00097     }
00098     
00099     // RemoveFromWorkList - remove I from the worklist if it exists.
00100     void RemoveFromWorkList(Instruction *I) {
00101       DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
00102       if (It == WorklistMap.end()) return; // Not in worklist.
00103       
00104       // Don't bother moving everything down, just null out the slot.
00105       Worklist[It->second] = 0;
00106       
00107       WorklistMap.erase(It);
00108     }
00109     
00110     Instruction *RemoveOneFromWorkList() {
00111       Instruction *I = Worklist.back();
00112       Worklist.pop_back();
00113       WorklistMap.erase(I);
00114       return I;
00115     }
00116 
00117     
00118     /// AddUsersToWorkList - When an instruction is simplified, add all users of
00119     /// the instruction to the work lists because they might get more simplified
00120     /// now.
00121     ///
00122     void AddUsersToWorkList(Value &I) {
00123       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
00124            UI != UE; ++UI)
00125         AddToWorkList(cast<Instruction>(*UI));
00126     }
00127 
00128     /// AddUsesToWorkList - When an instruction is simplified, add operands to
00129     /// the work lists because they might get more simplified now.
00130     ///
00131     void AddUsesToWorkList(Instruction &I) {
00132       for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
00133         if (Instruction *Op = dyn_cast<Instruction>(*i))
00134           AddToWorkList(Op);
00135     }
00136     
00137     /// AddSoonDeadInstToWorklist - The specified instruction is about to become
00138     /// dead.  Add all of its operands to the worklist, turning them into
00139     /// undef's to reduce the number of uses of those instructions.
00140     ///
00141     /// Return the specified operand before it is turned into an undef.
00142     ///
00143     Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) {
00144       Value *R = I.getOperand(op);
00145       
00146       for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
00147         if (Instruction *Op = dyn_cast<Instruction>(*i)) {
00148           AddToWorkList(Op);
00149           // Set the operand to undef to drop the use.
00150           *i = UndefValue::get(Op->getType());
00151         }
00152       
00153       return R;
00154     }
00155 
00156   public:
00157     virtual bool runOnFunction(Function &F);
00158     
00159     bool DoOneIteration(Function &F, unsigned ItNum);
00160 
00161     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00162       AU.addPreservedID(LCSSAID);
00163       AU.setPreservesCFG();
00164     }
00165 
00166     TargetData *getTargetData() const { return TD; }
00167 
00168     // Visitation implementation - Implement instruction combining for different
00169     // instruction types.  The semantics are as follows:
00170     // Return Value:
00171     //    null        - No change was made
00172     //     I          - Change was made, I is still valid, I may be dead though
00173     //   otherwise    - Change was made, replace I with returned instruction
00174     //
00175     Instruction *visitAdd(BinaryOperator &I);
00176     Instruction *visitFAdd(BinaryOperator &I);
00177     Instruction *visitSub(BinaryOperator &I);
00178     Instruction *visitFSub(BinaryOperator &I);
00179     Instruction *visitMul(BinaryOperator &I);
00180     Instruction *visitFMul(BinaryOperator &I);
00181     Instruction *visitURem(BinaryOperator &I);
00182     Instruction *visitSRem(BinaryOperator &I);
00183     Instruction *visitFRem(BinaryOperator &I);
00184     bool SimplifyDivRemOfSelect(BinaryOperator &I);
00185     Instruction *commonRemTransforms(BinaryOperator &I);
00186     Instruction *commonIRemTransforms(BinaryOperator &I);
00187     Instruction *commonDivTransforms(BinaryOperator &I);
00188     Instruction *commonIDivTransforms(BinaryOperator &I);
00189     Instruction *visitUDiv(BinaryOperator &I);
00190     Instruction *visitSDiv(BinaryOperator &I);
00191     Instruction *visitFDiv(BinaryOperator &I);
00192     Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
00193     Instruction *FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS);
00194     Instruction *visitAnd(BinaryOperator &I);
00195     Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
00196     Instruction *FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS);
00197     Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op,
00198                                      Value *A, Value *B, Value *C);
00199     Instruction *visitOr (BinaryOperator &I);
00200     Instruction *visitXor(BinaryOperator &I);
00201     Instruction *visitShl(BinaryOperator &I);
00202     Instruction *visitAShr(BinaryOperator &I);
00203     Instruction *visitLShr(BinaryOperator &I);
00204     Instruction *commonShiftTransforms(BinaryOperator &I);
00205     Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
00206                                       Constant *RHSC);
00207     Instruction *visitFCmpInst(FCmpInst &I);
00208     Instruction *visitICmpInst(ICmpInst &I);
00209     Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
00210     Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
00211                                                 Instruction *LHS,
00212                                                 ConstantInt *RHS);
00213     Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
00214                                 ConstantInt *DivRHS);
00215 
00216     Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
00217                              ICmpInst::Predicate Cond, Instruction &I);
00218     Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
00219                                      BinaryOperator &I);
00220     Instruction *commonCastTransforms(CastInst &CI);
00221     Instruction *commonIntCastTransforms(CastInst &CI);
00222     Instruction *commonPointerCastTransforms(CastInst &CI);
00223     Instruction *visitTrunc(TruncInst &CI);
00224     Instruction *visitZExt(ZExtInst &CI);
00225     Instruction *visitSExt(SExtInst &CI);
00226     Instruction *visitFPTrunc(FPTruncInst &CI);
00227     Instruction *visitFPExt(CastInst &CI);
00228     Instruction *visitFPToUI(FPToUIInst &FI);
00229     Instruction *visitFPToSI(FPToSIInst &FI);
00230     Instruction *visitUIToFP(CastInst &CI);
00231     Instruction *visitSIToFP(CastInst &CI);
00232     Instruction *visitPtrToInt(PtrToIntInst &CI);
00233     Instruction *visitIntToPtr(IntToPtrInst &CI);
00234     Instruction *visitBitCast(BitCastInst &CI);
00235     Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
00236                                 Instruction *FI);
00237     Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*);
00238     Instruction *visitSelectInst(SelectInst &SI);
00239     Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
00240     Instruction *visitCallInst(CallInst &CI);
00241     Instruction *visitInvokeInst(InvokeInst &II);
00242     Instruction *visitPHINode(PHINode &PN);
00243     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
00244     Instruction *visitAllocationInst(AllocationInst &AI);
00245     Instruction *visitFreeInst(FreeInst &FI);
00246     Instruction *visitLoadInst(LoadInst &LI);
00247     Instruction *visitStoreInst(StoreInst &SI);
00248     Instruction *visitBranchInst(BranchInst &BI);
00249     Instruction *visitSwitchInst(SwitchInst &SI);
00250     Instruction *visitInsertElementInst(InsertElementInst &IE);
00251     Instruction *visitExtractElementInst(ExtractElementInst &EI);
00252     Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
00253     Instruction *visitExtractValueInst(ExtractValueInst &EV);
00254 
00255     // visitInstruction - Specify what to return for unhandled instructions...
00256     Instruction *visitInstruction(Instruction &I) { return 0; }
00257 
00258   private:
00259     Instruction *visitCallSite(CallSite CS);
00260     bool transformConstExprCastCall(CallSite CS);
00261     Instruction *transformCallThroughTrampoline(CallSite CS);
00262     Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
00263                                    bool DoXform = true);
00264     bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
00265     DbgDeclareInst *hasOneUsePlusDeclare(Value *V);
00266 
00267 
00268   public:
00269     // InsertNewInstBefore - insert an instruction New before instruction Old
00270     // in the program.  Add the new instruction to the worklist.
00271     //
00272     Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
00273       assert(New && New->getParent() == 0 &&
00274              "New instruction already inserted into a basic block!");
00275       BasicBlock *BB = Old.getParent();
00276       BB->getInstList().insert(&Old, New);  // Insert inst
00277       AddToWorkList(New);
00278       return New;
00279     }
00280 
00281     /// InsertCastBefore - Insert a cast of V to TY before the instruction POS.
00282     /// This also adds the cast to the worklist.  Finally, this returns the
00283     /// cast.
00284     Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty,
00285                             Instruction &Pos) {
00286       if (V->getType() == Ty) return V;
00287 
00288       if (Constant *CV = dyn_cast<Constant>(V))
00289         return ConstantExpr::getCast(opc, CV, Ty);
00290       
00291       Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos);
00292       AddToWorkList(C);
00293       return C;
00294     }
00295         
00296     Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) {
00297       return InsertCastBefore(Instruction::BitCast, V, Ty, Pos);
00298     }
00299 
00300 
00301     // ReplaceInstUsesWith - This method is to be used when an instruction is
00302     // found to be dead, replacable with another preexisting expression.  Here
00303     // we add all uses of I to the worklist, replace all uses of I with the new
00304     // value, then return I, so that the inst combiner will know that I was
00305     // modified.
00306     //
00307     Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
00308       AddUsersToWorkList(I);         // Add all modified instrs to worklist
00309       if (&I != V) {
00310         I.replaceAllUsesWith(V);
00311         return &I;
00312       } else {
00313         // If we are replacing the instruction with itself, this must be in a
00314         // segment of unreachable code, so just clobber the instruction.
00315         I.replaceAllUsesWith(UndefValue::get(I.getType()));
00316         return &I;
00317       }
00318     }
00319 
00320     // EraseInstFromFunction - When dealing with an instruction that has side
00321     // effects or produces a void value, we can't rely on DCE to delete the
00322     // instruction.  Instead, visit methods should return the value returned by
00323     // this function.
00324     Instruction *EraseInstFromFunction(Instruction &I) {
00325       assert(I.use_empty() && "Cannot erase instruction that is used!");
00326       AddUsesToWorkList(I);
00327       RemoveFromWorkList(&I);
00328       I.eraseFromParent();
00329       return 0;  // Don't do anything with FI
00330     }
00331         
00332     void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero,
00333                            APInt &KnownOne, unsigned Depth = 0) const {
00334       return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
00335     }
00336     
00337     bool MaskedValueIsZero(Value *V, const APInt &Mask, 
00338                            unsigned Depth = 0) const {
00339       return llvm::MaskedValueIsZero(V, Mask, TD, Depth);
00340     }
00341     unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const {
00342       return llvm::ComputeNumSignBits(Op, TD, Depth);
00343     }
00344 
00345   private:
00346 
00347     /// SimplifyCommutative - This performs a few simplifications for 
00348     /// commutative operators.
00349     bool SimplifyCommutative(BinaryOperator &I);
00350 
00351     /// SimplifyCompare - This reorders the operands of a CmpInst to get them in
00352     /// most-complex to least-complex order.
00353     bool SimplifyCompare(CmpInst &I);
00354 
00355     /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
00356     /// based on the demanded bits.
00357     Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, 
00358                                    APInt& KnownZero, APInt& KnownOne,
00359                                    unsigned Depth);
00360     bool SimplifyDemandedBits(Use &U, APInt DemandedMask, 
00361                               APInt& KnownZero, APInt& KnownOne,
00362                               unsigned Depth=0);
00363         
00364     /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
00365     /// SimplifyDemandedBits knows about.  See if the instruction has any
00366     /// properties that allow us to simplify its operands.
00367     bool SimplifyDemandedInstructionBits(Instruction &Inst);
00368         
00369     Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
00370                                       APInt& UndefElts, unsigned Depth = 0);
00371       
00372     // FoldOpIntoPhi - Given a binary operator or cast instruction which has a
00373     // PHI node as operand #0, see if we can fold the instruction into the PHI
00374     // (which is only possible if all operands to the PHI are constants).
00375     Instruction *FoldOpIntoPhi(Instruction &I);
00376 
00377     // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
00378     // operator and they all are only used by the PHI, PHI together their
00379     // inputs, and do the operation once, to the result of the PHI.
00380     Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
00381     Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
00382     Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
00383 
00384     
00385     Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
00386                           ConstantInt *AndRHS, BinaryOperator &TheAnd);
00387     
00388     Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
00389                               bool isSub, Instruction &I);
00390     Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
00391                                  bool isSigned, bool Inside, Instruction &IB);
00392     Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI);
00393     Instruction *MatchBSwap(BinaryOperator &I);
00394     bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
00395     Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
00396     Instruction *SimplifyMemSet(MemSetInst *MI);
00397 
00398 
00399     Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned);
00400 
00401     bool CanEvaluateInDifferentType(Value *V, const Type *Ty,
00402                                     unsigned CastOpc, int &NumCastsRemoved);
00403     unsigned GetOrEnforceKnownAlignment(Value *V,
00404                                         unsigned PrefAlign = 0);
00405 
00406   };
00407 }
00408 
00409 char InstCombiner::ID = 0;
00410 static RegisterPass<InstCombiner>
00411 X("instcombine", "Combine redundant instructions");
00412 
00413 // getComplexity:  Assign a complexity or rank value to LLVM Values...
00414 //   0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
00415 static unsigned getComplexity(LLVMContext *Context, Value *V) {
00416   if (isa<Instruction>(V)) {
00417     if (BinaryOperator::isNeg(V) ||
00418         BinaryOperator::isFNeg(V) ||
00419         BinaryOperator::isNot(V))
00420       return 3;
00421     return 4;
00422   }
00423   if (isa<Argument>(V)) return 3;
00424   return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
00425 }
00426 
00427 // isOnlyUse - Return true if this instruction will be deleted if we stop using
00428 // it.
00429 static bool isOnlyUse(Value *V) {
00430   return V->hasOneUse() || isa<Constant>(V);
00431 }
00432 
00433 // getPromotedType - Return the specified type promoted as it would be to pass
00434 // though a va_arg area...
00435 static const Type *getPromotedType(const Type *Ty) {
00436   if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
00437     if (ITy->getBitWidth() < 32)
00438       return Type::getInt32Ty(Ty->getContext());
00439   }
00440   return Ty;
00441 }
00442 
00443 /// getBitCastOperand - If the specified operand is a CastInst, a constant
00444 /// expression bitcast, or a GetElementPtrInst with all zero indices, return the
00445 /// operand value, otherwise return null.
00446 static Value *getBitCastOperand(Value *V) {
00447   if (Operator *O = dyn_cast<Operator>(V)) {
00448     if (O->getOpcode() == Instruction::BitCast)
00449       return O->getOperand(0);
00450     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
00451       if (GEP->hasAllZeroIndices())
00452         return GEP->getPointerOperand();
00453   }
00454   return 0;
00455 }
00456 
00457 /// This function is a wrapper around CastInst::isEliminableCastPair. It
00458 /// simply extracts arguments and returns what that function returns.
00459 static Instruction::CastOps 
00460 isEliminableCastPair(
00461   const CastInst *CI, ///< The first cast instruction
00462   unsigned opcode,       ///< The opcode of the second cast instruction
00463   const Type *DstTy,     ///< The target type for the second cast instruction
00464   TargetData *TD         ///< The target data for pointer size
00465 ) {
00466 
00467   const Type *SrcTy = CI->getOperand(0)->getType();   // A from above
00468   const Type *MidTy = CI->getType();                  // B from above
00469 
00470   // Get the opcodes of the two Cast instructions
00471   Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
00472   Instruction::CastOps secondOp = Instruction::CastOps(opcode);
00473 
00474   unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
00475                                                 DstTy,
00476                                   TD ? TD->getIntPtrType(CI->getContext()) : 0);
00477   
00478   // We don't want to form an inttoptr or ptrtoint that converts to an integer
00479   // type that differs from the pointer size.
00480   if ((Res == Instruction::IntToPtr &&
00481           (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) ||
00482       (Res == Instruction::PtrToInt &&
00483           (!TD || DstTy != TD->getIntPtrType(CI->getContext()))))
00484     Res = 0;
00485   
00486   return Instruction::CastOps(Res);
00487 }
00488 
00489 /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
00490 /// in any code being generated.  It does not require codegen if V is simple
00491 /// enough or if the cast can be folded into other casts.
00492 static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, 
00493                               const Type *Ty, TargetData *TD) {
00494   if (V->getType() == Ty || isa<Constant>(V)) return false;
00495   
00496   // If this is another cast that can be eliminated, it isn't codegen either.
00497   if (const CastInst *CI = dyn_cast<CastInst>(V))
00498     if (isEliminableCastPair(CI, opcode, Ty, TD))
00499       return false;
00500   return true;
00501 }
00502 
00503 // SimplifyCommutative - This performs a few simplifications for commutative
00504 // operators:
00505 //
00506 //  1. Order operands such that they are listed from right (least complex) to
00507 //     left (most complex).  This puts constants before unary operators before
00508 //     binary operators.
00509 //
00510 //  2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
00511 //  3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
00512 //
00513 bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
00514   bool Changed = false;
00515   if (getComplexity(Context, I.getOperand(0)) < 
00516       getComplexity(Context, I.getOperand(1)))
00517     Changed = !I.swapOperands();
00518 
00519   if (!I.isAssociative()) return Changed;
00520   Instruction::BinaryOps Opcode = I.getOpcode();
00521   if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
00522     if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
00523       if (isa<Constant>(I.getOperand(1))) {
00524         Constant *Folded = ConstantExpr::get(I.getOpcode(),
00525                                              cast<Constant>(I.getOperand(1)),
00526                                              cast<Constant>(Op->getOperand(1)));
00527         I.setOperand(0, Op->getOperand(0));
00528         I.setOperand(1, Folded);
00529         return true;
00530       } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
00531         if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
00532             isOnlyUse(Op) && isOnlyUse(Op1)) {
00533           Constant *C1 = cast<Constant>(Op->getOperand(1));
00534           Constant *C2 = cast<Constant>(Op1->getOperand(1));
00535 
00536           // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
00537           Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
00538           Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0),
00539                                                     Op1->getOperand(0),
00540                                                     Op1->getName(), &I);
00541           AddToWorkList(New);
00542           I.setOperand(0, New);
00543           I.setOperand(1, Folded);
00544           return true;
00545         }
00546     }
00547   return Changed;
00548 }
00549 
00550 /// SimplifyCompare - For a CmpInst this function just orders the operands
00551 /// so that theyare listed from right (least complex) to left (most complex).
00552 /// This puts constants before unary operators before binary operators.
00553 bool InstCombiner::SimplifyCompare(CmpInst &I) {
00554   if (getComplexity(Context, I.getOperand(0)) >=
00555       getComplexity(Context, I.getOperand(1)))
00556     return false;
00557   I.swapOperands();
00558   // Compare instructions are not associative so there's nothing else we can do.
00559   return true;
00560 }
00561 
00562 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
00563 // if the LHS is a constant zero (which is the 'negate' form).
00564 //
00565 static inline Value *dyn_castNegVal(Value *V) {
00566   if (BinaryOperator::isNeg(V))
00567     return BinaryOperator::getNegArgument(V);
00568 
00569   // Constants can be considered to be negated values if they can be folded.
00570   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
00571     return ConstantExpr::getNeg(C);
00572 
00573   if (ConstantVector *C = dyn_cast<ConstantVector>(V))
00574     if (C->getType()->getElementType()->isInteger())
00575       return ConstantExpr::getNeg(C);
00576 
00577   return 0;
00578 }
00579 
00580 // dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the
00581 // instruction if the LHS is a constant negative zero (which is the 'negate'
00582 // form).
00583 //
00584 static inline Value *dyn_castFNegVal(Value *V) {
00585   if (BinaryOperator::isFNeg(V))
00586     return BinaryOperator::getFNegArgument(V);
00587 
00588   // Constants can be considered to be negated values if they can be folded.
00589   if (ConstantFP *C = dyn_cast<ConstantFP>(V))
00590     return ConstantExpr::getFNeg(C);
00591 
00592   if (ConstantVector *C = dyn_cast<ConstantVector>(V))
00593     if (C->getType()->getElementType()->isFloatingPoint())
00594       return ConstantExpr::getFNeg(C);
00595 
00596   return 0;
00597 }
00598 
00599 static inline Value *dyn_castNotVal(Value *V) {
00600   if (BinaryOperator::isNot(V))
00601     return BinaryOperator::getNotArgument(V);
00602 
00603   // Constants can be considered to be not'ed values...
00604   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
00605     return ConstantInt::get(C->getType(), ~C->getValue());
00606   return 0;
00607 }
00608 
00609 // dyn_castFoldableMul - If this value is a multiply that can be folded into
00610 // other computations (because it has a constant operand), return the
00611 // non-constant operand of the multiply, and set CST to point to the multiplier.
00612 // Otherwise, return null.
00613 //
00614 static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
00615   if (V->hasOneUse() && V->getType()->isInteger())
00616     if (Instruction *I = dyn_cast<Instruction>(V)) {
00617       if (I->getOpcode() == Instruction::Mul)
00618         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
00619           return I->getOperand(0);
00620       if (I->getOpcode() == Instruction::Shl)
00621         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
00622           // The multiplier is really 1 << CST.
00623           uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
00624           uint32_t CSTVal = CST->getLimitedValue(BitWidth);
00625           CST = ConstantInt::get(V->getType()->getContext(),
00626                                  APInt(BitWidth, 1).shl(CSTVal));
00627           return I->getOperand(0);
00628         }
00629     }
00630   return 0;
00631 }
00632 
00633 /// AddOne - Add one to a ConstantInt
00634 static Constant *AddOne(Constant *C) {
00635   return ConstantExpr::getAdd(C, 
00636     ConstantInt::get(C->getType(), 1));
00637 }
00638 /// SubOne - Subtract one from a ConstantInt
00639 static Constant *SubOne(ConstantInt *C) {
00640   return ConstantExpr::getSub(C, 
00641     ConstantInt::get(C->getType(), 1));
00642 }
00643 /// MultiplyOverflows - True if the multiply can not be expressed in an int
00644 /// this size.
00645 static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
00646   uint32_t W = C1->getBitWidth();
00647   APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
00648   if (sign) {
00649     LHSExt.sext(W * 2);
00650     RHSExt.sext(W * 2);
00651   } else {
00652     LHSExt.zext(W * 2);
00653     RHSExt.zext(W * 2);
00654   }
00655 
00656   APInt MulExt = LHSExt * RHSExt;
00657 
00658   if (sign) {
00659     APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
00660     APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
00661     return MulExt.slt(Min) || MulExt.sgt(Max);
00662   } else 
00663     return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
00664 }
00665 
00666 
00667 /// ShrinkDemandedConstant - Check to see if the specified operand of the 
00668 /// specified instruction is a constant integer.  If so, check to see if there
00669 /// are any bits set in the constant that are not demanded.  If so, shrink the
00670 /// constant and return true.
00671 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, 
00672                                    APInt Demanded) {
00673   assert(I && "No instruction?");
00674   assert(OpNo < I->getNumOperands() && "Operand index too large");
00675 
00676   // If the operand is not a constant integer, nothing to do.
00677   ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
00678   if (!OpC) return false;
00679 
00680   // If there are no bits set that aren't demanded, nothing to do.
00681   Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
00682   if ((~Demanded & OpC->getValue()) == 0)
00683     return false;
00684 
00685   // This instruction is producing bits that are not demanded. Shrink the RHS.
00686   Demanded &= OpC->getValue();
00687   I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
00688   return true;
00689 }
00690 
00691 // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a 
00692 // set of known zero and one bits, compute the maximum and minimum values that
00693 // could have the specified known zero and known one bits, returning them in
00694 // min/max.
00695 static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero,
00696                                                    const APInt& KnownOne,
00697                                                    APInt& Min, APInt& Max) {
00698   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
00699          KnownZero.getBitWidth() == Min.getBitWidth() &&
00700          KnownZero.getBitWidth() == Max.getBitWidth() &&
00701          "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
00702   APInt UnknownBits = ~(KnownZero|KnownOne);
00703 
00704   // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
00705   // bit if it is unknown.
00706   Min = KnownOne;
00707   Max = KnownOne|UnknownBits;
00708   
00709   if (UnknownBits.isNegative()) { // Sign bit is unknown
00710     Min.set(Min.getBitWidth()-1);
00711     Max.clear(Max.getBitWidth()-1);
00712   }
00713 }
00714 
00715 // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
00716 // a set of known zero and one bits, compute the maximum and minimum values that
00717 // could have the specified known zero and known one bits, returning them in
00718 // min/max.
00719 static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
00720                                                      const APInt &KnownOne,
00721                                                      APInt &Min, APInt &Max) {
00722   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
00723          KnownZero.getBitWidth() == Min.getBitWidth() &&
00724          KnownZero.getBitWidth() == Max.getBitWidth() &&
00725          "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
00726   APInt UnknownBits = ~(KnownZero|KnownOne);
00727   
00728   // The minimum value is when the unknown bits are all zeros.
00729   Min = KnownOne;
00730   // The maximum value is when the unknown bits are all ones.
00731   Max = KnownOne|UnknownBits;
00732 }
00733 
00734 /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
00735 /// SimplifyDemandedBits knows about.  See if the instruction has any
00736 /// properties that allow us to simplify its operands.
00737 bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
00738   unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
00739   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
00740   APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
00741   
00742   Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, 
00743                                      KnownZero, KnownOne, 0);
00744   if (V == 0) return false;
00745   if (V == &Inst) return true;
00746   ReplaceInstUsesWith(Inst, V);
00747   return true;
00748 }
00749 
00750 /// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
00751 /// specified instruction operand if possible, updating it in place.  It returns
00752 /// true if it made any change and false otherwise.
00753 bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask, 
00754                                         APInt &KnownZero, APInt &KnownOne,
00755                                         unsigned Depth) {
00756   Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
00757                                           KnownZero, KnownOne, Depth);
00758   if (NewVal == 0) return false;
00759   U.set(NewVal);
00760   return true;
00761 }
00762 
00763 
00764 /// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
00765 /// value based on the demanded bits.  When this function is called, it is known
00766 /// that only the bits set in DemandedMask of the result of V are ever used
00767 /// downstream. Consequently, depending on the mask and V, it may be possible
00768 /// to replace V with a constant or one of its operands. In such cases, this
00769 /// function does the replacement and returns true. In all other cases, it
00770 /// returns false after analyzing the expression and setting KnownOne and known
00771 /// to be one in the expression.  KnownZero contains all the bits that are known
00772 /// to be zero in the expression. These are provided to potentially allow the
00773 /// caller (which might recursively be SimplifyDemandedBits itself) to simplify
00774 /// the expression. KnownOne and KnownZero always follow the invariant that 
00775 /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
00776 /// the bits in KnownOne and KnownZero may only be accurate for those bits set
00777 /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
00778 /// and KnownOne must all be the same.
00779 ///
00780 /// This returns null if it did not change anything and it permits no
00781 /// simplification.  This returns V itself if it did some simplification of V's
00782 /// operands based on the information about what bits are demanded. This returns
00783 /// some other non-null value if it found out that V is equal to another value
00784 /// in the context where the specified bits are demanded, but not for all users.
00785 Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
00786                                              APInt &KnownZero, APInt &KnownOne,
00787                                              unsigned Depth) {
00788   assert(V != 0 && "Null pointer of Value???");
00789   assert(Depth <= 6 && "Limit Search Depth");
00790   uint32_t BitWidth = DemandedMask.getBitWidth();
00791   const Type *VTy = V->getType();
00792   assert((TD || !isa<PointerType>(VTy)) &&
00793          "SimplifyDemandedBits needs to know bit widths!");
00794   assert((!TD || TD->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
00795          (!VTy->isIntOrIntVector() ||
00796           VTy->getScalarSizeInBits() == BitWidth) &&
00797          KnownZero.getBitWidth() == BitWidth &&
00798          KnownOne.getBitWidth() == BitWidth &&
00799          "Value *V, DemandedMask, KnownZero and KnownOne "
00800          "must have same BitWidth");
00801   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00802     // We know all of the bits for a constant!
00803     KnownOne = CI->getValue() & DemandedMask;
00804     KnownZero = ~KnownOne & DemandedMask;
00805     return 0;
00806   }
00807   if (isa<ConstantPointerNull>(V)) {
00808     // We know all of the bits for a constant!
00809     KnownOne.clear();
00810     KnownZero = DemandedMask;
00811     return 0;
00812   }
00813 
00814   KnownZero.clear();
00815   KnownOne.clear();
00816   if (DemandedMask == 0) {   // Not demanding any bits from V.
00817     if (isa<UndefValue>(V))
00818       return 0;
00819     return UndefValue::get(VTy);
00820   }
00821   
00822   if (Depth == 6)        // Limit search depth.
00823     return 0;
00824   
00825   APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
00826   APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne;
00827 
00828   Instruction *I = dyn_cast<Instruction>(V);
00829   if (!I) {
00830     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
00831     return 0;        // Only analyze instructions.
00832   }
00833 
00834   // If there are multiple uses of this value and we aren't at the root, then
00835   // we can't do any simplifications of the operands, because DemandedMask
00836   // only reflects the bits demanded by *one* of the users.
00837   if (Depth != 0 && !I->hasOneUse()) {
00838     // Despite the fact that we can't simplify this instruction in all User's
00839     // context, we can at least compute the knownzero/knownone bits, and we can
00840     // do simplifications that apply to *just* the one user if we know that
00841     // this instruction has a simpler value in that context.
00842     if (I->getOpcode() == Instruction::And) {
00843       // If either the LHS or the RHS are Zero, the result is zero.
00844       ComputeMaskedBits(I->getOperand(1), DemandedMask,
00845                         RHSKnownZero, RHSKnownOne, Depth+1);
00846       ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero,
00847                         LHSKnownZero, LHSKnownOne, Depth+1);
00848       
00849       // If all of the demanded bits are known 1 on one side, return the other.
00850       // These bits cannot contribute to the result of the 'and' in this
00851       // context.
00852       if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == 
00853           (DemandedMask & ~LHSKnownZero))
00854         return I->getOperand(0);
00855       if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == 
00856           (DemandedMask & ~RHSKnownZero))
00857         return I->getOperand(1);
00858       
00859       // If all of the demanded bits in the inputs are known zeros, return zero.
00860       if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
00861         return Constant::getNullValue(VTy);
00862       
00863     } else if (I->getOpcode() == Instruction::Or) {
00864       // We can simplify (X|Y) -> X or Y in the user's context if we know that
00865       // only bits from X or Y are demanded.
00866       
00867       // If either the LHS or the RHS are One, the result is One.
00868       ComputeMaskedBits(I->getOperand(1), DemandedMask, 
00869                         RHSKnownZero, RHSKnownOne, Depth+1);
00870       ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, 
00871                         LHSKnownZero, LHSKnownOne, Depth+1);
00872       
00873       // If all of the demanded bits are known zero on one side, return the
00874       // other.  These bits cannot contribute to the result of the 'or' in this
00875       // context.
00876       if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == 
00877           (DemandedMask & ~LHSKnownOne))
00878         return I->getOperand(0);
00879       if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == 
00880           (DemandedMask & ~RHSKnownOne))
00881         return I->getOperand(1);
00882       
00883       // If all of the potentially set bits on one side are known to be set on
00884       // the other side, just use the 'other' side.
00885       if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == 
00886           (DemandedMask & (~RHSKnownZero)))
00887         return I->getOperand(0);
00888       if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == 
00889           (DemandedMask & (~LHSKnownZero)))
00890         return I->getOperand(1);
00891     }
00892     
00893     // Compute the KnownZero/KnownOne bits to simplify things downstream.
00894     ComputeMaskedBits(I, DemandedMask, KnownZero, KnownOne, Depth);
00895     return 0;
00896   }
00897   
00898   // If this is the root being simplified, allow it to have multiple uses,
00899   // just set the DemandedMask to all bits so that we can try to simplify the
00900   // operands.  This allows visitTruncInst (for example) to simplify the
00901   // operand of a trunc without duplicating all the logic below.
00902   if (Depth == 0 && !V->hasOneUse())
00903     DemandedMask = APInt::getAllOnesValue(BitWidth);
00904   
00905   switch (I->getOpcode()) {
00906   default:
00907     ComputeMaskedBits(I, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
00908     break;
00909   case Instruction::And:
00910     // If either the LHS or the RHS are Zero, the result is zero.
00911     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
00912                              RHSKnownZero, RHSKnownOne, Depth+1) ||
00913         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
00914                              LHSKnownZero, LHSKnownOne, Depth+1))
00915       return I;
00916     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
00917     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
00918 
00919     // If all of the demanded bits are known 1 on one side, return the other.
00920     // These bits cannot contribute to the result of the 'and'.
00921     if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == 
00922         (DemandedMask & ~LHSKnownZero))
00923       return I->getOperand(0);
00924     if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == 
00925         (DemandedMask & ~RHSKnownZero))
00926       return I->getOperand(1);
00927     
00928     // If all of the demanded bits in the inputs are known zeros, return zero.
00929     if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
00930       return Constant::getNullValue(VTy);
00931       
00932     // If the RHS is a constant, see if we can simplify it.
00933     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
00934       return I;
00935       
00936     // Output known-1 bits are only known if set in both the LHS & RHS.
00937     RHSKnownOne &= LHSKnownOne;
00938     // Output known-0 are known to be clear if zero in either the LHS | RHS.
00939     RHSKnownZero |= LHSKnownZero;
00940     break;
00941   case Instruction::Or:
00942     // If either the LHS or the RHS are One, the result is One.
00943     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, 
00944                              RHSKnownZero, RHSKnownOne, Depth+1) ||
00945         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne, 
00946                              LHSKnownZero, LHSKnownOne, Depth+1))
00947       return I;
00948     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
00949     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
00950     
00951     // If all of the demanded bits are known zero on one side, return the other.
00952     // These bits cannot contribute to the result of the 'or'.
00953     if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == 
00954         (DemandedMask & ~LHSKnownOne))
00955       return I->getOperand(0);
00956     if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == 
00957         (DemandedMask & ~RHSKnownOne))
00958       return I->getOperand(1);
00959 
00960     // If all of the potentially set bits on one side are known to be set on
00961     // the other side, just use the 'other' side.
00962     if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == 
00963         (DemandedMask & (~RHSKnownZero)))
00964       return I->getOperand(0);
00965     if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == 
00966         (DemandedMask & (~LHSKnownZero)))
00967       return I->getOperand(1);
00968         
00969     // If the RHS is a constant, see if we can simplify it.
00970     if (ShrinkDemandedConstant(I, 1, DemandedMask))
00971       return I;
00972           
00973     // Output known-0 bits are only known if clear in both the LHS & RHS.
00974     RHSKnownZero &= LHSKnownZero;
00975     // Output known-1 are known to be set if set in either the LHS | RHS.
00976     RHSKnownOne |= LHSKnownOne;
00977     break;
00978   case Instruction::Xor: {
00979     if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
00980                              RHSKnownZero, RHSKnownOne, Depth+1) ||
00981         SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, 
00982                              LHSKnownZero, LHSKnownOne, Depth+1))
00983       return I;
00984     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
00985     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
00986     
00987     // If all of the demanded bits are known zero on one side, return the other.
00988     // These bits cannot contribute to the result of the 'xor'.
00989     if ((DemandedMask & RHSKnownZero) == DemandedMask)
00990       return I->getOperand(0);
00991     if ((DemandedMask & LHSKnownZero) == DemandedMask)
00992       return I->getOperand(1);
00993     
00994     // Output known-0 bits are known if clear or set in both the LHS & RHS.
00995     APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | 
00996                          (RHSKnownOne & LHSKnownOne);
00997     // Output known-1 are known to be set if set in only one of the LHS, RHS.
00998     APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | 
00999                         (RHSKnownOne & LHSKnownZero);
01000     
01001     // If all of the demanded bits are known to be zero on one side or the
01002     // other, turn this into an *inclusive* or.
01003     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
01004     if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
01005       Instruction *Or =
01006         BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
01007                                  I->getName());
01008       return InsertNewInstBefore(Or, *I);
01009     }
01010     
01011     // If all of the demanded bits on one side are known, and all of the set
01012     // bits on that side are also known to be set on the other side, turn this
01013     // into an AND, as we know the bits will be cleared.
01014     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
01015     if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { 
01016       // all known
01017       if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
01018         Constant *AndC = Constant::getIntegerValue(VTy,
01019                                                    ~RHSKnownOne & DemandedMask);
01020         Instruction *And = 
01021           BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
01022         return InsertNewInstBefore(And, *I);
01023       }
01024     }
01025     
01026     // If the RHS is a constant, see if we can simplify it.
01027     // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
01028     if (ShrinkDemandedConstant(I, 1, DemandedMask))
01029       return I;
01030     
01031     RHSKnownZero = KnownZeroOut;
01032     RHSKnownOne  = KnownOneOut;
01033     break;
01034   }
01035   case Instruction::Select:
01036     if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
01037                              RHSKnownZero, RHSKnownOne, Depth+1) ||
01038         SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, 
01039                              LHSKnownZero, LHSKnownOne, Depth+1))
01040       return I;
01041     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
01042     assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); 
01043     
01044     // If the operands are constants, see if we can simplify them.
01045     if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
01046         ShrinkDemandedConstant(I, 2, DemandedMask))
01047       return I;
01048     
01049     // Only known if known in both the LHS and RHS.
01050     RHSKnownOne &= LHSKnownOne;
01051     RHSKnownZero &= LHSKnownZero;
01052     break;
01053   case Instruction::Trunc: {
01054     unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
01055     DemandedMask.zext(truncBf);
01056     RHSKnownZero.zext(truncBf);
01057     RHSKnownOne.zext(truncBf);
01058     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, 
01059                              RHSKnownZero, RHSKnownOne, Depth+1))
01060       return I;
01061     DemandedMask.trunc(BitWidth);
01062     RHSKnownZero.trunc(BitWidth);
01063     RHSKnownOne.trunc(BitWidth);
01064     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
01065     break;
01066   }
01067   case Instruction::BitCast:
01068     if (!I->getOperand(0)->getType()->isIntOrIntVector())
01069       return false;  // vector->int or fp->int?
01070 
01071     if (const VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
01072       if (const VectorType *SrcVTy =
01073             dyn_cast<VectorType>(I->getOperand(0)->getType())) {
01074         if (DstVTy->getNumElements() != SrcVTy->getNumElements())
01075           // Don't touch a bitcast between vectors of different element counts.
01076           return false;
01077       } else
01078         // Don't touch a scalar-to-vector bitcast.
01079         return false;
01080     } else if (isa<VectorType>(I->getOperand(0)->getType()))
01081       // Don't touch a vector-to-scalar bitcast.
01082       return false;
01083 
01084     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
01085                              RHSKnownZero, RHSKnownOne, Depth+1))
01086       return I;
01087     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
01088     break;
01089   case Instruction::ZExt: {
01090     // Compute the bits in the result that are not present in the input.
01091     unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
01092     
01093     DemandedMask.trunc(SrcBitWidth);
01094     RHSKnownZero.trunc(SrcBitWidth);
01095     RHSKnownOne.trunc(SrcBitWidth);
01096     if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
01097                              RHSKnownZero, RHSKnownOne, Depth+1))
01098       return I;
01099     DemandedMask.zext(BitWidth);
01100     RHSKnownZero.zext(BitWidth);
01101     RHSKnownOne.zext(BitWidth);
01102     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
01103     // The top bits are known to be zero.
01104     RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
01105     break;
01106   }
01107   case Instruction::SExt: {
01108     // Compute the bits in the result that are not present in the input.
01109     unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
01110     
01111     APInt InputDemandedBits = DemandedMask & 
01112                               APInt::getLowBitsSet(BitWidth, SrcBitWidth);
01113 
01114     APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
01115     // If any of the sign extended bits are demanded, we know that the sign
01116     // bit is demanded.
01117     if ((NewBits & DemandedMask) != 0)
01118       InputDemandedBits.set(SrcBitWidth-1);
01119       
01120     InputDemandedBits.trunc(SrcBitWidth);
01121     RHSKnownZero.trunc(SrcBitWidth);
01122     RHSKnownOne.trunc(SrcBitWidth);
01123     if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
01124                              RHSKnownZero, RHSKnownOne, Depth+1))
01125       return I;
01126     InputDemandedBits.zext(BitWidth);
01127     RHSKnownZero.zext(BitWidth);
01128     RHSKnownOne.zext(BitWidth);
01129     assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); 
01130       
01131     // If the sign bit of the input is known set or clear, then we know the
01132     // top bits of the result.
01133 
01134     // If the input sign bit is known zero, or if the NewBits are not demanded
01135     // convert this into a zero extension.
01136     if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
01137       // Convert to ZExt cast
01138       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
01139       return InsertNewInstBefore(NewCast, *I);
01140     } else if (RHSKnownOne[SrcBitWidth-1]) {    // Input sign bit known set
01141       RHSKnownOne |= NewBits;
01142     }
01143     break;
01144   }
01145   case Instruction::Add: {
01146     // Figure out what the input bits are.  If the top bits of the and result
01147     // are not demanded, then the add doesn't demand them from its input
01148     // either.
01149     unsigned NLZ = DemandedMask.countLeadingZeros();
01150       
01151     // If there is a constant on the RHS, there are a variety of xformations
01152     // we can do.
01153     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
01154       // If null, this should be simplified elsewhere.  Some of the xforms here
01155       // won't work if the RHS is zero.
01156       if (RHS->isZero())
01157         break;
01158       
01159       // If the top bit of the output is demanded, demand everything from the
01160       // input.  Otherwise, we demand all the input bits except NLZ top bits.
01161       APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
01162 
01163       // Find information about known zero/one bits in the input.
01164       if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits, 
01165                                LHSKnownZero, LHSKnownOne, Depth+1))
01166         return I;
01167 
01168       // If the RHS of the add has bits set that can't affect the input, reduce
01169       // the constant.
01170       if (ShrinkDemandedConstant(I, 1, InDemandedBits))
01171         return I;
01172       
01173       // Avoid excess work.
01174       if (LHSKnownZero == 0 && LHSKnownOne == 0)
01175         break;
01176       
01177       // Turn it into OR if input bits are zero.
01178       if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
01179         Instruction *Or =
01180           BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
01181                                    I->getName());
01182         return InsertNewInstBefore(Or, *I);
01183       }
01184       
01185       // We can say something about the output known-zero and known-one bits,
01186       // depending on potential carries from the input constant and the
01187       // unknowns.  For example if the LHS is known to have at most the 0x0F0F0
01188       // bits set and the RHS constant is 0x01001, then we know we have a known
01189       // one mask of 0x00001 and a known zero mask of 0xE0F0E.
01190       
01191       // To compute this, we first compute the potential carry bits.  These are
01192       // the bits which may be modified.  I'm not aware of a better way to do
01193       // this scan.
01194       const APInt &RHSVal = RHS->getValue();
01195       APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
01196       
01197       // Now that we know which bits have carries, compute the known-1/0 sets.
01198       
01199       // Bits are known one if they are known zero in one operand and one in the
01200       // other, and there is no input carry.
01201       RHSKnownOne = ((LHSKnownZero & RHSVal) | 
01202                      (LHSKnownOne & ~RHSVal)) & ~CarryBits;
01203       
01204       // Bits are known zero if they are known zero in both operands and there
01205       // is no input carry.
01206       RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
01207     } else {
01208       // If the high-bits of this ADD are not demanded, then it does not demand
01209       // the high bits of its LHS or RHS.
01210       if (DemandedMask[BitWidth-1] == 0) {
01211         // Right fill the mask of bits for this ADD to demand the most
01212         // significant bit and all those below it.
01213         APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
01214         if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
01215                                  LHSKnownZero, LHSKnownOne, Depth+1) ||
01216             SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
01217                                  LHSKnownZero, LHSKnownOne, Depth+1))
01218           return I;
01219       }
01220     }
01221     break;
01222   }
01223   case Instruction::Sub:
01224     // If the high-bits of this SUB are not demanded, then it does not demand
01225     // the high bits of its LHS or RHS.
01226     if (DemandedMask[BitWidth-1] == 0) {
01227       // Right fill the mask of bits for this SUB to demand the most
01228       // significant bit and all those below it.
01229       uint32_t NLZ = DemandedMask.countLeadingZeros();
01230       APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
01231       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
01232                                LHSKnownZero, LHSKnownOne, Depth+1) ||
01233           SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
01234                                LHSKnownZero, LHSKnownOne, Depth+1))
01235         return I;
01236     }
01237     // Otherwise just hand the sub off to ComputeMaskedBits to fill in
01238     // the known zeros and ones.
01239     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
01240     break;
01241   case Instruction::Shl:
01242     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
01243       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
01244       APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
01245       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, 
01246                                RHSKnownZero, RHSKnownOne, Depth+1))
01247         return I;
01248       assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
01249       RHSKnownZero <<= ShiftAmt;
01250       RHSKnownOne  <<= ShiftAmt;
01251       // low bits known zero.
01252       if (ShiftAmt)
01253         RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
01254     }
01255     break;
01256   case Instruction::LShr:
01257     // For a logical shift right
01258     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
01259       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
01260       
01261       // Unsigned shift right.
01262       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
01263       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
01264                                RHSKnownZero, RHSKnownOne, Depth+1))
01265         return I;
01266       assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
01267       RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
01268       RHSKnownOne  = APIntOps::lshr(RHSKnownOne, ShiftAmt);
01269       if (ShiftAmt) {
01270         // Compute the new bits that are at the top now.
01271         APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
01272         RHSKnownZero |= HighBits;  // high bits known zero.
01273       }
01274     }
01275     break;
01276   case Instruction::AShr:
01277     // If this is an arithmetic shift right and only the low-bit is set, we can
01278     // always convert this into a logical shr, even if the shift amount is
01279     // variable.  The low bit of the shift cannot be an input sign bit unless
01280     // the shift amount is >= the size of the datatype, which is undefined.
01281     if (DemandedMask == 1) {
01282       // Perform the logical shift right.
01283       Instruction *NewVal = BinaryOperator::CreateLShr(
01284                         I->getOperand(0), I->getOperand(1), I->getName());
01285       return InsertNewInstBefore(NewVal, *I);
01286     }    
01287 
01288     // If the sign bit is the only bit demanded by this ashr, then there is no
01289     // need to do it, the shift doesn't change the high bit.
01290     if (DemandedMask.isSignBit())
01291       return I->getOperand(0);
01292     
01293     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
01294       uint32_t ShiftAmt = SA->getLimitedValue(BitWidth);
01295       
01296       // Signed shift right.
01297       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
01298       // If any of the "high bits" are demanded, we should set the sign bit as
01299       // demanded.
01300       if (DemandedMask.countLeadingZeros() <= ShiftAmt)
01301         DemandedMaskIn.set(BitWidth-1);
01302       if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
01303                                RHSKnownZero, RHSKnownOne, Depth+1))
01304         return I;
01305       assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
01306       // Compute the new bits that are at the top now.
01307       APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
01308       RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
01309       RHSKnownOne  = APIntOps::lshr(RHSKnownOne, ShiftAmt);
01310         
01311       // Handle the sign bits.
01312       APInt SignBit(APInt::getSignBit(BitWidth));
01313       // Adjust to where it is now in the mask.
01314       SignBit = APIntOps::lshr(SignBit, ShiftAmt);  
01315         
01316       // If the input sign bit is known to be zero, or if none of the top bits
01317       // are demanded, turn this into an unsigned shift right.
01318       if (BitWidth <= ShiftAmt || RHSKnownZero[BitWidth-ShiftAmt-1] || 
01319           (HighBits & ~DemandedMask) == HighBits) {
01320         // Perform the logical shift right.
01321         Instruction *NewVal = BinaryOperator::CreateLShr(
01322                           I->getOperand(0), SA, I->getName());
01323         return InsertNewInstBefore(NewVal, *I);
01324       } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one.
01325         RHSKnownOne |= HighBits;
01326       }
01327     }
01328     break;
01329   case Instruction::SRem:
01330     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
01331       APInt RA = Rem->getValue().abs();
01332       if (RA.isPowerOf2()) {
01333         if (DemandedMask.ult(RA))    // srem won't affect demanded bits
01334           return I->getOperand(0);
01335 
01336         APInt LowBits = RA - 1;
01337         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
01338         if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
01339                                  LHSKnownZero, LHSKnownOne, Depth+1))
01340           return I;
01341 
01342         if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
01343           LHSKnownZero |= ~LowBits;
01344 
01345         KnownZero |= LHSKnownZero & DemandedMask;
01346 
01347         assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?"); 
01348       }
01349     }
01350     break;
01351   case Instruction::URem: {
01352     APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
01353     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
01354     if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
01355                              KnownZero2, KnownOne2, Depth+1) ||
01356         SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
01357                              KnownZero2, KnownOne2, Depth+1))
01358       return I;
01359 
01360     unsigned Leaders = KnownZero2.countLeadingOnes();
01361     Leaders = std::max(Leaders,
01362                        KnownZero2.countLeadingOnes());
01363     KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
01364     break;
01365   }
01366   case Instruction::Call:
01367     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
01368       switch (II->getIntrinsicID()) {
01369       default: break;
01370       case Intrinsic::bswap: {
01371         // If the only bits demanded come from one byte of the bswap result,
01372         // just shift the input byte into position to eliminate the bswap.
01373         unsigned NLZ = DemandedMask.countLeadingZeros();
01374         unsigned NTZ = DemandedMask.countTrailingZeros();
01375           
01376         // Round NTZ down to the next byte.  If we have 11 trailing zeros, then
01377         // we need all the bits down to bit 8.  Likewise, round NLZ.  If we
01378         // have 14 leading zeros, round to 8.
01379         NLZ &= ~7;
01380         NTZ &= ~7;
01381         // If we need exactly one byte, we can do this transformation.
01382         if (BitWidth-NLZ-NTZ == 8) {
01383           unsigned ResultBit = NTZ;
01384           unsigned InputBit = BitWidth-NTZ-8;
01385           
01386           // Replace this with either a left or right shift to get the byte into
01387           // the right place.
01388           Instruction *NewVal;
01389           if (InputBit > ResultBit)
01390             NewVal = BinaryOperator::CreateLShr(I->getOperand(1),
01391                     ConstantInt::get(I->getType(), InputBit-ResultBit));
01392           else
01393             NewVal = BinaryOperator::CreateShl(I->getOperand(1),
01394                     ConstantInt::get(I->getType(), ResultBit-InputBit));
01395           NewVal->takeName(I);
01396           return InsertNewInstBefore(NewVal, *I);
01397         }
01398           
01399         // TODO: Could compute known zero/one bits based on the input.
01400         break;
01401       }
01402       }
01403     }
01404     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
01405     break;
01406   }
01407   
01408   // If the client is only demanding bits that we know, return the known
01409   // constant.
01410   if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask)
01411     return Constant::getIntegerValue(VTy, RHSKnownOne);
01412   return false;
01413 }
01414 
01415 
01416 /// SimplifyDemandedVectorElts - The specified value produces a vector with
01417 /// any number of elements. DemandedElts contains the set of elements that are
01418 /// actually used by the caller.  This method analyzes which elements of the
01419 /// operand are undef and returns that information in UndefElts.
01420 ///
01421 /// If the information about demanded elements can be used to simplify the
01422 /// operation, the operation is simplified, then the resultant value is
01423 /// returned.  This returns null if no change was made.
01424 Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
01425                                                 APInt& UndefElts,
01426                                                 unsigned Depth) {
01427   unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
01428   APInt EltMask(APInt::getAllOnesValue(VWidth));
01429   assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
01430 
01431   if (isa<UndefValue>(V)) {
01432     // If the entire vector is undefined, just return this info.
01433     UndefElts = EltMask;
01434     return 0;
01435   } else if (DemandedElts == 0) { // If nothing is demanded, provide undef.
01436     UndefElts = EltMask;
01437     return UndefValue::get(V->getType());
01438   }
01439 
01440   UndefElts = 0;
01441   if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) {
01442     const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
01443     Constant *Undef = UndefValue::get(EltTy);
01444 
01445     std::vector<Constant*> Elts;
01446     for (unsigned i = 0; i != VWidth; ++i)
01447       if (!DemandedElts[i]) {   // If not demanded, set to undef.
01448         Elts.push_back(Undef);
01449         UndefElts.set(i);
01450       } else if (isa<UndefValue>(CP->getOperand(i))) {   // Already undef.
01451         Elts.push_back(Undef);
01452         UndefElts.set(i);
01453       } else {                               // Otherwise, defined.
01454         Elts.push_back(CP->getOperand(i));
01455       }
01456 
01457     // If we changed the constant, return it.
01458     Constant *NewCP = ConstantVector::get(Elts);
01459     return NewCP != CP ? NewCP : 0;
01460   } else if (isa<ConstantAggregateZero>(V)) {
01461     // Simplify the CAZ to a ConstantVector where the non-demanded elements are
01462     // set to undef.
01463     
01464     // Check if this is identity. If so, return 0 since we are not simplifying
01465     // anything.
01466     if (DemandedElts == ((1ULL << VWidth) -1))
01467       return 0;
01468     
01469     const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
01470     Constant *Zero = Constant::getNullValue(EltTy);
01471     Constant *Undef = UndefValue::get(EltTy);
01472     std::vector<Constant*> Elts;
01473     for (unsigned i = 0; i != VWidth; ++i) {
01474       Constant *Elt = DemandedElts[i] ? Zero : Undef;
01475       Elts.push_back(Elt);
01476     }
01477     UndefElts = DemandedElts ^ EltMask;
01478     return ConstantVector::get(Elts);
01479   }
01480   
01481   // Limit search depth.
01482   if (Depth == 10)
01483     return 0;
01484 
01485   // If multiple users are using the root value, procede with
01486   // simplification conservatively assuming that all elements
01487   // are needed.
01488   if (!V->hasOneUse()) {
01489     // Quit if we find multiple users of a non-root value though.
01490     // They'll be handled when it's their turn to be visited by
01491     // the main instcombine process.
01492     if (Depth != 0)
01493       // TODO: Just compute the UndefElts information recursively.
01494       return 0;
01495 
01496     // Conservatively assume that all elements are needed.
01497     DemandedElts = EltMask;
01498   }
01499   
01500   Instruction *I = dyn_cast<Instruction>(V);
01501   if (!I) return 0;        // Only analyze instructions.
01502   
01503   bool MadeChange = false;
01504   APInt UndefElts2(VWidth, 0);
01505   Value *TmpV;
01506   switch (I->getOpcode()) {
01507   default: break;
01508     
01509   case Instruction::InsertElement: {
01510     // If this is a variable index, we don't know which element it overwrites.
01511     // demand exactly the same input as we produce.
01512     ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
01513     if (Idx == 0) {
01514       // Note that we can't propagate undef elt info, because we don't know
01515       // which elt is getting updated.
01516       TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
01517                                         UndefElts2, Depth+1);
01518       if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
01519       break;
01520     }
01521     
01522     // If this is inserting an element that isn't demanded, remove this
01523     // insertelement.
01524     unsigned IdxNo = Idx->getZExtValue();
01525     if (IdxNo >= VWidth || !DemandedElts[IdxNo])
01526       return AddSoonDeadInstToWorklist(*I, 0);
01527     
01528     // Otherwise, the element inserted overwrites whatever was there, so the
01529     // input demanded set is simpler than the output set.
01530     APInt DemandedElts2 = DemandedElts;
01531     DemandedElts2.clear(IdxNo);
01532     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
01533                                       UndefElts, Depth+1);
01534     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
01535 
01536     // The inserted element is defined.
01537     UndefElts.clear(IdxNo);
01538     break;
01539   }
01540   case Instruction::ShuffleVector: {
01541     ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
01542     uint64_t LHSVWidth =
01543       cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
01544     APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
01545     for (unsigned i = 0; i < VWidth; i++) {
01546       if (DemandedElts[i]) {
01547         unsigned MaskVal = Shuffle->getMaskValue(i);
01548         if (MaskVal != -1u) {
01549           assert(MaskVal < LHSVWidth * 2 &&
01550                  "shufflevector mask index out of range!");
01551           if (MaskVal < LHSVWidth)
01552             LeftDemanded.set(MaskVal);
01553           else
01554             RightDemanded.set(MaskVal - LHSVWidth);
01555         }
01556       }
01557     }
01558 
01559     APInt UndefElts4(LHSVWidth, 0);
01560     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
01561                                       UndefElts4, Depth+1);
01562     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
01563 
01564     APInt UndefElts3(LHSVWidth, 0);
01565     TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
01566                                       UndefElts3, Depth+1);
01567     if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
01568 
01569     bool NewUndefElts = false;
01570     for (unsigned i = 0; i < VWidth; i++) {
01571       unsigned MaskVal = Shuffle->getMaskValue(i);
01572       if (MaskVal == -1u) {
01573         UndefElts.set(i);
01574       } else if (MaskVal < LHSVWidth) {
01575         if (UndefElts4[MaskVal]) {
01576           NewUndefElts = true;
01577           UndefElts.set(i);
01578         }
01579       } else {
01580         if (UndefElts3[MaskVal - LHSVWidth]) {
01581           NewUndefElts = true;
01582           UndefElts.set(i);
01583         }
01584       }
01585     }
01586 
01587     if (NewUndefElts) {
01588       // Add additional discovered undefs.
01589       std::vector<Constant*> Elts;
01590       for (unsigned i = 0; i < VWidth; ++i) {
01591         if (UndefElts[i])
01592           Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
01593         else
01594           Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context),
01595                                           Shuffle->getMaskValue(i)));
01596       }
01597       I->setOperand(2, ConstantVector::get(Elts));
01598       MadeChange = true;
01599     }
01600     break;
01601   }
01602   case Instruction::BitCast: {
01603     // Vector->vector casts only.
01604     const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
01605     if (!VTy) break;
01606     unsigned InVWidth = VTy->getNumElements();
01607     APInt InputDemandedElts(InVWidth, 0);
01608     unsigned Ratio;
01609 
01610     if (VWidth == InVWidth) {
01611       // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
01612       // elements as are demanded of us.
01613       Ratio = 1;
01614       InputDemandedElts = DemandedElts;
01615     } else if (VWidth > InVWidth) {
01616       // Untested so far.
01617       break;
01618       
01619       // If there are more elements in the result than there are in the source,
01620       // then an input element is live if any of the corresponding output
01621       // elements are live.
01622       Ratio = VWidth/InVWidth;
01623       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
01624         if (DemandedElts[OutIdx])
01625           InputDemandedElts.set(OutIdx/Ratio);
01626       }
01627     } else {
01628       // Untested so far.
01629       break;
01630       
01631       // If there are more elements in the source than there are in the result,
01632       // then an input element is live if the corresponding output element is
01633       // live.
01634       Ratio = InVWidth/VWidth;
01635       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
01636         if (DemandedElts[InIdx/Ratio])
01637           InputDemandedElts.set(InIdx);
01638     }
01639     
01640     // div/rem demand all inputs, because they don't want divide by zero.
01641     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
01642                                       UndefElts2, Depth+1);
01643     if (TmpV) {
01644       I->setOperand(0, TmpV);
01645       MadeChange = true;
01646     }
01647     
01648     UndefElts = UndefElts2;
01649     if (VWidth > InVWidth) {
01650       llvm_unreachable("Unimp");
01651       // If there are more elements in the result than there are in the source,
01652       // then an output element is undef if the corresponding input element is
01653       // undef.
01654       for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
01655         if (UndefElts2[OutIdx/Ratio])
01656           UndefElts.set(OutIdx);
01657     } else if (VWidth < InVWidth) {
01658       llvm_unreachable("Unimp");
01659       // If there are more elements in the source than there are in the result,
01660       // then a result element is undef if all of the corresponding input
01661       // elements are undef.
01662       UndefElts = ~0ULL >> (64-VWidth);  // Start out all undef.
01663       for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
01664         if (!UndefElts2[InIdx])            // Not undef?
01665           UndefElts.clear(InIdx/Ratio);    // Clear undef bit.
01666     }
01667     break;
01668   }
01669   case Instruction::And:
01670   case Instruction::Or:
01671   case Instruction::Xor:
01672   case Instruction::Add:
01673   case Instruction::Sub:
01674   case Instruction::Mul:
01675     // div/rem demand all inputs, because they don't want divide by zero.
01676     TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
01677                                       UndefElts, Depth+1);
01678     if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
01679     TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
01680                                       UndefElts2, Depth+1);
01681     if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
01682       
01683     // Output elements are undefined if both are undefined.  Consider things
01684     // like undef&0.  The result is known zero, not undef.
01685     UndefElts &= UndefElts2;
01686     break;
01687     
01688   case Instruction::Call: {
01689     IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
01690     if (!II) break;
01691     switch (II->getIntrinsicID()) {
01692     default: break;
01693       
01694     // Binary vector operations that work column-wise.  A dest element is a
01695     // function of the corresponding input elements from the two inputs.
01696     case Intrinsic::x86_sse_sub_ss:
01697     case Intrinsic::x86_sse_mul_ss:
01698     case Intrinsic::x86_sse_min_ss:
01699     case Intrinsic::x86_sse_max_ss:
01700     case Intrinsic::x86_sse2_sub_sd:
01701     case Intrinsic::x86_sse2_mul_sd:
01702     case Intrinsic::x86_sse2_min_sd:
01703     case Intrinsic::x86_sse2_max_sd:
01704       TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
01705                                         UndefElts, Depth+1);
01706       if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; }
01707       TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts,
01708                                         UndefElts2, Depth+1);
01709       if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; }
01710 
01711       // If only the low elt is demanded and this is a scalarizable intrinsic,
01712       // scalarize it now.
01713       if (DemandedElts == 1) {
01714         switch (II->getIntrinsicID()) {
01715         default: break;
01716         case Intrinsic::x86_sse_sub_ss:
01717         case Intrinsic::x86_sse_mul_ss:
01718         case Intrinsic::x86_sse2_sub_sd:
01719         case Intrinsic::x86_sse2_mul_sd:
01720           // TODO: Lower MIN/MAX/ABS/etc
01721           Value *LHS = II->getOperand(1);
01722           Value *RHS = II->getOperand(2);
01723           // Extract the element as scalars.
01724           LHS = InsertNewInstBefore(ExtractElementInst::Create(LHS, 
01725             ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), "tmp"), *II);
01726           RHS = InsertNewInstBefore(ExtractElementInst::Create(RHS,
01727             ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), "tmp"), *II);
01728           
01729           switch (II->getIntrinsicID()) {
01730           default: llvm_unreachable("Case stmts out of sync!");
01731           case Intrinsic::x86_sse_sub_ss:
01732           case Intrinsic::x86_sse2_sub_sd:
01733             TmpV = InsertNewInstBefore(BinaryOperator::CreateFSub(LHS, RHS,
01734                                                         II->getName()), *II);
01735             break;
01736           case Intrinsic::x86_sse_mul_ss:
01737           case Intrinsic::x86_sse2_mul_sd:
01738             TmpV = InsertNewInstBefore(BinaryOperator::CreateFMul(LHS, RHS,
01739                                                          II->getName()), *II);
01740             break;
01741           }
01742           
01743           Instruction *New =
01744             InsertElementInst::Create(
01745               UndefValue::get(II->getType()), TmpV,
01746               ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), II->getName());
01747           InsertNewInstBefore(New, *II);
01748           AddSoonDeadInstToWorklist(*II, 0);
01749           return New;
01750         }            
01751       }
01752         
01753       // Output elements are undefined if both are undefined.  Consider things
01754       // like undef&0.  The result is known zero, not undef.
01755       UndefElts &= UndefElts2;
01756       break;
01757     }
01758     break;
01759   }
01760   }
01761   return MadeChange ? I : 0;
01762 }
01763 
01764 
01765 /// AssociativeOpt - Perform an optimization on an associative operator.  This
01766 /// function is designed to check a chain of associative operators for a
01767 /// potential to apply a certain optimization.  Since the optimization may be
01768 /// applicable if the expression was reassociated, this checks the chain, then
01769 /// reassociates the expression as necessary to expose the optimization
01770 /// opportunity.  This makes use of a special Functor, which must define
01771 /// 'shouldApply' and 'apply' methods.
01772 ///
01773 template<typename Functor>
01774 static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
01775   unsigned Opcode = Root.getOpcode();
01776   Value *LHS = Root.getOperand(0);
01777 
01778   // Quick check, see if the immediate LHS matches...
01779   if (F.shouldApply(LHS))
01780     return F.apply(Root);
01781 
01782   // Otherwise, if the LHS is not of the same opcode as the root, return.
01783   Instruction *LHSI = dyn_cast<Instruction>(LHS);
01784   while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) {
01785     // Should we apply this transform to the RHS?
01786     bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
01787 
01788     // If not to the RHS, check to see if we should apply to the LHS...
01789     if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
01790       cast<BinaryOperator>(LHSI)->swapOperands();   // Make the LHS the RHS
01791       ShouldApply = true;
01792     }
01793 
01794     // If the functor wants to apply the optimization to the RHS of LHSI,
01795     // reassociate the expression from ((? op A) op B) to (? op (A op B))
01796     if (ShouldApply) {
01797       // Now all of the instructions are in the current basic block, go ahead
01798       // and perform the reassociation.
01799       Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
01800 
01801       // First move the selected RHS to the LHS of the root...
01802       Root.setOperand(0, LHSI->getOperand(1));
01803 
01804       // Make what used to be the LHS of the root be the user of the root...
01805       Value *ExtraOperand = TmpLHSI->getOperand(1);
01806       if (&Root == TmpLHSI) {
01807         Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
01808         return 0;
01809       }
01810       Root.replaceAllUsesWith(TmpLHSI);          // Users now use TmpLHSI
01811       TmpLHSI->setOperand(1, &Root);             // TmpLHSI now uses the root
01812       BasicBlock::iterator ARI = &Root; ++ARI;
01813       TmpLHSI->moveBefore(ARI);                  // Move TmpLHSI to after Root
01814       ARI = Root;
01815 
01816       // Now propagate the ExtraOperand down the chain of instructions until we
01817       // get to LHSI.
01818       while (TmpLHSI != LHSI) {
01819         Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
01820         // Move the instruction to immediately before the chain we are
01821         // constructing to avoid breaking dominance properties.
01822         NextLHSI->moveBefore(ARI);
01823         ARI = NextLHSI;
01824 
01825         Value *NextOp = NextLHSI->getOperand(1);
01826         NextLHSI->setOperand(1, ExtraOperand);
01827         TmpLHSI = NextLHSI;
01828         ExtraOperand = NextOp;
01829       }
01830 
01831       // Now that the instructions are reassociated, have the functor perform
01832       // the transformation...
01833       return F.apply(Root);
01834     }
01835 
01836     LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
01837   }
01838   return 0;
01839 }
01840 
01841 namespace {
01842 
01843 // AddRHS - Implements: X + X --> X << 1
01844 struct AddRHS {
01845   Value *RHS;
01846   explicit AddRHS(Value *rhs) : RHS(rhs) {}
01847   bool shouldApply(Value *LHS) const { return LHS == RHS; }
01848   Instruction *apply(BinaryOperator &Add) const {
01849     return BinaryOperator::CreateShl(Add.getOperand(0),
01850                                      ConstantInt::get(Add.getType(), 1));
01851   }
01852 };
01853 
01854 // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
01855 //                 iff C1&C2 == 0
01856 struct AddMaskingAnd {
01857   Constant *C2;
01858   explicit AddMaskingAnd(Constant *c) : C2(c) {}
01859   bool shouldApply(Value *LHS) const {
01860     ConstantInt *C1;
01861     return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
01862            ConstantExpr::getAnd(C1, C2)->isNullValue();
01863   }
01864   Instruction *apply(BinaryOperator &Add) const {
01865     return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1));
01866   }
01867 };
01868 
01869 }
01870 
01871 static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
01872                                              InstCombiner *IC) {
01873   LLVMContext *Context = IC->getContext();
01874   
01875   if (CastInst *CI = dyn_cast<CastInst>(&I)) {
01876     return IC->InsertCastBefore(CI->getOpcode(), SO, I.getType(), I);
01877   }
01878 
01879   // Figure out if the constant is the left or the right argument.
01880   bool ConstIsRHS = isa<Constant>(I.getOperand(1));
01881   Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
01882 
01883   if (Constant *SOC = dyn_cast<Constant>(SO)) {
01884     if (ConstIsRHS)
01885       return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
01886     return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
01887   }
01888 
01889   Value *Op0 = SO, *Op1 = ConstOperand;
01890   if (!ConstIsRHS)
01891     std::swap(Op0, Op1);
01892   Instruction *New;
01893   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
01894     New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
01895   else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
01896     New = CmpInst::Create(*Context, CI->getOpcode(), CI->getPredicate(),
01897                           Op0, Op1, SO->getName()+".cmp");
01898   else {
01899     llvm_unreachable("Unknown binary instruction type!");
01900   }
01901   return IC->InsertNewInstBefore(New, I);
01902 }
01903 
01904 // FoldOpIntoSelect - Given an instruction with a select as one operand and a
01905 // constant as the other operand, try to fold the binary operator into the
01906 // select arguments.  This also works for Cast instructions, which obviously do
01907 // not have a second operand.
01908 static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
01909                                      InstCombiner *IC) {
01910   // Don't modify shared select instructions
01911   if (!SI->hasOneUse()) return 0;
01912   Value *TV = SI->getOperand(1);
01913   Value *FV = SI->getOperand(2);
01914 
01915   if (isa<Constant>(TV) || isa<Constant>(FV)) {
01916     // Bool selects with constant operands can be folded to logical ops.
01917     if (SI->getType() == Type::getInt1Ty(*IC->getContext())) return 0;
01918 
01919     Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC);
01920     Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC);
01921 
01922     return SelectInst::Create(SI->getCondition(), SelectTrueVal,
01923                               SelectFalseVal);
01924   }
01925   return 0;
01926 }
01927 
01928 
01929 /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI
01930 /// node as operand #0, see if we can fold the instruction into the PHI (which
01931 /// is only possible if all operands to the PHI are constants).
01932 Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
01933   PHINode *PN = cast<PHINode>(I.getOperand(0));
01934   unsigned NumPHIValues = PN->getNumIncomingValues();
01935   if (!PN->hasOneUse() || NumPHIValues == 0) return 0;
01936 
01937   // Check to see if all of the operands of the PHI are constants.  If there is
01938   // one non-constant value, remember the BB it is.  If there is more than one
01939   // or if *it* is a PHI, bail out.
01940   BasicBlock *NonConstBB = 0;
01941   for (unsigned i = 0; i != NumPHIValues; ++i)
01942     if (!isa<Constant>(PN->getIncomingValue(i))) {
01943       if (NonConstBB) return 0;  // More than one non-const value.
01944       if (isa<PHINode>(PN->getIncomingValue(i))) return 0;  // Itself a phi.
01945       NonConstBB = PN->getIncomingBlock(i);
01946       
01947       // If the incoming non-constant value is in I's block, we have an infinite
01948       // loop.
01949       if (NonConstBB == I.getParent())
01950         return 0;
01951     }
01952   
01953   // If there is exactly one non-constant value, we can insert a copy of the
01954   // operation in that block.  However, if this is a critical edge, we would be
01955   // inserting the computation one some other paths (e.g. inside a loop).  Only
01956   // do this if the pred block is unconditionally branching into the phi block.
01957   if (NonConstBB) {
01958     BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
01959     if (!BI || !BI->isUnconditional()) return 0;
01960   }
01961 
01962   // Okay, we can do the transformation: create the new PHI node.
01963   PHINode *NewPN = PHINode::Create(I.getType(), "");
01964   NewPN->reserveOperandSpace(PN->getNumOperands()/2);
01965   InsertNewInstBefore(NewPN, *PN);
01966   NewPN->takeName(PN);
01967 
01968   // Next, add all of the operands to the PHI.
01969   if (I.getNumOperands() == 2) {
01970     Constant *C = cast<Constant>(I.getOperand(1));
01971     for (unsigned i = 0; i != NumPHIValues; ++i) {
01972       Value *InV = 0;
01973       if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
01974         if (CmpInst *CI = dyn_cast<CmpInst>(&I))
01975           InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
01976         else
01977           InV = ConstantExpr::get(I.getOpcode(), InC, C);
01978       } else {
01979         assert(PN->getIncomingBlock(i) == NonConstBB);
01980         if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) 
01981           InV = BinaryOperator::Create(BO->getOpcode(),
01982                                        PN->getIncomingValue(i), C, "phitmp",
01983                                        NonConstBB->getTerminator());
01984         else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
01985           InV = CmpInst::Create(*Context, CI->getOpcode(), 
01986                                 CI->getPredicate(),
01987                                 PN->getIncomingValue(i), C, "phitmp",
01988                                 NonConstBB->getTerminator());
01989         else
01990           llvm_unreachable("Unknown binop!");
01991         
01992         AddToWorkList(cast<Instruction>(InV));
01993       }
01994       NewPN->addIncoming(InV, PN->getIncomingBlock(i));
01995     }
01996   } else { 
01997     CastInst *CI = cast<CastInst>(&I);
01998     const Type *RetTy = CI->getType();
01999     for (unsigned i = 0; i != NumPHIValues; ++i) {
02000       Value *InV;
02001       if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
02002         InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
02003       } else {
02004         assert(PN->getIncomingBlock(i) == NonConstBB);
02005         InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), 
02006                                I.getType(), "phitmp", 
02007                                NonConstBB->getTerminator());
02008         AddToWorkList(cast<Instruction>(InV));
02009       }
02010       NewPN->addIncoming(InV, PN->getIncomingBlock(i));
02011     }
02012   }
02013   return ReplaceInstUsesWith(I, NewPN);
02014 }
02015 
02016 
02017 /// WillNotOverflowSignedAdd - Return true if we can prove that:
02018 ///    (sext (add LHS, RHS))  === (add (sext LHS), (sext RHS))
02019 /// This basically requires proving that the add in the original type would not
02020 /// overflow to change the sign bit or have a carry out.
02021 bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) {
02022   // There are different heuristics we can use for this.  Here are some simple
02023   // ones.
02024   
02025   // Add has the property that adding any two 2's complement numbers can only 
02026   // have one carry bit which can change a sign.  As such, if LHS and RHS each
02027   // have at least two sign bits, we know that the addition of the two values will
02028   // sign extend fine.
02029   if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1)
02030     return true;
02031   
02032   
02033   // If one of the operands only has one non-zero bit, and if the other operand
02034   // has a known-zero bit in a more significant place than it (not including the
02035   // sign bit) the ripple may go up to and fill the zero, but won't change the
02036   // sign.  For example, (X & ~4) + 1.
02037   
02038   // TODO: Implement.
02039   
02040   return false;
02041 }
02042 
02043 
02044 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
02045   bool Changed = SimplifyCommutative(I);
02046   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
02047 
02048   if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
02049     // X + undef -> undef
02050     if (isa<UndefValue>(RHS))
02051       return ReplaceInstUsesWith(I, RHS);
02052 
02053     // X + 0 --> X
02054     if (RHSC->isNullValue())
02055       return ReplaceInstUsesWith(I, LHS);
02056 
02057     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
02058       // X + (signbit) --> X ^ signbit
02059       const APInt& Val = CI->getValue();
02060       uint32_t BitWidth = Val.getBitWidth();
02061       if (Val == APInt::getSignBit(BitWidth))
02062         return BinaryOperator::CreateXor(LHS, RHS);
02063       
02064       // See if SimplifyDemandedBits can simplify this.  This handles stuff like
02065       // (X & 254)+1 -> (X&254)|1
02066       if (SimplifyDemandedInstructionBits(I))
02067         return &I;
02068 
02069       // zext(bool) + C -> bool ? C + 1 : C
02070       if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
02071         if (ZI->getSrcTy() == Type::getInt1Ty(*Context))
02072           return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
02073     }
02074 
02075     if (isa<PHINode>(LHS))
02076       if (Instruction *NV = FoldOpIntoPhi(I))
02077         return NV;
02078     
02079     ConstantInt *XorRHS = 0;
02080     Value *XorLHS = 0;
02081     if (isa<ConstantInt>(RHSC) &&
02082         match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
02083       uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
02084       const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue();
02085       
02086       uint32_t Size = TySizeBits / 2;
02087       APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1));
02088       APInt CFF80Val(-C0080Val);
02089       do {
02090         if (TySizeBits > Size) {
02091           // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
02092           // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
02093           if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) ||
02094               (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) {
02095             // This is a sign extend if the top bits are known zero.
02096             if (!MaskedValueIsZero(XorLHS, 
02097                    APInt::getHighBitsSet(TySizeBits, TySizeBits - Size)))
02098               Size = 0;  // Not a sign ext, but can't be any others either.
02099             break;
02100           }
02101         }
02102         Size >>= 1;
02103         C0080Val = APIntOps::lshr(C0080Val, Size);
02104         CFF80Val = APIntOps::ashr(CFF80Val, Size);
02105       } while (Size >= 1);
02106       
02107       // FIXME: This shouldn't be necessary. When the backends can handle types
02108       // with funny bit widths then this switch statement should be removed. It
02109       // is just here to get the size of the "middle" type back up to something
02110       // that the back ends can handle.
02111       const Type *MiddleType = 0;
02112       switch (Size) {
02113         default: break;
02114         case 32: MiddleType = Type::getInt32Ty(*Context); break;
02115         case 16: MiddleType = Type::getInt16Ty(*Context); break;
02116         case  8: MiddleType = Type::getInt8Ty(*Context); break;
02117       }
02118       if (MiddleType) {
02119         Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext");
02120         InsertNewInstBefore(NewTrunc, I);
02121         return new SExtInst(NewTrunc, I.getType(), I.getName());
02122       }
02123     }
02124   }
02125 
02126   if (I.getType() == Type::getInt1Ty(*Context))
02127     return BinaryOperator::CreateXor(LHS, RHS);
02128 
02129   // X + X --> X << 1
02130   if (I.getType()->isInteger()) {
02131     if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS)))
02132       return Result;
02133 
02134     if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
02135       if (RHSI->getOpcode() == Instruction::Sub)
02136         if (LHS == RHSI->getOperand(1))                   // A + (B - A) --> B
02137           return ReplaceInstUsesWith(I, RHSI->getOperand(0));
02138     }
02139     if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
02140       if (LHSI->getOpcode() == Instruction::Sub)
02141         if (RHS == LHSI->getOperand(1))                   // (B - A) + A --> B
02142           return ReplaceInstUsesWith(I, LHSI->getOperand(0));
02143     }
02144   }
02145 
02146   // -A + B  -->  B - A
02147   // -A + -B  -->  -(A + B)
02148   if (Value *LHSV = dyn_castNegVal(LHS)) {
02149     if (LHS->getType()->isIntOrIntVector()) {
02150       if (Value *RHSV = dyn_castNegVal(RHS)) {
02151         Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum");
02152         InsertNewInstBefore(NewAdd, I);
02153         return BinaryOperator::CreateNeg(NewAdd);
02154       }
02155     }
02156     
02157     return BinaryOperator::CreateSub(RHS, LHSV);
02158   }
02159 
02160   // A + -B  -->  A - B
02161   if (!isa<Constant>(RHS))
02162     if (Value *V = dyn_castNegVal(RHS))
02163       return BinaryOperator::CreateSub(LHS, V);
02164 
02165 
02166   ConstantInt *C2;
02167   if (Value *X = dyn_castFoldableMul(LHS, C2)) {
02168     if (X == RHS)   // X*C + X --> X * (C+1)
02169       return BinaryOperator::CreateMul(RHS, AddOne(C2));
02170 
02171     // X*C1 + X*C2 --> X * (C1+C2)
02172     ConstantInt *C1;
02173     if (X == dyn_castFoldableMul(RHS, C1))
02174       return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2));
02175   }
02176 
02177   // X + X*C --> X * (C+1)
02178   if (dyn_castFoldableMul(RHS, C2) == LHS)
02179     return BinaryOperator::CreateMul(LHS, AddOne(C2));
02180 
02181   // X + ~X --> -1   since   ~X = -X-1
02182   if (dyn_castNotVal(LHS) == RHS ||
02183       dyn_castNotVal(RHS) == LHS)
02184     return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
02185   
02186 
02187   // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
02188   if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
02189     if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
02190       return R;
02191   
02192   // A+B --> A|B iff A and B have no bits set in common.
02193   if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
02194     APInt Mask = APInt::getAllOnesValue(IT->getBitWidth());
02195     APInt LHSKnownOne(IT->getBitWidth(), 0);
02196     APInt LHSKnownZero(IT->getBitWidth(), 0);
02197     ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
02198     if (LHSKnownZero != 0) {
02199       APInt RHSKnownOne(IT->getBitWidth(), 0);
02200       APInt RHSKnownZero(IT->getBitWidth(), 0);
02201       ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
02202       
02203       // No bits in common -> bitwise or.
02204       if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
02205         return BinaryOperator::CreateOr(LHS, RHS);
02206     }
02207   }
02208 
02209   // W*X + Y*Z --> W * (X+Z)  iff W == Y
02210   if (I.getType()->isIntOrIntVector()) {
02211     Value *W, *X, *Y, *Z;
02212     if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
02213         match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
02214       if (W != Y) {
02215         if (W == Z) {
02216           std::swap(Y, Z);
02217         } else if (Y == X) {
02218           std::swap(W, X);
02219         } else if (X == Z) {
02220           std::swap(Y, Z);
02221           std::swap(W, X);
02222         }
02223       }
02224 
02225       if (W == Y) {
02226         Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, Z,
02227                                                             LHS->getName()), I);
02228         return BinaryOperator::CreateMul(W, NewAdd);
02229       }
02230     }
02231   }
02232 
02233   if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
02234     Value *X = 0;
02235     if (match(LHS, m_Not(m_Value(X))))    // ~X + C --> (C-1) - X
02236       return BinaryOperator::CreateSub(SubOne(CRHS), X);
02237 
02238     // (X & FF00) + xx00  -> (X+xx00) & FF00
02239     if (LHS->hasOneUse() &&
02240         match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
02241       Constant *Anded = ConstantExpr::getAnd(CRHS, C2);
02242       if (Anded == CRHS) {
02243         // See if all bits from the first bit set in the Add RHS up are included
02244         // in the mask.  First, get the rightmost bit.
02245         const APInt& AddRHSV = CRHS->getValue();
02246 
02247         // Form a mask of all bits from the lowest bit added through the top.
02248         APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
02249 
02250         // See if the and mask includes all of these bits.
02251         APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
02252 
02253         if (AddRHSHighBits == AddRHSHighBitsAnd) {
02254           // Okay, the xform is safe.  Insert the new add pronto.
02255           Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, CRHS,
02256                                                             LHS->getName()), I);
02257           return BinaryOperator::CreateAnd(NewAdd, C2);
02258         }
02259       }
02260     }
02261 
02262     // Try to fold constant add into select arguments.
02263     if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
02264       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02265         return R;
02266   }
02267 
02268   // add (select X 0 (sub n A)) A  -->  select X A n
02269   {
02270     SelectInst *SI = dyn_cast<SelectInst>(LHS);
02271     Value *A = RHS;
02272     if (!SI) {
02273       SI = dyn_cast<SelectInst>(RHS);
02274       A = LHS;
02275     }
02276     if (SI && SI->hasOneUse()) {
02277       Value *TV = SI->getTrueValue();
02278       Value *FV = SI->getFalseValue();
02279       Value *N;
02280 
02281       // Can we fold the add into the argument of the select?
02282       // We check both true and false select arguments for a matching subtract.
02283       if (match(FV, m_Zero()) &&
02284           match(TV, m_Sub(m_Value(N), m_Specific(A))))
02285         // Fold the add into the true select value.
02286         return SelectInst::Create(SI->getCondition(), N, A);
02287       if (match(TV, m_Zero()) &&
02288           match(FV, m_Sub(m_Value(N), m_Specific(A))))
02289         // Fold the add into the false select value.
02290         return SelectInst::Create(SI->getCondition(), A, N);
02291     }
02292   }
02293 
02294   // Check for (add (sext x), y), see if we can merge this into an
02295   // integer add followed by a sext.
02296   if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
02297     // (add (sext x), cst) --> (sext (add x, cst'))
02298     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
02299       Constant *CI = 
02300         ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
02301       if (LHSConv->hasOneUse() &&
02302           ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
02303           WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
02304         // Insert the new, smaller add.
02305         Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), 
02306                                                         CI, "addconv");
02307         InsertNewInstBefore(NewAdd, I);
02308         return new SExtInst(NewAdd, I.getType());
02309       }
02310     }
02311     
02312     // (add (sext x), (sext y)) --> (sext (add int x, y))
02313     if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
02314       // Only do this if x/y have the same type, if at last one of them has a
02315       // single use (so we don't increase the number of sexts), and if the
02316       // integer add will not overflow.
02317       if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
02318           (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
02319           WillNotOverflowSignedAdd(LHSConv->getOperand(0),
02320                                    RHSConv->getOperand(0))) {
02321         // Insert the new integer add.
02322         Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), 
02323                                                         RHSConv->getOperand(0),
02324                                                         "addconv");
02325         InsertNewInstBefore(NewAdd, I);
02326         return new SExtInst(NewAdd, I.getType());
02327       }
02328     }
02329   }
02330 
02331   return Changed ? &I : 0;
02332 }
02333 
02334 Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
02335   bool Changed = SimplifyCommutative(I);
02336   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
02337 
02338   if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
02339     // X + 0 --> X
02340     if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
02341       if (CFP->isExactlyValue(ConstantFP::getNegativeZero
02342                               (I.getType())->getValueAPF()))
02343         return ReplaceInstUsesWith(I, LHS);
02344     }
02345 
02346     if (isa<PHINode>(LHS))
02347       if (Instruction *NV = FoldOpIntoPhi(I))
02348         return NV;
02349   }
02350 
02351   // -A + B  -->  B - A
02352   // -A + -B  -->  -(A + B)
02353   if (Value *LHSV = dyn_castFNegVal(LHS))
02354     return BinaryOperator::CreateFSub(RHS, LHSV);
02355 
02356   // A + -B  -->  A - B
02357   if (!isa<Constant>(RHS))
02358     if (Value *V = dyn_castFNegVal(RHS))
02359       return BinaryOperator::CreateFSub(LHS, V);
02360 
02361   // Check for X+0.0.  Simplify it to X if we know X is not -0.0.
02362   if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
02363     if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
02364       return ReplaceInstUsesWith(I, LHS);
02365 
02366   // Check for (add double (sitofp x), y), see if we can merge this into an
02367   // integer add followed by a promotion.
02368   if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
02369     // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
02370     // ... if the constant fits in the integer value.  This is useful for things
02371     // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
02372     // requires a constant pool load, and generally allows the add to be better
02373     // instcombined.
02374     if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
02375       Constant *CI = 
02376       ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
02377       if (LHSConv->hasOneUse() &&
02378           ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
02379           WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
02380         // Insert the new integer add.
02381         Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), 
02382                                                         CI, "addconv");
02383         InsertNewInstBefore(NewAdd, I);
02384         return new SIToFPInst(NewAdd, I.getType());
02385       }
02386     }
02387     
02388     // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
02389     if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
02390       // Only do this if x/y have the same type, if at last one of them has a
02391       // single use (so we don't increase the number of int->fp conversions),
02392       // and if the integer add will not overflow.
02393       if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
02394           (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
02395           WillNotOverflowSignedAdd(LHSConv->getOperand(0),
02396                                    RHSConv->getOperand(0))) {
02397         // Insert the new integer add.
02398         Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), 
02399                                                         RHSConv->getOperand(0),
02400                                                         "addconv");
02401         InsertNewInstBefore(NewAdd, I);
02402         return new SIToFPInst(NewAdd, I.getType());
02403       }
02404     }
02405   }
02406   
02407   return Changed ? &I : 0;
02408 }
02409 
02410 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
02411   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02412 
02413   if (Op0 == Op1)                        // sub X, X  -> 0
02414     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02415 
02416   // If this is a 'B = x-(-A)', change to B = x+A...
02417   if (Value *V = dyn_castNegVal(Op1))
02418     return BinaryOperator::CreateAdd(Op0, V);
02419 
02420   if (isa<UndefValue>(Op0))
02421     return ReplaceInstUsesWith(I, Op0);    // undef - X -> undef
02422   if (isa<UndefValue>(Op1))
02423     return ReplaceInstUsesWith(I, Op1);    // X - undef -> undef
02424 
02425   if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
02426     // Replace (-1 - A) with (~A)...
02427     if (C->isAllOnesValue())
02428       return BinaryOperator::CreateNot(Op1);
02429 
02430     // C - ~X == X + (1+C)
02431     Value *X = 0;
02432     if (match(Op1, m_Not(m_Value(X))))
02433       return BinaryOperator::CreateAdd(X, AddOne(C));
02434 
02435     // -(X >>u 31) -> (X >>s 31)
02436     // -(X >>s 31) -> (X >>u 31)
02437     if (C->isZero()) {
02438       if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) {
02439         if (SI->getOpcode() == Instruction::LShr) {
02440           if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
02441             // Check to see if we are shifting out everything but the sign bit.
02442             if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
02443                 SI->getType()->getPrimitiveSizeInBits()-1) {
02444               // Ok, the transformation is safe.  Insert AShr.
02445               return BinaryOperator::Create(Instruction::AShr, 
02446                                           SI->getOperand(0), CU, SI->getName());
02447             }
02448           }
02449         }
02450         else if (SI->getOpcode() == Instruction::AShr) {
02451           if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
02452             // Check to see if we are shifting out everything but the sign bit.
02453             if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
02454                 SI->getType()->getPrimitiveSizeInBits()-1) {
02455               // Ok, the transformation is safe.  Insert LShr. 
02456               return BinaryOperator::CreateLShr(
02457                                           SI->getOperand(0), CU, SI->getName());
02458             }
02459           }
02460         }
02461       }
02462     }
02463 
02464     // Try to fold constant sub into select arguments.
02465     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
02466       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02467         return R;
02468 
02469     // C - zext(bool) -> bool ? C - 1 : C
02470     if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1))
02471       if (ZI->getSrcTy() == Type::getInt1Ty(*Context))
02472         return SelectInst::Create(ZI->getOperand(0), SubOne(C), C);
02473   }
02474 
02475   if (I.getType() == Type::getInt1Ty(*Context))
02476     return BinaryOperator::CreateXor(Op0, Op1);
02477 
02478   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
02479     if (Op1I->getOpcode() == Instruction::Add) {
02480       if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y
02481         return BinaryOperator::CreateNeg(Op1I->getOperand(1),
02482                                          I.getName());
02483       else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y
02484         return BinaryOperator::CreateNeg(Op1I->getOperand(0),
02485                                          I.getName());
02486       else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
02487         if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
02488           // C1-(X+C2) --> (C1-C2)-X
02489           return BinaryOperator::CreateSub(
02490             ConstantExpr::getSub(CI1, CI2), Op1I->getOperand(0));
02491       }
02492     }
02493 
02494     if (Op1I->hasOneUse()) {
02495       // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
02496       // is not used by anyone else...
02497       //
02498       if (Op1I->getOpcode() == Instruction::Sub) {
02499         // Swap the two operands of the subexpr...
02500         Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
02501         Op1I->setOperand(0, IIOp1);
02502         Op1I->setOperand(1, IIOp0);
02503 
02504         // Create the new top level add instruction...
02505         return BinaryOperator::CreateAdd(Op0, Op1);
02506       }
02507 
02508       // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
02509       //
02510       if (Op1I->getOpcode() == Instruction::And &&
02511           (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
02512         Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
02513 
02514         Value *NewNot =
02515           InsertNewInstBefore(BinaryOperator::CreateNot(OtherOp, "B.not"), I);
02516         return BinaryOperator::CreateAnd(Op0, NewNot);
02517       }
02518 
02519       // 0 - (X sdiv C)  -> (X sdiv -C)
02520       if (Op1I->getOpcode() == Instruction::SDiv)
02521         if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
02522           if (CSI->isZero())
02523             if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1)))
02524               return BinaryOperator::CreateSDiv(Op1I->getOperand(0),
02525                                           ConstantExpr::getNeg(DivRHS));
02526 
02527       // X - X*C --> X * (1-C)
02528       ConstantInt *C2 = 0;
02529       if (dyn_castFoldableMul(Op1I, C2) == Op0) {
02530         Constant *CP1 = 
02531           ConstantExpr::getSub(ConstantInt::get(I.getType(), 1),
02532                                              C2);
02533         return BinaryOperator::CreateMul(Op0, CP1);
02534       }
02535     }
02536   }
02537 
02538   if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
02539     if (Op0I->getOpcode() == Instruction::Add) {
02540       if (Op0I->getOperand(0) == Op1)             // (Y+X)-Y == X
02541         return ReplaceInstUsesWith(I, Op0I->getOperand(1));
02542       else if (Op0I->getOperand(1) == Op1)        // (X+Y)-Y == X
02543         return ReplaceInstUsesWith(I, Op0I->getOperand(0));
02544     } else if (Op0I->getOpcode() == Instruction::Sub) {
02545       if (Op0I->getOperand(0) == Op1)             // (X-Y)-X == -Y
02546         return BinaryOperator::CreateNeg(Op0I->getOperand(1),
02547                                          I.getName());
02548     }
02549   }
02550 
02551   ConstantInt *C1;
02552   if (Value *X = dyn_castFoldableMul(Op0, C1)) {
02553     if (X == Op1)  // X*C - X --> X * (C-1)
02554       return BinaryOperator::CreateMul(Op1, SubOne(C1));
02555 
02556     ConstantInt *C2;   // X*C1 - X*C2 -> X * (C1-C2)
02557     if (X == dyn_castFoldableMul(Op1, C2))
02558       return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2));
02559   }
02560   return 0;
02561 }
02562 
02563 Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
02564   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02565 
02566   // If this is a 'B = x-(-A)', change to B = x+A...
02567   if (Value *V = dyn_castFNegVal(Op1))
02568     return BinaryOperator::CreateFAdd(Op0, V);
02569 
02570   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
02571     if (Op1I->getOpcode() == Instruction::FAdd) {
02572       if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y
02573         return BinaryOperator::CreateFNeg(Op1I->getOperand(1),
02574                                           I.getName());
02575       else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y
02576         return BinaryOperator::CreateFNeg(Op1I->getOperand(0),
02577                                           I.getName());
02578     }
02579   }
02580 
02581   return 0;
02582 }
02583 
02584 /// isSignBitCheck - Given an exploded icmp instruction, return true if the
02585 /// comparison only checks the sign bit.  If it only checks the sign bit, set
02586 /// TrueIfSigned if the result of the comparison is true when the input value is
02587 /// signed.
02588 static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
02589                            bool &TrueIfSigned) {
02590   switch (pred) {
02591   case ICmpInst::ICMP_SLT:   // True if LHS s< 0
02592     TrueIfSigned = true;
02593     return RHS->isZero();
02594   case ICmpInst::ICMP_SLE:   // True if LHS s<= RHS and RHS == -1
02595     TrueIfSigned = true;
02596     return RHS->isAllOnesValue();
02597   case ICmpInst::ICMP_SGT:   // True if LHS s> -1
02598     TrueIfSigned = false;
02599     return RHS->isAllOnesValue();
02600   case ICmpInst::ICMP_UGT:
02601     // True if LHS u> RHS and RHS == high-bit-mask - 1
02602     TrueIfSigned = true;
02603     return RHS->getValue() ==
02604       APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits());
02605   case ICmpInst::ICMP_UGE: 
02606     // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
02607     TrueIfSigned = true;
02608     return RHS->getValue().isSignBit();
02609   default:
02610     return false;
02611   }
02612 }
02613 
02614 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
02615   bool Changed = SimplifyCommutative(I);
02616   Value *Op0 = I.getOperand(0);
02617 
02618   if (isa<UndefValue>(I.getOperand(1)))              // undef * X -> 0
02619     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02620 
02621   // Simplify mul instructions with a constant RHS...
02622   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
02623     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
02624 
02625       // ((X << C1)*C2) == (X * (C2 << C1))
02626       if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0))
02627         if (SI->getOpcode() == Instruction::Shl)
02628           if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
02629             return BinaryOperator::CreateMul(SI->getOperand(0),
02630                                         ConstantExpr::getShl(CI, ShOp));
02631 
02632       if (CI->isZero())
02633         return ReplaceInstUsesWith(I, Op1);  // X * 0  == 0
02634       if (CI->equalsInt(1))                  // X * 1  == X
02635         return ReplaceInstUsesWith(I, Op0);
02636       if (CI->isAllOnesValue())              // X * -1 == 0 - X
02637         return BinaryOperator::CreateNeg(Op0, I.getName());
02638 
02639       const APInt& Val = cast<ConstantInt>(CI)->getValue();
02640       if (Val.isPowerOf2()) {          // Replace X*(2^C) with X << C
02641         return BinaryOperator::CreateShl(Op0,
02642                  ConstantInt::get(Op0->getType(), Val.logBase2()));
02643       }
02644     } else if (isa<VectorType>(Op1->getType())) {
02645       if (Op1->isNullValue())
02646         return ReplaceInstUsesWith(I, Op1);
02647 
02648       if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
02649         if (Op1V->isAllOnesValue())              // X * -1 == 0 - X
02650           return BinaryOperator::CreateNeg(Op0, I.getName());
02651 
02652         // As above, vector X*splat(1.0) -> X in all defined cases.
02653         if (Constant *Splat = Op1V->getSplatValue()) {
02654           if (ConstantInt *CI = dyn_cast<ConstantInt>(Splat))
02655             if (CI->equalsInt(1))
02656               return ReplaceInstUsesWith(I, Op0);
02657         }
02658       }
02659     }
02660     
02661     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
02662       if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() &&
02663           isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1)) {
02664         // Canonicalize (X+C1)*C2 -> X*C2+C1*C2.
02665         Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0),
02666                                                      Op1, "tmp");
02667         InsertNewInstBefore(Add, I);
02668         Value *C1C2 = ConstantExpr::getMul(Op1, 
02669                                            cast<Constant>(Op0I->getOperand(1)));
02670         return BinaryOperator::CreateAdd(Add, C1C2);
02671         
02672       }
02673 
02674     // Try to fold constant mul into select arguments.
02675     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
02676       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02677         return R;
02678 
02679     if (isa<PHINode>(Op0))
02680       if (Instruction *NV = FoldOpIntoPhi(I))
02681         return NV;
02682   }
02683 
02684   if (Value *Op0v = dyn_castNegVal(Op0))     // -X * -Y = X*Y
02685     if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
02686       return BinaryOperator::CreateMul(Op0v, Op1v);
02687 
02688   // (X / Y) *  Y = X - (X % Y)
02689   // (X / Y) * -Y = (X % Y) - X
02690   {
02691     Value *Op1 = I.getOperand(1);
02692     BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0);
02693     if (!BO ||
02694         (BO->getOpcode() != Instruction::UDiv && 
02695          BO->getOpcode() != Instruction::SDiv)) {
02696       Op1 = Op0;
02697       BO = dyn_cast<BinaryOperator>(I.getOperand(1));
02698     }
02699     Value *Neg = dyn_castNegVal(Op1);
02700     if (BO && BO->hasOneUse() &&
02701         (BO->getOperand(1) == Op1 || BO->getOperand(1) == Neg) &&
02702         (BO->getOpcode() == Instruction::UDiv ||
02703          BO->getOpcode() == Instruction::SDiv)) {
02704       Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
02705 
02706       // If the division is exact, X % Y is zero.
02707       if (SDivOperator *SDiv = dyn_cast<SDivOperator>(BO))
02708         if (SDiv->isExact()) {
02709           if (Op1BO == Op1)
02710             return ReplaceInstUsesWith(I, Op0BO);
02711           else
02712             return BinaryOperator::CreateNeg(Op0BO);
02713         }
02714 
02715       Instruction *Rem;
02716       if (BO->getOpcode() == Instruction::UDiv)
02717         Rem = BinaryOperator::CreateURem(Op0BO, Op1BO);
02718       else
02719         Rem = BinaryOperator::CreateSRem(Op0BO, Op1BO);
02720 
02721       InsertNewInstBefore(Rem, I);
02722       Rem->takeName(BO);
02723 
02724       if (Op1BO == Op1)
02725         return BinaryOperator::CreateSub(Op0BO, Rem);
02726       else
02727         return BinaryOperator::CreateSub(Rem, Op0BO);
02728     }
02729   }
02730 
02731   if (I.getType() == Type::getInt1Ty(*Context))
02732     return BinaryOperator::CreateAnd(Op0, I.getOperand(1));
02733 
02734   // If one of the operands of the multiply is a cast from a boolean value, then
02735   // we know the bool is either zero or one, so this is a 'masking' multiply.
02736   // See if we can simplify things based on how the boolean was originally
02737   // formed.
02738   CastInst *BoolCast = 0;
02739   if (ZExtInst *CI = dyn_cast<ZExtInst>(Op0))
02740     if (CI->getOperand(0)->getType() == Type::getInt1Ty(*Context))
02741       BoolCast = CI;
02742   if (!BoolCast)
02743     if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1)))
02744       if (CI->getOperand(0)->getType() == Type::getInt1Ty(*Context))
02745         BoolCast = CI;
02746   if (BoolCast) {
02747     if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) {
02748       Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1);
02749       const Type *SCOpTy = SCIOp0->getType();
02750       bool TIS = false;
02751       
02752       // If the icmp is true iff the sign bit of X is set, then convert this
02753       // multiply into a shift/and combination.
02754       if (isa<ConstantInt>(SCIOp1) &&
02755           isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) &&
02756           TIS) {
02757         // Shift the X value right to turn it into "all signbits".
02758         Constant *Amt = ConstantInt::get(SCIOp0->getType(),
02759                                           SCOpTy->getPrimitiveSizeInBits()-1);
02760         Value *V =
02761           InsertNewInstBefore(
02762             BinaryOperator::Create(Instruction::AShr, SCIOp0, Amt,
02763                                             BoolCast->getOperand(0)->getName()+
02764                                             ".mask"), I);
02765 
02766         // If the multiply type is not the same as the source type, sign extend
02767         // or truncate to the multiply type.
02768         if (I.getType() != V->getType()) {
02769           uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits();
02770           uint32_t DstBits = I.getType()->getPrimitiveSizeInBits();
02771           Instruction::CastOps opcode = 
02772             (SrcBits == DstBits ? Instruction::BitCast : 
02773              (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc));
02774           V = InsertCastBefore(opcode, V, I.getType(), I);
02775         }
02776 
02777         Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
02778         return BinaryOperator::CreateAnd(V, OtherOp);
02779       }
02780     }
02781   }
02782 
02783   return Changed ? &I : 0;
02784 }
02785 
02786 Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
02787   bool Changed = SimplifyCommutative(I);
02788   Value *Op0 = I.getOperand(0);
02789 
02790   // Simplify mul instructions with a constant RHS...
02791   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
02792     if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
02793       // "In IEEE floating point, x*1 is not equivalent to x for nans.  However,
02794       // ANSI says we can drop signals, so we can do this anyway." (from GCC)
02795       if (Op1F->isExactlyValue(1.0))
02796         return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul double %X, 1.0'
02797     } else if (isa<VectorType>(Op1->getType())) {
02798       if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
02799         // As above, vector X*splat(1.0) -> X in all defined cases.
02800         if (Constant *Splat = Op1V->getSplatValue()) {
02801           if (ConstantFP *F = dyn_cast<ConstantFP>(Splat))
02802             if (F->isExactlyValue(1.0))
02803               return ReplaceInstUsesWith(I, Op0);
02804         }
02805       }
02806     }
02807 
02808     // Try to fold constant mul into select arguments.
02809     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
02810       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02811         return R;
02812 
02813     if (isa<PHINode>(Op0))
02814       if (Instruction *NV = FoldOpIntoPhi(I))
02815         return NV;
02816   }
02817 
02818   if (Value *Op0v = dyn_castFNegVal(Op0))     // -X * -Y = X*Y
02819     if (Value *Op1v = dyn_castFNegVal(I.getOperand(1)))
02820       return BinaryOperator::CreateFMul(Op0v, Op1v);
02821 
02822   return Changed ? &I : 0;
02823 }
02824 
02825 /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select
02826 /// instruction.
02827 bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
02828   SelectInst *SI = cast<SelectInst>(I.getOperand(1));
02829   
02830   // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
02831   int NonNullOperand = -1;
02832   if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
02833     if (ST->isNullValue())
02834       NonNullOperand = 2;
02835   // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
02836   if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
02837     if (ST->isNullValue())
02838       NonNullOperand = 1;
02839   
02840   if (NonNullOperand == -1)
02841     return false;
02842   
02843   Value *SelectCond = SI->getOperand(0);
02844   
02845   // Change the div/rem to use 'Y' instead of the select.
02846   I.setOperand(1, SI->getOperand(NonNullOperand));
02847   
02848   // Okay, we know we replace the operand of the div/rem with 'Y' with no
02849   // problem.  However, the select, or the condition of the select may have
02850   // multiple uses.  Based on our knowledge that the operand must be non-zero,
02851   // propagate the known value for the select into other uses of it, and
02852   // propagate a known value of the condition into its other users.
02853   
02854   // If the select and condition only have a single use, don't bother with this,
02855   // early exit.
02856   if (SI->use_empty() && SelectCond->hasOneUse())
02857     return true;
02858   
02859   // Scan the current block backward, looking for other uses of SI.
02860   BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin();
02861   
02862   while (BBI != BBFront) {
02863     --BBI;
02864     // If we found a call to a function, we can't assume it will return, so
02865     // information from below it cannot be propagated above it.
02866     if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
02867       break;
02868     
02869     // Replace uses of the select or its condition with the known values.
02870     for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
02871          I != E; ++I) {
02872       if (*I == SI) {
02873         *I = SI->getOperand(NonNullOperand);
02874         AddToWorkList(BBI);
02875       } else if (*I == SelectCond) {
02876         *I = NonNullOperand == 1 ? ConstantInt::getTrue(*Context) :
02877                                    ConstantInt::getFalse(*Context);
02878         AddToWorkList(BBI);
02879       }
02880     }
02881     
02882     // If we past the instruction, quit looking for it.
02883     if (&*BBI == SI)
02884       SI = 0;
02885     if (&*BBI == SelectCond)
02886       SelectCond = 0;
02887     
02888     // If we ran out of things to eliminate, break out of the loop.
02889     if (SelectCond == 0 && SI == 0)
02890       break;
02891     
02892   }
02893   return true;
02894 }
02895 
02896 
02897 /// This function implements the transforms on div instructions that work
02898 /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is
02899 /// used by the visitors to those instructions.
02900 /// @brief Transforms common to all three div instructions
02901 Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) {
02902   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02903 
02904   // undef / X -> 0        for integer.
02905   // undef / X -> undef    for FP (the undef could be a snan).
02906   if (isa<UndefValue>(Op0)) {
02907     if (Op0->getType()->isFPOrFPVector())
02908       return ReplaceInstUsesWith(I, Op0);
02909     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02910   }
02911 
02912   // X / undef -> undef
02913   if (isa<UndefValue>(Op1))
02914     return ReplaceInstUsesWith(I, Op1);
02915 
02916   return 0;
02917 }
02918 
02919 /// This function implements the transforms common to both integer division
02920 /// instructions (udiv and sdiv). It is called by the visitors to those integer
02921 /// division instructions.
02922 /// @brief Common integer divide transforms
02923 Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
02924   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02925 
02926   // (sdiv X, X) --> 1     (udiv X, X) --> 1
02927   if (Op0 == Op1) {
02928     if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) {
02929       Constant *CI = ConstantInt::get(Ty->getElementType(), 1);
02930       std::vector<Constant*> Elts(Ty->getNumElements(), CI);
02931       return ReplaceInstUsesWith(I, ConstantVector::get(Elts));
02932     }
02933 
02934     Constant *CI = ConstantInt::get(I.getType(), 1);
02935     return ReplaceInstUsesWith(I, CI);
02936   }
02937   
02938   if (Instruction *Common = commonDivTransforms(I))
02939     return Common;
02940   
02941   // Handle cases involving: [su]div X, (select Cond, Y, Z)
02942   // This does not apply for fdiv.
02943   if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
02944     return &I;
02945 
02946   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
02947     // div X, 1 == X
02948     if (RHS->equalsInt(1))
02949       return ReplaceInstUsesWith(I, Op0);
02950 
02951     // (X / C1) / C2  -> X / (C1*C2)
02952     if (Instruction *LHS = dyn_cast<Instruction>(Op0))
02953       if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
02954         if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
02955           if (MultiplyOverflows(RHS, LHSRHS,
02956                                 I.getOpcode()==Instruction::SDiv))
02957             return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02958           else 
02959             return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
02960                                       ConstantExpr::getMul(RHS, LHSRHS));
02961         }
02962 
02963     if (!RHS->isZero()) { // avoid X udiv 0
02964       if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
02965         if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02966           return R;
02967       if (isa<PHINode>(Op0))
02968         if (Instruction *NV = FoldOpIntoPhi(I))
02969           return NV;
02970     }
02971   }
02972 
02973   // 0 / X == 0, we don't need to preserve faults!
02974   if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0))
02975     if (LHS->equalsInt(0))
02976       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02977 
02978   // It can't be division by zero, hence it must be division by one.
02979   if (I.getType() == Type::getInt1Ty(*Context))
02980     return ReplaceInstUsesWith(I, Op0);
02981 
02982   if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
02983     if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue()))
02984       // div X, 1 == X
02985       if (X->isOne())
02986         return ReplaceInstUsesWith(I, Op0);
02987   }
02988 
02989   return 0;
02990 }
02991 
02992 Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
02993   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02994 
02995   // Handle the integer div common cases
02996   if (Instruction *Common = commonIDivTransforms(I))
02997     return Common;
02998 
02999   if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) {
03000     // X udiv C^2 -> X >> C
03001     // Check to see if this is an unsigned division with an exact power of 2,
03002     // if so, convert to a right shift.
03003     if (C->getValue().isPowerOf2())  // 0 not included in isPowerOf2
03004       return BinaryOperator::CreateLShr(Op0, 
03005             ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
03006 
03007     // X udiv C, where C >= signbit
03008     if (C->getValue().isNegative()) {
03009       Value *IC = InsertNewInstBefore(new ICmpInst(*Context,
03010                                                     ICmpInst::ICMP_ULT, Op0, C),
03011                                       I);
03012       return SelectInst::Create(IC, Constant::getNullValue(I.getType()),
03013                                 ConstantInt::get(I.getType(), 1));
03014     }
03015   }
03016 
03017   // X udiv (C1 << N), where C1 is "1<<C2"  -->  X >> (N+C2)
03018   if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) {
03019     if (RHSI->getOpcode() == Instruction::Shl &&
03020         isa<ConstantInt>(RHSI->getOperand(0))) {
03021       const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue();
03022       if (C1.isPowerOf2()) {
03023         Value *N = RHSI->getOperand(1);
03024         const Type *NTy = N->getType();
03025         if (uint32_t C2 = C1.logBase2()) {
03026           Constant *C2V = ConstantInt::get(NTy, C2);
03027           N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I);
03028         }
03029         return BinaryOperator::CreateLShr(Op0, N);
03030       }
03031     }
03032   }
03033   
03034   // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2)
03035   // where C1&C2 are powers of two.
03036   if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) 
03037     if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
03038       if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2)))  {
03039         const APInt &TVA = STO->getValue(), &FVA = SFO->getValue();
03040         if (TVA.isPowerOf2() && FVA.isPowerOf2()) {
03041           // Compute the shift amounts
03042           uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
03043           // Construct the "on true" case of the select
03044           Constant *TC = ConstantInt::get(Op0->getType(), TSA);
03045           Instruction *TSI = BinaryOperator::CreateLShr(
03046                                                  Op0, TC, SI->getName()+".t");
03047           TSI = InsertNewInstBefore(TSI, I);
03048   
03049           // Construct the "on false" case of the select
03050           Constant *FC = ConstantInt::get(Op0->getType(), FSA); 
03051           Instruction *FSI = BinaryOperator::CreateLShr(
03052                                                  Op0, FC, SI->getName()+".f");
03053           FSI = InsertNewInstBefore(FSI, I);
03054 
03055           // construct the select instruction and return it.
03056           return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName());
03057         }
03058       }
03059   return 0;
03060 }
03061 
03062 Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
03063   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
03064 
03065   // Handle the integer div common cases
03066   if (Instruction *Common = commonIDivTransforms(I))
03067     return Common;
03068 
03069   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
03070     // sdiv X, -1 == -X
03071     if (RHS->isAllOnesValue())
03072       return BinaryOperator::CreateNeg(Op0);
03073 
03074     // sdiv X, C  -->  ashr X, log2(C)
03075     if (cast<SDivOperator>(&I)->isExact() &&
03076         RHS->getValue().isNonNegative() &&
03077         RHS->getValue().isPowerOf2()) {
03078       Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
03079                                             RHS->getValue().exactLogBase2());
03080       return BinaryOperator::CreateAShr(Op0, ShAmt, I.getName());
03081     }
03082 
03083     // -X/C  -->  X/-C  provided the negation doesn't overflow.
03084     if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
03085       if (isa<Constant>(Sub->getOperand(0)) &&
03086           cast<Constant>(Sub->getOperand(0))->isNullValue() &&
03087           Sub->hasNoSignedWrap())
03088         return BinaryOperator::CreateSDiv(Sub->getOperand(1),
03089                                           ConstantExpr::getNeg(RHS));
03090   }
03091 
03092   // If the sign bits of both operands are zero (i.e. we can prove they are
03093   // unsigned inputs), turn this into a udiv.
03094   if (I.getType()->isInteger()) {
03095     APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
03096     if (MaskedValueIsZero(Op0, Mask)) {
03097       if (MaskedValueIsZero(Op1, Mask)) {
03098         // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
03099         return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
03100       }
03101       ConstantInt *ShiftedInt;
03102       if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value())) &&
03103           ShiftedInt->getValue().isPowerOf2()) {
03104         // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
03105         // Safe because the only negative value (1 << Y) can take on is
03106         // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
03107         // the sign bit set.
03108         return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
03109       }
03110     }
03111   }
03112   
03113   return 0;
03114 }
03115 
03116 Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
03117   return commonDivTransforms(I);
03118 }
03119 
03120 /// This function implements the transforms on rem instructions that work
03121 /// regardless of the kind of rem instruction it is (urem, srem, or frem). It 
03122 /// is used by the visitors to those instructions.
03123 /// @brief Transforms common to all three rem instructions
03124 Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) {
03125   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
03126 
03127   if (isa<UndefValue>(Op0)) {             // undef % X -> 0
03128     if (I.getType()->isFPOrFPVector())
03129       return ReplaceInstUsesWith(I, Op0);  // X % undef -> undef (could be SNaN)
03130     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
03131   }
03132   if (isa<UndefValue>(Op1))
03133     return ReplaceInstUsesWith(I, Op1);  // X % undef -> undef
03134 
03135   // Handle cases involving: rem X, (select Cond, Y, Z)
03136   if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
03137     return &I;
03138 
03139   return 0;
03140 }
03141 
03142 /// This function implements the transforms common to both integer remainder
03143 /// instructions (urem and srem). It is called by the visitors to those integer
03144 /// remainder instructions.
03145 /// @brief Common integer remainder transforms
03146 Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
03147   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
03148 
03149   if (Instruction *common = commonRemTransforms(I))
03150     return common;
03151 
03152   // 0 % X == 0 for integer, we don't need to preserve faults!
03153   if (Constant *LHS = dyn_cast<Constant>(Op0))
03154     if (LHS->isNullValue())
03155       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
03156 
03157   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
03158     // X % 0 == undef, we don't need to preserve faults!
03159     if (RHS->equalsInt(0))
03160       return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
03161     
03162     if (RHS->equalsInt(1))  // X % 1 == 0
03163       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
03164 
03165     if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
03166       if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
03167         if (Instruction *R = FoldOpIntoSelect(I, SI, this))
03168           return R;
03169       } else if (isa<PHINode>(Op0I)) {
03170         if (Instruction *NV = FoldOpIntoPhi(I))
03171           return NV;
03172       }
03173 
03174       // See if we can fold away this rem instruction.
03175       if (SimplifyDemandedInstructionBits(I))
03176         return &I;
03177     }
03178   }
03179 
03180   return 0;
03181 }
03182 
03183 Instruction *InstCombiner::visitURem(BinaryOperator &I) {
03184   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
03185 
03186   if (Instruction *common = commonIRemTransforms(I))
03187     return common;
03188   
03189   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
03190     // X urem C^2 -> X and C
03191     // Check to see if this is an unsigned remainder with an exact power of 2,
03192     // if so, convert to a bitwise and.
03193     if (ConstantInt *C = dyn_cast<ConstantInt>(RHS))
03194       if (C->getValue().isPowerOf2())
03195         return BinaryOperator::CreateAnd(Op0, SubOne(C));
03196   }
03197 
03198   if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
03199     // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1)  
03200     if (RHSI->getOpcode() == Instruction::Shl &&
03201         isa<ConstantInt>(RHSI->getOperand(0))) {
03202       if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) {
03203         Constant *N1 = Constant::getAllOnesValue(I.getType());
03204         Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1,
03205                                                                    "tmp"), I);
03206         return BinaryOperator::CreateAnd(Op0, Add);
03207       }
03208     }
03209   }
03210 
03211   // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2)
03212   // where C1&C2 are powers of two.
03213   if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
03214     if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
03215       if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) {
03216         // STO == 0 and SFO == 0 handled above.
03217         if ((STO->getValue().isPowerOf2()) && 
03218             (SFO->getValue().isPowerOf2())) {
03219           Value *TrueAnd = InsertNewInstBefore(
03220             BinaryOperator::CreateAnd(Op0, SubOne(STO),
03221                                       SI->getName()+".t"), I);
03222           Value *FalseAnd = InsertNewInstBefore(
03223             BinaryOperator::CreateAnd(Op0, SubOne(SFO),
03224                                       SI->getName()+".f"), I);
03225           return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
03226         }
03227       }
03228   }
03229   
03230   return 0;
03231 }
03232 
03233 Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
03234   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
03235 
03236   // Handle the integer rem common cases
03237   if (Instruction *common = commonIRemTransforms(I))
03238     return common;
03239   
03240   if (Value *RHSNeg = dyn_castNegVal(Op1))
03241     if (!isa<Constant>(RHSNeg) ||
03242         (isa<ConstantInt>(RHSNeg) &&
03243          cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
03244       // X % -Y -> X % Y
03245       AddUsesToWorkList(I);
03246       I.setOperand(1, RHSNeg);
03247       return &I;
03248     }
03249 
03250   // If the sign bits of both operands are zero (i.e. we can prove they are
03251   // unsigned inputs), turn this into a urem.
03252   if (I.getType()->isInteger()) {
03253     APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
03254     if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
03255       // X srem Y -> X urem Y, iff X and Y don't have sign bit set
03256       return BinaryOperator::CreateURem(Op0, Op1, I.getName());
03257     }
03258   }
03259 
03260   // If it's a constant vector, flip any negative values positive.
03261   if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) {
03262     unsigned VWidth = RHSV->getNumOperands();
03263 
03264     bool hasNegative = false;
03265     for (unsigned i = 0; !hasNegative && i != VWidth; ++i)
03266       if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i)))
03267         if (RHS->getValue().isNegative())
03268           hasNegative = true;
03269 
03270     if (hasNegative) {
03271       std::vector<Constant *> Elts(VWidth);
03272       for (unsigned i = 0; i != VWidth; ++i) {
03273         if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) {
03274           if (RHS->getValue().isNegative())
03275             Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
03276           else
03277             Elts[i] = RHS;
03278         }
03279       }
03280 
03281       Constant *NewRHSV = ConstantVector::get(Elts);
03282       if (NewRHSV != RHSV) {
03283         AddUsesToWorkList(I);
03284         I.setOperand(1, NewRHSV);
03285         return &I;
03286       }
03287     }
03288   }
03289 
03290   return 0;
03291 }
03292 
03293 Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
03294   return commonRemTransforms(I);
03295 }
03296 
03297 // isOneBitSet - Return true if there is exactly one bit set in the specified
03298 // constant.
03299 static bool isOneBitSet(const ConstantInt *CI) {
03300   return CI->getValue().isPowerOf2();
03301 }
03302 
03303 // isHighOnes - Return true if the constant is of the form 1+0+.
03304 // This is the same as lowones(~X).
03305 static bool isHighOnes(const ConstantInt *CI) {
03306   return (~CI->getValue() + 1).isPowerOf2();
03307 }
03308 
03309 /// getICmpCode - Encode a icmp predicate into a three bit mask.  These bits
03310 /// are carefully arranged to allow folding of expressions such as:
03311 ///
03312 ///      (A < B) | (A > B) --> (A != B)
03313 ///
03314 /// Note that this is only valid if the first and second predicates have the
03315 /// same sign. Is illegal to do: (A u< B) | (A s> B) 
03316 ///
03317 /// Three bits are used to represent the condition, as follows:
03318 ///   0  A > B
03319 ///   1  A == B
03320 ///   2  A < B
03321 ///
03322 /// <=>  Value  Definition
03323 /// 000     0   Always false
03324 /// 001     1   A >  B
03325 /// 010     2   A == B
03326 /// 011     3   A >= B
03327 /// 100     4   A <  B
03328 /// 101     5   A != B
03329 /// 110     6   A <= B
03330 /// 111     7   Always true
03331 ///  
03332 static unsigned getICmpCode(const ICmpInst *ICI) {
03333   switch (ICI->getPredicate()) {
03334     // False -> 0
03335   case ICmpInst::ICMP_UGT: return 1;  // 001
03336   case ICmpInst::ICMP_SGT: return 1;  // 001
03337   case ICmpInst::ICMP_EQ:  return 2;  // 010
03338   case ICmpInst::ICMP_UGE: return 3;  // 011
03339   case ICmpInst::ICMP_SGE: return 3;  // 011
03340   case ICmpInst::ICMP_ULT: return 4;  // 100
03341   case ICmpInst::ICMP_SLT: return 4;  // 100
03342   case ICmpInst::ICMP_NE:  return 5;  // 101
03343   case ICmpInst::ICMP_ULE: return 6;  // 110
03344   case ICmpInst::ICMP_SLE: return 6;  // 110
03345     // True -> 7
03346   default:
03347     llvm_unreachable("Invalid ICmp predicate!");
03348     return 0;
03349   }
03350 }
03351 
03352 /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp
03353 /// predicate into a three bit mask. It also returns whether it is an ordered
03354 /// predicate by reference.
03355 static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) {
03356   isOrdered = false;
03357   switch (CC) {
03358   case FCmpInst::FCMP_ORD: isOrdered = true; return 0;  // 000
03359   case FCmpInst::FCMP_UNO:                   return 0;  // 000
03360   case FCmpInst::FCMP_OGT: isOrdered = true; return 1;  // 001
03361   case FCmpInst::FCMP_UGT:                   return 1;  // 001
03362   case FCmpInst::FCMP_OEQ: isOrdered = true; return 2;  // 010
03363   case FCmpInst::FCMP_UEQ:                   return 2;  // 010
03364   case FCmpInst::FCMP_OGE: isOrdered = true; return 3;  // 011
03365   case FCmpInst::FCMP_UGE:                   return 3;  // 011
03366   case FCmpInst::FCMP_OLT: isOrdered = true; return 4;  // 100
03367   case FCmpInst::FCMP_ULT:                   return 4;  // 100
03368   case FCmpInst::FCMP_ONE: isOrdered = true; return 5;  // 101
03369   case FCmpInst::FCMP_UNE:                   return 5;  // 101
03370   case FCmpInst::FCMP_OLE: isOrdered = true; return 6;  // 110
03371   case FCmpInst::FCMP_ULE:                   return 6;  // 110
03372     // True -> 7
03373   default:
03374     // Not expecting FCMP_FALSE and FCMP_TRUE;
03375     llvm_unreachable("Unexpected FCmp predicate!");
03376     return 0;
03377   }
03378 }
03379 
03380 /// getICmpValue - This is the complement of getICmpCode, which turns an
03381 /// opcode and two operands into either a constant true or false, or a brand 
03382 /// new ICmp instruction. The sign is passed in to determine which kind
03383 /// of predicate to use in the new icmp instruction.
03384 static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS,
03385                            LLVMContext *Context) {
03386   switch (code) {
03387   default: llvm_unreachable("Illegal ICmp code!");
03388   case  0: return ConstantInt::getFalse(*Context);
03389   case  1: 
03390     if (sign)
03391       return new ICmpInst(*Context, ICmpInst::ICMP_SGT, LHS, RHS);
03392     else
03393       return new ICmpInst(*Context, ICmpInst::ICMP_UGT, LHS, RHS);
03394   case  2: return new ICmpInst(*Context, ICmpInst::ICMP_EQ,  LHS, RHS);
03395   case  3: 
03396     if (sign)
03397       return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHS, RHS);
03398     else
03399       return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHS, RHS);
03400   case  4: 
03401     if (sign)
03402       return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHS, RHS);
03403     else
03404       return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHS, RHS);
03405   case  5: return new ICmpInst(*Context, ICmpInst::ICMP_NE,  LHS, RHS);
03406   case  6: 
03407     if (sign)
03408       return new ICmpInst(*Context, ICmpInst::ICMP_SLE, LHS, RHS);
03409     else
03410       return new ICmpInst(*Context, ICmpInst::ICMP_ULE, LHS, RHS);
03411   case  7: return ConstantInt::getTrue(*Context);
03412   }
03413 }
03414 
03415 /// getFCmpValue - This is the complement of getFCmpCode, which turns an
03416 /// opcode and two operands into either a FCmp instruction. isordered is passed
03417 /// in to determine which kind of predicate to use in the new fcmp instruction.
03418 static Value *getFCmpValue(bool isordered, unsigned code,
03419                            Value *LHS, Value *RHS, LLVMContext *Context) {
03420   switch (code) {
03421   default: llvm_unreachable("Illegal FCmp code!");
03422   case  0:
03423     if (isordered)
03424       return new FCmpInst(*Context, FCmpInst::FCMP_ORD, LHS, RHS);
03425     else
03426       return new FCmpInst(*Context, FCmpInst::FCMP_UNO, LHS, RHS);
03427   case  1: 
03428     if (isordered)
03429       return new FCmpInst(*Context, FCmpInst::FCMP_OGT, LHS, RHS);
03430     else
03431       return new FCmpInst(*Context, FCmpInst::FCMP_UGT, LHS, RHS);
03432   case  2: 
03433     if (isordered)
03434       return new FCmpInst(*Context, FCmpInst::FCMP_OEQ, LHS, RHS);
03435     else
03436       return new FCmpInst(*Context, FCmpInst::FCMP_UEQ, LHS, RHS);
03437   case  3: 
03438     if (isordered)
03439       return new FCmpInst(*Context, FCmpInst::FCMP_OGE, LHS, RHS);
03440     else
03441       return new FCmpInst(*Context, FCmpInst::FCMP_UGE, LHS, RHS);
03442   case  4: 
03443     if (isordered)
03444       return new FCmpInst(*Context, FCmpInst::FCMP_OLT, LHS, RHS);
03445     else
03446       return new FCmpInst(*Context, FCmpInst::FCMP_ULT, LHS, RHS);
03447   case  5: 
03448     if (isordered)
03449       return new FCmpInst(*Context, FCmpInst::FCMP_ONE, LHS, RHS);
03450     else
03451       return new FCmpInst(*Context, FCmpInst::FCMP_UNE, LHS, RHS);
03452   case  6: 
03453     if (isordered)
03454       return new FCmpInst(*Context, FCmpInst::FCMP_OLE, LHS, RHS);
03455     else
03456       return new FCmpInst(*Context, FCmpInst::FCMP_ULE, LHS, RHS);
03457   case  7: return ConstantInt::getTrue(*Context);
03458   }
03459 }
03460 
03461 /// PredicatesFoldable - Return true if both predicates match sign or if at
03462 /// least one of them is an equality comparison (which is signless).
03463 static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
03464   return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) ||
03465          (ICmpInst::isSignedPredicate(p1) && ICmpInst::isEquality(p2)) ||
03466          (ICmpInst::isSignedPredicate(p2) && ICmpInst::isEquality(p1));
03467 }
03468 
03469 namespace { 
03470 // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
03471 struct FoldICmpLogical {
03472   InstCombiner &IC;
03473   Value *LHS, *RHS;
03474   ICmpInst::Predicate pred;
03475   FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI)
03476     : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)),
03477       pred(ICI->getPredicate()) {}
03478   bool shouldApply(Value *V) const {
03479     if (ICmpInst *ICI = dyn_cast<ICmpInst>(V))
03480       if (PredicatesFoldable(pred, ICI->getPredicate()))
03481         return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) ||
03482                 (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS));
03483     return false;
03484   }
03485   Instruction *apply(Instruction &Log) const {
03486     ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0));
03487     if (ICI->getOperand(0) != LHS) {
03488       assert(ICI->getOperand(1) == LHS);
03489       ICI->swapOperands();  // Swap the LHS and RHS of the ICmp
03490     }
03491 
03492     ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1));
03493     unsigned LHSCode = getICmpCode(ICI);
03494     unsigned RHSCode = getICmpCode(RHSICI);
03495     unsigned Code;
03496     switch (Log.getOpcode()) {
03497     case Instruction::And: Code = LHSCode & RHSCode; break;
03498     case Instruction::Or:  Code = LHSCode | RHSCode; break;
03499     case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
03500     default: llvm_unreachable("Illegal logical opcode!"); return 0;
03501     }
03502 
03503     bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || 
03504                     ICmpInst::isSignedPredicate(ICI->getPredicate());
03505       
03506     Value *RV = getICmpValue(isSigned, Code, LHS, RHS, IC.getContext());
03507     if (Instruction *I = dyn_cast<Instruction>(RV))
03508       return I;
03509     // Otherwise, it's a constant boolean value...
03510     return IC.ReplaceInstUsesWith(Log, RV);
03511   }
03512 };
03513 } // end anonymous namespace
03514 
03515 // OptAndOp - This handles expressions of the form ((val OP C1) & C2).  Where
03516 // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is
03517 // guaranteed to be a binary operator.
03518 Instruction *InstCombiner::OptAndOp(Instruction *Op,
03519                                     ConstantInt *OpRHS,
03520                                     ConstantInt *AndRHS,
03521                                     BinaryOperator &TheAnd) {
03522   Value *X = Op->getOperand(0);
03523   Constant *Together = 0;
03524   if (!Op->isShift())
03525     Together = ConstantExpr::getAnd(AndRHS, OpRHS);
03526 
03527   switch (Op->getOpcode()) {
03528   case Instruction::Xor:
03529     if (Op->hasOneUse()) {
03530       // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
03531       Instruction *And = BinaryOperator::CreateAnd(X, AndRHS);
03532       InsertNewInstBefore(And, TheAnd);
03533       And->takeName(Op);
03534       return BinaryOperator::CreateXor(And, Together);
03535     }
03536     break;
03537   case Instruction::Or:
03538     if (Together == AndRHS) // (X | C) & C --> C
03539       return ReplaceInstUsesWith(TheAnd, AndRHS);
03540 
03541     if (Op->hasOneUse() && Together != OpRHS) {
03542       // (X | C1) & C2 --> (X | (C1&C2)) & C2
03543       Instruction *Or = BinaryOperator::CreateOr(X, Together);
03544       InsertNewInstBefore(Or, TheAnd);
03545       Or->takeName(Op);
03546       return BinaryOperator::CreateAnd(Or, AndRHS);
03547     }
03548     break;
03549   case Instruction::Add:
03550     if (Op->hasOneUse()) {
03551       // Adding a one to a single bit bit-field should be turned into an XOR
03552       // of the bit.  First thing to check is to see if this AND is with a
03553       // single bit constant.
03554       const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
03555 
03556       // If there is only one bit set...
03557       if (isOneBitSet(cast<ConstantInt>(AndRHS))) {
03558         // Ok, at this point, we know that we are masking the result of the
03559         // ADD down to exactly one bit.  If the constant we are adding has
03560         // no bits set below this bit, then we can eliminate the ADD.
03561         const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
03562 
03563         // Check to see if any bits below the one bit set in AndRHSV are set.
03564         if ((AddRHS & (AndRHSV-1)) == 0) {
03565           // If not, the only thing that can effect the output of the AND is
03566           // the bit specified by AndRHSV.  If that bit is set, the effect of
03567           // the XOR is to toggle the bit.  If it is clear, then the ADD has
03568           // no effect.
03569           if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
03570             TheAnd.setOperand(0, X);
03571             return &TheAnd;
03572           } else {
03573             // Pull the XOR out of the AND.
03574             Instruction *NewAnd = BinaryOperator::CreateAnd(X, AndRHS);
03575             InsertNewInstBefore(NewAnd, TheAnd);
03576             NewAnd->takeName(Op);
03577             return BinaryOperator::CreateXor(NewAnd, AndRHS);
03578           }
03579         }
03580       }
03581     }
03582     break;
03583 
03584   case Instruction::Shl: {
03585     // We know that the AND will not produce any of the bits shifted in, so if
03586     // the anded constant includes them, clear them now!
03587     //
03588     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
03589     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
03590     APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
03591     ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShlMask);
03592 
03593     if (CI->getValue() == ShlMask) { 
03594     // Masking out bits that the shift already masks
03595       return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and.
03596     } else if (CI != AndRHS) {                  // Reducing bits set in and.
03597       TheAnd.setOperand(1, CI);
03598       return &TheAnd;
03599     }
03600     break;
03601   }
03602   case Instruction::LShr:
03603   {
03604     // We know that the AND will not produce any of the bits shifted in, so if
03605     // the anded constant includes them, clear them now!  This only applies to
03606     // unsigned shifts, because a signed shr may bring in set bits!
03607     //
03608     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
03609     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
03610     APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
03611     ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
03612 
03613     if (CI->getValue() == ShrMask) {   
03614     // Masking out bits that the shift already masks.
03615       return ReplaceInstUsesWith(TheAnd, Op);
03616     } else if (CI != AndRHS) {
03617       TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
03618       return &TheAnd;
03619     }
03620     break;
03621   }
03622   case Instruction::AShr:
03623     // Signed shr.
03624     // See if this is shifting in some sign extension, then masking it out
03625     // with an and.
03626     if (Op->hasOneUse()) {
03627       uint32_t BitWidth = AndRHS->getType()->getBitWidth();
03628       uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
03629       APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
03630       Constant *C = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
03631       if (C == AndRHS) {          // Masking out bits shifted in.
03632         // (Val ashr C1) & C2 -> (Val lshr C1) & C2
03633         // Make the argument unsigned.
03634         Value *ShVal = Op->getOperand(0);
03635         ShVal = InsertNewInstBefore(
03636             BinaryOperator::CreateLShr(ShVal, OpRHS, 
03637                                    Op->getName()), TheAnd);
03638         return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
03639       }
03640     }
03641     break;
03642   }
03643   return 0;
03644 }
03645 
03646 
03647 /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is
03648 /// true, otherwise (V < Lo || V >= Hi).  In pratice, we emit the more efficient
03649 /// (V-Lo) <u Hi-Lo.  This method expects that Lo <= Hi. isSigned indicates
03650 /// whether to treat the V, Lo and HI as signed or not. IB is the location to
03651 /// insert new instructions.
03652 Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
03653                                            bool isSigned, bool Inside, 
03654                                            Instruction &IB) {
03655   assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? 
03656             ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
03657          "Lo is not <= Hi in range emission code!");
03658     
03659   if (Inside) {
03660     if (Lo == Hi)  // Trivially false.
03661       return new ICmpInst(*Context, ICmpInst::ICMP_NE, V, V);
03662 
03663     // V >= Min && V < Hi --> V < Hi
03664     if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
03665       ICmpInst::Predicate pred = (isSigned ? 
03666         ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
03667       return new ICmpInst(*Context, pred, V, Hi);
03668     }
03669 
03670     // Emit V-Lo <u Hi-Lo
03671     Constant *NegLo = ConstantExpr::getNeg(Lo);
03672     Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
03673     InsertNewInstBefore(Add, IB);
03674     Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
03675     return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, UpperBound);
03676   }
03677 
03678   if (Lo == Hi)  // Trivially true.
03679     return new ICmpInst(*Context, ICmpInst::ICMP_EQ, V, V);
03680 
03681   // V < Min || V >= Hi -> V > Hi-1
03682   Hi = SubOne(cast<ConstantInt>(Hi));
03683   if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
03684     ICmpInst::Predicate pred = (isSigned ? 
03685         ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
03686     return new ICmpInst(*Context, pred, V, Hi);
03687   }
03688 
03689   // Emit V-Lo >u Hi-1-Lo
03690   // Note that Hi has already had one subtracted from it, above.
03691   ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
03692   Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
03693   InsertNewInstBefore(Add, IB);
03694   Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
03695   return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add, LowerBound);
03696 }
03697 
03698 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
03699 // any number of 0s on either side.  The 1s are allowed to wrap from LSB to
03700 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
03701 // not, since all 1s are not contiguous.
03702 static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
03703   const APInt& V = Val->getValue();
03704   uint32_t BitWidth = Val->getType()->getBitWidth();
03705   if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
03706 
03707   // look for the first zero bit after the run of ones
03708   MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
03709   // look for the first non-zero bit
03710   ME = V.getActiveBits(); 
03711   return true;
03712 }
03713 
03714 /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
03715 /// where isSub determines whether the operator is a sub.  If we can fold one of
03716 /// the following xforms:
03717 /// 
03718 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
03719 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
03720 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
03721 ///
03722 /// return (A +/- B).
03723 ///
03724 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
03725                                         ConstantInt *Mask, bool isSub,
03726                                         Instruction &I) {
03727   Instruction *LHSI = dyn_cast<Instruction>(LHS);
03728   if (!LHSI || LHSI->getNumOperands() != 2 ||
03729       !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
03730 
03731   ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
03732 
03733   switch (LHSI->getOpcode()) {
03734   default: return 0;
03735   case Instruction::And:
03736     if (ConstantExpr::getAnd(N, Mask) == Mask) {
03737       // If the AndRHS is a power of two minus one (0+1+), this is simple.
03738       if ((Mask->getValue().countLeadingZeros() + 
03739            Mask->getValue().countPopulation()) == 
03740           Mask->getValue().getBitWidth())
03741         break;
03742 
03743       // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
03744       // part, we don't need any explicit masks to take them out of A.  If that
03745       // is all N is, ignore it.
03746       uint32_t MB = 0, ME = 0;
03747       if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
03748         uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
03749         APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
03750         if (MaskedValueIsZero(RHS, Mask))
03751           break;
03752       }
03753     }
03754     return 0;
03755   case Instruction::Or:
03756   case Instruction::Xor:
03757     // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
03758     if ((Mask->getValue().countLeadingZeros() + 
03759          Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
03760         && ConstantExpr::getAnd(N, Mask)->isNullValue())
03761       break;
03762     return 0;
03763   }
03764   
03765   Instruction *New;
03766   if (isSub)
03767     New = BinaryOperator::CreateSub(LHSI->getOperand(0), RHS, "fold");
03768   else
03769     New = BinaryOperator::CreateAdd(LHSI->getOperand(0), RHS, "fold");
03770   return InsertNewInstBefore(New, I);
03771 }
03772 
03773 /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
03774 Instruction *InstCombiner::FoldAndOfICmps(Instruction &I,
03775                                           ICmpInst *LHS, ICmpInst *RHS) {
03776   Value *Val, *Val2;
03777   ConstantInt *LHSCst, *RHSCst;
03778   ICmpInst::Predicate LHSCC, RHSCC;
03779   
03780   // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
03781   if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
03782                          m_ConstantInt(LHSCst))) ||
03783       !match(RHS, m_ICmp(RHSCC, m_Value(Val2),
03784                          m_ConstantInt(RHSCst))))
03785     return 0;
03786   
03787   // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
03788   // where C is a power of 2
03789   if (LHSCst == RHSCst && LHSCC == RHSCC && LHSCC == ICmpInst::ICMP_ULT &&
03790       LHSCst->getValue().isPowerOf2()) {
03791     Instruction *NewOr = BinaryOperator::CreateOr(Val, Val2);
03792     InsertNewInstBefore(NewOr, I);
03793     return new ICmpInst(*Context, LHSCC, NewOr, LHSCst);
03794   }
03795   
03796   // From here on, we only handle:
03797   //    (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
03798   if (Val != Val2) return 0;
03799   
03800   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
03801   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
03802       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
03803       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
03804       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
03805     return 0;
03806   
03807   // We can't fold (ugt x, C) & (sgt x, C2).
03808   if (!PredicatesFoldable(LHSCC, RHSCC))
03809     return 0;
03810     
03811   // Ensure that the larger constant is on the RHS.
03812   bool ShouldSwap;
03813   if (ICmpInst::isSignedPredicate(LHSCC) ||
03814       (ICmpInst::isEquality(LHSCC) && 
03815        ICmpInst::isSignedPredicate(RHSCC)))
03816     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
03817   else
03818     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
03819     
03820   if (ShouldSwap) {
03821     std::swap(LHS, RHS);
03822     std::swap(LHSCst, RHSCst);
03823     std::swap(LHSCC, RHSCC);
03824   }
03825 
03826   // At this point, we know we have have two icmp instructions
03827   // comparing a value against two constants and and'ing the result
03828   // together.  Because of the above check, we know that we only have
03829   // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know 
03830   // (from the FoldICmpLogical check above), that the two constants 
03831   // are not equal and that the larger constant is on the RHS
03832   assert(LHSCst != RHSCst && "Compares not folded above?");
03833 
03834   switch (LHSCC) {
03835   default: llvm_unreachable("Unknown integer condition code!");
03836   case ICmpInst::ICMP_EQ:
03837     switch (RHSCC) {
03838     default: llvm_unreachable("Unknown integer condition code!");
03839     case ICmpInst::ICMP_EQ:         // (X == 13 & X == 15) -> false
03840     case ICmpInst::ICMP_UGT:        // (X == 13 & X >  15) -> false
03841     case ICmpInst::ICMP_SGT:        // (X == 13 & X >  15) -> false
03842       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
03843     case ICmpInst::ICMP_NE:         // (X == 13 & X != 15) -> X == 13
03844     case ICmpInst::ICMP_ULT:        // (X == 13 & X <  15) -> X == 13
03845     case ICmpInst::ICMP_SLT:        // (X == 13 & X <  15) -> X == 13
03846       return ReplaceInstUsesWith(I, LHS);
03847     }
03848   case ICmpInst::ICMP_NE:
03849     switch (RHSCC) {
03850     default: llvm_unreachable("Unknown integer condition code!");
03851     case ICmpInst::ICMP_ULT:
03852       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
03853         return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Val, LHSCst);
03854       break;                        // (X != 13 & X u< 15) -> no change
03855     case ICmpInst::ICMP_SLT:
03856       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
03857         return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Val, LHSCst);
03858       break;                        // (X != 13 & X s< 15) -> no change
03859     case ICmpInst::ICMP_EQ:         // (X != 13 & X == 15) -> X == 15
03860     case ICmpInst::ICMP_UGT:        // (X != 13 & X u> 15) -> X u> 15
03861     case ICmpInst::ICMP_SGT:        // (X != 13 & X s> 15) -> X s> 15
03862       return ReplaceInstUsesWith(I, RHS);
03863     case ICmpInst::ICMP_NE:
03864       if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
03865         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
03866         Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST,
03867                                                      Val->getName()+".off");
03868         InsertNewInstBefore(Add, I);
03869         return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add,
03870                             ConstantInt::get(Add->getType(), 1));
03871       }
03872       break;                        // (X != 13 & X != 15) -> no change
03873     }
03874     break;
03875   case ICmpInst::ICMP_ULT:
03876     switch (RHSCC) {
03877     default: llvm_unreachable("Unknown integer condition code!");
03878     case ICmpInst::ICMP_EQ:         // (X u< 13 & X == 15) -> false
03879     case ICmpInst::ICMP_UGT:        // (X u< 13 & X u> 15) -> false
03880       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
03881     case ICmpInst::ICMP_SGT:        // (X u< 13 & X s> 15) -> no change
03882       break;
03883     case ICmpInst::ICMP_NE:         // (X u< 13 & X != 15) -> X u< 13
03884     case ICmpInst::ICMP_ULT:        // (X u< 13 & X u< 15) -> X u< 13
03885       return ReplaceInstUsesWith(I, LHS);
03886     case ICmpInst::ICMP_SLT:        // (X u< 13 & X s< 15) -> no change
03887       break;
03888     }
03889     break;
03890   case ICmpInst::ICMP_SLT:
03891     switch (RHSCC) {
03892     default: llvm_unreachable("Unknown integer condition code!");
03893     case ICmpInst::ICMP_EQ:         // (X s< 13 & X == 15) -> false
03894     case ICmpInst::ICMP_SGT:        // (X s< 13 & X s> 15) -> false
03895       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
03896     case ICmpInst::ICMP_UGT:        // (X s< 13 & X u> 15) -> no change
03897       break;
03898     case ICmpInst::ICMP_NE:         // (X s< 13 & X != 15) -> X < 13
03899     case ICmpInst::ICMP_SLT:        // (X s< 13 & X s< 15) -> X < 13
03900       return ReplaceInstUsesWith(I, LHS);
03901     case ICmpInst::ICMP_ULT:        // (X s< 13 & X u< 15) -> no change
03902       break;
03903     }
03904     break;
03905   case ICmpInst::ICMP_UGT:
03906     switch (RHSCC) {
03907     default: llvm_unreachable("Unknown integer condition code!");
03908     case ICmpInst::ICMP_EQ:         // (X u> 13 & X == 15) -> X == 15
03909     case ICmpInst::ICMP_UGT:        // (X u> 13 & X u> 15) -> X u> 15
03910       return ReplaceInstUsesWith(I, RHS);
03911     case ICmpInst::ICMP_SGT:        // (X u> 13 & X s> 15) -> no change
03912       break;
03913     case ICmpInst::ICMP_NE:
03914       if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
03915         return new ICmpInst(*Context, LHSCC, Val, RHSCst);
03916       break;                        // (X u> 13 & X != 15) -> no change
03917     case ICmpInst::ICMP_ULT:        // (X u> 13 & X u< 15) -> (X-14) <u 1
03918       return InsertRangeTest(Val, AddOne(LHSCst),
03919                              RHSCst, false, true, I);
03920     case ICmpInst::ICMP_SLT:        // (X u> 13 & X s< 15) -> no change
03921       break;
03922     }
03923     break;
03924   case ICmpInst::ICMP_SGT:
03925     switch (RHSCC) {
03926     default: llvm_unreachable("Unknown integer condition code!");
03927     case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X == 15
03928     case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15
03929       return ReplaceInstUsesWith(I, RHS);
03930     case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change
03931       break;
03932     case ICmpInst::ICMP_NE:
03933       if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
03934         return new ICmpInst(*Context, LHSCC, Val, RHSCst);
03935       break;                        // (X s> 13 & X != 15) -> no change
03936     case ICmpInst::ICMP_SLT:        // (X s> 13 & X s< 15) -> (X-14) s< 1
03937       return InsertRangeTest(Val, AddOne(LHSCst),
03938                              RHSCst, true, true, I);
03939     case ICmpInst::ICMP_ULT:        // (X s> 13 & X u< 15) -> no change
03940       break;
03941     }
03942     break;
03943   }
03944  
03945   return 0;
03946 }
03947 
03948 Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS,
03949                                           FCmpInst *RHS) {
03950   
03951   if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
03952       RHS->getPredicate() == FCmpInst::FCMP_ORD) {
03953     // (fcmp ord x, c) & (fcmp ord y, c)  -> (fcmp ord x, y)
03954     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
03955       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
03956         // If either of the constants are nans, then the whole thing returns
03957         // false.
03958         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
03959           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
03960         return new FCmpInst(*Context, FCmpInst::FCMP_ORD, 
03961                             LHS->getOperand(0), RHS->getOperand(0));
03962       }
03963     
03964     // Handle vector zeros.  This occurs because the canonical form of
03965     // "fcmp ord x,x" is "fcmp ord x, 0".
03966     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
03967         isa<ConstantAggregateZero>(RHS->getOperand(1)))
03968       return new FCmpInst(*Context, FCmpInst::FCMP_ORD, 
03969                           LHS->getOperand(0), RHS->getOperand(0));
03970     return 0;
03971   }
03972   
03973   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
03974   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
03975   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
03976   
03977   
03978   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
03979     // Swap RHS operands to match LHS.
03980     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
03981     std::swap(Op1LHS, Op1RHS);
03982   }
03983   
03984   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
03985     // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
03986     if (Op0CC == Op1CC)
03987       return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
03988     
03989     if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
03990       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
03991     if (Op0CC == FCmpInst::FCMP_TRUE)
03992       return ReplaceInstUsesWith(I, RHS);
03993     if (Op1CC == FCmpInst::FCMP_TRUE)
03994       return ReplaceInstUsesWith(I, LHS);
03995     
03996     bool Op0Ordered;
03997     bool Op1Ordered;
03998     unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
03999     unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
04000     if (Op1Pred == 0) {
04001       std::swap(LHS, RHS);
04002       std::swap(Op0Pred, Op1Pred);
04003       std::swap(Op0Ordered, Op1Ordered);
04004     }
04005     if (Op0Pred == 0) {
04006       // uno && ueq -> uno && (uno || eq) -> ueq
04007       // ord && olt -> ord && (ord && lt) -> olt
04008       if (Op0Ordered == Op1Ordered)
04009         return ReplaceInstUsesWith(I, RHS);
04010       
04011       // uno && oeq -> uno && (ord && eq) -> false
04012       // uno && ord -> false
04013       if (!Op0Ordered)
04014         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
04015       // ord && ueq -> ord && (uno || eq) -> oeq
04016       return cast<Instruction>(getFCmpValue(true, Op1Pred,
04017                                             Op0LHS, Op0RHS, Context));
04018     }
04019   }
04020 
04021   return 0;
04022 }
04023 
04024 
04025 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
04026   bool Changed = SimplifyCommutative(I);
04027   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
04028 
04029   if (isa<UndefValue>(Op1))                         // X & undef -> 0
04030     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
04031 
04032   // and X, X = X
04033   if (Op0 == Op1)
04034     return ReplaceInstUsesWith(I, Op1);
04035 
04036   // See if we can simplify any instructions used by the instruction whose sole 
04037   // purpose is to compute bits we don't care about.
04038   if (SimplifyDemandedInstructionBits(I))
04039     return &I;
04040   if (isa<VectorType>(I.getType())) {
04041     if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
04042       if (CP->isAllOnesValue())            // X & <-1,-1> -> X
04043         return ReplaceInstUsesWith(I, I.getOperand(0));
04044     } else if (isa<ConstantAggregateZero>(Op1)) {
04045       return ReplaceInstUsesWith(I, Op1);  // X & <0,0> -> <0,0>
04046     }
04047   }
04048 
04049   if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
04050     const APInt& AndRHSMask = AndRHS->getValue();
04051     APInt NotAndRHS(~AndRHSMask);
04052 
04053     // Optimize a variety of ((val OP C1) & C2) combinations...
04054     if (isa<BinaryOperator>(Op0)) {
04055       Instruction *Op0I = cast<Instruction>(Op0);
04056       Value *Op0LHS = Op0I->getOperand(0);
04057       Value *Op0RHS = Op0I->getOperand(1);
04058       switch (Op0I->getOpcode()) {
04059       case Instruction::Xor:
04060       case Instruction::Or:
04061         // If the mask is only needed on one incoming arm, push it up.
04062         if (Op0I->hasOneUse()) {
04063           if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
04064             // Not masking anything out for the LHS, move to RHS.
04065             Instruction *NewRHS = BinaryOperator::CreateAnd(Op0RHS, AndRHS,
04066                                                    Op0RHS->getName()+".masked");
04067             InsertNewInstBefore(NewRHS, I);
04068             return BinaryOperator::Create(
04069                        cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS);
04070           }
04071           if (!isa<Constant>(Op0RHS) &&
04072               MaskedValueIsZero(Op0RHS, NotAndRHS)) {
04073             // Not masking anything out for the RHS, move to LHS.
04074             Instruction *NewLHS = BinaryOperator::CreateAnd(Op0LHS, AndRHS,
04075                                                    Op0LHS->getName()+".masked");
04076             InsertNewInstBefore(NewLHS, I);
04077             return BinaryOperator::Create(
04078                        cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS);
04079           }
04080         }
04081 
04082         break;
04083       case Instruction::Add:
04084         // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
04085         // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
04086         // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
04087         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
04088           return BinaryOperator::CreateAnd(V, AndRHS);
04089         if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
04090           return BinaryOperator::CreateAnd(V, AndRHS);  // Add commutes
04091         break;
04092 
04093       case Instruction::Sub:
04094         // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
04095         // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
04096         // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
04097         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
04098           return BinaryOperator::CreateAnd(V, AndRHS);
04099 
04100         // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
04101         // has 1's for all bits that the subtraction with A might affect.
04102         if (Op0I->hasOneUse()) {
04103           uint32_t BitWidth = AndRHSMask.getBitWidth();
04104           uint32_t Zeros = AndRHSMask.countLeadingZeros();
04105           APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
04106 
04107           ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
04108           if (!(A && A->isZero()) &&               // avoid infinite recursion.
04109               MaskedValueIsZero(Op0LHS, Mask)) {
04110             Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS);
04111             InsertNewInstBefore(NewNeg, I);
04112             return BinaryOperator::CreateAnd(NewNeg, AndRHS);
04113           }
04114         }
04115         break;
04116 
04117       case Instruction::Shl:
04118       case Instruction::LShr:
04119         // (1 << x) & 1 --> zext(x == 0)
04120         // (1 >> x) & 1 --> zext(x == 0)
04121         if (AndRHSMask == 1 && Op0LHS == AndRHS) {
04122           Instruction *NewICmp = new ICmpInst(*Context, ICmpInst::ICMP_EQ,
04123                                     Op0RHS, Constant::getNullValue(I.getType()));
04124           InsertNewInstBefore(NewICmp, I);
04125           return new ZExtInst(NewICmp, I.getType());
04126         }
04127         break;
04128       }
04129 
04130       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
04131         if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
04132           return Res;
04133     } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
04134       // If this is an integer truncation or change from signed-to-unsigned, and
04135       // if the source is an and/or with immediate, transform it.  This
04136       // frequently occurs for bitfield accesses.
04137       if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
04138         if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) &&
04139             CastOp->getNumOperands() == 2)
04140           if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) {
04141             if (CastOp->getOpcode() == Instruction::And) {
04142               // Change: and (cast (and X, C1) to T), C2
04143               // into  : and (cast X to T), trunc_or_bitcast(C1)&C2
04144               // This will fold the two constants together, which may allow 
04145               // other simplifications.
04146               Instruction *NewCast = CastInst::CreateTruncOrBitCast(
04147                 CastOp->getOperand(0), I.getType(), 
04148                 CastOp->getName()+".shrunk");
04149               NewCast = InsertNewInstBefore(NewCast, I);
04150               // trunc_or_bitcast(C1)&C2
04151               Constant *C3 =
04152                       ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
04153               C3 = ConstantExpr::getAnd(C3, AndRHS);
04154               return BinaryOperator::CreateAnd(NewCast, C3);
04155             } else if (CastOp->getOpcode() == Instruction::Or) {
04156               // Change: and (cast (or X, C1) to T), C2
04157               // into  : trunc(C1)&C2 iff trunc(C1)&C2 == C2
04158               Constant *C3 =
04159                       ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
04160               if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS)
04161                 // trunc(C1)&C2
04162                 return ReplaceInstUsesWith(I, AndRHS);
04163             }
04164           }
04165       }
04166     }
04167 
04168     // Try to fold constant and into select arguments.
04169     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
04170       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
04171         return R;
04172     if (isa<PHINode>(Op0))
04173       if (Instruction *NV = FoldOpIntoPhi(I))
04174         return NV;
04175   }
04176 
04177   Value *Op0NotVal = dyn_castNotVal(Op0);
04178   Value *Op1NotVal = dyn_castNotVal(Op1);
04179 
04180   if (Op0NotVal == Op1 || Op1NotVal == Op0)  // A & ~A  == ~A & A == 0
04181     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
04182 
04183   // (~A & ~B) == (~(A | B)) - De Morgan's Law
04184   if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
04185     Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal,
04186                                                I.getName()+".demorgan");
04187     InsertNewInstBefore(Or, I);
04188     return BinaryOperator::CreateNot(Or);
04189   }
04190   
04191   {
04192     Value *A = 0, *B = 0, *C = 0, *D = 0;
04193     if (match(Op0, m_Or(m_Value(A), m_Value(B)))) {
04194       if (A == Op1 || B == Op1)    // (A | ?) & A  --> A
04195         return ReplaceInstUsesWith(I, Op1);
04196     
04197       // (A|B) & ~(A&B) -> A^B
04198       if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) {
04199         if ((A == C && B == D) || (A == D && B == C))
04200           return BinaryOperator::CreateXor(A, B);
04201       }
04202     }
04203     
04204     if (match(Op1, m_Or(m_Value(A), m_Value(B)))) {
04205       if (A == Op0 || B == Op0)    // A & (A | ?)  --> A
04206         return ReplaceInstUsesWith(I, Op0);
04207 
04208       // ~(A&B) & (A|B) -> A^B
04209       if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) {
04210         if ((A == C && B == D) || (A == D && B == C))
04211           return BinaryOperator::CreateXor(A, B);
04212       }
04213     }
04214     
04215     if (Op0->hasOneUse() &&
04216         match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
04217       if (A == Op1) {                                // (A^B)&A -> A&(A^B)
04218         I.swapOperands();     // Simplify below
04219         std::swap(Op0, Op1);
04220       } else if (B == Op1) {                         // (A^B)&B -> B&(B^A)
04221         cast<BinaryOperator>(Op0)->swapOperands();
04222         I.swapOperands();     // Simplify below
04223         std::swap(Op0, Op1);
04224       }
04225     }
04226 
04227     if (Op1->hasOneUse() &&
04228         match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
04229       if (B == Op0) {                                // B&(A^B) -> B&(B^A)
04230         cast<BinaryOperator>(Op1)->swapOperands();
04231         std::swap(A, B);
04232       }
04233       if (A == Op0) {                                // A&(A^B) -> A & ~B
04234         Instruction *NotB = BinaryOperator::CreateNot(B, "tmp");
04235         InsertNewInstBefore(NotB, I);
04236         return BinaryOperator::CreateAnd(A, NotB);
04237       }
04238     }
04239 
04240     // (A&((~A)|B)) -> A&B
04241     if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
04242         match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
04243       return BinaryOperator::CreateAnd(A, Op1);
04244     if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
04245         match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
04246       return BinaryOperator::CreateAnd(A, Op0);
04247   }
04248   
04249   if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
04250     // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
04251     if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
04252       return R;
04253 
04254     if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
04255       if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS))
04256         return Res;
04257   }
04258 
04259   // fold (and (cast A), (cast B)) -> (cast (and A, B))
04260   if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
04261     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
04262       if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ?
04263         const Type *SrcTy = Op0C->getOperand(0)->getType();
04264         if (SrcTy == Op1C->getOperand(0)->getType() &&
04265             SrcTy->isIntOrIntVector() &&
04266             // Only do this if the casts both really cause code to be generated.
04267             ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), 
04268                               I.getType(), TD) &&
04269             ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), 
04270                               I.getType(), TD)) {
04271           Instruction *NewOp = BinaryOperator::CreateAnd(Op0C->getOperand(0),
04272                                                          Op1C->getOperand(0),
04273                                                          I.getName());
04274           InsertNewInstBefore(NewOp, I);
04275           return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
04276         }
04277       }
04278     
04279   // (X >> Z) & (Y >> Z)  -> (X&Y) >> Z  for all shifts.
04280   if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
04281     if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
04282       if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && 
04283           SI0->getOperand(1) == SI1->getOperand(1) &&
04284           (SI0->hasOneUse() || SI1->hasOneUse())) {
04285         Instruction *NewOp =
04286           InsertNewInstBefore(BinaryOperator::CreateAnd(SI0->getOperand(0),
04287                                                         SI1->getOperand(0),
04288                                                         SI0->getName()), I);
04289         return BinaryOperator::Create(SI1->getOpcode(), NewOp, 
04290                                       SI1->getOperand(1));
04291       }
04292   }
04293 
04294   // If and'ing two fcmp, try combine them into one.
04295   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
04296     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
04297       if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS))
04298         return Res;
04299   }
04300 
04301   return Changed ? &I : 0;
04302 }
04303 
04304 /// CollectBSwapParts - Analyze the specified subexpression and see if it is
04305 /// capable of providing pieces of a bswap.  The subexpression provides pieces
04306 /// of a bswap if it is proven that each of the non-zero bytes in the output of
04307 /// the expression came from the corresponding "byte swapped" byte in some other
04308 /// value.  For example, if the current subexpression is "(shl i32 %X, 24)" then
04309 /// we know that the expression deposits the low byte of %X into the high byte
04310 /// of the bswap result and that all other bytes are zero.  This expression is
04311 /// accepted, the high byte of ByteValues is set to X to indicate a correct
04312 /// match.
04313 ///
04314 /// This function returns true if the match was unsuccessful and false if so.
04315 /// On entry to the function the "OverallLeftShift" is a signed integer value
04316 /// indicating the number of bytes that the subexpression is later shifted.  For
04317 /// example, if the expression is later right shifted by 16 bits, the
04318 /// OverallLeftShift value would be -2 on entry.  This is used to specify which
04319 /// byte of ByteValues is actually being set.
04320 ///
04321 /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
04322 /// byte is masked to zero by a user.  For example, in (X & 255), X will be
04323 /// processed with a bytemask of 1.  Because bytemask is 32-bits, this limits
04324 /// this function to working on up to 32-byte (256 bit) values.  ByteMask is
04325 /// always in the local (OverallLeftShift) coordinate space.
04326 ///
04327 static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
04328                               SmallVector<Value*, 8> &ByteValues) {
04329   if (Instruction *I = dyn_cast<Instruction>(V)) {
04330     // If this is an or instruction, it may be an inner node of the bswap.
04331     if (I->getOpcode() == Instruction::Or) {
04332       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
04333                                ByteValues) ||
04334              CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
04335                                ByteValues);
04336     }
04337   
04338     // If this is a logical shift by a constant multiple of 8, recurse with
04339     // OverallLeftShift and ByteMask adjusted.
04340     if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
04341       unsigned ShAmt = 
04342         cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
04343       // Ensure the shift amount is defined and of a byte value.
04344       if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
04345         return true;
04346 
04347       unsigned ByteShift = ShAmt >> 3;
04348       if (I->getOpcode() == Instruction::Shl) {
04349         // X << 2 -> collect(X, +2)
04350         OverallLeftShift += ByteShift;
04351         ByteMask >>= ByteShift;
04352       } else {
04353         // X >>u 2 -> collect(X, -2)
04354         OverallLeftShift -= ByteShift;
04355         ByteMask <<= ByteShift;
04356         ByteMask &= (~0U >> (32-ByteValues.size()));
04357       }
04358 
04359       if (OverallLeftShift >= (int)ByteValues.size()) return true;
04360       if (OverallLeftShift <= -(int)ByteValues.size()) return true;
04361 
04362       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, 
04363                                ByteValues);
04364     }
04365 
04366     // If this is a logical 'and' with a mask that clears bytes, clear the
04367     // corresponding bytes in ByteMask.
04368     if (I->getOpcode() == Instruction::And &&
04369         isa<ConstantInt>(I->getOperand(1))) {
04370       // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
04371       unsigned NumBytes = ByteValues.size();
04372       APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
04373       const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
04374       
04375       for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
04376         // If this byte is masked out by a later operation, we don't care what
04377         // the and mask is.
04378         if ((ByteMask & (1 << i)) == 0)
04379           continue;
04380         
04381         // If the AndMask is all zeros for this byte, clear the bit.
04382         APInt MaskB = AndMask & Byte;
04383         if (MaskB == 0) {
04384           ByteMask &= ~(1U << i);
04385           continue;
04386         }
04387         
04388         // If the AndMask is not all ones for this byte, it's not a bytezap.
04389         if (MaskB != Byte)
04390           return true;
04391 
04392         // Otherwise, this byte is kept.
04393       }
04394 
04395       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, 
04396                                ByteValues);
04397     }
04398   }
04399   
04400   // Okay, we got to something that isn't a shift, 'or' or 'and'.  This must be
04401   // the input value to the bswap.  Some observations: 1) if more than one byte
04402   // is demanded from this input, then it could not be successfully assembled
04403   // into a byteswap.  At least one of the two bytes would not be aligned with
04404   // their ultimate destination.
04405   if (!isPowerOf2_32(ByteMask)) return true;
04406   unsigned InputByteNo = CountTrailingZeros_32(ByteMask);
04407   
04408   // 2) The input and ultimate destinations must line up: if byte 3 of an i32
04409   // is demanded, it needs to go into byte 0 of the result.  This means that the
04410   // byte needs to be shifted until it lands in the right byte bucket.  The
04411   // shift amount depends on the position: if the byte is coming from the high
04412   // part of the value (e.g. byte 3) then it must be shifted right.  If from the
04413   // low part, it must be shifted left.
04414   unsigned DestByteNo = InputByteNo + OverallLeftShift;
04415   if (InputByteNo < ByteValues.size()/2) {
04416     if (ByteValues.size()-1-DestByteNo != InputByteNo)
04417       return true;
04418   } else {
04419     if (ByteValues.size()-1-DestByteNo != InputByteNo)
04420       return true;
04421   }
04422   
04423   // If the destination byte value is already defined, the values are or'd
04424   // together, which isn't a bswap (unless it's an or of the same bits).
04425   if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
04426     return true;
04427   ByteValues[DestByteNo] = V;
04428   return false;
04429 }
04430 
04431 /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
04432 /// If so, insert the new bswap intrinsic and return it.
04433 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
04434   const IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
04435   if (!ITy || ITy->getBitWidth() % 16 || 
04436       // ByteMask only allows up to 32-byte values.
04437       ITy->getBitWidth() > 32*8) 
04438     return 0;   // Can only bswap pairs of bytes.  Can't do vectors.
04439   
04440   /// ByteValues - For each byte of the result, we keep track of which value
04441   /// defines each byte.
04442   SmallVector<Value*, 8> ByteValues;
04443   ByteValues.resize(ITy->getBitWidth()/8);
04444     
04445   // Try to find all the pieces corresponding to the bswap.
04446   uint32_t ByteMask = ~0U >> (32-ByteValues.size());
04447   if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
04448     return 0;
04449   
04450   // Check to see if all of the bytes come from the same value.
04451   Value *V = ByteValues[0];
04452   if (V == 0) return 0;  // Didn't find a byte?  Must be zero.
04453   
04454   // Check to make sure that all of the bytes come from the same value.
04455   for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
04456     if (ByteValues[i] != V)
04457       return 0;
04458   const Type *Tys[] = { ITy };
04459   Module *M = I.getParent()->getParent()->getParent();
04460   Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
04461   return CallInst::Create(F, V);
04462 }
04463 
04464 /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D).  Check
04465 /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
04466 /// we can simplify this expression to "cond ? C : D or B".
04467 static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
04468                                          Value *C, Value *D,
04469                                          LLVMContext *Context) {
04470   // If A is not a select of -1/0, this cannot match.
04471   Value *Cond = 0;
04472   if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond))))
04473     return 0;
04474 
04475   // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
04476   if (match(D, m_SelectCst<0, -1>(m_Specific(Cond))))
04477     return SelectInst::Create(Cond, C, B);
04478   if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
04479     return SelectInst::Create(Cond, C, B);
04480   // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
04481   if (match(B, m_SelectCst<0, -1>(m_Specific(Cond))))
04482     return SelectInst::Create(Cond, C, D);
04483   if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
04484     return SelectInst::Create(Cond, C, D);
04485   return 0;
04486 }
04487 
04488 /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
04489 Instruction *InstCombiner::FoldOrOfICmps(Instruction &I,
04490                                          ICmpInst *LHS, ICmpInst *RHS) {
04491   Value *Val, *Val2;
04492   ConstantInt *LHSCst, *RHSCst;
04493   ICmpInst::Predicate LHSCC, RHSCC;
04494   
04495   // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
04496   if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
04497              m_ConstantInt(LHSCst))) ||
04498       !match(RHS, m_ICmp(RHSCC, m_Value(Val2),
04499              m_ConstantInt(RHSCst))))
04500     return 0;
04501   
04502   // From here on, we only handle:
04503   //    (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
04504   if (Val != Val2) return 0;
04505   
04506   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
04507   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
04508       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
04509       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
04510       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
04511     return 0;
04512   
04513   // We can't fold (ugt x, C) | (sgt x, C2).
04514   if (!PredicatesFoldable(LHSCC, RHSCC))
04515     return 0;
04516   
04517   // Ensure that the larger constant is on the RHS.
04518   bool ShouldSwap;
04519   if (ICmpInst::isSignedPredicate(LHSCC) ||
04520       (ICmpInst::isEquality(LHSCC) && 
04521        ICmpInst::isSignedPredicate(RHSCC)))
04522     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
04523   else
04524     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
04525   
04526   if (ShouldSwap) {
04527     std::swap(LHS, RHS);
04528     std::swap(LHSCst, RHSCst);
04529     std::swap(LHSCC, RHSCC);
04530   }
04531   
04532   // At this point, we know we have have two icmp instructions
04533   // comparing a value against two constants and or'ing the result
04534   // together.  Because of the above check, we know that we only have
04535   // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
04536   // FoldICmpLogical check above), that the two constants are not
04537   // equal.
04538   assert(LHSCst != RHSCst && "Compares not folded above?");
04539 
04540   switch (LHSCC) {
04541   default: llvm_unreachable("Unknown integer condition code!");
04542   case ICmpInst::ICMP_EQ:
04543     switch (RHSCC) {
04544     default: llvm_unreachable("Unknown integer condition code!");
04545     case ICmpInst::ICMP_EQ:
04546       if (LHSCst == SubOne(RHSCst)) {
04547         // (X == 13 | X == 14) -> X-13 <u 2
04548         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
04549         Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST,
04550                                                      Val->getName()+".off");
04551         InsertNewInstBefore(Add, I);
04552         AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
04553         return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, AddCST);
04554       }
04555       break;                         // (X == 13 | X == 15) -> no change
04556     case ICmpInst::ICMP_UGT:         // (X == 13 | X u> 14) -> no change
04557     case ICmpInst::ICMP_SGT:         // (X == 13 | X s> 14) -> no change
04558       break;
04559     case ICmpInst::ICMP_NE:          // (X == 13 | X != 15) -> X != 15
04560     case ICmpInst::ICMP_ULT:         // (X == 13 | X u< 15) -> X u< 15
04561     case ICmpInst::ICMP_SLT:         // (X == 13 | X s< 15) -> X s< 15
04562       return ReplaceInstUsesWith(I, RHS);
04563     }
04564     break;
04565   case ICmpInst::ICMP_NE:
04566     switch (RHSCC) {
04567     default: llvm_unreachable("Unknown integer condition code!");
04568     case ICmpInst::ICMP_EQ:          // (X != 13 | X == 15) -> X != 13
04569     case ICmpInst::ICMP_UGT:         // (X != 13 | X u> 15) -> X != 13
04570     case ICmpInst::ICMP_SGT:         // (X != 13 | X s> 15) -> X != 13
04571       return ReplaceInstUsesWith(I, LHS);
04572     case ICmpInst::ICMP_NE:          // (X != 13 | X != 15) -> true
04573     case ICmpInst::ICMP_ULT:         // (X != 13 | X u< 15) -> true
04574     case ICmpInst::ICMP_SLT:         // (X != 13 | X s< 15) -> true
04575       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
04576     }
04577     break;
04578   case ICmpInst::ICMP_ULT:
04579     switch (RHSCC) {
04580     default: llvm_unreachable("Unknown integer condition code!");
04581     case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change
04582       break;
04583     case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) -> (X-13) u> 2
04584       // If RHSCst is [us]MAXINT, it is always false.  Not handling
04585       // this can cause overflow.
04586       if (RHSCst->isMaxValue(false))
04587         return ReplaceInstUsesWith(I, LHS);
04588       return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
04589                              false, false, I);
04590     case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change
04591       break;
04592     case ICmpInst::ICMP_NE:         // (X u< 13 | X != 15) -> X != 15
04593     case ICmpInst::ICMP_ULT:        // (X u< 13 | X u< 15) -> X u< 15
04594       return ReplaceInstUsesWith(I, RHS);
04595     case ICmpInst::ICMP_SLT:        // (X u< 13 | X s< 15) -> no change
04596       break;
04597     }
04598     break;
04599   case ICmpInst::ICMP_SLT:
04600     switch (RHSCC) {
04601     default: llvm_unreachable("Unknown integer condition code!");
04602     case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change
04603       break;
04604     case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) -> (X-13) s> 2
04605       // If RHSCst is [us]MAXINT, it is always false.  Not handling
04606       // this can cause overflow.
04607       if (RHSCst->isMaxValue(true))
04608         return ReplaceInstUsesWith(I, LHS);
04609       return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
04610                              true, false, I);
04611     case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change
04612       break;
04613     case ICmpInst::ICMP_NE:         // (X s< 13 | X != 15) -> X != 15
04614     case ICmpInst::ICMP_SLT:        // (X s< 13 | X s< 15) -> X s< 15
04615       return ReplaceInstUsesWith(I, RHS);
04616     case ICmpInst::ICMP_ULT:        // (X s< 13 | X u< 15) -> no change
04617       break;
04618     }
04619     break;
04620   case ICmpInst::ICMP_UGT:
04621     switch (RHSCC) {
04622     default: llvm_unreachable("Unknown integer condition code!");
04623     case ICmpInst::ICMP_EQ:         // (X u> 13 | X == 15) -> X u> 13
04624     case ICmpInst::ICMP_UGT:        // (X u> 13 | X u> 15) -> X u> 13
04625       return ReplaceInstUsesWith(I, LHS);
04626     case ICmpInst::ICMP_SGT:        // (X u> 13 | X s> 15) -> no change
04627       break;
04628     case ICmpInst::ICMP_NE:         // (X u> 13 | X != 15) -> true
04629     case ICmpInst::ICMP_ULT:        // (X u> 13 | X u< 15) -> true
04630       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
04631     case ICmpInst::ICMP_SLT:        // (X u> 13 | X s< 15) -> no change
04632       break;
04633     }
04634     break;
04635   case ICmpInst::ICMP_SGT:
04636     switch (RHSCC) {
04637     default: llvm_unreachable("Unknown integer condition code!");
04638     case ICmpInst::ICMP_EQ:         // (X s> 13 | X == 15) -> X > 13
04639     case ICmpInst::ICMP_SGT:        // (X s> 13 | X s> 15) -> X > 13
04640       return ReplaceInstUsesWith(I, LHS);
04641     case ICmpInst::ICMP_UGT:        // (X s> 13 | X u> 15) -> no change
04642       break;
04643     case ICmpInst::ICMP_NE:         // (X s> 13 | X != 15) -> true
04644     case ICmpInst::ICMP_SLT:        // (X s> 13 | X s< 15) -> true
04645       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
04646     case ICmpInst::ICMP_ULT:        // (X s> 13 | X u< 15) -> no change
04647       break;
04648     }
04649     break;
04650   }
04651   return 0;
04652 }
04653 
04654 Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS,
04655                                          FCmpInst *RHS) {
04656   if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
04657       RHS->getPredicate() == FCmpInst::FCMP_UNO && 
04658       LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
04659     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
04660       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
04661         // If either of the constants are nans, then the whole thing returns
04662         // true.
04663         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
04664           return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
04665         
04666         // Otherwise, no need to compare the two constants, compare the
04667         // rest.
04668         return new FCmpInst(*Context, FCmpInst::FCMP_UNO, 
04669                             LHS->getOperand(0), RHS->getOperand(0));
04670       }
04671     
04672     // Handle vector zeros.  This occurs because the canonical form of
04673     // "fcmp uno x,x" is "fcmp uno x, 0".
04674     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
04675         isa<ConstantAggregateZero>(RHS->getOperand(1)))
04676       return new FCmpInst(*Context, FCmpInst::FCMP_UNO, 
04677                           LHS->getOperand(0), RHS->getOperand(0));
04678     
04679     return 0;
04680   }
04681   
04682   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
04683   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
04684   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
04685   
04686   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
04687     // Swap RHS operands to match LHS.
04688     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
04689     std::swap(Op1LHS, Op1RHS);
04690   }
04691   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
04692     // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
04693     if (Op0CC == Op1CC)
04694       return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC,
04695                           Op0LHS, Op0RHS);
04696     if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
04697       return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
04698     if (Op0CC == FCmpInst::FCMP_FALSE)
04699       return ReplaceInstUsesWith(I, RHS);
04700     if (Op1CC == FCmpInst::FCMP_FALSE)
04701       return ReplaceInstUsesWith(I, LHS);
04702     bool Op0Ordered;
04703     bool Op1Ordered;
04704     unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
04705     unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
04706     if (Op0Ordered == Op1Ordered) {
04707       // If both are ordered or unordered, return a new fcmp with
04708       // or'ed predicates.
04709       Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred,
04710                                Op0LHS, Op0RHS, Context);
04711       if (Instruction *I = dyn_cast<Instruction>(RV))
04712         return I;
04713       // Otherwise, it's a constant boolean value...
04714       return ReplaceInstUsesWith(I, RV);
04715     }
04716   }
04717   return 0;
04718 }
04719 
04720 /// FoldOrWithConstants - This helper function folds:
04721 ///
04722 ///     ((A | B) & C1) | (B & C2)
04723 ///
04724 /// into:
04725 /// 
04726 ///     (A & C1) | B
04727 ///
04728 /// when the XOR of the two constants is "all ones" (-1).
04729 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
04730                                                Value *A, Value *B, Value *C) {
04731   ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
04732   if (!CI1) return 0;
04733 
04734   Value *V1 = 0;
04735   ConstantInt *CI2 = 0;
04736   if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
04737 
04738   APInt Xor = CI1->getValue() ^ CI2->getValue();
04739   if (!Xor.isAllOnesValue()) return 0;
04740 
04741   if (V1 == A || V1 == B) {
04742     Instruction *NewOp =
04743       InsertNewInstBefore(BinaryOperator::CreateAnd((V1 == A) ? B : A, CI1), I);
04744     return BinaryOperator::CreateOr(NewOp, V1);
04745   }
04746 
04747   return 0;
04748 }
04749 
04750 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
04751   bool Changed = SimplifyCommutative(I);
04752   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
04753 
04754   if (isa<UndefValue>(Op1))                       // X | undef -> -1
04755     return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
04756 
04757   // or X, X = X
04758   if (Op0 == Op1)
04759     return ReplaceInstUsesWith(I, Op0);
04760 
04761   // See if we can simplify any instructions used by the instruction whose sole 
04762   // purpose is to compute bits we don't care about.
04763   if (SimplifyDemandedInstructionBits(I))
04764     return &I;
04765   if (isa<VectorType>(I.getType())) {
04766     if (isa<ConstantAggregateZero>(Op1)) {
04767       return ReplaceInstUsesWith(I, Op0);  // X | <0,0> -> X
04768     } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
04769       if (CP->isAllOnesValue())            // X | <-1,-1> -> <-1,-1>
04770         return ReplaceInstUsesWith(I, I.getOperand(1));
04771     }
04772   }
04773 
04774   // or X, -1 == -1
04775   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
04776     ConstantInt *C1 = 0; Value *X = 0;
04777     // (X & C1) | C2 --> (X | C2) & (C1|C2)
04778     if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
04779         isOnlyUse(Op0)) {
04780       Instruction *Or = BinaryOperator::CreateOr(X, RHS);
04781       InsertNewInstBefore(Or, I);
04782       Or->takeName(Op0);
04783       return BinaryOperator::CreateAnd(Or, 
04784                ConstantInt::get(*Context, RHS->getValue() | C1->getValue()));
04785     }
04786 
04787     // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
04788     if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
04789         isOnlyUse(Op0)) {
04790       Instruction *Or = BinaryOperator::CreateOr(X, RHS);
04791       InsertNewInstBefore(Or, I);
04792       Or->takeName(Op0);
04793       return BinaryOperator::CreateXor(Or,
04794                  ConstantInt::get(*Context, C1->getValue() & ~RHS->getValue()));
04795     }
04796 
04797     // Try to fold constant and into select arguments.
04798     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
04799       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
04800         return R;
04801     if (isa<PHINode>(Op0))
04802       if (Instruction *NV = FoldOpIntoPhi(I))
04803         return NV;
04804   }
04805 
04806   Value *A = 0, *B = 0;
04807   ConstantInt *C1 = 0, *C2 = 0;
04808 
04809   if (match(Op0, m_And(m_Value(A), m_Value(B))))
04810     if (A == Op1 || B == Op1)    // (A & ?) | A  --> A
04811       return ReplaceInstUsesWith(I, Op1);
04812   if (match(Op1, m_And(m_Value(A), m_Value(B))))
04813     if (A == Op0 || B == Op0)    // A | (A & ?)  --> A
04814       return ReplaceInstUsesWith(I, Op0);
04815 
04816   // (A | B) | C  and  A | (B | C)                  -> bswap if possible.
04817   // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible.
04818   if (match(Op0, m_Or(m_Value(), m_Value())) ||
04819       match(Op1, m_Or(m_Value(), m_Value())) ||
04820       (match(Op0, m_Shift(m_Value(), m_Value())) &&
04821        match(Op1, m_Shift(m_Value(), m_Value())))) {
04822     if (Instruction *BSwap = MatchBSwap(I))
04823       return BSwap;
04824   }
04825   
04826   // (X^C)|Y -> (X|Y)^C iff Y&C == 0
04827   if (Op0->hasOneUse() &&
04828       match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
04829       MaskedValueIsZero(Op1, C1->getValue())) {
04830     Instruction *NOr = BinaryOperator::CreateOr(A, Op1);
04831     InsertNewInstBefore(NOr, I);
04832     NOr->takeName(Op0);
04833     return BinaryOperator::CreateXor(NOr, C1);
04834   }
04835 
04836   // Y|(X^C) -> (X|Y)^C iff Y&C == 0
04837   if (Op1->hasOneUse() &&
04838       match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
04839       MaskedValueIsZero(Op0, C1->getValue())) {
04840     Instruction *NOr = BinaryOperator::CreateOr(A, Op0);
04841     InsertNewInstBefore(NOr, I);
04842     NOr->takeName(Op0);
04843     return BinaryOperator::CreateXor(NOr, C1);
04844   }
04845 
04846   // (A & C)|(B & D)
04847   Value *C = 0, *D = 0;
04848   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
04849       match(Op1, m_And(m_Value(B), m_Value(D)))) {
04850     Value *V1 = 0, *V2 = 0, *V3 = 0;
04851     C1 = dyn_cast<ConstantInt>(C);
04852     C2 = dyn_cast<ConstantInt>(D);
04853     if (C1 && C2) {  // (A & C1)|(B & C2)
04854       // If we have: ((V + N) & C1) | (V & C2)
04855       // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
04856       // replace with V+N.
04857       if (C1->getValue() == ~C2->getValue()) {
04858         if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
04859             match(A, m_Add(m_Value(V1), m_Value(V2)))) {
04860           // Add commutes, try both ways.
04861           if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
04862             return ReplaceInstUsesWith(I, A);
04863           if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
04864             return ReplaceInstUsesWith(I, A);
04865         }
04866         // Or commutes, try both ways.
04867         if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
04868             match(B, m_Add(m_Value(V1), m_Value(V2)))) {
04869           // Add commutes, try both ways.
04870           if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
04871             return ReplaceInstUsesWith(I, B);
04872           if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
04873             return ReplaceInstUsesWith(I, B);
04874         }
04875       }
04876       V1 = 0; V2 = 0; V3 = 0;
04877     }
04878     
04879     // Check to see if we have any common things being and'ed.  If so, find the
04880     // terms for V1 & (V2|V3).
04881     if (isOnlyUse(Op0) || isOnlyUse(Op1)) {
04882       if (A == B)      // (A & C)|(A & D) == A & (C|D)
04883         V1 = A, V2 = C, V3 = D;
04884       else if (A == D) // (A & C)|(B & A) == A & (B|C)
04885         V1 = A, V2 = B, V3 = C;
04886       else if (C == B) // (A & C)|(C & D) == C & (A|D)
04887         V1 = C, V2 = A, V3 = D;
04888       else if (C == D) // (A & C)|(B & C) == C & (A|B)
04889         V1 = C, V2 = A, V3 = B;
04890       
04891       if (V1) {
04892         Value *Or =
04893           InsertNewInstBefore(BinaryOperator::CreateOr(V2, V3, "tmp"), I);
04894         return BinaryOperator::CreateAnd(V1, Or);
04895       }
04896     }
04897 
04898     // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) ->  C0 ? A : B, and commuted variants
04899     if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D, Context))
04900       return Match;
04901     if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C, Context))
04902       return Match;
04903     if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D, Context))
04904       return Match;
04905     if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C, Context))
04906       return Match;
04907 
04908     // ((A&~B)|(~A&B)) -> A^B
04909     if ((match(C, m_Not(m_Specific(D))) &&
04910          match(B, m_Not(m_Specific(A)))))
04911       return BinaryOperator::CreateXor(A, D);
04912     // ((~B&A)|(~A&B)) -> A^B
04913     if ((match(A, m_Not(m_Specific(D))) &&
04914          match(B, m_Not(m_Specific(C)))))
04915       return BinaryOperator::CreateXor(C, D);
04916     // ((A&~B)|(B&~A)) -> A^B
04917     if ((match(C, m_Not(m_Specific(B))) &&
04918          match(D, m_Not(m_Specific(A)))))
04919       return BinaryOperator::CreateXor(A, B);
04920     // ((~B&A)|(B&~A)) -> A^B
04921     if ((match(A, m_Not(m_Specific(B))) &&
04922          match(D, m_Not(m_Specific(C)))))
04923       return BinaryOperator::CreateXor(C, B);
04924   }
04925   
04926   // (X >> Z) | (Y >> Z)  -> (X|Y) >> Z  for all shifts.
04927   if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
04928     if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
04929       if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && 
04930           SI0->getOperand(1) == SI1->getOperand(1) &&
04931           (SI0->hasOneUse() || SI1->hasOneUse())) {
04932         Instruction *NewOp =
04933         InsertNewInstBefore(BinaryOperator::CreateOr(SI0->getOperand(0),
04934                                                      SI1->getOperand(0),
04935                                                      SI0->getName()), I);
04936         return BinaryOperator::Create(SI1->getOpcode(), NewOp, 
04937                                       SI1->getOperand(1));
04938       }
04939   }
04940 
04941   // ((A|B)&1)|(B&-2) -> (A&1) | B
04942   if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
04943       match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
04944     Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C);
04945     if (Ret) return Ret;
04946   }
04947   // (B&-2)|((A|B)&1) -> (A&1) | B
04948   if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
04949       match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
04950     Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C);
04951     if (Ret) return Ret;
04952   }
04953 
04954   if (match(Op0, m_Not(m_Value(A)))) {   // ~A | Op1
04955     if (A == Op1)   // ~A | A == -1
04956       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
04957   } else {
04958     A = 0;
04959   }
04960   // Note, A is still live here!
04961   if (match(Op1, m_Not(m_Value(B)))) {   // Op0 | ~B
04962     if (Op0 == B)
04963       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
04964 
04965     // (~A | ~B) == (~(A & B)) - De Morgan's Law
04966     if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
04967       Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B,
04968                                               I.getName()+".demorgan"), I);
04969       return BinaryOperator::CreateNot(And);
04970     }
04971   }
04972 
04973   // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
04974   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) {
04975     if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
04976       return R;
04977 
04978     if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
04979       if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS))
04980         return Res;
04981   }
04982     
04983   // fold (or (cast A), (cast B)) -> (cast (or A, B))
04984   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
04985     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
04986       if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
04987         if (!isa<ICmpInst>(Op0C->getOperand(0)) ||
04988             !isa<ICmpInst>(Op1C->getOperand(0))) {
04989           const Type *SrcTy = Op0C->getOperand(0)->getType();
04990           if (SrcTy == Op1C->getOperand(0)->getType() &&
04991               SrcTy->isIntOrIntVector() &&
04992               // Only do this if the casts both really cause code to be
04993               // generated.
04994               ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), 
04995                                 I.getType(), TD) &&
04996               ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), 
04997                                 I.getType(), TD)) {
04998             Instruction *NewOp = BinaryOperator::CreateOr(Op0C->getOperand(0),
04999                                                           Op1C->getOperand(0),
05000                                                           I.getName());
05001             InsertNewInstBefore(NewOp, I);
05002             return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
05003           }
05004         }
05005       }
05006   }
05007   
05008     
05009   // (fcmp uno x, c) | (fcmp uno y, c)  -> (fcmp uno x, y)
05010   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
05011     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
05012       if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS))
05013         return Res;
05014   }
05015 
05016   return Changed ? &I : 0;
05017 }
05018 
05019 namespace {
05020 
05021 // XorSelf - Implements: X ^ X --> 0
05022 struct XorSelf {
05023   Value *RHS;
05024   XorSelf(Value *rhs) : RHS(rhs) {}
05025   bool shouldApply(Value *LHS) const { return LHS == RHS; }
05026   Instruction *apply(BinaryOperator &Xor) const {
05027     return &Xor;
05028   }
05029 };
05030 
05031 }
05032 
05033 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
05034   bool Changed = SimplifyCommutative(I);
05035   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
05036 
05037   if (isa<UndefValue>(Op1)) {
05038     if (isa<UndefValue>(Op0))
05039       // Handle undef ^ undef -> 0 special case. This is a common
05040       // idiom (misuse).
05041       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
05042     return ReplaceInstUsesWith(I, Op1);  // X ^ undef -> undef
05043   }
05044 
05045   // xor X, X = 0, even if X is nested in a sequence of Xor's.
05046   if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
05047     assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
05048     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
05049   }
05050   
05051   // See if we can simplify any instructions used by the instruction whose sole 
05052   // purpose is to compute bits we don't care about.
05053   if (SimplifyDemandedInstructionBits(I))
05054     return &I;
05055   if (isa<VectorType>(I.getType()))
05056     if (isa<ConstantAggregateZero>(Op1))
05057       return ReplaceInstUsesWith(I, Op0);  // X ^ <0,0> -> X
05058 
05059   // Is this a ~ operation?
05060   if (Value *NotOp = dyn_castNotVal(&I)) {
05061     // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
05062     // ~(~X | Y) === (X & ~Y) - De Morgan's Law
05063     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
05064       if (Op0I->getOpcode() == Instruction::And || 
05065           Op0I->getOpcode() == Instruction::Or) {
05066         if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
05067         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
05068           Instruction *NotY =
05069             BinaryOperator::CreateNot(Op0I->getOperand(1),
05070                                       Op0I->getOperand(1)->getName()+".not");
05071           InsertNewInstBefore(NotY, I);
05072           if (Op0I->getOpcode() == Instruction::And)
05073             return BinaryOperator::CreateOr(Op0NotVal, NotY);
05074           else
05075             return BinaryOperator::CreateAnd(Op0NotVal, NotY);
05076         }
05077       }
05078     }
05079   }
05080   
05081   
05082   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
05083     if (RHS == ConstantInt::getTrue(*Context) && Op0->hasOneUse()) {
05084       // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
05085       if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
05086         return new ICmpInst(*Context, ICI->getInversePredicate(),
05087                             ICI->getOperand(0), ICI->getOperand(1));
05088 
05089       if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
05090         return new FCmpInst(*Context, FCI->getInversePredicate(),
05091                             FCI->getOperand(0), FCI->getOperand(1));
05092     }
05093 
05094     // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
05095     if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
05096       if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
05097         if (CI->hasOneUse() && Op0C->hasOneUse()) {
05098           Instruction::CastOps Opcode = Op0C->getOpcode();
05099           if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
05100             if (RHS == ConstantExpr::getCast(Opcode, 
05101                                              ConstantInt::getTrue(*Context),
05102                                              Op0C->getDestTy())) {
05103               Instruction *NewCI = InsertNewInstBefore(CmpInst::Create(
05104                                      *Context,
05105                                      CI->getOpcode(), CI->getInversePredicate(),
05106                                      CI->getOperand(0), CI->getOperand(1)), I);
05107               NewCI->takeName(CI);
05108               return CastInst::Create(Opcode, NewCI, Op0C->getType());
05109             }
05110           }
05111         }
05112       }
05113     }
05114 
05115     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
05116       // ~(c-X) == X-c-1 == X+(-c-1)
05117       if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
05118         if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
05119           Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
05120           Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
05121                                       ConstantInt::get(I.getType(), 1));
05122           return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
05123         }
05124           
05125       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
05126         if (Op0I->getOpcode() == Instruction::Add) {
05127           // ~(X-c) --> (-c-1)-X
05128           if (RHS->isAllOnesValue()) {
05129             Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
05130             return BinaryOperator::CreateSub(
05131                            ConstantExpr::getSub(NegOp0CI,
05132                                       ConstantInt::get(I.getType(), 1)),
05133                                       Op0I->getOperand(0));
05134           } else if (RHS->getValue().isSignBit()) {
05135             // (X + C) ^ signbit -> (X + C + signbit)
05136             Constant *C = ConstantInt::get(*Context,
05137                                            RHS->getValue() + Op0CI->getValue());
05138             return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
05139 
05140           }
05141         } else if (Op0I->getOpcode() == Instruction::Or) {
05142           // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
05143           if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
05144             Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
05145             // Anything in both C1 and C2 is known to be zero, remove it from
05146             // NewRHS.
05147             Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
05148             NewRHS = ConstantExpr::getAnd(NewRHS, 
05149                                        ConstantExpr::getNot(CommonBits));
05150             AddToWorkList(Op0I);
05151             I.setOperand(0, Op0I->getOperand(0));
05152             I.setOperand(1, NewRHS);
05153             return &I;
05154           }
05155         }
05156       }
05157     }
05158 
05159     // Try to fold constant and into select arguments.
05160     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
05161       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
05162         return R;
05163     if (isa<PHINode>(Op0))
05164       if (Instruction *NV = FoldOpIntoPhi(I))
05165         return NV;
05166   }
05167 
05168   if (Value *X = dyn_castNotVal(Op0))   // ~A ^ A == -1
05169     if (X == Op1)
05170       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
05171 
05172   if (Value *X = dyn_castNotVal(Op1))   // A ^ ~A == -1
05173     if (X == Op0)
05174       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
05175 
05176   
05177   BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
05178   if (Op1I) {
05179     Value *A, *B;
05180     if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
05181       if (A == Op0) {              // B^(B|A) == (A|B)^B
05182         Op1I->swapOperands();
05183         I.swapOperands();
05184         std::swap(Op0, Op1);
05185       } else if (B == Op0) {       // B^(A|B) == (A|B)^B
05186         I.swapOperands();     // Simplified below.
05187         std::swap(Op0, Op1);
05188       }
05189     } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) {
05190       return ReplaceInstUsesWith(I, B);                      // A^(A^B) == B
05191     } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) {
05192       return ReplaceInstUsesWith(I, A);                      // A^(B^A) == B
05193     } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && 
05194                Op1I->hasOneUse()){
05195       if (A == Op0) {                                      // A^(A&B) -> A^(B&A)
05196         Op1I->swapOperands();
05197         std::swap(A, B);
05198       }
05199       if (B == Op0) {                                      // A^(B&A) -> (B&A)^A
05200         I.swapOperands();     // Simplified below.
05201         std::swap(Op0, Op1);
05202       }
05203     }
05204   }
05205   
05206   BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
05207   if (Op0I) {
05208     Value *A, *B;
05209     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
05210         Op0I->hasOneUse()) {
05211       if (A == Op1)                                  // (B|A)^B == (A|B)^B
05212         std::swap(A, B);
05213       if (B == Op1) {                                // (A|B)^B == A & ~B
05214         Instruction *NotB =
05215           InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I);
05216         return BinaryOperator::CreateAnd(A, NotB);
05217       }
05218     } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) {
05219       return ReplaceInstUsesWith(I, B);                      // (A^B)^A == B
05220     } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) {
05221       return ReplaceInstUsesWith(I, A);                      // (B^A)^A == B
05222     } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && 
05223                Op0I->hasOneUse()){
05224       if (A == Op1)                                        // (A&B)^A -> (B&A)^A
05225         std::swap(A, B);
05226       if (B == Op1 &&                                      // (B&A)^A == ~B & A
05227           !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C
05228         Instruction *N =
05229           InsertNewInstBefore(BinaryOperator::CreateNot(A, "tmp"), I);
05230         return BinaryOperator::CreateAnd(N, Op1);
05231       }
05232     }
05233   }
05234   
05235   // (X >> Z) ^ (Y >> Z)  -> (X^Y) >> Z  for all shifts.
05236   if (Op0I && Op1I && Op0I->isShift() && 
05237       Op0I->getOpcode() == Op1I->getOpcode() && 
05238       Op0I->getOperand(1) == Op1I->getOperand(1) &&
05239       (Op1I->hasOneUse() || Op1I->hasOneUse())) {
05240     Instruction *NewOp =
05241       InsertNewInstBefore(BinaryOperator::CreateXor(Op0I->getOperand(0),
05242                                                     Op1I->getOperand(0),
05243                                                     Op0I->getName()), I);
05244     return BinaryOperator::Create(Op1I->getOpcode(), NewOp, 
05245                                   Op1I->getOperand(1));
05246   }
05247     
05248   if (Op0I && Op1I) {
05249     Value *A, *B, *C, *D;
05250     // (A & B)^(A | B) -> A ^ B
05251     if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
05252         match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
05253       if ((A == C && B == D) || (A == D && B == C)) 
05254         return BinaryOperator::CreateXor(A, B);
05255     }
05256     // (A | B)^(A & B) -> A ^ B
05257     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
05258         match(Op1I, m_And(m_Value(C), m_Value(D)))) {
05259       if ((A == C && B == D) || (A == D && B == C)) 
05260         return BinaryOperator::CreateXor(A, B);
05261     }
05262     
05263     // (A & B)^(C & D)
05264     if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
05265         match(Op0I, m_And(m_Value(A), m_Value(B))) &&
05266         match(Op1I, m_And(m_Value(C), m_Value(D)))) {
05267       // (X & Y)^(X & Y) -> (Y^Z) & X
05268       Value *X = 0, *Y = 0, *Z = 0;
05269       if (A == C)
05270         X = A, Y = B, Z = D;
05271       else if (A == D)
05272         X = A, Y = B, Z = C;
05273       else if (B == C)
05274         X = B, Y = A, Z = D;
05275       else if (B == D)
05276         X = B, Y = A, Z = C;
05277       
05278       if (X) {
05279         Instruction *NewOp =
05280         InsertNewInstBefore(BinaryOperator::CreateXor(Y, Z, Op0->getName()), I);
05281         return BinaryOperator::CreateAnd(NewOp, X);
05282       }
05283     }
05284   }
05285     
05286   // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
05287   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
05288     if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
05289       return R;
05290 
05291   // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
05292   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
05293     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
05294       if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
05295         const Type *SrcTy = Op0C->getOperand(0)->getType();
05296         if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
05297             // Only do this if the casts both really cause code to be generated.
05298             ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), 
05299                               I.getType(), TD) &&
05300             ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), 
05301                               I.getType(), TD)) {
05302           Instruction *NewOp = BinaryOperator::CreateXor(Op0C->getOperand(0),
05303                                                          Op1C->getOperand(0),
05304                                                          I.getName());
05305           InsertNewInstBefore(NewOp, I);
05306           return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
05307         }
05308       }
05309   }
05310 
05311   return Changed ? &I : 0;
05312 }
05313 
05314 static ConstantInt *ExtractElement(Constant *V, Constant *Idx,
05315                                    LLVMContext *Context) {
05316   return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
05317 }
05318 
05319 static bool HasAddOverflow(ConstantInt *Result,
05320                            ConstantInt *In1, ConstantInt *In2,
05321                            bool IsSigned) {
05322   if (IsSigned)
05323     if (In2->getValue().isNegative())
05324       return Result->getValue().sgt(In1->getValue());
05325     else
05326       return Result->getValue().slt(In1->getValue());
05327   else
05328     return Result->getValue().ult(In1->getValue());
05329 }
05330 
05331 /// AddWithOverflow - Compute Result = In1+In2, returning true if the result
05332 /// overflowed for this type.
05333 static bool AddWithOverflow(Constant *&Result, Constant *In1,
05334                             Constant *In2, LLVMContext *Context,
05335                             bool IsSigned = false) {
05336   Result = ConstantExpr::getAdd(In1, In2);
05337 
05338   if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
05339     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
05340       Constant *Idx = ConstantInt::get(Type::getInt32Ty(*Context), i);
05341       if (HasAddOverflow(ExtractElement(Result, Idx, Context),
05342                          ExtractElement(In1, Idx, Context),
05343                          ExtractElement(In2, Idx, Context),
05344                          IsSigned))
05345         return true;
05346     }
05347     return false;
05348   }
05349 
05350   return HasAddOverflow(cast<ConstantInt>(Result),
05351                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
05352                         IsSigned);
05353 }
05354 
05355 static bool HasSubOverflow(ConstantInt *Result,
05356                            ConstantInt *In1, ConstantInt *In2,
05357                            bool IsSigned) {
05358   if (IsSigned)
05359     if (In2->getValue().isNegative())
05360       return Result->getValue().slt(In1->getValue());
05361     else
05362       return Result->getValue().sgt(In1->getValue());
05363   else
05364     return Result->getValue().ugt(In1->getValue());
05365 }
05366 
05367 /// SubWithOverflow - Compute Result = In1-In2, returning true if the result
05368 /// overflowed for this type.
05369 static bool SubWithOverflow(Constant *&Result, Constant *In1,
05370                             Constant *In2, LLVMContext *Context,
05371                             bool IsSigned = false) {
05372   Result = ConstantExpr::getSub(In1, In2);
05373 
05374   if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
05375     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
05376       Constant *Idx = ConstantInt::get(Type::getInt32Ty(*Context), i);
05377       if (HasSubOverflow(ExtractElement(Result, Idx, Context),
05378                          ExtractElement(In1, Idx, Context),
05379                          ExtractElement(In2, Idx, Context),
05380                          IsSigned))
05381         return true;
05382     }
05383     return false;
05384   }
05385 
05386   return HasSubOverflow(cast<ConstantInt>(Result),
05387                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
05388                         IsSigned);
05389 }
05390 
05391 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
05392 /// code necessary to compute the offset from the base pointer (without adding
05393 /// in the base pointer).  Return the result as a signed integer of intptr size.
05394 static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
05395   TargetData &TD = *IC.getTargetData();
05396   gep_type_iterator GTI = gep_type_begin(GEP);
05397   const Type *IntPtrTy = TD.getIntPtrType(I.getContext());
05398   LLVMContext *Context = IC.getContext();
05399   Value *Result = Constant::getNullValue(IntPtrTy);
05400 
05401   // Build a mask for high order bits.
05402   unsigned IntPtrWidth = TD.getPointerSizeInBits();
05403   uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
05404 
05405   for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
05406        ++i, ++GTI) {
05407     Value *Op = *i;
05408     uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
05409     if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
05410       if (OpC->isZero()) continue;
05411       
05412       // Handle a struct index, which adds its field offset to the pointer.
05413       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
05414         Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
05415         
05416         if (ConstantInt *RC = dyn_cast<ConstantInt>(Result))
05417           Result = 
05418              ConstantInt::get(*Context, 
05419                               RC->getValue() + APInt(IntPtrWidth, Size));
05420         else
05421           Result = IC.InsertNewInstBefore(
05422                    BinaryOperator::CreateAdd(Result,
05423                                         ConstantInt::get(IntPtrTy, Size),
05424                                              GEP->getName()+".offs"), I);
05425         continue;
05426       }
05427       
05428       Constant *Scale = ConstantInt::get(IntPtrTy, Size);
05429       Constant *OC =
05430               ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
05431       Scale = ConstantExpr::getMul(OC, Scale);
05432       if (Constant *RC = dyn_cast<Constant>(Result))
05433         Result = ConstantExpr::getAdd(RC, Scale);
05434       else {
05435         // Emit an add instruction.
05436         Result = IC.InsertNewInstBefore(
05437            BinaryOperator::CreateAdd(Result, Scale,
05438                                      GEP->getName()+".offs"), I);
05439       }
05440       continue;
05441     }
05442     // Convert to correct type.
05443     if (Op->getType() != IntPtrTy) {
05444       if (Constant *OpC = dyn_cast<Constant>(Op))
05445         Op = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true);
05446       else
05447         Op = IC.InsertNewInstBefore(CastInst::CreateIntegerCast(Op, IntPtrTy,
05448                                                                 true,
05449                                                       Op->getName()+".c"), I);
05450     }
05451     if (Size != 1) {
05452       Constant *Scale = ConstantInt::get(IntPtrTy, Size);
05453       if (Constant *OpC = dyn_cast<Constant>(Op))
05454         Op = ConstantExpr::getMul(OpC, Scale);
05455       else    // We'll let instcombine(mul) convert this to a shl if possible.
05456         Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale,
05457                                                   GEP->getName()+".idx"), I);
05458     }
05459 
05460     // Emit an add instruction.
05461     if (isa<Constant>(Op) && isa<Constant>(Result))
05462       Result = ConstantExpr::getAdd(cast<Constant>(Op),
05463                                     cast<Constant>(Result));
05464     else
05465       Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result,
05466                                                   GEP->getName()+".offs"), I);
05467   }
05468   return Result;
05469 }
05470 
05471 
05472 /// EvaluateGEPOffsetExpression - Return a value that can be used to compare
05473 /// the *offset* implied by a GEP to zero.  For example, if we have &A[i], we
05474 /// want to return 'i' for "icmp ne i, 0".  Note that, in general, indices can
05475 /// be complex, and scales are involved.  The above expression would also be
05476 /// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32).
05477 /// This later form is less amenable to optimization though, and we are allowed
05478 /// to generate the first by knowing that pointer arithmetic doesn't overflow.
05479 ///
05480 /// If we can't emit an optimized form for this expression, this returns null.
05481 /// 
05482 static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I,
05483                                           InstCombiner &IC) {
05484   TargetData &TD = *IC.getTargetData();
05485   gep_type_iterator GTI = gep_type_begin(GEP);
05486 
05487   // Check to see if this gep only has a single variable index.  If so, and if
05488   // any constant indices are a multiple of its scale, then we can compute this
05489   // in terms of the scale of the variable index.  For example, if the GEP
05490   // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
05491   // because the expression will cross zero at the same point.
05492   unsigned i, e = GEP->getNumOperands();
05493   int64_t Offset = 0;
05494   for (i = 1; i != e; ++i, ++GTI) {
05495     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
05496       // Compute the aggregate offset of constant indices.
05497       if (CI->isZero()) continue;
05498 
05499       // Handle a struct index, which adds its field offset to the pointer.
05500       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
05501         Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
05502       } else {
05503         uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
05504         Offset += Size*CI->getSExtValue();
05505       }
05506     } else {
05507       // Found our variable index.
05508       break;
05509     }
05510   }
05511   
05512   // If there are no variable indices, we must have a constant offset, just
05513   // evaluate it the general way.
05514   if (i == e) return 0;
05515   
05516   Value *VariableIdx = GEP->getOperand(i);
05517   // Determine the scale factor of the variable element.  For example, this is
05518   // 4 if the variable index is into an array of i32.
05519   uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType());
05520   
05521   // Verify that there are no other variable indices.  If so, emit the hard way.
05522   for (++i, ++GTI; i != e; ++i, ++GTI) {
05523     ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
05524     if (!CI) return 0;
05525    
05526     // Compute the aggregate offset of constant indices.
05527     if (CI->isZero()) continue;
05528     
05529     // Handle a struct index, which adds its field offset to the pointer.
05530     if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
05531       Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
05532     } else {
05533       uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
05534       Offset += Size*CI->getSExtValue();
05535     }
05536   }
05537   
05538   // Okay, we know we have a single variable index, which must be a
05539   // pointer/array/vector index.  If there is no offset, life is simple, return
05540   // the index.
05541   unsigned IntPtrWidth = TD.getPointerSizeInBits();
05542   if (Offset == 0) {
05543     // Cast to intptrty in case a truncation occurs.  If an extension is needed,
05544     // we don't need to bother extending: the extension won't affect where the
05545     // computation crosses zero.
05546     if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth)
05547       VariableIdx = new TruncInst(VariableIdx, 
05548                                   TD.getIntPtrType(VariableIdx->getContext()),
05549                                   VariableIdx->getName(), &I);
05550     return VariableIdx;
05551   }
05552   
05553   // Otherwise, there is an index.  The computation we will do will be modulo
05554   // the pointer size, so get it.
05555   uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
05556   
05557   Offset &= PtrSizeMask;
05558   VariableScale &= PtrSizeMask;
05559 
05560   // To do this transformation, any constant index must be a multiple of the
05561   // variable scale factor.  For example, we can evaluate "12 + 4*i" as "3 + i",
05562   // but we can't evaluate "10 + 3*i" in terms of i.  Check that the offset is a
05563   // multiple of the variable scale.
05564   int64_t NewOffs = Offset / (int64_t)VariableScale;
05565   if (Offset != NewOffs*(int64_t)VariableScale)
05566     return 0;
05567 
05568   // Okay, we can do this evaluation.  Start by converting the index to intptr.
05569   const Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
05570   if (VariableIdx->getType() != IntPtrTy)
05571     VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
05572                                               true /*SExt*/, 
05573                                               VariableIdx->getName(), &I);
05574   Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
05575   return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I);
05576 }
05577 
05578 
05579 /// FoldGEPICmp - Fold comparisons between a GEP instruction and something
05580 /// else.  At this point we know that the GEP is on the LHS of the comparison.
05581 Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
05582                                        ICmpInst::Predicate Cond,
05583                                        Instruction &I) {
05584   // Look through bitcasts.
05585   if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS))
05586     RHS = BCI->getOperand(0);
05587 
05588   Value *PtrBase = GEPLHS->getOperand(0);
05589   if (TD && PtrBase == RHS && GEPLHS->isInBounds()) {
05590     // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0).
05591     // This transformation (ignoring the base and scales) is valid because we
05592     // know pointers can't overflow since the gep is inbounds.  See if we can
05593     // output an optimized form.
05594     Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this);
05595     
05596     // If not, synthesize the offset the hard way.
05597     if (Offset == 0)
05598       Offset = EmitGEPOffset(GEPLHS, I, *this);
05599     return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), Offset,
05600                         Constant::getNullValue(Offset->getType()));
05601   } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
05602     // If the base pointers are different, but the indices are the same, just
05603     // compare the base pointer.
05604     if (PtrBase != GEPRHS->getOperand(0)) {
05605       bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
05606       IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
05607                         GEPRHS->getOperand(0)->getType();
05608       if (IndicesTheSame)
05609         for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
05610           if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
05611             IndicesTheSame = false;
05612             break;
05613           }
05614 
05615       // If all indices are the same, just compare the base pointers.
05616       if (IndicesTheSame)
05617         return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), 
05618                             GEPLHS->getOperand(0), GEPRHS->getOperand(0));
05619 
05620       // Otherwise, the base pointers are different and the indices are
05621       // different, bail out.
05622       return 0;
05623     }
05624 
05625     // If one of the GEPs has all zero indices, recurse.
05626     bool AllZeros = true;
05627     for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
05628       if (!isa<Constant>(GEPLHS->getOperand(i)) ||
05629           !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
05630         AllZeros = false;
05631         break;
05632       }
05633     if (AllZeros)
05634       return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
05635                           ICmpInst::getSwappedPredicate(Cond), I);
05636 
05637     // If the other GEP has all zero indices, recurse.
05638     AllZeros = true;
05639     for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
05640       if (!isa<Constant>(GEPRHS->getOperand(i)) ||
05641           !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
05642         AllZeros = false;
05643         break;
05644       }
05645     if (AllZeros)
05646       return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
05647 
05648     if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
05649       // If the GEPs only differ by one index, compare it.
05650       unsigned NumDifferences = 0;  // Keep track of # differences.
05651       unsigned DiffOperand = 0;     // The operand that differs.
05652       for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
05653         if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
05654           if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
05655                    GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
05656             // Irreconcilable differences.
05657             NumDifferences = 2;
05658             break;
05659           } else {
05660             if (NumDifferences++) break;
05661             DiffOperand = i;
05662           }
05663         }
05664 
05665       if (NumDifferences == 0)   // SAME GEP?
05666         return ReplaceInstUsesWith(I, // No comparison is needed here.
05667                                    ConstantInt::get(Type::getInt1Ty(*Context),
05668                                              ICmpInst::isTrueWhenEqual(Cond)));
05669 
05670       else if (NumDifferences == 1) {
05671         Value *LHSV = GEPLHS->getOperand(DiffOperand);
05672         Value *RHSV = GEPRHS->getOperand(DiffOperand);
05673         // Make sure we do a signed comparison here.
05674         return new ICmpInst(*Context,
05675                             ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
05676       }
05677     }
05678 
05679     // Only lower this if the icmp is the only user of the GEP or if we expect
05680     // the result to fold to a constant!
05681     if (TD &&
05682         (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
05683         (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
05684       // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)  --->  (OFFSET1 cmp OFFSET2)
05685       Value *L = EmitGEPOffset(GEPLHS, I, *this);
05686       Value *R = EmitGEPOffset(GEPRHS, I, *this);
05687       return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), L, R);
05688     }
05689   }
05690   return 0;
05691 }
05692 
05693 /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.
05694 ///
05695 Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
05696                                                 Instruction *LHSI,
05697                                                 Constant *RHSC) {
05698   if (!isa<ConstantFP>(RHSC)) return 0;
05699   const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
05700   
05701   // Get the width of the mantissa.  We don't want to hack on conversions that
05702   // might lose information from the integer, e.g. "i64 -> float"
05703   int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
05704   if (MantissaWidth == -1) return 0;  // Unknown.
05705   
05706   // Check to see that the input is converted from an integer type that is small
05707   // enough that preserves all bits.  TODO: check here for "known" sign bits.
05708   // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
05709   unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits();
05710   
05711   // If this is a uitofp instruction, we need an extra bit to hold the sign.
05712   bool LHSUnsigned = isa<UIToFPInst>(LHSI);
05713   if (LHSUnsigned)
05714     ++InputSize;
05715   
05716   // If the conversion would lose info, don't hack on this.
05717   if ((int)InputSize > MantissaWidth)
05718     return 0;
05719   
05720   // Otherwise, we can potentially simplify the comparison.  We know that it
05721   // will always come through as an integer value and we know the constant is
05722   // not a NAN (it would have been previously simplified).
05723   assert(!RHS.isNaN() && "NaN comparison not already folded!");
05724   
05725   ICmpInst::Predicate Pred;
05726   switch (I.getPredicate()) {
05727   default: llvm_unreachable("Unexpected predicate!");
05728   case FCmpInst::FCMP_UEQ:
05729   case FCmpInst::FCMP_OEQ:
05730     Pred = ICmpInst::ICMP_EQ;
05731     break;
05732   case FCmpInst::FCMP_UGT:
05733   case FCmpInst::FCMP_OGT:
05734     Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
05735     break;
05736   case FCmpInst::FCMP_UGE:
05737   case FCmpInst::FCMP_OGE:
05738     Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
05739     break;
05740   case FCmpInst::FCMP_ULT:
05741   case FCmpInst::FCMP_OLT:
05742     Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
05743     break;
05744   case FCmpInst::FCMP_ULE:
05745   case FCmpInst::FCMP_OLE:
05746     Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
05747     break;
05748   case FCmpInst::FCMP_UNE:
05749   case FCmpInst::FCMP_ONE:
05750     Pred = ICmpInst::ICMP_NE;
05751     break;
05752   case FCmpInst::FCMP_ORD:
05753     return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05754   case FCmpInst::FCMP_UNO:
05755     return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05756   }
05757   
05758   const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
05759   
05760   // Now we know that the APFloat is a normal number, zero or inf.
05761   
05762   // See if the FP constant is too large for the integer.  For example,
05763   // comparing an i8 to 300.0.
05764   unsigned IntWidth = IntTy->getScalarSizeInBits();
05765   
05766   if (!LHSUnsigned) {
05767     // If the RHS value is > SignedMax, fold the comparison.  This handles +INF
05768     // and large values.
05769     APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false);
05770     SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
05771                           APFloat::rmNearestTiesToEven);
05772     if (SMax.compare(RHS) == APFloat::cmpLessThan) {  // smax < 13123.0
05773       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_SLT ||
05774           Pred == ICmpInst::ICMP_SLE)
05775         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05776       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05777     }
05778   } else {
05779     // If the RHS value is > UnsignedMax, fold the comparison. This handles
05780     // +INF and large values.
05781     APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false);
05782     UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
05783                           APFloat::rmNearestTiesToEven);
05784     if (UMax.compare(RHS) == APFloat::cmpLessThan) {  // umax < 13123.0
05785       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_ULT ||
05786           Pred == ICmpInst::ICMP_ULE)
05787         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05788       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05789     }
05790   }
05791   
05792   if (!LHSUnsigned) {
05793     // See if the RHS value is < SignedMin.
05794     APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false);
05795     SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
05796                           APFloat::rmNearestTiesToEven);
05797     if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
05798       if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
05799           Pred == ICmpInst::ICMP_SGE)
05800         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05801       return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05802     }
05803   }
05804 
05805   // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
05806   // [0, UMAX], but it may still be fractional.  See if it is fractional by
05807   // casting the FP value to the integer value and back, checking for equality.
05808   // Don't do this for zero, because -0.0 is not fractional.
05809   Constant *RHSInt = LHSUnsigned
05810     ? ConstantExpr::getFPToUI(RHSC, IntTy)
05811     : ConstantExpr::getFPToSI(RHSC, IntTy);
05812   if (!RHS.isZero()) {
05813     bool Equal = LHSUnsigned
05814       ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
05815       : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
05816     if (!Equal) {
05817       // If we had a comparison against a fractional value, we have to adjust
05818       // the compare predicate and sometimes the value.  RHSC is rounded towards
05819       // zero at this point.
05820       switch (Pred) {
05821       default: llvm_unreachable("Unexpected integer comparison!");
05822       case ICmpInst::ICMP_NE:  // (float)int != 4.4   --> true
05823         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05824       case ICmpInst::ICMP_EQ:  // (float)int == 4.4   --> false
05825         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05826       case ICmpInst::ICMP_ULE:
05827         // (float)int <= 4.4   --> int <= 4
05828         // (float)int <= -4.4  --> false
05829         if (RHS.isNegative())
05830           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05831         break;
05832       case ICmpInst::ICMP_SLE:
05833         // (float)int <= 4.4   --> int <= 4
05834         // (float)int <= -4.4  --> int < -4
05835         if (RHS.isNegative())
05836           Pred = ICmpInst::ICMP_SLT;
05837         break;
05838       case ICmpInst::ICMP_ULT:
05839         // (float)int < -4.4   --> false
05840         // (float)int < 4.4    --> int <= 4
05841         if (RHS.isNegative())
05842           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05843         Pred = ICmpInst::ICMP_ULE;
05844         break;
05845       case ICmpInst::ICMP_SLT:
05846         // (float)int < -4.4   --> int < -4
05847         // (float)int < 4.4    --> int <= 4
05848         if (!RHS.isNegative())
05849           Pred = ICmpInst::ICMP_SLE;
05850         break;
05851       case ICmpInst::ICMP_UGT:
05852         // (float)int > 4.4    --> int > 4
05853         // (float)int > -4.4   --> true
05854         if (RHS.isNegative())
05855           return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05856         break;
05857       case ICmpInst::ICMP_SGT:
05858         // (float)int > 4.4    --> int > 4
05859         // (float)int > -4.4   --> int >= -4
05860         if (RHS.isNegative())
05861           Pred = ICmpInst::ICMP_SGE;
05862         break;
05863       case ICmpInst::ICMP_UGE:
05864         // (float)int >= -4.4   --> true
05865         // (float)int >= 4.4    --> int > 4
05866         if (!RHS.isNegative())
05867           return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05868         Pred = ICmpInst::ICMP_UGT;
05869         break;
05870       case ICmpInst::ICMP_SGE:
05871         // (float)int >= -4.4   --> int >= -4
05872         // (float)int >= 4.4    --> int > 4
05873         if (!RHS.isNegative())
05874           Pred = ICmpInst::ICMP_SGT;
05875         break;
05876       }
05877     }
05878   }
05879 
05880   // Lower this FP comparison into an appropriate integer version of the
05881   // comparison.
05882   return new ICmpInst(*Context, Pred, LHSI->getOperand(0), RHSInt);
05883 }
05884 
05885 Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
05886   bool Changed = SimplifyCompare(I);
05887   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
05888 
05889   // Fold trivial predicates.
05890   if (I.getPredicate() == FCmpInst::FCMP_FALSE)
05891     return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 0));
05892   if (I.getPredicate() == FCmpInst::FCMP_TRUE)
05893     return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 1));
05894   
05895   // Simplify 'fcmp pred X, X'
05896   if (Op0 == Op1) {
05897     switch (I.getPredicate()) {
05898     default: llvm_unreachable("Unknown predicate!");
05899     case FCmpInst::FCMP_UEQ:    // True if unordered or equal
05900     case FCmpInst::FCMP_UGE:    // True if unordered, greater than, or equal
05901     case FCmpInst::FCMP_ULE:    // True if unordered, less than, or equal
05902       return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 1));
05903     case FCmpInst::FCMP_OGT:    // True if ordered and greater than
05904     case FCmpInst::FCMP_OLT:    // True if ordered and less than
05905     case FCmpInst::FCMP_ONE:    // True if ordered and operands are unequal
05906       return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 0));
05907       
05908     case FCmpInst::FCMP_UNO:    // True if unordered: isnan(X) | isnan(Y)
05909     case FCmpInst::FCMP_ULT:    // True if unordered or less than
05910     case FCmpInst::FCMP_UGT:    // True if unordered or greater than
05911     case FCmpInst::FCMP_UNE:    // True if unordered or not equal
05912       // Canonicalize these to be 'fcmp uno %X, 0.0'.
05913       I.setPredicate(FCmpInst::FCMP_UNO);
05914       I.setOperand(1, Constant::getNullValue(Op0->getType()));
05915       return &I;
05916       
05917     case FCmpInst::FCMP_ORD:    // True if ordered (no nans)
05918     case FCmpInst::FCMP_OEQ:    // True if ordered and equal
05919     case FCmpInst::FCMP_OGE:    // True if ordered and greater than or equal
05920     case FCmpInst::FCMP_OLE:    // True if ordered and less than or equal
05921       // Canonicalize these to be 'fcmp ord %X, 0.0'.
05922       I.setPredicate(FCmpInst::FCMP_ORD);
05923       I.setOperand(1, Constant::getNullValue(Op0->getType()));
05924       return &I;
05925     }
05926   }
05927     
05928   if (isa<UndefValue>(Op1))                  // fcmp pred X, undef -> undef
05929     return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
05930 
05931   // Handle fcmp with constant RHS
05932   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
05933     // If the constant is a nan, see if we can fold the comparison based on it.
05934     if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
05935       if (CFP->getValueAPF().isNaN()) {
05936         if (FCmpInst::isOrdered(I.getPredicate()))   // True if ordered and...
05937           return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
05938         assert(FCmpInst::isUnordered(I.getPredicate()) &&
05939                "Comparison must be either ordered or unordered!");
05940         // True if unordered.
05941         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
05942       }
05943     }
05944     
05945     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
05946       switch (LHSI->getOpcode()) {
05947       case Instruction::PHI:
05948         // Only fold fcmp into the PHI if the phi and fcmp are in the same
05949         // block.  If in the same block, we're encouraging jump threading.  If
05950         // not, we are just pessimizing the code by making an i1 phi.
05951         if (LHSI->getParent() == I.getParent())
05952           if (Instruction *NV = FoldOpIntoPhi(I))
05953             return NV;
05954         break;
05955       case Instruction::SIToFP:
05956       case Instruction::UIToFP:
05957         if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC))
05958           return NV;
05959         break;
05960       case Instruction::Select:
05961         // If either operand of the select is a constant, we can fold the
05962         // comparison into the select arms, which will cause one to be
05963         // constant folded and the select turned into a bitwise or.
05964         Value *Op1 = 0, *Op2 = 0;
05965         if (LHSI->hasOneUse()) {
05966           if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
05967             // Fold the known value into the constant operand.
05968             Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
05969             // Insert a new FCmp of the other select operand.
05970             Op2 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(),
05971                                                       LHSI->getOperand(2), RHSC,
05972                                                       I.getName()), I);
05973           } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
05974             // Fold the known value into the constant operand.
05975             Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
05976             // Insert a new FCmp of the other select operand.
05977             Op1 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(),
05978                                                       LHSI->getOperand(1), RHSC,
05979                                                       I.getName()), I);
05980           }
05981         }
05982 
05983         if (Op1)
05984           return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
05985         break;
05986       }
05987   }
05988 
05989   return Changed ? &I : 0;
05990 }
05991 
05992 Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
05993   bool Changed = SimplifyCompare(I);
05994   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
05995   const Type *Ty = Op0->getType();
05996 
05997   // icmp X, X
05998   if (Op0 == Op1)
05999     return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(),
06000                                                    I.isTrueWhenEqual()));
06001 
06002   if (isa<UndefValue>(Op1))                  // X icmp undef -> undef
06003     return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
06004   
06005   // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
06006   // addresses never equal each other!  We already know that Op0 != Op1.
06007   if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) ||
06008        isa<ConstantPointerNull>(Op0)) &&
06009       (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
06010        isa<ConstantPointerNull>(Op1)))
06011     return ReplaceInstUsesWith(I, ConstantInt::get(Type::getInt1Ty(*Context), 
06012                                                    !I.isTrueWhenEqual()));
06013 
06014   // icmp's with boolean values can always be turned into bitwise operations
06015   if (Ty == Type::getInt1Ty(*Context)) {
06016     switch (I.getPredicate()) {
06017     default: llvm_unreachable("Invalid icmp instruction!");
06018     case ICmpInst::ICMP_EQ: {               // icmp eq i1 A, B -> ~(A^B)
06019       Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp");
06020       InsertNewInstBefore(Xor, I);
06021       return BinaryOperator::CreateNot(Xor);
06022     }
06023     case ICmpInst::ICMP_NE:                  // icmp eq i1 A, B -> A^B
06024       return BinaryOperator::CreateXor(Op0, Op1);
06025 
06026     case ICmpInst::ICMP_UGT:
06027       std::swap(Op0, Op1);                   // Change icmp ugt -> icmp ult
06028       // FALL THROUGH
06029     case ICmpInst::ICMP_ULT:{               // icmp ult i1 A, B -> ~A & B
06030       Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp");
06031       InsertNewInstBefore(Not, I);
06032       return BinaryOperator::CreateAnd(Not, Op1);
06033     }
06034     case ICmpInst::ICMP_SGT:
06035       std::swap(Op0, Op1);                   // Change icmp sgt -> icmp slt
06036       // FALL THROUGH
06037     case ICmpInst::ICMP_SLT: {               // icmp slt i1 A, B -> A & ~B
06038       Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp");
06039       InsertNewInstBefore(Not, I);
06040       return BinaryOperator::CreateAnd(Not, Op0);
06041     }
06042     case ICmpInst::ICMP_UGE:
06043       std::swap(Op0, Op1);                   // Change icmp uge -> icmp ule
06044       // FALL THROUGH
06045     case ICmpInst::ICMP_ULE: {               //  icmp ule i1 A, B -> ~A | B
06046       Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp");
06047       InsertNewInstBefore(Not, I);
06048       return BinaryOperator::CreateOr(Not, Op1);
06049     }
06050     case ICmpInst::ICMP_SGE:
06051       std::swap(Op0, Op1);                   // Change icmp sge -> icmp sle
06052       // FALL THROUGH
06053     case ICmpInst::ICMP_SLE: {               //  icmp sle i1 A, B -> A | ~B
06054       Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp");
06055       InsertNewInstBefore(Not, I);
06056       return BinaryOperator::CreateOr(Not, Op0);
06057     }
06058     }
06059   }
06060 
06061   unsigned BitWidth = 0;
06062   if (TD)
06063     BitWidth = TD->getTypeSizeInBits(Ty->getScalarType());
06064   else if (Ty->isIntOrIntVector())
06065     BitWidth = Ty->getScalarSizeInBits();
06066 
06067   bool isSignBit = false;
06068 
06069   // See if we are doing a comparison with a constant.
06070   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
06071     Value *A = 0, *B = 0;
06072     
06073     // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
06074     if (I.isEquality() && CI->isNullValue() &&
06075         match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
06076       // (icmp cond A B) if cond is equality
06077       return new ICmpInst(*Context, I.getPredicate(), A, B);
06078     }
06079     
06080     // If we have an icmp le or icmp ge instruction, turn it into the
06081     // appropriate icmp lt or icmp gt instruction.  This allows us to rely on
06082     // them being folded in the code below.
06083     switch (I.getPredicate()) {
06084     default: break;
06085     case ICmpInst::ICMP_ULE:
06086       if (CI->isMaxValue(false))                 // A <=u MAX -> TRUE
06087         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06088       return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Op0,
06089                           AddOne(CI));
06090     case ICmpInst::ICMP_SLE:
06091       if (CI->isMaxValue(true))                  // A <=s MAX -> TRUE
06092         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06093       return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0,
06094                           AddOne(CI));
06095     case ICmpInst::ICMP_UGE:
06096       if (CI->isMinValue(false))                 // A >=u MIN -> TRUE
06097         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06098       return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Op0,
06099                           SubOne(CI));
06100     case ICmpInst::ICMP_SGE:
06101       if (CI->isMinValue(true))                  // A >=s MIN -> TRUE
06102         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06103       return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
06104                           SubOne(CI));
06105     }
06106     
06107     // If this comparison is a normal comparison, it demands all
06108     // bits, if it is a sign bit comparison, it only demands the sign bit.
06109     bool UnusedBit;
06110     isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
06111   }
06112 
06113   // See if we can fold the comparison based on range information we can get
06114   // by checking whether bits are known to be zero or one in the input.
06115   if (BitWidth != 0) {
06116     APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
06117     APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
06118 
06119     if (SimplifyDemandedBits(I.getOperandUse(0),
06120                              isSignBit ? APInt::getSignBit(BitWidth)
06121                                        : APInt::getAllOnesValue(BitWidth),
06122                              Op0KnownZero, Op0KnownOne, 0))
06123       return &I;
06124     if (SimplifyDemandedBits(I.getOperandUse(1),
06125                              APInt::getAllOnesValue(BitWidth),
06126                              Op1KnownZero, Op1KnownOne, 0))
06127       return &I;
06128 
06129     // Given the known and unknown bits, compute a range that the LHS could be
06130     // in.  Compute the Min, Max and RHS values based on the known bits. For the
06131     // EQ and NE we use unsigned values.
06132     APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
06133     APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
06134     if (ICmpInst::isSignedPredicate(I.getPredicate())) {
06135       ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
06136                                              Op0Min, Op0Max);
06137       ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
06138                                              Op1Min, Op1Max);
06139     } else {
06140       ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
06141                                                Op0Min, Op0Max);
06142       ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
06143                                                Op1Min, Op1Max);
06144     }
06145 
06146     // If Min and Max are known to be the same, then SimplifyDemandedBits
06147     // figured out that the LHS is a constant.  Just constant fold this now so
06148     // that code below can assume that Min != Max.
06149     if (!isa<Constant>(Op0) && Op0Min == Op0Max)
06150       return new ICmpInst(*Context, I.getPredicate(),
06151                           ConstantInt::get(*Context, Op0Min), Op1);
06152     if (!isa<Constant>(Op1) && Op1Min == Op1Max)
06153       return new ICmpInst(*Context, I.getPredicate(), Op0, 
06154                           ConstantInt::get(*Context, Op1Min));
06155 
06156     // Based on the range information we know about the LHS, see if we can
06157     // simplify this comparison.  For example, (x&4) < 8  is always true.
06158     switch (I.getPredicate()) {
06159     default: llvm_unreachable("Unknown icmp opcode!");
06160     case ICmpInst::ICMP_EQ:
06161       if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
06162         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06163       break;
06164     case ICmpInst::ICMP_NE:
06165       if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
06166         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06167       break;
06168     case ICmpInst::ICMP_ULT:
06169       if (Op0Max.ult(Op1Min))          // A <u B -> true if max(A) < min(B)
06170         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06171       if (Op0Min.uge(Op1Max))          // A <u B -> false if min(A) >= max(B)
06172         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06173       if (Op1Min == Op0Max)            // A <u B -> A != B if max(A) == min(B)
06174         return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
06175       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
06176         if (Op1Max == Op0Min+1)        // A <u C -> A == C-1 if min(A)+1 == C
06177           return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
06178                               SubOne(CI));
06179 
06180         // (x <u 2147483648) -> (x >s -1)  -> true if sign bit clear
06181         if (CI->isMinValue(true))
06182           return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
06183                            Constant::getAllOnesValue(Op0->getType()));
06184       }
06185       break;
06186     case ICmpInst::ICMP_UGT:
06187       if (Op0Min.ugt(Op1Max))          // A >u B -> true if min(A) > max(B)
06188         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06189       if (Op0Max.ule(Op1Min))          // A >u B -> false if max(A) <= max(B)
06190         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06191 
06192       if (Op1Max == Op0Min)            // A >u B -> A != B if min(A) == max(B)
06193         return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
06194       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
06195         if (Op1Min == Op0Max-1)        // A >u C -> A == C+1 if max(a)-1 == C
06196           return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
06197                               AddOne(CI));
06198 
06199         // (x >u 2147483647) -> (x <s 0)  -> true if sign bit set
06200         if (CI->isMaxValue(true))
06201           return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0,
06202                               Constant::getNullValue(Op0->getType()));
06203       }
06204       break;
06205     case ICmpInst::ICMP_SLT:
06206       if (Op0Max.slt(Op1Min))          // A <s B -> true if max(A) < min(C)
06207         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06208       if (Op0Min.sge(Op1Max))          // A <s B -> false if min(A) >= max(C)
06209         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06210       if (Op1Min == Op0Max)            // A <s B -> A != B if max(A) == min(B)
06211         return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
06212       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
06213         if (Op1Max == Op0Min+1)        // A <s C -> A == C-1 if min(A)+1 == C
06214           return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
06215                               SubOne(CI));
06216       }
06217       break;
06218     case ICmpInst::ICMP_SGT:
06219       if (Op0Min.sgt(Op1Max))          // A >s B -> true if min(A) > max(B)
06220         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06221       if (Op0Max.sle(Op1Min))          // A >s B -> false if max(A) <= min(B)
06222         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06223 
06224       if (Op1Max == Op0Min)            // A >s B -> A != B if min(A) == max(B)
06225         return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
06226       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
06227         if (Op1Min == Op0Max-1)        // A >s C -> A == C+1 if max(A)-1 == C
06228           return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
06229                               AddOne(CI));
06230       }
06231       break;
06232     case ICmpInst::ICMP_SGE:
06233       assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
06234       if (Op0Min.sge(Op1Max))          // A >=s B -> true if min(A) >= max(B)
06235         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06236       if (Op0Max.slt(Op1Min))          // A >=s B -> false if max(A) < min(B)
06237         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06238       break;
06239     case ICmpInst::ICMP_SLE:
06240       assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
06241       if (Op0Max.sle(Op1Min))          // A <=s B -> true if max(A) <= min(B)
06242         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06243       if (Op0Min.sgt(Op1Max))          // A <=s B -> false if min(A) > max(B)
06244         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06245       break;
06246     case ICmpInst::ICMP_UGE:
06247       assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
06248       if (Op0Min.uge(Op1Max))          // A >=u B -> true if min(A) >= max(B)
06249         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06250       if (Op0Max.ult(Op1Min))          // A >=u B -> false if max(A) < min(B)
06251         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06252       break;
06253     case ICmpInst::ICMP_ULE:
06254       assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
06255       if (Op0Max.ule(Op1Min))          // A <=u B -> true if max(A) <= min(B)
06256         return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
06257       if (Op0Min.ugt(Op1Max))          // A <=u B -> false if min(A) > max(B)
06258         return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
06259       break;
06260     }
06261 
06262     // Turn a signed comparison into an unsigned one if both operands
06263     // are known to have the same sign.
06264     if (I.isSignedPredicate() &&
06265         ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
06266          (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
06267       return new ICmpInst(*Context, I.getUnsignedPredicate(), Op0, Op1);
06268   }
06269 
06270   // Test if the ICmpInst instruction is used exclusively by a select as
06271   // part of a minimum or maximum operation. If so, refrain from doing
06272   // any other folding. This helps out other analyses which understand
06273   // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
06274   // and CodeGen. And in this case, at least one of the comparison
06275   // operands has at least one user besides the compare (the select),
06276   // which would often largely negate the benefit of folding anyway.
06277   if (I.hasOneUse())
06278     if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin()))
06279       if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
06280           (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
06281         return 0;
06282 
06283   // See if we are doing a comparison between a constant and an instruction that
06284   // can be folded into the comparison.
06285   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
06286     // Since the RHS is a ConstantInt (CI), if the left hand side is an 
06287     // instruction, see if that instruction also has constants so that the 
06288     // instruction can be folded into the icmp 
06289     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
06290       if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
06291         return Res;
06292   }
06293 
06294   // Handle icmp with constant (but not simple integer constant) RHS
06295   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
06296     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
06297       switch (LHSI->getOpcode()) {
06298       case Instruction::GetElementPtr:
06299         if (RHSC->isNullValue()) {
06300           // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
06301           bool isAllZeros = true;
06302           for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i)
06303             if (!isa<Constant>(LHSI->getOperand(i)) ||
06304                 !cast<Constant>(LHSI->getOperand(i))->isNullValue()) {
06305               isAllZeros = false;
06306               break;
06307             }
06308           if (isAllZeros)
06309             return new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(0),
06310                     Constant::getNullValue(LHSI->getOperand(0)->getType()));
06311         }
06312         break;
06313 
06314       case Instruction::PHI:
06315         // Only fold icmp into the PHI if the phi and fcmp are in the same
06316         // block.  If in the same block, we're encouraging jump threading.  If
06317         // not, we are just pessimizing the code by making an i1 phi.
06318         if (LHSI->getParent() == I.getParent())
06319           if (Instruction *NV = FoldOpIntoPhi(I))
06320             return NV;
06321         break;
06322       case Instruction::Select: {
06323         // If either operand of the select is a constant, we can fold the
06324         // comparison into the select arms, which will cause one to be
06325         // constant folded and the select turned into a bitwise or.
06326         Value *Op1 = 0, *Op2 = 0;
06327         if (LHSI->hasOneUse()) {
06328           if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
06329             // Fold the known value into the constant operand.
06330             Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
06331             // Insert a new ICmp of the other select operand.
06332             Op2 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(),
06333                                                    LHSI->getOperand(2), RHSC,
06334                                                    I.getName()), I);
06335           } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
06336             // Fold the known value into the constant operand.
06337             Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
06338             // Insert a new ICmp of the other select operand.
06339             Op1 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(),
06340                                                    LHSI->getOperand(1), RHSC,
06341                                                    I.getName()), I);
06342           }
06343         }
06344 
06345         if (Op1)
06346           return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
06347         break;
06348       }
06349       case Instruction::Malloc:
06350         // If we have (malloc != null), and if the malloc has a single use, we
06351         // can assume it is successful and remove the malloc.
06352         if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) {
06353           AddToWorkList(LHSI);
06354           return ReplaceInstUsesWith(I, ConstantInt::get(Type::getInt1Ty(*Context),
06355                                                          !I.isTrueWhenEqual()));
06356         }
06357         break;
06358       }
06359   }
06360 
06361   // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
06362   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
06363     if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
06364       return NI;
06365   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
06366     if (Instruction *NI = FoldGEPICmp(GEP, Op0,
06367                            ICmpInst::getSwappedPredicate(I.getPredicate()), I))
06368       return NI;
06369 
06370   // Test to see if the operands of the icmp are casted versions of other
06371   // values.  If the ptr->ptr cast can be stripped off both arguments, we do so
06372   // now.
06373   if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
06374     if (isa<PointerType>(Op0->getType()) && 
06375         (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { 
06376       // We keep moving the cast from the left operand over to the right
06377       // operand, where it can often be eliminated completely.
06378       Op0 = CI->getOperand(0);
06379 
06380       // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
06381       // so eliminate it as well.
06382       if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
06383         Op1 = CI2->getOperand(0);
06384 
06385       // If Op1 is a constant, we can fold the cast into the constant.
06386       if (Op0->getType() != Op1->getType()) {
06387         if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
06388           Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
06389         } else {
06390           // Otherwise, cast the RHS right before the icmp
06391           Op1 = InsertBitCastBefore(Op1, Op0->getType(), I);
06392         }
06393       }
06394       return new ICmpInst(*Context, I.getPredicate(), Op0, Op1);
06395     }
06396   }
06397   
06398   if (isa<CastInst>(Op0)) {
06399     // Handle the special case of: icmp (cast bool to X), <cst>
06400     // This comes up when you have code like
06401     //   int X = A < B;
06402     //   if (X) ...
06403     // For generality, we handle any zero-extension of any operand comparison
06404     // with a constant or another cast from the same type.
06405     if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1))
06406       if (Instruction *R = visitICmpInstWithCastAndCast(I))
06407         return R;
06408   }
06409   
06410   // See if it's the same type of instruction on the left and right.
06411   if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
06412     if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
06413       if (Op0I->getOpcode() == Op1I->getOpcode() && Op0I->hasOneUse() &&
06414           Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1)) {
06415         switch (Op0I->getOpcode()) {
06416         default: break;
06417         case Instruction::Add:
06418         case Instruction::Sub:
06419         case Instruction::Xor:
06420           if (I.isEquality())    // a+x icmp eq/ne b+x --> a icmp b
06421             return new ICmpInst(*Context, I.getPredicate(), Op0I->getOperand(0),
06422                                 Op1I->getOperand(0));
06423           // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
06424           if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
06425             if (CI->getValue().isSignBit()) {
06426               ICmpInst::Predicate Pred = I.isSignedPredicate()
06427                                              ? I.getUnsignedPredicate()
06428                                              : I.getSignedPredicate();
06429               return new ICmpInst(*Context, Pred, Op0I->getOperand(0),
06430                                   Op1I->getOperand(0));
06431             }
06432             
06433             if (CI->getValue().isMaxSignedValue()) {
06434               ICmpInst::Predicate Pred = I.isSignedPredicate()
06435                                              ? I.getUnsignedPredicate()
06436                                              : I.getSignedPredicate();
06437               Pred = I.getSwappedPredicate(Pred);
06438               return new ICmpInst(*Context, Pred, Op0I->getOperand(0),
06439                                   Op1I->getOperand(0));
06440             }
06441           }
06442           break;
06443         case Instruction::Mul:
06444           if (!I.isEquality())
06445             break;
06446 
06447           if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
06448             // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
06449             // Mask = -1 >> count-trailing-zeros(Cst).
06450             if (!CI->isZero() && !CI->isOne()) {
06451               const APInt &AP = CI->getValue();
06452               ConstantInt *Mask = ConstantInt::get(*Context, 
06453                                       APInt::getLowBitsSet(AP.getBitWidth(),
06454                                                            AP.getBitWidth() -
06455                                                       AP.countTrailingZeros()));
06456               Instruction *And1 = BinaryOperator::CreateAnd(Op0I->getOperand(0),
06457                                                             Mask);
06458               Instruction *And2 = BinaryOperator::CreateAnd(Op1I->getOperand(0),
06459                                                             Mask);
06460               InsertNewInstBefore(And1, I);
06461               InsertNewInstBefore(And2, I);
06462               return new ICmpInst(*Context, I.getPredicate(), And1, And2);
06463             }
06464           }
06465           break;
06466         }
06467       }
06468     }
06469   }
06470   
06471   // ~x < ~y --> y < x
06472   { Value *A, *B;
06473     if (match(Op0, m_Not(m_Value(A))) &&
06474         match(Op1, m_Not(m_Value(B))))
06475       return new ICmpInst(*Context, I.getPredicate(), B, A);
06476   }
06477   
06478   if (I.isEquality()) {
06479     Value *A, *B, *C, *D;
06480     
06481     // -x == -y --> x == y
06482     if (match(Op0, m_Neg(m_Value(A))) &&
06483         match(Op1, m_Neg(m_Value(B))))
06484       return new ICmpInst(*Context, I.getPredicate(), A, B);
06485     
06486     if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
06487       if (A == Op1 || B == Op1) {    // (A^B) == A  ->  B == 0
06488         Value *OtherVal = A == Op1 ? B : A;
06489         return new ICmpInst(*Context, I.getPredicate(), OtherVal,
06490                             Constant::getNullValue(A->getType()));
06491       }
06492 
06493       if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
06494         // A^c1 == C^c2 --> A == C^(c1^c2)
06495         ConstantInt *C1, *C2;
06496         if (match(B, m_ConstantInt(C1)) &&
06497             match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
06498           Constant *NC = 
06499                    ConstantInt::get(*Context, C1->getValue() ^ C2->getValue());
06500           Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp");
06501           return new ICmpInst(*Context, I.getPredicate(), A,
06502                               InsertNewInstBefore(Xor, I));
06503         }
06504         
06505         // A^B == A^D -> B == D
06506         if (A == C) return new ICmpInst(*Context, I.getPredicate(), B, D);
06507         if (A == D) return new ICmpInst(*Context, I.getPredicate(), B, C);
06508         if (B == C) return new ICmpInst(*Context, I.getPredicate(), A, D);
06509         if (B == D) return new ICmpInst(*Context, I.getPredicate(), A, C);
06510       }
06511     }
06512     
06513     if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
06514         (A == Op0 || B == Op0)) {
06515       // A == (A^B)  ->  B == 0
06516       Value *OtherVal = A == Op0 ? B : A;
06517       return new ICmpInst(*Context, I.getPredicate(), OtherVal,
06518                           Constant::getNullValue(A->getType()));
06519     }
06520 
06521     // (A-B) == A  ->  B == 0
06522     if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B))))
06523       return new ICmpInst(*Context, I.getPredicate(), B, 
06524                           Constant::getNullValue(B->getType()));
06525 
06526     // A == (A-B)  ->  B == 0
06527     if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B))))
06528       return new ICmpInst(*Context, I.getPredicate(), B,
06529                           Constant::getNullValue(B->getType()));
06530     
06531     // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
06532     if (Op0->hasOneUse() && Op1->hasOneUse() &&
06533         match(Op0, m_And(m_Value(A), m_Value(B))) && 
06534         match(Op1, m_And(m_Value(C), m_Value(D)))) {
06535       Value *X = 0, *Y = 0, *Z = 0;
06536       
06537       if (A == C) {
06538         X = B; Y = D; Z = A;
06539       } else if (A == D) {
06540         X = B; Y = C; Z = A;
06541       } else if (B == C) {
06542         X = A; Y = D; Z = B;
06543       } else if (B == D) {
06544         X = A; Y = C; Z = B;
06545       }
06546       
06547       if (X) {   // Build (X^Y) & Z
06548         Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I);
06549         Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I);
06550         I.setOperand(0, Op1);
06551         I.setOperand(1, Constant::getNullValue(Op1->getType()));
06552         return &I;
06553       }
06554     }
06555   }
06556   return Changed ? &I : 0;
06557 }
06558 
06559 
06560 /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
06561 /// and CmpRHS are both known to be integer constants.
06562 Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
06563                                           ConstantInt *DivRHS) {
06564   ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
06565   const APInt &CmpRHSV = CmpRHS->getValue();
06566   
06567   // FIXME: If the operand types don't match the type of the divide 
06568   // then don't attempt this transform. The code below doesn't have the
06569   // logic to deal with a signed divide and an unsigned compare (and
06570   // vice versa). This is because (x /s C1) <s C2  produces different 
06571   // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
06572   // (x /u C1) <u C2.  Simply casting the operands and result won't 
06573   // work. :(  The if statement below tests that condition and bails 
06574   // if it finds it. 
06575   bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
06576   if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate())
06577     return 0;
06578   if (DivRHS->isZero())
06579     return 0; // The ProdOV computation fails on divide by zero.
06580   if (DivIsSigned && DivRHS->isAllOnesValue())
06581     return 0; // The overflow computation also screws up here
06582   if (DivRHS->isOne())
06583     return 0; // Not worth bothering, and eliminates some funny cases
06584               // with INT_MIN.
06585 
06586   // Compute Prod = CI * DivRHS. We are essentially solving an equation
06587   // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and 
06588   // C2 (CI). By solving for X we can turn this into a range check 
06589   // instead of computing a divide. 
06590   Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
06591 
06592   // Determine if the product overflows by seeing if the product is
06593   // not equal to the divide. Make sure we do the same kind of divide
06594   // as in the LHS instruction that we're folding. 
06595   bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
06596                  ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
06597 
06598   // Get the ICmp opcode
06599   ICmpInst::Predicate Pred = ICI.getPredicate();
06600 
06601   // Figure out the interval that is being checked.  For example, a comparison
06602   // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). 
06603   // Compute this interval based on the constants involved and the signedness of
06604   // the compare/divide.  This computes a half-open interval, keeping track of
06605   // whether either value in the interval overflows.  After analysis each
06606   // overflow variable is set to 0 if it's corresponding bound variable is valid
06607   // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
06608   int LoOverflow = 0, HiOverflow = 0;
06609   Constant *LoBound = 0, *HiBound = 0;
06610   
06611   if (!DivIsSigned) {  // udiv
06612     // e.g. X/5 op 3  --> [15, 20)
06613     LoBound = Prod;
06614     HiOverflow = LoOverflow = ProdOV;
06615     if (!HiOverflow)
06616       HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, Context, false);
06617   } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
06618     if (CmpRHSV == 0) {       // (X / pos) op 0
06619       // Can't overflow.  e.g.  X/2 op 0 --> [-1, 2)
06620       LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
06621       HiBound = DivRHS;
06622     } else if (CmpRHSV.isStrictlyPositive()) {   // (X / pos) op pos
06623       LoBound = Prod;     // e.g.   X/5 op 3 --> [15, 20)
06624       HiOverflow = LoOverflow = ProdOV;
06625       if (!HiOverflow)
06626         HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, Context, true);
06627     } else {                       // (X / pos) op neg
06628       // e.g. X/5 op -3  --> [-15-4, -15+1) --> [-19, -14)
06629       HiBound = AddOne(Prod);
06630       LoOverflow = HiOverflow = ProdOV ? -1 : 0;
06631       if (!LoOverflow) {
06632         ConstantInt* DivNeg =
06633                          cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
06634         LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, Context,
06635                                      true) ? -1 : 0;
06636        }
06637     }
06638   } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0.
06639     if (CmpRHSV == 0) {       // (X / neg) op 0
06640       // e.g. X/-5 op 0  --> [-4, 5)
06641       LoBound = AddOne(DivRHS);
06642       HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
06643       if (HiBound == DivRHS) {     // -INTMIN = INTMIN
06644         HiOverflow = 1;            // [INTMIN+1, overflow)
06645         HiBound = 0;               // e.g. X/INTMIN = 0 --> X > INTMIN
06646       }
06647     } else if (CmpRHSV.isStrictlyPositive()) {   // (X / neg) op pos
06648       // e.g. X/-5 op 3  --> [-19, -14)
06649       HiBound = AddOne(Prod);
06650       HiOverflow = LoOverflow = ProdOV ? -1 : 0;
06651       if (!LoOverflow)
06652         LoOverflow = AddWithOverflow(LoBound, HiBound,
06653                                      DivRHS, Context, true) ? -1 : 0;
06654     } else {                       // (X / neg) op neg
06655       LoBound = Prod;       // e.g. X/-5 op -3  --> [15, 20)
06656       LoOverflow = HiOverflow = ProdOV;
06657       if (!HiOverflow)
06658         HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, Context, true);
06659     }
06660     
06661     // Dividing by a negative swaps the condition.  LT <-> GT
06662     Pred = ICmpInst::getSwappedPredicate(Pred);
06663   }
06664 
06665   Value *X = DivI->getOperand(0);
06666   switch (Pred) {
06667   default: llvm_unreachable("Unhandled icmp opcode!");
06668   case ICmpInst::ICMP_EQ:
06669     if (LoOverflow && HiOverflow)
06670       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
06671     else if (HiOverflow)
06672       return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE : 
06673                           ICmpInst::ICMP_UGE, X, LoBound);
06674     else if (LoOverflow)
06675       return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT : 
06676                           ICmpInst::ICMP_ULT, X, HiBound);
06677     else
06678       return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI);
06679   case ICmpInst::ICMP_NE:
06680     if (LoOverflow && HiOverflow)
06681       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
06682     else if (HiOverflow)
06683       return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT : 
06684                           ICmpInst::ICMP_ULT, X, LoBound);
06685     else if (LoOverflow)
06686       return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE : 
06687                           ICmpInst::ICMP_UGE, X, HiBound);
06688     else
06689       return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI);
06690   case ICmpInst::ICMP_ULT:
06691   case ICmpInst::ICMP_SLT:
06692     if (LoOverflow == +1)   // Low bound is greater than input range.
06693       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
06694     if (LoOverflow == -1)   // Low bound is less than input range.
06695       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
06696     return new ICmpInst(*Context, Pred, X, LoBound);
06697   case ICmpInst::ICMP_UGT:
06698   case ICmpInst::ICMP_SGT:
06699     if (HiOverflow == +1)       // High bound greater than input range.
06700       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
06701     else if (HiOverflow == -1)  // High bound less than input range.
06702       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
06703     if (Pred == ICmpInst::ICMP_UGT)
06704       return new ICmpInst(*Context, ICmpInst::ICMP_UGE, X, HiBound);
06705     else
06706       return new ICmpInst(*Context, ICmpInst::ICMP_SGE, X, HiBound);
06707   }
06708 }
06709 
06710 
06711 /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
06712 ///
06713 Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
06714                                                           Instruction *LHSI,
06715                                                           ConstantInt *RHS) {
06716   const APInt &RHSV = RHS->getValue();
06717   
06718   switch (LHSI->getOpcode()) {
06719   case Instruction::Trunc:
06720     if (ICI.isEquality() && LHSI->hasOneUse()) {
06721       // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
06722       // of the high bits truncated out of x are known.
06723       unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
06724              SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
06725       APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits));
06726       APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
06727       ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne);
06728       
06729       // If all the high bits are known, we can do this xform.
06730       if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
06731         // Pull in the high bits from known-ones set.
06732         APInt NewRHS(RHS->getValue());
06733         NewRHS.zext(SrcBits);
06734         NewRHS |= KnownOne;
06735         return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
06736                             ConstantInt::get(*Context, NewRHS));
06737       }
06738     }
06739     break;
06740       
06741   case Instruction::Xor:         // (icmp pred (xor X, XorCST), CI)
06742     if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
06743       // If this is a comparison that tests the signbit (X < 0) or (x > -1),
06744       // fold the xor.
06745       if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
06746           (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
06747         Value *CompareVal = LHSI->getOperand(0);
06748         
06749         // If the sign bit of the XorCST is not set, there is no change to
06750         // the operation, just stop using the Xor.
06751         if (!XorCST->getValue().isNegative()) {
06752           ICI.setOperand(0, CompareVal);
06753           AddToWorkList(LHSI);
06754           return &ICI;
06755         }
06756         
06757         // Was the old condition true if the operand is positive?
06758         bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
06759         
06760         // If so, the new one isn't.
06761         isTrueIfPositive ^= true;
06762         
06763         if (isTrueIfPositive)
06764           return new ICmpInst(*Context, ICmpInst::ICMP_SGT, CompareVal,
06765                               SubOne(RHS));
06766         else
06767           return new ICmpInst(*Context, ICmpInst::ICMP_SLT, CompareVal,
06768                               AddOne(RHS));
06769       }
06770 
06771       if (LHSI->hasOneUse()) {
06772         // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
06773         if (!ICI.isEquality() && XorCST->getValue().isSignBit()) {
06774           const APInt &SignBit = XorCST->getValue();
06775           ICmpInst::Predicate Pred = ICI.isSignedPredicate()
06776                                          ? ICI.getUnsignedPredicate()
06777                                          : ICI.getSignedPredicate();
06778           return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
06779                               ConstantInt::get(*Context, RHSV ^ SignBit));
06780         }
06781 
06782         // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
06783         if (!ICI.isEquality() && XorCST->getValue().isMaxSignedValue()) {
06784           const APInt &NotSignBit = XorCST->getValue();
06785           ICmpInst::Predicate Pred = ICI.isSignedPredicate()
06786                                          ? ICI.getUnsignedPredicate()
06787                                          : ICI.getSignedPredicate();
06788           Pred = ICI.getSwappedPredicate(Pred);
06789           return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
06790                               ConstantInt::get(*Context, RHSV ^ NotSignBit));
06791         }
06792       }
06793     }
06794     break;
06795   case Instruction::And:         // (icmp pred (and X, AndCST), RHS)
06796     if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
06797         LHSI->getOperand(0)->hasOneUse()) {
06798       ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
06799       
06800       // If the LHS is an AND of a truncating cast, we can widen the
06801       // and/compare to be the input width without changing the value
06802       // produced, eliminating a cast.
06803       if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
06804         // We can do this transformation if either the AND constant does not
06805         // have its sign bit set or if it is an equality comparison. 
06806         // Extending a relational comparison when we're checking the sign
06807         // bit would not work.
06808         if (Cast->hasOneUse() &&
06809             (ICI.isEquality() ||
06810              (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) {
06811           uint32_t BitWidth = 
06812             cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth();
06813           APInt NewCST = AndCST->getValue();
06814           NewCST.zext(BitWidth);
06815           APInt NewCI = RHSV;
06816           NewCI.zext(BitWidth);
06817           Instruction *NewAnd = 
06818             BinaryOperator::CreateAnd(Cast->getOperand(0),
06819                            ConstantInt::get(*Context, NewCST), LHSI->getName());
06820           InsertNewInstBefore(NewAnd, ICI);
06821           return new ICmpInst(*Context, ICI.getPredicate(), NewAnd,
06822                               ConstantInt::get(*Context, NewCI));
06823         }
06824       }
06825       
06826       // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
06827       // could exist), turn it into (X & (C2 << C1)) != (C3 << C1).  This
06828       // happens a LOT in code produced by the C front-end, for bitfield
06829       // access.
06830       BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
06831       if (Shift && !Shift->isShift())
06832         Shift = 0;
06833       
06834       ConstantInt *ShAmt;
06835       ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0;
06836       const Type *Ty = Shift ? Shift->getType() : 0;  // Type of the shift.
06837       const Type *AndTy = AndCST->getType();          // Type of the and.
06838       
06839       // We can fold this as long as we can't shift unknown bits
06840       // into the mask.  This can only happen with signed shift
06841       // rights, as they sign-extend.
06842       if (ShAmt) {
06843         bool CanFold = Shift->isLogicalShift();
06844         if (!CanFold) {
06845           // To test for the bad case of the signed shr, see if any
06846           // of the bits shifted in could be tested after the mask.
06847           uint32_t TyBits = Ty->getPrimitiveSizeInBits();
06848           int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits);
06849           
06850           uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
06851           if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & 
06852                AndCST->getValue()) == 0)
06853             CanFold = true;
06854         }
06855         
06856         if (CanFold) {
06857           Constant *NewCst;
06858           if (Shift->getOpcode() == Instruction::Shl)
06859             NewCst = ConstantExpr::getLShr(RHS, ShAmt);
06860           else
06861             NewCst = ConstantExpr::getShl(RHS, ShAmt);
06862           
06863           // Check to see if we are shifting out any of the bits being
06864           // compared.
06865           if (ConstantExpr::get(Shift->getOpcode(),
06866                                        NewCst, ShAmt) != RHS) {
06867             // If we shifted bits out, the fold is not going to work out.
06868             // As a special case, check to see if this means that the
06869             // result is always true or false now.
06870             if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
06871               return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
06872             if (ICI.getPredicate() == ICmpInst::ICMP_NE)
06873               return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
06874           } else {
06875             ICI.setOperand(1, NewCst);
06876             Constant *NewAndCST;
06877             if (Shift->getOpcode() == Instruction::Shl)
06878               NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
06879             else
06880               NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
06881             LHSI->setOperand(1, NewAndCST);
06882             LHSI->setOperand(0, Shift->getOperand(0));
06883             AddToWorkList(Shift); // Shift is dead.
06884             AddUsesToWorkList(ICI);
06885             return &ICI;
06886           }
06887         }
06888       }
06889       
06890       // Turn ((X >> Y) & C) == 0  into  (X & (C << Y)) == 0.  The later is
06891       // preferable because it allows the C<<Y expression to be hoisted out
06892       // of a loop if Y is invariant and X is not.
06893       if (Shift && Shift->hasOneUse() && RHSV == 0 &&
06894           ICI.isEquality() && !Shift->isArithmeticShift() &&
06895           !isa<Constant>(Shift->getOperand(0))) {
06896         // Compute C << Y.
06897         Value *NS;
06898         if (Shift->getOpcode() == Instruction::LShr) {
06899           NS = BinaryOperator::CreateShl(AndCST, 
06900                                          Shift->getOperand(1), "tmp");
06901         } else {
06902           // Insert a logical shift.
06903           NS = BinaryOperator::CreateLShr(AndCST,
06904                                           Shift->getOperand(1), "tmp");
06905         }
06906         InsertNewInstBefore(cast<Instruction>(NS), ICI);
06907         
06908         // Compute X & (C << Y).
06909         Instruction *NewAnd = 
06910           BinaryOperator::CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
06911         InsertNewInstBefore(NewAnd, ICI);
06912         
06913         ICI.setOperand(0, NewAnd);
06914         return &ICI;
06915       }
06916     }
06917     break;
06918     
06919   case Instruction::Shl: {       // (icmp pred (shl X, ShAmt), CI)
06920     ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
06921     if (!ShAmt) break;
06922     
06923     uint32_t TypeBits = RHSV.getBitWidth();
06924     
06925     // Check that the shift amount is in range.  If not, don't perform
06926     // undefined shifts.  When the shift is visited it will be
06927     // simplified.
06928     if (ShAmt->uge(TypeBits))
06929       break;
06930     
06931     if (ICI.isEquality()) {
06932       // If we are comparing against bits always shifted out, the
06933       // comparison cannot succeed.
06934       Constant *Comp =
06935         ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt),
06936                                                                  ShAmt);
06937       if (Comp != RHS) {// Comparing against a bit that we know is zero.
06938         bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
06939         Constant *Cst = ConstantInt::get(Type::getInt1Ty(*Context), IsICMP_NE);
06940         return ReplaceInstUsesWith(ICI, Cst);
06941       }
06942       
06943       if (LHSI->hasOneUse()) {
06944         // Otherwise strength reduce the shift into an and.
06945         uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
06946         Constant *Mask =
06947           ConstantInt::get(*Context, APInt::getLowBitsSet(TypeBits, 
06948                                                        TypeBits-ShAmtVal));
06949         
06950         Instruction *AndI =
06951           BinaryOperator::CreateAnd(LHSI->getOperand(0),
06952                                     Mask, LHSI->getName()+".mask");
06953         Value *And = InsertNewInstBefore(AndI, ICI);
06954         return new ICmpInst(*Context, ICI.getPredicate(), And,
06955                             ConstantInt::get(*Context, RHSV.lshr(ShAmtVal)));
06956       }
06957     }
06958     
06959     // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
06960     bool TrueIfSigned = false;
06961     if (LHSI->hasOneUse() &&
06962         isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
06963       // (X << 31) <s 0  --> (X&1) != 0
06964       Constant *Mask = ConstantInt::get(*Context, APInt(TypeBits, 1) <<
06965                                            (TypeBits-ShAmt->getZExtValue()-1));
06966       Instruction *AndI =
06967         BinaryOperator::CreateAnd(LHSI->getOperand(0),
06968                                   Mask, LHSI->getName()+".mask");
06969       Value *And = InsertNewInstBefore(AndI, ICI);
06970       
06971       return new ICmpInst(*Context,
06972                           TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
06973                           And, Constant::getNullValue(And->getType()));
06974     }
06975     break;
06976   }
06977     
06978   case Instruction::LShr:         // (icmp pred (shr X, ShAmt), CI)
06979   case Instruction::AShr: {
06980     // Only handle equality comparisons of shift-by-constant.
06981     ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
06982     if (!ShAmt || !ICI.isEquality()) break;
06983 
06984     // Check that the shift amount is in range.  If not, don't perform
06985     // undefined shifts.  When the shift is visited it will be
06986     // simplified.
06987     uint32_t TypeBits = RHSV.getBitWidth();
06988     if (ShAmt->uge(TypeBits))
06989       break;
06990     
06991     uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
06992       
06993     // If we are comparing against bits always shifted out, the
06994     // comparison cannot succeed.
06995     APInt Comp = RHSV << ShAmtVal;
06996     if (LHSI->getOpcode() == Instruction::LShr)
06997       Comp = Comp.lshr(ShAmtVal);
06998     else
06999       Comp = Comp.ashr(ShAmtVal);
07000     
07001     if (Comp != RHSV) { // Comparing against a bit that we know is zero.
07002       bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
07003       Constant *Cst = ConstantInt::get(Type::getInt1Ty(*Context), IsICMP_NE);
07004       return ReplaceInstUsesWith(ICI, Cst);
07005     }
07006     
07007     // Otherwise, check to see if the bits shifted out are known to be zero.
07008     // If so, we can compare against the unshifted value:
07009     //  (X & 4) >> 1 == 2  --> (X & 4) == 4.
07010     if (LHSI->hasOneUse() &&
07011         MaskedValueIsZero(LHSI->getOperand(0), 
07012                           APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) {
07013       return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
07014                           ConstantExpr::getShl(RHS, ShAmt));
07015     }
07016       
07017     if (LHSI->hasOneUse()) {
07018       // Otherwise strength reduce the shift into an and.
07019       APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
07020       Constant *Mask = ConstantInt::get(*Context, Val);
07021       
07022       Instruction *AndI =
07023         BinaryOperator::CreateAnd(LHSI->getOperand(0),
07024                                   Mask, LHSI->getName()+".mask");
07025       Value *And = InsertNewInstBefore(AndI, ICI);
07026       return new ICmpInst(*Context, ICI.getPredicate(), And,
07027                           ConstantExpr::getShl(RHS, ShAmt));
07028     }
07029     break;
07030   }
07031     
07032   case Instruction::SDiv:
07033   case Instruction::UDiv:
07034     // Fold: icmp pred ([us]div X, C1), C2 -> range test
07035     // Fold this div into the comparison, producing a range check. 
07036     // Determine, based on the divide type, what the range is being 
07037     // checked.  If there is an overflow on the low or high side, remember 
07038     // it, otherwise compute the range [low, hi) bounding the new value.
07039     // See: InsertRangeTest above for the kinds of replacements possible.
07040     if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
07041       if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
07042                                           DivRHS))
07043         return R;
07044     break;
07045 
07046   case Instruction::Add:
07047     // Fold: icmp pred (add, X, C1), C2
07048 
07049     if (!ICI.isEquality()) {
07050       ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
07051       if (!LHSC) break;
07052       const APInt &LHSV = LHSC->getValue();
07053 
07054       ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
07055                             .subtract(LHSV);
07056 
07057       if (ICI.isSignedPredicate()) {
07058         if (CR.getLower().isSignBit()) {
07059           return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHSI->getOperand(0),
07060                               ConstantInt::get(*Context, CR.getUpper()));
07061         } else if (CR.getUpper().isSignBit()) {
07062           return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHSI->getOperand(0),
07063                               ConstantInt::get(*Context, CR.getLower()));
07064         }
07065       } else {
07066         if (CR.getLower().isMinValue()) {
07067           return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHSI->getOperand(0),
07068                               ConstantInt::get(*Context, CR.getUpper()));
07069         } else if (CR.getUpper().isMinValue()) {
07070           return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHSI->getOperand(0),
07071                               ConstantInt::get(*Context, CR.getLower()));
07072         }
07073       }
07074     }
07075     break;
07076   }
07077   
07078   // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
07079   if (ICI.isEquality()) {
07080     bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
07081     
07082     // If the first operand is (add|sub|and|or|xor|rem) with a constant, and 
07083     // the second operand is a constant, simplify a bit.
07084     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
07085       switch (BO->getOpcode()) {
07086       case Instruction::SRem:
07087         // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
07088         if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
07089           const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
07090           if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) {
07091             Instruction *NewRem =
07092               BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1),
07093                                          BO->getName());
07094             InsertNewInstBefore(NewRem, ICI);
07095             return new ICmpInst(*Context, ICI.getPredicate(), NewRem, 
07096                                 Constant::getNullValue(BO->getType()));
07097           }
07098         }
07099         break;
07100       case Instruction::Add:
07101         // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
07102         if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
07103           if (BO->hasOneUse())
07104             return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
07105                                 ConstantExpr::getSub(RHS, BOp1C));
07106         } else if (RHSV == 0) {
07107           // Replace ((add A, B) != 0) with (A != -B) if A or B is
07108           // efficiently invertible, or if the add has just this one use.
07109           Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
07110           
07111           if (Value *NegVal = dyn_castNegVal(BOp1))
07112             return new ICmpInst(*Context, ICI.getPredicate(), BOp0, NegVal);
07113           else if (Value *NegVal = dyn_castNegVal(BOp0))
07114             return new ICmpInst(*Context, ICI.getPredicate(), NegVal, BOp1);
07115           else if (BO->hasOneUse()) {
07116             Instruction *Neg = BinaryOperator::CreateNeg(BOp1);
07117             InsertNewInstBefore(Neg, ICI);
07118             Neg->takeName(BO);
07119             return new ICmpInst(*Context, ICI.getPredicate(), BOp0, Neg);
07120           }
07121         }
07122         break;
07123       case Instruction::Xor:
07124         // For the xor case, we can xor two constants together, eliminating
07125         // the explicit xor.
07126         if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
07127           return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0), 
07128                               ConstantExpr::getXor(RHS, BOC));
07129         
07130         // FALLTHROUGH
07131       case Instruction::Sub:
07132         // Replace (([sub|xor] A, B) != 0) with (A != B)
07133         if (RHSV == 0)
07134           return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
07135                               BO->getOperand(1));
07136         break;
07137         
07138       case Instruction::Or:
07139         // If bits are being or'd in that are not present in the constant we
07140         // are comparing against, then the comparison could never succeed!
07141         if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
07142           Constant *NotCI = ConstantExpr::getNot(RHS);
07143           if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
07144             return ReplaceInstUsesWith(ICI,
07145                                        ConstantInt::get(Type::getInt1Ty(*Context), 
07146                                        isICMP_NE));
07147         }
07148         break;
07149         
07150       case Instruction::And:
07151         if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
07152           // If bits are being compared against that are and'd out, then the
07153           // comparison can never succeed!
07154           if ((RHSV & ~BOC->getValue()) != 0)
07155             return ReplaceInstUsesWith(ICI,
07156                                        ConstantInt::get(Type::getInt1Ty(*Context),
07157                                        isICMP_NE));
07158           
07159           // If we have ((X & C) == C), turn it into ((X & C) != 0).
07160           if (RHS == BOC && RHSV.isPowerOf2())
07161             return new ICmpInst(*Context, isICMP_NE ? ICmpInst::ICMP_EQ :
07162                                 ICmpInst::ICMP_NE, LHSI,
07163                                 Constant::getNullValue(RHS->getType()));
07164           
07165           // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
07166           if (BOC->getValue().isSignBit()) {
07167             Value *X = BO->getOperand(0);
07168             Constant *Zero = Constant::getNullValue(X->getType());
07169             ICmpInst::Predicate pred = isICMP_NE ? 
07170               ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
07171             return new ICmpInst(*Context, pred, X, Zero);
07172           }
07173           
07174           // ((X & ~7) == 0) --> X < 8
07175           if (RHSV == 0 && isHighOnes(BOC)) {
07176             Value *X = BO->getOperand(0);
07177             Constant *NegX = ConstantExpr::getNeg(BOC);
07178             ICmpInst::Predicate pred = isICMP_NE ? 
07179               ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
07180             return new ICmpInst(*Context, pred, X, NegX);
07181           }
07182         }
07183       default: break;
07184       }
07185     } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
07186       // Handle icmp {eq|ne} <intrinsic>, intcst.
07187       if (II->getIntrinsicID() == Intrinsic::bswap) {
07188         AddToWorkList(II);
07189         ICI.setOperand(0, II->getOperand(1));
07190         ICI.setOperand(1, ConstantInt::get(*Context, RHSV.byteSwap()));
07191         return &ICI;
07192       }
07193     }
07194   }
07195   return 0;
07196 }
07197 
07198 /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
07199 /// We only handle extending casts so far.
07200 ///
07201 Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
07202   const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
07203   Value *LHSCIOp        = LHSCI->getOperand(0);
07204   const Type *SrcTy     = LHSCIOp->getType();
07205   const Type *DestTy    = LHSCI->getType();
07206   Value *RHSCIOp;
07207 
07208   // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the 
07209   // integer type is the same size as the pointer type.
07210   if (TD && LHSCI->getOpcode() == Instruction::PtrToInt &&
07211       TD->getPointerSizeInBits() ==
07212          cast<IntegerType>(DestTy)->getBitWidth()) {
07213     Value *RHSOp = 0;
07214     if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
07215       RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
07216     } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
07217       RHSOp = RHSC->getOperand(0);
07218       // If the pointer types don't match, insert a bitcast.
07219       if (LHSCIOp->getType() != RHSOp->getType())
07220         RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI);
07221     }
07222 
07223     if (RHSOp)
07224       return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSOp);
07225   }
07226   
07227   // The code below only handles extension cast instructions, so far.
07228   // Enforce this.
07229   if (LHSCI->getOpcode() != Instruction::ZExt &&
07230       LHSCI->getOpcode() != Instruction::SExt)
07231     return 0;
07232 
07233   bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
07234   bool isSignedCmp = ICI.isSignedPredicate();
07235 
07236   if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
07237     // Not an extension from the same type?
07238     RHSCIOp = CI->getOperand(0);
07239     if (RHSCIOp->getType() != LHSCIOp->getType()) 
07240       return 0;
07241     
07242     // If the signedness of the two casts doesn't agree (i.e. one is a sext
07243     // and the other is a zext), then we can't handle this.
07244     if (CI->getOpcode() != LHSCI->getOpcode())
07245       return 0;
07246 
07247     // Deal with equality cases early.
07248     if (ICI.isEquality())
07249       return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp);
07250 
07251     // A signed comparison of sign extended values simplifies into a
07252     // signed comparison.
07253     if (isSignedCmp && isSignedExt)
07254       return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp);
07255 
07256     // The other three cases all fold into an unsigned comparison.
07257     return new ICmpInst(*Context, ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
07258   }
07259 
07260   // If we aren't dealing with a constant on the RHS, exit early
07261   ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
07262   if (!CI)
07263     return 0;
07264 
07265   // Compute the constant that would happen if we truncated to SrcTy then
07266   // reextended to DestTy.
07267   Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
07268   Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(),
07269                                                 Res1, DestTy);
07270 
07271   // If the re-extended constant didn't change...
07272   if (Res2 == CI) {
07273     // Make sure that sign of the Cmp and the sign of the Cast are the same.
07274     // For example, we might have:
07275     //    %A = sext i16 %X to i32
07276     //    %B = icmp ugt i32 %A, 1330
07277     // It is incorrect to transform this into 
07278     //    %B = icmp ugt i16 %X, 1330
07279     // because %A may have negative value. 
07280     //
07281     // However, we allow this when the compare is EQ/NE, because they are
07282     // signless.
07283     if (isSignedExt == isSignedCmp || ICI.isEquality())
07284       return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, Res1);
07285     return 0;
07286   }
07287 
07288   // The re-extended constant changed so the constant cannot be represented 
07289   // in the shorter type. Consequently, we cannot emit a simple comparison.
07290 
07291   // First, handle some easy cases. We know the result cannot be equal at this
07292   // point so handle the ICI.isEquality() cases
07293   if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
07294     return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
07295   if (ICI.getPredicate() == ICmpInst::ICMP_NE)
07296     return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
07297 
07298   // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
07299   // should have been folded away previously and not enter in here.
07300   Value *Result;
07301   if (isSignedCmp) {
07302     // We're performing a signed comparison.
07303     if (cast<ConstantInt>(CI)->getValue().isNegative())
07304       Result = ConstantInt::getFalse(*Context);          // X < (small) --> false
07305     else
07306       Result = ConstantInt::getTrue(*Context);           // X < (large) --> true
07307   } else {
07308     // We're performing an unsigned comparison.
07309     if (isSignedExt) {
07310       // We're performing an unsigned comp with a sign extended value.
07311       // This is true if the input is >= 0. [aka >s -1]
07312       Constant *NegOne = Constant::getAllOnesValue(SrcTy);
07313       Result = InsertNewInstBefore(new ICmpInst(*Context, ICmpInst::ICMP_SGT, 
07314                                    LHSCIOp, NegOne, ICI.getName()), ICI);
07315     } else {
07316       // Unsigned extend & unsigned compare -> always true.
07317       Result = ConstantInt::getTrue(*Context);
07318     }
07319   }
07320 
07321   // Finally, return the value computed.
07322   if (ICI.getPredicate() == ICmpInst::ICMP_ULT ||
07323       ICI.getPredicate() == ICmpInst::ICMP_SLT)
07324     return ReplaceInstUsesWith(ICI, Result);
07325 
07326   assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || 
07327           ICI.getPredicate()==ICmpInst::ICMP_SGT) &&
07328          "ICmp should be folded!");
07329   if (Constant *CI = dyn_cast<Constant>(Result))
07330     return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
07331   return BinaryOperator::CreateNot(Result);
07332 }
07333 
07334 Instruction *InstCombiner::visitShl(BinaryOperator &I) {
07335   return commonShiftTransforms(I);
07336 }
07337 
07338 Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
07339   return commonShiftTransforms(I);
07340 }
07341 
07342 Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
07343   if (Instruction *R = commonShiftTransforms(I))
07344     return R;
07345   
07346   Value *Op0 = I.getOperand(0);
07347   
07348   // ashr int -1, X = -1   (for any arithmetic shift rights of ~0)
07349   if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
07350     if (CSI->isAllOnesValue())
07351       return ReplaceInstUsesWith(I, CSI);
07352 
07353   // See if we can turn a signed shr into an unsigned shr.
07354   if (MaskedValueIsZero(Op0,
07355                         APInt::getSignBit(I.getType()->getScalarSizeInBits())))
07356     return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
07357 
07358   // Arithmetic shifting an all-sign-bit value is a no-op.
07359   unsigned NumSignBits = ComputeNumSignBits(Op0);
07360   if (NumSignBits == Op0->getType()->getScalarSizeInBits())
07361     return ReplaceInstUsesWith(I, Op0);
07362 
07363   return 0;
07364 }
07365 
07366 Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
07367   assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
07368   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
07369 
07370   // shl X, 0 == X and shr X, 0 == X
07371   // shl 0, X == 0 and shr 0, X == 0
07372   if (Op1 == Constant::getNullValue(Op1->getType()) ||
07373       Op0 == Constant::getNullValue(Op0->getType()))
07374     return ReplaceInstUsesWith(I, Op0);
07375   
07376   if (isa<UndefValue>(Op0)) {            
07377     if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
07378       return ReplaceInstUsesWith(I, Op0);
07379     else                                    // undef << X -> 0, undef >>u X -> 0
07380       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
07381   }
07382   if (isa<UndefValue>(Op1)) {
07383     if (I.getOpcode() == Instruction::AShr)  // X >>s undef -> X
07384       return ReplaceInstUsesWith(I, Op0);          
07385     else                                     // X << undef, X >>u undef -> 0
07386       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
07387   }
07388 
07389   // See if we can fold away this shift.
07390   if (SimplifyDemandedInstructionBits(I))
07391     return &I;
07392 
07393   // Try to fold constant and into select arguments.
07394   if (isa<Constant>(Op0))
07395     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
07396       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
07397         return R;
07398 
07399   if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
07400     if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
07401       return Res;
07402   return 0;
07403 }
07404 
07405 Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
07406                                                BinaryOperator &I) {
07407   bool isLeftShift = I.getOpcode() == Instruction::Shl;
07408 
07409   // See if we can simplify any instructions used by the instruction whose sole 
07410   // purpose is to compute bits we don't care about.
07411   uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
07412   
07413   // shl i32 X, 32 = 0 and srl i8 Y, 9 = 0, ... just don't eliminate
07414   // a signed shift.
07415   //
07416   if (Op1->uge(TypeBits)) {
07417     if (I.getOpcode() != Instruction::AShr)
07418       return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
07419     else {
07420       I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
07421       return &I;
07422     }
07423   }
07424   
07425   // ((X*C1) << C2) == (X * (C1 << C2))
07426   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
07427     if (BO->getOpcode() == Instruction::Mul && isLeftShift)
07428       if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
07429         return BinaryOperator::CreateMul(BO->getOperand(0),
07430                                         ConstantExpr::getShl(BOOp, Op1));
07431   
07432   // Try to fold constant and into select arguments.
07433   if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
07434     if (Instruction *R = FoldOpIntoSelect(I, SI, this))
07435       return R;
07436   if (isa<PHINode>(Op0))
07437     if (Instruction *NV = FoldOpIntoPhi(I))
07438       return NV;
07439   
07440   // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
07441   if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
07442     Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
07443     // If 'shift2' is an ashr, we would have to get the sign bit into a funny
07444     // place.  Don't try to do this transformation in this case.  Also, we
07445     // require that the input operand is a shift-by-constant so that we have
07446     // confidence that the shifts will get folded together.  We could do this
07447     // xform in more cases, but it is unlikely to be profitable.
07448     if (TrOp && I.isLogicalShift() && TrOp->isShift() && 
07449         isa<ConstantInt>(TrOp->getOperand(1))) {
07450       // Okay, we'll do this xform.  Make the shift of shift.
07451       Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
07452       Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt,
07453                                                 I.getName());
07454       InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2)
07455 
07456       // For logical shifts, the truncation has the effect of making the high
07457       // part of the register be zeros.  Emulate this by inserting an AND to
07458       // clear the top bits as needed.  This 'and' will usually be zapped by
07459       // other xforms later if dead.
07460       unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
07461       unsigned DstSize = TI->getType()->getScalarSizeInBits();
07462       APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
07463       
07464       // The mask we constructed says what the trunc would do if occurring
07465       // between the shifts.  We want to know the effect *after* the second
07466       // shift.  We know that it is a logical shift by a constant, so adjust the
07467       // mask as appropriate.
07468       if (I.getOpcode() == Instruction::Shl)
07469         MaskV <<= Op1->getZExtValue();
07470       else {
07471         assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
07472         MaskV = MaskV.lshr(Op1->getZExtValue());
07473       }
07474 
07475       Instruction *And =
07476         BinaryOperator::CreateAnd(NSh, ConstantInt::get(*Context, MaskV), 
07477                                   TI->getName());
07478       InsertNewInstBefore(And, I); // shift1 & 0x00FF
07479 
07480       // Return the value truncated to the interesting size.
07481       return new TruncInst(And, I.getType());
07482     }
07483   }
07484   
07485   if (Op0->hasOneUse()) {
07486     if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
07487       // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
07488       Value *V1, *V2;
07489       ConstantInt *CC;
07490       switch (Op0BO->getOpcode()) {
07491         default: break;
07492         case Instruction::Add:
07493         case Instruction::And:
07494         case Instruction::Or:
07495         case Instruction::Xor: {
07496           // These operators commute.
07497           // Turn (Y + (X >> C)) << C  ->  (X + (Y << C)) & (~0 << C)
07498           if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
07499               match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
07500                     m_Specific(Op1)))){
07501             Instruction *YS = BinaryOperator::CreateShl(
07502                                             Op0BO->getOperand(0), Op1,
07503                                             Op0BO->getName());
07504             InsertNewInstBefore(YS, I); // (Y << C)
07505             Instruction *X = 
07506               BinaryOperator::Create(Op0BO->getOpcode(), YS, V1,
07507                                      Op0BO->getOperand(1)->getName());
07508             InsertNewInstBefore(X, I);  // (X + (Y << C))
07509             uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
07510             return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
07511                        APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
07512           }
07513           
07514           // Turn (Y + ((X >> C) & CC)) << C  ->  ((X & (CC << C)) + (Y << C))
07515           Value *Op0BOOp1 = Op0BO->getOperand(1);
07516           if (isLeftShift && Op0BOOp1->hasOneUse() &&
07517               match(Op0BOOp1, 
07518                     m_And(m_Shr(m_Value(V1), m_Specific(Op1)),
07519                           m_ConstantInt(CC))) &&
07520               cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) {
07521             Instruction *YS = BinaryOperator::CreateShl(
07522                                                      Op0BO->getOperand(0), Op1,
07523                                                      Op0BO->getName());
07524             InsertNewInstBefore(YS, I); // (Y << C)
07525             Instruction *XM =
07526               BinaryOperator::CreateAnd(V1,
07527                                         ConstantExpr::getShl(CC, Op1),
07528                                         V1->getName()+".mask");
07529             InsertNewInstBefore(XM, I); // X & (CC << C)
07530             
07531             return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
07532           }
07533         }
07534           
07535         // FALL THROUGH.
07536         case Instruction::Sub: {
07537           // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
07538           if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
07539               match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
07540                     m_Specific(Op1)))) {
07541             Instruction *YS = BinaryOperator::CreateShl(
07542                                                      Op0BO->getOperand(1), Op1,
07543                                                      Op0BO->getName());
07544             InsertNewInstBefore(YS, I); // (Y << C)
07545             Instruction *X =
07546               BinaryOperator::Create(Op0BO->getOpcode(), V1, YS,
07547                                      Op0BO->getOperand(0)->getName());
07548             InsertNewInstBefore(X, I);  // (X + (Y << C))
07549             uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
07550             return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
07551                        APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
07552           }
07553           
07554           // Turn (((X >> C)&CC) + Y) << C  ->  (X + (Y << C)) & (CC << C)
07555           if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
07556               match(Op0BO->getOperand(0),
07557                     m_And(m_Shr(m_Value(V1), m_Value(V2)),
07558                           m_ConstantInt(CC))) && V2 == Op1 &&
07559               cast<BinaryOperator>(Op0BO->getOperand(0))
07560                   ->getOperand(0)->hasOneUse()) {
07561             Instruction *YS = BinaryOperator::CreateShl(
07562                                                      Op0BO->getOperand(1), Op1,
07563                                                      Op0BO->getName());
07564             InsertNewInstBefore(YS, I); // (Y << C)
07565             Instruction *XM =
07566               BinaryOperator::CreateAnd(V1, 
07567                                         ConstantExpr::getShl(CC, Op1),
07568                                         V1->getName()+".mask");
07569             InsertNewInstBefore(XM, I); // X & (CC << C)
07570             
07571             return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
07572           }
07573           
07574           break;
07575         }
07576       }
07577       
07578       
07579       // If the operand is an bitwise operator with a constant RHS, and the
07580       // shift is the only use, we can pull it out of the shift.
07581       if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
07582         bool isValid = true;     // Valid only for And, Or, Xor
07583         bool highBitSet = false; // Transform if high bit of constant set?
07584         
07585         switch (Op0BO->getOpcode()) {
07586           default: isValid = false; break;   // Do not perform transform!
07587           case Instruction::Add:
07588             isValid = isLeftShift;
07589             break;
07590           case Instruction::Or:
07591           case Instruction::Xor:
07592             highBitSet = false;
07593             break;
07594           case Instruction::And:
07595             highBitSet = true;
07596             break;
07597         }
07598         
07599         // If this is a signed shift right, and the high bit is modified
07600         // by the logical operation, do not perform the transformation.
07601         // The highBitSet boolean indicates the value of the high bit of
07602         // the constant which would cause it to be modified for this
07603         // operation.
07604         //
07605         if (isValid && I.getOpcode() == Instruction::AShr)
07606           isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
07607         
07608         if (isValid) {
07609           Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
07610           
07611           Instruction *NewShift =
07612             BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1);
07613           InsertNewInstBefore(NewShift, I);
07614           NewShift->takeName(Op0BO);
07615           
07616           return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
07617                                         NewRHS);
07618         }
07619       }
07620     }
07621   }
07622   
07623   // Find out if this is a shift of a shift by a constant.
07624   BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
07625   if (ShiftOp && !ShiftOp->isShift())
07626     ShiftOp = 0;
07627   
07628   if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
07629     ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
07630     uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
07631     uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits);
07632     assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
07633     if (ShiftAmt1 == 0) return 0;  // Will be simplified in the future.
07634     Value *X = ShiftOp->getOperand(0);
07635     
07636     uint32_t AmtSum = ShiftAmt1+ShiftAmt2;   // Fold into one big shift.
07637     
07638     const IntegerType *Ty = cast<IntegerType>(I.getType());
07639     
07640     // Check for (X << c1) << c2  and  (X >> c1) >> c2
07641     if (I.getOpcode() == ShiftOp->getOpcode()) {
07642       // If this is oversized composite shift, then unsigned shifts get 0, ashr
07643       // saturates.
07644       if (AmtSum >= TypeBits) {
07645         if (I.getOpcode() != Instruction::AShr)
07646           return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
07647         AmtSum = TypeBits-1;  // Saturate to 31 for i32 ashr.
07648       }
07649       
07650       return BinaryOperator::Create(I.getOpcode(), X,
07651                                     ConstantInt::get(Ty, AmtSum));
07652     } else if (ShiftOp->getOpcode() == Instruction::LShr &&
07653                I.getOpcode() == Instruction::AShr) {
07654       if (AmtSum >= TypeBits)
07655         return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
07656       
07657       // ((X >>u C1) >>s C2) -> (X >>u (C1+C2))  since C1 != 0.
07658       return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
07659     } else if (ShiftOp->getOpcode() == Instruction::AShr &&
07660                I.getOpcode() == Instruction::LShr) {
07661       // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
07662       if (AmtSum >= TypeBits)
07663         AmtSum = TypeBits-1;
07664       
07665       Instruction *Shift =
07666         BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
07667       InsertNewInstBefore(Shift, I);
07668 
07669       APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
07670       return BinaryOperator::CreateAnd(Shift, ConstantInt::get(*Context, Mask));
07671     }
07672     
07673     // Okay, if we get here, one shift must be left, and the other shift must be
07674     // right.  See if the amounts are equal.
07675     if (ShiftAmt1 == ShiftAmt2) {
07676       // If we have ((X >>? C) << C), turn this into X & (-1 << C).
07677       if (I.getOpcode() == Instruction::Shl) {
07678         APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
07679         return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
07680       }
07681       // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
07682       if (I.getOpcode() == Instruction::LShr) {
07683         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
07684         return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
07685       }
07686       // We can simplify ((X << C) >>s C) into a trunc + sext.
07687       // NOTE: we could do this for any C, but that would make 'unusual' integer
07688       // types.  For now, just stick to ones well-supported by the code
07689       // generators.
07690       const Type *SExtType = 0;
07691       switch (Ty->getBitWidth() - ShiftAmt1) {
07692       case 1  :
07693       case 8  :
07694       case 16 :
07695       case 32 :
07696       case 64 :
07697       case 128:
07698         SExtType = IntegerType::get(*Context, Ty->getBitWidth() - ShiftAmt1);
07699         break;
07700       default: break;
07701       }
07702       if (SExtType) {
07703         Instruction *NewTrunc = new TruncInst(X, SExtType, "sext");
07704         InsertNewInstBefore(NewTrunc, I);
07705         return new SExtInst(NewTrunc, Ty);
07706       }
07707       // Otherwise, we can't handle it yet.
07708     } else if (ShiftAmt1 < ShiftAmt2) {
07709       uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
07710       
07711       // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2)
07712       if (I.getOpcode() == Instruction::Shl) {
07713         assert(ShiftOp->getOpcode() == Instruction::LShr ||
07714                ShiftOp->getOpcode() == Instruction::AShr);
07715         Instruction *Shift =
07716           BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
07717         InsertNewInstBefore(Shift, I);
07718         
07719         APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
07720         return BinaryOperator::CreateAnd(Shift,
07721                                          ConstantInt::get(*Context, Mask));
07722       }
07723       
07724       // (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2)
07725       if (I.getOpcode() == Instruction::LShr) {
07726         assert(ShiftOp->getOpcode() == Instruction::Shl);
07727         Instruction *Shift =
07728           BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff));
07729         InsertNewInstBefore(Shift, I);
07730         
07731         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
07732         return BinaryOperator::CreateAnd(Shift,
07733                                          ConstantInt::get(*Context, Mask));
07734       }
07735       
07736       // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
07737     } else {
07738       assert(ShiftAmt2 < ShiftAmt1);
07739       uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
07740 
07741       // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2)
07742       if (I.getOpcode() == Instruction::Shl) {
07743         assert(ShiftOp->getOpcode() == Instruction::LShr ||
07744                ShiftOp->getOpcode() == Instruction::AShr);
07745         Instruction *Shift =
07746           BinaryOperator::Create(ShiftOp->getOpcode(), X,
07747                                  ConstantInt::get(Ty, ShiftDiff));
07748         InsertNewInstBefore(Shift, I);
07749         
07750         APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
07751         return BinaryOperator::CreateAnd(Shift,
07752                                          ConstantInt::get(*Context, Mask));
07753       }
07754       
07755       // (X << C1) >>u C2  --> X << (C1-C2) & (-1 >> C2)
07756       if (I.getOpcode() == Instruction::LShr) {
07757         assert(ShiftOp->getOpcode() == Instruction::Shl);
07758         Instruction *Shift =
07759           BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
07760         InsertNewInstBefore(Shift, I);
07761         
07762         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
07763         return BinaryOperator::CreateAnd(Shift,
07764                                          ConstantInt::get(*Context, Mask));
07765       }
07766       
07767       // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
07768     }
07769   }
07770   return 0;
07771 }
07772 
07773 
07774 /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear
07775 /// expression.  If so, decompose it, returning some value X, such that Val is
07776 /// X*Scale+Offset.
07777 ///
07778 static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
07779                                         int &Offset, LLVMContext *Context) {
07780   assert(Val->getType() == Type::getInt32Ty(*Context) && "Unexpected allocation size type!");
07781   if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
07782     Offset = CI->getZExtValue();
07783     Scale  = 0;
07784     return ConstantInt::get(Type::getInt32Ty(*Context), 0);
07785   } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
07786     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
07787       if (I->getOpcode() == Instruction::Shl) {
07788         // This is a value scaled by '1 << the shift amt'.
07789         Scale = 1U << RHS->getZExtValue();
07790         Offset = 0;
07791         return I->getOperand(0);
07792       } else if (I->getOpcode() == Instruction::Mul) {
07793         // This value is scaled by 'RHS'.
07794         Scale = RHS->getZExtValue();
07795         Offset = 0;
07796         return I->getOperand(0);
07797       } else if (I->getOpcode() == Instruction::Add) {
07798         // We have X+C.  Check to see if we really have (X*C2)+C1, 
07799         // where C1 is divisible by C2.
07800         unsigned SubScale;
07801         Value *SubVal = 
07802           DecomposeSimpleLinearExpr(I->getOperand(0), SubScale,
07803                                     Offset, Context);
07804         Offset += RHS->getZExtValue();
07805         Scale = SubScale;
07806         return SubVal;
07807       }
07808     }
07809   }
07810 
07811   // Otherwise, we can't look past this.
07812   Scale = 1;
07813   Offset = 0;
07814   return Val;
07815 }
07816 
07817 
07818 /// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
07819 /// try to eliminate the cast by moving the type information into the alloc.
07820 Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
07821                                                    AllocationInst &AI) {
07822   const PointerType *PTy = cast<PointerType>(CI.getType());
07823   
07824   // Remove any uses of AI that are dead.
07825   assert(!CI.use_empty() && "Dead instructions should be removed earlier!");
07826   
07827   for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) {
07828     Instruction *User = cast<Instruction>(*UI++);
07829     if (isInstructionTriviallyDead(User)) {
07830       while (UI != E && *UI == User)
07831         ++UI; // If this instruction uses AI more than once, don't break UI.
07832       
07833       ++NumDeadInst;
07834       DOUT << "IC: DCE: " << *User << '\n';
07835       EraseInstFromFunction(*User);
07836     }
07837   }
07838 
07839   // This requires TargetData to get the alloca alignment and size information.
07840   if (!TD) return 0;
07841 
07842   // Get the type really allocated and the type casted to.
07843   const Type *AllocElTy = AI.getAllocatedType();
07844   const Type *CastElTy = PTy->getElementType();
07845   if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
07846 
07847   unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy);
07848   unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy);
07849   if (CastElTyAlign < AllocElTyAlign) return 0;
07850 
07851   // If the allocation has multiple uses, only promote it if we are strictly
07852   // increasing the alignment of the resultant allocation.  If we keep it the
07853   // same, we open the door to infinite loops of various kinds.  (A reference
07854   // from a dbg.declare doesn't count as a use for this purpose.)
07855   if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) &&
07856       CastElTyAlign == AllocElTyAlign) return 0;
07857 
07858   uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy);
07859   uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy);
07860   if (CastElTySize == 0 || AllocElTySize == 0) return 0;
07861 
07862   // See if we can satisfy the modulus by pulling a scale out of the array
07863   // size argument.
07864   unsigned ArraySizeScale;
07865   int ArrayOffset;
07866   Value *NumElements = // See if the array size is a decomposable linear expr.
07867     DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale,
07868                               ArrayOffset, Context);
07869  
07870   // If we can now satisfy the modulus, by using a non-1 scale, we really can
07871   // do the xform.
07872   if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
07873       (AllocElTySize*ArrayOffset   ) % CastElTySize != 0) return 0;
07874 
07875   unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
07876   Value *Amt = 0;
07877   if (Scale == 1) {
07878     Amt = NumElements;
07879   } else {
07880     // If the allocation size is constant, form a constant mul expression
07881     Amt = ConstantInt::get(Type::getInt32Ty(*Context), Scale);
07882     if (isa<ConstantInt>(NumElements))
07883       Amt = ConstantExpr::getMul(cast<ConstantInt>(NumElements),
07884                                  cast<ConstantInt>(Amt));
07885     // otherwise multiply the amount and the number of elements
07886     else {
07887       Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp");
07888       Amt = InsertNewInstBefore(Tmp, AI);
07889     }
07890   }
07891   
07892   if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
07893     Value *Off = ConstantInt::get(Type::getInt32Ty(*Context), Offset, true);
07894     Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp");
07895     Amt = InsertNewInstBefore(Tmp, AI);
07896   }
07897   
07898   AllocationInst *New;
07899   if (isa<MallocInst>(AI))
07900     New = new MallocInst(CastElTy, Amt, AI.getAlignment());
07901   else
07902     New = new AllocaInst(CastElTy, Amt, AI.getAlignment());
07903   InsertNewInstBefore(New, AI);
07904   New->takeName(&AI);
07905   
07906   // If the allocation has one real use plus a dbg.declare, just remove the
07907   // declare.
07908   if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) {
07909     EraseInstFromFunction(*DI);
07910   }
07911   // If the allocation has multiple real uses, insert a cast and change all
07912   // things that used it to use the new cast.  This will also hack on CI, but it
07913   // will die soon.
07914   else if (!AI.hasOneUse()) {
07915     AddUsesToWorkList(AI);
07916     // New is the allocation instruction, pointer typed. AI is the original
07917     // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
07918     CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast");
07919     InsertNewInstBefore(NewCast, AI);
07920     AI.replaceAllUsesWith(NewCast);
07921   }
07922   return ReplaceInstUsesWith(CI, New);
07923 }
07924 
07925 /// CanEvaluateInDifferentType - Return true if we can take the specified value
07926 /// and return it as type Ty without inserting any new casts and without
07927 /// changing the computed value.  This is used by code that tries to decide
07928 /// whether promoting or shrinking integer operations to wider or smaller types
07929 /// will allow us to eliminate a truncate or extend.
07930 ///
07931 /// This is a truncation operation if Ty is smaller than V->getType(), or an
07932 /// extension operation if Ty is larger.
07933 ///
07934 /// If CastOpc is a truncation, then Ty will be a type smaller than V.  We
07935 /// should return true if trunc(V) can be computed by computing V in the smaller
07936 /// type.  If V is an instruction, then trunc(inst(x,y)) can be computed as
07937 /// inst(trunc(x),trunc(y)), which only makes sense if x and y can be
07938 /// efficiently truncated.
07939 ///
07940 /// If CastOpc is a sext or zext, we are asking if the low bits of the value can
07941 /// bit computed in a larger type, which is then and'd or sext_in_reg'd to get
07942 /// the final result.
07943 bool InstCombiner::CanEvaluateInDifferentType(Value *V, const Type *Ty,
07944                                               unsigned CastOpc,
07945                                               int &NumCastsRemoved){
07946   // We can always evaluate constants in another type.
07947   if (isa<Constant>(V))
07948     return true;
07949   
07950   Instruction *I = dyn_cast<Instruction>(V);
07951   if (!I) return false;
07952   
07953   const Type *OrigTy = V->getType();
07954   
07955   // If this is an extension or truncate, we can often eliminate it.
07956   if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) {
07957     // If this is a cast from the destination type, we can trivially eliminate
07958     // it, and this will remove a cast overall.
07959     if (I->getOperand(0)->getType() == Ty) {
07960       // If the first operand is itself a cast, and is eliminable, do not count
07961       // this as an eliminable cast.  We would prefer to eliminate those two
07962       // casts first.
07963       if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse())
07964         ++NumCastsRemoved;
07965       return true;
07966     }
07967   }
07968 
07969   // We can't extend or shrink something that has multiple uses: doing so would
07970   // require duplicating the instruction in general, which isn't profitable.
07971   if (!I->hasOneUse()) return false;
07972 
07973   unsigned Opc = I->getOpcode();
07974   switch (Opc) {
07975   case Instruction::Add:
07976   case Instruction::Sub:
07977   case Instruction::Mul:
07978   case Instruction::And:
07979   case Instruction::Or:
07980   case Instruction::Xor:
07981     // These operators can all arbitrarily be extended or truncated.
07982     return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
07983                                       NumCastsRemoved) &&
07984            CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
07985                                       NumCastsRemoved);
07986 
07987   case Instruction::UDiv:
07988   case Instruction::URem: {
07989     // UDiv and URem can be truncated if all the truncated bits are zero.
07990     uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
07991     uint32_t BitWidth = Ty->getScalarSizeInBits();
07992     if (BitWidth < OrigBitWidth) {
07993       APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth);
07994       if (MaskedValueIsZero(I->getOperand(0), Mask) &&
07995           MaskedValueIsZero(I->getOperand(1), Mask)) {
07996         return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
07997                                           NumCastsRemoved) &&
07998                CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
07999                                           NumCastsRemoved);
08000       }
08001     }
08002     break;
08003   }
08004   case Instruction::Shl:
08005     // If we are truncating the result of this SHL, and if it's a shift of a
08006     // constant amount, we can always perform a SHL in a smaller type.
08007     if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
08008       uint32_t BitWidth = Ty->getScalarSizeInBits();
08009       if (BitWidth < OrigTy->getScalarSizeInBits() &&
08010           CI->getLimitedValue(BitWidth) < BitWidth)
08011         return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
08012                                           NumCastsRemoved);
08013     }
08014     break;
08015   case Instruction::LShr:
08016     // If this is a truncate of a logical shr, we can truncate it to a smaller
08017     // lshr iff we know that the bits we would otherwise be shifting in are
08018     // already zeros.
08019     if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
08020       uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
08021       uint32_t BitWidth = Ty->getScalarSizeInBits();
08022       if (BitWidth < OrigBitWidth &&
08023           MaskedValueIsZero(I->getOperand(0),
08024             APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
08025           CI->getLimitedValue(BitWidth) < BitWidth) {
08026         return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
08027                                           NumCastsRemoved);
08028       }
08029     }
08030     break;
08031   case Instruction::ZExt:
08032   case Instruction::SExt:
08033   case Instruction::Trunc:
08034     // If this is the same kind of case as our original (e.g. zext+zext), we
08035     // can safely replace it.  Note that replacing it does not reduce the number
08036     // of casts in the input.
08037     if (Opc == CastOpc)
08038       return true;
08039 
08040     // sext (zext ty1), ty2 -> zext ty2
08041     if (CastOpc == Instruction::SExt && Opc == Instruction::ZExt)
08042       return true;
08043     break;
08044   case Instruction::Select: {
08045     SelectInst *SI = cast<SelectInst>(I);
08046     return CanEvaluateInDifferentType(SI->getTrueValue(), Ty, CastOpc,
08047                                       NumCastsRemoved) &&
08048            CanEvaluateInDifferentType(SI->getFalseValue(), Ty, CastOpc,
08049                                       NumCastsRemoved);
08050   }
08051   case Instruction::PHI: {
08052     // We can change a phi if we can change all operands.
08053     PHINode *PN = cast<PHINode>(I);
08054     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
08055       if (!CanEvaluateInDifferentType(PN->getIncomingValue(i), Ty, CastOpc,
08056                                       NumCastsRemoved))
08057         return false;
08058     return true;
08059   }
08060   default:
08061     // TODO: Can handle more cases here.
08062     break;
08063   }
08064   
08065   return false;
08066 }
08067 
08068 /// EvaluateInDifferentType - Given an expression that 
08069 /// CanEvaluateInDifferentType returns true for, actually insert the code to
08070 /// evaluate the expression.
08071 Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, 
08072                                              bool isSigned) {
08073   if (Constant *C = dyn_cast<Constant>(V))
08074     return ConstantExpr::getIntegerCast(C, Ty,
08075                                                isSigned /*Sext or ZExt*/);
08076 
08077   // Otherwise, it must be an instruction.
08078   Instruction *I = cast<Instruction>(V);
08079   Instruction *Res = 0;
08080   unsigned Opc = I->getOpcode();
08081   switch (Opc) {
08082   case Instruction::Add:
08083   case Instruction::Sub:
08084   case Instruction::Mul:
08085   case Instruction::And:
08086   case Instruction::Or:
08087   case Instruction::Xor:
08088   case Instruction::AShr:
08089   case Instruction::LShr:
08090   case Instruction::Shl:
08091   case Instruction::UDiv:
08092   case Instruction::URem: {
08093     Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
08094     Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
08095     Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
08096     break;
08097   }    
08098   case Instruction::Trunc:
08099   case Instruction::ZExt:
08100   case Instruction::SExt:
08101     // If the source type of the cast is the type we're trying for then we can
08102     // just return the source.  There's no need to insert it because it is not
08103     // new.
08104     if (I->getOperand(0)->getType() == Ty)
08105       return I->getOperand(0);
08106     
08107     // Otherwise, must be the same type of cast, so just reinsert a new one.
08108     Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0),
08109                            Ty);
08110     break;
08111   case Instruction::Select: {
08112     Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
08113     Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
08114     Res = SelectInst::Create(I->getOperand(0), True, False);
08115     break;
08116   }
08117   case Instruction::PHI: {
08118     PHINode *OPN = cast<PHINode>(I);
08119     PHINode *NPN = PHINode::Create(Ty);
08120     for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
08121       Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
08122       NPN->addIncoming(V, OPN->getIncomingBlock(i));
08123     }
08124     Res = NPN;
08125     break;
08126   }
08127   default: 
08128     // TODO: Can handle more cases here.
08129     llvm_unreachable("Unreachable!");
08130     break;
08131   }
08132   
08133   Res->takeName(I);
08134   return InsertNewInstBefore(Res, *I);
08135 }
08136 
08137 /// @brief Implement the transforms common to all CastInst visitors.
08138 Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
08139   Value *Src = CI.getOperand(0);
08140 
08141   // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
08142   // eliminate it now.
08143   if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {   // A->B->C cast
08144     if (Instruction::CastOps opc = 
08145         isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
08146       // The first cast (CSrc) is eliminable so we need to fix up or replace
08147       // the second cast (CI). CSrc will then have a good chance of being dead.
08148       return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
08149     }
08150   }
08151 
08152   // If we are casting a select then fold the cast into the select
08153   if (SelectInst *SI = dyn_cast<SelectInst>(Src))
08154     if (Instruction *NV = FoldOpIntoSelect(CI, SI, this))
08155       return NV;
08156 
08157   // If we are casting a PHI then fold the cast into the PHI
08158   if (isa<PHINode>(Src))
08159     if (Instruction *NV = FoldOpIntoPhi(CI))
08160       return NV;
08161   
08162   return 0;
08163 }
08164 
08165 /// FindElementAtOffset - Given a type and a constant offset, determine whether
08166 /// or not there is a sequence of GEP indices into the type that will land us at
08167 /// the specified offset.  If so, fill them into NewIndices and return the
08168 /// resultant element type, otherwise return null.
08169 static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset, 
08170                                        SmallVectorImpl<Value*> &NewIndices,
08171                                        const TargetData *TD,
08172                                        LLVMContext *Context) {
08173   if (!TD) return 0;
08174   if (!Ty->isSized()) return 0;
08175   
08176   // Start with the index over the outer type.  Note that the type size
08177   // might be zero (even if the offset isn't zero) if the indexed type
08178   // is something like [0 x {int, int}]
08179   const Type *IntPtrTy = TD->getIntPtrType(*Context);
08180   int64_t FirstIdx = 0;
08181   if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
08182     FirstIdx = Offset/TySize;
08183     Offset -= FirstIdx*TySize;
08184     
08185     // Handle hosts where % returns negative instead of values [0..TySize).
08186     if (Offset < 0) {
08187       --FirstIdx;
08188       Offset += TySize;
08189       assert(Offset >= 0);
08190     }
08191     assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
08192   }
08193   
08194   NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
08195     
08196   // Index into the types.  If we fail, set OrigBase to null.
08197   while (Offset) {
08198     // Indexing into tail padding between struct/array elements.
08199     if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty))
08200       return 0;
08201     
08202     if (const StructType *STy = dyn_cast<StructType>(Ty)) {
08203       const StructLayout *SL = TD->getStructLayout(STy);
08204       assert(Offset < (int64_t)SL->getSizeInBytes() &&
08205              "Offset must stay within the indexed type");
08206       
08207       unsigned Elt = SL->getElementContainingOffset(Offset);
08208       NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(*Context), Elt));
08209       
08210       Offset -= SL->getElementOffset(Elt);
08211       Ty = STy->getElementType(Elt);
08212     } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
08213       uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType());
08214       assert(EltSize && "Cannot index into a zero-sized array");
08215       NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
08216       Offset %= EltSize;
08217       Ty = AT->getElementType();
08218     } else {
08219       // Otherwise, we can't index into the middle of this atomic type, bail.
08220       return 0;
08221     }
08222   }
08223   
08224   return Ty;
08225 }
08226 
08227 /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
08228 Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
08229   Value *Src = CI.getOperand(0);
08230   
08231   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
08232     // If casting the result of a getelementptr instruction with no offset, turn
08233     // this into a cast of the original pointer!
08234     if (GEP->hasAllZeroIndices()) {
08235       // Changing the cast operand is usually not a good idea but it is safe
08236       // here because the pointer operand is being replaced with another 
08237       // pointer operand so the opcode doesn't need to change.
08238       AddToWorkList(GEP);
08239       CI.setOperand(0, GEP->getOperand(0));
08240       return &CI;
08241     }
08242     
08243     // If the GEP has a single use, and the base pointer is a bitcast, and the
08244     // GEP computes a constant offset, see if we can convert these three
08245     // instructions into fewer.  This typically happens with unions and other
08246     // non-type-safe code.
08247     if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) {
08248       if (GEP->hasAllConstantIndices()) {
08249         // We are guaranteed to get a constant from EmitGEPOffset.
08250         ConstantInt *OffsetV =
08251                       cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this));
08252         int64_t Offset = OffsetV->getSExtValue();
08253         
08254         // Get the base pointer input of the bitcast, and the type it points to.
08255         Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0);
08256         const Type *GEPIdxTy =
08257           cast<PointerType>(OrigBase->getType())->getElementType();
08258         SmallVector<Value*, 8> NewIndices;
08259         if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices, TD, Context)) {
08260           // If we were able to index down into an element, create the GEP
08261           // and bitcast the result.  This eliminates one bitcast, potentially
08262           // two.
08263           Instruction *NGEP = GetElementPtrInst::Create(OrigBase, 
08264                                                         NewIndices.begin(),
08265                                                         NewIndices.end(), "");
08266           InsertNewInstBefore(NGEP, CI);
08267           NGEP->takeName(GEP);
08268           if (cast<GEPOperator>(GEP)->isInBounds())
08269             cast<GEPOperator>(NGEP)->setIsInBounds(true);
08270           
08271           if (isa<BitCastInst>(CI))
08272             return new BitCastInst(NGEP, CI.getType());
08273           assert(isa<PtrToIntInst>(CI));
08274           return new PtrToIntInst(NGEP, CI.getType());
08275         }
08276       }      
08277     }
08278   }
08279     
08280   return commonCastTransforms(CI);
08281 }
08282 
08283 /// isSafeIntegerType - Return true if this is a basic integer type, not a crazy
08284 /// type like i42.  We don't want to introduce operations on random non-legal
08285 /// integer types where they don't already exist in the code.  In the future,
08286 /// we should consider making this based off target-data, so that 32-bit targets
08287 /// won't get i64 operations etc.
08288 static bool isSafeIntegerType(const Type *Ty) {
08289   switch (Ty->getPrimitiveSizeInBits()) {
08290   case 8:
08291   case 16:
08292   case 32:
08293   case 64:
08294     return true;
08295   default: 
08296     return false;
08297   }
08298 }
08299 
08300 /// commonIntCastTransforms - This function implements the common transforms
08301 /// for trunc, zext, and sext.
08302 Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) {
08303   if (Instruction *Result = commonCastTransforms(CI))
08304     return Result;
08305 
08306   Value *Src = CI.getOperand(0);
08307   const Type *SrcTy = Src->getType();
08308   const Type *DestTy = CI.getType();
08309   uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
08310   uint32_t DestBitSize = DestTy->getScalarSizeInBits();
08311 
08312   // See if we can simplify any instructions used by the LHS whose sole 
08313   // purpose is to compute bits we don't care about.
08314   if (SimplifyDemandedInstructionBits(CI))
08315     return &CI;
08316 
08317   // If the source isn't an instruction or has more than one use then we
08318   // can't do anything more. 
08319   Instruction *SrcI = dyn_cast<Instruction>(Src);
08320   if (!SrcI || !Src->hasOneUse())
08321     return 0;
08322 
08323   // Attempt to propagate the cast into the instruction for int->int casts.
08324   int NumCastsRemoved = 0;
08325   // Only do this if the dest type is a simple type, don't convert the
08326   // expression tree to something weird like i93 unless the source is also
08327   // strange.
08328   if ((isSafeIntegerType(DestTy->getScalarType()) ||
08329        !isSafeIntegerType(SrcI->getType()->getScalarType())) &&
08330       CanEvaluateInDifferentType(SrcI, DestTy,
08331                                  CI.getOpcode(), NumCastsRemoved)) {
08332     // If this cast is a truncate, evaluting in a different type always
08333     // eliminates the cast, so it is always a win.  If this is a zero-extension,
08334     // we need to do an AND to maintain the clear top-part of the computation,
08335     // so we require that the input have eliminated at least one cast.  If this
08336     // is a sign extension, we insert two new casts (to do the extension) so we
08337     // require that two casts have been eliminated.
08338     bool DoXForm = false;
08339     bool JustReplace = false;
08340     switch (CI.getOpcode()) {
08341     default:
08342       // All the others use floating point so we shouldn't actually 
08343       // get here because of the check above.
08344       llvm_unreachable("Unknown cast type");
08345     case Instruction::Trunc:
08346       DoXForm = true;
08347       break;
08348     case Instruction::ZExt: {
08349       DoXForm = NumCastsRemoved >= 1;
08350       if (!DoXForm && 0) {
08351         // If it's unnecessary to issue an AND to clear the high bits, it's
08352         // always profitable to do this xform.
08353         Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, false);
08354         APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize));
08355         if (MaskedValueIsZero(TryRes, Mask))
08356           return ReplaceInstUsesWith(CI, TryRes);
08357         
08358         if (Instruction *TryI = dyn_cast<Instruction>(TryRes))
08359           if (TryI->use_empty())
08360             EraseInstFromFunction(*TryI);
08361       }
08362       break;
08363     }
08364     case Instruction::SExt: {
08365       DoXForm = NumCastsRemoved >= 2;
08366       if (!DoXForm && !isa<TruncInst>(SrcI) && 0) {
08367         // If we do not have to emit the truncate + sext pair, then it's always
08368         // profitable to do this xform.
08369         //
08370         // It's not safe to eliminate the trunc + sext pair if one of the
08371         // eliminated cast is a truncate. e.g.
08372         // t2 = trunc i32 t1 to i16
08373         // t3 = sext i16 t2 to i32
08374         // !=
08375         // i32 t1
08376         Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, true);
08377         unsigned NumSignBits = ComputeNumSignBits(TryRes);
08378         if (NumSignBits > (DestBitSize - SrcBitSize))
08379           return ReplaceInstUsesWith(CI, TryRes);
08380         
08381         if (Instruction *TryI = dyn_cast<Instruction>(TryRes))
08382           if (TryI->use_empty())
08383             EraseInstFromFunction(*TryI);
08384       }
08385       break;
08386     }
08387     }
08388     
08389     if (DoXForm) {
08390       DOUT << "ICE: EvaluateInDifferentType converting expression type to avoid"
08391            << " cast: " << CI;
08392       Value *Res = EvaluateInDifferentType(SrcI, DestTy, 
08393                                            CI.getOpcode() == Instruction::SExt);
08394       if (JustReplace)
08395         // Just replace this cast with the result.
08396         return ReplaceInstUsesWith(CI, Res);
08397 
08398       assert(Res->getType() == DestTy);
08399       switch (CI.getOpcode()) {
08400       default: llvm_unreachable("Unknown cast type!");
08401       case Instruction::Trunc:
08402         // Just replace this cast with the result.
08403         return ReplaceInstUsesWith(CI, Res);
08404       case Instruction::ZExt: {
08405         assert(SrcBitSize < DestBitSize && "Not a zext?");
08406 
08407         // If the high bits are already zero, just replace this cast with the
08408         // result.
08409         APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize));
08410         if (MaskedValueIsZero(Res, Mask))
08411           return ReplaceInstUsesWith(CI, Res);
08412 
08413         // We need to emit an AND to clear the high bits.
08414         Constant *C = ConstantInt::get(*Context, 
08415                                  APInt::getLowBitsSet(DestBitSize, SrcBitSize));
08416         return BinaryOperator::CreateAnd(Res, C);
08417       }
08418       case Instruction::SExt: {
08419         // If the high bits are already filled with sign bit, just replace this
08420         // cast with the result.
08421         unsigned NumSignBits = ComputeNumSignBits(Res);
08422         if (NumSignBits > (DestBitSize - SrcBitSize))
08423           return ReplaceInstUsesWith(CI, Res);
08424 
08425         // We need to emit a cast to truncate, then a cast to sext.
08426         return CastInst::Create(Instruction::SExt,
08427             InsertCastBefore(Instruction::Trunc, Res, Src->getType(), 
08428                              CI), DestTy);
08429       }
08430       }
08431     }
08432   }
08433   
08434   Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0;
08435   Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0;
08436 
08437   switch (SrcI->getOpcode()) {
08438   case Instruction::Add:
08439   case Instruction::Mul:
08440   case Instruction::And:
08441   case Instruction::Or:
08442   case Instruction::Xor:
08443     // If we are discarding information, rewrite.
08444     if (DestBitSize < SrcBitSize && DestBitSize != 1) {
08445       // Don't insert two casts unless at least one can be eliminated.
08446       if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) ||
08447           !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) {
08448         Value *Op0c = InsertCastBefore(Instruction::Trunc, Op0, DestTy, *SrcI);
08449         Value *Op1c = InsertCastBefore(Instruction::Trunc, Op1, DestTy, *SrcI);
08450         return BinaryOperator::Create(
08451             cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c);
08452       }
08453     }
08454 
08455     // cast (xor bool X, true) to int  --> xor (cast bool X to int), 1
08456     if (isa<ZExtInst>(CI) && SrcBitSize == 1 && 
08457         SrcI->getOpcode() == Instruction::Xor &&
08458         Op1 == ConstantInt::getTrue(*Context) &&
08459         (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) {
08460       Value *New = InsertCastBefore(Instruction::ZExt, Op0, DestTy, CI);
08461       return BinaryOperator::CreateXor(New,
08462                                       ConstantInt::get(CI.getType(), 1));
08463     }
08464     break;
08465 
08466   case Instruction::Shl: {
08467     // Canonicalize trunc inside shl, if we can.
08468     ConstantInt *CI = dyn_cast<ConstantInt>(Op1);
08469     if (CI && DestBitSize < SrcBitSize &&
08470         CI->getLimitedValue(DestBitSize) < DestBitSize) {
08471       Value *Op0c = InsertCastBefore(Instruction::Trunc, Op0, DestTy, *SrcI);
08472       Value *Op1c = InsertCastBefore(Instruction::Trunc, Op1, DestTy, *SrcI);
08473       return BinaryOperator::CreateShl(Op0c, Op1c);
08474     }
08475     break;
08476   }
08477   }
08478   return 0;
08479 }
08480 
08481 Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
08482   if (Instruction *Result = commonIntCastTransforms(CI))
08483     return Result;
08484   
08485   Value *Src = CI.getOperand(0);
08486   const Type *Ty = CI.getType();
08487   uint32_t DestBitWidth = Ty->getScalarSizeInBits();
08488   uint32_t SrcBitWidth = Src->getType()->getScalarSizeInBits();
08489 
08490   // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0)
08491   if (DestBitWidth == 1) {
08492     Constant *One = ConstantInt::get(Src->getType(), 1);
08493     Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI);
08494     Value *Zero = Constant::getNullValue(Src->getType());
08495     return new ICmpInst(*Context, ICmpInst::ICMP_NE, Src, Zero);
08496   }
08497 
08498   // Optimize trunc(lshr(), c) to pull the shift through the truncate.
08499   ConstantInt *ShAmtV = 0;
08500   Value *ShiftOp = 0;
08501   if (Src->hasOneUse() &&
08502       match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)))) {
08503     uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
08504     
08505     // Get a mask for the bits shifting in.
08506     APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
08507     if (MaskedValueIsZero(ShiftOp, Mask)) {
08508       if (ShAmt >= DestBitWidth)        // All zeros.
08509         return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
08510       
08511       // Okay, we can shrink this.  Truncate the input, then return a new
08512       // shift.
08513       Value *V1 = InsertCastBefore(Instruction::Trunc, ShiftOp, Ty, CI);
08514       Value *V2 = ConstantExpr::getTrunc(ShAmtV, Ty);
08515       return BinaryOperator::CreateLShr(V1, V2);
08516     }
08517   }
08518   
08519   return 0;
08520 }
08521 
08522 /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
08523 /// in order to eliminate the icmp.
08524 Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
08525                                              bool DoXform) {
08526   // If we are just checking for a icmp eq of a single bit and zext'ing it
08527   // to an integer, then shift the bit to the appropriate place and then
08528   // cast to integer to avoid the comparison.
08529   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
08530     const APInt &Op1CV = Op1C->getValue();
08531       
08532     // zext (x <s  0) to i32 --> x>>u31      true if signbit set.
08533     // zext (x >s -1) to i32 --> (x>>u31)^1  true if signbit clear.
08534     if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
08535         (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
08536       if (!DoXform) return ICI;
08537 
08538       Value *In = ICI->getOperand(0);
08539       Value *Sh = ConstantInt::get(In->getType(),
08540                                    In->getType()->getScalarSizeInBits()-1);
08541       In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh,
08542                                                         In->getName()+".lobit"),
08543                                CI);
08544       if (In->getType() != CI.getType())
08545         In = CastInst::CreateIntegerCast(In, CI.getType(),
08546                                          false/*ZExt*/, "tmp", &CI);
08547 
08548       if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
08549         Constant *One = ConstantInt::get(In->getType(), 1);
08550         In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One,
08551                                                          In->getName()+".not"),
08552                                  CI);
08553       }
08554 
08555       return ReplaceInstUsesWith(CI, In);
08556     }
08557       
08558       
08559       
08560     // zext (X == 0) to i32 --> X^1      iff X has only the low bit set.
08561     // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
08562     // zext (X == 1) to i32 --> X        iff X has only the low bit set.
08563     // zext (X == 2) to i32 --> X>>1     iff X has only the 2nd bit set.
08564     // zext (X != 0) to i32 --> X        iff X has only the low bit set.
08565     // zext (X != 0) to i32 --> X>>1     iff X has only the 2nd bit set.
08566     // zext (X != 1) to i32 --> X^1      iff X has only the low bit set.
08567     // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
08568     if ((Op1CV == 0 || Op1CV.isPowerOf2()) && 
08569         // This only works for EQ and NE
08570         ICI->isEquality()) {
08571       // If Op1C some other power of two, convert:
08572       uint32_t BitWidth = Op1C->getType()->getBitWidth();
08573       APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
08574       APInt TypeMask(APInt::getAllOnesValue(BitWidth));
08575       ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne);
08576         
08577       APInt KnownZeroMask(~KnownZero);
08578       if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
08579         if (!DoXform) return ICI;
08580 
08581         bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
08582         if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
08583           // (X&4) == 2 --> false
08584           // (X&4) != 2 --> true
08585           Constant *Res = ConstantInt::get(Type::getInt1Ty(*Context), isNE);
08586           Res = ConstantExpr::getZExt(Res, CI.getType());
08587           return ReplaceInstUsesWith(CI, Res);
08588         }
08589           
08590         uint32_t ShiftAmt = KnownZeroMask.logBase2();
08591         Value *In = ICI->getOperand(0);
08592         if (ShiftAmt) {
08593           // Perform a logical shr by shiftamt.
08594           // Insert the shift to put the result in the low bit.
08595           In = InsertNewInstBefore(BinaryOperator::CreateLShr(In,
08596                               ConstantInt::get(In->getType(), ShiftAmt),
08597                                                    In->getName()+".lobit"), CI);
08598         }
08599           
08600         if ((Op1CV != 0) == isNE) { // Toggle the low bit.
08601           Constant *One = ConstantInt::get(In->getType(), 1);
08602           In = BinaryOperator::CreateXor(In, One, "tmp");
08603           InsertNewInstBefore(cast<Instruction>(In), CI);
08604         }
08605           
08606         if (CI.getType() == In->getType())
08607           return ReplaceInstUsesWith(CI, In);
08608         else
08609           return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
08610       }
08611     }
08612   }
08613 
08614   return 0;
08615 }
08616 
08617 Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
08618   // If one of the common conversion will work ..
08619   if (Instruction *Result = commonIntCastTransforms(CI))
08620     return Result;
08621 
08622   Value *Src = CI.getOperand(0);
08623 
08624   // If this is a TRUNC followed by a ZEXT then we are dealing with integral
08625   // types and if the sizes are just right we can convert this into a logical
08626   // 'and' which will be much cheaper than the pair of casts.
08627   if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) {   // A->B->C cast
08628     // Get the sizes of the types involved.  We know that the intermediate type
08629     // will be smaller than A or C, but don't know the relation between A and C.
08630     Value *A = CSrc->getOperand(0);
08631     unsigned SrcSize = A->getType()->getScalarSizeInBits();
08632     unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
08633     unsigned DstSize = CI.getType()->getScalarSizeInBits();
08634     // If we're actually extending zero bits, then if
08635     // SrcSize <  DstSize: zext(a & mask)
08636     // SrcSize == DstSize: a & mask
08637     // SrcSize  > DstSize: trunc(a) & mask
08638     if (SrcSize < DstSize) {
08639       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
08640       Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
08641       Instruction *And =
08642         BinaryOperator::CreateAnd(A, AndConst, CSrc->getName()+".mask");
08643       InsertNewInstBefore(And, CI);
08644       return new ZExtInst(And, CI.getType());
08645     } else if (SrcSize == DstSize) {
08646       APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
08647       return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
08648                                                            AndValue));
08649     } else if (SrcSize > DstSize) {
08650       Instruction *Trunc = new TruncInst(A, CI.getType(), "tmp");
08651       InsertNewInstBefore(Trunc, CI);
08652       APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
08653       return BinaryOperator::CreateAnd(Trunc, 
08654                                        ConstantInt::get(Trunc->getType(),
08655                                                                AndValue));
08656     }
08657   }
08658 
08659   if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
08660     return transformZExtICmp(ICI, CI);
08661 
08662   BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
08663   if (SrcI && SrcI->getOpcode() == Instruction::Or) {
08664     // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
08665     // of the (zext icmp) will be transformed.
08666     ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
08667     ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
08668     if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
08669         (transformZExtICmp(LHS, CI, false) ||
08670          transformZExtICmp(RHS, CI, false))) {
08671       Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI);
08672       Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI);
08673       return BinaryOperator::Create(Instruction::Or, LCast, RCast);
08674     }
08675   }
08676 
08677   // zext(trunc(t) & C) -> (t & zext(C)).
08678   if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse())
08679     if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
08680       if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) {
08681         Value *TI0 = TI->getOperand(0);
08682         if (TI0->getType() == CI.getType())
08683           return
08684             BinaryOperator::CreateAnd(TI0,
08685                                 ConstantExpr::getZExt(C, CI.getType()));
08686       }
08687 
08688   // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)).
08689   if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse())
08690     if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
08691       if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0)))
08692         if (And->getOpcode() == Instruction::And && And->hasOneUse() &&
08693             And->getOperand(1) == C)
08694           if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) {
08695             Value *TI0 = TI->getOperand(0);
08696             if (TI0->getType() == CI.getType()) {
08697               Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
08698               Instruction *NewAnd = BinaryOperator::CreateAnd(TI0, ZC, "tmp");
08699               InsertNewInstBefore(NewAnd, *And);
08700               return BinaryOperator::CreateXor(NewAnd, ZC);
08701             }
08702           }
08703 
08704   return 0;
08705 }
08706 
08707 Instruction *InstCombiner::visitSExt(SExtInst &CI) {
08708   if (Instruction *I = commonIntCastTransforms(CI))
08709     return I;
08710   
08711   Value *Src = CI.getOperand(0);
08712   
08713   // Canonicalize sign-extend from i1 to a select.
08714   if (Src->getType() == Type::getInt1Ty(*Context))
08715     return SelectInst::Create(Src,
08716                               Constant::getAllOnesValue(CI.getType()),
08717                               Constant::getNullValue(CI.getType()));
08718 
08719   // See if the value being truncated is already sign extended.  If so, just
08720   // eliminate the trunc/sext pair.
08721   if (Operator::getOpcode(Src) == Instruction::Trunc) {
08722     Value *Op = cast<User>(Src)->getOperand(0);
08723     unsigned OpBits   = Op->getType()->getScalarSizeInBits();
08724     unsigned MidBits  = Src->getType()->getScalarSizeInBits();
08725     unsigned DestBits = CI.getType()->getScalarSizeInBits();
08726     unsigned NumSignBits = ComputeNumSignBits(Op);
08727 
08728     if (OpBits == DestBits) {
08729       // Op is i32, Mid is i8, and Dest is i32.  If Op has more than 24 sign
08730       // bits, it is already ready.
08731       if (NumSignBits > DestBits-MidBits)
08732         return ReplaceInstUsesWith(CI, Op);
08733     } else if (OpBits < DestBits) {
08734       // Op is i32, Mid is i8, and Dest is i64.  If Op has more than 24 sign
08735       // bits, just sext from i32.
08736       if (NumSignBits > OpBits-MidBits)
08737         return new SExtInst(Op, CI.getType(), "tmp");
08738     } else {
08739       // Op is i64, Mid is i8, and Dest is i32.  If Op has more than 56 sign
08740       // bits, just truncate to i32.
08741       if (NumSignBits > OpBits-MidBits)
08742         return new TruncInst(Op, CI.getType(), "tmp");
08743     }
08744   }
08745 
08746   // If the input is a shl/ashr pair of a same constant, then this is a sign
08747   // extension from a smaller value.  If we could trust arbitrary bitwidth
08748   // integers, we could turn this into a truncate to the smaller bit and then
08749   // use a sext for the whole extension.  Since we don't, look deeper and check
08750   // for a truncate.  If the source and dest are the same type, eliminate the
08751   // trunc and extend and just do shifts.  For example, turn:
08752   //   %a = trunc i32 %i to i8
08753   //   %b = shl i8 %a, 6
08754   //   %c = ashr i8 %b, 6
08755   //   %d = sext i8 %c to i32
08756   // into:
08757   //   %a = shl i32 %i, 30
08758   //   %d = ashr i32 %a, 30
08759   Value *A = 0;
08760   ConstantInt *BA = 0, *CA = 0;
08761   if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)),
08762                         m_ConstantInt(CA))) &&
08763       BA == CA && isa<TruncInst>(A)) {
08764     Value *I = cast<TruncInst>(A)->getOperand(0);
08765     if (I->getType() == CI.getType()) {
08766       unsigned MidSize = Src->getType()->getScalarSizeInBits();
08767       unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
08768       unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
08769       Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
08770       I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV,
08771                                                         CI.getName()), CI);
08772       return BinaryOperator::CreateAShr(I, ShAmtV);
08773     }
08774   }
08775   
08776   return 0;
08777 }
08778 
08779 /// FitsInFPType - Return a Constant* for the specified FP constant if it fits
08780 /// in the specified FP type without changing its value.
08781 static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem,
08782                               LLVMContext *Context) {
08783   bool losesInfo;
08784   APFloat F = CFP->getValueAPF();
08785   (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
08786   if (!losesInfo)
08787     return ConstantFP::get(*Context, F);
08788   return 0;
08789 }
08790 
08791 /// LookThroughFPExtensions - If this is an fp extension instruction, look
08792 /// through it until we get the source value.
08793 static Value *LookThroughFPExtensions(Value *V, LLVMContext *Context) {
08794   if (Instruction *I = dyn_cast<Instruction>(V))
08795     if (I->getOpcode() == Instruction::FPExt)
08796       return LookThroughFPExtensions(I->getOperand(0), Context);
08797   
08798   // If this value is a constant, return the constant in the smallest FP type
08799   // that can accurately represent it.  This allows us to turn
08800   // (float)((double)X+2.0) into x+2.0f.
08801   if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
08802     if (CFP->getType() == Type::getPPC_FP128Ty(*Context))
08803       return V;  // No constant folding of this.
08804     // See if the value can be truncated to float and then reextended.
08805     if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle, Context))
08806       return V;
08807     if (CFP->getType() == Type::getDoubleTy(*Context))
08808       return V;  // Won't shrink.
08809     if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble, Context))
08810       return V;
08811     // Don't try to shrink to various long double types.
08812   }
08813   
08814   return V;
08815 }
08816 
08817 Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
08818   if (Instruction *I = commonCastTransforms(CI))
08819     return I;
08820   
08821   // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are
08822   // smaller than the destination type, we can eliminate the truncate by doing
08823   // the add as the smaller type.  This applies to fadd/fsub/fmul/fdiv as well as
08824   // many builtins (sqrt, etc).
08825   BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
08826   if (OpI && OpI->hasOneUse()) {
08827     switch (OpI->getOpcode()) {
08828     default: break;
08829     case Instruction::FAdd:
08830     case Instruction::FSub:
08831     case Instruction::FMul:
08832     case Instruction::FDiv:
08833     case Instruction::FRem:
08834       const Type *SrcTy = OpI->getType();
08835       Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0), Context);
08836       Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1), Context);
08837       if (LHSTrunc->getType() != SrcTy && 
08838           RHSTrunc->getType() != SrcTy) {
08839         unsigned DstSize = CI.getType()->getScalarSizeInBits();
08840         // If the source types were both smaller than the destination type of
08841         // the cast, do this xform.
08842         if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize &&
08843             RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) {
08844           LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc,
08845                                       CI.getType(), CI);
08846           RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc,
08847                                       CI.getType(), CI);
08848           return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
08849         }
08850       }
08851       break;  
08852     }
08853   }
08854   return 0;
08855 }
08856 
08857 Instruction *InstCombiner::visitFPExt(CastInst &CI) {
08858   return commonCastTransforms(CI);
08859 }
08860 
08861 Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
08862   Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
08863   if (OpI == 0)
08864     return commonCastTransforms(FI);
08865 
08866   // fptoui(uitofp(X)) --> X
08867   // fptoui(sitofp(X)) --> X
08868   // This is safe if the intermediate type has enough bits in its mantissa to
08869   // accurately represent all values of X.  For example, do not do this with
08870   // i64->float->i64.  This is also safe for sitofp case, because any negative
08871   // 'X' value would cause an undefined result for the fptoui. 
08872   if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
08873       OpI->getOperand(0)->getType() == FI.getType() &&
08874       (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
08875                     OpI->getType()->getFPMantissaWidth())
08876     return ReplaceInstUsesWith(FI, OpI->getOperand(0));
08877 
08878   return commonCastTransforms(FI);
08879 }
08880 
08881 Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
08882   Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
08883   if (OpI == 0)
08884     return commonCastTransforms(FI);
08885   
08886   // fptosi(sitofp(X)) --> X
08887   // fptosi(uitofp(X)) --> X
08888   // This is safe if the intermediate type has enough bits in its mantissa to
08889   // accurately represent all values of X.  For example, do not do this with
08890   // i64->float->i64.  This is also safe for sitofp case, because any negative
08891   // 'X' value would cause an undefined result for the fptoui. 
08892   if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
08893       OpI->getOperand(0)->getType() == FI.getType() &&
08894       (int)FI.getType()->getScalarSizeInBits() <=
08895                     OpI->getType()->getFPMantissaWidth())
08896     return ReplaceInstUsesWith(FI, OpI->getOperand(0));
08897   
08898   return commonCastTransforms(FI);
08899 }
08900 
08901 Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
08902   return commonCastTransforms(CI);
08903 }
08904 
08905 Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
08906   return commonCastTransforms(CI);
08907 }
08908 
08909 Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) {
08910   // If the destination integer type is smaller than the intptr_t type for
08911   // this target, do a ptrtoint to intptr_t then do a trunc.  This allows the
08912   // trunc to be exposed to other transforms.  Don't do this for extending
08913   // ptrtoint's, because we don't know if the target sign or zero extends its
08914   // pointers.
08915   if (TD &&
08916       CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
08917     Value *P = InsertNewInstBefore(new PtrToIntInst(CI.getOperand(0),
08918                                              TD->getIntPtrType(CI.getContext()),
08919                                                     "tmp"), CI);
08920     return new TruncInst(P, CI.getType());
08921   }
08922   
08923   return commonPointerCastTransforms(CI);
08924 }
08925 
08926 Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
08927   // If the source integer type is larger than the intptr_t type for
08928   // this target, do a trunc to the intptr_t type, then inttoptr of it.  This
08929   // allows the trunc to be exposed to other transforms.  Don't do this for
08930   // extending inttoptr's, because we don't know if the target sign or zero
08931   // extends to pointers.
08932   if (TD &&
08933       CI.getOperand(0)->getType()->getScalarSizeInBits() >
08934       TD->getPointerSizeInBits()) {
08935     Value *P = InsertNewInstBefore(new TruncInst(CI.getOperand(0),
08936                                              TD->getIntPtrType(CI.getContext()),
08937                                                  "tmp"), CI);
08938     return new IntToPtrInst(P, CI.getType());
08939   }
08940   
08941   if (Instruction *I = commonCastTransforms(CI))
08942     return I;
08943 
08944   return 0;
08945 }
08946 
08947 Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
08948   // If the operands are integer typed then apply the integer transforms,
08949   // otherwise just apply the common ones.
08950   Value *Src = CI.getOperand(0);
08951   const Type *SrcTy = Src->getType();
08952   const Type *DestTy = CI.getType();
08953 
08954   if (isa<PointerType>(SrcTy)) {
08955     if (Instruction *I = commonPointerCastTransforms(CI))
08956       return I;
08957   } else {
08958     if (Instruction *Result = commonCastTransforms(CI))
08959       return Result;
08960   }
08961 
08962 
08963   // Get rid of casts from one type to the same type. These are useless and can
08964   // be replaced by the operand.
08965   if (DestTy == Src->getType())
08966     return ReplaceInstUsesWith(CI, Src);
08967 
08968   if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
08969     const PointerType *SrcPTy = cast<PointerType>(SrcTy);
08970     const Type *DstElTy = DstPTy->getElementType();
08971     const Type *SrcElTy = SrcPTy->getElementType();
08972     
08973     // If the address spaces don't match, don't eliminate the bitcast, which is
08974     // required for changing types.
08975     if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
08976       return 0;
08977     
08978     // If we are casting a malloc or alloca to a pointer to a type of the same
08979     // size, rewrite the allocation instruction to allocate the "right" type.
08980     if (AllocationInst *AI = dyn_cast<AllocationInst>(Src))
08981       if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
08982         return V;
08983     
08984     // If the source and destination are pointers, and this cast is equivalent
08985     // to a getelementptr X, 0, 0, 0...  turn it into the appropriate gep.
08986     // This can enhance SROA and other transforms that want type-safe pointers.
08987     Constant *ZeroUInt = Constant::getNullValue(Type::getInt32Ty(*Context));
08988     unsigned NumZeros = 0;
08989     while (SrcElTy != DstElTy && 
08990            isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) &&
08991            SrcElTy->getNumContainedTypes() /* not "{}" */) {
08992       SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
08993       ++NumZeros;
08994     }
08995 
08996     // If we found a path from the src to dest, create the getelementptr now.
08997     if (SrcElTy == DstElTy) {
08998       SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
08999       Instruction *GEP = GetElementPtrInst::Create(Src,
09000                                                    Idxs.begin(), Idxs.end(), "",
09001                                                    ((Instruction*) NULL));
09002       cast<GEPOperator>(GEP)->setIsInBounds(true);
09003       return GEP;
09004     }
09005   }
09006 
09007   if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
09008     if (DestVTy->getNumElements() == 1) {
09009       if (!isa<VectorType>(SrcTy)) {
09010         Value *Elem = InsertCastBefore(Instruction::BitCast, Src,
09011                                        DestVTy->getElementType(), CI);
09012         return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
09013                                          Constant::getNullValue(Type::getInt32Ty(*Context)));
09014       }
09015       // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
09016     }
09017   }
09018 
09019   if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
09020     if (SrcVTy->getNumElements() == 1) {
09021       if (!isa<VectorType>(DestTy)) {
09022         Instruction *Elem =
09023           ExtractElementInst::Create(Src, Constant::getNullValue(Type::getInt32Ty(*Context)));
09024         InsertNewInstBefore(Elem, CI);
09025         return CastInst::Create(Instruction::BitCast, Elem, DestTy);
09026       }
09027     }
09028   }
09029 
09030   if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
09031     if (SVI->hasOneUse()) {
09032       // Okay, we have (bitconvert (shuffle ..)).  Check to see if this is
09033       // a bitconvert to a vector with the same # elts.
09034       if (isa<VectorType>(DestTy) && 
09035           cast<VectorType>(DestTy)->getNumElements() ==
09036                 SVI->getType()->getNumElements() &&
09037           SVI->getType()->getNumElements() ==
09038             cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) {
09039         CastInst *Tmp;
09040         // If either of the operands is a cast from CI.getType(), then
09041         // evaluating the shuffle in the casted destination's type will allow
09042         // us to eliminate at least one cast.
09043         if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && 
09044              Tmp->getOperand(0)->getType() == DestTy) ||
09045             ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && 
09046              Tmp->getOperand(0)->getType() == DestTy)) {
09047           Value *LHS = InsertCastBefore(Instruction::BitCast,
09048                                         SVI->getOperand(0), DestTy, CI);
09049           Value *RHS = InsertCastBefore(Instruction::BitCast,
09050                                         SVI->getOperand(1), DestTy, CI);
09051           // Return a new shuffle vector.  Use the same element ID's, as we
09052           // know the vector types match #elts.
09053           return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
09054         }
09055       }
09056     }
09057   }
09058   return 0;
09059 }
09060 
09061 /// GetSelectFoldableOperands - We want to turn code that looks like this:
09062 ///   %C = or %A, %B
09063 ///   %D = select %cond, %C, %A
09064 /// into:
09065 ///   %C = select %cond, %B, 0
09066 ///   %D = or %A, %C
09067 ///
09068 /// Assuming that the specified instruction is an operand to the select, return
09069 /// a bitmask indicating which operands of this instruction are foldable if they
09070 /// equal the other incoming value of the select.
09071 ///
09072 static unsigned GetSelectFoldableOperands(Instruction *I) {
09073   switch (I->getOpcode()) {
09074   case Instruction::Add:
09075   case Instruction::Mul:
09076   case Instruction::And:
09077   case Instruction::Or:
09078   case Instruction::Xor:
09079     return 3;              // Can fold through either operand.
09080   case Instruction::Sub:   // Can only fold on the amount subtracted.
09081   case Instruction::Shl:   // Can only fold on the shift amount.
09082   case Instruction::LShr:
09083   case Instruction::AShr:
09084     return 1;
09085   default:
09086     return 0;              // Cannot fold
09087   }
09088 }
09089 
09090 /// GetSelectFoldableConstant - For the same transformation as the previous
09091 /// function, return the identity constant that goes into the select.
09092 static Constant *GetSelectFoldableConstant(Instruction *I,
09093                                            LLVMContext *Context) {
09094   switch (I->getOpcode()) {
09095   default: llvm_unreachable("This cannot happen!");
09096   case Instruction::Add:
09097   case Instruction::Sub:
09098   case Instruction::Or:
09099   case Instruction::Xor:
09100   case Instruction::Shl:
09101   case Instruction::LShr:
09102   case Instruction::AShr:
09103     return Constant::getNullValue(I->getType());
09104   case Instruction::And:
09105     return Constant::getAllOnesValue(I->getType());
09106   case Instruction::Mul:
09107     return ConstantInt::get(I->getType(), 1);
09108   }
09109 }
09110 
09111 /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
09112 /// have the same opcode and only one use each.  Try to simplify this.
09113 Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
09114                                           Instruction *FI) {
09115   if (TI->getNumOperands() == 1) {
09116     // If this is a non-volatile load or a cast from the same type,
09117     // merge.
09118     if (TI->isCast()) {
09119       if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType())
09120         return 0;
09121     } else {
09122       return 0;  // unknown unary op.
09123     }
09124 
09125     // Fold this by inserting a select from the input values.
09126     SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0),
09127                                           FI->getOperand(0), SI.getName()+".v");
09128     InsertNewInstBefore(NewSI, SI);
09129     return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, 
09130                             TI->getType());
09131   }
09132 
09133   // Only handle binary operators here.
09134   if (!isa<BinaryOperator>(TI))
09135     return 0;
09136 
09137   // Figure out if the operations have any operands in common.
09138   Value *MatchOp, *OtherOpT, *OtherOpF;
09139   bool MatchIsOpZero;
09140   if (TI->getOperand(0) == FI->getOperand(0)) {
09141     MatchOp  = TI->getOperand(0);
09142     OtherOpT = TI->getOperand(1);
09143     OtherOpF = FI->getOperand(1);
09144     MatchIsOpZero = true;
09145   } else if (TI->getOperand(1) == FI->getOperand(1)) {
09146     MatchOp  = TI->getOperand(1);
09147     OtherOpT = TI->getOperand(0);
09148     OtherOpF = FI->getOperand(0);
09149     MatchIsOpZero = false;
09150   } else if (!TI->isCommutative()) {
09151     return 0;
09152   } else if (TI->getOperand(0) == FI->getOperand(1)) {
09153     MatchOp  = TI->getOperand(0);
09154     OtherOpT = TI->getOperand(1);
09155     OtherOpF = FI->getOperand(0);
09156     MatchIsOpZero = true;
09157   } else if (TI->getOperand(1) == FI->getOperand(0)) {
09158     MatchOp  = TI->getOperand(1);
09159     OtherOpT = TI->getOperand(0);
09160     OtherOpF = FI->getOperand(1);
09161     MatchIsOpZero = true;
09162   } else {
09163     return 0;
09164   }
09165 
09166   // If we reach here, they do have operations in common.
09167   SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT,
09168                                          OtherOpF, SI.getName()+".v");
09169   InsertNewInstBefore(NewSI, SI);
09170 
09171   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
09172     if (MatchIsOpZero)
09173       return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI);
09174     else
09175       return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp);
09176   }
09177   llvm_unreachable("Shouldn't get here");
09178   return 0;
09179 }
09180 
09181 static bool isSelect01(Constant *C1, Constant *C2) {
09182   ConstantInt *C1I = dyn_cast<ConstantInt>(C1);
09183   if (!C1I)
09184     return false;
09185   ConstantInt *C2I = dyn_cast<ConstantInt>(C2);
09186   if (!C2I)
09187     return false;
09188   return (C1I->isZero() || C1I->isOne()) && (C2I->isZero() || C2I->isOne());
09189 }
09190 
09191 /// FoldSelectIntoOp - Try fold the select into one of the operands to
09192 /// facilitate further optimization.
09193 Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
09194                                             Value *FalseVal) {
09195   // See the comment above GetSelectFoldableOperands for a description of the
09196   // transformation we are doing here.
09197   if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
09198     if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
09199         !isa<Constant>(FalseVal)) {
09200       if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
09201         unsigned OpToFold = 0;
09202         if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
09203           OpToFold = 1;
09204         } else  if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
09205           OpToFold = 2;
09206         }
09207 
09208         if (OpToFold) {
09209           Constant *C = GetSelectFoldableConstant(TVI, Context);
09210           Value *OOp = TVI->getOperand(2-OpToFold);
09211           // Avoid creating select between 2 constants unless it's selecting
09212           // between 0 and 1.
09213           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
09214             Instruction *NewSel = SelectInst::Create(SI.getCondition(), OOp, C);
09215             InsertNewInstBefore(NewSel, SI);
09216             NewSel->takeName(TVI);
09217             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
09218               return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel);
09219             llvm_unreachable("Unknown instruction!!");
09220           }
09221         }
09222       }
09223     }
09224   }
09225 
09226   if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
09227     if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
09228         !isa<Constant>(TrueVal)) {
09229       if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
09230         unsigned OpToFold = 0;
09231         if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
09232           OpToFold = 1;
09233         } else  if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
09234           OpToFold = 2;
09235         }
09236 
09237         if (OpToFold) {
09238           Constant *C = GetSelectFoldableConstant(FVI, Context);
09239           Value *OOp = FVI->getOperand(2-OpToFold);
09240           // Avoid creating select between 2 constants unless it's selecting
09241           // between 0 and 1.
09242           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
09243             Instruction *NewSel = SelectInst::Create(SI.getCondition(), C, OOp);
09244             InsertNewInstBefore(NewSel, SI);
09245             NewSel->takeName(FVI);
09246             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
09247               return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel);
09248             llvm_unreachable("Unknown instruction!!");
09249           }
09250         }
09251       }
09252     }
09253   }
09254 
09255   return 0;
09256 }
09257 
09258 /// visitSelectInstWithICmp - Visit a SelectInst that has an
09259 /// ICmpInst as its first operand.
09260 ///
09261 Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
09262                                                    ICmpInst *ICI) {
09263   bool Changed = false;
09264   ICmpInst::Predicate Pred = ICI->getPredicate();
09265   Value *CmpLHS = ICI->getOperand(0);
09266   Value *CmpRHS = ICI->getOperand(1);
09267   Value *TrueVal = SI.getTrueValue();
09268   Value *FalseVal = SI.getFalseValue();
09269 
09270   // Check cases where the comparison is with a constant that
09271   // can be adjusted to fit the min/max idiom. We may edit ICI in
09272   // place here, so make sure the select is the only user.
09273   if (ICI->hasOneUse())
09274     if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
09275       switch (Pred) {
09276       default: break;
09277       case ICmpInst::ICMP_ULT:
09278       case ICmpInst::ICMP_SLT: {
09279         // X < MIN ? T : F  -->  F
09280         if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
09281           return ReplaceInstUsesWith(SI, FalseVal);
09282         // X < C ? X : C-1  -->  X > C-1 ? C-1 : X
09283         Constant *AdjustedRHS = SubOne(CI);
09284         if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
09285             (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
09286           Pred = ICmpInst::getSwappedPredicate(Pred);
09287           CmpRHS = AdjustedRHS;
09288           std::swap(FalseVal, TrueVal);
09289           ICI->setPredicate(Pred);
09290           ICI->setOperand(1, CmpRHS);
09291           SI.setOperand(1, TrueVal);
09292           SI.setOperand(2, FalseVal);
09293           Changed = true;
09294         }
09295         break;
09296       }
09297       case ICmpInst::ICMP_UGT:
09298       case ICmpInst::ICMP_SGT: {
09299         // X > MAX ? T : F  -->  F
09300         if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
09301           return ReplaceInstUsesWith(SI, FalseVal);
09302         // X > C ? X : C+1  -->  X < C+1 ? C+1 : X
09303         Constant *AdjustedRHS = AddOne(CI);
09304         if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
09305             (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
09306           Pred = ICmpInst::getSwappedPredicate(Pred);
09307           CmpRHS = AdjustedRHS;
09308           std::swap(FalseVal, TrueVal);
09309           ICI->setPredicate(Pred);
09310           ICI->setOperand(1, CmpRHS);
09311           SI.setOperand(1, TrueVal);
09312           SI.setOperand(2, FalseVal);
09313           Changed = true;
09314         }
09315         break;
09316       }
09317       }
09318 
09319       // (x <s 0) ? -1 : 0 -> ashr x, 31   -> all ones if signed
09320       // (x >s -1) ? -1 : 0 -> ashr x, 31  -> all ones if not signed
09321       CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
09322       if (match(TrueVal, m_ConstantInt<-1>()) &&
09323           match(FalseVal, m_ConstantInt<0>()))
09324         Pred = ICI->getPredicate();
09325       else if (match(TrueVal, m_ConstantInt<0>()) &&
09326                match(FalseVal, m_ConstantInt<-1>()))
09327         Pred = CmpInst::getInversePredicate(ICI->getPredicate());
09328       
09329       if (Pred != CmpInst::BAD_ICMP_PREDICATE) {
09330         // If we are just checking for a icmp eq of a single bit and zext'ing it
09331         // to an integer, then shift the bit to the appropriate place and then
09332         // cast to integer to avoid the comparison.
09333         const APInt &Op1CV = CI->getValue();
09334     
09335         // sext (x <s  0) to i32 --> x>>s31      true if signbit set.
09336         // sext (x >s -1) to i32 --> (x>>s31)^-1  true if signbit clear.
09337         if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) ||
09338             (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) {
09339           Value *In = ICI->getOperand(0);
09340           Value *Sh = ConstantInt::get(In->getType(),
09341                                        In->getType()->getScalarSizeInBits()-1);
09342           In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh,
09343                                                         In->getName()+".lobit"),
09344                                    *ICI);
09345           if (In->getType() != SI.getType())
09346             In = CastInst::CreateIntegerCast(In, SI.getType(),
09347                                              true/*SExt*/, "tmp", ICI);
09348     
09349           if (Pred == ICmpInst::ICMP_SGT)
09350             In = InsertNewInstBefore(BinaryOperator::CreateNot(In,
09351                                        In->getName()+".not"), *ICI);
09352     
09353           return ReplaceInstUsesWith(SI, In);
09354         }
09355       }
09356     }
09357 
09358   if (CmpLHS == TrueVal && CmpRHS == FalseVal) {
09359     // Transform (X == Y) ? X : Y  -> Y
09360     if (Pred == ICmpInst::ICMP_EQ)
09361       return ReplaceInstUsesWith(SI, FalseVal);
09362     // Transform (X != Y) ? X : Y  -> X
09363     if (Pred == ICmpInst::ICMP_NE)
09364       return ReplaceInstUsesWith(SI, TrueVal);
09365     /// NOTE: if we wanted to, this is where to detect integer MIN/MAX
09366 
09367   } else if (CmpLHS == FalseVal && CmpRHS == TrueVal) {
09368     // Transform (X == Y) ? Y : X  -> X
09369     if (Pred == ICmpInst::ICMP_EQ)
09370       return ReplaceInstUsesWith(SI, FalseVal);
09371     // Transform (X != Y) ? Y : X  -> Y
09372     if (Pred == ICmpInst::ICMP_NE)
09373       return ReplaceInstUsesWith(SI, TrueVal);
09374     /// NOTE: if we wanted to, this is where to detect integer MIN/MAX
09375   }
09376 
09377   /// NOTE: if we wanted to, this is where to detect integer ABS
09378 
09379   return Changed ? &SI : 0;
09380 }
09381 
09382 Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
09383   Value *CondVal = SI.getCondition();
09384   Value *TrueVal = SI.getTrueValue();
09385   Value *FalseVal = SI.getFalseValue();
09386 
09387   // select true, X, Y  -> X
09388   // select false, X, Y -> Y
09389   if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal))
09390     return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal);
09391 
09392   // select C, X, X -> X
09393   if (TrueVal == FalseVal)
09394     return ReplaceInstUsesWith(SI, TrueVal);
09395 
09396   if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X
09397     return ReplaceInstUsesWith(SI, FalseVal);
09398   if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X
09399     return ReplaceInstUsesWith(SI, TrueVal);
09400   if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y
09401     if (isa<Constant>(TrueVal))
09402       return ReplaceInstUsesWith(SI, TrueVal);
09403     else
09404       return ReplaceInstUsesWith(SI, FalseVal);
09405   }
09406 
09407   if (SI.getType() == Type::getInt1Ty(*Context)) {
09408     if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
09409       if (C->getZExtValue()) {
09410         // Change: A = select B, true, C --> A = or B, C
09411         return BinaryOperator::CreateOr(CondVal, FalseVal);
09412       } else {
09413         // Change: A = select B, false, C --> A = and !B, C
09414         Value *NotCond =
09415           InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
09416                                              "not."+CondVal->getName()), SI);
09417         return BinaryOperator::CreateAnd(NotCond, FalseVal);
09418       }
09419     } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
09420       if (C->getZExtValue() == false) {
09421         // Change: A = select B, C, false --> A = and B, C
09422         return BinaryOperator::CreateAnd(CondVal, TrueVal);
09423       } else {
09424         // Change: A = select B, C, true --> A = or !B, C
09425         Value *NotCond =
09426           InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
09427                                              "not."+CondVal->getName()), SI);
09428         return BinaryOperator::CreateOr(NotCond, TrueVal);
09429       }
09430     }
09431     
09432     // select a, b, a  -> a&b
09433     // select a, a, b  -> a|b
09434     if (CondVal == TrueVal)
09435       return BinaryOperator::CreateOr(CondVal, FalseVal);
09436     else if (CondVal == FalseVal)
09437       return BinaryOperator::CreateAnd(CondVal, TrueVal);
09438   }
09439 
09440   // Selecting between two integer constants?
09441   if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
09442     if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
09443       // select C, 1, 0 -> zext C to int
09444       if (FalseValC->isZero() && TrueValC->getValue() == 1) {
09445         return CastInst::Create(Instruction::ZExt, CondVal, SI.getType());
09446       } else if (TrueValC->isZero() && FalseValC->getValue() == 1) {
09447         // select C, 0, 1 -> zext !C to int
09448         Value *NotCond =
09449           InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
09450                                                "not."+CondVal->getName()), SI);
09451         return CastInst::Create(Instruction::ZExt, NotCond, SI.getType());
09452       }
09453 
09454       if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) {
09455         // If one of the constants is zero (we know they can't both be) and we
09456         // have an icmp instruction with zero, and we have an 'and' with the
09457         // non-constant value, eliminate this whole mess.  This corresponds to
09458         // cases like this: ((X & 27) ? 27 : 0)
09459         if (TrueValC->isZero() || FalseValC->isZero())
09460           if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) &&
09461               cast<Constant>(IC->getOperand(1))->isNullValue())
09462             if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0)))
09463               if (ICA->getOpcode() == Instruction::And &&
09464                   isa<ConstantInt>(ICA->getOperand(1)) &&
09465                   (ICA->getOperand(1) == TrueValC ||
09466                    ICA->getOperand(1) == FalseValC) &&
09467                   isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) {
09468                 // Okay, now we know that everything is set up, we just don't
09469                 // know whether we have a icmp_ne or icmp_eq and whether the 
09470                 // true or false val is the zero.
09471                 bool ShouldNotVal = !TrueValC->isZero();
09472                 ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
09473                 Value *V = ICA;
09474                 if (ShouldNotVal)
09475                   V = InsertNewInstBefore(BinaryOperator::Create(
09476                                   Instruction::Xor, V, ICA->getOperand(1)), SI);
09477                 return ReplaceInstUsesWith(SI, V);
09478               }
09479       }
09480     }
09481 
09482   // See if we are selecting two values based on a comparison of the two values.
09483   if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
09484     if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
09485       // Transform (X == Y) ? X : Y  -> Y
09486       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
09487         // This is not safe in general for floating point:  
09488         // consider X== -0, Y== +0.
09489         // It becomes safe if either operand is a nonzero constant.
09490         ConstantFP *CFPt, *CFPf;
09491         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
09492               !CFPt->getValueAPF().isZero()) ||
09493             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
09494              !CFPf->getValueAPF().isZero()))
09495         return ReplaceInstUsesWith(SI, FalseVal);
09496       }
09497       // Transform (X != Y) ? X : Y  -> X
09498       if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
09499         return ReplaceInstUsesWith(SI, TrueVal);
09500       // NOTE: if we wanted to, this is where to detect MIN/MAX
09501 
09502     } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
09503       // Transform (X == Y) ? Y : X  -> X
09504       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
09505         // This is not safe in general for floating point:  
09506         // consider X== -0, Y== +0.
09507         // It becomes safe if either operand is a nonzero constant.
09508         ConstantFP *CFPt, *CFPf;
09509         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
09510               !CFPt->getValueAPF().isZero()) ||
09511             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
09512              !CFPf->getValueAPF().isZero()))
09513           return ReplaceInstUsesWith(SI, FalseVal);
09514       }
09515       // Transform (X != Y) ? Y : X  -> Y
09516       if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
09517         return ReplaceInstUsesWith(SI, TrueVal);
09518       // NOTE: if we wanted to, this is where to detect MIN/MAX
09519     }
09520     // NOTE: if we wanted to, this is where to detect ABS
09521   }
09522 
09523   // See if we are selecting two values based on a comparison of the two values.
09524   if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
09525     if (Instruction *Result = visitSelectInstWithICmp(SI, ICI))
09526       return Result;
09527 
09528   if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
09529     if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
09530       if (TI->hasOneUse() && FI->hasOneUse()) {
09531         Instruction *AddOp = 0, *SubOp = 0;
09532 
09533         // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
09534         if (TI->getOpcode() == FI->getOpcode())
09535           if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
09536             return IV;
09537 
09538         // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))).  This is
09539         // even legal for FP.
09540         if ((TI->getOpcode() == Instruction::Sub &&
09541              FI->getOpcode() == Instruction::Add) ||
09542             (TI->getOpcode() == Instruction::FSub &&
09543              FI->getOpcode() == Instruction::FAdd)) {
09544           AddOp = FI; SubOp = TI;
09545         } else if ((FI->getOpcode() == Instruction::Sub &&
09546                     TI->getOpcode() == Instruction::Add) ||
09547                    (FI->getOpcode() == Instruction::FSub &&
09548                     TI->getOpcode() == Instruction::FAdd)) {
09549           AddOp = TI; SubOp = FI;
09550         }
09551 
09552         if (AddOp) {
09553           Value *OtherAddOp = 0;
09554           if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
09555             OtherAddOp = AddOp->getOperand(1);
09556           } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
09557             OtherAddOp = AddOp->getOperand(0);
09558           }
09559 
09560           if (OtherAddOp) {
09561             // So at this point we know we have (Y -> OtherAddOp):
09562             //        select C, (add X, Y), (sub X, Z)
09563             Value *NegVal;  // Compute -Z
09564             if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) {
09565               NegVal = ConstantExpr::getNeg(C);
09566             } else {
09567               NegVal = InsertNewInstBefore(
09568                     BinaryOperator::CreateNeg(SubOp->getOperand(1),
09569                                               "tmp"), SI);
09570             }
09571 
09572             Value *NewTrueOp = OtherAddOp;
09573             Value *NewFalseOp = NegVal;
09574             if (AddOp != TI)
09575               std::swap(NewTrueOp, NewFalseOp);
09576             Instruction *NewSel =
09577               SelectInst::Create(CondVal, NewTrueOp,
09578                                  NewFalseOp, SI.getName() + ".p");
09579 
09580             NewSel = InsertNewInstBefore(NewSel, SI);
09581             return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
09582           }
09583         }
09584       }
09585 
09586   // See if we can fold the select into one of our operands.
09587   if (SI.getType()->isInteger()) {
09588     Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal);
09589     if (FoldI)
09590       return FoldI;
09591   }
09592 
09593   if (BinaryOperator::isNot(CondVal)) {
09594     SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
09595     SI.setOperand(1, FalseVal);
09596     SI.setOperand(2, TrueVal);
09597     return &SI;
09598   }
09599 
09600   return 0;
09601 }
09602 
09603 /// EnforceKnownAlignment - If the specified pointer points to an object that
09604 /// we control, modify the object's alignment to PrefAlign. This isn't
09605 /// often possible though. If alignment is important, a more reliable approach
09606 /// is to simply align all global variables and allocation instructions to
09607 /// their preferred alignment from the beginning.
09608 ///
09609 static unsigned EnforceKnownAlignment(Value *V,
09610                                       unsigned Align, unsigned PrefAlign) {
09611 
09612   User *U = dyn_cast<User>(V);
09613   if (!U) return Align;
09614 
09615   switch (Operator::getOpcode(U)) {
09616   default: break;
09617   case Instruction::BitCast:
09618     return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
09619   case Instruction::GetElementPtr: {
09620     // If all indexes are zero, it is just the alignment of the base pointer.
09621     bool AllZeroOperands = true;
09622     for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
09623       if (!isa<Constant>(*i) ||
09624           !cast<Constant>(*i)->isNullValue()) {
09625         AllZeroOperands = false;
09626         break;
09627       }
09628 
09629     if (AllZeroOperands) {
09630       // Treat this like a bitcast.
09631       return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
09632     }
09633     break;
09634   }
09635   }
09636 
09637   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
09638     // If there is a large requested alignment and we can, bump up the alignment
09639     // of the global.
09640     if (!GV->isDeclaration()) {
09641       if (GV->getAlignment() >= PrefAlign)
09642         Align = GV->getAlignment();
09643       else {
09644         GV->setAlignment(PrefAlign);
09645         Align = PrefAlign;
09646       }
09647     }
09648   } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
09649     // If there is a requested alignment and if this is an alloca, round up.  We
09650     // don't do this for malloc, because some systems can't respect the request.
09651     if (isa<AllocaInst>(AI)) {
09652       if (AI->getAlignment() >= PrefAlign)
09653         Align = AI->getAlignment();
09654       else {
09655         AI->setAlignment(PrefAlign);
09656         Align = PrefAlign;
09657       }
09658     }
09659   }
09660 
09661   return Align;
09662 }
09663 
09664 /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that
09665 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
09666 /// and it is more than the alignment of the ultimate object, see if we can
09667 /// increase the alignment of the ultimate object, making this check succeed.
09668 unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V,
09669                                                   unsigned PrefAlign) {
09670   unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) :
09671                       sizeof(PrefAlign) * CHAR_BIT;
09672   APInt Mask = APInt::getAllOnesValue(BitWidth);
09673   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
09674   ComputeMaskedBits(V, Mask, KnownZero, KnownOne);
09675   unsigned TrailZ = KnownZero.countTrailingOnes();
09676   unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
09677 
09678   if (PrefAlign > Align)
09679     Align = EnforceKnownAlignment(V, Align, PrefAlign);
09680   
09681     // We don't need to make any adjustment.
09682   return Align;
09683 }
09684 
09685 Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
09686   unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
09687   unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
09688   unsigned MinAlign = std::min(DstAlign, SrcAlign);
09689   unsigned CopyAlign = MI->getAlignment();
09690 
09691   if (CopyAlign < MinAlign) {
09692     MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), 
09693                                              MinAlign, false));
09694     return MI;
09695   }
09696   
09697   // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
09698   // load/store.
09699   ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3));
09700   if (MemOpLength == 0) return 0;
09701   
09702   // Source and destination pointer types are always "i8*" for intrinsic.  See
09703   // if the size is something we can handle with a single primitive load/store.
09704   // A single load+store correctly handles overlapping memory in the memmove
09705   // case.
09706   unsigned Size = MemOpLength->getZExtValue();
09707   if (Size == 0) return MI;  // Delete this mem transfer.
09708   
09709   if (Size > 8 || (Size&(Size-1)))
09710     return 0;  // If not 1/2/4/8 bytes, exit.
09711   
09712   // Use an integer load+store unless we can find something better.
09713   Type *NewPtrTy =
09714                 PointerType::getUnqual(IntegerType::get(*Context, Size<<3));
09715   
09716   // Memcpy forces the use of i8* for the source and destination.  That means
09717   // that if you're using memcpy to move one double around, you'll get a cast
09718   // from double* to i8*.  We'd much rather use a double load+store rather than
09719   // an i64 load+store, here because this improves the odds that the source or
09720   // dest address will be promotable.  See if we can find a better type than the
09721   // integer datatype.
09722   if (Value *Op = getBitCastOperand(MI->getOperand(1))) {
09723     const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType();
09724     if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
09725       // The SrcETy might be something like {{{double}}} or [1 x double].  Rip
09726       // down through these levels if so.
09727       while (!SrcETy->isSingleValueType()) {
09728         if (const StructType *STy = dyn_cast<StructType>(SrcETy)) {
09729           if (STy->getNumElements() == 1)
09730             SrcETy = STy->getElementType(0);
09731           else
09732             break;
09733         } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
09734           if (ATy->getNumElements() == 1)
09735             SrcETy = ATy->getElementType();
09736           else
09737             break;
09738         } else
09739           break;
09740       }
09741       
09742       if (SrcETy->isSingleValueType())
09743         NewPtrTy = PointerType::getUnqual(SrcETy);
09744     }
09745   }
09746   
09747   
09748   // If the memcpy/memmove provides better alignment info than we can
09749   // infer, use it.
09750   SrcAlign = std::max(SrcAlign, CopyAlign);
09751   DstAlign = std::max(DstAlign, CopyAlign);
09752   
09753   Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI);
09754   Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI);
09755   Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign);
09756   InsertNewInstBefore(L, *MI);
09757   InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
09758 
09759   // Set the size of the copy to 0, it will be deleted on the next iteration.
09760   MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
09761   return MI;
09762 }
09763 
09764 Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
09765   unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
09766   if (MI->getAlignment() < Alignment) {
09767     MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
09768                                              Alignment, false));
09769     return MI;
09770   }
09771   
09772   // Extract the length and alignment and fill if they are constant.
09773   ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
09774   ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
09775   if (!LenC || !FillC || FillC->getType() != Type::getInt8Ty(*Context))
09776     return 0;
09777   uint64_t Len = LenC->getZExtValue();
09778   Alignment = MI->getAlignment();
09779   
09780   // If the length is zero, this is a no-op
09781   if (Len == 0) return MI; // memset(d,c,0,a) -> noop
09782   
09783   // memset(s,c,n) -> store s, c (for n=1,2,4,8)
09784   if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
09785     const Type *ITy = IntegerType::get(*Context, Len*8);  // n=1 -> i8.
09786     
09787     Value *Dest = MI->getDest();
09788     Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI);
09789 
09790     // Alignment 0 is identity for alignment 1 for memset, but not store.
09791     if (Alignment == 0) Alignment = 1;
09792     
09793     // Extract the fill value and store.
09794     uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
09795     InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill),
09796                                       Dest, false, Alignment), *MI);
09797     
09798     // Set the size of the copy to 0, it will be deleted on the next iteration.
09799     MI->setLength(Constant::getNullValue(LenC->getType()));
09800     return MI;
09801   }
09802 
09803   return 0;
09804 }
09805 
09806 
09807 /// visitCallInst - CallInst simplification.  This mostly only handles folding 
09808 /// of intrinsic instructions.  For normal calls, it allows visitCallSite to do
09809 /// the heavy lifting.
09810 ///
09811 Instruction *InstCombiner::visitCallInst(CallInst &CI) {
09812   // If the caller function is nounwind, mark the call as nounwind, even if the
09813   // callee isn't.
09814   if (CI.getParent()->getParent()->doesNotThrow() &&
09815       !CI.doesNotThrow()) {
09816     CI.setDoesNotThrow();
09817     return &CI;
09818   }
09819   
09820   
09821   
09822   IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
09823   if (!II) return visitCallSite(&CI);
09824   
09825   // Intrinsics cannot occur in an invoke, so handle them here instead of in
09826   // visitCallSite.
09827   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
09828     bool Changed = false;
09829 
09830     // memmove/cpy/set of zero bytes is a noop.
09831     if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
09832       if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
09833 
09834       if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
09835         if (CI->getZExtValue() == 1) {
09836           // Replace the instruction with just byte operations.  We would
09837           // transform other cases to loads/stores, but we don't know if
09838           // alignment is sufficient.
09839         }
09840     }
09841 
09842     // If we have a memmove and the source operation is a constant global,
09843     // then the source and dest pointers can't alias, so we can change this
09844     // into a call to memcpy.
09845     if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
09846       if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
09847         if (GVSrc->isConstant()) {
09848           Module *M = CI.getParent()->getParent()->getParent();
09849           Intrinsic::ID MemCpyID = Intrinsic::memcpy;
09850           const Type *Tys[1];
09851           Tys[0] = CI.getOperand(3)->getType();
09852           CI.setOperand(0, 
09853                         Intrinsic::getDeclaration(M, MemCpyID, Tys, 1));
09854           Changed = true;
09855         }
09856 
09857       // memmove(x,x,size) -> noop.
09858       if (MMI->getSource() == MMI->getDest())
09859         return EraseInstFromFunction(CI);
09860     }
09861 
09862     // If we can determine a pointer alignment that is bigger than currently
09863     // set, update the alignment.
09864     if (isa<MemTransferInst>(MI)) {
09865       if (Instruction *I = SimplifyMemTransfer(MI))
09866         return I;
09867     } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
09868       if (Instruction *I = SimplifyMemSet(MSI))
09869         return I;
09870     }
09871           
09872     if (Changed) return II;
09873   }
09874   
09875   switch (II->getIntrinsicID()) {
09876   default: break;
09877   case Intrinsic::bswap:
09878     // bswap(bswap(x)) -> x
09879     if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1)))
09880       if (Operand->getIntrinsicID() == Intrinsic::bswap)
09881         return ReplaceInstUsesWith(CI, Operand->getOperand(1));
09882     break;
09883   case Intrinsic::ppc_altivec_lvx:
09884   case Intrinsic::ppc_altivec_lvxl:
09885   case Intrinsic::x86_sse_loadu_ps:
09886   case Intrinsic::x86_sse2_loadu_pd:
09887   case Intrinsic::x86_sse2_loadu_dq:
09888     // Turn PPC lvx     -> load if the pointer is known aligned.
09889     // Turn X86 loadups -> load if the pointer is known aligned.
09890     if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
09891       Value *Ptr = InsertBitCastBefore(II->getOperand(1),
09892                                    PointerType::getUnqual(II->getType()),
09893                                        CI);
09894       return new LoadInst(Ptr);
09895     }
09896     break;
09897   case Intrinsic::ppc_altivec_stvx:
09898   case Intrinsic::ppc_altivec_stvxl:
09899     // Turn stvx -> store if the pointer is known aligned.
09900     if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
09901       const Type *OpPtrTy = 
09902         PointerType::getUnqual(II->getOperand(1)->getType());
09903       Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI);
09904       return new StoreInst(II->getOperand(1), Ptr);
09905     }
09906     break;
09907   case Intrinsic::x86_sse_storeu_ps:
09908   case Intrinsic::x86_sse2_storeu_pd:
09909   case Intrinsic::x86_sse2_storeu_dq:
09910     // Turn X86 storeu -> store if the pointer is known aligned.
09911     if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
09912       const Type *OpPtrTy = 
09913         PointerType::getUnqual(II->getOperand(2)->getType());
09914       Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI);
09915       return new StoreInst(II->getOperand(2), Ptr);
09916     }
09917     break;
09918     
09919   case Intrinsic::x86_sse_cvttss2si: {
09920     // These intrinsics only demands the 0th element of its input vector.  If
09921     // we can simplify the input based on that, do so now.
09922     unsigned VWidth =
09923       cast<VectorType>(II->getOperand(1)->getType())->getNumElements();
09924     APInt DemandedElts(VWidth, 1);
09925     APInt UndefElts(VWidth, 0);
09926     if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
09927                                               UndefElts)) {
09928       II->setOperand(1, V);
09929       return II;
09930     }
09931     break;
09932   }
09933     
09934   case Intrinsic::ppc_altivec_vperm:
09935     // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
09936     if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) {
09937       assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
09938       
09939       // Check that all of the elements are integer constants or undefs.
09940       bool AllEltsOk = true;
09941       for (unsigned i = 0; i != 16; ++i) {
09942         if (!isa<ConstantInt>(Mask->getOperand(i)) && 
09943             !isa<UndefValue>(Mask->getOperand(i))) {
09944           AllEltsOk = false;
09945           break;
09946         }
09947       }
09948       
09949       if (AllEltsOk) {
09950         // Cast the input vectors to byte vectors.
09951         Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI);
09952         Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI);
09953         Value *Result = UndefValue::get(Op0->getType());
09954         
09955         // Only extract each element once.
09956         Value *ExtractedElts[32];
09957         memset(ExtractedElts, 0, sizeof(ExtractedElts));
09958         
09959         for (unsigned i = 0; i != 16; ++i) {
09960           if (isa<UndefValue>(Mask->getOperand(i)))
09961             continue;
09962           unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
09963           Idx &= 31;  // Match the hardware behavior.
09964           
09965           if (ExtractedElts[Idx] == 0) {
09966             Instruction *Elt = 
09967               ExtractElementInst::Create(Idx < 16 ? Op0 : Op1, 
09968                   ConstantInt::get(Type::getInt32Ty(*Context), Idx&15, false), "tmp");
09969             InsertNewInstBefore(Elt, CI);
09970             ExtractedElts[Idx] = Elt;
09971           }
09972         
09973           // Insert this value into the result vector.
09974           Result = InsertElementInst::Create(Result, ExtractedElts[Idx],
09975                                ConstantInt::get(Type::getInt32Ty(*Context), i, false), 
09976                                "tmp");
09977           InsertNewInstBefore(cast<Instruction>(Result), CI);
09978         }
09979         return CastInst::Create(Instruction::BitCast, Result, CI.getType());
09980       }
09981     }
09982     break;
09983 
09984   case Intrinsic::stackrestore: {
09985     // If the save is right next to the restore, remove the restore.  This can
09986     // happen when variable allocas are DCE'd.
09987     if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
09988       if (SS->getIntrinsicID() == Intrinsic::stacksave) {
09989         BasicBlock::iterator BI = SS;
09990         if (&*++BI == II)
09991           return EraseInstFromFunction(CI);
09992       }
09993     }
09994     
09995     // Scan down this block to see if there is another stack restore in the
09996     // same block without an intervening call/alloca.
09997     BasicBlock::iterator BI = II;
09998     TerminatorInst *TI = II->getParent()->getTerminator();
09999     bool CannotRemove = false;
10000     for (++BI; &*BI != TI; ++BI) {
10001       if (isa<AllocaInst>(BI)) {
10002         CannotRemove = true;
10003         break;
10004       }
10005       if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
10006         if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
10007           // If there is a stackrestore below this one, remove this one.
10008           if (II->getIntrinsicID() == Intrinsic::stackrestore)
10009             return EraseInstFromFunction(CI);
10010           // Otherwise, ignore the intrinsic.
10011         } else {
10012           // If we found a non-intrinsic call, we can't remove the stack
10013           // restore.
10014           CannotRemove = true;
10015           break;
10016         }
10017       }
10018     }
10019     
10020     // If the stack restore is in a return/unwind block and if there are no
10021     // allocas or calls between the restore and the return, nuke the restore.
10022     if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
10023       return EraseInstFromFunction(CI);
10024     break;
10025   }
10026   }
10027 
10028   return visitCallSite(II);
10029 }
10030 
10031 // InvokeInst simplification
10032 //
10033 Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
10034   return visitCallSite(&II);
10035 }
10036 
10037 /// isSafeToEliminateVarargsCast - If this cast does not affect the value 
10038 /// passed through the varargs area, we can eliminate the use of the cast.
10039 static bool isSafeToEliminateVarargsCast(const CallSite CS,
10040                                          const CastInst * const CI,
10041                                          const TargetData * const TD,
10042                                          const int ix) {
10043   if (!CI->isLosslessCast())
10044     return false;
10045 
10046   // The size of ByVal arguments is derived from the type, so we
10047   // can't change to a type with a different size.  If the size were
10048   // passed explicitly we could avoid this check.
10049   if (!CS.paramHasAttr(ix, Attribute::ByVal))
10050     return true;
10051 
10052   const Type* SrcTy = 
10053             cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
10054   const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
10055   if (!SrcTy->isSized() || !DstTy->isSized())
10056     return false;
10057   if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
10058     return false;
10059   return true;
10060 }
10061 
10062 // visitCallSite - Improvements for call and invoke instructions.
10063 //
10064 Instruction *InstCombiner::visitCallSite(CallSite CS) {
10065   bool Changed = false;
10066 
10067   // If the callee is a constexpr cast of a function, attempt to move the cast
10068   // to the arguments of the call/invoke.
10069   if (transformConstExprCastCall(CS)) return 0;
10070 
10071   Value *Callee = CS.getCalledValue();
10072 
10073   if (Function *CalleeF = dyn_cast<Function>(Callee))
10074     if (CalleeF->getCallingConv() != CS.getCallingConv()) {
10075       Instruction *OldCall = CS.getInstruction();
10076       // If the call and callee calling conventions don't match, this call must
10077       // be unreachable, as the call is undefined.
10078       new StoreInst(ConstantInt::getTrue(*Context),
10079                 UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))), 
10080                                   OldCall);
10081       if (!OldCall->use_empty())
10082         OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
10083       if (isa<CallInst>(OldCall))   // Not worth removing an invoke here.
10084         return EraseInstFromFunction(*OldCall);
10085       return 0;
10086     }
10087 
10088   if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
10089     // This instruction is not reachable, just remove it.  We insert a store to
10090     // undef so that we know that this code is not reachable, despite the fact
10091     // that we can't modify the CFG here.
10092     new StoreInst(ConstantInt::getTrue(*Context),
10093                UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))),
10094                   CS.getInstruction());
10095 
10096     if (!CS.getInstruction()->use_empty())
10097       CS.getInstruction()->
10098         replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
10099 
10100     if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
10101       // Don't break the CFG, insert a dummy cond branch.
10102       BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
10103                          ConstantInt::getTrue(*Context), II);
10104     }
10105     return EraseInstFromFunction(*CS.getInstruction());
10106   }
10107 
10108   if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
10109     if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
10110       if (In->getIntrinsicID() == Intrinsic::init_trampoline)
10111         return transformCallThroughTrampoline(CS);
10112 
10113   const PointerType *PTy = cast<PointerType>(Callee->getType());
10114   const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
10115   if (FTy->isVarArg()) {
10116     int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
10117     // See if we can optimize any arguments passed through the varargs area of
10118     // the call.
10119     for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
10120            E = CS.arg_end(); I != E; ++I, ++ix) {
10121       CastInst *CI = dyn_cast<CastInst>(*I);
10122       if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
10123         *I = CI->getOperand(0);
10124         Changed = true;
10125       }
10126     }
10127   }
10128 
10129   if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
10130     // Inline asm calls cannot throw - mark them 'nounwind'.
10131     CS.setDoesNotThrow();
10132     Changed = true;
10133   }
10134 
10135   return Changed ? CS.getInstruction() : 0;
10136 }
10137 
10138 // transformConstExprCastCall - If the callee is a constexpr cast of a function,
10139 // attempt to move the cast to the arguments of the call/invoke.
10140 //
10141 bool InstCombiner::transformConstExprCastCall(CallSite CS) {
10142   if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
10143   ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
10144   if (CE->getOpcode() != Instruction::BitCast || 
10145       !isa<Function>(CE->getOperand(0)))
10146     return false;
10147   Function *Callee = cast<Function>(CE->getOperand(0));
10148   Instruction *Caller = CS.getInstruction();
10149   const AttrListPtr &CallerPAL = CS.getAttributes();
10150 
10151   // Okay, this is a cast from a function to a different type.  Unless doing so
10152   // would cause a type conversion of one of our arguments, change this call to
10153   // be a direct call with arguments casted to the appropriate types.
10154   //
10155   const FunctionType *FT = Callee->getFunctionType();
10156   const Type *OldRetTy = Caller->getType();
10157   const Type *NewRetTy = FT->getReturnType();
10158 
10159   if (isa<StructType>(NewRetTy))
10160     return false; // TODO: Handle multiple return values.
10161 
10162   // Check to see if we are changing the return type...
10163   if (OldRetTy != NewRetTy) {
10164     if (Callee->isDeclaration() &&
10165         // Conversion is ok if changing from one pointer type to another or from
10166         // a pointer to an integer of the same size.
10167         !((isa<PointerType>(OldRetTy) || !TD ||
10168            OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
10169           (isa<PointerType>(NewRetTy) || !TD ||
10170            NewRetTy == TD->getIntPtrType(Caller->getContext()))))
10171       return false;   // Cannot transform this return value.
10172 
10173     if (!Caller->use_empty() &&
10174         // void -> non-void is handled specially
10175         NewRetTy != Type::getVoidTy(*Context) && !CastInst::isCastable(NewRetTy, OldRetTy))
10176       return false;   // Cannot transform this return value.
10177 
10178     if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
10179       Attributes RAttrs = CallerPAL.getRetAttributes();
10180       if (RAttrs & Attribute::typeIncompatible(NewRetTy))
10181         return false;   // Attribute not compatible with transformed value.
10182     }
10183 
10184     // If the callsite is an invoke instruction, and the return value is used by
10185     // a PHI node in a successor, we cannot change the return type of the call
10186     // because there is no place to put the cast instruction (without breaking
10187     // the critical edge).  Bail out in this case.
10188     if (!Caller->use_empty())
10189       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
10190         for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
10191              UI != E; ++UI)
10192           if (PHINode *PN = dyn_cast<PHINode>(*UI))
10193             if (PN->getParent() == II->getNormalDest() ||
10194                 PN->getParent() == II->getUnwindDest())
10195               return false;
10196   }
10197 
10198   unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
10199   unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
10200 
10201   CallSite::arg_iterator AI = CS.arg_begin();
10202   for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
10203     const Type *ParamTy = FT->getParamType(i);
10204     const Type *ActTy = (*AI)->getType();
10205 
10206     if (!CastInst::isCastable(ActTy, ParamTy))
10207       return false;   // Cannot transform this parameter value.
10208 
10209     if (CallerPAL.getParamAttributes(i + 1) 
10210         & Attribute::typeIncompatible(ParamTy))
10211       return false;   // Attribute not compatible with transformed value.
10212 
10213     // Converting from one pointer type to another or between a pointer and an
10214     // integer of the same size is safe even if we do not have a body.
10215     bool isConvertible = ActTy == ParamTy ||
10216       (TD && ((isa<PointerType>(ParamTy) ||
10217       ParamTy == TD->getIntPtrType(Caller->getContext())) &&
10218               (isa<PointerType>(ActTy) ||
10219               ActTy == TD->getIntPtrType(Caller->getContext()))));
10220     if (Callee->isDeclaration() && !isConvertible) return false;
10221   }
10222 
10223   if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
10224       Callee->isDeclaration())
10225     return false;   // Do not delete arguments unless we have a function body.
10226 
10227   if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
10228       !CallerPAL.isEmpty())
10229     // In this case we have more arguments than the new function type, but we
10230     // won't be dropping them.  Check that these extra arguments have attributes
10231     // that are compatible with being a vararg call argument.
10232     for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
10233       if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
10234         break;
10235       Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
10236       if (PAttrs & Attribute::VarArgsIncompatible)
10237         return false;
10238     }
10239 
10240   // Okay, we decided that this is a safe thing to do: go ahead and start
10241   // inserting cast instructions as necessary...
10242   std::vector<Value*> Args;
10243   Args.reserve(NumActualArgs);
10244   SmallVector<AttributeWithIndex, 8> attrVec;
10245   attrVec.reserve(NumCommonArgs);
10246 
10247   // Get any return attributes.
10248   Attributes RAttrs = CallerPAL.getRetAttributes();
10249 
10250   // If the return value is not being used, the type may not be compatible
10251   // with the existing attributes.  Wipe out any problematic attributes.
10252   RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
10253 
10254   // Add the new return attributes.
10255   if (RAttrs)
10256     attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
10257 
10258   AI = CS.arg_begin();
10259   for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
10260     const Type *ParamTy = FT->getParamType(i);
10261     if ((*AI)->getType() == ParamTy) {
10262       Args.push_back(*AI);
10263     } else {
10264       Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
10265           false, ParamTy, false);
10266       CastInst *NewCast = CastInst::Create(opcode, *AI, ParamTy, "tmp");
10267       Args.push_back(InsertNewInstBefore(NewCast, *Caller));
10268     }
10269 
10270     // Add any parameter attributes.
10271     if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
10272       attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
10273   }
10274 
10275   // If the function takes more arguments than the call was taking, add them
10276   // now...
10277   for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
10278     Args.push_back(Constant::getNullValue(FT->getParamType(i)));
10279 
10280   // If we are removing arguments to the function, emit an obnoxious warning...
10281   if (FT->getNumParams() < NumActualArgs) {
10282     if (!FT->isVarArg()) {
10283       errs() << "WARNING: While resolving call to function '"
10284              << Callee->getName() << "' arguments were dropped!\n";
10285     } else {
10286       // Add all of the arguments in their promoted form to the arg list...
10287       for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
10288         const Type *PTy = getPromotedType((*AI)->getType());
10289         if (PTy != (*AI)->getType()) {
10290           // Must promote to pass through va_arg area!
10291           Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, 
10292                                                                 PTy, false);
10293           Instruction *Cast = CastInst::Create(opcode, *AI, PTy, "tmp");
10294           InsertNewInstBefore(Cast, *Caller);
10295           Args.push_back(Cast);
10296         } else {
10297           Args.push_back(*AI);
10298         }
10299 
10300         // Add any parameter attributes.
10301         if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
10302           attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
10303       }
10304     }
10305   }
10306 
10307   if (Attributes FnAttrs =  CallerPAL.getFnAttributes())
10308     attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
10309 
10310   if (NewRetTy == Type::getVoidTy(*Context))
10311     Caller->setName("");   // Void type should not have a name.
10312 
10313   const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
10314                                                      attrVec.end());
10315 
10316   Instruction *NC;
10317   if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
10318     NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
10319                             Args.begin(), Args.end(),
10320                             Caller->getName(), Caller);
10321     cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
10322     cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
10323   } else {
10324     NC = CallInst::Create(Callee, Args.begin(), Args.end(),
10325                           Caller->getName(), Caller);
10326     CallInst *CI = cast<CallInst>(Caller);
10327     if (CI->isTailCall())
10328       cast<CallInst>(NC)->setTailCall();
10329     cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
10330     cast<CallInst>(NC)->setAttributes(NewCallerPAL);
10331   }
10332 
10333   // Insert a cast of the return type as necessary.
10334   Value *NV = NC;
10335   if (OldRetTy != NV->getType() && !Caller->use_empty()) {
10336     if (NV->getType() != Type::getVoidTy(*Context)) {
10337       Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, 
10338                                                             OldRetTy, false);
10339       NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
10340 
10341       // If this is an invoke instruction, we should insert it after the first
10342       // non-phi, instruction in the normal successor block.
10343       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
10344         BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI();
10345         InsertNewInstBefore(NC, *I);
10346       } else {
10347         // Otherwise, it's a call, just insert cast right after the call instr
10348         InsertNewInstBefore(NC, *Caller);
10349       }
10350       AddUsersToWorkList(*Caller);
10351     } else {
10352       NV = UndefValue::get(Caller->getType());
10353     }
10354   }
10355 
10356   if (Caller->getType() != Type::getVoidTy(*Context) && !Caller->use_empty())
10357     Caller->replaceAllUsesWith(NV);
10358   Caller->eraseFromParent();
10359   RemoveFromWorkList(Caller);
10360   return true;
10361 }
10362 
10363 // transformCallThroughTrampoline - Turn a call to a function created by the
10364 // init_trampoline intrinsic into a direct call to the underlying function.
10365 //
10366 Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
10367   Value *Callee = CS.getCalledValue();
10368   const PointerType *PTy = cast<PointerType>(Callee->getType());
10369   const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
10370   const AttrListPtr &Attrs = CS.getAttributes();
10371 
10372   // If the call already has the 'nest' attribute somewhere then give up -
10373   // otherwise 'nest' would occur twice after splicing in the chain.
10374   if (Attrs.hasAttrSomewhere(Attribute::Nest))
10375     return 0;
10376 
10377   IntrinsicInst *Tramp =
10378     cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
10379 
10380   Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts());
10381   const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
10382   const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
10383 
10384   const AttrListPtr &NestAttrs = NestF->getAttributes();
10385   if (!NestAttrs.isEmpty()) {
10386     unsigned NestIdx = 1;
10387     const Type *NestTy = 0;
10388     Attributes NestAttr = Attribute::None;
10389 
10390     // Look for a parameter marked with the 'nest' attribute.
10391     for (FunctionType::param_iterator I = NestFTy->param_begin(),
10392          E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
10393       if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
10394         // Record the parameter type and any other attributes.
10395         NestTy = *I;
10396         NestAttr = NestAttrs.getParamAttributes(NestIdx);
10397         break;
10398       }
10399 
10400     if (NestTy) {
10401       Instruction *Caller = CS.getInstruction();
10402       std::vector<Value*> NewArgs;
10403       NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
10404 
10405       SmallVector<AttributeWithIndex, 8> NewAttrs;
10406       NewAttrs.reserve(Attrs.getNumSlots() + 1);
10407 
10408       // Insert the nest argument into the call argument list, which may
10409       // mean appending it.  Likewise for attributes.
10410 
10411       // Add any result attributes.
10412       if (Attributes Attr = Attrs.getRetAttributes())
10413         NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
10414 
10415       {
10416         unsigned Idx = 1;
10417         CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
10418         do {
10419           if (Idx == NestIdx) {
10420             // Add the chain argument and attributes.
10421             Value *NestVal = Tramp->getOperand(3);
10422             if (NestVal->getType() != NestTy)
10423               NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
10424             NewArgs.push_back(NestVal);
10425             NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
10426           }
10427 
10428           if (I == E)
10429             break;
10430 
10431           // Add the original argument and attributes.
10432           NewArgs.push_back(*I);
10433           if (Attributes Attr = Attrs.getParamAttributes(Idx))
10434             NewAttrs.push_back
10435               (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
10436 
10437           ++Idx, ++I;
10438         } while (1);
10439       }
10440 
10441       // Add any function attributes.
10442       if (Attributes Attr = Attrs.getFnAttributes())
10443         NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
10444 
10445       // The trampoline may have been bitcast to a bogus type (FTy).
10446       // Handle this by synthesizing a new function type, equal to FTy
10447       // with the chain parameter inserted.
10448 
10449       std::vector<const Type*> NewTypes;
10450       NewTypes.reserve(FTy->getNumParams()+1);
10451 
10452       // Insert the chain's type into the list of parameter types, which may
10453       // mean appending it.
10454       {
10455         unsigned Idx = 1;
10456         FunctionType::param_iterator I = FTy->param_begin(),
10457           E = FTy->param_end();
10458 
10459         do {
10460           if (Idx == NestIdx)
10461             // Add the chain's type.
10462             NewTypes.push_back(NestTy);
10463 
10464           if (I == E)
10465             break;
10466 
10467           // Add the original type.
10468           NewTypes.push_back(*I);
10469 
10470           ++Idx, ++I;
10471         } while (1);
10472       }
10473 
10474       // Replace the trampoline call with a direct call.  Let the generic
10475       // code sort out any function type mismatches.
10476       FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, 
10477                                                 FTy->isVarArg());
10478       Constant *NewCallee =
10479         NestF->getType() == PointerType::getUnqual(NewFTy) ?
10480         NestF : ConstantExpr::getBitCast(NestF, 
10481                                          PointerType::getUnqual(NewFTy));
10482       const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
10483                                                    NewAttrs.end());
10484 
10485       Instruction *NewCaller;
10486       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
10487         NewCaller = InvokeInst::Create(NewCallee,
10488                                        II->getNormalDest(), II->getUnwindDest(),
10489                                        NewArgs.begin(), NewArgs.end(),
10490                                        Caller->getName(), Caller);
10491         cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
10492         cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
10493       } else {
10494         NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
10495                                      Caller->getName(), Caller);
10496         if (cast<CallInst>(Caller)->isTailCall())
10497           cast<CallInst>(NewCaller)->setTailCall();
10498         cast<CallInst>(NewCaller)->
10499           setCallingConv(cast<CallInst>(Caller)->getCallingConv());
10500         cast<CallInst>(NewCaller)->setAttributes(NewPAL);
10501       }
10502       if (Caller->getType() != Type::getVoidTy(*Context) && !Caller->use_empty())
10503         Caller->replaceAllUsesWith(NewCaller);
10504       Caller->eraseFromParent();
10505       RemoveFromWorkList(Caller);
10506       return 0;
10507     }
10508   }
10509 
10510   // Replace the trampoline call with a direct call.  Since there is no 'nest'
10511   // parameter, there is no need to adjust the argument list.  Let the generic
10512   // code sort out any function type mismatches.
10513   Constant *NewCallee =
10514     NestF->getType() == PTy ? NestF : 
10515                               ConstantExpr::getBitCast(NestF, PTy);
10516   CS.setCalledFunction(NewCallee);
10517   return CS.getInstruction();
10518 }
10519 
10520 /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)]
10521 /// and if a/b/c/d and the add's all have a single use, turn this into two phi's
10522 /// and a single binop.
10523 Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) {
10524   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
10525   assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
10526   unsigned Opc = FirstInst->getOpcode();
10527   Value *LHSVal = FirstInst->getOperand(0);
10528   Value *RHSVal = FirstInst->getOperand(1);
10529     
10530   const Type *LHSType = LHSVal->getType();
10531   const Type *RHSType = RHSVal->getType();
10532   
10533   // Scan to see if all operands are the same opcode, all have one use, and all
10534   // kill their operands (i.e. the operands have one use).
10535   for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
10536     Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
10537     if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
10538         // Verify type of the LHS matches so we don't fold cmp's of different
10539         // types or GEP's with different index types.
10540         I->getOperand(0)->getType() != LHSType ||
10541         I->getOperand(1)->getType() != RHSType)
10542       return 0;
10543 
10544     // If they are CmpInst instructions, check their predicates
10545     if (Opc == Instruction::ICmp || Opc == Instruction::FCmp)
10546       if (cast<CmpInst>(I)->getPredicate() !=
10547           cast<CmpInst>(FirstInst)->getPredicate())
10548         return 0;
10549     
10550     // Keep track of which operand needs a phi node.
10551     if (I->getOperand(0) != LHSVal) LHSVal = 0;
10552     if (I->getOperand(1) != RHSVal) RHSVal = 0;
10553   }
10554   
10555   // Otherwise, this is safe to transform!
10556   
10557   Value *InLHS = FirstInst->getOperand(0);
10558   Value *InRHS = FirstInst->getOperand(1);
10559   PHINode *NewLHS = 0, *NewRHS = 0;
10560   if (LHSVal == 0) {
10561     NewLHS = PHINode::Create(LHSType,
10562                              FirstInst->getOperand(0)->getName() + ".pn");
10563     NewLHS->reserveOperandSpace(PN.getNumOperands()/2);
10564     NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
10565     InsertNewInstBefore(NewLHS, PN);
10566     LHSVal = NewLHS;
10567   }
10568   
10569   if (RHSVal == 0) {
10570     NewRHS = PHINode::Create(RHSType,
10571                              FirstInst->getOperand(1)->getName() + ".pn");
10572     NewRHS->reserveOperandSpace(PN.getNumOperands()/2);
10573     NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
10574     InsertNewInstBefore(NewRHS, PN);
10575     RHSVal = NewRHS;
10576   }
10577   
10578   // Add all operands to the new PHIs.
10579   if (NewLHS || NewRHS) {
10580     for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10581       Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i));
10582       if (NewLHS) {
10583         Value *NewInLHS = InInst->getOperand(0);
10584         NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i));
10585       }
10586       if (NewRHS) {
10587         Value *NewInRHS = InInst->getOperand(1);
10588         NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i));
10589       }
10590     }
10591   }
10592     
10593   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
10594     return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
10595   CmpInst *CIOp = cast<CmpInst>(FirstInst);
10596   return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(), 
10597                          LHSVal, RHSVal);
10598 }
10599 
10600 Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) {
10601   GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0));
10602   
10603   SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(), 
10604                                         FirstInst->op_end());
10605   // This is true if all GEP bases are allocas and if all indices into them are
10606   // constants.
10607   bool AllBasePointersAreAllocas = true;
10608   
10609   // Scan to see if all operands are the same opcode, all have one use, and all
10610   // kill their operands (i.e. the operands have one use).
10611   for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
10612     GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i));
10613     if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() ||
10614       GEP->getNumOperands() != FirstInst->getNumOperands())
10615       return 0;
10616 
10617     // Keep track of whether or not all GEPs are of alloca pointers.
10618     if (AllBasePointersAreAllocas &&
10619         (!isa<AllocaInst>(GEP->getOperand(0)) ||
10620          !GEP->hasAllConstantIndices()))
10621       AllBasePointersAreAllocas = false;
10622     
10623     // Compare the operand lists.
10624     for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) {
10625       if (FirstInst->getOperand(op) == GEP->getOperand(op))
10626         continue;
10627       
10628       // Don't merge two GEPs when two operands differ (introducing phi nodes)
10629       // if one of the PHIs has a constant for the index.  The index may be
10630       // substantially cheaper to compute for the constants, so making it a
10631       // variable index could pessimize the path.  This also handles the case
10632       // for struct indices, which must always be constant.
10633       if (isa<ConstantInt>(FirstInst->getOperand(op)) ||
10634           isa<ConstantInt>(GEP->getOperand(op)))
10635         return 0;
10636       
10637       if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType())
10638         return 0;
10639       FixedOperands[op] = 0;  // Needs a PHI.
10640     }
10641   }
10642   
10643   // If all of the base pointers of the PHI'd GEPs are from allocas, don't
10644   // bother doing this transformation.  At best, this will just save a bit of
10645   // offset calculation, but all the predecessors will have to materialize the
10646   // stack address into a register anyway.  We'd actually rather *clone* the
10647   // load up into the predecessors so that we have a load of a gep of an alloca,
10648   // which can usually all be folded into the load.
10649   if (AllBasePointersAreAllocas)
10650     return 0;
10651   
10652   // Otherwise, this is safe to transform.  Insert PHI nodes for each operand
10653   // that is variable.
10654   SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size());
10655   
10656   bool HasAnyPHIs = false;
10657   for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) {
10658     if (FixedOperands[i]) continue;  // operand doesn't need a phi.
10659     Value *FirstOp = FirstInst->getOperand(i);
10660     PHINode *NewPN = PHINode::Create(FirstOp->getType(),
10661                                      FirstOp->getName()+".pn");
10662     InsertNewInstBefore(NewPN, PN);
10663     
10664     NewPN->reserveOperandSpace(e);
10665     NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0));
10666     OperandPhis[i] = NewPN;
10667     FixedOperands[i] = NewPN;
10668     HasAnyPHIs = true;
10669   }
10670 
10671   
10672   // Add all operands to the new PHIs.
10673   if (HasAnyPHIs) {
10674     for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10675       GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i));
10676       BasicBlock *InBB = PN.getIncomingBlock(i);
10677       
10678       for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op)
10679         if (PHINode *OpPhi = OperandPhis[op])
10680           OpPhi->addIncoming(InGEP->getOperand(op), InBB);
10681     }
10682   }
10683   
10684   Value *Base = FixedOperands[0];
10685   GetElementPtrInst *GEP =
10686     GetElementPtrInst::Create(Base, FixedOperands.begin()+1,
10687                               FixedOperands.end());
10688   if (cast<GEPOperator>(FirstInst)->isInBounds())
10689     cast<GEPOperator>(GEP)->setIsInBounds(true);
10690   return GEP;
10691 }
10692 
10693 
10694 /// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to
10695 /// sink the load out of the block that defines it.  This means that it must be
10696 /// obvious the value of the load is not changed from the point of the load to
10697 /// the end of the block it is in.
10698 ///
10699 /// Finally, it is safe, but not profitable, to sink a load targetting a
10700 /// non-address-taken alloca.  Doing so will cause us to not promote the alloca
10701 /// to a register.
10702 static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
10703   BasicBlock::iterator BBI = L, E = L->getParent()->end();
10704   
10705   for (++BBI; BBI != E; ++BBI)
10706     if (BBI->mayWriteToMemory())
10707       return false;
10708   
10709   // Check for non-address taken alloca.  If not address-taken already, it isn't
10710   // profitable to do this xform.
10711   if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) {
10712     bool isAddressTaken = false;
10713     for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
10714          UI != E; ++UI) {
10715       if (isa<LoadInst>(UI)) continue;
10716       if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
10717         // If storing TO the alloca, then the address isn't taken.
10718         if (SI->getOperand(1) == AI) continue;
10719       }
10720       isAddressTaken = true;
10721       break;
10722     }
10723     
10724     if (!isAddressTaken && AI->isStaticAlloca())
10725       return false;
10726   }
10727   
10728   // If this load is a load from a GEP with a constant offset from an alloca,
10729   // then we don't want to sink it.  In its present form, it will be
10730   // load [constant stack offset].  Sinking it will cause us to have to
10731   // materialize the stack addresses in each predecessor in a register only to
10732   // do a shared load from register in the successor.
10733   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0)))
10734     if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0)))
10735       if (AI->isStaticAlloca() && GEP->hasAllConstantIndices())
10736         return false;
10737   
10738   return true;
10739 }
10740 
10741 
10742 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
10743 // operator and they all are only used by the PHI, PHI together their
10744 // inputs, and do the operation once, to the result of the PHI.
10745 Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
10746   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
10747 
10748   // Scan the instruction, looking for input operations that can be folded away.
10749   // If all input operands to the phi are the same instruction (e.g. a cast from
10750   // the same type or "+42") we can pull the operation through the PHI, reducing
10751   // code size and simplifying code.
10752   Constant *ConstantOp = 0;
10753   const Type *CastSrcTy = 0;
10754   bool isVolatile = false;
10755   if (isa<CastInst>(FirstInst)) {
10756     CastSrcTy = FirstInst->getOperand(0)->getType();
10757   } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
10758     // Can fold binop, compare or shift here if the RHS is a constant, 
10759     // otherwise call FoldPHIArgBinOpIntoPHI.
10760     ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
10761     if (ConstantOp == 0)
10762       return FoldPHIArgBinOpIntoPHI(PN);
10763   } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) {
10764     isVolatile = LI->isVolatile();
10765     // We can't sink the load if the loaded value could be modified between the
10766     // load and the PHI.
10767     if (LI->getParent() != PN.getIncomingBlock(0) ||
10768         !isSafeAndProfitableToSinkLoad(LI))
10769       return 0;
10770     
10771     // If the PHI is of volatile loads and the load block has multiple
10772     // successors, sinking it would remove a load of the volatile value from
10773     // the path through the other successor.
10774     if (isVolatile &&
10775         LI->getParent()->getTerminator()->getNumSuccessors() != 1)
10776       return 0;
10777     
10778   } else if (isa<GetElementPtrInst>(FirstInst)) {
10779     return FoldPHIArgGEPIntoPHI(PN);
10780   } else {
10781     return 0;  // Cannot fold this operation.
10782   }
10783 
10784   // Check to see if all arguments are the same operation.
10785   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10786     if (!isa<Instruction>(PN.getIncomingValue(i))) return 0;
10787     Instruction *I = cast<Instruction>(PN.getIncomingValue(i));
10788     if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst))
10789       return 0;
10790     if (CastSrcTy) {
10791       if (I->getOperand(0)->getType() != CastSrcTy)
10792         return 0;  // Cast operation must match.
10793     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
10794       // We can't sink the load if the loaded value could be modified between 
10795       // the load and the PHI.
10796       if (LI->isVolatile() != isVolatile ||
10797           LI->getParent() != PN.getIncomingBlock(i) ||
10798           !isSafeAndProfitableToSinkLoad(LI))
10799         return 0;
10800       
10801       // If the PHI is of volatile loads and the load block has multiple
10802       // successors, sinking it would remove a load of the volatile value from
10803       // the path through the other successor.
10804       if (isVolatile &&
10805           LI->getParent()->getTerminator()->getNumSuccessors() != 1)
10806         return 0;
10807       
10808     } else if (I->getOperand(1) != ConstantOp) {
10809       return 0;
10810     }
10811   }
10812 
10813   // Okay, they are all the same operation.  Create a new PHI node of the
10814   // correct type, and PHI together all of the LHS's of the instructions.
10815   PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
10816                                    PN.getName()+".in");
10817   NewPN->reserveOperandSpace(PN.getNumOperands()/2);
10818 
10819   Value *InVal = FirstInst->getOperand(0);
10820   NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
10821 
10822   // Add all operands to the new PHI.
10823   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
10824     Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
10825     if (NewInVal != InVal)
10826       InVal = 0;
10827     NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
10828   }
10829 
10830   Value *PhiVal;
10831   if (InVal) {
10832     // The new PHI unions all of the same values together.  This is really
10833     // common, so we handle it intelligently here for compile-time speed.
10834     PhiVal = InVal;
10835     delete NewPN;
10836   } else {
10837     InsertNewInstBefore(NewPN, PN);
10838     PhiVal = NewPN;
10839   }
10840 
10841   // Insert and return the new operation.
10842   if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst))
10843     return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType());
10844   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
10845     return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
10846   if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
10847     return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(), 
10848                            PhiVal, ConstantOp);
10849   assert(isa<LoadInst>(FirstInst) && "Unknown operation");
10850   
10851   // If this was a volatile load that we are merging, make sure to loop through
10852   // and mark all the input loads as non-volatile.  If we don't do this, we will
10853   // insert a new volatile load and the old ones will not be deletable.
10854   if (isVolatile)
10855     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
10856       cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false);
10857   
10858   return new LoadInst(PhiVal, "", isVolatile);
10859 }
10860 
10861 /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle
10862 /// that is dead.
10863 static bool DeadPHICycle(PHINode *PN,
10864                          SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) {
10865   if (PN->use_empty()) return true;
10866   if (!PN->hasOneUse()) return false;
10867 
10868   // Remember this node, and if we find the cycle, return.
10869   if (!PotentiallyDeadPHIs.insert(PN))
10870     return true;
10871   
10872   // Don't scan crazily complex things.
10873   if (PotentiallyDeadPHIs.size() == 16)
10874     return false;
10875 
10876   if (PHINode *PU = dyn_cast<PHINode>(PN->use_back()))
10877     return DeadPHICycle(PU, PotentiallyDeadPHIs);
10878 
10879   return false;
10880 }
10881 
10882 /// PHIsEqualValue - Return true if this phi node is always equal to
10883 /// NonPhiInVal.  This happens with mutually cyclic phi nodes like:
10884 ///   z = some value; x = phi (y, z); y = phi (x, z)
10885 static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, 
10886                            SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) {
10887   // See if we already saw this PHI node.
10888   if (!ValueEqualPHIs.insert(PN))
10889     return true;
10890   
10891   // Don't scan crazily complex things.
10892   if (ValueEqualPHIs.size() == 16)
10893     return false;
10894  
10895   // Scan the operands to see if they are either phi nodes or are equal to
10896   // the value.
10897   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
10898     Value *Op = PN->getIncomingValue(i);
10899     if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
10900       if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
10901         return false;
10902     } else if (Op != NonPhiInVal)
10903       return false;
10904   }
10905   
10906   return true;
10907 }
10908 
10909 
10910 // PHINode simplification
10911 //
10912 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
10913   // If LCSSA is around, don't mess with Phi nodes
10914   if (MustPreserveLCSSA) return 0;
10915   
10916   if (Value *V = PN.hasConstantValue())
10917     return ReplaceInstUsesWith(PN, V);
10918 
10919   // If all PHI operands are the same operation, pull them through the PHI,
10920   // reducing code size.
10921   if (isa<Instruction>(PN.getIncomingValue(0)) &&
10922       isa<Instruction>(PN.getIncomingValue(1)) &&
10923       cast<Instruction>(PN.getIncomingValue(0))->getOpcode() ==
10924       cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
10925       // FIXME: The hasOneUse check will fail for PHIs that use the value more
10926       // than themselves more than once.
10927       PN.getIncomingValue(0)->hasOneUse())
10928     if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
10929       return Result;
10930 
10931   // If this is a trivial cycle in the PHI node graph, remove it.  Basically, if
10932   // this PHI only has a single use (a PHI), and if that PHI only has one use (a
10933   // PHI)... break the cycle.
10934   if (PN.hasOneUse()) {
10935     Instruction *PHIUser = cast<Instruction>(PN.use_back());
10936     if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
10937       SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
10938       PotentiallyDeadPHIs.insert(&PN);
10939       if (DeadPHICycle(PU, PotentiallyDeadPHIs))
10940         return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
10941     }
10942    
10943     // If this phi has a single use, and if that use just computes a value for
10944     // the next iteration of a loop, delete the phi.  This occurs with unused
10945     // induction variables, e.g. "for (int j = 0; ; ++j);".  Detecting this
10946     // common case here is good because the only other things that catch this
10947     // are induction variable analysis (sometimes) and ADCE, which is only run
10948     // late.
10949     if (PHIUser->hasOneUse() &&
10950         (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
10951         PHIUser->use_back() == &PN) {
10952       return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
10953     }
10954   }
10955 
10956   // We sometimes end up with phi cycles that non-obviously end up being the
10957   // same value, for example:
10958   //   z = some value; x = phi (y, z); y = phi (x, z)
10959   // where the phi nodes don't necessarily need to be in the same block.  Do a
10960   // quick check to see if the PHI node only contains a single non-phi value, if
10961   // so, scan to see if the phi cycle is actually equal to that value.
10962   {
10963     unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues();
10964     // Scan for the first non-phi operand.
10965     while (InValNo != NumOperandVals && 
10966            isa<PHINode>(PN.getIncomingValue(InValNo)))
10967       ++InValNo;
10968 
10969     if (InValNo != NumOperandVals) {
10970       Value *NonPhiInVal = PN.getOperand(InValNo);
10971       
10972       // Scan the rest of the operands to see if there are any conflicts, if so
10973       // there is no need to recursively scan other phis.
10974       for (++InValNo; InValNo != NumOperandVals; ++InValNo) {
10975         Value *OpVal = PN.getIncomingValue(InValNo);
10976         if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
10977           break;
10978       }
10979       
10980       // If we scanned over all operands, then we have one unique value plus
10981       // phi values.  Scan PHI nodes to see if they all merge in each other or
10982       // the value.
10983       if (InValNo == NumOperandVals) {
10984         SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
10985         if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
10986           return ReplaceInstUsesWith(PN, NonPhiInVal);
10987       }
10988     }
10989   }
10990   return 0;
10991 }
10992 
10993 static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy,
10994                                    Instruction *InsertPoint,
10995                                    InstCombiner *IC) {
10996   unsigned PtrSize = DTy->getScalarSizeInBits();
10997   unsigned VTySize = V->getType()->getScalarSizeInBits();
10998   // We must cast correctly to the pointer type. Ensure that we
10999   // sign extend the integer value if it is smaller as this is
11000   // used for address computation.
11001   Instruction::CastOps opcode = 
11002      (VTySize < PtrSize ? Instruction::SExt :
11003       (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc));
11004   return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint);
11005 }
11006 
11007 
11008 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
11009   Value *PtrOp = GEP.getOperand(0);
11010   // Is it 'getelementptr %P, i32 0'  or 'getelementptr %P'
11011   // If so, eliminate the noop.
11012   if (GEP.getNumOperands() == 1)
11013     return ReplaceInstUsesWith(GEP, PtrOp);
11014 
11015   if (isa<UndefValue>(GEP.getOperand(0)))
11016     return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
11017 
11018   bool HasZeroPointerIndex = false;
11019   if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
11020     HasZeroPointerIndex = C->isNullValue();
11021 
11022   if (GEP.getNumOperands() == 2 && HasZeroPointerIndex)
11023     return ReplaceInstUsesWith(GEP, PtrOp);
11024 
11025   // Eliminate unneeded casts for indices.
11026   bool MadeChange = false;
11027   
11028   gep_type_iterator GTI = gep_type_begin(GEP);
11029   for (User::op_iterator i = GEP.op_begin() + 1, e = GEP.op_end();
11030        i != e; ++i, ++GTI) {
11031     if (TD && isa<SequentialType>(*GTI)) {
11032       if (CastInst *CI = dyn_cast<CastInst>(*i)) {
11033         if (CI->getOpcode() == Instruction::ZExt ||
11034             CI->getOpcode() == Instruction::SExt) {
11035           const Type *SrcTy = CI->getOperand(0)->getType();
11036           // We can eliminate a cast from i32 to i64 iff the target 
11037           // is a 32-bit pointer target.
11038           if (SrcTy->getScalarSizeInBits() >= TD->getPointerSizeInBits()) {
11039             MadeChange = true;
11040             *i = CI->getOperand(0);
11041           }
11042         }
11043       }
11044       // If we are using a wider index than needed for this platform, shrink it
11045       // to what we need.  If narrower, sign-extend it to what we need.
11046       // If the incoming value needs a cast instruction,
11047       // insert it.  This explicit cast can make subsequent optimizations more
11048       // obvious.
11049       Value *Op = *i;
11050       if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) {
11051         if (Constant *C = dyn_cast<Constant>(Op)) {
11052           *i = ConstantExpr::getTrunc(C, TD->getIntPtrType(GEP.getContext()));
11053           MadeChange = true;
11054         } else {
11055           Op = InsertCastBefore(Instruction::Trunc, Op, 
11056                                 TD->getIntPtrType(GEP.getContext()),
11057                                 GEP);
11058           *i = Op;
11059           MadeChange = true;
11060         }
11061       } else if (TD->getTypeSizeInBits(Op->getType()) 
11062                   < TD->getPointerSizeInBits()) {
11063         if (Constant *C = dyn_cast<Constant>(Op)) {
11064           *i = ConstantExpr::getSExt(C, TD->getIntPtrType(GEP.getContext()));
11065           MadeChange = true;
11066         } else {
11067           Op = InsertCastBefore(Instruction::SExt, Op, 
11068                                 TD->getIntPtrType(GEP.getContext()), GEP);
11069           *i = Op;
11070           MadeChange = true;
11071         }
11072       }
11073     }
11074   }
11075   if (MadeChange) return &GEP;
11076 
11077   // Combine Indices - If the source pointer to this getelementptr instruction
11078   // is a getelementptr instruction, combine the indices of the two
11079   // getelementptr instructions into a single instruction.
11080   //
11081   SmallVector<Value*, 8> SrcGEPOperands;
11082   bool BothInBounds = cast<GEPOperator>(&GEP)->isInBounds();
11083   if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
11084     SrcGEPOperands.append(Src->op_begin(), Src->op_end());
11085     if (!Src->isInBounds())
11086       BothInBounds = false;
11087   }
11088 
11089   if (!SrcGEPOperands.empty()) {
11090     // Note that if our source is a gep chain itself that we wait for that
11091     // chain to be resolved before we perform this transformation.  This
11092     // avoids us creating a TON of code in some cases.
11093     //
11094     if (isa<GetElementPtrInst>(SrcGEPOperands[0]) &&
11095         cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2)
11096       return 0;   // Wait until our source is folded to completion.
11097 
11098     SmallVector<Value*, 8> Indices;
11099 
11100     // Find out whether the last index in the source GEP is a sequential idx.
11101     bool EndsWithSequential = false;
11102     for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)),
11103            E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I)
11104       EndsWithSequential = !isa<StructType>(*I);
11105 
11106     // Can we combine the two pointer arithmetics offsets?
11107     if (EndsWithSequential) {
11108       // Replace: gep (gep %P, long B), long A, ...
11109       // With:    T = long A+B; gep %P, T, ...
11110       //
11111       Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1);
11112       if (SO1 == Constant::getNullValue(SO1->getType())) {
11113         Sum = GO1;
11114       } else if (GO1 == Constant::getNullValue(GO1->getType())) {
11115         Sum = SO1;
11116       } else {
11117         // If they aren't the same type, convert both to an integer of the
11118         // target's pointer size.
11119         if (SO1->getType() != GO1->getType()) {
11120           if (Constant *SO1C = dyn_cast<Constant>(SO1)) {
11121             SO1 =
11122                 ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true);
11123           } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
11124             GO1 =
11125                 ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true);
11126           } else if (TD) {
11127             unsigned PS = TD->getPointerSizeInBits();
11128             if (TD->getTypeSizeInBits(SO1->getType()) == PS) {
11129               // Convert GO1 to SO1's type.
11130               GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this);
11131 
11132             } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) {
11133               // Convert SO1 to GO1's type.
11134               SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this);
11135             } else {
11136               const Type *PT = TD->getIntPtrType(GEP.getContext());
11137               SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this);
11138               GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this);
11139             }
11140           }
11141         }
11142         if (isa<Constant>(SO1) && isa<Constant>(GO1))
11143           Sum = ConstantExpr::getAdd(cast<Constant>(SO1), 
11144                                             cast<Constant>(GO1));
11145         else {
11146           Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
11147           InsertNewInstBefore(cast<Instruction>(Sum), GEP);
11148         }
11149       }
11150 
11151       // Recycle the GEP we already have if possible.
11152       if (SrcGEPOperands.size() == 2) {
11153         GEP.setOperand(0, SrcGEPOperands[0]);
11154         GEP.setOperand(1, Sum);
11155         return &GEP;
11156       } else {
11157         Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
11158                        SrcGEPOperands.end()-1);
11159         Indices.push_back(Sum);
11160         Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end());
11161       }
11162     } else if (isa<Constant>(*GEP.idx_begin()) &&
11163                cast<Constant>(*GEP.idx_begin())->isNullValue() &&
11164                SrcGEPOperands.size() != 1) {
11165       // Otherwise we can do the fold if the first index of the GEP is a zero
11166       Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
11167                      SrcGEPOperands.end());
11168       Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
11169     }
11170 
11171     if (!Indices.empty()) {
11172       GetElementPtrInst *NewGEP = GetElementPtrInst::Create(SrcGEPOperands[0],
11173                                                             Indices.begin(),
11174                                                             Indices.end(),
11175                                                             GEP.getName());
11176       if (BothInBounds)
11177         cast<GEPOperator>(NewGEP)->setIsInBounds(true);
11178       return NewGEP;
11179     }
11180 
11181   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) {
11182     // GEP of global variable.  If all of the indices for this GEP are
11183     // constants, we can promote this to a constexpr instead of an instruction.
11184 
11185     // Scan for nonconstants...
11186     SmallVector<Constant*, 8> Indices;
11187     User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
11188     for (; I != E && isa<Constant>(*I); ++I)
11189       Indices.push_back(cast<Constant>(*I));
11190 
11191     if (I == E) {  // If they are all constants...
11192       Constant *CE = ConstantExpr::getGetElementPtr(GV,
11193                                                     &Indices[0],Indices.size());
11194 
11195       // Replace all uses of the GEP with the new constexpr...
11196       return ReplaceInstUsesWith(GEP, CE);
11197     }
11198   } else if (Value *X = getBitCastOperand(PtrOp)) {  // Is the operand a cast?
11199     if (!isa<PointerType>(X->getType())) {
11200       // Not interesting.  Source pointer must be a cast from pointer.
11201     } else if (HasZeroPointerIndex) {
11202       // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
11203       // into     : GEP [10 x i8]* X, i32 0, ...
11204       //
11205       // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
11206       //           into     : GEP i8* X, ...
11207       // 
11208       // This occurs when the program declares an array extern like "int X[];"
11209       const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
11210       const PointerType *XTy = cast<PointerType>(X->getType());
11211       if (const ArrayType *CATy =
11212           dyn_cast<ArrayType>(CPTy->getElementType())) {
11213         // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
11214         if (CATy->getElementType() == XTy->getElementType()) {
11215           // -> GEP i8* X, ...
11216           SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end());
11217           GetElementPtrInst *NewGEP =
11218             GetElementPtrInst::Create(X, Indices.begin(), Indices.end(),
11219                                       GEP.getName());
11220           if (cast<GEPOperator>(&GEP)->isInBounds())
11221             cast<GEPOperator>(NewGEP)->setIsInBounds(true);
11222           return NewGEP;
11223         } else if (const ArrayType *XATy =
11224                  dyn_cast<ArrayType>(XTy->getElementType())) {
11225           // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
11226           if (CATy->getElementType() == XATy->getElementType()) {
11227             // -> GEP [10 x i8]* X, i32 0, ...
11228             // At this point, we know that the cast source type is a pointer
11229             // to an array of the same type as the destination pointer
11230             // array.  Because the array type is never stepped over (there
11231             // is a leading zero) we can fold the cast into this GEP.
11232             GEP.setOperand(0, X);
11233             return &GEP;
11234           }
11235         }
11236       }
11237     } else if (GEP.getNumOperands() == 2) {
11238       // Transform things like:
11239       // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
11240       // into:  %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
11241       const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
11242       const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
11243       if (TD && isa<ArrayType>(SrcElTy) &&
11244           TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
11245           TD->getTypeAllocSize(ResElTy)) {
11246         Value *Idx[2];
11247         Idx[0] = Constant::getNullValue(Type::getInt32Ty(*Context));
11248         Idx[1] = GEP.getOperand(1);
11249         GetElementPtrInst *NewGEP =
11250           GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
11251         if (cast<GEPOperator>(&GEP)->isInBounds())
11252           cast<GEPOperator>(NewGEP)->setIsInBounds(true);
11253         Value *V = InsertNewInstBefore(NewGEP, GEP);
11254         // V and GEP are both pointer types --> BitCast
11255         return new BitCastInst(V, GEP.getType());
11256       }
11257       
11258       // Transform things like:
11259       // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
11260       //   (where tmp = 8*tmp2) into:
11261       // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
11262       
11263       if (TD && isa<ArrayType>(SrcElTy) && ResElTy == Type::getInt8Ty(*Context)) {
11264         uint64_t ArrayEltSize =
11265             TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
11266         
11267         // Check to see if "tmp" is a scale by a multiple of ArrayEltSize.  We
11268         // allow either a mul, shift, or constant here.
11269         Value *NewIdx = 0;
11270         ConstantInt *Scale = 0;
11271         if (ArrayEltSize == 1) {
11272           NewIdx = GEP.getOperand(1);
11273           Scale = 
11274                ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1);
11275         } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
11276           NewIdx = ConstantInt::get(CI->getType(), 1);
11277           Scale = CI;
11278         } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
11279           if (Inst->getOpcode() == Instruction::Shl &&
11280               isa<ConstantInt>(Inst->getOperand(1))) {
11281             ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
11282             uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
11283             Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()),
11284                                      1ULL << ShAmtVal);
11285             NewIdx = Inst->getOperand(0);
11286           } else if (Inst->getOpcode() == Instruction::Mul &&
11287                      isa<ConstantInt>(Inst->getOperand(1))) {
11288             Scale = cast<ConstantInt>(Inst->getOperand(1));
11289             NewIdx = Inst->getOperand(0);
11290           }
11291         }
11292         
11293         // If the index will be to exactly the right offset with the scale taken
11294         // out, perform the transformation. Note, we don't know whether Scale is
11295         // signed or not. We'll use unsigned version of division/modulo
11296         // operation after making sure Scale doesn't have the sign bit set.
11297         if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
11298             Scale->getZExtValue() % ArrayEltSize == 0) {
11299           Scale = ConstantInt::get(Scale->getType(),
11300                                    Scale->getZExtValue() / ArrayEltSize);
11301           if (Scale->getZExtValue() != 1) {
11302             Constant *C =
11303                    ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
11304                                                        false /*ZExt*/);
11305             Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale");
11306             NewIdx = InsertNewInstBefore(Sc, GEP);
11307           }
11308 
11309           // Insert the new GEP instruction.
11310           Value *Idx[2];
11311           Idx[0] = Constant::getNullValue(Type::getInt32Ty(*Context));
11312           Idx[1] = NewIdx;
11313           Instruction *NewGEP =
11314             GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
11315           if (cast<GEPOperator>(&GEP)->isInBounds())
11316             cast<GEPOperator>(NewGEP)->setIsInBounds(true);
11317           NewGEP = InsertNewInstBefore(NewGEP, GEP);
11318           // The NewGEP must be pointer typed, so must the old one -> BitCast
11319           return new BitCastInst(NewGEP, GEP.getType());
11320         }
11321       }
11322     }
11323   }
11324   
11325   /// See if we can simplify:
11326   ///   X = bitcast A to B*
11327   ///   Y = gep X, <...constant indices...>
11328   /// into a gep of the original struct.  This is important for SROA and alias
11329   /// analysis of unions.  If "A" is also a bitcast, wait for A/X to be merged.
11330   if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
11331     if (TD &&
11332         !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) {
11333       // Determine how much the GEP moves the pointer.  We are guaranteed to get
11334       // a constant back from EmitGEPOffset.
11335       ConstantInt *OffsetV =
11336                     cast<ConstantInt>(EmitGEPOffset(&GEP, GEP, *this));
11337       int64_t Offset = OffsetV->getSExtValue();
11338       
11339       // If this GEP instruction doesn't move the pointer, just replace the GEP
11340       // with a bitcast of the real input to the dest type.
11341       if (Offset == 0) {
11342         // If the bitcast is of an allocation, and the allocation will be
11343         // converted to match the type of the cast, don't touch this.
11344         if (isa<AllocationInst>(BCI->getOperand(0))) {
11345           // See if the bitcast simplifies, if so, don't nuke this GEP yet.
11346           if (Instruction *I = visitBitCast(*BCI)) {
11347             if (I != BCI) {
11348               I->takeName(BCI);
11349               BCI->getParent()->getInstList().insert(BCI, I);
11350               ReplaceInstUsesWith(*BCI, I);
11351             }
11352             return &GEP;
11353           }
11354         }
11355         return new BitCastInst(BCI->getOperand(0), GEP.getType());
11356       }
11357       
11358       // Otherwise, if the offset is non-zero, we need to find out if there is a
11359       // field at Offset in 'A's type.  If so, we can pull the cast through the
11360       // GEP.
11361       SmallVector<Value*, 8> NewIndices;
11362       const Type *InTy =
11363         cast<PointerType>(BCI->getOperand(0)->getType())->getElementType();
11364       if (FindElementAtOffset(InTy, Offset, NewIndices, TD, Context)) {
11365         Instruction *NGEP =
11366            GetElementPtrInst::Create(BCI->getOperand(0), NewIndices.begin(),
11367                                      NewIndices.end());
11368         if (NGEP->getType() == GEP.getType()) return NGEP;
11369         if (cast<GEPOperator>(&GEP)->isInBounds())
11370           cast<GEPOperator>(NGEP)->setIsInBounds(true);
11371         InsertNewInstBefore(NGEP, GEP);
11372         NGEP->takeName(&GEP);
11373         return new BitCastInst(NGEP, GEP.getType());
11374       }
11375     }
11376   }    
11377     
11378   return 0;
11379 }
11380 
11381 Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
11382   // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
11383   if (AI.isArrayAllocation()) {  // Check C != 1
11384     if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
11385       const Type *NewTy = 
11386         ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
11387       AllocationInst *New = 0;
11388 
11389       // Create and insert the replacement instruction...
11390       if (isa<MallocInst>(AI))
11391         New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName());
11392       else {
11393         assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
11394         New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName());
11395       }
11396 
11397       InsertNewInstBefore(New, AI);
11398 
11399       // Scan to the end of the allocation instructions, to skip over a block of
11400       // allocas if possible...also skip interleaved debug info
11401       //
11402       BasicBlock::iterator It = New;
11403       while (isa<AllocationInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
11404 
11405       // Now that I is pointing to the first non-allocation-inst in the block,
11406       // insert our getelementptr instruction...
11407       //
11408       Value *NullIdx = Constant::getNullValue(Type::getInt32Ty(*Context));
11409       Value *Idx[2];
11410       Idx[0] = NullIdx;
11411       Idx[1] = NullIdx;
11412       Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2,
11413                                            New->getName()+".sub", It);
11414       cast<GEPOperator>(V)->setIsInBounds(true);
11415 
11416       // Now make everything use the getelementptr instead of the original
11417       // allocation.
11418       return ReplaceInstUsesWith(AI, V);
11419     } else if (isa<UndefValue>(AI.getArraySize())) {
11420       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
11421     }
11422   }
11423 
11424   if (TD && isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) {
11425     // If alloca'ing a zero byte object, replace the alloca with a null pointer.
11426     // Note that we only do this for alloca's, because malloc should allocate
11427     // and return a unique pointer, even for a zero byte allocation.
11428     if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0)
11429       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
11430 
11431     // If the alignment is 0 (unspecified), assign it the preferred alignment.
11432     if (AI.getAlignment() == 0)
11433       AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType()));
11434   }
11435 
11436   return 0;
11437 }
11438 
11439 Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
11440   Value *Op = FI.getOperand(0);
11441 
11442   // free undef -> unreachable.
11443   if (isa<UndefValue>(Op)) {
11444     // Insert a new store to null because we cannot modify the CFG here.
11445     new StoreInst(ConstantInt::getTrue(*Context),
11446            UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))), &FI);
11447     return EraseInstFromFunction(FI);
11448   }
11449   
11450   // If we have 'free null' delete the instruction.  This can happen in stl code
11451   // when lots of inlining happens.
11452   if (isa<ConstantPointerNull>(Op))
11453     return EraseInstFromFunction(FI);
11454   
11455   // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X
11456   if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) {
11457     FI.setOperand(0, CI->getOperand(0));
11458     return &FI;
11459   }
11460   
11461   // Change free (gep X, 0,0,0,0) into free(X)
11462   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
11463     if (GEPI->hasAllZeroIndices()) {
11464       AddToWorkList(GEPI);
11465       FI.setOperand(0, GEPI->getOperand(0));
11466       return &FI;
11467     }
11468   }
11469   
11470   // Change free(malloc) into nothing, if the malloc has a single use.
11471   if (MallocInst *MI = dyn_cast<MallocInst>(Op))
11472     if (MI->hasOneUse()) {
11473       EraseInstFromFunction(FI);
11474       return EraseInstFromFunction(*MI);
11475     }
11476 
11477   return 0;
11478 }
11479 
11480 
11481 /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
11482 static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
11483                                         const TargetData *TD) {
11484   User *CI = cast<User>(LI.getOperand(0));
11485   Value *CastOp = CI->getOperand(0);
11486   LLVMContext *Context = IC.getContext();
11487 
11488   if (TD) {
11489     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
11490       // Instead of loading constant c string, use corresponding integer value
11491       // directly if string length is small enough.
11492       std::string Str;
11493       if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) {
11494         unsigned len = Str.length();
11495         const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
11496         unsigned numBits = Ty->getPrimitiveSizeInBits();
11497         // Replace LI with immediate integer store.
11498         if ((numBits >> 3) == len + 1) {
11499           APInt StrVal(numBits, 0);
11500           APInt SingleChar(numBits, 0);
11501           if (TD->isLittleEndian()) {
11502             for (signed i = len-1; i >= 0; i--) {
11503               SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
11504               StrVal = (StrVal << 8) | SingleChar;
11505             }
11506           } else {
11507             for (unsigned i = 0; i < len; i++) {
11508               SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
11509               StrVal = (StrVal << 8) | SingleChar;
11510             }
11511             // Append NULL at the end.
11512             SingleChar = 0;
11513             StrVal = (StrVal << 8) | SingleChar;
11514           }
11515           Value *NL = ConstantInt::get(*Context, StrVal);
11516           return IC.ReplaceInstUsesWith(LI, NL);
11517         }
11518       }
11519     }
11520   }
11521 
11522   const PointerType *DestTy = cast<PointerType>(CI->getType());
11523   const Type *DestPTy = DestTy->getElementType();
11524   if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
11525 
11526     // If the address spaces don't match, don't eliminate the cast.
11527     if (DestTy->getAddressSpace() != SrcTy->getAddressSpace())
11528       return 0;
11529 
11530     const Type *SrcPTy = SrcTy->getElementType();
11531 
11532     if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || 
11533          isa<VectorType>(DestPTy)) {
11534       // If the source is an array, the code below will not succeed.  Check to
11535       // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
11536       // constants.
11537       if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
11538         if (Constant *CSrc = dyn_cast<Constant>(CastOp))
11539           if (ASrcTy->getNumElements() != 0) {
11540             Value *Idxs[2];
11541             Idxs[0] = Idxs[1] = Constant::getNullValue(Type::getInt32Ty(*Context));
11542             CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
11543             SrcTy = cast<PointerType>(CastOp->getType());
11544             SrcPTy = SrcTy->getElementType();
11545           }
11546 
11547       if (IC.getTargetData() &&
11548           (SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || 
11549             isa<VectorType>(SrcPTy)) &&
11550           // Do not allow turning this into a load of an integer, which is then
11551           // casted to a pointer, this pessimizes pointer analysis a lot.
11552           (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) &&
11553           IC.getTargetData()->getTypeSizeInBits(SrcPTy) ==
11554                IC.getTargetData()->getTypeSizeInBits(DestPTy)) {
11555 
11556         // Okay, we are casting from one integer or pointer type to another of
11557         // the same size.  Instead of casting the pointer before the load, cast
11558         // the result of the loaded value.
11559         Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp,
11560                                                              CI->getName(),
11561                                                          LI.isVolatile()),LI);
11562         // Now cast the result of the load.
11563         return new BitCastInst(NewLoad, LI.getType());
11564       }
11565     }
11566   }
11567   return 0;
11568 }
11569 
11570 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
11571   Value *Op = LI.getOperand(0);
11572 
11573   // Attempt to improve the alignment.
11574   if (TD) {
11575     unsigned KnownAlign =
11576       GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()));
11577     if (KnownAlign >
11578         (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) :
11579                                   LI.getAlignment()))
11580       LI.setAlignment(KnownAlign);
11581   }
11582 
11583   // load (cast X) --> cast (load X) iff safe
11584   if (isa<CastInst>(Op))
11585     if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
11586       return Res;
11587 
11588   // None of the following transforms are legal for volatile loads.
11589   if (LI.isVolatile()) return 0;
11590   
11591   // Do really simple store-to-load forwarding and load CSE, to catch cases
11592   // where there are several consequtive memory accesses to the same location,
11593   // separated by a few arithmetic operations.
11594   BasicBlock::iterator BBI = &LI;
11595   if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
11596     return ReplaceInstUsesWith(LI, AvailableVal);
11597 
11598   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
11599     const Value *GEPI0 = GEPI->getOperand(0);
11600     // TODO: Consider a target hook for valid address spaces for this xform.
11601     if (isa<ConstantPointerNull>(GEPI0) &&
11602         cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) {
11603       // Insert a new store to null instruction before the load to indicate
11604       // that this code is not reachable.  We do this instead of inserting
11605       // an unreachable instruction directly because we cannot modify the
11606       // CFG.
11607       new StoreInst(UndefValue::get(LI.getType()),
11608                     Constant::getNullValue(Op->getType()), &LI);
11609       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11610     }
11611   } 
11612 
11613   if (Constant *C = dyn_cast<Constant>(Op)) {
11614     // load null/undef -> undef
11615     // TODO: Consider a target hook for valid address spaces for this xform.
11616     if (isa<UndefValue>(C) || (C->isNullValue() && 
11617         cast<PointerType>(Op->getType())->getAddressSpace() == 0)) {
11618       // Insert a new store to null instruction before the load to indicate that
11619       // this code is not reachable.  We do this instead of inserting an
11620       // unreachable instruction directly because we cannot modify the CFG.
11621       new StoreInst(UndefValue::get(LI.getType()),
11622                     Constant::getNullValue(Op->getType()), &LI);
11623       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11624     }
11625 
11626     // Instcombine load (constant global) into the value loaded.
11627     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
11628       if (GV->isConstant() && GV->hasDefinitiveInitializer())
11629         return ReplaceInstUsesWith(LI, GV->getInitializer());
11630 
11631     // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded.
11632     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) {
11633       if (CE->getOpcode() == Instruction::GetElementPtr) {
11634         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
11635           if (GV->isConstant() && GV->hasDefinitiveInitializer())
11636             if (Constant *V = 
11637                ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE, 
11638                                                       *Context))
11639               return ReplaceInstUsesWith(LI, V);
11640         if (CE->getOperand(0)->isNullValue()) {
11641           // Insert a new store to null instruction before the load to indicate
11642           // that this code is not reachable.  We do this instead of inserting
11643           // an unreachable instruction directly because we cannot modify the
11644           // CFG.
11645           new StoreInst(UndefValue::get(LI.getType()),
11646                         Constant::getNullValue(Op->getType()), &LI);
11647           return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11648         }
11649 
11650       } else if (CE->isCast()) {
11651         if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
11652           return Res;
11653       }
11654     }
11655   }
11656     
11657   // If this load comes from anywhere in a constant global, and if the global
11658   // is all undef or zero, we know what it loads.
11659   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){
11660     if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
11661       if (GV->getInitializer()->isNullValue())
11662         return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
11663       else if (isa<UndefValue>(GV->getInitializer()))
11664         return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
11665     }
11666   }
11667 
11668   if (Op->hasOneUse()) {
11669     // Change select and PHI nodes to select values instead of addresses: this
11670     // helps alias analysis out a lot, allows many others simplifications, and
11671     // exposes redundancy in the code.
11672     //
11673     // Note that we cannot do the transformation unless we know that the
11674     // introduced loads cannot trap!  Something like this is valid as long as
11675     // the condition is always false: load (select bool %C, int* null, int* %G),
11676     // but it would not be valid if we transformed it to load from null
11677     // unconditionally.
11678     //
11679     if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
11680       // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2).
11681       if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) &&
11682           isSafeToLoadUnconditionally(SI->getOperand(2), SI)) {
11683         Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1),
11684                                      SI->getOperand(1)->getName()+".val"), LI);
11685         Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2),
11686                                      SI->getOperand(2)->getName()+".val"), LI);
11687         return SelectInst::Create(SI->getCondition(), V1, V2);
11688       }
11689 
11690       // load (select (cond, null, P)) -> load P
11691       if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
11692         if (C->isNullValue()) {
11693           LI.setOperand(0, SI->getOperand(2));
11694           return &LI;
11695         }
11696 
11697       // load (select (cond, P, null)) -> load P
11698       if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
11699         if (C->isNullValue()) {
11700           LI.setOperand(0, SI->getOperand(1));
11701           return &LI;
11702         }
11703     }
11704   }
11705   return 0;
11706 }
11707 
11708 /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
11709 /// when possible.  This makes it generally easy to do alias analysis and/or
11710 /// SROA/mem2reg of the memory object.
11711 static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
11712   User *CI = cast<User>(SI.getOperand(1));
11713   Value *CastOp = CI->getOperand(0);
11714 
11715   const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
11716   const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
11717   if (SrcTy == 0) return 0;
11718   
11719   const Type *SrcPTy = SrcTy->getElementType();
11720 
11721   if (!DestPTy->isInteger() && !isa<PointerType>(DestPTy))
11722     return 0;
11723   
11724   /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
11725   /// to its first element.  This allows us to handle things like:
11726   ///   store i32 xxx, (bitcast {foo*, float}* %P to i32*)
11727   /// on 32-bit hosts.
11728   SmallVector<Value*, 4> NewGEPIndices;
11729   
11730   // If the source is an array, the code below will not succeed.  Check to
11731   // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
11732   // constants.
11733   if (isa<ArrayType>(SrcPTy) || isa<StructType>(SrcPTy)) {
11734     // Index through pointer.
11735     Constant *Zero = Constant::getNullValue(Type::getInt32Ty(*IC.getContext()));
11736     NewGEPIndices.push_back(Zero);
11737     
11738     while (1) {
11739       if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) {
11740         if (!STy->getNumElements()) /* Struct can be empty {} */
11741           break;
11742         NewGEPIndices.push_back(Zero);
11743         SrcPTy = STy->getElementType(0);
11744       } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
11745         NewGEPIndices.push_back(Zero);
11746         SrcPTy = ATy->getElementType();
11747       } else {
11748         break;
11749       }
11750     }
11751     
11752     SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
11753   }
11754 
11755   if (!SrcPTy->isInteger() && !isa<PointerType>(SrcPTy))
11756     return 0;
11757   
11758   // If the pointers point into different address spaces or if they point to
11759   // values with different sizes, we can't do the transformation.
11760   if (!IC.getTargetData() ||
11761       SrcTy->getAddressSpace() != 
11762         cast<PointerType>(CI->getType())->getAddressSpace() ||
11763       IC.getTargetData()->getTypeSizeInBits(SrcPTy) !=
11764       IC.getTargetData()->getTypeSizeInBits(DestPTy))
11765     return 0;
11766 
11767   // Okay, we are casting from one integer or pointer type to another of
11768   // the same size.  Instead of casting the pointer before 
11769   // the store, cast the value to be stored.
11770   Value *NewCast;
11771   Value *SIOp0 = SI.getOperand(0);
11772   Instruction::CastOps opcode = Instruction::BitCast;
11773   const Type* CastSrcTy = SIOp0->getType();
11774   const Type* CastDstTy = SrcPTy;
11775   if (isa<PointerType>(CastDstTy)) {
11776     if (CastSrcTy->isInteger())
11777       opcode = Instruction::IntToPtr;
11778   } else if (isa<IntegerType>(CastDstTy)) {
11779     if (isa<PointerType>(SIOp0->getType()))
11780       opcode = Instruction::PtrToInt;
11781   }
11782   
11783   // SIOp0 is a pointer to aggregate and this is a store to the first field,
11784   // emit a GEP to index into its first field.
11785   if (!NewGEPIndices.empty()) {
11786     if (Constant *C = dyn_cast<Constant>(CastOp))
11787       CastOp = ConstantExpr::getGetElementPtr(C, &NewGEPIndices[0], 
11788                                               NewGEPIndices.size());
11789     else
11790       CastOp = IC.InsertNewInstBefore(
11791               GetElementPtrInst::Create(CastOp, NewGEPIndices.begin(),
11792                                         NewGEPIndices.end()), SI);
11793     cast<GEPOperator>(CastOp)->setIsInBounds(true);
11794   }
11795   
11796   if (Constant *C = dyn_cast<Constant>(SIOp0))
11797     NewCast = ConstantExpr::getCast(opcode, C, CastDstTy);
11798   else
11799     NewCast = IC.InsertNewInstBefore(
11800       CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), 
11801       SI);
11802   return new StoreInst(NewCast, CastOp);
11803 }
11804 
11805 /// equivalentAddressValues - Test if A and B will obviously have the same
11806 /// value. This includes recognizing that %t0 and %t1 will have the same
11807 /// value in code like this:
11808 ///   %t0 = getelementptr \@a, 0, 3
11809 ///   store i32 0, i32* %t0
11810 ///   %t1 = getelementptr \@a, 0, 3
11811 ///   %t2 = load i32* %t1
11812 ///
11813 static bool equivalentAddressValues(Value *A, Value *B) {
11814   // Test if the values are trivially equivalent.
11815   if (A == B) return true;
11816   
11817   // Test if the values come form identical arithmetic instructions.
11818   // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
11819   // its only used to compare two uses within the same basic block, which
11820   // means that they'll always either have the same value or one of them
11821   // will have an undefined value.
11822   if (isa<BinaryOperator>(A) ||
11823       isa<CastInst>(A) ||
11824       isa<PHINode>(A) ||
11825       isa<GetElementPtrInst>(A))
11826     if (Instruction *BI = dyn_cast<Instruction>(B))
11827       if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
11828         return true;
11829   
11830   // Otherwise they may not be equivalent.
11831   return false;
11832 }
11833 
11834 // If this instruction has two uses, one of which is a llvm.dbg.declare,
11835 // return the llvm.dbg.declare.
11836 DbgDeclareInst *InstCombiner::hasOneUsePlusDeclare(Value *V) {
11837   if (!V->hasNUses(2))
11838     return 0;
11839   for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
11840        UI != E; ++UI) {
11841     if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI))
11842       return DI;
11843     if (isa<BitCastInst>(UI) && UI->hasOneUse()) {
11844       if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI->use_begin()))
11845         return DI;
11846       }
11847   }
11848   return 0;
11849 }
11850 
11851 Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
11852   Value *Val = SI.getOperand(0);
11853   Value *Ptr = SI.getOperand(1);
11854 
11855   if (isa<UndefValue>(Ptr)) {     // store X, undef -> noop (even if volatile)
11856     EraseInstFromFunction(SI);
11857     ++NumCombined;
11858     return 0;
11859   }
11860   
11861   // If the RHS is an alloca with a single use, zapify the store, making the
11862   // alloca dead.
11863   // If the RHS is an alloca with a two uses, the other one being a 
11864   // llvm.dbg.declare, zapify the store and the declare, making the
11865   // alloca dead.  We must do this to prevent declare's from affecting
11866   // codegen.
11867   if (!SI.isVolatile()) {
11868     if (Ptr->hasOneUse()) {
11869       if (isa<AllocaInst>(Ptr)) {
11870         EraseInstFromFunction(SI);
11871         ++NumCombined;
11872         return 0;
11873       }
11874       if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
11875         if (isa<AllocaInst>(GEP->getOperand(0))) {
11876           if (GEP->getOperand(0)->hasOneUse()) {
11877             EraseInstFromFunction(SI);
11878             ++NumCombined;
11879             return 0;
11880           }
11881           if (DbgDeclareInst *DI = hasOneUsePlusDeclare(GEP->getOperand(0))) {
11882             EraseInstFromFunction(*DI);
11883             EraseInstFromFunction(SI);
11884             ++NumCombined;
11885             return 0;
11886           }
11887         }
11888       }
11889     }
11890     if (DbgDeclareInst *DI = hasOneUsePlusDeclare(Ptr)) {
11891       EraseInstFromFunction(*DI);
11892       EraseInstFromFunction(SI);
11893       ++NumCombined;
11894       return 0;
11895     }
11896   }
11897 
11898   // Attempt to improve the alignment.
11899   if (TD) {
11900     unsigned KnownAlign =
11901       GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType()));
11902     if (KnownAlign >
11903         (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) :
11904                                   SI.getAlignment()))
11905       SI.setAlignment(KnownAlign);
11906   }
11907 
11908   // Do really simple DSE, to catch cases where there are several consecutive
11909   // stores to the same location, separated by a few arithmetic operations. This
11910   // situation often occurs with bitfield accesses.
11911   BasicBlock::iterator BBI = &SI;
11912   for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
11913        --ScanInsts) {
11914     --BBI;
11915     // Don't count debug info directives, lest they affect codegen,
11916     // and we skip pointer-to-pointer bitcasts, which are NOPs.
11917     // It is necessary for correctness to skip those that feed into a
11918     // llvm.dbg.declare, as these are not present when debugging is off.
11919     if (isa<DbgInfoIntrinsic>(BBI) ||
11920         (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) {
11921       ScanInsts++;
11922       continue;
11923     }    
11924     
11925     if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
11926       // Prev store isn't volatile, and stores to the same location?
11927       if (!PrevSI->isVolatile() &&equivalentAddressValues(PrevSI->getOperand(1),
11928                                                           SI.getOperand(1))) {
11929         ++NumDeadStore;
11930         ++BBI;
11931         EraseInstFromFunction(*PrevSI);
11932         continue;
11933       }
11934       break;
11935     }
11936     
11937     // If this is a load, we have to stop.  However, if the loaded value is from
11938     // the pointer we're loading and is producing the pointer we're storing,
11939     // then *this* store is dead (X = load P; store X -> P).
11940     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
11941       if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
11942           !SI.isVolatile()) {
11943         EraseInstFromFunction(SI);
11944         ++NumCombined;
11945         return 0;
11946       }
11947       // Otherwise, this is a load from some other location.  Stores before it
11948       // may not be dead.
11949       break;
11950     }
11951     
11952     // Don't skip over loads or things that can modify memory.
11953     if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
11954       break;
11955   }
11956   
11957   
11958   if (SI.isVolatile()) return 0;  // Don't hack volatile stores.
11959 
11960   // store X, null    -> turns into 'unreachable' in SimplifyCFG
11961   if (isa<ConstantPointerNull>(Ptr) &&
11962       cast<PointerType>(Ptr->getType())->getAddressSpace() == 0) {
11963     if (!isa<UndefValue>(Val)) {
11964       SI.setOperand(0, UndefValue::get(Val->getType()));
11965       if (Instruction *U = dyn_cast<Instruction>(Val))
11966         AddToWorkList(U);  // Dropped a use.
11967       ++NumCombined;
11968     }
11969     return 0;  // Do not modify these!
11970   }
11971 
11972   // store undef, Ptr -> noop
11973   if (isa<UndefValue>(Val)) {
11974     EraseInstFromFunction(SI);
11975     ++NumCombined;
11976     return 0;
11977   }
11978 
11979   // If the pointer destination is a cast, see if we can fold the cast into the
11980   // source instead.
11981   if (isa<CastInst>(Ptr))
11982     if (Instruction *Res = InstCombineStoreToCast(*this, SI))
11983       return Res;
11984   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
11985     if (CE->isCast())
11986       if (Instruction *Res = InstCombineStoreToCast(*this, SI))
11987         return Res;
11988 
11989   
11990   // If this store is the last instruction in the basic block (possibly
11991   // excepting debug info instructions and the pointer bitcasts that feed
11992   // into them), and if the block ends with an unconditional branch, try
11993   // to move it to the successor block.
11994   BBI = &SI; 
11995   do {
11996     ++BBI;
11997   } while (isa<DbgInfoIntrinsic>(BBI) ||
11998            (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType())));
11999   if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
12000     if (BI->isUnconditional())
12001       if (SimplifyStoreAtEndOfBlock(SI))
12002         return 0;  // xform done!
12003   
12004   return 0;
12005 }
12006 
12007 /// SimplifyStoreAtEndOfBlock - Turn things like:
12008 ///   if () { *P = v1; } else { *P = v2 }
12009 /// into a phi node with a store in the successor.
12010 ///
12011 /// Simplify things like:
12012 ///   *P = v1; if () { *P = v2; }
12013 /// into a phi node with a store in the successor.
12014 ///
12015 bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
12016   BasicBlock *StoreBB = SI.getParent();
12017   
12018   // Check to see if the successor block has exactly two incoming edges.  If
12019   // so, see if the other predecessor contains a store to the same location.
12020   // if so, insert a PHI node (if needed) and move the stores down.
12021   BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
12022   
12023   // Determine whether Dest has exactly two predecessors and, if so, compute
12024   // the other predecessor.
12025   pred_iterator PI = pred_begin(DestBB);
12026   BasicBlock *OtherBB = 0;
12027   if (*PI != StoreBB)
12028     OtherBB = *PI;
12029   ++PI;
12030   if (PI == pred_end(DestBB))
12031     return false;
12032   
12033   if (*PI != StoreBB) {
12034     if (OtherBB)
12035       return false;
12036     OtherBB = *PI;
12037   }
12038   if (++PI != pred_end(DestBB))
12039     return false;
12040 
12041   // Bail out if all the relevant blocks aren't distinct (this can happen,
12042   // for example, if SI is in an infinite loop)
12043   if (StoreBB == DestBB || OtherBB == DestBB)
12044     return false;
12045 
12046   // Verify that the other block ends in a branch and is not otherwise empty.
12047   BasicBlock::iterator BBI = OtherBB->getTerminator();
12048   BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
12049   if (!OtherBr || BBI == OtherBB->begin())
12050     return false;
12051   
12052   // If the other block ends in an unconditional branch, check for the 'if then
12053   // else' case.  there is an instruction before the branch.
12054   StoreInst *OtherStore = 0;
12055   if (OtherBr->isUnconditional()) {
12056     --BBI;
12057     // Skip over debugging info.
12058     while (isa<DbgInfoIntrinsic>(BBI) ||
12059            (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) {
12060       if (BBI==OtherBB->begin())
12061         return false;
12062       --BBI;
12063     }
12064     // If this isn't a store, or isn't a store to the same location, bail out.
12065     OtherStore = dyn_cast<StoreInst>(BBI);
12066     if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1))
12067       return false;
12068   } else {
12069     // Otherwise, the other block ended with a conditional branch. If one of the
12070     // destinations is StoreBB, then we have the if/then case.
12071     if (OtherBr->getSuccessor(0) != StoreBB && 
12072         OtherBr->getSuccessor(1) != StoreBB)
12073       return false;
12074     
12075     // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
12076     // if/then triangle.  See if there is a store to the same ptr as SI that
12077     // lives in OtherBB.
12078     for (;; --BBI) {
12079       // Check to see if we find the matching store.
12080       if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
12081         if (OtherStore->getOperand(1) != SI.getOperand(1))
12082           return false;
12083         break;
12084       }
12085       // If we find something that may be using or overwriting the stored
12086       // value, or if we run out of instructions, we can't do the xform.
12087       if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
12088           BBI == OtherBB->begin())
12089         return false;
12090     }
12091     
12092     // In order to eliminate the store in OtherBr, we have to
12093     // make sure nothing reads or overwrites the stored value in
12094     // StoreBB.
12095     for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
12096       // FIXME: This should really be AA driven.
12097       if (I->mayReadFromMemory() || I->mayWriteToMemory())
12098         return false;
12099     }
12100   }
12101   
12102   // Insert a PHI node now if we need it.
12103   Value *MergedVal = OtherStore->getOperand(0);
12104   if (MergedVal != SI.getOperand(0)) {
12105     PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge");
12106     PN->reserveOperandSpace(2);
12107     PN->addIncoming(SI.getOperand(0), SI.getParent());
12108     PN->addIncoming(OtherStore->getOperand(0), OtherBB);
12109     MergedVal = InsertNewInstBefore(PN, DestBB->front());
12110   }
12111   
12112   // Advance to a place where it is safe to insert the new store and
12113   // insert it.
12114   BBI = DestBB->getFirstNonPHI();
12115   InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
12116                                     OtherStore->isVolatile()), *BBI);
12117   
12118   // Nuke the old stores.
12119   EraseInstFromFunction(SI);
12120   EraseInstFromFunction(*OtherStore);
12121   ++NumCombined;
12122   return true;
12123 }
12124 
12125 
12126 Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
12127   // Change br (not X), label True, label False to: br X, label False, True
12128   Value *X = 0;
12129   BasicBlock *TrueDest;
12130   BasicBlock *FalseDest;
12131   if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
12132       !isa<Constant>(X)) {
12133     // Swap Destinations and condition...
12134     BI.setCondition(X);
12135     BI.setSuccessor(0, FalseDest);
12136     BI.setSuccessor(1, TrueDest);
12137     return &BI;
12138   }
12139 
12140   // Cannonicalize fcmp_one -> fcmp_oeq
12141   FCmpInst::Predicate FPred; Value *Y;
12142   if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), 
12143                              TrueDest, FalseDest)))
12144     if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
12145          FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) {
12146       FCmpInst *I = cast<FCmpInst>(BI.getCondition());
12147       FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred);
12148       Instruction *NewSCC = new FCmpInst(I, NewPred, X, Y, "");
12149       NewSCC->takeName(I);
12150       // Swap Destinations and condition...
12151       BI.setCondition(NewSCC);
12152       BI.setSuccessor(0, FalseDest);
12153       BI.setSuccessor(1, TrueDest);
12154       RemoveFromWorkList(I);
12155       I->eraseFromParent();
12156       AddToWorkList(NewSCC);
12157       return &BI;
12158     }
12159 
12160   // Cannonicalize icmp_ne -> icmp_eq
12161   ICmpInst::Predicate IPred;
12162   if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
12163                       TrueDest, FalseDest)))
12164     if ((IPred == ICmpInst::ICMP_NE  || IPred == ICmpInst::ICMP_ULE ||
12165          IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
12166          IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) {
12167       ICmpInst *I = cast<ICmpInst>(BI.getCondition());
12168       ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred);
12169       Instruction *NewSCC = new ICmpInst(I, NewPred, X, Y, "");
12170       NewSCC->takeName(I);
12171       // Swap Destinations and condition...
12172       BI.setCondition(NewSCC);
12173       BI.setSuccessor(0, FalseDest);
12174       BI.setSuccessor(1, TrueDest);
12175       RemoveFromWorkList(I);
12176       I->eraseFromParent();;
12177       AddToWorkList(NewSCC);
12178       return &BI;
12179     }
12180 
12181   return 0;
12182 }
12183 
12184 Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
12185   Value *Cond = SI.getCondition();
12186   if (Instruction *I = dyn_cast<Instruction>(Cond)) {
12187     if (I->getOpcode() == Instruction::Add)
12188       if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
12189         // change 'switch (X+4) case 1:' into 'switch (X) case -3'
12190         for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
12191           SI.setOperand(i,
12192                    ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
12193                                                 AddRHS));
12194         SI.setOperand(0, I->getOperand(0));
12195         AddToWorkList(I);
12196         return &SI;
12197       }
12198   }
12199   return 0;
12200 }
12201 
12202 Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) {
12203   Value *Agg = EV.getAggregateOperand();
12204 
12205   if (!EV.hasIndices())
12206     return ReplaceInstUsesWith(EV, Agg);
12207 
12208   if (Constant *C = dyn_cast<Constant>(Agg)) {
12209     if (isa<UndefValue>(C))
12210       return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
12211       
12212     if (isa<ConstantAggregateZero>(C))
12213       return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
12214 
12215     if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
12216       // Extract the element indexed by the first index out of the constant
12217       Value *V = C->getOperand(*EV.idx_begin());
12218       if (EV.getNumIndices() > 1)
12219         // Extract the remaining indices out of the constant indexed by the
12220         // first index
12221         return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end());
12222       else
12223         return ReplaceInstUsesWith(EV, V);
12224     }
12225     return 0; // Can't handle other constants
12226   } 
12227   if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
12228     // We're extracting from an insertvalue instruction, compare the indices
12229     const unsigned *exti, *exte, *insi, *inse;
12230     for (exti = EV.idx_begin(), insi = IV->idx_begin(),
12231          exte = EV.idx_end(), inse = IV->idx_end();
12232          exti != exte && insi != inse;
12233          ++exti, ++insi) {
12234       if (*insi != *exti)
12235         // The insert and extract both reference distinctly different elements.
12236         // This means the extract is not influenced by the insert, and we can
12237         // replace the aggregate operand of the extract with the aggregate
12238         // operand of the insert. i.e., replace
12239         // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
12240         // %E = extractvalue { i32, { i32 } } %I, 0
12241         // with
12242         // %E = extractvalue { i32, { i32 } } %A, 0
12243         return ExtractValueInst::Create(IV->getAggregateOperand(),
12244                                         EV.idx_begin(), EV.idx_end());
12245     }
12246     if (exti == exte && insi == inse)
12247       // Both iterators are at the end: Index lists are identical. Replace
12248       // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
12249       // %C = extractvalue { i32, { i32 } } %B, 1, 0
12250       // with "i32 42"
12251       return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand());
12252     if (exti == exte) {
12253       // The extract list is a prefix of the insert list. i.e. replace
12254       // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
12255       // %E = extractvalue { i32, { i32 } } %I, 1
12256       // with
12257       // %X = extractvalue { i32, { i32 } } %A, 1
12258       // %E = insertvalue { i32 } %X, i32 42, 0
12259       // by switching the order of the insert and extract (though the
12260       // insertvalue should be left in, since it may have other uses).
12261       Value *NewEV = InsertNewInstBefore(
12262         ExtractValueInst::Create(IV->getAggregateOperand(),
12263                                  EV.idx_begin(), EV.idx_end()),
12264         EV);
12265       return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
12266                                      insi, inse);
12267     }
12268     if (insi == inse)
12269       // The insert list is a prefix of the extract list
12270       // We can simply remove the common indices from the extract and make it
12271       // operate on the inserted value instead of the insertvalue result.
12272       // i.e., replace
12273       // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
12274       // %E = extractvalue { i32, { i32 } } %I, 1, 0
12275       // with
12276       // %E extractvalue { i32 } { i32 42 }, 0
12277       return ExtractValueInst::Create(IV->getInsertedValueOperand(), 
12278                                       exti, exte);
12279   }
12280   // Can't simplify extracts from other values. Note that nested extracts are
12281   // already simplified implicitely by the above (extract ( extract (insert) )
12282   // will be translated into extract ( insert ( extract ) ) first and then just
12283   // the value inserted, if appropriate).
12284   return 0;
12285 }
12286 
12287 /// CheapToScalarize - Return true if the value is cheaper to scalarize than it
12288 /// is to leave as a vector operation.
12289 static bool CheapToScalarize(Value *V, bool isConstant) {
12290   if (isa<ConstantAggregateZero>(V)) 
12291     return true;
12292   if (ConstantVector *C = dyn_cast<ConstantVector>(V)) {
12293     if (isConstant) return true;
12294     // If all elts are the same, we can extract.
12295     Constant *Op0 = C->getOperand(0);
12296     for (unsigned i = 1; i < C->getNumOperands(); ++i)
12297       if (C->getOperand(i) != Op0)
12298         return false;
12299     return true;
12300   }
12301   Instruction *I = dyn_cast<Instruction>(V);
12302   if (!I) return false;
12303   
12304   // Insert element gets simplified to the inserted element or is deleted if
12305   // this is constant idx extract element and its a constant idx insertelt.
12306   if (I->getOpcode() == Instruction::InsertElement && isConstant &&
12307       isa<ConstantInt>(I->getOperand(2)))
12308     return true;
12309   if (I->getOpcode() == Instruction::Load && I->hasOneUse())
12310     return true;
12311   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
12312     if (BO->hasOneUse() &&
12313         (CheapToScalarize(BO->getOperand(0), isConstant) ||
12314          CheapToScalarize(BO->getOperand(1), isConstant)))
12315       return true;
12316   if (CmpInst *CI = dyn_cast<CmpInst>(I))
12317     if (CI->hasOneUse() &&
12318         (CheapToScalarize(CI->getOperand(0), isConstant) ||
12319          CheapToScalarize(CI->getOperand(1), isConstant)))
12320       return true;
12321   
12322   return false;
12323 }
12324 
12325 /// Read and decode a shufflevector mask.
12326 ///
12327 /// It turns undef elements into values that are larger than the number of
12328 /// elements in the input.
12329 static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) {
12330   unsigned NElts = SVI->getType()->getNumElements();
12331   if (isa<ConstantAggregateZero>(SVI->getOperand(2)))
12332     return std::vector<unsigned>(NElts, 0);
12333   if (isa<UndefValue>(SVI->getOperand(2)))
12334     return std::vector<unsigned>(NElts, 2*NElts);
12335 
12336   std::vector<unsigned> Result;
12337   const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2));
12338   for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i)
12339     if (isa<UndefValue>(*i))
12340       Result.push_back(NElts*2);  // undef -> 8
12341     else
12342       Result.push_back(cast<ConstantInt>(*i)->getZExtValue());
12343   return Result;
12344 }
12345 
12346 /// FindScalarElement - Given a vector and an element number, see if the scalar
12347 /// value is already around as a register, for example if it were inserted then
12348 /// extracted from the vector.
12349 static Value *FindScalarElement(Value *V, unsigned EltNo,
12350                                 LLVMContext *Context) {
12351   assert(isa<VectorType>(V->getType()) && "Not looking at a vector?");
12352   const VectorType *PTy = cast<VectorType>(V->getType());
12353   unsigned Width = PTy->getNumElements();
12354   if (EltNo >= Width)  // Out of range access.
12355     return UndefValue::get(PTy->getElementType());
12356   
12357   if (isa<UndefValue>(V))
12358     return UndefValue::get(PTy->getElementType());
12359   else if (isa<ConstantAggregateZero>(V))
12360     return Constant::getNullValue(PTy->getElementType());
12361   else if (ConstantVector *CP = dyn_cast<ConstantVector>(V))
12362     return CP->getOperand(EltNo);
12363   else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
12364     // If this is an insert to a variable element, we don't know what it is.
12365     if (!isa<ConstantInt>(III->getOperand(2))) 
12366       return 0;
12367     unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
12368     
12369     // If this is an insert to the element we are looking for, return the
12370     // inserted value.
12371     if (EltNo == IIElt) 
12372       return III->getOperand(1);
12373     
12374     // Otherwise, the insertelement doesn't modify the value, recurse on its
12375     // vector input.
12376     return FindScalarElement(III->getOperand(0), EltNo, Context);
12377   } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
12378     unsigned LHSWidth =
12379       cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
12380     unsigned InEl = getShuffleMask(SVI)[EltNo];
12381     if (InEl < LHSWidth)
12382       return FindScalarElement(SVI->getOperand(0), InEl, Context);
12383     else if (InEl < LHSWidth*2)
12384       return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth, Context);
12385     else
12386       return UndefValue::get(PTy->getElementType());
12387   }
12388   
12389   // Otherwise, we don't know.
12390   return 0;
12391 }
12392 
12393 Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
12394   // If vector val is undef, replace extract with scalar undef.
12395   if (isa<UndefValue>(EI.getOperand(0)))
12396     return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
12397 
12398   // If vector val is constant 0, replace extract with scalar 0.
12399   if (isa<ConstantAggregateZero>(EI.getOperand(0)))
12400     return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
12401   
12402   if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) {
12403     // If vector val is constant with all elements the same, replace EI with
12404     // that element. When the elements are not identical, we cannot replace yet
12405     // (we do that below, but only when the index is constant).
12406     Constant *op0 = C->getOperand(0);
12407     for (unsigned i = 1; i < C->getNumOperands(); ++i)
12408       if (C->getOperand(i) != op0) {
12409         op0 = 0; 
12410         break;
12411       }
12412     if (op0)
12413       return ReplaceInstUsesWith(EI, op0);
12414   }
12415   
12416   // If extracting a specified index from the vector, see if we can recursively
12417   // find a previously computed scalar that was inserted into the vector.
12418   if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
12419     unsigned IndexVal = IdxC->getZExtValue();
12420     unsigned VectorWidth = 
12421       cast<VectorType>(EI.getOperand(0)->getType())->getNumElements();
12422       
12423     // If this is extracting an invalid index, turn this into undef, to avoid
12424     // crashing the code below.
12425     if (IndexVal >= VectorWidth)
12426       return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
12427     
12428     // This instruction only demands the single element from the input vector.
12429     // If the input vector has a single use, simplify it based on this use
12430     // property.
12431     if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
12432       APInt UndefElts(VectorWidth, 0);
12433       APInt DemandedMask(VectorWidth, 1 << IndexVal);
12434       if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
12435                                                 DemandedMask, UndefElts)) {
12436         EI.setOperand(0, V);
12437         return &EI;
12438       }
12439     }
12440     
12441     if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal, Context))
12442       return ReplaceInstUsesWith(EI, Elt);
12443     
12444     // If the this extractelement is directly using a bitcast from a vector of
12445     // the same number of elements, see if we can find the source element from
12446     // it.  In this case, we will end up needing to bitcast the scalars.
12447     if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
12448       if (const VectorType *VT = 
12449               dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
12450         if (VT->getNumElements() == VectorWidth)
12451           if (Value *Elt = FindScalarElement(BCI->getOperand(0),
12452                                              IndexVal, Context))
12453             return new BitCastInst(Elt, EI.getType());
12454     }
12455   }
12456   
12457   if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
12458     if (I->hasOneUse()) {
12459       // Push extractelement into predecessor operation if legal and
12460       // profitable to do so
12461       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
12462         bool isConstantElt = isa<ConstantInt>(EI.getOperand(1));
12463         if (CheapToScalarize(BO, isConstantElt)) {
12464           ExtractElementInst *newEI0 = 
12465             ExtractElementInst::Create(BO->getOperand(0), EI.getOperand(1),
12466                                    EI.getName()+".lhs");
12467           ExtractElementInst *newEI1 =
12468             ExtractElementInst::Create(BO->getOperand(1), EI.getOperand(1),
12469                                    EI.getName()+".rhs");
12470           InsertNewInstBefore(newEI0, EI);
12471           InsertNewInstBefore(newEI1, EI);
12472           return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
12473         }
12474       } else if (isa<LoadInst>(I)) {
12475         unsigned AS = 
12476           cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace();
12477         Value *Ptr = InsertBitCastBefore(I->getOperand(0),
12478                                   PointerType::get(EI.getType(), AS),*I);
12479         GetElementPtrInst *GEP =
12480           GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep");
12481         cast<GEPOperator>(GEP)->setIsInBounds(true);
12482         InsertNewInstBefore(GEP, *I);
12483         LoadInst* Load = new LoadInst(GEP, "tmp");
12484         InsertNewInstBefore(Load, *I);
12485         return ReplaceInstUsesWith(EI, Load);
12486       }
12487     }
12488     if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
12489       // Extracting the inserted element?
12490       if (IE->getOperand(2) == EI.getOperand(1))
12491         return ReplaceInstUsesWith(EI, IE->getOperand(1));
12492       // If the inserted and extracted elements are constants, they must not
12493       // be the same value, extract from the pre-inserted value instead.
12494       if (isa<Constant>(IE->getOperand(2)) &&
12495           isa<Constant>(EI.getOperand(1))) {
12496         AddUsesToWorkList(EI);
12497         EI.setOperand(0, IE->getOperand(0));
12498         return &EI;
12499       }
12500     } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
12501       // If this is extracting an element from a shufflevector, figure out where
12502       // it came from and extract from the appropriate input element instead.
12503       if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
12504         unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()];
12505         Value *Src;
12506         unsigned LHSWidth =
12507           cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
12508 
12509         if (SrcIdx < LHSWidth)
12510           Src = SVI->getOperand(0);
12511         else if (SrcIdx < LHSWidth*2) {
12512           SrcIdx -= LHSWidth;
12513           Src = SVI->getOperand(1);
12514         } else {
12515           return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
12516         }
12517         return ExtractElementInst::Create(Src,
12518                          ConstantInt::get(Type::getInt32Ty(*Context), SrcIdx, false));
12519       }
12520     }
12521     // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
12522   }
12523   return 0;
12524 }
12525 
12526 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
12527 /// elements from either LHS or RHS, return the shuffle mask and true. 
12528 /// Otherwise, return false.
12529 static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
12530                                          std::vector<Constant*> &Mask,
12531                                          LLVMContext *Context) {
12532   assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
12533          "Invalid CollectSingleShuffleElements");
12534   unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
12535 
12536   if (isa<UndefValue>(V)) {
12537     Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(*Context)));
12538     return true;
12539   } else if (V == LHS) {
12540     for (unsigned i = 0; i != NumElts; ++i)
12541       Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i));
12542     return true;
12543   } else if (V == RHS) {
12544     for (unsigned i = 0; i != NumElts; ++i)
12545       Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i+NumElts));
12546     return true;
12547   } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
12548     // If this is an insert of an extract from some other vector, include it.
12549     Value *VecOp    = IEI->getOperand(0);
12550     Value *ScalarOp = IEI->getOperand(1);
12551     Value *IdxOp    = IEI->getOperand(2);
12552     
12553     if (!isa<ConstantInt>(IdxOp))
12554       return false;
12555     unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
12556     
12557     if (isa<UndefValue>(ScalarOp)) {  // inserting undef into vector.
12558       // Okay, we can handle this if the vector we are insertinting into is
12559       // transitively ok.
12560       if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) {
12561         // If so, update the mask to reflect the inserted undef.
12562         Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(*Context));
12563         return true;
12564       }      
12565     } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
12566       if (isa<ConstantInt>(EI->getOperand(1)) &&
12567           EI->getOperand(0)->getType() == V->getType()) {
12568         unsigned ExtractedIdx =
12569           cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
12570         
12571         // This must be extracting from either LHS or RHS.
12572         if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
12573           // Okay, we can handle this if the vector we are insertinting into is
12574           // transitively ok.
12575           if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) {
12576             // If so, update the mask to reflect the inserted value.
12577             if (EI->getOperand(0) == LHS) {
12578               Mask[InsertedIdx % NumElts] = 
12579                  ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx);
12580             } else {
12581               assert(EI->getOperand(0) == RHS);
12582               Mask[InsertedIdx % NumElts] = 
12583                 ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx+NumElts);
12584               
12585             }
12586             return true;
12587           }
12588         }
12589       }
12590     }
12591   }
12592   // TODO: Handle shufflevector here!
12593   
12594   return false;
12595 }
12596 
12597 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the
12598 /// RHS of the shuffle instruction, if it is not null.  Return a shuffle mask
12599 /// that computes V and the LHS value of the shuffle.
12600 static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
12601                                      Value *&RHS, LLVMContext *Context) {
12602   assert(isa<VectorType>(V->getType()) && 
12603          (RHS == 0 || V->getType() == RHS->getType()) &&
12604          "Invalid shuffle!");
12605   unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
12606 
12607   if (isa<UndefValue>(V)) {
12608     Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(*Context)));
12609     return V;
12610   } else if (isa<ConstantAggregateZero>(V)) {
12611     Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(*Context), 0));
12612     return V;
12613   } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
12614     // If this is an insert of an extract from some other vector, include it.
12615     Value *VecOp    = IEI->getOperand(0);
12616     Value *ScalarOp = IEI->getOperand(1);
12617     Value *IdxOp    = IEI->getOperand(2);
12618     
12619     if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
12620       if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
12621           EI->getOperand(0)->getType() == V->getType()) {
12622         unsigned ExtractedIdx =
12623           cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
12624         unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
12625         
12626         // Either the extracted from or inserted into vector must be RHSVec,
12627         // otherwise we'd end up with a shuffle of three inputs.
12628         if (EI->getOperand(0) == RHS || RHS == 0) {
12629           RHS = EI->getOperand(0);
12630           Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context);
12631           Mask[InsertedIdx % NumElts] = 
12632             ConstantInt::get(Type::getInt32Ty(*Context), NumElts+ExtractedIdx);
12633           return V;
12634         }
12635         
12636         if (VecOp == RHS) {
12637           Value *V = CollectShuffleElements(EI->getOperand(0), Mask,
12638                                             RHS, Context);
12639           // Everything but the extracted element is replaced with the RHS.
12640           for (unsigned i = 0; i != NumElts; ++i) {
12641             if (i != InsertedIdx)
12642               Mask[i] = ConstantInt::get(Type::getInt32Ty(*Context), NumElts+i);
12643           }
12644           return V;
12645         }
12646         
12647         // If this insertelement is a chain that comes from exactly these two
12648         // vectors, return the vector and the effective shuffle.
12649         if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask,
12650                                          Context))
12651           return EI->getOperand(0);
12652         
12653       }
12654     }
12655   }
12656   // TODO: Handle shufflevector here!
12657   
12658   // Otherwise, can't do anything fancy.  Return an identity vector.
12659   for (unsigned i = 0; i != NumElts; ++i)
12660     Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i));
12661   return V;
12662 }
12663 
12664 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
12665   Value *VecOp    = IE.getOperand(0);
12666   Value *ScalarOp = IE.getOperand(1);
12667   Value *IdxOp    = IE.getOperand(2);
12668   
12669   // Inserting an undef or into an undefined place, remove this.
12670   if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
12671     ReplaceInstUsesWith(IE, VecOp);
12672   
12673   // If the inserted element was extracted from some other vector, and if the 
12674   // indexes are constant, try to turn this into a shufflevector operation.
12675   if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
12676     if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
12677         EI->getOperand(0)->getType() == IE.getType()) {
12678       unsigned NumVectorElts = IE.getType()->getNumElements();
12679       unsigned ExtractedIdx =
12680         cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
12681       unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
12682       
12683       if (ExtractedIdx >= NumVectorElts) // Out of range extract.
12684         return ReplaceInstUsesWith(IE, VecOp);
12685       
12686       if (InsertedIdx >= NumVectorElts)  // Out of range insert.
12687         return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
12688       
12689       // If we are extracting a value from a vector, then inserting it right
12690       // back into the same place, just use the input vector.
12691       if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
12692         return ReplaceInstUsesWith(IE, VecOp);      
12693       
12694       // We could theoretically do this for ANY input.  However, doing so could
12695       // turn chains of insertelement instructions into a chain of shufflevector
12696       // instructions, and right now we do not merge shufflevectors.  As such,
12697       // only do this in a situation where it is clear that there is benefit.
12698       if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) {
12699         // Turn this into shuffle(EIOp0, VecOp, Mask).  The result has all of
12700         // the values of VecOp, except then one read from EIOp0.
12701         // Build a new shuffle mask.
12702         std::vector<Constant*> Mask;
12703         if (isa<UndefValue>(VecOp))
12704           Mask.assign(NumVectorElts, UndefValue::get(Type::getInt32Ty(*Context)));
12705         else {
12706           assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
12707           Mask.assign(NumVectorElts, ConstantInt::get(Type::getInt32Ty(*Context),
12708                                                        NumVectorElts));
12709         } 
12710         Mask[InsertedIdx] = 
12711                            ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx);
12712         return new ShuffleVectorInst(EI->getOperand(0), VecOp,
12713                                      ConstantVector::get(Mask));
12714       }
12715       
12716       // If this insertelement isn't used by some other insertelement, turn it
12717       // (and any insertelements it points to), into one big shuffle.
12718       if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
12719         std::vector<Constant*> Mask;
12720         Value *RHS = 0;
12721         Value *LHS = CollectShuffleElements(&IE, Mask, RHS, Context);
12722         if (RHS == 0) RHS = UndefValue::get(LHS->getType());
12723         // We now have a shuffle of LHS, RHS, Mask.
12724         return new ShuffleVectorInst(LHS, RHS,
12725                                      ConstantVector::get(Mask));
12726       }
12727     }
12728   }
12729 
12730   unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
12731   APInt UndefElts(VWidth, 0);
12732   APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
12733   if (SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts))
12734     return &IE;
12735 
12736   return 0;
12737 }
12738 
12739 
12740 Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
12741   Value *LHS = SVI.getOperand(0);
12742   Value *RHS = SVI.getOperand(1);
12743   std::vector<unsigned> Mask = getShuffleMask(&SVI);
12744 
12745   bool MadeChange = false;
12746 
12747   // Undefined shuffle mask -> undefined value.
12748   if (isa<UndefValue>(SVI.getOperand(2)))
12749     return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
12750 
12751   unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
12752 
12753   if (VWidth != cast<VectorType>(LHS->getType())->getNumElements())
12754     return 0;
12755 
12756   APInt UndefElts(VWidth, 0);
12757   APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
12758   if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
12759     LHS = SVI.getOperand(0);
12760     RHS = SVI.getOperand(1);
12761     MadeChange = true;
12762   }
12763   
12764   // Canonicalize shuffle(x    ,x,mask) -> shuffle(x, undef,mask')
12765   // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
12766   if (LHS == RHS || isa<UndefValue>(LHS)) {
12767     if (isa<UndefValue>(LHS) && LHS == RHS) {
12768       // shuffle(undef,undef,mask) -> undef.
12769       return ReplaceInstUsesWith(SVI, LHS);
12770     }
12771     
12772     // Remap any references to RHS to use LHS.
12773     std::vector<Constant*> Elts;
12774     for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
12775       if (Mask[i] >= 2*e)
12776         Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
12777       else {
12778         if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
12779             (Mask[i] <  e && isa<UndefValue>(LHS))) {
12780           Mask[i] = 2*e;     // Turn into undef.
12781           Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
12782         } else {
12783           Mask[i] = Mask[i] % e;  // Force to LHS.
12784           Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context), Mask[i]));
12785         }
12786       }
12787     }
12788     SVI.setOperand(0, SVI.getOperand(1));
12789     SVI.setOperand(1, UndefValue::get(RHS->getType()));
12790     SVI.setOperand(2, ConstantVector::get(Elts));
12791     LHS = SVI.getOperand(0);
12792     RHS = SVI.getOperand(1);
12793     MadeChange = true;
12794   }
12795   
12796   // Analyze the shuffle, are the LHS or RHS and identity shuffles?
12797   bool isLHSID = true, isRHSID = true;
12798     
12799   for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
12800     if (Mask[i] >= e*2) continue;  // Ignore undef values.
12801     // Is this an identity shuffle of the LHS value?
12802     isLHSID &= (Mask[i] == i);
12803       
12804     // Is this an identity shuffle of the RHS value?
12805     isRHSID &= (Mask[i]-e == i);
12806   }
12807 
12808   // Eliminate identity shuffles.
12809   if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
12810   if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
12811   
12812   // If the LHS is a shufflevector itself, see if we can combine it with this
12813   // one without producing an unusual shuffle.  Here we are really conservative:
12814   // we are absolutely afraid of producing a shuffle mask not in the input
12815   // program, because the code gen may not be smart enough to turn a merged
12816   // shuffle into two specific shuffles: it may produce worse code.  As such,
12817   // we only merge two shuffles if the result is one of the two input shuffle
12818   // masks.  In this case, merging the shuffles just removes one instruction,
12819   // which we know is safe.  This is good for things like turning:
12820   // (splat(splat)) -> splat.
12821   if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
12822     if (isa<UndefValue>(RHS)) {
12823       std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI);
12824 
12825       std::vector<unsigned> NewMask;
12826       for (unsigned i = 0, e = Mask.size(); i != e; ++i)
12827         if (Mask[i] >= 2*e)
12828           NewMask.push_back(2*e);
12829         else
12830           NewMask.push_back(LHSMask[Mask[i]]);
12831       
12832       // If the result mask is equal to the src shuffle or this shuffle mask, do
12833       // the replacement.
12834       if (NewMask == LHSMask || NewMask == Mask) {
12835         unsigned LHSInNElts =
12836           cast<VectorType>(LHSSVI->getOperand(0)->getType())->getNumElements();
12837         std::vector<Constant*> Elts;
12838         for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
12839           if (NewMask[i] >= LHSInNElts*2) {
12840             Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
12841           } else {
12842             Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context), NewMask[i]));
12843           }
12844         }
12845         return new ShuffleVectorInst(LHSSVI->getOperand(0),
12846                                      LHSSVI->getOperand(1),
12847                                      ConstantVector::get(Elts));
12848       }
12849     }
12850   }
12851 
12852   return MadeChange ? &SVI : 0;
12853 }
12854 
12855 
12856 
12857 
12858 /// TryToSinkInstruction - Try to move the specified instruction from its
12859 /// current block into the beginning of DestBlock, which can only happen if it's
12860 /// safe to move the instruction past all of the instructions between it and the
12861 /// end of its block.
12862 static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
12863   assert(I->hasOneUse() && "Invariants didn't hold!");
12864 
12865   // Cannot move control-flow-involving, volatile loads, vaarg, etc.
12866   if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I))
12867     return false;
12868 
12869   // Do not sink alloca instructions out of the entry block.
12870   if (isa<AllocaInst>(I) && I->getParent() ==
12871         &DestBlock->getParent()->getEntryBlock())
12872     return false;
12873 
12874   // We can only sink load instructions if there is nothing between the load and
12875   // the end of block that could change the value.
12876   if (I->mayReadFromMemory()) {
12877     for (BasicBlock::iterator Scan = I, E = I->getParent()->end();
12878          Scan != E; ++Scan)
12879       if (Scan->mayWriteToMemory())
12880         return false;
12881   }
12882 
12883   BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
12884 
12885   CopyPrecedingStopPoint(I, InsertPos);
12886   I->moveBefore(InsertPos);
12887   ++NumSunkInst;
12888   return true;
12889 }
12890 
12891 
12892 /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
12893 /// all reachable code to the worklist.
12894 ///
12895 /// This has a couple of tricks to make the code faster and more powerful.  In
12896 /// particular, we constant fold and DCE instructions as we go, to avoid adding
12897 /// them to the worklist (this significantly speeds up instcombine on code where
12898 /// many instructions are dead or constant).  Additionally, if we find a branch
12899 /// whose condition is a known constant, we only visit the reachable successors.
12900 ///
12901 static void AddReachableCodeToWorklist(BasicBlock *BB, 
12902                                        SmallPtrSet<BasicBlock*, 64> &Visited,
12903                                        InstCombiner &IC,
12904                                        const TargetData *TD) {
12905   SmallVector<BasicBlock*, 256> Worklist;
12906   Worklist.push_back(BB);
12907 
12908   while (!Worklist.empty()) {
12909     BB = Worklist.back();
12910     Worklist.pop_back();
12911     
12912     // We have now visited this block!  If we've already been here, ignore it.
12913     if (!Visited.insert(BB)) continue;
12914 
12915     DbgInfoIntrinsic *DBI_Prev = NULL;
12916     for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
12917       Instruction *Inst = BBI++;
12918       
12919       // DCE instruction if trivially dead.
12920       if (isInstructionTriviallyDead(Inst)) {
12921         ++NumDeadInst;
12922         DOUT << "IC: DCE: " << *Inst << '\n';
12923         Inst->eraseFromParent();
12924         continue;
12925       }
12926       
12927       // ConstantProp instruction if trivially constant.
12928       if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) {
12929         DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst << '\n';
12930         Inst->replaceAllUsesWith(C);
12931         ++NumConstProp;
12932         Inst->eraseFromParent();
12933         continue;
12934       }
12935      
12936       // If there are two consecutive llvm.dbg.stoppoint calls then
12937       // it is likely that the optimizer deleted code in between these
12938       // two intrinsics. 
12939       DbgInfoIntrinsic *DBI_Next = dyn_cast<DbgInfoIntrinsic>(Inst);
12940       if (DBI_Next) {
12941         if (DBI_Prev
12942             && DBI_Prev->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint
12943             && DBI_Next->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint) {
12944           IC.RemoveFromWorkList(DBI_Prev);
12945           DBI_Prev->eraseFromParent();
12946         }
12947         DBI_Prev = DBI_Next;
12948       } else {
12949         DBI_Prev = 0;
12950       }
12951 
12952       IC.AddToWorkList(Inst);
12953     }
12954 
12955     // Recursively visit successors.  If this is a branch or switch on a
12956     // constant, only visit the reachable successor.
12957     TerminatorInst *TI = BB->getTerminator();
12958     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
12959       if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
12960         bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
12961         BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
12962         Worklist.push_back(ReachableBB);
12963         continue;
12964       }
12965     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
12966       if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
12967         // See if this is an explicit destination.
12968         for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
12969           if (SI->getCaseValue(i) == Cond) {
12970             BasicBlock *ReachableBB = SI->getSuccessor(i);
12971             Worklist.push_back(ReachableBB);
12972             continue;
12973           }
12974         
12975         // Otherwise it is the default destination.
12976         Worklist.push_back(SI->getSuccessor(0));
12977         continue;
12978       }
12979     }
12980     
12981     for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
12982       Worklist.push_back(TI->getSuccessor(i));
12983   }
12984 }
12985 
12986 bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
12987   bool Changed = false;
12988   TD = getAnalysisIfAvailable<TargetData>();
12989   
12990   DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
12991         << F.getNameStr() << "\n");
12992 
12993   {
12994     // Do a depth-first traversal of the function, populate the worklist with
12995     // the reachable instructions.  Ignore blocks that are not reachable.  Keep
12996     // track of which blocks we visit.
12997     SmallPtrSet<BasicBlock*, 64> Visited;
12998     AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
12999 
13000     // Do a quick scan over the function.  If we find any blocks that are
13001     // unreachable, remove any instructions inside of them.  This prevents
13002     // the instcombine code from having to deal with some bad special cases.
13003     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
13004       if (!Visited.count(BB)) {
13005         Instruction *Term = BB->getTerminator();
13006         while (Term != BB->begin()) {   // Remove instrs bottom-up
13007           BasicBlock::iterator I = Term; --I;
13008 
13009           DOUT << "IC: DCE: " << *I << '\n';
13010           // A debug intrinsic shouldn't force another iteration if we weren't
13011           // going to do one without it.
13012           if (!isa<DbgInfoIntrinsic>(I)) {
13013             ++NumDeadInst;
13014             Changed = true;
13015           }
13016           if (!I->use_empty())
13017             I->replaceAllUsesWith(UndefValue::get(I->getType()));
13018           I->eraseFromParent();
13019         }
13020       }
13021   }
13022 
13023   while (!Worklist.empty()) {
13024     Instruction *I = RemoveOneFromWorkList();
13025     if (I == 0) continue;  // skip null values.
13026 
13027     // Check to see if we can DCE the instruction.
13028     if (isInstructionTriviallyDead(I)) {
13029       // Add operands to the worklist.
13030       if (I->getNumOperands() < 4)
13031         AddUsesToWorkList(*I);
13032       ++NumDeadInst;
13033 
13034       DOUT << "IC: DCE: " << *I << '\n';
13035 
13036       I->eraseFromParent();
13037       RemoveFromWorkList(I);
13038       Changed = true;
13039       continue;
13040     }
13041 
13042     // Instruction isn't dead, see if we can constant propagate it.
13043     if (Constant *C = ConstantFoldInstruction(I, F.getContext(), TD)) {
13044       DOUT << "IC: ConstFold to: " << *C << " from: " << *I << '\n';
13045 
13046       // Add operands to the worklist.
13047       AddUsesToWorkList(*I);
13048       ReplaceInstUsesWith(*I, C);
13049 
13050       ++NumConstProp;
13051       I->eraseFromParent();
13052       RemoveFromWorkList(I);
13053       Changed = true;
13054       continue;
13055     }
13056 
13057     if (TD) {
13058       // See if we can constant fold its operands.
13059       for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
13060         if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i))
13061           if (Constant *NewC = ConstantFoldConstantExpression(CE,   
13062                                   F.getContext(), TD))
13063             if (NewC != CE) {
13064               i->set(NewC);
13065               Changed = true;
13066             }
13067     }
13068 
13069     // See if we can trivially sink this instruction to a successor basic block.
13070     if (I->hasOneUse()) {
13071       BasicBlock *BB = I->getParent();
13072       BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent();
13073       if (UserParent != BB) {
13074         bool UserIsSuccessor = false;
13075         // See if the user is one of our successors.
13076         for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
13077           if (*SI == UserParent) {
13078             UserIsSuccessor = true;
13079             break;
13080           }
13081 
13082         // If the user is one of our immediate successors, and if that successor
13083         // only has us as a predecessors (we'd have to split the critical edge
13084         // otherwise), we can keep going.
13085         if (UserIsSuccessor && !isa<PHINode>(I->use_back()) &&
13086             next(pred_begin(UserParent)) == pred_end(UserParent))
13087           // Okay, the CFG is simple enough, try to sink this instruction.
13088           Changed |= TryToSinkInstruction(I, UserParent);
13089       }
13090     }
13091 
13092     // Now that we have an instruction, try combining it to simplify it...
13093 #ifndef NDEBUG
13094     std::string OrigI;
13095 #endif
13096     DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str(););
13097     if (Instruction *Result = visit(*I)) {
13098       ++NumCombined;
13099       // Should we replace the old instruction with a new one?
13100       if (Result != I) {
13101         DOUT << "IC: Old = " << *I << '\n'
13102              << "    New = " << *Result << '\n';
13103 
13104         // Everything uses the new instruction now.
13105         I->replaceAllUsesWith(Result);
13106 
13107         // Push the new instruction and any users onto the worklist.
13108         AddToWorkList(Result);
13109         AddUsersToWorkList(*Result);
13110 
13111         // Move the name to the new instruction first.
13112         Result->takeName(I);
13113 
13114         // Insert the new instruction into the basic block...
13115         BasicBlock *InstParent = I->getParent();
13116         BasicBlock::iterator InsertPos = I;
13117 
13118         if (!isa<PHINode>(Result))        // If combining a PHI, don't insert
13119           while (isa<PHINode>(InsertPos)) // middle of a block of PHIs.
13120             ++InsertPos;
13121 
13122         InstParent->getInstList().insert(InsertPos, Result);
13123 
13124         // Make sure that we reprocess all operands now that we reduced their
13125         // use counts.
13126         AddUsesToWorkList(*I);
13127 
13128         // Instructions can end up on the worklist more than once.  Make sure
13129         // we do not process an instruction that has been deleted.
13130         RemoveFromWorkList(I);
13131 
13132         // Erase the old instruction.
13133         InstParent->getInstList().erase(I);
13134       } else {
13135 #ifndef NDEBUG
13136         DOUT << "IC: Mod = " << OrigI << '\n'
13137              << "    New = " << *I << '\n';
13138 #endif
13139 
13140         // If the instruction was modified, it's possible that it is now dead.
13141         // if so, remove it.
13142         if (isInstructionTriviallyDead(I)) {
13143           // Make sure we process all operands now that we are reducing their
13144           // use counts.
13145           AddUsesToWorkList(*I);
13146 
13147           // Instructions may end up in the worklist more than once.  Erase all
13148           // occurrences of this instruction.
13149           RemoveFromWorkList(I);
13150           I->eraseFromParent();
13151         } else {
13152           AddToWorkList(I);
13153           AddUsersToWorkList(*I);
13154         }
13155       }
13156       Changed = true;
13157     }
13158   }
13159 
13160   assert(WorklistMap.empty() && "Worklist empty, but map not?");
13161     
13162   // Do an explicit clear, this shrinks the map if needed.
13163   WorklistMap.clear();
13164   return Changed;
13165 }
13166 
13167 
13168 bool InstCombiner::runOnFunction(Function &F) {
13169   MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
13170   Context = &F.getContext();
13171   
13172   bool EverMadeChange = false;
13173 
13174   // Iterate while there is work to do.
13175   unsigned Iteration = 0;
13176   while (DoOneIteration(F, Iteration++))
13177     EverMadeChange = true;
13178   return EverMadeChange;
13179 }
13180 
13181 FunctionPass *llvm::createInstructionCombiningPass() {
13182   return new InstCombiner();
13183 }