LLVM API Documentation

InstCombineAndOrXor.cpp
Go to the documentation of this file.
00001 //===- InstCombineAndOrXor.cpp --------------------------------------------===//
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 // This file implements the visitAnd, visitOr, and visitXor functions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "InstCombine.h"
00015 #include "llvm/Analysis/InstructionSimplify.h"
00016 #include "llvm/IR/Intrinsics.h"
00017 #include "llvm/Support/ConstantRange.h"
00018 #include "llvm/Support/PatternMatch.h"
00019 #include "llvm/Transforms/Utils/CmpInstAnalysis.h"
00020 using namespace llvm;
00021 using namespace PatternMatch;
00022 
00023 
00024 /// AddOne - Add one to a ConstantInt.
00025 static Constant *AddOne(ConstantInt *C) {
00026   return ConstantInt::get(C->getContext(), C->getValue() + 1);
00027 }
00028 /// SubOne - Subtract one from a ConstantInt.
00029 static Constant *SubOne(ConstantInt *C) {
00030   return ConstantInt::get(C->getContext(), C->getValue()-1);
00031 }
00032 
00033 /// isFreeToInvert - Return true if the specified value is free to invert (apply
00034 /// ~ to).  This happens in cases where the ~ can be eliminated.
00035 static inline bool isFreeToInvert(Value *V) {
00036   // ~(~(X)) -> X.
00037   if (BinaryOperator::isNot(V))
00038     return true;
00039 
00040   // Constants can be considered to be not'ed values.
00041   if (isa<ConstantInt>(V))
00042     return true;
00043 
00044   // Compares can be inverted if they have a single use.
00045   if (CmpInst *CI = dyn_cast<CmpInst>(V))
00046     return CI->hasOneUse();
00047 
00048   return false;
00049 }
00050 
00051 static inline Value *dyn_castNotVal(Value *V) {
00052   // If this is not(not(x)) don't return that this is a not: we want the two
00053   // not's to be folded first.
00054   if (BinaryOperator::isNot(V)) {
00055     Value *Operand = BinaryOperator::getNotArgument(V);
00056     if (!isFreeToInvert(Operand))
00057       return Operand;
00058   }
00059 
00060   // Constants can be considered to be not'ed values...
00061   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
00062     return ConstantInt::get(C->getType(), ~C->getValue());
00063   return 0;
00064 }
00065 
00066 /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp
00067 /// predicate into a three bit mask. It also returns whether it is an ordered
00068 /// predicate by reference.
00069 static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) {
00070   isOrdered = false;
00071   switch (CC) {
00072   case FCmpInst::FCMP_ORD: isOrdered = true; return 0;  // 000
00073   case FCmpInst::FCMP_UNO:                   return 0;  // 000
00074   case FCmpInst::FCMP_OGT: isOrdered = true; return 1;  // 001
00075   case FCmpInst::FCMP_UGT:                   return 1;  // 001
00076   case FCmpInst::FCMP_OEQ: isOrdered = true; return 2;  // 010
00077   case FCmpInst::FCMP_UEQ:                   return 2;  // 010
00078   case FCmpInst::FCMP_OGE: isOrdered = true; return 3;  // 011
00079   case FCmpInst::FCMP_UGE:                   return 3;  // 011
00080   case FCmpInst::FCMP_OLT: isOrdered = true; return 4;  // 100
00081   case FCmpInst::FCMP_ULT:                   return 4;  // 100
00082   case FCmpInst::FCMP_ONE: isOrdered = true; return 5;  // 101
00083   case FCmpInst::FCMP_UNE:                   return 5;  // 101
00084   case FCmpInst::FCMP_OLE: isOrdered = true; return 6;  // 110
00085   case FCmpInst::FCMP_ULE:                   return 6;  // 110
00086     // True -> 7
00087   default:
00088     // Not expecting FCMP_FALSE and FCMP_TRUE;
00089     llvm_unreachable("Unexpected FCmp predicate!");
00090   }
00091 }
00092 
00093 /// getNewICmpValue - This is the complement of getICmpCode, which turns an
00094 /// opcode and two operands into either a constant true or false, or a brand
00095 /// new ICmp instruction. The sign is passed in to determine which kind
00096 /// of predicate to use in the new icmp instruction.
00097 static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
00098                               InstCombiner::BuilderTy *Builder) {
00099   ICmpInst::Predicate NewPred;
00100   if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred))
00101     return NewConstant;
00102   return Builder->CreateICmp(NewPred, LHS, RHS);
00103 }
00104 
00105 /// getFCmpValue - This is the complement of getFCmpCode, which turns an
00106 /// opcode and two operands into either a FCmp instruction. isordered is passed
00107 /// in to determine which kind of predicate to use in the new fcmp instruction.
00108 static Value *getFCmpValue(bool isordered, unsigned code,
00109                            Value *LHS, Value *RHS,
00110                            InstCombiner::BuilderTy *Builder) {
00111   CmpInst::Predicate Pred;
00112   switch (code) {
00113   default: llvm_unreachable("Illegal FCmp code!");
00114   case 0: Pred = isordered ? FCmpInst::FCMP_ORD : FCmpInst::FCMP_UNO; break;
00115   case 1: Pred = isordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; break;
00116   case 2: Pred = isordered ? FCmpInst::FCMP_OEQ : FCmpInst::FCMP_UEQ; break;
00117   case 3: Pred = isordered ? FCmpInst::FCMP_OGE : FCmpInst::FCMP_UGE; break;
00118   case 4: Pred = isordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; break;
00119   case 5: Pred = isordered ? FCmpInst::FCMP_ONE : FCmpInst::FCMP_UNE; break;
00120   case 6: Pred = isordered ? FCmpInst::FCMP_OLE : FCmpInst::FCMP_ULE; break;
00121   case 7:
00122     if (!isordered) return ConstantInt::getTrue(LHS->getContext());
00123     Pred = FCmpInst::FCMP_ORD; break;
00124   }
00125   return Builder->CreateFCmp(Pred, LHS, RHS);
00126 }
00127 
00128 // OptAndOp - This handles expressions of the form ((val OP C1) & C2).  Where
00129 // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is
00130 // guaranteed to be a binary operator.
00131 Instruction *InstCombiner::OptAndOp(Instruction *Op,
00132                                     ConstantInt *OpRHS,
00133                                     ConstantInt *AndRHS,
00134                                     BinaryOperator &TheAnd) {
00135   Value *X = Op->getOperand(0);
00136   Constant *Together = 0;
00137   if (!Op->isShift())
00138     Together = ConstantExpr::getAnd(AndRHS, OpRHS);
00139 
00140   switch (Op->getOpcode()) {
00141   case Instruction::Xor:
00142     if (Op->hasOneUse()) {
00143       // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
00144       Value *And = Builder->CreateAnd(X, AndRHS);
00145       And->takeName(Op);
00146       return BinaryOperator::CreateXor(And, Together);
00147     }
00148     break;
00149   case Instruction::Or:
00150     if (Op->hasOneUse()){
00151       if (Together != OpRHS) {
00152         // (X | C1) & C2 --> (X | (C1&C2)) & C2
00153         Value *Or = Builder->CreateOr(X, Together);
00154         Or->takeName(Op);
00155         return BinaryOperator::CreateAnd(Or, AndRHS);
00156       }
00157 
00158       ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together);
00159       if (TogetherCI && !TogetherCI->isZero()){
00160         // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1
00161         // NOTE: This reduces the number of bits set in the & mask, which
00162         // can expose opportunities for store narrowing.
00163         Together = ConstantExpr::getXor(AndRHS, Together);
00164         Value *And = Builder->CreateAnd(X, Together);
00165         And->takeName(Op);
00166         return BinaryOperator::CreateOr(And, OpRHS);
00167       }
00168     }
00169 
00170     break;
00171   case Instruction::Add:
00172     if (Op->hasOneUse()) {
00173       // Adding a one to a single bit bit-field should be turned into an XOR
00174       // of the bit.  First thing to check is to see if this AND is with a
00175       // single bit constant.
00176       const APInt &AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
00177 
00178       // If there is only one bit set.
00179       if (AndRHSV.isPowerOf2()) {
00180         // Ok, at this point, we know that we are masking the result of the
00181         // ADD down to exactly one bit.  If the constant we are adding has
00182         // no bits set below this bit, then we can eliminate the ADD.
00183         const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
00184 
00185         // Check to see if any bits below the one bit set in AndRHSV are set.
00186         if ((AddRHS & (AndRHSV-1)) == 0) {
00187           // If not, the only thing that can effect the output of the AND is
00188           // the bit specified by AndRHSV.  If that bit is set, the effect of
00189           // the XOR is to toggle the bit.  If it is clear, then the ADD has
00190           // no effect.
00191           if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
00192             TheAnd.setOperand(0, X);
00193             return &TheAnd;
00194           } else {
00195             // Pull the XOR out of the AND.
00196             Value *NewAnd = Builder->CreateAnd(X, AndRHS);
00197             NewAnd->takeName(Op);
00198             return BinaryOperator::CreateXor(NewAnd, AndRHS);
00199           }
00200         }
00201       }
00202     }
00203     break;
00204 
00205   case Instruction::Shl: {
00206     // We know that the AND will not produce any of the bits shifted in, so if
00207     // the anded constant includes them, clear them now!
00208     //
00209     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
00210     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
00211     APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
00212     ConstantInt *CI = ConstantInt::get(AndRHS->getContext(),
00213                                        AndRHS->getValue() & ShlMask);
00214 
00215     if (CI->getValue() == ShlMask)
00216       // Masking out bits that the shift already masks.
00217       return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and.
00218 
00219     if (CI != AndRHS) {                  // Reducing bits set in and.
00220       TheAnd.setOperand(1, CI);
00221       return &TheAnd;
00222     }
00223     break;
00224   }
00225   case Instruction::LShr: {
00226     // We know that the AND will not produce any of the bits shifted in, so if
00227     // the anded constant includes them, clear them now!  This only applies to
00228     // unsigned shifts, because a signed shr may bring in set bits!
00229     //
00230     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
00231     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
00232     APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
00233     ConstantInt *CI = ConstantInt::get(Op->getContext(),
00234                                        AndRHS->getValue() & ShrMask);
00235 
00236     if (CI->getValue() == ShrMask)
00237       // Masking out bits that the shift already masks.
00238       return ReplaceInstUsesWith(TheAnd, Op);
00239 
00240     if (CI != AndRHS) {
00241       TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
00242       return &TheAnd;
00243     }
00244     break;
00245   }
00246   case Instruction::AShr:
00247     // Signed shr.
00248     // See if this is shifting in some sign extension, then masking it out
00249     // with an and.
00250     if (Op->hasOneUse()) {
00251       uint32_t BitWidth = AndRHS->getType()->getBitWidth();
00252       uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
00253       APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
00254       Constant *C = ConstantInt::get(Op->getContext(),
00255                                      AndRHS->getValue() & ShrMask);
00256       if (C == AndRHS) {          // Masking out bits shifted in.
00257         // (Val ashr C1) & C2 -> (Val lshr C1) & C2
00258         // Make the argument unsigned.
00259         Value *ShVal = Op->getOperand(0);
00260         ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
00261         return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
00262       }
00263     }
00264     break;
00265   }
00266   return 0;
00267 }
00268 
00269 /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
00270 /// (V < Lo || V >= Hi).  In practice, we emit the more efficient
00271 /// (V-Lo) <u Hi-Lo.  This method expects that Lo <= Hi. isSigned indicates
00272 /// whether to treat the V, Lo and HI as signed or not. IB is the location to
00273 /// insert new instructions.
00274 Value *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
00275                                      bool isSigned, bool Inside) {
00276   assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
00277             ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
00278          "Lo is not <= Hi in range emission code!");
00279 
00280   if (Inside) {
00281     if (Lo == Hi)  // Trivially false.
00282       return ConstantInt::getFalse(V->getContext());
00283 
00284     // V >= Min && V < Hi --> V < Hi
00285     if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
00286       ICmpInst::Predicate pred = (isSigned ?
00287         ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
00288       return Builder->CreateICmp(pred, V, Hi);
00289     }
00290 
00291     // Emit V-Lo <u Hi-Lo
00292     Constant *NegLo = ConstantExpr::getNeg(Lo);
00293     Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
00294     Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
00295     return Builder->CreateICmpULT(Add, UpperBound);
00296   }
00297 
00298   if (Lo == Hi)  // Trivially true.
00299     return ConstantInt::getTrue(V->getContext());
00300 
00301   // V < Min || V >= Hi -> V > Hi-1
00302   Hi = SubOne(cast<ConstantInt>(Hi));
00303   if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
00304     ICmpInst::Predicate pred = (isSigned ?
00305         ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
00306     return Builder->CreateICmp(pred, V, Hi);
00307   }
00308 
00309   // Emit V-Lo >u Hi-1-Lo
00310   // Note that Hi has already had one subtracted from it, above.
00311   ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
00312   Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
00313   Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
00314   return Builder->CreateICmpUGT(Add, LowerBound);
00315 }
00316 
00317 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
00318 // any number of 0s on either side.  The 1s are allowed to wrap from LSB to
00319 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
00320 // not, since all 1s are not contiguous.
00321 static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
00322   const APInt& V = Val->getValue();
00323   uint32_t BitWidth = Val->getType()->getBitWidth();
00324   if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
00325 
00326   // look for the first zero bit after the run of ones
00327   MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
00328   // look for the first non-zero bit
00329   ME = V.getActiveBits();
00330   return true;
00331 }
00332 
00333 /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
00334 /// where isSub determines whether the operator is a sub.  If we can fold one of
00335 /// the following xforms:
00336 ///
00337 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
00338 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
00339 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
00340 ///
00341 /// return (A +/- B).
00342 ///
00343 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
00344                                         ConstantInt *Mask, bool isSub,
00345                                         Instruction &I) {
00346   Instruction *LHSI = dyn_cast<Instruction>(LHS);
00347   if (!LHSI || LHSI->getNumOperands() != 2 ||
00348       !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
00349 
00350   ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
00351 
00352   switch (LHSI->getOpcode()) {
00353   default: return 0;
00354   case Instruction::And:
00355     if (ConstantExpr::getAnd(N, Mask) == Mask) {
00356       // If the AndRHS is a power of two minus one (0+1+), this is simple.
00357       if ((Mask->getValue().countLeadingZeros() +
00358            Mask->getValue().countPopulation()) ==
00359           Mask->getValue().getBitWidth())
00360         break;
00361 
00362       // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
00363       // part, we don't need any explicit masks to take them out of A.  If that
00364       // is all N is, ignore it.
00365       uint32_t MB = 0, ME = 0;
00366       if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
00367         uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
00368         APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
00369         if (MaskedValueIsZero(RHS, Mask))
00370           break;
00371       }
00372     }
00373     return 0;
00374   case Instruction::Or:
00375   case Instruction::Xor:
00376     // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
00377     if ((Mask->getValue().countLeadingZeros() +
00378          Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
00379         && ConstantExpr::getAnd(N, Mask)->isNullValue())
00380       break;
00381     return 0;
00382   }
00383 
00384   if (isSub)
00385     return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
00386   return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
00387 }
00388 
00389 /// enum for classifying (icmp eq (A & B), C) and (icmp ne (A & B), C)
00390 /// One of A and B is considered the mask, the other the value. This is
00391 /// described as the "AMask" or "BMask" part of the enum. If the enum
00392 /// contains only "Mask", then both A and B can be considered masks.
00393 /// If A is the mask, then it was proven, that (A & C) == C. This
00394 /// is trivial if C == A, or C == 0. If both A and C are constants, this
00395 /// proof is also easy.
00396 /// For the following explanations we assume that A is the mask.
00397 /// The part "AllOnes" declares, that the comparison is true only
00398 /// if (A & B) == A, or all bits of A are set in B.
00399 ///   Example: (icmp eq (A & 3), 3) -> FoldMskICmp_AMask_AllOnes
00400 /// The part "AllZeroes" declares, that the comparison is true only
00401 /// if (A & B) == 0, or all bits of A are cleared in B.
00402 ///   Example: (icmp eq (A & 3), 0) -> FoldMskICmp_Mask_AllZeroes
00403 /// The part "Mixed" declares, that (A & B) == C and C might or might not
00404 /// contain any number of one bits and zero bits.
00405 ///   Example: (icmp eq (A & 3), 1) -> FoldMskICmp_AMask_Mixed
00406 /// The Part "Not" means, that in above descriptions "==" should be replaced
00407 /// by "!=".
00408 ///   Example: (icmp ne (A & 3), 3) -> FoldMskICmp_AMask_NotAllOnes
00409 /// If the mask A contains a single bit, then the following is equivalent:
00410 ///    (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
00411 ///    (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
00412 enum MaskedICmpType {
00413   FoldMskICmp_AMask_AllOnes           =     1,
00414   FoldMskICmp_AMask_NotAllOnes        =     2,
00415   FoldMskICmp_BMask_AllOnes           =     4,
00416   FoldMskICmp_BMask_NotAllOnes        =     8,
00417   FoldMskICmp_Mask_AllZeroes          =    16,
00418   FoldMskICmp_Mask_NotAllZeroes       =    32,
00419   FoldMskICmp_AMask_Mixed             =    64,
00420   FoldMskICmp_AMask_NotMixed          =   128,
00421   FoldMskICmp_BMask_Mixed             =   256,
00422   FoldMskICmp_BMask_NotMixed          =   512
00423 };
00424 
00425 /// return the set of pattern classes (from MaskedICmpType)
00426 /// that (icmp SCC (A & B), C) satisfies
00427 static unsigned getTypeOfMaskedICmp(Value* A, Value* B, Value* C,
00428                                     ICmpInst::Predicate SCC)
00429 {
00430   ConstantInt *ACst = dyn_cast<ConstantInt>(A);
00431   ConstantInt *BCst = dyn_cast<ConstantInt>(B);
00432   ConstantInt *CCst = dyn_cast<ConstantInt>(C);
00433   bool icmp_eq = (SCC == ICmpInst::ICMP_EQ);
00434   bool icmp_abit = (ACst != 0 && !ACst->isZero() &&
00435                     ACst->getValue().isPowerOf2());
00436   bool icmp_bbit = (BCst != 0 && !BCst->isZero() &&
00437                     BCst->getValue().isPowerOf2());
00438   unsigned result = 0;
00439   if (CCst != 0 && CCst->isZero()) {
00440     // if C is zero, then both A and B qualify as mask
00441     result |= (icmp_eq ? (FoldMskICmp_Mask_AllZeroes |
00442                           FoldMskICmp_Mask_AllZeroes |
00443                           FoldMskICmp_AMask_Mixed |
00444                           FoldMskICmp_BMask_Mixed)
00445                        : (FoldMskICmp_Mask_NotAllZeroes |
00446                           FoldMskICmp_Mask_NotAllZeroes |
00447                           FoldMskICmp_AMask_NotMixed |
00448                           FoldMskICmp_BMask_NotMixed));
00449     if (icmp_abit)
00450       result |= (icmp_eq ? (FoldMskICmp_AMask_NotAllOnes |
00451                             FoldMskICmp_AMask_NotMixed)
00452                          : (FoldMskICmp_AMask_AllOnes |
00453                             FoldMskICmp_AMask_Mixed));
00454     if (icmp_bbit)
00455       result |= (icmp_eq ? (FoldMskICmp_BMask_NotAllOnes |
00456                             FoldMskICmp_BMask_NotMixed)
00457                          : (FoldMskICmp_BMask_AllOnes |
00458                             FoldMskICmp_BMask_Mixed));
00459     return result;
00460   }
00461   if (A == C) {
00462     result |= (icmp_eq ? (FoldMskICmp_AMask_AllOnes |
00463                           FoldMskICmp_AMask_Mixed)
00464                        : (FoldMskICmp_AMask_NotAllOnes |
00465                           FoldMskICmp_AMask_NotMixed));
00466     if (icmp_abit)
00467       result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
00468                             FoldMskICmp_AMask_NotMixed)
00469                          : (FoldMskICmp_Mask_AllZeroes |
00470                             FoldMskICmp_AMask_Mixed));
00471   } else if (ACst != 0 && CCst != 0 &&
00472              ConstantExpr::getAnd(ACst, CCst) == CCst) {
00473     result |= (icmp_eq ? FoldMskICmp_AMask_Mixed
00474                        : FoldMskICmp_AMask_NotMixed);
00475   }
00476   if (B == C) {
00477     result |= (icmp_eq ? (FoldMskICmp_BMask_AllOnes |
00478                           FoldMskICmp_BMask_Mixed)
00479                        : (FoldMskICmp_BMask_NotAllOnes |
00480                           FoldMskICmp_BMask_NotMixed));
00481     if (icmp_bbit)
00482       result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
00483                             FoldMskICmp_BMask_NotMixed)
00484                          : (FoldMskICmp_Mask_AllZeroes |
00485                             FoldMskICmp_BMask_Mixed));
00486   } else if (BCst != 0 && CCst != 0 &&
00487              ConstantExpr::getAnd(BCst, CCst) == CCst) {
00488     result |= (icmp_eq ? FoldMskICmp_BMask_Mixed
00489                        : FoldMskICmp_BMask_NotMixed);
00490   }
00491   return result;
00492 }
00493 
00494 /// decomposeBitTestICmp - Decompose an icmp into the form ((X & Y) pred Z)
00495 /// if possible. The returned predicate is either == or !=. Returns false if
00496 /// decomposition fails.
00497 static bool decomposeBitTestICmp(const ICmpInst *I, ICmpInst::Predicate &Pred,
00498                                  Value *&X, Value *&Y, Value *&Z) {
00499   // X < 0 is equivalent to (X & SignBit) != 0.
00500   if (I->getPredicate() == ICmpInst::ICMP_SLT)
00501     if (ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
00502       if (C->isZero()) {
00503         X = I->getOperand(0);
00504         Y = ConstantInt::get(I->getContext(),
00505                              APInt::getSignBit(C->getBitWidth()));
00506         Pred = ICmpInst::ICMP_NE;
00507         Z = C;
00508         return true;
00509       }
00510 
00511   // X > -1 is equivalent to (X & SignBit) == 0.
00512   if (I->getPredicate() == ICmpInst::ICMP_SGT)
00513     if (ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
00514       if (C->isAllOnesValue()) {
00515         X = I->getOperand(0);
00516         Y = ConstantInt::get(I->getContext(),
00517                              APInt::getSignBit(C->getBitWidth()));
00518         Pred = ICmpInst::ICMP_EQ;
00519         Z = ConstantInt::getNullValue(C->getType());
00520         return true;
00521       }
00522 
00523   return false;
00524 }
00525 
00526 /// foldLogOpOfMaskedICmpsHelper:
00527 /// handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
00528 /// return the set of pattern classes (from MaskedICmpType)
00529 /// that both LHS and RHS satisfy
00530 static unsigned foldLogOpOfMaskedICmpsHelper(Value*& A,
00531                                              Value*& B, Value*& C,
00532                                              Value*& D, Value*& E,
00533                                              ICmpInst *LHS, ICmpInst *RHS,
00534                                              ICmpInst::Predicate &LHSCC,
00535                                              ICmpInst::Predicate &RHSCC) {
00536   if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) return 0;
00537   // vectors are not (yet?) supported
00538   if (LHS->getOperand(0)->getType()->isVectorTy()) return 0;
00539 
00540   // Here comes the tricky part:
00541   // LHS might be of the form L11 & L12 == X, X == L21 & L22,
00542   // and L11 & L12 == L21 & L22. The same goes for RHS.
00543   // Now we must find those components L** and R**, that are equal, so
00544   // that we can extract the parameters A, B, C, D, and E for the canonical
00545   // above.
00546   Value *L1 = LHS->getOperand(0);
00547   Value *L2 = LHS->getOperand(1);
00548   Value *L11,*L12,*L21,*L22;
00549   // Check whether the icmp can be decomposed into a bit test.
00550   if (decomposeBitTestICmp(LHS, LHSCC, L11, L12, L2)) {
00551     L21 = L22 = L1 = 0;
00552   } else {
00553     // Look for ANDs in the LHS icmp.
00554     if (match(L1, m_And(m_Value(L11), m_Value(L12)))) {
00555       if (!match(L2, m_And(m_Value(L21), m_Value(L22))))
00556         L21 = L22 = 0;
00557     } else {
00558       if (!match(L2, m_And(m_Value(L11), m_Value(L12))))
00559         return 0;
00560       std::swap(L1, L2);
00561       L21 = L22 = 0;
00562     }
00563   }
00564 
00565   // Bail if LHS was a icmp that can't be decomposed into an equality.
00566   if (!ICmpInst::isEquality(LHSCC))
00567     return 0;
00568 
00569   Value *R1 = RHS->getOperand(0);
00570   Value *R2 = RHS->getOperand(1);
00571   Value *R11,*R12;
00572   bool ok = false;
00573   if (decomposeBitTestICmp(RHS, RHSCC, R11, R12, R2)) {
00574     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
00575       A = R11; D = R12;
00576     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
00577       A = R12; D = R11;
00578     } else {
00579       return 0;
00580     }
00581     E = R2; R1 = 0; ok = true;
00582   } else if (match(R1, m_And(m_Value(R11), m_Value(R12)))) {
00583     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
00584       A = R11; D = R12; E = R2; ok = true;
00585     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
00586       A = R12; D = R11; E = R2; ok = true;
00587     }
00588   }
00589 
00590   // Bail if RHS was a icmp that can't be decomposed into an equality.
00591   if (!ICmpInst::isEquality(RHSCC))
00592     return 0;
00593 
00594   // Look for ANDs in on the right side of the RHS icmp.
00595   if (!ok && match(R2, m_And(m_Value(R11), m_Value(R12)))) {
00596     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
00597       A = R11; D = R12; E = R1; ok = true;
00598     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
00599       A = R12; D = R11; E = R1; ok = true;
00600     } else {
00601       return 0;
00602     }
00603   }
00604   if (!ok)
00605     return 0;
00606 
00607   if (L11 == A) {
00608     B = L12; C = L2;
00609   } else if (L12 == A) {
00610     B = L11; C = L2;
00611   } else if (L21 == A) {
00612     B = L22; C = L1;
00613   } else if (L22 == A) {
00614     B = L21; C = L1;
00615   }
00616 
00617   unsigned left_type = getTypeOfMaskedICmp(A, B, C, LHSCC);
00618   unsigned right_type = getTypeOfMaskedICmp(A, D, E, RHSCC);
00619   return left_type & right_type;
00620 }
00621 /// foldLogOpOfMaskedICmps:
00622 /// try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
00623 /// into a single (icmp(A & X) ==/!= Y)
00624 static Value* foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS,
00625                                      ICmpInst::Predicate NEWCC,
00626                                      llvm::InstCombiner::BuilderTy* Builder) {
00627   Value *A = 0, *B = 0, *C = 0, *D = 0, *E = 0;
00628   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
00629   unsigned mask = foldLogOpOfMaskedICmpsHelper(A, B, C, D, E, LHS, RHS,
00630                                                LHSCC, RHSCC);
00631   if (mask == 0) return 0;
00632   assert(ICmpInst::isEquality(LHSCC) && ICmpInst::isEquality(RHSCC) &&
00633          "foldLogOpOfMaskedICmpsHelper must return an equality predicate.");
00634 
00635   if (NEWCC == ICmpInst::ICMP_NE)
00636     mask >>= 1; // treat "Not"-states as normal states
00637 
00638   if (mask & FoldMskICmp_Mask_AllZeroes) {
00639     // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
00640     // -> (icmp eq (A & (B|D)), 0)
00641     Value* newOr = Builder->CreateOr(B, D);
00642     Value* newAnd = Builder->CreateAnd(A, newOr);
00643     // we can't use C as zero, because we might actually handle
00644     //   (icmp ne (A & B), B) & (icmp ne (A & D), D)
00645     // with B and D, having a single bit set
00646     Value* zero = Constant::getNullValue(A->getType());
00647     return Builder->CreateICmp(NEWCC, newAnd, zero);
00648   }
00649   if (mask & FoldMskICmp_BMask_AllOnes) {
00650     // (icmp eq (A & B), B) & (icmp eq (A & D), D)
00651     // -> (icmp eq (A & (B|D)), (B|D))
00652     Value* newOr = Builder->CreateOr(B, D);
00653     Value* newAnd = Builder->CreateAnd(A, newOr);
00654     return Builder->CreateICmp(NEWCC, newAnd, newOr);
00655   }
00656   if (mask & FoldMskICmp_AMask_AllOnes) {
00657     // (icmp eq (A & B), A) & (icmp eq (A & D), A)
00658     // -> (icmp eq (A & (B&D)), A)
00659     Value* newAnd1 = Builder->CreateAnd(B, D);
00660     Value* newAnd = Builder->CreateAnd(A, newAnd1);
00661     return Builder->CreateICmp(NEWCC, newAnd, A);
00662   }
00663   if (mask & FoldMskICmp_BMask_Mixed) {
00664     // (icmp eq (A & B), C) & (icmp eq (A & D), E)
00665     // We already know that B & C == C && D & E == E.
00666     // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
00667     // C and E, which are shared by both the mask B and the mask D, don't
00668     // contradict, then we can transform to
00669     // -> (icmp eq (A & (B|D)), (C|E))
00670     // Currently, we only handle the case of B, C, D, and E being constant.
00671     ConstantInt *BCst = dyn_cast<ConstantInt>(B);
00672     if (BCst == 0) return 0;
00673     ConstantInt *DCst = dyn_cast<ConstantInt>(D);
00674     if (DCst == 0) return 0;
00675     // we can't simply use C and E, because we might actually handle
00676     //   (icmp ne (A & B), B) & (icmp eq (A & D), D)
00677     // with B and D, having a single bit set
00678 
00679     ConstantInt *CCst = dyn_cast<ConstantInt>(C);
00680     if (CCst == 0) return 0;
00681     if (LHSCC != NEWCC)
00682       CCst = dyn_cast<ConstantInt>( ConstantExpr::getXor(BCst, CCst) );
00683     ConstantInt *ECst = dyn_cast<ConstantInt>(E);
00684     if (ECst == 0) return 0;
00685     if (RHSCC != NEWCC)
00686       ECst = dyn_cast<ConstantInt>( ConstantExpr::getXor(DCst, ECst) );
00687     ConstantInt* MCst = dyn_cast<ConstantInt>(
00688       ConstantExpr::getAnd(ConstantExpr::getAnd(BCst, DCst),
00689                            ConstantExpr::getXor(CCst, ECst)) );
00690     // if there is a conflict we should actually return a false for the
00691     // whole construct
00692     if (!MCst->isZero())
00693       return 0;
00694     Value *newOr1 = Builder->CreateOr(B, D);
00695     Value *newOr2 = ConstantExpr::getOr(CCst, ECst);
00696     Value *newAnd = Builder->CreateAnd(A, newOr1);
00697     return Builder->CreateICmp(NEWCC, newAnd, newOr2);
00698   }
00699   return 0;
00700 }
00701 
00702 /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
00703 Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
00704   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
00705 
00706   // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
00707   if (PredicatesFoldable(LHSCC, RHSCC)) {
00708     if (LHS->getOperand(0) == RHS->getOperand(1) &&
00709         LHS->getOperand(1) == RHS->getOperand(0))
00710       LHS->swapOperands();
00711     if (LHS->getOperand(0) == RHS->getOperand(0) &&
00712         LHS->getOperand(1) == RHS->getOperand(1)) {
00713       Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
00714       unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
00715       bool isSigned = LHS->isSigned() || RHS->isSigned();
00716       return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
00717     }
00718   }
00719 
00720   // handle (roughly):  (icmp eq (A & B), C) & (icmp eq (A & D), E)
00721   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, ICmpInst::ICMP_EQ, Builder))
00722     return V;
00723 
00724   // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
00725   Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
00726   ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
00727   ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
00728   if (LHSCst == 0 || RHSCst == 0) return 0;
00729 
00730   if (LHSCst == RHSCst && LHSCC == RHSCC) {
00731     // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
00732     // where C is a power of 2
00733     if (LHSCC == ICmpInst::ICMP_ULT &&
00734         LHSCst->getValue().isPowerOf2()) {
00735       Value *NewOr = Builder->CreateOr(Val, Val2);
00736       return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
00737     }
00738 
00739     // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
00740     if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) {
00741       Value *NewOr = Builder->CreateOr(Val, Val2);
00742       return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
00743     }
00744   }
00745 
00746   // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
00747   // where CMAX is the all ones value for the truncated type,
00748   // iff the lower bits of C2 and CA are zero.
00749   if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
00750       LHS->hasOneUse() && RHS->hasOneUse()) {
00751     Value *V;
00752     ConstantInt *AndCst, *SmallCst = 0, *BigCst = 0;
00753 
00754     // (trunc x) == C1 & (and x, CA) == C2
00755     // (and x, CA) == C2 & (trunc x) == C1
00756     if (match(Val2, m_Trunc(m_Value(V))) &&
00757         match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
00758       SmallCst = RHSCst;
00759       BigCst = LHSCst;
00760     } else if (match(Val, m_Trunc(m_Value(V))) &&
00761                match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
00762       SmallCst = LHSCst;
00763       BigCst = RHSCst;
00764     }
00765 
00766     if (SmallCst && BigCst) {
00767       unsigned BigBitSize = BigCst->getType()->getBitWidth();
00768       unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
00769 
00770       // Check that the low bits are zero.
00771       APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
00772       if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
00773         Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
00774         APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
00775         Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
00776         return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
00777       }
00778     }
00779   }
00780 
00781   // From here on, we only handle:
00782   //    (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
00783   if (Val != Val2) return 0;
00784 
00785   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
00786   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
00787       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
00788       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
00789       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
00790     return 0;
00791 
00792   // Make a constant range that's the intersection of the two icmp ranges.
00793   // If the intersection is empty, we know that the result is false.
00794   ConstantRange LHSRange =
00795     ConstantRange::makeICmpRegion(LHSCC, LHSCst->getValue());
00796   ConstantRange RHSRange =
00797     ConstantRange::makeICmpRegion(RHSCC, RHSCst->getValue());
00798 
00799   if (LHSRange.intersectWith(RHSRange).isEmptySet())
00800     return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
00801 
00802   // We can't fold (ugt x, C) & (sgt x, C2).
00803   if (!PredicatesFoldable(LHSCC, RHSCC))
00804     return 0;
00805 
00806   // Ensure that the larger constant is on the RHS.
00807   bool ShouldSwap;
00808   if (CmpInst::isSigned(LHSCC) ||
00809       (ICmpInst::isEquality(LHSCC) &&
00810        CmpInst::isSigned(RHSCC)))
00811     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
00812   else
00813     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
00814 
00815   if (ShouldSwap) {
00816     std::swap(LHS, RHS);
00817     std::swap(LHSCst, RHSCst);
00818     std::swap(LHSCC, RHSCC);
00819   }
00820 
00821   // At this point, we know we have two icmp instructions
00822   // comparing a value against two constants and and'ing the result
00823   // together.  Because of the above check, we know that we only have
00824   // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
00825   // (from the icmp folding check above), that the two constants
00826   // are not equal and that the larger constant is on the RHS
00827   assert(LHSCst != RHSCst && "Compares not folded above?");
00828 
00829   switch (LHSCC) {
00830   default: llvm_unreachable("Unknown integer condition code!");
00831   case ICmpInst::ICMP_EQ:
00832     switch (RHSCC) {
00833     default: llvm_unreachable("Unknown integer condition code!");
00834     case ICmpInst::ICMP_NE:         // (X == 13 & X != 15) -> X == 13
00835     case ICmpInst::ICMP_ULT:        // (X == 13 & X <  15) -> X == 13
00836     case ICmpInst::ICMP_SLT:        // (X == 13 & X <  15) -> X == 13
00837       return LHS;
00838     }
00839   case ICmpInst::ICMP_NE:
00840     switch (RHSCC) {
00841     default: llvm_unreachable("Unknown integer condition code!");
00842     case ICmpInst::ICMP_ULT:
00843       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
00844         return Builder->CreateICmpULT(Val, LHSCst);
00845       break;                        // (X != 13 & X u< 15) -> no change
00846     case ICmpInst::ICMP_SLT:
00847       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
00848         return Builder->CreateICmpSLT(Val, LHSCst);
00849       break;                        // (X != 13 & X s< 15) -> no change
00850     case ICmpInst::ICMP_EQ:         // (X != 13 & X == 15) -> X == 15
00851     case ICmpInst::ICMP_UGT:        // (X != 13 & X u> 15) -> X u> 15
00852     case ICmpInst::ICMP_SGT:        // (X != 13 & X s> 15) -> X s> 15
00853       return RHS;
00854     case ICmpInst::ICMP_NE:
00855       if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
00856         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
00857         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
00858         return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1));
00859       }
00860       break;                        // (X != 13 & X != 15) -> no change
00861     }
00862     break;
00863   case ICmpInst::ICMP_ULT:
00864     switch (RHSCC) {
00865     default: llvm_unreachable("Unknown integer condition code!");
00866     case ICmpInst::ICMP_EQ:         // (X u< 13 & X == 15) -> false
00867     case ICmpInst::ICMP_UGT:        // (X u< 13 & X u> 15) -> false
00868       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
00869     case ICmpInst::ICMP_SGT:        // (X u< 13 & X s> 15) -> no change
00870       break;
00871     case ICmpInst::ICMP_NE:         // (X u< 13 & X != 15) -> X u< 13
00872     case ICmpInst::ICMP_ULT:        // (X u< 13 & X u< 15) -> X u< 13
00873       return LHS;
00874     case ICmpInst::ICMP_SLT:        // (X u< 13 & X s< 15) -> no change
00875       break;
00876     }
00877     break;
00878   case ICmpInst::ICMP_SLT:
00879     switch (RHSCC) {
00880     default: llvm_unreachable("Unknown integer condition code!");
00881     case ICmpInst::ICMP_UGT:        // (X s< 13 & X u> 15) -> no change
00882       break;
00883     case ICmpInst::ICMP_NE:         // (X s< 13 & X != 15) -> X < 13
00884     case ICmpInst::ICMP_SLT:        // (X s< 13 & X s< 15) -> X < 13
00885       return LHS;
00886     case ICmpInst::ICMP_ULT:        // (X s< 13 & X u< 15) -> no change
00887       break;
00888     }
00889     break;
00890   case ICmpInst::ICMP_UGT:
00891     switch (RHSCC) {
00892     default: llvm_unreachable("Unknown integer condition code!");
00893     case ICmpInst::ICMP_EQ:         // (X u> 13 & X == 15) -> X == 15
00894     case ICmpInst::ICMP_UGT:        // (X u> 13 & X u> 15) -> X u> 15
00895       return RHS;
00896     case ICmpInst::ICMP_SGT:        // (X u> 13 & X s> 15) -> no change
00897       break;
00898     case ICmpInst::ICMP_NE:
00899       if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
00900         return Builder->CreateICmp(LHSCC, Val, RHSCst);
00901       break;                        // (X u> 13 & X != 15) -> no change
00902     case ICmpInst::ICMP_ULT:        // (X u> 13 & X u< 15) -> (X-14) <u 1
00903       return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true);
00904     case ICmpInst::ICMP_SLT:        // (X u> 13 & X s< 15) -> no change
00905       break;
00906     }
00907     break;
00908   case ICmpInst::ICMP_SGT:
00909     switch (RHSCC) {
00910     default: llvm_unreachable("Unknown integer condition code!");
00911     case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X == 15
00912     case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15
00913       return RHS;
00914     case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change
00915       break;
00916     case ICmpInst::ICMP_NE:
00917       if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
00918         return Builder->CreateICmp(LHSCC, Val, RHSCst);
00919       break;                        // (X s> 13 & X != 15) -> no change
00920     case ICmpInst::ICMP_SLT:        // (X s> 13 & X s< 15) -> (X-14) s< 1
00921       return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true);
00922     case ICmpInst::ICMP_ULT:        // (X s> 13 & X u< 15) -> no change
00923       break;
00924     }
00925     break;
00926   }
00927 
00928   return 0;
00929 }
00930 
00931 /// FoldAndOfFCmps - Optimize (fcmp)&(fcmp).  NOTE: Unlike the rest of
00932 /// instcombine, this returns a Value which should already be inserted into the
00933 /// function.
00934 Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
00935   if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
00936       RHS->getPredicate() == FCmpInst::FCMP_ORD) {
00937     if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
00938       return 0;
00939 
00940     // (fcmp ord x, c) & (fcmp ord y, c)  -> (fcmp ord x, y)
00941     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
00942       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
00943         // If either of the constants are nans, then the whole thing returns
00944         // false.
00945         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
00946           return ConstantInt::getFalse(LHS->getContext());
00947         return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
00948       }
00949 
00950     // Handle vector zeros.  This occurs because the canonical form of
00951     // "fcmp ord x,x" is "fcmp ord x, 0".
00952     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
00953         isa<ConstantAggregateZero>(RHS->getOperand(1)))
00954       return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
00955     return 0;
00956   }
00957 
00958   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
00959   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
00960   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
00961 
00962 
00963   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
00964     // Swap RHS operands to match LHS.
00965     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
00966     std::swap(Op1LHS, Op1RHS);
00967   }
00968 
00969   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
00970     // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
00971     if (Op0CC == Op1CC)
00972       return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
00973     if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
00974       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
00975     if (Op0CC == FCmpInst::FCMP_TRUE)
00976       return RHS;
00977     if (Op1CC == FCmpInst::FCMP_TRUE)
00978       return LHS;
00979 
00980     bool Op0Ordered;
00981     bool Op1Ordered;
00982     unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
00983     unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
00984     // uno && ord -> false
00985     if (Op0Pred == 0 && Op1Pred == 0 && Op0Ordered != Op1Ordered)
00986         return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
00987     if (Op1Pred == 0) {
00988       std::swap(LHS, RHS);
00989       std::swap(Op0Pred, Op1Pred);
00990       std::swap(Op0Ordered, Op1Ordered);
00991     }
00992     if (Op0Pred == 0) {
00993       // uno && ueq -> uno && (uno || eq) -> uno
00994       // ord && olt -> ord && (ord && lt) -> olt
00995       if (!Op0Ordered && (Op0Ordered == Op1Ordered))
00996         return LHS;
00997       if (Op0Ordered && (Op0Ordered == Op1Ordered))
00998         return RHS;
00999 
01000       // uno && oeq -> uno && (ord && eq) -> false
01001       if (!Op0Ordered)
01002         return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
01003       // ord && ueq -> ord && (uno || eq) -> oeq
01004       return getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS, Builder);
01005     }
01006   }
01007 
01008   return 0;
01009 }
01010 
01011 
01012 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
01013   bool Changed = SimplifyAssociativeOrCommutative(I);
01014   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01015 
01016   if (Value *V = SimplifyAndInst(Op0, Op1, TD))
01017     return ReplaceInstUsesWith(I, V);
01018 
01019   // (A|B)&(A|C) -> A|(B&C) etc
01020   if (Value *V = SimplifyUsingDistributiveLaws(I))
01021     return ReplaceInstUsesWith(I, V);
01022 
01023   // See if we can simplify any instructions used by the instruction whose sole
01024   // purpose is to compute bits we don't care about.
01025   if (SimplifyDemandedInstructionBits(I))
01026     return &I;
01027 
01028   if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
01029     const APInt &AndRHSMask = AndRHS->getValue();
01030 
01031     // Optimize a variety of ((val OP C1) & C2) combinations...
01032     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
01033       Value *Op0LHS = Op0I->getOperand(0);
01034       Value *Op0RHS = Op0I->getOperand(1);
01035       switch (Op0I->getOpcode()) {
01036       default: break;
01037       case Instruction::Xor:
01038       case Instruction::Or: {
01039         // If the mask is only needed on one incoming arm, push it up.
01040         if (!Op0I->hasOneUse()) break;
01041 
01042         APInt NotAndRHS(~AndRHSMask);
01043         if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
01044           // Not masking anything out for the LHS, move to RHS.
01045           Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
01046                                              Op0RHS->getName()+".masked");
01047           return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
01048         }
01049         if (!isa<Constant>(Op0RHS) &&
01050             MaskedValueIsZero(Op0RHS, NotAndRHS)) {
01051           // Not masking anything out for the RHS, move to LHS.
01052           Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
01053                                              Op0LHS->getName()+".masked");
01054           return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
01055         }
01056 
01057         break;
01058       }
01059       case Instruction::Add:
01060         // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
01061         // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
01062         // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
01063         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
01064           return BinaryOperator::CreateAnd(V, AndRHS);
01065         if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
01066           return BinaryOperator::CreateAnd(V, AndRHS);  // Add commutes
01067         break;
01068 
01069       case Instruction::Sub:
01070         // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
01071         // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
01072         // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
01073         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
01074           return BinaryOperator::CreateAnd(V, AndRHS);
01075 
01076         // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
01077         // has 1's for all bits that the subtraction with A might affect.
01078         if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {
01079           uint32_t BitWidth = AndRHSMask.getBitWidth();
01080           uint32_t Zeros = AndRHSMask.countLeadingZeros();
01081           APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
01082 
01083           if (MaskedValueIsZero(Op0LHS, Mask)) {
01084             Value *NewNeg = Builder->CreateNeg(Op0RHS);
01085             return BinaryOperator::CreateAnd(NewNeg, AndRHS);
01086           }
01087         }
01088         break;
01089 
01090       case Instruction::Shl:
01091       case Instruction::LShr:
01092         // (1 << x) & 1 --> zext(x == 0)
01093         // (1 >> x) & 1 --> zext(x == 0)
01094         if (AndRHSMask == 1 && Op0LHS == AndRHS) {
01095           Value *NewICmp =
01096             Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
01097           return new ZExtInst(NewICmp, I.getType());
01098         }
01099         break;
01100       }
01101 
01102       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
01103         if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
01104           return Res;
01105     }
01106 
01107     // If this is an integer truncation, and if the source is an 'and' with
01108     // immediate, transform it.  This frequently occurs for bitfield accesses.
01109     {
01110       Value *X = 0; ConstantInt *YC = 0;
01111       if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
01112         // Change: and (trunc (and X, YC) to T), C2
01113         // into  : and (trunc X to T), trunc(YC) & C2
01114         // This will fold the two constants together, which may allow
01115         // other simplifications.
01116         Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
01117         Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
01118         C3 = ConstantExpr::getAnd(C3, AndRHS);
01119         return BinaryOperator::CreateAnd(NewCast, C3);
01120       }
01121     }
01122 
01123     // Try to fold constant and into select arguments.
01124     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
01125       if (Instruction *R = FoldOpIntoSelect(I, SI))
01126         return R;
01127     if (isa<PHINode>(Op0))
01128       if (Instruction *NV = FoldOpIntoPhi(I))
01129         return NV;
01130   }
01131 
01132 
01133   // (~A & ~B) == (~(A | B)) - De Morgan's Law
01134   if (Value *Op0NotVal = dyn_castNotVal(Op0))
01135     if (Value *Op1NotVal = dyn_castNotVal(Op1))
01136       if (Op0->hasOneUse() && Op1->hasOneUse()) {
01137         Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
01138                                       I.getName()+".demorgan");
01139         return BinaryOperator::CreateNot(Or);
01140       }
01141 
01142   {
01143     Value *A = 0, *B = 0, *C = 0, *D = 0;
01144     // (A|B) & ~(A&B) -> A^B
01145     if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
01146         match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
01147         ((A == C && B == D) || (A == D && B == C)))
01148       return BinaryOperator::CreateXor(A, B);
01149 
01150     // ~(A&B) & (A|B) -> A^B
01151     if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
01152         match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
01153         ((A == C && B == D) || (A == D && B == C)))
01154       return BinaryOperator::CreateXor(A, B);
01155 
01156     // A&(A^B) => A & ~B
01157     {
01158       Value *tmpOp0 = Op0;
01159       Value *tmpOp1 = Op1;
01160       if (Op0->hasOneUse() &&
01161           match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
01162         if (A == Op1 || B == Op1 ) {
01163           tmpOp1 = Op0;
01164           tmpOp0 = Op1;
01165           // Simplify below
01166         }
01167       }
01168 
01169       if (tmpOp1->hasOneUse() &&
01170           match(tmpOp1, m_Xor(m_Value(A), m_Value(B)))) {
01171         if (B == tmpOp0) {
01172           std::swap(A, B);
01173         }
01174         // Notice that the patten (A&(~B)) is actually (A&(-1^B)), so if
01175         // A is originally -1 (or a vector of -1 and undefs), then we enter
01176         // an endless loop. By checking that A is non-constant we ensure that
01177         // we will never get to the loop.
01178         if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
01179           return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
01180       }
01181     }
01182 
01183     // (A&((~A)|B)) -> A&B
01184     if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
01185         match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
01186       return BinaryOperator::CreateAnd(A, Op1);
01187     if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
01188         match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
01189       return BinaryOperator::CreateAnd(A, Op0);
01190   }
01191 
01192   if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1))
01193     if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
01194       if (Value *Res = FoldAndOfICmps(LHS, RHS))
01195         return ReplaceInstUsesWith(I, Res);
01196 
01197   // If and'ing two fcmp, try combine them into one.
01198   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
01199     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
01200       if (Value *Res = FoldAndOfFCmps(LHS, RHS))
01201         return ReplaceInstUsesWith(I, Res);
01202 
01203 
01204   // fold (and (cast A), (cast B)) -> (cast (and A, B))
01205   if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
01206     if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
01207       Type *SrcTy = Op0C->getOperand(0)->getType();
01208       if (Op0C->getOpcode() == Op1C->getOpcode() && // same cast kind ?
01209           SrcTy == Op1C->getOperand(0)->getType() &&
01210           SrcTy->isIntOrIntVectorTy()) {
01211         Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
01212 
01213         // Only do this if the casts both really cause code to be generated.
01214         if (ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
01215             ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
01216           Value *NewOp = Builder->CreateAnd(Op0COp, Op1COp, I.getName());
01217           return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
01218         }
01219 
01220         // If this is and(cast(icmp), cast(icmp)), try to fold this even if the
01221         // cast is otherwise not optimizable.  This happens for vector sexts.
01222         if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
01223           if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
01224             if (Value *Res = FoldAndOfICmps(LHS, RHS))
01225               return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
01226 
01227         // If this is and(cast(fcmp), cast(fcmp)), try to fold this even if the
01228         // cast is otherwise not optimizable.  This happens for vector sexts.
01229         if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
01230           if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
01231             if (Value *Res = FoldAndOfFCmps(LHS, RHS))
01232               return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
01233       }
01234     }
01235 
01236   // (X >> Z) & (Y >> Z)  -> (X&Y) >> Z  for all shifts.
01237   if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
01238     if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
01239       if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
01240           SI0->getOperand(1) == SI1->getOperand(1) &&
01241           (SI0->hasOneUse() || SI1->hasOneUse())) {
01242         Value *NewOp =
01243           Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
01244                              SI0->getName());
01245         return BinaryOperator::Create(SI1->getOpcode(), NewOp,
01246                                       SI1->getOperand(1));
01247       }
01248   }
01249 
01250   {
01251     Value *X = 0;
01252     bool OpsSwapped = false;
01253     // Canonicalize SExt or Not to the LHS
01254     if (match(Op1, m_SExt(m_Value())) ||
01255         match(Op1, m_Not(m_Value()))) {
01256       std::swap(Op0, Op1);
01257       OpsSwapped = true;
01258     }
01259 
01260     // Fold (and (sext bool to A), B) --> (select bool, B, 0)
01261     if (match(Op0, m_SExt(m_Value(X))) &&
01262         X->getType()->getScalarType()->isIntegerTy(1)) {
01263       Value *Zero = Constant::getNullValue(Op1->getType());
01264       return SelectInst::Create(X, Op1, Zero);
01265     }
01266 
01267     // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
01268     if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
01269         X->getType()->getScalarType()->isIntegerTy(1)) {
01270       Value *Zero = Constant::getNullValue(Op0->getType());
01271       return SelectInst::Create(X, Zero, Op1);
01272     }
01273 
01274     if (OpsSwapped)
01275       std::swap(Op0, Op1);
01276   }
01277 
01278   return Changed ? &I : 0;
01279 }
01280 
01281 /// CollectBSwapParts - Analyze the specified subexpression and see if it is
01282 /// capable of providing pieces of a bswap.  The subexpression provides pieces
01283 /// of a bswap if it is proven that each of the non-zero bytes in the output of
01284 /// the expression came from the corresponding "byte swapped" byte in some other
01285 /// value.  For example, if the current subexpression is "(shl i32 %X, 24)" then
01286 /// we know that the expression deposits the low byte of %X into the high byte
01287 /// of the bswap result and that all other bytes are zero.  This expression is
01288 /// accepted, the high byte of ByteValues is set to X to indicate a correct
01289 /// match.
01290 ///
01291 /// This function returns true if the match was unsuccessful and false if so.
01292 /// On entry to the function the "OverallLeftShift" is a signed integer value
01293 /// indicating the number of bytes that the subexpression is later shifted.  For
01294 /// example, if the expression is later right shifted by 16 bits, the
01295 /// OverallLeftShift value would be -2 on entry.  This is used to specify which
01296 /// byte of ByteValues is actually being set.
01297 ///
01298 /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
01299 /// byte is masked to zero by a user.  For example, in (X & 255), X will be
01300 /// processed with a bytemask of 1.  Because bytemask is 32-bits, this limits
01301 /// this function to working on up to 32-byte (256 bit) values.  ByteMask is
01302 /// always in the local (OverallLeftShift) coordinate space.
01303 ///
01304 static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
01305                               SmallVector<Value*, 8> &ByteValues) {
01306   if (Instruction *I = dyn_cast<Instruction>(V)) {
01307     // If this is an or instruction, it may be an inner node of the bswap.
01308     if (I->getOpcode() == Instruction::Or) {
01309       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
01310                                ByteValues) ||
01311              CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
01312                                ByteValues);
01313     }
01314 
01315     // If this is a logical shift by a constant multiple of 8, recurse with
01316     // OverallLeftShift and ByteMask adjusted.
01317     if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
01318       unsigned ShAmt =
01319         cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
01320       // Ensure the shift amount is defined and of a byte value.
01321       if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
01322         return true;
01323 
01324       unsigned ByteShift = ShAmt >> 3;
01325       if (I->getOpcode() == Instruction::Shl) {
01326         // X << 2 -> collect(X, +2)
01327         OverallLeftShift += ByteShift;
01328         ByteMask >>= ByteShift;
01329       } else {
01330         // X >>u 2 -> collect(X, -2)
01331         OverallLeftShift -= ByteShift;
01332         ByteMask <<= ByteShift;
01333         ByteMask &= (~0U >> (32-ByteValues.size()));
01334       }
01335 
01336       if (OverallLeftShift >= (int)ByteValues.size()) return true;
01337       if (OverallLeftShift <= -(int)ByteValues.size()) return true;
01338 
01339       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
01340                                ByteValues);
01341     }
01342 
01343     // If this is a logical 'and' with a mask that clears bytes, clear the
01344     // corresponding bytes in ByteMask.
01345     if (I->getOpcode() == Instruction::And &&
01346         isa<ConstantInt>(I->getOperand(1))) {
01347       // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
01348       unsigned NumBytes = ByteValues.size();
01349       APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
01350       const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
01351 
01352       for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
01353         // If this byte is masked out by a later operation, we don't care what
01354         // the and mask is.
01355         if ((ByteMask & (1 << i)) == 0)
01356           continue;
01357 
01358         // If the AndMask is all zeros for this byte, clear the bit.
01359         APInt MaskB = AndMask & Byte;
01360         if (MaskB == 0) {
01361           ByteMask &= ~(1U << i);
01362           continue;
01363         }
01364 
01365         // If the AndMask is not all ones for this byte, it's not a bytezap.
01366         if (MaskB != Byte)
01367           return true;
01368 
01369         // Otherwise, this byte is kept.
01370       }
01371 
01372       return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
01373                                ByteValues);
01374     }
01375   }
01376 
01377   // Okay, we got to something that isn't a shift, 'or' or 'and'.  This must be
01378   // the input value to the bswap.  Some observations: 1) if more than one byte
01379   // is demanded from this input, then it could not be successfully assembled
01380   // into a byteswap.  At least one of the two bytes would not be aligned with
01381   // their ultimate destination.
01382   if (!isPowerOf2_32(ByteMask)) return true;
01383   unsigned InputByteNo = CountTrailingZeros_32(ByteMask);
01384 
01385   // 2) The input and ultimate destinations must line up: if byte 3 of an i32
01386   // is demanded, it needs to go into byte 0 of the result.  This means that the
01387   // byte needs to be shifted until it lands in the right byte bucket.  The
01388   // shift amount depends on the position: if the byte is coming from the high
01389   // part of the value (e.g. byte 3) then it must be shifted right.  If from the
01390   // low part, it must be shifted left.
01391   unsigned DestByteNo = InputByteNo + OverallLeftShift;
01392   if (ByteValues.size()-1-DestByteNo != InputByteNo)
01393     return true;
01394 
01395   // If the destination byte value is already defined, the values are or'd
01396   // together, which isn't a bswap (unless it's an or of the same bits).
01397   if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
01398     return true;
01399   ByteValues[DestByteNo] = V;
01400   return false;
01401 }
01402 
01403 /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
01404 /// If so, insert the new bswap intrinsic and return it.
01405 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
01406   IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
01407   if (!ITy || ITy->getBitWidth() % 16 ||
01408       // ByteMask only allows up to 32-byte values.
01409       ITy->getBitWidth() > 32*8)
01410     return 0;   // Can only bswap pairs of bytes.  Can't do vectors.
01411 
01412   /// ByteValues - For each byte of the result, we keep track of which value
01413   /// defines each byte.
01414   SmallVector<Value*, 8> ByteValues;
01415   ByteValues.resize(ITy->getBitWidth()/8);
01416 
01417   // Try to find all the pieces corresponding to the bswap.
01418   uint32_t ByteMask = ~0U >> (32-ByteValues.size());
01419   if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
01420     return 0;
01421 
01422   // Check to see if all of the bytes come from the same value.
01423   Value *V = ByteValues[0];
01424   if (V == 0) return 0;  // Didn't find a byte?  Must be zero.
01425 
01426   // Check to make sure that all of the bytes come from the same value.
01427   for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
01428     if (ByteValues[i] != V)
01429       return 0;
01430   Module *M = I.getParent()->getParent()->getParent();
01431   Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, ITy);
01432   return CallInst::Create(F, V);
01433 }
01434 
01435 /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D).  Check
01436 /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
01437 /// we can simplify this expression to "cond ? C : D or B".
01438 static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
01439                                          Value *C, Value *D) {
01440   // If A is not a select of -1/0, this cannot match.
01441   Value *Cond = 0;
01442   if (!match(A, m_SExt(m_Value(Cond))) ||
01443       !Cond->getType()->isIntegerTy(1))
01444     return 0;
01445 
01446   // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
01447   if (match(D, m_Not(m_SExt(m_Specific(Cond)))))
01448     return SelectInst::Create(Cond, C, B);
01449   if (match(D, m_SExt(m_Not(m_Specific(Cond)))))
01450     return SelectInst::Create(Cond, C, B);
01451 
01452   // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
01453   if (match(B, m_Not(m_SExt(m_Specific(Cond)))))
01454     return SelectInst::Create(Cond, C, D);
01455   if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
01456     return SelectInst::Create(Cond, C, D);
01457   return 0;
01458 }
01459 
01460 /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
01461 Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
01462   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
01463 
01464   // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
01465   if (PredicatesFoldable(LHSCC, RHSCC)) {
01466     if (LHS->getOperand(0) == RHS->getOperand(1) &&
01467         LHS->getOperand(1) == RHS->getOperand(0))
01468       LHS->swapOperands();
01469     if (LHS->getOperand(0) == RHS->getOperand(0) &&
01470         LHS->getOperand(1) == RHS->getOperand(1)) {
01471       Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
01472       unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
01473       bool isSigned = LHS->isSigned() || RHS->isSigned();
01474       return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
01475     }
01476   }
01477 
01478   // handle (roughly):
01479   // (icmp ne (A & B), C) | (icmp ne (A & D), E)
01480   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, ICmpInst::ICMP_NE, Builder))
01481     return V;
01482 
01483   // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
01484   Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
01485   ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
01486   ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
01487   if (LHSCst == 0 || RHSCst == 0) return 0;
01488 
01489   if (LHSCst == RHSCst && LHSCC == RHSCC) {
01490     // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
01491     if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
01492       Value *NewOr = Builder->CreateOr(Val, Val2);
01493       return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
01494     }
01495   }
01496 
01497   // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
01498   //   iff C2 + CA == C1.
01499   if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
01500     ConstantInt *AddCst;
01501     if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
01502       if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
01503         return Builder->CreateICmpULE(Val, LHSCst);
01504   }
01505 
01506   // From here on, we only handle:
01507   //    (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
01508   if (Val != Val2) return 0;
01509 
01510   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
01511   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
01512       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
01513       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
01514       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
01515     return 0;
01516 
01517   // We can't fold (ugt x, C) | (sgt x, C2).
01518   if (!PredicatesFoldable(LHSCC, RHSCC))
01519     return 0;
01520 
01521   // Ensure that the larger constant is on the RHS.
01522   bool ShouldSwap;
01523   if (CmpInst::isSigned(LHSCC) ||
01524       (ICmpInst::isEquality(LHSCC) &&
01525        CmpInst::isSigned(RHSCC)))
01526     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
01527   else
01528     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
01529 
01530   if (ShouldSwap) {
01531     std::swap(LHS, RHS);
01532     std::swap(LHSCst, RHSCst);
01533     std::swap(LHSCC, RHSCC);
01534   }
01535 
01536   // At this point, we know we have two icmp instructions
01537   // comparing a value against two constants and or'ing the result
01538   // together.  Because of the above check, we know that we only have
01539   // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
01540   // icmp folding check above), that the two constants are not
01541   // equal.
01542   assert(LHSCst != RHSCst && "Compares not folded above?");
01543 
01544   switch (LHSCC) {
01545   default: llvm_unreachable("Unknown integer condition code!");
01546   case ICmpInst::ICMP_EQ:
01547     switch (RHSCC) {
01548     default: llvm_unreachable("Unknown integer condition code!");
01549     case ICmpInst::ICMP_EQ:
01550       if (LHS->getOperand(0) == RHS->getOperand(0)) {
01551         // if LHSCst and RHSCst differ only by one bit:
01552         // (A == C1 || A == C2) -> (A & ~(C1 ^ C2)) == C1
01553         assert(LHSCst->getValue().ule(LHSCst->getValue()));
01554 
01555         APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
01556         if (Xor.isPowerOf2()) {
01557           Value *NegCst = Builder->getInt(~Xor);
01558           Value *And = Builder->CreateAnd(LHS->getOperand(0), NegCst);
01559           return Builder->CreateICmp(ICmpInst::ICMP_EQ, And, LHSCst);
01560         }
01561       }
01562 
01563       if (LHSCst == SubOne(RHSCst)) {
01564         // (X == 13 | X == 14) -> X-13 <u 2
01565         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
01566         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
01567         AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
01568         return Builder->CreateICmpULT(Add, AddCST);
01569       }
01570 
01571       break;                         // (X == 13 | X == 15) -> no change
01572     case ICmpInst::ICMP_UGT:         // (X == 13 | X u> 14) -> no change
01573     case ICmpInst::ICMP_SGT:         // (X == 13 | X s> 14) -> no change
01574       break;
01575     case ICmpInst::ICMP_NE:          // (X == 13 | X != 15) -> X != 15
01576     case ICmpInst::ICMP_ULT:         // (X == 13 | X u< 15) -> X u< 15
01577     case ICmpInst::ICMP_SLT:         // (X == 13 | X s< 15) -> X s< 15
01578       return RHS;
01579     }
01580     break;
01581   case ICmpInst::ICMP_NE:
01582     switch (RHSCC) {
01583     default: llvm_unreachable("Unknown integer condition code!");
01584     case ICmpInst::ICMP_EQ:          // (X != 13 | X == 15) -> X != 13
01585     case ICmpInst::ICMP_UGT:         // (X != 13 | X u> 15) -> X != 13
01586     case ICmpInst::ICMP_SGT:         // (X != 13 | X s> 15) -> X != 13
01587       return LHS;
01588     case ICmpInst::ICMP_NE:          // (X != 13 | X != 15) -> true
01589     case ICmpInst::ICMP_ULT:         // (X != 13 | X u< 15) -> true
01590     case ICmpInst::ICMP_SLT:         // (X != 13 | X s< 15) -> true
01591       return ConstantInt::getTrue(LHS->getContext());
01592     }
01593   case ICmpInst::ICMP_ULT:
01594     switch (RHSCC) {
01595     default: llvm_unreachable("Unknown integer condition code!");
01596     case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change
01597       break;
01598     case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) -> (X-13) u> 2
01599       // If RHSCst is [us]MAXINT, it is always false.  Not handling
01600       // this can cause overflow.
01601       if (RHSCst->isMaxValue(false))
01602         return LHS;
01603       return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false);
01604     case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change
01605       break;
01606     case ICmpInst::ICMP_NE:         // (X u< 13 | X != 15) -> X != 15
01607     case ICmpInst::ICMP_ULT:        // (X u< 13 | X u< 15) -> X u< 15
01608       return RHS;
01609     case ICmpInst::ICMP_SLT:        // (X u< 13 | X s< 15) -> no change
01610       break;
01611     }
01612     break;
01613   case ICmpInst::ICMP_SLT:
01614     switch (RHSCC) {
01615     default: llvm_unreachable("Unknown integer condition code!");
01616     case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change
01617       break;
01618     case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) -> (X-13) s> 2
01619       // If RHSCst is [us]MAXINT, it is always false.  Not handling
01620       // this can cause overflow.
01621       if (RHSCst->isMaxValue(true))
01622         return LHS;
01623       return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false);
01624     case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change
01625       break;
01626     case ICmpInst::ICMP_NE:         // (X s< 13 | X != 15) -> X != 15
01627     case ICmpInst::ICMP_SLT:        // (X s< 13 | X s< 15) -> X s< 15
01628       return RHS;
01629     case ICmpInst::ICMP_ULT:        // (X s< 13 | X u< 15) -> no change
01630       break;
01631     }
01632     break;
01633   case ICmpInst::ICMP_UGT:
01634     switch (RHSCC) {
01635     default: llvm_unreachable("Unknown integer condition code!");
01636     case ICmpInst::ICMP_EQ:         // (X u> 13 | X == 15) -> X u> 13
01637     case ICmpInst::ICMP_UGT:        // (X u> 13 | X u> 15) -> X u> 13
01638       return LHS;
01639     case ICmpInst::ICMP_SGT:        // (X u> 13 | X s> 15) -> no change
01640       break;
01641     case ICmpInst::ICMP_NE:         // (X u> 13 | X != 15) -> true
01642     case ICmpInst::ICMP_ULT:        // (X u> 13 | X u< 15) -> true
01643       return ConstantInt::getTrue(LHS->getContext());
01644     case ICmpInst::ICMP_SLT:        // (X u> 13 | X s< 15) -> no change
01645       break;
01646     }
01647     break;
01648   case ICmpInst::ICMP_SGT:
01649     switch (RHSCC) {
01650     default: llvm_unreachable("Unknown integer condition code!");
01651     case ICmpInst::ICMP_EQ:         // (X s> 13 | X == 15) -> X > 13
01652     case ICmpInst::ICMP_SGT:        // (X s> 13 | X s> 15) -> X > 13
01653       return LHS;
01654     case ICmpInst::ICMP_UGT:        // (X s> 13 | X u> 15) -> no change
01655       break;
01656     case ICmpInst::ICMP_NE:         // (X s> 13 | X != 15) -> true
01657     case ICmpInst::ICMP_SLT:        // (X s> 13 | X s< 15) -> true
01658       return ConstantInt::getTrue(LHS->getContext());
01659     case ICmpInst::ICMP_ULT:        // (X s> 13 | X u< 15) -> no change
01660       break;
01661     }
01662     break;
01663   }
01664   return 0;
01665 }
01666 
01667 /// FoldOrOfFCmps - Optimize (fcmp)|(fcmp).  NOTE: Unlike the rest of
01668 /// instcombine, this returns a Value which should already be inserted into the
01669 /// function.
01670 Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
01671   if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
01672       RHS->getPredicate() == FCmpInst::FCMP_UNO &&
01673       LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
01674     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
01675       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
01676         // If either of the constants are nans, then the whole thing returns
01677         // true.
01678         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
01679           return ConstantInt::getTrue(LHS->getContext());
01680 
01681         // Otherwise, no need to compare the two constants, compare the
01682         // rest.
01683         return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
01684       }
01685 
01686     // Handle vector zeros.  This occurs because the canonical form of
01687     // "fcmp uno x,x" is "fcmp uno x, 0".
01688     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
01689         isa<ConstantAggregateZero>(RHS->getOperand(1)))
01690       return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
01691 
01692     return 0;
01693   }
01694 
01695   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
01696   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
01697   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
01698 
01699   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
01700     // Swap RHS operands to match LHS.
01701     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
01702     std::swap(Op1LHS, Op1RHS);
01703   }
01704   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
01705     // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
01706     if (Op0CC == Op1CC)
01707       return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
01708     if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
01709       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
01710     if (Op0CC == FCmpInst::FCMP_FALSE)
01711       return RHS;
01712     if (Op1CC == FCmpInst::FCMP_FALSE)
01713       return LHS;
01714     bool Op0Ordered;
01715     bool Op1Ordered;
01716     unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
01717     unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
01718     if (Op0Ordered == Op1Ordered) {
01719       // If both are ordered or unordered, return a new fcmp with
01720       // or'ed predicates.
01721       return getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS, Builder);
01722     }
01723   }
01724   return 0;
01725 }
01726 
01727 /// FoldOrWithConstants - This helper function folds:
01728 ///
01729 ///     ((A | B) & C1) | (B & C2)
01730 ///
01731 /// into:
01732 ///
01733 ///     (A & C1) | B
01734 ///
01735 /// when the XOR of the two constants is "all ones" (-1).
01736 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
01737                                                Value *A, Value *B, Value *C) {
01738   ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
01739   if (!CI1) return 0;
01740 
01741   Value *V1 = 0;
01742   ConstantInt *CI2 = 0;
01743   if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
01744 
01745   APInt Xor = CI1->getValue() ^ CI2->getValue();
01746   if (!Xor.isAllOnesValue()) return 0;
01747 
01748   if (V1 == A || V1 == B) {
01749     Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
01750     return BinaryOperator::CreateOr(NewOp, V1);
01751   }
01752 
01753   return 0;
01754 }
01755 
01756 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
01757   bool Changed = SimplifyAssociativeOrCommutative(I);
01758   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01759 
01760   if (Value *V = SimplifyOrInst(Op0, Op1, TD))
01761     return ReplaceInstUsesWith(I, V);
01762 
01763   // (A&B)|(A&C) -> A&(B|C) etc
01764   if (Value *V = SimplifyUsingDistributiveLaws(I))
01765     return ReplaceInstUsesWith(I, V);
01766 
01767   // See if we can simplify any instructions used by the instruction whose sole
01768   // purpose is to compute bits we don't care about.
01769   if (SimplifyDemandedInstructionBits(I))
01770     return &I;
01771 
01772   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
01773     ConstantInt *C1 = 0; Value *X = 0;
01774     // (X & C1) | C2 --> (X | C2) & (C1|C2)
01775     // iff (C1 & C2) == 0.
01776     if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
01777         (RHS->getValue() & C1->getValue()) != 0 &&
01778         Op0->hasOneUse()) {
01779       Value *Or = Builder->CreateOr(X, RHS);
01780       Or->takeName(Op0);
01781       return BinaryOperator::CreateAnd(Or,
01782                          ConstantInt::get(I.getContext(),
01783                                           RHS->getValue() | C1->getValue()));
01784     }
01785 
01786     // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
01787     if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
01788         Op0->hasOneUse()) {
01789       Value *Or = Builder->CreateOr(X, RHS);
01790       Or->takeName(Op0);
01791       return BinaryOperator::CreateXor(Or,
01792                  ConstantInt::get(I.getContext(),
01793                                   C1->getValue() & ~RHS->getValue()));
01794     }
01795 
01796     // Try to fold constant and into select arguments.
01797     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
01798       if (Instruction *R = FoldOpIntoSelect(I, SI))
01799         return R;
01800 
01801     if (isa<PHINode>(Op0))
01802       if (Instruction *NV = FoldOpIntoPhi(I))
01803         return NV;
01804   }
01805 
01806   Value *A = 0, *B = 0;
01807   ConstantInt *C1 = 0, *C2 = 0;
01808 
01809   // (A | B) | C  and  A | (B | C)                  -> bswap if possible.
01810   // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible.
01811   if (match(Op0, m_Or(m_Value(), m_Value())) ||
01812       match(Op1, m_Or(m_Value(), m_Value())) ||
01813       (match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
01814        match(Op1, m_LogicalShift(m_Value(), m_Value())))) {
01815     if (Instruction *BSwap = MatchBSwap(I))
01816       return BSwap;
01817   }
01818 
01819   // (X^C)|Y -> (X|Y)^C iff Y&C == 0
01820   if (Op0->hasOneUse() &&
01821       match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
01822       MaskedValueIsZero(Op1, C1->getValue())) {
01823     Value *NOr = Builder->CreateOr(A, Op1);
01824     NOr->takeName(Op0);
01825     return BinaryOperator::CreateXor(NOr, C1);
01826   }
01827 
01828   // Y|(X^C) -> (X|Y)^C iff Y&C == 0
01829   if (Op1->hasOneUse() &&
01830       match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
01831       MaskedValueIsZero(Op0, C1->getValue())) {
01832     Value *NOr = Builder->CreateOr(A, Op0);
01833     NOr->takeName(Op0);
01834     return BinaryOperator::CreateXor(NOr, C1);
01835   }
01836 
01837   // (A & C)|(B & D)
01838   Value *C = 0, *D = 0;
01839   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
01840       match(Op1, m_And(m_Value(B), m_Value(D)))) {
01841     Value *V1 = 0, *V2 = 0;
01842     C1 = dyn_cast<ConstantInt>(C);
01843     C2 = dyn_cast<ConstantInt>(D);
01844     if (C1 && C2) {  // (A & C1)|(B & C2)
01845       // If we have: ((V + N) & C1) | (V & C2)
01846       // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
01847       // replace with V+N.
01848       if (C1->getValue() == ~C2->getValue()) {
01849         if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
01850             match(A, m_Add(m_Value(V1), m_Value(V2)))) {
01851           // Add commutes, try both ways.
01852           if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
01853             return ReplaceInstUsesWith(I, A);
01854           if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
01855             return ReplaceInstUsesWith(I, A);
01856         }
01857         // Or commutes, try both ways.
01858         if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
01859             match(B, m_Add(m_Value(V1), m_Value(V2)))) {
01860           // Add commutes, try both ways.
01861           if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
01862             return ReplaceInstUsesWith(I, B);
01863           if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
01864             return ReplaceInstUsesWith(I, B);
01865         }
01866       }
01867 
01868       if ((C1->getValue() & C2->getValue()) == 0) {
01869         // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
01870         // iff (C1&C2) == 0 and (N&~C1) == 0
01871         if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
01872             ((V1 == B && MaskedValueIsZero(V2, ~C1->getValue())) ||  // (V|N)
01873              (V2 == B && MaskedValueIsZero(V1, ~C1->getValue()))))   // (N|V)
01874           return BinaryOperator::CreateAnd(A,
01875                                ConstantInt::get(A->getContext(),
01876                                                 C1->getValue()|C2->getValue()));
01877         // Or commutes, try both ways.
01878         if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
01879             ((V1 == A && MaskedValueIsZero(V2, ~C2->getValue())) ||  // (V|N)
01880              (V2 == A && MaskedValueIsZero(V1, ~C2->getValue()))))   // (N|V)
01881           return BinaryOperator::CreateAnd(B,
01882                                ConstantInt::get(B->getContext(),
01883                                                 C1->getValue()|C2->getValue()));
01884 
01885         // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
01886         // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
01887         ConstantInt *C3 = 0, *C4 = 0;
01888         if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
01889             (C3->getValue() & ~C1->getValue()) == 0 &&
01890             match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
01891             (C4->getValue() & ~C2->getValue()) == 0) {
01892           V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
01893           return BinaryOperator::CreateAnd(V2,
01894                                ConstantInt::get(B->getContext(),
01895                                                 C1->getValue()|C2->getValue()));
01896         }
01897       }
01898     }
01899 
01900     // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) ->  C0 ? A : B, and commuted variants.
01901     // Don't do this for vector select idioms, the code generator doesn't handle
01902     // them well yet.
01903     if (!I.getType()->isVectorTy()) {
01904       if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
01905         return Match;
01906       if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
01907         return Match;
01908       if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
01909         return Match;
01910       if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
01911         return Match;
01912     }
01913 
01914     // ((A&~B)|(~A&B)) -> A^B
01915     if ((match(C, m_Not(m_Specific(D))) &&
01916          match(B, m_Not(m_Specific(A)))))
01917       return BinaryOperator::CreateXor(A, D);
01918     // ((~B&A)|(~A&B)) -> A^B
01919     if ((match(A, m_Not(m_Specific(D))) &&
01920          match(B, m_Not(m_Specific(C)))))
01921       return BinaryOperator::CreateXor(C, D);
01922     // ((A&~B)|(B&~A)) -> A^B
01923     if ((match(C, m_Not(m_Specific(B))) &&
01924          match(D, m_Not(m_Specific(A)))))
01925       return BinaryOperator::CreateXor(A, B);
01926     // ((~B&A)|(B&~A)) -> A^B
01927     if ((match(A, m_Not(m_Specific(B))) &&
01928          match(D, m_Not(m_Specific(C)))))
01929       return BinaryOperator::CreateXor(C, B);
01930 
01931     // ((A|B)&1)|(B&-2) -> (A&1) | B
01932     if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
01933         match(A, m_Or(m_Specific(B), m_Value(V1)))) {
01934       Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
01935       if (Ret) return Ret;
01936     }
01937     // (B&-2)|((A|B)&1) -> (A&1) | B
01938     if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
01939         match(B, m_Or(m_Value(V1), m_Specific(A)))) {
01940       Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
01941       if (Ret) return Ret;
01942     }
01943   }
01944 
01945   // (X >> Z) | (Y >> Z)  -> (X|Y) >> Z  for all shifts.
01946   if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
01947     if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
01948       if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
01949           SI0->getOperand(1) == SI1->getOperand(1) &&
01950           (SI0->hasOneUse() || SI1->hasOneUse())) {
01951         Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
01952                                          SI0->getName());
01953         return BinaryOperator::Create(SI1->getOpcode(), NewOp,
01954                                       SI1->getOperand(1));
01955       }
01956   }
01957 
01958   // (~A | ~B) == (~(A & B)) - De Morgan's Law
01959   if (Value *Op0NotVal = dyn_castNotVal(Op0))
01960     if (Value *Op1NotVal = dyn_castNotVal(Op1))
01961       if (Op0->hasOneUse() && Op1->hasOneUse()) {
01962         Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
01963                                         I.getName()+".demorgan");
01964         return BinaryOperator::CreateNot(And);
01965       }
01966 
01967   // Canonicalize xor to the RHS.
01968   bool SwappedForXor = false;
01969   if (match(Op0, m_Xor(m_Value(), m_Value()))) {
01970     std::swap(Op0, Op1);
01971     SwappedForXor = true;
01972   }
01973 
01974   // A | ( A ^ B) -> A |  B
01975   // A | (~A ^ B) -> A | ~B
01976   // (A & B) | (A ^ B)
01977   if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
01978     if (Op0 == A || Op0 == B)
01979       return BinaryOperator::CreateOr(A, B);
01980 
01981     if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
01982         match(Op0, m_And(m_Specific(B), m_Specific(A))))
01983       return BinaryOperator::CreateOr(A, B);
01984 
01985     if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
01986       Value *Not = Builder->CreateNot(B, B->getName()+".not");
01987       return BinaryOperator::CreateOr(Not, Op0);
01988     }
01989     if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
01990       Value *Not = Builder->CreateNot(A, A->getName()+".not");
01991       return BinaryOperator::CreateOr(Not, Op0);
01992     }
01993   }
01994 
01995   // A | ~(A | B) -> A | ~B
01996   // A | ~(A ^ B) -> A | ~B
01997   if (match(Op1, m_Not(m_Value(A))))
01998     if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
01999       if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
02000           Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
02001                                B->getOpcode() == Instruction::Xor)) {
02002         Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
02003                                                  B->getOperand(0);
02004         Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
02005         return BinaryOperator::CreateOr(Not, Op0);
02006       }
02007 
02008   if (SwappedForXor)
02009     std::swap(Op0, Op1);
02010 
02011   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
02012     if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
02013       if (Value *Res = FoldOrOfICmps(LHS, RHS))
02014         return ReplaceInstUsesWith(I, Res);
02015 
02016   // (fcmp uno x, c) | (fcmp uno y, c)  -> (fcmp uno x, y)
02017   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
02018     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
02019       if (Value *Res = FoldOrOfFCmps(LHS, RHS))
02020         return ReplaceInstUsesWith(I, Res);
02021 
02022   // fold (or (cast A), (cast B)) -> (cast (or A, B))
02023   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
02024     CastInst *Op1C = dyn_cast<CastInst>(Op1);
02025     if (Op1C && Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
02026       Type *SrcTy = Op0C->getOperand(0)->getType();
02027       if (SrcTy == Op1C->getOperand(0)->getType() &&
02028           SrcTy->isIntOrIntVectorTy()) {
02029         Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
02030 
02031         if ((!isa<ICmpInst>(Op0COp) || !isa<ICmpInst>(Op1COp)) &&
02032             // Only do this if the casts both really cause code to be
02033             // generated.
02034             ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
02035             ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
02036           Value *NewOp = Builder->CreateOr(Op0COp, Op1COp, I.getName());
02037           return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
02038         }
02039 
02040         // If this is or(cast(icmp), cast(icmp)), try to fold this even if the
02041         // cast is otherwise not optimizable.  This happens for vector sexts.
02042         if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
02043           if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
02044             if (Value *Res = FoldOrOfICmps(LHS, RHS))
02045               return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
02046 
02047         // If this is or(cast(fcmp), cast(fcmp)), try to fold this even if the
02048         // cast is otherwise not optimizable.  This happens for vector sexts.
02049         if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
02050           if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
02051             if (Value *Res = FoldOrOfFCmps(LHS, RHS))
02052               return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
02053       }
02054     }
02055   }
02056 
02057   // or(sext(A), B) -> A ? -1 : B where A is an i1
02058   // or(A, sext(B)) -> B ? -1 : A where B is an i1
02059   if (match(Op0, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
02060     return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
02061   if (match(Op1, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
02062     return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
02063 
02064   // Note: If we've gotten to the point of visiting the outer OR, then the
02065   // inner one couldn't be simplified.  If it was a constant, then it won't
02066   // be simplified by a later pass either, so we try swapping the inner/outer
02067   // ORs in the hopes that we'll be able to simplify it this way.
02068   // (X|C) | V --> (X|V) | C
02069   if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
02070       match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
02071     Value *Inner = Builder->CreateOr(A, Op1);
02072     Inner->takeName(Op0);
02073     return BinaryOperator::CreateOr(Inner, C1);
02074   }
02075 
02076   // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
02077   // Since this OR statement hasn't been optimized further yet, we hope
02078   // that this transformation will allow the new ORs to be optimized.
02079   {
02080     Value *X = 0, *Y = 0;
02081     if (Op0->hasOneUse() && Op1->hasOneUse() &&
02082         match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
02083         match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
02084       Value *orTrue = Builder->CreateOr(A, C);
02085       Value *orFalse = Builder->CreateOr(B, D);
02086       return SelectInst::Create(X, orTrue, orFalse);
02087     }
02088   }
02089 
02090   return Changed ? &I : 0;
02091 }
02092 
02093 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
02094   bool Changed = SimplifyAssociativeOrCommutative(I);
02095   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
02096 
02097   if (Value *V = SimplifyXorInst(Op0, Op1, TD))
02098     return ReplaceInstUsesWith(I, V);
02099 
02100   // (A&B)^(A&C) -> A&(B^C) etc
02101   if (Value *V = SimplifyUsingDistributiveLaws(I))
02102     return ReplaceInstUsesWith(I, V);
02103 
02104   // See if we can simplify any instructions used by the instruction whose sole
02105   // purpose is to compute bits we don't care about.
02106   if (SimplifyDemandedInstructionBits(I))
02107     return &I;
02108 
02109   // Is this a ~ operation?
02110   if (Value *NotOp = dyn_castNotVal(&I)) {
02111     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
02112       if (Op0I->getOpcode() == Instruction::And ||
02113           Op0I->getOpcode() == Instruction::Or) {
02114         // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
02115         // ~(~X | Y) === (X & ~Y) - De Morgan's Law
02116         if (dyn_castNotVal(Op0I->getOperand(1)))
02117           Op0I->swapOperands();
02118         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
02119           Value *NotY =
02120             Builder->CreateNot(Op0I->getOperand(1),
02121                                Op0I->getOperand(1)->getName()+".not");
02122           if (Op0I->getOpcode() == Instruction::And)
02123             return BinaryOperator::CreateOr(Op0NotVal, NotY);
02124           return BinaryOperator::CreateAnd(Op0NotVal, NotY);
02125         }
02126 
02127         // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
02128         // ~(X | Y) === (~X & ~Y) - De Morgan's Law
02129         if (isFreeToInvert(Op0I->getOperand(0)) &&
02130             isFreeToInvert(Op0I->getOperand(1))) {
02131           Value *NotX =
02132             Builder->CreateNot(Op0I->getOperand(0), "notlhs");
02133           Value *NotY =
02134             Builder->CreateNot(Op0I->getOperand(1), "notrhs");
02135           if (Op0I->getOpcode() == Instruction::And)
02136             return BinaryOperator::CreateOr(NotX, NotY);
02137           return BinaryOperator::CreateAnd(NotX, NotY);
02138         }
02139 
02140       } else if (Op0I->getOpcode() == Instruction::AShr) {
02141         // ~(~X >>s Y) --> (X >>s Y)
02142         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
02143           return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
02144       }
02145     }
02146   }
02147 
02148 
02149   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
02150     if (RHS->isOne() && Op0->hasOneUse())
02151       // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
02152       if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
02153         return CmpInst::Create(CI->getOpcode(),
02154                                CI->getInversePredicate(),
02155                                CI->getOperand(0), CI->getOperand(1));
02156 
02157     // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
02158     if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
02159       if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
02160         if (CI->hasOneUse() && Op0C->hasOneUse()) {
02161           Instruction::CastOps Opcode = Op0C->getOpcode();
02162           if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
02163               (RHS == ConstantExpr::getCast(Opcode,
02164                                            ConstantInt::getTrue(I.getContext()),
02165                                             Op0C->getDestTy()))) {
02166             CI->setPredicate(CI->getInversePredicate());
02167             return CastInst::Create(Opcode, CI, Op0C->getType());
02168           }
02169         }
02170       }
02171     }
02172 
02173     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
02174       // ~(c-X) == X-c-1 == X+(-c-1)
02175       if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
02176         if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
02177           Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
02178           Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
02179                                       ConstantInt::get(I.getType(), 1));
02180           return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
02181         }
02182 
02183       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
02184         if (Op0I->getOpcode() == Instruction::Add) {
02185           // ~(X-c) --> (-c-1)-X
02186           if (RHS->isAllOnesValue()) {
02187             Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
02188             return BinaryOperator::CreateSub(
02189                            ConstantExpr::getSub(NegOp0CI,
02190                                       ConstantInt::get(I.getType(), 1)),
02191                                       Op0I->getOperand(0));
02192           } else if (RHS->getValue().isSignBit()) {
02193             // (X + C) ^ signbit -> (X + C + signbit)
02194             Constant *C = ConstantInt::get(I.getContext(),
02195                                            RHS->getValue() + Op0CI->getValue());
02196             return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
02197 
02198           }
02199         } else if (Op0I->getOpcode() == Instruction::Or) {
02200           // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
02201           if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
02202             Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
02203             // Anything in both C1 and C2 is known to be zero, remove it from
02204             // NewRHS.
02205             Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
02206             NewRHS = ConstantExpr::getAnd(NewRHS,
02207                                        ConstantExpr::getNot(CommonBits));
02208             Worklist.Add(Op0I);
02209             I.setOperand(0, Op0I->getOperand(0));
02210             I.setOperand(1, NewRHS);
02211             return &I;
02212           }
02213         } else if (Op0I->getOpcode() == Instruction::LShr) {
02214           // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
02215           // E1 = "X ^ C1"
02216           BinaryOperator *E1;
02217           ConstantInt *C1;
02218           if (Op0I->hasOneUse() &&
02219               (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
02220               E1->getOpcode() == Instruction::Xor &&
02221               (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
02222             // fold (C1 >> C2) ^ C3
02223             ConstantInt *C2 = Op0CI, *C3 = RHS;
02224             APInt FoldConst = C1->getValue().lshr(C2->getValue());
02225             FoldConst ^= C3->getValue();
02226             // Prepare the two operands.
02227             Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
02228             Opnd0->takeName(Op0I);
02229             cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
02230             Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
02231 
02232             return BinaryOperator::CreateXor(Opnd0, FoldVal);
02233           }
02234         }
02235       }
02236     }
02237 
02238     // Try to fold constant and into select arguments.
02239     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
02240       if (Instruction *R = FoldOpIntoSelect(I, SI))
02241         return R;
02242     if (isa<PHINode>(Op0))
02243       if (Instruction *NV = FoldOpIntoPhi(I))
02244         return NV;
02245   }
02246 
02247   BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
02248   if (Op1I) {
02249     Value *A, *B;
02250     if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
02251       if (A == Op0) {              // B^(B|A) == (A|B)^B
02252         Op1I->swapOperands();
02253         I.swapOperands();
02254         std::swap(Op0, Op1);
02255       } else if (B == Op0) {       // B^(A|B) == (A|B)^B
02256         I.swapOperands();     // Simplified below.
02257         std::swap(Op0, Op1);
02258       }
02259     } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
02260                Op1I->hasOneUse()){
02261       if (A == Op0) {                                      // A^(A&B) -> A^(B&A)
02262         Op1I->swapOperands();
02263         std::swap(A, B);
02264       }
02265       if (B == Op0) {                                      // A^(B&A) -> (B&A)^A
02266         I.swapOperands();     // Simplified below.
02267         std::swap(Op0, Op1);
02268       }
02269     }
02270   }
02271 
02272   BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
02273   if (Op0I) {
02274     Value *A, *B;
02275     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
02276         Op0I->hasOneUse()) {
02277       if (A == Op1)                                  // (B|A)^B == (A|B)^B
02278         std::swap(A, B);
02279       if (B == Op1)                                  // (A|B)^B == A & ~B
02280         return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
02281     } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
02282                Op0I->hasOneUse()){
02283       if (A == Op1)                                        // (A&B)^A -> (B&A)^A
02284         std::swap(A, B);
02285       if (B == Op1 &&                                      // (B&A)^A == ~B & A
02286           !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C
02287         return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
02288       }
02289     }
02290   }
02291 
02292   // (X >> Z) ^ (Y >> Z)  -> (X^Y) >> Z  for all shifts.
02293   if (Op0I && Op1I && Op0I->isShift() &&
02294       Op0I->getOpcode() == Op1I->getOpcode() &&
02295       Op0I->getOperand(1) == Op1I->getOperand(1) &&
02296       (Op0I->hasOneUse() || Op1I->hasOneUse())) {
02297     Value *NewOp =
02298       Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0),
02299                          Op0I->getName());
02300     return BinaryOperator::Create(Op1I->getOpcode(), NewOp,
02301                                   Op1I->getOperand(1));
02302   }
02303 
02304   if (Op0I && Op1I) {
02305     Value *A, *B, *C, *D;
02306     // (A & B)^(A | B) -> A ^ B
02307     if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
02308         match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
02309       if ((A == C && B == D) || (A == D && B == C))
02310         return BinaryOperator::CreateXor(A, B);
02311     }
02312     // (A | B)^(A & B) -> A ^ B
02313     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
02314         match(Op1I, m_And(m_Value(C), m_Value(D)))) {
02315       if ((A == C && B == D) || (A == D && B == C))
02316         return BinaryOperator::CreateXor(A, B);
02317     }
02318   }
02319 
02320   // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
02321   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
02322     if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
02323       if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
02324         if (LHS->getOperand(0) == RHS->getOperand(1) &&
02325             LHS->getOperand(1) == RHS->getOperand(0))
02326           LHS->swapOperands();
02327         if (LHS->getOperand(0) == RHS->getOperand(0) &&
02328             LHS->getOperand(1) == RHS->getOperand(1)) {
02329           Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
02330           unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
02331           bool isSigned = LHS->isSigned() || RHS->isSigned();
02332           return ReplaceInstUsesWith(I,
02333                                getNewICmpValue(isSigned, Code, Op0, Op1,
02334                                                Builder));
02335         }
02336       }
02337 
02338   // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
02339   if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
02340     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
02341       if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
02342         Type *SrcTy = Op0C->getOperand(0)->getType();
02343         if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegerTy() &&
02344             // Only do this if the casts both really cause code to be generated.
02345             ShouldOptimizeCast(Op0C->getOpcode(), Op0C->getOperand(0),
02346                                I.getType()) &&
02347             ShouldOptimizeCast(Op1C->getOpcode(), Op1C->getOperand(0),
02348                                I.getType())) {
02349           Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
02350                                             Op1C->getOperand(0), I.getName());
02351           return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
02352         }
02353       }
02354   }
02355 
02356   return Changed ? &I : 0;
02357 }