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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 algebraic
00012 // simplification happens.
00013 //
00014 // This pass combines things like:
00015 //    %Y = add int %X, 1
00016 //    %Z = add int %Y, 1
00017 // into:
00018 //    %Z = add int %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. SetCC instructions are converted from <,>,<=,>= to ==,!= if possible
00028 //    4. All SetCC 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/Pass.h"
00040 #include "llvm/DerivedTypes.h"
00041 #include "llvm/GlobalVariable.h"
00042 #include "llvm/Target/TargetData.h"
00043 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00044 #include "llvm/Transforms/Utils/Local.h"
00045 #include "llvm/Support/CallSite.h"
00046 #include "llvm/Support/Debug.h"
00047 #include "llvm/Support/GetElementPtrTypeIterator.h"
00048 #include "llvm/Support/InstVisitor.h"
00049 #include "llvm/Support/MathExtras.h"
00050 #include "llvm/Support/PatternMatch.h"
00051 #include "llvm/Support/Visibility.h"
00052 #include "llvm/ADT/Statistic.h"
00053 #include "llvm/ADT/STLExtras.h"
00054 #include <algorithm>
00055 #include <iostream>
00056 using namespace llvm;
00057 using namespace llvm::PatternMatch;
00058 
00059 namespace {
00060   Statistic<> NumCombined ("instcombine", "Number of insts combined");
00061   Statistic<> NumConstProp("instcombine", "Number of constant folds");
00062   Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
00063   Statistic<> NumDeadStore("instcombine", "Number of dead stores eliminated");
00064   Statistic<> NumSunkInst ("instcombine", "Number of instructions sunk");
00065 
00066   class VISIBILITY_HIDDEN InstCombiner
00067     : public FunctionPass,
00068       public InstVisitor<InstCombiner, Instruction*> {
00069     // Worklist of all of the instructions that need to be simplified.
00070     std::vector<Instruction*> WorkList;
00071     TargetData *TD;
00072 
00073     /// AddUsersToWorkList - When an instruction is simplified, add all users of
00074     /// the instruction to the work lists because they might get more simplified
00075     /// now.
00076     ///
00077     void AddUsersToWorkList(Value &I) {
00078       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
00079            UI != UE; ++UI)
00080         WorkList.push_back(cast<Instruction>(*UI));
00081     }
00082 
00083     /// AddUsesToWorkList - When an instruction is simplified, add operands to
00084     /// the work lists because they might get more simplified now.
00085     ///
00086     void AddUsesToWorkList(Instruction &I) {
00087       for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
00088         if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i)))
00089           WorkList.push_back(Op);
00090     }
00091 
00092     // removeFromWorkList - remove all instances of I from the worklist.
00093     void removeFromWorkList(Instruction *I);
00094   public:
00095     virtual bool runOnFunction(Function &F);
00096 
00097     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00098       AU.addRequired<TargetData>();
00099       AU.addPreservedID(LCSSAID);
00100       AU.setPreservesCFG();
00101     }
00102 
00103     TargetData &getTargetData() const { return *TD; }
00104 
00105     // Visitation implementation - Implement instruction combining for different
00106     // instruction types.  The semantics are as follows:
00107     // Return Value:
00108     //    null        - No change was made
00109     //     I          - Change was made, I is still valid, I may be dead though
00110     //   otherwise    - Change was made, replace I with returned instruction
00111     //
00112     Instruction *visitAdd(BinaryOperator &I);
00113     Instruction *visitSub(BinaryOperator &I);
00114     Instruction *visitMul(BinaryOperator &I);
00115     Instruction *visitDiv(BinaryOperator &I);
00116     Instruction *visitRem(BinaryOperator &I);
00117     Instruction *visitAnd(BinaryOperator &I);
00118     Instruction *visitOr (BinaryOperator &I);
00119     Instruction *visitXor(BinaryOperator &I);
00120     Instruction *visitSetCondInst(SetCondInst &I);
00121     Instruction *visitSetCondInstWithCastAndCast(SetCondInst &SCI);
00122 
00123     Instruction *FoldGEPSetCC(User *GEPLHS, Value *RHS,
00124                               Instruction::BinaryOps Cond, Instruction &I);
00125     Instruction *visitShiftInst(ShiftInst &I);
00126     Instruction *FoldShiftByConstant(Value *Op0, ConstantUInt *Op1,
00127                                      ShiftInst &I);
00128     Instruction *visitCastInst(CastInst &CI);
00129     Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
00130                                 Instruction *FI);
00131     Instruction *visitSelectInst(SelectInst &CI);
00132     Instruction *visitCallInst(CallInst &CI);
00133     Instruction *visitInvokeInst(InvokeInst &II);
00134     Instruction *visitPHINode(PHINode &PN);
00135     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
00136     Instruction *visitAllocationInst(AllocationInst &AI);
00137     Instruction *visitFreeInst(FreeInst &FI);
00138     Instruction *visitLoadInst(LoadInst &LI);
00139     Instruction *visitStoreInst(StoreInst &SI);
00140     Instruction *visitBranchInst(BranchInst &BI);
00141     Instruction *visitSwitchInst(SwitchInst &SI);
00142     Instruction *visitInsertElementInst(InsertElementInst &IE);
00143     Instruction *visitExtractElementInst(ExtractElementInst &EI);
00144     Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
00145 
00146     // visitInstruction - Specify what to return for unhandled instructions...
00147     Instruction *visitInstruction(Instruction &I) { return 0; }
00148 
00149   private:
00150     Instruction *visitCallSite(CallSite CS);
00151     bool transformConstExprCastCall(CallSite CS);
00152 
00153   public:
00154     // InsertNewInstBefore - insert an instruction New before instruction Old
00155     // in the program.  Add the new instruction to the worklist.
00156     //
00157     Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
00158       assert(New && New->getParent() == 0 &&
00159              "New instruction already inserted into a basic block!");
00160       BasicBlock *BB = Old.getParent();
00161       BB->getInstList().insert(&Old, New);  // Insert inst
00162       WorkList.push_back(New);              // Add to worklist
00163       return New;
00164     }
00165 
00166     /// InsertCastBefore - Insert a cast of V to TY before the instruction POS.
00167     /// This also adds the cast to the worklist.  Finally, this returns the
00168     /// cast.
00169     Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) {
00170       if (V->getType() == Ty) return V;
00171 
00172       if (Constant *CV = dyn_cast<Constant>(V))
00173         return ConstantExpr::getCast(CV, Ty);
00174       
00175       Instruction *C = new CastInst(V, Ty, V->getName(), &Pos);
00176       WorkList.push_back(C);
00177       return C;
00178     }
00179 
00180     // ReplaceInstUsesWith - This method is to be used when an instruction is
00181     // found to be dead, replacable with another preexisting expression.  Here
00182     // we add all uses of I to the worklist, replace all uses of I with the new
00183     // value, then return I, so that the inst combiner will know that I was
00184     // modified.
00185     //
00186     Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
00187       AddUsersToWorkList(I);         // Add all modified instrs to worklist
00188       if (&I != V) {
00189         I.replaceAllUsesWith(V);
00190         return &I;
00191       } else {
00192         // If we are replacing the instruction with itself, this must be in a
00193         // segment of unreachable code, so just clobber the instruction.
00194         I.replaceAllUsesWith(UndefValue::get(I.getType()));
00195         return &I;
00196       }
00197     }
00198 
00199     // UpdateValueUsesWith - This method is to be used when an value is
00200     // found to be replacable with another preexisting expression or was
00201     // updated.  Here we add all uses of I to the worklist, replace all uses of
00202     // I with the new value (unless the instruction was just updated), then
00203     // return true, so that the inst combiner will know that I was modified.
00204     //
00205     bool UpdateValueUsesWith(Value *Old, Value *New) {
00206       AddUsersToWorkList(*Old);         // Add all modified instrs to worklist
00207       if (Old != New)
00208         Old->replaceAllUsesWith(New);
00209       if (Instruction *I = dyn_cast<Instruction>(Old))
00210         WorkList.push_back(I);
00211       if (Instruction *I = dyn_cast<Instruction>(New))
00212         WorkList.push_back(I);
00213       return true;
00214     }
00215     
00216     // EraseInstFromFunction - When dealing with an instruction that has side
00217     // effects or produces a void value, we can't rely on DCE to delete the
00218     // instruction.  Instead, visit methods should return the value returned by
00219     // this function.
00220     Instruction *EraseInstFromFunction(Instruction &I) {
00221       assert(I.use_empty() && "Cannot erase instruction that is used!");
00222       AddUsesToWorkList(I);
00223       removeFromWorkList(&I);
00224       I.eraseFromParent();
00225       return 0;  // Don't do anything with FI
00226     }
00227 
00228   private:
00229     /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
00230     /// InsertBefore instruction.  This is specialized a bit to avoid inserting
00231     /// casts that are known to not do anything...
00232     ///
00233     Value *InsertOperandCastBefore(Value *V, const Type *DestTy,
00234                                    Instruction *InsertBefore);
00235 
00236     // SimplifyCommutative - This performs a few simplifications for commutative
00237     // operators.
00238     bool SimplifyCommutative(BinaryOperator &I);
00239 
00240     bool SimplifyDemandedBits(Value *V, uint64_t Mask, 
00241                               uint64_t &KnownZero, uint64_t &KnownOne,
00242                               unsigned Depth = 0);
00243 
00244     // FoldOpIntoPhi - Given a binary operator or cast instruction which has a
00245     // PHI node as operand #0, see if we can fold the instruction into the PHI
00246     // (which is only possible if all operands to the PHI are constants).
00247     Instruction *FoldOpIntoPhi(Instruction &I);
00248 
00249     // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
00250     // operator and they all are only used by the PHI, PHI together their
00251     // inputs, and do the operation once, to the result of the PHI.
00252     Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
00253 
00254     Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS,
00255                           ConstantIntegral *AndRHS, BinaryOperator &TheAnd);
00256     
00257     Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantIntegral *Mask,
00258                               bool isSub, Instruction &I);
00259     Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
00260                                  bool Inside, Instruction &IB);
00261     Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI);
00262     Instruction *MatchBSwap(BinaryOperator &I);
00263 
00264     Value *EvaluateInDifferentType(Value *V, const Type *Ty);
00265   };
00266 
00267   RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
00268 }
00269 
00270 // getComplexity:  Assign a complexity or rank value to LLVM Values...
00271 //   0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
00272 static unsigned getComplexity(Value *V) {
00273   if (isa<Instruction>(V)) {
00274     if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
00275       return 3;
00276     return 4;
00277   }
00278   if (isa<Argument>(V)) return 3;
00279   return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
00280 }
00281 
00282 // isOnlyUse - Return true if this instruction will be deleted if we stop using
00283 // it.
00284 static bool isOnlyUse(Value *V) {
00285   return V->hasOneUse() || isa<Constant>(V);
00286 }
00287 
00288 // getPromotedType - Return the specified type promoted as it would be to pass
00289 // though a va_arg area...
00290 static const Type *getPromotedType(const Type *Ty) {
00291   switch (Ty->getTypeID()) {
00292   case Type::SByteTyID:
00293   case Type::ShortTyID:  return Type::IntTy;
00294   case Type::UByteTyID:
00295   case Type::UShortTyID: return Type::UIntTy;
00296   case Type::FloatTyID:  return Type::DoubleTy;
00297   default:               return Ty;
00298   }
00299 }
00300 
00301 /// isCast - If the specified operand is a CastInst or a constant expr cast,
00302 /// return the operand value, otherwise return null.
00303 static Value *isCast(Value *V) {
00304   if (CastInst *I = dyn_cast<CastInst>(V))
00305     return I->getOperand(0);
00306   else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00307     if (CE->getOpcode() == Instruction::Cast)
00308       return CE->getOperand(0);
00309   return 0;
00310 }
00311 
00312 enum CastType {
00313   Noop     = 0,
00314   Truncate = 1,
00315   Signext  = 2,
00316   Zeroext  = 3
00317 };
00318 
00319 /// getCastType - In the future, we will split the cast instruction into these
00320 /// various types.  Until then, we have to do the analysis here.
00321 static CastType getCastType(const Type *Src, const Type *Dest) {
00322   assert(Src->isIntegral() && Dest->isIntegral() &&
00323          "Only works on integral types!");
00324   unsigned SrcSize = Src->getPrimitiveSizeInBits();
00325   unsigned DestSize = Dest->getPrimitiveSizeInBits();
00326   
00327   if (SrcSize == DestSize) return Noop;
00328   if (SrcSize > DestSize)  return Truncate;
00329   if (Src->isSigned()) return Signext;
00330   return Zeroext;
00331 }
00332 
00333 
00334 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
00335 // instruction.
00336 //
00337 static bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy,
00338                                    const Type *DstTy, TargetData *TD) {
00339   
00340   // It is legal to eliminate the instruction if casting A->B->A if the sizes
00341   // are identical and the bits don't get reinterpreted (for example
00342   // int->float->int would not be allowed).
00343   if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy))
00344     return true;
00345   
00346   // If we are casting between pointer and integer types, treat pointers as
00347   // integers of the appropriate size for the code below.
00348   if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType();
00349   if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType();
00350   if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType();
00351   
00352   // Allow free casting and conversion of sizes as long as the sign doesn't
00353   // change...
00354   if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
00355     CastType FirstCast = getCastType(SrcTy, MidTy);
00356     CastType SecondCast = getCastType(MidTy, DstTy);
00357     
00358     // Capture the effect of these two casts.  If the result is a legal cast,
00359     // the CastType is stored here, otherwise a special code is used.
00360     static const unsigned CastResult[] = {
00361       // First cast is noop
00362       0, 1, 2, 3,
00363       // First cast is a truncate
00364       1, 1, 4, 4,         // trunc->extend is not safe to eliminate
00365                           // First cast is a sign ext
00366       2, 5, 2, 4,         // signext->zeroext never ok
00367                           // First cast is a zero ext
00368       3, 5, 3, 3,
00369     };
00370     
00371     unsigned Result = CastResult[FirstCast*4+SecondCast];
00372     switch (Result) {
00373     default: assert(0 && "Illegal table value!");
00374     case 0:
00375     case 1:
00376     case 2:
00377     case 3:
00378       // FIXME: in the future, when LLVM has explicit sign/zeroextends and
00379       // truncates, we could eliminate more casts.
00380       return (unsigned)getCastType(SrcTy, DstTy) == Result;
00381     case 4:
00382       return false;  // Not possible to eliminate this here.
00383     case 5:
00384       // Sign or zero extend followed by truncate is always ok if the result
00385       // is a truncate or noop.
00386       CastType ResultCast = getCastType(SrcTy, DstTy);
00387       if (ResultCast == Noop || ResultCast == Truncate)
00388         return true;
00389         // Otherwise we are still growing the value, we are only safe if the
00390         // result will match the sign/zeroextendness of the result.
00391         return ResultCast == FirstCast;
00392     }
00393   }
00394   
00395   // If this is a cast from 'float -> double -> integer', cast from
00396   // 'float -> integer' directly, as the value isn't changed by the 
00397   // float->double conversion.
00398   if (SrcTy->isFloatingPoint() && MidTy->isFloatingPoint() &&
00399       DstTy->isIntegral() && 
00400       SrcTy->getPrimitiveSize() < MidTy->getPrimitiveSize())
00401     return true;
00402   
00403   // Packed type conversions don't modify bits.
00404   if (isa<PackedType>(SrcTy) && isa<PackedType>(MidTy) &&isa<PackedType>(DstTy))
00405     return true;
00406   
00407   return false;
00408 }
00409 
00410 /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
00411 /// in any code being generated.  It does not require codegen if V is simple
00412 /// enough or if the cast can be folded into other casts.
00413 static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) {
00414   if (V->getType() == Ty || isa<Constant>(V)) return false;
00415   
00416   // If this is a noop cast, it isn't real codegen.
00417   if (V->getType()->isLosslesslyConvertibleTo(Ty))
00418     return false;
00419 
00420   // If this is another cast that can be eliminated, it isn't codegen either.
00421   if (const CastInst *CI = dyn_cast<CastInst>(V))
00422     if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty,
00423                                TD))
00424       return false;
00425   return true;
00426 }
00427 
00428 /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
00429 /// InsertBefore instruction.  This is specialized a bit to avoid inserting
00430 /// casts that are known to not do anything...
00431 ///
00432 Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy,
00433                                              Instruction *InsertBefore) {
00434   if (V->getType() == DestTy) return V;
00435   if (Constant *C = dyn_cast<Constant>(V))
00436     return ConstantExpr::getCast(C, DestTy);
00437   
00438   CastInst *CI = new CastInst(V, DestTy, V->getName());
00439   InsertNewInstBefore(CI, *InsertBefore);
00440   return CI;
00441 }
00442 
00443 // SimplifyCommutative - This performs a few simplifications for commutative
00444 // operators:
00445 //
00446 //  1. Order operands such that they are listed from right (least complex) to
00447 //     left (most complex).  This puts constants before unary operators before
00448 //     binary operators.
00449 //
00450 //  2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
00451 //  3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
00452 //
00453 bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
00454   bool Changed = false;
00455   if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
00456     Changed = !I.swapOperands();
00457 
00458   if (!I.isAssociative()) return Changed;
00459   Instruction::BinaryOps Opcode = I.getOpcode();
00460   if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
00461     if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
00462       if (isa<Constant>(I.getOperand(1))) {
00463         Constant *Folded = ConstantExpr::get(I.getOpcode(),
00464                                              cast<Constant>(I.getOperand(1)),
00465                                              cast<Constant>(Op->getOperand(1)));
00466         I.setOperand(0, Op->getOperand(0));
00467         I.setOperand(1, Folded);
00468         return true;
00469       } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
00470         if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
00471             isOnlyUse(Op) && isOnlyUse(Op1)) {
00472           Constant *C1 = cast<Constant>(Op->getOperand(1));
00473           Constant *C2 = cast<Constant>(Op1->getOperand(1));
00474 
00475           // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
00476           Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
00477           Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
00478                                                     Op1->getOperand(0),
00479                                                     Op1->getName(), &I);
00480           WorkList.push_back(New);
00481           I.setOperand(0, New);
00482           I.setOperand(1, Folded);
00483           return true;
00484         }
00485     }
00486   return Changed;
00487 }
00488 
00489 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
00490 // if the LHS is a constant zero (which is the 'negate' form).
00491 //
00492 static inline Value *dyn_castNegVal(Value *V) {
00493   if (BinaryOperator::isNeg(V))
00494     return BinaryOperator::getNegArgument(V);
00495 
00496   // Constants can be considered to be negated values if they can be folded.
00497   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
00498     return ConstantExpr::getNeg(C);
00499   return 0;
00500 }
00501 
00502 static inline Value *dyn_castNotVal(Value *V) {
00503   if (BinaryOperator::isNot(V))
00504     return BinaryOperator::getNotArgument(V);
00505 
00506   // Constants can be considered to be not'ed values...
00507   if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V))
00508     return ConstantExpr::getNot(C);
00509   return 0;
00510 }
00511 
00512 // dyn_castFoldableMul - If this value is a multiply that can be folded into
00513 // other computations (because it has a constant operand), return the
00514 // non-constant operand of the multiply, and set CST to point to the multiplier.
00515 // Otherwise, return null.
00516 //
00517 static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
00518   if (V->hasOneUse() && V->getType()->isInteger())
00519     if (Instruction *I = dyn_cast<Instruction>(V)) {
00520       if (I->getOpcode() == Instruction::Mul)
00521         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
00522           return I->getOperand(0);
00523       if (I->getOpcode() == Instruction::Shl)
00524         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
00525           // The multiplier is really 1 << CST.
00526           Constant *One = ConstantInt::get(V->getType(), 1);
00527           CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST));
00528           return I->getOperand(0);
00529         }
00530     }
00531   return 0;
00532 }
00533 
00534 /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant
00535 /// expression, return it.
00536 static User *dyn_castGetElementPtr(Value *V) {
00537   if (isa<GetElementPtrInst>(V)) return cast<User>(V);
00538   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00539     if (CE->getOpcode() == Instruction::GetElementPtr)
00540       return cast<User>(V);
00541   return false;
00542 }
00543 
00544 // AddOne, SubOne - Add or subtract a constant one from an integer constant...
00545 static ConstantInt *AddOne(ConstantInt *C) {
00546   return cast<ConstantInt>(ConstantExpr::getAdd(C,
00547                                          ConstantInt::get(C->getType(), 1)));
00548 }
00549 static ConstantInt *SubOne(ConstantInt *C) {
00550   return cast<ConstantInt>(ConstantExpr::getSub(C,
00551                                          ConstantInt::get(C->getType(), 1)));
00552 }
00553 
00554 /// GetConstantInType - Return a ConstantInt with the specified type and value.
00555 ///
00556 static ConstantIntegral *GetConstantInType(const Type *Ty, uint64_t Val) {
00557   if (Ty->isUnsigned())
00558     return ConstantUInt::get(Ty, Val);
00559   else if (Ty->getTypeID() == Type::BoolTyID)
00560     return ConstantBool::get(Val);
00561   int64_t SVal = Val;
00562   SVal <<= 64-Ty->getPrimitiveSizeInBits();
00563   SVal >>= 64-Ty->getPrimitiveSizeInBits();
00564   return ConstantSInt::get(Ty, SVal);
00565 }
00566 
00567 
00568 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
00569 /// known to be either zero or one and return them in the KnownZero/KnownOne
00570 /// bitsets.  This code only analyzes bits in Mask, in order to short-circuit
00571 /// processing.
00572 static void ComputeMaskedBits(Value *V, uint64_t Mask, uint64_t &KnownZero,
00573                               uint64_t &KnownOne, unsigned Depth = 0) {
00574   // Note, we cannot consider 'undef' to be "IsZero" here.  The problem is that
00575   // we cannot optimize based on the assumption that it is zero without changing
00576   // it to be an explicit zero.  If we don't change it to zero, other code could
00577   // optimized based on the contradictory assumption that it is non-zero.
00578   // Because instcombine aggressively folds operations with undef args anyway,
00579   // this won't lose us code quality.
00580   if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) {
00581     // We know all of the bits for a constant!
00582     KnownOne = CI->getZExtValue() & Mask;
00583     KnownZero = ~KnownOne & Mask;
00584     return;
00585   }
00586 
00587   KnownZero = KnownOne = 0;   // Don't know anything.
00588   if (Depth == 6 || Mask == 0)
00589     return;  // Limit search depth.
00590 
00591   uint64_t KnownZero2, KnownOne2;
00592   Instruction *I = dyn_cast<Instruction>(V);
00593   if (!I) return;
00594 
00595   Mask &= V->getType()->getIntegralTypeMask();
00596   
00597   switch (I->getOpcode()) {
00598   case Instruction::And:
00599     // If either the LHS or the RHS are Zero, the result is zero.
00600     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
00601     Mask &= ~KnownZero;
00602     ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
00603     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00604     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00605     
00606     // Output known-1 bits are only known if set in both the LHS & RHS.
00607     KnownOne &= KnownOne2;
00608     // Output known-0 are known to be clear if zero in either the LHS | RHS.
00609     KnownZero |= KnownZero2;
00610     return;
00611   case Instruction::Or:
00612     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
00613     Mask &= ~KnownOne;
00614     ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
00615     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00616     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00617     
00618     // Output known-0 bits are only known if clear in both the LHS & RHS.
00619     KnownZero &= KnownZero2;
00620     // Output known-1 are known to be set if set in either the LHS | RHS.
00621     KnownOne |= KnownOne2;
00622     return;
00623   case Instruction::Xor: {
00624     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
00625     ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
00626     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00627     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00628     
00629     // Output known-0 bits are known if clear or set in both the LHS & RHS.
00630     uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
00631     // Output known-1 are known to be set if set in only one of the LHS, RHS.
00632     KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
00633     KnownZero = KnownZeroOut;
00634     return;
00635   }
00636   case Instruction::Select:
00637     ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
00638     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
00639     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00640     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00641 
00642     // Only known if known in both the LHS and RHS.
00643     KnownOne &= KnownOne2;
00644     KnownZero &= KnownZero2;
00645     return;
00646   case Instruction::Cast: {
00647     const Type *SrcTy = I->getOperand(0)->getType();
00648     if (!SrcTy->isIntegral()) return;
00649     
00650     // If this is an integer truncate or noop, just look in the input.
00651     if (SrcTy->getPrimitiveSizeInBits() >= 
00652            I->getType()->getPrimitiveSizeInBits()) {
00653       ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
00654       return;
00655     }
00656 
00657     // Sign or Zero extension.  Compute the bits in the result that are not
00658     // present in the input.
00659     uint64_t NotIn = ~SrcTy->getIntegralTypeMask();
00660     uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn;
00661       
00662     // Handle zero extension.
00663     if (!SrcTy->isSigned()) {
00664       Mask &= SrcTy->getIntegralTypeMask();
00665       ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
00666       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00667       // The top bits are known to be zero.
00668       KnownZero |= NewBits;
00669     } else {
00670       // Sign extension.
00671       Mask &= SrcTy->getIntegralTypeMask();
00672       ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
00673       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00674 
00675       // If the sign bit of the input is known set or clear, then we know the
00676       // top bits of the result.
00677       uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1);
00678       if (KnownZero & InSignBit) {          // Input sign bit known zero
00679         KnownZero |= NewBits;
00680         KnownOne &= ~NewBits;
00681       } else if (KnownOne & InSignBit) {    // Input sign bit known set
00682         KnownOne |= NewBits;
00683         KnownZero &= ~NewBits;
00684       } else {                              // Input sign bit unknown
00685         KnownZero &= ~NewBits;
00686         KnownOne &= ~NewBits;
00687       }
00688     }
00689     return;
00690   }
00691   case Instruction::Shl:
00692     // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0
00693     if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) {
00694       Mask >>= SA->getValue();
00695       ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
00696       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00697       KnownZero <<= SA->getValue();
00698       KnownOne  <<= SA->getValue();
00699       KnownZero |= (1ULL << SA->getValue())-1;  // low bits known zero.
00700       return;
00701     }
00702     break;
00703   case Instruction::Shr:
00704     // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
00705     if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) {
00706       // Compute the new bits that are at the top now.
00707       uint64_t HighBits = (1ULL << SA->getValue())-1;
00708       HighBits <<= I->getType()->getPrimitiveSizeInBits()-SA->getValue();
00709       
00710       if (I->getType()->isUnsigned()) {   // Unsigned shift right.
00711         Mask <<= SA->getValue();
00712         ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1);
00713         assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 
00714         KnownZero >>= SA->getValue();
00715         KnownOne  >>= SA->getValue();
00716         KnownZero |= HighBits;  // high bits known zero.
00717       } else {
00718         Mask <<= SA->getValue();
00719         ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1);
00720         assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 
00721         KnownZero >>= SA->getValue();
00722         KnownOne  >>= SA->getValue();
00723         
00724         // Handle the sign bits.
00725         uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1);
00726         SignBit >>= SA->getValue();  // Adjust to where it is now in the mask.
00727         
00728         if (KnownZero & SignBit) {       // New bits are known zero.
00729           KnownZero |= HighBits;
00730         } else if (KnownOne & SignBit) { // New bits are known one.
00731           KnownOne |= HighBits;
00732         }
00733       }
00734       return;
00735     }
00736     break;
00737   }
00738 }
00739 
00740 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
00741 /// this predicate to simplify operations downstream.  Mask is known to be zero
00742 /// for bits that V cannot have.
00743 static bool MaskedValueIsZero(Value *V, uint64_t Mask, unsigned Depth = 0) {
00744   uint64_t KnownZero, KnownOne;
00745   ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth);
00746   assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00747   return (KnownZero & Mask) == Mask;
00748 }
00749 
00750 /// ShrinkDemandedConstant - Check to see if the specified operand of the 
00751 /// specified instruction is a constant integer.  If so, check to see if there
00752 /// are any bits set in the constant that are not demanded.  If so, shrink the
00753 /// constant and return true.
00754 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, 
00755                                    uint64_t Demanded) {
00756   ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
00757   if (!OpC) return false;
00758 
00759   // If there are no bits set that aren't demanded, nothing to do.
00760   if ((~Demanded & OpC->getZExtValue()) == 0)
00761     return false;
00762 
00763   // This is producing any bits that are not needed, shrink the RHS.
00764   uint64_t Val = Demanded & OpC->getZExtValue();
00765   I->setOperand(OpNo, GetConstantInType(OpC->getType(), Val));
00766   return true;
00767 }
00768 
00769 // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a 
00770 // set of known zero and one bits, compute the maximum and minimum values that
00771 // could have the specified known zero and known one bits, returning them in
00772 // min/max.
00773 static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty,
00774                                                    uint64_t KnownZero,
00775                                                    uint64_t KnownOne,
00776                                                    int64_t &Min, int64_t &Max) {
00777   uint64_t TypeBits = Ty->getIntegralTypeMask();
00778   uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits;
00779 
00780   uint64_t SignBit = 1ULL << (Ty->getPrimitiveSizeInBits()-1);
00781   
00782   // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
00783   // bit if it is unknown.
00784   Min = KnownOne;
00785   Max = KnownOne|UnknownBits;
00786   
00787   if (SignBit & UnknownBits) { // Sign bit is unknown
00788     Min |= SignBit;
00789     Max &= ~SignBit;
00790   }
00791   
00792   // Sign extend the min/max values.
00793   int ShAmt = 64-Ty->getPrimitiveSizeInBits();
00794   Min = (Min << ShAmt) >> ShAmt;
00795   Max = (Max << ShAmt) >> ShAmt;
00796 }
00797 
00798 // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
00799 // a set of known zero and one bits, compute the maximum and minimum values that
00800 // could have the specified known zero and known one bits, returning them in
00801 // min/max.
00802 static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty,
00803                                                      uint64_t KnownZero,
00804                                                      uint64_t KnownOne,
00805                                                      uint64_t &Min,
00806                                                      uint64_t &Max) {
00807   uint64_t TypeBits = Ty->getIntegralTypeMask();
00808   uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits;
00809   
00810   // The minimum value is when the unknown bits are all zeros.
00811   Min = KnownOne;
00812   // The maximum value is when the unknown bits are all ones.
00813   Max = KnownOne|UnknownBits;
00814 }
00815 
00816 
00817 /// SimplifyDemandedBits - Look at V.  At this point, we know that only the
00818 /// DemandedMask bits of the result of V are ever used downstream.  If we can
00819 /// use this information to simplify V, do so and return true.  Otherwise,
00820 /// analyze the expression and return a mask of KnownOne and KnownZero bits for
00821 /// the expression (used to simplify the caller).  The KnownZero/One bits may
00822 /// only be accurate for those bits in the DemandedMask.
00823 bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask,
00824                                         uint64_t &KnownZero, uint64_t &KnownOne,
00825                                         unsigned Depth) {
00826   if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) {
00827     // We know all of the bits for a constant!
00828     KnownOne = CI->getZExtValue() & DemandedMask;
00829     KnownZero = ~KnownOne & DemandedMask;
00830     return false;
00831   }
00832   
00833   KnownZero = KnownOne = 0;
00834   if (!V->hasOneUse()) {    // Other users may use these bits.
00835     if (Depth != 0) {       // Not at the root.
00836       // Just compute the KnownZero/KnownOne bits to simplify things downstream.
00837       ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth);
00838       return false;
00839     }
00840     // If this is the root being simplified, allow it to have multiple uses,
00841     // just set the DemandedMask to all bits.
00842     DemandedMask = V->getType()->getIntegralTypeMask();
00843   } else if (DemandedMask == 0) {   // Not demanding any bits from V.
00844     if (V != UndefValue::get(V->getType()))
00845       return UpdateValueUsesWith(V, UndefValue::get(V->getType()));
00846     return false;
00847   } else if (Depth == 6) {        // Limit search depth.
00848     return false;
00849   }
00850   
00851   Instruction *I = dyn_cast<Instruction>(V);
00852   if (!I) return false;        // Only analyze instructions.
00853 
00854   DemandedMask &= V->getType()->getIntegralTypeMask();
00855   
00856   uint64_t KnownZero2, KnownOne2;
00857   switch (I->getOpcode()) {
00858   default: break;
00859   case Instruction::And:
00860     // If either the LHS or the RHS are Zero, the result is zero.
00861     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
00862                              KnownZero, KnownOne, Depth+1))
00863       return true;
00864     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00865 
00866     // If something is known zero on the RHS, the bits aren't demanded on the
00867     // LHS.
00868     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownZero,
00869                              KnownZero2, KnownOne2, Depth+1))
00870       return true;
00871     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00872 
00873     // If all of the demanded bits are known one on one side, return the other.
00874     // These bits cannot contribute to the result of the 'and'.
00875     if ((DemandedMask & ~KnownZero2 & KnownOne) == (DemandedMask & ~KnownZero2))
00876       return UpdateValueUsesWith(I, I->getOperand(0));
00877     if ((DemandedMask & ~KnownZero & KnownOne2) == (DemandedMask & ~KnownZero))
00878       return UpdateValueUsesWith(I, I->getOperand(1));
00879     
00880     // If all of the demanded bits in the inputs are known zeros, return zero.
00881     if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
00882       return UpdateValueUsesWith(I, Constant::getNullValue(I->getType()));
00883       
00884     // If the RHS is a constant, see if we can simplify it.
00885     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~KnownZero2))
00886       return UpdateValueUsesWith(I, I);
00887       
00888     // Output known-1 bits are only known if set in both the LHS & RHS.
00889     KnownOne &= KnownOne2;
00890     // Output known-0 are known to be clear if zero in either the LHS | RHS.
00891     KnownZero |= KnownZero2;
00892     break;
00893   case Instruction::Or:
00894     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, 
00895                              KnownZero, KnownOne, Depth+1))
00896       return true;
00897     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00898     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownOne, 
00899                              KnownZero2, KnownOne2, Depth+1))
00900       return true;
00901     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00902     
00903     // If all of the demanded bits are known zero on one side, return the other.
00904     // These bits cannot contribute to the result of the 'or'.
00905     if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
00906       return UpdateValueUsesWith(I, I->getOperand(0));
00907     if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
00908       return UpdateValueUsesWith(I, I->getOperand(1));
00909 
00910     // If all of the potentially set bits on one side are known to be set on
00911     // the other side, just use the 'other' side.
00912     if ((DemandedMask & (~KnownZero) & KnownOne2) == 
00913         (DemandedMask & (~KnownZero)))
00914       return UpdateValueUsesWith(I, I->getOperand(0));
00915     if ((DemandedMask & (~KnownZero2) & KnownOne) == 
00916         (DemandedMask & (~KnownZero2)))
00917       return UpdateValueUsesWith(I, I->getOperand(1));
00918         
00919     // If the RHS is a constant, see if we can simplify it.
00920     if (ShrinkDemandedConstant(I, 1, DemandedMask))
00921       return UpdateValueUsesWith(I, I);
00922           
00923     // Output known-0 bits are only known if clear in both the LHS & RHS.
00924     KnownZero &= KnownZero2;
00925     // Output known-1 are known to be set if set in either the LHS | RHS.
00926     KnownOne |= KnownOne2;
00927     break;
00928   case Instruction::Xor: {
00929     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
00930                              KnownZero, KnownOne, Depth+1))
00931       return true;
00932     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00933     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, 
00934                              KnownZero2, KnownOne2, Depth+1))
00935       return true;
00936     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00937     
00938     // If all of the demanded bits are known zero on one side, return the other.
00939     // These bits cannot contribute to the result of the 'xor'.
00940     if ((DemandedMask & KnownZero) == DemandedMask)
00941       return UpdateValueUsesWith(I, I->getOperand(0));
00942     if ((DemandedMask & KnownZero2) == DemandedMask)
00943       return UpdateValueUsesWith(I, I->getOperand(1));
00944     
00945     // Output known-0 bits are known if clear or set in both the LHS & RHS.
00946     uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
00947     // Output known-1 are known to be set if set in only one of the LHS, RHS.
00948     uint64_t KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
00949     
00950     // If all of the unknown bits are known to be zero on one side or the other
00951     // (but not both) turn this into an *inclusive* or.
00952     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
00953     if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut)) {
00954       if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits) {
00955         Instruction *Or =
00956           BinaryOperator::createOr(I->getOperand(0), I->getOperand(1),
00957                                    I->getName());
00958         InsertNewInstBefore(Or, *I);
00959         return UpdateValueUsesWith(I, Or);
00960       }
00961     }
00962     
00963     // If all of the demanded bits on one side are known, and all of the set
00964     // bits on that side are also known to be set on the other side, turn this
00965     // into an AND, as we know the bits will be cleared.
00966     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
00967     if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
00968       if ((KnownOne & KnownOne2) == KnownOne) {
00969         Constant *AndC = GetConstantInType(I->getType(), 
00970                                            ~KnownOne & DemandedMask);
00971         Instruction *And = 
00972           BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp");
00973         InsertNewInstBefore(And, *I);
00974         return UpdateValueUsesWith(I, And);
00975       }
00976     }
00977     
00978     // If the RHS is a constant, see if we can simplify it.
00979     // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
00980     if (ShrinkDemandedConstant(I, 1, DemandedMask))
00981       return UpdateValueUsesWith(I, I);
00982     
00983     KnownZero = KnownZeroOut;
00984     KnownOne  = KnownOneOut;
00985     break;
00986   }
00987   case Instruction::Select:
00988     if (SimplifyDemandedBits(I->getOperand(2), DemandedMask,
00989                              KnownZero, KnownOne, Depth+1))
00990       return true;
00991     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, 
00992                              KnownZero2, KnownOne2, Depth+1))
00993       return true;
00994     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00995     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00996     
00997     // If the operands are constants, see if we can simplify them.
00998     if (ShrinkDemandedConstant(I, 1, DemandedMask))
00999       return UpdateValueUsesWith(I, I);
01000     if (ShrinkDemandedConstant(I, 2, DemandedMask))
01001       return UpdateValueUsesWith(I, I);
01002     
01003     // Only known if known in both the LHS and RHS.
01004     KnownOne &= KnownOne2;
01005     KnownZero &= KnownZero2;
01006     break;
01007   case Instruction::Cast: {
01008     const Type *SrcTy = I->getOperand(0)->getType();
01009     if (!SrcTy->isIntegral()) return false;
01010     
01011     // If this is an integer truncate or noop, just look in the input.
01012     if (SrcTy->getPrimitiveSizeInBits() >= 
01013         I->getType()->getPrimitiveSizeInBits()) {
01014       if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
01015                                KnownZero, KnownOne, Depth+1))
01016         return true;
01017       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
01018       break;
01019     }
01020     
01021     // Sign or Zero extension.  Compute the bits in the result that are not
01022     // present in the input.
01023     uint64_t NotIn = ~SrcTy->getIntegralTypeMask();
01024     uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn;
01025     
01026     // Handle zero extension.
01027     if (!SrcTy->isSigned()) {
01028       DemandedMask &= SrcTy->getIntegralTypeMask();
01029       if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
01030                                KnownZero, KnownOne, Depth+1))
01031         return true;
01032       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
01033       // The top bits are known to be zero.
01034       KnownZero |= NewBits;
01035     } else {
01036       // Sign extension.
01037       uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1);
01038       int64_t InputDemandedBits = DemandedMask & SrcTy->getIntegralTypeMask();
01039 
01040       // If any of the sign extended bits are demanded, we know that the sign
01041       // bit is demanded.
01042       if (NewBits & DemandedMask)
01043         InputDemandedBits |= InSignBit;
01044       
01045       if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits,
01046                                KnownZero, KnownOne, Depth+1))
01047         return true;
01048       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
01049       
01050       // If the sign bit of the input is known set or clear, then we know the
01051       // top bits of the result.
01052 
01053       // If the input sign bit is known zero, or if the NewBits are not demanded
01054       // convert this into a zero extension.
01055       if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) {
01056         // Convert to unsigned first.
01057         Instruction *NewVal;
01058         NewVal = new CastInst(I->getOperand(0), SrcTy->getUnsignedVersion(),
01059                               I->getOperand(0)->getName());
01060         InsertNewInstBefore(NewVal, *I);
01061         // Then cast that to the destination type.
01062         NewVal = new CastInst(NewVal, I->getType(), I->getName());
01063         InsertNewInstBefore(NewVal, *I);
01064         return UpdateValueUsesWith(I, NewVal);
01065       } else if (KnownOne & InSignBit) {    // Input sign bit known set
01066         KnownOne |= NewBits;
01067         KnownZero &= ~NewBits;
01068       } else {                              // Input sign bit unknown
01069         KnownZero &= ~NewBits;
01070         KnownOne &= ~NewBits;
01071       }
01072     }
01073     break;
01074   }
01075   case Instruction::Shl:
01076     if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) {
01077       if (SimplifyDemandedBits(I->getOperand(0), DemandedMask >> SA->getValue(), 
01078                                KnownZero, KnownOne, Depth+1))
01079         return true;
01080       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
01081       KnownZero <<= SA->getValue();
01082       KnownOne  <<= SA->getValue();
01083       KnownZero |= (1ULL << SA->getValue())-1;  // low bits known zero.
01084     }
01085     break;
01086   case Instruction::Shr:
01087     if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) {
01088       unsigned ShAmt = SA->getValue();
01089       
01090       // Compute the new bits that are at the top now.
01091       uint64_t HighBits = (1ULL << ShAmt)-1;
01092       HighBits <<= I->getType()->getPrimitiveSizeInBits() - ShAmt;
01093       uint64_t TypeMask = I->getType()->getIntegralTypeMask();
01094       if (I->getType()->isUnsigned()) {   // Unsigned shift right.
01095         if (SimplifyDemandedBits(I->getOperand(0),
01096                                  (DemandedMask << ShAmt) & TypeMask,
01097                                  KnownZero, KnownOne, Depth+1))
01098           return true;
01099         assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
01100         KnownZero &= TypeMask;
01101         KnownOne  &= TypeMask;
01102         KnownZero >>= ShAmt;
01103         KnownOne  >>= ShAmt;
01104         KnownZero |= HighBits;  // high bits known zero.
01105       } else {                            // Signed shift right.
01106         if (SimplifyDemandedBits(I->getOperand(0),
01107                                  (DemandedMask << ShAmt) & TypeMask,
01108                                  KnownZero, KnownOne, Depth+1))
01109           return true;
01110         assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
01111         KnownZero &= TypeMask;
01112         KnownOne  &= TypeMask;
01113         KnownZero >>= SA->getValue();
01114         KnownOne  >>= SA->getValue();
01115         
01116         // Handle the sign bits.
01117         uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1);
01118         SignBit >>= SA->getValue();  // Adjust to where it is now in the mask.
01119         
01120         // If the input sign bit is known to be zero, or if none of the top bits
01121         // are demanded, turn this into an unsigned shift right.
01122         if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
01123           // Convert the input to unsigned.
01124           Instruction *NewVal;
01125           NewVal = new CastInst(I->getOperand(0), 
01126                                 I->getType()->getUnsignedVersion(),
01127                                 I->getOperand(0)->getName());
01128           InsertNewInstBefore(NewVal, *I);
01129           // Perform the unsigned shift right.
01130           NewVal = new ShiftInst(Instruction::Shr, NewVal, SA, I->getName());
01131           InsertNewInstBefore(NewVal, *I);
01132           // Then cast that to the destination type.
01133           NewVal = new CastInst(NewVal, I->getType(), I->getName());
01134           InsertNewInstBefore(NewVal, *I);
01135           return UpdateValueUsesWith(I, NewVal);
01136         } else if (KnownOne & SignBit) { // New bits are known one.
01137           KnownOne |= HighBits;
01138         }
01139       }
01140     }
01141     break;
01142   }
01143   
01144   // If the client is only demanding bits that we know, return the known
01145   // constant.
01146   if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
01147     return UpdateValueUsesWith(I, GetConstantInType(I->getType(), KnownOne));
01148   return false;
01149 }  
01150 
01151 // isTrueWhenEqual - Return true if the specified setcondinst instruction is
01152 // true when both operands are equal...
01153 //
01154 static bool isTrueWhenEqual(Instruction &I) {
01155   return I.getOpcode() == Instruction::SetEQ ||
01156          I.getOpcode() == Instruction::SetGE ||
01157          I.getOpcode() == Instruction::SetLE;
01158 }
01159 
01160 /// AssociativeOpt - Perform an optimization on an associative operator.  This
01161 /// function is designed to check a chain of associative operators for a
01162 /// potential to apply a certain optimization.  Since the optimization may be
01163 /// applicable if the expression was reassociated, this checks the chain, then
01164 /// reassociates the expression as necessary to expose the optimization
01165 /// opportunity.  This makes use of a special Functor, which must define
01166 /// 'shouldApply' and 'apply' methods.
01167 ///
01168 template<typename Functor>
01169 Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
01170   unsigned Opcode = Root.getOpcode();
01171   Value *LHS = Root.getOperand(0);
01172 
01173   // Quick check, see if the immediate LHS matches...
01174   if (F.shouldApply(LHS))
01175     return F.apply(Root);
01176 
01177   // Otherwise, if the LHS is not of the same opcode as the root, return.
01178   Instruction *LHSI = dyn_cast<Instruction>(LHS);
01179   while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) {
01180     // Should we apply this transform to the RHS?
01181     bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
01182 
01183     // If not to the RHS, check to see if we should apply to the LHS...
01184     if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
01185       cast<BinaryOperator>(LHSI)->swapOperands();   // Make the LHS the RHS
01186       ShouldApply = true;
01187     }
01188 
01189     // If the functor wants to apply the optimization to the RHS of LHSI,
01190     // reassociate the expression from ((? op A) op B) to (? op (A op B))
01191     if (ShouldApply) {
01192       BasicBlock *BB = Root.getParent();
01193 
01194       // Now all of the instructions are in the current basic block, go ahead
01195       // and perform the reassociation.
01196       Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
01197 
01198       // First move the selected RHS to the LHS of the root...
01199       Root.setOperand(0, LHSI->getOperand(1));
01200 
01201       // Make what used to be the LHS of the root be the user of the root...
01202       Value *ExtraOperand = TmpLHSI->getOperand(1);
01203       if (&Root == TmpLHSI) {
01204         Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
01205         return 0;
01206       }
01207       Root.replaceAllUsesWith(TmpLHSI);          // Users now use TmpLHSI
01208       TmpLHSI->setOperand(1, &Root);             // TmpLHSI now uses the root
01209       TmpLHSI->getParent()->getInstList().remove(TmpLHSI);
01210       BasicBlock::iterator ARI = &Root; ++ARI;
01211       BB->getInstList().insert(ARI, TmpLHSI);    // Move TmpLHSI to after Root
01212       ARI = Root;
01213 
01214       // Now propagate the ExtraOperand down the chain of instructions until we
01215       // get to LHSI.
01216       while (TmpLHSI != LHSI) {
01217         Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
01218         // Move the instruction to immediately before the chain we are
01219         // constructing to avoid breaking dominance properties.
01220         NextLHSI->getParent()->getInstList().remove(NextLHSI);
01221         BB->getInstList().insert(ARI, NextLHSI);
01222         ARI = NextLHSI;
01223 
01224         Value *NextOp = NextLHSI->getOperand(1);
01225         NextLHSI->setOperand(1, ExtraOperand);
01226         TmpLHSI = NextLHSI;
01227         ExtraOperand = NextOp;
01228       }
01229 
01230       // Now that the instructions are reassociated, have the functor perform
01231       // the transformation...
01232       return F.apply(Root);
01233     }
01234 
01235     LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
01236   }
01237   return 0;
01238 }
01239 
01240 
01241 // AddRHS - Implements: X + X --> X << 1
01242 struct AddRHS {
01243   Value *RHS;
01244   AddRHS(Value *rhs) : RHS(rhs) {}
01245   bool shouldApply(Value *LHS) const { return LHS == RHS; }
01246   Instruction *apply(BinaryOperator &Add) const {
01247     return new ShiftInst(Instruction::Shl, Add.getOperand(0),
01248                          ConstantInt::get(Type::UByteTy, 1));
01249   }
01250 };
01251 
01252 // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
01253 //                 iff C1&C2 == 0
01254 struct AddMaskingAnd {
01255   Constant *C2;
01256   AddMaskingAnd(Constant *c) : C2(c) {}
01257   bool shouldApply(Value *LHS) const {
01258     ConstantInt *C1;
01259     return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
01260            ConstantExpr::getAnd(C1, C2)->isNullValue();
01261   }
01262   Instruction *apply(BinaryOperator &Add) const {
01263     return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1));
01264   }
01265 };
01266 
01267 static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
01268                                              InstCombiner *IC) {
01269   if (isa<CastInst>(I)) {
01270     if (Constant *SOC = dyn_cast<Constant>(SO))
01271       return ConstantExpr::getCast(SOC, I.getType());
01272 
01273     return IC->InsertNewInstBefore(new CastInst(SO, I.getType(),
01274                                                 SO->getName() + ".cast"), I);
01275   }
01276 
01277   // Figure out if the constant is the left or the right argument.
01278   bool ConstIsRHS = isa<Constant>(I.getOperand(1));
01279   Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
01280 
01281   if (Constant *SOC = dyn_cast<Constant>(SO)) {
01282     if (ConstIsRHS)
01283       return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
01284     return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
01285   }
01286 
01287   Value *Op0 = SO, *Op1 = ConstOperand;
01288   if (!ConstIsRHS)
01289     std::swap(Op0, Op1);
01290   Instruction *New;
01291   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
01292     New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
01293   else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I))
01294     New = new ShiftInst(SI->getOpcode(), Op0, Op1, SO->getName()+".sh");
01295   else {
01296     assert(0 && "Unknown binary instruction type!");
01297     abort();
01298   }
01299   return IC->InsertNewInstBefore(New, I);
01300 }
01301 
01302 // FoldOpIntoSelect - Given an instruction with a select as one operand and a
01303 // constant as the other operand, try to fold the binary operator into the
01304 // select arguments.  This also works for Cast instructions, which obviously do
01305 // not have a second operand.
01306 static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
01307                                      InstCombiner *IC) {
01308   // Don't modify shared select instructions
01309   if (!SI->hasOneUse()) return 0;
01310   Value *TV = SI->getOperand(1);
01311   Value *FV = SI->getOperand(2);
01312 
01313   if (isa<Constant>(TV) || isa<Constant>(FV)) {
01314     // Bool selects with constant operands can be folded to logical ops.
01315     if (SI->getType() == Type::BoolTy) return 0;
01316 
01317     Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC);
01318     Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC);
01319 
01320     return new SelectInst(SI->getCondition(), SelectTrueVal,
01321                           SelectFalseVal);
01322   }
01323   return 0;
01324 }
01325 
01326 
01327 /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI
01328 /// node as operand #0, see if we can fold the instruction into the PHI (which
01329 /// is only possible if all operands to the PHI are constants).
01330 Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
01331   PHINode *PN = cast<PHINode>(I.getOperand(0));
01332   unsigned NumPHIValues = PN->getNumIncomingValues();
01333   if (!PN->hasOneUse() || NumPHIValues == 0 ||
01334       !isa<Constant>(PN->getIncomingValue(0))) return 0;
01335 
01336   // Check to see if all of the operands of the PHI are constants.  If not, we
01337   // cannot do the transformation.
01338   for (unsigned i = 1; i != NumPHIValues; ++i)
01339     if (!isa<Constant>(PN->getIncomingValue(i)))
01340       return 0;
01341 
01342   // Okay, we can do the transformation: create the new PHI node.
01343   PHINode *NewPN = new PHINode(I.getType(), I.getName());
01344   I.setName("");
01345   NewPN->reserveOperandSpace(PN->getNumOperands()/2);
01346   InsertNewInstBefore(NewPN, *PN);
01347 
01348   // Next, add all of the operands to the PHI.
01349   if (I.getNumOperands() == 2) {
01350     Constant *C = cast<Constant>(I.getOperand(1));
01351     for (unsigned i = 0; i != NumPHIValues; ++i) {
01352       Constant *InV = cast<Constant>(PN->getIncomingValue(i));
01353       NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C),
01354                          PN->getIncomingBlock(i));
01355     }
01356   } else {
01357     assert(isa<CastInst>(I) && "Unary op should be a cast!");
01358     const Type *RetTy = I.getType();
01359     for (unsigned i = 0; i != NumPHIValues; ++i) {
01360       Constant *InV = cast<Constant>(PN->getIncomingValue(i));
01361       NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy),
01362                          PN->getIncomingBlock(i));
01363     }
01364   }
01365   return ReplaceInstUsesWith(I, NewPN);
01366 }
01367 
01368 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
01369   bool Changed = SimplifyCommutative(I);
01370   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
01371 
01372   if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
01373     // X + undef -> undef
01374     if (isa<UndefValue>(RHS))
01375       return ReplaceInstUsesWith(I, RHS);
01376 
01377     // X + 0 --> X
01378     if (!I.getType()->isFloatingPoint()) { // NOTE: -0 + +0 = +0.
01379       if (RHSC->isNullValue())
01380         return ReplaceInstUsesWith(I, LHS);
01381     } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
01382       if (CFP->isExactlyValue(-0.0))
01383         return ReplaceInstUsesWith(I, LHS);
01384     }
01385 
01386     // X + (signbit) --> X ^ signbit
01387     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
01388       uint64_t Val = CI->getZExtValue();
01389       if (Val == (1ULL << (CI->getType()->getPrimitiveSizeInBits()-1)))
01390         return BinaryOperator::createXor(LHS, RHS);
01391     }
01392 
01393     if (isa<PHINode>(LHS))
01394       if (Instruction *NV = FoldOpIntoPhi(I))
01395         return NV;
01396     
01397     ConstantInt *XorRHS = 0;
01398     Value *XorLHS = 0;
01399     if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
01400       unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits();
01401       int64_t  RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue();
01402       uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue();
01403       
01404       uint64_t C0080Val = 1ULL << 31;
01405       int64_t CFF80Val = -C0080Val;
01406       unsigned Size = 32;
01407       do {
01408         if (TySizeBits > Size) {
01409           bool Found = false;
01410           // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
01411           // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
01412           if (RHSSExt == CFF80Val) {
01413             if (XorRHS->getZExtValue() == C0080Val)
01414               Found = true;
01415           } else if (RHSZExt == C0080Val) {
01416             if (XorRHS->getSExtValue() == CFF80Val)
01417               Found = true;
01418           }
01419           if (Found) {
01420             // This is a sign extend if the top bits are known zero.
01421             uint64_t Mask = ~0ULL;
01422             Mask <<= 64-(TySizeBits-Size);
01423             Mask &= XorLHS->getType()->getIntegralTypeMask();
01424             if (!MaskedValueIsZero(XorLHS, Mask))
01425               Size = 0;  // Not a sign ext, but can't be any others either.
01426             goto FoundSExt;
01427           }
01428         }
01429         Size >>= 1;
01430         C0080Val >>= Size;
01431         CFF80Val >>= Size;
01432       } while (Size >= 8);
01433       
01434 FoundSExt:
01435       const Type *MiddleType = 0;
01436       switch (Size) {
01437       default: break;
01438       case 32: MiddleType = Type::IntTy; break;
01439       case 16: MiddleType = Type::ShortTy; break;
01440       case 8:  MiddleType = Type::SByteTy; break;
01441       }
01442       if (MiddleType) {
01443         Instruction *NewTrunc = new CastInst(XorLHS, MiddleType, "sext");
01444         InsertNewInstBefore(NewTrunc, I);
01445         return new CastInst(NewTrunc, I.getType());
01446       }
01447     }
01448   }
01449 
01450   // X + X --> X << 1
01451   if (I.getType()->isInteger()) {
01452     if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;
01453 
01454     if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
01455       if (RHSI->getOpcode() == Instruction::Sub)
01456         if (LHS == RHSI->getOperand(1))                   // A + (B - A) --> B
01457           return ReplaceInstUsesWith(I, RHSI->getOperand(0));
01458     }
01459     if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
01460       if (LHSI->getOpcode() == Instruction::Sub)
01461         if (RHS == LHSI->getOperand(1))                   // (B - A) + A --> B
01462           return ReplaceInstUsesWith(I, LHSI->getOperand(0));
01463     }
01464   }
01465 
01466   // -A + B  -->  B - A
01467   if (Value *V = dyn_castNegVal(LHS))
01468     return BinaryOperator::createSub(RHS, V);
01469 
01470   // A + -B  -->  A - B
01471   if (!isa<Constant>(RHS))
01472     if (Value *V = dyn_castNegVal(RHS))
01473       return BinaryOperator::createSub(LHS, V);
01474 
01475 
01476   ConstantInt *C2;
01477   if (Value *X = dyn_castFoldableMul(LHS, C2)) {
01478     if (X == RHS)   // X*C + X --> X * (C+1)
01479       return BinaryOperator::createMul(RHS, AddOne(C2));
01480 
01481     // X*C1 + X*C2 --> X * (C1+C2)
01482     ConstantInt *C1;
01483     if (X == dyn_castFoldableMul(RHS, C1))
01484       return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2));
01485   }
01486 
01487   // X + X*C --> X * (C+1)
01488   if (dyn_castFoldableMul(RHS, C2) == LHS)
01489     return BinaryOperator::createMul(LHS, AddOne(C2));
01490 
01491 
01492   // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
01493   if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
01494     if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R;
01495 
01496   if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
01497     Value *X = 0;
01498     if (match(LHS, m_Not(m_Value(X)))) {   // ~X + C --> (C-1) - X
01499       Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1));
01500       return BinaryOperator::createSub(C, X);
01501     }
01502 
01503     // (X & FF00) + xx00  -> (X+xx00) & FF00
01504     if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
01505       Constant *Anded = ConstantExpr::getAnd(CRHS, C2);
01506       if (Anded == CRHS) {
01507         // See if all bits from the first bit set in the Add RHS up are included
01508         // in the mask.  First, get the rightmost bit.
01509         uint64_t AddRHSV = CRHS->getRawValue();
01510 
01511         // Form a mask of all bits from the lowest bit added through the top.
01512         uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1);
01513         AddRHSHighBits &= C2->getType()->getIntegralTypeMask();
01514 
01515         // See if the and mask includes all of these bits.
01516         uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getRawValue();
01517 
01518         if (AddRHSHighBits == AddRHSHighBitsAnd) {
01519           // Okay, the xform is safe.  Insert the new add pronto.
01520           Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS,
01521                                                             LHS->getName()), I);
01522           return BinaryOperator::createAnd(NewAdd, C2);
01523         }
01524       }
01525     }
01526 
01527     // Try to fold constant add into select arguments.
01528     if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
01529       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
01530         return R;
01531   }
01532 
01533   return Changed ? &I : 0;
01534 }
01535 
01536 // isSignBit - Return true if the value represented by the constant only has the
01537 // highest order bit set.
01538 static bool isSignBit(ConstantInt *CI) {
01539   unsigned NumBits = CI->getType()->getPrimitiveSizeInBits();
01540   return (CI->getRawValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1));
01541 }
01542 
01543 /// RemoveNoopCast - Strip off nonconverting casts from the value.
01544 ///
01545 static Value *RemoveNoopCast(Value *V) {
01546   if (CastInst *CI = dyn_cast<CastInst>(V)) {
01547     const Type *CTy = CI->getType();
01548     const Type *OpTy = CI->getOperand(0)->getType();
01549     if (CTy->isInteger() && OpTy->isInteger()) {
01550       if (CTy->getPrimitiveSizeInBits() == OpTy->getPrimitiveSizeInBits())
01551         return RemoveNoopCast(CI->getOperand(0));
01552     } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy))
01553       return RemoveNoopCast(CI->getOperand(0));
01554   }
01555   return V;
01556 }
01557 
01558 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
01559   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01560 
01561   if (Op0 == Op1)         // sub X, X  -> 0
01562     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
01563 
01564   // If this is a 'B = x-(-A)', change to B = x+A...
01565   if (Value *V = dyn_castNegVal(Op1))
01566     return BinaryOperator::createAdd(Op0, V);
01567 
01568   if (isa<UndefValue>(Op0))
01569     return ReplaceInstUsesWith(I, Op0);    // undef - X -> undef
01570   if (isa<UndefValue>(Op1))
01571     return ReplaceInstUsesWith(I, Op1);    // X - undef -> undef
01572 
01573   if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
01574     // Replace (-1 - A) with (~A)...
01575     if (C->isAllOnesValue())
01576       return BinaryOperator::createNot(Op1);
01577 
01578     // C - ~X == X + (1+C)
01579     Value *X = 0;
01580     if (match(Op1, m_Not(m_Value(X))))
01581       return BinaryOperator::createAdd(X,
01582                     ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1)));
01583     // -((uint)X >> 31) -> ((int)X >> 31)
01584     // -((int)X >> 31) -> ((uint)X >> 31)
01585     if (C->isNullValue()) {
01586       Value *NoopCastedRHS = RemoveNoopCast(Op1);
01587       if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS))
01588         if (SI->getOpcode() == Instruction::Shr)
01589           if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) {
01590             const Type *NewTy;
01591             if (SI->getType()->isSigned())
01592               NewTy = SI->getType()->getUnsignedVersion();
01593             else
01594               NewTy = SI->getType()->getSignedVersion();
01595             // Check to see if we are shifting out everything but the sign bit.
01596             if (CU->getValue() == SI->getType()->getPrimitiveSizeInBits()-1) {
01597               // Ok, the transformation is safe.  Insert a cast of the incoming
01598               // value, then the new shift, then the new cast.
01599               Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy,
01600                                                  SI->getOperand(0)->getName());
01601               Value *InV = InsertNewInstBefore(FirstCast, I);
01602               Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast,
01603                                                     CU, SI->getName());
01604               if (NewShift->getType() == I.getType())
01605                 return NewShift;
01606               else {
01607                 InV = InsertNewInstBefore(NewShift, I);
01608                 return new CastInst(NewShift, I.getType());
01609               }
01610             }
01611           }
01612     }
01613 
01614     // Try to fold constant sub into select arguments.
01615     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
01616       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
01617         return R;
01618 
01619     if (isa<PHINode>(Op0))
01620       if (Instruction *NV = FoldOpIntoPhi(I))
01621         return NV;
01622   }
01623 
01624   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
01625     if (Op1I->getOpcode() == Instruction::Add &&
01626         !Op0->getType()->isFloatingPoint()) {
01627       if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y
01628         return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName());
01629       else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y
01630         return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName());
01631       else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
01632         if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
01633           // C1-(X+C2) --> (C1-C2)-X
01634           return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2),
01635                                            Op1I->getOperand(0));
01636       }
01637     }
01638 
01639     if (Op1I->hasOneUse()) {
01640       // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
01641       // is not used by anyone else...
01642       //
01643       if (Op1I->getOpcode() == Instruction::Sub &&
01644           !Op1I->getType()->isFloatingPoint()) {
01645         // Swap the two operands of the subexpr...
01646         Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
01647         Op1I->setOperand(0, IIOp1);
01648         Op1I->setOperand(1, IIOp0);
01649 
01650         // Create the new top level add instruction...
01651         return BinaryOperator::createAdd(Op0, Op1);
01652       }
01653 
01654       // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
01655       //
01656       if (Op1I->getOpcode() == Instruction::And &&
01657           (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
01658         Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
01659 
01660         Value *NewNot =
01661           InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I);
01662         return BinaryOperator::createAnd(Op0, NewNot);
01663       }
01664 
01665       // -(X sdiv C)  -> (X sdiv -C)
01666       if (Op1I->getOpcode() == Instruction::Div)
01667         if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
01668           if (CSI->isNullValue())
01669             if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1)))
01670               return BinaryOperator::createDiv(Op1I->getOperand(0),
01671                                                ConstantExpr::getNeg(DivRHS));
01672 
01673       // X - X*C --> X * (1-C)
01674       ConstantInt *C2 = 0;
01675       if (dyn_castFoldableMul(Op1I, C2) == Op0) {
01676         Constant *CP1 =
01677           ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2);
01678         return BinaryOperator::createMul(Op0, CP1);
01679       }
01680     }
01681   }
01682 
01683   if (!Op0->getType()->isFloatingPoint())
01684     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
01685       if (Op0I->getOpcode() == Instruction::Add) {
01686         if (Op0I->getOperand(0) == Op1)             // (Y+X)-Y == X
01687           return ReplaceInstUsesWith(I, Op0I->getOperand(1));
01688         else if (Op0I->getOperand(1) == Op1)        // (X+Y)-Y == X
01689           return ReplaceInstUsesWith(I, Op0I->getOperand(0));
01690       } else if (Op0I->getOpcode() == Instruction::Sub) {
01691         if (Op0I->getOperand(0) == Op1)             // (X-Y)-X == -Y
01692           return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName());
01693       }
01694 
01695   ConstantInt *C1;
01696   if (Value *X = dyn_castFoldableMul(Op0, C1)) {
01697     if (X == Op1) { // X*C - X --> X * (C-1)
01698       Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1));
01699       return BinaryOperator::createMul(Op1, CP1);
01700     }
01701 
01702     ConstantInt *C2;   // X*C1 - X*C2 -> X * (C1-C2)
01703     if (X == dyn_castFoldableMul(Op1, C2))
01704       return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2));
01705   }
01706   return 0;
01707 }
01708 
01709 /// isSignBitCheck - Given an exploded setcc instruction, return true if it is
01710 /// really just returns true if the most significant (sign) bit is set.
01711 static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) {
01712   if (RHS->getType()->isSigned()) {
01713     // True if source is LHS < 0 or LHS <= -1
01714     return Opcode == Instruction::SetLT && RHS->isNullValue() ||
01715            Opcode == Instruction::SetLE && RHS->isAllOnesValue();
01716   } else {
01717     ConstantUInt *RHSC = cast<ConstantUInt>(RHS);
01718     // True if source is LHS > 127 or LHS >= 128, where the constants depend on
01719     // the size of the integer type.
01720     if (Opcode == Instruction::SetGE)
01721       return RHSC->getValue() ==
01722         1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1);
01723     if (Opcode == Instruction::SetGT)
01724       return RHSC->getValue() ==
01725         (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1;
01726   }
01727   return false;
01728 }
01729 
01730 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
01731   bool Changed = SimplifyCommutative(I);
01732   Value *Op0 = I.getOperand(0);
01733 
01734   if (isa<UndefValue>(I.getOperand(1)))              // undef * X -> 0
01735     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
01736 
01737   // Simplify mul instructions with a constant RHS...
01738   if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
01739     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
01740 
01741       // ((X << C1)*C2) == (X * (C2 << C1))
01742       if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0))
01743         if (SI->getOpcode() == Instruction::Shl)
01744           if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
01745             return BinaryOperator::createMul(SI->getOperand(0),
01746                                              ConstantExpr::getShl(CI, ShOp));
01747 
01748       if (CI->isNullValue())
01749         return ReplaceInstUsesWith(I, Op1);  // X * 0  == 0
01750       if (CI->equalsInt(1))                  // X * 1  == X
01751         return ReplaceInstUsesWith(I, Op0);
01752       if (CI->isAllOnesValue())              // X * -1 == 0 - X
01753         return BinaryOperator::createNeg(Op0, I.getName());
01754 
01755       int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue();
01756       if (isPowerOf2_64(Val)) {          // Replace X*(2^C) with X << C
01757         uint64_t C = Log2_64(Val);
01758         return new ShiftInst(Instruction::Shl, Op0,
01759                              ConstantUInt::get(Type::UByteTy, C));
01760       }
01761     } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
01762       if (Op1F->isNullValue())
01763         return ReplaceInstUsesWith(I, Op1);
01764 
01765       // "In IEEE floating point, x*1 is not equivalent to x for nans.  However,
01766       // ANSI says we can drop signals, so we can do this anyway." (from GCC)
01767       if (Op1F->getValue() == 1.0)
01768         return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul double %X, 1.0'
01769     }
01770     
01771     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
01772       if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() &&
01773           isa<ConstantInt>(Op0I->getOperand(1))) {
01774         // Canonicalize (X+C1)*C2 -> X*C2+C1*C2.
01775         Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0),
01776                                                      Op1, "tmp");
01777         InsertNewInstBefore(Add, I);
01778         Value *C1C2 = ConstantExpr::getMul(Op1, 
01779                                            cast<Constant>(Op0I->getOperand(1)));
01780         return BinaryOperator::createAdd(Add, C1C2);
01781         
01782       }
01783 
01784     // Try to fold constant mul into select arguments.
01785     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
01786       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
01787         return R;
01788 
01789     if (isa<PHINode>(Op0))
01790       if (Instruction *NV = FoldOpIntoPhi(I))
01791         return NV;
01792   }
01793 
01794   if (Value *Op0v = dyn_castNegVal(Op0))     // -X * -Y = X*Y
01795     if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
01796       return BinaryOperator::createMul(Op0v, Op1v);
01797 
01798   // If one of the operands of the multiply is a cast from a boolean value, then
01799   // we know the bool is either zero or one, so this is a 'masking' multiply.
01800   // See if we can simplify things based on how the boolean was originally
01801   // formed.
01802   CastInst *BoolCast = 0;
01803   if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0)))
01804     if (CI->getOperand(0)->getType() == Type::BoolTy)
01805       BoolCast = CI;
01806   if (!BoolCast)
01807     if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1)))
01808       if (CI->getOperand(0)->getType() == Type::BoolTy)
01809         BoolCast = CI;
01810   if (BoolCast) {
01811     if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) {
01812       Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1);
01813       const Type *SCOpTy = SCIOp0->getType();
01814 
01815       // If the setcc is true iff the sign bit of X is set, then convert this
01816       // multiply into a shift/and combination.
01817       if (isa<ConstantInt>(SCIOp1) &&
01818           isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) {
01819         // Shift the X value right to turn it into "all signbits".
01820         Constant *Amt = ConstantUInt::get(Type::UByteTy,
01821                                           SCOpTy->getPrimitiveSizeInBits()-1);
01822         if (SCIOp0->getType()->isUnsigned()) {
01823           const Type *NewTy = SCIOp0->getType()->getSignedVersion();
01824           SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy,
01825                                                     SCIOp0->getName()), I);
01826         }
01827 
01828         Value *V =
01829           InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt,
01830                                             BoolCast->getOperand(0)->getName()+
01831                                             ".mask"), I);
01832 
01833         // If the multiply type is not the same as the source type, sign extend
01834         // or truncate to the multiply type.
01835         if (I.getType() != V->getType())
01836           V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I);
01837 
01838         Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
01839         return BinaryOperator::createAnd(V, OtherOp);
01840       }
01841     }
01842   }
01843 
01844   return Changed ? &I : 0;
01845 }
01846 
01847 Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
01848   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01849 
01850   if (isa<UndefValue>(Op0))              // undef / X -> 0
01851     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
01852   if (isa<UndefValue>(Op1))
01853     return ReplaceInstUsesWith(I, Op1);  // X / undef -> undef
01854 
01855   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
01856     // div X, 1 == X
01857     if (RHS->equalsInt(1))
01858       return ReplaceInstUsesWith(I, Op0);
01859 
01860     // div X, -1 == -X
01861     if (RHS->isAllOnesValue())
01862       return BinaryOperator::createNeg(Op0);
01863 
01864     if (Instruction *LHS = dyn_cast<Instruction>(Op0))
01865       if (LHS->getOpcode() == Instruction::Div)
01866         if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
01867           // (X / C1) / C2  -> X / (C1*C2)
01868           return BinaryOperator::createDiv(LHS->getOperand(0),
01869                                            ConstantExpr::getMul(RHS, LHSRHS));
01870         }
01871 
01872     // Check to see if this is an unsigned division with an exact power of 2,
01873     // if so, convert to a right shift.
01874     if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
01875       if (uint64_t Val = C->getValue())    // Don't break X / 0
01876         if (isPowerOf2_64(Val)) {
01877           uint64_t C = Log2_64(Val);
01878           return new ShiftInst(Instruction::Shr, Op0,
01879                                ConstantUInt::get(Type::UByteTy, C));
01880         }
01881 
01882     // -X/C -> X/-C
01883     if (RHS->getType()->isSigned())
01884       if (Value *LHSNeg = dyn_castNegVal(Op0))
01885         return BinaryOperator::createDiv(LHSNeg, ConstantExpr::getNeg(RHS));
01886 
01887     if (!RHS->isNullValue()) {
01888       if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
01889         if (Instruction *R = FoldOpIntoSelect(I, SI, this))
01890           return R;
01891       if (isa<PHINode>(Op0))
01892         if (Instruction *NV = FoldOpIntoPhi(I))
01893           return NV;
01894     }
01895   }
01896 
01897   // If this is 'udiv X, (Cond ? C1, C2)' where C1&C2 are powers of two,
01898   // transform this into: '(Cond ? (udiv X, C1) : (udiv X, C2))'.
01899   if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
01900     if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1)))
01901       if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) {
01902         if (STO->getValue() == 0) { // Couldn't be this argument.
01903           I.setOperand(1, SFO);
01904           return &I;
01905         } else if (SFO->getValue() == 0) {
01906           I.setOperand(1, STO);
01907           return &I;
01908         }
01909 
01910         uint64_t TVA = STO->getValue(), FVA = SFO->getValue();
01911         if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) {
01912           unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA);
01913           Constant *TC = ConstantUInt::get(Type::UByteTy, TSA);
01914           Instruction *TSI = new ShiftInst(Instruction::Shr, Op0,
01915                                            TC, SI->getName()+".t");
01916           TSI = InsertNewInstBefore(TSI, I);
01917 
01918           Constant *FC = ConstantUInt::get(Type::UByteTy, FSA);
01919           Instruction *FSI = new ShiftInst(Instruction::Shr, Op0,
01920                                            FC, SI->getName()+".f");
01921           FSI = InsertNewInstBefore(FSI, I);
01922           return new SelectInst(SI->getOperand(0), TSI, FSI);
01923         }
01924       }
01925 
01926   // 0 / X == 0, we don't need to preserve faults!
01927   if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0))
01928     if (LHS->equalsInt(0))
01929       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
01930 
01931   if (I.getType()->isSigned()) {
01932     // If the sign bits of both operands are zero (i.e. we can prove they are
01933     // unsigned inputs), turn this into a udiv.
01934     uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1);
01935     if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
01936       const Type *NTy = Op0->getType()->getUnsignedVersion();
01937       Instruction *LHS = new CastInst(Op0, NTy, Op0->getName());
01938       InsertNewInstBefore(LHS, I);
01939       Value *RHS;
01940       if (Constant *R = dyn_cast<Constant>(Op1))
01941         RHS = ConstantExpr::getCast(R, NTy);
01942       else
01943         RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I);
01944       Instruction *Div = BinaryOperator::createDiv(LHS, RHS, I.getName());
01945       InsertNewInstBefore(Div, I);
01946       return new CastInst(Div, I.getType());
01947     }      
01948   } else {
01949     // Known to be an unsigned division.
01950     if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
01951       // Turn A / (C1 << N), where C1 is "1<<C2" into A >> (N+C2) [udiv only].
01952       if (RHSI->getOpcode() == Instruction::Shl &&
01953           isa<ConstantUInt>(RHSI->getOperand(0))) {
01954         unsigned C1 = cast<ConstantUInt>(RHSI->getOperand(0))->getRawValue();
01955         if (isPowerOf2_64(C1)) {
01956           unsigned C2 = Log2_64(C1);
01957           Value *Add = RHSI->getOperand(1);
01958           if (C2) {
01959             Constant *C2V = ConstantUInt::get(Add->getType(), C2);
01960             Add = InsertNewInstBefore(BinaryOperator::createAdd(Add, C2V,
01961                                                                 "tmp"), I);
01962           }
01963           return new ShiftInst(Instruction::Shr, Op0, Add);
01964         }
01965       }
01966     }
01967   }
01968   
01969   return 0;
01970 }
01971 
01972 
01973 /// GetFactor - If we can prove that the specified value is at least a multiple
01974 /// of some factor, return that factor.
01975 static Constant *GetFactor(Value *V) {
01976   if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
01977     return CI;
01978   
01979   // Unless we can be tricky, we know this is a multiple of 1.
01980   Constant *Result = ConstantInt::get(V->getType(), 1);
01981   
01982   Instruction *I = dyn_cast<Instruction>(V);
01983   if (!I) return Result;
01984   
01985   if (I->getOpcode() == Instruction::Mul) {
01986     // Handle multiplies by a constant, etc.
01987     return ConstantExpr::getMul(GetFactor(I->getOperand(0)),
01988                                 GetFactor(I->getOperand(1)));
01989   } else if (I->getOpcode() == Instruction::Shl) {
01990     // (X<<C) -> X * (1 << C)
01991     if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) {
01992       ShRHS = ConstantExpr::getShl(Result, ShRHS);
01993       return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS);
01994     }
01995   } else if (I->getOpcode() == Instruction::And) {
01996     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
01997       // X & 0xFFF0 is known to be a multiple of 16.
01998       unsigned Zeros = CountTrailingZeros_64(RHS->getZExtValue());
01999       if (Zeros != V->getType()->getPrimitiveSizeInBits())
02000         return ConstantExpr::getShl(Result, 
02001                                     ConstantUInt::get(Type::UByteTy, Zeros));
02002     }
02003   } else if (I->getOpcode() == Instruction::Cast) {
02004     Value *Op = I->getOperand(0);
02005     // Only handle int->int casts.
02006     if (!Op->getType()->isInteger()) return Result;
02007     return ConstantExpr::getCast(GetFactor(Op), V->getType());
02008   }    
02009   return Result;
02010 }
02011 
02012 Instruction *InstCombiner::visitRem(BinaryOperator &I) {
02013   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02014   
02015   // 0 % X == 0, we don't need to preserve faults!
02016   if (Constant *LHS = dyn_cast<Constant>(Op0))
02017     if (LHS->isNullValue())
02018       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02019 
02020   if (isa<UndefValue>(Op0))              // undef % X -> 0
02021     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02022   if (isa<UndefValue>(Op1))
02023     return ReplaceInstUsesWith(I, Op1);  // X % undef -> undef
02024   
02025   if (I.getType()->isSigned()) {
02026     if (Value *RHSNeg = dyn_castNegVal(Op1))
02027       if (!isa<ConstantSInt>(RHSNeg) ||
02028           cast<ConstantSInt>(RHSNeg)->getValue() > 0) {
02029         // X % -Y -> X % Y
02030         AddUsesToWorkList(I);
02031         I.setOperand(1, RHSNeg);
02032         return &I;
02033       }
02034    
02035     // If the top bits of both operands are zero (i.e. we can prove they are
02036     // unsigned inputs), turn this into a urem.
02037     uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1);
02038     if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
02039       const Type *NTy = Op0->getType()->getUnsignedVersion();
02040       Instruction *LHS = new CastInst(Op0, NTy, Op0->getName());
02041       InsertNewInstBefore(LHS, I);
02042       Value *RHS;
02043       if (Constant *R = dyn_cast<Constant>(Op1))
02044         RHS = ConstantExpr::getCast(R, NTy);
02045       else
02046         RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I);
02047       Instruction *Rem = BinaryOperator::createRem(LHS, RHS, I.getName());
02048       InsertNewInstBefore(Rem, I);
02049       return new CastInst(Rem, I.getType());
02050     }
02051   }
02052 
02053   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
02054     // X % 0 == undef, we don't need to preserve faults!
02055     if (RHS->equalsInt(0))
02056       return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
02057     
02058     if (RHS->equalsInt(1))  // X % 1 == 0
02059       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02060 
02061     // Check to see if this is an unsigned remainder with an exact power of 2,
02062     // if so, convert to a bitwise and.
02063     if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
02064       if (isPowerOf2_64(C->getValue()))
02065         return BinaryOperator::createAnd(Op0, SubOne(C));
02066 
02067     if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
02068       if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
02069         if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02070           return R;
02071       } else if (isa<PHINode>(Op0I)) {
02072         if (Instruction *NV = FoldOpIntoPhi(I))
02073           return NV;
02074       }
02075       
02076       // X*C1%C2 --> 0  iff  C1%C2 == 0
02077       if (ConstantExpr::getRem(GetFactor(Op0I), RHS)->isNullValue())
02078         return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02079     }
02080   }
02081 
02082   if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
02083     // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) [urem only].
02084     if (I.getType()->isUnsigned() && 
02085         RHSI->getOpcode() == Instruction::Shl &&
02086         isa<ConstantUInt>(RHSI->getOperand(0))) {
02087       unsigned C1 = cast<ConstantUInt>(RHSI->getOperand(0))->getRawValue();
02088       if (isPowerOf2_64(C1)) {
02089         Constant *N1 = ConstantInt::getAllOnesValue(I.getType());
02090         Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1,
02091                                                                    "tmp"), I);
02092         return BinaryOperator::createAnd(Op0, Add);
02093       }
02094     }
02095     
02096     // If this is 'urem X, (Cond ? C1, C2)' where C1&C2 are powers of two,
02097     // transform this into: '(Cond ? (urem X, C1) : (urem X, C2))'.
02098     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
02099       if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1)))
02100         if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) {
02101           if (STO->getValue() == 0) { // Couldn't be this argument.
02102             I.setOperand(1, SFO);
02103             return &I;
02104           } else if (SFO->getValue() == 0) {
02105             I.setOperand(1, STO);
02106             return &I;
02107           }
02108           
02109           if (isPowerOf2_64(STO->getValue()) && isPowerOf2_64(SFO->getValue())){
02110             Value *TrueAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0,
02111                                           SubOne(STO), SI->getName()+".t"), I);
02112             Value *FalseAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0,
02113                                           SubOne(SFO), SI->getName()+".f"), I);
02114             return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd);
02115           }
02116         }
02117   }
02118   
02119   return 0;
02120 }
02121 
02122 // isMaxValueMinusOne - return true if this is Max-1
02123 static bool isMaxValueMinusOne(const ConstantInt *C) {
02124   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
02125     return CU->getValue() == C->getType()->getIntegralTypeMask()-1;
02126 
02127   const ConstantSInt *CS = cast<ConstantSInt>(C);
02128 
02129   // Calculate 0111111111..11111
02130   unsigned TypeBits = C->getType()->getPrimitiveSizeInBits();
02131   int64_t Val = INT64_MAX;             // All ones
02132   Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
02133   return CS->getValue() == Val-1;
02134 }
02135 
02136 // isMinValuePlusOne - return true if this is Min+1
02137 static bool isMinValuePlusOne(const ConstantInt *C) {
02138   if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
02139     return CU->getValue() == 1;
02140 
02141   const ConstantSInt *CS = cast<ConstantSInt>(C);
02142 
02143   // Calculate 1111111111000000000000
02144   unsigned TypeBits = C->getType()->getPrimitiveSizeInBits();
02145   int64_t Val = -1;                    // All ones
02146   Val <<= TypeBits-1;                  // Shift over to the right spot
02147   return CS->getValue() == Val+1;
02148 }
02149 
02150 // isOneBitSet - Return true if there is exactly one bit set in the specified
02151 // constant.
02152 static bool isOneBitSet(const ConstantInt *CI) {
02153   uint64_t V = CI->getRawValue();
02154   return V && (V & (V-1)) == 0;
02155 }
02156 
02157 #if 0   // Currently unused
02158 // isLowOnes - Return true if the constant is of the form 0+1+.
02159 static bool isLowOnes(const ConstantInt *CI) {
02160   uint64_t V = CI->getRawValue();
02161 
02162   // There won't be bits set in parts that the type doesn't contain.
02163   V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue();
02164 
02165   uint64_t U = V+1;  // If it is low ones, this should be a power of two.
02166   return U && V && (U & V) == 0;
02167 }
02168 #endif
02169 
02170 // isHighOnes - Return true if the constant is of the form 1+0+.
02171 // This is the same as lowones(~X).
02172 static bool isHighOnes(const ConstantInt *CI) {
02173   uint64_t V = ~CI->getRawValue();
02174   if (~V == 0) return false;  // 0's does not match "1+"
02175 
02176   // There won't be bits set in parts that the type doesn't contain.
02177   V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue();
02178 
02179   uint64_t U = V+1;  // If it is low ones, this should be a power of two.
02180   return U && V && (U & V) == 0;
02181 }
02182 
02183 
02184 /// getSetCondCode - Encode a setcc opcode into a three bit mask.  These bits
02185 /// are carefully arranged to allow folding of expressions such as:
02186 ///
02187 ///      (A < B) | (A > B) --> (A != B)
02188 ///
02189 /// Bit value '4' represents that the comparison is true if A > B, bit value '2'
02190 /// represents that the comparison is true if A == B, and bit value '1' is true
02191 /// if A < B.
02192 ///
02193 static unsigned getSetCondCode(const SetCondInst *SCI) {
02194   switch (SCI->getOpcode()) {
02195     // False -> 0
02196   case Instruction::SetGT: return 1;
02197   case Instruction::SetEQ: return 2;
02198   case Instruction::SetGE: return 3;
02199   case Instruction::SetLT: return 4;
02200   case Instruction::SetNE: return 5;
02201   case Instruction::SetLE: return 6;
02202     // True -> 7
02203   default:
02204     assert(0 && "Invalid SetCC opcode!");
02205     return 0;
02206   }
02207 }
02208 
02209 /// getSetCCValue - This is the complement of getSetCondCode, which turns an
02210 /// opcode and two operands into either a constant true or false, or a brand new
02211 /// SetCC instruction.
02212 static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) {
02213   switch (Opcode) {
02214   case 0: return ConstantBool::False;
02215   case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS);
02216   case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS);
02217   case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS);
02218   case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS);
02219   case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS);
02220   case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS);
02221   case 7: return ConstantBool::True;
02222   default: assert(0 && "Illegal SetCCCode!"); return 0;
02223   }
02224 }
02225 
02226 // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B)
02227 struct FoldSetCCLogical {
02228   InstCombiner &IC;
02229   Value *LHS, *RHS;
02230   FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI)
02231     : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {}
02232   bool shouldApply(Value *V) const {
02233     if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
02234       return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS ||
02235               SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS);
02236     return false;
02237   }
02238   Instruction *apply(BinaryOperator &Log) const {
02239     SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0));
02240     if (SCI->getOperand(0) != LHS) {
02241       assert(SCI->getOperand(1) == LHS);
02242       SCI->swapOperands();  // Swap the LHS and RHS of the SetCC
02243     }
02244 
02245     unsigned LHSCode = getSetCondCode(SCI);
02246     unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1)));
02247     unsigned Code;
02248     switch (Log.getOpcode()) {
02249     case Instruction::And: Code = LHSCode & RHSCode; break;
02250     case Instruction::Or:  Code = LHSCode | RHSCode; break;
02251     case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
02252     default: assert(0 && "Illegal logical opcode!"); return 0;
02253     }
02254 
02255     Value *RV = getSetCCValue(Code, LHS, RHS);
02256     if (Instruction *I = dyn_cast<Instruction>(RV))
02257       return I;
02258     // Otherwise, it's a constant boolean value...
02259     return IC.ReplaceInstUsesWith(Log, RV);
02260   }
02261 };
02262 
02263 // OptAndOp - This handles expressions of the form ((val OP C1) & C2).  Where
02264 // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is
02265 // guaranteed to be either a shift instruction or a binary operator.
02266 Instruction *InstCombiner::OptAndOp(Instruction *Op,
02267                                     ConstantIntegral *OpRHS,
02268                                     ConstantIntegral *AndRHS,
02269                                     BinaryOperator &TheAnd) {
02270   Value *X = Op->getOperand(0);
02271   Constant *Together = 0;
02272   if (!isa<ShiftInst>(Op))
02273     Together = ConstantExpr::getAnd(AndRHS, OpRHS);
02274 
02275   switch (Op->getOpcode()) {
02276   case Instruction::Xor:
02277     if (Op->hasOneUse()) {
02278       // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
02279       std::string OpName = Op->getName(); Op->setName("");
02280       Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName);
02281       InsertNewInstBefore(And, TheAnd);
02282       return BinaryOperator::createXor(And, Together);
02283     }
02284     break;
02285   case Instruction::Or:
02286     if (Together == AndRHS) // (X | C) & C --> C
02287       return ReplaceInstUsesWith(TheAnd, AndRHS);
02288 
02289     if (Op->hasOneUse() && Together != OpRHS) {
02290       // (X | C1) & C2 --> (X | (C1&C2)) & C2
02291       std::string Op0Name = Op->getName(); Op->setName("");
02292       Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name);
02293       InsertNewInstBefore(Or, TheAnd);
02294       return BinaryOperator::createAnd(Or, AndRHS);
02295     }
02296     break;
02297   case Instruction::Add:
02298     if (Op->hasOneUse()) {
02299       // Adding a one to a single bit bit-field should be turned into an XOR
02300       // of the bit.  First thing to check is to see if this AND is with a
02301       // single bit constant.
02302       uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue();
02303 
02304       // Clear bits that are not part of the constant.
02305       AndRHSV &= AndRHS->getType()->getIntegralTypeMask();
02306 
02307       // If there is only one bit set...
02308       if (isOneBitSet(cast<ConstantInt>(AndRHS))) {
02309         // Ok, at this point, we know that we are masking the result of the
02310         // ADD down to exactly one bit.  If the constant we are adding has
02311         // no bits set below this bit, then we can eliminate the ADD.
02312         uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue();
02313 
02314         // Check to see if any bits below the one bit set in AndRHSV are set.
02315         if ((AddRHS & (AndRHSV-1)) == 0) {
02316           // If not, the only thing that can effect the output of the AND is
02317           // the bit specified by AndRHSV.  If that bit is set, the effect of
02318           // the XOR is to toggle the bit.  If it is clear, then the ADD has
02319           // no effect.
02320           if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
02321             TheAnd.setOperand(0, X);
02322             return &TheAnd;
02323           } else {
02324             std::string Name = Op->getName(); Op->setName("");
02325             // Pull the XOR out of the AND.
02326             Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name);
02327             InsertNewInstBefore(NewAnd, TheAnd);
02328             return BinaryOperator::createXor(NewAnd, AndRHS);
02329           }
02330         }
02331       }
02332     }
02333     break;
02334 
02335   case Instruction::Shl: {
02336     // We know that the AND will not produce any of the bits shifted in, so if
02337     // the anded constant includes them, clear them now!
02338     //
02339     Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
02340     Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS);
02341     Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask);
02342 
02343     if (CI == ShlMask) {   // Masking out bits that the shift already masks
02344       return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and.
02345     } else if (CI != AndRHS) {                  // Reducing bits set in and.
02346       TheAnd.setOperand(1, CI);
02347       return &TheAnd;
02348     }
02349     break;
02350   }
02351   case Instruction::Shr:
02352     // We know that the AND will not produce any of the bits shifted in, so if
02353     // the anded constant includes them, clear them now!  This only applies to
02354     // unsigned shifts, because a signed shr may bring in set bits!
02355     //
02356     if (AndRHS->getType()->isUnsigned()) {
02357       Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
02358       Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS);
02359       Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask);
02360 
02361       if (CI == ShrMask) {   // Masking out bits that the shift already masks.
02362         return ReplaceInstUsesWith(TheAnd, Op);
02363       } else if (CI != AndRHS) {
02364         TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
02365         return &TheAnd;
02366       }
02367     } else {   // Signed shr.
02368       // See if this is shifting in some sign extension, then masking it out
02369       // with an and.
02370       if (Op->hasOneUse()) {
02371         Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
02372         Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS);
02373         Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask);
02374         if (CI == AndRHS) {          // Masking out bits shifted in.
02375           // Make the argument unsigned.
02376           Value *ShVal = Op->getOperand(0);
02377           ShVal = InsertCastBefore(ShVal,
02378                                    ShVal->getType()->getUnsignedVersion(),
02379                                    TheAnd);
02380           ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal,
02381                                                     OpRHS, Op->getName()),
02382                                       TheAnd);
02383           Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType());
02384           ShVal = InsertNewInstBefore(BinaryOperator::createAnd(ShVal, AndRHS2,
02385                                                              TheAnd.getName()),
02386                                       TheAnd);
02387           return new CastInst(ShVal, Op->getType());
02388         }
02389       }
02390     }
02391     break;
02392   }
02393   return 0;
02394 }
02395 
02396 
02397 /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is
02398 /// true, otherwise (V < Lo || V >= Hi).  In pratice, we emit the more efficient
02399 /// (V-Lo) <u Hi-Lo.  This method expects that Lo <= Hi.  IB is the location to
02400 /// insert new instructions.
02401 Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
02402                                            bool Inside, Instruction &IB) {
02403   assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() &&
02404          "Lo is not <= Hi in range emission code!");
02405   if (Inside) {
02406     if (Lo == Hi)  // Trivially false.
02407       return new SetCondInst(Instruction::SetNE, V, V);
02408     if (cast<ConstantIntegral>(Lo)->isMinValue())
02409       return new SetCondInst(Instruction::SetLT, V, Hi);
02410 
02411     Constant *AddCST = ConstantExpr::getNeg(Lo);
02412     Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off");
02413     InsertNewInstBefore(Add, IB);
02414     // Convert to unsigned for the comparison.
02415     const Type *UnsType = Add->getType()->getUnsignedVersion();
02416     Value *OffsetVal = InsertCastBefore(Add, UnsType, IB);
02417     AddCST = ConstantExpr::getAdd(AddCST, Hi);
02418     AddCST = ConstantExpr::getCast(AddCST, UnsType);
02419     return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST);
02420   }
02421 
02422   if (Lo == Hi)  // Trivially true.
02423     return new SetCondInst(Instruction::SetEQ, V, V);
02424 
02425   Hi = SubOne(cast<ConstantInt>(Hi));
02426   if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1'
02427     return new SetCondInst(Instruction::SetGT, V, Hi);
02428 
02429   // Emit X-Lo > Hi-Lo-1
02430   Constant *AddCST = ConstantExpr::getNeg(Lo);
02431   Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off");
02432   InsertNewInstBefore(Add, IB);
02433   // Convert to unsigned for the comparison.
02434   const Type *UnsType = Add->getType()->getUnsignedVersion();
02435   Value *OffsetVal = InsertCastBefore(Add, UnsType, IB);
02436   AddCST = ConstantExpr::getAdd(AddCST, Hi);
02437   AddCST = ConstantExpr::getCast(AddCST, UnsType);
02438   return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST);
02439 }
02440 
02441 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
02442 // any number of 0s on either side.  The 1s are allowed to wrap from LSB to
02443 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
02444 // not, since all 1s are not contiguous.
02445 static bool isRunOfOnes(ConstantIntegral *Val, unsigned &MB, unsigned &ME) {
02446   uint64_t V = Val->getRawValue();
02447   if (!isShiftedMask_64(V)) return false;
02448 
02449   // look for the first zero bit after the run of ones
02450   MB = 64-CountLeadingZeros_64((V - 1) ^ V);
02451   // look for the first non-zero bit
02452   ME = 64-CountLeadingZeros_64(V);
02453   return true;
02454 }
02455 
02456 
02457 
02458 /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
02459 /// where isSub determines whether the operator is a sub.  If we can fold one of
02460 /// the following xforms:
02461 /// 
02462 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
02463 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
02464 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
02465 ///
02466 /// return (A +/- B).
02467 ///
02468 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
02469                                         ConstantIntegral *Mask, bool isSub,
02470                                         Instruction &I) {
02471   Instruction *LHSI = dyn_cast<Instruction>(LHS);
02472   if (!LHSI || LHSI->getNumOperands() != 2 ||
02473       !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
02474 
02475   ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
02476 
02477   switch (LHSI->getOpcode()) {
02478   default: return 0;
02479   case Instruction::And:
02480     if (ConstantExpr::getAnd(N, Mask) == Mask) {
02481       // If the AndRHS is a power of two minus one (0+1+), this is simple.
02482       if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0)
02483         break;
02484 
02485       // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
02486       // part, we don't need any explicit masks to take them out of A.  If that
02487       // is all N is, ignore it.
02488       unsigned MB, ME;
02489       if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
02490         uint64_t Mask = RHS->getType()->getIntegralTypeMask();
02491         Mask >>= 64-MB+1;
02492         if (MaskedValueIsZero(RHS, Mask))
02493           break;
02494       }
02495     }
02496     return 0;
02497   case Instruction::Or:
02498   case Instruction::Xor:
02499     // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
02500     if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0 &&
02501         ConstantExpr::getAnd(N, Mask)->isNullValue())
02502       break;
02503     return 0;
02504   }
02505   
02506   Instruction *New;
02507   if (isSub)
02508     New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold");
02509   else
02510     New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold");
02511   return InsertNewInstBefore(New, I);
02512 }
02513 
02514 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
02515   bool Changed = SimplifyCommutative(I);
02516   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02517 
02518   if (isa<UndefValue>(Op1))                         // X & undef -> 0
02519     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02520 
02521   // and X, X = X
02522   if (Op0 == Op1)
02523     return ReplaceInstUsesWith(I, Op1);
02524 
02525   // See if we can simplify any instructions used by the instruction whose sole 
02526   // purpose is to compute bits we don't care about.
02527   uint64_t KnownZero, KnownOne;
02528   if (!isa<PackedType>(I.getType()) &&
02529       SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(),
02530                            KnownZero, KnownOne))
02531     return &I;
02532   
02533   if (ConstantIntegral *AndRHS = dyn_cast<ConstantIntegral>(Op1)) {
02534     uint64_t AndRHSMask = AndRHS->getZExtValue();
02535     uint64_t TypeMask = Op0->getType()->getIntegralTypeMask();
02536     uint64_t NotAndRHS = AndRHSMask^TypeMask;
02537 
02538     // Optimize a variety of ((val OP C1) & C2) combinations...
02539     if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) {
02540       Instruction *Op0I = cast<Instruction>(Op0);
02541       Value *Op0LHS = Op0I->getOperand(0);
02542       Value *Op0RHS = Op0I->getOperand(1);
02543       switch (Op0I->getOpcode()) {
02544       case Instruction::Xor:
02545       case Instruction::Or:
02546         // If the mask is only needed on one incoming arm, push it up.
02547         if (Op0I->hasOneUse()) {
02548           if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
02549             // Not masking anything out for the LHS, move to RHS.
02550             Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS,
02551                                                    Op0RHS->getName()+".masked");
02552             InsertNewInstBefore(NewRHS, I);
02553             return BinaryOperator::create(
02554                        cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS);
02555           }
02556           if (!isa<Constant>(Op0RHS) &&
02557               MaskedValueIsZero(Op0RHS, NotAndRHS)) {
02558             // Not masking anything out for the RHS, move to LHS.
02559             Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS,
02560                                                    Op0LHS->getName()+".masked");
02561             InsertNewInstBefore(NewLHS, I);
02562             return BinaryOperator::create(
02563                        cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS);
02564           }
02565         }
02566 
02567         break;
02568       case Instruction::Add:
02569         // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
02570         // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
02571         // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
02572         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
02573           return BinaryOperator::createAnd(V, AndRHS);
02574         if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
02575           return BinaryOperator::createAnd(V, AndRHS);  // Add commutes
02576         break;
02577 
02578       case Instruction::Sub:
02579         // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
02580         // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
02581         // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
02582         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
02583           return BinaryOperator::createAnd(V, AndRHS);
02584         break;
02585       }
02586 
02587       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
02588         if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
02589           return Res;
02590     } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
02591       const Type *SrcTy = CI->getOperand(0)->getType();
02592 
02593       // If this is an integer truncation or change from signed-to-unsigned, and
02594       // if the source is an and/or with immediate, transform it.  This
02595       // frequently occurs for bitfield accesses.
02596       if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
02597         if (SrcTy->getPrimitiveSizeInBits() >= 
02598               I.getType()->getPrimitiveSizeInBits() &&
02599             CastOp->getNumOperands() == 2)
02600           if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1)))
02601             if (CastOp->getOpcode() == Instruction::And) {
02602               // Change: and (cast (and X, C1) to T), C2
02603               // into  : and (cast X to T), trunc(C1)&C2
02604               // This will folds the two ands together, which may allow other
02605               // simplifications.
02606               Instruction *NewCast =
02607                 new CastInst(CastOp->getOperand(0), I.getType(),
02608                              CastOp->getName()+".shrunk");
02609               NewCast = InsertNewInstBefore(NewCast, I);
02610               
02611               Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1)
02612               C3 = ConstantExpr::getAnd(C3, AndRHS);            // trunc(C1)&C2
02613               return BinaryOperator::createAnd(NewCast, C3);
02614             } else if (CastOp->getOpcode() == Instruction::Or) {
02615               // Change: and (cast (or X, C1) to T), C2
02616               // into  : trunc(C1)&C2 iff trunc(C1)&C2 == C2
02617               Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1)
02618               if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS)   // trunc(C1)&C2
02619                 return ReplaceInstUsesWith(I, AndRHS);
02620             }
02621       }
02622     }
02623 
02624     // Try to fold constant and into select arguments.
02625     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
02626       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02627         return R;
02628     if (isa<PHINode>(Op0))
02629       if (Instruction *NV = FoldOpIntoPhi(I))
02630         return NV;
02631   }
02632 
02633   Value *Op0NotVal = dyn_castNotVal(Op0);
02634   Value *Op1NotVal = dyn_castNotVal(Op1);
02635 
02636   if (Op0NotVal == Op1 || Op1NotVal == Op0)  // A & ~A  == ~A & A == 0
02637     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
02638 
02639   // (~A & ~B) == (~(A | B)) - De Morgan's Law
02640   if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
02641     Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal,
02642                                                I.getName()+".demorgan");
02643     InsertNewInstBefore(Or, I);
02644     return BinaryOperator::createNot(Or);
02645   }
02646   
02647   {
02648     Value *A = 0, *B = 0;
02649     ConstantInt *C1 = 0, *C2 = 0;
02650     if (match(Op0, m_Or(m_Value(A), m_Value(B))))
02651       if (A == Op1 || B == Op1)    // (A | ?) & A  --> A
02652         return ReplaceInstUsesWith(I, Op1);
02653     if (match(Op1, m_Or(m_Value(A), m_Value(B))))
02654       if (A == Op0 || B == Op0)    // A & (A | ?)  --> A
02655         return ReplaceInstUsesWith(I, Op0);
02656     
02657     if (Op0->hasOneUse() &&
02658         match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
02659       if (A == Op1) {                                // (A^B)&A -> A&(A^B)
02660         I.swapOperands();     // Simplify below
02661         std::swap(Op0, Op1);
02662       } else if (B == Op1) {                         // (A^B)&B -> B&(B^A)
02663         cast<BinaryOperator>(Op0)->swapOperands();
02664         I.swapOperands();     // Simplify below
02665         std::swap(Op0, Op1);
02666       }
02667     }
02668     if (Op1->hasOneUse() &&
02669         match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
02670       if (B == Op0) {                                // B&(A^B) -> B&(B^A)
02671         cast<BinaryOperator>(Op1)->swapOperands();
02672         std::swap(A, B);
02673       }
02674       if (A == Op0) {                                // A&(A^B) -> A & ~B
02675         Instruction *NotB = BinaryOperator::createNot(B, "tmp");
02676         InsertNewInstBefore(NotB, I);
02677         return BinaryOperator::createAnd(A, NotB);
02678       }
02679     }
02680   }
02681   
02682 
02683   if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) {
02684     // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B)
02685     if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
02686       return R;
02687 
02688     Value *LHSVal, *RHSVal;
02689     ConstantInt *LHSCst, *RHSCst;
02690     Instruction::BinaryOps LHSCC, RHSCC;
02691     if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst))))
02692       if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst))))
02693         if (LHSVal == RHSVal &&    // Found (X setcc C1) & (X setcc C2)
02694             // Set[GL]E X, CST is folded to Set[GL]T elsewhere.
02695             LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE &&
02696             RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) {
02697           // Ensure that the larger constant is on the RHS.
02698           Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst);
02699           SetCondInst *LHS = cast<SetCondInst>(Op0);
02700           if (cast<ConstantBool>(Cmp)->getValue()) {
02701             std::swap(LHS, RHS);
02702             std::swap(LHSCst, RHSCst);
02703             std::swap(LHSCC, RHSCC);
02704           }
02705 
02706           // At this point, we know we have have two setcc instructions
02707           // comparing a value against two constants and and'ing the result
02708           // together.  Because of the above check, we know that we only have
02709           // SetEQ, SetNE, SetLT, and SetGT here.  We also know (from the
02710           // FoldSetCCLogical check above), that the two constants are not
02711           // equal.
02712           assert(LHSCst != RHSCst && "Compares not folded above?");
02713 
02714           switch (LHSCC) {
02715           default: assert(0 && "Unknown integer condition code!");
02716           case Instruction::SetEQ:
02717             switch (RHSCC) {
02718             default: assert(0 && "Unknown integer condition code!");
02719             case Instruction::SetEQ:  // (X == 13 & X == 15) -> false
02720             case Instruction::SetGT:  // (X == 13 & X > 15)  -> false
02721               return ReplaceInstUsesWith(I, ConstantBool::False);
02722             case Instruction::SetNE:  // (X == 13 & X != 15) -> X == 13
02723             case Instruction::SetLT:  // (X == 13 & X < 15)  -> X == 13
02724               return ReplaceInstUsesWith(I, LHS);
02725             }
02726           case Instruction::SetNE:
02727             switch (RHSCC) {
02728             default: assert(0 && "Unknown integer condition code!");
02729             case Instruction::SetLT:
02730               if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13
02731                 return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst);
02732               break;                        // (X != 13 & X < 15) -> no change
02733             case Instruction::SetEQ:        // (X != 13 & X == 15) -> X == 15
02734             case Instruction::SetGT:        // (X != 13 & X > 15)  -> X > 15
02735               return ReplaceInstUsesWith(I, RHS);
02736             case Instruction::SetNE:
02737               if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1
02738                 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
02739                 Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
02740                                                       LHSVal->getName()+".off");
02741                 InsertNewInstBefore(Add, I);
02742                 const Type *UnsType = Add->getType()->getUnsignedVersion();
02743                 Value *OffsetVal = InsertCastBefore(Add, UnsType, I);
02744                 AddCST = ConstantExpr::getSub(RHSCst, LHSCst);
02745                 AddCST = ConstantExpr::getCast(AddCST, UnsType);
02746                 return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST);
02747               }
02748               break;                        // (X != 13 & X != 15) -> no change
02749             }
02750             break;
02751           case Instruction::SetLT:
02752             switch (RHSCC) {
02753             default: assert(0 && "Unknown integer condition code!");
02754             case Instruction::SetEQ:  // (X < 13 & X == 15) -> false
02755             case Instruction::SetGT:  // (X < 13 & X > 15)  -> false
02756               return ReplaceInstUsesWith(I, ConstantBool::False);
02757             case Instruction::SetNE:  // (X < 13 & X != 15) -> X < 13
02758             case Instruction::SetLT:  // (X < 13 & X < 15) -> X < 13
02759               return ReplaceInstUsesWith(I, LHS);
02760             }
02761           case Instruction::SetGT:
02762             switch (RHSCC) {
02763             default: assert(0 && "Unknown integer condition code!");
02764             case Instruction::SetEQ:  // (X > 13 & X == 15) -> X > 13
02765               return ReplaceInstUsesWith(I, LHS);
02766             case Instruction::SetGT:  // (X > 13 & X > 15)  -> X > 15
02767               return ReplaceInstUsesWith(I, RHS);
02768             case Instruction::SetNE:
02769               if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14
02770                 return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst);
02771               break;                        // (X > 13 & X != 15) -> no change
02772             case Instruction::SetLT:   // (X > 13 & X < 15) -> (X-14) <u 1
02773               return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I);
02774             }
02775           }
02776         }
02777   }
02778 
02779   // fold (and (cast A), (cast B)) -> (cast (and A, B))
02780   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
02781     const Type *SrcTy = Op0C->getOperand(0)->getType();
02782     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
02783       if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegral() &&
02784           // Only do this if the casts both really cause code to be generated.
02785           ValueRequiresCast(Op0C->getOperand(0), I.getType(), TD) &&
02786           ValueRequiresCast(Op1C->getOperand(0), I.getType(), TD)) {
02787         Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0),
02788                                                        Op1C->getOperand(0),
02789                                                        I.getName());
02790         InsertNewInstBefore(NewOp, I);
02791         return new CastInst(NewOp, I.getType());
02792       }
02793   }
02794 
02795   return Changed ? &I : 0;
02796 }
02797 
02798 /// CollectBSwapParts - Look to see if the specified value defines a single byte
02799 /// in the result.  If it does, and if the specified byte hasn't been filled in
02800 /// yet, fill it in and return false.
02801 static bool CollectBSwapParts(Value *V, std::vector<Value*> &ByteValues) {
02802   Instruction *I = dyn_cast<Instruction>(V);
02803   if (I == 0) return true;
02804 
02805   // If this is an or instruction, it is an inner node of the bswap.
02806   if (I->getOpcode() == Instruction::Or)
02807     return CollectBSwapParts(I->getOperand(0), ByteValues) ||
02808            CollectBSwapParts(I->getOperand(1), ByteValues);
02809   
02810   // If this is a shift by a constant int, and it is "24", then its operand
02811   // defines a byte.  We only handle unsigned types here.
02812   if (isa<ShiftInst>(I) && isa<ConstantInt>(I->getOperand(1))) {
02813     // Not shifting the entire input by N-1 bytes?
02814     if (cast<ConstantInt>(I->getOperand(1))->getRawValue() !=
02815         8*(ByteValues.size()-1))
02816       return true;
02817     
02818     unsigned DestNo;
02819     if (I->getOpcode() == Instruction::Shl) {
02820       // X << 24 defines the top byte with the lowest of the input bytes.
02821       DestNo = ByteValues.size()-1;
02822     } else {
02823       // X >>u 24 defines the low byte with the highest of the input bytes.
02824       DestNo = 0;
02825     }
02826     
02827     // If the destination byte value is already defined, the values are or'd
02828     // together, which isn't a bswap (unless it's an or of the same bits).
02829     if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0))
02830       return true;
02831     ByteValues[DestNo] = I->getOperand(0);
02832     return false;
02833   }
02834   
02835   // Otherwise, we can only handle and(shift X, imm), imm).  Bail out of if we
02836   // don't have this.
02837   Value *Shift = 0, *ShiftLHS = 0;
02838   ConstantInt *AndAmt = 0, *ShiftAmt = 0;
02839   if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) ||
02840       !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt))))
02841     return true;
02842   Instruction *SI = cast<Instruction>(Shift);
02843 
02844   // Make sure that the shift amount is by a multiple of 8 and isn't too big.
02845   if (ShiftAmt->getRawValue() & 7 ||
02846       ShiftAmt->getRawValue() > 8*ByteValues.size())
02847     return true;
02848   
02849   // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc.
02850   unsigned DestByte;
02851   for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte)
02852     if (AndAmt->getRawValue() == uint64_t(0xFF) << 8*DestByte)
02853       break;
02854   // Unknown mask for bswap.
02855   if (DestByte == ByteValues.size()) return true;
02856   
02857   unsigned ShiftBytes = ShiftAmt->getRawValue()/8;
02858   unsigned SrcByte;
02859   if (SI->getOpcode() == Instruction::Shl)
02860     SrcByte = DestByte - ShiftBytes;
02861   else
02862     SrcByte = DestByte + ShiftBytes;
02863   
02864   // If the SrcByte isn't a bswapped value from the DestByte, reject it.
02865   if (SrcByte != ByteValues.size()-DestByte-1)
02866     return true;
02867   
02868   // If the destination byte value is already defined, the values are or'd
02869   // together, which isn't a bswap (unless it's an or of the same bits).
02870   if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0))
02871     return true;
02872   ByteValues[DestByte] = SI->getOperand(0);
02873   return false;
02874 }
02875 
02876 /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
02877 /// If so, insert the new bswap intrinsic and return it.
02878 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
02879   // We can only handle bswap of unsigned integers, and cannot bswap one byte.
02880   if (!I.getType()->isUnsigned() || I.getType() == Type::UByteTy)
02881     return 0;
02882   
02883   /// ByteValues - For each byte of the result, we keep track of which value
02884   /// defines each byte.
02885   std::vector<Value*> ByteValues;
02886   ByteValues.resize(I.getType()->getPrimitiveSize());
02887     
02888   // Try to find all the pieces corresponding to the bswap.
02889   if (CollectBSwapParts(I.getOperand(0), ByteValues) ||
02890       CollectBSwapParts(I.getOperand(1), ByteValues))
02891     return 0;
02892   
02893   // Check to see if all of the bytes come from the same value.
02894   Value *V = ByteValues[0];
02895   if (V == 0) return 0;  // Didn't find a byte?  Must be zero.
02896   
02897   // Check to make sure that all of the bytes come from the same value.
02898   for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
02899     if (ByteValues[i] != V)
02900       return 0;
02901     
02902   // If they do then *success* we can turn this into a bswap.  Figure out what
02903   // bswap to make it into.
02904   Module *M = I.getParent()->getParent()->getParent();
02905   const char *FnName = 0;
02906   if (I.getType() == Type::UShortTy)
02907     FnName = "llvm.bswap.i16";
02908   else if (I.getType() == Type::UIntTy)
02909     FnName = "llvm.bswap.i32";
02910   else if (I.getType() == Type::ULongTy)
02911     FnName = "llvm.bswap.i64";
02912   else
02913     assert(0 && "Unknown integer type!");
02914   Function *F = M->getOrInsertFunction(FnName, I.getType(), I.getType(), NULL);
02915   
02916   return new CallInst(F, V);
02917 }
02918 
02919 
02920 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
02921   bool Changed = SimplifyCommutative(I);
02922   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02923 
02924   if (isa<UndefValue>(Op1))
02925     return ReplaceInstUsesWith(I,                         // X | undef -> -1
02926                                ConstantIntegral::getAllOnesValue(I.getType()));
02927 
02928   // or X, X = X
02929   if (Op0 == Op1)
02930     return ReplaceInstUsesWith(I, Op0);
02931 
02932   // See if we can simplify any instructions used by the instruction whose sole 
02933   // purpose is to compute bits we don't care about.
02934   uint64_t KnownZero, KnownOne;
02935   if (!isa<PackedType>(I.getType()) &&
02936       SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(),
02937                            KnownZero, KnownOne))
02938     return &I;
02939   
02940   // or X, -1 == -1
02941   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
02942     ConstantInt *C1 = 0; Value *X = 0;
02943     // (X & C1) | C2 --> (X | C2) & (C1|C2)
02944     if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
02945       Instruction *Or = BinaryOperator::createOr(X, RHS, Op0->getName());
02946       Op0->setName("");
02947       InsertNewInstBefore(Or, I);
02948       return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1));
02949     }
02950 
02951     // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
02952     if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
02953       std::string Op0Name = Op0->getName(); Op0->setName("");
02954       Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name);
02955       InsertNewInstBefore(Or, I);
02956       return BinaryOperator::createXor(Or,
02957                  ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS)));
02958     }
02959 
02960     // Try to fold constant and into select arguments.
02961     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
02962       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
02963         return R;
02964     if (isa<PHINode>(Op0))
02965       if (Instruction *NV = FoldOpIntoPhi(I))
02966         return NV;
02967   }
02968 
02969   Value *A = 0, *B = 0;
02970   ConstantInt *C1 = 0, *C2 = 0;
02971 
02972   if (match(Op0, m_And(m_Value(A), m_Value(B))))
02973     if (A == Op1 || B == Op1)    // (A & ?) | A  --> A
02974       return ReplaceInstUsesWith(I, Op1);
02975   if (match(Op1, m_And(m_Value(A), m_Value(B))))
02976     if (A == Op0 || B == Op0)    // A | (A & ?)  --> A
02977       return ReplaceInstUsesWith(I, Op0);
02978 
02979   // (A | B) | C  and  A | (B | C)                  -> bswap if possible.
02980   // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible.
02981   if (match(Op0, m_Or(m_Value(), m_Value())) ||
02982       match(Op1, m_Or(m_Value(), m_Value())) ||
02983       (match(Op0, m_Shift(m_Value(), m_Value())) &&
02984        match(Op1, m_Shift(m_Value(), m_Value())))) {
02985     if (Instruction *BSwap = MatchBSwap(I))
02986       return BSwap;
02987   }
02988   
02989   // (X^C)|Y -> (X|Y)^C iff Y&C == 0
02990   if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
02991       MaskedValueIsZero(Op1, C1->getZExtValue())) {
02992     Instruction *NOr = BinaryOperator::createOr(A, Op1, Op0->getName());
02993     Op0->setName("");
02994     return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1);
02995   }
02996 
02997   // Y|(X^C) -> (X|Y)^C iff Y&C == 0
02998   if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
02999       MaskedValueIsZero(Op0, C1->getZExtValue())) {
03000     Instruction *NOr = BinaryOperator::createOr(A, Op0, Op1->getName());
03001     Op0->setName("");
03002     return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1);
03003   }
03004 
03005   // (A & C1)|(B & C2)
03006   if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) &&
03007       match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) {
03008 
03009     if (A == B)  // (A & C1)|(A & C2) == A & (C1|C2)
03010       return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2));
03011 
03012 
03013     // If we have: ((V + N) & C1) | (V & C2)
03014     // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
03015     // replace with V+N.
03016     if (C1 == ConstantExpr::getNot(C2)) {
03017       Value *V1 = 0, *V2 = 0;
03018       if ((C2->getRawValue() & (C2->getRawValue()+1)) == 0 && // C2 == 0+1+
03019           match(A, m_Add(m_Value(V1), m_Value(V2)))) {
03020         // Add commutes, try both ways.
03021         if (V1 == B && MaskedValueIsZero(V2, C2->getZExtValue()))
03022           return ReplaceInstUsesWith(I, A);
03023         if (V2 == B && MaskedValueIsZero(V1, C2->getZExtValue()))
03024           return ReplaceInstUsesWith(I, A);
03025       }
03026       // Or commutes, try both ways.
03027       if ((C1->getRawValue() & (C1->getRawValue()+1)) == 0 &&
03028           match(B, m_Add(m_Value(V1), m_Value(V2)))) {
03029         // Add commutes, try both ways.
03030         if (V1 == A && MaskedValueIsZero(V2, C1->getZExtValue()))
03031           return ReplaceInstUsesWith(I, B);
03032         if (V2 == A && MaskedValueIsZero(V1, C1->getZExtValue()))
03033           return ReplaceInstUsesWith(I, B);
03034       }
03035     }
03036   }
03037 
03038   if (match(Op0, m_Not(m_Value(A)))) {   // ~A | Op1
03039     if (A == Op1)   // ~A | A == -1
03040       return ReplaceInstUsesWith(I,
03041                                 ConstantIntegral::getAllOnesValue(I.getType()));
03042   } else {
03043     A = 0;
03044   }
03045   // Note, A is still live here!
03046   if (match(Op1, m_Not(m_Value(B)))) {   // Op0 | ~B
03047     if (Op0 == B)
03048       return ReplaceInstUsesWith(I,
03049                                 ConstantIntegral::getAllOnesValue(I.getType()));
03050 
03051     // (~A | ~B) == (~(A & B)) - De Morgan's Law
03052     if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
03053       Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B,
03054                                               I.getName()+".demorgan"), I);
03055       return BinaryOperator::createNot(And);
03056     }
03057   }
03058 
03059   // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B)
03060   if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) {
03061     if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
03062       return R;
03063 
03064     Value *LHSVal, *RHSVal;
03065     ConstantInt *LHSCst, *RHSCst;
03066     Instruction::BinaryOps LHSCC, RHSCC;
03067     if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst))))
03068       if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst))))
03069         if (LHSVal == RHSVal &&    // Found (X setcc C1) | (X setcc C2)
03070             // Set[GL]E X, CST is folded to Set[GL]T elsewhere.
03071             LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE &&
03072             RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) {
03073           // Ensure that the larger constant is on the RHS.
03074           Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst);
03075           SetCondInst *LHS = cast<SetCondInst>(Op0);
03076           if (cast<ConstantBool>(Cmp)->getValue()) {
03077             std::swap(LHS, RHS);
03078             std::swap(LHSCst, RHSCst);
03079             std::swap(LHSCC, RHSCC);
03080           }
03081 
03082           // At this point, we know we have have two setcc instructions
03083           // comparing a value against two constants and or'ing the result
03084           // together.  Because of the above check, we know that we only have
03085           // SetEQ, SetNE, SetLT, and SetGT here.  We also know (from the
03086           // FoldSetCCLogical check above), that the two constants are not
03087           // equal.
03088           assert(LHSCst != RHSCst && "Compares not folded above?");
03089 
03090           switch (LHSCC) {
03091           default: assert(0 && "Unknown integer condition code!");
03092           case Instruction::SetEQ:
03093             switch (RHSCC) {
03094             default: assert(0 && "Unknown integer condition code!");
03095             case Instruction::SetEQ:
03096               if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2
03097                 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
03098                 Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
03099                                                       LHSVal->getName()+".off");
03100                 InsertNewInstBefore(Add, I);
03101                 const Type *UnsType = Add->getType()->getUnsignedVersion();
03102                 Value *OffsetVal = InsertCastBefore(Add, UnsType, I);
03103                 AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
03104                 AddCST = ConstantExpr::getCast(AddCST, UnsType);
03105                 return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST);
03106               }
03107               break;                  // (X == 13 | X == 15) -> no change
03108 
03109             case Instruction::SetGT:  // (X == 13 | X > 14) -> no change
03110               break;
03111             case Instruction::SetNE:  // (X == 13 | X != 15) -> X != 15
03112             case Instruction::SetLT:  // (X == 13 | X < 15)  -> X < 15
03113               return ReplaceInstUsesWith(I, RHS);
03114             }
03115             break;
03116           case Instruction::SetNE:
03117             switch (RHSCC) {
03118             default: assert(0 && "Unknown integer condition code!");
03119             case Instruction::SetEQ:        // (X != 13 | X == 15) -> X != 13
03120             case Instruction::SetGT:        // (X != 13 | X > 15)  -> X != 13
03121               return ReplaceInstUsesWith(I, LHS);
03122             case Instruction::SetNE:        // (X != 13 | X != 15) -> true
03123             case Instruction::SetLT:        // (X != 13 | X < 15)  -> true
03124               return ReplaceInstUsesWith(I, ConstantBool::True);
03125             }
03126             break;
03127           case Instruction::SetLT:
03128             switch (RHSCC) {
03129             default: assert(0 && "Unknown integer condition code!");
03130             case Instruction::SetEQ:  // (X < 13 | X == 14) -> no change
03131               break;
03132             case Instruction::SetGT:  // (X < 13 | X > 15)  -> (X-13) > 2
03133               return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I);
03134             case Instruction::SetNE:  // (X < 13 | X != 15) -> X != 15
03135             case Instruction::SetLT:  // (X < 13 | X < 15) -> X < 15
03136               return ReplaceInstUsesWith(I, RHS);
03137             }
03138             break;
03139           case Instruction::SetGT:
03140             switch (RHSCC) {
03141             default: assert(0 && "Unknown integer condition code!");
03142             case Instruction::SetEQ:  // (X > 13 | X == 15) -> X > 13
03143             case Instruction::SetGT:  // (X > 13 | X > 15)  -> X > 13
03144               return ReplaceInstUsesWith(I, LHS);
03145             case Instruction::SetNE:  // (X > 13 | X != 15)  -> true
03146             case Instruction::SetLT:  // (X > 13 | X < 15) -> true
03147               return ReplaceInstUsesWith(I, ConstantBool::True);
03148             }
03149           }
03150         }
03151   }
03152     
03153   // fold (or (cast A), (cast B)) -> (cast (or A, B))
03154   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
03155     const Type *SrcTy = Op0C->getOperand(0)->getType();
03156     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
03157       if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegral() &&
03158           // Only do this if the casts both really cause code to be generated.
03159           ValueRequiresCast(Op0C->getOperand(0), I.getType(), TD) &&
03160           ValueRequiresCast(Op1C->getOperand(0), I.getType(), TD)) {
03161         Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0),
03162                                                       Op1C->getOperand(0),
03163                                                       I.getName());
03164         InsertNewInstBefore(NewOp, I);
03165         return new CastInst(NewOp, I.getType());
03166       }
03167   }
03168       
03169 
03170   return Changed ? &I : 0;
03171 }
03172 
03173 // XorSelf - Implements: X ^ X --> 0
03174 struct XorSelf {
03175   Value *RHS;
03176   XorSelf(Value *rhs) : RHS(rhs) {}
03177   bool shouldApply(Value *LHS) const { return LHS == RHS; }
03178   Instruction *apply(BinaryOperator &Xor) const {
03179     return &Xor;
03180   }
03181 };
03182 
03183 
03184 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
03185   bool Changed = SimplifyCommutative(I);
03186   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
03187 
03188   if (isa<UndefValue>(Op1))
03189     return ReplaceInstUsesWith(I, Op1);  // X ^ undef -> undef
03190 
03191   // xor X, X = 0, even if X is nested in a sequence of Xor's.
03192   if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
03193     assert(Result == &I && "AssociativeOpt didn't work?");
03194     return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
03195   }
03196   
03197   // See if we can simplify any instructions used by the instruction whose sole 
03198   // purpose is to compute bits we don't care about.
03199   uint64_t KnownZero, KnownOne;
03200   if (!isa<PackedType>(I.getType()) &&
03201       SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(),
03202                            KnownZero, KnownOne))
03203     return &I;
03204 
03205   if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
03206     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
03207       // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
03208       if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I))
03209         if (RHS == ConstantBool::True && SCI->hasOneUse())
03210           return new SetCondInst(SCI->getInverseCondition(),
03211                                  SCI->getOperand(0), SCI->getOperand(1));
03212 
03213       // ~(c-X) == X-c-1 == X+(-c-1)
03214       if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
03215         if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
03216           Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
03217           Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
03218                                               ConstantInt::get(I.getType(), 1));
03219           return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS);
03220         }
03221 
03222       // ~(~X & Y) --> (X | ~Y)
03223       if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) {
03224         if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
03225         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
03226           Instruction *NotY =
03227             BinaryOperator::createNot(Op0I->getOperand(1),
03228                                       Op0I->getOperand(1)->getName()+".not");
03229           InsertNewInstBefore(NotY, I);
03230           return BinaryOperator::createOr(Op0NotVal, NotY);
03231         }
03232       }
03233 
03234       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
03235         if (Op0I->getOpcode() == Instruction::Add) {
03236           // ~(X-c) --> (-c-1)-X
03237           if (RHS->isAllOnesValue()) {
03238             Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
03239             return BinaryOperator::createSub(
03240                            ConstantExpr::getSub(NegOp0CI,
03241                                              ConstantInt::get(I.getType(), 1)),
03242                                           Op0I->getOperand(0));
03243           }
03244         } else if (Op0I->getOpcode() == Instruction::Or) {
03245           // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
03246           if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getZExtValue())) {
03247             Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
03248             // Anything in both C1 and C2 is known to be zero, remove it from
03249             // NewRHS.
03250             Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
03251             NewRHS = ConstantExpr::getAnd(NewRHS, 
03252                                           ConstantExpr::getNot(CommonBits));
03253             WorkList.push_back(Op0I);
03254             I.setOperand(0, Op0I->getOperand(0));
03255             I.setOperand(1, NewRHS);
03256             return &I;
03257           }
03258         }
03259     }
03260 
03261     // Try to fold constant and into select arguments.
03262     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
03263       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
03264         return R;
03265     if (isa<PHINode>(Op0))
03266       if (Instruction *NV = FoldOpIntoPhi(I))
03267         return NV;
03268   }
03269 
03270   if (Value *X = dyn_castNotVal(Op0))   // ~A ^ A == -1
03271     if (X == Op1)
03272       return ReplaceInstUsesWith(I,
03273                                 ConstantIntegral::getAllOnesValue(I.getType()));
03274 
03275   if (Value *X = dyn_castNotVal(Op1))   // A ^ ~A == -1
03276     if (X == Op0)
03277       return ReplaceInstUsesWith(I,
03278                                 ConstantIntegral::getAllOnesValue(I.getType()));
03279 
03280   if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
03281     if (Op1I->getOpcode() == Instruction::Or) {
03282       if (Op1I->getOperand(0) == Op0) {              // B^(B|A) == (A|B)^B
03283         Op1I->swapOperands();
03284         I.swapOperands();
03285         std::swap(Op0, Op1);
03286       } else if (Op1I->getOperand(1) == Op0) {       // B^(A|B) == (A|B)^B
03287         I.swapOperands();     // Simplified below.
03288         std::swap(Op0, Op1);
03289       }
03290     } else if (Op1I->getOpcode() == Instruction::Xor) {
03291       if (Op0 == Op1I->getOperand(0))                        // A^(A^B) == B
03292         return ReplaceInstUsesWith(I, Op1I->getOperand(1));
03293       else if (Op0 == Op1I->getOperand(1))                   // A^(B^A) == B
03294         return ReplaceInstUsesWith(I, Op1I->getOperand(0));
03295     } else if (Op1I->getOpcode() == Instruction::And && Op1I->hasOneUse()) {
03296       if (Op1I->getOperand(0) == Op0)                      // A^(A&B) -> A^(B&A)
03297         Op1I->swapOperands();
03298       if (Op0 == Op1I->getOperand(1)) {                    // A^(B&A) -> (B&A)^A
03299         I.swapOperands();     // Simplified below.
03300         std::swap(Op0, Op1);
03301       }
03302     }
03303 
03304   if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
03305     if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) {
03306       if (Op0I->getOperand(0) == Op1)                // (B|A)^B == (A|B)^B
03307         Op0I->swapOperands();
03308       if (Op0I->getOperand(1) == Op1) {              // (A|B)^B == A & ~B
03309         Instruction *NotB = BinaryOperator::createNot(Op1, "tmp");
03310         InsertNewInstBefore(NotB, I);
03311         return BinaryOperator::createAnd(Op0I->getOperand(0), NotB);
03312       }
03313     } else if (Op0I->getOpcode() == Instruction::Xor) {
03314       if (Op1 == Op0I->getOperand(0))                        // (A^B)^A == B
03315         return ReplaceInstUsesWith(I, Op0I->getOperand(1));
03316       else if (Op1 == Op0I->getOperand(1))                   // (B^A)^A == B
03317         return ReplaceInstUsesWith(I, Op0I->getOperand(0));
03318     } else if (Op0I->getOpcode() == Instruction::And && Op0I->hasOneUse()) {
03319       if (Op0I->getOperand(0) == Op1)                      // (A&B)^A -> (B&A)^A
03320         Op0I->swapOperands();
03321       if (Op0I->getOperand(1) == Op1 &&                    // (B&A)^A == ~B & A
03322           !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C
03323         Instruction *N = BinaryOperator::createNot(Op0I->getOperand(0), "tmp");
03324         InsertNewInstBefore(N, I);
03325         return BinaryOperator::createAnd(N, Op1);
03326       }
03327     }
03328 
03329   // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B)
03330   if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1)))
03331     if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
03332       return R;
03333 
03334   // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
03335   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
03336     const Type *SrcTy = Op0C->getOperand(0)->getType();
03337     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
03338       if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegral() &&
03339           // Only do this if the casts both really cause code to be generated.
03340           ValueRequiresCast(Op0C->getOperand(0), I.getType(), TD) &&
03341           ValueRequiresCast(Op1C->getOperand(0), I.getType(), TD)) {
03342         Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0),
03343                                                        Op1C->getOperand(0),
03344                                                        I.getName());
03345         InsertNewInstBefore(NewOp, I);
03346         return new CastInst(NewOp, I.getType());
03347       }
03348   }
03349     
03350   return Changed ? &I : 0;
03351 }
03352 
03353 /// MulWithOverflow - Compute Result = In1*In2, returning true if the result
03354 /// overflowed for this type.
03355 static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1,
03356                             ConstantInt *In2) {
03357   Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2));
03358   return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1;
03359 }
03360 
03361 static bool isPositive(ConstantInt *C) {
03362   return cast<ConstantSInt>(C)->getValue() >= 0;
03363 }
03364 
03365 /// AddWithOverflow - Compute Result = In1+In2, returning true if the result
03366 /// overflowed for this type.
03367 static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1,
03368                             ConstantInt *In2) {
03369   Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2));
03370 
03371   if (In1->getType()->isUnsigned())
03372     return cast<ConstantUInt>(Result)->getValue() <
03373            cast<ConstantUInt>(In1)->getValue();
03374   if (isPositive(In1) != isPositive(In2))
03375     return false;
03376   if (isPositive(In1))
03377     return cast<ConstantSInt>(Result)->getValue() <
03378            cast<ConstantSInt>(In1)->getValue();
03379   return cast<ConstantSInt>(Result)->getValue() >
03380          cast<ConstantSInt>(In1)->getValue();
03381 }
03382 
03383 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
03384 /// code necessary to compute the offset from the base pointer (without adding
03385 /// in the base pointer).  Return the result as a signed integer of intptr size.
03386 static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
03387   TargetData &TD = IC.getTargetData();
03388   gep_type_iterator GTI = gep_type_begin(GEP);
03389   const Type *UIntPtrTy = TD.getIntPtrType();
03390   const Type *SIntPtrTy = UIntPtrTy->getSignedVersion();
03391   Value *Result = Constant::getNullValue(SIntPtrTy);
03392 
03393   // Build a mask for high order bits.
03394   uint64_t PtrSizeMask = ~0ULL >> (64-TD.getPointerSize()*8);
03395 
03396   for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
03397     Value *Op = GEP->getOperand(i);
03398     uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask;
03399     Constant *Scale = ConstantExpr::getCast(ConstantUInt::get(UIntPtrTy, Size),
03400                                             SIntPtrTy);
03401     if (Constant *OpC = dyn_cast<Constant>(Op)) {
03402       if (!OpC->isNullValue()) {
03403         OpC = ConstantExpr::getCast(OpC, SIntPtrTy);
03404         Scale = ConstantExpr::getMul(OpC, Scale);
03405         if (Constant *RC = dyn_cast<Constant>(Result))
03406           Result = ConstantExpr::getAdd(RC, Scale);
03407         else {
03408           // Emit an add instruction.
03409           Result = IC.InsertNewInstBefore(
03410              BinaryOperator::createAdd(Result, Scale,
03411                                        GEP->getName()+".offs"), I);
03412         }
03413       }
03414     } else {
03415       // Convert to correct type.
03416       Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy,
03417                                                Op->getName()+".c"), I);
03418       if (Size != 1)
03419         // We'll let instcombine(mul) convert this to a shl if possible.
03420         Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale,
03421                                                     GEP->getName()+".idx"), I);
03422 
03423       // Emit an add instruction.
03424       Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result,
03425                                                     GEP->getName()+".offs"), I);
03426     }
03427   }
03428   return Result;
03429 }
03430 
03431 /// FoldGEPSetCC - Fold comparisons between a GEP instruction and something
03432 /// else.  At this point we know that the GEP is on the LHS of the comparison.
03433 Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS,
03434                                         Instruction::BinaryOps Cond,
03435                                         Instruction &I) {
03436   assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!");
03437 
03438   if (CastInst *CI = dyn_cast<CastInst>(RHS))
03439     if (isa<PointerType>(CI->getOperand(0)->getType()))
03440       RHS = CI->getOperand(0);
03441 
03442   Value *PtrBase = GEPLHS->getOperand(0);
03443   if (PtrBase == RHS) {
03444     // As an optimization, we don't actually have to compute the actual value of
03445     // OFFSET if this is a seteq or setne comparison, just return whether each
03446     // index is zero or not.
03447     if (Cond == Instruction::SetEQ || Cond == Instruction::SetNE) {
03448       Instruction *InVal = 0;
03449       gep_type_iterator GTI = gep_type_begin(GEPLHS);
03450       for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) {
03451         bool EmitIt = true;
03452         if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) {
03453           if (isa<UndefValue>(C))  // undef index -> undef.
03454             return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
03455           if (C->isNullValue())
03456             EmitIt = false;
03457           else if (TD->getTypeSize(GTI.getIndexedType()) == 0) {
03458             EmitIt = false;  // This is indexing into a zero sized array?
03459           } else if (isa<ConstantInt>(C))
03460             return ReplaceInstUsesWith(I, // No comparison is needed here.
03461                                  ConstantBool::get(Cond == Instruction::SetNE));
03462         }
03463 
03464         if (EmitIt) {
03465           Instruction *Comp =
03466             new SetCondInst(Cond, GEPLHS->getOperand(i),
03467                     Constant::getNullValue(GEPLHS->getOperand(i)->getType()));
03468           if (InVal == 0)
03469             InVal = Comp;
03470           else {
03471             InVal = InsertNewInstBefore(InVal, I);
03472             InsertNewInstBefore(Comp, I);
03473             if (Cond == Instruction::SetNE)   // True if any are unequal
03474               InVal = BinaryOperator::createOr(InVal, Comp);
03475             else                              // True if all are equal
03476               InVal = BinaryOperator::createAnd(InVal, Comp);
03477           }
03478         }
03479       }
03480 
03481       if (InVal)
03482         return InVal;
03483       else
03484         ReplaceInstUsesWith(I, // No comparison is needed here, all indexes = 0
03485                             ConstantBool::get(Cond == Instruction::SetEQ));
03486     }
03487 
03488     // Only lower this if the setcc is the only user of the GEP or if we expect
03489     // the result to fold to a constant!
03490     if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) {
03491       // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0).
03492       Value *Offset = EmitGEPOffset(GEPLHS, I, *this);
03493       return new SetCondInst(Cond, Offset,
03494                              Constant::getNullValue(Offset->getType()));
03495     }
03496   } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) {
03497     // If the base pointers are different, but the indices are the same, just
03498     // compare the base pointer.
03499     if (PtrBase != GEPRHS->getOperand(0)) {
03500       bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
03501       IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
03502                         GEPRHS->getOperand(0)->getType();
03503       if (IndicesTheSame)
03504         for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
03505           if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
03506             IndicesTheSame = false;
03507             break;
03508           }
03509 
03510       // If all indices are the same, just compare the base pointers.
03511       if (IndicesTheSame)
03512         return new SetCondInst(Cond, GEPLHS->getOperand(0),
03513                                GEPRHS->getOperand(0));
03514 
03515       // Otherwise, the base pointers are different and the indices are
03516       // different, bail out.
03517       return 0;
03518     }
03519 
03520     // If one of the GEPs has all zero indices, recurse.
03521     bool AllZeros = true;
03522     for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
03523       if (!isa<Constant>(GEPLHS->getOperand(i)) ||
03524           !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
03525         AllZeros = false;
03526         break;
03527       }
03528     if (AllZeros)
03529       return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0),
03530                           SetCondInst::getSwappedCondition(Cond), I);
03531 
03532     // If the other GEP has all zero indices, recurse.
03533     AllZeros = true;
03534     for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
03535       if (!isa<Constant>(GEPRHS->getOperand(i)) ||
03536           !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
03537         AllZeros = false;
03538         break;
03539       }
03540     if (AllZeros)
03541       return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I);
03542 
03543     if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
03544       // If the GEPs only differ by one index, compare it.
03545       unsigned NumDifferences = 0;  // Keep track of # differences.
03546       unsigned DiffOperand = 0;     // The operand that differs.
03547       for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
03548         if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
03549           if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
03550                    GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
03551             // Irreconcilable differences.
03552             NumDifferences = 2;
03553             break;
03554           } else {
03555             if (NumDifferences++) break;
03556             DiffOperand = i;
03557           }
03558         }
03559 
03560       if (NumDifferences == 0)   // SAME GEP?
03561         return ReplaceInstUsesWith(I, // No comparison is needed here.
03562                                  ConstantBool::get(Cond == Instruction::SetEQ));
03563       else if (NumDifferences == 1) {
03564         Value *LHSV = GEPLHS->getOperand(DiffOperand);
03565         Value *RHSV = GEPRHS->getOperand(DiffOperand);
03566 
03567         // Convert the operands to signed values to make sure to perform a
03568         // signed comparison.
03569         const Type *NewTy = LHSV->getType()->getSignedVersion();
03570         if (LHSV->getType() != NewTy)
03571           LHSV = InsertNewInstBefore(new CastInst(LHSV, NewTy,
03572                                                   LHSV->getName()), I);
03573         if (RHSV->getType() != NewTy)
03574           RHSV = InsertNewInstBefore(new CastInst(RHSV, NewTy,
03575                                                   RHSV->getName()), I);
03576         return new SetCondInst(Cond, LHSV, RHSV);
03577       }
03578     }
03579 
03580     // Only lower this if the setcc is the only user of the GEP or if we expect
03581     // the result to fold to a constant!
03582     if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
03583         (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
03584       // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)  --->  (OFFSET1 cmp OFFSET2)
03585       Value *L = EmitGEPOffset(GEPLHS, I, *this);
03586       Value *R = EmitGEPOffset(GEPRHS, I, *this);
03587       return new SetCondInst(Cond, L, R);
03588     }
03589   }
03590   return 0;
03591 }
03592 
03593 
03594 Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
03595   bool Changed = SimplifyCommutative(I);
03596   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
03597   const Type *Ty = Op0->getType();
03598 
03599   // setcc X, X
03600   if (Op0 == Op1)
03601     return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
03602 
03603   if (isa<UndefValue>(Op1))                  // X setcc undef -> undef
03604     return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy));
03605 
03606   // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
03607   // addresses never equal each other!  We already know that Op0 != Op1.
03608   if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) ||
03609        isa<ConstantPointerNull>(Op0)) &&
03610       (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
03611        isa<ConstantPointerNull>(Op1)))
03612     return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
03613 
03614   // setcc's with boolean values can always be turned into bitwise operations
03615   if (Ty == Type::BoolTy) {
03616     switch (I.getOpcode()) {
03617     default: assert(0 && "Invalid setcc instruction!");
03618     case Instruction::SetEQ: {     //  seteq bool %A, %B -> ~(A^B)
03619       Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp");
03620       InsertNewInstBefore(Xor, I);
03621       return BinaryOperator::createNot(Xor);
03622     }
03623     case Instruction::SetNE:
03624       return BinaryOperator::createXor(Op0, Op1);
03625 
03626     case Instruction::SetGT:
03627       std::swap(Op0, Op1);                   // Change setgt -> setlt
03628       // FALL THROUGH
03629     case Instruction::SetLT: {               // setlt bool A, B -> ~X & Y
03630       Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
03631       InsertNewInstBefore(Not, I);
03632       return BinaryOperator::createAnd(Not, Op1);
03633     }
03634     case Instruction::SetGE:
03635       std::swap(Op0, Op1);                   // Change setge -> setle
03636       // FALL THROUGH
03637     case Instruction::SetLE: {     //  setle bool %A, %B -> ~A | B
03638       Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
03639       InsertNewInstBefore(Not, I);
03640       return BinaryOperator::createOr(Not, Op1);
03641     }
03642     }
03643   }
03644 
03645   // See if we are doing a comparison between a constant and an instruction that
03646   // can be folded into the comparison.
03647   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
03648     // Check to see if we are comparing against the minimum or maximum value...
03649     if (CI->isMinValue()) {
03650       if (I.getOpcode() == Instruction::SetLT)       // A < MIN -> FALSE
03651         return ReplaceInstUsesWith(I, ConstantBool::False);
03652       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN -> TRUE
03653         return ReplaceInstUsesWith(I, ConstantBool::True);
03654       if (I.getOpcode() == Instruction::SetLE)       // A <= MIN -> A == MIN
03655         return BinaryOperator::createSetEQ(Op0, Op1);
03656       if (I.getOpcode() == Instruction::SetGT)       // A > MIN -> A != MIN
03657         return BinaryOperator::createSetNE(Op0, Op1);
03658 
03659     } else if (CI->isMaxValue()) {
03660       if (I.getOpcode() == Instruction::SetGT)       // A > MAX -> FALSE
03661         return ReplaceInstUsesWith(I, ConstantBool::False);
03662       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX -> TRUE
03663         return ReplaceInstUsesWith(I, ConstantBool::True);
03664       if (I.getOpcode() == Instruction::SetGE)       // A >= MAX -> A == MAX
03665         return BinaryOperator::createSetEQ(Op0, Op1);
03666       if (I.getOpcode() == Instruction::SetLT)       // A < MAX -> A != MAX
03667         return BinaryOperator::createSetNE(Op0, Op1);
03668 
03669       // Comparing against a value really close to min or max?
03670     } else if (isMinValuePlusOne(CI)) {
03671       if (I.getOpcode() == Instruction::SetLT)       // A < MIN+1 -> A == MIN
03672         return BinaryOperator::createSetEQ(Op0, SubOne(CI));
03673       if (I.getOpcode() == Instruction::SetGE)       // A >= MIN-1 -> A != MIN
03674         return BinaryOperator::createSetNE(Op0, SubOne(CI));
03675 
03676     } else if (isMaxValueMinusOne(CI)) {
03677       if (I.getOpcode() == Instruction::SetGT)       // A > MAX-1 -> A == MAX
03678         return BinaryOperator::createSetEQ(Op0, AddOne(CI));
03679       if (I.getOpcode() == Instruction::SetLE)       // A <= MAX-1 -> A != MAX
03680         return BinaryOperator::createSetNE(Op0, AddOne(CI));
03681     }
03682 
03683     // If we still have a setle or setge instruction, turn it into the
03684     // appropriate setlt or setgt instruction.  Since the border cases have
03685     // already been handled above, this requires little checking.
03686     //
03687     if (I.getOpcode() == Instruction::SetLE)
03688       return BinaryOperator::createSetLT(Op0, AddOne(CI));
03689     if (I.getOpcode() == Instruction::SetGE)
03690       return BinaryOperator::createSetGT(Op0, SubOne(CI));
03691 
03692     
03693     // See if we can fold the comparison based on bits known to be zero or one
03694     // in the input.
03695     uint64_t KnownZero, KnownOne;
03696     if (SimplifyDemandedBits(Op0, Ty->getIntegralTypeMask(),
03697                              KnownZero, KnownOne, 0))
03698       return &I;
03699         
03700     // Given the known and unknown bits, compute a range that the LHS could be
03701     // in.
03702     if (KnownOne | KnownZero) {
03703       if (Ty->isUnsigned()) {   // Unsigned comparison.
03704         uint64_t Min, Max;
03705         uint64_t RHSVal = CI->getZExtValue();
03706         ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne,
03707                                                  Min, Max);
03708         switch (I.getOpcode()) {  // LE/GE have been folded already.
03709         default: assert(0 && "Unknown setcc opcode!");
03710         case Instruction::SetEQ:
03711           if (Max < RHSVal || Min > RHSVal)
03712             return ReplaceInstUsesWith(I, ConstantBool::False);
03713           break;
03714         case Instruction::SetNE:
03715           if (Max < RHSVal || Min > RHSVal)
03716             return ReplaceInstUsesWith(I, ConstantBool::True);
03717           break;
03718         case Instruction::SetLT:
03719           if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True);
03720           if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False);
03721           break;
03722         case Instruction::SetGT:
03723           if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True);
03724           if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False);
03725           break;
03726         }
03727       } else {              // Signed comparison.
03728         int64_t Min, Max;
03729         int64_t RHSVal = CI->getSExtValue();
03730         ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne,
03731                                                Min, Max);
03732         switch (I.getOpcode()) {  // LE/GE have been folded already.
03733         default: assert(0 && "Unknown setcc opcode!");
03734         case Instruction::SetEQ:
03735           if (Max < RHSVal || Min > RHSVal)
03736             return ReplaceInstUsesWith(I, ConstantBool::False);
03737           break;
03738         case Instruction::SetNE:
03739           if (Max < RHSVal || Min > RHSVal)
03740             return ReplaceInstUsesWith(I, ConstantBool::True);
03741           break;
03742         case Instruction::SetLT:
03743           if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True);
03744           if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False);
03745           break;
03746         case Instruction::SetGT:
03747           if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True);
03748           if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False);
03749           break;
03750         }
03751       }
03752     }
03753           
03754     
03755     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
03756       switch (LHSI->getOpcode()) {
03757       case Instruction::And:
03758         if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
03759             LHSI->getOperand(0)->hasOneUse()) {
03760           // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
03761           // could exist), turn it into (X & (C2 << C1)) != (C3 << C1).  This
03762           // happens a LOT in code produced by the C front-end, for bitfield
03763           // access.
03764           ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0));
03765           ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
03766 
03767           // Check to see if there is a noop-cast between the shift and the and.
03768           if (!Shift) {
03769             if (CastInst *CI = dyn_cast<CastInst>(LHSI->getOperand(0)))
03770               if (CI->getOperand(0)->getType()->isIntegral() &&
03771                   CI->getOperand(0)->getType()->getPrimitiveSizeInBits() ==
03772                      CI->getType()->getPrimitiveSizeInBits())
03773                 Shift = dyn_cast<ShiftInst>(CI->getOperand(0));
03774           }
03775           
03776           ConstantUInt *ShAmt;
03777           ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0;
03778           const Type *Ty = Shift ? Shift->getType() : 0;  // Type of the shift.
03779           const Type *AndTy = AndCST->getType();          // Type of the and.
03780 
03781           // We can fold this as long as we can't shift unknown bits
03782           // into the mask.  This can only happen with signed shift
03783           // rights, as they sign-extend.
03784           if (ShAmt) {
03785             bool CanFold = Shift->getOpcode() != Instruction::Shr ||
03786                            Ty->isUnsigned();
03787             if (!CanFold) {
03788               // To test for the bad case of the signed shr, see if any
03789               // of the bits shifted in could be tested after the mask.
03790               int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getValue();
03791               if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift.
03792 
03793               Constant *OShAmt = ConstantUInt::get(Type::UByteTy, ShAmtVal);
03794               Constant *ShVal =
03795                 ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), 
03796                                      OShAmt);
03797               if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue())
03798                 CanFold = true;
03799             }
03800 
03801             if (CanFold) {
03802               Constant *NewCst;
03803               if (Shift->getOpcode() == Instruction::Shl)
03804                 NewCst = ConstantExpr::getUShr(CI, ShAmt);
03805               else
03806                 NewCst = ConstantExpr::getShl(CI, ShAmt);
03807 
03808               // Check to see if we are shifting out any of the bits being
03809               // compared.
03810               if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){
03811                 // If we shifted bits out, the fold is not going to work out.
03812                 // As a special case, check to see if this means that the
03813                 // result is always true or false now.
03814                 if (I.getOpcode() == Instruction::SetEQ)
03815                   return ReplaceInstUsesWith(I, ConstantBool::False);
03816                 if (I.getOpcode() == Instruction::SetNE)
03817                   return ReplaceInstUsesWith(I, ConstantBool::True);
03818               } else {
03819                 I.setOperand(1, NewCst);
03820                 Constant *NewAndCST;
03821                 if (Shift->getOpcode() == Instruction::Shl)
03822                   NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt);
03823                 else
03824                   NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
03825                 LHSI->setOperand(1, NewAndCST);
03826                 if (AndTy == Ty) 
03827                   LHSI->setOperand(0, Shift->getOperand(0));
03828                 else {
03829                   Value *NewCast = InsertCastBefore(Shift->getOperand(0), AndTy,
03830                                                     *Shift);
03831                   LHSI->setOperand(0, NewCast);
03832                 }
03833                 WorkList.push_back(Shift); // Shift is dead.
03834                 AddUsesToWorkList(I);
03835                 return &I;
03836               }
03837             }
03838           }
03839         }
03840         break;
03841 
03842       case Instruction::Shl:         // (setcc (shl X, ShAmt), CI)
03843         if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) {
03844           switch (I.getOpcode()) {
03845           default: break;
03846           case Instruction::SetEQ:
03847           case Instruction::SetNE: {
03848             unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits();
03849 
03850             // Check that the shift amount is in range.  If not, don't perform
03851             // undefined shifts.  When the shift is visited it will be
03852             // simplified.
03853             if (ShAmt->getValue() >= TypeBits)
03854               break;
03855 
03856             // If we are comparing against bits always shifted out, the
03857             // comparison cannot succeed.
03858             Constant *Comp =
03859               ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt);
03860             if (Comp != CI) {// Comparing against a bit that we know is zero.
03861               bool IsSetNE = I.getOpcode() == Instruction::SetNE;
03862               Constant *Cst = ConstantBool::get(IsSetNE);
03863               return ReplaceInstUsesWith(I, Cst);
03864             }
03865 
03866             if (LHSI->hasOneUse()) {
03867               // Otherwise strength reduce the shift into an and.
03868               unsigned ShAmtVal = (unsigned)ShAmt->getValue();
03869               uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1;
03870 
03871               Constant *Mask;
03872               if (CI->getType()->isUnsigned()) {
03873                 Mask = ConstantUInt::get(CI->getType(), Val);
03874               } else if (ShAmtVal != 0) {
03875                 Mask = ConstantSInt::get(CI->getType(), Val);
03876               } else {
03877                 Mask = ConstantInt::getAllOnesValue(CI->getType());
03878               }
03879 
03880               Instruction *AndI =
03881                 BinaryOperator::createAnd(LHSI->getOperand(0),
03882                                           Mask, LHSI->getName()+".mask");
03883               Value *And = InsertNewInstBefore(AndI, I);
03884               return new SetCondInst(I.getOpcode(), And,
03885                                      ConstantExpr::getUShr(CI, ShAmt));
03886             }
03887           }
03888           }
03889         }
03890         break;
03891 
03892       case Instruction::Shr:         // (setcc (shr X, ShAmt), CI)
03893         if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) {
03894           switch (I.getOpcode()) {
03895           default: break;
03896           case Instruction::SetEQ:
03897           case Instruction::SetNE: {
03898 
03899             // Check that the shift amount is in range.  If not, don't perform
03900             // undefined shifts.  When the shift is visited it will be
03901             // simplified.
03902             unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits();
03903             if (ShAmt->getValue() >= TypeBits)
03904               break;
03905 
03906             // If we are comparing against bits always shifted out, the
03907             // comparison cannot succeed.
03908             Constant *Comp =
03909               ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt);
03910 
03911             if (Comp != CI) {// Comparing against a bit that we know is zero.
03912               bool IsSetNE = I.getOpcode() == Instruction::SetNE;
03913               Constant *Cst = ConstantBool::get(IsSetNE);
03914               return ReplaceInstUsesWith(I, Cst);
03915             }
03916 
03917             if (LHSI->hasOneUse() || CI->isNullValue()) {
03918               unsigned ShAmtVal = (unsigned)ShAmt->getValue();
03919 
03920               // Otherwise strength reduce the shift into an and.
03921               uint64_t Val = ~0ULL;          // All ones.
03922               Val <<= ShAmtVal;              // Shift over to the right spot.
03923 
03924               Constant *Mask;
03925               if (CI->getType()->isUnsigned()) {
03926                 Val &= ~0ULL >> (64-TypeBits);
03927                 Mask = ConstantUInt::get(CI->getType(), Val);
03928               } else {
03929                 Mask = ConstantSInt::get(CI->getType(), Val);
03930               }
03931 
03932               Instruction *AndI =
03933                 BinaryOperator::createAnd(LHSI->getOperand(0),
03934                                           Mask, LHSI->getName()+".mask");
03935               Value *And = InsertNewInstBefore(AndI, I);
03936               return new SetCondInst(I.getOpcode(), And,
03937                                      ConstantExpr::getShl(CI, ShAmt));
03938             }
03939             break;
03940           }
03941           }
03942         }
03943         break;
03944 
03945       case Instruction::Div:
03946         // Fold: (div X, C1) op C2 -> range check
03947         if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
03948           // Fold this div into the comparison, producing a range check.
03949           // Determine, based on the divide type, what the range is being
03950           // checked.  If there is an overflow on the low or high side, remember
03951           // it, otherwise compute the range [low, hi) bounding the new value.
03952           bool LoOverflow = false, HiOverflow = 0;
03953           ConstantInt *LoBound = 0, *HiBound = 0;
03954 
03955           ConstantInt *Prod;
03956           bool ProdOV = MulWithOverflow(Prod, CI, DivRHS);
03957 
03958           Instruction::BinaryOps Opcode = I.getOpcode();
03959 
03960           if (DivRHS->isNullValue()) {  // Don't hack on divide by zeros.
03961           } else if (LHSI->getType()->isUnsigned()) {  // udiv
03962             LoBound = Prod;
03963             LoOverflow = ProdOV;
03964             HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS);
03965           } else if (isPositive(DivRHS)) {             // Divisor is > 0.
03966             if (CI->isNullValue()) {       // (X / pos) op 0
03967               // Can't overflow.
03968               LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
03969               HiBound = DivRHS;
03970             } else if (isPositive(CI)) {   // (X / pos) op pos
03971               LoBound = Prod;
03972               LoOverflow = ProdOV;
03973               HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS);
03974             } else {                       // (X / pos) op neg
03975               Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS));
03976               LoOverflow = AddWithOverflow(LoBound, Prod,
03977                                            cast<ConstantInt>(DivRHSH));
03978               HiBound = Prod;
03979               HiOverflow = ProdOV;
03980             }
03981           } else {                                     // Divisor is < 0.
03982             if (CI->isNullValue()) {       // (X / neg) op 0
03983               LoBound = AddOne(DivRHS);
03984               HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
03985               if (HiBound == DivRHS)
03986                 LoBound = 0;  // - INTMIN = INTMIN
03987             } else if (isPositive(CI)) {   // (X / neg) op pos
03988               HiOverflow = LoOverflow = ProdOV;
03989               if (!LoOverflow)
03990                 LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS));
03991               HiBound = AddOne(Prod);
03992             } else {                       // (X / neg) op neg
03993               LoBound = Prod;
03994               LoOverflow = HiOverflow = ProdOV;
03995               HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS));
03996             }
03997 
03998             // Dividing by a negate swaps the condition.
03999             Opcode = SetCondInst::getSwappedCondition(Opcode);
04000           }
04001 
04002           if (LoBound) {
04003             Value *X = LHSI->getOperand(0);
04004             switch (Opcode) {
04005             default: assert(0 && "Unhandled setcc opcode!");
04006             case Instruction::SetEQ:
04007               if (LoOverflow && HiOverflow)
04008                 return ReplaceInstUsesWith(I, ConstantBool::False);
04009               else if (HiOverflow)
04010                 return new SetCondInst(Instruction::SetGE, X, LoBound);
04011               else if (LoOverflow)
04012                 return new SetCondInst(Instruction::SetLT, X, HiBound);
04013               else
04014                 return InsertRangeTest(X, LoBound, HiBound, true, I);
04015             case Instruction::SetNE:
04016               if (LoOverflow && HiOverflow)
04017                 return ReplaceInstUsesWith(I, ConstantBool::True);
04018               else if (HiOverflow)
04019                 return new SetCondInst(Instruction::SetLT, X, LoBound);
04020               else if (LoOverflow)
04021                 return new SetCondInst(Instruction::SetGE, X, HiBound);
04022               else
04023                 return InsertRangeTest(X, LoBound, HiBound, false, I);
04024             case Instruction::SetLT:
04025               if (LoOverflow)
04026                 return ReplaceInstUsesWith(I, ConstantBool::False);
04027               return new SetCondInst(Instruction::SetLT, X, LoBound);
04028             case Instruction::SetGT:
04029               if (HiOverflow)
04030                 return ReplaceInstUsesWith(I, ConstantBool::False);
04031               return new SetCondInst(Instruction::SetGE, X, HiBound);
04032             }
04033           }
04034         }
04035         break;
04036       }
04037 
04038     // Simplify seteq and setne instructions...
04039     if (I.getOpcode() == Instruction::SetEQ ||
04040         I.getOpcode() == Instruction::SetNE) {
04041       bool isSetNE = I.getOpcode() == Instruction::SetNE;
04042 
04043       // If the first operand is (and|or|xor) with a constant, and the second
04044       // operand is a constant, simplify a bit.
04045       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) {
04046         switch (BO->getOpcode()) {
04047         case Instruction::Rem:
04048           // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
04049           if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) &&
04050               BO->hasOneUse() &&
04051               cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) {
04052             int64_t V = cast<ConstantSInt>(BO->getOperand(1))->getValue();
04053             if (isPowerOf2_64(V)) {
04054               unsigned L2 = Log2_64(V);
04055               const Type *UTy = BO->getType()->getUnsignedVersion();
04056               Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0),
04057                                                              UTy, "tmp"), I);
04058               Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2);
04059               Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX,
04060                                                     RHSCst, BO->getName()), I);
04061               return BinaryOperator::create(I.getOpcode(), NewRem,
04062                                             Constant::getNullValue(UTy));
04063             }
04064           }
04065           break;
04066 
04067         case Instruction::Add:
04068           // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
04069           if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
04070             if (BO->hasOneUse())
04071               return new SetCondInst(I.getOpcode(), BO->getOperand(0),
04072                                      ConstantExpr::getSub(CI, BOp1C));
04073           } else if (CI->isNullValue()) {
04074             // Replace ((add A, B) != 0) with (A != -B) if A or B is
04075             // efficiently invertible, or if the add has just this one use.
04076             Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
04077 
04078             if (Value *NegVal = dyn_castNegVal(BOp1))
04079               return new SetCondInst(I.getOpcode(), BOp0, NegVal);
04080             else if (Value *NegVal = dyn_castNegVal(BOp0))
04081               return new SetCondInst(I.getOpcode(), NegVal, BOp1);
04082             else if (BO->hasOneUse()) {
04083               Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName());
04084               BO->setName("");
04085               InsertNewInstBefore(Neg, I);
04086               return new SetCondInst(I.getOpcode(), BOp0, Neg);
04087             }
04088           }
04089           break;
04090         case Instruction::Xor:
04091           // For the xor case, we can xor two constants together, eliminating
04092           // the explicit xor.
04093           if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
04094             return BinaryOperator::create(I.getOpcode(), BO->getOperand(0),
04095                                   ConstantExpr::getXor(CI, BOC));
04096 
04097           // FALLTHROUGH
04098         case Instruction::Sub:
04099           // Replace (([sub|xor] A, B) != 0) with (A != B)
04100           if (CI->isNullValue())
04101             return new SetCondInst(I.getOpcode(), BO->getOperand(0),
04102                                    BO->getOperand(1));
04103           break;
04104 
04105         case Instruction::Or:
04106           // If bits are being or'd in that are not present in the constant we
04107           // are comparing against, then the comparison could never succeed!
04108           if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
04109             Constant *NotCI = ConstantExpr::getNot(CI);
04110             if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
04111               return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE));
04112           }
04113           break;
04114 
04115         case Instruction::And:
04116           if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
04117             // If bits are being compared against that are and'd out, then the
04118             // comparison can never succeed!
04119             if (!ConstantExpr::getAnd(CI,
04120                                       ConstantExpr::getNot(BOC))->isNullValue())
04121               return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE));
04122 
04123             // If we have ((X & C) == C), turn it into ((X & C) != 0).
04124             if (CI == BOC && isOneBitSet(CI))
04125               return new SetCondInst(isSetNE ? Instruction::SetEQ :
04126                                      Instruction::SetNE, Op0,
04127                                      Constant::getNullValue(CI->getType()));
04128 
04129             // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X
04130             // to be a signed value as appropriate.
04131             if (isSignBit(BOC)) {
04132               Value *X = BO->getOperand(0);
04133               // If 'X' is not signed, insert a cast now...
04134               if (!BOC->getType()->isSigned()) {
04135                 const Type *DestTy = BOC->getType()->getSignedVersion();
04136                 X = InsertCastBefore(X, DestTy, I);
04137               }
04138               return new SetCondInst(isSetNE ? Instruction::SetLT :
04139                                          Instruction::SetGE, X,
04140                                      Constant::getNullValue(X->getType()));
04141             }
04142 
04143             // ((X & ~7) == 0) --> X < 8
04144             if (CI->isNullValue() && isHighOnes(BOC)) {
04145               Value *X = BO->getOperand(0);
04146               Constant *NegX = ConstantExpr::getNeg(BOC);
04147 
04148               // If 'X' is signed, insert a cast now.
04149               if (NegX->getType()->isSigned()) {
04150                 const Type *DestTy = NegX->getType()->getUnsignedVersion();
04151                 X = InsertCastBefore(X, DestTy, I);
04152                 NegX = ConstantExpr::getCast(NegX, DestTy);
04153               }
04154 
04155               return new SetCondInst(isSetNE ? Instruction::SetGE :
04156                                      Instruction::SetLT, X, NegX);
04157             }
04158 
04159           }
04160         default: break;
04161         }
04162       }
04163     } else {  // Not a SetEQ/SetNE
04164       // If the LHS is a cast from an integral value of the same size,
04165       if (CastInst *Cast = dyn_cast<CastInst>(Op0)) {
04166         Value *CastOp = Cast->getOperand(0);
04167         const Type *SrcTy = CastOp->getType();
04168         unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits();
04169         if (SrcTy != Cast->getType() && SrcTy->isInteger() &&
04170             SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) {
04171           assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) &&
04172                  "Source and destination signednesses should differ!");
04173           if (Cast->getType()->isSigned()) {
04174             // If this is a signed comparison, check for comparisons in the
04175             // vicinity of zero.
04176             if (I.getOpcode() == Instruction::SetLT && CI->isNullValue())
04177               // X < 0  => x > 127
04178               return BinaryOperator::createSetGT(CastOp,
04179                          ConstantUInt::get(SrcTy, (1ULL << (SrcTySize-1))-1));
04180             else if (I.getOpcode() == Instruction::SetGT &&
04181                      cast<ConstantSInt>(CI)->getValue() == -1)
04182               // X > -1  => x < 128
04183               return BinaryOperator::createSetLT(CastOp,
04184                          ConstantUInt::get(SrcTy, 1ULL << (SrcTySize-1)));
04185           } else {
04186             ConstantUInt *CUI = cast<ConstantUInt>(CI);
04187             if (I.getOpcode() == Instruction::SetLT &&
04188                 CUI->getValue() == 1ULL << (SrcTySize-1))
04189               // X < 128 => X > -1
04190               return BinaryOperator::createSetGT(CastOp,
04191                                                  ConstantSInt::get(SrcTy, -1));
04192             else if (I.getOpcode() == Instruction::SetGT &&
04193                      CUI->getValue() == (1ULL << (SrcTySize-1))-1)
04194               // X > 127 => X < 0
04195               return BinaryOperator::createSetLT(CastOp,
04196                                                  Constant::getNullValue(SrcTy));
04197           }
04198         }
04199       }
04200     }
04201   }
04202 
04203   // Handle setcc with constant RHS's that can be integer, FP or pointer.
04204   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
04205     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
04206       switch (LHSI->getOpcode()) {
04207       case Instruction::GetElementPtr:
04208         if (RHSC->isNullValue()) {
04209           // Transform setcc GEP P, int 0, int 0, int 0, null -> setcc P, null
04210           bool isAllZeros = true;
04211           for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i)
04212             if (!isa<Constant>(LHSI->getOperand(i)) ||
04213                 !cast<Constant>(LHSI->getOperand(i))->isNullValue()) {
04214               isAllZeros = false;
04215               break;
04216             }
04217           if (isAllZeros)
04218             return new SetCondInst(I.getOpcode(), LHSI->getOperand(0),
04219                     Constant::getNullValue(LHSI->getOperand(0)->getType()));
04220         }
04221         break;
04222 
04223       case Instruction::PHI:
04224         if (Instruction *NV = FoldOpIntoPhi(I))
04225           return NV;
04226         break;
04227       case Instruction::Select:
04228         // If either operand of the select is a constant, we can fold the
04229         // comparison into the select arms, which will cause one to be
04230         // constant folded and the select turned into a bitwise or.
04231         Value *Op1 = 0, *Op2 = 0;
04232         if (LHSI->hasOneUse()) {
04233           if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
04234             // Fold the known value into the constant operand.
04235             Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC);
04236             // Insert a new SetCC of the other select operand.
04237             Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
04238                                                       LHSI->getOperand(2), RHSC,
04239                                                       I.getName()), I);
04240           } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
04241             // Fold the known value into the constant operand.
04242             Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC);
04243             // Insert a new SetCC of the other select operand.
04244             Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(),
04245                                                       LHSI->getOperand(1), RHSC,
04246                                                       I.getName()), I);
04247           }
04248         }
04249 
04250         if (Op1)
04251           return new SelectInst(LHSI->getOperand(0), Op1, Op2);
04252         break;
04253       }
04254   }
04255 
04256   // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now.
04257   if (User *GEP = dyn_castGetElementPtr(Op0))
04258     if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I))
04259       return NI;
04260   if (User *GEP = dyn_castGetElementPtr(Op1))
04261     if (Instruction *NI = FoldGEPSetCC(GEP, Op0,
04262                            SetCondInst::getSwappedCondition(I.getOpcode()), I))
04263       return NI;
04264 
04265   // Test to see if the operands of the setcc are casted versions of other
04266   // values.  If the cast can be stripped off both arguments, we do so now.
04267   if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
04268     Value *CastOp0 = CI->getOperand(0);
04269     if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) &&
04270         (isa<Constant>(Op1) || isa<CastInst>(Op1)) &&
04271         (I.getOpcode() == Instruction::SetEQ ||
04272          I.getOpcode() == Instruction::SetNE)) {
04273       // We keep moving the cast from the left operand over to the right
04274       // operand, where it can often be eliminated completely.
04275       Op0 = CastOp0;
04276 
04277       // If operand #1 is a cast instruction, see if we can eliminate it as
04278       // well.
04279       if (CastInst *CI2 = dyn_cast<CastInst>(Op1))
04280         if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo(
04281                                                                Op0->getType()))
04282           Op1 = CI2->getOperand(0);
04283 
04284       // If Op1 is a constant, we can fold the cast into the constant.
04285       if (Op1->getType() != Op0->getType())
04286         if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
04287           Op1 = ConstantExpr::getCast(Op1C, Op0->getType());
04288         } else {
04289           // Otherwise, cast the RHS right before the setcc
04290           Op1 = new CastInst(Op1, Op0->getType(), Op1->getName());
04291           InsertNewInstBefore(cast<Instruction>(Op1), I);
04292         }
04293       return BinaryOperator::create(I.getOpcode(), Op0, Op1);
04294     }
04295 
04296     // Handle the special case of: setcc (cast bool to X), <cst>
04297     // This comes up when you have code like
04298     //   int X = A < B;
04299     //   if (X) ...
04300     // For generality, we handle any zero-extension of any operand comparison
04301     // with a constant or another cast from the same type.
04302     if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1))
04303       if (Instruction *R = visitSetCondInstWithCastAndCast(I))
04304         return R;
04305   }
04306   
04307   if (I.getOpcode() == Instruction::SetNE ||
04308       I.getOpcode() == Instruction::SetEQ) {
04309     Value *A, *B;
04310     if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
04311         (A == Op1 || B == Op1)) {
04312       // (A^B) == A  ->  B == 0
04313       Value *OtherVal = A == Op1 ? B : A;
04314       return BinaryOperator::create(I.getOpcode(), OtherVal,
04315                                     Constant::getNullValue(A->getType()));
04316     } else if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
04317                (A == Op0 || B == Op0)) {
04318       // A == (A^B)  ->  B == 0
04319       Value *OtherVal = A == Op0 ? B : A;
04320       return BinaryOperator::create(I.getOpcode(), OtherVal,
04321                                     Constant::getNullValue(A->getType()));
04322     } else if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) {
04323       // (A-B) == A  ->  B == 0
04324       return BinaryOperator::create(I.getOpcode(), B,
04325                                     Constant::getNullValue(B->getType()));
04326     } else if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) {
04327       // A == (A-B)  ->  B == 0
04328       return BinaryOperator::create(I.getOpcode(), B,
04329                                     Constant::getNullValue(B->getType()));
04330     }
04331   }
04332   return Changed ? &I : 0;
04333 }
04334 
04335 // visitSetCondInstWithCastAndCast - Handle setcond (cast x to y), (cast/cst).
04336 // We only handle extending casts so far.
04337 //
04338 Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) {
04339   Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0);
04340   const Type *SrcTy = LHSCIOp->getType();
04341   const Type *DestTy = SCI.getOperand(0)->getType();
04342   Value *RHSCIOp;
04343 
04344   if (!DestTy->isIntegral() || !SrcTy->isIntegral())
04345     return 0;
04346 
04347   unsigned SrcBits  = SrcTy->getPrimitiveSizeInBits();
04348   unsigned DestBits = DestTy->getPrimitiveSizeInBits();
04349   if (SrcBits >= DestBits) return 0;  // Only handle extending cast.
04350 
04351   // Is this a sign or zero extension?
04352   bool isSignSrc  = SrcTy->isSigned();
04353   bool isSignDest = DestTy->isSigned();
04354 
04355   if (CastInst *CI = dyn_cast<CastInst>(SCI.getOperand(1))) {
04356     // Not an extension from the same type?
04357     RHSCIOp = CI->getOperand(0);
04358     if (RHSCIOp->getType() != LHSCIOp->getType()) return 0;
04359   } else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) {
04360     // Compute the constant that would happen if we truncated to SrcTy then
04361     // reextended to DestTy.
04362     Constant *Res = ConstantExpr::getCast(CI, SrcTy);
04363 
04364     if (ConstantExpr::getCast(Res, DestTy) == CI) {
04365       RHSCIOp = Res;
04366     } else {
04367       // If the value cannot be represented in the shorter type, we cannot emit
04368       // a simple comparison.
04369       if (SCI.getOpcode() == Instruction::SetEQ)
04370         return ReplaceInstUsesWith(SCI, ConstantBool::False);
04371       if (SCI.getOpcode() == Instruction::SetNE)
04372         return ReplaceInstUsesWith(SCI, ConstantBool::True);
04373 
04374       // Evaluate the comparison for LT.
04375       Value *Result;
04376       if (DestTy->isSigned()) {
04377         // We're performing a signed comparison.
04378         if (isSignSrc) {
04379           // Signed extend and signed comparison.
04380           if (cast<ConstantSInt>(CI)->getValue() < 0) // X < (small) --> false
04381             Result = ConstantBool::False;
04382           else
04383             Result = ConstantBool::True;              // X < (large) --> true
04384         } else {
04385           // Unsigned extend and signed comparison.
04386           if (cast<ConstantSInt>(CI)->getValue() < 0)
04387             Result = ConstantBool::False;
04388           else
04389             Result = ConstantBool::True;
04390         }
04391       } else {
04392         // We're performing an unsigned comparison.
04393         if (!isSignSrc) {
04394           // Unsigned extend & compare -> always true.
04395           Result = ConstantBool::True;
04396         } else {
04397           // We're performing an unsigned comp with a sign extended value.
04398           // This is true if the input is >= 0. [aka >s -1]
04399           Constant *NegOne = ConstantIntegral::getAllOnesValue(SrcTy);
04400           Result = InsertNewInstBefore(BinaryOperator::createSetGT(LHSCIOp,
04401                                                   NegOne, SCI.getName()), SCI);
04402         }
04403       }
04404 
04405       // Finally, return the value computed.
04406       if (SCI.getOpcode() == Instruction::SetLT) {
04407         return ReplaceInstUsesWith(SCI, Result);
04408       } else {
04409         assert(SCI.getOpcode()==Instruction::SetGT &&"SetCC should be folded!");
04410         if (Constant *CI = dyn_cast<Constant>(Result))
04411           return ReplaceInstUsesWith(SCI, ConstantExpr::getNot(CI));
04412         else
04413           return BinaryOperator::createNot(Result);
04414       }
04415     }
04416   } else {
04417     return 0;
04418   }
04419 
04420   // Okay, just insert a compare of the reduced operands now!
04421   return BinaryOperator::create(SCI.getOpcode(), LHSCIOp, RHSCIOp);
04422 }
04423 
04424 Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
04425   assert(I.getOperand(1)->getType() == Type::UByteTy);
04426   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
04427   bool isLeftShift = I.getOpcode() == Instruction::Shl;
04428 
04429   // shl X, 0 == X and shr X, 0 == X
04430   // shl 0, X == 0 and shr 0, X == 0
04431   if (Op1 == Constant::getNullValue(Type::UByteTy) ||
04432       Op0 == Constant::getNullValue(Op0->getType()))
04433     return ReplaceInstUsesWith(I, Op0);
04434   
04435   if (isa<UndefValue>(Op0)) {            // undef >>s X -> undef
04436     if (!isLeftShift && I.getType()->isSigned())
04437       return ReplaceInstUsesWith(I, Op0);
04438     else                         // undef << X -> 0   AND  undef >>u X -> 0
04439       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
04440   }
04441   if (isa<UndefValue>(Op1)) {
04442     if (isLeftShift || I.getType()->isUnsigned())// X << undef, X >>u undef -> 0
04443       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
04444     else
04445       return ReplaceInstUsesWith(I, Op0);          // X >>s undef -> X
04446   }
04447 
04448   // shr int -1, X = -1   (for any arithmetic shift rights of ~0)
04449   if (!isLeftShift)
04450     if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
04451       if (CSI->isAllOnesValue())
04452         return ReplaceInstUsesWith(I, CSI);
04453 
04454   // Try to fold constant and into select arguments.
04455   if (isa<Constant>(Op0))
04456     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
04457       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
04458         return R;
04459 
04460   // See if we can turn a signed shr into an unsigned shr.
04461   if (!isLeftShift && I.getType()->isSigned()) {
04462     if (MaskedValueIsZero(Op0,
04463                           1ULL << (I.getType()->getPrimitiveSizeInBits()-1))) {
04464       Value *V = InsertCastBefore(Op0, I.getType()->getUnsignedVersion(), I);
04465       V = InsertNewInstBefore(new ShiftInst(Instruction::Shr, V, Op1,
04466                                             I.getName()), I);
04467       return new CastInst(V, I.getType());
04468     }
04469   }
04470 
04471   if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1))
04472     if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
04473       return Res;
04474   return 0;
04475 }
04476 
04477 Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantUInt *Op1,
04478                                                ShiftInst &I) {
04479   bool isLeftShift = I.getOpcode() == Instruction::Shl;
04480   bool isSignedShift = Op0->getType()->isSigned();
04481   bool isUnsignedShift = !isSignedShift;
04482 
04483   // See if we can simplify any instructions used by the instruction whose sole 
04484   // purpose is to compute bits we don't care about.
04485   uint64_t KnownZero, KnownOne;
04486   if (SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(),
04487                            KnownZero, KnownOne))
04488     return &I;
04489   
04490   // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr
04491   // of a signed value.
04492   //
04493   unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits();
04494   if (Op1->getValue() >= TypeBits) {
04495     if (isUnsignedShift || isLeftShift)
04496       return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
04497     else {
04498       I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1));
04499       return &I;
04500     }
04501   }
04502   
04503   // ((X*C1) << C2) == (X * (C1 << C2))
04504   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
04505     if (BO->getOpcode() == Instruction::Mul && isLeftShift)
04506       if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
04507         return BinaryOperator::createMul(BO->getOperand(0),
04508                                          ConstantExpr::getShl(BOOp, Op1));
04509   
04510   // Try to fold constant and into select arguments.
04511   if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
04512     if (Instruction *R = FoldOpIntoSelect(I, SI, this))
04513       return R;
04514   if (isa<PHINode>(Op0))
04515     if (Instruction *NV = FoldOpIntoPhi(I))
04516       return NV;
04517   
04518   if (Op0->hasOneUse()) {
04519     if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
04520       // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
04521       Value *V1, *V2;
04522       ConstantInt *CC;
04523       switch (Op0BO->getOpcode()) {
04524         default: break;
04525         case Instruction::Add:
04526         case Instruction::And:
04527         case Instruction::Or:
04528         case Instruction::Xor:
04529           // These operators commute.
04530           // Turn (Y + (X >> C)) << C  ->  (X + (Y << C)) & (~0 << C)
04531           if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
04532               match(Op0BO->getOperand(1),
04533                     m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
04534             Instruction *YS = new ShiftInst(Instruction::Shl, 
04535                                             Op0BO->getOperand(0), Op1,
04536                                             Op0BO->getName());
04537             InsertNewInstBefore(YS, I); // (Y << C)
04538             Instruction *X = 
04539               BinaryOperator::create(Op0BO->getOpcode(), YS, V1,
04540                                      Op0BO->getOperand(1)->getName());
04541             InsertNewInstBefore(X, I);  // (X + (Y << C))
04542             Constant *C2 = ConstantInt::getAllOnesValue(X->getType());
04543             C2 = ConstantExpr::getShl(C2, Op1);
04544             return BinaryOperator::createAnd(X, C2);
04545           }
04546           
04547           // Turn (Y + ((X >> C) & CC)) << C  ->  ((X & (CC << C)) + (Y << C))
04548           if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
04549               match(Op0BO->getOperand(1),
04550                     m_And(m_Shr(m_Value(V1), m_Value(V2)),
04551                           m_ConstantInt(CC))) && V2 == Op1 &&
04552       cast<BinaryOperator>(Op0BO->getOperand(1))->getOperand(0)->hasOneUse()) {
04553             Instruction *YS = new ShiftInst(Instruction::Shl, 
04554                                             Op0BO->getOperand(0), Op1,
04555                                             Op0BO->getName());
04556             InsertNewInstBefore(YS, I); // (Y << C)
04557             Instruction *XM =
04558               BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
04559                                         V1->getName()+".mask");
04560             InsertNewInstBefore(XM, I); // X & (CC << C)
04561             
04562             return BinaryOperator::create(Op0BO->getOpcode(), YS, XM);
04563           }
04564           
04565           // FALL THROUGH.
04566         case Instruction::Sub:
04567           // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
04568           if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
04569               match(Op0BO->getOperand(0),
04570                     m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
04571             Instruction *YS = new ShiftInst(Instruction::Shl, 
04572                                             Op0BO->getOperand(1), Op1,
04573                                             Op0BO->getName());
04574             InsertNewInstBefore(YS, I); // (Y << C)
04575             Instruction *X =
04576               BinaryOperator::create(Op0BO->getOpcode(), V1, YS,
04577                                      Op0BO->getOperand(0)->getName());
04578             InsertNewInstBefore(X, I);  // (X + (Y << C))
04579             Constant *C2 = ConstantInt::getAllOnesValue(X->getType());
04580             C2 = ConstantExpr::getShl(C2, Op1);
04581             return BinaryOperator::createAnd(X, C2);
04582           }
04583           
04584           // Turn (((X >> C)&CC) + Y) << C  ->  (X + (Y << C)) & (CC << C)
04585           if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
04586               match(Op0BO->getOperand(0),
04587                     m_And(m_Shr(m_Value(V1), m_Value(V2)),
04588                           m_ConstantInt(CC))) && V2 == Op1 &&
04589               cast<BinaryOperator>(Op0BO->getOperand(0))
04590                   ->getOperand(0)->hasOneUse()) {
04591             Instruction *YS = new ShiftInst(Instruction::Shl, 
04592                                             Op0BO->getOperand(1), Op1,
04593                                             Op0BO->getName());
04594             InsertNewInstBefore(YS, I); // (Y << C)
04595             Instruction *XM =
04596               BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
04597                                         V1->getName()+".mask");
04598             InsertNewInstBefore(XM, I); // X & (CC << C)
04599             
04600             return BinaryOperator::create(Op0BO->getOpcode(), XM, YS);
04601           }
04602           
04603           break;
04604       }
04605       
04606       
04607       // If the operand is an bitwise operator with a constant RHS, and the
04608       // shift is the only use, we can pull it out of the shift.
04609       if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
04610         bool isValid = true;     // Valid only for And, Or, Xor
04611         bool highBitSet = false; // Transform if high bit of constant set?
04612         
04613         switch (Op0BO->getOpcode()) {
04614           default: isValid = false; break;   // Do not perform transform!
04615           case Instruction::Add:
04616             isValid = isLeftShift;
04617             break;
04618           case Instruction::Or:
04619           case Instruction::Xor:
04620             highBitSet = false;
04621             break;
04622           case Instruction::And:
04623             highBitSet = true;
04624             break;
04625         }
04626         
04627         // If this is a signed shift right, and the high bit is modified
04628         // by the logical operation, do not perform the transformation.
04629         // The highBitSet boolean indicates the value of the high bit of
04630         // the constant which would cause it to be modified for this
04631         // operation.
04632         //
04633         if (isValid && !isLeftShift && isSignedShift) {
04634           uint64_t Val = Op0C->getRawValue();
04635           isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet;
04636         }
04637         
04638         if (isValid) {
04639           Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
04640           
04641           Instruction *NewShift =
04642             new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), Op1,
04643                           Op0BO->getName());
04644           Op0BO->setName("");
04645           InsertNewInstBefore(NewShift, I);
04646           
04647           return BinaryOperator::create(Op0BO->getOpcode(), NewShift,
04648                                         NewRHS);
04649         }
04650       }
04651     }
04652   }
04653   
04654   // Find out if this is a shift of a shift by a constant.
04655   ShiftInst *ShiftOp = 0;
04656   if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0))
04657     ShiftOp = Op0SI;
04658   else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
04659     // If this is a noop-integer case of a shift instruction, use the shift.
04660     if (CI->getOperand(0)->getType()->isInteger() &&
04661         CI->getOperand(0)->getType()->getPrimitiveSizeInBits() ==
04662         CI->getType()->getPrimitiveSizeInBits() &&
04663         isa<ShiftInst>(CI->getOperand(0))) {
04664       ShiftOp = cast<ShiftInst>(CI->getOperand(0));
04665     }
04666   }
04667   
04668   if (ShiftOp && isa<ConstantUInt>(ShiftOp->getOperand(1))) {
04669     // Find the operands and properties of the input shift.  Note that the
04670     // signedness of the input shift may differ from the current shift if there
04671     // is a noop cast between the two.
04672     bool isShiftOfLeftShift = ShiftOp->getOpcode() == Instruction::Shl;
04673     bool isShiftOfSignedShift = ShiftOp->getType()->isSigned();
04674     bool isShiftOfUnsignedShift = !isShiftOfSignedShift;
04675     
04676     ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(ShiftOp->getOperand(1));
04677 
04678     unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getValue();
04679     unsigned ShiftAmt2 = (unsigned)Op1->getValue();
04680     
04681     // Check for (A << c1) << c2   and   (A >> c1) >> c2.
04682     if (isLeftShift == isShiftOfLeftShift) {
04683       // Do not fold these shifts if the first one is signed and the second one
04684       // is unsigned and this is a right shift.  Further, don't do any folding
04685       // on them.
04686       if (isShiftOfSignedShift && isUnsignedShift && !isLeftShift)
04687         return 0;
04688       
04689       unsigned Amt = ShiftAmt1+ShiftAmt2;   // Fold into one big shift.
04690       if (Amt > Op0->getType()->getPrimitiveSizeInBits())
04691         Amt = Op0->getType()->getPrimitiveSizeInBits();
04692       
04693       Value *Op = ShiftOp->getOperand(0);
04694       if (isShiftOfSignedShift != isSignedShift)
04695         Op = InsertNewInstBefore(new CastInst(Op, I.getType(), "tmp"), I);
04696       return new ShiftInst(I.getOpcode(), Op,
04697                            ConstantUInt::get(Type::UByteTy, Amt));
04698     }
04699     
04700     // Check for (A << c1) >> c2 or (A >> c1) << c2.  If we are dealing with
04701     // signed types, we can only support the (A >> c1) << c2 configuration,
04702     // because it can not turn an arbitrary bit of A into a sign bit.
04703     if (isUnsignedShift || isLeftShift) {
04704       // Calculate bitmask for what gets shifted off the edge.
04705       Constant *C = ConstantIntegral::getAllOnesValue(I.getType());
04706       if (isLeftShift)
04707         C = ConstantExpr::getShl(C, ShiftAmt1C);
04708       else
04709         C = ConstantExpr::getUShr(C, ShiftAmt1C);
04710       
04711       Value *Op = ShiftOp->getOperand(0);
04712       if (isShiftOfSignedShift != isSignedShift)
04713         Op = InsertNewInstBefore(new CastInst(Op, I.getType(),Op->getName()),I);
04714       
04715       Instruction *Mask =
04716         BinaryOperator::createAnd(Op, C, Op->getName()+".mask");
04717       InsertNewInstBefore(Mask, I);
04718       
04719       // Figure out what flavor of shift we should use...
04720       if (ShiftAmt1 == ShiftAmt2) {
04721         return ReplaceInstUsesWith(I, Mask);  // (A << c) >> c  === A & c2
04722       } else if (ShiftAmt1 < ShiftAmt2) {
04723         return new ShiftInst(I.getOpcode(), Mask,
04724                          ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1));
04725       } else if (isShiftOfUnsignedShift || isShiftOfLeftShift) {
04726         if (isShiftOfUnsignedShift && !isShiftOfLeftShift && isSignedShift) {
04727           // Make sure to emit an unsigned shift right, not a signed one.
04728           Mask = InsertNewInstBefore(new CastInst(Mask, 
04729                                         Mask->getType()->getUnsignedVersion(),
04730                                                   Op->getName()), I);
04731           Mask = new ShiftInst(Instruction::Shr, Mask,
04732                          ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
04733           InsertNewInstBefore(Mask, I);
04734           return new CastInst(Mask, I.getType());
04735         } else {
04736           return new ShiftInst(ShiftOp->getOpcode(), Mask,
04737                     ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
04738         }
04739       } else {
04740         // (X >>s C1) << C2  where C1 > C2  === (X >>s (C1-C2)) & mask
04741         Op = InsertNewInstBefore(new CastInst(Mask,
04742                                               I.getType()->getSignedVersion(),
04743                                               Mask->getName()), I);
04744         Instruction *Shift =
04745           new ShiftInst(ShiftOp->getOpcode(), Op,
04746                         ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
04747         InsertNewInstBefore(Shift, I);
04748         
04749         C = ConstantIntegral::getAllOnesValue(Shift->getType());
04750         C = ConstantExpr::getShl(C, Op1);
04751         Mask = BinaryOperator::createAnd(Shift, C, Op->getName()+".mask");
04752         InsertNewInstBefore(Mask, I);
04753         return new CastInst(Mask, I.getType());
04754       }
04755     } else {
04756       // We can handle signed (X << C1) >>s C2 if it's a sign extend.  In
04757       // this case, C1 == C2 and C1 is 8, 16, or 32.
04758       if (ShiftAmt1 == ShiftAmt2) {
04759         const Type *SExtType = 0;
04760         switch (Op0->getType()->getPrimitiveSizeInBits() - ShiftAmt1) {
04761         case 8 : SExtType = Type::SByteTy; break;
04762         case 16: SExtType = Type::ShortTy; break;
04763         case 32: SExtType = Type::IntTy; break;
04764         }
04765         
04766         if (SExtType) {
04767           Instruction *NewTrunc = new CastInst(ShiftOp->getOperand(0),
04768                                                SExtType, "sext");
04769           InsertNewInstBefore(NewTrunc, I);
04770           return new CastInst(NewTrunc, I.getType());
04771         }
04772       }
04773     }
04774   }
04775   return 0;
04776 }
04777 
04778 
04779 /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear
04780 /// expression.  If so, decompose it, returning some value X, such that Val is
04781 /// X*Scale+Offset.
04782 ///
04783 static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
04784                                         unsigned &Offset) {
04785   assert(Val->getType() == Type::UIntTy && "Unexpected allocation size type!");
04786   if (ConstantUInt *CI = dyn_cast<ConstantUInt>(Val)) {
04787     Offset = CI->getValue();
04788     Scale  = 1;
04789     return ConstantUInt::get(Type::UIntTy, 0);
04790   } else if (Instruction *I = dyn_cast<Instruction>(Val)) {
04791     if (I->getNumOperands() == 2) {
04792       if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I->getOperand(1))) {
04793         if (I->getOpcode() == Instruction::Shl) {
04794           // This is a value scaled by '1 << the shift amt'.
04795           Scale = 1U << CUI->getValue();
04796           Offset = 0;
04797           return I->getOperand(0);
04798         } else if (I->getOpcode() == Instruction::Mul) {
04799           // This value is scaled by 'CUI'.
04800           Scale = CUI->getValue();
04801           Offset = 0;
04802           return I->getOperand(0);
04803         } else if (I->getOpcode() == Instruction::Add) {
04804           // We have X+C.  Check to see if we really have (X*C2)+C1, where C1 is
04805           // divisible by C2.
04806           unsigned SubScale;
04807           Value *SubVal = DecomposeSimpleLinearExpr(I->getOperand(0), SubScale,
04808                                                     Offset);
04809           Offset += CUI->getValue();
04810           if (SubScale > 1 && (Offset % SubScale == 0)) {
04811             Scale = SubScale;
04812             return SubVal;
04813           }
04814         }
04815       }
04816     }
04817   }
04818 
04819   // Otherwise, we can't look past this.
04820   Scale = 1;
04821   Offset = 0;
04822   return Val;
04823 }
04824 
04825 
04826 /// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
04827 /// try to eliminate the cast by moving the type information into the alloc.
04828 Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI,
04829                                                    AllocationInst &AI) {
04830   const PointerType *PTy = dyn_cast<PointerType>(CI.getType());
04831   if (!PTy) return 0;   // Not casting the allocation to a pointer type.
04832   
04833   // Remove any uses of AI that are dead.
04834   assert(!CI.use_empty() && "Dead instructions should be removed earlier!");
04835   std::vector<Instruction*> DeadUsers;
04836   for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) {
04837     Instruction *User = cast<Instruction>(*UI++);
04838     if (isInstructionTriviallyDead(User)) {
04839       while (UI != E && *UI == User)
04840         ++UI; // If this instruction uses AI more than once, don't break UI.
04841       
04842       // Add operands to the worklist.
04843       AddUsesToWorkList(*User);
04844       ++NumDeadInst;
04845       DEBUG(std::cerr << "IC: DCE: " << *User);
04846       
04847       User->eraseFromParent();
04848       removeFromWorkList(User);
04849     }
04850   }
04851   
04852   // Get the type really allocated and the type casted to.
04853   const Type *AllocElTy = AI.getAllocatedType();
04854   const Type *CastElTy = PTy->getElementType();
04855   if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
04856 
04857   unsigned AllocElTyAlign = TD->getTypeSize(AllocElTy);
04858   unsigned CastElTyAlign = TD->getTypeSize(CastElTy);
04859   if (CastElTyAlign < AllocElTyAlign) return 0;
04860 
04861   // If the allocation has multiple uses, only promote it if we are strictly
04862   // increasing the alignment of the resultant allocation.  If we keep it the
04863   // same, we open the door to infinite loops of various kinds.
04864   if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
04865 
04866   uint64_t AllocElTySize = TD->getTypeSize(AllocElTy);
04867   uint64_t CastElTySize = TD->getTypeSize(CastElTy);
04868   if (CastElTySize == 0 || AllocElTySize == 0) return 0;
04869 
04870   // See if we can satisfy the modulus by pulling a scale out of the array
04871   // size argument.
04872   unsigned ArraySizeScale, ArrayOffset;
04873   Value *NumElements = // See if the array size is a decomposable linear expr.
04874     DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
04875  
04876   // If we can now satisfy the modulus, by using a non-1 scale, we really can
04877   // do the xform.
04878   if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
04879       (AllocElTySize*ArrayOffset   ) % CastElTySize != 0) return 0;
04880 
04881   unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
04882   Value *Amt = 0;
04883   if (Scale == 1) {
04884     Amt = NumElements;
04885   } else {
04886     Amt = ConstantUInt::get(Type::UIntTy, Scale);
04887     if (ConstantUInt *CI = dyn_cast<ConstantUInt>(NumElements))
04888       Amt = ConstantExpr::getMul(CI, cast<ConstantUInt>(Amt));
04889     else if (Scale != 1) {
04890       Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp");
04891       Amt = InsertNewInstBefore(Tmp, AI);
04892     }
04893   }
04894   
04895   if (unsigned Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
04896     Value *Off = ConstantUInt::get(Type::UIntTy, Offset);
04897     Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp");
04898     Amt = InsertNewInstBefore(Tmp, AI);
04899   }
04900   
04901   std::string Name = AI.getName(); AI.setName("");
04902   AllocationInst *New;
04903   if (isa<MallocInst>(AI))
04904     New = new MallocInst(CastElTy, Amt, AI.getAlignment(), Name);
04905   else
04906     New = new AllocaInst(CastElTy, Amt, AI.getAlignment(), Name);
04907   InsertNewInstBefore(New, AI);
04908   
04909   // If the allocation has multiple uses, insert a cast and change all things
04910   // that used it to use the new cast.  This will also hack on CI, but it will
04911   // die soon.
04912   if (!AI.hasOneUse()) {
04913     AddUsesToWorkList(AI);
04914     CastInst *NewCast = new CastInst(New, AI.getType(), "tmpcast");
04915     InsertNewInstBefore(NewCast, AI);
04916     AI.replaceAllUsesWith(NewCast);
04917   }
04918   return ReplaceInstUsesWith(CI, New);
04919 }
04920 
04921 /// CanEvaluateInDifferentType - Return true if we can take the specified value
04922 /// and return it without inserting any new casts.  This is used by code that
04923 /// tries to decide whether promoting or shrinking integer operations to wider
04924 /// or smaller types will allow us to eliminate a truncate or extend.
04925 static bool CanEvaluateInDifferentType(Value *V, const Type *Ty,
04926                                        int &NumCastsRemoved) {
04927   if (isa<Constant>(V)) return true;
04928   
04929   Instruction *I = dyn_cast<Instruction>(V);
04930   if (!I || !I->hasOneUse()) return false;
04931   
04932   switch (I->getOpcode()) {
04933   case Instruction::And:
04934   case Instruction::Or:
04935   case Instruction::Xor:
04936     // These operators can all arbitrarily be extended or truncated.
04937     return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) &&
04938            CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved);
04939   case Instruction::Cast:
04940     // If this is a cast from the destination type, we can trivially eliminate
04941     // it, and this will remove a cast overall.
04942     if (I->getOperand(0)->getType() == Ty) {
04943       // If the first operand is itself a cast, and is eliminable, do not count
04944       // this as an eliminable cast.  We would prefer to eliminate those two
04945       // casts first.
04946       if (CastInst *OpCast = dyn_cast<CastInst>(I->getOperand(0)))
04947         return true;
04948       
04949       ++NumCastsRemoved;
04950       return true;
04951     }
04952     // TODO: Can handle more cases here.
04953     break;
04954   }
04955   
04956   return false;
04957 }
04958 
04959 /// EvaluateInDifferentType - Given an expression that 
04960 /// CanEvaluateInDifferentType returns true for, actually insert the code to
04961 /// evaluate the expression.
04962 Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty) {
04963   if (Constant *C = dyn_cast<Constant>(V))
04964     return ConstantExpr::getCast(C, Ty);
04965 
04966   // Otherwise, it must be an instruction.
04967   Instruction *I = cast<Instruction>(V);
04968   Instruction *Res = 0;
04969   switch (I->getOpcode()) {
04970   case Instruction::And:
04971   case Instruction::Or:
04972   case Instruction::Xor: {
04973     Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty);
04974     Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty);
04975     Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(),
04976                                  LHS, RHS, I->getName());
04977     break;
04978   }
04979   case Instruction::Cast:
04980     // If this is a cast from the destination type, return the input.
04981     if (I->getOperand(0)->getType() == Ty)
04982       return I->getOperand(0);
04983     
04984     // TODO: Can handle more cases here.
04985     assert(0 && "Unreachable!");
04986     break;
04987   }
04988   
04989   return InsertNewInstBefore(Res, *I);
04990 }
04991 
04992 
04993 // CastInst simplification
04994 //
04995 Instruction *InstCombiner::visitCastInst(CastInst &CI) {
04996   Value *Src = CI.getOperand(0);
04997 
04998   // If the user is casting a value to the same type, eliminate this cast
04999   // instruction...
05000   if (CI.getType() == Src->getType())
05001     return ReplaceInstUsesWith(CI, Src);
05002 
05003   if (isa<UndefValue>(Src))   // cast undef -> undef
05004     return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType()));
05005 
05006   // If casting the result of another cast instruction, try to eliminate this
05007   // one!
05008   //
05009   if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {   // A->B->C cast
05010     Value *A = CSrc->getOperand(0);
05011     if (isEliminableCastOfCast(A->getType(), CSrc->getType(),
05012                                CI.getType(), TD)) {
05013       // This instruction now refers directly to the cast's src operand.  This
05014       // has a good chance of making CSrc dead.
05015       CI.setOperand(0, CSrc->getOperand(0));
05016       return &CI;
05017     }
05018 
05019     // If this is an A->B->A cast, and we are dealing with integral types, try
05020     // to convert this into a logical 'and' instruction.
05021     //
05022     if (A->getType()->isInteger() &&
05023         CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
05024         CSrc->getType()->isUnsigned() &&   // B->A cast must zero extend
05025         CSrc->getType()->getPrimitiveSizeInBits() <
05026                     CI.getType()->getPrimitiveSizeInBits()&&
05027         A->getType()->getPrimitiveSizeInBits() ==
05028               CI.getType()->getPrimitiveSizeInBits()) {
05029       assert(CSrc->getType() != Type::ULongTy &&
05030              "Cannot have type bigger than ulong!");
05031       uint64_t AndValue = CSrc->getType()->getIntegralTypeMask();
05032       Constant *AndOp = ConstantUInt::get(A->getType()->getUnsignedVersion(),
05033                                           AndValue);
05034       AndOp = ConstantExpr::getCast(AndOp, A->getType());
05035       Instruction *And = BinaryOperator::createAnd(CSrc->getOperand(0), AndOp);
05036       if (And->getType() != CI.getType()) {
05037         And->setName(CSrc->getName()+".mask");
05038         InsertNewInstBefore(And, CI);
05039         And = new CastInst(And, CI.getType());
05040       }
05041       return And;
05042     }
05043   }
05044   
05045   // If this is a cast to bool, turn it into the appropriate setne instruction.
05046   if (CI.getType() == Type::BoolTy)
05047     return BinaryOperator::createSetNE(CI.getOperand(0),
05048                        Constant::getNullValue(CI.getOperand(0)->getType()));
05049 
05050   // See if we can simplify any instructions used by the LHS whose sole 
05051   // purpose is to compute bits we don't care about.
05052   if (CI.getType()->isInteger() && CI.getOperand(0)->getType()->isIntegral()) {
05053     uint64_t KnownZero, KnownOne;
05054     if (SimplifyDemandedBits(&CI, CI.getType()->getIntegralTypeMask(),
05055                              KnownZero, KnownOne))
05056       return &CI;
05057   }
05058   
05059   // If casting the result of a getelementptr instruction with no offset, turn
05060   // this into a cast of the original pointer!
05061   //
05062   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
05063     bool AllZeroOperands = true;
05064     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
05065       if (!isa<Constant>(GEP->getOperand(i)) ||
05066           !cast<Constant>(GEP->getOperand(i))->isNullValue()) {
05067         AllZeroOperands = false;
05068         break;
05069       }
05070     if (AllZeroOperands) {
05071       CI.setOperand(0, GEP->getOperand(0));
05072       return &CI;
05073     }
05074   }
05075 
05076   // If we are casting a malloc or alloca to a pointer to a type of the same
05077   // size, rewrite the allocation instruction to allocate the "right" type.
05078   //
05079   if (AllocationInst *AI = dyn_cast<AllocationInst>(Src))
05080     if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
05081       return V;
05082 
05083   if (SelectInst *SI = dyn_cast<SelectInst>(Src))
05084     if (Instruction *NV = FoldOpIntoSelect(CI, SI, this))
05085       return NV;
05086   if (isa<PHINode>(Src))
05087     if (Instruction *NV = FoldOpIntoPhi(CI))
05088       return NV;
05089   
05090   // If the source and destination are pointers, and this cast is equivalent to
05091   // a getelementptr X, 0, 0, 0...  turn it into the appropriate getelementptr.
05092   // This can enhance SROA and other transforms that want type-safe pointers.
05093   if (const PointerType *DstPTy = dyn_cast<PointerType>(CI.getType()))
05094     if (const PointerType *SrcPTy = dyn_cast<PointerType>(Src->getType())) {
05095       const Type *DstTy = DstPTy->getElementType();
05096       const Type *SrcTy = SrcPTy->getElementType();
05097       
05098       Constant *ZeroUInt = Constant::getNullValue(Type::UIntTy);
05099       unsigned NumZeros = 0;
05100       while (SrcTy != DstTy && 
05101              isa<CompositeType>(SrcTy) && !isa<PointerType>(SrcTy)) {
05102         SrcTy = cast<CompositeType>(SrcTy)->getTypeAtIndex(ZeroUInt);
05103         ++NumZeros;
05104       }
05105 
05106       // If we found a path from the src to dest, create the getelementptr now.
05107       if (SrcTy == DstTy) {
05108         std::vector<Value*> Idxs(NumZeros+1, ZeroUInt);
05109         return new GetElementPtrInst(Src, Idxs);
05110       }
05111     }
05112       
05113   // If the source value is an instruction with only this use, we can attempt to
05114   // propagate the cast into the instruction.  Also, only handle integral types
05115   // for now.
05116   if (Instruction *SrcI = dyn_cast<Instruction>(Src)) {
05117     if (SrcI->hasOneUse() && Src->getType()->isIntegral() &&
05118         CI.getType()->isInteger()) {  // Don't mess with casts to bool here
05119       
05120       int NumCastsRemoved = 0;
05121       if (CanEvaluateInDifferentType(SrcI, CI.getType(), NumCastsRemoved)) {
05122         // If this cast is a truncate, evaluting in a different type always
05123         // eliminates the cast, so it is always a win.  If this is a noop-cast
05124         // this just removes a noop cast which isn't pointful, but simplifies
05125         // the code.  If this is a zero-extension, we need to do an AND to
05126         // maintain the clear top-part of the computation, so we require that
05127         // the input have eliminated at least one cast.  If this is a sign
05128         // extension, we insert two new casts (to do the extension) so we
05129         // require that two casts have been eliminated.
05130         bool DoXForm;
05131         switch (getCastType(Src->getType(), CI.getType())) {
05132         default: assert(0 && "Unknown cast type!");
05133         case Noop:
05134         case Truncate:
05135           DoXForm = true;
05136           break;
05137         case Zeroext:
05138           DoXForm = NumCastsRemoved >= 1;
05139           break;
05140         case Signext:
05141           DoXForm = NumCastsRemoved >= 2;
05142           break;
05143         }
05144         
05145         if (DoXForm) {
05146           Value *Res = EvaluateInDifferentType(SrcI, CI.getType());
05147           assert(Res->getType() == CI.getType());
05148           switch (getCastType(Src->getType(), CI.getType())) {
05149           default: assert(0 && "Unknown cast type!");
05150           case Noop:
05151           case Truncate:
05152             // Just replace this cast with the result.
05153             return ReplaceInstUsesWith(CI, Res);
05154           case Zeroext: {
05155             // We need to emit an AND to clear the high bits.
05156             unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits();
05157             unsigned DestBitSize = CI.getType()->getPrimitiveSizeInBits();
05158             assert(SrcBitSize < DestBitSize && "Not a zext?");
05159             Constant *C = ConstantUInt::get(Type::ULongTy, (1 << SrcBitSize)-1);
05160             C = ConstantExpr::getCast(C, CI.getType());
05161             return BinaryOperator::createAnd(Res, C);
05162           }
05163           case Signext:
05164             // We need to emit a cast to truncate, then a cast to sext.
05165             return new CastInst(InsertCastBefore(Res, Src->getType(), CI),
05166                                 CI.getType());
05167           }
05168         }
05169       }
05170       
05171       const Type *DestTy = CI.getType();
05172       unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits();
05173       unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
05174 
05175       Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0;
05176       Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0;
05177 
05178       switch (SrcI->getOpcode()) {
05179       case Instruction::Add:
05180       case Instruction::Mul:
05181       case Instruction::And:
05182       case Instruction::Or:
05183       case Instruction::Xor:
05184         // If we are discarding information, or just changing the sign, rewrite.
05185         if (DestBitSize <= SrcBitSize && DestBitSize != 1) {
05186           // Don't insert two casts if they cannot be eliminated.  We allow two
05187           // casts to be inserted if the sizes are the same.  This could only be
05188           // converting signedness, which is a noop.
05189           if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) ||
05190               !ValueRequiresCast(Op0, DestTy, TD)) {
05191             Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI);
05192             Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI);
05193             return BinaryOperator::create(cast<BinaryOperator>(SrcI)
05194                              ->getOpcode(), Op0c, Op1c);
05195           }
05196         }
05197 
05198         // cast (xor bool X, true) to int  --> xor (cast bool X to int), 1
05199         if (SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor &&
05200             Op1 == ConstantBool::True &&
05201             (!Op0->hasOneUse() || !isa<SetCondInst>(Op0))) {
05202           Value *New = InsertOperandCastBefore(Op0, DestTy, &CI);
05203           return BinaryOperator::createXor(New,
05204                                            ConstantInt::get(CI.getType(), 1));
05205         }
05206         break;
05207       case Instruction::Shl:
05208         // Allow changing the sign of the source operand.  Do not allow changing
05209         // the size of the shift, UNLESS the shift amount is a constant.  We
05210         // mush not change variable sized shifts to a smaller size, because it
05211         // is undefined to shift more bits out than exist in the value.
05212         if (DestBitSize == SrcBitSize ||
05213             (DestBitSize < SrcBitSize && isa<Constant>(Op1))) {
05214           Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI);
05215           return new ShiftInst(Instruction::Shl, Op0c, Op1);
05216         }
05217         break;
05218       case Instruction::Shr:
05219         // If this is a signed shr, and if all bits shifted in are about to be
05220         // truncated off, turn it into an unsigned shr to allow greater
05221         // simplifications.
05222         if (DestBitSize < SrcBitSize && Src->getType()->isSigned() &&
05223             isa<ConstantInt>(Op1)) {
05224           unsigned ShiftAmt = cast<ConstantUInt>(Op1)->getValue();
05225           if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) {
05226             // Convert to unsigned.
05227             Value *N1 = InsertOperandCastBefore(Op0,
05228                                      Op0->getType()->getUnsignedVersion(), &CI);
05229             // Insert the new shift, which is now unsigned.
05230             N1 = InsertNewInstBefore(new ShiftInst(Instruction::Shr, N1,
05231                                                    Op1, Src->getName()), CI);
05232             return new CastInst(N1, CI.getType());
05233           }
05234         }
05235         break;
05236 
05237       case Instruction::SetEQ:
05238       case Instruction::SetNE:
05239         // We if we are just checking for a seteq of a single bit and casting it
05240         // to an integer.  If so, shift the bit to the appropriate place then
05241         // cast to integer to avoid the comparison.
05242         if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
05243           uint64_t Op1CV = Op1C->getZExtValue();
05244           // cast (X == 0) to int --> X^1        iff X has only the low bit set.
05245           // cast (X == 0) to int --> (X>>1)^1   iff X has only the 2nd bit set.
05246           // cast (X == 1) to int --> X          iff X has only the low bit set.
05247           // cast (X == 2) to int --> X>>1       iff X has only the 2nd bit set.
05248           // cast (X != 0) to int --> X          iff X has only the low bit set.
05249           // cast (X != 0) to int --> X>>1       iff X has only the 2nd bit set.
05250           // cast (X != 1) to int --> X^1        iff X has only the low bit set.
05251           // cast (X != 2) to int --> (X>>1)^1   iff X has only the 2nd bit set.
05252           if (Op1CV == 0 || isPowerOf2_64(Op1CV)) {
05253             // If Op1C some other power of two, convert:
05254             uint64_t KnownZero, KnownOne;
05255             uint64_t TypeMask = Op1->getType()->getIntegralTypeMask();
05256             ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne);
05257             
05258             if (isPowerOf2_64(KnownZero^TypeMask)) { // Exactly one possible 1?
05259               bool isSetNE = SrcI->getOpcode() == Instruction::SetNE;
05260               if (Op1CV && (Op1CV != (KnownZero^TypeMask))) {
05261                 // (X&4) == 2 --> false
05262                 // (X&4) != 2 --> true
05263                 Constant *Res = ConstantBool::get(isSetNE);
05264                 Res = ConstantExpr::getCast(Res, CI.getType());
05265                 return ReplaceInstUsesWith(CI, Res);
05266               }
05267               
05268               unsigned ShiftAmt = Log2_64(KnownZero^TypeMask);
05269               Value *In = Op0;
05270               if (ShiftAmt) {
05271                 // Perform an unsigned shr by shiftamt.  Convert input to
05272                 // unsigned if it is signed.
05273                 if (In->getType()->isSigned())
05274                   In = InsertNewInstBefore(new CastInst(In,
05275                         In->getType()->getUnsignedVersion(), In->getName()),CI);
05276                 // Insert the shift to put the result in the low bit.
05277                 In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In,
05278                                      ConstantInt::get(Type::UByteTy, ShiftAmt),
05279                                      In->getName()+".lobit"), CI);
05280               }
05281               
05282               if ((Op1CV != 0) == isSetNE) { // Toggle the low bit.
05283                 Constant *One = ConstantInt::get(In->getType(), 1);
05284                 In = BinaryOperator::createXor(In, One, "tmp");
05285                 InsertNewInstBefore(cast<Instruction>(In), CI);
05286               }
05287               
05288               if (CI.getType() == In->getType())
05289                 return ReplaceInstUsesWith(CI, In);
05290               else
05291                 return new CastInst(In, CI.getType());
05292             }
05293           }
05294         }
05295         break;
05296       }
05297     }
05298     
05299     if (SrcI->hasOneUse()) {
05300       if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(SrcI)) {
05301         // Okay, we have (cast (shuffle ..)).  We know this cast is a bitconvert
05302         // because the inputs are known to be a vector.  Check to see if this is
05303         // a cast to a vector with the same # elts.
05304         if (isa<PackedType>(CI.getType()) && 
05305             cast<PackedType>(CI.getType())->getNumElements() == 
05306                   SVI->getType()->getNumElements()) {
05307           CastInst *Tmp;
05308           // If either of the operands is a cast from CI.getType(), then
05309           // evaluating the shuffle in the casted destination's type will allow
05310           // us to eliminate at least one cast.
05311           if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && 
05312                Tmp->getOperand(0)->getType() == CI.getType()) ||
05313               ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && 
05314                Tmp->getOperand(0)->getType() == CI.getType())) {
05315             Value *LHS = InsertOperandCastBefore(SVI->getOperand(0),
05316                                                  CI.getType(), &CI);
05317             Value *RHS = InsertOperandCastBefore(SVI->getOperand(1),
05318                                                  CI.getType(), &CI);
05319             // Return a new shuffle vector.  Use the same element ID's, as we
05320             // know the vector types match #elts.
05321             return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
05322           }
05323         }
05324       }
05325     }
05326   }
05327       
05328   return 0;
05329 }
05330 
05331 /// GetSelectFoldableOperands - We want to turn code that looks like this:
05332 ///   %C = or %A, %B
05333 ///   %D = select %cond, %C, %A
05334 /// into:
05335 ///   %C = select %cond, %B, 0
05336 ///   %D = or %A, %C
05337 ///
05338 /// Assuming that the specified instruction is an operand to the select, return
05339 /// a bitmask indicating which operands of this instruction are foldable if they
05340 /// equal the other incoming value of the select.
05341 ///
05342 static unsigned GetSelectFoldableOperands(Instruction *I) {
05343   switch (I->getOpcode()) {
05344   case Instruction::Add:
05345   case Instruction::Mul:
05346   case Instruction::And:
05347   case Instruction::Or:
05348   case Instruction::Xor:
05349     return 3;              // Can fold through either operand.
05350   case Instruction::Sub:   // Can only fold on the amount subtracted.
05351   case Instruction::Shl:   // Can only fold on the shift amount.
05352   case Instruction::Shr:
05353     return 1;
05354   default:
05355     return 0;              // Cannot fold
05356   }
05357 }
05358 
05359 /// GetSelectFoldableConstant - For the same transformation as the previous
05360 /// function, return the identity constant that goes into the select.
05361 static Constant *GetSelectFoldableConstant(Instruction *I) {
05362   switch (I->getOpcode()) {
05363   default: assert(0 && "This cannot happen!"); abort();
05364   case Instruction::Add:
05365   case Instruction::Sub:
05366   case Instruction::Or:
05367   case Instruction::Xor:
05368     return Constant::getNullValue(I->getType());
05369   case Instruction::Shl:
05370   case Instruction::Shr:
05371     return Constant::getNullValue(Type::UByteTy);
05372   case Instruction::And:
05373     return ConstantInt::getAllOnesValue(I->getType());
05374   case Instruction::Mul:
05375     return ConstantInt::get(I->getType(), 1);
05376   }
05377 }
05378 
05379 /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
05380 /// have the same opcode and only one use each.  Try to simplify this.
05381 Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
05382                                           Instruction *FI) {
05383   if (TI->getNumOperands() == 1) {
05384     // If this is a non-volatile load or a cast from the same type,
05385     // merge.
05386     if (TI->getOpcode() == Instruction::Cast) {
05387       if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType())
05388         return 0;
05389     } else {
05390       return 0;  // unknown unary op.
05391     }
05392 
05393     // Fold this by inserting a select from the input values.
05394     SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0),
05395                                        FI->getOperand(0), SI.getName()+".v");
05396     InsertNewInstBefore(NewSI, SI);
05397     return new CastInst(NewSI, TI->getType());
05398   }
05399 
05400   // Only handle binary operators here.
05401   if (!isa<ShiftInst>(TI) && !isa<BinaryOperator>(TI))
05402     return 0;
05403 
05404   // Figure out if the operations have any operands in common.
05405   Value *MatchOp, *OtherOpT, *OtherOpF;
05406   bool MatchIsOpZero;
05407   if (TI->getOperand(0) == FI->getOperand(0)) {
05408     MatchOp  = TI->getOperand(0);
05409     OtherOpT = TI->getOperand(1);
05410     OtherOpF = FI->getOperand(1);
05411     MatchIsOpZero = true;
05412   } else if (TI->getOperand(1) == FI->getOperand(1)) {
05413     MatchOp  = TI->getOperand(1);
05414     OtherOpT = TI->getOperand(0);
05415     OtherOpF = FI->getOperand(0);
05416     MatchIsOpZero = false;
05417   } else if (!TI->isCommutative()) {
05418     return 0;
05419   } else if (TI->getOperand(0) == FI->getOperand(1)) {
05420     MatchOp  = TI->getOperand(0);
05421     OtherOpT = TI->getOperand(1);
05422     OtherOpF = FI->getOperand(0);
05423     MatchIsOpZero = true;
05424   } else if (TI->getOperand(1) == FI->getOperand(0)) {
05425     MatchOp  = TI->getOperand(1);
05426     OtherOpT = TI->getOperand(0);
05427     OtherOpF = FI->getOperand(1);
05428     MatchIsOpZero = true;
05429   } else {
05430     return 0;
05431   }
05432 
05433   // If we reach here, they do have operations in common.
05434   SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT,
05435                                      OtherOpF, SI.getName()+".v");
05436   InsertNewInstBefore(NewSI, SI);
05437 
05438   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
05439     if (MatchIsOpZero)
05440       return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI);
05441     else
05442       return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp);
05443   } else {
05444     if (MatchIsOpZero)
05445       return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), MatchOp, NewSI);
05446     else
05447       return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), NewSI, MatchOp);
05448   }
05449 }
05450 
05451 Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
05452   Value *CondVal = SI.getCondition();
05453   Value *TrueVal = SI.getTrueValue();
05454   Value *FalseVal = SI.getFalseValue();
05455 
05456   // select true, X, Y  -> X
05457   // select false, X, Y -> Y
05458   if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal))
05459     if (C == ConstantBool::True)
05460       return ReplaceInstUsesWith(SI, TrueVal);
05461     else {
05462       assert(C == ConstantBool::False);
05463       return ReplaceInstUsesWith(SI, FalseVal);
05464     }
05465 
05466   // select C, X, X -> X
05467   if (TrueVal == FalseVal)
05468     return ReplaceInstUsesWith(SI, TrueVal);
05469 
05470   if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X
05471     return ReplaceInstUsesWith(SI, FalseVal);
05472   if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X
05473     return ReplaceInstUsesWith(SI, TrueVal);
05474   if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y
05475     if (isa<Constant>(TrueVal))
05476       return ReplaceInstUsesWith(SI, TrueVal);
05477     else
05478       return ReplaceInstUsesWith(SI, FalseVal);
05479   }
05480 
05481   if (SI.getType() == Type::BoolTy)
05482     if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) {
05483       if (C == ConstantBool::True) {
05484         // Change: A = select B, true, C --> A = or B, C
05485         return BinaryOperator::createOr(CondVal, FalseVal);
05486       } else {
05487         // Change: A = select B, false, C --> A = and !B, C
05488         Value *NotCond =
05489           InsertNewInstBefore(BinaryOperator::createNot(CondVal,
05490                                              "not."+CondVal->getName()), SI);
05491         return BinaryOperator::createAnd(NotCond, FalseVal);
05492       }
05493     } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) {
05494       if (C == ConstantBool::False) {
05495         // Change: A = select B, C, false --> A = and B, C
05496         return BinaryOperator::createAnd(CondVal, TrueVal);
05497       } else {
05498         // Change: A = select B, C, true --> A = or !B, C
05499         Value *NotCond =
05500           InsertNewInstBefore(BinaryOperator::createNot(CondVal,
05501                                              "not."+CondVal->getName()), SI);
05502         return BinaryOperator::createOr(NotCond, TrueVal);
05503       }
05504     }
05505 
05506   // Selecting between two integer constants?
05507   if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
05508     if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
05509       // select C, 1, 0 -> cast C to int
05510       if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) {
05511         return new CastInst(CondVal, SI.getType());
05512       } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) {
05513         // select C, 0, 1 -> cast !C to int
05514         Value *NotCond =
05515           InsertNewInstBefore(BinaryOperator::createNot(CondVal,
05516                                                "not."+CondVal->getName()), SI);
05517         return new CastInst(NotCond, SI.getType());
05518       }
05519 
05520       // If one of the constants is zero (we know they can't both be) and we
05521       // have a setcc instruction with zero, and we have an 'and' with the
05522       // non-constant value, eliminate this whole mess.  This corresponds to
05523       // cases like this: ((X & 27) ? 27 : 0)
05524       if (TrueValC->isNullValue() || FalseValC->isNullValue())
05525         if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition()))
05526           if ((IC->getOpcode() == Instruction::SetEQ ||
05527                IC->getOpcode() == Instruction::SetNE) &&
05528               isa<ConstantInt>(IC->getOperand(1)) &&
05529               cast<Constant>(IC->getOperand(1))->isNullValue())
05530             if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0)))
05531               if (ICA->getOpcode() == Instruction::And &&
05532                   isa<ConstantInt>(ICA->getOperand(1)) &&
05533                   (ICA->getOperand(1) == TrueValC ||
05534                    ICA->getOperand(1) == FalseValC) &&
05535                   isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) {
05536                 // Okay, now we know that everything is set up, we just don't
05537                 // know whether we have a setne or seteq and whether the true or
05538                 // false val is the zero.
05539                 bool ShouldNotVal = !TrueValC->isNullValue();
05540                 ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE;
05541                 Value *V = ICA;
05542                 if (ShouldNotVal)
05543                   V = InsertNewInstBefore(BinaryOperator::create(
05544                                   Instruction::Xor, V, ICA->getOperand(1)), SI);
05545                 return ReplaceInstUsesWith(SI, V);
05546               }
05547     }
05548 
05549   // See if we are selecting two values based on a comparison of the two values.
05550   if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) {
05551     if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) {
05552       // Transform (X == Y) ? X : Y  -> Y
05553       if (SCI->getOpcode() == Instruction::SetEQ)
05554         return ReplaceInstUsesWith(SI, FalseVal);
05555       // Transform (X != Y) ? X : Y  -> X
05556       if (SCI->getOpcode() == Instruction::SetNE)
05557         return ReplaceInstUsesWith(SI, TrueVal);
05558       // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
05559 
05560     } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){
05561       // Transform (X == Y) ? Y : X  -> X
05562       if (SCI->getOpcode() == Instruction::SetEQ)
05563         return ReplaceInstUsesWith(SI, FalseVal);
05564       // Transform (X != Y) ? Y : X  -> Y
05565       if (SCI->getOpcode() == Instruction::SetNE)
05566         return ReplaceInstUsesWith(SI, TrueVal);
05567       // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
05568     }
05569   }
05570 
05571   if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
05572     if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
05573       if (TI->hasOneUse() && FI->hasOneUse()) {
05574         bool isInverse = false;
05575         Instruction *AddOp = 0, *SubOp = 0;
05576 
05577         // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
05578         if (TI->getOpcode() == FI->getOpcode())
05579           if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
05580             return IV;
05581 
05582         // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))).  This is
05583         // even legal for FP.
05584         if (TI->getOpcode() == Instruction::Sub &&
05585             FI->getOpcode() == Instruction::Add) {
05586           AddOp = FI; SubOp = TI;
05587         } else if (FI->getOpcode() == Instruction::Sub &&
05588                    TI->getOpcode() == Instruction::Add) {
05589           AddOp = TI; SubOp = FI;
05590         }
05591 
05592         if (AddOp) {
05593           Value *OtherAddOp = 0;
05594           if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
05595             OtherAddOp = AddOp->getOperand(1);
05596           } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
05597             OtherAddOp = AddOp->getOperand(0);
05598           }
05599 
05600           if (OtherAddOp) {
05601             // So at this point we know we have (Y -> OtherAddOp):
05602             //        select C, (add X, Y), (sub X, Z)
05603             Value *NegVal;  // Compute -Z
05604             if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) {
05605               NegVal = ConstantExpr::getNeg(C);
05606             } else {
05607               NegVal = InsertNewInstBefore(
05608                     BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI);
05609             }
05610 
05611             Value *NewTrueOp = OtherAddOp;
05612             Value *NewFalseOp = NegVal;
05613             if (AddOp != TI)
05614               std::swap(NewTrueOp, NewFalseOp);
05615             Instruction *NewSel =
05616               new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p");
05617 
05618             NewSel = InsertNewInstBefore(NewSel, SI);
05619             return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel);
05620           }
05621         }
05622       }
05623 
05624   // See if we can fold the select into one of our operands.
05625   if (SI.getType()->isInteger()) {
05626     // See the comment above GetSelectFoldableOperands for a description of the
05627     // transformation we are doing here.
05628     if (Instruction *TVI = dyn_cast<Instruction>(TrueVal))
05629       if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
05630           !isa<Constant>(FalseVal))
05631         if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
05632           unsigned OpToFold = 0;
05633           if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
05634             OpToFold = 1;
05635           } else  if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
05636             OpToFold = 2;
05637           }
05638 
05639           if (OpToFold) {
05640             Constant *C = GetSelectFoldableConstant(TVI);
05641             std::string Name = TVI->getName(); TVI->setName("");
05642             Instruction *NewSel =
05643               new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C,
05644                              Name);
05645             InsertNewInstBefore(NewSel, SI);
05646             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
05647               return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel);
05648             else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI))
05649               return new ShiftInst(SI->getOpcode(), FalseVal, NewSel);
05650             else {
05651               assert(0 && "Unknown instruction!!");
05652             }
05653           }
05654         }
05655 
05656     if (Instruction *FVI = dyn_cast<Instruction>(FalseVal))
05657       if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
05658           !isa<Constant>(TrueVal))
05659         if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
05660           unsigned OpToFold = 0;
05661           if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
05662             OpToFold = 1;
05663           } else  if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
05664             OpToFold = 2;
05665           }
05666 
05667           if (OpToFold) {
05668             Constant *C = GetSelectFoldableConstant(FVI);
05669             std::string Name = FVI->getName(); FVI->setName("");
05670             Instruction *NewSel =
05671               new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold),
05672                              Name);
05673             InsertNewInstBefore(NewSel, SI);
05674             if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
05675               return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel);
05676             else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI))
05677               return new ShiftInst(SI->getOpcode(), TrueVal, NewSel);
05678             else {
05679               assert(0 && "Unknown instruction!!");
05680             }
05681           }
05682         }
05683   }
05684 
05685   if (BinaryOperator::isNot(CondVal)) {
05686     SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
05687     SI.setOperand(1, FalseVal);
05688     SI.setOperand(2, TrueVal);
05689     return &SI;
05690   }
05691 
05692   return 0;
05693 }
05694 
05695 /// GetKnownAlignment - If the specified pointer has an alignment that we can
05696 /// determine, return it, otherwise return 0.
05697 static unsigned GetKnownAlignment(Value *V, TargetData *TD) {
05698   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
05699     unsigned Align = GV->getAlignment();
05700     if (Align == 0 && TD) 
05701       Align = TD->getTypeAlignment(GV->getType()->getElementType());
05702     return Align;
05703   } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
05704     unsigned Align = AI->getAlignment();
05705     if (Align == 0 && TD) {
05706       if (isa<AllocaInst>(AI))
05707         Align = TD->getTypeAlignment(AI->getType()->getElementType());
05708       else if (isa<MallocInst>(AI)) {
05709         // Malloc returns maximally aligned memory.
05710         Align = TD->getTypeAlignment(AI->getType()->getElementType());
05711         Align = std::max(Align, (unsigned)TD->getTypeAlignment(Type::DoubleTy));
05712         Align = std::max(Align, (unsigned)TD->getTypeAlignment(Type::LongTy));
05713       }
05714     }
05715     return Align;
05716   } else if (isa<CastInst>(V) ||
05717              (isa<ConstantExpr>(V) && 
05718               cast<ConstantExpr>(V)->getOpcode() == Instruction::Cast)) {
05719     User *CI = cast<User>(V);
05720     if (isa<PointerType>(CI->getOperand(0)->getType()))
05721       return GetKnownAlignment(CI->getOperand(0), TD);
05722     return 0;
05723   } else if (isa<GetElementPtrInst>(V) ||
05724              (isa<ConstantExpr>(V) && 
05725               cast<ConstantExpr>(V)->getOpcode()==Instruction::GetElementPtr)) {
05726     User *GEPI = cast<User>(V);
05727     unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD);
05728     if (BaseAlignment == 0) return 0;
05729     
05730     // If all indexes are zero, it is just the alignment of the base pointer.
05731     bool AllZeroOperands = true;
05732     for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
05733       if (!isa<Constant>(GEPI->getOperand(i)) ||
05734           !cast<Constant>(GEPI->getOperand(i))->isNullValue()) {
05735         AllZeroOperands = false;
05736         break;
05737       }
05738     if (AllZeroOperands)
05739       return BaseAlignment;
05740     
05741     // Otherwise, if the base alignment is >= the alignment we expect for the
05742     // base pointer type, then we know that the resultant pointer is aligned at
05743     // least as much as its type requires.
05744     if (!TD) return 0;
05745 
05746     const Type *BasePtrTy = GEPI->getOperand(0)->getType();
05747     if (TD->getTypeAlignment(cast<PointerType>(BasePtrTy)->getElementType())
05748         <= BaseAlignment) {
05749       const Type *GEPTy = GEPI->getType();
05750       return TD->getTypeAlignment(cast<PointerType>(GEPTy)->getElementType());
05751     }
05752     return 0;
05753   }
05754   return 0;
05755 }
05756 
05757 
05758 /// visitCallInst - CallInst simplification.  This mostly only handles folding 
05759 /// of intrinsic instructions.  For normal calls, it allows visitCallSite to do
05760 /// the heavy lifting.
05761 ///
05762 Instruction *InstCombiner::visitCallInst(CallInst &CI) {
05763   IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
05764   if (!II) return visitCallSite(&CI);
05765   
05766   // Intrinsics cannot occur in an invoke, so handle them here instead of in
05767   // visitCallSite.
05768   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
05769     bool Changed = false;
05770 
05771     // memmove/cpy/set of zero bytes is a noop.
05772     if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
05773       if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
05774 
05775       if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
05776         if (CI->getRawValue() == 1) {
05777           // Replace the instruction with just byte operations.  We would
05778           // transform other cases to loads/stores, but we don't know if
05779           // alignment is sufficient.
05780         }
05781     }
05782 
05783     // If we have a memmove and the source operation is a constant global,
05784     // then the source and dest pointers can't alias, so we can change this
05785     // into a call to memcpy.
05786     if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) {
05787       if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
05788         if (GVSrc->isConstant()) {
05789           Module *M = CI.getParent()->getParent()->getParent();
05790           const char *Name;
05791           if (CI.getCalledFunction()->getFunctionType()->getParamType(3) == 
05792               Type::UIntTy)
05793             Name = "llvm.memcpy.i32";
05794           else
05795             Name = "llvm.memcpy.i64";
05796           Function *MemCpy = M->getOrInsertFunction(Name,
05797                                      CI.getCalledFunction()->getFunctionType());
05798           CI.setOperand(0, MemCpy);
05799           Changed = true;
05800         }
05801     }
05802 
05803     // If we can determine a pointer alignment that is bigger than currently
05804     // set, update the alignment.
05805     if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) {
05806       unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD);
05807       unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD);
05808       unsigned Align = std::min(Alignment1, Alignment2);
05809       if (MI->getAlignment()->getRawValue() < Align) {
05810         MI->setAlignment(ConstantUInt::get(Type::UIntTy, Align));
05811         Changed = true;
05812       }
05813     } else if (isa<MemSetInst>(MI)) {
05814       unsigned Alignment = GetKnownAlignment(MI->getDest(), TD);
05815       if (MI->getAlignment()->getRawValue() < Alignment) {
05816         MI->setAlignment(ConstantUInt::get(Type::UIntTy, Alignment));
05817         Changed = true;
05818       }
05819     }
05820           
05821     if (Changed) return II;
05822   } else {
05823     switch (II->getIntrinsicID()) {
05824     default: break;
05825     case Intrinsic::ppc_altivec_lvx:
05826     case Intrinsic::ppc_altivec_lvxl:
05827     case Intrinsic::x86_sse_loadu_ps:
05828     case Intrinsic::x86_sse2_loadu_pd:
05829     case Intrinsic::x86_sse2_loadu_dq:
05830       // Turn PPC lvx     -> load if the pointer is known aligned.
05831       // Turn X86 loadups -> load if the pointer is known aligned.
05832       if (GetKnownAlignment(II->getOperand(1), TD) >= 16) {
05833         Value *Ptr = InsertCastBefore(II->getOperand(1),
05834                                       PointerType::get(II->getType()), CI);
05835         return new LoadInst(Ptr);
05836       }
05837       break;
05838     case Intrinsic::ppc_altivec_stvx:
05839     case Intrinsic::ppc_altivec_stvxl:
05840       // Turn stvx -> store if the pointer is known aligned.
05841       if (GetKnownAlignment(II->getOperand(2), TD) >= 16) {
05842         const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType());
05843         Value *Ptr = InsertCastBefore(II->getOperand(2), OpPtrTy, CI);
05844         return new StoreInst(II->getOperand(1), Ptr);
05845       }
05846       break;
05847     case Intrinsic::x86_sse_storeu_ps:
05848     case Intrinsic::x86_sse2_storeu_pd:
05849     case Intrinsic::x86_sse2_storeu_dq:
05850     case Intrinsic::x86_sse2_storel_dq:
05851       // Turn X86 storeu -> store if the pointer is known aligned.
05852       if (GetKnownAlignment(II->getOperand(1), TD) >= 16) {
05853         const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType());
05854         Value *Ptr = InsertCastBefore(II->getOperand(1), OpPtrTy, CI);
05855         return new StoreInst(II->getOperand(2), Ptr);
05856       }
05857       break;
05858     case Intrinsic::ppc_altivec_vperm:
05859       // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
05860       if (ConstantPacked *Mask = dyn_cast<ConstantPacked>(II->getOperand(3))) {
05861         assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
05862         
05863         // Check that all of the elements are integer constants or undefs.
05864         bool AllEltsOk = true;
05865         for (unsigned i = 0; i != 16; ++i) {
05866           if (!isa<ConstantInt>(Mask->getOperand(i)) && 
05867               !isa<UndefValue>(Mask->getOperand(i))) {
05868             AllEltsOk = false;
05869             break;
05870           }
05871         }
05872         
05873         if (AllEltsOk) {
05874           // Cast the input vectors to byte vectors.
05875           Value *Op0 = InsertCastBefore(II->getOperand(1), Mask->getType(), CI);
05876           Value *Op1 = InsertCastBefore(II->getOperand(2), Mask->getType(), CI);
05877           Value *Result = UndefValue::get(Op0->getType());
05878           
05879           // Only extract each element once.
05880           Value *ExtractedElts[32];
05881           memset(ExtractedElts, 0, sizeof(ExtractedElts));
05882           
05883           for (unsigned i = 0; i != 16; ++i) {
05884             if (isa<UndefValue>(Mask->getOperand(i)))
05885               continue;
05886             unsigned Idx =cast<ConstantInt>(Mask->getOperand(i))->getRawValue();
05887             Idx &= 31;  // Match the hardware behavior.
05888             
05889             if (ExtractedElts[Idx] == 0) {
05890               Instruction *Elt = 
05891                 new ExtractElementInst(Idx < 16 ? Op0 : Op1,
05892                                        ConstantUInt::get(Type::UIntTy, Idx&15),
05893                                        "tmp");
05894               InsertNewInstBefore(Elt, CI);
05895               ExtractedElts[Idx] = Elt;
05896             }
05897           
05898             // Insert this value into the result vector.
05899             Result = new InsertElementInst(Result, ExtractedElts[Idx],
05900                                            ConstantUInt::get(Type::UIntTy, i),
05901                                            "tmp");
05902             InsertNewInstBefore(cast<Instruction>(Result), CI);
05903           }
05904           return new CastInst(Result, CI.getType());
05905         }
05906       }
05907       break;
05908 
05909     case Intrinsic::stackrestore: {
05910       // If the save is right next to the restore, remove the restore.  This can
05911       // happen when variable allocas are DCE'd.
05912       if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
05913         if (SS->getIntrinsicID() == Intrinsic::stacksave) {
05914           BasicBlock::iterator BI = SS;
05915           if (&*++BI == II)
05916             return EraseInstFromFunction(CI);
05917         }
05918       }
05919       
05920       // If the stack restore is in a return/unwind block and if there are no
05921       // allocas or calls between the restore and the return, nuke the restore.
05922       TerminatorInst *TI = II->getParent()->getTerminator();
05923       if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) {
05924         BasicBlock::iterator BI = II;
05925         bool CannotRemove = false;
05926         for (++BI; &*BI != TI; ++BI) {
05927           if (isa<AllocaInst>(BI) ||
05928               (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) {
05929             CannotRemove = true;
05930             break;
05931           }
05932         }
05933         if (!CannotRemove)
05934           return EraseInstFromFunction(CI);
05935       }
05936       break;
05937     }
05938     }
05939   }
05940 
05941   return visitCallSite(II);
05942 }
05943 
05944 // InvokeInst simplification
05945 //
05946 Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
05947   return visitCallSite(&II);
05948 }
05949 
05950 // visitCallSite - Improvements for call and invoke instructions.
05951 //
05952 Instruction *InstCombiner::visitCallSite(CallSite CS) {
05953   bool Changed = false;
05954 
05955   // If the callee is a constexpr cast of a function, attempt to move the cast
05956   // to the arguments of the call/invoke.
05957   if (transformConstExprCastCall(CS)) return 0;
05958 
05959   Value *Callee = CS.getCalledValue();
05960 
05961   if (Function *CalleeF = dyn_cast<Function>(Callee))
05962     if (CalleeF->getCallingConv() != CS.getCallingConv()) {
05963       Instruction *OldCall = CS.getInstruction();
05964       // If the call and callee calling conventions don't match, this call must
05965       // be unreachable, as the call is undefined.
05966       new StoreInst(ConstantBool::True,
05967                     UndefValue::get(PointerType::get(Type::BoolTy)), OldCall);
05968       if (!OldCall->use_empty())
05969         OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
05970       if (isa<CallInst>(OldCall))   // Not worth removing an invoke here.
05971         return EraseInstFromFunction(*OldCall);
05972       return 0;
05973     }
05974 
05975   if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
05976     // This instruction is not reachable, just remove it.  We insert a store to
05977     // undef so that we know that this code is not reachable, despite the fact
05978     // that we can't modify the CFG here.
05979     new StoreInst(ConstantBool::True,
05980                   UndefValue::get(PointerType::get(Type::BoolTy)),
05981                   CS.getInstruction());
05982 
05983     if (!CS.getInstruction()->use_empty())
05984       CS.getInstruction()->
05985         replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
05986 
05987     if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
05988       // Don't break the CFG, insert a dummy cond branch.
05989       new BranchInst(II->getNormalDest(), II->getUnwindDest(),
05990                      ConstantBool::True, II);
05991     }
05992     return EraseInstFromFunction(*CS.getInstruction());
05993   }
05994 
05995   const PointerType *PTy = cast<PointerType>(Callee->getType());
05996   const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
05997   if (FTy->isVarArg()) {
05998     // See if we can optimize any arguments passed through the varargs area of
05999     // the call.
06000     for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
06001            E = CS.arg_end(); I != E; ++I)
06002       if (CastInst *CI = dyn_cast<CastInst>(*I)) {
06003         // If this cast does not effect the value passed through the varargs
06004         // area, we can eliminate the use of the cast.
06005         Value *Op = CI->getOperand(0);
06006         if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) {
06007           *I = Op;
06008           Changed = true;
06009         }
06010       }
06011   }
06012 
06013   return Changed ? CS.getInstruction() : 0;
06014 }
06015 
06016 // transformConstExprCastCall - If the callee is a constexpr cast of a function,
06017 // attempt to move the cast to the arguments of the call/invoke.
06018 //
06019 bool InstCombiner::transformConstExprCastCall(CallSite CS) {
06020   if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
06021   ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
06022   if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0)))
06023     return false;
06024   Function *Callee = cast<Function>(CE->getOperand(0));
06025   Instruction *Caller = CS.getInstruction();
06026 
06027   // Okay, this is a cast from a function to a different type.  Unless doing so
06028   // would cause a type conversion of one of our arguments, change this call to
06029   // be a direct call with arguments casted to the appropriate types.
06030   //
06031   const FunctionType *FT = Callee->getFunctionType();
06032   const Type *OldRetTy = Caller->getType();
06033 
06034   // Check to see if we are changing the return type...
06035   if (OldRetTy != FT->getReturnType()) {
06036     if (Callee->isExternal() &&
06037         !(OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) ||
06038           (isa<PointerType>(FT->getReturnType()) && 
06039            TD->getIntPtrType()->isLosslesslyConvertibleTo(OldRetTy)))
06040         && !Caller->use_empty())
06041       return false;   // Cannot transform this return value...
06042 
06043     // If the callsite is an invoke instruction, and the return value is used by
06044     // a PHI node in a successor, we cannot change the return type of the call
06045     // because there is no place to put the cast instruction (without breaking
06046     // the critical edge).  Bail out in this case.
06047     if (!Caller->use_empty())
06048       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
06049         for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
06050              UI != E; ++UI)
06051           if (PHINode *PN = dyn_cast<PHINode>(*UI))
06052             if (PN->getParent() == II->getNormalDest() ||
06053                 PN->getParent() == II->getUnwindDest())
06054               return false;
06055   }
06056 
06057   unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
06058   unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
06059 
06060   CallSite::arg_iterator AI = CS.arg_begin();
06061   for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
06062     const Type *ParamTy = FT->getParamType(i);
06063     const Type *ActTy = (*AI)->getType();
06064     ConstantSInt* c = dyn_cast<ConstantSInt>(*AI);
06065     //Either we can cast directly, or we can upconvert the argument
06066     bool isConvertible = ActTy->isLosslesslyConvertibleTo(ParamTy) ||
06067       (ParamTy->isIntegral() && ActTy->isIntegral() &&
06068        ParamTy->isSigned() == ActTy->isSigned() &&
06069        ParamTy->getPrimitiveSize() >= ActTy->getPrimitiveSize()) ||
06070       (c && ParamTy->getPrimitiveSize() >= ActTy->getPrimitiveSize() &&
06071        c->getValue() > 0);
06072     if (Callee->isExternal() && !isConvertible) return false;
06073   }
06074 
06075   if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
06076       Callee->isExternal())
06077     return false;   // Do not delete arguments unless we have a function body...
06078 
06079   // Okay, we decided that this is a safe thing to do: go ahead and start
06080   // inserting cast instructions as necessary...
06081   std::vector<Value*> Args;
06082   Args.reserve(NumActualArgs);
06083 
06084   AI = CS.arg_begin();
06085   for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
06086     const Type *ParamTy = FT->getParamType(i);
06087     if ((*AI)->getType() == ParamTy) {
06088       Args.push_back(*AI);
06089     } else {
06090       Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"),
06091                                          *Caller));
06092     }
06093   }
06094 
06095   // If the function takes more arguments than the call was taking, add them
06096   // now...
06097   for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
06098     Args.push_back(Constant::getNullValue(FT->getParamType(i)));
06099 
06100   // If we are removing arguments to the function, emit an obnoxious warning...
06101   if (FT->getNumParams() < NumActualArgs)
06102     if (!FT->isVarArg()) {
06103       std::cerr << "WARNING: While resolving call to function '"
06104                 << Callee->getName() << "' arguments were dropped!\n";
06105     } else {
06106       // Add all of the arguments in their promoted form to the arg list...
06107       for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
06108         const Type *PTy = getPromotedType((*AI)->getType());
06109         if (PTy != (*AI)->getType()) {
06110           // Must promote to pass through va_arg area!
06111           Instruction *Cast = new CastInst(*AI, PTy, "tmp");
06112           InsertNewInstBefore(Cast, *Caller);
06113           Args.push_back(Cast);
06114         } else {
06115           Args.push_back(*AI);
06116         }
06117       }
06118     }
06119 
06120   if (FT->getReturnType() == Type::VoidTy)
06121     Caller->setName("");   // Void type should not have a name...
06122 
06123   Instruction *NC;
06124   if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
06125     NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(),
06126                         Args, Caller->getName(), Caller);
06127     cast<InvokeInst>(II)->setCallingConv(II->getCallingConv());
06128   } else {
06129     NC = new CallInst(Callee, Args, Caller->getName(), Caller);
06130     if (cast<CallInst>(Caller)->isTailCall())
06131       cast<CallInst>(NC)->setTailCall();
06132    cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv());
06133   }
06134 
06135   // Insert a cast of the return type as necessary...
06136   Value *NV = NC;
06137   if (Caller->getType() != NV->getType() && !Caller->use_empty()) {
06138     if (NV->getType() != Type::VoidTy) {
06139       NV = NC = new CastInst(NC, Caller->getType(), "tmp");
06140 
06141       // If this is an invoke instruction, we should insert it after the first
06142       // non-phi, instruction in the normal successor block.
06143       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
06144         BasicBlock::iterator I = II->getNormalDest()->begin();
06145         while (isa<PHINode>(I)) ++I;
06146         InsertNewInstBefore(NC, *I);
06147       } else {
06148         // Otherwise, it's a call, just insert cast right after the call instr
06149         InsertNewInstBefore(NC, *Caller);
06150       }
06151       AddUsersToWorkList(*Caller);
06152     } else {
06153       NV = UndefValue::get(Caller->getType());
06154     }
06155   }
06156 
06157   if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
06158     Caller->replaceAllUsesWith(NV);
06159   Caller->getParent()->getInstList().erase(Caller);
06160   removeFromWorkList(Caller);
06161   return true;
06162 }
06163 
06164 
06165 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
06166 // operator and they all are only used by the PHI, PHI together their
06167 // inputs, and do the operation once, to the result of the PHI.
06168 Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
06169   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
06170 
06171   // Scan the instruction, looking for input operations that can be folded away.
06172   // If all input operands to the phi are the same instruction (e.g. a cast from
06173   // the same type or "+42") we can pull the operation through the PHI, reducing
06174   // code size and simplifying code.
06175   Constant *ConstantOp = 0;
06176   const Type *CastSrcTy = 0;
06177   if (isa<CastInst>(FirstInst)) {
06178     CastSrcTy = FirstInst->getOperand(0)->getType();
06179   } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) {
06180     // Can fold binop or shift if the RHS is a constant.
06181     ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
06182     if (ConstantOp == 0) return 0;
06183   } else {
06184     return 0;  // Cannot fold this operation.
06185   }
06186 
06187   // Check to see if all arguments are the same operation.
06188   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
06189     if (!isa<Instruction>(PN.getIncomingValue(i))) return 0;
06190     Instruction *I = cast<Instruction>(PN.getIncomingValue(i));
06191     if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode())
06192       return 0;
06193     if (CastSrcTy) {
06194       if (I->getOperand(0)->getType() != CastSrcTy)
06195         return 0;  // Cast operation must match.
06196     } else if (I->getOperand(1) != ConstantOp) {
06197       return 0;
06198     }
06199   }
06200 
06201   // Okay, they are all the same operation.  Create a new PHI node of the
06202   // correct type, and PHI together all of the LHS's of the instructions.
06203   PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(),
06204                                PN.getName()+".in");
06205   NewPN->reserveOperandSpace(PN.getNumOperands()/2);
06206 
06207   Value *InVal = FirstInst->getOperand(0);
06208   NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
06209 
06210   // Add all operands to the new PHI.
06211   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
06212     Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
06213     if (NewInVal != InVal)
06214       InVal = 0;
06215     NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
06216   }
06217 
06218   Value *PhiVal;
06219   if (InVal) {
06220     // The new PHI unions all of the same values together.  This is really
06221     // common, so we handle it intelligently here for compile-time speed.
06222     PhiVal = InVal;
06223     delete NewPN;
06224   } else {
06225     InsertNewInstBefore(NewPN, PN);
06226     PhiVal = NewPN;
06227   }
06228 
06229   // Insert and return the new operation.
06230   if (isa<CastInst>(FirstInst))
06231     return new CastInst(PhiVal, PN.getType());
06232   else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
06233     return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp);
06234   else
06235     return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(),
06236                          PhiVal, ConstantOp);
06237 }
06238 
06239 /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle
06240 /// that is dead.
06241 static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) {
06242   if (PN->use_empty()) return true;
06243   if (!PN->hasOneUse()) return false;
06244 
06245   // Remember this node, and if we find the cycle, return.
06246   if (!PotentiallyDeadPHIs.insert(PN).second)
06247     return true;
06248 
06249   if (PHINode *PU = dyn_cast<PHINode>(PN->use_back()))
06250     return DeadPHICycle(PU, PotentiallyDeadPHIs);
06251 
06252   return false;
06253 }
06254 
06255 // PHINode simplification
06256 //
06257 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
06258   // If LCSSA is around, don't mess with Phi nodes
06259   if (mustPreserveAnalysisID(LCSSAID)) return 0;
06260   
06261   if (Value *V = PN.hasConstantValue())
06262     return ReplaceInstUsesWith(PN, V);
06263 
06264   // If the only user of this instruction is a cast instruction, and all of the
06265   // incoming values are constants, change this PHI to merge together the casted
06266   // constants.
06267   if (PN.hasOneUse())
06268     if (CastInst *CI = dyn_cast<CastInst>(PN.use_back()))
06269       if (CI->getType() != PN.getType()) {  // noop casts will be folded
06270         bool AllConstant = true;
06271         for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
06272           if (!isa<Constant>(PN.getIncomingValue(i))) {
06273             AllConstant = false;
06274             break;
06275           }
06276         if (AllConstant) {
06277           // Make a new PHI with all casted values.
06278           PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN);
06279           for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
06280             Constant *OldArg = cast<Constant>(PN.getIncomingValue(i));
06281             New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()),
06282                              PN.getIncomingBlock(i));
06283           }
06284 
06285           // Update the cast instruction.
06286           CI->setOperand(0, New);
06287           WorkList.push_back(CI);    // revisit the cast instruction to fold.
06288           WorkList.push_back(New);   // Make sure to revisit the new Phi
06289           return &PN;                // PN is now dead!
06290         }
06291       }
06292 
06293   // If all PHI operands are the same operation, pull them through the PHI,
06294   // reducing code size.
06295   if (isa<Instruction>(PN.getIncomingValue(0)) &&
06296       PN.getIncomingValue(0)->hasOneUse())
06297     if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
06298       return Result;
06299 
06300   // If this is a trivial cycle in the PHI node graph, remove it.  Basically, if
06301   // this PHI only has a single use (a PHI), and if that PHI only has one use (a
06302   // PHI)... break the cycle.
06303   if (PN.hasOneUse())
06304     if (PHINode *PU = dyn_cast<PHINode>(PN.use_back())) {
06305       std::set<PHINode*> PotentiallyDeadPHIs;
06306       PotentiallyDeadPHIs.insert(&PN);
06307       if (DeadPHICycle(PU, PotentiallyDeadPHIs))
06308         return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
06309     }
06310 
06311   return 0;
06312 }
06313 
06314 static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy,
06315                                       Instruction *InsertPoint,
06316                                       InstCombiner *IC) {
06317   unsigned PS = IC->getTargetData().getPointerSize();
06318   const Type *VTy = V->getType();
06319   if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS)
06320     // We must insert a cast to ensure we sign-extend.
06321     V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(),
06322                                              V->getName()), *InsertPoint);
06323   return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()),
06324                                  *InsertPoint);
06325 }
06326 
06327 
06328 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
06329   Value *PtrOp = GEP.getOperand(0);
06330   // Is it 'getelementptr %P, long 0'  or 'getelementptr %P'
06331   // If so, eliminate the noop.
06332   if (GEP.getNumOperands() == 1)
06333     return ReplaceInstUsesWith(GEP, PtrOp);
06334 
06335   if (isa<UndefValue>(GEP.getOperand(0)))
06336     return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
06337 
06338   bool HasZeroPointerIndex = false;
06339   if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
06340     HasZeroPointerIndex = C->isNullValue();
06341 
06342   if (GEP.getNumOperands() == 2 && HasZeroPointerIndex)
06343     return ReplaceInstUsesWith(GEP, PtrOp);
06344 
06345   // Eliminate unneeded casts for indices.
06346   bool MadeChange = false;
06347   gep_type_iterator GTI = gep_type_begin(GEP);
06348   for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI)
06349     if (isa<SequentialType>(*GTI)) {
06350       if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) {
06351         Value *Src = CI->getOperand(0);
06352         const Type *SrcTy = Src->getType();
06353         const Type *DestTy = CI->getType();
06354         if (Src->getType()->isInteger()) {
06355           if (SrcTy->getPrimitiveSizeInBits() ==
06356                        DestTy->getPrimitiveSizeInBits()) {
06357             // We can always eliminate a cast from ulong or long to the other.
06358             // We can always eliminate a cast from uint to int or the other on
06359             // 32-bit pointer platforms.
06360             if (DestTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()){
06361               MadeChange = true;
06362               GEP.setOperand(i, Src);
06363             }
06364           } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() &&
06365                      SrcTy->getPrimitiveSize() == 4) {
06366             // We can always eliminate a cast from int to [u]long.  We can
06367             // eliminate a cast from uint to [u]long iff the target is a 32-bit
06368             // pointer target.
06369             if (SrcTy->isSigned() ||
06370                 SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) {
06371               MadeChange = true;
06372               GEP.setOperand(i, Src);
06373             }
06374           }
06375         }
06376       }
06377       // If we are using a wider index than needed for this platform, shrink it
06378       // to what we need.  If the incoming value needs a cast instruction,
06379       // insert it.  This explicit cast can make subsequent optimizations more
06380       // obvious.
06381       Value *Op = GEP.getOperand(i);
06382       if (Op->getType()->getPrimitiveSize() > TD->getPointerSize())
06383         if (Constant *C = dyn_cast<Constant>(Op)) {
06384           GEP.setOperand(i, ConstantExpr::getCast(C,
06385                                      TD->getIntPtrType()->getSignedVersion()));
06386           MadeChange = true;
06387         } else {
06388           Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(),
06389                                                 Op->getName()), GEP);
06390           GEP.setOperand(i, Op);
06391           MadeChange = true;
06392         }
06393 
06394       // If this is a constant idx, make sure to canonicalize it to be a signed
06395       // operand, otherwise CSE and other optimizations are pessimized.
06396       if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) {
06397         GEP.setOperand(i, ConstantExpr::getCast(CUI,
06398                                           CUI->getType()->getSignedVersion()));
06399         MadeChange = true;
06400       }
06401     }
06402   if (MadeChange) return &GEP;
06403 
06404   // Combine Indices - If the source pointer to this getelementptr instruction
06405   // is a getelementptr instruction, combine the indices of the two
06406   // getelementptr instructions into a single instruction.
06407   //
06408   std::vector<Value*> SrcGEPOperands;
06409   if (User *Src = dyn_castGetElementPtr(PtrOp))
06410     SrcGEPOperands.assign(Src->op_begin(), Src->op_end());
06411 
06412   if (!SrcGEPOperands.empty()) {
06413     // Note that if our source is a gep chain itself that we wait for that
06414     // chain to be resolved before we perform this transformation.  This
06415     // avoids us creating a TON of code in some cases.
06416     //
06417     if (isa<GetElementPtrInst>(SrcGEPOperands[0]) &&
06418         cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2)
06419       return 0;   // Wait until our source is folded to completion.
06420 
06421     std::vector<Value *> Indices;
06422 
06423     // Find out whether the last index in the source GEP is a sequential idx.
06424     bool EndsWithSequential = false;
06425     for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)),
06426            E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I)
06427       EndsWithSequential = !isa<StructType>(*I);
06428 
06429     // Can we combine the two pointer arithmetics offsets?
06430     if (EndsWithSequential) {
06431       // Replace: gep (gep %P, long B), long A, ...
06432       // With:    T = long A+B; gep %P, T, ...
06433       //
06434       Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1);
06435       if (SO1 == Constant::getNullValue(SO1->getType())) {
06436         Sum = GO1;
06437       } else if (GO1 == Constant::getNullValue(GO1->getType())) {
06438         Sum = SO1;
06439       } else {
06440         // If they aren't the same type, convert both to an integer of the
06441         // target's pointer size.
06442         if (SO1->getType() != GO1->getType()) {
06443           if (Constant *SO1C = dyn_cast<Constant>(SO1)) {
06444             SO1 = ConstantExpr::getCast(SO1C, GO1->getType());
06445           } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
06446             GO1 = ConstantExpr::getCast(GO1C, SO1->getType());
06447           } else {
06448             unsigned PS = TD->getPointerSize();
06449             if (SO1->getType()->getPrimitiveSize() == PS) {
06450               // Convert GO1 to SO1's type.
06451               GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this);
06452 
06453             } else if (GO1->getType()->getPrimitiveSize() == PS) {
06454               // Convert SO1 to GO1's type.
06455               SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this);
06456             } else {
06457               const Type *PT = TD->getIntPtrType();
06458               SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this);
06459               GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this);
06460             }
06461           }
06462         }
06463         if (isa<Constant>(SO1) && isa<Constant>(GO1))
06464           Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1));
06465         else {
06466           Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum");
06467           InsertNewInstBefore(cast<Instruction>(Sum), GEP);
06468         }
06469       }
06470 
06471       // Recycle the GEP we already have if possible.
06472       if (SrcGEPOperands.size() == 2) {
06473         GEP.setOperand(0, SrcGEPOperands[0]);
06474         GEP.setOperand(1, Sum);
06475         return &GEP;
06476       } else {
06477         Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
06478                        SrcGEPOperands.end()-1);
06479         Indices.push_back(Sum);
06480         Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end());
06481       }
06482     } else if (isa<Constant>(*GEP.idx_begin()) &&
06483                cast<Constant>(*GEP.idx_begin())->isNullValue() &&
06484                SrcGEPOperands.size() != 1) {
06485       // Otherwise we can do the fold if the first index of the GEP is a zero
06486       Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
06487                      SrcGEPOperands.end());
06488       Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
06489     }
06490 
06491     if (!Indices.empty())
06492       return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName());
06493 
06494   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) {
06495     // GEP of global variable.  If all of the indices for this GEP are
06496     // constants, we can promote this to a constexpr instead of an instruction.
06497 
06498     // Scan for nonconstants...
06499     std::vector<Constant*> Indices;
06500     User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
06501     for (; I != E && isa<Constant>(*I); ++I)
06502       Indices.push_back(cast<Constant>(*I));
06503 
06504     if (I == E) {  // If they are all constants...
06505       Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices);
06506 
06507       // Replace all uses of the GEP with the new constexpr...
06508       return ReplaceInstUsesWith(GEP, CE);
06509     }
06510   } else if (Value *X = isCast(PtrOp)) {  // Is the operand a cast?
06511     if (!isa<PointerType>(X->getType())) {
06512       // Not interesting.  Source pointer must be a cast from pointer.
06513     } else if (HasZeroPointerIndex) {
06514       // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ...
06515       // into     : GEP [10 x ubyte]* X, long 0, ...
06516       //
06517       // This occurs when the program declares an array extern like "int X[];"
06518       //
06519       const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
06520       const PointerType *XTy = cast<PointerType>(X->getType());
06521       if (const ArrayType *XATy =
06522           dyn_cast<ArrayType>(XTy->getElementType()))
06523         if (const ArrayType *CATy =
06524             dyn_cast<ArrayType>(CPTy->getElementType()))
06525           if (CATy->getElementType() == XATy->getElementType()) {
06526             // At this point, we know that the cast source type is a pointer
06527             // to an array of the same type as the destination pointer
06528             // array.  Because the array type is never stepped over (there
06529             // is a leading zero) we can fold the cast into this GEP.
06530             GEP.setOperand(0, X);
06531             return &GEP;
06532           }
06533     } else if (GEP.getNumOperands() == 2) {
06534       // Transform things like:
06535       // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V
06536       // into:  %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast
06537       const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
06538       const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
06539       if (isa<ArrayType>(SrcElTy) &&
06540           TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
06541           TD->getTypeSize(ResElTy)) {
06542         Value *V = InsertNewInstBefore(
06543                new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy),
06544                                      GEP.getOperand(1), GEP.getName()), GEP);
06545         return new CastInst(V, GEP.getType());
06546       }
06547       
06548       // Transform things like:
06549       // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp
06550       //   (where tmp = 8*tmp2) into:
06551       // getelementptr [100 x double]* %arr, int 0, int %tmp.2
06552       
06553       if (isa<ArrayType>(SrcElTy) &&
06554           (ResElTy == Type::SByteTy || ResElTy == Type::UByteTy)) {
06555         uint64_t ArrayEltSize =
06556             TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType());
06557         
06558         // Check to see if "tmp" is a scale by a multiple of ArrayEltSize.  We
06559         // allow either a mul, shift, or constant here.
06560         Value *NewIdx = 0;
06561         ConstantInt *Scale = 0;
06562         if (ArrayEltSize == 1) {
06563           NewIdx = GEP.getOperand(1);
06564           Scale = ConstantInt::get(NewIdx->getType(), 1);
06565         } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
06566           NewIdx = ConstantInt::get(CI->getType(), 1);
06567           Scale = CI;
06568         } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
06569           if (Inst->getOpcode() == Instruction::Shl &&
06570               isa<ConstantInt>(Inst->getOperand(1))) {
06571             unsigned ShAmt =cast<ConstantUInt>(Inst->getOperand(1))->getValue();
06572             if (Inst->getType()->isSigned())
06573               Scale = ConstantSInt::get(Inst->getType(), 1ULL << ShAmt);
06574             else
06575               Scale = ConstantUInt::get(Inst->getType(), 1ULL << ShAmt);
06576             NewIdx = Inst->getOperand(0);
06577           } else if (Inst->getOpcode() == Instruction::Mul &&
06578                      isa<ConstantInt>(Inst->getOperand(1))) {
06579             Scale = cast<ConstantInt>(Inst->getOperand(1));
06580             NewIdx = Inst->getOperand(0);
06581           }
06582         }
06583 
06584         // If the index will be to exactly the right offset with the scale taken
06585         // out, perform the transformation.
06586         if (Scale && Scale->getRawValue() % ArrayEltSize == 0) {
06587           if (ConstantSInt *C = dyn_cast<ConstantSInt>(Scale))
06588             Scale = ConstantSInt::get(C->getType(),
06589                                       (int64_t)C->getRawValue() / 
06590                                       (int64_t)ArrayEltSize);
06591           else
06592             Scale = ConstantUInt::get(Scale->getType(),
06593                                       Scale->getRawValue() / ArrayEltSize);
06594           if (Scale->getRawValue() != 1) {
06595             Constant *C = ConstantExpr::getCast(Scale, NewIdx->getType());
06596             Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale");
06597             NewIdx = InsertNewInstBefore(Sc, GEP);
06598           }
06599 
06600           // Insert the new GEP instruction.
06601           Instruction *Idx =
06602             new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy),
06603                                   NewIdx, GEP.getName());
06604           Idx = InsertNewInstBefore(Idx, GEP);
06605           return new CastInst(Idx, GEP.getType());
06606         }
06607       }
06608     }
06609   }
06610 
06611   return 0;
06612 }
06613 
06614 Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
06615   // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
06616   if (AI.isArrayAllocation())    // Check C != 1
06617     if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
06618       const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
06619       AllocationInst *New = 0;
06620 
06621       // Create and insert the replacement instruction...
06622       if (isa<MallocInst>(AI))
06623         New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName());
06624       else {
06625         assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
06626         New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName());
06627       }
06628 
06629       InsertNewInstBefore(New, AI);
06630 
06631       // Scan to the end of the allocation instructions, to skip over a block of
06632       // allocas if possible...
06633       //
06634       BasicBlock::iterator It = New;
06635       while (isa<AllocationInst>(*It)) ++It;
06636 
06637       // Now that I is pointing to the first non-allocation-inst in the block,
06638       // insert our getelementptr instruction...
06639       //
06640       Value *NullIdx = Constant::getNullValue(Type::IntTy);
06641       Value *V = new GetElementPtrInst(New, NullIdx, NullIdx,
06642                                        New->getName()+".sub", It);
06643 
06644       // Now make everything use the getelementptr instead of the original
06645       // allocation.
06646       return ReplaceInstUsesWith(AI, V);
06647     } else if (isa<UndefValue>(AI.getArraySize())) {
06648       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
06649     }
06650 
06651   // If alloca'ing a zero byte object, replace the alloca with a null pointer.
06652   // Note that we only do this for alloca's, because malloc should allocate and
06653   // return a unique pointer, even for a zero byte allocation.
06654   if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() &&
06655       TD->getTypeSize(AI.getAllocatedType()) == 0)
06656     return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
06657 
06658   return 0;
06659 }
06660 
06661 Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
06662   Value *Op = FI.getOperand(0);
06663 
06664   // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X
06665   if (CastInst *CI = dyn_cast<CastInst>(Op))
06666     if (isa<PointerType>(CI->getOperand(0)->getType())) {
06667       FI.setOperand(0, CI->getOperand(0));
06668       return &FI;
06669     }
06670 
06671   // free undef -> unreachable.
06672   if (isa<UndefValue>(Op)) {
06673     // Insert a new store to null because we cannot modify the CFG here.
06674     new StoreInst(ConstantBool::True,
06675                   UndefValue::get(PointerType::get(Type::BoolTy)), &FI);
06676     return EraseInstFromFunction(FI);
06677   }
06678 
06679   // If we have 'free null' delete the instruction.  This can happen in stl code
06680   // when lots of inlining happens.
06681   if (isa<ConstantPointerNull>(Op))
06682     return EraseInstFromFunction(FI);
06683 
06684   return 0;
06685 }
06686 
06687 
06688 /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
06689 static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) {
06690   User *CI = cast<User>(LI.getOperand(0));
06691   Value *CastOp = CI->getOperand(0);
06692 
06693   const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
06694   if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
06695     const Type *SrcPTy = SrcTy->getElementType();
06696 
06697     if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || 
06698         isa<PackedType>(DestPTy)) {
06699       // If the source is an array, the code below will not succeed.  Check to
06700       // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
06701       // constants.
06702       if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
06703         if (Constant *CSrc = dyn_cast<Constant>(CastOp))
06704           if (ASrcTy->getNumElements() != 0) {
06705             std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy));
06706             CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs);
06707             SrcTy = cast<PointerType>(CastOp->getType());
06708             SrcPTy = SrcTy->getElementType();
06709           }
06710 
06711       if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || 
06712            isa<PackedType>(SrcPTy)) &&
06713           // Do not allow turning this into a load of an integer, which is then
06714           // casted to a pointer, this pessimizes pointer analysis a lot.
06715           (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) &&
06716           IC.getTargetData().getTypeSize(SrcPTy) ==
06717                IC.getTargetData().getTypeSize(DestPTy)) {
06718 
06719         // Okay, we are casting from one integer or pointer type to another of
06720         // the same size.  Instead of casting the pointer before the load, cast
06721         // the result of the loaded value.
06722         Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp,
06723                                                              CI->getName(),
06724                                                          LI.isVolatile()),LI);
06725         // Now cast the result of the load.
06726         return new CastInst(NewLoad, LI.getType());
06727       }
06728     }
06729   }
06730   return 0;
06731 }
06732 
06733 /// isSafeToLoadUnconditionally - Return true if we know that executing a load
06734 /// from this value cannot trap.  If it is not obviously safe to load from the
06735 /// specified pointer, we do a quick local scan of the basic block containing
06736 /// ScanFrom, to determine if the address is already accessed.
06737 static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) {
06738   // If it is an alloca or global variable, it is always safe to load from.
06739   if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true;
06740 
06741   // Otherwise, be a little bit agressive by scanning the local block where we
06742   // want to check to see if the pointer is already being loaded or stored
06743   // from/to.  If so, the previous load or store would have already trapped,
06744   // so there is no harm doing an extra load (also, CSE will later eliminate
06745   // the load entirely).
06746   BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
06747 
06748   while (BBI != E) {
06749     --BBI;
06750 
06751     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
06752       if (LI->getOperand(0) == V) return true;
06753     } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
06754       if (SI->getOperand(1) == V) return true;
06755 
06756   }
06757   return false;
06758 }
06759 
06760 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
06761   Value *Op = LI.getOperand(0);
06762 
06763   // load (cast X) --> cast (load X) iff safe
06764   if (CastInst *CI = dyn_cast<CastInst>(Op))
06765     if (Instruction *Res = InstCombineLoadCast(*this, LI))
06766       return Res;
06767 
06768   // None of the following transforms are legal for volatile loads.
06769   if (LI.isVolatile()) return 0;
06770   
06771   if (&LI.getParent()->front() != &LI) {
06772     BasicBlock::iterator BBI = &LI; --BBI;
06773     // If the instruction immediately before this is a store to the same
06774     // address, do a simple form of store->load forwarding.
06775     if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
06776       if (SI->getOperand(1) == LI.getOperand(0))
06777         return ReplaceInstUsesWith(LI, SI->getOperand(0));
06778     if (LoadInst *LIB = dyn_cast<LoadInst>(BBI))
06779       if (LIB->getOperand(0) == LI.getOperand(0))
06780         return ReplaceInstUsesWith(LI, LIB);
06781   }
06782 
06783   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op))
06784     if (isa<ConstantPointerNull>(GEPI->getOperand(0)) ||
06785         isa<UndefValue>(GEPI->getOperand(0))) {
06786       // Insert a new store to null instruction before the load to indicate
06787       // that this code is not reachable.  We do this instead of inserting
06788       // an unreachable instruction directly because we cannot modify the
06789       // CFG.
06790       new StoreInst(UndefValue::get(LI.getType()),
06791                     Constant::getNullValue(Op->getType()), &LI);
06792       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
06793     }
06794 
06795   if (Constant *C = dyn_cast<Constant>(Op)) {
06796     // load null/undef -> undef
06797     if ((C->isNullValue() || isa<UndefValue>(C))) {
06798       // Insert a new store to null instruction before the load to indicate that
06799       // this code is not reachable.  We do this instead of inserting an
06800       // unreachable instruction directly because we cannot modify the CFG.
06801       new StoreInst(UndefValue::get(LI.getType()),
06802                     Constant::getNullValue(Op->getType()), &LI);
06803       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
06804     }
06805 
06806     // Instcombine load (constant global) into the value loaded.
06807     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
06808       if (GV->isConstant() && !GV->isExternal())
06809         return ReplaceInstUsesWith(LI, GV->getInitializer());
06810 
06811     // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded.
06812     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
06813       if (CE->getOpcode() == Instruction::GetElementPtr) {
06814         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
06815           if (GV->isConstant() && !GV->isExternal())
06816             if (Constant *V = 
06817                ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
06818               return ReplaceInstUsesWith(LI, V);
06819         if (CE->getOperand(0)->isNullValue()) {
06820           // Insert a new store to null instruction before the load to indicate
06821           // that this code is not reachable.  We do this instead of inserting
06822           // an unreachable instruction directly because we cannot modify the
06823           // CFG.
06824           new StoreInst(UndefValue::get(LI.getType()),
06825                         Constant::getNullValue(Op->getType()), &LI);
06826           return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
06827         }
06828 
06829       } else if (CE->getOpcode() == Instruction::Cast) {
06830         if (Instruction *Res = InstCombineLoadCast(*this, LI))
06831           return Res;
06832       }
06833   }
06834 
06835   if (Op->hasOneUse()) {
06836     // Change select and PHI nodes to select values instead of addresses: this
06837     // helps alias analysis out a lot, allows many others simplifications, and
06838     // exposes redundancy in the code.
06839     //
06840     // Note that we cannot do the transformation unless we know that the
06841     // introduced loads cannot trap!  Something like this is valid as long as
06842     // the condition is always false: load (select bool %C, int* null, int* %G),
06843     // but it would not be valid if we transformed it to load from null
06844     // unconditionally.
06845     //
06846     if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
06847       // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2).
06848       if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) &&
06849           isSafeToLoadUnconditionally(SI->getOperand(2), SI)) {
06850         Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1),
06851                                      SI->getOperand(1)->getName()+".val"), LI);
06852         Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2),
06853                                      SI->getOperand(2)->getName()+".val"), LI);
06854         return new SelectInst(SI->getCondition(), V1, V2);
06855       }
06856 
06857       // load (select (cond, null, P)) -> load P
06858       if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
06859         if (C->isNullValue()) {
06860           LI.setOperand(0, SI->getOperand(2));
06861           return &LI;
06862         }
06863 
06864       // load (select (cond, P, null)) -> load P
06865       if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
06866         if (C->isNullValue()) {
06867           LI.setOperand(0, SI->getOperand(1));
06868           return &LI;
06869         }
06870 
06871     } else if (PHINode *PN = dyn_cast<PHINode>(Op)) {
06872       // load (phi (&V1, &V2, &V3))  --> phi(load &V1, load &V2, load &V3)
06873       bool Safe = PN->getParent() == LI.getParent();
06874 
06875       // Scan all of the instructions between the PHI and the load to make
06876       // sure there are no instructions that might possibly alter the value
06877       // loaded from the PHI.
06878       if (Safe) {
06879         BasicBlock::iterator I = &LI;
06880         for (--I; !isa<PHINode>(I); --I)
06881           if (isa<StoreInst>(I) || isa<CallInst>(I)) {
06882             Safe = false;
06883             break;
06884           }
06885       }
06886 
06887       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i)
06888         if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i),
06889                                     PN->getIncomingBlock(i)->getTerminator()))
06890           Safe = false;
06891 
06892       if (Safe) {
06893         // Create the PHI.
06894         PHINode *NewPN = new PHINode(LI.getType(), PN->getName());
06895         InsertNewInstBefore(NewPN, *PN);
06896         std::map<BasicBlock*,Value*> LoadMap;  // Don't insert duplicate loads
06897 
06898         for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
06899           BasicBlock *BB = PN->getIncomingBlock(i);
06900           Value *&TheLoad = LoadMap[BB];
06901           if (TheLoad == 0) {
06902             Value *InVal = PN->getIncomingValue(i);
06903             TheLoad = InsertNewInstBefore(new LoadInst(InVal,
06904                                                        InVal->getName()+".val"),
06905                                           *BB->getTerminator());
06906           }
06907           NewPN->addIncoming(TheLoad, BB);
06908         }
06909         return ReplaceInstUsesWith(LI, NewPN);
06910       }
06911     }
06912   }
06913   return 0;
06914 }
06915 
06916 /// InstCombineStoreToCast - Fold 'store V, (cast P)' -> store (cast V), P'
06917 /// when possible.
06918 static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
06919   User *CI = cast<User>(SI.getOperand(1));
06920   Value *CastOp = CI->getOperand(0);
06921 
06922   const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
06923   if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
06924     const Type *SrcPTy = SrcTy->getElementType();
06925 
06926     if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) {
06927       // If the source is an array, the code below will not succeed.  Check to
06928       // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
06929       // constants.
06930       if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
06931         if (Constant *CSrc = dyn_cast<Constant>(CastOp))
06932           if (ASrcTy->getNumElements() != 0) {
06933             std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy));
06934             CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs);
06935             SrcTy = cast<PointerType>(CastOp->getType());
06936             SrcPTy = SrcTy->getElementType();
06937           }
06938 
06939       if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) &&
06940           IC.getTargetData().getTypeSize(SrcPTy) ==
06941                IC.getTargetData().getTypeSize(DestPTy)) {
06942 
06943         // Okay, we are casting from one integer or pointer type to another of
06944         // the same size.  Instead of casting the pointer before the store, cast
06945         // the value to be stored.
06946         Value *NewCast;
06947         if (Constant *C = dyn_cast<Constant>(SI.getOperand(0)))
06948           NewCast = ConstantExpr::getCast(C, SrcPTy);
06949         else
06950           NewCast = IC.InsertNewInstBefore(new CastInst(SI.getOperand(0),
06951                                                         SrcPTy,
06952                                          SI.getOperand(0)->getName()+".c"), SI);
06953 
06954         return new StoreInst(NewCast, CastOp);
06955       }
06956     }
06957   }
06958   return 0;
06959 }
06960 
06961 Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
06962   Value *Val = SI.getOperand(0);
06963   Value *Ptr = SI.getOperand(1);
06964 
06965   if (isa<UndefValue>(Ptr)) {     // store X, undef -> noop (even if volatile)
06966     EraseInstFromFunction(SI);
06967     ++NumCombined;
06968     return 0;
06969   }
06970 
06971   // Do really simple DSE, to catch cases where there are several consequtive
06972   // stores to the same location, separated by a few arithmetic operations. This
06973   // situation often occurs with bitfield accesses.
06974   BasicBlock::iterator BBI = &SI;
06975   for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
06976        --ScanInsts) {
06977     --BBI;
06978     
06979     if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
06980       // Prev store isn't volatile, and stores to the same location?
06981       if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) {
06982         ++NumDeadStore;
06983         ++BBI;
06984         EraseInstFromFunction(*PrevSI);
06985         continue;
06986       }
06987       break;
06988     }
06989     
06990     // If this is a load, we have to stop.  However, if the loaded value is from
06991     // the pointer we're loading and is producing the pointer we're storing,
06992     // then *this* store is dead (X = load P; store X -> P).
06993     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
06994       if (LI == Val && LI->getOperand(0) == Ptr) {
06995         EraseInstFromFunction(SI);
06996         ++NumCombined;
06997         return 0;
06998       }
06999       // Otherwise, this is a load from some other location.  Stores before it
07000       // may not be dead.
07001       break;
07002     }
07003     
07004     // Don't skip over loads or things that can modify memory.
07005     if (BBI->mayWriteToMemory())
07006       break;
07007   }
07008   
07009   
07010   if (SI.isVolatile()) return 0;  // Don't hack volatile stores.
07011 
07012   // store X, null    -> turns into 'unreachable' in SimplifyCFG
07013   if (isa<ConstantPointerNull>(Ptr)) {
07014     if (!isa<UndefValue>(Val)) {
07015       SI.setOperand(0, UndefValue::get(Val->getType()));
07016       if (Instruction *U = dyn_cast<Instruction>(Val))
07017         WorkList.push_back(U);  // Dropped a use.
07018       ++NumCombined;
07019     }
07020     return 0;  // Do not modify these!
07021   }
07022 
07023   // store undef, Ptr -> noop
07024   if (isa<UndefValue>(Val)) {
07025     EraseInstFromFunction(SI);
07026     ++NumCombined;
07027     return 0;
07028   }
07029 
07030   // If the pointer destination is a cast, see if we can fold the cast into the
07031   // source instead.
07032   if (CastInst *CI = dyn_cast<CastInst>(Ptr))
07033     if (Instruction *Res = InstCombineStoreToCast(*this, SI))
07034       return Res;
07035   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
07036     if (CE->getOpcode() == Instruction::Cast)
07037       if (Instruction *Res = InstCombineStoreToCast(*this, SI))
07038         return Res;
07039 
07040   
07041   // If this store is the last instruction in the basic block, and if the block
07042   // ends with an unconditional branch, try to move it to the successor block.
07043   BBI = &SI; ++BBI;
07044   if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
07045     if (BI->isUnconditional()) {
07046       // Check to see if the successor block has exactly two incoming edges.  If
07047       // so, see if the other predecessor contains a store to the same location.
07048       // if so, insert a PHI node (if needed) and move the stores down.
07049       BasicBlock *Dest = BI->getSuccessor(0);
07050 
07051       pred_iterator PI = pred_begin(Dest);
07052       BasicBlock *Other = 0;
07053       if (*PI != BI->getParent())
07054         Other = *PI;
07055       ++PI;
07056       if (PI != pred_end(Dest)) {
07057         if (*PI != BI->getParent())
07058           if (Other)
07059             Other = 0;
07060           else
07061             Other = *PI;
07062         if (++PI != pred_end(Dest))
07063           Other = 0;
07064       }
07065       if (Other) {  // If only one other pred...
07066         BBI = Other->getTerminator();
07067         // Make sure this other block ends in an unconditional branch and that
07068         // there is an instruction before the branch.
07069         if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() &&
07070             BBI != Other->begin()) {
07071           --BBI;
07072           StoreInst *OtherStore = dyn_cast<StoreInst>(BBI);
07073           
07074           // If this instruction is a store to the same location.
07075           if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) {
07076             // Okay, we know we can perform this transformation.  Insert a PHI
07077             // node now if we need it.
07078             Value *MergedVal = OtherStore->getOperand(0);
07079             if (MergedVal != SI.getOperand(0)) {
07080               PHINode *PN = new PHINode(MergedVal->getType(), "storemerge");
07081               PN->reserveOperandSpace(2);
07082               PN->addIncoming(SI.getOperand(0), SI.getParent());
07083               PN->addIncoming(OtherStore->getOperand(0), Other);
07084               MergedVal = InsertNewInstBefore(PN, Dest->front());
07085             }
07086             
07087             // Advance to a place where it is safe to insert the new store and
07088             // insert it.
07089             BBI = Dest->begin();
07090             while (isa<PHINode>(BBI)) ++BBI;
07091             InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
07092                                               OtherStore->isVolatile()), *BBI);
07093 
07094             // Nuke the old stores.
07095             EraseInstFromFunction(SI);
07096             EraseInstFromFunction(*OtherStore);
07097             ++NumCombined;
07098             return 0;
07099           }
07100         }
07101       }
07102     }
07103   
07104   return 0;
07105 }
07106 
07107 
07108 Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
07109   // Change br (not X), label True, label False to: br X, label False, True
07110   Value *X = 0;
07111   BasicBlock *TrueDest;
07112   BasicBlock *FalseDest;
07113   if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
07114       !isa<Constant>(X)) {
07115     // Swap Destinations and condition...
07116     BI.setCondition(X);
07117     BI.setSuccessor(0, FalseDest);
07118     BI.setSuccessor(1, TrueDest);
07119     return &BI;
07120   }
07121 
07122   // Cannonicalize setne -> seteq
07123   Instruction::BinaryOps Op; Value *Y;
07124   if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)),
07125                       TrueDest, FalseDest)))
07126     if ((Op == Instruction::SetNE || Op == Instruction::SetLE ||
07127          Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) {
07128       SetCondInst *I = cast<SetCondInst>(BI.getCondition());
07129       std::string Name = I->getName(); I->setName("");
07130       Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op);
07131       Value *NewSCC =  BinaryOperator::create(NewOpcode, X, Y, Name, I);
07132       // Swap Destinations and condition...
07133       BI.setCondition(NewSCC);
07134       BI.setSuccessor(0, FalseDest);
07135       BI.setSuccessor(1, TrueDest);
07136       removeFromWorkList(I);
07137       I->getParent()->getInstList().erase(I);
07138       WorkList.push_back(cast<Instruction>(NewSCC));
07139       return &BI;
07140     }
07141 
07142   return 0;
07143 }
07144 
07145 Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
07146   Value *Cond = SI.getCondition();
07147   if (Instruction *I = dyn_cast<Instruction>(Cond)) {
07148     if (I->getOpcode() == Instruction::Add)
07149       if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
07150         // change 'switch (X+4) case 1:' into 'switch (X) case -3'
07151         for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
07152           SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
07153                                                 AddRHS));
07154         SI.setOperand(0, I->getOperand(0));
07155         WorkList.push_back(I);
07156         return &SI;
07157       }
07158   }
07159   return 0;
07160 }
07161 
07162 /// CheapToScalarize - Return true if the value is cheaper to scalarize than it
07163 /// is to leave as a vector operation.
07164 static bool CheapToScalarize(Value *V, bool isConstant) {
07165   if (isa<ConstantAggregateZero>(V)) 
07166     return true;
07167   if (ConstantPacked *C = dyn_cast<ConstantPacked>(V)) {
07168     if (isConstant) return true;
07169     // If all elts are the same, we can extract.
07170     Constant *Op0 = C->getOperand(0);
07171     for (unsigned i = 1; i < C->getNumOperands(); ++i)
07172       if (C->getOperand(i) != Op0)
07173         return false;
07174     return true;
07175   }
07176   Instruction *I = dyn_cast<Instruction>(V);
07177   if (!I) return false;
07178   
07179   // Insert element gets simplified to the inserted element or is deleted if
07180   // this is constant idx extract element and its a constant idx insertelt.
07181   if (I->getOpcode() == Instruction::InsertElement && isConstant &&
07182       isa<ConstantInt>(I->getOperand(2)))
07183     return true;
07184   if (I->getOpcode() == Instruction::Load && I->hasOneUse())
07185     return true;
07186   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
07187     if (BO->hasOneUse() &&
07188         (CheapToScalarize(BO->getOperand(0), isConstant) ||
07189          CheapToScalarize(BO->getOperand(1), isConstant)))
07190       return true;
07191   
07192   return false;
07193 }
07194 
07195 /// getShuffleMask - Read and decode a shufflevector mask.  It turns undef
07196 /// elements into values that are larger than the #elts in the input.
07197 static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) {
07198   unsigned NElts = SVI->getType()->getNumElements();
07199   if (isa<ConstantAggregateZero>(SVI->getOperand(2)))
07200     return std::vector<unsigned>(NElts, 0);
07201   if (isa<UndefValue>(SVI->getOperand(2)))
07202     return std::vector<unsigned>(NElts, 2*NElts);
07203 
07204   std::vector<unsigned> Result;
07205   const ConstantPacked *CP = cast<ConstantPacked>(SVI->getOperand(2));
07206   for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
07207     if (isa<UndefValue>(CP->getOperand(i)))
07208       Result.push_back(NElts*2);  // undef -> 8
07209     else
07210       Result.push_back(cast<ConstantUInt>(CP->getOperand(i))->getValue());
07211   return Result;
07212 }
07213 
07214 /// FindScalarElement - Given a vector and an element number, see if the scalar
07215 /// value is already around as a register, for example if it were inserted then
07216 /// extracted from the vector.
07217 static Value *FindScalarElement(Value *V, unsigned EltNo) {
07218   assert(isa<PackedType>(V->getType()) && "Not looking at a vector?");
07219   const PackedType *PTy = cast<PackedType>(V->getType());
07220   unsigned Width = PTy->getNumElements();
07221   if (EltNo >= Width)  // Out of range access.
07222     return UndefValue::get(PTy->getElementType());
07223   
07224   if (isa<UndefValue>(V))
07225     return UndefValue::get(PTy->getElementType());
07226   else if (isa<ConstantAggregateZero>(V))
07227     return Constant::getNullValue(PTy->getElementType());
07228   else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(V))
07229     return CP->getOperand(EltNo);
07230   else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
07231     // If this is an insert to a variable element, we don't know what it is.
07232     if (!isa<ConstantUInt>(III->getOperand(2))) return 0;
07233     unsigned IIElt = cast<ConstantUInt>(III->getOperand(2))->getValue();
07234     
07235     // If this is an insert to the element we are looking for, return the
07236     // inserted value.
07237     if (EltNo == IIElt) return III->getOperand(1);
07238     
07239     // Otherwise, the insertelement doesn't modify the value, recurse on its
07240     // vector input.
07241     return FindScalarElement(III->getOperand(0), EltNo);
07242   } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
07243     unsigned InEl = getShuffleMask(SVI)[EltNo];
07244     if (InEl < Width)
07245       return FindScalarElement(SVI->getOperand(0), InEl);
07246     else if (InEl < Width*2)
07247       return FindScalarElement(SVI->getOperand(1), InEl - Width);
07248     else
07249       return UndefValue::get(PTy->getElementType());
07250   }
07251   
07252   // Otherwise, we don't know.
07253   return 0;
07254 }
07255 
07256 Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
07257 
07258   // If packed val is undef, replace extract with scalar undef.
07259   if (isa<UndefValue>(EI.getOperand(0)))
07260     return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
07261 
07262   // If packed val is constant 0, replace extract with scalar 0.
07263   if (isa<ConstantAggregateZero>(EI.getOperand(0)))
07264     return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
07265   
07266   if (ConstantPacked *C = dyn_cast<ConstantPacked>(EI.getOperand(0))) {
07267     // If packed val is constant with uniform operands, replace EI
07268     // with that operand
07269     Constant *op0 = C->getOperand(0);
07270     for (unsigned i = 1; i < C->getNumOperands(); ++i)
07271       if (C->getOperand(i) != op0) {
07272         op0 = 0; 
07273         break;
07274       }
07275     if (op0)
07276       return ReplaceInstUsesWith(EI, op0);
07277   }
07278   
07279   // If extracting a specified index from the vector, see if we can recursively
07280   // find a previously computed scalar that was inserted into the vector.
07281   if (ConstantUInt *IdxC = dyn_cast<ConstantUInt>(EI.getOperand(1))) {
07282     if (Value *Elt = FindScalarElement(EI.getOperand(0), IdxC->getValue()))
07283       return ReplaceInstUsesWith(EI, Elt);
07284   }
07285   
07286   if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
07287     if (I->hasOneUse()) {
07288       // Push extractelement into predecessor operation if legal and
07289       // profitable to do so
07290       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
07291         bool isConstantElt = isa<ConstantInt>(EI.getOperand(1));
07292         if (CheapToScalarize(BO, isConstantElt)) {
07293           ExtractElementInst *newEI0 = 
07294             new ExtractElementInst(BO->getOperand(0), EI.getOperand(1),
07295                                    EI.getName()+".lhs");
07296           ExtractElementInst *newEI1 =
07297             new ExtractElementInst(BO->getOperand(1), EI.getOperand(1),
07298                                    EI.getName()+".rhs");
07299           InsertNewInstBefore(newEI0, EI);
07300           InsertNewInstBefore(newEI1, EI);
07301           return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1);
07302         }
07303       } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
07304         Value *Ptr = InsertCastBefore(I->getOperand(0),
07305                                       PointerType::get(EI.getType()), EI);
07306         GetElementPtrInst *GEP = 
07307           new GetElementPtrInst(Ptr, EI.getOperand(1),
07308                                 I->getName() + ".gep");
07309         InsertNewInstBefore(GEP, EI);
07310         return new LoadInst(GEP);
07311       }
07312     }
07313     if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
07314       // Extracting the inserted element?
07315       if (IE->getOperand(2) == EI.getOperand(1))
07316         return ReplaceInstUsesWith(EI, IE->getOperand(1));
07317       // If the inserted and extracted elements are constants, they must not
07318       // be the same value, extract from the pre-inserted value instead.
07319       if (isa<Constant>(IE->getOperand(2)) &&
07320           isa<Constant>(EI.getOperand(1))) {
07321         AddUsesToWorkList(EI);
07322         EI.setOperand(0, IE->getOperand(0));
07323         return &EI;
07324       }
07325     } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
07326       // If this is extracting an element from a shufflevector, figure out where
07327       // it came from and extract from the appropriate input element instead.
07328       if (ConstantUInt *Elt = dyn_cast<ConstantUInt>(EI.getOperand(1))) {
07329         unsigned SrcIdx = getShuffleMask(SVI)[Elt->getValue()];
07330         Value *Src;
07331         if (SrcIdx < SVI->getType()->getNumElements())
07332           Src = SVI->getOperand(0);
07333         else if (SrcIdx < SVI->getType()->getNumElements()*2) {
07334           SrcIdx -= SVI->getType()->getNumElements();
07335           Src = SVI->getOperand(1);
07336         } else {
07337           return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
07338         }
07339         return new ExtractElementInst(Src,
07340                                       ConstantUInt::get(Type::UIntTy, SrcIdx));
07341       }
07342     }
07343   }
07344   return 0;
07345 }
07346 
07347 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
07348 /// elements from either LHS or RHS, return the shuffle mask and true. 
07349 /// Otherwise, return false.
07350 static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
07351                                          std::vector<Constant*> &Mask) {
07352   assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
07353          "Invalid CollectSingleShuffleElements");
07354   unsigned NumElts = cast<PackedType>(V->getType())->getNumElements();
07355 
07356   if (isa<UndefValue>(V)) {
07357     Mask.assign(NumElts, UndefValue::get(Type::UIntTy));
07358     return true;
07359   } else if (V == LHS) {
07360     for (unsigned i = 0; i != NumElts; ++i)
07361       Mask.push_back(ConstantUInt::get(Type::UIntTy, i));
07362     return true;
07363   } else if (V == RHS) {
07364     for (unsigned i = 0; i != NumElts; ++i)
07365       Mask.push_back(ConstantUInt::get(Type::UIntTy, i+NumElts));
07366     return true;
07367   } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
07368     // If this is an insert of an extract from some other vector, include it.
07369     Value *VecOp    = IEI->getOperand(0);
07370     Value *ScalarOp = IEI->getOperand(1);
07371     Value *IdxOp    = IEI->getOperand(2);
07372     
07373     if (!isa<ConstantInt>(IdxOp))
07374       return false;
07375     unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getRawValue();
07376     
07377     if (isa<UndefValue>(ScalarOp)) {  // inserting undef into vector.
07378       // Okay, we can handle this if the vector we are insertinting into is
07379       // transitively ok.
07380       if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
07381         // If so, update the mask to reflect the inserted undef.
07382         Mask[InsertedIdx] = UndefValue::get(Type::UIntTy);
07383         return true;
07384       }      
07385     } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
07386       if (isa<ConstantInt>(EI->getOperand(1)) &&
07387           EI->getOperand(0)->getType() == V->getType()) {
07388         unsigned ExtractedIdx =
07389           cast<ConstantInt>(EI->getOperand(1))->getRawValue();
07390         
07391         // This must be extracting from either LHS or RHS.
07392         if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
07393           // Okay, we can handle this if the vector we are insertinting into is
07394           // transitively ok.
07395           if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
07396             // If so, update the mask to reflect the inserted value.
07397             if (EI->getOperand(0) == LHS) {
07398               Mask[InsertedIdx & (NumElts-1)] = 
07399                  ConstantUInt::get(Type::UIntTy, ExtractedIdx);
07400             } else {
07401               assert(EI->getOperand(0) == RHS);
07402               Mask[InsertedIdx & (NumElts-1)] = 
07403                 ConstantUInt::get(Type::UIntTy, ExtractedIdx+NumElts);
07404               
07405             }
07406             return true;
07407           }
07408         }
07409       }
07410     }
07411   }
07412   // TODO: Handle shufflevector here!
07413   
07414   return false;
07415 }
07416 
07417 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the
07418 /// RHS of the shuffle instruction, if it is not null.  Return a shuffle mask
07419 /// that computes V and the LHS value of the shuffle.
07420 static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
07421                                      Value *&RHS) {
07422   assert(isa<PackedType>(V->getType()) && 
07423          (RHS == 0 || V->getType() == RHS->getType()) &&
07424          "Invalid shuffle!");
07425   unsigned NumElts = cast<PackedType>(V->getType())->getNumElements();
07426 
07427   if (isa<UndefValue>(V)) {
07428     Mask.assign(NumElts, UndefValue::get(Type::UIntTy));
07429     return V;
07430   } else if (isa<ConstantAggregateZero>(V)) {
07431     Mask.assign(NumElts, ConstantUInt::get(Type::UIntTy, 0));
07432     return V;
07433   } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
07434     // If this is an insert of an extract from some other vector, include it.
07435     Value *VecOp    = IEI->getOperand(0);
07436     Value *ScalarOp = IEI->getOperand(1);
07437     Value *IdxOp    = IEI->getOperand(2);
07438     
07439     if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
07440       if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
07441           EI->getOperand(0)->getType() == V->getType()) {
07442         unsigned ExtractedIdx =
07443           cast<ConstantInt>(EI->getOperand(1))->getRawValue();
07444         unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getRawValue();
07445         
07446         // Either the extracted from or inserted into vector must be RHSVec,
07447         // otherwise we'd end up with a shuffle of three inputs.
07448         if (EI->getOperand(0) == RHS || RHS == 0) {
07449           RHS = EI->getOperand(0);
07450           Value *V = CollectShuffleElements(VecOp, Mask, RHS);
07451           Mask[InsertedIdx & (NumElts-1)] = 
07452             ConstantUInt::get(Type::UIntTy, NumElts+ExtractedIdx);
07453           return V;
07454         }
07455         
07456         if (VecOp == RHS) {
07457           Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
07458           // Everything but the extracted element is replaced with the RHS.
07459           for (unsigned i = 0; i != NumElts; ++i) {
07460             if (i != InsertedIdx)
07461               Mask[i] = ConstantUInt::get(Type::UIntTy, NumElts+i);
07462           }
07463           return V;
07464         }
07465         
07466         // If this insertelement is a chain that comes from exactly these two
07467         // vectors, return the vector and the effective shuffle.
07468         if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
07469           return EI->getOperand(0);
07470         
07471       }
07472     }
07473   }
07474   // TODO: Handle shufflevector here!
07475   
07476   // Otherwise, can't do anything fancy.  Return an identity vector.
07477   for (unsigned i = 0; i != NumElts; ++i)
07478     Mask.push_back(ConstantUInt::get(Type::UIntTy, i));
07479   return V;
07480 }
07481 
07482 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
07483   Value *VecOp    = IE.getOperand(0);
07484   Value *ScalarOp = IE.getOperand(1);
07485   Value *IdxOp    = IE.getOperand(2);
07486   
07487   // If the inserted element was extracted from some other vector, and if the 
07488   // indexes are constant, try to turn this into a shufflevector operation.
07489   if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
07490     if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
07491         EI->getOperand(0)->getType() == IE.getType()) {
07492       unsigned NumVectorElts = IE.getType()->getNumElements();
07493       unsigned ExtractedIdx=cast<ConstantInt>(EI->getOperand(1))->getRawValue();
07494       unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getRawValue();
07495       
07496       if (ExtractedIdx >= NumVectorElts) // Out of range extract.
07497         return ReplaceInstUsesWith(IE, VecOp);
07498       
07499       if (InsertedIdx >= NumVectorElts)  // Out of range insert.
07500         return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
07501       
07502       // If we are extracting a value from a vector, then inserting it right
07503       // back into the same place, just use the input vector.
07504       if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
07505         return ReplaceInstUsesWith(IE, VecOp);      
07506       
07507       // We could theoretically do this for ANY input.  However, doing so could
07508       // turn chains of insertelement instructions into a chain of shufflevector
07509       // instructions, and right now we do not merge shufflevectors.  As such,
07510       // only do this in a situation where it is clear that there is benefit.
07511       if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) {
07512         // Turn this into shuffle(EIOp0, VecOp, Mask).  The result has all of
07513         // the values of VecOp, except then one read from EIOp0.
07514         // Build a new shuffle mask.
07515         std::vector<Constant*> Mask;
07516         if (isa<UndefValue>(VecOp))
07517           Mask.assign(NumVectorElts, UndefValue::get(Type::UIntTy));
07518         else {
07519           assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
07520           Mask.assign(NumVectorElts, ConstantUInt::get(Type::UIntTy,
07521                                                        NumVectorElts));
07522         } 
07523         Mask[InsertedIdx] = ConstantUInt::get(Type::UIntTy, ExtractedIdx);
07524         return new ShuffleVectorInst(EI->getOperand(0), VecOp,
07525                                      ConstantPacked::get(Mask));
07526       }
07527       
07528       // If this insertelement isn't used by some other insertelement, turn it
07529       // (and any insertelements it points to), into one big shuffle.
07530       if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
07531         std::vector<Constant*> Mask;
07532         Value *RHS = 0;
07533         Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
07534         if (RHS == 0) RHS = UndefValue::get(LHS->getType());
07535         // We now have a shuffle of LHS, RHS, Mask.
07536         return new ShuffleVectorInst(LHS, RHS, ConstantPacked::get(Mask));
07537       }
07538     }
07539   }
07540 
07541   return 0;
07542 }
07543 
07544 
07545 Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
07546   Value *LHS = SVI.getOperand(0);
07547   Value *RHS = SVI.getOperand(1);
07548   std::vector<unsigned> Mask = getShuffleMask(&SVI);
07549 
07550   bool MadeChange = false;
07551   
07552   if (isa<UndefValue>(SVI.getOperand(2)))
07553     return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
07554   
07555   // TODO: If we have shuffle(x, undef, mask) and any elements of mask refer to
07556   // the undef, change them to undefs.
07557   
07558   // Canonicalize shuffle(x    ,x,mask) -> shuffle(x, undef,mask')
07559   // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
07560   if (LHS == RHS || isa<UndefValue>(LHS)) {
07561     if (isa<UndefValue>(LHS) && LHS == RHS) {
07562       // shuffle(undef,undef,mask) -> undef.
07563       return ReplaceInstUsesWith(SVI, LHS);
07564     }
07565     
07566     // Remap any references to RHS to use LHS.
07567     std::vector<Constant*> Elts;
07568     for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
07569       if (Mask[i] >= 2*e)
07570         Elts.push_back(UndefValue::get(Type::UIntTy));
07571       else {
07572         if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
07573             (Mask[i] <  e && isa<UndefValue>(LHS)))
07574           Mask[i] = 2*e;     // Turn into undef.
07575         else
07576           Mask[i] &= (e-1);  // Force to LHS.
07577         Elts.push_back(ConstantUInt::get(Type::UIntTy, Mask[i]));
07578       }
07579     }
07580     SVI.setOperand(0, SVI.getOperand(1));
07581     SVI.setOperand(1, UndefValue::get(RHS->getType()));
07582     SVI.setOperand(2, ConstantPacked::get(Elts));
07583     LHS = SVI.getOperand(0);
07584     RHS = SVI.getOperand(1);
07585     MadeChange = true;
07586   }
07587   
07588   // Analyze the shuffle, are the LHS or RHS and identity shuffles?
07589   bool isLHSID = true, isRHSID = true;
07590     
07591   for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
07592     if (Mask[i] >= e*2) continue;  // Ignore undef values.
07593     // Is this an identity shuffle of the LHS value?
07594     isLHSID &= (Mask[i] == i);
07595       
07596     // Is this an identity shuffle of the RHS value?
07597     isRHSID &= (Mask[i]-e == i);
07598   }
07599 
07600   // Eliminate identity shuffles.
07601   if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
07602   if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
07603   
07604   // If the LHS is a shufflevector itself, see if we can combine it with this
07605   // one without producing an unusual shuffle.  Here we are really conservative:
07606   // we are absolutely afraid of producing a shuffle mask not in the input
07607   // program, because the code gen may not be smart enough to turn a merged
07608   // shuffle into two specific shuffles: it may produce worse code.  As such,
07609   // we only merge two shuffles if the result is one of the two input shuffle
07610   // masks.  In this case, merging the shuffles just removes one instruction,
07611   // which we know is safe.  This is good for things like turning:
07612   // (splat(splat)) -> splat.
07613   if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
07614     if (isa<UndefValue>(RHS)) {
07615       std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI);
07616 
07617       std::vector<unsigned> NewMask;
07618       for (unsigned i = 0, e = Mask.size(); i != e; ++i)
07619         if (Mask[i] >= 2*e)
07620           NewMask.push_back(2*e);
07621         else
07622           NewMask.push_back(LHSMask[Mask[i]]);
07623       
07624       // If the result mask is equal to the src shuffle or this shuffle mask, do
07625       // the replacement.
07626       if (NewMask == LHSMask || NewMask == Mask) {
07627         std::vector<Constant*> Elts;
07628         for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
07629           if (NewMask[i] >= e*2) {
07630             Elts.push_back(UndefValue::get(Type::UIntTy));
07631           } else {
07632             Elts.push_back(ConstantUInt::get(Type::UIntTy, NewMask[i]));
07633           }
07634         }
07635         return new ShuffleVectorInst(LHSSVI->getOperand(0),
07636                                      LHSSVI->getOperand(1),
07637                                      ConstantPacked::get(Elts));
07638       }
07639     }
07640   }
07641   
07642   return MadeChange ? &SVI : 0;
07643 }
07644 
07645 
07646 
07647 void InstCombiner::removeFromWorkList(Instruction *I) {
07648   WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
07649                  WorkList.end());
07650 }
07651 
07652 
07653 /// TryToSinkInstruction - Try to move the specified instruction from its
07654 /// current block into the beginning of DestBlock, which can only happen if it's
07655 /// safe to move the instruction past all of the instructions between it and the
07656 /// end of its block.
07657 static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
07658   assert(I->hasOneUse() && "Invariants didn't hold!");
07659 
07660   // Cannot move control-flow-involving, volatile loads, vaarg, etc.
07661   if (isa<PHINode>(I) || I->mayWriteToMemory()) return false;
07662 
07663   // Do not sink alloca instructions out of the entry block.
07664   if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front())
07665     return false;
07666 
07667   // We can only sink load instructions if there is nothing between the load and
07668   // the end of block that could change the value.
07669   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
07670     for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end();
07671          Scan != E; ++Scan)
07672       if (Scan->mayWriteToMemory())
07673         return false;
07674   }
07675 
07676   BasicBlock::iterator InsertPos = DestBlock->begin();
07677   while (isa<PHINode>(InsertPos)) ++InsertPos;
07678 
07679   I->moveBefore(InsertPos);
07680   ++NumSunkInst;
07681   return true;
07682 }
07683 
07684 /// OptimizeConstantExpr - Given a constant expression and target data layout
07685 /// information, symbolically evaluation the constant expr to something simpler
07686 /// if possible.
07687 static Constant *OptimizeConstantExpr(ConstantExpr *CE, const TargetData *TD) {
07688   if (!TD) return CE;
07689   
07690   Constant *Ptr = CE->getOperand(0);
07691   if (CE->getOpcode() == Instruction::GetElementPtr && Ptr->isNullValue() &&
07692       cast<PointerType>(Ptr->getType())->getElementType()->isSized()) {
07693     // If this is a constant expr gep that is effectively computing an
07694     // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
07695     bool isFoldableGEP = true;
07696     for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
07697       if (!isa<ConstantInt>(CE->getOperand(i)))
07698         isFoldableGEP = false;
07699     if (isFoldableGEP) {
07700       std::vector<Value*> Ops(CE->op_begin()+1, CE->op_end());
07701       uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), Ops);
07702       Constant *C = ConstantUInt::get(Type::ULongTy, Offset);
07703       C = ConstantExpr::getCast(C, TD->getIntPtrType());
07704       return ConstantExpr::getCast(C, CE->getType());
07705     }
07706   }
07707   
07708   return CE;
07709 }
07710 
07711 
07712 /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
07713 /// all reachable code to the worklist.
07714 ///
07715 /// This has a couple of tricks to make the code faster and more powerful.  In
07716 /// particular, we constant fold and DCE instructions as we go, to avoid adding
07717 /// them to the worklist (this significantly speeds up instcombine on code where
07718 /// many instructions are dead or constant).  Additionally, if we find a branch
07719 /// whose condition is a known constant, we only visit the reachable successors.
07720 ///
07721 static void AddReachableCodeToWorklist(BasicBlock *BB, 
07722                                        std::set<BasicBlock*> &Visited,
07723                                        std::vector<Instruction*> &WorkList,
07724                                        const TargetData *TD) {
07725   // We have now visited this block!  If we've already been here, bail out.
07726   if (!Visited.insert(BB).second) return;
07727     
07728   for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
07729     Instruction *Inst = BBI++;
07730     
07731     // DCE instruction if trivially dead.
07732     if (isInstructionTriviallyDead(Inst)) {
07733       ++NumDeadInst;
07734       DEBUG(std::cerr << "IC: DCE: " << *Inst);
07735       Inst->eraseFromParent();
07736       continue;
07737     }
07738     
07739     // ConstantProp instruction if trivially constant.
07740     if (Constant *C = ConstantFoldInstruction(Inst)) {
07741       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
07742         C = OptimizeConstantExpr(CE, TD);
07743       DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *Inst);
07744       Inst->replaceAllUsesWith(C);
07745       ++NumConstProp;
07746       Inst->eraseFromParent();
07747       continue;
07748     }
07749     
07750     WorkList.push_back(Inst);
07751   }
07752 
07753   // Recursively visit successors.  If this is a branch or switch on a constant,
07754   // only visit the reachable successor.
07755   TerminatorInst *TI = BB->getTerminator();
07756   if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
07757     if (BI->isConditional() && isa<ConstantBool>(BI->getCondition())) {
07758       bool CondVal = cast<ConstantBool>(BI->getCondition())->getValue();
07759       AddReachableCodeToWorklist(BI->getSuccessor(!CondVal), Visited, WorkList,
07760                                  TD);
07761       return;
07762     }
07763   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
07764     if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
07765       // See if this is an explicit destination.
07766       for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
07767         if (SI->getCaseValue(i) == Cond) {
07768           AddReachableCodeToWorklist(SI->getSuccessor(i), Visited, WorkList,TD);
07769           return;
07770         }
07771       
07772       // Otherwise it is the default destination.
07773       AddReachableCodeToWorklist(SI->getSuccessor(0), Visited, WorkList, TD);
07774       return;
07775     }
07776   }
07777   
07778   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
07779     AddReachableCodeToWorklist(TI->getSuccessor(i), Visited, WorkList, TD);
07780 }
07781 
07782 bool InstCombiner::runOnFunction(Function &F) {
07783   bool Changed = false;
07784   TD = &getAnalysis<TargetData>();
07785 
07786   {
07787     // Do a depth-first traversal of the function, populate the worklist with
07788     // the reachable instructions.  Ignore blocks that are not reachable.  Keep
07789     // track of which blocks we visit.
07790     std::set<BasicBlock*> Visited;
07791     AddReachableCodeToWorklist(F.begin(), Visited, WorkList, TD);
07792 
07793     // Do a quick scan over the function.  If we find any blocks that are
07794     // unreachable, remove any instructions inside of them.  This prevents
07795     // the instcombine code from having to deal with some bad special cases.
07796     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
07797       if (!Visited.count(BB)) {
07798         Instruction *Term = BB->getTerminator();
07799         while (Term != BB->begin()) {   // Remove instrs bottom-up
07800           BasicBlock::iterator I = Term; --I;
07801 
07802           DEBUG(std::cerr << "IC: DCE: " << *I);
07803           ++NumDeadInst;
07804 
07805           if (!I->use_empty())
07806             I->replaceAllUsesWith(UndefValue::get(I->getType()));
07807           I->eraseFromParent();
07808         }
07809       }
07810   }
07811 
07812   while (!WorkList.empty()) {
07813     Instruction *I = WorkList.back();  // Get an instruction from the worklist
07814     WorkList.pop_back();
07815 
07816     // Check to see if we can DCE the instruction.
07817     if (isInstructionTriviallyDead(I)) {
07818       // Add operands to the worklist.
07819       if (I->getNumOperands() < 4)
07820         AddUsesToWorkList(*I);
07821       ++NumDeadInst;
07822 
07823       DEBUG(std::cerr << "IC: DCE: " << *I);
07824 
07825       I->eraseFromParent();
07826       removeFromWorkList(I);
07827       continue;
07828     }
07829 
07830     // Instruction isn't dead, see if we can constant propagate it.
07831     if (Constant *C = ConstantFoldInstruction(I)) {
07832       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
07833         C = OptimizeConstantExpr(CE, TD);
07834       DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *I);
07835 
07836       // Add operands to the worklist.
07837       AddUsesToWorkList(*I);
07838       ReplaceInstUsesWith(*I, C);
07839 
07840       ++NumConstProp;
07841       I->eraseFromParent();
07842       removeFromWorkList(I);
07843       continue;
07844     }
07845 
07846     // See if we can trivially sink this instruction to a successor basic block.
07847     if (I->hasOneUse()) {
07848       BasicBlock *BB = I->getParent();
07849       BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent();
07850       if (UserParent != BB) {
07851         bool UserIsSuccessor = false;
07852         // See if the user is one of our successors.
07853         for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
07854           if (*SI == UserParent) {
07855             UserIsSuccessor = true;
07856             break;
07857           }
07858 
07859         // If the user is one of our immediate successors, and if that successor
07860         // only has us as a predecessors (we'd have to split the critical edge
07861         // otherwise), we can keep going.
07862         if (UserIsSuccessor && !isa<PHINode>(I->use_back()) &&
07863             next(pred_begin(UserParent)) == pred_end(UserParent))
07864           // Okay, the CFG is simple enough, try to sink this instruction.
07865           Changed |= TryToSinkInstruction(I, UserParent);
07866       }
07867     }
07868 
07869     // Now that we have an instruction, try combining it to simplify it...
07870     if (Instruction *Result = visit(*I)) {
07871       ++NumCombined;
07872       // Should we replace the old instruction with a new one?
07873       if (Result != I) {
07874         DEBUG(std::cerr << "IC: Old = " << *I
07875                         << "    New = " << *Result);
07876 
07877         // Everything uses the new instruction now.
07878         I->replaceAllUsesWith(Result);
07879 
07880         // Push the new instruction and any users onto the worklist.
07881         WorkList.push_back(Result);
07882         AddUsersToWorkList(*Result);
07883 
07884         // Move the name to the new instruction first...
07885         std::string OldName = I->getName(); I->setName("");
07886         Result->setName(OldName);
07887 
07888         // Insert the new instruction into the basic block...
07889         BasicBlock *InstParent = I->getParent();
07890         BasicBlock::iterator InsertPos = I;
07891 
07892         if (!isa<PHINode>(Result))        // If combining a PHI, don't insert
07893           while (isa<PHINode>(InsertPos)) // middle of a block of PHIs.
07894             ++InsertPos;
07895 
07896         InstParent->getInstList().insert(InsertPos, Result);
07897 
07898         // Make sure that we reprocess all operands now that we reduced their
07899         // use counts.
07900         for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
07901           if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i)))
07902             WorkList.push_back(OpI);
07903 
07904         // Instructions can end up on the worklist more than once.  Make sure
07905         // we do not process an instruction that has been deleted.
07906         removeFromWorkList(I);
07907 
07908         // Erase the old instruction.
07909         InstParent->getInstList().erase(I);
07910       } else {
07911         DEBUG(std::cerr << "IC: MOD = " << *I);
07912 
07913         // If the instruction was modified, it's possible that it is now dead.
07914         // if so, remove it.
07915         if (isInstructionTriviallyDead(I)) {
07916           // Make sure we process all operands now that we are reducing their
07917           // use counts.
07918           for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
07919             if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i)))
07920               WorkList.push_back(OpI);
07921 
07922           // Instructions may end up in the worklist more than once.  Erase all
07923           // occurrences of this instruction.
07924           removeFromWorkList(I);
07925           I->eraseFromParent();
07926         } else {
07927           WorkList.push_back(Result);
07928           AddUsersToWorkList(*Result);
07929         }
07930       }
07931       Changed = true;
07932     }
07933   }
07934 
07935   return Changed;
07936 }
07937 
07938 FunctionPass *llvm::createInstructionCombiningPass() {
07939   return new InstCombiner();
07940 }
07941