LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

InstructionCombining.cpp

Go to the documentation of this file.
00001 //===- InstructionCombining.cpp - Combine multiple instructions -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // InstructionCombining - Combine instructions to form fewer, simple
00011 // instructions.  This pass does not modify the CFG.  This pass is where
00012 // algebraic simplification happens.
00013 //
00014 // This pass combines things like:
00015 //    %Y = add i32 %X, 1
00016 //    %Z = add i32 %Y, 1
00017 // into:
00018 //    %Z = add i32 %X, 2
00019 //
00020 // This is a simple worklist driven algorithm.
00021 //
00022 // This pass guarantees that the following canonicalizations are performed on
00023 // the program:
00024 //    1. If a binary operator has a constant operand, it is moved to the RHS
00025 //    2. Bitwise operators with constant operands are always grouped so that
00026 //       shifts are performed first, then or's, then and's, then xor's.
00027 //    3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
00028 //    4. All cmp instructions on boolean values are replaced with logical ops
00029 //    5. add X, X is represented as (X*2) => (X << 1)
00030 //    6. Multiplies with a power-of-two constant argument are transformed into
00031 //       shifts.
00032 //   ... etc.
00033 //
00034 //===----------------------------------------------------------------------===//
00035 
00036 #define DEBUG_TYPE "instcombine"
00037 #include "llvm/Transforms/Scalar.h"
00038 #include "llvm/IntrinsicInst.h"
00039 #include "llvm/Pass.h"
00040 #include "llvm/DerivedTypes.h"
00041 #include "llvm/GlobalVariable.h"
00042 #include "llvm/Analysis/ConstantFolding.h"
00043 #include "llvm/Analysis/ValueTracking.h"
00044 #include "llvm/Target/TargetData.h"
00045 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00046 #include "llvm/Transforms/Utils/Local.h"
00047 #include "llvm/Support/CallSite.h"
00048 #include "llvm/Support/ConstantRange.h"
00049 #include "llvm/Support/Debug.h"
00050 #include "llvm/Support/GetElementPtrTypeIterator.h"
00051 #include "llvm/Support/InstVisitor.h"
00052 #include "llvm/Support/MathExtras.h"
00053 #include "llvm/Support/PatternMatch.h"
00054 #include "llvm/Support/Compiler.h"
00055 #include "llvm/ADT/DenseMap.h"
00056 #include "llvm/ADT/SmallVector.h"
00057 #include "llvm/ADT/SmallPtrSet.h"
00058 #include "llvm/ADT/Statistic.h"
00059 #include "llvm/ADT/STLExtras.h"
00060 #include <algorithm>
00061 #include <climits>
00062 #include <sstream>
00063 using namespace llvm;
00064 using namespace llvm::PatternMatch;
00065 
00066 STATISTIC(NumCombined , "Number of insts combined");
00067 STATISTIC(NumConstProp, "Number of constant folds");
00068 STATISTIC(NumDeadInst , "Number of dead inst eliminated");
00069 STATISTIC(NumDeadStore, "Number of dead stores eliminated");
00070 STATISTIC(NumSunkInst , "Number of instructions sunk");
00071 
00072 namespace {
00073   class VISIBILITY_HIDDEN InstCombiner
00074     : public FunctionPass,
00075       public InstVisitor<InstCombiner, Instruction*> {
00076     // Worklist of all of the instructions that need to be simplified.
00077     SmallVector<Instruction*, 256> Worklist;
00078     DenseMap<Instruction*, unsigned> WorklistMap;
00079     TargetData *TD;
00080     bool MustPreserveLCSSA;
00081   public:
00082     static char ID; // Pass identification, replacement for typeid
00083     InstCombiner() : FunctionPass(&ID) {}
00084 
00085     /// AddToWorkList - Add the specified instruction to the worklist if it
00086     /// isn't already in it.
00087     void AddToWorkList(Instruction *I) {
00088       if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second)
00089         Worklist.push_back(I);
00090     }
00091     
00092     // RemoveFromWorkList - remove I from the worklist if it exists.
00093     void RemoveFromWorkList(Instruction *I) {
00094       DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
00095       if (It == WorklistMap.end()) return; // Not in worklist.
00096       
00097       // Don't bother moving everything down, just null out the slot.
00098       Worklist[It->second] = 0;
00099       
00100       WorklistMap.erase(It);
00101     }
00102     
00103     Instruction *RemoveOneFromWorkList() {
00104       Instruction *I = Worklist.back();
00105       Worklist.pop_back();
00106       WorklistMap.erase(I);
00107       return I;
00108     }
00109 
00110     
00111     /// AddUsersToWorkList - When an instruction is simplified, add all users of
00112     /// the instruction to the work lists because they might get more simplified
00113     /// now.
00114     ///
00115     void AddUsersToWorkList(Value &I) {
00116       for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
00117            UI != UE; ++UI)
00118         AddToWorkList(cast<Instruction>(*UI));
00119     }
00120 
00121     /// AddUsesToWorkList - When an instruction is simplified, add operands to
00122     /// the work lists because they might get more simplified now.
00123     ///
00124     void AddUsesToWorkList(Instruction &I) {
00125       for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
00126         if (Instruction *Op = dyn_cast<Instruction>(*i))
00127           AddToWorkList(Op);
00128     }
00129     
00130     /// AddSoonDeadInstToWorklist - The specified instruction is about to become
00131     /// dead.  Add all of its operands to the worklist, turning them into
00132     /// undef's to reduce the number of uses of those instructions.
00133     ///
00134     /// Return the specified operand before it is turned into an undef.
00135     ///
00136     Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) {
00137       Value *R = I.getOperand(op);
00138       
00139       for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
00140         if (Instruction *Op = dyn_cast<Instruction>(*i)) {
00141           AddToWorkList(Op);
00142           // Set the operand to undef to drop the use.
00143           *i = UndefValue::get(Op->getType());
00144         }
00145       
00146       return R;
00147     }
00148 
00149   public:
00150     virtual bool runOnFunction(Function &F);
00151     
00152     bool DoOneIteration(Function &F, unsigned ItNum);
00153 
00154     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00155       AU.addRequired<TargetData>();
00156       AU.addPreservedID(LCSSAID);
00157       AU.setPreservesCFG();
00158     }
00159 
00160     TargetData &getTargetData() const { return *TD; }
00161 
00162     // Visitation implementation - Implement instruction combining for different
00163     // instruction types.  The semantics are as follows:
00164     // Return Value:
00165     //    null        - No change was made
00166     //     I          - Change was made, I is still valid, I may be dead though
00167     //   otherwise    - Change was made, replace I with returned instruction
00168     //
00169     Instruction *visitAdd(BinaryOperator &I);
00170     Instruction *visitSub(BinaryOperator &I);
00171     Instruction *visitMul(BinaryOperator &I);
00172     Instruction *visitURem(BinaryOperator &I);
00173     Instruction *visitSRem(BinaryOperator &I);
00174     Instruction *visitFRem(BinaryOperator &I);
00175     bool SimplifyDivRemOfSelect(BinaryOperator &I);
00176     Instruction *commonRemTransforms(BinaryOperator &I);
00177     Instruction *commonIRemTransforms(BinaryOperator &I);
00178     Instruction *commonDivTransforms(BinaryOperator &I);
00179     Instruction *commonIDivTransforms(BinaryOperator &I);
00180     Instruction *visitUDiv(BinaryOperator &I);
00181     Instruction *visitSDiv(BinaryOperator &I);
00182     Instruction *visitFDiv(BinaryOperator &I);
00183     Instruction *visitAnd(BinaryOperator &I);
00184     Instruction *visitOr (BinaryOperator &I);
00185     Instruction *visitXor(BinaryOperator &I);
00186     Instruction *visitShl(BinaryOperator &I);
00187     Instruction *visitAShr(BinaryOperator &I);
00188     Instruction *visitLShr(BinaryOperator &I);
00189     Instruction *commonShiftTransforms(BinaryOperator &I);
00190     Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
00191                                       Constant *RHSC);
00192     Instruction *visitFCmpInst(FCmpInst &I);
00193     Instruction *visitICmpInst(ICmpInst &I);
00194     Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
00195     Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
00196                                                 Instruction *LHS,
00197                                                 ConstantInt *RHS);
00198     Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
00199                                 ConstantInt *DivRHS);
00200 
00201     Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS,
00202                              ICmpInst::Predicate Cond, Instruction &I);
00203     Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
00204                                      BinaryOperator &I);
00205     Instruction *commonCastTransforms(CastInst &CI);
00206     Instruction *commonIntCastTransforms(CastInst &CI);
00207     Instruction *commonPointerCastTransforms(CastInst &CI);
00208     Instruction *visitTrunc(TruncInst &CI);
00209     Instruction *visitZExt(ZExtInst &CI);
00210     Instruction *visitSExt(SExtInst &CI);
00211     Instruction *visitFPTrunc(FPTruncInst &CI);
00212     Instruction *visitFPExt(CastInst &CI);
00213     Instruction *visitFPToUI(FPToUIInst &FI);
00214     Instruction *visitFPToSI(FPToSIInst &FI);
00215     Instruction *visitUIToFP(CastInst &CI);
00216     Instruction *visitSIToFP(CastInst &CI);
00217     Instruction *visitPtrToInt(CastInst &CI);
00218     Instruction *visitIntToPtr(IntToPtrInst &CI);
00219     Instruction *visitBitCast(BitCastInst &CI);
00220     Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
00221                                 Instruction *FI);
00222     Instruction *visitSelectInst(SelectInst &CI);
00223     Instruction *visitCallInst(CallInst &CI);
00224     Instruction *visitInvokeInst(InvokeInst &II);
00225     Instruction *visitPHINode(PHINode &PN);
00226     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
00227     Instruction *visitAllocationInst(AllocationInst &AI);
00228     Instruction *visitFreeInst(FreeInst &FI);
00229     Instruction *visitLoadInst(LoadInst &LI);
00230     Instruction *visitStoreInst(StoreInst &SI);
00231     Instruction *visitBranchInst(BranchInst &BI);
00232     Instruction *visitSwitchInst(SwitchInst &SI);
00233     Instruction *visitInsertElementInst(InsertElementInst &IE);
00234     Instruction *visitExtractElementInst(ExtractElementInst &EI);
00235     Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
00236     Instruction *visitExtractValueInst(ExtractValueInst &EV);
00237 
00238     // visitInstruction - Specify what to return for unhandled instructions...
00239     Instruction *visitInstruction(Instruction &I) { return 0; }
00240 
00241   private:
00242     Instruction *visitCallSite(CallSite CS);
00243     bool transformConstExprCastCall(CallSite CS);
00244     Instruction *transformCallThroughTrampoline(CallSite CS);
00245     Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
00246                                    bool DoXform = true);
00247     bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
00248 
00249   public:
00250     // InsertNewInstBefore - insert an instruction New before instruction Old
00251     // in the program.  Add the new instruction to the worklist.
00252     //
00253     Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
00254       assert(New && New->getParent() == 0 &&
00255              "New instruction already inserted into a basic block!");
00256       BasicBlock *BB = Old.getParent();
00257       BB->getInstList().insert(&Old, New);  // Insert inst
00258       AddToWorkList(New);
00259       return New;
00260     }
00261 
00262     /// InsertCastBefore - Insert a cast of V to TY before the instruction POS.
00263     /// This also adds the cast to the worklist.  Finally, this returns the
00264     /// cast.
00265     Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty,
00266                             Instruction &Pos) {
00267       if (V->getType() == Ty) return V;
00268 
00269       if (Constant *CV = dyn_cast<Constant>(V))
00270         return ConstantExpr::getCast(opc, CV, Ty);
00271       
00272       Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos);
00273       AddToWorkList(C);
00274       return C;
00275     }
00276         
00277     Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) {
00278       return InsertCastBefore(Instruction::BitCast, V, Ty, Pos);
00279     }
00280 
00281 
00282     // ReplaceInstUsesWith - This method is to be used when an instruction is
00283     // found to be dead, replacable with another preexisting expression.  Here
00284     // we add all uses of I to the worklist, replace all uses of I with the new
00285     // value, then return I, so that the inst combiner will know that I was
00286     // modified.
00287     //
00288     Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
00289       AddUsersToWorkList(I);         // Add all modified instrs to worklist
00290       if (&I != V) {
00291         I.replaceAllUsesWith(V);
00292         return &I;
00293       } else {
00294         // If we are replacing the instruction with itself, this must be in a
00295         // segment of unreachable code, so just clobber the instruction.
00296         I.replaceAllUsesWith(UndefValue::get(I.getType()));
00297         return &I;
00298       }
00299     }
00300 
00301     // UpdateValueUsesWith - This method is to be used when an value is
00302     // found to be replacable with another preexisting expression or was
00303     // updated.  Here we add all uses of I to the worklist, replace all uses of
00304     // I with the new value (unless the instruction was just updated), then
00305     // return true, so that the inst combiner will know that I was modified.
00306     //
00307     bool UpdateValueUsesWith(Value *Old, Value *New) {
00308       AddUsersToWorkList(*Old);         // Add all modified instrs to worklist
00309       if (Old != New)
00310         Old->replaceAllUsesWith(New);
00311       if (Instruction *I = dyn_cast<Instruction>(Old))
00312         AddToWorkList(I);
00313       if (Instruction *I = dyn_cast<Instruction>(New))
00314         AddToWorkList(I);
00315       return true;
00316     }
00317     
00318     // EraseInstFromFunction - When dealing with an instruction that has side
00319     // effects or produces a void value, we can't rely on DCE to delete the
00320     // instruction.  Instead, visit methods should return the value returned by
00321     // this function.
00322     Instruction *EraseInstFromFunction(Instruction &I) {
00323       assert(I.use_empty() && "Cannot erase instruction that is used!");
00324       AddUsesToWorkList(I);
00325       RemoveFromWorkList(&I);
00326       I.eraseFromParent();
00327       return 0;  // Don't do anything with FI
00328     }
00329         
00330     void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero,
00331                            APInt &KnownOne, unsigned Depth = 0) const {
00332       return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
00333     }
00334     
00335     bool MaskedValueIsZero(Value *V, const APInt &Mask, 
00336                            unsigned Depth = 0) const {
00337       return llvm::MaskedValueIsZero(V, Mask, TD, Depth);
00338     }
00339     unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const {
00340       return llvm::ComputeNumSignBits(Op, TD, Depth);
00341     }
00342 
00343   private:
00344     /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
00345     /// InsertBefore instruction.  This is specialized a bit to avoid inserting
00346     /// casts that are known to not do anything...
00347     ///
00348     Value *InsertOperandCastBefore(Instruction::CastOps opcode,
00349                                    Value *V, const Type *DestTy,
00350                                    Instruction *InsertBefore);
00351 
00352     /// SimplifyCommutative - This performs a few simplifications for 
00353     /// commutative operators.
00354     bool SimplifyCommutative(BinaryOperator &I);
00355 
00356     /// SimplifyCompare - This reorders the operands of a CmpInst to get them in
00357     /// most-complex to least-complex order.
00358     bool SimplifyCompare(CmpInst &I);
00359 
00360     /// SimplifyDemandedBits - Attempts to replace V with a simpler value based
00361     /// on the demanded bits.
00362     bool SimplifyDemandedBits(Value *V, APInt DemandedMask, 
00363                               APInt& KnownZero, APInt& KnownOne,
00364                               unsigned Depth = 0);
00365 
00366     Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts,
00367                                       uint64_t &UndefElts, unsigned Depth = 0);
00368       
00369     // FoldOpIntoPhi - Given a binary operator or cast instruction which has a
00370     // PHI node as operand #0, see if we can fold the instruction into the PHI
00371     // (which is only possible if all operands to the PHI are constants).
00372     Instruction *FoldOpIntoPhi(Instruction &I);
00373 
00374     // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
00375     // operator and they all are only used by the PHI, PHI together their
00376     // inputs, and do the operation once, to the result of the PHI.
00377     Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
00378     Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
00379     
00380     
00381     Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
00382                           ConstantInt *AndRHS, BinaryOperator &TheAnd);
00383     
00384     Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
00385                               bool isSub, Instruction &I);
00386     Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
00387                                  bool isSigned, bool Inside, Instruction &IB);
00388     Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI);
00389     Instruction *MatchBSwap(BinaryOperator &I);
00390     bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
00391     Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
00392     Instruction *SimplifyMemSet(MemSetInst *MI);
00393 
00394 
00395     Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned);
00396 
00397     bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty,
00398                                     unsigned CastOpc,
00399                                     int &NumCastsRemoved);
00400     unsigned GetOrEnforceKnownAlignment(Value *V,
00401                                         unsigned PrefAlign = 0);
00402 
00403   };
00404 }
00405 
00406 char InstCombiner::ID = 0;
00407 static RegisterPass<InstCombiner>
00408 X("instcombine", "Combine redundant instructions");
00409 
00410 // getComplexity:  Assign a complexity or rank value to LLVM Values...
00411 //   0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
00412 static unsigned getComplexity(Value *V) {
00413   if (isa<Instruction>(V)) {
00414     if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
00415       return 3;
00416     return 4;
00417   }
00418   if (isa<Argument>(V)) return 3;
00419   return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
00420 }
00421 
00422 // isOnlyUse - Return true if this instruction will be deleted if we stop using
00423 // it.
00424 static bool isOnlyUse(Value *V) {
00425   return V->hasOneUse() || isa<Constant>(V);
00426 }
00427 
00428 // getPromotedType - Return the specified type promoted as it would be to pass
00429 // though a va_arg area...
00430 static const Type *getPromotedType(const Type *Ty) {
00431   if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
00432     if (ITy->getBitWidth() < 32)
00433       return Type::Int32Ty;
00434   }
00435   return Ty;
00436 }
00437 
00438 /// getBitCastOperand - If the specified operand is a CastInst or a constant 
00439 /// expression bitcast,  return the operand value, otherwise return null.
00440 static Value *getBitCastOperand(Value *V) {
00441   if (BitCastInst *I = dyn_cast<BitCastInst>(V))
00442     return I->getOperand(0);
00443   else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00444     if (CE->getOpcode() == Instruction::BitCast)
00445       return CE->getOperand(0);
00446   return 0;
00447 }
00448 
00449 /// This function is a wrapper around CastInst::isEliminableCastPair. It
00450 /// simply extracts arguments and returns what that function returns.
00451 static Instruction::CastOps 
00452 isEliminableCastPair(
00453   const CastInst *CI, ///< The first cast instruction
00454   unsigned opcode,       ///< The opcode of the second cast instruction
00455   const Type *DstTy,     ///< The target type for the second cast instruction
00456   TargetData *TD         ///< The target data for pointer size
00457 ) {
00458   
00459   const Type *SrcTy = CI->getOperand(0)->getType();   // A from above
00460   const Type *MidTy = CI->getType();                  // B from above
00461 
00462   // Get the opcodes of the two Cast instructions
00463   Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
00464   Instruction::CastOps secondOp = Instruction::CastOps(opcode);
00465 
00466   return Instruction::CastOps(
00467       CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
00468                                      DstTy, TD->getIntPtrType()));
00469 }
00470 
00471 /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
00472 /// in any code being generated.  It does not require codegen if V is simple
00473 /// enough or if the cast can be folded into other casts.
00474 static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, 
00475                               const Type *Ty, TargetData *TD) {
00476   if (V->getType() == Ty || isa<Constant>(V)) return false;
00477   
00478   // If this is another cast that can be eliminated, it isn't codegen either.
00479   if (const CastInst *CI = dyn_cast<CastInst>(V))
00480     if (isEliminableCastPair(CI, opcode, Ty, TD)) 
00481       return false;
00482   return true;
00483 }
00484 
00485 /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
00486 /// InsertBefore instruction.  This is specialized a bit to avoid inserting
00487 /// casts that are known to not do anything...
00488 ///
00489 Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode,
00490                                              Value *V, const Type *DestTy,
00491                                              Instruction *InsertBefore) {
00492   if (V->getType() == DestTy) return V;
00493   if (Constant *C = dyn_cast<Constant>(V))
00494     return ConstantExpr::getCast(opcode, C, DestTy);
00495   
00496   return InsertCastBefore(opcode, V, DestTy, *InsertBefore);
00497 }
00498 
00499 // SimplifyCommutative - This performs a few simplifications for commutative
00500 // operators:
00501 //
00502 //  1. Order operands such that they are listed from right (least complex) to
00503 //     left (most complex).  This puts constants before unary operators before
00504 //     binary operators.
00505 //
00506 //  2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
00507 //  3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
00508 //
00509 bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
00510   bool Changed = false;
00511   if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
00512     Changed = !I.swapOperands();
00513 
00514   if (!I.isAssociative()) return Changed;
00515   Instruction::BinaryOps Opcode = I.getOpcode();
00516   if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
00517     if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
00518       if (isa<Constant>(I.getOperand(1))) {
00519         Constant *Folded = ConstantExpr::get(I.getOpcode(),
00520                                              cast<Constant>(I.getOperand(1)),
00521                                              cast<Constant>(Op->getOperand(1)));
00522         I.setOperand(0, Op->getOperand(0));
00523         I.setOperand(1, Folded);
00524         return true;
00525       } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
00526         if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
00527             isOnlyUse(Op) && isOnlyUse(Op1)) {
00528           Constant *C1 = cast<Constant>(Op->getOperand(1));
00529           Constant *C2 = cast<Constant>(Op1->getOperand(1));
00530 
00531           // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
00532           Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
00533           Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0),
00534                                                     Op1->getOperand(0),
00535                                                     Op1->getName(), &I);
00536           AddToWorkList(New);
00537           I.setOperand(0, New);
00538           I.setOperand(1, Folded);
00539           return true;
00540         }
00541     }
00542   return Changed;
00543 }
00544 
00545 /// SimplifyCompare - For a CmpInst this function just orders the operands
00546 /// so that theyare listed from right (least complex) to left (most complex).
00547 /// This puts constants before unary operators before binary operators.
00548 bool InstCombiner::SimplifyCompare(CmpInst &I) {
00549   if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1)))
00550     return false;
00551   I.swapOperands();
00552   // Compare instructions are not associative so there's nothing else we can do.
00553   return true;
00554 }
00555 
00556 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
00557 // if the LHS is a constant zero (which is the 'negate' form).
00558 //
00559 static inline Value *dyn_castNegVal(Value *V) {
00560   if (BinaryOperator::isNeg(V))
00561     return BinaryOperator::getNegArgument(V);
00562 
00563   // Constants can be considered to be negated values if they can be folded.
00564   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
00565     return ConstantExpr::getNeg(C);
00566 
00567   if (ConstantVector *C = dyn_cast<ConstantVector>(V))
00568     if (C->getType()->getElementType()->isInteger())
00569       return ConstantExpr::getNeg(C);
00570 
00571   return 0;
00572 }
00573 
00574 static inline Value *dyn_castNotVal(Value *V) {
00575   if (BinaryOperator::isNot(V))
00576     return BinaryOperator::getNotArgument(V);
00577 
00578   // Constants can be considered to be not'ed values...
00579   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
00580     return ConstantInt::get(~C->getValue());
00581   return 0;
00582 }
00583 
00584 // dyn_castFoldableMul - If this value is a multiply that can be folded into
00585 // other computations (because it has a constant operand), return the
00586 // non-constant operand of the multiply, and set CST to point to the multiplier.
00587 // Otherwise, return null.
00588 //
00589 static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
00590   if (V->hasOneUse() && V->getType()->isInteger())
00591     if (Instruction *I = dyn_cast<Instruction>(V)) {
00592       if (I->getOpcode() == Instruction::Mul)
00593         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
00594           return I->getOperand(0);
00595       if (I->getOpcode() == Instruction::Shl)
00596         if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
00597           // The multiplier is really 1 << CST.
00598           uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
00599           uint32_t CSTVal = CST->getLimitedValue(BitWidth);
00600           CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal));
00601           return I->getOperand(0);
00602         }
00603     }
00604   return 0;
00605 }
00606 
00607 /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant
00608 /// expression, return it.
00609 static User *dyn_castGetElementPtr(Value *V) {
00610   if (isa<GetElementPtrInst>(V)) return cast<User>(V);
00611   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00612     if (CE->getOpcode() == Instruction::GetElementPtr)
00613       return cast<User>(V);
00614   return false;
00615 }
00616 
00617 /// getOpcode - If this is an Instruction or a ConstantExpr, return the
00618 /// opcode value. Otherwise return UserOp1.
00619 static unsigned getOpcode(const Value *V) {
00620   if (const Instruction *I = dyn_cast<Instruction>(V))
00621     return I->getOpcode();
00622   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00623     return CE->getOpcode();
00624   // Use UserOp1 to mean there's no opcode.
00625   return Instruction::UserOp1;
00626 }
00627 
00628 /// AddOne - Add one to a ConstantInt
00629 static ConstantInt *AddOne(ConstantInt *C) {
00630   APInt Val(C->getValue());
00631   return ConstantInt::get(++Val);
00632 }
00633 /// SubOne - Subtract one from a ConstantInt
00634 static ConstantInt *SubOne(ConstantInt *C) {
00635   APInt Val(C->getValue());
00636   return ConstantInt::get(--Val);
00637 }
00638 /// Add - Add two ConstantInts together
00639 static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) {
00640   return ConstantInt::get(C1->getValue() + C2->getValue());
00641 }
00642 /// And - Bitwise AND two ConstantInts together
00643 static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) {
00644   return ConstantInt::get(C1->getValue() & C2->getValue());
00645 }
00646 /// Subtract - Subtract one ConstantInt from another
00647 static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) {
00648   return ConstantInt::get(C1->getValue() - C2->getValue());
00649 }
00650 /// Multiply - Multiply two ConstantInts together
00651 static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) {
00652   return ConstantInt::get(C1->getValue() * C2->getValue());
00653 }
00654 /// MultiplyOverflows - True if the multiply can not be expressed in an int
00655 /// this size.
00656 static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
00657   uint32_t W = C1->getBitWidth();
00658   APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
00659   if (sign) {
00660     LHSExt.sext(W * 2);
00661     RHSExt.sext(W * 2);
00662   } else {
00663     LHSExt.zext(W * 2);
00664     RHSExt.zext(W * 2);
00665   }
00666 
00667   APInt MulExt = LHSExt * RHSExt;
00668 
00669   if (sign) {
00670     APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
00671     APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
00672     return MulExt.slt(Min) || MulExt.sgt(Max);
00673   } else 
00674     return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
00675 }
00676 
00677 
00678 /// ShrinkDemandedConstant - Check to see if the specified operand of the 
00679 /// specified instruction is a constant integer.  If so, check to see if there
00680 /// are any bits set in the constant that are not demanded.  If so, shrink the
00681 /// constant and return true.
00682 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, 
00683                                    APInt Demanded) {
00684   assert(I && "No instruction?");
00685   assert(OpNo < I->getNumOperands() && "Operand index too large");
00686 
00687   // If the operand is not a constant integer, nothing to do.
00688   ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
00689   if (!OpC) return false;
00690 
00691   // If there are no bits set that aren't demanded, nothing to do.
00692   Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
00693   if ((~Demanded & OpC->getValue()) == 0)
00694     return false;
00695 
00696   // This instruction is producing bits that are not demanded. Shrink the RHS.
00697   Demanded &= OpC->getValue();
00698   I->setOperand(OpNo, ConstantInt::get(Demanded));
00699   return true;
00700 }
00701 
00702 // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a 
00703 // set of known zero and one bits, compute the maximum and minimum values that
00704 // could have the specified known zero and known one bits, returning them in
00705 // min/max.
00706 static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty,
00707                                                    const APInt& KnownZero,
00708                                                    const APInt& KnownOne,
00709                                                    APInt& Min, APInt& Max) {
00710   uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
00711   assert(KnownZero.getBitWidth() == BitWidth && 
00712          KnownOne.getBitWidth() == BitWidth &&
00713          Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth &&
00714          "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
00715   APInt UnknownBits = ~(KnownZero|KnownOne);
00716 
00717   // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
00718   // bit if it is unknown.
00719   Min = KnownOne;
00720   Max = KnownOne|UnknownBits;
00721   
00722   if (UnknownBits[BitWidth-1]) { // Sign bit is unknown
00723     Min.set(BitWidth-1);
00724     Max.clear(BitWidth-1);
00725   }
00726 }
00727 
00728 // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
00729 // a set of known zero and one bits, compute the maximum and minimum values that
00730 // could have the specified known zero and known one bits, returning them in
00731 // min/max.
00732 static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty,
00733                                                      const APInt &KnownZero,
00734                                                      const APInt &KnownOne,
00735                                                      APInt &Min, APInt &Max) {
00736   uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth;
00737   assert(KnownZero.getBitWidth() == BitWidth && 
00738          KnownOne.getBitWidth() == BitWidth &&
00739          Min.getBitWidth() == BitWidth && Max.getBitWidth() &&
00740          "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
00741   APInt UnknownBits = ~(KnownZero|KnownOne);
00742   
00743   // The minimum value is when the unknown bits are all zeros.
00744   Min = KnownOne;
00745   // The maximum value is when the unknown bits are all ones.
00746   Max = KnownOne|UnknownBits;
00747 }
00748 
00749 /// SimplifyDemandedBits - This function attempts to replace V with a simpler
00750 /// value based on the demanded bits. When this function is called, it is known
00751 /// that only the bits set in DemandedMask of the result of V are ever used
00752 /// downstream. Consequently, depending on the mask and V, it may be possible
00753 /// to replace V with a constant or one of its operands. In such cases, this
00754 /// function does the replacement and returns true. In all other cases, it
00755 /// returns false after analyzing the expression and setting KnownOne and known
00756 /// to be one in the expression. KnownZero contains all the bits that are known
00757 /// to be zero in the expression. These are provided to potentially allow the
00758 /// caller (which might recursively be SimplifyDemandedBits itself) to simplify
00759 /// the expression. KnownOne and KnownZero always follow the invariant that 
00760 /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
00761 /// the bits in KnownOne and KnownZero may only be accurate for those bits set
00762 /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
00763 /// and KnownOne must all be the same.
00764 bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask,
00765                                         APInt& KnownZero, APInt& KnownOne,
00766                                         unsigned Depth) {
00767   assert(V != 0 && "Null pointer of Value???");
00768   assert(Depth <= 6 && "Limit Search Depth");
00769   uint32_t BitWidth = DemandedMask.getBitWidth();
00770   const IntegerType *VTy = cast<IntegerType>(V->getType());
00771   assert(VTy->getBitWidth() == BitWidth && 
00772          KnownZero.getBitWidth() == BitWidth && 
00773          KnownOne.getBitWidth() == BitWidth &&
00774          "Value *V, DemandedMask, KnownZero and KnownOne \
00775           must have same BitWidth");
00776   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00777     // We know all of the bits for a constant!
00778     KnownOne = CI->getValue() & DemandedMask;
00779     KnownZero = ~KnownOne & DemandedMask;
00780     return false;
00781   }
00782   
00783   KnownZero.clear(); 
00784   KnownOne.clear();
00785   if (!V->hasOneUse()) {    // Other users may use these bits.
00786     if (Depth != 0) {       // Not at the root.
00787       // Just compute the KnownZero/KnownOne bits to simplify things downstream.
00788       ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth);
00789       return false;
00790     }
00791     // If this is the root being simplified, allow it to have multiple uses,
00792     // just set the DemandedMask to all bits.
00793     DemandedMask = APInt::getAllOnesValue(BitWidth);
00794   } else if (DemandedMask == 0) {   // Not demanding any bits from V.
00795     if (V != UndefValue::get(VTy))
00796       return UpdateValueUsesWith(V, UndefValue::get(VTy));
00797     return false;
00798   } else if (Depth == 6) {        // Limit search depth.
00799     return false;
00800   }
00801   
00802   Instruction *I = dyn_cast<Instruction>(V);
00803   if (!I) return false;        // Only analyze instructions.
00804 
00805   APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
00806   APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne;
00807   switch (I->getOpcode()) {
00808   default:
00809     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
00810     break;
00811   case Instruction::And:
00812     // If either the LHS or the RHS are Zero, the result is zero.
00813     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
00814                              RHSKnownZero, RHSKnownOne, Depth+1))
00815       return true;
00816     assert((RHSKnownZero & RHSKnownOne) == 0 && 
00817            "Bits known to be one AND zero?"); 
00818 
00819     // If something is known zero on the RHS, the bits aren't demanded on the
00820     // LHS.
00821     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero,
00822                              LHSKnownZero, LHSKnownOne, Depth+1))
00823       return true;
00824     assert((LHSKnownZero & LHSKnownOne) == 0 && 
00825            "Bits known to be one AND zero?"); 
00826 
00827     // If all of the demanded bits are known 1 on one side, return the other.
00828     // These bits cannot contribute to the result of the 'and'.
00829     if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == 
00830         (DemandedMask & ~LHSKnownZero))
00831       return UpdateValueUsesWith(I, I->getOperand(0));
00832     if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == 
00833         (DemandedMask & ~RHSKnownZero))
00834       return UpdateValueUsesWith(I, I->getOperand(1));
00835     
00836     // If all of the demanded bits in the inputs are known zeros, return zero.
00837     if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
00838       return UpdateValueUsesWith(I, Constant::getNullValue(VTy));
00839       
00840     // If the RHS is a constant, see if we can simplify it.
00841     if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
00842       return UpdateValueUsesWith(I, I);
00843       
00844     // Output known-1 bits are only known if set in both the LHS & RHS.
00845     RHSKnownOne &= LHSKnownOne;
00846     // Output known-0 are known to be clear if zero in either the LHS | RHS.
00847     RHSKnownZero |= LHSKnownZero;
00848     break;
00849   case Instruction::Or:
00850     // If either the LHS or the RHS are One, the result is One.
00851     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, 
00852                              RHSKnownZero, RHSKnownOne, Depth+1))
00853       return true;
00854     assert((RHSKnownZero & RHSKnownOne) == 0 && 
00855            "Bits known to be one AND zero?"); 
00856     // If something is known one on the RHS, the bits aren't demanded on the
00857     // LHS.
00858     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, 
00859                              LHSKnownZero, LHSKnownOne, Depth+1))
00860       return true;
00861     assert((LHSKnownZero & LHSKnownOne) == 0 && 
00862            "Bits known to be one AND zero?"); 
00863     
00864     // If all of the demanded bits are known zero on one side, return the other.
00865     // These bits cannot contribute to the result of the 'or'.
00866     if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == 
00867         (DemandedMask & ~LHSKnownOne))
00868       return UpdateValueUsesWith(I, I->getOperand(0));
00869     if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == 
00870         (DemandedMask & ~RHSKnownOne))
00871       return UpdateValueUsesWith(I, I->getOperand(1));
00872 
00873     // If all of the potentially set bits on one side are known to be set on
00874     // the other side, just use the 'other' side.
00875     if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == 
00876         (DemandedMask & (~RHSKnownZero)))
00877       return UpdateValueUsesWith(I, I->getOperand(0));
00878     if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == 
00879         (DemandedMask & (~LHSKnownZero)))
00880       return UpdateValueUsesWith(I, I->getOperand(1));
00881         
00882     // If the RHS is a constant, see if we can simplify it.
00883     if (ShrinkDemandedConstant(I, 1, DemandedMask))
00884       return UpdateValueUsesWith(I, I);
00885           
00886     // Output known-0 bits are only known if clear in both the LHS & RHS.
00887     RHSKnownZero &= LHSKnownZero;
00888     // Output known-1 are known to be set if set in either the LHS | RHS.
00889     RHSKnownOne |= LHSKnownOne;
00890     break;
00891   case Instruction::Xor: {
00892     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
00893                              RHSKnownZero, RHSKnownOne, Depth+1))
00894       return true;
00895     assert((RHSKnownZero & RHSKnownOne) == 0 && 
00896            "Bits known to be one AND zero?"); 
00897     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, 
00898                              LHSKnownZero, LHSKnownOne, Depth+1))
00899       return true;
00900     assert((LHSKnownZero & LHSKnownOne) == 0 && 
00901            "Bits known to be one AND zero?"); 
00902     
00903     // If all of the demanded bits are known zero on one side, return the other.
00904     // These bits cannot contribute to the result of the 'xor'.
00905     if ((DemandedMask & RHSKnownZero) == DemandedMask)
00906       return UpdateValueUsesWith(I, I->getOperand(0));
00907     if ((DemandedMask & LHSKnownZero) == DemandedMask)
00908       return UpdateValueUsesWith(I, I->getOperand(1));
00909     
00910     // Output known-0 bits are known if clear or set in both the LHS & RHS.
00911     APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | 
00912                          (RHSKnownOne & LHSKnownOne);
00913     // Output known-1 are known to be set if set in only one of the LHS, RHS.
00914     APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | 
00915                         (RHSKnownOne & LHSKnownZero);
00916     
00917     // If all of the demanded bits are known to be zero on one side or the
00918     // other, turn this into an *inclusive* or.
00919     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
00920     if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
00921       Instruction *Or =
00922         BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
00923                                  I->getName());
00924       InsertNewInstBefore(Or, *I);
00925       return UpdateValueUsesWith(I, Or);
00926     }
00927     
00928     // If all of the demanded bits on one side are known, and all of the set
00929     // bits on that side are also known to be set on the other side, turn this
00930     // into an AND, as we know the bits will be cleared.
00931     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
00932     if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { 
00933       // all known
00934       if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
00935         Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask);
00936         Instruction *And = 
00937           BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
00938         InsertNewInstBefore(And, *I);
00939         return UpdateValueUsesWith(I, And);
00940       }
00941     }
00942     
00943     // If the RHS is a constant, see if we can simplify it.
00944     // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
00945     if (ShrinkDemandedConstant(I, 1, DemandedMask))
00946       return UpdateValueUsesWith(I, I);
00947     
00948     RHSKnownZero = KnownZeroOut;
00949     RHSKnownOne  = KnownOneOut;
00950     break;
00951   }
00952   case Instruction::Select:
00953     if (SimplifyDemandedBits(I->getOperand(2), DemandedMask,
00954                              RHSKnownZero, RHSKnownOne, Depth+1))
00955       return true;
00956     if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, 
00957                              LHSKnownZero, LHSKnownOne, Depth+1))
00958       return true;
00959     assert((RHSKnownZero & RHSKnownOne) == 0 && 
00960            "Bits known to be one AND zero?"); 
00961     assert((LHSKnownZero & LHSKnownOne) == 0 && 
00962            "Bits known to be one AND zero?"); 
00963     
00964     // If the operands are constants, see if we can simplify them.
00965     if (ShrinkDemandedConstant(I, 1, DemandedMask))
00966       return UpdateValueUsesWith(I, I);
00967     if (ShrinkDemandedConstant(I, 2, DemandedMask))
00968       return UpdateValueUsesWith(I, I);
00969     
00970     // Only known if known in both the LHS and RHS.
00971     RHSKnownOne &= LHSKnownOne;
00972     RHSKnownZero &= LHSKnownZero;
00973     break;
00974   case Instruction::Trunc: {
00975     uint32_t truncBf = 
00976       cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth();
00977     DemandedMask.zext(truncBf);
00978     RHSKnownZero.zext(truncBf);
00979     RHSKnownOne.zext(truncBf);
00980     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, 
00981                              RHSKnownZero, RHSKnownOne, Depth+1))
00982       return true;
00983     DemandedMask.trunc(BitWidth);
00984     RHSKnownZero.trunc(BitWidth);
00985     RHSKnownOne.trunc(BitWidth);
00986     assert((RHSKnownZero & RHSKnownOne) == 0 && 
00987            "Bits known to be one AND zero?"); 
00988     break;
00989   }
00990   case Instruction::BitCast:
00991     if (!I->getOperand(0)->getType()->isInteger())
00992       return false;
00993       
00994     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
00995                              RHSKnownZero, RHSKnownOne, Depth+1))
00996       return true;
00997     assert((RHSKnownZero & RHSKnownOne) == 0 && 
00998            "Bits known to be one AND zero?"); 
00999     break;
01000   case Instruction::ZExt: {
01001     // Compute the bits in the result that are not present in the input.
01002     const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
01003     uint32_t SrcBitWidth = SrcTy->getBitWidth();
01004     
01005     DemandedMask.trunc(SrcBitWidth);
01006     RHSKnownZero.trunc(SrcBitWidth);
01007     RHSKnownOne.trunc(SrcBitWidth);
01008     if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
01009                              RHSKnownZero, RHSKnownOne, Depth+1))
01010       return true;
01011     DemandedMask.zext(BitWidth);
01012     RHSKnownZero.zext(BitWidth);
01013     RHSKnownOne.zext(BitWidth);
01014     assert((RHSKnownZero & RHSKnownOne) == 0 && 
01015            "Bits known to be one AND zero?"); 
01016     // The top bits are known to be zero.
01017     RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
01018     break;
01019   }
01020   case Instruction::SExt: {
01021     // Compute the bits in the result that are not present in the input.
01022     const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
01023     uint32_t SrcBitWidth = SrcTy->getBitWidth();
01024     
01025     APInt InputDemandedBits = DemandedMask & 
01026                               APInt::getLowBitsSet(BitWidth, SrcBitWidth);
01027 
01028     APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
01029     // If any of the sign extended bits are demanded, we know that the sign
01030     // bit is demanded.
01031     if ((NewBits & DemandedMask) != 0)
01032       InputDemandedBits.set(SrcBitWidth-1);
01033       
01034     InputDemandedBits.trunc(SrcBitWidth);
01035     RHSKnownZero.trunc(SrcBitWidth);
01036     RHSKnownOne.trunc(SrcBitWidth);
01037     if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits,
01038                              RHSKnownZero, RHSKnownOne, Depth+1))
01039       return true;
01040     InputDemandedBits.zext(BitWidth);
01041     RHSKnownZero.zext(BitWidth);
01042     RHSKnownOne.zext(BitWidth);
01043     assert((RHSKnownZero & RHSKnownOne) == 0 && 
01044            "Bits known to be one AND zero?"); 
01045       
01046     // If the sign bit of the input is known set or clear, then we know the
01047     // top bits of the result.
01048 
01049     // If the input sign bit is known zero, or if the NewBits are not demanded
01050     // convert this into a zero extension.
01051     if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits)
01052     {
01053       // Convert to ZExt cast
01054       CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I);
01055       return UpdateValueUsesWith(I, NewCast);
01056     } else if (RHSKnownOne[SrcBitWidth-1]) {    // Input sign bit known set
01057       RHSKnownOne |= NewBits;
01058     }
01059     break;
01060   }
01061   case Instruction::Add: {
01062     // Figure out what the input bits are.  If the top bits of the and result
01063     // are not demanded, then the add doesn't demand them from its input
01064     // either.
01065     uint32_t NLZ = DemandedMask.countLeadingZeros();
01066       
01067     // If there is a constant on the RHS, there are a variety of xformations
01068     // we can do.
01069     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
01070       // If null, this should be simplified elsewhere.  Some of the xforms here
01071       // won't work if the RHS is zero.
01072       if (RHS->isZero())
01073         break;
01074       
01075       // If the top bit of the output is demanded, demand everything from the
01076       // input.  Otherwise, we demand all the input bits except NLZ top bits.
01077       APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
01078 
01079       // Find information about known zero/one bits in the input.
01080       if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, 
01081                                LHSKnownZero, LHSKnownOne, Depth+1))
01082         return true;
01083 
01084       // If the RHS of the add has bits set that can't affect the input, reduce
01085       // the constant.
01086       if (ShrinkDemandedConstant(I, 1, InDemandedBits))
01087         return UpdateValueUsesWith(I, I);
01088       
01089       // Avoid excess work.
01090       if (LHSKnownZero == 0 && LHSKnownOne == 0)
01091         break;
01092       
01093       // Turn it into OR if input bits are zero.
01094       if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
01095         Instruction *Or =
01096           BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
01097                                    I->getName());
01098         InsertNewInstBefore(Or, *I);
01099         return UpdateValueUsesWith(I, Or);
01100       }
01101       
01102       // We can say something about the output known-zero and known-one bits,
01103       // depending on potential carries from the input constant and the
01104       // unknowns.  For example if the LHS is known to have at most the 0x0F0F0
01105       // bits set and the RHS constant is 0x01001, then we know we have a known
01106       // one mask of 0x00001 and a known zero mask of 0xE0F0E.
01107       
01108       // To compute this, we first compute the potential carry bits.  These are
01109       // the bits which may be modified.  I'm not aware of a better way to do
01110       // this scan.
01111       const APInt& RHSVal = RHS->getValue();
01112       APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
01113       
01114       // Now that we know which bits have carries, compute the known-1/0 sets.
01115       
01116       // Bits are known one if they are known zero in one operand and one in the
01117       // other, and there is no input carry.
01118       RHSKnownOne = ((LHSKnownZero & RHSVal) | 
01119                      (LHSKnownOne & ~RHSVal)) & ~CarryBits;
01120       
01121       // Bits are known zero if they are known zero in both operands and there
01122       // is no input carry.
01123       RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
01124     } else {
01125       // If the high-bits of this ADD are not demanded, then it does not demand
01126       // the high bits of its LHS or RHS.
01127       if (DemandedMask[BitWidth-1] == 0) {
01128         // Right fill the mask of bits for this ADD to demand the most
01129         // significant bit and all those below it.
01130         APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
01131         if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
01132                                  LHSKnownZero, LHSKnownOne, Depth+1))
01133           return true;
01134         if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
01135                                  LHSKnownZero, LHSKnownOne, Depth+1))
01136           return true;
01137       }
01138     }
01139     break;
01140   }
01141   case Instruction::Sub:
01142     // If the high-bits of this SUB are not demanded, then it does not demand
01143     // the high bits of its LHS or RHS.
01144     if (DemandedMask[BitWidth-1] == 0) {
01145       // Right fill the mask of bits for this SUB to demand the most
01146       // significant bit and all those below it.
01147       uint32_t NLZ = DemandedMask.countLeadingZeros();
01148       APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
01149       if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
01150                                LHSKnownZero, LHSKnownOne, Depth+1))
01151         return true;
01152       if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
01153                                LHSKnownZero, LHSKnownOne, Depth+1))
01154         return true;
01155     }
01156     // Otherwise just hand the sub off to ComputeMaskedBits to fill in
01157     // the known zeros and ones.
01158     ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
01159     break;
01160   case Instruction::Shl:
01161     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
01162       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
01163       APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
01164       if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, 
01165                                RHSKnownZero, RHSKnownOne, Depth+1))
01166         return true;
01167       assert((RHSKnownZero & RHSKnownOne) == 0 && 
01168              "Bits known to be one AND zero?"); 
01169       RHSKnownZero <<= ShiftAmt;
01170       RHSKnownOne  <<= ShiftAmt;
01171       // low bits known zero.
01172       if (ShiftAmt)
01173         RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
01174     }
01175     break;
01176   case Instruction::LShr:
01177     // For a logical shift right
01178     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
01179       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
01180       
01181       // Unsigned shift right.
01182       APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
01183       if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn,
01184                                RHSKnownZero, RHSKnownOne, Depth+1))
01185         return true;
01186       assert((RHSKnownZero & RHSKnownOne) == 0 && 
01187              "Bits known to be one AND zero?"); 
01188       RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
01189       RHSKnownOne  = APIntOps::lshr(RHSKnownOne, ShiftAmt);
01190       if (ShiftAmt) {
01191         // Compute the new bits that are at the top now.
01192         APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
01193         RHSKnownZero |= HighBits;  // high bits known zero.
01194       }
01195     }
01196     break;
01197   case Instruction::AShr:
01198     // If this is an arithmetic shift right and only the low-bit is set, we can
01199     // always convert this into a logical shr, even if the shift amount is
01200     // variable.  The low bit of the shift cannot be an input sign bit unless
01201     // the shift amount is >= the size of the datatype, which is undefined.
01202     if (DemandedMask == 1) {
01203       // Perform the logical shift right.
01204       Value *NewVal = BinaryOperator::CreateLShr(
01205                         I->getOperand(0), I->getOperand(1), I->getName());
01206       InsertNewInstBefore(cast<Instruction>(NewVal), *I);
01207       return UpdateValueUsesWith(I, NewVal);
01208     }    
01209 
01210     // If the sign bit is the only bit demanded by this ashr, th