1 //===- InstCombineAndOrXor.cpp --------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the visitAnd, visitOr, and visitXor functions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "InstCombineInternal.h" 15 #include "llvm/Analysis/InstructionSimplify.h" 16 #include "llvm/IR/ConstantRange.h" 17 #include "llvm/IR/Intrinsics.h" 18 #include "llvm/IR/PatternMatch.h" 19 #include "llvm/Transforms/Utils/CmpInstAnalysis.h" 20 #include "llvm/Transforms/Utils/Local.h" 21 using namespace llvm; 22 using namespace PatternMatch; 23 24 #define DEBUG_TYPE "instcombine" 25 26 static inline Value *dyn_castNotVal(Value *V) { 27 // If this is not(not(x)) don't return that this is a not: we want the two 28 // not's to be folded first. 29 if (BinaryOperator::isNot(V)) { 30 Value *Operand = BinaryOperator::getNotArgument(V); 31 if (!IsFreeToInvert(Operand, Operand->hasOneUse())) 32 return Operand; 33 } 34 35 // Constants can be considered to be not'ed values... 36 if (ConstantInt *C = dyn_cast<ConstantInt>(V)) 37 return ConstantInt::get(C->getType(), ~C->getValue()); 38 return nullptr; 39 } 40 41 /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate into 42 /// a three bit mask. It also returns whether it is an ordered predicate by 43 /// reference. 44 static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) { 45 isOrdered = false; 46 switch (CC) { 47 case FCmpInst::FCMP_ORD: isOrdered = true; return 0; // 000 48 case FCmpInst::FCMP_UNO: return 0; // 000 49 case FCmpInst::FCMP_OGT: isOrdered = true; return 1; // 001 50 case FCmpInst::FCMP_UGT: return 1; // 001 51 case FCmpInst::FCMP_OEQ: isOrdered = true; return 2; // 010 52 case FCmpInst::FCMP_UEQ: return 2; // 010 53 case FCmpInst::FCMP_OGE: isOrdered = true; return 3; // 011 54 case FCmpInst::FCMP_UGE: return 3; // 011 55 case FCmpInst::FCMP_OLT: isOrdered = true; return 4; // 100 56 case FCmpInst::FCMP_ULT: return 4; // 100 57 case FCmpInst::FCMP_ONE: isOrdered = true; return 5; // 101 58 case FCmpInst::FCMP_UNE: return 5; // 101 59 case FCmpInst::FCMP_OLE: isOrdered = true; return 6; // 110 60 case FCmpInst::FCMP_ULE: return 6; // 110 61 // True -> 7 62 default: 63 // Not expecting FCMP_FALSE and FCMP_TRUE; 64 llvm_unreachable("Unexpected FCmp predicate!"); 65 } 66 } 67 68 /// This is the complement of getICmpCode, which turns an opcode and two 69 /// operands into either a constant true or false, or a brand new ICmp 70 /// instruction. The sign is passed in to determine which kind of predicate to 71 /// use in the new icmp instruction. 72 static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, 73 InstCombiner::BuilderTy *Builder) { 74 ICmpInst::Predicate NewPred; 75 if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred)) 76 return NewConstant; 77 return Builder->CreateICmp(NewPred, LHS, RHS); 78 } 79 80 /// This is the complement of getFCmpCode, which turns an opcode and two 81 /// operands into either a FCmp instruction. isordered is passed in to determine 82 /// which kind of predicate to use in the new fcmp instruction. 83 static Value *getFCmpValue(bool isordered, unsigned code, 84 Value *LHS, Value *RHS, 85 InstCombiner::BuilderTy *Builder) { 86 CmpInst::Predicate Pred; 87 switch (code) { 88 default: llvm_unreachable("Illegal FCmp code!"); 89 case 0: Pred = isordered ? FCmpInst::FCMP_ORD : FCmpInst::FCMP_UNO; break; 90 case 1: Pred = isordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; break; 91 case 2: Pred = isordered ? FCmpInst::FCMP_OEQ : FCmpInst::FCMP_UEQ; break; 92 case 3: Pred = isordered ? FCmpInst::FCMP_OGE : FCmpInst::FCMP_UGE; break; 93 case 4: Pred = isordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; break; 94 case 5: Pred = isordered ? FCmpInst::FCMP_ONE : FCmpInst::FCMP_UNE; break; 95 case 6: Pred = isordered ? FCmpInst::FCMP_OLE : FCmpInst::FCMP_ULE; break; 96 case 7: 97 if (!isordered) 98 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1); 99 Pred = FCmpInst::FCMP_ORD; break; 100 } 101 return Builder->CreateFCmp(Pred, LHS, RHS); 102 } 103 104 /// \brief Transform BITWISE_OP(BSWAP(A),BSWAP(B)) to BSWAP(BITWISE_OP(A, B)) 105 /// \param I Binary operator to transform. 106 /// \return Pointer to node that must replace the original binary operator, or 107 /// null pointer if no transformation was made. 108 Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) { 109 IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); 110 111 // Can't do vectors. 112 if (I.getType()->isVectorTy()) return nullptr; 113 114 // Can only do bitwise ops. 115 unsigned Op = I.getOpcode(); 116 if (Op != Instruction::And && Op != Instruction::Or && 117 Op != Instruction::Xor) 118 return nullptr; 119 120 Value *OldLHS = I.getOperand(0); 121 Value *OldRHS = I.getOperand(1); 122 ConstantInt *ConstLHS = dyn_cast<ConstantInt>(OldLHS); 123 ConstantInt *ConstRHS = dyn_cast<ConstantInt>(OldRHS); 124 IntrinsicInst *IntrLHS = dyn_cast<IntrinsicInst>(OldLHS); 125 IntrinsicInst *IntrRHS = dyn_cast<IntrinsicInst>(OldRHS); 126 bool IsBswapLHS = (IntrLHS && IntrLHS->getIntrinsicID() == Intrinsic::bswap); 127 bool IsBswapRHS = (IntrRHS && IntrRHS->getIntrinsicID() == Intrinsic::bswap); 128 129 if (!IsBswapLHS && !IsBswapRHS) 130 return nullptr; 131 132 if (!IsBswapLHS && !ConstLHS) 133 return nullptr; 134 135 if (!IsBswapRHS && !ConstRHS) 136 return nullptr; 137 138 /// OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) ) 139 /// OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) ) 140 Value *NewLHS = IsBswapLHS ? IntrLHS->getOperand(0) : 141 Builder->getInt(ConstLHS->getValue().byteSwap()); 142 143 Value *NewRHS = IsBswapRHS ? IntrRHS->getOperand(0) : 144 Builder->getInt(ConstRHS->getValue().byteSwap()); 145 146 Value *BinOp = nullptr; 147 if (Op == Instruction::And) 148 BinOp = Builder->CreateAnd(NewLHS, NewRHS); 149 else if (Op == Instruction::Or) 150 BinOp = Builder->CreateOr(NewLHS, NewRHS); 151 else //if (Op == Instruction::Xor) 152 BinOp = Builder->CreateXor(NewLHS, NewRHS); 153 154 Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap, ITy); 155 return Builder->CreateCall(F, BinOp); 156 } 157 158 /// This handles expressions of the form ((val OP C1) & C2). Where 159 /// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is 160 /// guaranteed to be a binary operator. 161 Instruction *InstCombiner::OptAndOp(Instruction *Op, 162 ConstantInt *OpRHS, 163 ConstantInt *AndRHS, 164 BinaryOperator &TheAnd) { 165 Value *X = Op->getOperand(0); 166 Constant *Together = nullptr; 167 if (!Op->isShift()) 168 Together = ConstantExpr::getAnd(AndRHS, OpRHS); 169 170 switch (Op->getOpcode()) { 171 case Instruction::Xor: 172 if (Op->hasOneUse()) { 173 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) 174 Value *And = Builder->CreateAnd(X, AndRHS); 175 And->takeName(Op); 176 return BinaryOperator::CreateXor(And, Together); 177 } 178 break; 179 case Instruction::Or: 180 if (Op->hasOneUse()){ 181 if (Together != OpRHS) { 182 // (X | C1) & C2 --> (X | (C1&C2)) & C2 183 Value *Or = Builder->CreateOr(X, Together); 184 Or->takeName(Op); 185 return BinaryOperator::CreateAnd(Or, AndRHS); 186 } 187 188 ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together); 189 if (TogetherCI && !TogetherCI->isZero()){ 190 // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1 191 // NOTE: This reduces the number of bits set in the & mask, which 192 // can expose opportunities for store narrowing. 193 Together = ConstantExpr::getXor(AndRHS, Together); 194 Value *And = Builder->CreateAnd(X, Together); 195 And->takeName(Op); 196 return BinaryOperator::CreateOr(And, OpRHS); 197 } 198 } 199 200 break; 201 case Instruction::Add: 202 if (Op->hasOneUse()) { 203 // Adding a one to a single bit bit-field should be turned into an XOR 204 // of the bit. First thing to check is to see if this AND is with a 205 // single bit constant. 206 const APInt &AndRHSV = AndRHS->getValue(); 207 208 // If there is only one bit set. 209 if (AndRHSV.isPowerOf2()) { 210 // Ok, at this point, we know that we are masking the result of the 211 // ADD down to exactly one bit. If the constant we are adding has 212 // no bits set below this bit, then we can eliminate the ADD. 213 const APInt& AddRHS = OpRHS->getValue(); 214 215 // Check to see if any bits below the one bit set in AndRHSV are set. 216 if ((AddRHS & (AndRHSV-1)) == 0) { 217 // If not, the only thing that can effect the output of the AND is 218 // the bit specified by AndRHSV. If that bit is set, the effect of 219 // the XOR is to toggle the bit. If it is clear, then the ADD has 220 // no effect. 221 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop 222 TheAnd.setOperand(0, X); 223 return &TheAnd; 224 } else { 225 // Pull the XOR out of the AND. 226 Value *NewAnd = Builder->CreateAnd(X, AndRHS); 227 NewAnd->takeName(Op); 228 return BinaryOperator::CreateXor(NewAnd, AndRHS); 229 } 230 } 231 } 232 } 233 break; 234 235 case Instruction::Shl: { 236 // We know that the AND will not produce any of the bits shifted in, so if 237 // the anded constant includes them, clear them now! 238 // 239 uint32_t BitWidth = AndRHS->getType()->getBitWidth(); 240 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); 241 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); 242 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask); 243 244 if (CI->getValue() == ShlMask) 245 // Masking out bits that the shift already masks. 246 return replaceInstUsesWith(TheAnd, Op); // No need for the and. 247 248 if (CI != AndRHS) { // Reducing bits set in and. 249 TheAnd.setOperand(1, CI); 250 return &TheAnd; 251 } 252 break; 253 } 254 case Instruction::LShr: { 255 // We know that the AND will not produce any of the bits shifted in, so if 256 // the anded constant includes them, clear them now! This only applies to 257 // unsigned shifts, because a signed shr may bring in set bits! 258 // 259 uint32_t BitWidth = AndRHS->getType()->getBitWidth(); 260 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); 261 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); 262 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask); 263 264 if (CI->getValue() == ShrMask) 265 // Masking out bits that the shift already masks. 266 return replaceInstUsesWith(TheAnd, Op); 267 268 if (CI != AndRHS) { 269 TheAnd.setOperand(1, CI); // Reduce bits set in and cst. 270 return &TheAnd; 271 } 272 break; 273 } 274 case Instruction::AShr: 275 // Signed shr. 276 // See if this is shifting in some sign extension, then masking it out 277 // with an and. 278 if (Op->hasOneUse()) { 279 uint32_t BitWidth = AndRHS->getType()->getBitWidth(); 280 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); 281 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); 282 Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask); 283 if (C == AndRHS) { // Masking out bits shifted in. 284 // (Val ashr C1) & C2 -> (Val lshr C1) & C2 285 // Make the argument unsigned. 286 Value *ShVal = Op->getOperand(0); 287 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName()); 288 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); 289 } 290 } 291 break; 292 } 293 return nullptr; 294 } 295 296 /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise 297 /// (V < Lo || V >= Hi). In practice, we emit the more efficient 298 /// (V-Lo) \<u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates 299 /// whether to treat the V, Lo and HI as signed or not. IB is the location to 300 /// insert new instructions. 301 Value *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, 302 bool isSigned, bool Inside) { 303 assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? 304 ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && 305 "Lo is not <= Hi in range emission code!"); 306 307 if (Inside) { 308 if (Lo == Hi) // Trivially false. 309 return Builder->getFalse(); 310 311 // V >= Min && V < Hi --> V < Hi 312 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { 313 ICmpInst::Predicate pred = (isSigned ? 314 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); 315 return Builder->CreateICmp(pred, V, Hi); 316 } 317 318 // Emit V-Lo <u Hi-Lo 319 Constant *NegLo = ConstantExpr::getNeg(Lo); 320 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off"); 321 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); 322 return Builder->CreateICmpULT(Add, UpperBound); 323 } 324 325 if (Lo == Hi) // Trivially true. 326 return Builder->getTrue(); 327 328 // V < Min || V >= Hi -> V > Hi-1 329 Hi = SubOne(cast<ConstantInt>(Hi)); 330 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { 331 ICmpInst::Predicate pred = (isSigned ? 332 ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); 333 return Builder->CreateICmp(pred, V, Hi); 334 } 335 336 // Emit V-Lo >u Hi-1-Lo 337 // Note that Hi has already had one subtracted from it, above. 338 ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); 339 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off"); 340 Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); 341 return Builder->CreateICmpUGT(Add, LowerBound); 342 } 343 344 /// Returns true iff Val consists of one contiguous run of 1s with any number 345 /// of 0s on either side. The 1s are allowed to wrap from LSB to MSB, 346 /// so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is 347 /// not, since all 1s are not contiguous. 348 static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { 349 const APInt& V = Val->getValue(); 350 uint32_t BitWidth = Val->getType()->getBitWidth(); 351 if (!APIntOps::isShiftedMask(BitWidth, V)) return false; 352 353 // look for the first zero bit after the run of ones 354 MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); 355 // look for the first non-zero bit 356 ME = V.getActiveBits(); 357 return true; 358 } 359 360 /// This is part of an expression (LHS +/- RHS) & Mask, where isSub determines 361 /// whether the operator is a sub. If we can fold one of the following xforms: 362 /// 363 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask 364 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 365 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 366 /// 367 /// return (A +/- B). 368 /// 369 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, 370 ConstantInt *Mask, bool isSub, 371 Instruction &I) { 372 Instruction *LHSI = dyn_cast<Instruction>(LHS); 373 if (!LHSI || LHSI->getNumOperands() != 2 || 374 !isa<ConstantInt>(LHSI->getOperand(1))) return nullptr; 375 376 ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); 377 378 switch (LHSI->getOpcode()) { 379 default: return nullptr; 380 case Instruction::And: 381 if (ConstantExpr::getAnd(N, Mask) == Mask) { 382 // If the AndRHS is a power of two minus one (0+1+), this is simple. 383 if ((Mask->getValue().countLeadingZeros() + 384 Mask->getValue().countPopulation()) == 385 Mask->getValue().getBitWidth()) 386 break; 387 388 // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ 389 // part, we don't need any explicit masks to take them out of A. If that 390 // is all N is, ignore it. 391 uint32_t MB = 0, ME = 0; 392 if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive 393 uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); 394 APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); 395 if (MaskedValueIsZero(RHS, Mask, 0, &I)) 396 break; 397 } 398 } 399 return nullptr; 400 case Instruction::Or: 401 case Instruction::Xor: 402 // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 403 if ((Mask->getValue().countLeadingZeros() + 404 Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() 405 && ConstantExpr::getAnd(N, Mask)->isNullValue()) 406 break; 407 return nullptr; 408 } 409 410 if (isSub) 411 return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold"); 412 return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold"); 413 } 414 415 /// enum for classifying (icmp eq (A & B), C) and (icmp ne (A & B), C) 416 /// One of A and B is considered the mask, the other the value. This is 417 /// described as the "AMask" or "BMask" part of the enum. If the enum 418 /// contains only "Mask", then both A and B can be considered masks. 419 /// If A is the mask, then it was proven, that (A & C) == C. This 420 /// is trivial if C == A, or C == 0. If both A and C are constants, this 421 /// proof is also easy. 422 /// For the following explanations we assume that A is the mask. 423 /// The part "AllOnes" declares, that the comparison is true only 424 /// if (A & B) == A, or all bits of A are set in B. 425 /// Example: (icmp eq (A & 3), 3) -> FoldMskICmp_AMask_AllOnes 426 /// The part "AllZeroes" declares, that the comparison is true only 427 /// if (A & B) == 0, or all bits of A are cleared in B. 428 /// Example: (icmp eq (A & 3), 0) -> FoldMskICmp_Mask_AllZeroes 429 /// The part "Mixed" declares, that (A & B) == C and C might or might not 430 /// contain any number of one bits and zero bits. 431 /// Example: (icmp eq (A & 3), 1) -> FoldMskICmp_AMask_Mixed 432 /// The Part "Not" means, that in above descriptions "==" should be replaced 433 /// by "!=". 434 /// Example: (icmp ne (A & 3), 3) -> FoldMskICmp_AMask_NotAllOnes 435 /// If the mask A contains a single bit, then the following is equivalent: 436 /// (icmp eq (A & B), A) equals (icmp ne (A & B), 0) 437 /// (icmp ne (A & B), A) equals (icmp eq (A & B), 0) 438 enum MaskedICmpType { 439 FoldMskICmp_AMask_AllOnes = 1, 440 FoldMskICmp_AMask_NotAllOnes = 2, 441 FoldMskICmp_BMask_AllOnes = 4, 442 FoldMskICmp_BMask_NotAllOnes = 8, 443 FoldMskICmp_Mask_AllZeroes = 16, 444 FoldMskICmp_Mask_NotAllZeroes = 32, 445 FoldMskICmp_AMask_Mixed = 64, 446 FoldMskICmp_AMask_NotMixed = 128, 447 FoldMskICmp_BMask_Mixed = 256, 448 FoldMskICmp_BMask_NotMixed = 512 449 }; 450 451 /// Return the set of pattern classes (from MaskedICmpType) 452 /// that (icmp SCC (A & B), C) satisfies. 453 static unsigned getTypeOfMaskedICmp(Value* A, Value* B, Value* C, 454 ICmpInst::Predicate SCC) 455 { 456 ConstantInt *ACst = dyn_cast<ConstantInt>(A); 457 ConstantInt *BCst = dyn_cast<ConstantInt>(B); 458 ConstantInt *CCst = dyn_cast<ConstantInt>(C); 459 bool icmp_eq = (SCC == ICmpInst::ICMP_EQ); 460 bool icmp_abit = (ACst && !ACst->isZero() && 461 ACst->getValue().isPowerOf2()); 462 bool icmp_bbit = (BCst && !BCst->isZero() && 463 BCst->getValue().isPowerOf2()); 464 unsigned result = 0; 465 if (CCst && CCst->isZero()) { 466 // if C is zero, then both A and B qualify as mask 467 result |= (icmp_eq ? (FoldMskICmp_Mask_AllZeroes | 468 FoldMskICmp_AMask_Mixed | 469 FoldMskICmp_BMask_Mixed) 470 : (FoldMskICmp_Mask_NotAllZeroes | 471 FoldMskICmp_AMask_NotMixed | 472 FoldMskICmp_BMask_NotMixed)); 473 if (icmp_abit) 474 result |= (icmp_eq ? (FoldMskICmp_AMask_NotAllOnes | 475 FoldMskICmp_AMask_NotMixed) 476 : (FoldMskICmp_AMask_AllOnes | 477 FoldMskICmp_AMask_Mixed)); 478 if (icmp_bbit) 479 result |= (icmp_eq ? (FoldMskICmp_BMask_NotAllOnes | 480 FoldMskICmp_BMask_NotMixed) 481 : (FoldMskICmp_BMask_AllOnes | 482 FoldMskICmp_BMask_Mixed)); 483 return result; 484 } 485 if (A == C) { 486 result |= (icmp_eq ? (FoldMskICmp_AMask_AllOnes | 487 FoldMskICmp_AMask_Mixed) 488 : (FoldMskICmp_AMask_NotAllOnes | 489 FoldMskICmp_AMask_NotMixed)); 490 if (icmp_abit) 491 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes | 492 FoldMskICmp_AMask_NotMixed) 493 : (FoldMskICmp_Mask_AllZeroes | 494 FoldMskICmp_AMask_Mixed)); 495 } else if (ACst && CCst && 496 ConstantExpr::getAnd(ACst, CCst) == CCst) { 497 result |= (icmp_eq ? FoldMskICmp_AMask_Mixed 498 : FoldMskICmp_AMask_NotMixed); 499 } 500 if (B == C) { 501 result |= (icmp_eq ? (FoldMskICmp_BMask_AllOnes | 502 FoldMskICmp_BMask_Mixed) 503 : (FoldMskICmp_BMask_NotAllOnes | 504 FoldMskICmp_BMask_NotMixed)); 505 if (icmp_bbit) 506 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes | 507 FoldMskICmp_BMask_NotMixed) 508 : (FoldMskICmp_Mask_AllZeroes | 509 FoldMskICmp_BMask_Mixed)); 510 } else if (BCst && CCst && 511 ConstantExpr::getAnd(BCst, CCst) == CCst) { 512 result |= (icmp_eq ? FoldMskICmp_BMask_Mixed 513 : FoldMskICmp_BMask_NotMixed); 514 } 515 return result; 516 } 517 518 /// Convert an analysis of a masked ICmp into its equivalent if all boolean 519 /// operations had the opposite sense. Since each "NotXXX" flag (recording !=) 520 /// is adjacent to the corresponding normal flag (recording ==), this just 521 /// involves swapping those bits over. 522 static unsigned conjugateICmpMask(unsigned Mask) { 523 unsigned NewMask; 524 NewMask = (Mask & (FoldMskICmp_AMask_AllOnes | FoldMskICmp_BMask_AllOnes | 525 FoldMskICmp_Mask_AllZeroes | FoldMskICmp_AMask_Mixed | 526 FoldMskICmp_BMask_Mixed)) 527 << 1; 528 529 NewMask |= 530 (Mask & (FoldMskICmp_AMask_NotAllOnes | FoldMskICmp_BMask_NotAllOnes | 531 FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_AMask_NotMixed | 532 FoldMskICmp_BMask_NotMixed)) 533 >> 1; 534 535 return NewMask; 536 } 537 538 /// Decompose an icmp into the form ((X & Y) pred Z) if possible. 539 /// The returned predicate is either == or !=. Returns false if 540 /// decomposition fails. 541 static bool decomposeBitTestICmp(const ICmpInst *I, ICmpInst::Predicate &Pred, 542 Value *&X, Value *&Y, Value *&Z) { 543 ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)); 544 if (!C) 545 return false; 546 547 switch (I->getPredicate()) { 548 default: 549 return false; 550 case ICmpInst::ICMP_SLT: 551 // X < 0 is equivalent to (X & SignBit) != 0. 552 if (!C->isZero()) 553 return false; 554 Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth())); 555 Pred = ICmpInst::ICMP_NE; 556 break; 557 case ICmpInst::ICMP_SGT: 558 // X > -1 is equivalent to (X & SignBit) == 0. 559 if (!C->isAllOnesValue()) 560 return false; 561 Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth())); 562 Pred = ICmpInst::ICMP_EQ; 563 break; 564 case ICmpInst::ICMP_ULT: 565 // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0. 566 if (!C->getValue().isPowerOf2()) 567 return false; 568 Y = ConstantInt::get(I->getContext(), -C->getValue()); 569 Pred = ICmpInst::ICMP_EQ; 570 break; 571 case ICmpInst::ICMP_UGT: 572 // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0. 573 if (!(C->getValue() + 1).isPowerOf2()) 574 return false; 575 Y = ConstantInt::get(I->getContext(), ~C->getValue()); 576 Pred = ICmpInst::ICMP_NE; 577 break; 578 } 579 580 X = I->getOperand(0); 581 Z = ConstantInt::getNullValue(C->getType()); 582 return true; 583 } 584 585 /// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) 586 /// Return the set of pattern classes (from MaskedICmpType) 587 /// that both LHS and RHS satisfy. 588 static unsigned foldLogOpOfMaskedICmpsHelper(Value*& A, 589 Value*& B, Value*& C, 590 Value*& D, Value*& E, 591 ICmpInst *LHS, ICmpInst *RHS, 592 ICmpInst::Predicate &LHSCC, 593 ICmpInst::Predicate &RHSCC) { 594 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) return 0; 595 // vectors are not (yet?) supported 596 if (LHS->getOperand(0)->getType()->isVectorTy()) return 0; 597 598 // Here comes the tricky part: 599 // LHS might be of the form L11 & L12 == X, X == L21 & L22, 600 // and L11 & L12 == L21 & L22. The same goes for RHS. 601 // Now we must find those components L** and R**, that are equal, so 602 // that we can extract the parameters A, B, C, D, and E for the canonical 603 // above. 604 Value *L1 = LHS->getOperand(0); 605 Value *L2 = LHS->getOperand(1); 606 Value *L11,*L12,*L21,*L22; 607 // Check whether the icmp can be decomposed into a bit test. 608 if (decomposeBitTestICmp(LHS, LHSCC, L11, L12, L2)) { 609 L21 = L22 = L1 = nullptr; 610 } else { 611 // Look for ANDs in the LHS icmp. 612 if (!L1->getType()->isIntegerTy()) { 613 // You can icmp pointers, for example. They really aren't masks. 614 L11 = L12 = nullptr; 615 } else if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) { 616 // Any icmp can be viewed as being trivially masked; if it allows us to 617 // remove one, it's worth it. 618 L11 = L1; 619 L12 = Constant::getAllOnesValue(L1->getType()); 620 } 621 622 if (!L2->getType()->isIntegerTy()) { 623 // You can icmp pointers, for example. They really aren't masks. 624 L21 = L22 = nullptr; 625 } else if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) { 626 L21 = L2; 627 L22 = Constant::getAllOnesValue(L2->getType()); 628 } 629 } 630 631 // Bail if LHS was a icmp that can't be decomposed into an equality. 632 if (!ICmpInst::isEquality(LHSCC)) 633 return 0; 634 635 Value *R1 = RHS->getOperand(0); 636 Value *R2 = RHS->getOperand(1); 637 Value *R11,*R12; 638 bool ok = false; 639 if (decomposeBitTestICmp(RHS, RHSCC, R11, R12, R2)) { 640 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { 641 A = R11; D = R12; 642 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { 643 A = R12; D = R11; 644 } else { 645 return 0; 646 } 647 E = R2; R1 = nullptr; ok = true; 648 } else if (R1->getType()->isIntegerTy()) { 649 if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) { 650 // As before, model no mask as a trivial mask if it'll let us do an 651 // optimization. 652 R11 = R1; 653 R12 = Constant::getAllOnesValue(R1->getType()); 654 } 655 656 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { 657 A = R11; D = R12; E = R2; ok = true; 658 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { 659 A = R12; D = R11; E = R2; ok = true; 660 } 661 } 662 663 // Bail if RHS was a icmp that can't be decomposed into an equality. 664 if (!ICmpInst::isEquality(RHSCC)) 665 return 0; 666 667 // Look for ANDs on the right side of the RHS icmp. 668 if (!ok && R2->getType()->isIntegerTy()) { 669 if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) { 670 R11 = R2; 671 R12 = Constant::getAllOnesValue(R2->getType()); 672 } 673 674 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { 675 A = R11; D = R12; E = R1; ok = true; 676 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { 677 A = R12; D = R11; E = R1; ok = true; 678 } else { 679 return 0; 680 } 681 } 682 if (!ok) 683 return 0; 684 685 if (L11 == A) { 686 B = L12; C = L2; 687 } else if (L12 == A) { 688 B = L11; C = L2; 689 } else if (L21 == A) { 690 B = L22; C = L1; 691 } else if (L22 == A) { 692 B = L21; C = L1; 693 } 694 695 unsigned LeftType = getTypeOfMaskedICmp(A, B, C, LHSCC); 696 unsigned RightType = getTypeOfMaskedICmp(A, D, E, RHSCC); 697 return LeftType & RightType; 698 } 699 700 /// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) 701 /// into a single (icmp(A & X) ==/!= Y). 702 static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, 703 llvm::InstCombiner::BuilderTy *Builder) { 704 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr; 705 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate(); 706 unsigned Mask = foldLogOpOfMaskedICmpsHelper(A, B, C, D, E, LHS, RHS, 707 LHSCC, RHSCC); 708 if (Mask == 0) return nullptr; 709 assert(ICmpInst::isEquality(LHSCC) && ICmpInst::isEquality(RHSCC) && 710 "foldLogOpOfMaskedICmpsHelper must return an equality predicate."); 711 712 // In full generality: 713 // (icmp (A & B) Op C) | (icmp (A & D) Op E) 714 // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ] 715 // 716 // If the latter can be converted into (icmp (A & X) Op Y) then the former is 717 // equivalent to (icmp (A & X) !Op Y). 718 // 719 // Therefore, we can pretend for the rest of this function that we're dealing 720 // with the conjunction, provided we flip the sense of any comparisons (both 721 // input and output). 722 723 // In most cases we're going to produce an EQ for the "&&" case. 724 ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; 725 if (!IsAnd) { 726 // Convert the masking analysis into its equivalent with negated 727 // comparisons. 728 Mask = conjugateICmpMask(Mask); 729 } 730 731 if (Mask & FoldMskICmp_Mask_AllZeroes) { 732 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0) 733 // -> (icmp eq (A & (B|D)), 0) 734 Value *NewOr = Builder->CreateOr(B, D); 735 Value *NewAnd = Builder->CreateAnd(A, NewOr); 736 // We can't use C as zero because we might actually handle 737 // (icmp ne (A & B), B) & (icmp ne (A & D), D) 738 // with B and D, having a single bit set. 739 Value *Zero = Constant::getNullValue(A->getType()); 740 return Builder->CreateICmp(NewCC, NewAnd, Zero); 741 } 742 if (Mask & FoldMskICmp_BMask_AllOnes) { 743 // (icmp eq (A & B), B) & (icmp eq (A & D), D) 744 // -> (icmp eq (A & (B|D)), (B|D)) 745 Value *NewOr = Builder->CreateOr(B, D); 746 Value *NewAnd = Builder->CreateAnd(A, NewOr); 747 return Builder->CreateICmp(NewCC, NewAnd, NewOr); 748 } 749 if (Mask & FoldMskICmp_AMask_AllOnes) { 750 // (icmp eq (A & B), A) & (icmp eq (A & D), A) 751 // -> (icmp eq (A & (B&D)), A) 752 Value *NewAnd1 = Builder->CreateAnd(B, D); 753 Value *NewAnd2 = Builder->CreateAnd(A, NewAnd1); 754 return Builder->CreateICmp(NewCC, NewAnd2, A); 755 } 756 757 // Remaining cases assume at least that B and D are constant, and depend on 758 // their actual values. This isn't strictly necessary, just a "handle the 759 // easy cases for now" decision. 760 ConstantInt *BCst = dyn_cast<ConstantInt>(B); 761 if (!BCst) return nullptr; 762 ConstantInt *DCst = dyn_cast<ConstantInt>(D); 763 if (!DCst) return nullptr; 764 765 if (Mask & (FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_BMask_NotAllOnes)) { 766 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and 767 // (icmp ne (A & B), B) & (icmp ne (A & D), D) 768 // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0) 769 // Only valid if one of the masks is a superset of the other (check "B&D" is 770 // the same as either B or D). 771 APInt NewMask = BCst->getValue() & DCst->getValue(); 772 773 if (NewMask == BCst->getValue()) 774 return LHS; 775 else if (NewMask == DCst->getValue()) 776 return RHS; 777 } 778 if (Mask & FoldMskICmp_AMask_NotAllOnes) { 779 // (icmp ne (A & B), B) & (icmp ne (A & D), D) 780 // -> (icmp ne (A & B), A) or (icmp ne (A & D), A) 781 // Only valid if one of the masks is a superset of the other (check "B|D" is 782 // the same as either B or D). 783 APInt NewMask = BCst->getValue() | DCst->getValue(); 784 785 if (NewMask == BCst->getValue()) 786 return LHS; 787 else if (NewMask == DCst->getValue()) 788 return RHS; 789 } 790 if (Mask & FoldMskICmp_BMask_Mixed) { 791 // (icmp eq (A & B), C) & (icmp eq (A & D), E) 792 // We already know that B & C == C && D & E == E. 793 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of 794 // C and E, which are shared by both the mask B and the mask D, don't 795 // contradict, then we can transform to 796 // -> (icmp eq (A & (B|D)), (C|E)) 797 // Currently, we only handle the case of B, C, D, and E being constant. 798 // We can't simply use C and E because we might actually handle 799 // (icmp ne (A & B), B) & (icmp eq (A & D), D) 800 // with B and D, having a single bit set. 801 ConstantInt *CCst = dyn_cast<ConstantInt>(C); 802 if (!CCst) return nullptr; 803 ConstantInt *ECst = dyn_cast<ConstantInt>(E); 804 if (!ECst) return nullptr; 805 if (LHSCC != NewCC) 806 CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst)); 807 if (RHSCC != NewCC) 808 ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst)); 809 // If there is a conflict, we should actually return a false for the 810 // whole construct. 811 if (((BCst->getValue() & DCst->getValue()) & 812 (CCst->getValue() ^ ECst->getValue())) != 0) 813 return ConstantInt::get(LHS->getType(), !IsAnd); 814 Value *NewOr1 = Builder->CreateOr(B, D); 815 Value *NewOr2 = ConstantExpr::getOr(CCst, ECst); 816 Value *NewAnd = Builder->CreateAnd(A, NewOr1); 817 return Builder->CreateICmp(NewCC, NewAnd, NewOr2); 818 } 819 return nullptr; 820 } 821 822 /// Try to fold a signed range checked with lower bound 0 to an unsigned icmp. 823 /// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n 824 /// If \p Inverted is true then the check is for the inverted range, e.g. 825 /// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n 826 Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, 827 bool Inverted) { 828 // Check the lower range comparison, e.g. x >= 0 829 // InstCombine already ensured that if there is a constant it's on the RHS. 830 ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1)); 831 if (!RangeStart) 832 return nullptr; 833 834 ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() : 835 Cmp0->getPredicate()); 836 837 // Accept x > -1 or x >= 0 (after potentially inverting the predicate). 838 if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) || 839 (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero()))) 840 return nullptr; 841 842 ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() : 843 Cmp1->getPredicate()); 844 845 Value *Input = Cmp0->getOperand(0); 846 Value *RangeEnd; 847 if (Cmp1->getOperand(0) == Input) { 848 // For the upper range compare we have: icmp x, n 849 RangeEnd = Cmp1->getOperand(1); 850 } else if (Cmp1->getOperand(1) == Input) { 851 // For the upper range compare we have: icmp n, x 852 RangeEnd = Cmp1->getOperand(0); 853 Pred1 = ICmpInst::getSwappedPredicate(Pred1); 854 } else { 855 return nullptr; 856 } 857 858 // Check the upper range comparison, e.g. x < n 859 ICmpInst::Predicate NewPred; 860 switch (Pred1) { 861 case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break; 862 case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break; 863 default: return nullptr; 864 } 865 866 // This simplification is only valid if the upper range is not negative. 867 bool IsNegative, IsNotNegative; 868 ComputeSignBit(RangeEnd, IsNotNegative, IsNegative, /*Depth=*/0, Cmp1); 869 if (!IsNotNegative) 870 return nullptr; 871 872 if (Inverted) 873 NewPred = ICmpInst::getInversePredicate(NewPred); 874 875 return Builder->CreateICmp(NewPred, Input, RangeEnd); 876 } 877 878 /// Fold (icmp)&(icmp) if possible. 879 Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) { 880 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate(); 881 882 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) 883 if (PredicatesFoldable(LHSCC, RHSCC)) { 884 if (LHS->getOperand(0) == RHS->getOperand(1) && 885 LHS->getOperand(1) == RHS->getOperand(0)) 886 LHS->swapOperands(); 887 if (LHS->getOperand(0) == RHS->getOperand(0) && 888 LHS->getOperand(1) == RHS->getOperand(1)) { 889 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); 890 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS); 891 bool isSigned = LHS->isSigned() || RHS->isSigned(); 892 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder); 893 } 894 } 895 896 // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E) 897 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder)) 898 return V; 899 900 // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n 901 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false)) 902 return V; 903 904 // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n 905 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false)) 906 return V; 907 908 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). 909 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0); 910 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1)); 911 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1)); 912 if (!LHSCst || !RHSCst) return nullptr; 913 914 if (LHSCst == RHSCst && LHSCC == RHSCC) { 915 // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) 916 // where C is a power of 2 or 917 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0) 918 if ((LHSCC == ICmpInst::ICMP_ULT && LHSCst->getValue().isPowerOf2()) || 919 (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero())) { 920 Value *NewOr = Builder->CreateOr(Val, Val2); 921 return Builder->CreateICmp(LHSCC, NewOr, LHSCst); 922 } 923 } 924 925 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2 926 // where CMAX is the all ones value for the truncated type, 927 // iff the lower bits of C2 and CA are zero. 928 if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC && 929 LHS->hasOneUse() && RHS->hasOneUse()) { 930 Value *V; 931 ConstantInt *AndCst, *SmallCst = nullptr, *BigCst = nullptr; 932 933 // (trunc x) == C1 & (and x, CA) == C2 934 // (and x, CA) == C2 & (trunc x) == C1 935 if (match(Val2, m_Trunc(m_Value(V))) && 936 match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) { 937 SmallCst = RHSCst; 938 BigCst = LHSCst; 939 } else if (match(Val, m_Trunc(m_Value(V))) && 940 match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) { 941 SmallCst = LHSCst; 942 BigCst = RHSCst; 943 } 944 945 if (SmallCst && BigCst) { 946 unsigned BigBitSize = BigCst->getType()->getBitWidth(); 947 unsigned SmallBitSize = SmallCst->getType()->getBitWidth(); 948 949 // Check that the low bits are zero. 950 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize); 951 if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) { 952 Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue()); 953 APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue(); 954 Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N); 955 return Builder->CreateICmp(LHSCC, NewAnd, NewVal); 956 } 957 } 958 } 959 960 // From here on, we only handle: 961 // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler. 962 if (Val != Val2) return nullptr; 963 964 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. 965 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || 966 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || 967 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || 968 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) 969 return nullptr; 970 971 // We can't fold (ugt x, C) & (sgt x, C2). 972 if (!PredicatesFoldable(LHSCC, RHSCC)) 973 return nullptr; 974 975 // Ensure that the larger constant is on the RHS. 976 bool ShouldSwap; 977 if (CmpInst::isSigned(LHSCC) || 978 (ICmpInst::isEquality(LHSCC) && 979 CmpInst::isSigned(RHSCC))) 980 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); 981 else 982 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); 983 984 if (ShouldSwap) { 985 std::swap(LHS, RHS); 986 std::swap(LHSCst, RHSCst); 987 std::swap(LHSCC, RHSCC); 988 } 989 990 // At this point, we know we have two icmp instructions 991 // comparing a value against two constants and and'ing the result 992 // together. Because of the above check, we know that we only have 993 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know 994 // (from the icmp folding check above), that the two constants 995 // are not equal and that the larger constant is on the RHS 996 assert(LHSCst != RHSCst && "Compares not folded above?"); 997 998 switch (LHSCC) { 999 default: llvm_unreachable("Unknown integer condition code!"); 1000 case ICmpInst::ICMP_EQ: 1001 switch (RHSCC) { 1002 default: llvm_unreachable("Unknown integer condition code!"); 1003 case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 1004 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 1005 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 1006 return LHS; 1007 } 1008 case ICmpInst::ICMP_NE: 1009 switch (RHSCC) { 1010 default: llvm_unreachable("Unknown integer condition code!"); 1011 case ICmpInst::ICMP_ULT: 1012 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 1013 return Builder->CreateICmpULT(Val, LHSCst); 1014 if (LHSCst->isNullValue()) // (X != 0 & X u< 14) -> X-1 u< 13 1015 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true); 1016 break; // (X != 13 & X u< 15) -> no change 1017 case ICmpInst::ICMP_SLT: 1018 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 1019 return Builder->CreateICmpSLT(Val, LHSCst); 1020 break; // (X != 13 & X s< 15) -> no change 1021 case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 1022 case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 1023 case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 1024 return RHS; 1025 case ICmpInst::ICMP_NE: 1026 // Special case to get the ordering right when the values wrap around 1027 // zero. 1028 if (LHSCst->getValue() == 0 && RHSCst->getValue().isAllOnesValue()) 1029 std::swap(LHSCst, RHSCst); 1030 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 1031 Constant *AddCST = ConstantExpr::getNeg(LHSCst); 1032 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off"); 1033 return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1), 1034 Val->getName()+".cmp"); 1035 } 1036 break; // (X != 13 & X != 15) -> no change 1037 } 1038 break; 1039 case ICmpInst::ICMP_ULT: 1040 switch (RHSCC) { 1041 default: llvm_unreachable("Unknown integer condition code!"); 1042 case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false 1043 case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false 1044 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0); 1045 case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change 1046 break; 1047 case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 1048 case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 1049 return LHS; 1050 case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change 1051 break; 1052 } 1053 break; 1054 case ICmpInst::ICMP_SLT: 1055 switch (RHSCC) { 1056 default: llvm_unreachable("Unknown integer condition code!"); 1057 case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change 1058 break; 1059 case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 1060 case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 1061 return LHS; 1062 case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change 1063 break; 1064 } 1065 break; 1066 case ICmpInst::ICMP_UGT: 1067 switch (RHSCC) { 1068 default: llvm_unreachable("Unknown integer condition code!"); 1069 case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15 1070 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 1071 return RHS; 1072 case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change 1073 break; 1074 case ICmpInst::ICMP_NE: 1075 if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 1076 return Builder->CreateICmp(LHSCC, Val, RHSCst); 1077 break; // (X u> 13 & X != 15) -> no change 1078 case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1 1079 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true); 1080 case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change 1081 break; 1082 } 1083 break; 1084 case ICmpInst::ICMP_SGT: 1085 switch (RHSCC) { 1086 default: llvm_unreachable("Unknown integer condition code!"); 1087 case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 1088 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 1089 return RHS; 1090 case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change 1091 break; 1092 case ICmpInst::ICMP_NE: 1093 if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 1094 return Builder->CreateICmp(LHSCC, Val, RHSCst); 1095 break; // (X s> 13 & X != 15) -> no change 1096 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1 1097 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true); 1098 case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change 1099 break; 1100 } 1101 break; 1102 } 1103 1104 return nullptr; 1105 } 1106 1107 /// Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of instcombine, this returns 1108 /// a Value which should already be inserted into the function. 1109 Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) { 1110 if (LHS->getPredicate() == FCmpInst::FCMP_ORD && 1111 RHS->getPredicate() == FCmpInst::FCMP_ORD) { 1112 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) 1113 return nullptr; 1114 1115 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) 1116 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) 1117 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { 1118 // If either of the constants are nans, then the whole thing returns 1119 // false. 1120 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) 1121 return Builder->getFalse(); 1122 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0)); 1123 } 1124 1125 // Handle vector zeros. This occurs because the canonical form of 1126 // "fcmp ord x,x" is "fcmp ord x, 0". 1127 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && 1128 isa<ConstantAggregateZero>(RHS->getOperand(1))) 1129 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0)); 1130 return nullptr; 1131 } 1132 1133 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); 1134 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); 1135 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); 1136 1137 1138 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { 1139 // Swap RHS operands to match LHS. 1140 Op1CC = FCmpInst::getSwappedPredicate(Op1CC); 1141 std::swap(Op1LHS, Op1RHS); 1142 } 1143 1144 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { 1145 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y). 1146 if (Op0CC == Op1CC) 1147 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS); 1148 if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE) 1149 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0); 1150 if (Op0CC == FCmpInst::FCMP_TRUE) 1151 return RHS; 1152 if (Op1CC == FCmpInst::FCMP_TRUE) 1153 return LHS; 1154 1155 bool Op0Ordered; 1156 bool Op1Ordered; 1157 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); 1158 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); 1159 // uno && ord -> false 1160 if (Op0Pred == 0 && Op1Pred == 0 && Op0Ordered != Op1Ordered) 1161 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0); 1162 if (Op1Pred == 0) { 1163 std::swap(LHS, RHS); 1164 std::swap(Op0Pred, Op1Pred); 1165 std::swap(Op0Ordered, Op1Ordered); 1166 } 1167 if (Op0Pred == 0) { 1168 // uno && ueq -> uno && (uno || eq) -> uno 1169 // ord && olt -> ord && (ord && lt) -> olt 1170 if (!Op0Ordered && (Op0Ordered == Op1Ordered)) 1171 return LHS; 1172 if (Op0Ordered && (Op0Ordered == Op1Ordered)) 1173 return RHS; 1174 1175 // uno && oeq -> uno && (ord && eq) -> false 1176 if (!Op0Ordered) 1177 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0); 1178 // ord && ueq -> ord && (uno || eq) -> oeq 1179 return getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS, Builder); 1180 } 1181 } 1182 1183 return nullptr; 1184 } 1185 1186 /// Match De Morgan's Laws: 1187 /// (~A & ~B) == (~(A | B)) 1188 /// (~A | ~B) == (~(A & B)) 1189 static Instruction *matchDeMorgansLaws(BinaryOperator &I, 1190 InstCombiner::BuilderTy *Builder) { 1191 auto Opcode = I.getOpcode(); 1192 assert((Opcode == Instruction::And || Opcode == Instruction::Or) && 1193 "Trying to match De Morgan's Laws with something other than and/or"); 1194 // Flip the logic operation. 1195 if (Opcode == Instruction::And) 1196 Opcode = Instruction::Or; 1197 else 1198 Opcode = Instruction::And; 1199 1200 Value *Op0 = I.getOperand(0); 1201 Value *Op1 = I.getOperand(1); 1202 // TODO: Use pattern matchers instead of dyn_cast. 1203 if (Value *Op0NotVal = dyn_castNotVal(Op0)) 1204 if (Value *Op1NotVal = dyn_castNotVal(Op1)) 1205 if (Op0->hasOneUse() && Op1->hasOneUse()) { 1206 Value *LogicOp = Builder->CreateBinOp(Opcode, Op0NotVal, Op1NotVal, 1207 I.getName() + ".demorgan"); 1208 return BinaryOperator::CreateNot(LogicOp); 1209 } 1210 1211 // De Morgan's Law in disguise: 1212 // (zext(bool A) ^ 1) & (zext(bool B) ^ 1) -> zext(~(A | B)) 1213 // (zext(bool A) ^ 1) | (zext(bool B) ^ 1) -> zext(~(A & B)) 1214 Value *A = nullptr; 1215 Value *B = nullptr; 1216 ConstantInt *C1 = nullptr; 1217 if (match(Op0, m_OneUse(m_Xor(m_ZExt(m_Value(A)), m_ConstantInt(C1)))) && 1218 match(Op1, m_OneUse(m_Xor(m_ZExt(m_Value(B)), m_Specific(C1))))) { 1219 // TODO: This check could be loosened to handle different type sizes. 1220 // Alternatively, we could fix the definition of m_Not to recognize a not 1221 // operation hidden by a zext? 1222 if (A->getType()->isIntegerTy(1) && B->getType()->isIntegerTy(1) && 1223 C1->isOne()) { 1224 Value *LogicOp = Builder->CreateBinOp(Opcode, A, B, 1225 I.getName() + ".demorgan"); 1226 Value *Not = Builder->CreateNot(LogicOp); 1227 return CastInst::CreateZExtOrBitCast(Not, I.getType()); 1228 } 1229 } 1230 1231 return nullptr; 1232 } 1233 1234 Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) { 1235 auto LogicOpc = I.getOpcode(); 1236 assert((LogicOpc == Instruction::And || LogicOpc == Instruction::Or || 1237 LogicOpc == Instruction::Xor) && 1238 "Unexpected opcode for bitwise logic folding"); 1239 1240 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1241 CastInst *Cast0 = dyn_cast<CastInst>(Op0); 1242 if (!Cast0) 1243 return nullptr; 1244 1245 // This must be a cast from an integer or integer vector source type to allow 1246 // transformation of the logic operation to the source type. 1247 Type *DestTy = I.getType(); 1248 Type *SrcTy = Cast0->getSrcTy(); 1249 if (!SrcTy->isIntOrIntVectorTy()) 1250 return nullptr; 1251 1252 // If one operand is a bitcast and the other is a constant, move the logic 1253 // operation ahead of the bitcast. That is, do the logic operation in the 1254 // original type. This can eliminate useless bitcasts and allow normal 1255 // combines that would otherwise be impeded by the bitcast. Canonicalization 1256 // ensures that if there is a constant operand, it will be the second operand. 1257 Value *BC = nullptr; 1258 Constant *C = nullptr; 1259 if ((match(Op0, m_BitCast(m_Value(BC))) && match(Op1, m_Constant(C)))) { 1260 // A bitcast of a constant will be removed. 1261 Value *NewConstant = Builder->CreateBitCast(C, SrcTy); 1262 Value *NewOp = Builder->CreateBinOp(LogicOpc, BC, NewConstant, I.getName()); 1263 return CastInst::CreateBitOrPointerCast(NewOp, DestTy); 1264 } 1265 1266 CastInst *Cast1 = dyn_cast<CastInst>(Op1); 1267 if (!Cast1) 1268 return nullptr; 1269 1270 // Both operands of the logic operation are casts. The casts must be of the 1271 // same type for reduction. 1272 auto CastOpcode = Cast0->getOpcode(); 1273 if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy()) 1274 return nullptr; 1275 1276 Value *Cast0Src = Cast0->getOperand(0); 1277 Value *Cast1Src = Cast1->getOperand(0); 1278 1279 // fold (logic (cast A), (cast B)) -> (cast (logic A, B)) 1280 1281 // Only do this if the casts both really cause code to be generated. 1282 if ((!isa<ICmpInst>(Cast0Src) || !isa<ICmpInst>(Cast1Src)) && 1283 ShouldOptimizeCast(CastOpcode, Cast0Src, DestTy) && 1284 ShouldOptimizeCast(CastOpcode, Cast1Src, DestTy)) { 1285 Value *NewOp = Builder->CreateBinOp(LogicOpc, Cast0Src, Cast1Src, 1286 I.getName()); 1287 return CastInst::Create(CastOpcode, NewOp, DestTy); 1288 } 1289 1290 // For now, only 'and'/'or' have optimizations after this. 1291 if (LogicOpc == Instruction::Xor) 1292 return nullptr; 1293 1294 // If this is logic(cast(icmp), cast(icmp)), try to fold this even if the 1295 // cast is otherwise not optimizable. This happens for vector sexts. 1296 ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src); 1297 ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src); 1298 if (ICmp0 && ICmp1) { 1299 Value *Res = LogicOpc == Instruction::And ? FoldAndOfICmps(ICmp0, ICmp1) 1300 : FoldOrOfICmps(ICmp0, ICmp1, &I); 1301 if (Res) 1302 return CastInst::Create(CastOpcode, Res, DestTy); 1303 return nullptr; 1304 } 1305 1306 // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the 1307 // cast is otherwise not optimizable. This happens for vector sexts. 1308 FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src); 1309 FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src); 1310 if (FCmp0 && FCmp1) { 1311 Value *Res = LogicOpc == Instruction::And ? FoldAndOfFCmps(FCmp0, FCmp1) 1312 : FoldOrOfFCmps(FCmp0, FCmp1); 1313 if (Res) 1314 return CastInst::Create(CastOpcode, Res, DestTy); 1315 return nullptr; 1316 } 1317 1318 return nullptr; 1319 } 1320 1321 static Instruction *foldBoolSextMaskToSelect(BinaryOperator &I) { 1322 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1323 1324 // Canonicalize SExt or Not to the LHS 1325 if (match(Op1, m_SExt(m_Value())) || match(Op1, m_Not(m_Value()))) { 1326 std::swap(Op0, Op1); 1327 } 1328 1329 // Fold (and (sext bool to A), B) --> (select bool, B, 0) 1330 Value *X = nullptr; 1331 if (match(Op0, m_SExt(m_Value(X))) && 1332 X->getType()->getScalarType()->isIntegerTy(1)) { 1333 Value *Zero = Constant::getNullValue(Op1->getType()); 1334 return SelectInst::Create(X, Op1, Zero); 1335 } 1336 1337 // Fold (and ~(sext bool to A), B) --> (select bool, 0, B) 1338 if (match(Op0, m_Not(m_SExt(m_Value(X)))) && 1339 X->getType()->getScalarType()->isIntegerTy(1)) { 1340 Value *Zero = Constant::getNullValue(Op0->getType()); 1341 return SelectInst::Create(X, Zero, Op1); 1342 } 1343 1344 return nullptr; 1345 } 1346 1347 Instruction *InstCombiner::visitAnd(BinaryOperator &I) { 1348 bool Changed = SimplifyAssociativeOrCommutative(I); 1349 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1350 1351 if (Value *V = SimplifyVectorOp(I)) 1352 return replaceInstUsesWith(I, V); 1353 1354 if (Value *V = SimplifyAndInst(Op0, Op1, DL, TLI, DT, AC)) 1355 return replaceInstUsesWith(I, V); 1356 1357 // (A|B)&(A|C) -> A|(B&C) etc 1358 if (Value *V = SimplifyUsingDistributiveLaws(I)) 1359 return replaceInstUsesWith(I, V); 1360 1361 // See if we can simplify any instructions used by the instruction whose sole 1362 // purpose is to compute bits we don't care about. 1363 if (SimplifyDemandedInstructionBits(I)) 1364 return &I; 1365 1366 if (Value *V = SimplifyBSwap(I)) 1367 return replaceInstUsesWith(I, V); 1368 1369 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { 1370 const APInt &AndRHSMask = AndRHS->getValue(); 1371 1372 // Optimize a variety of ((val OP C1) & C2) combinations... 1373 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { 1374 Value *Op0LHS = Op0I->getOperand(0); 1375 Value *Op0RHS = Op0I->getOperand(1); 1376 switch (Op0I->getOpcode()) { 1377 default: break; 1378 case Instruction::Xor: 1379 case Instruction::Or: { 1380 // If the mask is only needed on one incoming arm, push it up. 1381 if (!Op0I->hasOneUse()) break; 1382 1383 APInt NotAndRHS(~AndRHSMask); 1384 if (MaskedValueIsZero(Op0LHS, NotAndRHS, 0, &I)) { 1385 // Not masking anything out for the LHS, move to RHS. 1386 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS, 1387 Op0RHS->getName()+".masked"); 1388 return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS); 1389 } 1390 if (!isa<Constant>(Op0RHS) && 1391 MaskedValueIsZero(Op0RHS, NotAndRHS, 0, &I)) { 1392 // Not masking anything out for the RHS, move to LHS. 1393 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS, 1394 Op0LHS->getName()+".masked"); 1395 return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS); 1396 } 1397 1398 break; 1399 } 1400 case Instruction::Add: 1401 // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. 1402 // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 1403 // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 1404 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) 1405 return BinaryOperator::CreateAnd(V, AndRHS); 1406 if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) 1407 return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes 1408 break; 1409 1410 case Instruction::Sub: 1411 // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. 1412 // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 1413 // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 1414 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) 1415 return BinaryOperator::CreateAnd(V, AndRHS); 1416 1417 // -x & 1 -> x & 1 1418 if (AndRHSMask == 1 && match(Op0LHS, m_Zero())) 1419 return BinaryOperator::CreateAnd(Op0RHS, AndRHS); 1420 1421 // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS 1422 // has 1's for all bits that the subtraction with A might affect. 1423 if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) { 1424 uint32_t BitWidth = AndRHSMask.getBitWidth(); 1425 uint32_t Zeros = AndRHSMask.countLeadingZeros(); 1426 APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros); 1427 1428 if (MaskedValueIsZero(Op0LHS, Mask, 0, &I)) { 1429 Value *NewNeg = Builder->CreateNeg(Op0RHS); 1430 return BinaryOperator::CreateAnd(NewNeg, AndRHS); 1431 } 1432 } 1433 break; 1434 1435 case Instruction::Shl: 1436 case Instruction::LShr: 1437 // (1 << x) & 1 --> zext(x == 0) 1438 // (1 >> x) & 1 --> zext(x == 0) 1439 if (AndRHSMask == 1 && Op0LHS == AndRHS) { 1440 Value *NewICmp = 1441 Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType())); 1442 return new ZExtInst(NewICmp, I.getType()); 1443 } 1444 break; 1445 } 1446 1447 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) 1448 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) 1449 return Res; 1450 } 1451 1452 // If this is an integer truncation, and if the source is an 'and' with 1453 // immediate, transform it. This frequently occurs for bitfield accesses. 1454 { 1455 Value *X = nullptr; ConstantInt *YC = nullptr; 1456 if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) { 1457 // Change: and (trunc (and X, YC) to T), C2 1458 // into : and (trunc X to T), trunc(YC) & C2 1459 // This will fold the two constants together, which may allow 1460 // other simplifications. 1461 Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk"); 1462 Constant *C3 = ConstantExpr::getTrunc(YC, I.getType()); 1463 C3 = ConstantExpr::getAnd(C3, AndRHS); 1464 return BinaryOperator::CreateAnd(NewCast, C3); 1465 } 1466 } 1467 1468 // Try to fold constant and into select arguments. 1469 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 1470 if (Instruction *R = FoldOpIntoSelect(I, SI)) 1471 return R; 1472 if (isa<PHINode>(Op0)) 1473 if (Instruction *NV = FoldOpIntoPhi(I)) 1474 return NV; 1475 } 1476 1477 if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder)) 1478 return DeMorgan; 1479 1480 { 1481 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; 1482 // (A|B) & ~(A&B) -> A^B 1483 if (match(Op0, m_Or(m_Value(A), m_Value(B))) && 1484 match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) && 1485 ((A == C && B == D) || (A == D && B == C))) 1486 return BinaryOperator::CreateXor(A, B); 1487 1488 // ~(A&B) & (A|B) -> A^B 1489 if (match(Op1, m_Or(m_Value(A), m_Value(B))) && 1490 match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) && 1491 ((A == C && B == D) || (A == D && B == C))) 1492 return BinaryOperator::CreateXor(A, B); 1493 1494 // A&(A^B) => A & ~B 1495 { 1496 Value *tmpOp0 = Op0; 1497 Value *tmpOp1 = Op1; 1498 if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) { 1499 if (A == Op1 || B == Op1 ) { 1500 tmpOp1 = Op0; 1501 tmpOp0 = Op1; 1502 // Simplify below 1503 } 1504 } 1505 1506 if (match(tmpOp1, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) { 1507 if (B == tmpOp0) { 1508 std::swap(A, B); 1509 } 1510 // Notice that the pattern (A&(~B)) is actually (A&(-1^B)), so if 1511 // A is originally -1 (or a vector of -1 and undefs), then we enter 1512 // an endless loop. By checking that A is non-constant we ensure that 1513 // we will never get to the loop. 1514 if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B 1515 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B)); 1516 } 1517 } 1518 1519 // (A&((~A)|B)) -> A&B 1520 if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) || 1521 match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))))) 1522 return BinaryOperator::CreateAnd(A, Op1); 1523 if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) || 1524 match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))))) 1525 return BinaryOperator::CreateAnd(A, Op0); 1526 1527 // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C 1528 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) 1529 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) 1530 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse()) 1531 return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C)); 1532 1533 // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C 1534 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B)))) 1535 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) 1536 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse()) 1537 return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C)); 1538 1539 // (A | B) & ((~A) ^ B) -> (A & B) 1540 if (match(Op0, m_Or(m_Value(A), m_Value(B))) && 1541 match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B)))) 1542 return BinaryOperator::CreateAnd(A, B); 1543 1544 // ((~A) ^ B) & (A | B) -> (A & B) 1545 if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) && 1546 match(Op1, m_Or(m_Specific(A), m_Specific(B)))) 1547 return BinaryOperator::CreateAnd(A, B); 1548 } 1549 1550 { 1551 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0); 1552 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1); 1553 if (LHS && RHS) 1554 if (Value *Res = FoldAndOfICmps(LHS, RHS)) 1555 return replaceInstUsesWith(I, Res); 1556 1557 // TODO: Make this recursive; it's a little tricky because an arbitrary 1558 // number of 'and' instructions might have to be created. 1559 Value *X, *Y; 1560 if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) { 1561 if (auto *Cmp = dyn_cast<ICmpInst>(X)) 1562 if (Value *Res = FoldAndOfICmps(LHS, Cmp)) 1563 return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y)); 1564 if (auto *Cmp = dyn_cast<ICmpInst>(Y)) 1565 if (Value *Res = FoldAndOfICmps(LHS, Cmp)) 1566 return replaceInstUsesWith(I, Builder->CreateAnd(Res, X)); 1567 } 1568 if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) { 1569 if (auto *Cmp = dyn_cast<ICmpInst>(X)) 1570 if (Value *Res = FoldAndOfICmps(Cmp, RHS)) 1571 return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y)); 1572 if (auto *Cmp = dyn_cast<ICmpInst>(Y)) 1573 if (Value *Res = FoldAndOfICmps(Cmp, RHS)) 1574 return replaceInstUsesWith(I, Builder->CreateAnd(Res, X)); 1575 } 1576 } 1577 1578 // If and'ing two fcmp, try combine them into one. 1579 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) 1580 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) 1581 if (Value *Res = FoldAndOfFCmps(LHS, RHS)) 1582 return replaceInstUsesWith(I, Res); 1583 1584 if (Instruction *CastedAnd = foldCastedBitwiseLogic(I)) 1585 return CastedAnd; 1586 1587 if (Instruction *Select = foldBoolSextMaskToSelect(I)) 1588 return Select; 1589 1590 return Changed ? &I : nullptr; 1591 } 1592 1593 /// Given an OR instruction, check to see if this is a bswap idiom. If so, 1594 /// insert the new intrinsic and return it. 1595 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { 1596 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 1597 1598 // Look through zero extends. 1599 if (Instruction *Ext = dyn_cast<ZExtInst>(Op0)) 1600 Op0 = Ext->getOperand(0); 1601 1602 if (Instruction *Ext = dyn_cast<ZExtInst>(Op1)) 1603 Op1 = Ext->getOperand(0); 1604 1605 // (A | B) | C and A | (B | C) -> bswap if possible. 1606 bool OrOfOrs = match(Op0, m_Or(m_Value(), m_Value())) || 1607 match(Op1, m_Or(m_Value(), m_Value())); 1608 1609 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. 1610 bool OrOfShifts = match(Op0, m_LogicalShift(m_Value(), m_Value())) && 1611 match(Op1, m_LogicalShift(m_Value(), m_Value())); 1612 1613 // (A & B) | (C & D) -> bswap if possible. 1614 bool OrOfAnds = match(Op0, m_And(m_Value(), m_Value())) && 1615 match(Op1, m_And(m_Value(), m_Value())); 1616 1617 if (!OrOfOrs && !OrOfShifts && !OrOfAnds) 1618 return nullptr; 1619 1620 SmallVector<Instruction*, 4> Insts; 1621 if (!recognizeBSwapOrBitReverseIdiom(&I, true, false, Insts)) 1622 return nullptr; 1623 Instruction *LastInst = Insts.pop_back_val(); 1624 LastInst->removeFromParent(); 1625 1626 for (auto *Inst : Insts) 1627 Worklist.Add(Inst); 1628 return LastInst; 1629 } 1630 1631 /// We have an expression of the form (A & C) | (B & D). If A is (Cond?-1:0) 1632 /// and B is ~(Cond?-1,0), then simplify this expression to "Cond ? C : D". 1633 static Value *matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D, 1634 InstCombiner::BuilderTy &Builder) { 1635 // The potential condition of the select may be bitcasted. In that case, look 1636 // through its bitcast and the corresponding bitcast of the 'not' condition. 1637 Type *OrigType = A->getType(); 1638 Value *SrcA, *SrcB; 1639 if (match(A, m_BitCast(m_Value(SrcA))) && 1640 match(B, m_BitCast(m_Value(SrcB)))) { 1641 A = SrcA; 1642 B = SrcB; 1643 } 1644 1645 // The condition must be a value of -1/0, and B must be the 'not' of that 1646 // condition. 1647 Value *Cond; 1648 if (match(A, m_SExt(m_Value(Cond))) && 1649 Cond->getType()->getScalarType()->isIntegerTy(1) && 1650 match(B, m_CombineOr(m_Not(m_SExt(m_Specific(Cond))), 1651 m_SExt(m_Not(m_Specific(Cond)))))) { 1652 // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D)) 1653 // The bitcasts will either all exist or all not exist. The builder will 1654 // not create unnecessary casts if the types already match. 1655 Value *BitcastC = Builder.CreateBitCast(C, A->getType()); 1656 Value *BitcastD = Builder.CreateBitCast(D, A->getType()); 1657 Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD); 1658 return Builder.CreateBitCast(Select, OrigType); 1659 } 1660 1661 return nullptr; 1662 } 1663 1664 /// Fold (icmp)|(icmp) if possible. 1665 Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, 1666 Instruction *CxtI) { 1667 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate(); 1668 1669 // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2) 1670 // if K1 and K2 are a one-bit mask. 1671 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1)); 1672 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1)); 1673 1674 if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero() && 1675 RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) { 1676 1677 BinaryOperator *LAnd = dyn_cast<BinaryOperator>(LHS->getOperand(0)); 1678 BinaryOperator *RAnd = dyn_cast<BinaryOperator>(RHS->getOperand(0)); 1679 if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() && 1680 LAnd->getOpcode() == Instruction::And && 1681 RAnd->getOpcode() == Instruction::And) { 1682 1683 Value *Mask = nullptr; 1684 Value *Masked = nullptr; 1685 if (LAnd->getOperand(0) == RAnd->getOperand(0) && 1686 isKnownToBeAPowerOfTwo(LAnd->getOperand(1), DL, false, 0, AC, CxtI, 1687 DT) && 1688 isKnownToBeAPowerOfTwo(RAnd->getOperand(1), DL, false, 0, AC, CxtI, 1689 DT)) { 1690 Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1)); 1691 Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask); 1692 } else if (LAnd->getOperand(1) == RAnd->getOperand(1) && 1693 isKnownToBeAPowerOfTwo(LAnd->getOperand(0), DL, false, 0, AC, 1694 CxtI, DT) && 1695 isKnownToBeAPowerOfTwo(RAnd->getOperand(0), DL, false, 0, AC, 1696 CxtI, DT)) { 1697 Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0)); 1698 Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask); 1699 } 1700 1701 if (Masked) 1702 return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask); 1703 } 1704 } 1705 1706 // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3) 1707 // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3) 1708 // The original condition actually refers to the following two ranges: 1709 // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3] 1710 // We can fold these two ranges if: 1711 // 1) C1 and C2 is unsigned greater than C3. 1712 // 2) The two ranges are separated. 1713 // 3) C1 ^ C2 is one-bit mask. 1714 // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask. 1715 // This implies all values in the two ranges differ by exactly one bit. 1716 1717 if ((LHSCC == ICmpInst::ICMP_ULT || LHSCC == ICmpInst::ICMP_ULE) && 1718 LHSCC == RHSCC && LHSCst && RHSCst && LHS->hasOneUse() && 1719 RHS->hasOneUse() && LHSCst->getType() == RHSCst->getType() && 1720 LHSCst->getValue() == (RHSCst->getValue())) { 1721 1722 Value *LAdd = LHS->getOperand(0); 1723 Value *RAdd = RHS->getOperand(0); 1724 1725 Value *LAddOpnd, *RAddOpnd; 1726 ConstantInt *LAddCst, *RAddCst; 1727 if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddCst))) && 1728 match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddCst))) && 1729 LAddCst->getValue().ugt(LHSCst->getValue()) && 1730 RAddCst->getValue().ugt(LHSCst->getValue())) { 1731 1732 APInt DiffCst = LAddCst->getValue() ^ RAddCst->getValue(); 1733 if (LAddOpnd == RAddOpnd && DiffCst.isPowerOf2()) { 1734 ConstantInt *MaxAddCst = nullptr; 1735 if (LAddCst->getValue().ult(RAddCst->getValue())) 1736 MaxAddCst = RAddCst; 1737 else 1738 MaxAddCst = LAddCst; 1739 1740 APInt RRangeLow = -RAddCst->getValue(); 1741 APInt RRangeHigh = RRangeLow + LHSCst->getValue(); 1742 APInt LRangeLow = -LAddCst->getValue(); 1743 APInt LRangeHigh = LRangeLow + LHSCst->getValue(); 1744 APInt LowRangeDiff = RRangeLow ^ LRangeLow; 1745 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh; 1746 APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow 1747 : RRangeLow - LRangeLow; 1748 1749 if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff && 1750 RangeDiff.ugt(LHSCst->getValue())) { 1751 Value *MaskCst = ConstantInt::get(LAddCst->getType(), ~DiffCst); 1752 1753 Value *NewAnd = Builder->CreateAnd(LAddOpnd, MaskCst); 1754 Value *NewAdd = Builder->CreateAdd(NewAnd, MaxAddCst); 1755 return (Builder->CreateICmp(LHS->getPredicate(), NewAdd, LHSCst)); 1756 } 1757 } 1758 } 1759 } 1760 1761 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) 1762 if (PredicatesFoldable(LHSCC, RHSCC)) { 1763 if (LHS->getOperand(0) == RHS->getOperand(1) && 1764 LHS->getOperand(1) == RHS->getOperand(0)) 1765 LHS->swapOperands(); 1766 if (LHS->getOperand(0) == RHS->getOperand(0) && 1767 LHS->getOperand(1) == RHS->getOperand(1)) { 1768 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); 1769 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS); 1770 bool isSigned = LHS->isSigned() || RHS->isSigned(); 1771 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder); 1772 } 1773 } 1774 1775 // handle (roughly): 1776 // (icmp ne (A & B), C) | (icmp ne (A & D), E) 1777 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder)) 1778 return V; 1779 1780 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0); 1781 if (LHS->hasOneUse() || RHS->hasOneUse()) { 1782 // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1) 1783 // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1) 1784 Value *A = nullptr, *B = nullptr; 1785 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero()) { 1786 B = Val; 1787 if (RHSCC == ICmpInst::ICMP_ULT && Val == RHS->getOperand(1)) 1788 A = Val2; 1789 else if (RHSCC == ICmpInst::ICMP_UGT && Val == Val2) 1790 A = RHS->getOperand(1); 1791 } 1792 // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1) 1793 // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1) 1794 else if (RHSCC == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) { 1795 B = Val2; 1796 if (LHSCC == ICmpInst::ICMP_ULT && Val2 == LHS->getOperand(1)) 1797 A = Val; 1798 else if (LHSCC == ICmpInst::ICMP_UGT && Val2 == Val) 1799 A = LHS->getOperand(1); 1800 } 1801 if (A && B) 1802 return Builder->CreateICmp( 1803 ICmpInst::ICMP_UGE, 1804 Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A); 1805 } 1806 1807 // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n 1808 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true)) 1809 return V; 1810 1811 // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n 1812 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true)) 1813 return V; 1814 1815 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). 1816 if (!LHSCst || !RHSCst) return nullptr; 1817 1818 if (LHSCst == RHSCst && LHSCC == RHSCC) { 1819 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0) 1820 if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) { 1821 Value *NewOr = Builder->CreateOr(Val, Val2); 1822 return Builder->CreateICmp(LHSCC, NewOr, LHSCst); 1823 } 1824 } 1825 1826 // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1) 1827 // iff C2 + CA == C1. 1828 if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) { 1829 ConstantInt *AddCst; 1830 if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst)))) 1831 if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue()) 1832 return Builder->CreateICmpULE(Val, LHSCst); 1833 } 1834 1835 // From here on, we only handle: 1836 // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler. 1837 if (Val != Val2) return nullptr; 1838 1839 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. 1840 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || 1841 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || 1842 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || 1843 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) 1844 return nullptr; 1845 1846 // We can't fold (ugt x, C) | (sgt x, C2). 1847 if (!PredicatesFoldable(LHSCC, RHSCC)) 1848 return nullptr; 1849 1850 // Ensure that the larger constant is on the RHS. 1851 bool ShouldSwap; 1852 if (CmpInst::isSigned(LHSCC) || 1853 (ICmpInst::isEquality(LHSCC) && 1854 CmpInst::isSigned(RHSCC))) 1855 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); 1856 else 1857 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); 1858 1859 if (ShouldSwap) { 1860 std::swap(LHS, RHS); 1861 std::swap(LHSCst, RHSCst); 1862 std::swap(LHSCC, RHSCC); 1863 } 1864 1865 // At this point, we know we have two icmp instructions 1866 // comparing a value against two constants and or'ing the result 1867 // together. Because of the above check, we know that we only have 1868 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the 1869 // icmp folding check above), that the two constants are not 1870 // equal. 1871 assert(LHSCst != RHSCst && "Compares not folded above?"); 1872 1873 switch (LHSCC) { 1874 default: llvm_unreachable("Unknown integer condition code!"); 1875 case ICmpInst::ICMP_EQ: 1876 switch (RHSCC) { 1877 default: llvm_unreachable("Unknown integer condition code!"); 1878 case ICmpInst::ICMP_EQ: 1879 if (LHS->getOperand(0) == RHS->getOperand(0)) { 1880 // if LHSCst and RHSCst differ only by one bit: 1881 // (A == C1 || A == C2) -> (A | (C1 ^ C2)) == C2 1882 assert(LHSCst->getValue().ule(LHSCst->getValue())); 1883 1884 APInt Xor = LHSCst->getValue() ^ RHSCst->getValue(); 1885 if (Xor.isPowerOf2()) { 1886 Value *Cst = Builder->getInt(Xor); 1887 Value *Or = Builder->CreateOr(LHS->getOperand(0), Cst); 1888 return Builder->CreateICmp(ICmpInst::ICMP_EQ, Or, RHSCst); 1889 } 1890 } 1891 1892 if (LHSCst == SubOne(RHSCst)) { 1893 // (X == 13 | X == 14) -> X-13 <u 2 1894 Constant *AddCST = ConstantExpr::getNeg(LHSCst); 1895 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off"); 1896 AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); 1897 return Builder->CreateICmpULT(Add, AddCST); 1898 } 1899 1900 break; // (X == 13 | X == 15) -> no change 1901 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change 1902 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change 1903 break; 1904 case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 1905 case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 1906 case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 1907 return RHS; 1908 } 1909 break; 1910 case ICmpInst::ICMP_NE: 1911 switch (RHSCC) { 1912 default: llvm_unreachable("Unknown integer condition code!"); 1913 case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 1914 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 1915 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 1916 return LHS; 1917 case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true 1918 case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true 1919 case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true 1920 return Builder->getTrue(); 1921 } 1922 case ICmpInst::ICMP_ULT: 1923 switch (RHSCC) { 1924 default: llvm_unreachable("Unknown integer condition code!"); 1925 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change 1926 break; 1927 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2 1928 // If RHSCst is [us]MAXINT, it is always false. Not handling 1929 // this can cause overflow. 1930 if (RHSCst->isMaxValue(false)) 1931 return LHS; 1932 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false); 1933 case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change 1934 break; 1935 case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 1936 case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 1937 return RHS; 1938 case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change 1939 break; 1940 } 1941 break; 1942 case ICmpInst::ICMP_SLT: 1943 switch (RHSCC) { 1944 default: llvm_unreachable("Unknown integer condition code!"); 1945 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change 1946 break; 1947 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2 1948 // If RHSCst is [us]MAXINT, it is always false. Not handling 1949 // this can cause overflow. 1950 if (RHSCst->isMaxValue(true)) 1951 return LHS; 1952 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false); 1953 case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change 1954 break; 1955 case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 1956 case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 1957 return RHS; 1958 case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change 1959 break; 1960 } 1961 break; 1962 case ICmpInst::ICMP_UGT: 1963 switch (RHSCC) { 1964 default: llvm_unreachable("Unknown integer condition code!"); 1965 case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 1966 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 1967 return LHS; 1968 case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change 1969 break; 1970 case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true 1971 case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true 1972 return Builder->getTrue(); 1973 case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change 1974 break; 1975 } 1976 break; 1977 case ICmpInst::ICMP_SGT: 1978 switch (RHSCC) { 1979 default: llvm_unreachable("Unknown integer condition code!"); 1980 case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 1981 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 1982 return LHS; 1983 case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change 1984 break; 1985 case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true 1986 case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true 1987 return Builder->getTrue(); 1988 case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change 1989 break; 1990 } 1991 break; 1992 } 1993 return nullptr; 1994 } 1995 1996 /// Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of instcombine, this returns 1997 /// a Value which should already be inserted into the function. 1998 Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) { 1999 if (LHS->getPredicate() == FCmpInst::FCMP_UNO && 2000 RHS->getPredicate() == FCmpInst::FCMP_UNO && 2001 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) { 2002 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) 2003 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { 2004 // If either of the constants are nans, then the whole thing returns 2005 // true. 2006 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) 2007 return Builder->getTrue(); 2008 2009 // Otherwise, no need to compare the two constants, compare the 2010 // rest. 2011 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0)); 2012 } 2013 2014 // Handle vector zeros. This occurs because the canonical form of 2015 // "fcmp uno x,x" is "fcmp uno x, 0". 2016 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && 2017 isa<ConstantAggregateZero>(RHS->getOperand(1))) 2018 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0)); 2019 2020 return nullptr; 2021 } 2022 2023 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); 2024 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); 2025 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); 2026 2027 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { 2028 // Swap RHS operands to match LHS. 2029 Op1CC = FCmpInst::getSwappedPredicate(Op1CC); 2030 std::swap(Op1LHS, Op1RHS); 2031 } 2032 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { 2033 // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y). 2034 if (Op0CC == Op1CC) 2035 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS); 2036 if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE) 2037 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1); 2038 if (Op0CC == FCmpInst::FCMP_FALSE) 2039 return RHS; 2040 if (Op1CC == FCmpInst::FCMP_FALSE) 2041 return LHS; 2042 bool Op0Ordered; 2043 bool Op1Ordered; 2044 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); 2045 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); 2046 if (Op0Ordered == Op1Ordered) { 2047 // If both are ordered or unordered, return a new fcmp with 2048 // or'ed predicates. 2049 return getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS, Builder); 2050 } 2051 } 2052 return nullptr; 2053 } 2054 2055 /// This helper function folds: 2056 /// 2057 /// ((A | B) & C1) | (B & C2) 2058 /// 2059 /// into: 2060 /// 2061 /// (A & C1) | B 2062 /// 2063 /// when the XOR of the two constants is "all ones" (-1). 2064 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op, 2065 Value *A, Value *B, Value *C) { 2066 ConstantInt *CI1 = dyn_cast<ConstantInt>(C); 2067 if (!CI1) return nullptr; 2068 2069 Value *V1 = nullptr; 2070 ConstantInt *CI2 = nullptr; 2071 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr; 2072 2073 APInt Xor = CI1->getValue() ^ CI2->getValue(); 2074 if (!Xor.isAllOnesValue()) return nullptr; 2075 2076 if (V1 == A || V1 == B) { 2077 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1); 2078 return BinaryOperator::CreateOr(NewOp, V1); 2079 } 2080 2081 return nullptr; 2082 } 2083 2084 /// \brief This helper function folds: 2085 /// 2086 /// ((A | B) & C1) ^ (B & C2) 2087 /// 2088 /// into: 2089 /// 2090 /// (A & C1) ^ B 2091 /// 2092 /// when the XOR of the two constants is "all ones" (-1). 2093 Instruction *InstCombiner::FoldXorWithConstants(BinaryOperator &I, Value *Op, 2094 Value *A, Value *B, Value *C) { 2095 ConstantInt *CI1 = dyn_cast<ConstantInt>(C); 2096 if (!CI1) 2097 return nullptr; 2098 2099 Value *V1 = nullptr; 2100 ConstantInt *CI2 = nullptr; 2101 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) 2102 return nullptr; 2103 2104 APInt Xor = CI1->getValue() ^ CI2->getValue(); 2105 if (!Xor.isAllOnesValue()) 2106 return nullptr; 2107 2108 if (V1 == A || V1 == B) { 2109 Value *NewOp = Builder->CreateAnd(V1 == A ? B : A, CI1); 2110 return BinaryOperator::CreateXor(NewOp, V1); 2111 } 2112 2113 return nullptr; 2114 } 2115 2116 Instruction *InstCombiner::visitOr(BinaryOperator &I) { 2117 bool Changed = SimplifyAssociativeOrCommutative(I); 2118 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 2119 2120 if (Value *V = SimplifyVectorOp(I)) 2121 return replaceInstUsesWith(I, V); 2122 2123 if (Value *V = SimplifyOrInst(Op0, Op1, DL, TLI, DT, AC)) 2124 return replaceInstUsesWith(I, V); 2125 2126 // (A&B)|(A&C) -> A&(B|C) etc 2127 if (Value *V = SimplifyUsingDistributiveLaws(I)) 2128 return replaceInstUsesWith(I, V); 2129 2130 // See if we can simplify any instructions used by the instruction whose sole 2131 // purpose is to compute bits we don't care about. 2132 if (SimplifyDemandedInstructionBits(I)) 2133 return &I; 2134 2135 if (Value *V = SimplifyBSwap(I)) 2136 return replaceInstUsesWith(I, V); 2137 2138 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { 2139 ConstantInt *C1 = nullptr; Value *X = nullptr; 2140 // (X & C1) | C2 --> (X | C2) & (C1|C2) 2141 // iff (C1 & C2) == 0. 2142 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && 2143 (RHS->getValue() & C1->getValue()) != 0 && 2144 Op0->hasOneUse()) { 2145 Value *Or = Builder->CreateOr(X, RHS); 2146 Or->takeName(Op0); 2147 return BinaryOperator::CreateAnd(Or, 2148 Builder->getInt(RHS->getValue() | C1->getValue())); 2149 } 2150 2151 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) 2152 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && 2153 Op0->hasOneUse()) { 2154 Value *Or = Builder->CreateOr(X, RHS); 2155 Or->takeName(Op0); 2156 return BinaryOperator::CreateXor(Or, 2157 Builder->getInt(C1->getValue() & ~RHS->getValue())); 2158 } 2159 2160 // Try to fold constant and into select arguments. 2161 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 2162 if (Instruction *R = FoldOpIntoSelect(I, SI)) 2163 return R; 2164 2165 if (isa<PHINode>(Op0)) 2166 if (Instruction *NV = FoldOpIntoPhi(I)) 2167 return NV; 2168 } 2169 2170 // Given an OR instruction, check to see if this is a bswap. 2171 if (Instruction *BSwap = MatchBSwap(I)) 2172 return BSwap; 2173 2174 Value *A = nullptr, *B = nullptr; 2175 ConstantInt *C1 = nullptr, *C2 = nullptr; 2176 2177 // (X^C)|Y -> (X|Y)^C iff Y&C == 0 2178 if (Op0->hasOneUse() && 2179 match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && 2180 MaskedValueIsZero(Op1, C1->getValue(), 0, &I)) { 2181 Value *NOr = Builder->CreateOr(A, Op1); 2182 NOr->takeName(Op0); 2183 return BinaryOperator::CreateXor(NOr, C1); 2184 } 2185 2186 // Y|(X^C) -> (X|Y)^C iff Y&C == 0 2187 if (Op1->hasOneUse() && 2188 match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && 2189 MaskedValueIsZero(Op0, C1->getValue(), 0, &I)) { 2190 Value *NOr = Builder->CreateOr(A, Op0); 2191 NOr->takeName(Op0); 2192 return BinaryOperator::CreateXor(NOr, C1); 2193 } 2194 2195 // ((~A & B) | A) -> (A | B) 2196 if (match(Op0, m_And(m_Not(m_Value(A)), m_Value(B))) && 2197 match(Op1, m_Specific(A))) 2198 return BinaryOperator::CreateOr(A, B); 2199 2200 // ((A & B) | ~A) -> (~A | B) 2201 if (match(Op0, m_And(m_Value(A), m_Value(B))) && 2202 match(Op1, m_Not(m_Specific(A)))) 2203 return BinaryOperator::CreateOr(Builder->CreateNot(A), B); 2204 2205 // (A & (~B)) | (A ^ B) -> (A ^ B) 2206 if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) && 2207 match(Op1, m_Xor(m_Specific(A), m_Specific(B)))) 2208 return BinaryOperator::CreateXor(A, B); 2209 2210 // (A ^ B) | ( A & (~B)) -> (A ^ B) 2211 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && 2212 match(Op1, m_And(m_Specific(A), m_Not(m_Specific(B))))) 2213 return BinaryOperator::CreateXor(A, B); 2214 2215 // (A & C)|(B & D) 2216 Value *C = nullptr, *D = nullptr; 2217 if (match(Op0, m_And(m_Value(A), m_Value(C))) && 2218 match(Op1, m_And(m_Value(B), m_Value(D)))) { 2219 Value *V1 = nullptr, *V2 = nullptr; 2220 C1 = dyn_cast<ConstantInt>(C); 2221 C2 = dyn_cast<ConstantInt>(D); 2222 if (C1 && C2) { // (A & C1)|(B & C2) 2223 if ((C1->getValue() & C2->getValue()) == 0) { 2224 // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2) 2225 // iff (C1&C2) == 0 and (N&~C1) == 0 2226 if (match(A, m_Or(m_Value(V1), m_Value(V2))) && 2227 ((V1 == B && 2228 MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N) 2229 (V2 == B && 2230 MaskedValueIsZero(V1, ~C1->getValue(), 0, &I)))) // (N|V) 2231 return BinaryOperator::CreateAnd(A, 2232 Builder->getInt(C1->getValue()|C2->getValue())); 2233 // Or commutes, try both ways. 2234 if (match(B, m_Or(m_Value(V1), m_Value(V2))) && 2235 ((V1 == A && 2236 MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N) 2237 (V2 == A && 2238 MaskedValueIsZero(V1, ~C2->getValue(), 0, &I)))) // (N|V) 2239 return BinaryOperator::CreateAnd(B, 2240 Builder->getInt(C1->getValue()|C2->getValue())); 2241 2242 // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2) 2243 // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0. 2244 ConstantInt *C3 = nullptr, *C4 = nullptr; 2245 if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) && 2246 (C3->getValue() & ~C1->getValue()) == 0 && 2247 match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) && 2248 (C4->getValue() & ~C2->getValue()) == 0) { 2249 V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield"); 2250 return BinaryOperator::CreateAnd(V2, 2251 Builder->getInt(C1->getValue()|C2->getValue())); 2252 } 2253 } 2254 } 2255 2256 // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants. 2257 if (Value *V = matchSelectFromAndOr(A, C, B, D, *Builder)) 2258 return replaceInstUsesWith(I, V); 2259 if (Value *V = matchSelectFromAndOr(A, C, D, B, *Builder)) 2260 return replaceInstUsesWith(I, V); 2261 if (Value *V = matchSelectFromAndOr(C, A, B, D, *Builder)) 2262 return replaceInstUsesWith(I, V); 2263 if (Value *V = matchSelectFromAndOr(C, A, D, B, *Builder)) 2264 return replaceInstUsesWith(I, V); 2265 if (Value *V = matchSelectFromAndOr(B, D, A, C, *Builder)) 2266 return replaceInstUsesWith(I, V); 2267 if (Value *V = matchSelectFromAndOr(B, D, C, A, *Builder)) 2268 return replaceInstUsesWith(I, V); 2269 if (Value *V = matchSelectFromAndOr(D, B, A, C, *Builder)) 2270 return replaceInstUsesWith(I, V); 2271 if (Value *V = matchSelectFromAndOr(D, B, C, A, *Builder)) 2272 return replaceInstUsesWith(I, V); 2273 2274 // ((A&~B)|(~A&B)) -> A^B 2275 if ((match(C, m_Not(m_Specific(D))) && 2276 match(B, m_Not(m_Specific(A))))) 2277 return BinaryOperator::CreateXor(A, D); 2278 // ((~B&A)|(~A&B)) -> A^B 2279 if ((match(A, m_Not(m_Specific(D))) && 2280 match(B, m_Not(m_Specific(C))))) 2281 return BinaryOperator::CreateXor(C, D); 2282 // ((A&~B)|(B&~A)) -> A^B 2283 if ((match(C, m_Not(m_Specific(B))) && 2284 match(D, m_Not(m_Specific(A))))) 2285 return BinaryOperator::CreateXor(A, B); 2286 // ((~B&A)|(B&~A)) -> A^B 2287 if ((match(A, m_Not(m_Specific(B))) && 2288 match(D, m_Not(m_Specific(C))))) 2289 return BinaryOperator::CreateXor(C, B); 2290 2291 // ((A|B)&1)|(B&-2) -> (A&1) | B 2292 if (match(A, m_Or(m_Value(V1), m_Specific(B))) || 2293 match(A, m_Or(m_Specific(B), m_Value(V1)))) { 2294 Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C); 2295 if (Ret) return Ret; 2296 } 2297 // (B&-2)|((A|B)&1) -> (A&1) | B 2298 if (match(B, m_Or(m_Specific(A), m_Value(V1))) || 2299 match(B, m_Or(m_Value(V1), m_Specific(A)))) { 2300 Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D); 2301 if (Ret) return Ret; 2302 } 2303 // ((A^B)&1)|(B&-2) -> (A&1) ^ B 2304 if (match(A, m_Xor(m_Value(V1), m_Specific(B))) || 2305 match(A, m_Xor(m_Specific(B), m_Value(V1)))) { 2306 Instruction *Ret = FoldXorWithConstants(I, Op1, V1, B, C); 2307 if (Ret) return Ret; 2308 } 2309 // (B&-2)|((A^B)&1) -> (A&1) ^ B 2310 if (match(B, m_Xor(m_Specific(A), m_Value(V1))) || 2311 match(B, m_Xor(m_Value(V1), m_Specific(A)))) { 2312 Instruction *Ret = FoldXorWithConstants(I, Op0, A, V1, D); 2313 if (Ret) return Ret; 2314 } 2315 } 2316 2317 // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C 2318 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) 2319 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) 2320 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse()) 2321 return BinaryOperator::CreateOr(Op0, C); 2322 2323 // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C 2324 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B)))) 2325 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) 2326 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse()) 2327 return BinaryOperator::CreateOr(Op1, C); 2328 2329 // ((B | C) & A) | B -> B | (A & C) 2330 if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A)))) 2331 return BinaryOperator::CreateOr(Op1, Builder->CreateAnd(A, C)); 2332 2333 if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder)) 2334 return DeMorgan; 2335 2336 // Canonicalize xor to the RHS. 2337 bool SwappedForXor = false; 2338 if (match(Op0, m_Xor(m_Value(), m_Value()))) { 2339 std::swap(Op0, Op1); 2340 SwappedForXor = true; 2341 } 2342 2343 // A | ( A ^ B) -> A | B 2344 // A | (~A ^ B) -> A | ~B 2345 // (A & B) | (A ^ B) 2346 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) { 2347 if (Op0 == A || Op0 == B) 2348 return BinaryOperator::CreateOr(A, B); 2349 2350 if (match(Op0, m_And(m_Specific(A), m_Specific(B))) || 2351 match(Op0, m_And(m_Specific(B), m_Specific(A)))) 2352 return BinaryOperator::CreateOr(A, B); 2353 2354 if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) { 2355 Value *Not = Builder->CreateNot(B, B->getName()+".not"); 2356 return BinaryOperator::CreateOr(Not, Op0); 2357 } 2358 if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) { 2359 Value *Not = Builder->CreateNot(A, A->getName()+".not"); 2360 return BinaryOperator::CreateOr(Not, Op0); 2361 } 2362 } 2363 2364 // A | ~(A | B) -> A | ~B 2365 // A | ~(A ^ B) -> A | ~B 2366 if (match(Op1, m_Not(m_Value(A)))) 2367 if (BinaryOperator *B = dyn_cast<BinaryOperator>(A)) 2368 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) && 2369 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or || 2370 B->getOpcode() == Instruction::Xor)) { 2371 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) : 2372 B->getOperand(0); 2373 Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not"); 2374 return BinaryOperator::CreateOr(Not, Op0); 2375 } 2376 2377 // (A & B) | ((~A) ^ B) -> (~A ^ B) 2378 if (match(Op0, m_And(m_Value(A), m_Value(B))) && 2379 match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B)))) 2380 return BinaryOperator::CreateXor(Builder->CreateNot(A), B); 2381 2382 // ((~A) ^ B) | (A & B) -> (~A ^ B) 2383 if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) && 2384 match(Op1, m_And(m_Specific(A), m_Specific(B)))) 2385 return BinaryOperator::CreateXor(Builder->CreateNot(A), B); 2386 2387 if (SwappedForXor) 2388 std::swap(Op0, Op1); 2389 2390 { 2391 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0); 2392 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1); 2393 if (LHS && RHS) 2394 if (Value *Res = FoldOrOfICmps(LHS, RHS, &I)) 2395 return replaceInstUsesWith(I, Res); 2396 2397 // TODO: Make this recursive; it's a little tricky because an arbitrary 2398 // number of 'or' instructions might have to be created. 2399 Value *X, *Y; 2400 if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) { 2401 if (auto *Cmp = dyn_cast<ICmpInst>(X)) 2402 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I)) 2403 return replaceInstUsesWith(I, Builder->CreateOr(Res, Y)); 2404 if (auto *Cmp = dyn_cast<ICmpInst>(Y)) 2405 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I)) 2406 return replaceInstUsesWith(I, Builder->CreateOr(Res, X)); 2407 } 2408 if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) { 2409 if (auto *Cmp = dyn_cast<ICmpInst>(X)) 2410 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I)) 2411 return replaceInstUsesWith(I, Builder->CreateOr(Res, Y)); 2412 if (auto *Cmp = dyn_cast<ICmpInst>(Y)) 2413 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I)) 2414 return replaceInstUsesWith(I, Builder->CreateOr(Res, X)); 2415 } 2416 } 2417 2418 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) 2419 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) 2420 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) 2421 if (Value *Res = FoldOrOfFCmps(LHS, RHS)) 2422 return replaceInstUsesWith(I, Res); 2423 2424 if (Instruction *CastedOr = foldCastedBitwiseLogic(I)) 2425 return CastedOr; 2426 2427 // or(sext(A), B) -> A ? -1 : B where A is an i1 2428 // or(A, sext(B)) -> B ? -1 : A where B is an i1 2429 if (match(Op0, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1)) 2430 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1); 2431 if (match(Op1, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1)) 2432 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0); 2433 2434 // Note: If we've gotten to the point of visiting the outer OR, then the 2435 // inner one couldn't be simplified. If it was a constant, then it won't 2436 // be simplified by a later pass either, so we try swapping the inner/outer 2437 // ORs in the hopes that we'll be able to simplify it this way. 2438 // (X|C) | V --> (X|V) | C 2439 if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) && 2440 match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) { 2441 Value *Inner = Builder->CreateOr(A, Op1); 2442 Inner->takeName(Op0); 2443 return BinaryOperator::CreateOr(Inner, C1); 2444 } 2445 2446 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D)) 2447 // Since this OR statement hasn't been optimized further yet, we hope 2448 // that this transformation will allow the new ORs to be optimized. 2449 { 2450 Value *X = nullptr, *Y = nullptr; 2451 if (Op0->hasOneUse() && Op1->hasOneUse() && 2452 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) && 2453 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) { 2454 Value *orTrue = Builder->CreateOr(A, C); 2455 Value *orFalse = Builder->CreateOr(B, D); 2456 return SelectInst::Create(X, orTrue, orFalse); 2457 } 2458 } 2459 2460 return Changed ? &I : nullptr; 2461 } 2462 2463 Instruction *InstCombiner::visitXor(BinaryOperator &I) { 2464 bool Changed = SimplifyAssociativeOrCommutative(I); 2465 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 2466 2467 if (Value *V = SimplifyVectorOp(I)) 2468 return replaceInstUsesWith(I, V); 2469 2470 if (Value *V = SimplifyXorInst(Op0, Op1, DL, TLI, DT, AC)) 2471 return replaceInstUsesWith(I, V); 2472 2473 // (A&B)^(A&C) -> A&(B^C) etc 2474 if (Value *V = SimplifyUsingDistributiveLaws(I)) 2475 return replaceInstUsesWith(I, V); 2476 2477 // See if we can simplify any instructions used by the instruction whose sole 2478 // purpose is to compute bits we don't care about. 2479 if (SimplifyDemandedInstructionBits(I)) 2480 return &I; 2481 2482 if (Value *V = SimplifyBSwap(I)) 2483 return replaceInstUsesWith(I, V); 2484 2485 // Is this a ~ operation? 2486 if (Value *NotOp = dyn_castNotVal(&I)) { 2487 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { 2488 if (Op0I->getOpcode() == Instruction::And || 2489 Op0I->getOpcode() == Instruction::Or) { 2490 // ~(~X & Y) --> (X | ~Y) - De Morgan's Law 2491 // ~(~X | Y) === (X & ~Y) - De Morgan's Law 2492 if (dyn_castNotVal(Op0I->getOperand(1))) 2493 Op0I->swapOperands(); 2494 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { 2495 Value *NotY = 2496 Builder->CreateNot(Op0I->getOperand(1), 2497 Op0I->getOperand(1)->getName()+".not"); 2498 if (Op0I->getOpcode() == Instruction::And) 2499 return BinaryOperator::CreateOr(Op0NotVal, NotY); 2500 return BinaryOperator::CreateAnd(Op0NotVal, NotY); 2501 } 2502 2503 // ~(X & Y) --> (~X | ~Y) - De Morgan's Law 2504 // ~(X | Y) === (~X & ~Y) - De Morgan's Law 2505 if (IsFreeToInvert(Op0I->getOperand(0), 2506 Op0I->getOperand(0)->hasOneUse()) && 2507 IsFreeToInvert(Op0I->getOperand(1), 2508 Op0I->getOperand(1)->hasOneUse())) { 2509 Value *NotX = 2510 Builder->CreateNot(Op0I->getOperand(0), "notlhs"); 2511 Value *NotY = 2512 Builder->CreateNot(Op0I->getOperand(1), "notrhs"); 2513 if (Op0I->getOpcode() == Instruction::And) 2514 return BinaryOperator::CreateOr(NotX, NotY); 2515 return BinaryOperator::CreateAnd(NotX, NotY); 2516 } 2517 2518 } else if (Op0I->getOpcode() == Instruction::AShr) { 2519 // ~(~X >>s Y) --> (X >>s Y) 2520 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) 2521 return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1)); 2522 } 2523 } 2524 } 2525 2526 if (Constant *RHS = dyn_cast<Constant>(Op1)) { 2527 if (RHS->isAllOnesValue() && Op0->hasOneUse()) 2528 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B 2529 if (CmpInst *CI = dyn_cast<CmpInst>(Op0)) 2530 return CmpInst::Create(CI->getOpcode(), 2531 CI->getInversePredicate(), 2532 CI->getOperand(0), CI->getOperand(1)); 2533 } 2534 2535 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { 2536 // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp). 2537 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { 2538 if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) { 2539 if (CI->hasOneUse() && Op0C->hasOneUse()) { 2540 Instruction::CastOps Opcode = Op0C->getOpcode(); 2541 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) && 2542 (RHS == ConstantExpr::getCast(Opcode, Builder->getTrue(), 2543 Op0C->getDestTy()))) { 2544 CI->setPredicate(CI->getInversePredicate()); 2545 return CastInst::Create(Opcode, CI, Op0C->getType()); 2546 } 2547 } 2548 } 2549 } 2550 2551 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { 2552 // ~(c-X) == X-c-1 == X+(-c-1) 2553 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) 2554 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { 2555 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); 2556 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, 2557 ConstantInt::get(I.getType(), 1)); 2558 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); 2559 } 2560 2561 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { 2562 if (Op0I->getOpcode() == Instruction::Add) { 2563 // ~(X-c) --> (-c-1)-X 2564 if (RHS->isAllOnesValue()) { 2565 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); 2566 return BinaryOperator::CreateSub( 2567 ConstantExpr::getSub(NegOp0CI, 2568 ConstantInt::get(I.getType(), 1)), 2569 Op0I->getOperand(0)); 2570 } else if (RHS->getValue().isSignBit()) { 2571 // (X + C) ^ signbit -> (X + C + signbit) 2572 Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue()); 2573 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); 2574 2575 } 2576 } else if (Op0I->getOpcode() == Instruction::Or) { 2577 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 2578 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue(), 2579 0, &I)) { 2580 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); 2581 // Anything in both C1 and C2 is known to be zero, remove it from 2582 // NewRHS. 2583 Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); 2584 NewRHS = ConstantExpr::getAnd(NewRHS, 2585 ConstantExpr::getNot(CommonBits)); 2586 Worklist.Add(Op0I); 2587 I.setOperand(0, Op0I->getOperand(0)); 2588 I.setOperand(1, NewRHS); 2589 return &I; 2590 } 2591 } else if (Op0I->getOpcode() == Instruction::LShr) { 2592 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3) 2593 // E1 = "X ^ C1" 2594 BinaryOperator *E1; 2595 ConstantInt *C1; 2596 if (Op0I->hasOneUse() && 2597 (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) && 2598 E1->getOpcode() == Instruction::Xor && 2599 (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) { 2600 // fold (C1 >> C2) ^ C3 2601 ConstantInt *C2 = Op0CI, *C3 = RHS; 2602 APInt FoldConst = C1->getValue().lshr(C2->getValue()); 2603 FoldConst ^= C3->getValue(); 2604 // Prepare the two operands. 2605 Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2); 2606 Opnd0->takeName(Op0I); 2607 cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc()); 2608 Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst); 2609 2610 return BinaryOperator::CreateXor(Opnd0, FoldVal); 2611 } 2612 } 2613 } 2614 } 2615 2616 // Try to fold constant and into select arguments. 2617 if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) 2618 if (Instruction *R = FoldOpIntoSelect(I, SI)) 2619 return R; 2620 if (isa<PHINode>(Op0)) 2621 if (Instruction *NV = FoldOpIntoPhi(I)) 2622 return NV; 2623 } 2624 2625 BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); 2626 if (Op1I) { 2627 Value *A, *B; 2628 if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { 2629 if (A == Op0) { // B^(B|A) == (A|B)^B 2630 Op1I->swapOperands(); 2631 I.swapOperands(); 2632 std::swap(Op0, Op1); 2633 } else if (B == Op0) { // B^(A|B) == (A|B)^B 2634 I.swapOperands(); // Simplified below. 2635 std::swap(Op0, Op1); 2636 } 2637 } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && 2638 Op1I->hasOneUse()){ 2639 if (A == Op0) { // A^(A&B) -> A^(B&A) 2640 Op1I->swapOperands(); 2641 std::swap(A, B); 2642 } 2643 if (B == Op0) { // A^(B&A) -> (B&A)^A 2644 I.swapOperands(); // Simplified below. 2645 std::swap(Op0, Op1); 2646 } 2647 } 2648 } 2649 2650 BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); 2651 if (Op0I) { 2652 Value *A, *B; 2653 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && 2654 Op0I->hasOneUse()) { 2655 if (A == Op1) // (B|A)^B == (A|B)^B 2656 std::swap(A, B); 2657 if (B == Op1) // (A|B)^B == A & ~B 2658 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1)); 2659 } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && 2660 Op0I->hasOneUse()){ 2661 if (A == Op1) // (A&B)^A -> (B&A)^A 2662 std::swap(A, B); 2663 if (B == Op1 && // (B&A)^A == ~B & A 2664 !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C 2665 return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1); 2666 } 2667 } 2668 } 2669 2670 if (Op0I && Op1I) { 2671 Value *A, *B, *C, *D; 2672 // (A & B)^(A | B) -> A ^ B 2673 if (match(Op0I, m_And(m_Value(A), m_Value(B))) && 2674 match(Op1I, m_Or(m_Value(C), m_Value(D)))) { 2675 if ((A == C && B == D) || (A == D && B == C)) 2676 return BinaryOperator::CreateXor(A, B); 2677 } 2678 // (A | B)^(A & B) -> A ^ B 2679 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && 2680 match(Op1I, m_And(m_Value(C), m_Value(D)))) { 2681 if ((A == C && B == D) || (A == D && B == C)) 2682 return BinaryOperator::CreateXor(A, B); 2683 } 2684 // (A | ~B) ^ (~A | B) -> A ^ B 2685 if (match(Op0I, m_Or(m_Value(A), m_Not(m_Value(B)))) && 2686 match(Op1I, m_Or(m_Not(m_Specific(A)), m_Specific(B)))) { 2687 return BinaryOperator::CreateXor(A, B); 2688 } 2689 // (~A | B) ^ (A | ~B) -> A ^ B 2690 if (match(Op0I, m_Or(m_Not(m_Value(A)), m_Value(B))) && 2691 match(Op1I, m_Or(m_Specific(A), m_Not(m_Specific(B))))) { 2692 return BinaryOperator::CreateXor(A, B); 2693 } 2694 // (A & ~B) ^ (~A & B) -> A ^ B 2695 if (match(Op0I, m_And(m_Value(A), m_Not(m_Value(B)))) && 2696 match(Op1I, m_And(m_Not(m_Specific(A)), m_Specific(B)))) { 2697 return BinaryOperator::CreateXor(A, B); 2698 } 2699 // (~A & B) ^ (A & ~B) -> A ^ B 2700 if (match(Op0I, m_And(m_Not(m_Value(A)), m_Value(B))) && 2701 match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B))))) { 2702 return BinaryOperator::CreateXor(A, B); 2703 } 2704 // (A ^ C)^(A | B) -> ((~A) & B) ^ C 2705 if (match(Op0I, m_Xor(m_Value(D), m_Value(C))) && 2706 match(Op1I, m_Or(m_Value(A), m_Value(B)))) { 2707 if (D == A) 2708 return BinaryOperator::CreateXor( 2709 Builder->CreateAnd(Builder->CreateNot(A), B), C); 2710 if (D == B) 2711 return BinaryOperator::CreateXor( 2712 Builder->CreateAnd(Builder->CreateNot(B), A), C); 2713 } 2714 // (A | B)^(A ^ C) -> ((~A) & B) ^ C 2715 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && 2716 match(Op1I, m_Xor(m_Value(D), m_Value(C)))) { 2717 if (D == A) 2718 return BinaryOperator::CreateXor( 2719 Builder->CreateAnd(Builder->CreateNot(A), B), C); 2720 if (D == B) 2721 return BinaryOperator::CreateXor( 2722 Builder->CreateAnd(Builder->CreateNot(B), A), C); 2723 } 2724 // (A & B) ^ (A ^ B) -> (A | B) 2725 if (match(Op0I, m_And(m_Value(A), m_Value(B))) && 2726 match(Op1I, m_Xor(m_Specific(A), m_Specific(B)))) 2727 return BinaryOperator::CreateOr(A, B); 2728 // (A ^ B) ^ (A & B) -> (A | B) 2729 if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) && 2730 match(Op1I, m_And(m_Specific(A), m_Specific(B)))) 2731 return BinaryOperator::CreateOr(A, B); 2732 } 2733 2734 Value *A = nullptr, *B = nullptr; 2735 // (A & ~B) ^ (~A) -> ~(A & B) 2736 if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) && 2737 match(Op1, m_Not(m_Specific(A)))) 2738 return BinaryOperator::CreateNot(Builder->CreateAnd(A, B)); 2739 2740 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) 2741 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) 2742 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0))) 2743 if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) { 2744 if (LHS->getOperand(0) == RHS->getOperand(1) && 2745 LHS->getOperand(1) == RHS->getOperand(0)) 2746 LHS->swapOperands(); 2747 if (LHS->getOperand(0) == RHS->getOperand(0) && 2748 LHS->getOperand(1) == RHS->getOperand(1)) { 2749 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); 2750 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS); 2751 bool isSigned = LHS->isSigned() || RHS->isSigned(); 2752 return replaceInstUsesWith(I, 2753 getNewICmpValue(isSigned, Code, Op0, Op1, 2754 Builder)); 2755 } 2756 } 2757 2758 if (Instruction *CastedXor = foldCastedBitwiseLogic(I)) 2759 return CastedXor; 2760 2761 return Changed ? &I : nullptr; 2762 } 2763