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