1 //===- InstCombineCompares.cpp --------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the visitICmp and visitFCmp functions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "InstCombineInternal.h" 14 #include "llvm/ADT/APSInt.h" 15 #include "llvm/ADT/SetVector.h" 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/Analysis/CaptureTracking.h" 18 #include "llvm/Analysis/CmpInstAnalysis.h" 19 #include "llvm/Analysis/ConstantFolding.h" 20 #include "llvm/Analysis/InstructionSimplify.h" 21 #include "llvm/Analysis/Utils/Local.h" 22 #include "llvm/Analysis/VectorUtils.h" 23 #include "llvm/IR/ConstantRange.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/InstrTypes.h" 26 #include "llvm/IR/IntrinsicInst.h" 27 #include "llvm/IR/PatternMatch.h" 28 #include "llvm/Support/KnownBits.h" 29 #include "llvm/Transforms/InstCombine/InstCombiner.h" 30 #include <bitset> 31 32 using namespace llvm; 33 using namespace PatternMatch; 34 35 #define DEBUG_TYPE "instcombine" 36 37 // How many times is a select replaced by one of its operands? 38 STATISTIC(NumSel, "Number of select opts"); 39 40 41 /// Compute Result = In1+In2, returning true if the result overflowed for this 42 /// type. 43 static bool addWithOverflow(APInt &Result, const APInt &In1, 44 const APInt &In2, bool IsSigned = false) { 45 bool Overflow; 46 if (IsSigned) 47 Result = In1.sadd_ov(In2, Overflow); 48 else 49 Result = In1.uadd_ov(In2, Overflow); 50 51 return Overflow; 52 } 53 54 /// Compute Result = In1-In2, returning true if the result overflowed for this 55 /// type. 56 static bool subWithOverflow(APInt &Result, const APInt &In1, 57 const APInt &In2, bool IsSigned = false) { 58 bool Overflow; 59 if (IsSigned) 60 Result = In1.ssub_ov(In2, Overflow); 61 else 62 Result = In1.usub_ov(In2, Overflow); 63 64 return Overflow; 65 } 66 67 /// Given an icmp instruction, return true if any use of this comparison is a 68 /// branch on sign bit comparison. 69 static bool hasBranchUse(ICmpInst &I) { 70 for (auto *U : I.users()) 71 if (isa<BranchInst>(U)) 72 return true; 73 return false; 74 } 75 76 /// Returns true if the exploded icmp can be expressed as a signed comparison 77 /// to zero and updates the predicate accordingly. 78 /// The signedness of the comparison is preserved. 79 /// TODO: Refactor with decomposeBitTestICmp()? 80 static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) { 81 if (!ICmpInst::isSigned(Pred)) 82 return false; 83 84 if (C.isZero()) 85 return ICmpInst::isRelational(Pred); 86 87 if (C.isOne()) { 88 if (Pred == ICmpInst::ICMP_SLT) { 89 Pred = ICmpInst::ICMP_SLE; 90 return true; 91 } 92 } else if (C.isAllOnes()) { 93 if (Pred == ICmpInst::ICMP_SGT) { 94 Pred = ICmpInst::ICMP_SGE; 95 return true; 96 } 97 } 98 99 return false; 100 } 101 102 /// This is called when we see this pattern: 103 /// cmp pred (load (gep GV, ...)), cmpcst 104 /// where GV is a global variable with a constant initializer. Try to simplify 105 /// this into some simple computation that does not need the load. For example 106 /// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3". 107 /// 108 /// If AndCst is non-null, then the loaded value is masked with that constant 109 /// before doing the comparison. This handles cases like "A[i]&4 == 0". 110 Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal( 111 LoadInst *LI, GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, 112 ConstantInt *AndCst) { 113 if (LI->isVolatile() || LI->getType() != GEP->getResultElementType() || 114 GV->getValueType() != GEP->getSourceElementType() || !GV->isConstant() || 115 !GV->hasDefinitiveInitializer()) 116 return nullptr; 117 118 Constant *Init = GV->getInitializer(); 119 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init)) 120 return nullptr; 121 122 uint64_t ArrayElementCount = Init->getType()->getArrayNumElements(); 123 // Don't blow up on huge arrays. 124 if (ArrayElementCount > MaxArraySizeForCombine) 125 return nullptr; 126 127 // There are many forms of this optimization we can handle, for now, just do 128 // the simple index into a single-dimensional array. 129 // 130 // Require: GEP GV, 0, i {{, constant indices}} 131 if (GEP->getNumOperands() < 3 || !isa<ConstantInt>(GEP->getOperand(1)) || 132 !cast<ConstantInt>(GEP->getOperand(1))->isZero() || 133 isa<Constant>(GEP->getOperand(2))) 134 return nullptr; 135 136 // Check that indices after the variable are constants and in-range for the 137 // type they index. Collect the indices. This is typically for arrays of 138 // structs. 139 SmallVector<unsigned, 4> LaterIndices; 140 141 Type *EltTy = Init->getType()->getArrayElementType(); 142 for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) { 143 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i)); 144 if (!Idx) 145 return nullptr; // Variable index. 146 147 uint64_t IdxVal = Idx->getZExtValue(); 148 if ((unsigned)IdxVal != IdxVal) 149 return nullptr; // Too large array index. 150 151 if (StructType *STy = dyn_cast<StructType>(EltTy)) 152 EltTy = STy->getElementType(IdxVal); 153 else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) { 154 if (IdxVal >= ATy->getNumElements()) 155 return nullptr; 156 EltTy = ATy->getElementType(); 157 } else { 158 return nullptr; // Unknown type. 159 } 160 161 LaterIndices.push_back(IdxVal); 162 } 163 164 enum { Overdefined = -3, Undefined = -2 }; 165 166 // Variables for our state machines. 167 168 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form 169 // "i == 47 | i == 87", where 47 is the first index the condition is true for, 170 // and 87 is the second (and last) index. FirstTrueElement is -2 when 171 // undefined, otherwise set to the first true element. SecondTrueElement is 172 // -2 when undefined, -3 when overdefined and >= 0 when that index is true. 173 int FirstTrueElement = Undefined, SecondTrueElement = Undefined; 174 175 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the 176 // form "i != 47 & i != 87". Same state transitions as for true elements. 177 int FirstFalseElement = Undefined, SecondFalseElement = Undefined; 178 179 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these 180 /// define a state machine that triggers for ranges of values that the index 181 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'. 182 /// This is -2 when undefined, -3 when overdefined, and otherwise the last 183 /// index in the range (inclusive). We use -2 for undefined here because we 184 /// use relative comparisons and don't want 0-1 to match -1. 185 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined; 186 187 // MagicBitvector - This is a magic bitvector where we set a bit if the 188 // comparison is true for element 'i'. If there are 64 elements or less in 189 // the array, this will fully represent all the comparison results. 190 uint64_t MagicBitvector = 0; 191 192 // Scan the array and see if one of our patterns matches. 193 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1)); 194 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) { 195 Constant *Elt = Init->getAggregateElement(i); 196 if (!Elt) 197 return nullptr; 198 199 // If this is indexing an array of structures, get the structure element. 200 if (!LaterIndices.empty()) { 201 Elt = ConstantFoldExtractValueInstruction(Elt, LaterIndices); 202 if (!Elt) 203 return nullptr; 204 } 205 206 // If the element is masked, handle it. 207 if (AndCst) { 208 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL); 209 if (!Elt) 210 return nullptr; 211 } 212 213 // Find out if the comparison would be true or false for the i'th element. 214 Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt, 215 CompareRHS, DL, &TLI); 216 if (!C) 217 return nullptr; 218 219 // If the result is undef for this element, ignore it. 220 if (isa<UndefValue>(C)) { 221 // Extend range state machines to cover this element in case there is an 222 // undef in the middle of the range. 223 if (TrueRangeEnd == (int)i - 1) 224 TrueRangeEnd = i; 225 if (FalseRangeEnd == (int)i - 1) 226 FalseRangeEnd = i; 227 continue; 228 } 229 230 // If we can't compute the result for any of the elements, we have to give 231 // up evaluating the entire conditional. 232 if (!isa<ConstantInt>(C)) 233 return nullptr; 234 235 // Otherwise, we know if the comparison is true or false for this element, 236 // update our state machines. 237 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero(); 238 239 // State machine for single/double/range index comparison. 240 if (IsTrueForElt) { 241 // Update the TrueElement state machine. 242 if (FirstTrueElement == Undefined) 243 FirstTrueElement = TrueRangeEnd = i; // First true element. 244 else { 245 // Update double-compare state machine. 246 if (SecondTrueElement == Undefined) 247 SecondTrueElement = i; 248 else 249 SecondTrueElement = Overdefined; 250 251 // Update range state machine. 252 if (TrueRangeEnd == (int)i - 1) 253 TrueRangeEnd = i; 254 else 255 TrueRangeEnd = Overdefined; 256 } 257 } else { 258 // Update the FalseElement state machine. 259 if (FirstFalseElement == Undefined) 260 FirstFalseElement = FalseRangeEnd = i; // First false element. 261 else { 262 // Update double-compare state machine. 263 if (SecondFalseElement == Undefined) 264 SecondFalseElement = i; 265 else 266 SecondFalseElement = Overdefined; 267 268 // Update range state machine. 269 if (FalseRangeEnd == (int)i - 1) 270 FalseRangeEnd = i; 271 else 272 FalseRangeEnd = Overdefined; 273 } 274 } 275 276 // If this element is in range, update our magic bitvector. 277 if (i < 64 && IsTrueForElt) 278 MagicBitvector |= 1ULL << i; 279 280 // If all of our states become overdefined, bail out early. Since the 281 // predicate is expensive, only check it every 8 elements. This is only 282 // really useful for really huge arrays. 283 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined && 284 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined && 285 FalseRangeEnd == Overdefined) 286 return nullptr; 287 } 288 289 // Now that we've scanned the entire array, emit our new comparison(s). We 290 // order the state machines in complexity of the generated code. 291 Value *Idx = GEP->getOperand(2); 292 293 // If the index is larger than the pointer offset size of the target, truncate 294 // the index down like the GEP would do implicitly. We don't have to do this 295 // for an inbounds GEP because the index can't be out of range. 296 if (!GEP->isInBounds()) { 297 Type *PtrIdxTy = DL.getIndexType(GEP->getType()); 298 unsigned OffsetSize = PtrIdxTy->getIntegerBitWidth(); 299 if (Idx->getType()->getPrimitiveSizeInBits().getFixedValue() > OffsetSize) 300 Idx = Builder.CreateTrunc(Idx, PtrIdxTy); 301 } 302 303 // If inbounds keyword is not present, Idx * ElementSize can overflow. 304 // Let's assume that ElementSize is 2 and the wanted value is at offset 0. 305 // Then, there are two possible values for Idx to match offset 0: 306 // 0x00..00, 0x80..00. 307 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the 308 // comparison is false if Idx was 0x80..00. 309 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx. 310 unsigned ElementSize = 311 DL.getTypeAllocSize(Init->getType()->getArrayElementType()); 312 auto MaskIdx = [&](Value *Idx) { 313 if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) { 314 Value *Mask = Constant::getAllOnesValue(Idx->getType()); 315 Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize)); 316 Idx = Builder.CreateAnd(Idx, Mask); 317 } 318 return Idx; 319 }; 320 321 // If the comparison is only true for one or two elements, emit direct 322 // comparisons. 323 if (SecondTrueElement != Overdefined) { 324 Idx = MaskIdx(Idx); 325 // None true -> false. 326 if (FirstTrueElement == Undefined) 327 return replaceInstUsesWith(ICI, Builder.getFalse()); 328 329 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement); 330 331 // True for one element -> 'i == 47'. 332 if (SecondTrueElement == Undefined) 333 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx); 334 335 // True for two elements -> 'i == 47 | i == 72'. 336 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx); 337 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement); 338 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx); 339 return BinaryOperator::CreateOr(C1, C2); 340 } 341 342 // If the comparison is only false for one or two elements, emit direct 343 // comparisons. 344 if (SecondFalseElement != Overdefined) { 345 Idx = MaskIdx(Idx); 346 // None false -> true. 347 if (FirstFalseElement == Undefined) 348 return replaceInstUsesWith(ICI, Builder.getTrue()); 349 350 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement); 351 352 // False for one element -> 'i != 47'. 353 if (SecondFalseElement == Undefined) 354 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx); 355 356 // False for two elements -> 'i != 47 & i != 72'. 357 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx); 358 Value *SecondFalseIdx = 359 ConstantInt::get(Idx->getType(), SecondFalseElement); 360 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx); 361 return BinaryOperator::CreateAnd(C1, C2); 362 } 363 364 // If the comparison can be replaced with a range comparison for the elements 365 // where it is true, emit the range check. 366 if (TrueRangeEnd != Overdefined) { 367 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare"); 368 Idx = MaskIdx(Idx); 369 370 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1). 371 if (FirstTrueElement) { 372 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement); 373 Idx = Builder.CreateAdd(Idx, Offs); 374 } 375 376 Value *End = 377 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1); 378 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End); 379 } 380 381 // False range check. 382 if (FalseRangeEnd != Overdefined) { 383 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare"); 384 Idx = MaskIdx(Idx); 385 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse). 386 if (FirstFalseElement) { 387 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement); 388 Idx = Builder.CreateAdd(Idx, Offs); 389 } 390 391 Value *End = 392 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement); 393 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End); 394 } 395 396 // If a magic bitvector captures the entire comparison state 397 // of this load, replace it with computation that does: 398 // ((magic_cst >> i) & 1) != 0 399 { 400 Type *Ty = nullptr; 401 402 // Look for an appropriate type: 403 // - The type of Idx if the magic fits 404 // - The smallest fitting legal type 405 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth()) 406 Ty = Idx->getType(); 407 else 408 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount); 409 410 if (Ty) { 411 Idx = MaskIdx(Idx); 412 Value *V = Builder.CreateIntCast(Idx, Ty, false); 413 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V); 414 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V); 415 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0)); 416 } 417 } 418 419 return nullptr; 420 } 421 422 /// Returns true if we can rewrite Start as a GEP with pointer Base 423 /// and some integer offset. The nodes that need to be re-written 424 /// for this transformation will be added to Explored. 425 static bool canRewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags &NW, 426 const DataLayout &DL, 427 SetVector<Value *> &Explored) { 428 SmallVector<Value *, 16> WorkList(1, Start); 429 Explored.insert(Base); 430 431 // The following traversal gives us an order which can be used 432 // when doing the final transformation. Since in the final 433 // transformation we create the PHI replacement instructions first, 434 // we don't have to get them in any particular order. 435 // 436 // However, for other instructions we will have to traverse the 437 // operands of an instruction first, which means that we have to 438 // do a post-order traversal. 439 while (!WorkList.empty()) { 440 SetVector<PHINode *> PHIs; 441 442 while (!WorkList.empty()) { 443 if (Explored.size() >= 100) 444 return false; 445 446 Value *V = WorkList.back(); 447 448 if (Explored.contains(V)) { 449 WorkList.pop_back(); 450 continue; 451 } 452 453 if (!isa<GetElementPtrInst>(V) && !isa<PHINode>(V)) 454 // We've found some value that we can't explore which is different from 455 // the base. Therefore we can't do this transformation. 456 return false; 457 458 if (auto *GEP = dyn_cast<GEPOperator>(V)) { 459 // Only allow inbounds GEPs with at most one variable offset. 460 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); }; 461 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1) 462 return false; 463 464 NW = NW.intersectForOffsetAdd(GEP->getNoWrapFlags()); 465 if (!Explored.contains(GEP->getOperand(0))) 466 WorkList.push_back(GEP->getOperand(0)); 467 } 468 469 if (WorkList.back() == V) { 470 WorkList.pop_back(); 471 // We've finished visiting this node, mark it as such. 472 Explored.insert(V); 473 } 474 475 if (auto *PN = dyn_cast<PHINode>(V)) { 476 // We cannot transform PHIs on unsplittable basic blocks. 477 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator())) 478 return false; 479 Explored.insert(PN); 480 PHIs.insert(PN); 481 } 482 } 483 484 // Explore the PHI nodes further. 485 for (auto *PN : PHIs) 486 for (Value *Op : PN->incoming_values()) 487 if (!Explored.contains(Op)) 488 WorkList.push_back(Op); 489 } 490 491 // Make sure that we can do this. Since we can't insert GEPs in a basic 492 // block before a PHI node, we can't easily do this transformation if 493 // we have PHI node users of transformed instructions. 494 for (Value *Val : Explored) { 495 for (Value *Use : Val->uses()) { 496 497 auto *PHI = dyn_cast<PHINode>(Use); 498 auto *Inst = dyn_cast<Instruction>(Val); 499 500 if (Inst == Base || Inst == PHI || !Inst || !PHI || 501 !Explored.contains(PHI)) 502 continue; 503 504 if (PHI->getParent() == Inst->getParent()) 505 return false; 506 } 507 } 508 return true; 509 } 510 511 // Sets the appropriate insert point on Builder where we can add 512 // a replacement Instruction for V (if that is possible). 513 static void setInsertionPoint(IRBuilder<> &Builder, Value *V, 514 bool Before = true) { 515 if (auto *PHI = dyn_cast<PHINode>(V)) { 516 BasicBlock *Parent = PHI->getParent(); 517 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt()); 518 return; 519 } 520 if (auto *I = dyn_cast<Instruction>(V)) { 521 if (!Before) 522 I = &*std::next(I->getIterator()); 523 Builder.SetInsertPoint(I); 524 return; 525 } 526 if (auto *A = dyn_cast<Argument>(V)) { 527 // Set the insertion point in the entry block. 528 BasicBlock &Entry = A->getParent()->getEntryBlock(); 529 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt()); 530 return; 531 } 532 // Otherwise, this is a constant and we don't need to set a new 533 // insertion point. 534 assert(isa<Constant>(V) && "Setting insertion point for unknown value!"); 535 } 536 537 /// Returns a re-written value of Start as an indexed GEP using Base as a 538 /// pointer. 539 static Value *rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW, 540 const DataLayout &DL, 541 SetVector<Value *> &Explored, 542 InstCombiner &IC) { 543 // Perform all the substitutions. This is a bit tricky because we can 544 // have cycles in our use-def chains. 545 // 1. Create the PHI nodes without any incoming values. 546 // 2. Create all the other values. 547 // 3. Add the edges for the PHI nodes. 548 // 4. Emit GEPs to get the original pointers. 549 // 5. Remove the original instructions. 550 Type *IndexType = IntegerType::get( 551 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType())); 552 553 DenseMap<Value *, Value *> NewInsts; 554 NewInsts[Base] = ConstantInt::getNullValue(IndexType); 555 556 // Create the new PHI nodes, without adding any incoming values. 557 for (Value *Val : Explored) { 558 if (Val == Base) 559 continue; 560 // Create empty phi nodes. This avoids cyclic dependencies when creating 561 // the remaining instructions. 562 if (auto *PHI = dyn_cast<PHINode>(Val)) 563 NewInsts[PHI] = 564 PHINode::Create(IndexType, PHI->getNumIncomingValues(), 565 PHI->getName() + ".idx", PHI->getIterator()); 566 } 567 IRBuilder<> Builder(Base->getContext()); 568 569 // Create all the other instructions. 570 for (Value *Val : Explored) { 571 if (NewInsts.contains(Val)) 572 continue; 573 574 if (auto *GEP = dyn_cast<GEPOperator>(Val)) { 575 setInsertionPoint(Builder, GEP); 576 Value *Op = NewInsts[GEP->getOperand(0)]; 577 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP); 578 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero()) 579 NewInsts[GEP] = OffsetV; 580 else 581 NewInsts[GEP] = Builder.CreateAdd( 582 Op, OffsetV, GEP->getOperand(0)->getName() + ".add", 583 /*NUW=*/NW.hasNoUnsignedWrap(), 584 /*NSW=*/NW.hasNoUnsignedSignedWrap()); 585 continue; 586 } 587 if (isa<PHINode>(Val)) 588 continue; 589 590 llvm_unreachable("Unexpected instruction type"); 591 } 592 593 // Add the incoming values to the PHI nodes. 594 for (Value *Val : Explored) { 595 if (Val == Base) 596 continue; 597 // All the instructions have been created, we can now add edges to the 598 // phi nodes. 599 if (auto *PHI = dyn_cast<PHINode>(Val)) { 600 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]); 601 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) { 602 Value *NewIncoming = PHI->getIncomingValue(I); 603 604 auto It = NewInsts.find(NewIncoming); 605 if (It != NewInsts.end()) 606 NewIncoming = It->second; 607 608 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I)); 609 } 610 } 611 } 612 613 for (Value *Val : Explored) { 614 if (Val == Base) 615 continue; 616 617 setInsertionPoint(Builder, Val, false); 618 // Create GEP for external users. 619 Value *NewVal = Builder.CreateGEP(Builder.getInt8Ty(), Base, NewInsts[Val], 620 Val->getName() + ".ptr", NW); 621 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal); 622 // Add old instruction to worklist for DCE. We don't directly remove it 623 // here because the original compare is one of the users. 624 IC.addToWorklist(cast<Instruction>(Val)); 625 } 626 627 return NewInsts[Start]; 628 } 629 630 /// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant. 631 /// We can look through PHIs, GEPs and casts in order to determine a common base 632 /// between GEPLHS and RHS. 633 static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, 634 CmpPredicate Cond, 635 const DataLayout &DL, 636 InstCombiner &IC) { 637 // FIXME: Support vector of pointers. 638 if (GEPLHS->getType()->isVectorTy()) 639 return nullptr; 640 641 if (!GEPLHS->hasAllConstantIndices()) 642 return nullptr; 643 644 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0); 645 Value *PtrBase = 646 GEPLHS->stripAndAccumulateConstantOffsets(DL, Offset, 647 /*AllowNonInbounds*/ false); 648 649 // Bail if we looked through addrspacecast. 650 if (PtrBase->getType() != GEPLHS->getType()) 651 return nullptr; 652 653 // The set of nodes that will take part in this transformation. 654 SetVector<Value *> Nodes; 655 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags(); 656 if (!canRewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes)) 657 return nullptr; 658 659 // We know we can re-write this as 660 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) 661 // Since we've only looked through inbouds GEPs we know that we 662 // can't have overflow on either side. We can therefore re-write 663 // this as: 664 // OFFSET1 cmp OFFSET2 665 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes, IC); 666 667 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written 668 // GEP having PtrBase as the pointer base, and has returned in NewRHS the 669 // offset. Since Index is the offset of LHS to the base pointer, we will now 670 // compare the offsets instead of comparing the pointers. 671 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), 672 IC.Builder.getInt(Offset), NewRHS); 673 } 674 675 /// Fold comparisons between a GEP instruction and something else. At this point 676 /// we know that the GEP is on the LHS of the comparison. 677 Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, 678 CmpPredicate Cond, Instruction &I) { 679 // Don't transform signed compares of GEPs into index compares. Even if the 680 // GEP is inbounds, the final add of the base pointer can have signed overflow 681 // and would change the result of the icmp. 682 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be 683 // the maximum signed value for the pointer type. 684 if (ICmpInst::isSigned(Cond)) 685 return nullptr; 686 687 // Look through bitcasts and addrspacecasts. We do not however want to remove 688 // 0 GEPs. 689 if (!isa<GetElementPtrInst>(RHS)) 690 RHS = RHS->stripPointerCasts(); 691 692 auto CanFold = [Cond](GEPNoWrapFlags NW) { 693 if (ICmpInst::isEquality(Cond)) 694 return true; 695 696 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags. 697 assert(ICmpInst::isUnsigned(Cond)); 698 return NW != GEPNoWrapFlags::none(); 699 }; 700 701 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) { 702 if (!NW.hasNoUnsignedWrap()) { 703 // Convert signed to unsigned comparison. 704 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Op1, Op2); 705 } 706 707 auto *I = new ICmpInst(Cond, Op1, Op2); 708 I->setSameSign(NW.hasNoUnsignedSignedWrap()); 709 return I; 710 }; 711 712 Value *PtrBase = GEPLHS->getOperand(0); 713 if (PtrBase == RHS && CanFold(GEPLHS->getNoWrapFlags())) { 714 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). 715 Value *Offset = EmitGEPOffset(GEPLHS); 716 return NewICmp(GEPLHS->getNoWrapFlags(), Offset, 717 Constant::getNullValue(Offset->getType())); 718 } 719 720 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) && 721 isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() && 722 !NullPointerIsDefined(I.getFunction(), 723 RHS->getType()->getPointerAddressSpace())) { 724 // For most address spaces, an allocation can't be placed at null, but null 725 // itself is treated as a 0 size allocation in the in bounds rules. Thus, 726 // the only valid inbounds address derived from null, is null itself. 727 // Thus, we have four cases to consider: 728 // 1) Base == nullptr, Offset == 0 -> inbounds, null 729 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds 730 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations) 731 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison) 732 // 733 // (Note if we're indexing a type of size 0, that simply collapses into one 734 // of the buckets above.) 735 // 736 // In general, we're allowed to make values less poison (i.e. remove 737 // sources of full UB), so in this case, we just select between the two 738 // non-poison cases (1 and 4 above). 739 // 740 // For vectors, we apply the same reasoning on a per-lane basis. 741 auto *Base = GEPLHS->getPointerOperand(); 742 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) { 743 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount(); 744 Base = Builder.CreateVectorSplat(EC, Base); 745 } 746 return new ICmpInst(Cond, Base, 747 ConstantExpr::getPointerBitCastOrAddrSpaceCast( 748 cast<Constant>(RHS), Base->getType())); 749 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) { 750 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags(); 751 752 // If the base pointers are different, but the indices are the same, just 753 // compare the base pointer. 754 if (PtrBase != GEPRHS->getOperand(0)) { 755 bool IndicesTheSame = 756 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() && 757 GEPLHS->getPointerOperand()->getType() == 758 GEPRHS->getPointerOperand()->getType() && 759 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType(); 760 if (IndicesTheSame) 761 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) 762 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { 763 IndicesTheSame = false; 764 break; 765 } 766 767 // If all indices are the same, just compare the base pointers. 768 Type *BaseType = GEPLHS->getOperand(0)->getType(); 769 if (IndicesTheSame && 770 CmpInst::makeCmpResultType(BaseType) == I.getType() && CanFold(NW)) 771 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0)); 772 773 // If we're comparing GEPs with two base pointers that only differ in type 774 // and both GEPs have only constant indices or just one use, then fold 775 // the compare with the adjusted indices. 776 // FIXME: Support vector of pointers. 777 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() && 778 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) && 779 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) && 780 PtrBase->stripPointerCasts() == 781 GEPRHS->getOperand(0)->stripPointerCasts() && 782 !GEPLHS->getType()->isVectorTy()) { 783 Value *LOffset = EmitGEPOffset(GEPLHS); 784 Value *ROffset = EmitGEPOffset(GEPRHS); 785 786 // If we looked through an addrspacecast between different sized address 787 // spaces, the LHS and RHS pointers are different sized 788 // integers. Truncate to the smaller one. 789 Type *LHSIndexTy = LOffset->getType(); 790 Type *RHSIndexTy = ROffset->getType(); 791 if (LHSIndexTy != RHSIndexTy) { 792 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() < 793 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) { 794 ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy); 795 } else 796 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy); 797 } 798 799 Value *Cmp = Builder.CreateICmp(ICmpInst::getSignedPredicate(Cond), 800 LOffset, ROffset); 801 return replaceInstUsesWith(I, Cmp); 802 } 803 804 // Otherwise, the base pointers are different and the indices are 805 // different. Try convert this to an indexed compare by looking through 806 // PHIs/casts. 807 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this); 808 } 809 810 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands() && 811 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) { 812 // If the GEPs only differ by one index, compare it. 813 unsigned NumDifferences = 0; // Keep track of # differences. 814 unsigned DiffOperand = 0; // The operand that differs. 815 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) 816 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { 817 Type *LHSType = GEPLHS->getOperand(i)->getType(); 818 Type *RHSType = GEPRHS->getOperand(i)->getType(); 819 // FIXME: Better support for vector of pointers. 820 if (LHSType->getPrimitiveSizeInBits() != 821 RHSType->getPrimitiveSizeInBits() || 822 (GEPLHS->getType()->isVectorTy() && 823 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) { 824 // Irreconcilable differences. 825 NumDifferences = 2; 826 break; 827 } 828 829 if (NumDifferences++) break; 830 DiffOperand = i; 831 } 832 833 if (NumDifferences == 0) // SAME GEP? 834 return replaceInstUsesWith(I, // No comparison is needed here. 835 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond))); 836 837 else if (NumDifferences == 1 && CanFold(NW)) { 838 Value *LHSV = GEPLHS->getOperand(DiffOperand); 839 Value *RHSV = GEPRHS->getOperand(DiffOperand); 840 return NewICmp(NW, LHSV, RHSV); 841 } 842 } 843 844 if (CanFold(NW)) { 845 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) 846 Value *L = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true); 847 Value *R = EmitGEPOffset(GEPRHS, /*RewriteGEP=*/true); 848 return NewICmp(NW, L, R); 849 } 850 } 851 852 // Try convert this to an indexed compare by looking through PHIs/casts as a 853 // last resort. 854 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this); 855 } 856 857 bool InstCombinerImpl::foldAllocaCmp(AllocaInst *Alloca) { 858 // It would be tempting to fold away comparisons between allocas and any 859 // pointer not based on that alloca (e.g. an argument). However, even 860 // though such pointers cannot alias, they can still compare equal. 861 // 862 // But LLVM doesn't specify where allocas get their memory, so if the alloca 863 // doesn't escape we can argue that it's impossible to guess its value, and we 864 // can therefore act as if any such guesses are wrong. 865 // 866 // However, we need to ensure that this folding is consistent: We can't fold 867 // one comparison to false, and then leave a different comparison against the 868 // same value alone (as it might evaluate to true at runtime, leading to a 869 // contradiction). As such, this code ensures that all comparisons are folded 870 // at the same time, and there are no other escapes. 871 872 struct CmpCaptureTracker : public CaptureTracker { 873 AllocaInst *Alloca; 874 bool Captured = false; 875 /// The value of the map is a bit mask of which icmp operands the alloca is 876 /// used in. 877 SmallMapVector<ICmpInst *, unsigned, 4> ICmps; 878 879 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {} 880 881 void tooManyUses() override { Captured = true; } 882 883 bool captured(const Use *U) override { 884 auto *ICmp = dyn_cast<ICmpInst>(U->getUser()); 885 // We need to check that U is based *only* on the alloca, and doesn't 886 // have other contributions from a select/phi operand. 887 // TODO: We could check whether getUnderlyingObjects() reduces to one 888 // object, which would allow looking through phi nodes. 889 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) { 890 // Collect equality icmps of the alloca, and don't treat them as 891 // captures. 892 ICmps[ICmp] |= 1u << U->getOperandNo(); 893 return false; 894 } 895 896 Captured = true; 897 return true; 898 } 899 }; 900 901 CmpCaptureTracker Tracker(Alloca); 902 PointerMayBeCaptured(Alloca, &Tracker); 903 if (Tracker.Captured) 904 return false; 905 906 bool Changed = false; 907 for (auto [ICmp, Operands] : Tracker.ICmps) { 908 switch (Operands) { 909 case 1: 910 case 2: { 911 // The alloca is only used in one icmp operand. Assume that the 912 // equality is false. 913 auto *Res = ConstantInt::get( 914 ICmp->getType(), ICmp->getPredicate() == ICmpInst::ICMP_NE); 915 replaceInstUsesWith(*ICmp, Res); 916 eraseInstFromFunction(*ICmp); 917 Changed = true; 918 break; 919 } 920 case 3: 921 // Both icmp operands are based on the alloca, so this is comparing 922 // pointer offsets, without leaking any information about the address 923 // of the alloca. Ignore such comparisons. 924 break; 925 default: 926 llvm_unreachable("Cannot happen"); 927 } 928 } 929 930 return Changed; 931 } 932 933 /// Fold "icmp pred (X+C), X". 934 Instruction *InstCombinerImpl::foldICmpAddOpConst(Value *X, const APInt &C, 935 CmpPredicate Pred) { 936 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0, 937 // so the values can never be equal. Similarly for all other "or equals" 938 // operators. 939 assert(!!C && "C should not be zero!"); 940 941 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255 942 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253 943 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0 944 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { 945 Constant *R = ConstantInt::get(X->getType(), 946 APInt::getMaxValue(C.getBitWidth()) - C); 947 return new ICmpInst(ICmpInst::ICMP_UGT, X, R); 948 } 949 950 // (X+1) >u X --> X <u (0-1) --> X != 255 951 // (X+2) >u X --> X <u (0-2) --> X <u 254 952 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0 953 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) 954 return new ICmpInst(ICmpInst::ICMP_ULT, X, 955 ConstantInt::get(X->getType(), -C)); 956 957 APInt SMax = APInt::getSignedMaxValue(C.getBitWidth()); 958 959 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127 960 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125 961 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0 962 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1 963 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126 964 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127 965 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) 966 return new ICmpInst(ICmpInst::ICMP_SGT, X, 967 ConstantInt::get(X->getType(), SMax - C)); 968 969 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127 970 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126 971 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1 972 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2 973 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126 974 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128 975 976 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE); 977 return new ICmpInst(ICmpInst::ICMP_SLT, X, 978 ConstantInt::get(X->getType(), SMax - (C - 1))); 979 } 980 981 /// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" -> 982 /// (icmp eq/ne A, Log2(AP2/AP1)) -> 983 /// (icmp eq/ne A, Log2(AP2) - Log2(AP1)). 984 Instruction *InstCombinerImpl::foldICmpShrConstConst(ICmpInst &I, Value *A, 985 const APInt &AP1, 986 const APInt &AP2) { 987 assert(I.isEquality() && "Cannot fold icmp gt/lt"); 988 989 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) { 990 if (I.getPredicate() == I.ICMP_NE) 991 Pred = CmpInst::getInversePredicate(Pred); 992 return new ICmpInst(Pred, LHS, RHS); 993 }; 994 995 // Don't bother doing any work for cases which InstSimplify handles. 996 if (AP2.isZero()) 997 return nullptr; 998 999 bool IsAShr = isa<AShrOperator>(I.getOperand(0)); 1000 if (IsAShr) { 1001 if (AP2.isAllOnes()) 1002 return nullptr; 1003 if (AP2.isNegative() != AP1.isNegative()) 1004 return nullptr; 1005 if (AP2.sgt(AP1)) 1006 return nullptr; 1007 } 1008 1009 if (!AP1) 1010 // 'A' must be large enough to shift out the highest set bit. 1011 return getICmp(I.ICMP_UGT, A, 1012 ConstantInt::get(A->getType(), AP2.logBase2())); 1013 1014 if (AP1 == AP2) 1015 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType())); 1016 1017 int Shift; 1018 if (IsAShr && AP1.isNegative()) 1019 Shift = AP1.countl_one() - AP2.countl_one(); 1020 else 1021 Shift = AP1.countl_zero() - AP2.countl_zero(); 1022 1023 if (Shift > 0) { 1024 if (IsAShr && AP1 == AP2.ashr(Shift)) { 1025 // There are multiple solutions if we are comparing against -1 and the LHS 1026 // of the ashr is not a power of two. 1027 if (AP1.isAllOnes() && !AP2.isPowerOf2()) 1028 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift)); 1029 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); 1030 } else if (AP1 == AP2.lshr(Shift)) { 1031 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); 1032 } 1033 } 1034 1035 // Shifting const2 will never be equal to const1. 1036 // FIXME: This should always be handled by InstSimplify? 1037 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE); 1038 return replaceInstUsesWith(I, TorF); 1039 } 1040 1041 /// Handle "(icmp eq/ne (shl AP2, A), AP1)" -> 1042 /// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)). 1043 Instruction *InstCombinerImpl::foldICmpShlConstConst(ICmpInst &I, Value *A, 1044 const APInt &AP1, 1045 const APInt &AP2) { 1046 assert(I.isEquality() && "Cannot fold icmp gt/lt"); 1047 1048 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) { 1049 if (I.getPredicate() == I.ICMP_NE) 1050 Pred = CmpInst::getInversePredicate(Pred); 1051 return new ICmpInst(Pred, LHS, RHS); 1052 }; 1053 1054 // Don't bother doing any work for cases which InstSimplify handles. 1055 if (AP2.isZero()) 1056 return nullptr; 1057 1058 unsigned AP2TrailingZeros = AP2.countr_zero(); 1059 1060 if (!AP1 && AP2TrailingZeros != 0) 1061 return getICmp( 1062 I.ICMP_UGE, A, 1063 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros)); 1064 1065 if (AP1 == AP2) 1066 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType())); 1067 1068 // Get the distance between the lowest bits that are set. 1069 int Shift = AP1.countr_zero() - AP2TrailingZeros; 1070 1071 if (Shift > 0 && AP2.shl(Shift) == AP1) 1072 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); 1073 1074 // Shifting const2 will never be equal to const1. 1075 // FIXME: This should always be handled by InstSimplify? 1076 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE); 1077 return replaceInstUsesWith(I, TorF); 1078 } 1079 1080 /// The caller has matched a pattern of the form: 1081 /// I = icmp ugt (add (add A, B), CI2), CI1 1082 /// If this is of the form: 1083 /// sum = a + b 1084 /// if (sum+128 >u 255) 1085 /// Then replace it with llvm.sadd.with.overflow.i8. 1086 /// 1087 static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, 1088 ConstantInt *CI2, ConstantInt *CI1, 1089 InstCombinerImpl &IC) { 1090 // The transformation we're trying to do here is to transform this into an 1091 // llvm.sadd.with.overflow. To do this, we have to replace the original add 1092 // with a narrower add, and discard the add-with-constant that is part of the 1093 // range check (if we can't eliminate it, this isn't profitable). 1094 1095 // In order to eliminate the add-with-constant, the compare can be its only 1096 // use. 1097 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0)); 1098 if (!AddWithCst->hasOneUse()) 1099 return nullptr; 1100 1101 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow. 1102 if (!CI2->getValue().isPowerOf2()) 1103 return nullptr; 1104 unsigned NewWidth = CI2->getValue().countr_zero(); 1105 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) 1106 return nullptr; 1107 1108 // The width of the new add formed is 1 more than the bias. 1109 ++NewWidth; 1110 1111 // Check to see that CI1 is an all-ones value with NewWidth bits. 1112 if (CI1->getBitWidth() == NewWidth || 1113 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth)) 1114 return nullptr; 1115 1116 // This is only really a signed overflow check if the inputs have been 1117 // sign-extended; check for that condition. For example, if CI2 is 2^31 and 1118 // the operands of the add are 64 bits wide, we need at least 33 sign bits. 1119 if (IC.ComputeMaxSignificantBits(A, 0, &I) > NewWidth || 1120 IC.ComputeMaxSignificantBits(B, 0, &I) > NewWidth) 1121 return nullptr; 1122 1123 // In order to replace the original add with a narrower 1124 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant 1125 // and truncates that discard the high bits of the add. Verify that this is 1126 // the case. 1127 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0)); 1128 for (User *U : OrigAdd->users()) { 1129 if (U == AddWithCst) 1130 continue; 1131 1132 // Only accept truncates for now. We would really like a nice recursive 1133 // predicate like SimplifyDemandedBits, but which goes downwards the use-def 1134 // chain to see which bits of a value are actually demanded. If the 1135 // original add had another add which was then immediately truncated, we 1136 // could still do the transformation. 1137 TruncInst *TI = dyn_cast<TruncInst>(U); 1138 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth) 1139 return nullptr; 1140 } 1141 1142 // If the pattern matches, truncate the inputs to the narrower type and 1143 // use the sadd_with_overflow intrinsic to efficiently compute both the 1144 // result and the overflow bit. 1145 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth); 1146 Function *F = Intrinsic::getOrInsertDeclaration( 1147 I.getModule(), Intrinsic::sadd_with_overflow, NewType); 1148 1149 InstCombiner::BuilderTy &Builder = IC.Builder; 1150 1151 // Put the new code above the original add, in case there are any uses of the 1152 // add between the add and the compare. 1153 Builder.SetInsertPoint(OrigAdd); 1154 1155 Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc"); 1156 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc"); 1157 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd"); 1158 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result"); 1159 Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType()); 1160 1161 // The inner add was the result of the narrow add, zero extended to the 1162 // wider type. Replace it with the result computed by the intrinsic. 1163 IC.replaceInstUsesWith(*OrigAdd, ZExt); 1164 IC.eraseInstFromFunction(*OrigAdd); 1165 1166 // The original icmp gets replaced with the overflow value. 1167 return ExtractValueInst::Create(Call, 1, "sadd.overflow"); 1168 } 1169 1170 /// If we have: 1171 /// icmp eq/ne (urem/srem %x, %y), 0 1172 /// iff %y is a power-of-two, we can replace this with a bit test: 1173 /// icmp eq/ne (and %x, (add %y, -1)), 0 1174 Instruction *InstCombinerImpl::foldIRemByPowerOfTwoToBitTest(ICmpInst &I) { 1175 // This fold is only valid for equality predicates. 1176 if (!I.isEquality()) 1177 return nullptr; 1178 CmpPredicate Pred; 1179 Value *X, *Y, *Zero; 1180 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))), 1181 m_CombineAnd(m_Zero(), m_Value(Zero))))) 1182 return nullptr; 1183 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, 0, &I)) 1184 return nullptr; 1185 // This may increase instruction count, we don't enforce that Y is a constant. 1186 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType())); 1187 Value *Masked = Builder.CreateAnd(X, Mask); 1188 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, Zero); 1189 } 1190 1191 /// Fold equality-comparison between zero and any (maybe truncated) right-shift 1192 /// by one-less-than-bitwidth into a sign test on the original value. 1193 Instruction *InstCombinerImpl::foldSignBitTest(ICmpInst &I) { 1194 Instruction *Val; 1195 CmpPredicate Pred; 1196 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero()))) 1197 return nullptr; 1198 1199 Value *X; 1200 Type *XTy; 1201 1202 Constant *C; 1203 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) { 1204 XTy = X->getType(); 1205 unsigned XBitWidth = XTy->getScalarSizeInBits(); 1206 if (!match(C, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, 1207 APInt(XBitWidth, XBitWidth - 1)))) 1208 return nullptr; 1209 } else if (isa<BinaryOperator>(Val) && 1210 (X = reassociateShiftAmtsOfTwoSameDirectionShifts( 1211 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val), 1212 /*AnalyzeForSignBitExtraction=*/true))) { 1213 XTy = X->getType(); 1214 } else 1215 return nullptr; 1216 1217 return ICmpInst::Create(Instruction::ICmp, 1218 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SGE 1219 : ICmpInst::ICMP_SLT, 1220 X, ConstantInt::getNullValue(XTy)); 1221 } 1222 1223 // Handle icmp pred X, 0 1224 Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) { 1225 CmpInst::Predicate Pred = Cmp.getPredicate(); 1226 if (!match(Cmp.getOperand(1), m_Zero())) 1227 return nullptr; 1228 1229 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0) 1230 if (Pred == ICmpInst::ICMP_SGT) { 1231 Value *A, *B; 1232 if (match(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) { 1233 if (isKnownPositive(A, SQ.getWithInstruction(&Cmp))) 1234 return new ICmpInst(Pred, B, Cmp.getOperand(1)); 1235 if (isKnownPositive(B, SQ.getWithInstruction(&Cmp))) 1236 return new ICmpInst(Pred, A, Cmp.getOperand(1)); 1237 } 1238 } 1239 1240 if (Instruction *New = foldIRemByPowerOfTwoToBitTest(Cmp)) 1241 return New; 1242 1243 // Given: 1244 // icmp eq/ne (urem %x, %y), 0 1245 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem': 1246 // icmp eq/ne %x, 0 1247 Value *X, *Y; 1248 if (match(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) && 1249 ICmpInst::isEquality(Pred)) { 1250 KnownBits XKnown = computeKnownBits(X, 0, &Cmp); 1251 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp); 1252 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2) 1253 return new ICmpInst(Pred, X, Cmp.getOperand(1)); 1254 } 1255 1256 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are 1257 // odd/non-zero/there is no overflow. 1258 if (match(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) && 1259 ICmpInst::isEquality(Pred)) { 1260 1261 KnownBits XKnown = computeKnownBits(X, 0, &Cmp); 1262 // if X % 2 != 0 1263 // (icmp eq/ne Y) 1264 if (XKnown.countMaxTrailingZeros() == 0) 1265 return new ICmpInst(Pred, Y, Cmp.getOperand(1)); 1266 1267 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp); 1268 // if Y % 2 != 0 1269 // (icmp eq/ne X) 1270 if (YKnown.countMaxTrailingZeros() == 0) 1271 return new ICmpInst(Pred, X, Cmp.getOperand(1)); 1272 1273 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0)); 1274 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) { 1275 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp); 1276 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()` 1277 // but to avoid unnecessary work, first just if this is an obvious case. 1278 1279 // if X non-zero and NoOverflow(X * Y) 1280 // (icmp eq/ne Y) 1281 if (!XKnown.One.isZero() || isKnownNonZero(X, Q)) 1282 return new ICmpInst(Pred, Y, Cmp.getOperand(1)); 1283 1284 // if Y non-zero and NoOverflow(X * Y) 1285 // (icmp eq/ne X) 1286 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q)) 1287 return new ICmpInst(Pred, X, Cmp.getOperand(1)); 1288 } 1289 // Note, we are skipping cases: 1290 // if Y % 2 != 0 AND X % 2 != 0 1291 // (false/true) 1292 // if X non-zero and Y non-zero and NoOverflow(X * Y) 1293 // (false/true) 1294 // Those can be simplified later as we would have already replaced the (icmp 1295 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that 1296 // will fold to a constant elsewhere. 1297 } 1298 return nullptr; 1299 } 1300 1301 /// Fold icmp Pred X, C. 1302 /// TODO: This code structure does not make sense. The saturating add fold 1303 /// should be moved to some other helper and extended as noted below (it is also 1304 /// possible that code has been made unnecessary - do we canonicalize IR to 1305 /// overflow/saturating intrinsics or not?). 1306 Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) { 1307 // Match the following pattern, which is a common idiom when writing 1308 // overflow-safe integer arithmetic functions. The source performs an addition 1309 // in wider type and explicitly checks for overflow using comparisons against 1310 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic. 1311 // 1312 // TODO: This could probably be generalized to handle other overflow-safe 1313 // operations if we worked out the formulas to compute the appropriate magic 1314 // constants. 1315 // 1316 // sum = a + b 1317 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 1318 CmpInst::Predicate Pred = Cmp.getPredicate(); 1319 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1); 1320 Value *A, *B; 1321 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI 1322 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) && 1323 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) 1324 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this)) 1325 return Res; 1326 1327 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...). 1328 Constant *C = dyn_cast<Constant>(Op1); 1329 if (!C) 1330 return nullptr; 1331 1332 if (auto *Phi = dyn_cast<PHINode>(Op0)) 1333 if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) { 1334 SmallVector<Constant *> Ops; 1335 for (Value *V : Phi->incoming_values()) { 1336 Constant *Res = 1337 ConstantFoldCompareInstOperands(Pred, cast<Constant>(V), C, DL); 1338 if (!Res) 1339 return nullptr; 1340 Ops.push_back(Res); 1341 } 1342 Builder.SetInsertPoint(Phi); 1343 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands()); 1344 for (auto [V, Pred] : zip(Ops, Phi->blocks())) 1345 NewPhi->addIncoming(V, Pred); 1346 return replaceInstUsesWith(Cmp, NewPhi); 1347 } 1348 1349 if (Instruction *R = tryFoldInstWithCtpopWithNot(&Cmp)) 1350 return R; 1351 1352 return nullptr; 1353 } 1354 1355 /// Canonicalize icmp instructions based on dominating conditions. 1356 Instruction *InstCombinerImpl::foldICmpWithDominatingICmp(ICmpInst &Cmp) { 1357 // We already checked simple implication in InstSimplify, only handle complex 1358 // cases here. 1359 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1); 1360 const APInt *C; 1361 if (!match(Y, m_APInt(C))) 1362 return nullptr; 1363 1364 CmpInst::Predicate Pred = Cmp.getPredicate(); 1365 ConstantRange CR = ConstantRange::makeExactICmpRegion(Pred, *C); 1366 1367 auto handleDomCond = [&](ICmpInst::Predicate DomPred, 1368 const APInt *DomC) -> Instruction * { 1369 // We have 2 compares of a variable with constants. Calculate the constant 1370 // ranges of those compares to see if we can transform the 2nd compare: 1371 // DomBB: 1372 // DomCond = icmp DomPred X, DomC 1373 // br DomCond, CmpBB, FalseBB 1374 // CmpBB: 1375 // Cmp = icmp Pred X, C 1376 ConstantRange DominatingCR = 1377 ConstantRange::makeExactICmpRegion(DomPred, *DomC); 1378 ConstantRange Intersection = DominatingCR.intersectWith(CR); 1379 ConstantRange Difference = DominatingCR.difference(CR); 1380 if (Intersection.isEmptySet()) 1381 return replaceInstUsesWith(Cmp, Builder.getFalse()); 1382 if (Difference.isEmptySet()) 1383 return replaceInstUsesWith(Cmp, Builder.getTrue()); 1384 1385 // Canonicalizing a sign bit comparison that gets used in a branch, 1386 // pessimizes codegen by generating branch on zero instruction instead 1387 // of a test and branch. So we avoid canonicalizing in such situations 1388 // because test and branch instruction has better branch displacement 1389 // than compare and branch instruction. 1390 bool UnusedBit; 1391 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit); 1392 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp))) 1393 return nullptr; 1394 1395 // Avoid an infinite loop with min/max canonicalization. 1396 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics. 1397 if (Cmp.hasOneUse() && 1398 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value()))) 1399 return nullptr; 1400 1401 if (const APInt *EqC = Intersection.getSingleElement()) 1402 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC)); 1403 if (const APInt *NeC = Difference.getSingleElement()) 1404 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC)); 1405 return nullptr; 1406 }; 1407 1408 for (BranchInst *BI : DC.conditionsFor(X)) { 1409 CmpPredicate DomPred; 1410 const APInt *DomC; 1411 if (!match(BI->getCondition(), 1412 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC)))) 1413 continue; 1414 1415 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0)); 1416 if (DT.dominates(Edge0, Cmp.getParent())) { 1417 if (auto *V = handleDomCond(DomPred, DomC)) 1418 return V; 1419 } else { 1420 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1)); 1421 if (DT.dominates(Edge1, Cmp.getParent())) 1422 if (auto *V = 1423 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC)) 1424 return V; 1425 } 1426 } 1427 1428 return nullptr; 1429 } 1430 1431 /// Fold icmp (trunc X), C. 1432 Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp, 1433 TruncInst *Trunc, 1434 const APInt &C) { 1435 ICmpInst::Predicate Pred = Cmp.getPredicate(); 1436 Value *X = Trunc->getOperand(0); 1437 Type *SrcTy = X->getType(); 1438 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(), 1439 SrcBits = SrcTy->getScalarSizeInBits(); 1440 1441 // Match (icmp pred (trunc nuw/nsw X), C) 1442 // Which we can convert to (icmp pred X, (sext/zext C)) 1443 if (shouldChangeType(Trunc->getType(), SrcTy)) { 1444 if (Trunc->hasNoSignedWrap()) 1445 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits))); 1446 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap()) 1447 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits))); 1448 } 1449 1450 if (C.isOne() && C.getBitWidth() > 1) { 1451 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1 1452 Value *V = nullptr; 1453 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V)))) 1454 return new ICmpInst(ICmpInst::ICMP_SLT, V, 1455 ConstantInt::get(V->getType(), 1)); 1456 } 1457 1458 // TODO: Handle any shifted constant by subtracting trailing zeros. 1459 // TODO: Handle non-equality predicates. 1460 Value *Y; 1461 if (Cmp.isEquality() && match(X, m_Shl(m_One(), m_Value(Y)))) { 1462 // (trunc (1 << Y) to iN) == 0 --> Y u>= N 1463 // (trunc (1 << Y) to iN) != 0 --> Y u< N 1464 if (C.isZero()) { 1465 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT; 1466 return new ICmpInst(NewPred, Y, ConstantInt::get(SrcTy, DstBits)); 1467 } 1468 // (trunc (1 << Y) to iN) == 2**C --> Y == C 1469 // (trunc (1 << Y) to iN) != 2**C --> Y != C 1470 if (C.isPowerOf2()) 1471 return new ICmpInst(Pred, Y, ConstantInt::get(SrcTy, C.logBase2())); 1472 } 1473 1474 if (Cmp.isEquality() && Trunc->hasOneUse()) { 1475 // Canonicalize to a mask and wider compare if the wide type is suitable: 1476 // (trunc X to i8) == C --> (X & 0xff) == (zext C) 1477 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) { 1478 Constant *Mask = 1479 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits)); 1480 Value *And = Builder.CreateAnd(X, Mask); 1481 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits)); 1482 return new ICmpInst(Pred, And, WideC); 1483 } 1484 1485 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all 1486 // of the high bits truncated out of x are known. 1487 KnownBits Known = computeKnownBits(X, 0, &Cmp); 1488 1489 // If all the high bits are known, we can do this xform. 1490 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) { 1491 // Pull in the high bits from known-ones set. 1492 APInt NewRHS = C.zext(SrcBits); 1493 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits); 1494 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS)); 1495 } 1496 } 1497 1498 // Look through truncated right-shift of the sign-bit for a sign-bit check: 1499 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0 1500 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1 1501 Value *ShOp; 1502 const APInt *ShAmtC; 1503 bool TrueIfSigned; 1504 if (isSignBitCheck(Pred, C, TrueIfSigned) && 1505 match(X, m_Shr(m_Value(ShOp), m_APInt(ShAmtC))) && 1506 DstBits == SrcBits - ShAmtC->getZExtValue()) { 1507 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp, 1508 ConstantInt::getNullValue(SrcTy)) 1509 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp, 1510 ConstantInt::getAllOnesValue(SrcTy)); 1511 } 1512 1513 return nullptr; 1514 } 1515 1516 /// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y). 1517 /// Fold icmp (trunc nuw/nsw X), (zext/sext Y). 1518 Instruction * 1519 InstCombinerImpl::foldICmpTruncWithTruncOrExt(ICmpInst &Cmp, 1520 const SimplifyQuery &Q) { 1521 Value *X, *Y; 1522 CmpPredicate Pred; 1523 bool YIsSExt = false; 1524 // Try to match icmp (trunc X), (trunc Y) 1525 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) { 1526 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() & 1527 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind(); 1528 if (Cmp.isSigned()) { 1529 // For signed comparisons, both truncs must be nsw. 1530 if (!(NoWrapFlags & TruncInst::NoSignedWrap)) 1531 return nullptr; 1532 } else { 1533 // For unsigned and equality comparisons, either both must be nuw or 1534 // both must be nsw, we don't care which. 1535 if (!NoWrapFlags) 1536 return nullptr; 1537 } 1538 1539 if (X->getType() != Y->getType() && 1540 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse())) 1541 return nullptr; 1542 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) && 1543 isDesirableIntType(Y->getType()->getScalarSizeInBits())) { 1544 std::swap(X, Y); 1545 Pred = Cmp.getSwappedPredicate(Pred); 1546 } 1547 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap); 1548 } 1549 // Try to match icmp (trunc nuw X), (zext Y) 1550 else if (!Cmp.isSigned() && 1551 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)), 1552 m_OneUse(m_ZExt(m_Value(Y)))))) { 1553 // Can fold trunc nuw + zext for unsigned and equality predicates. 1554 } 1555 // Try to match icmp (trunc nsw X), (sext Y) 1556 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)), 1557 m_OneUse(m_ZExtOrSExt(m_Value(Y)))))) { 1558 // Can fold trunc nsw + zext/sext for all predicates. 1559 YIsSExt = 1560 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1)); 1561 } else 1562 return nullptr; 1563 1564 Type *TruncTy = Cmp.getOperand(0)->getType(); 1565 unsigned TruncBits = TruncTy->getScalarSizeInBits(); 1566 1567 // If this transform will end up changing from desirable types -> undesirable 1568 // types skip it. 1569 if (isDesirableIntType(TruncBits) && 1570 !isDesirableIntType(X->getType()->getScalarSizeInBits())) 1571 return nullptr; 1572 1573 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt); 1574 return new ICmpInst(Pred, X, NewY); 1575 } 1576 1577 /// Fold icmp (xor X, Y), C. 1578 Instruction *InstCombinerImpl::foldICmpXorConstant(ICmpInst &Cmp, 1579 BinaryOperator *Xor, 1580 const APInt &C) { 1581 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C)) 1582 return I; 1583 1584 Value *X = Xor->getOperand(0); 1585 Value *Y = Xor->getOperand(1); 1586 const APInt *XorC; 1587 if (!match(Y, m_APInt(XorC))) 1588 return nullptr; 1589 1590 // If this is a comparison that tests the signbit (X < 0) or (x > -1), 1591 // fold the xor. 1592 ICmpInst::Predicate Pred = Cmp.getPredicate(); 1593 bool TrueIfSigned = false; 1594 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) { 1595 1596 // If the sign bit of the XorCst is not set, there is no change to 1597 // the operation, just stop using the Xor. 1598 if (!XorC->isNegative()) 1599 return replaceOperand(Cmp, 0, X); 1600 1601 // Emit the opposite comparison. 1602 if (TrueIfSigned) 1603 return new ICmpInst(ICmpInst::ICMP_SGT, X, 1604 ConstantInt::getAllOnesValue(X->getType())); 1605 else 1606 return new ICmpInst(ICmpInst::ICMP_SLT, X, 1607 ConstantInt::getNullValue(X->getType())); 1608 } 1609 1610 if (Xor->hasOneUse()) { 1611 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask)) 1612 if (!Cmp.isEquality() && XorC->isSignMask()) { 1613 Pred = Cmp.getFlippedSignednessPredicate(); 1614 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC)); 1615 } 1616 1617 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask)) 1618 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) { 1619 Pred = Cmp.getFlippedSignednessPredicate(); 1620 Pred = Cmp.getSwappedPredicate(Pred); 1621 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC)); 1622 } 1623 } 1624 1625 // Mask constant magic can eliminate an 'xor' with unsigned compares. 1626 if (Pred == ICmpInst::ICMP_UGT) { 1627 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2) 1628 if (*XorC == ~C && (C + 1).isPowerOf2()) 1629 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y); 1630 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2) 1631 if (*XorC == C && (C + 1).isPowerOf2()) 1632 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y); 1633 } 1634 if (Pred == ICmpInst::ICMP_ULT) { 1635 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2) 1636 if (*XorC == -C && C.isPowerOf2()) 1637 return new ICmpInst(ICmpInst::ICMP_UGT, X, 1638 ConstantInt::get(X->getType(), ~C)); 1639 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2) 1640 if (*XorC == C && (-C).isPowerOf2()) 1641 return new ICmpInst(ICmpInst::ICMP_UGT, X, 1642 ConstantInt::get(X->getType(), ~C)); 1643 } 1644 return nullptr; 1645 } 1646 1647 /// For power-of-2 C: 1648 /// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1) 1649 /// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1) 1650 Instruction *InstCombinerImpl::foldICmpXorShiftConst(ICmpInst &Cmp, 1651 BinaryOperator *Xor, 1652 const APInt &C) { 1653 CmpInst::Predicate Pred = Cmp.getPredicate(); 1654 APInt PowerOf2; 1655 if (Pred == ICmpInst::ICMP_ULT) 1656 PowerOf2 = C; 1657 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue()) 1658 PowerOf2 = C + 1; 1659 else 1660 return nullptr; 1661 if (!PowerOf2.isPowerOf2()) 1662 return nullptr; 1663 Value *X; 1664 const APInt *ShiftC; 1665 if (!match(Xor, m_OneUse(m_c_Xor(m_Value(X), 1666 m_AShr(m_Deferred(X), m_APInt(ShiftC)))))) 1667 return nullptr; 1668 uint64_t Shift = ShiftC->getLimitedValue(); 1669 Type *XType = X->getType(); 1670 if (Shift == 0 || PowerOf2.isMinSignedValue()) 1671 return nullptr; 1672 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2)); 1673 APInt Bound = 1674 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1); 1675 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound)); 1676 } 1677 1678 /// Fold icmp (and (sh X, Y), C2), C1. 1679 Instruction *InstCombinerImpl::foldICmpAndShift(ICmpInst &Cmp, 1680 BinaryOperator *And, 1681 const APInt &C1, 1682 const APInt &C2) { 1683 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0)); 1684 if (!Shift || !Shift->isShift()) 1685 return nullptr; 1686 1687 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could 1688 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in 1689 // code produced by the clang front-end, for bitfield access. 1690 // This seemingly simple opportunity to fold away a shift turns out to be 1691 // rather complicated. See PR17827 for details. 1692 unsigned ShiftOpcode = Shift->getOpcode(); 1693 bool IsShl = ShiftOpcode == Instruction::Shl; 1694 const APInt *C3; 1695 if (match(Shift->getOperand(1), m_APInt(C3))) { 1696 APInt NewAndCst, NewCmpCst; 1697 bool AnyCmpCstBitsShiftedOut; 1698 if (ShiftOpcode == Instruction::Shl) { 1699 // For a left shift, we can fold if the comparison is not signed. We can 1700 // also fold a signed comparison if the mask value and comparison value 1701 // are not negative. These constraints may not be obvious, but we can 1702 // prove that they are correct using an SMT solver. 1703 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative())) 1704 return nullptr; 1705 1706 NewCmpCst = C1.lshr(*C3); 1707 NewAndCst = C2.lshr(*C3); 1708 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1; 1709 } else if (ShiftOpcode == Instruction::LShr) { 1710 // For a logical right shift, we can fold if the comparison is not signed. 1711 // We can also fold a signed comparison if the shifted mask value and the 1712 // shifted comparison value are not negative. These constraints may not be 1713 // obvious, but we can prove that they are correct using an SMT solver. 1714 NewCmpCst = C1.shl(*C3); 1715 NewAndCst = C2.shl(*C3); 1716 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1; 1717 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative())) 1718 return nullptr; 1719 } else { 1720 // For an arithmetic shift, check that both constants don't use (in a 1721 // signed sense) the top bits being shifted out. 1722 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode"); 1723 NewCmpCst = C1.shl(*C3); 1724 NewAndCst = C2.shl(*C3); 1725 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1; 1726 if (NewAndCst.ashr(*C3) != C2) 1727 return nullptr; 1728 } 1729 1730 if (AnyCmpCstBitsShiftedOut) { 1731 // If we shifted bits out, the fold is not going to work out. As a 1732 // special case, check to see if this means that the result is always 1733 // true or false now. 1734 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ) 1735 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType())); 1736 if (Cmp.getPredicate() == ICmpInst::ICMP_NE) 1737 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType())); 1738 } else { 1739 Value *NewAnd = Builder.CreateAnd( 1740 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst)); 1741 return new ICmpInst(Cmp.getPredicate(), 1742 NewAnd, ConstantInt::get(And->getType(), NewCmpCst)); 1743 } 1744 } 1745 1746 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is 1747 // preferable because it allows the C2 << Y expression to be hoisted out of a 1748 // loop if Y is invariant and X is not. 1749 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() && 1750 !Shift->isArithmeticShift() && 1751 ((!IsShl && C2.isOne()) || !isa<Constant>(Shift->getOperand(0)))) { 1752 // Compute C2 << Y. 1753 Value *NewShift = 1754 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1)) 1755 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1)); 1756 1757 // Compute X & (C2 << Y). 1758 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift); 1759 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1)); 1760 } 1761 1762 return nullptr; 1763 } 1764 1765 /// Fold icmp (and X, C2), C1. 1766 Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp, 1767 BinaryOperator *And, 1768 const APInt &C1) { 1769 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE; 1770 1771 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1 1772 // TODO: We canonicalize to the longer form for scalars because we have 1773 // better analysis/folds for icmp, and codegen may be better with icmp. 1774 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() && 1775 match(And->getOperand(1), m_One())) 1776 return new TruncInst(And->getOperand(0), Cmp.getType()); 1777 1778 const APInt *C2; 1779 Value *X; 1780 if (!match(And, m_And(m_Value(X), m_APInt(C2)))) 1781 return nullptr; 1782 1783 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask 1784 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(~*C2) && 1785 C2->isNegatedPowerOf2()) 1786 return new ICmpInst(ICmpInst::ICMP_SGT, X, 1787 ConstantInt::get(X->getType(), ~*C2)); 1788 // (and X, highmask) s< [1, -highmask] --> X s< -highmask 1789 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() && 1790 (C1 - 1).ule(~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask()) 1791 return new ICmpInst(ICmpInst::ICMP_SLT, X, 1792 ConstantInt::get(X->getType(), -*C2)); 1793 1794 // Don't perform the following transforms if the AND has multiple uses 1795 if (!And->hasOneUse()) 1796 return nullptr; 1797 1798 if (Cmp.isEquality() && C1.isZero()) { 1799 // Restrict this fold to single-use 'and' (PR10267). 1800 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0 1801 if (C2->isSignMask()) { 1802 Constant *Zero = Constant::getNullValue(X->getType()); 1803 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; 1804 return new ICmpInst(NewPred, X, Zero); 1805 } 1806 1807 APInt NewC2 = *C2; 1808 KnownBits Know = computeKnownBits(And->getOperand(0), 0, And); 1809 // Set high zeros of C2 to allow matching negated power-of-2. 1810 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(), 1811 Know.countMinLeadingZeros()); 1812 1813 // Restrict this fold only for single-use 'and' (PR10267). 1814 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two. 1815 if (NewC2.isNegatedPowerOf2()) { 1816 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2); 1817 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; 1818 return new ICmpInst(NewPred, X, NegBOC); 1819 } 1820 } 1821 1822 // If the LHS is an 'and' of a truncate and we can widen the and/compare to 1823 // the input width without changing the value produced, eliminate the cast: 1824 // 1825 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1' 1826 // 1827 // We can do this transformation if the constants do not have their sign bits 1828 // set or if it is an equality comparison. Extending a relational comparison 1829 // when we're checking the sign bit would not work. 1830 Value *W; 1831 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) && 1832 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) { 1833 // TODO: Is this a good transform for vectors? Wider types may reduce 1834 // throughput. Should this transform be limited (even for scalars) by using 1835 // shouldChangeType()? 1836 if (!Cmp.getType()->isVectorTy()) { 1837 Type *WideType = W->getType(); 1838 unsigned WideScalarBits = WideType->getScalarSizeInBits(); 1839 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits)); 1840 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits)); 1841 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName()); 1842 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1); 1843 } 1844 } 1845 1846 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2)) 1847 return I; 1848 1849 // (icmp pred (and (or (lshr A, B), A), 1), 0) --> 1850 // (icmp pred (and A, (or (shl 1, B), 1), 0)) 1851 // 1852 // iff pred isn't signed 1853 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() && 1854 match(And->getOperand(1), m_One())) { 1855 Constant *One = cast<Constant>(And->getOperand(1)); 1856 Value *Or = And->getOperand(0); 1857 Value *A, *B, *LShr; 1858 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) && 1859 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) { 1860 unsigned UsesRemoved = 0; 1861 if (And->hasOneUse()) 1862 ++UsesRemoved; 1863 if (Or->hasOneUse()) 1864 ++UsesRemoved; 1865 if (LShr->hasOneUse()) 1866 ++UsesRemoved; 1867 1868 // Compute A & ((1 << B) | 1) 1869 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3; 1870 if (UsesRemoved >= RequireUsesRemoved) { 1871 Value *NewOr = 1872 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(), 1873 /*HasNUW=*/true), 1874 One, Or->getName()); 1875 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName()); 1876 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1)); 1877 } 1878 } 1879 } 1880 1881 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) --> 1882 // llvm.is.fpclass(X, fcInf|fcNan) 1883 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) --> 1884 // llvm.is.fpclass(X, ~(fcInf|fcNan)) 1885 Value *V; 1886 if (!Cmp.getParent()->getParent()->hasFnAttribute( 1887 Attribute::NoImplicitFloat) && 1888 Cmp.isEquality() && 1889 match(X, m_OneUse(m_ElementWiseBitCast(m_Value(V))))) { 1890 Type *FPType = V->getType()->getScalarType(); 1891 if (FPType->isIEEELikeFPTy() && C1 == *C2) { 1892 APInt ExponentMask = 1893 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt(); 1894 if (C1 == ExponentMask) { 1895 unsigned Mask = FPClassTest::fcNan | FPClassTest::fcInf; 1896 if (isICMP_NE) 1897 Mask = ~Mask & fcAllFlags; 1898 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask)); 1899 } 1900 } 1901 } 1902 1903 return nullptr; 1904 } 1905 1906 /// Fold icmp (and X, Y), C. 1907 Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp, 1908 BinaryOperator *And, 1909 const APInt &C) { 1910 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C)) 1911 return I; 1912 1913 const ICmpInst::Predicate Pred = Cmp.getPredicate(); 1914 bool TrueIfNeg; 1915 if (isSignBitCheck(Pred, C, TrueIfNeg)) { 1916 // ((X - 1) & ~X) < 0 --> X == 0 1917 // ((X - 1) & ~X) >= 0 --> X != 0 1918 Value *X; 1919 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) && 1920 match(And->getOperand(1), m_Not(m_Specific(X)))) { 1921 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE; 1922 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType())); 1923 } 1924 // (X & -X) < 0 --> X == MinSignedC 1925 // (X & -X) > -1 --> X != MinSignedC 1926 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) { 1927 Constant *MinSignedC = ConstantInt::get( 1928 X->getType(), 1929 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits())); 1930 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE; 1931 return new ICmpInst(NewPred, X, MinSignedC); 1932 } 1933 } 1934 1935 // TODO: These all require that Y is constant too, so refactor with the above. 1936 1937 // Try to optimize things like "A[i] & 42 == 0" to index computations. 1938 Value *X = And->getOperand(0); 1939 Value *Y = And->getOperand(1); 1940 if (auto *C2 = dyn_cast<ConstantInt>(Y)) 1941 if (auto *LI = dyn_cast<LoadInst>(X)) 1942 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0))) 1943 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) 1944 if (Instruction *Res = 1945 foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2)) 1946 return Res; 1947 1948 if (!Cmp.isEquality()) 1949 return nullptr; 1950 1951 // X & -C == -C -> X > u ~C 1952 // X & -C != -C -> X <= u ~C 1953 // iff C is a power of 2 1954 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) { 1955 auto NewPred = 1956 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGT : CmpInst::ICMP_ULE; 1957 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1)))); 1958 } 1959 1960 // If we are testing the intersection of 2 select-of-nonzero-constants with no 1961 // common bits set, it's the same as checking if exactly one select condition 1962 // is set: 1963 // ((A ? TC : FC) & (B ? TC : FC)) == 0 --> xor A, B 1964 // ((A ? TC : FC) & (B ? TC : FC)) != 0 --> not(xor A, B) 1965 // TODO: Generalize for non-constant values. 1966 // TODO: Handle signed/unsigned predicates. 1967 // TODO: Handle other bitwise logic connectors. 1968 // TODO: Extend to handle a non-zero compare constant. 1969 if (C.isZero() && (Pred == CmpInst::ICMP_EQ || And->hasOneUse())) { 1970 assert(Cmp.isEquality() && "Not expecting non-equality predicates"); 1971 Value *A, *B; 1972 const APInt *TC, *FC; 1973 if (match(X, m_Select(m_Value(A), m_APInt(TC), m_APInt(FC))) && 1974 match(Y, 1975 m_Select(m_Value(B), m_SpecificInt(*TC), m_SpecificInt(*FC))) && 1976 !TC->isZero() && !FC->isZero() && !TC->intersects(*FC)) { 1977 Value *R = Builder.CreateXor(A, B); 1978 if (Pred == CmpInst::ICMP_NE) 1979 R = Builder.CreateNot(R); 1980 return replaceInstUsesWith(Cmp, R); 1981 } 1982 } 1983 1984 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X) 1985 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X) 1986 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X) 1987 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X) 1988 if (match(And, m_OneUse(m_c_And(m_OneUse(m_ZExt(m_Value(X))), m_Value(Y)))) && 1989 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) { 1990 Value *TruncY = Builder.CreateTrunc(Y, X->getType()); 1991 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) { 1992 Value *And = Builder.CreateAnd(TruncY, X); 1993 return BinaryOperator::CreateNot(And); 1994 } 1995 return BinaryOperator::CreateAnd(TruncY, X); 1996 } 1997 1998 // (icmp eq/ne (and (shl -1, X), Y), 0) 1999 // -> (icmp eq/ne (lshr Y, X), 0) 2000 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems 2001 // highly unlikely the non-zero case will ever show up in code. 2002 if (C.isZero() && 2003 match(And, m_OneUse(m_c_And(m_OneUse(m_Shl(m_AllOnes(), m_Value(X))), 2004 m_Value(Y))))) { 2005 Value *LShr = Builder.CreateLShr(Y, X); 2006 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType())); 2007 } 2008 2009 // (icmp eq/ne (and (add A, Addend), Msk), C) 2010 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk)) 2011 { 2012 Value *A; 2013 const APInt *Addend, *Msk; 2014 if (match(And, m_And(m_OneUse(m_Add(m_Value(A), m_APInt(Addend))), 2015 m_APInt(Msk))) && 2016 Msk->isMask() && C.ule(*Msk)) { 2017 APInt NewComperand = (C - *Addend) & *Msk; 2018 Value* MaskA = Builder.CreateAnd(A, ConstantInt::get(A->getType(), *Msk)); 2019 return new ICmpInst( 2020 Pred, MaskA, 2021 Constant::getIntegerValue(MaskA->getType(), NewComperand)); 2022 } 2023 } 2024 2025 return nullptr; 2026 } 2027 2028 /// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0. 2029 static Value *foldICmpOrXorSubChain(ICmpInst &Cmp, BinaryOperator *Or, 2030 InstCombiner::BuilderTy &Builder) { 2031 // Are we using xors or subs to bitwise check for a pair or pairs of 2032 // (in)equalities? Convert to a shorter form that has more potential to be 2033 // folded even further. 2034 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4) 2035 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4) 2036 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 --> 2037 // (X1 == X2) && (X3 == X4) && (X5 == X6) 2038 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 --> 2039 // (X1 != X2) || (X3 != X4) || (X5 != X6) 2040 SmallVector<std::pair<Value *, Value *>, 2> CmpValues; 2041 SmallVector<Value *, 16> WorkList(1, Or); 2042 2043 while (!WorkList.empty()) { 2044 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) { 2045 Value *Lhs, *Rhs; 2046 2047 if (match(OrOperatorArgument, 2048 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) { 2049 CmpValues.emplace_back(Lhs, Rhs); 2050 return; 2051 } 2052 2053 if (match(OrOperatorArgument, 2054 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) { 2055 CmpValues.emplace_back(Lhs, Rhs); 2056 return; 2057 } 2058 2059 WorkList.push_back(OrOperatorArgument); 2060 }; 2061 2062 Value *CurrentValue = WorkList.pop_back_val(); 2063 Value *OrOperatorLhs, *OrOperatorRhs; 2064 2065 if (!match(CurrentValue, 2066 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) { 2067 return nullptr; 2068 } 2069 2070 MatchOrOperatorArgument(OrOperatorRhs); 2071 MatchOrOperatorArgument(OrOperatorLhs); 2072 } 2073 2074 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2075 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; 2076 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first, 2077 CmpValues.rbegin()->second); 2078 2079 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) { 2080 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second); 2081 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp); 2082 } 2083 2084 return LhsCmp; 2085 } 2086 2087 /// Fold icmp (or X, Y), C. 2088 Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp, 2089 BinaryOperator *Or, 2090 const APInt &C) { 2091 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2092 if (C.isOne()) { 2093 // icmp slt signum(V) 1 --> icmp slt V, 1 2094 Value *V = nullptr; 2095 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V)))) 2096 return new ICmpInst(ICmpInst::ICMP_SLT, V, 2097 ConstantInt::get(V->getType(), 1)); 2098 } 2099 2100 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1); 2101 2102 // (icmp eq/ne (or disjoint x, C0), C1) 2103 // -> (icmp eq/ne x, C0^C1) 2104 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) && 2105 cast<PossiblyDisjointInst>(Or)->isDisjoint()) { 2106 Value *NewC = 2107 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C)); 2108 return new ICmpInst(Pred, OrOp0, NewC); 2109 } 2110 2111 const APInt *MaskC; 2112 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) { 2113 if (*MaskC == C && (C + 1).isPowerOf2()) { 2114 // X | C == C --> X <=u C 2115 // X | C != C --> X >u C 2116 // iff C+1 is a power of 2 (C is a bitmask of the low bits) 2117 Pred = (Pred == CmpInst::ICMP_EQ) ? CmpInst::ICMP_ULE : CmpInst::ICMP_UGT; 2118 return new ICmpInst(Pred, OrOp0, OrOp1); 2119 } 2120 2121 // More general: canonicalize 'equality with set bits mask' to 2122 // 'equality with clear bits mask'. 2123 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC 2124 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC 2125 if (Or->hasOneUse()) { 2126 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC)); 2127 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC)); 2128 return new ICmpInst(Pred, And, NewC); 2129 } 2130 } 2131 2132 // (X | (X-1)) s< 0 --> X s< 1 2133 // (X | (X-1)) s> -1 --> X s> 0 2134 Value *X; 2135 bool TrueIfSigned; 2136 if (isSignBitCheck(Pred, C, TrueIfSigned) && 2137 match(Or, m_c_Or(m_Add(m_Value(X), m_AllOnes()), m_Deferred(X)))) { 2138 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT; 2139 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0); 2140 return new ICmpInst(NewPred, X, NewC); 2141 } 2142 2143 const APInt *OrC; 2144 // icmp(X | OrC, C) --> icmp(X, 0) 2145 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) { 2146 switch (Pred) { 2147 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0 2148 case ICmpInst::ICMP_SLT: 2149 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0 2150 case ICmpInst::ICMP_SGE: 2151 if (OrC->sge(C)) 2152 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType())); 2153 break; 2154 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0 2155 case ICmpInst::ICMP_SLE: 2156 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0 2157 case ICmpInst::ICMP_SGT: 2158 if (OrC->sgt(C)) 2159 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(Pred), X, 2160 ConstantInt::getNullValue(X->getType())); 2161 break; 2162 default: 2163 break; 2164 } 2165 } 2166 2167 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse()) 2168 return nullptr; 2169 2170 Value *P, *Q; 2171 if (match(Or, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) { 2172 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0 2173 // -> and (icmp eq P, null), (icmp eq Q, null). 2174 Value *CmpP = 2175 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType())); 2176 Value *CmpQ = 2177 Builder.CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType())); 2178 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; 2179 return BinaryOperator::Create(BOpc, CmpP, CmpQ); 2180 } 2181 2182 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder)) 2183 return replaceInstUsesWith(Cmp, V); 2184 2185 return nullptr; 2186 } 2187 2188 /// Fold icmp (mul X, Y), C. 2189 Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp, 2190 BinaryOperator *Mul, 2191 const APInt &C) { 2192 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2193 Type *MulTy = Mul->getType(); 2194 Value *X = Mul->getOperand(0); 2195 2196 // If there's no overflow: 2197 // X * X == 0 --> X == 0 2198 // X * X != 0 --> X != 0 2199 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) && 2200 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap())) 2201 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy)); 2202 2203 const APInt *MulC; 2204 if (!match(Mul->getOperand(1), m_APInt(MulC))) 2205 return nullptr; 2206 2207 // If this is a test of the sign bit and the multiply is sign-preserving with 2208 // a constant operand, use the multiply LHS operand instead: 2209 // (X * +MulC) < 0 --> X < 0 2210 // (X * -MulC) < 0 --> X > 0 2211 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) { 2212 if (MulC->isNegative()) 2213 Pred = ICmpInst::getSwappedPredicate(Pred); 2214 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy)); 2215 } 2216 2217 if (MulC->isZero()) 2218 return nullptr; 2219 2220 // If the multiply does not wrap or the constant is odd, try to divide the 2221 // compare constant by the multiplication factor. 2222 if (Cmp.isEquality()) { 2223 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC 2224 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) { 2225 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC)); 2226 return new ICmpInst(Pred, X, NewC); 2227 } 2228 2229 // C % MulC == 0 is weaker than we could use if MulC is odd because it 2230 // correct to transform if MulC * N == C including overflow. I.e with i8 2231 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we 2232 // miss that case. 2233 if (C.urem(*MulC).isZero()) { 2234 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC 2235 // (mul X, OddC) eq/ne N * C --> X eq/ne N 2236 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) { 2237 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC)); 2238 return new ICmpInst(Pred, X, NewC); 2239 } 2240 } 2241 } 2242 2243 // With a matching no-overflow guarantee, fold the constants: 2244 // (X * MulC) < C --> X < (C / MulC) 2245 // (X * MulC) > C --> X > (C / MulC) 2246 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE? 2247 Constant *NewC = nullptr; 2248 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) { 2249 // MININT / -1 --> overflow. 2250 if (C.isMinSignedValue() && MulC->isAllOnes()) 2251 return nullptr; 2252 if (MulC->isNegative()) 2253 Pred = ICmpInst::getSwappedPredicate(Pred); 2254 2255 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) { 2256 NewC = ConstantInt::get( 2257 MulTy, APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::UP)); 2258 } else { 2259 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) && 2260 "Unexpected predicate"); 2261 NewC = ConstantInt::get( 2262 MulTy, APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::DOWN)); 2263 } 2264 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) { 2265 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) { 2266 NewC = ConstantInt::get( 2267 MulTy, APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::UP)); 2268 } else { 2269 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) && 2270 "Unexpected predicate"); 2271 NewC = ConstantInt::get( 2272 MulTy, APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::DOWN)); 2273 } 2274 } 2275 2276 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr; 2277 } 2278 2279 /// Fold icmp (shl nuw C2, Y), C. 2280 static Instruction *foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl, 2281 const APInt &C) { 2282 Value *Y; 2283 const APInt *C2; 2284 if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y)))) 2285 return nullptr; 2286 2287 Type *ShiftType = Shl->getType(); 2288 unsigned TypeBits = C.getBitWidth(); 2289 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2290 if (Cmp.isUnsigned()) { 2291 if (C2->isZero() || C2->ugt(C)) 2292 return nullptr; 2293 APInt Div, Rem; 2294 APInt::udivrem(C, *C2, Div, Rem); 2295 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2(); 2296 2297 // (1 << Y) pred C -> Y pred Log2(C) 2298 if (!CIsPowerOf2) { 2299 // (1 << Y) < 30 -> Y <= 4 2300 // (1 << Y) <= 30 -> Y <= 4 2301 // (1 << Y) >= 30 -> Y > 4 2302 // (1 << Y) > 30 -> Y > 4 2303 if (Pred == ICmpInst::ICMP_ULT) 2304 Pred = ICmpInst::ICMP_ULE; 2305 else if (Pred == ICmpInst::ICMP_UGE) 2306 Pred = ICmpInst::ICMP_UGT; 2307 } 2308 2309 unsigned CLog2 = Div.logBase2(); 2310 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2)); 2311 } else if (Cmp.isSigned() && C2->isOne()) { 2312 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1); 2313 // (1 << Y) > 0 -> Y != 31 2314 // (1 << Y) > C -> Y != 31 if C is negative. 2315 if (Pred == ICmpInst::ICMP_SGT && C.sle(0)) 2316 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne); 2317 2318 // (1 << Y) < 0 -> Y == 31 2319 // (1 << Y) < 1 -> Y == 31 2320 // (1 << Y) < C -> Y == 31 if C is negative and not signed min. 2321 // Exclude signed min by subtracting 1 and lower the upper bound to 0. 2322 if (Pred == ICmpInst::ICMP_SLT && (C-1).sle(0)) 2323 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne); 2324 } 2325 2326 return nullptr; 2327 } 2328 2329 /// Fold icmp (shl X, Y), C. 2330 Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp, 2331 BinaryOperator *Shl, 2332 const APInt &C) { 2333 const APInt *ShiftVal; 2334 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal))) 2335 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal); 2336 2337 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2338 // (icmp pred (shl nuw&nsw X, Y), Csle0) 2339 // -> (icmp pred X, Csle0) 2340 // 2341 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op 2342 // so X's must be what is used. 2343 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap()) 2344 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1)); 2345 2346 // (icmp eq/ne (shl nuw|nsw X, Y), 0) 2347 // -> (icmp eq/ne X, 0) 2348 if (ICmpInst::isEquality(Pred) && C.isZero() && 2349 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap())) 2350 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1)); 2351 2352 // (icmp slt (shl nsw X, Y), 0/1) 2353 // -> (icmp slt X, 0/1) 2354 // (icmp sgt (shl nsw X, Y), 0/-1) 2355 // -> (icmp sgt X, 0/-1) 2356 // 2357 // NB: sge/sle with a constant will canonicalize to sgt/slt. 2358 if (Shl->hasNoSignedWrap() && 2359 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) 2360 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne())) 2361 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1)); 2362 2363 const APInt *ShiftAmt; 2364 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt))) 2365 return foldICmpShlLHSC(Cmp, Shl, C); 2366 2367 // Check that the shift amount is in range. If not, don't perform undefined 2368 // shifts. When the shift is visited, it will be simplified. 2369 unsigned TypeBits = C.getBitWidth(); 2370 if (ShiftAmt->uge(TypeBits)) 2371 return nullptr; 2372 2373 Value *X = Shl->getOperand(0); 2374 Type *ShType = Shl->getType(); 2375 2376 // NSW guarantees that we are only shifting out sign bits from the high bits, 2377 // so we can ASHR the compare constant without needing a mask and eliminate 2378 // the shift. 2379 if (Shl->hasNoSignedWrap()) { 2380 if (Pred == ICmpInst::ICMP_SGT) { 2381 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt) 2382 APInt ShiftedC = C.ashr(*ShiftAmt); 2383 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); 2384 } 2385 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && 2386 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) { 2387 APInt ShiftedC = C.ashr(*ShiftAmt); 2388 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); 2389 } 2390 if (Pred == ICmpInst::ICMP_SLT) { 2391 // SLE is the same as above, but SLE is canonicalized to SLT, so convert: 2392 // (X << S) <=s C is equiv to X <=s (C >> S) for all C 2393 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX 2394 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN 2395 assert(!C.isMinSignedValue() && "Unexpected icmp slt"); 2396 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1; 2397 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); 2398 } 2399 } 2400 2401 // NUW guarantees that we are only shifting out zero bits from the high bits, 2402 // so we can LSHR the compare constant without needing a mask and eliminate 2403 // the shift. 2404 if (Shl->hasNoUnsignedWrap()) { 2405 if (Pred == ICmpInst::ICMP_UGT) { 2406 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt) 2407 APInt ShiftedC = C.lshr(*ShiftAmt); 2408 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); 2409 } 2410 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && 2411 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) { 2412 APInt ShiftedC = C.lshr(*ShiftAmt); 2413 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); 2414 } 2415 if (Pred == ICmpInst::ICMP_ULT) { 2416 // ULE is the same as above, but ULE is canonicalized to ULT, so convert: 2417 // (X << S) <=u C is equiv to X <=u (C >> S) for all C 2418 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u 2419 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0 2420 assert(C.ugt(0) && "ult 0 should have been eliminated"); 2421 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1; 2422 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); 2423 } 2424 } 2425 2426 if (Cmp.isEquality() && Shl->hasOneUse()) { 2427 // Strength-reduce the shift into an 'and'. 2428 Constant *Mask = ConstantInt::get( 2429 ShType, 2430 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue())); 2431 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask"); 2432 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt)); 2433 return new ICmpInst(Pred, And, LShrC); 2434 } 2435 2436 // Otherwise, if this is a comparison of the sign bit, simplify to and/test. 2437 bool TrueIfSigned = false; 2438 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) { 2439 // (X << 31) <s 0 --> (X & 1) != 0 2440 Constant *Mask = ConstantInt::get( 2441 ShType, 2442 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1)); 2443 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask"); 2444 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, 2445 And, Constant::getNullValue(ShType)); 2446 } 2447 2448 // Simplify 'shl' inequality test into 'and' equality test. 2449 if (Cmp.isUnsigned() && Shl->hasOneUse()) { 2450 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0 2451 if ((C + 1).isPowerOf2() && 2452 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) { 2453 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue())); 2454 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ 2455 : ICmpInst::ICMP_NE, 2456 And, Constant::getNullValue(ShType)); 2457 } 2458 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0 2459 if (C.isPowerOf2() && 2460 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) { 2461 Value *And = 2462 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue())); 2463 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ 2464 : ICmpInst::ICMP_NE, 2465 And, Constant::getNullValue(ShType)); 2466 } 2467 } 2468 2469 // Transform (icmp pred iM (shl iM %v, N), C) 2470 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N)) 2471 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N. 2472 // This enables us to get rid of the shift in favor of a trunc that may be 2473 // free on the target. It has the additional benefit of comparing to a 2474 // smaller constant that may be more target-friendly. 2475 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1); 2476 if (Shl->hasOneUse() && Amt != 0 && 2477 shouldChangeType(ShType->getScalarSizeInBits(), TypeBits - Amt)) { 2478 ICmpInst::Predicate CmpPred = Pred; 2479 APInt RHSC = C; 2480 2481 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(CmpPred)) { 2482 // Try the flipped strictness predicate. 2483 // e.g.: 2484 // icmp ult i64 (shl X, 32), 8589934593 -> 2485 // icmp ule i64 (shl X, 32), 8589934592 -> 2486 // icmp ule i32 (trunc X, i32), 2 -> 2487 // icmp ult i32 (trunc X, i32), 3 2488 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant( 2489 Pred, ConstantInt::get(ShType->getContext(), C))) { 2490 CmpPred = FlippedStrictness->first; 2491 RHSC = cast<ConstantInt>(FlippedStrictness->second)->getValue(); 2492 } 2493 } 2494 2495 if (RHSC.countr_zero() >= Amt) { 2496 Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt); 2497 Constant *NewC = 2498 ConstantInt::get(TruncTy, RHSC.ashr(*ShiftAmt).trunc(TypeBits - Amt)); 2499 return new ICmpInst(CmpPred, 2500 Builder.CreateTrunc(X, TruncTy, "", /*IsNUW=*/false, 2501 Shl->hasNoSignedWrap()), 2502 NewC); 2503 } 2504 } 2505 2506 return nullptr; 2507 } 2508 2509 /// Fold icmp ({al}shr X, Y), C. 2510 Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp, 2511 BinaryOperator *Shr, 2512 const APInt &C) { 2513 // An exact shr only shifts out zero bits, so: 2514 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0 2515 Value *X = Shr->getOperand(0); 2516 CmpInst::Predicate Pred = Cmp.getPredicate(); 2517 if (Cmp.isEquality() && Shr->isExact() && C.isZero()) 2518 return new ICmpInst(Pred, X, Cmp.getOperand(1)); 2519 2520 bool IsAShr = Shr->getOpcode() == Instruction::AShr; 2521 const APInt *ShiftValC; 2522 if (match(X, m_APInt(ShiftValC))) { 2523 if (Cmp.isEquality()) 2524 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC); 2525 2526 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0 2527 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0 2528 bool TrueIfSigned; 2529 if (!IsAShr && ShiftValC->isNegative() && 2530 isSignBitCheck(Pred, C, TrueIfSigned)) 2531 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE, 2532 Shr->getOperand(1), 2533 ConstantInt::getNullValue(X->getType())); 2534 2535 // If the shifted constant is a power-of-2, test the shift amount directly: 2536 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC)) 2537 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC)) 2538 if (!IsAShr && ShiftValC->isPowerOf2() && 2539 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) { 2540 bool IsUGT = Pred == CmpInst::ICMP_UGT; 2541 assert(ShiftValC->uge(C) && "Expected simplify of compare"); 2542 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify"); 2543 2544 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero(); 2545 unsigned ShiftLZ = ShiftValC->countl_zero(); 2546 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ); 2547 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE; 2548 return new ICmpInst(NewPred, Shr->getOperand(1), NewC); 2549 } 2550 } 2551 2552 const APInt *ShiftAmtC; 2553 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC))) 2554 return nullptr; 2555 2556 // Check that the shift amount is in range. If not, don't perform undefined 2557 // shifts. When the shift is visited it will be simplified. 2558 unsigned TypeBits = C.getBitWidth(); 2559 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits); 2560 if (ShAmtVal >= TypeBits || ShAmtVal == 0) 2561 return nullptr; 2562 2563 bool IsExact = Shr->isExact(); 2564 Type *ShrTy = Shr->getType(); 2565 // TODO: If we could guarantee that InstSimplify would handle all of the 2566 // constant-value-based preconditions in the folds below, then we could assert 2567 // those conditions rather than checking them. This is difficult because of 2568 // undef/poison (PR34838). 2569 if (IsAShr && Shr->hasOneUse()) { 2570 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) && 2571 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) { 2572 // When C - 1 is a power of two and the transform can be legally 2573 // performed, prefer this form so the produced constant is close to a 2574 // power of two. 2575 // icmp slt/ult (ashr exact X, ShAmtC), C 2576 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1 2577 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1; 2578 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); 2579 } 2580 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) { 2581 // When ShAmtC can be shifted losslessly: 2582 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC) 2583 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC) 2584 APInt ShiftedC = C.shl(ShAmtVal); 2585 if (ShiftedC.ashr(ShAmtVal) == C) 2586 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); 2587 } 2588 if (Pred == CmpInst::ICMP_SGT) { 2589 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1 2590 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1; 2591 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() && 2592 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1)) 2593 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); 2594 } 2595 if (Pred == CmpInst::ICMP_UGT) { 2596 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1 2597 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd 2598 // clause accounts for that pattern. 2599 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1; 2600 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) || 2601 (C + 1).shl(ShAmtVal).isMinSignedValue()) 2602 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); 2603 } 2604 2605 // If the compare constant has significant bits above the lowest sign-bit, 2606 // then convert an unsigned cmp to a test of the sign-bit: 2607 // (ashr X, ShiftC) u> C --> X s< 0 2608 // (ashr X, ShiftC) u< C --> X s> -1 2609 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) { 2610 if (Pred == CmpInst::ICMP_UGT) { 2611 return new ICmpInst(CmpInst::ICMP_SLT, X, 2612 ConstantInt::getNullValue(ShrTy)); 2613 } 2614 if (Pred == CmpInst::ICMP_ULT) { 2615 return new ICmpInst(CmpInst::ICMP_SGT, X, 2616 ConstantInt::getAllOnesValue(ShrTy)); 2617 } 2618 } 2619 } else if (!IsAShr) { 2620 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) { 2621 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC) 2622 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC) 2623 APInt ShiftedC = C.shl(ShAmtVal); 2624 if (ShiftedC.lshr(ShAmtVal) == C) 2625 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); 2626 } 2627 if (Pred == CmpInst::ICMP_UGT) { 2628 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1 2629 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1; 2630 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1)) 2631 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); 2632 } 2633 } 2634 2635 if (!Cmp.isEquality()) 2636 return nullptr; 2637 2638 // Handle equality comparisons of shift-by-constant. 2639 2640 // If the comparison constant changes with the shift, the comparison cannot 2641 // succeed (bits of the comparison constant cannot match the shifted value). 2642 // This should be known by InstSimplify and already be folded to true/false. 2643 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) || 2644 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) && 2645 "Expected icmp+shr simplify did not occur."); 2646 2647 // If the bits shifted out are known zero, compare the unshifted value: 2648 // (X & 4) >> 1 == 2 --> (X & 4) == 4. 2649 if (Shr->isExact()) 2650 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal)); 2651 2652 if (C.isZero()) { 2653 // == 0 is u< 1. 2654 if (Pred == CmpInst::ICMP_EQ) 2655 return new ICmpInst(CmpInst::ICMP_ULT, X, 2656 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal))); 2657 else 2658 return new ICmpInst(CmpInst::ICMP_UGT, X, 2659 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal) - 1)); 2660 } 2661 2662 if (Shr->hasOneUse()) { 2663 // Canonicalize the shift into an 'and': 2664 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt) 2665 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); 2666 Constant *Mask = ConstantInt::get(ShrTy, Val); 2667 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask"); 2668 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal)); 2669 } 2670 2671 return nullptr; 2672 } 2673 2674 Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp, 2675 BinaryOperator *SRem, 2676 const APInt &C) { 2677 const ICmpInst::Predicate Pred = Cmp.getPredicate(); 2678 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) { 2679 // Canonicalize unsigned predicates to signed: 2680 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 0 2681 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1 2682 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1 2683 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1 2684 2685 const APInt *DivisorC; 2686 if (!match(SRem->getOperand(1), m_APInt(DivisorC))) 2687 return nullptr; 2688 2689 APInt NormalizedC = C; 2690 if (Pred == ICmpInst::ICMP_ULT) { 2691 assert(!NormalizedC.isZero() && 2692 "ult X, 0 should have been simplified already."); 2693 --NormalizedC; 2694 } 2695 if (C.isNegative()) 2696 NormalizedC.flipAllBits(); 2697 assert(!DivisorC->isZero() && 2698 "srem X, 0 should have been simplified already."); 2699 if (!NormalizedC.uge(DivisorC->abs() - 1)) 2700 return nullptr; 2701 2702 Type *Ty = SRem->getType(); 2703 if (Pred == ICmpInst::ICMP_UGT) 2704 return new ICmpInst(ICmpInst::ICMP_SLT, SRem, 2705 ConstantInt::getNullValue(Ty)); 2706 return new ICmpInst(ICmpInst::ICMP_SGT, SRem, 2707 ConstantInt::getAllOnesValue(Ty)); 2708 } 2709 // Match an 'is positive' or 'is negative' comparison of remainder by a 2710 // constant power-of-2 value: 2711 // (X % pow2C) sgt/slt 0 2712 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT && 2713 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE) 2714 return nullptr; 2715 2716 // TODO: The one-use check is standard because we do not typically want to 2717 // create longer instruction sequences, but this might be a special-case 2718 // because srem is not good for analysis or codegen. 2719 if (!SRem->hasOneUse()) 2720 return nullptr; 2721 2722 const APInt *DivisorC; 2723 if (!match(SRem->getOperand(1), m_Power2(DivisorC))) 2724 return nullptr; 2725 2726 // For cmp_sgt/cmp_slt only zero valued C is handled. 2727 // For cmp_eq/cmp_ne only positive valued C is handled. 2728 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) && 2729 !C.isZero()) || 2730 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && 2731 !C.isStrictlyPositive())) 2732 return nullptr; 2733 2734 // Mask off the sign bit and the modulo bits (low-bits). 2735 Type *Ty = SRem->getType(); 2736 APInt SignMask = APInt::getSignMask(Ty->getScalarSizeInBits()); 2737 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1)); 2738 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC); 2739 2740 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) 2741 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C)); 2742 2743 // For 'is positive?' check that the sign-bit is clear and at least 1 masked 2744 // bit is set. Example: 2745 // (i8 X % 32) s> 0 --> (X & 159) s> 0 2746 if (Pred == ICmpInst::ICMP_SGT) 2747 return new ICmpInst(ICmpInst::ICMP_SGT, And, ConstantInt::getNullValue(Ty)); 2748 2749 // For 'is negative?' check that the sign-bit is set and at least 1 masked 2750 // bit is set. Example: 2751 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768 2752 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask)); 2753 } 2754 2755 /// Fold icmp (udiv X, Y), C. 2756 Instruction *InstCombinerImpl::foldICmpUDivConstant(ICmpInst &Cmp, 2757 BinaryOperator *UDiv, 2758 const APInt &C) { 2759 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2760 Value *X = UDiv->getOperand(0); 2761 Value *Y = UDiv->getOperand(1); 2762 Type *Ty = UDiv->getType(); 2763 2764 const APInt *C2; 2765 if (!match(X, m_APInt(C2))) 2766 return nullptr; 2767 2768 assert(*C2 != 0 && "udiv 0, X should have been simplified already."); 2769 2770 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1)) 2771 if (Pred == ICmpInst::ICMP_UGT) { 2772 assert(!C.isMaxValue() && 2773 "icmp ugt X, UINT_MAX should have been simplified already."); 2774 return new ICmpInst(ICmpInst::ICMP_ULE, Y, 2775 ConstantInt::get(Ty, C2->udiv(C + 1))); 2776 } 2777 2778 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C) 2779 if (Pred == ICmpInst::ICMP_ULT) { 2780 assert(C != 0 && "icmp ult X, 0 should have been simplified already."); 2781 return new ICmpInst(ICmpInst::ICMP_UGT, Y, 2782 ConstantInt::get(Ty, C2->udiv(C))); 2783 } 2784 2785 return nullptr; 2786 } 2787 2788 /// Fold icmp ({su}div X, Y), C. 2789 Instruction *InstCombinerImpl::foldICmpDivConstant(ICmpInst &Cmp, 2790 BinaryOperator *Div, 2791 const APInt &C) { 2792 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2793 Value *X = Div->getOperand(0); 2794 Value *Y = Div->getOperand(1); 2795 Type *Ty = Div->getType(); 2796 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv; 2797 2798 // If unsigned division and the compare constant is bigger than 2799 // UMAX/2 (negative), there's only one pair of values that satisfies an 2800 // equality check, so eliminate the division: 2801 // (X u/ Y) == C --> (X == C) && (Y == 1) 2802 // (X u/ Y) != C --> (X != C) || (Y != 1) 2803 // Similarly, if signed division and the compare constant is exactly SMIN: 2804 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1) 2805 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1) 2806 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() && 2807 (!DivIsSigned || C.isMinSignedValue())) { 2808 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C)); 2809 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1)); 2810 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; 2811 return BinaryOperator::Create(Logic, XBig, YOne); 2812 } 2813 2814 // Fold: icmp pred ([us]div X, C2), C -> range test 2815 // Fold this div into the comparison, producing a range check. 2816 // Determine, based on the divide type, what the range is being 2817 // checked. If there is an overflow on the low or high side, remember 2818 // it, otherwise compute the range [low, hi) bounding the new value. 2819 // See: InsertRangeTest above for the kinds of replacements possible. 2820 const APInt *C2; 2821 if (!match(Y, m_APInt(C2))) 2822 return nullptr; 2823 2824 // FIXME: If the operand types don't match the type of the divide 2825 // then don't attempt this transform. The code below doesn't have the 2826 // logic to deal with a signed divide and an unsigned compare (and 2827 // vice versa). This is because (x /s C2) <s C produces different 2828 // results than (x /s C2) <u C or (x /u C2) <s C or even 2829 // (x /u C2) <u C. Simply casting the operands and result won't 2830 // work. :( The if statement below tests that condition and bails 2831 // if it finds it. 2832 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned()) 2833 return nullptr; 2834 2835 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with 2836 // INT_MIN will also fail if the divisor is 1. Although folds of all these 2837 // division-by-constant cases should be present, we can not assert that they 2838 // have happened before we reach this icmp instruction. 2839 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes())) 2840 return nullptr; 2841 2842 // Compute Prod = C * C2. We are essentially solving an equation of 2843 // form X / C2 = C. We solve for X by multiplying C2 and C. 2844 // By solving for X, we can turn this into a range check instead of computing 2845 // a divide. 2846 APInt Prod = C * *C2; 2847 2848 // Determine if the product overflows by seeing if the product is not equal to 2849 // the divide. Make sure we do the same kind of divide as in the LHS 2850 // instruction that we're folding. 2851 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C; 2852 2853 // If the division is known to be exact, then there is no remainder from the 2854 // divide, so the covered range size is unit, otherwise it is the divisor. 2855 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2; 2856 2857 // Figure out the interval that is being checked. For example, a comparison 2858 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). 2859 // Compute this interval based on the constants involved and the signedness of 2860 // the compare/divide. This computes a half-open interval, keeping track of 2861 // whether either value in the interval overflows. After analysis each 2862 // overflow variable is set to 0 if it's corresponding bound variable is valid 2863 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. 2864 int LoOverflow = 0, HiOverflow = 0; 2865 APInt LoBound, HiBound; 2866 2867 if (!DivIsSigned) { // udiv 2868 // e.g. X/5 op 3 --> [15, 20) 2869 LoBound = Prod; 2870 HiOverflow = LoOverflow = ProdOV; 2871 if (!HiOverflow) { 2872 // If this is not an exact divide, then many values in the range collapse 2873 // to the same result value. 2874 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false); 2875 } 2876 } else if (C2->isStrictlyPositive()) { // Divisor is > 0. 2877 if (C.isZero()) { // (X / pos) op 0 2878 // Can't overflow. e.g. X/2 op 0 --> [-1, 2) 2879 LoBound = -(RangeSize - 1); 2880 HiBound = RangeSize; 2881 } else if (C.isStrictlyPositive()) { // (X / pos) op pos 2882 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) 2883 HiOverflow = LoOverflow = ProdOV; 2884 if (!HiOverflow) 2885 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true); 2886 } else { // (X / pos) op neg 2887 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) 2888 HiBound = Prod + 1; 2889 LoOverflow = HiOverflow = ProdOV ? -1 : 0; 2890 if (!LoOverflow) { 2891 APInt DivNeg = -RangeSize; 2892 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0; 2893 } 2894 } 2895 } else if (C2->isNegative()) { // Divisor is < 0. 2896 if (Div->isExact()) 2897 RangeSize.negate(); 2898 if (C.isZero()) { // (X / neg) op 0 2899 // e.g. X/-5 op 0 --> [-4, 5) 2900 LoBound = RangeSize + 1; 2901 HiBound = -RangeSize; 2902 if (HiBound == *C2) { // -INTMIN = INTMIN 2903 HiOverflow = 1; // [INTMIN+1, overflow) 2904 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN 2905 } 2906 } else if (C.isStrictlyPositive()) { // (X / neg) op pos 2907 // e.g. X/-5 op 3 --> [-19, -14) 2908 HiBound = Prod + 1; 2909 HiOverflow = LoOverflow = ProdOV ? -1 : 0; 2910 if (!LoOverflow) 2911 LoOverflow = 2912 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0; 2913 } else { // (X / neg) op neg 2914 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20) 2915 LoOverflow = HiOverflow = ProdOV; 2916 if (!HiOverflow) 2917 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true); 2918 } 2919 2920 // Dividing by a negative swaps the condition. LT <-> GT 2921 Pred = ICmpInst::getSwappedPredicate(Pred); 2922 } 2923 2924 switch (Pred) { 2925 default: 2926 llvm_unreachable("Unhandled icmp predicate!"); 2927 case ICmpInst::ICMP_EQ: 2928 if (LoOverflow && HiOverflow) 2929 return replaceInstUsesWith(Cmp, Builder.getFalse()); 2930 if (HiOverflow) 2931 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, 2932 X, ConstantInt::get(Ty, LoBound)); 2933 if (LoOverflow) 2934 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, 2935 X, ConstantInt::get(Ty, HiBound)); 2936 return replaceInstUsesWith( 2937 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true)); 2938 case ICmpInst::ICMP_NE: 2939 if (LoOverflow && HiOverflow) 2940 return replaceInstUsesWith(Cmp, Builder.getTrue()); 2941 if (HiOverflow) 2942 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, 2943 X, ConstantInt::get(Ty, LoBound)); 2944 if (LoOverflow) 2945 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, 2946 X, ConstantInt::get(Ty, HiBound)); 2947 return replaceInstUsesWith( 2948 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false)); 2949 case ICmpInst::ICMP_ULT: 2950 case ICmpInst::ICMP_SLT: 2951 if (LoOverflow == +1) // Low bound is greater than input range. 2952 return replaceInstUsesWith(Cmp, Builder.getTrue()); 2953 if (LoOverflow == -1) // Low bound is less than input range. 2954 return replaceInstUsesWith(Cmp, Builder.getFalse()); 2955 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound)); 2956 case ICmpInst::ICMP_UGT: 2957 case ICmpInst::ICMP_SGT: 2958 if (HiOverflow == +1) // High bound greater than input range. 2959 return replaceInstUsesWith(Cmp, Builder.getFalse()); 2960 if (HiOverflow == -1) // High bound less than input range. 2961 return replaceInstUsesWith(Cmp, Builder.getTrue()); 2962 if (Pred == ICmpInst::ICMP_UGT) 2963 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound)); 2964 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound)); 2965 } 2966 2967 return nullptr; 2968 } 2969 2970 /// Fold icmp (sub X, Y), C. 2971 Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp, 2972 BinaryOperator *Sub, 2973 const APInt &C) { 2974 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1); 2975 ICmpInst::Predicate Pred = Cmp.getPredicate(); 2976 Type *Ty = Sub->getType(); 2977 2978 // (SubC - Y) == C) --> Y == (SubC - C) 2979 // (SubC - Y) != C) --> Y != (SubC - C) 2980 Constant *SubC; 2981 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) { 2982 return new ICmpInst(Pred, Y, 2983 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C))); 2984 } 2985 2986 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C) 2987 const APInt *C2; 2988 APInt SubResult; 2989 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate(); 2990 bool HasNSW = Sub->hasNoSignedWrap(); 2991 bool HasNUW = Sub->hasNoUnsignedWrap(); 2992 if (match(X, m_APInt(C2)) && 2993 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) && 2994 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned())) 2995 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult)); 2996 2997 // X - Y == 0 --> X == Y. 2998 // X - Y != 0 --> X != Y. 2999 // TODO: We allow this with multiple uses as long as the other uses are not 3000 // in phis. The phi use check is guarding against a codegen regression 3001 // for a loop test. If the backend could undo this (and possibly 3002 // subsequent transforms), we would not need this hack. 3003 if (Cmp.isEquality() && C.isZero() && 3004 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); })) 3005 return new ICmpInst(Pred, X, Y); 3006 3007 // The following transforms are only worth it if the only user of the subtract 3008 // is the icmp. 3009 // TODO: This is an artificial restriction for all of the transforms below 3010 // that only need a single replacement icmp. Can these use the phi test 3011 // like the transform above here? 3012 if (!Sub->hasOneUse()) 3013 return nullptr; 3014 3015 if (Sub->hasNoSignedWrap()) { 3016 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y) 3017 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes()) 3018 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y); 3019 3020 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y) 3021 if (Pred == ICmpInst::ICMP_SGT && C.isZero()) 3022 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y); 3023 3024 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y) 3025 if (Pred == ICmpInst::ICMP_SLT && C.isZero()) 3026 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y); 3027 3028 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y) 3029 if (Pred == ICmpInst::ICMP_SLT && C.isOne()) 3030 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y); 3031 } 3032 3033 if (!match(X, m_APInt(C2))) 3034 return nullptr; 3035 3036 // C2 - Y <u C -> (Y | (C - 1)) == C2 3037 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2 3038 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && 3039 (*C2 & (C - 1)) == (C - 1)) 3040 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X); 3041 3042 // C2 - Y >u C -> (Y | C) != C2 3043 // iff C2 & C == C and C + 1 is a power of 2 3044 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C) 3045 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X); 3046 3047 // We have handled special cases that reduce. 3048 // Canonicalize any remaining sub to add as: 3049 // (C2 - Y) > C --> (Y + ~C2) < ~C 3050 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub", 3051 HasNUW, HasNSW); 3052 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C)); 3053 } 3054 3055 static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0, 3056 Value *Op1, IRBuilderBase &Builder, 3057 bool HasOneUse) { 3058 auto FoldConstant = [&](bool Val) { 3059 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse(); 3060 if (Op0->getType()->isVectorTy()) 3061 Res = ConstantVector::getSplat( 3062 cast<VectorType>(Op0->getType())->getElementCount(), Res); 3063 return Res; 3064 }; 3065 3066 switch (Table.to_ulong()) { 3067 case 0: // 0 0 0 0 3068 return FoldConstant(false); 3069 case 1: // 0 0 0 1 3070 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr; 3071 case 2: // 0 0 1 0 3072 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr; 3073 case 3: // 0 0 1 1 3074 return Builder.CreateNot(Op0); 3075 case 4: // 0 1 0 0 3076 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr; 3077 case 5: // 0 1 0 1 3078 return Builder.CreateNot(Op1); 3079 case 6: // 0 1 1 0 3080 return Builder.CreateXor(Op0, Op1); 3081 case 7: // 0 1 1 1 3082 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr; 3083 case 8: // 1 0 0 0 3084 return Builder.CreateAnd(Op0, Op1); 3085 case 9: // 1 0 0 1 3086 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr; 3087 case 10: // 1 0 1 0 3088 return Op1; 3089 case 11: // 1 0 1 1 3090 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr; 3091 case 12: // 1 1 0 0 3092 return Op0; 3093 case 13: // 1 1 0 1 3094 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr; 3095 case 14: // 1 1 1 0 3096 return Builder.CreateOr(Op0, Op1); 3097 case 15: // 1 1 1 1 3098 return FoldConstant(true); 3099 default: 3100 llvm_unreachable("Invalid Operation"); 3101 } 3102 return nullptr; 3103 } 3104 3105 /// Fold icmp (add X, Y), C. 3106 Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp, 3107 BinaryOperator *Add, 3108 const APInt &C) { 3109 Value *Y = Add->getOperand(1); 3110 Value *X = Add->getOperand(0); 3111 3112 Value *Op0, *Op1; 3113 Instruction *Ext0, *Ext1; 3114 const CmpInst::Predicate Pred = Cmp.getPredicate(); 3115 if (match(Add, 3116 m_Add(m_CombineAnd(m_Instruction(Ext0), m_ZExtOrSExt(m_Value(Op0))), 3117 m_CombineAnd(m_Instruction(Ext1), 3118 m_ZExtOrSExt(m_Value(Op1))))) && 3119 Op0->getType()->isIntOrIntVectorTy(1) && 3120 Op1->getType()->isIntOrIntVectorTy(1)) { 3121 unsigned BW = C.getBitWidth(); 3122 std::bitset<4> Table; 3123 auto ComputeTable = [&](bool Op0Val, bool Op1Val) { 3124 APInt Res(BW, 0); 3125 if (Op0Val) 3126 Res += APInt(BW, isa<ZExtInst>(Ext0) ? 1 : -1, /*isSigned=*/true); 3127 if (Op1Val) 3128 Res += APInt(BW, isa<ZExtInst>(Ext1) ? 1 : -1, /*isSigned=*/true); 3129 return ICmpInst::compare(Res, C, Pred); 3130 }; 3131 3132 Table[0] = ComputeTable(false, false); 3133 Table[1] = ComputeTable(false, true); 3134 Table[2] = ComputeTable(true, false); 3135 Table[3] = ComputeTable(true, true); 3136 if (auto *Cond = 3137 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse())) 3138 return replaceInstUsesWith(Cmp, Cond); 3139 } 3140 const APInt *C2; 3141 if (Cmp.isEquality() || !match(Y, m_APInt(C2))) 3142 return nullptr; 3143 3144 // Fold icmp pred (add X, C2), C. 3145 Type *Ty = Add->getType(); 3146 3147 // If the add does not wrap, we can always adjust the compare by subtracting 3148 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE 3149 // are canonicalized to SGT/SLT/UGT/ULT. 3150 if ((Add->hasNoSignedWrap() && 3151 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) || 3152 (Add->hasNoUnsignedWrap() && 3153 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) { 3154 bool Overflow; 3155 APInt NewC = 3156 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow); 3157 // If there is overflow, the result must be true or false. 3158 // TODO: Can we assert there is no overflow because InstSimplify always 3159 // handles those cases? 3160 if (!Overflow) 3161 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2) 3162 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC)); 3163 } 3164 3165 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() && 3166 C.isNonNegative() && (C - *C2).isNonNegative() && 3167 computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative()) 3168 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X, 3169 ConstantInt::get(Ty, C - *C2)); 3170 3171 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2); 3172 const APInt &Upper = CR.getUpper(); 3173 const APInt &Lower = CR.getLower(); 3174 if (Cmp.isSigned()) { 3175 if (Lower.isSignMask()) 3176 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper)); 3177 if (Upper.isSignMask()) 3178 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower)); 3179 } else { 3180 if (Lower.isMinValue()) 3181 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper)); 3182 if (Upper.isMinValue()) 3183 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower)); 3184 } 3185 3186 // This set of folds is intentionally placed after folds that use no-wrapping 3187 // flags because those folds are likely better for later analysis/codegen. 3188 const APInt SMax = APInt::getSignedMaxValue(Ty->getScalarSizeInBits()); 3189 const APInt SMin = APInt::getSignedMinValue(Ty->getScalarSizeInBits()); 3190 3191 // Fold compare with offset to opposite sign compare if it eliminates offset: 3192 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX) 3193 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax) 3194 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2))); 3195 3196 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN) 3197 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin) 3198 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2))); 3199 3200 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1) 3201 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1) 3202 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C)); 3203 3204 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2) 3205 if (Pred == CmpInst::ICMP_SLT && C == *C2) 3206 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax)); 3207 3208 // (X + -1) <u C --> X <=u C (if X is never null) 3209 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) { 3210 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp); 3211 if (llvm::isKnownNonZero(X, Q)) 3212 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C)); 3213 } 3214 3215 if (!Add->hasOneUse()) 3216 return nullptr; 3217 3218 // X+C <u C2 -> (X & -C2) == C 3219 // iff C & (C2-1) == 0 3220 // C2 is a power of 2 3221 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0) 3222 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(X, -C), 3223 ConstantExpr::getNeg(cast<Constant>(Y))); 3224 3225 // X+C2 <u C -> (X & C) == 2C 3226 // iff C == -(C2) 3227 // C2 is a power of 2 3228 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2) 3229 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, C), 3230 ConstantInt::get(Ty, C * 2)); 3231 3232 // X+C >u C2 -> (X & ~C2) != C 3233 // iff C & C2 == 0 3234 // C2+1 is a power of 2 3235 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0) 3236 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, ~C), 3237 ConstantExpr::getNeg(cast<Constant>(Y))); 3238 3239 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize 3240 // to the ult form. 3241 // X+C2 >u C -> X+(C2-C-1) <u ~C 3242 if (Pred == ICmpInst::ICMP_UGT) 3243 return new ICmpInst(ICmpInst::ICMP_ULT, 3244 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)), 3245 ConstantInt::get(Ty, ~C)); 3246 3247 // zext(V) + C2 pred C -> V + C3 pred' C4 3248 Value *V; 3249 if (match(X, m_ZExt(m_Value(V)))) { 3250 Type *NewCmpTy = V->getType(); 3251 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits(); 3252 if (shouldChangeType(Ty, NewCmpTy)) { 3253 if (CR.getActiveBits() <= NewCmpBW) { 3254 ConstantRange SrcCR = CR.truncate(NewCmpBW); 3255 CmpInst::Predicate EquivPred; 3256 APInt EquivInt; 3257 APInt EquivOffset; 3258 3259 SrcCR.getEquivalentICmp(EquivPred, EquivInt, EquivOffset); 3260 return new ICmpInst( 3261 EquivPred, 3262 EquivOffset.isZero() 3263 ? V 3264 : Builder.CreateAdd(V, ConstantInt::get(NewCmpTy, EquivOffset)), 3265 ConstantInt::get(NewCmpTy, EquivInt)); 3266 } 3267 } 3268 } 3269 3270 return nullptr; 3271 } 3272 3273 bool InstCombinerImpl::matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, 3274 Value *&RHS, ConstantInt *&Less, 3275 ConstantInt *&Equal, 3276 ConstantInt *&Greater) { 3277 // TODO: Generalize this to work with other comparison idioms or ensure 3278 // they get canonicalized into this form. 3279 3280 // select i1 (a == b), 3281 // i32 Equal, 3282 // i32 (select i1 (a < b), i32 Less, i32 Greater) 3283 // where Equal, Less and Greater are placeholders for any three constants. 3284 CmpPredicate PredA; 3285 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) || 3286 !ICmpInst::isEquality(PredA)) 3287 return false; 3288 Value *EqualVal = SI->getTrueValue(); 3289 Value *UnequalVal = SI->getFalseValue(); 3290 // We still can get non-canonical predicate here, so canonicalize. 3291 if (PredA == ICmpInst::ICMP_NE) 3292 std::swap(EqualVal, UnequalVal); 3293 if (!match(EqualVal, m_ConstantInt(Equal))) 3294 return false; 3295 CmpPredicate PredB; 3296 Value *LHS2, *RHS2; 3297 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)), 3298 m_ConstantInt(Less), m_ConstantInt(Greater)))) 3299 return false; 3300 // We can get predicate mismatch here, so canonicalize if possible: 3301 // First, ensure that 'LHS' match. 3302 if (LHS2 != LHS) { 3303 // x sgt y <--> y slt x 3304 std::swap(LHS2, RHS2); 3305 PredB = ICmpInst::getSwappedPredicate(PredB); 3306 } 3307 if (LHS2 != LHS) 3308 return false; 3309 // We also need to canonicalize 'RHS'. 3310 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) { 3311 // x sgt C-1 <--> x sge C <--> not(x slt C) 3312 auto FlippedStrictness = 3313 getFlippedStrictnessPredicateAndConstant(PredB, cast<Constant>(RHS2)); 3314 if (!FlippedStrictness) 3315 return false; 3316 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE && 3317 "basic correctness failure"); 3318 RHS2 = FlippedStrictness->second; 3319 // And kind-of perform the result swap. 3320 std::swap(Less, Greater); 3321 PredB = ICmpInst::ICMP_SLT; 3322 } 3323 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2; 3324 } 3325 3326 Instruction *InstCombinerImpl::foldICmpSelectConstant(ICmpInst &Cmp, 3327 SelectInst *Select, 3328 ConstantInt *C) { 3329 3330 assert(C && "Cmp RHS should be a constant int!"); 3331 // If we're testing a constant value against the result of a three way 3332 // comparison, the result can be expressed directly in terms of the 3333 // original values being compared. Note: We could possibly be more 3334 // aggressive here and remove the hasOneUse test. The original select is 3335 // really likely to simplify or sink when we remove a test of the result. 3336 Value *OrigLHS, *OrigRHS; 3337 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan; 3338 if (Cmp.hasOneUse() && 3339 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal, 3340 C3GreaterThan)) { 3341 assert(C1LessThan && C2Equal && C3GreaterThan); 3342 3343 bool TrueWhenLessThan = ICmpInst::compare( 3344 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate()); 3345 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(), 3346 Cmp.getPredicate()); 3347 bool TrueWhenGreaterThan = ICmpInst::compare( 3348 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate()); 3349 3350 // This generates the new instruction that will replace the original Cmp 3351 // Instruction. Instead of enumerating the various combinations when 3352 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus 3353 // false, we rely on chaining of ORs and future passes of InstCombine to 3354 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b). 3355 3356 // When none of the three constants satisfy the predicate for the RHS (C), 3357 // the entire original Cmp can be simplified to a false. 3358 Value *Cond = Builder.getFalse(); 3359 if (TrueWhenLessThan) 3360 Cond = Builder.CreateOr(Cond, Builder.CreateICmp(ICmpInst::ICMP_SLT, 3361 OrigLHS, OrigRHS)); 3362 if (TrueWhenEqual) 3363 Cond = Builder.CreateOr(Cond, Builder.CreateICmp(ICmpInst::ICMP_EQ, 3364 OrigLHS, OrigRHS)); 3365 if (TrueWhenGreaterThan) 3366 Cond = Builder.CreateOr(Cond, Builder.CreateICmp(ICmpInst::ICMP_SGT, 3367 OrigLHS, OrigRHS)); 3368 3369 return replaceInstUsesWith(Cmp, Cond); 3370 } 3371 return nullptr; 3372 } 3373 3374 Instruction *InstCombinerImpl::foldICmpBitCast(ICmpInst &Cmp) { 3375 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0)); 3376 if (!Bitcast) 3377 return nullptr; 3378 3379 ICmpInst::Predicate Pred = Cmp.getPredicate(); 3380 Value *Op1 = Cmp.getOperand(1); 3381 Value *BCSrcOp = Bitcast->getOperand(0); 3382 Type *SrcType = Bitcast->getSrcTy(); 3383 Type *DstType = Bitcast->getType(); 3384 3385 // Make sure the bitcast doesn't change between scalar and vector and 3386 // doesn't change the number of vector elements. 3387 if (SrcType->isVectorTy() == DstType->isVectorTy() && 3388 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) { 3389 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast. 3390 Value *X; 3391 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) { 3392 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0 3393 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0 3394 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0 3395 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0 3396 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT || 3397 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) && 3398 match(Op1, m_Zero())) 3399 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType())); 3400 3401 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1 3402 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One())) 3403 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1)); 3404 3405 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1 3406 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes())) 3407 return new ICmpInst(Pred, X, 3408 ConstantInt::getAllOnesValue(X->getType())); 3409 } 3410 3411 // Zero-equality checks are preserved through unsigned floating-point casts: 3412 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0 3413 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0 3414 if (match(BCSrcOp, m_UIToFP(m_Value(X)))) 3415 if (Cmp.isEquality() && match(Op1, m_Zero())) 3416 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType())); 3417 3418 const APInt *C; 3419 bool TrueIfSigned; 3420 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) { 3421 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate 3422 // the FP extend/truncate because that cast does not change the sign-bit. 3423 // This is true for all standard IEEE-754 types and the X86 80-bit type. 3424 // The sign-bit is always the most significant bit in those types. 3425 if (isSignBitCheck(Pred, *C, TrueIfSigned) && 3426 (match(BCSrcOp, m_FPExt(m_Value(X))) || 3427 match(BCSrcOp, m_FPTrunc(m_Value(X))))) { 3428 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0 3429 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1 3430 Type *XType = X->getType(); 3431 3432 // We can't currently handle Power style floating point operations here. 3433 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) { 3434 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits()); 3435 if (auto *XVTy = dyn_cast<VectorType>(XType)) 3436 NewType = VectorType::get(NewType, XVTy->getElementCount()); 3437 Value *NewBitcast = Builder.CreateBitCast(X, NewType); 3438 if (TrueIfSigned) 3439 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast, 3440 ConstantInt::getNullValue(NewType)); 3441 else 3442 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast, 3443 ConstantInt::getAllOnesValue(NewType)); 3444 } 3445 } 3446 3447 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class) 3448 Type *FPType = SrcType->getScalarType(); 3449 if (!Cmp.getParent()->getParent()->hasFnAttribute( 3450 Attribute::NoImplicitFloat) && 3451 Cmp.isEquality() && FPType->isIEEELikeFPTy()) { 3452 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify(); 3453 if (Mask & (fcInf | fcZero)) { 3454 if (Pred == ICmpInst::ICMP_NE) 3455 Mask = ~Mask; 3456 return replaceInstUsesWith(Cmp, 3457 Builder.createIsFPClass(BCSrcOp, Mask)); 3458 } 3459 } 3460 } 3461 } 3462 3463 const APInt *C; 3464 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() || 3465 !SrcType->isIntOrIntVectorTy()) 3466 return nullptr; 3467 3468 // If this is checking if all elements of a vector compare are set or not, 3469 // invert the casted vector equality compare and test if all compare 3470 // elements are clear or not. Compare against zero is generally easier for 3471 // analysis and codegen. 3472 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0 3473 // Example: are all elements equal? --> are zero elements not equal? 3474 // TODO: Try harder to reduce compare of 2 freely invertible operands? 3475 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) { 3476 if (Value *NotBCSrcOp = 3477 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) { 3478 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType); 3479 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType)); 3480 } 3481 } 3482 3483 // If this is checking if all elements of an extended vector are clear or not, 3484 // compare in a narrow type to eliminate the extend: 3485 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0 3486 Value *X; 3487 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() && 3488 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) { 3489 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) { 3490 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits()); 3491 Value *NewCast = Builder.CreateBitCast(X, NewType); 3492 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType)); 3493 } 3494 } 3495 3496 // Folding: icmp <pred> iN X, C 3497 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN 3498 // and C is a splat of a K-bit pattern 3499 // and SC is a constant vector = <C', C', C', ..., C'> 3500 // Into: 3501 // %E = extractelement <M x iK> %vec, i32 C' 3502 // icmp <pred> iK %E, trunc(C) 3503 Value *Vec; 3504 ArrayRef<int> Mask; 3505 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) { 3506 // Check whether every element of Mask is the same constant 3507 if (all_equal(Mask)) { 3508 auto *VecTy = cast<VectorType>(SrcType); 3509 auto *EltTy = cast<IntegerType>(VecTy->getElementType()); 3510 if (C->isSplat(EltTy->getBitWidth())) { 3511 // Fold the icmp based on the value of C 3512 // If C is M copies of an iK sized bit pattern, 3513 // then: 3514 // => %E = extractelement <N x iK> %vec, i32 Elem 3515 // icmp <pred> iK %SplatVal, <pattern> 3516 Value *Elem = Builder.getInt32(Mask[0]); 3517 Value *Extract = Builder.CreateExtractElement(Vec, Elem); 3518 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth())); 3519 return new ICmpInst(Pred, Extract, NewC); 3520 } 3521 } 3522 } 3523 return nullptr; 3524 } 3525 3526 /// Try to fold integer comparisons with a constant operand: icmp Pred X, C 3527 /// where X is some kind of instruction. 3528 Instruction *InstCombinerImpl::foldICmpInstWithConstant(ICmpInst &Cmp) { 3529 const APInt *C; 3530 3531 if (match(Cmp.getOperand(1), m_APInt(C))) { 3532 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0))) 3533 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C)) 3534 return I; 3535 3536 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0))) 3537 // For now, we only support constant integers while folding the 3538 // ICMP(SELECT)) pattern. We can extend this to support vector of integers 3539 // similar to the cases handled by binary ops above. 3540 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1))) 3541 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS)) 3542 return I; 3543 3544 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0))) 3545 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C)) 3546 return I; 3547 3548 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) 3549 if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, II, *C)) 3550 return I; 3551 3552 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y 3553 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y 3554 // TODO: This checks one-use, but that is not strictly necessary. 3555 Value *Cmp0 = Cmp.getOperand(0); 3556 Value *X, *Y; 3557 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() && 3558 (match(Cmp0, 3559 m_ExtractValue<0>(m_Intrinsic<Intrinsic::ssub_with_overflow>( 3560 m_Value(X), m_Value(Y)))) || 3561 match(Cmp0, 3562 m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>( 3563 m_Value(X), m_Value(Y)))))) 3564 return new ICmpInst(Cmp.getPredicate(), X, Y); 3565 } 3566 3567 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C))) 3568 return foldICmpInstWithConstantAllowPoison(Cmp, *C); 3569 3570 return nullptr; 3571 } 3572 3573 /// Fold an icmp equality instruction with binary operator LHS and constant RHS: 3574 /// icmp eq/ne BO, C. 3575 Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant( 3576 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) { 3577 // TODO: Some of these folds could work with arbitrary constants, but this 3578 // function is limited to scalar and vector splat constants. 3579 if (!Cmp.isEquality()) 3580 return nullptr; 3581 3582 ICmpInst::Predicate Pred = Cmp.getPredicate(); 3583 bool isICMP_NE = Pred == ICmpInst::ICMP_NE; 3584 Constant *RHS = cast<Constant>(Cmp.getOperand(1)); 3585 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); 3586 3587 switch (BO->getOpcode()) { 3588 case Instruction::SRem: 3589 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. 3590 if (C.isZero() && BO->hasOneUse()) { 3591 const APInt *BOC; 3592 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) { 3593 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName()); 3594 return new ICmpInst(Pred, NewRem, 3595 Constant::getNullValue(BO->getType())); 3596 } 3597 } 3598 break; 3599 case Instruction::Add: { 3600 // (A + C2) == C --> A == (C - C2) 3601 // (A + C2) != C --> A != (C - C2) 3602 // TODO: Remove the one-use limitation? See discussion in D58633. 3603 if (Constant *C2 = dyn_cast<Constant>(BOp1)) { 3604 if (BO->hasOneUse()) 3605 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2)); 3606 } else if (C.isZero()) { 3607 // Replace ((add A, B) != 0) with (A != -B) if A or B is 3608 // efficiently invertible, or if the add has just this one use. 3609 if (Value *NegVal = dyn_castNegVal(BOp1)) 3610 return new ICmpInst(Pred, BOp0, NegVal); 3611 if (Value *NegVal = dyn_castNegVal(BOp0)) 3612 return new ICmpInst(Pred, NegVal, BOp1); 3613 if (BO->hasOneUse()) { 3614 // (add nuw A, B) != 0 -> (or A, B) != 0 3615 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) { 3616 Value *Or = Builder.CreateOr(BOp0, BOp1); 3617 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType())); 3618 } 3619 Value *Neg = Builder.CreateNeg(BOp1); 3620 Neg->takeName(BO); 3621 return new ICmpInst(Pred, BOp0, Neg); 3622 } 3623 } 3624 break; 3625 } 3626 case Instruction::Xor: 3627 if (Constant *BOC = dyn_cast<Constant>(BOp1)) { 3628 // For the xor case, we can xor two constants together, eliminating 3629 // the explicit xor. 3630 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC)); 3631 } else if (C.isZero()) { 3632 // Replace ((xor A, B) != 0) with (A != B) 3633 return new ICmpInst(Pred, BOp0, BOp1); 3634 } 3635 break; 3636 case Instruction::Or: { 3637 const APInt *BOC; 3638 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) { 3639 // Comparing if all bits outside of a constant mask are set? 3640 // Replace (X | C) == -1 with (X & ~C) == ~C. 3641 // This removes the -1 constant. 3642 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1)); 3643 Value *And = Builder.CreateAnd(BOp0, NotBOC); 3644 return new ICmpInst(Pred, And, NotBOC); 3645 } 3646 // (icmp eq (or (select cond, 0, NonZero), Other), 0) 3647 // -> (and cond, (icmp eq Other, 0)) 3648 // (icmp ne (or (select cond, NonZero, 0), Other), 0) 3649 // -> (or cond, (icmp ne Other, 0)) 3650 Value *Cond, *TV, *FV, *Other, *Sel; 3651 if (C.isZero() && 3652 match(BO, 3653 m_OneUse(m_c_Or(m_CombineAnd(m_Value(Sel), 3654 m_Select(m_Value(Cond), m_Value(TV), 3655 m_Value(FV))), 3656 m_Value(Other)))) && 3657 Cond->getType() == Cmp.getType()) { 3658 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp); 3659 // Easy case is if eq/ne matches whether 0 is trueval/falseval. 3660 if (Pred == ICmpInst::ICMP_EQ 3661 ? (match(TV, m_Zero()) && isKnownNonZero(FV, Q)) 3662 : (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) { 3663 Value *Cmp = Builder.CreateICmp( 3664 Pred, Other, Constant::getNullValue(Other->getType())); 3665 return BinaryOperator::Create( 3666 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp, 3667 Cond); 3668 } 3669 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this 3670 // case we need to invert the select condition so we need to be careful to 3671 // avoid creating extra instructions. 3672 // (icmp ne (or (select cond, 0, NonZero), Other), 0) 3673 // -> (or (not cond), (icmp ne Other, 0)) 3674 // (icmp eq (or (select cond, NonZero, 0), Other), 0) 3675 // -> (and (not cond), (icmp eq Other, 0)) 3676 // 3677 // Only do this if the inner select has one use, in which case we are 3678 // replacing `select` with `(not cond)`. Otherwise, we will create more 3679 // uses. NB: Trying to freely invert cond doesn't make sense here, as if 3680 // cond was freely invertable, the select arms would have been inverted. 3681 if (Sel->hasOneUse() && 3682 (Pred == ICmpInst::ICMP_EQ 3683 ? (match(FV, m_Zero()) && isKnownNonZero(TV, Q)) 3684 : (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) { 3685 Value *NotCond = Builder.CreateNot(Cond); 3686 Value *Cmp = Builder.CreateICmp( 3687 Pred, Other, Constant::getNullValue(Other->getType())); 3688 return BinaryOperator::Create( 3689 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp, 3690 NotCond); 3691 } 3692 } 3693 break; 3694 } 3695 case Instruction::UDiv: 3696 case Instruction::SDiv: 3697 if (BO->isExact()) { 3698 // div exact X, Y eq/ne 0 -> X eq/ne 0 3699 // div exact X, Y eq/ne 1 -> X eq/ne Y 3700 // div exact X, Y eq/ne C -> 3701 // if Y * C never-overflow && OneUse: 3702 // -> Y * C eq/ne X 3703 if (C.isZero()) 3704 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType())); 3705 else if (C.isOne()) 3706 return new ICmpInst(Pred, BOp0, BOp1); 3707 else if (BO->hasOneUse()) { 3708 OverflowResult OR = computeOverflow( 3709 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1, 3710 Cmp.getOperand(1), BO); 3711 if (OR == OverflowResult::NeverOverflows) { 3712 Value *YC = 3713 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C)); 3714 return new ICmpInst(Pred, YC, BOp0); 3715 } 3716 } 3717 } 3718 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) { 3719 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A) 3720 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT; 3721 return new ICmpInst(NewPred, BOp1, BOp0); 3722 } 3723 break; 3724 default: 3725 break; 3726 } 3727 return nullptr; 3728 } 3729 3730 static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs, 3731 const APInt &CRhs, 3732 InstCombiner::BuilderTy &Builder, 3733 const SimplifyQuery &Q) { 3734 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop && 3735 "Non-ctpop intrin in ctpop fold"); 3736 if (!CtpopLhs->hasOneUse()) 3737 return nullptr; 3738 3739 // Power of 2 test: 3740 // isPow2OrZero : ctpop(X) u< 2 3741 // isPow2 : ctpop(X) == 1 3742 // NotPow2OrZero: ctpop(X) u> 1 3743 // NotPow2 : ctpop(X) != 1 3744 // If we know any bit of X can be folded to: 3745 // IsPow2 : X & (~Bit) == 0 3746 // NotPow2 : X & (~Bit) != 0 3747 const ICmpInst::Predicate Pred = I.getPredicate(); 3748 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) || 3749 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) { 3750 Value *Op = CtpopLhs->getArgOperand(0); 3751 KnownBits OpKnown = computeKnownBits(Op, Q.DL, 3752 /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT); 3753 // No need to check for count > 1, that should be already constant folded. 3754 if (OpKnown.countMinPopulation() == 1) { 3755 Value *And = Builder.CreateAnd( 3756 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One))); 3757 return new ICmpInst( 3758 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT) 3759 ? ICmpInst::ICMP_EQ 3760 : ICmpInst::ICMP_NE, 3761 And, Constant::getNullValue(Op->getType())); 3762 } 3763 } 3764 3765 return nullptr; 3766 } 3767 3768 /// Fold an equality icmp with LLVM intrinsic and constant operand. 3769 Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant( 3770 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) { 3771 Type *Ty = II->getType(); 3772 unsigned BitWidth = C.getBitWidth(); 3773 const ICmpInst::Predicate Pred = Cmp.getPredicate(); 3774 3775 switch (II->getIntrinsicID()) { 3776 case Intrinsic::abs: 3777 // abs(A) == 0 -> A == 0 3778 // abs(A) == INT_MIN -> A == INT_MIN 3779 if (C.isZero() || C.isMinSignedValue()) 3780 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C)); 3781 break; 3782 3783 case Intrinsic::bswap: 3784 // bswap(A) == C -> A == bswap(C) 3785 return new ICmpInst(Pred, II->getArgOperand(0), 3786 ConstantInt::get(Ty, C.byteSwap())); 3787 3788 case Intrinsic::bitreverse: 3789 // bitreverse(A) == C -> A == bitreverse(C) 3790 return new ICmpInst(Pred, II->getArgOperand(0), 3791 ConstantInt::get(Ty, C.reverseBits())); 3792 3793 case Intrinsic::ctlz: 3794 case Intrinsic::cttz: { 3795 // ctz(A) == bitwidth(A) -> A == 0 and likewise for != 3796 if (C == BitWidth) 3797 return new ICmpInst(Pred, II->getArgOperand(0), 3798 ConstantInt::getNullValue(Ty)); 3799 3800 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set 3801 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits. 3802 // Limit to one use to ensure we don't increase instruction count. 3803 unsigned Num = C.getLimitedValue(BitWidth); 3804 if (Num != BitWidth && II->hasOneUse()) { 3805 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz; 3806 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1) 3807 : APInt::getHighBitsSet(BitWidth, Num + 1); 3808 APInt Mask2 = IsTrailing 3809 ? APInt::getOneBitSet(BitWidth, Num) 3810 : APInt::getOneBitSet(BitWidth, BitWidth - Num - 1); 3811 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1), 3812 ConstantInt::get(Ty, Mask2)); 3813 } 3814 break; 3815 } 3816 3817 case Intrinsic::ctpop: { 3818 // popcount(A) == 0 -> A == 0 and likewise for != 3819 // popcount(A) == bitwidth(A) -> A == -1 and likewise for != 3820 bool IsZero = C.isZero(); 3821 if (IsZero || C == BitWidth) 3822 return new ICmpInst(Pred, II->getArgOperand(0), 3823 IsZero ? Constant::getNullValue(Ty) 3824 : Constant::getAllOnesValue(Ty)); 3825 3826 break; 3827 } 3828 3829 case Intrinsic::fshl: 3830 case Intrinsic::fshr: 3831 if (II->getArgOperand(0) == II->getArgOperand(1)) { 3832 const APInt *RotAmtC; 3833 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC) 3834 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC) 3835 if (match(II->getArgOperand(2), m_APInt(RotAmtC))) 3836 return new ICmpInst(Pred, II->getArgOperand(0), 3837 II->getIntrinsicID() == Intrinsic::fshl 3838 ? ConstantInt::get(Ty, C.rotr(*RotAmtC)) 3839 : ConstantInt::get(Ty, C.rotl(*RotAmtC))); 3840 } 3841 break; 3842 3843 case Intrinsic::umax: 3844 case Intrinsic::uadd_sat: { 3845 // uadd.sat(a, b) == 0 -> (a | b) == 0 3846 // umax(a, b) == 0 -> (a | b) == 0 3847 if (C.isZero() && II->hasOneUse()) { 3848 Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1)); 3849 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty)); 3850 } 3851 break; 3852 } 3853 3854 case Intrinsic::ssub_sat: 3855 // ssub.sat(a, b) == 0 -> a == b 3856 if (C.isZero()) 3857 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1)); 3858 break; 3859 case Intrinsic::usub_sat: { 3860 // usub.sat(a, b) == 0 -> a <= b 3861 if (C.isZero()) { 3862 ICmpInst::Predicate NewPred = 3863 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT; 3864 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1)); 3865 } 3866 break; 3867 } 3868 default: 3869 break; 3870 } 3871 3872 return nullptr; 3873 } 3874 3875 /// Fold an icmp with LLVM intrinsics 3876 static Instruction * 3877 foldICmpIntrinsicWithIntrinsic(ICmpInst &Cmp, 3878 InstCombiner::BuilderTy &Builder) { 3879 assert(Cmp.isEquality()); 3880 3881 ICmpInst::Predicate Pred = Cmp.getPredicate(); 3882 Value *Op0 = Cmp.getOperand(0); 3883 Value *Op1 = Cmp.getOperand(1); 3884 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0); 3885 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1); 3886 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID()) 3887 return nullptr; 3888 3889 switch (IIOp0->getIntrinsicID()) { 3890 case Intrinsic::bswap: 3891 case Intrinsic::bitreverse: 3892 // If both operands are byte-swapped or bit-reversed, just compare the 3893 // original values. 3894 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0)); 3895 case Intrinsic::fshl: 3896 case Intrinsic::fshr: { 3897 // If both operands are rotated by same amount, just compare the 3898 // original values. 3899 if (IIOp0->getOperand(0) != IIOp0->getOperand(1)) 3900 break; 3901 if (IIOp1->getOperand(0) != IIOp1->getOperand(1)) 3902 break; 3903 if (IIOp0->getOperand(2) == IIOp1->getOperand(2)) 3904 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0)); 3905 3906 // rotate(X, AmtX) == rotate(Y, AmtY) 3907 // -> rotate(X, AmtX - AmtY) == Y 3908 // Do this if either both rotates have one use or if only one has one use 3909 // and AmtX/AmtY are constants. 3910 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse(); 3911 if (OneUses == 2 || 3912 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) && 3913 match(IIOp1->getOperand(2), m_ImmConstant()))) { 3914 Value *SubAmt = 3915 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2)); 3916 Value *CombinedRotate = Builder.CreateIntrinsic( 3917 Op0->getType(), IIOp0->getIntrinsicID(), 3918 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt}); 3919 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate); 3920 } 3921 } break; 3922 default: 3923 break; 3924 } 3925 3926 return nullptr; 3927 } 3928 3929 /// Try to fold integer comparisons with a constant operand: icmp Pred X, C 3930 /// where X is some kind of instruction and C is AllowPoison. 3931 /// TODO: Move more folds which allow poison to this function. 3932 Instruction * 3933 InstCombinerImpl::foldICmpInstWithConstantAllowPoison(ICmpInst &Cmp, 3934 const APInt &C) { 3935 const ICmpInst::Predicate Pred = Cmp.getPredicate(); 3936 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) { 3937 switch (II->getIntrinsicID()) { 3938 default: 3939 break; 3940 case Intrinsic::fshl: 3941 case Intrinsic::fshr: 3942 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) { 3943 // (rot X, ?) == 0/-1 --> X == 0/-1 3944 if (C.isZero() || C.isAllOnes()) 3945 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1)); 3946 } 3947 break; 3948 } 3949 } 3950 3951 return nullptr; 3952 } 3953 3954 /// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C. 3955 Instruction *InstCombinerImpl::foldICmpBinOpWithConstant(ICmpInst &Cmp, 3956 BinaryOperator *BO, 3957 const APInt &C) { 3958 switch (BO->getOpcode()) { 3959 case Instruction::Xor: 3960 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C)) 3961 return I; 3962 break; 3963 case Instruction::And: 3964 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C)) 3965 return I; 3966 break; 3967 case Instruction::Or: 3968 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C)) 3969 return I; 3970 break; 3971 case Instruction::Mul: 3972 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C)) 3973 return I; 3974 break; 3975 case Instruction::Shl: 3976 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C)) 3977 return I; 3978 break; 3979 case Instruction::LShr: 3980 case Instruction::AShr: 3981 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C)) 3982 return I; 3983 break; 3984 case Instruction::SRem: 3985 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C)) 3986 return I; 3987 break; 3988 case Instruction::UDiv: 3989 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C)) 3990 return I; 3991 [[fallthrough]]; 3992 case Instruction::SDiv: 3993 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C)) 3994 return I; 3995 break; 3996 case Instruction::Sub: 3997 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C)) 3998 return I; 3999 break; 4000 case Instruction::Add: 4001 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C)) 4002 return I; 4003 break; 4004 default: 4005 break; 4006 } 4007 4008 // TODO: These folds could be refactored to be part of the above calls. 4009 return foldICmpBinOpEqualityWithConstant(Cmp, BO, C); 4010 } 4011 4012 static Instruction * 4013 foldICmpUSubSatOrUAddSatWithConstant(CmpPredicate Pred, SaturatingInst *II, 4014 const APInt &C, 4015 InstCombiner::BuilderTy &Builder) { 4016 // This transform may end up producing more than one instruction for the 4017 // intrinsic, so limit it to one user of the intrinsic. 4018 if (!II->hasOneUse()) 4019 return nullptr; 4020 4021 // Let Y = [add/sub]_sat(X, C) pred C2 4022 // SatVal = The saturating value for the operation 4023 // WillWrap = Whether or not the operation will underflow / overflow 4024 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2 4025 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2) 4026 // 4027 // When (SatVal pred C2) is true, then 4028 // Y = WillWrap ? true : ((X binop C) pred C2) 4029 // => Y = WillWrap || ((X binop C) pred C2) 4030 // else 4031 // Y = WillWrap ? false : ((X binop C) pred C2) 4032 // => Y = !WillWrap ? ((X binop C) pred C2) : false 4033 // => Y = !WillWrap && ((X binop C) pred C2) 4034 Value *Op0 = II->getOperand(0); 4035 Value *Op1 = II->getOperand(1); 4036 4037 const APInt *COp1; 4038 // This transform only works when the intrinsic has an integral constant or 4039 // splat vector as the second operand. 4040 if (!match(Op1, m_APInt(COp1))) 4041 return nullptr; 4042 4043 APInt SatVal; 4044 switch (II->getIntrinsicID()) { 4045 default: 4046 llvm_unreachable( 4047 "This function only works with usub_sat and uadd_sat for now!"); 4048 case Intrinsic::uadd_sat: 4049 SatVal = APInt::getAllOnes(C.getBitWidth()); 4050 break; 4051 case Intrinsic::usub_sat: 4052 SatVal = APInt::getZero(C.getBitWidth()); 4053 break; 4054 } 4055 4056 // Check (SatVal pred C2) 4057 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred); 4058 4059 // !WillWrap. 4060 ConstantRange C1 = ConstantRange::makeExactNoWrapRegion( 4061 II->getBinaryOp(), *COp1, II->getNoWrapKind()); 4062 4063 // WillWrap. 4064 if (SatValCheck) 4065 C1 = C1.inverse(); 4066 4067 ConstantRange C2 = ConstantRange::makeExactICmpRegion(Pred, C); 4068 if (II->getBinaryOp() == Instruction::Add) 4069 C2 = C2.sub(*COp1); 4070 else 4071 C2 = C2.add(*COp1); 4072 4073 Instruction::BinaryOps CombiningOp = 4074 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And; 4075 4076 std::optional<ConstantRange> Combination; 4077 if (CombiningOp == Instruction::BinaryOps::Or) 4078 Combination = C1.exactUnionWith(C2); 4079 else /* CombiningOp == Instruction::BinaryOps::And */ 4080 Combination = C1.exactIntersectWith(C2); 4081 4082 if (!Combination) 4083 return nullptr; 4084 4085 CmpInst::Predicate EquivPred; 4086 APInt EquivInt; 4087 APInt EquivOffset; 4088 4089 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset); 4090 4091 return new ICmpInst( 4092 EquivPred, 4093 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)), 4094 ConstantInt::get(Op1->getType(), EquivInt)); 4095 } 4096 4097 static Instruction * 4098 foldICmpOfCmpIntrinsicWithConstant(CmpPredicate Pred, IntrinsicInst *I, 4099 const APInt &C, 4100 InstCombiner::BuilderTy &Builder) { 4101 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt; 4102 switch (Pred) { 4103 case ICmpInst::ICMP_EQ: 4104 case ICmpInst::ICMP_NE: 4105 if (C.isZero()) 4106 NewPredicate = Pred; 4107 else if (C.isOne()) 4108 NewPredicate = 4109 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE; 4110 else if (C.isAllOnes()) 4111 NewPredicate = 4112 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE; 4113 break; 4114 4115 case ICmpInst::ICMP_SGT: 4116 if (C.isAllOnes()) 4117 NewPredicate = ICmpInst::ICMP_UGE; 4118 else if (C.isZero()) 4119 NewPredicate = ICmpInst::ICMP_UGT; 4120 break; 4121 4122 case ICmpInst::ICMP_SLT: 4123 if (C.isZero()) 4124 NewPredicate = ICmpInst::ICMP_ULT; 4125 else if (C.isOne()) 4126 NewPredicate = ICmpInst::ICMP_ULE; 4127 break; 4128 4129 case ICmpInst::ICMP_ULT: 4130 if (C.ugt(1)) 4131 NewPredicate = ICmpInst::ICMP_UGE; 4132 break; 4133 4134 case ICmpInst::ICMP_UGT: 4135 if (!C.isZero() && !C.isAllOnes()) 4136 NewPredicate = ICmpInst::ICMP_ULT; 4137 break; 4138 4139 default: 4140 break; 4141 } 4142 4143 if (!NewPredicate) 4144 return nullptr; 4145 4146 if (I->getIntrinsicID() == Intrinsic::scmp) 4147 NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate); 4148 Value *LHS = I->getOperand(0); 4149 Value *RHS = I->getOperand(1); 4150 return new ICmpInst(*NewPredicate, LHS, RHS); 4151 } 4152 4153 /// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C. 4154 Instruction *InstCombinerImpl::foldICmpIntrinsicWithConstant(ICmpInst &Cmp, 4155 IntrinsicInst *II, 4156 const APInt &C) { 4157 ICmpInst::Predicate Pred = Cmp.getPredicate(); 4158 4159 // Handle folds that apply for any kind of icmp. 4160 switch (II->getIntrinsicID()) { 4161 default: 4162 break; 4163 case Intrinsic::uadd_sat: 4164 case Intrinsic::usub_sat: 4165 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant( 4166 Pred, cast<SaturatingInst>(II), C, Builder)) 4167 return Folded; 4168 break; 4169 case Intrinsic::ctpop: { 4170 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp); 4171 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q)) 4172 return R; 4173 } break; 4174 case Intrinsic::scmp: 4175 case Intrinsic::ucmp: 4176 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, II, C, Builder)) 4177 return Folded; 4178 break; 4179 } 4180 4181 if (Cmp.isEquality()) 4182 return foldICmpEqIntrinsicWithConstant(Cmp, II, C); 4183 4184 Type *Ty = II->getType(); 4185 unsigned BitWidth = C.getBitWidth(); 4186 switch (II->getIntrinsicID()) { 4187 case Intrinsic::ctpop: { 4188 // (ctpop X > BitWidth - 1) --> X == -1 4189 Value *X = II->getArgOperand(0); 4190 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT) 4191 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X, 4192 ConstantInt::getAllOnesValue(Ty)); 4193 // (ctpop X < BitWidth) --> X != -1 4194 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT) 4195 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X, 4196 ConstantInt::getAllOnesValue(Ty)); 4197 break; 4198 } 4199 case Intrinsic::ctlz: { 4200 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000 4201 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) { 4202 unsigned Num = C.getLimitedValue(); 4203 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1); 4204 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, 4205 II->getArgOperand(0), ConstantInt::get(Ty, Limit)); 4206 } 4207 4208 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111 4209 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) { 4210 unsigned Num = C.getLimitedValue(); 4211 APInt Limit = APInt::getLowBitsSet(BitWidth, BitWidth - Num); 4212 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, 4213 II->getArgOperand(0), ConstantInt::get(Ty, Limit)); 4214 } 4215 break; 4216 } 4217 case Intrinsic::cttz: { 4218 // Limit to one use to ensure we don't increase instruction count. 4219 if (!II->hasOneUse()) 4220 return nullptr; 4221 4222 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0 4223 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) { 4224 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1); 4225 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, 4226 Builder.CreateAnd(II->getArgOperand(0), Mask), 4227 ConstantInt::getNullValue(Ty)); 4228 } 4229 4230 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0 4231 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) { 4232 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue()); 4233 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, 4234 Builder.CreateAnd(II->getArgOperand(0), Mask), 4235 ConstantInt::getNullValue(Ty)); 4236 } 4237 break; 4238 } 4239 case Intrinsic::ssub_sat: 4240 // ssub.sat(a, b) spred 0 -> a spred b 4241 if (ICmpInst::isSigned(Pred)) { 4242 if (C.isZero()) 4243 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1)); 4244 // X s<= 0 is cannonicalized to X s< 1 4245 if (Pred == ICmpInst::ICMP_SLT && C.isOne()) 4246 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0), 4247 II->getArgOperand(1)); 4248 // X s>= 0 is cannonicalized to X s> -1 4249 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes()) 4250 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0), 4251 II->getArgOperand(1)); 4252 } 4253 break; 4254 default: 4255 break; 4256 } 4257 4258 return nullptr; 4259 } 4260 4261 /// Handle icmp with constant (but not simple integer constant) RHS. 4262 Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) { 4263 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 4264 Constant *RHSC = dyn_cast<Constant>(Op1); 4265 Instruction *LHSI = dyn_cast<Instruction>(Op0); 4266 if (!RHSC || !LHSI) 4267 return nullptr; 4268 4269 switch (LHSI->getOpcode()) { 4270 case Instruction::PHI: 4271 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI))) 4272 return NV; 4273 break; 4274 case Instruction::IntToPtr: 4275 // icmp pred inttoptr(X), null -> icmp pred X, 0 4276 if (RHSC->isNullValue() && 4277 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType()) 4278 return new ICmpInst( 4279 I.getPredicate(), LHSI->getOperand(0), 4280 Constant::getNullValue(LHSI->getOperand(0)->getType())); 4281 break; 4282 4283 case Instruction::Load: 4284 // Try to optimize things like "A[i] > 4" to index computations. 4285 if (GetElementPtrInst *GEP = 4286 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) 4287 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) 4288 if (Instruction *Res = 4289 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, GV, I)) 4290 return Res; 4291 break; 4292 } 4293 4294 return nullptr; 4295 } 4296 4297 Instruction *InstCombinerImpl::foldSelectICmp(CmpPredicate Pred, SelectInst *SI, 4298 Value *RHS, const ICmpInst &I) { 4299 // Try to fold the comparison into the select arms, which will cause the 4300 // select to be converted into a logical and/or. 4301 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * { 4302 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ)) 4303 return Res; 4304 if (std::optional<bool> Impl = isImpliedCondition( 4305 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue)) 4306 return ConstantInt::get(I.getType(), *Impl); 4307 return nullptr; 4308 }; 4309 4310 ConstantInt *CI = nullptr; 4311 Value *Op1 = SimplifyOp(SI->getOperand(1), true); 4312 if (Op1) 4313 CI = dyn_cast<ConstantInt>(Op1); 4314 4315 Value *Op2 = SimplifyOp(SI->getOperand(2), false); 4316 if (Op2) 4317 CI = dyn_cast<ConstantInt>(Op2); 4318 4319 auto Simplifies = [&](Value *Op, unsigned Idx) { 4320 // A comparison of ucmp/scmp with a constant will fold into an icmp. 4321 const APInt *Dummy; 4322 return Op || 4323 (isa<CmpIntrinsic>(SI->getOperand(Idx)) && 4324 SI->getOperand(Idx)->hasOneUse() && match(RHS, m_APInt(Dummy))); 4325 }; 4326 4327 // We only want to perform this transformation if it will not lead to 4328 // additional code. This is true if either both sides of the select 4329 // fold to a constant (in which case the icmp is replaced with a select 4330 // which will usually simplify) or this is the only user of the 4331 // select (in which case we are trading a select+icmp for a simpler 4332 // select+icmp) or all uses of the select can be replaced based on 4333 // dominance information ("Global cases"). 4334 bool Transform = false; 4335 if (Op1 && Op2) 4336 Transform = true; 4337 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) { 4338 // Local case 4339 if (SI->hasOneUse()) 4340 Transform = true; 4341 // Global cases 4342 else if (CI && !CI->isZero()) 4343 // When Op1 is constant try replacing select with second operand. 4344 // Otherwise Op2 is constant and try replacing select with first 4345 // operand. 4346 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1); 4347 } 4348 if (Transform) { 4349 if (!Op1) 4350 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName()); 4351 if (!Op2) 4352 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName()); 4353 return SelectInst::Create(SI->getOperand(0), Op1, Op2); 4354 } 4355 4356 return nullptr; 4357 } 4358 4359 // Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero) 4360 static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q, 4361 unsigned Depth = 0) { 4362 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero())) 4363 return true; 4364 if (V->getType()->getScalarSizeInBits() == 1) 4365 return true; 4366 if (Depth++ >= MaxAnalysisRecursionDepth) 4367 return false; 4368 Value *X; 4369 const Instruction *I = dyn_cast<Instruction>(V); 4370 if (!I) 4371 return false; 4372 switch (I->getOpcode()) { 4373 case Instruction::ZExt: 4374 // ZExt(Mask) is a Mask. 4375 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth); 4376 case Instruction::SExt: 4377 // SExt(Mask) is a Mask. 4378 // SExt(~Mask) is a ~Mask. 4379 return isMaskOrZero(I->getOperand(0), Not, Q, Depth); 4380 case Instruction::And: 4381 case Instruction::Or: 4382 // Mask0 | Mask1 is a Mask. 4383 // Mask0 & Mask1 is a Mask. 4384 // ~Mask0 | ~Mask1 is a ~Mask. 4385 // ~Mask0 & ~Mask1 is a ~Mask. 4386 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) && 4387 isMaskOrZero(I->getOperand(0), Not, Q, Depth); 4388 case Instruction::Xor: 4389 if (match(V, m_Not(m_Value(X)))) 4390 return isMaskOrZero(X, !Not, Q, Depth); 4391 4392 // (X ^ -X) is a ~Mask 4393 if (Not) 4394 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X)))); 4395 // (X ^ (X - 1)) is a Mask 4396 else 4397 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes()))); 4398 case Instruction::Select: 4399 // c ? Mask0 : Mask1 is a Mask. 4400 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) && 4401 isMaskOrZero(I->getOperand(2), Not, Q, Depth); 4402 case Instruction::Shl: 4403 // (~Mask) << X is a ~Mask. 4404 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth); 4405 case Instruction::LShr: 4406 // Mask >> X is a Mask. 4407 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth); 4408 case Instruction::AShr: 4409 // Mask s>> X is a Mask. 4410 // ~Mask s>> X is a ~Mask. 4411 return isMaskOrZero(I->getOperand(0), Not, Q, Depth); 4412 case Instruction::Add: 4413 // Pow2 - 1 is a Mask. 4414 if (!Not && match(I->getOperand(1), m_AllOnes())) 4415 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true, 4416 Depth, Q.AC, Q.CxtI, Q.DT); 4417 break; 4418 case Instruction::Sub: 4419 // -Pow2 is a ~Mask. 4420 if (Not && match(I->getOperand(0), m_Zero())) 4421 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true, 4422 Depth, Q.AC, Q.CxtI, Q.DT); 4423 break; 4424 case Instruction::Call: { 4425 if (auto *II = dyn_cast<IntrinsicInst>(I)) { 4426 switch (II->getIntrinsicID()) { 4427 // min/max(Mask0, Mask1) is a Mask. 4428 // min/max(~Mask0, ~Mask1) is a ~Mask. 4429 case Intrinsic::umax: 4430 case Intrinsic::smax: 4431 case Intrinsic::umin: 4432 case Intrinsic::smin: 4433 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) && 4434 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth); 4435 4436 // In the context of masks, bitreverse(Mask) == ~Mask 4437 case Intrinsic::bitreverse: 4438 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth); 4439 default: 4440 break; 4441 } 4442 } 4443 break; 4444 } 4445 default: 4446 break; 4447 } 4448 return false; 4449 } 4450 4451 /// Some comparisons can be simplified. 4452 /// In this case, we are looking for comparisons that look like 4453 /// a check for a lossy truncation. 4454 /// Folds: 4455 /// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask 4456 /// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask 4457 /// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask 4458 /// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask 4459 /// Where Mask is some pattern that produces all-ones in low bits: 4460 /// (-1 >> y) 4461 /// ((-1 << y) >> y) <- non-canonical, has extra uses 4462 /// ~(-1 << y) 4463 /// ((1 << y) + (-1)) <- non-canonical, has extra uses 4464 /// The Mask can be a constant, too. 4465 /// For some predicates, the operands are commutative. 4466 /// For others, x can only be on a specific side. 4467 static Value *foldICmpWithLowBitMaskedVal(CmpPredicate Pred, Value *Op0, 4468 Value *Op1, const SimplifyQuery &Q, 4469 InstCombiner &IC) { 4470 4471 ICmpInst::Predicate DstPred; 4472 switch (Pred) { 4473 case ICmpInst::Predicate::ICMP_EQ: 4474 // x & Mask == x 4475 // x & ~Mask == 0 4476 // ~x | Mask == -1 4477 // -> x u<= Mask 4478 // x & ~Mask == ~Mask 4479 // -> ~Mask u<= x 4480 DstPred = ICmpInst::Predicate::ICMP_ULE; 4481 break; 4482 case ICmpInst::Predicate::ICMP_NE: 4483 // x & Mask != x 4484 // x & ~Mask != 0 4485 // ~x | Mask != -1 4486 // -> x u> Mask 4487 // x & ~Mask != ~Mask 4488 // -> ~Mask u> x 4489 DstPred = ICmpInst::Predicate::ICMP_UGT; 4490 break; 4491 case ICmpInst::Predicate::ICMP_ULT: 4492 // x & Mask u< x 4493 // -> x u> Mask 4494 // x & ~Mask u< ~Mask 4495 // -> ~Mask u> x 4496 DstPred = ICmpInst::Predicate::ICMP_UGT; 4497 break; 4498 case ICmpInst::Predicate::ICMP_UGE: 4499 // x & Mask u>= x 4500 // -> x u<= Mask 4501 // x & ~Mask u>= ~Mask 4502 // -> ~Mask u<= x 4503 DstPred = ICmpInst::Predicate::ICMP_ULE; 4504 break; 4505 case ICmpInst::Predicate::ICMP_SLT: 4506 // x & Mask s< x [iff Mask s>= 0] 4507 // -> x s> Mask 4508 // x & ~Mask s< ~Mask [iff ~Mask != 0] 4509 // -> ~Mask s> x 4510 DstPred = ICmpInst::Predicate::ICMP_SGT; 4511 break; 4512 case ICmpInst::Predicate::ICMP_SGE: 4513 // x & Mask s>= x [iff Mask s>= 0] 4514 // -> x s<= Mask 4515 // x & ~Mask s>= ~Mask [iff ~Mask != 0] 4516 // -> ~Mask s<= x 4517 DstPred = ICmpInst::Predicate::ICMP_SLE; 4518 break; 4519 default: 4520 // We don't support sgt,sle 4521 // ult/ugt are simplified to true/false respectively. 4522 return nullptr; 4523 } 4524 4525 Value *X, *M; 4526 // Put search code in lambda for early positive returns. 4527 auto IsLowBitMask = [&]() { 4528 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) { 4529 X = Op1; 4530 // Look for: x & Mask pred x 4531 if (isMaskOrZero(M, /*Not=*/false, Q)) { 4532 return !ICmpInst::isSigned(Pred) || 4533 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q)); 4534 } 4535 4536 // Look for: x & ~Mask pred ~Mask 4537 if (isMaskOrZero(X, /*Not=*/true, Q)) { 4538 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q); 4539 } 4540 return false; 4541 } 4542 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) && 4543 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) { 4544 4545 auto Check = [&]() { 4546 // Look for: ~x | Mask == -1 4547 if (isMaskOrZero(M, /*Not=*/false, Q)) { 4548 if (Value *NotX = 4549 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) { 4550 X = NotX; 4551 return true; 4552 } 4553 } 4554 return false; 4555 }; 4556 if (Check()) 4557 return true; 4558 std::swap(X, M); 4559 return Check(); 4560 } 4561 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) && 4562 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) { 4563 auto Check = [&]() { 4564 // Look for: x & ~Mask == 0 4565 if (isMaskOrZero(M, /*Not=*/true, Q)) { 4566 if (Value *NotM = 4567 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) { 4568 M = NotM; 4569 return true; 4570 } 4571 } 4572 return false; 4573 }; 4574 if (Check()) 4575 return true; 4576 std::swap(X, M); 4577 return Check(); 4578 } 4579 return false; 4580 }; 4581 4582 if (!IsLowBitMask()) 4583 return nullptr; 4584 4585 return IC.Builder.CreateICmp(DstPred, X, M); 4586 } 4587 4588 /// Some comparisons can be simplified. 4589 /// In this case, we are looking for comparisons that look like 4590 /// a check for a lossy signed truncation. 4591 /// Folds: (MaskedBits is a constant.) 4592 /// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x 4593 /// Into: 4594 /// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits) 4595 /// Where KeptBits = bitwidth(%x) - MaskedBits 4596 static Value * 4597 foldICmpWithTruncSignExtendedVal(ICmpInst &I, 4598 InstCombiner::BuilderTy &Builder) { 4599 CmpPredicate SrcPred; 4600 Value *X; 4601 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef. 4602 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use. 4603 if (!match(&I, m_c_ICmp(SrcPred, 4604 m_OneUse(m_AShr(m_Shl(m_Value(X), m_APInt(C0)), 4605 m_APInt(C1))), 4606 m_Deferred(X)))) 4607 return nullptr; 4608 4609 // Potential handling of non-splats: for each element: 4610 // * if both are undef, replace with constant 0. 4611 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0. 4612 // * if both are not undef, and are different, bailout. 4613 // * else, only one is undef, then pick the non-undef one. 4614 4615 // The shift amount must be equal. 4616 if (*C0 != *C1) 4617 return nullptr; 4618 const APInt &MaskedBits = *C0; 4619 assert(MaskedBits != 0 && "shift by zero should be folded away already."); 4620 4621 ICmpInst::Predicate DstPred; 4622 switch (SrcPred) { 4623 case ICmpInst::Predicate::ICMP_EQ: 4624 // ((%x << MaskedBits) a>> MaskedBits) == %x 4625 // => 4626 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits) 4627 DstPred = ICmpInst::Predicate::ICMP_ULT; 4628 break; 4629 case ICmpInst::Predicate::ICMP_NE: 4630 // ((%x << MaskedBits) a>> MaskedBits) != %x 4631 // => 4632 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits) 4633 DstPred = ICmpInst::Predicate::ICMP_UGE; 4634 break; 4635 // FIXME: are more folds possible? 4636 default: 4637 return nullptr; 4638 } 4639 4640 auto *XType = X->getType(); 4641 const unsigned XBitWidth = XType->getScalarSizeInBits(); 4642 const APInt BitWidth = APInt(XBitWidth, XBitWidth); 4643 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched"); 4644 4645 // KeptBits = bitwidth(%x) - MaskedBits 4646 const APInt KeptBits = BitWidth - MaskedBits; 4647 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable"); 4648 // ICmpCst = (1 << KeptBits) 4649 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits); 4650 assert(ICmpCst.isPowerOf2()); 4651 // AddCst = (1 << (KeptBits-1)) 4652 const APInt AddCst = ICmpCst.lshr(1); 4653 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2()); 4654 4655 // T0 = add %x, AddCst 4656 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst)); 4657 // T1 = T0 DstPred ICmpCst 4658 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst)); 4659 4660 return T1; 4661 } 4662 4663 // Given pattern: 4664 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0 4665 // we should move shifts to the same hand of 'and', i.e. rewrite as 4666 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x) 4667 // We are only interested in opposite logical shifts here. 4668 // One of the shifts can be truncated. 4669 // If we can, we want to end up creating 'lshr' shift. 4670 static Value * 4671 foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ, 4672 InstCombiner::BuilderTy &Builder) { 4673 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) || 4674 !I.getOperand(0)->hasOneUse()) 4675 return nullptr; 4676 4677 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value()); 4678 4679 // Look for an 'and' of two logical shifts, one of which may be truncated. 4680 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case. 4681 Instruction *XShift, *MaybeTruncation, *YShift; 4682 if (!match( 4683 I.getOperand(0), 4684 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)), 4685 m_CombineAnd(m_TruncOrSelf(m_CombineAnd( 4686 m_AnyLogicalShift, m_Instruction(YShift))), 4687 m_Instruction(MaybeTruncation))))) 4688 return nullptr; 4689 4690 // We potentially looked past 'trunc', but only when matching YShift, 4691 // therefore YShift must have the widest type. 4692 Instruction *WidestShift = YShift; 4693 // Therefore XShift must have the shallowest type. 4694 // Or they both have identical types if there was no truncation. 4695 Instruction *NarrowestShift = XShift; 4696 4697 Type *WidestTy = WidestShift->getType(); 4698 Type *NarrowestTy = NarrowestShift->getType(); 4699 assert(NarrowestTy == I.getOperand(0)->getType() && 4700 "We did not look past any shifts while matching XShift though."); 4701 bool HadTrunc = WidestTy != I.getOperand(0)->getType(); 4702 4703 // If YShift is a 'lshr', swap the shifts around. 4704 if (match(YShift, m_LShr(m_Value(), m_Value()))) 4705 std::swap(XShift, YShift); 4706 4707 // The shifts must be in opposite directions. 4708 auto XShiftOpcode = XShift->getOpcode(); 4709 if (XShiftOpcode == YShift->getOpcode()) 4710 return nullptr; // Do not care about same-direction shifts here. 4711 4712 Value *X, *XShAmt, *Y, *YShAmt; 4713 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt)))); 4714 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt)))); 4715 4716 // If one of the values being shifted is a constant, then we will end with 4717 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not, 4718 // however, we will need to ensure that we won't increase instruction count. 4719 if (!isa<Constant>(X) && !isa<Constant>(Y)) { 4720 // At least one of the hands of the 'and' should be one-use shift. 4721 if (!match(I.getOperand(0), 4722 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value()))) 4723 return nullptr; 4724 if (HadTrunc) { 4725 // Due to the 'trunc', we will need to widen X. For that either the old 4726 // 'trunc' or the shift amt in the non-truncated shift should be one-use. 4727 if (!MaybeTruncation->hasOneUse() && 4728 !NarrowestShift->getOperand(1)->hasOneUse()) 4729 return nullptr; 4730 } 4731 } 4732 4733 // We have two shift amounts from two different shifts. The types of those 4734 // shift amounts may not match. If that's the case let's bailout now. 4735 if (XShAmt->getType() != YShAmt->getType()) 4736 return nullptr; 4737 4738 // As input, we have the following pattern: 4739 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0 4740 // We want to rewrite that as: 4741 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x) 4742 // While we know that originally (Q+K) would not overflow 4743 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of 4744 // shift amounts. so it may now overflow in smaller bitwidth. 4745 // To ensure that does not happen, we need to ensure that the total maximal 4746 // shift amount is still representable in that smaller bit width. 4747 unsigned MaximalPossibleTotalShiftAmount = 4748 (WidestTy->getScalarSizeInBits() - 1) + 4749 (NarrowestTy->getScalarSizeInBits() - 1); 4750 APInt MaximalRepresentableShiftAmount = 4751 APInt::getAllOnes(XShAmt->getType()->getScalarSizeInBits()); 4752 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount)) 4753 return nullptr; 4754 4755 // Can we fold (XShAmt+YShAmt) ? 4756 auto *NewShAmt = dyn_cast_or_null<Constant>( 4757 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false, 4758 /*isNUW=*/false, SQ.getWithInstruction(&I))); 4759 if (!NewShAmt) 4760 return nullptr; 4761 if (NewShAmt->getType() != WidestTy) { 4762 NewShAmt = 4763 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL); 4764 if (!NewShAmt) 4765 return nullptr; 4766 } 4767 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits(); 4768 4769 // Is the new shift amount smaller than the bit width? 4770 // FIXME: could also rely on ConstantRange. 4771 if (!match(NewShAmt, 4772 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, 4773 APInt(WidestBitWidth, WidestBitWidth)))) 4774 return nullptr; 4775 4776 // An extra legality check is needed if we had trunc-of-lshr. 4777 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) { 4778 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ, 4779 WidestShift]() { 4780 // It isn't obvious whether it's worth it to analyze non-constants here. 4781 // Also, let's basically give up on non-splat cases, pessimizing vectors. 4782 // If *any* of these preconditions matches we can perform the fold. 4783 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy() 4784 ? NewShAmt->getSplatValue() 4785 : NewShAmt; 4786 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold. 4787 if (NewShAmtSplat && 4788 (NewShAmtSplat->isNullValue() || 4789 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1)) 4790 return true; 4791 // We consider *min* leading zeros so a single outlier 4792 // blocks the transform as opposed to allowing it. 4793 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) { 4794 KnownBits Known = computeKnownBits(C, SQ.DL); 4795 unsigned MinLeadZero = Known.countMinLeadingZeros(); 4796 // If the value being shifted has at most lowest bit set we can fold. 4797 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero; 4798 if (MaxActiveBits <= 1) 4799 return true; 4800 // Precondition: NewShAmt u<= countLeadingZeros(C) 4801 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero)) 4802 return true; 4803 } 4804 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) { 4805 KnownBits Known = computeKnownBits(C, SQ.DL); 4806 unsigned MinLeadZero = Known.countMinLeadingZeros(); 4807 // If the value being shifted has at most lowest bit set we can fold. 4808 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero; 4809 if (MaxActiveBits <= 1) 4810 return true; 4811 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C) 4812 if (NewShAmtSplat) { 4813 APInt AdjNewShAmt = 4814 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger(); 4815 if (AdjNewShAmt.ule(MinLeadZero)) 4816 return true; 4817 } 4818 } 4819 return false; // Can't tell if it's ok. 4820 }; 4821 if (!CanFold()) 4822 return nullptr; 4823 } 4824 4825 // All good, we can do this fold. 4826 X = Builder.CreateZExt(X, WidestTy); 4827 Y = Builder.CreateZExt(Y, WidestTy); 4828 // The shift is the same that was for X. 4829 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr 4830 ? Builder.CreateLShr(X, NewShAmt) 4831 : Builder.CreateShl(X, NewShAmt); 4832 Value *T1 = Builder.CreateAnd(T0, Y); 4833 return Builder.CreateICmp(I.getPredicate(), T1, 4834 Constant::getNullValue(WidestTy)); 4835 } 4836 4837 /// Fold 4838 /// (-1 u/ x) u< y 4839 /// ((x * y) ?/ x) != y 4840 /// to 4841 /// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit 4842 /// Note that the comparison is commutative, while inverted (u>=, ==) predicate 4843 /// will mean that we are looking for the opposite answer. 4844 Value *InstCombinerImpl::foldMultiplicationOverflowCheck(ICmpInst &I) { 4845 CmpPredicate Pred; 4846 Value *X, *Y; 4847 Instruction *Mul; 4848 Instruction *Div; 4849 bool NeedNegation; 4850 // Look for: (-1 u/ x) u</u>= y 4851 if (!I.isEquality() && 4852 match(&I, m_c_ICmp(Pred, 4853 m_CombineAnd(m_OneUse(m_UDiv(m_AllOnes(), m_Value(X))), 4854 m_Instruction(Div)), 4855 m_Value(Y)))) { 4856 Mul = nullptr; 4857 4858 // Are we checking that overflow does not happen, or does happen? 4859 switch (Pred) { 4860 case ICmpInst::Predicate::ICMP_ULT: 4861 NeedNegation = false; 4862 break; // OK 4863 case ICmpInst::Predicate::ICMP_UGE: 4864 NeedNegation = true; 4865 break; // OK 4866 default: 4867 return nullptr; // Wrong predicate. 4868 } 4869 } else // Look for: ((x * y) / x) !=/== y 4870 if (I.isEquality() && 4871 match(&I, 4872 m_c_ICmp(Pred, m_Value(Y), 4873 m_CombineAnd( 4874 m_OneUse(m_IDiv(m_CombineAnd(m_c_Mul(m_Deferred(Y), 4875 m_Value(X)), 4876 m_Instruction(Mul)), 4877 m_Deferred(X))), 4878 m_Instruction(Div))))) { 4879 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ; 4880 } else 4881 return nullptr; 4882 4883 BuilderTy::InsertPointGuard Guard(Builder); 4884 // If the pattern included (x * y), we'll want to insert new instructions 4885 // right before that original multiplication so that we can replace it. 4886 bool MulHadOtherUses = Mul && !Mul->hasOneUse(); 4887 if (MulHadOtherUses) 4888 Builder.SetInsertPoint(Mul); 4889 4890 CallInst *Call = Builder.CreateIntrinsic( 4891 Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow 4892 : Intrinsic::smul_with_overflow, 4893 X->getType(), {X, Y}, /*FMFSource=*/nullptr, "mul"); 4894 4895 // If the multiplication was used elsewhere, to ensure that we don't leave 4896 // "duplicate" instructions, replace uses of that original multiplication 4897 // with the multiplication result from the with.overflow intrinsic. 4898 if (MulHadOtherUses) 4899 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val")); 4900 4901 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov"); 4902 if (NeedNegation) // This technically increases instruction count. 4903 Res = Builder.CreateNot(Res, "mul.not.ov"); 4904 4905 // If we replaced the mul, erase it. Do this after all uses of Builder, 4906 // as the mul is used as insertion point. 4907 if (MulHadOtherUses) 4908 eraseInstFromFunction(*Mul); 4909 4910 return Res; 4911 } 4912 4913 static Instruction *foldICmpXNegX(ICmpInst &I, 4914 InstCombiner::BuilderTy &Builder) { 4915 CmpPredicate Pred; 4916 Value *X; 4917 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) { 4918 4919 if (ICmpInst::isSigned(Pred)) 4920 Pred = ICmpInst::getSwappedPredicate(Pred); 4921 else if (ICmpInst::isUnsigned(Pred)) 4922 Pred = ICmpInst::getSignedPredicate(Pred); 4923 // else for equality-comparisons just keep the predicate. 4924 4925 return ICmpInst::Create(Instruction::ICmp, Pred, X, 4926 Constant::getNullValue(X->getType()), I.getName()); 4927 } 4928 4929 // A value is not equal to its negation unless that value is 0 or 4930 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0 4931 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) && 4932 ICmpInst::isEquality(Pred)) { 4933 Type *Ty = X->getType(); 4934 uint32_t BitWidth = Ty->getScalarSizeInBits(); 4935 Constant *MaxSignedVal = 4936 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth)); 4937 Value *And = Builder.CreateAnd(X, MaxSignedVal); 4938 Constant *Zero = Constant::getNullValue(Ty); 4939 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero); 4940 } 4941 4942 return nullptr; 4943 } 4944 4945 static Instruction *foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q, 4946 InstCombinerImpl &IC) { 4947 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A; 4948 // Normalize and operand as operand 0. 4949 CmpInst::Predicate Pred = I.getPredicate(); 4950 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) { 4951 std::swap(Op0, Op1); 4952 Pred = ICmpInst::getSwappedPredicate(Pred); 4953 } 4954 4955 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A)))) 4956 return nullptr; 4957 4958 // (icmp (X & Y) u< X --> (X & Y) != X 4959 if (Pred == ICmpInst::ICMP_ULT) 4960 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); 4961 4962 // (icmp (X & Y) u>= X --> (X & Y) == X 4963 if (Pred == ICmpInst::ICMP_UGE) 4964 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); 4965 4966 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) { 4967 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and 4968 // Y is non-constant. If Y is constant the `X & C == C` form is preferable 4969 // so don't do this fold. 4970 if (!match(Op1, m_ImmConstant())) 4971 if (auto *NotOp1 = 4972 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder)) 4973 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1), 4974 Constant::getAllOnesValue(Op1->getType())); 4975 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible. 4976 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder)) 4977 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA), 4978 Constant::getNullValue(Op1->getType())); 4979 } 4980 4981 if (!ICmpInst::isSigned(Pred)) 4982 return nullptr; 4983 4984 KnownBits KnownY = IC.computeKnownBits(A, /*Depth=*/0, &I); 4985 // (X & NegY) spred X --> (X & NegY) upred X 4986 if (KnownY.isNegative()) 4987 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1); 4988 4989 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT) 4990 return nullptr; 4991 4992 if (KnownY.isNonNegative()) 4993 // (X & PosY) s<= X --> X s>= 0 4994 // (X & PosY) s> X --> X s< 0 4995 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1, 4996 Constant::getNullValue(Op1->getType())); 4997 4998 if (isKnownNegative(Op1, IC.getSimplifyQuery().getWithInstruction(&I))) 4999 // (NegX & Y) s<= NegX --> Y s< 0 5000 // (NegX & Y) s> NegX --> Y s>= 0 5001 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(Pred), A, 5002 Constant::getNullValue(A->getType())); 5003 5004 return nullptr; 5005 } 5006 5007 static Instruction *foldICmpOrXX(ICmpInst &I, const SimplifyQuery &Q, 5008 InstCombinerImpl &IC) { 5009 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A; 5010 5011 // Normalize or operand as operand 0. 5012 CmpInst::Predicate Pred = I.getPredicate(); 5013 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) { 5014 std::swap(Op0, Op1); 5015 Pred = ICmpInst::getSwappedPredicate(Pred); 5016 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) { 5017 return nullptr; 5018 } 5019 5020 // icmp (X | Y) u<= X --> (X | Y) == X 5021 if (Pred == ICmpInst::ICMP_ULE) 5022 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); 5023 5024 // icmp (X | Y) u> X --> (X | Y) != X 5025 if (Pred == ICmpInst::ICMP_UGT) 5026 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); 5027 5028 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) { 5029 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible 5030 if (Value *NotOp1 = 5031 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder)) 5032 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1), 5033 Constant::getNullValue(Op1->getType())); 5034 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible. 5035 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder)) 5036 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA), 5037 Constant::getAllOnesValue(Op1->getType())); 5038 } 5039 return nullptr; 5040 } 5041 5042 static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q, 5043 InstCombinerImpl &IC) { 5044 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A; 5045 // Normalize xor operand as operand 0. 5046 CmpInst::Predicate Pred = I.getPredicate(); 5047 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) { 5048 std::swap(Op0, Op1); 5049 Pred = ICmpInst::getSwappedPredicate(Pred); 5050 } 5051 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A)))) 5052 return nullptr; 5053 5054 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X 5055 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X 5056 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X 5057 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X 5058 CmpInst::Predicate PredOut = CmpInst::getStrictPredicate(Pred); 5059 if (PredOut != Pred && isKnownNonZero(A, Q)) 5060 return new ICmpInst(PredOut, Op0, Op1); 5061 5062 return nullptr; 5063 } 5064 5065 /// Try to fold icmp (binop), X or icmp X, (binop). 5066 /// TODO: A large part of this logic is duplicated in InstSimplify's 5067 /// simplifyICmpWithBinOp(). We should be able to share that and avoid the code 5068 /// duplication. 5069 Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I, 5070 const SimplifyQuery &SQ) { 5071 const SimplifyQuery Q = SQ.getWithInstruction(&I); 5072 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 5073 5074 // Special logic for binary operators. 5075 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0); 5076 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1); 5077 if (!BO0 && !BO1) 5078 return nullptr; 5079 5080 if (Instruction *NewICmp = foldICmpXNegX(I, Builder)) 5081 return NewICmp; 5082 5083 const CmpInst::Predicate Pred = I.getPredicate(); 5084 Value *X; 5085 5086 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare. 5087 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X 5088 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) && 5089 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) 5090 return new ICmpInst(Pred, Builder.CreateNot(Op1), X); 5091 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0 5092 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) && 5093 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) 5094 return new ICmpInst(Pred, X, Builder.CreateNot(Op0)); 5095 5096 { 5097 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1 5098 Constant *C; 5099 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)), 5100 m_ImmConstant(C)))) && 5101 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) { 5102 Constant *C2 = ConstantExpr::getNot(C); 5103 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1); 5104 } 5105 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X 5106 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)), 5107 m_ImmConstant(C)))) && 5108 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) { 5109 Constant *C2 = ConstantExpr::getNot(C); 5110 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X)); 5111 } 5112 } 5113 5114 // (icmp eq/ne (X, -P2), INT_MIN) 5115 // -> (icmp slt/sge X, INT_MIN + P2) 5116 if (ICmpInst::isEquality(Pred) && BO0 && 5117 match(I.getOperand(1), m_SignMask()) && 5118 match(BO0, m_And(m_Value(), m_NegatedPower2OrZero()))) { 5119 // Will Constant fold. 5120 Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1)); 5121 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT 5122 : ICmpInst::ICMP_SGE, 5123 BO0->getOperand(0), NewC); 5124 } 5125 5126 { 5127 // Similar to above: an unsigned overflow comparison may use offset + mask: 5128 // ((Op1 + C) & C) u< Op1 --> Op1 != 0 5129 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0 5130 // Op0 u> ((Op0 + C) & C) --> Op0 != 0 5131 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0 5132 BinaryOperator *BO; 5133 const APInt *C; 5134 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) && 5135 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) && 5136 match(BO, m_Add(m_Specific(Op1), m_SpecificIntAllowPoison(*C)))) { 5137 CmpInst::Predicate NewPred = 5138 Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ; 5139 Constant *Zero = ConstantInt::getNullValue(Op1->getType()); 5140 return new ICmpInst(NewPred, Op1, Zero); 5141 } 5142 5143 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) && 5144 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) && 5145 match(BO, m_Add(m_Specific(Op0), m_SpecificIntAllowPoison(*C)))) { 5146 CmpInst::Predicate NewPred = 5147 Pred == ICmpInst::ICMP_UGT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ; 5148 Constant *Zero = ConstantInt::getNullValue(Op1->getType()); 5149 return new ICmpInst(NewPred, Op0, Zero); 5150 } 5151 } 5152 5153 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false; 5154 bool Op0HasNUW = false, Op1HasNUW = false; 5155 bool Op0HasNSW = false, Op1HasNSW = false; 5156 // Analyze the case when either Op0 or Op1 is an add instruction. 5157 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null). 5158 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred, 5159 bool &HasNSW, bool &HasNUW) -> bool { 5160 if (isa<OverflowingBinaryOperator>(BO)) { 5161 HasNUW = BO.hasNoUnsignedWrap(); 5162 HasNSW = BO.hasNoSignedWrap(); 5163 return ICmpInst::isEquality(Pred) || 5164 (CmpInst::isUnsigned(Pred) && HasNUW) || 5165 (CmpInst::isSigned(Pred) && HasNSW); 5166 } else if (BO.getOpcode() == Instruction::Or) { 5167 HasNUW = true; 5168 HasNSW = true; 5169 return true; 5170 } else { 5171 return false; 5172 } 5173 }; 5174 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; 5175 5176 if (BO0) { 5177 match(BO0, m_AddLike(m_Value(A), m_Value(B))); 5178 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW); 5179 } 5180 if (BO1) { 5181 match(BO1, m_AddLike(m_Value(C), m_Value(D))); 5182 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW); 5183 } 5184 5185 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow. 5186 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow. 5187 if ((A == Op1 || B == Op1) && NoOp0WrapProblem) 5188 return new ICmpInst(Pred, A == Op1 ? B : A, 5189 Constant::getNullValue(Op1->getType())); 5190 5191 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow. 5192 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow. 5193 if ((C == Op0 || D == Op0) && NoOp1WrapProblem) 5194 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()), 5195 C == Op0 ? D : C); 5196 5197 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow. 5198 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem && 5199 NoOp1WrapProblem) { 5200 // Determine Y and Z in the form icmp (X+Y), (X+Z). 5201 Value *Y, *Z; 5202 if (A == C) { 5203 // C + B == C + D -> B == D 5204 Y = B; 5205 Z = D; 5206 } else if (A == D) { 5207 // D + B == C + D -> B == C 5208 Y = B; 5209 Z = C; 5210 } else if (B == C) { 5211 // A + C == C + D -> A == D 5212 Y = A; 5213 Z = D; 5214 } else { 5215 assert(B == D); 5216 // A + D == C + D -> A == C 5217 Y = A; 5218 Z = C; 5219 } 5220 return new ICmpInst(Pred, Y, Z); 5221 } 5222 5223 // icmp slt (A + -1), Op1 -> icmp sle A, Op1 5224 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT && 5225 match(B, m_AllOnes())) 5226 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1); 5227 5228 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1 5229 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE && 5230 match(B, m_AllOnes())) 5231 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1); 5232 5233 // icmp sle (A + 1), Op1 -> icmp slt A, Op1 5234 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One())) 5235 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1); 5236 5237 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1 5238 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One())) 5239 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1); 5240 5241 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C 5242 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT && 5243 match(D, m_AllOnes())) 5244 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C); 5245 5246 // icmp sle Op0, (C + -1) -> icmp slt Op0, C 5247 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE && 5248 match(D, m_AllOnes())) 5249 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C); 5250 5251 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C 5252 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One())) 5253 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C); 5254 5255 // icmp slt Op0, (C + 1) -> icmp sle Op0, C 5256 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One())) 5257 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C); 5258 5259 // TODO: The subtraction-related identities shown below also hold, but 5260 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations 5261 // wouldn't happen even if they were implemented. 5262 // 5263 // icmp ult (A - 1), Op1 -> icmp ule A, Op1 5264 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1 5265 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C 5266 // icmp ule Op0, (C - 1) -> icmp ult Op0, C 5267 5268 // icmp ule (A + 1), Op0 -> icmp ult A, Op1 5269 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One())) 5270 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1); 5271 5272 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1 5273 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One())) 5274 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1); 5275 5276 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C 5277 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One())) 5278 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C); 5279 5280 // icmp ult Op0, (C + 1) -> icmp ule Op0, C 5281 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One())) 5282 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C); 5283 5284 // if C1 has greater magnitude than C2: 5285 // icmp (A + C1), (C + C2) -> icmp (A + C3), C 5286 // s.t. C3 = C1 - C2 5287 // 5288 // if C2 has greater magnitude than C1: 5289 // icmp (A + C1), (C + C2) -> icmp A, (C + C3) 5290 // s.t. C3 = C2 - C1 5291 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem && 5292 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) { 5293 const APInt *AP1, *AP2; 5294 // TODO: Support non-uniform vectors. 5295 // TODO: Allow poison passthrough if B or D's element is poison. 5296 if (match(B, m_APIntAllowPoison(AP1)) && 5297 match(D, m_APIntAllowPoison(AP2)) && 5298 AP1->isNegative() == AP2->isNegative()) { 5299 APInt AP1Abs = AP1->abs(); 5300 APInt AP2Abs = AP2->abs(); 5301 if (AP1Abs.uge(AP2Abs)) { 5302 APInt Diff = *AP1 - *AP2; 5303 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff); 5304 Value *NewAdd = Builder.CreateAdd( 5305 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW); 5306 return new ICmpInst(Pred, NewAdd, C); 5307 } else { 5308 APInt Diff = *AP2 - *AP1; 5309 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff); 5310 Value *NewAdd = Builder.CreateAdd( 5311 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW); 5312 return new ICmpInst(Pred, A, NewAdd); 5313 } 5314 } 5315 Constant *Cst1, *Cst2; 5316 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) && 5317 ICmpInst::isEquality(Pred)) { 5318 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1); 5319 Value *NewAdd = Builder.CreateAdd(C, Diff); 5320 return new ICmpInst(Pred, A, NewAdd); 5321 } 5322 } 5323 5324 // Analyze the case when either Op0 or Op1 is a sub instruction. 5325 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null). 5326 A = nullptr; 5327 B = nullptr; 5328 C = nullptr; 5329 D = nullptr; 5330 if (BO0 && BO0->getOpcode() == Instruction::Sub) { 5331 A = BO0->getOperand(0); 5332 B = BO0->getOperand(1); 5333 } 5334 if (BO1 && BO1->getOpcode() == Instruction::Sub) { 5335 C = BO1->getOperand(0); 5336 D = BO1->getOperand(1); 5337 } 5338 5339 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow. 5340 if (A == Op1 && NoOp0WrapProblem) 5341 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B); 5342 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow. 5343 if (C == Op0 && NoOp1WrapProblem) 5344 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType())); 5345 5346 // Convert sub-with-unsigned-overflow comparisons into a comparison of args. 5347 // (A - B) u>/u<= A --> B u>/u<= A 5348 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) 5349 return new ICmpInst(Pred, B, A); 5350 // C u</u>= (C - D) --> C u</u>= D 5351 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) 5352 return new ICmpInst(Pred, C, D); 5353 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0 5354 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) && 5355 isKnownNonZero(B, Q)) 5356 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(Pred), B, A); 5357 // C u<=/u> (C - D) --> C u</u>= D iff B != 0 5358 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) && 5359 isKnownNonZero(D, Q)) 5360 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(Pred), C, D); 5361 5362 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow. 5363 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem) 5364 return new ICmpInst(Pred, A, C); 5365 5366 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow. 5367 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem) 5368 return new ICmpInst(Pred, D, B); 5369 5370 // icmp (0-X) < cst --> x > -cst 5371 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) { 5372 Value *X; 5373 if (match(BO0, m_Neg(m_Value(X)))) 5374 if (Constant *RHSC = dyn_cast<Constant>(Op1)) 5375 if (RHSC->isNotMinSignedValue()) 5376 return new ICmpInst(I.getSwappedPredicate(), X, 5377 ConstantExpr::getNeg(RHSC)); 5378 } 5379 5380 if (Instruction * R = foldICmpXorXX(I, Q, *this)) 5381 return R; 5382 if (Instruction *R = foldICmpOrXX(I, Q, *this)) 5383 return R; 5384 5385 { 5386 // Try to remove shared multiplier from comparison: 5387 // X * Z pred Y * Z 5388 Value *X, *Y, *Z; 5389 if ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) && 5390 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) || 5391 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) && 5392 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y))))) { 5393 if (ICmpInst::isSigned(Pred)) { 5394 if (Op0HasNSW && Op1HasNSW) { 5395 KnownBits ZKnown = computeKnownBits(Z, 0, &I); 5396 if (ZKnown.isStrictlyPositive()) 5397 return new ICmpInst(Pred, X, Y); 5398 if (ZKnown.isNegative()) 5399 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y); 5400 Value *LessThan = simplifyICmpInst(ICmpInst::ICMP_SLT, X, Y, 5401 SQ.getWithInstruction(&I)); 5402 if (LessThan && match(LessThan, m_One())) 5403 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z, 5404 Constant::getNullValue(Z->getType())); 5405 Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y, 5406 SQ.getWithInstruction(&I)); 5407 if (GreaterThan && match(GreaterThan, m_One())) 5408 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType())); 5409 } 5410 } else { 5411 bool NonZero; 5412 if (ICmpInst::isEquality(Pred)) { 5413 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0 5414 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) && 5415 isKnownNonEqual(X, Y, DL, &AC, &I, &DT)) 5416 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType())); 5417 5418 KnownBits ZKnown = computeKnownBits(Z, 0, &I); 5419 // if Z % 2 != 0 5420 // X * Z eq/ne Y * Z -> X eq/ne Y 5421 if (ZKnown.countMaxTrailingZeros() == 0) 5422 return new ICmpInst(Pred, X, Y); 5423 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q); 5424 // if Z != 0 and nsw(X * Z) and nsw(Y * Z) 5425 // X * Z eq/ne Y * Z -> X eq/ne Y 5426 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW) 5427 return new ICmpInst(Pred, X, Y); 5428 } else 5429 NonZero = isKnownNonZero(Z, Q); 5430 5431 // If Z != 0 and nuw(X * Z) and nuw(Y * Z) 5432 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y 5433 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW) 5434 return new ICmpInst(Pred, X, Y); 5435 } 5436 } 5437 } 5438 5439 BinaryOperator *SRem = nullptr; 5440 // icmp (srem X, Y), Y 5441 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1)) 5442 SRem = BO0; 5443 // icmp Y, (srem X, Y) 5444 else if (BO1 && BO1->getOpcode() == Instruction::SRem && 5445 Op0 == BO1->getOperand(1)) 5446 SRem = BO1; 5447 if (SRem) { 5448 // We don't check hasOneUse to avoid increasing register pressure because 5449 // the value we use is the same value this instruction was already using. 5450 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) { 5451 default: 5452 break; 5453 case ICmpInst::ICMP_EQ: 5454 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 5455 case ICmpInst::ICMP_NE: 5456 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 5457 case ICmpInst::ICMP_SGT: 5458 case ICmpInst::ICMP_SGE: 5459 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1), 5460 Constant::getAllOnesValue(SRem->getType())); 5461 case ICmpInst::ICMP_SLT: 5462 case ICmpInst::ICMP_SLE: 5463 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1), 5464 Constant::getNullValue(SRem->getType())); 5465 } 5466 } 5467 5468 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() && 5469 (BO0->hasOneUse() || BO1->hasOneUse()) && 5470 BO0->getOperand(1) == BO1->getOperand(1)) { 5471 switch (BO0->getOpcode()) { 5472 default: 5473 break; 5474 case Instruction::Add: 5475 case Instruction::Sub: 5476 case Instruction::Xor: { 5477 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b 5478 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); 5479 5480 const APInt *C; 5481 if (match(BO0->getOperand(1), m_APInt(C))) { 5482 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b 5483 if (C->isSignMask()) { 5484 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate(); 5485 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0)); 5486 } 5487 5488 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b 5489 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) { 5490 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate(); 5491 NewPred = I.getSwappedPredicate(NewPred); 5492 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0)); 5493 } 5494 } 5495 break; 5496 } 5497 case Instruction::Mul: { 5498 if (!I.isEquality()) 5499 break; 5500 5501 const APInt *C; 5502 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() && 5503 !C->isOne()) { 5504 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask) 5505 // Mask = -1 >> count-trailing-zeros(C). 5506 if (unsigned TZs = C->countr_zero()) { 5507 Constant *Mask = ConstantInt::get( 5508 BO0->getType(), 5509 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs)); 5510 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask); 5511 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask); 5512 return new ICmpInst(Pred, And1, And2); 5513 } 5514 } 5515 break; 5516 } 5517 case Instruction::UDiv: 5518 case Instruction::LShr: 5519 if (I.isSigned() || !BO0->isExact() || !BO1->isExact()) 5520 break; 5521 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); 5522 5523 case Instruction::SDiv: 5524 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) || 5525 !BO0->isExact() || !BO1->isExact()) 5526 break; 5527 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); 5528 5529 case Instruction::AShr: 5530 if (!BO0->isExact() || !BO1->isExact()) 5531 break; 5532 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); 5533 5534 case Instruction::Shl: { 5535 bool NUW = Op0HasNUW && Op1HasNUW; 5536 bool NSW = Op0HasNSW && Op1HasNSW; 5537 if (!NUW && !NSW) 5538 break; 5539 if (!NSW && I.isSigned()) 5540 break; 5541 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); 5542 } 5543 } 5544 } 5545 5546 if (BO0) { 5547 // Transform A & (L - 1) `ult` L --> L != 0 5548 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes()); 5549 auto BitwiseAnd = m_c_And(m_Value(), LSubOne); 5550 5551 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) { 5552 auto *Zero = Constant::getNullValue(BO0->getType()); 5553 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero); 5554 } 5555 } 5556 5557 // For unsigned predicates / eq / ne: 5558 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0 5559 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x 5560 if (!ICmpInst::isSigned(Pred)) { 5561 if (match(Op0, m_Shl(m_Specific(Op1), m_One()))) 5562 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1, 5563 Constant::getNullValue(Op1->getType())); 5564 else if (match(Op1, m_Shl(m_Specific(Op0), m_One()))) 5565 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), 5566 Constant::getNullValue(Op0->getType()), Op0); 5567 } 5568 5569 if (Value *V = foldMultiplicationOverflowCheck(I)) 5570 return replaceInstUsesWith(I, V); 5571 5572 if (Instruction *R = foldICmpAndXX(I, Q, *this)) 5573 return R; 5574 5575 if (Value *V = foldICmpWithTruncSignExtendedVal(I, Builder)) 5576 return replaceInstUsesWith(I, V); 5577 5578 if (Value *V = foldShiftIntoShiftInAnotherHandOfAndInICmp(I, SQ, Builder)) 5579 return replaceInstUsesWith(I, V); 5580 5581 return nullptr; 5582 } 5583 5584 /// Fold icmp Pred min|max(X, Y), Z. 5585 Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I, 5586 MinMaxIntrinsic *MinMax, 5587 Value *Z, CmpPredicate Pred) { 5588 Value *X = MinMax->getLHS(); 5589 Value *Y = MinMax->getRHS(); 5590 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned()) 5591 return nullptr; 5592 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) { 5593 // Revert the transform signed pred -> unsigned pred 5594 // TODO: We can flip the signedness of predicate if both operands of icmp 5595 // are negative. 5596 if (isKnownNonNegative(Z, SQ.getWithInstruction(&I)) && 5597 isKnownNonNegative(MinMax, SQ.getWithInstruction(&I))) { 5598 Pred = ICmpInst::getFlippedSignednessPredicate(Pred); 5599 } else 5600 return nullptr; 5601 } 5602 SimplifyQuery Q = SQ.getWithInstruction(&I); 5603 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> { 5604 if (!Val) 5605 return std::nullopt; 5606 if (match(Val, m_One())) 5607 return true; 5608 if (match(Val, m_Zero())) 5609 return false; 5610 return std::nullopt; 5611 }; 5612 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q)); 5613 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q)); 5614 if (!CmpXZ.has_value() && !CmpYZ.has_value()) 5615 return nullptr; 5616 if (!CmpXZ.has_value()) { 5617 std::swap(X, Y); 5618 std::swap(CmpXZ, CmpYZ); 5619 } 5620 5621 auto FoldIntoCmpYZ = [&]() -> Instruction * { 5622 if (CmpYZ.has_value()) 5623 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ)); 5624 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z); 5625 }; 5626 5627 switch (Pred) { 5628 case ICmpInst::ICMP_EQ: 5629 case ICmpInst::ICMP_NE: { 5630 // If X == Z: 5631 // Expr Result 5632 // min(X, Y) == Z X <= Y 5633 // max(X, Y) == Z X >= Y 5634 // min(X, Y) != Z X > Y 5635 // max(X, Y) != Z X < Y 5636 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) { 5637 ICmpInst::Predicate NewPred = 5638 ICmpInst::getNonStrictPredicate(MinMax->getPredicate()); 5639 if (Pred == ICmpInst::ICMP_NE) 5640 NewPred = ICmpInst::getInversePredicate(NewPred); 5641 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y); 5642 } 5643 // Otherwise (X != Z): 5644 ICmpInst::Predicate NewPred = MinMax->getPredicate(); 5645 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q)); 5646 if (!MinMaxCmpXZ.has_value()) { 5647 std::swap(X, Y); 5648 std::swap(CmpXZ, CmpYZ); 5649 // Re-check pre-condition X != Z 5650 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ) 5651 break; 5652 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q)); 5653 } 5654 if (!MinMaxCmpXZ.has_value()) 5655 break; 5656 if (*MinMaxCmpXZ) { 5657 // Expr Fact Result 5658 // min(X, Y) == Z X < Z false 5659 // max(X, Y) == Z X > Z false 5660 // min(X, Y) != Z X < Z true 5661 // max(X, Y) != Z X > Z true 5662 return replaceInstUsesWith( 5663 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE)); 5664 } else { 5665 // Expr Fact Result 5666 // min(X, Y) == Z X > Z Y == Z 5667 // max(X, Y) == Z X < Z Y == Z 5668 // min(X, Y) != Z X > Z Y != Z 5669 // max(X, Y) != Z X < Z Y != Z 5670 return FoldIntoCmpYZ(); 5671 } 5672 break; 5673 } 5674 case ICmpInst::ICMP_SLT: 5675 case ICmpInst::ICMP_ULT: 5676 case ICmpInst::ICMP_SLE: 5677 case ICmpInst::ICMP_ULE: 5678 case ICmpInst::ICMP_SGT: 5679 case ICmpInst::ICMP_UGT: 5680 case ICmpInst::ICMP_SGE: 5681 case ICmpInst::ICMP_UGE: { 5682 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred); 5683 if (*CmpXZ) { 5684 if (IsSame) { 5685 // Expr Fact Result 5686 // min(X, Y) < Z X < Z true 5687 // min(X, Y) <= Z X <= Z true 5688 // max(X, Y) > Z X > Z true 5689 // max(X, Y) >= Z X >= Z true 5690 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 5691 } else { 5692 // Expr Fact Result 5693 // max(X, Y) < Z X < Z Y < Z 5694 // max(X, Y) <= Z X <= Z Y <= Z 5695 // min(X, Y) > Z X > Z Y > Z 5696 // min(X, Y) >= Z X >= Z Y >= Z 5697 return FoldIntoCmpYZ(); 5698 } 5699 } else { 5700 if (IsSame) { 5701 // Expr Fact Result 5702 // min(X, Y) < Z X >= Z Y < Z 5703 // min(X, Y) <= Z X > Z Y <= Z 5704 // max(X, Y) > Z X <= Z Y > Z 5705 // max(X, Y) >= Z X < Z Y >= Z 5706 return FoldIntoCmpYZ(); 5707 } else { 5708 // Expr Fact Result 5709 // max(X, Y) < Z X >= Z false 5710 // max(X, Y) <= Z X > Z false 5711 // min(X, Y) > Z X <= Z false 5712 // min(X, Y) >= Z X < Z false 5713 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 5714 } 5715 } 5716 break; 5717 } 5718 default: 5719 break; 5720 } 5721 5722 return nullptr; 5723 } 5724 5725 // Canonicalize checking for a power-of-2-or-zero value: 5726 static Instruction *foldICmpPow2Test(ICmpInst &I, 5727 InstCombiner::BuilderTy &Builder) { 5728 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 5729 const CmpInst::Predicate Pred = I.getPredicate(); 5730 Value *A = nullptr; 5731 bool CheckIs; 5732 if (I.isEquality()) { 5733 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants) 5734 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants) 5735 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()), 5736 m_Deferred(A)))) || 5737 !match(Op1, m_ZeroInt())) 5738 A = nullptr; 5739 5740 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants) 5741 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants) 5742 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1))))) 5743 A = Op1; 5744 else if (match(Op1, 5745 m_OneUse(m_c_And(m_Neg(m_Specific(Op0)), m_Specific(Op0))))) 5746 A = Op0; 5747 5748 CheckIs = Pred == ICmpInst::ICMP_EQ; 5749 } else if (ICmpInst::isUnsigned(Pred)) { 5750 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants) 5751 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants) 5752 5753 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) && 5754 match(Op0, m_OneUse(m_c_Xor(m_Add(m_Specific(Op1), m_AllOnes()), 5755 m_Specific(Op1))))) { 5756 A = Op1; 5757 CheckIs = Pred == ICmpInst::ICMP_UGE; 5758 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) && 5759 match(Op1, m_OneUse(m_c_Xor(m_Add(m_Specific(Op0), m_AllOnes()), 5760 m_Specific(Op0))))) { 5761 A = Op0; 5762 CheckIs = Pred == ICmpInst::ICMP_ULE; 5763 } 5764 } 5765 5766 if (A) { 5767 Type *Ty = A->getType(); 5768 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A); 5769 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop, 5770 ConstantInt::get(Ty, 2)) 5771 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop, 5772 ConstantInt::get(Ty, 1)); 5773 } 5774 5775 return nullptr; 5776 } 5777 5778 Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) { 5779 if (!I.isEquality()) 5780 return nullptr; 5781 5782 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 5783 const CmpInst::Predicate Pred = I.getPredicate(); 5784 Value *A, *B, *C, *D; 5785 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { 5786 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 5787 Value *OtherVal = A == Op1 ? B : A; 5788 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType())); 5789 } 5790 5791 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { 5792 // A^c1 == C^c2 --> A == C^(c1^c2) 5793 ConstantInt *C1, *C2; 5794 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) && 5795 Op1->hasOneUse()) { 5796 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue()); 5797 Value *Xor = Builder.CreateXor(C, NC); 5798 return new ICmpInst(Pred, A, Xor); 5799 } 5800 5801 // A^B == A^D -> B == D 5802 if (A == C) 5803 return new ICmpInst(Pred, B, D); 5804 if (A == D) 5805 return new ICmpInst(Pred, B, C); 5806 if (B == C) 5807 return new ICmpInst(Pred, A, D); 5808 if (B == D) 5809 return new ICmpInst(Pred, A, C); 5810 } 5811 } 5812 5813 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) { 5814 // A == (A^B) -> B == 0 5815 Value *OtherVal = A == Op0 ? B : A; 5816 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType())); 5817 } 5818 5819 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 5820 if (match(Op0, m_And(m_Value(A), m_Value(B))) && 5821 match(Op1, m_And(m_Value(C), m_Value(D)))) { 5822 Value *X = nullptr, *Y = nullptr, *Z = nullptr; 5823 5824 if (A == C) { 5825 X = B; 5826 Y = D; 5827 Z = A; 5828 } else if (A == D) { 5829 X = B; 5830 Y = C; 5831 Z = A; 5832 } else if (B == C) { 5833 X = A; 5834 Y = D; 5835 Z = B; 5836 } else if (B == D) { 5837 X = A; 5838 Y = C; 5839 Z = B; 5840 } 5841 5842 if (X) { 5843 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0` 5844 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional 5845 // instructions. 5846 const APInt *C0, *C1; 5847 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) && 5848 (*C0 ^ *C1).isNegatedPowerOf2(); 5849 5850 // If either Op0/Op1 are both one use or X^Y will constant fold and one of 5851 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral 5852 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`. 5853 int UseCnt = 5854 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) + 5855 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant()))); 5856 if (XorIsNegP2 || UseCnt >= 2) { 5857 // Build (X^Y) & Z 5858 Op1 = Builder.CreateXor(X, Y); 5859 Op1 = Builder.CreateAnd(Op1, Z); 5860 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType())); 5861 } 5862 } 5863 } 5864 5865 { 5866 // Similar to above, but specialized for constant because invert is needed: 5867 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0 5868 Value *X, *Y; 5869 Constant *C; 5870 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) && 5871 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) { 5872 Value *Xor = Builder.CreateXor(X, Y); 5873 Value *And = Builder.CreateAnd(Xor, ConstantExpr::getNot(C)); 5874 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType())); 5875 } 5876 } 5877 5878 if (match(Op1, m_ZExt(m_Value(A))) && 5879 (Op0->hasOneUse() || Op1->hasOneUse())) { 5880 // (B & (Pow2C-1)) == zext A --> A == trunc B 5881 // (B & (Pow2C-1)) != zext A --> A != trunc B 5882 const APInt *MaskC; 5883 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) && 5884 MaskC->countr_one() == A->getType()->getScalarSizeInBits()) 5885 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType())); 5886 } 5887 5888 // (A >> C) == (B >> C) --> (A^B) u< (1 << C) 5889 // For lshr and ashr pairs. 5890 const APInt *AP1, *AP2; 5891 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) && 5892 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) || 5893 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) && 5894 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) { 5895 if (AP1 != AP2) 5896 return nullptr; 5897 unsigned TypeBits = AP1->getBitWidth(); 5898 unsigned ShAmt = AP1->getLimitedValue(TypeBits); 5899 if (ShAmt < TypeBits && ShAmt != 0) { 5900 ICmpInst::Predicate NewPred = 5901 Pred == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; 5902 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted"); 5903 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt); 5904 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal)); 5905 } 5906 } 5907 5908 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0 5909 ConstantInt *Cst1; 5910 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) && 5911 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) { 5912 unsigned TypeBits = Cst1->getBitWidth(); 5913 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits); 5914 if (ShAmt < TypeBits && ShAmt != 0) { 5915 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted"); 5916 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt); 5917 Value *And = Builder.CreateAnd(Xor, Builder.getInt(AndVal), 5918 I.getName() + ".mask"); 5919 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType())); 5920 } 5921 } 5922 5923 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to 5924 // "icmp (and X, mask), cst" 5925 uint64_t ShAmt = 0; 5926 if (Op0->hasOneUse() && 5927 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) && 5928 match(Op1, m_ConstantInt(Cst1)) && 5929 // Only do this when A has multiple uses. This is most important to do 5930 // when it exposes other optimizations. 5931 !A->hasOneUse()) { 5932 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits(); 5933 5934 if (ShAmt < ASize) { 5935 APInt MaskV = 5936 APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits()); 5937 MaskV <<= ShAmt; 5938 5939 APInt CmpV = Cst1->getValue().zext(ASize); 5940 CmpV <<= ShAmt; 5941 5942 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV)); 5943 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV)); 5944 } 5945 } 5946 5947 if (Instruction *ICmp = foldICmpIntrinsicWithIntrinsic(I, Builder)) 5948 return ICmp; 5949 5950 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks the 5951 // top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s INT_MAX", 5952 // which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a few steps 5953 // of instcombine. 5954 unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); 5955 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) && 5956 match(Op1, m_Trunc(m_LShr(m_Specific(A), m_SpecificInt(BitWidth)))) && 5957 A->getType()->getScalarSizeInBits() == BitWidth * 2 && 5958 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) { 5959 APInt C = APInt::getOneBitSet(BitWidth * 2, BitWidth - 1); 5960 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C)); 5961 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT 5962 : ICmpInst::ICMP_UGE, 5963 Add, ConstantInt::get(A->getType(), C.shl(1))); 5964 } 5965 5966 // Canonicalize: 5967 // Assume B_Pow2 != 0 5968 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0 5969 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0 5970 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) && 5971 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, 0, &I)) 5972 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0, 5973 ConstantInt::getNullValue(Op0->getType())); 5974 5975 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) && 5976 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, 0, &I)) 5977 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1, 5978 ConstantInt::getNullValue(Op1->getType())); 5979 5980 // Canonicalize: 5981 // icmp eq/ne X, OneUse(rotate-right(X)) 5982 // -> icmp eq/ne X, rotate-left(X) 5983 // We generally try to convert rotate-right -> rotate-left, this just 5984 // canonicalizes another case. 5985 if (match(&I, m_c_ICmp(m_Value(A), 5986 m_OneUse(m_Intrinsic<Intrinsic::fshr>( 5987 m_Deferred(A), m_Deferred(A), m_Value(B)))))) 5988 return new ICmpInst( 5989 Pred, A, 5990 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B})); 5991 5992 // Canonicalize: 5993 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst 5994 Constant *Cst; 5995 if (match(&I, m_c_ICmp(m_OneUse(m_Xor(m_Value(A), m_ImmConstant(Cst))), 5996 m_CombineAnd(m_Value(B), m_Unless(m_ImmConstant()))))) 5997 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst); 5998 5999 { 6000 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2) 6001 auto m_Matcher = 6002 m_CombineOr(m_CombineOr(m_c_Add(m_Value(B), m_Deferred(A)), 6003 m_c_Xor(m_Value(B), m_Deferred(A))), 6004 m_Sub(m_Value(B), m_Deferred(A))); 6005 std::optional<bool> IsZero = std::nullopt; 6006 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), 6007 m_Deferred(A)))) 6008 IsZero = false; 6009 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0) 6010 else if (match(&I, 6011 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero()))) 6012 IsZero = true; 6013 6014 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, /*Depth*/ 0, &I)) 6015 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2) 6016 // -> (icmp eq/ne (and X, P2), 0) 6017 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0) 6018 // -> (icmp eq/ne (and X, P2), P2) 6019 return new ICmpInst(Pred, Builder.CreateAnd(B, A), 6020 *IsZero ? A 6021 : ConstantInt::getNullValue(A->getType())); 6022 } 6023 6024 return nullptr; 6025 } 6026 6027 Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) { 6028 ICmpInst::Predicate Pred = ICmp.getPredicate(); 6029 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1); 6030 6031 // Try to canonicalize trunc + compare-to-constant into a mask + cmp. 6032 // The trunc masks high bits while the compare may effectively mask low bits. 6033 Value *X; 6034 const APInt *C; 6035 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C))) 6036 return nullptr; 6037 6038 // This matches patterns corresponding to tests of the signbit as well as: 6039 // (trunc X) pred C2 --> (X & Mask) == C 6040 if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*WithTrunc=*/true, 6041 /*AllowNonZeroC=*/true)) { 6042 Value *And = Builder.CreateAnd(Res->X, Res->Mask); 6043 Constant *C = ConstantInt::get(Res->X->getType(), Res->C); 6044 return new ICmpInst(Res->Pred, And, C); 6045 } 6046 6047 unsigned SrcBits = X->getType()->getScalarSizeInBits(); 6048 if (auto *II = dyn_cast<IntrinsicInst>(X)) { 6049 if (II->getIntrinsicID() == Intrinsic::cttz || 6050 II->getIntrinsicID() == Intrinsic::ctlz) { 6051 unsigned MaxRet = SrcBits; 6052 // If the "is_zero_poison" argument is set, then we know at least 6053 // one bit is set in the input, so the result is always at least one 6054 // less than the full bitwidth of that input. 6055 if (match(II->getArgOperand(1), m_One())) 6056 MaxRet--; 6057 6058 // Make sure the destination is wide enough to hold the largest output of 6059 // the intrinsic. 6060 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits()) 6061 if (Instruction *I = 6062 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits))) 6063 return I; 6064 } 6065 } 6066 6067 return nullptr; 6068 } 6069 6070 Instruction *InstCombinerImpl::foldICmpWithZextOrSext(ICmpInst &ICmp) { 6071 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0"); 6072 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0)); 6073 Value *X; 6074 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X)))) 6075 return nullptr; 6076 6077 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt; 6078 bool IsSignedCmp = ICmp.isSigned(); 6079 6080 // icmp Pred (ext X), (ext Y) 6081 Value *Y; 6082 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) { 6083 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0)); 6084 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1)); 6085 6086 if (IsZext0 != IsZext1) { 6087 // If X and Y and both i1 6088 // (icmp eq/ne (zext X) (sext Y)) 6089 // eq -> (icmp eq (or X, Y), 0) 6090 // ne -> (icmp ne (or X, Y), 0) 6091 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) && 6092 Y->getType()->isIntOrIntVectorTy(1)) 6093 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y), 6094 Constant::getNullValue(X->getType())); 6095 6096 // If we have mismatched casts and zext has the nneg flag, we can 6097 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit. 6098 6099 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0)); 6100 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1)); 6101 6102 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg(); 6103 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg(); 6104 6105 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1)) 6106 IsSignedExt = true; 6107 else 6108 return nullptr; 6109 } 6110 6111 // Not an extension from the same type? 6112 Type *XTy = X->getType(), *YTy = Y->getType(); 6113 if (XTy != YTy) { 6114 // One of the casts must have one use because we are creating a new cast. 6115 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse()) 6116 return nullptr; 6117 // Extend the narrower operand to the type of the wider operand. 6118 CastInst::CastOps CastOpcode = 6119 IsSignedExt ? Instruction::SExt : Instruction::ZExt; 6120 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits()) 6121 X = Builder.CreateCast(CastOpcode, X, YTy); 6122 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits()) 6123 Y = Builder.CreateCast(CastOpcode, Y, XTy); 6124 else 6125 return nullptr; 6126 } 6127 6128 // (zext X) == (zext Y) --> X == Y 6129 // (sext X) == (sext Y) --> X == Y 6130 if (ICmp.isEquality()) 6131 return new ICmpInst(ICmp.getPredicate(), X, Y); 6132 6133 // A signed comparison of sign extended values simplifies into a 6134 // signed comparison. 6135 if (IsSignedCmp && IsSignedExt) 6136 return new ICmpInst(ICmp.getPredicate(), X, Y); 6137 6138 // The other three cases all fold into an unsigned comparison. 6139 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y); 6140 } 6141 6142 // Below here, we are only folding a compare with constant. 6143 auto *C = dyn_cast<Constant>(ICmp.getOperand(1)); 6144 if (!C) 6145 return nullptr; 6146 6147 // If a lossless truncate is possible... 6148 Type *SrcTy = CastOp0->getSrcTy(); 6149 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode()); 6150 if (Res) { 6151 if (ICmp.isEquality()) 6152 return new ICmpInst(ICmp.getPredicate(), X, Res); 6153 6154 // A signed comparison of sign extended values simplifies into a 6155 // signed comparison. 6156 if (IsSignedExt && IsSignedCmp) 6157 return new ICmpInst(ICmp.getPredicate(), X, Res); 6158 6159 // The other three cases all fold into an unsigned comparison. 6160 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res); 6161 } 6162 6163 // The re-extended constant changed, partly changed (in the case of a vector), 6164 // or could not be determined to be equal (in the case of a constant 6165 // expression), so the constant cannot be represented in the shorter type. 6166 // All the cases that fold to true or false will have already been handled 6167 // by simplifyICmpInst, so only deal with the tricky case. 6168 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C)) 6169 return nullptr; 6170 6171 // Is source op positive? 6172 // icmp ult (sext X), C --> icmp sgt X, -1 6173 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT) 6174 return new ICmpInst(CmpInst::ICMP_SGT, X, Constant::getAllOnesValue(SrcTy)); 6175 6176 // Is source op negative? 6177 // icmp ugt (sext X), C --> icmp slt X, 0 6178 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!"); 6179 return new ICmpInst(CmpInst::ICMP_SLT, X, Constant::getNullValue(SrcTy)); 6180 } 6181 6182 /// Handle icmp (cast x), (cast or constant). 6183 Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) { 6184 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as 6185 // icmp compares only pointer's value. 6186 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2. 6187 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0)); 6188 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1)); 6189 if (SimplifiedOp0 || SimplifiedOp1) 6190 return new ICmpInst(ICmp.getPredicate(), 6191 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0), 6192 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1)); 6193 6194 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0)); 6195 if (!CastOp0) 6196 return nullptr; 6197 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1))) 6198 return nullptr; 6199 6200 Value *Op0Src = CastOp0->getOperand(0); 6201 Type *SrcTy = CastOp0->getSrcTy(); 6202 Type *DestTy = CastOp0->getDestTy(); 6203 6204 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the 6205 // integer type is the same size as the pointer type. 6206 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) { 6207 if (isa<VectorType>(PtrTy)) { 6208 PtrTy = cast<VectorType>(PtrTy)->getElementType(); 6209 IntTy = cast<VectorType>(IntTy)->getElementType(); 6210 } 6211 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth(); 6212 }; 6213 if (CastOp0->getOpcode() == Instruction::PtrToInt && 6214 CompatibleSizes(SrcTy, DestTy)) { 6215 Value *NewOp1 = nullptr; 6216 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) { 6217 Value *PtrSrc = PtrToIntOp1->getOperand(0); 6218 if (PtrSrc->getType() == Op0Src->getType()) 6219 NewOp1 = PtrToIntOp1->getOperand(0); 6220 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) { 6221 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy); 6222 } 6223 6224 if (NewOp1) 6225 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1); 6226 } 6227 6228 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c). 6229 if (CastOp0->getOpcode() == Instruction::IntToPtr && 6230 CompatibleSizes(DestTy, SrcTy)) { 6231 Value *NewOp1 = nullptr; 6232 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) { 6233 Value *IntSrc = IntToPtrOp1->getOperand(0); 6234 if (IntSrc->getType() == Op0Src->getType()) 6235 NewOp1 = IntToPtrOp1->getOperand(0); 6236 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) { 6237 NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL); 6238 } 6239 6240 if (NewOp1) 6241 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1); 6242 } 6243 6244 if (Instruction *R = foldICmpWithTrunc(ICmp)) 6245 return R; 6246 6247 return foldICmpWithZextOrSext(ICmp); 6248 } 6249 6250 static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned) { 6251 switch (BinaryOp) { 6252 default: 6253 llvm_unreachable("Unsupported binary op"); 6254 case Instruction::Add: 6255 case Instruction::Sub: 6256 return match(RHS, m_Zero()); 6257 case Instruction::Mul: 6258 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) && 6259 match(RHS, m_One()); 6260 } 6261 } 6262 6263 OverflowResult 6264 InstCombinerImpl::computeOverflow(Instruction::BinaryOps BinaryOp, 6265 bool IsSigned, Value *LHS, Value *RHS, 6266 Instruction *CxtI) const { 6267 switch (BinaryOp) { 6268 default: 6269 llvm_unreachable("Unsupported binary op"); 6270 case Instruction::Add: 6271 if (IsSigned) 6272 return computeOverflowForSignedAdd(LHS, RHS, CxtI); 6273 else 6274 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI); 6275 case Instruction::Sub: 6276 if (IsSigned) 6277 return computeOverflowForSignedSub(LHS, RHS, CxtI); 6278 else 6279 return computeOverflowForUnsignedSub(LHS, RHS, CxtI); 6280 case Instruction::Mul: 6281 if (IsSigned) 6282 return computeOverflowForSignedMul(LHS, RHS, CxtI); 6283 else 6284 return computeOverflowForUnsignedMul(LHS, RHS, CxtI); 6285 } 6286 } 6287 6288 bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, 6289 bool IsSigned, Value *LHS, 6290 Value *RHS, Instruction &OrigI, 6291 Value *&Result, 6292 Constant *&Overflow) { 6293 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS)) 6294 std::swap(LHS, RHS); 6295 6296 // If the overflow check was an add followed by a compare, the insertion point 6297 // may be pointing to the compare. We want to insert the new instructions 6298 // before the add in case there are uses of the add between the add and the 6299 // compare. 6300 Builder.SetInsertPoint(&OrigI); 6301 6302 Type *OverflowTy = Type::getInt1Ty(LHS->getContext()); 6303 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType())) 6304 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount()); 6305 6306 if (isNeutralValue(BinaryOp, RHS, IsSigned)) { 6307 Result = LHS; 6308 Overflow = ConstantInt::getFalse(OverflowTy); 6309 return true; 6310 } 6311 6312 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) { 6313 case OverflowResult::MayOverflow: 6314 return false; 6315 case OverflowResult::AlwaysOverflowsLow: 6316 case OverflowResult::AlwaysOverflowsHigh: 6317 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS); 6318 Result->takeName(&OrigI); 6319 Overflow = ConstantInt::getTrue(OverflowTy); 6320 return true; 6321 case OverflowResult::NeverOverflows: 6322 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS); 6323 Result->takeName(&OrigI); 6324 Overflow = ConstantInt::getFalse(OverflowTy); 6325 if (auto *Inst = dyn_cast<Instruction>(Result)) { 6326 if (IsSigned) 6327 Inst->setHasNoSignedWrap(); 6328 else 6329 Inst->setHasNoUnsignedWrap(); 6330 } 6331 return true; 6332 } 6333 6334 llvm_unreachable("Unexpected overflow result"); 6335 } 6336 6337 /// Recognize and process idiom involving test for multiplication 6338 /// overflow. 6339 /// 6340 /// The caller has matched a pattern of the form: 6341 /// I = cmp u (mul(zext A, zext B), V 6342 /// The function checks if this is a test for overflow and if so replaces 6343 /// multiplication with call to 'mul.with.overflow' intrinsic. 6344 /// 6345 /// \param I Compare instruction. 6346 /// \param MulVal Result of 'mult' instruction. It is one of the arguments of 6347 /// the compare instruction. Must be of integer type. 6348 /// \param OtherVal The other argument of compare instruction. 6349 /// \returns Instruction which must replace the compare instruction, NULL if no 6350 /// replacement required. 6351 static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal, 6352 const APInt *OtherVal, 6353 InstCombinerImpl &IC) { 6354 // Don't bother doing this transformation for pointers, don't do it for 6355 // vectors. 6356 if (!isa<IntegerType>(MulVal->getType())) 6357 return nullptr; 6358 6359 auto *MulInstr = dyn_cast<Instruction>(MulVal); 6360 if (!MulInstr) 6361 return nullptr; 6362 assert(MulInstr->getOpcode() == Instruction::Mul); 6363 6364 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)), 6365 *RHS = cast<ZExtInst>(MulInstr->getOperand(1)); 6366 assert(LHS->getOpcode() == Instruction::ZExt); 6367 assert(RHS->getOpcode() == Instruction::ZExt); 6368 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0); 6369 6370 // Calculate type and width of the result produced by mul.with.overflow. 6371 Type *TyA = A->getType(), *TyB = B->getType(); 6372 unsigned WidthA = TyA->getPrimitiveSizeInBits(), 6373 WidthB = TyB->getPrimitiveSizeInBits(); 6374 unsigned MulWidth; 6375 Type *MulType; 6376 if (WidthB > WidthA) { 6377 MulWidth = WidthB; 6378 MulType = TyB; 6379 } else { 6380 MulWidth = WidthA; 6381 MulType = TyA; 6382 } 6383 6384 // In order to replace the original mul with a narrower mul.with.overflow, 6385 // all uses must ignore upper bits of the product. The number of used low 6386 // bits must be not greater than the width of mul.with.overflow. 6387 if (MulVal->hasNUsesOrMore(2)) 6388 for (User *U : MulVal->users()) { 6389 if (U == &I) 6390 continue; 6391 if (TruncInst *TI = dyn_cast<TruncInst>(U)) { 6392 // Check if truncation ignores bits above MulWidth. 6393 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits(); 6394 if (TruncWidth > MulWidth) 6395 return nullptr; 6396 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) { 6397 // Check if AND ignores bits above MulWidth. 6398 if (BO->getOpcode() != Instruction::And) 6399 return nullptr; 6400 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) { 6401 const APInt &CVal = CI->getValue(); 6402 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth) 6403 return nullptr; 6404 } else { 6405 // In this case we could have the operand of the binary operation 6406 // being defined in another block, and performing the replacement 6407 // could break the dominance relation. 6408 return nullptr; 6409 } 6410 } else { 6411 // Other uses prohibit this transformation. 6412 return nullptr; 6413 } 6414 } 6415 6416 // Recognize patterns 6417 switch (I.getPredicate()) { 6418 case ICmpInst::ICMP_UGT: { 6419 // Recognize pattern: 6420 // mulval = mul(zext A, zext B) 6421 // cmp ugt mulval, max 6422 APInt MaxVal = APInt::getMaxValue(MulWidth); 6423 MaxVal = MaxVal.zext(OtherVal->getBitWidth()); 6424 if (MaxVal.eq(*OtherVal)) 6425 break; // Recognized 6426 return nullptr; 6427 } 6428 6429 case ICmpInst::ICMP_ULT: { 6430 // Recognize pattern: 6431 // mulval = mul(zext A, zext B) 6432 // cmp ule mulval, max + 1 6433 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth); 6434 if (MaxVal.eq(*OtherVal)) 6435 break; // Recognized 6436 return nullptr; 6437 } 6438 6439 default: 6440 return nullptr; 6441 } 6442 6443 InstCombiner::BuilderTy &Builder = IC.Builder; 6444 Builder.SetInsertPoint(MulInstr); 6445 6446 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B) 6447 Value *MulA = A, *MulB = B; 6448 if (WidthA < MulWidth) 6449 MulA = Builder.CreateZExt(A, MulType); 6450 if (WidthB < MulWidth) 6451 MulB = Builder.CreateZExt(B, MulType); 6452 CallInst *Call = 6453 Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, MulType, 6454 {MulA, MulB}, /*FMFSource=*/nullptr, "umul"); 6455 IC.addToWorklist(MulInstr); 6456 6457 // If there are uses of mul result other than the comparison, we know that 6458 // they are truncation or binary AND. Change them to use result of 6459 // mul.with.overflow and adjust properly mask/size. 6460 if (MulVal->hasNUsesOrMore(2)) { 6461 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value"); 6462 for (User *U : make_early_inc_range(MulVal->users())) { 6463 if (U == &I) 6464 continue; 6465 if (TruncInst *TI = dyn_cast<TruncInst>(U)) { 6466 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth) 6467 IC.replaceInstUsesWith(*TI, Mul); 6468 else 6469 TI->setOperand(0, Mul); 6470 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) { 6471 assert(BO->getOpcode() == Instruction::And); 6472 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask) 6473 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1)); 6474 APInt ShortMask = CI->getValue().trunc(MulWidth); 6475 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask); 6476 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType()); 6477 IC.replaceInstUsesWith(*BO, Zext); 6478 } else { 6479 llvm_unreachable("Unexpected Binary operation"); 6480 } 6481 IC.addToWorklist(cast<Instruction>(U)); 6482 } 6483 } 6484 6485 // The original icmp gets replaced with the overflow value, maybe inverted 6486 // depending on predicate. 6487 if (I.getPredicate() == ICmpInst::ICMP_ULT) { 6488 Value *Res = Builder.CreateExtractValue(Call, 1); 6489 return BinaryOperator::CreateNot(Res); 6490 } 6491 6492 return ExtractValueInst::Create(Call, 1); 6493 } 6494 6495 /// When performing a comparison against a constant, it is possible that not all 6496 /// the bits in the LHS are demanded. This helper method computes the mask that 6497 /// IS demanded. 6498 static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth) { 6499 const APInt *RHS; 6500 if (!match(I.getOperand(1), m_APInt(RHS))) 6501 return APInt::getAllOnes(BitWidth); 6502 6503 // If this is a normal comparison, it demands all bits. If it is a sign bit 6504 // comparison, it only demands the sign bit. 6505 bool UnusedBit; 6506 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit)) 6507 return APInt::getSignMask(BitWidth); 6508 6509 switch (I.getPredicate()) { 6510 // For a UGT comparison, we don't care about any bits that 6511 // correspond to the trailing ones of the comparand. The value of these 6512 // bits doesn't impact the outcome of the comparison, because any value 6513 // greater than the RHS must differ in a bit higher than these due to carry. 6514 case ICmpInst::ICMP_UGT: 6515 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one()); 6516 6517 // Similarly, for a ULT comparison, we don't care about the trailing zeros. 6518 // Any value less than the RHS must differ in a higher bit because of carries. 6519 case ICmpInst::ICMP_ULT: 6520 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero()); 6521 6522 default: 6523 return APInt::getAllOnes(BitWidth); 6524 } 6525 } 6526 6527 /// Check that one use is in the same block as the definition and all 6528 /// other uses are in blocks dominated by a given block. 6529 /// 6530 /// \param DI Definition 6531 /// \param UI Use 6532 /// \param DB Block that must dominate all uses of \p DI outside 6533 /// the parent block 6534 /// \return true when \p UI is the only use of \p DI in the parent block 6535 /// and all other uses of \p DI are in blocks dominated by \p DB. 6536 /// 6537 bool InstCombinerImpl::dominatesAllUses(const Instruction *DI, 6538 const Instruction *UI, 6539 const BasicBlock *DB) const { 6540 assert(DI && UI && "Instruction not defined\n"); 6541 // Ignore incomplete definitions. 6542 if (!DI->getParent()) 6543 return false; 6544 // DI and UI must be in the same block. 6545 if (DI->getParent() != UI->getParent()) 6546 return false; 6547 // Protect from self-referencing blocks. 6548 if (DI->getParent() == DB) 6549 return false; 6550 for (const User *U : DI->users()) { 6551 auto *Usr = cast<Instruction>(U); 6552 if (Usr != UI && !DT.dominates(DB, Usr->getParent())) 6553 return false; 6554 } 6555 return true; 6556 } 6557 6558 /// Return true when the instruction sequence within a block is select-cmp-br. 6559 static bool isChainSelectCmpBranch(const SelectInst *SI) { 6560 const BasicBlock *BB = SI->getParent(); 6561 if (!BB) 6562 return false; 6563 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator()); 6564 if (!BI || BI->getNumSuccessors() != 2) 6565 return false; 6566 auto *IC = dyn_cast<ICmpInst>(BI->getCondition()); 6567 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI)) 6568 return false; 6569 return true; 6570 } 6571 6572 /// True when a select result is replaced by one of its operands 6573 /// in select-icmp sequence. This will eventually result in the elimination 6574 /// of the select. 6575 /// 6576 /// \param SI Select instruction 6577 /// \param Icmp Compare instruction 6578 /// \param SIOpd Operand that replaces the select 6579 /// 6580 /// Notes: 6581 /// - The replacement is global and requires dominator information 6582 /// - The caller is responsible for the actual replacement 6583 /// 6584 /// Example: 6585 /// 6586 /// entry: 6587 /// %4 = select i1 %3, %C* %0, %C* null 6588 /// %5 = icmp eq %C* %4, null 6589 /// br i1 %5, label %9, label %7 6590 /// ... 6591 /// ; <label>:7 ; preds = %entry 6592 /// %8 = getelementptr inbounds %C* %4, i64 0, i32 0 6593 /// ... 6594 /// 6595 /// can be transformed to 6596 /// 6597 /// %5 = icmp eq %C* %0, null 6598 /// %6 = select i1 %3, i1 %5, i1 true 6599 /// br i1 %6, label %9, label %7 6600 /// ... 6601 /// ; <label>:7 ; preds = %entry 6602 /// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0! 6603 /// 6604 /// Similar when the first operand of the select is a constant or/and 6605 /// the compare is for not equal rather than equal. 6606 /// 6607 /// NOTE: The function is only called when the select and compare constants 6608 /// are equal, the optimization can work only for EQ predicates. This is not a 6609 /// major restriction since a NE compare should be 'normalized' to an equal 6610 /// compare, which usually happens in the combiner and test case 6611 /// select-cmp-br.ll checks for it. 6612 bool InstCombinerImpl::replacedSelectWithOperand(SelectInst *SI, 6613 const ICmpInst *Icmp, 6614 const unsigned SIOpd) { 6615 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!"); 6616 if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) { 6617 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1); 6618 // The check for the single predecessor is not the best that can be 6619 // done. But it protects efficiently against cases like when SI's 6620 // home block has two successors, Succ and Succ1, and Succ1 predecessor 6621 // of Succ. Then SI can't be replaced by SIOpd because the use that gets 6622 // replaced can be reached on either path. So the uniqueness check 6623 // guarantees that the path all uses of SI (outside SI's parent) are on 6624 // is disjoint from all other paths out of SI. But that information 6625 // is more expensive to compute, and the trade-off here is in favor 6626 // of compile-time. It should also be noticed that we check for a single 6627 // predecessor and not only uniqueness. This to handle the situation when 6628 // Succ and Succ1 points to the same basic block. 6629 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) { 6630 NumSel++; 6631 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent()); 6632 return true; 6633 } 6634 } 6635 return false; 6636 } 6637 6638 /// Try to fold the comparison based on range information we can get by checking 6639 /// whether bits are known to be zero or one in the inputs. 6640 Instruction *InstCombinerImpl::foldICmpUsingKnownBits(ICmpInst &I) { 6641 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 6642 Type *Ty = Op0->getType(); 6643 ICmpInst::Predicate Pred = I.getPredicate(); 6644 6645 // Get scalar or pointer size. 6646 unsigned BitWidth = Ty->isIntOrIntVectorTy() 6647 ? Ty->getScalarSizeInBits() 6648 : DL.getPointerTypeSizeInBits(Ty->getScalarType()); 6649 6650 if (!BitWidth) 6651 return nullptr; 6652 6653 KnownBits Op0Known(BitWidth); 6654 KnownBits Op1Known(BitWidth); 6655 6656 { 6657 // Don't use dominating conditions when folding icmp using known bits. This 6658 // may convert signed into unsigned predicates in ways that other passes 6659 // (especially IndVarSimplify) may not be able to reliably undo. 6660 SimplifyQuery Q = SQ.getWithoutDomCondCache().getWithInstruction(&I); 6661 if (SimplifyDemandedBits(&I, 0, getDemandedBitsLHSMask(I, BitWidth), 6662 Op0Known, /*Depth=*/0, Q)) 6663 return &I; 6664 6665 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnes(BitWidth), Op1Known, 6666 /*Depth=*/0, Q)) 6667 return &I; 6668 } 6669 6670 if (!isa<Constant>(Op0) && Op0Known.isConstant()) 6671 return new ICmpInst( 6672 Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1); 6673 if (!isa<Constant>(Op1) && Op1Known.isConstant()) 6674 return new ICmpInst( 6675 Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Known.getConstant())); 6676 6677 if (std::optional<bool> Res = ICmpInst::compare(Op0Known, Op1Known, Pred)) 6678 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *Res)); 6679 6680 // Given the known and unknown bits, compute a range that the LHS could be 6681 // in. Compute the Min, Max and RHS values based on the known bits. For the 6682 // EQ and NE we use unsigned values. 6683 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0); 6684 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0); 6685 if (I.isSigned()) { 6686 Op0Min = Op0Known.getSignedMinValue(); 6687 Op0Max = Op0Known.getSignedMaxValue(); 6688 Op1Min = Op1Known.getSignedMinValue(); 6689 Op1Max = Op1Known.getSignedMaxValue(); 6690 } else { 6691 Op0Min = Op0Known.getMinValue(); 6692 Op0Max = Op0Known.getMaxValue(); 6693 Op1Min = Op1Known.getMinValue(); 6694 Op1Max = Op1Known.getMaxValue(); 6695 } 6696 6697 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a 6698 // min/max canonical compare with some other compare. That could lead to 6699 // conflict with select canonicalization and infinite looping. 6700 // FIXME: This constraint may go away if min/max intrinsics are canonical. 6701 auto isMinMaxCmp = [&](Instruction &Cmp) { 6702 if (!Cmp.hasOneUse()) 6703 return false; 6704 Value *A, *B; 6705 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor; 6706 if (!SelectPatternResult::isMinOrMax(SPF)) 6707 return false; 6708 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) || 6709 match(Op1, m_MaxOrMin(m_Value(), m_Value())); 6710 }; 6711 if (!isMinMaxCmp(I)) { 6712 switch (Pred) { 6713 default: 6714 break; 6715 case ICmpInst::ICMP_ULT: { 6716 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B) 6717 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); 6718 const APInt *CmpC; 6719 if (match(Op1, m_APInt(CmpC))) { 6720 // A <u C -> A == C-1 if min(A)+1 == C 6721 if (*CmpC == Op0Min + 1) 6722 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, 6723 ConstantInt::get(Op1->getType(), *CmpC - 1)); 6724 // X <u C --> X == 0, if the number of zero bits in the bottom of X 6725 // exceeds the log2 of C. 6726 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2()) 6727 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, 6728 Constant::getNullValue(Op1->getType())); 6729 } 6730 break; 6731 } 6732 case ICmpInst::ICMP_UGT: { 6733 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B) 6734 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); 6735 const APInt *CmpC; 6736 if (match(Op1, m_APInt(CmpC))) { 6737 // A >u C -> A == C+1 if max(a)-1 == C 6738 if (*CmpC == Op0Max - 1) 6739 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, 6740 ConstantInt::get(Op1->getType(), *CmpC + 1)); 6741 // X >u C --> X != 0, if the number of zero bits in the bottom of X 6742 // exceeds the log2 of C. 6743 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits()) 6744 return new ICmpInst(ICmpInst::ICMP_NE, Op0, 6745 Constant::getNullValue(Op1->getType())); 6746 } 6747 break; 6748 } 6749 case ICmpInst::ICMP_SLT: { 6750 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B) 6751 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); 6752 const APInt *CmpC; 6753 if (match(Op1, m_APInt(CmpC))) { 6754 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C 6755 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, 6756 ConstantInt::get(Op1->getType(), *CmpC - 1)); 6757 } 6758 break; 6759 } 6760 case ICmpInst::ICMP_SGT: { 6761 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B) 6762 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); 6763 const APInt *CmpC; 6764 if (match(Op1, m_APInt(CmpC))) { 6765 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C 6766 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, 6767 ConstantInt::get(Op1->getType(), *CmpC + 1)); 6768 } 6769 break; 6770 } 6771 } 6772 } 6773 6774 // Based on the range information we know about the LHS, see if we can 6775 // simplify this comparison. For example, (x&4) < 8 is always true. 6776 switch (Pred) { 6777 default: 6778 break; 6779 case ICmpInst::ICMP_EQ: 6780 case ICmpInst::ICMP_NE: { 6781 // If all bits are known zero except for one, then we know at most one bit 6782 // is set. If the comparison is against zero, then this is a check to see if 6783 // *that* bit is set. 6784 APInt Op0KnownZeroInverted = ~Op0Known.Zero; 6785 if (Op1Known.isZero()) { 6786 // If the LHS is an AND with the same constant, look through it. 6787 Value *LHS = nullptr; 6788 const APInt *LHSC; 6789 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) || 6790 *LHSC != Op0KnownZeroInverted) 6791 LHS = Op0; 6792 6793 Value *X; 6794 const APInt *C1; 6795 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) { 6796 Type *XTy = X->getType(); 6797 unsigned Log2C1 = C1->countr_zero(); 6798 APInt C2 = Op0KnownZeroInverted; 6799 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1; 6800 if (C2Pow2.isPowerOf2()) { 6801 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2): 6802 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1)) 6803 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1)) 6804 unsigned Log2C2 = C2Pow2.countr_zero(); 6805 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1); 6806 auto NewPred = 6807 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT; 6808 return new ICmpInst(NewPred, X, CmpC); 6809 } 6810 } 6811 } 6812 6813 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero. 6814 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() && 6815 (Op0Known & Op1Known) == Op0Known) 6816 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0, 6817 ConstantInt::getNullValue(Op1->getType())); 6818 break; 6819 } 6820 case ICmpInst::ICMP_SGE: 6821 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B) 6822 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); 6823 break; 6824 case ICmpInst::ICMP_SLE: 6825 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B) 6826 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); 6827 break; 6828 case ICmpInst::ICMP_UGE: 6829 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B) 6830 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); 6831 break; 6832 case ICmpInst::ICMP_ULE: 6833 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B) 6834 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); 6835 break; 6836 } 6837 6838 // Turn a signed comparison into an unsigned one if both operands are known to 6839 // have the same sign. Set samesign if possible (except for equality 6840 // predicates). 6841 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) && 6842 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) || 6843 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) { 6844 I.setPredicate(I.getUnsignedPredicate()); 6845 I.setSameSign(); 6846 return &I; 6847 } 6848 6849 return nullptr; 6850 } 6851 6852 /// If one operand of an icmp is effectively a bool (value range of {0,1}), 6853 /// then try to reduce patterns based on that limit. 6854 Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) { 6855 Value *X, *Y; 6856 CmpPredicate Pred; 6857 6858 // X must be 0 and bool must be true for "ULT": 6859 // X <u (zext i1 Y) --> (X == 0) & Y 6860 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) && 6861 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT) 6862 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y); 6863 6864 // X must be 0 or bool must be true for "ULE": 6865 // X <=u (sext i1 Y) --> (X == 0) | Y 6866 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) && 6867 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE) 6868 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y); 6869 6870 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C)) 6871 CmpPredicate Pred1, Pred2; 6872 const APInt *C; 6873 Instruction *ExtI; 6874 if (match(&I, m_c_ICmp(Pred1, m_Value(X), 6875 m_CombineAnd(m_Instruction(ExtI), 6876 m_ZExtOrSExt(m_ICmp(Pred2, m_Deferred(X), 6877 m_APInt(C)))))) && 6878 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) { 6879 bool IsSExt = ExtI->getOpcode() == Instruction::SExt; 6880 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse(); 6881 auto CreateRangeCheck = [&] { 6882 Value *CmpV1 = 6883 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType())); 6884 Value *CmpV2 = Builder.CreateICmp( 6885 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1)); 6886 return BinaryOperator::Create( 6887 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And, 6888 CmpV1, CmpV2); 6889 }; 6890 if (C->isZero()) { 6891 if (Pred2 == ICmpInst::ICMP_EQ) { 6892 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false 6893 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true 6894 return replaceInstUsesWith( 6895 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE)); 6896 } else if (!IsSExt || HasOneUse) { 6897 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1 6898 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1 6899 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1 6900 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X == -1 6901 return CreateRangeCheck(); 6902 } 6903 } else if (IsSExt ? C->isAllOnes() : C->isOne()) { 6904 if (Pred2 == ICmpInst::ICMP_NE) { 6905 // icmp eq X, (zext (icmp ne X, 1)) --> false 6906 // icmp ne X, (zext (icmp ne X, 1)) --> true 6907 // icmp eq X, (sext (icmp ne X, -1)) --> false 6908 // icmp ne X, (sext (icmp ne X, -1)) --> true 6909 return replaceInstUsesWith( 6910 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE)); 6911 } else if (!IsSExt || HasOneUse) { 6912 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1 6913 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1 6914 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1 6915 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1 6916 return CreateRangeCheck(); 6917 } 6918 } else { 6919 // when C != 0 && C != 1: 6920 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0 6921 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1 6922 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0 6923 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1 6924 // when C != 0 && C != -1: 6925 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0 6926 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1 6927 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0 6928 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1 6929 return ICmpInst::Create( 6930 Instruction::ICmp, Pred1, X, 6931 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE 6932 ? (IsSExt ? -1 : 1) 6933 : 0)); 6934 } 6935 } 6936 6937 return nullptr; 6938 } 6939 6940 /// If we have an icmp le or icmp ge instruction with a constant operand, turn 6941 /// it into the appropriate icmp lt or icmp gt instruction. This transform 6942 /// allows them to be folded in visitICmpInst. 6943 static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) { 6944 ICmpInst::Predicate Pred = I.getPredicate(); 6945 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) || 6946 InstCombiner::isCanonicalPredicate(Pred)) 6947 return nullptr; 6948 6949 Value *Op0 = I.getOperand(0); 6950 Value *Op1 = I.getOperand(1); 6951 auto *Op1C = dyn_cast<Constant>(Op1); 6952 if (!Op1C) 6953 return nullptr; 6954 6955 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C); 6956 if (!FlippedStrictness) 6957 return nullptr; 6958 6959 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second); 6960 } 6961 6962 /// If we have a comparison with a non-canonical predicate, if we can update 6963 /// all the users, invert the predicate and adjust all the users. 6964 CmpInst *InstCombinerImpl::canonicalizeICmpPredicate(CmpInst &I) { 6965 // Is the predicate already canonical? 6966 CmpInst::Predicate Pred = I.getPredicate(); 6967 if (InstCombiner::isCanonicalPredicate(Pred)) 6968 return nullptr; 6969 6970 // Can all users be adjusted to predicate inversion? 6971 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr)) 6972 return nullptr; 6973 6974 // Ok, we can canonicalize comparison! 6975 // Let's first invert the comparison's predicate. 6976 I.setPredicate(CmpInst::getInversePredicate(Pred)); 6977 I.setName(I.getName() + ".not"); 6978 6979 // And, adapt users. 6980 freelyInvertAllUsersOf(&I); 6981 6982 return &I; 6983 } 6984 6985 /// Integer compare with boolean values can always be turned into bitwise ops. 6986 static Instruction *canonicalizeICmpBool(ICmpInst &I, 6987 InstCombiner::BuilderTy &Builder) { 6988 Value *A = I.getOperand(0), *B = I.getOperand(1); 6989 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only"); 6990 6991 // A boolean compared to true/false can be simplified to Op0/true/false in 6992 // 14 out of the 20 (10 predicates * 2 constants) possible combinations. 6993 // Cases not handled by InstSimplify are always 'not' of Op0. 6994 if (match(B, m_Zero())) { 6995 switch (I.getPredicate()) { 6996 case CmpInst::ICMP_EQ: // A == 0 -> !A 6997 case CmpInst::ICMP_ULE: // A <=u 0 -> !A 6998 case CmpInst::ICMP_SGE: // A >=s 0 -> !A 6999 return BinaryOperator::CreateNot(A); 7000 default: 7001 llvm_unreachable("ICmp i1 X, C not simplified as expected."); 7002 } 7003 } else if (match(B, m_One())) { 7004 switch (I.getPredicate()) { 7005 case CmpInst::ICMP_NE: // A != 1 -> !A 7006 case CmpInst::ICMP_ULT: // A <u 1 -> !A 7007 case CmpInst::ICMP_SGT: // A >s -1 -> !A 7008 return BinaryOperator::CreateNot(A); 7009 default: 7010 llvm_unreachable("ICmp i1 X, C not simplified as expected."); 7011 } 7012 } 7013 7014 switch (I.getPredicate()) { 7015 default: 7016 llvm_unreachable("Invalid icmp instruction!"); 7017 case ICmpInst::ICMP_EQ: 7018 // icmp eq i1 A, B -> ~(A ^ B) 7019 return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); 7020 7021 case ICmpInst::ICMP_NE: 7022 // icmp ne i1 A, B -> A ^ B 7023 return BinaryOperator::CreateXor(A, B); 7024 7025 case ICmpInst::ICMP_UGT: 7026 // icmp ugt -> icmp ult 7027 std::swap(A, B); 7028 [[fallthrough]]; 7029 case ICmpInst::ICMP_ULT: 7030 // icmp ult i1 A, B -> ~A & B 7031 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B); 7032 7033 case ICmpInst::ICMP_SGT: 7034 // icmp sgt -> icmp slt 7035 std::swap(A, B); 7036 [[fallthrough]]; 7037 case ICmpInst::ICMP_SLT: 7038 // icmp slt i1 A, B -> A & ~B 7039 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A); 7040 7041 case ICmpInst::ICMP_UGE: 7042 // icmp uge -> icmp ule 7043 std::swap(A, B); 7044 [[fallthrough]]; 7045 case ICmpInst::ICMP_ULE: 7046 // icmp ule i1 A, B -> ~A | B 7047 return BinaryOperator::CreateOr(Builder.CreateNot(A), B); 7048 7049 case ICmpInst::ICMP_SGE: 7050 // icmp sge -> icmp sle 7051 std::swap(A, B); 7052 [[fallthrough]]; 7053 case ICmpInst::ICMP_SLE: 7054 // icmp sle i1 A, B -> A | ~B 7055 return BinaryOperator::CreateOr(Builder.CreateNot(B), A); 7056 } 7057 } 7058 7059 // Transform pattern like: 7060 // (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X 7061 // (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X 7062 // Into: 7063 // (X l>> Y) != 0 7064 // (X l>> Y) == 0 7065 static Instruction *foldICmpWithHighBitMask(ICmpInst &Cmp, 7066 InstCombiner::BuilderTy &Builder) { 7067 CmpPredicate Pred, NewPred; 7068 Value *X, *Y; 7069 if (match(&Cmp, 7070 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) { 7071 switch (Pred) { 7072 case ICmpInst::ICMP_ULE: 7073 NewPred = ICmpInst::ICMP_NE; 7074 break; 7075 case ICmpInst::ICMP_UGT: 7076 NewPred = ICmpInst::ICMP_EQ; 7077 break; 7078 default: 7079 return nullptr; 7080 } 7081 } else if (match(&Cmp, m_c_ICmp(Pred, 7082 m_OneUse(m_CombineOr( 7083 m_Not(m_Shl(m_AllOnes(), m_Value(Y))), 7084 m_Add(m_Shl(m_One(), m_Value(Y)), 7085 m_AllOnes()))), 7086 m_Value(X)))) { 7087 // The variant with 'add' is not canonical, (the variant with 'not' is) 7088 // we only get it because it has extra uses, and can't be canonicalized, 7089 7090 switch (Pred) { 7091 case ICmpInst::ICMP_ULT: 7092 NewPred = ICmpInst::ICMP_NE; 7093 break; 7094 case ICmpInst::ICMP_UGE: 7095 NewPred = ICmpInst::ICMP_EQ; 7096 break; 7097 default: 7098 return nullptr; 7099 } 7100 } else 7101 return nullptr; 7102 7103 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits"); 7104 Constant *Zero = Constant::getNullValue(NewX->getType()); 7105 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero); 7106 } 7107 7108 static Instruction *foldVectorCmp(CmpInst &Cmp, 7109 InstCombiner::BuilderTy &Builder) { 7110 const CmpInst::Predicate Pred = Cmp.getPredicate(); 7111 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1); 7112 Value *V1, *V2; 7113 7114 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) { 7115 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName()); 7116 if (auto *I = dyn_cast<Instruction>(V)) 7117 I->copyIRFlags(&Cmp); 7118 Module *M = Cmp.getModule(); 7119 Function *F = Intrinsic::getOrInsertDeclaration( 7120 M, Intrinsic::vector_reverse, V->getType()); 7121 return CallInst::Create(F, V); 7122 }; 7123 7124 if (match(LHS, m_VecReverse(m_Value(V1)))) { 7125 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2) 7126 if (match(RHS, m_VecReverse(m_Value(V2))) && 7127 (LHS->hasOneUse() || RHS->hasOneUse())) 7128 return createCmpReverse(Pred, V1, V2); 7129 7130 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat) 7131 if (LHS->hasOneUse() && isSplatValue(RHS)) 7132 return createCmpReverse(Pred, V1, RHS); 7133 } 7134 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2) 7135 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2))))) 7136 return createCmpReverse(Pred, LHS, V2); 7137 7138 ArrayRef<int> M; 7139 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M)))) 7140 return nullptr; 7141 7142 // If both arguments of the cmp are shuffles that use the same mask and 7143 // shuffle within a single vector, move the shuffle after the cmp: 7144 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M 7145 Type *V1Ty = V1->getType(); 7146 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) && 7147 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) { 7148 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2); 7149 return new ShuffleVectorInst(NewCmp, M); 7150 } 7151 7152 // Try to canonicalize compare with splatted operand and splat constant. 7153 // TODO: We could generalize this for more than splats. See/use the code in 7154 // InstCombiner::foldVectorBinop(). 7155 Constant *C; 7156 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C))) 7157 return nullptr; 7158 7159 // Length-changing splats are ok, so adjust the constants as needed: 7160 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M 7161 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true); 7162 int MaskSplatIndex; 7163 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) { 7164 // We allow poison in matching, but this transform removes it for safety. 7165 // Demanded elements analysis should be able to recover some/all of that. 7166 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(), 7167 ScalarC); 7168 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex); 7169 Value *NewCmp = Builder.CreateCmp(Pred, V1, C); 7170 return new ShuffleVectorInst(NewCmp, NewM); 7171 } 7172 7173 return nullptr; 7174 } 7175 7176 // extract(uadd.with.overflow(A, B), 0) ult A 7177 // -> extract(uadd.with.overflow(A, B), 1) 7178 static Instruction *foldICmpOfUAddOv(ICmpInst &I) { 7179 CmpInst::Predicate Pred = I.getPredicate(); 7180 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 7181 7182 Value *UAddOv; 7183 Value *A, *B; 7184 auto UAddOvResultPat = m_ExtractValue<0>( 7185 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B))); 7186 if (match(Op0, UAddOvResultPat) && 7187 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) || 7188 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) && 7189 (match(A, m_One()) || match(B, m_One()))) || 7190 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) && 7191 (match(A, m_AllOnes()) || match(B, m_AllOnes()))))) 7192 // extract(uadd.with.overflow(A, B), 0) < A 7193 // extract(uadd.with.overflow(A, 1), 0) == 0 7194 // extract(uadd.with.overflow(A, -1), 0) != -1 7195 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand(); 7196 else if (match(Op1, UAddOvResultPat) && 7197 Pred == ICmpInst::ICMP_UGT && (Op0 == A || Op0 == B)) 7198 // A > extract(uadd.with.overflow(A, B), 0) 7199 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand(); 7200 else 7201 return nullptr; 7202 7203 return ExtractValueInst::Create(UAddOv, 1); 7204 } 7205 7206 static Instruction *foldICmpInvariantGroup(ICmpInst &I) { 7207 if (!I.getOperand(0)->getType()->isPointerTy() || 7208 NullPointerIsDefined( 7209 I.getParent()->getParent(), 7210 I.getOperand(0)->getType()->getPointerAddressSpace())) { 7211 return nullptr; 7212 } 7213 Instruction *Op; 7214 if (match(I.getOperand(0), m_Instruction(Op)) && 7215 match(I.getOperand(1), m_Zero()) && 7216 Op->isLaunderOrStripInvariantGroup()) { 7217 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(), 7218 Op->getOperand(0), I.getOperand(1)); 7219 } 7220 return nullptr; 7221 } 7222 7223 /// This function folds patterns produced by lowering of reduce idioms, such as 7224 /// llvm.vector.reduce.and which are lowered into instruction chains. This code 7225 /// attempts to generate fewer number of scalar comparisons instead of vector 7226 /// comparisons when possible. 7227 static Instruction *foldReductionIdiom(ICmpInst &I, 7228 InstCombiner::BuilderTy &Builder, 7229 const DataLayout &DL) { 7230 if (I.getType()->isVectorTy()) 7231 return nullptr; 7232 CmpPredicate OuterPred, InnerPred; 7233 Value *LHS, *RHS; 7234 7235 // Match lowering of @llvm.vector.reduce.and. Turn 7236 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs 7237 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8 7238 /// %res = icmp <pred> i8 %scalar_ne, 0 7239 /// 7240 /// into 7241 /// 7242 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64 7243 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64 7244 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar 7245 /// 7246 /// for <pred> in {ne, eq}. 7247 if (!match(&I, m_ICmp(OuterPred, 7248 m_OneUse(m_BitCast(m_OneUse( 7249 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))), 7250 m_Zero()))) 7251 return nullptr; 7252 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType()); 7253 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy()) 7254 return nullptr; 7255 unsigned NumBits = 7256 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth(); 7257 // TODO: Relax this to "not wider than max legal integer type"? 7258 if (!DL.isLegalInteger(NumBits)) 7259 return nullptr; 7260 7261 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) { 7262 auto *ScalarTy = Builder.getIntNTy(NumBits); 7263 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar"); 7264 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar"); 7265 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS, 7266 I.getName()); 7267 } 7268 7269 return nullptr; 7270 } 7271 7272 // This helper will be called with icmp operands in both orders. 7273 Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred, 7274 Value *Op0, Value *Op1, 7275 ICmpInst &CxtI) { 7276 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'. 7277 if (auto *GEP = dyn_cast<GEPOperator>(Op0)) 7278 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI)) 7279 return NI; 7280 7281 if (auto *SI = dyn_cast<SelectInst>(Op0)) 7282 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI)) 7283 return NI; 7284 7285 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0)) 7286 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred)) 7287 return Res; 7288 7289 { 7290 Value *X; 7291 const APInt *C; 7292 // icmp X+Cst, X 7293 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X) 7294 return foldICmpAddOpConst(X, *C, Pred); 7295 } 7296 7297 // abs(X) >= X --> true 7298 // abs(X) u<= X --> true 7299 // abs(X) < X --> false 7300 // abs(X) u> X --> false 7301 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN` 7302 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN` 7303 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN` 7304 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN` 7305 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN` 7306 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN` 7307 { 7308 Value *X; 7309 Constant *C; 7310 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) && 7311 match(Op1, m_Specific(X))) { 7312 Value *NullValue = Constant::getNullValue(X->getType()); 7313 Value *AllOnesValue = Constant::getAllOnesValue(X->getType()); 7314 const APInt SMin = 7315 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()); 7316 bool IsIntMinPosion = C->isAllOnesValue(); 7317 switch (Pred) { 7318 case CmpInst::ICMP_ULE: 7319 case CmpInst::ICMP_SGE: 7320 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType())); 7321 case CmpInst::ICMP_UGT: 7322 case CmpInst::ICMP_SLT: 7323 return replaceInstUsesWith(CxtI, ConstantInt::getFalse(CxtI.getType())); 7324 case CmpInst::ICMP_UGE: 7325 case CmpInst::ICMP_SLE: 7326 case CmpInst::ICMP_EQ: { 7327 return replaceInstUsesWith( 7328 CxtI, IsIntMinPosion 7329 ? Builder.CreateICmpSGT(X, AllOnesValue) 7330 : Builder.CreateICmpULT( 7331 X, ConstantInt::get(X->getType(), SMin + 1))); 7332 } 7333 case CmpInst::ICMP_ULT: 7334 case CmpInst::ICMP_SGT: 7335 case CmpInst::ICMP_NE: { 7336 return replaceInstUsesWith( 7337 CxtI, IsIntMinPosion 7338 ? Builder.CreateICmpSLT(X, NullValue) 7339 : Builder.CreateICmpUGT( 7340 X, ConstantInt::get(X->getType(), SMin))); 7341 } 7342 default: 7343 llvm_unreachable("Invalid predicate!"); 7344 } 7345 } 7346 } 7347 7348 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI); 7349 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this)) 7350 return replaceInstUsesWith(CxtI, V); 7351 7352 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1 7353 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); }; 7354 { 7355 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) { 7356 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1, 7357 Constant::getNullValue(Op1->getType())); 7358 } 7359 7360 if (!ICmpInst::isUnsigned(Pred) && 7361 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) { 7362 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1, 7363 Constant::getNullValue(Op1->getType())); 7364 } 7365 } 7366 7367 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0 7368 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); }; 7369 { 7370 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) { 7371 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1, 7372 Constant::getNullValue(Op1->getType())); 7373 } 7374 7375 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) && 7376 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) { 7377 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1, 7378 Constant::getNullValue(Op1->getType())); 7379 } 7380 } 7381 7382 return nullptr; 7383 } 7384 7385 Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) { 7386 bool Changed = false; 7387 const SimplifyQuery Q = SQ.getWithInstruction(&I); 7388 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 7389 unsigned Op0Cplxity = getComplexity(Op0); 7390 unsigned Op1Cplxity = getComplexity(Op1); 7391 7392 /// Orders the operands of the compare so that they are listed from most 7393 /// complex to least complex. This puts constants before unary operators, 7394 /// before binary operators. 7395 if (Op0Cplxity < Op1Cplxity) { 7396 I.swapOperands(); 7397 std::swap(Op0, Op1); 7398 Changed = true; 7399 } 7400 7401 if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q)) 7402 return replaceInstUsesWith(I, V); 7403 7404 // Comparing -val or val with non-zero is the same as just comparing val 7405 // ie, abs(val) != 0 -> val != 0 7406 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) { 7407 Value *Cond, *SelectTrue, *SelectFalse; 7408 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue), 7409 m_Value(SelectFalse)))) { 7410 if (Value *V = dyn_castNegVal(SelectTrue)) { 7411 if (V == SelectFalse) 7412 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); 7413 } 7414 else if (Value *V = dyn_castNegVal(SelectFalse)) { 7415 if (V == SelectTrue) 7416 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); 7417 } 7418 } 7419 } 7420 7421 if (Op0->getType()->isIntOrIntVectorTy(1)) 7422 if (Instruction *Res = canonicalizeICmpBool(I, Builder)) 7423 return Res; 7424 7425 if (Instruction *Res = canonicalizeCmpWithConstant(I)) 7426 return Res; 7427 7428 if (Instruction *Res = canonicalizeICmpPredicate(I)) 7429 return Res; 7430 7431 if (Instruction *Res = foldICmpWithConstant(I)) 7432 return Res; 7433 7434 if (Instruction *Res = foldICmpWithDominatingICmp(I)) 7435 return Res; 7436 7437 if (Instruction *Res = foldICmpUsingBoolRange(I)) 7438 return Res; 7439 7440 if (Instruction *Res = foldICmpUsingKnownBits(I)) 7441 return Res; 7442 7443 if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q)) 7444 return Res; 7445 7446 // Test if the ICmpInst instruction is used exclusively by a select as 7447 // part of a minimum or maximum operation. If so, refrain from doing 7448 // any other folding. This helps out other analyses which understand 7449 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution 7450 // and CodeGen. And in this case, at least one of the comparison 7451 // operands has at least one user besides the compare (the select), 7452 // which would often largely negate the benefit of folding anyway. 7453 // 7454 // Do the same for the other patterns recognized by matchSelectPattern. 7455 if (I.hasOneUse()) 7456 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) { 7457 Value *A, *B; 7458 SelectPatternResult SPR = matchSelectPattern(SI, A, B); 7459 if (SPR.Flavor != SPF_UNKNOWN) 7460 return nullptr; 7461 } 7462 7463 // Do this after checking for min/max to prevent infinite looping. 7464 if (Instruction *Res = foldICmpWithZero(I)) 7465 return Res; 7466 7467 // FIXME: We only do this after checking for min/max to prevent infinite 7468 // looping caused by a reverse canonicalization of these patterns for min/max. 7469 // FIXME: The organization of folds is a mess. These would naturally go into 7470 // canonicalizeCmpWithConstant(), but we can't move all of the above folds 7471 // down here after the min/max restriction. 7472 ICmpInst::Predicate Pred = I.getPredicate(); 7473 const APInt *C; 7474 if (match(Op1, m_APInt(C))) { 7475 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set 7476 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) { 7477 Constant *Zero = Constant::getNullValue(Op0->getType()); 7478 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero); 7479 } 7480 7481 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear 7482 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) { 7483 Constant *AllOnes = Constant::getAllOnesValue(Op0->getType()); 7484 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes); 7485 } 7486 } 7487 7488 // The folds in here may rely on wrapping flags and special constants, so 7489 // they can break up min/max idioms in some cases but not seemingly similar 7490 // patterns. 7491 // FIXME: It may be possible to enhance select folding to make this 7492 // unnecessary. It may also be moot if we canonicalize to min/max 7493 // intrinsics. 7494 if (Instruction *Res = foldICmpBinOp(I, Q)) 7495 return Res; 7496 7497 if (Instruction *Res = foldICmpInstWithConstant(I)) 7498 return Res; 7499 7500 // Try to match comparison as a sign bit test. Intentionally do this after 7501 // foldICmpInstWithConstant() to potentially let other folds to happen first. 7502 if (Instruction *New = foldSignBitTest(I)) 7503 return New; 7504 7505 if (Instruction *Res = foldICmpInstWithConstantNotInt(I)) 7506 return Res; 7507 7508 if (Instruction *Res = foldICmpCommutative(I.getCmpPredicate(), Op0, Op1, I)) 7509 return Res; 7510 if (Instruction *Res = 7511 foldICmpCommutative(I.getSwappedCmpPredicate(), Op1, Op0, I)) 7512 return Res; 7513 7514 if (I.isCommutative()) { 7515 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) { 7516 replaceOperand(I, 0, Pair->first); 7517 replaceOperand(I, 1, Pair->second); 7518 return &I; 7519 } 7520 } 7521 7522 // In case of a comparison with two select instructions having the same 7523 // condition, check whether one of the resulting branches can be simplified. 7524 // If so, just compare the other branch and select the appropriate result. 7525 // For example: 7526 // %tmp1 = select i1 %cmp, i32 %y, i32 %x 7527 // %tmp2 = select i1 %cmp, i32 %z, i32 %x 7528 // %cmp2 = icmp slt i32 %tmp2, %tmp1 7529 // The icmp will result false for the false value of selects and the result 7530 // will depend upon the comparison of true values of selects if %cmp is 7531 // true. Thus, transform this into: 7532 // %cmp = icmp slt i32 %y, %z 7533 // %sel = select i1 %cond, i1 %cmp, i1 false 7534 // This handles similar cases to transform. 7535 { 7536 Value *Cond, *A, *B, *C, *D; 7537 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) && 7538 match(Op1, m_Select(m_Specific(Cond), m_Value(C), m_Value(D))) && 7539 (Op0->hasOneUse() || Op1->hasOneUse())) { 7540 // Check whether comparison of TrueValues can be simplified 7541 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) { 7542 Value *NewICMP = Builder.CreateICmp(Pred, B, D); 7543 return SelectInst::Create(Cond, Res, NewICMP); 7544 } 7545 // Check whether comparison of FalseValues can be simplified 7546 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) { 7547 Value *NewICMP = Builder.CreateICmp(Pred, A, C); 7548 return SelectInst::Create(Cond, NewICMP, Res); 7549 } 7550 } 7551 } 7552 7553 // Try to optimize equality comparisons against alloca-based pointers. 7554 if (Op0->getType()->isPointerTy() && I.isEquality()) { 7555 assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?"); 7556 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0))) 7557 if (foldAllocaCmp(Alloca)) 7558 return nullptr; 7559 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1))) 7560 if (foldAllocaCmp(Alloca)) 7561 return nullptr; 7562 } 7563 7564 if (Instruction *Res = foldICmpBitCast(I)) 7565 return Res; 7566 7567 // TODO: Hoist this above the min/max bailout. 7568 if (Instruction *R = foldICmpWithCastOp(I)) 7569 return R; 7570 7571 { 7572 Value *X, *Y; 7573 // Transform (X & ~Y) == 0 --> (X & Y) != 0 7574 // and (X & ~Y) != 0 --> (X & Y) == 0 7575 // if A is a power of 2. 7576 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) && 7577 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, 0, &I) && 7578 I.isEquality()) 7579 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y), 7580 Op1); 7581 7582 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction. 7583 if (Op0->getType()->isIntOrIntVectorTy()) { 7584 bool ConsumesOp0, ConsumesOp1; 7585 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) && 7586 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) && 7587 (ConsumesOp0 || ConsumesOp1)) { 7588 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder); 7589 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder); 7590 assert(InvOp0 && InvOp1 && 7591 "Mismatch between isFreeToInvert and getFreelyInverted"); 7592 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1); 7593 } 7594 } 7595 7596 Instruction *AddI = nullptr; 7597 if (match(&I, m_UAddWithOverflow(m_Value(X), m_Value(Y), 7598 m_Instruction(AddI))) && 7599 isa<IntegerType>(X->getType())) { 7600 Value *Result; 7601 Constant *Overflow; 7602 // m_UAddWithOverflow can match patterns that do not include an explicit 7603 // "add" instruction, so check the opcode of the matched op. 7604 if (AddI->getOpcode() == Instruction::Add && 7605 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI, 7606 Result, Overflow)) { 7607 replaceInstUsesWith(*AddI, Result); 7608 eraseInstFromFunction(*AddI); 7609 return replaceInstUsesWith(I, Overflow); 7610 } 7611 } 7612 7613 // (zext X) * (zext Y) --> llvm.umul.with.overflow. 7614 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) && 7615 match(Op1, m_APInt(C))) { 7616 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this)) 7617 return R; 7618 } 7619 7620 // Signbit test folds 7621 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1 7622 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1 7623 Instruction *ExtI; 7624 if ((I.isUnsigned() || I.isEquality()) && 7625 match(Op1, 7626 m_CombineAnd(m_Instruction(ExtI), m_ZExtOrSExt(m_Value(Y)))) && 7627 Y->getType()->getScalarSizeInBits() == 1 && 7628 (Op0->hasOneUse() || Op1->hasOneUse())) { 7629 unsigned OpWidth = Op0->getType()->getScalarSizeInBits(); 7630 Instruction *ShiftI; 7631 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI), 7632 m_Shr(m_Value(X), m_SpecificIntAllowPoison( 7633 OpWidth - 1))))) { 7634 unsigned ExtOpc = ExtI->getOpcode(); 7635 unsigned ShiftOpc = ShiftI->getOpcode(); 7636 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) || 7637 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) { 7638 Value *SLTZero = 7639 Builder.CreateICmpSLT(X, Constant::getNullValue(X->getType())); 7640 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName()); 7641 return replaceInstUsesWith(I, Cmp); 7642 } 7643 } 7644 } 7645 } 7646 7647 if (Instruction *Res = foldICmpEquality(I)) 7648 return Res; 7649 7650 if (Instruction *Res = foldICmpPow2Test(I, Builder)) 7651 return Res; 7652 7653 if (Instruction *Res = foldICmpOfUAddOv(I)) 7654 return Res; 7655 7656 // The 'cmpxchg' instruction returns an aggregate containing the old value and 7657 // an i1 which indicates whether or not we successfully did the swap. 7658 // 7659 // Replace comparisons between the old value and the expected value with the 7660 // indicator that 'cmpxchg' returns. 7661 // 7662 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to 7663 // spuriously fail. In those cases, the old value may equal the expected 7664 // value but it is possible for the swap to not occur. 7665 if (I.getPredicate() == ICmpInst::ICMP_EQ) 7666 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0)) 7667 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand())) 7668 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 && 7669 !ACXI->isWeak()) 7670 return ExtractValueInst::Create(ACXI, 1); 7671 7672 if (Instruction *Res = foldICmpWithHighBitMask(I, Builder)) 7673 return Res; 7674 7675 if (I.getType()->isVectorTy()) 7676 if (Instruction *Res = foldVectorCmp(I, Builder)) 7677 return Res; 7678 7679 if (Instruction *Res = foldICmpInvariantGroup(I)) 7680 return Res; 7681 7682 if (Instruction *Res = foldReductionIdiom(I, Builder, DL)) 7683 return Res; 7684 7685 { 7686 Value *A; 7687 const APInt *C1, *C2; 7688 ICmpInst::Predicate Pred = I.getPredicate(); 7689 if (ICmpInst::isEquality(Pred)) { 7690 // sext(a) & c1 == c2 --> a & c3 == trunc(c2) 7691 // sext(a) & c1 != c2 --> a & c3 != trunc(c2) 7692 if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) && 7693 match(Op1, m_APInt(C2))) { 7694 Type *InputTy = A->getType(); 7695 unsigned InputBitWidth = InputTy->getScalarSizeInBits(); 7696 // c2 must be non-negative at the bitwidth of a. 7697 if (C2->getActiveBits() < InputBitWidth) { 7698 APInt TruncC1 = C1->trunc(InputBitWidth); 7699 // Check if there are 1s in C1 high bits of size InputBitWidth. 7700 if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth))) 7701 TruncC1.setBit(InputBitWidth - 1); 7702 Value *AndInst = Builder.CreateAnd(A, TruncC1); 7703 return new ICmpInst( 7704 Pred, AndInst, 7705 ConstantInt::get(InputTy, C2->trunc(InputBitWidth))); 7706 } 7707 } 7708 } 7709 } 7710 7711 return Changed ? &I : nullptr; 7712 } 7713 7714 /// Fold fcmp ([us]itofp x, cst) if possible. 7715 Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I, 7716 Instruction *LHSI, 7717 Constant *RHSC) { 7718 const APFloat *RHS; 7719 if (!match(RHSC, m_APFloat(RHS))) 7720 return nullptr; 7721 7722 // Get the width of the mantissa. We don't want to hack on conversions that 7723 // might lose information from the integer, e.g. "i64 -> float" 7724 int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); 7725 if (MantissaWidth == -1) return nullptr; // Unknown. 7726 7727 Type *IntTy = LHSI->getOperand(0)->getType(); 7728 unsigned IntWidth = IntTy->getScalarSizeInBits(); 7729 bool LHSUnsigned = isa<UIToFPInst>(LHSI); 7730 7731 if (I.isEquality()) { 7732 FCmpInst::Predicate P = I.getPredicate(); 7733 bool IsExact = false; 7734 APSInt RHSCvt(IntWidth, LHSUnsigned); 7735 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact); 7736 7737 // If the floating point constant isn't an integer value, we know if we will 7738 // ever compare equal / not equal to it. 7739 if (!IsExact) { 7740 // TODO: Can never be -0.0 and other non-representable values 7741 APFloat RHSRoundInt(*RHS); 7742 RHSRoundInt.roundToIntegral(APFloat::rmNearestTiesToEven); 7743 if (*RHS != RHSRoundInt) { 7744 if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ) 7745 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7746 7747 assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE); 7748 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7749 } 7750 } 7751 7752 // TODO: If the constant is exactly representable, is it always OK to do 7753 // equality compares as integer? 7754 } 7755 7756 // Check to see that the input is converted from an integer type that is small 7757 // enough that preserves all bits. TODO: check here for "known" sign bits. 7758 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. 7759 7760 // Following test does NOT adjust IntWidth downwards for signed inputs, 7761 // because the most negative value still requires all the mantissa bits 7762 // to distinguish it from one less than that value. 7763 if ((int)IntWidth > MantissaWidth) { 7764 // Conversion would lose accuracy. Check if loss can impact comparison. 7765 int Exp = ilogb(*RHS); 7766 if (Exp == APFloat::IEK_Inf) { 7767 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics())); 7768 if (MaxExponent < (int)IntWidth - !LHSUnsigned) 7769 // Conversion could create infinity. 7770 return nullptr; 7771 } else { 7772 // Note that if RHS is zero or NaN, then Exp is negative 7773 // and first condition is trivially false. 7774 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned) 7775 // Conversion could affect comparison. 7776 return nullptr; 7777 } 7778 } 7779 7780 // Otherwise, we can potentially simplify the comparison. We know that it 7781 // will always come through as an integer value and we know the constant is 7782 // not a NAN (it would have been previously simplified). 7783 assert(!RHS->isNaN() && "NaN comparison not already folded!"); 7784 7785 ICmpInst::Predicate Pred; 7786 switch (I.getPredicate()) { 7787 default: llvm_unreachable("Unexpected predicate!"); 7788 case FCmpInst::FCMP_UEQ: 7789 case FCmpInst::FCMP_OEQ: 7790 Pred = ICmpInst::ICMP_EQ; 7791 break; 7792 case FCmpInst::FCMP_UGT: 7793 case FCmpInst::FCMP_OGT: 7794 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT; 7795 break; 7796 case FCmpInst::FCMP_UGE: 7797 case FCmpInst::FCMP_OGE: 7798 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE; 7799 break; 7800 case FCmpInst::FCMP_ULT: 7801 case FCmpInst::FCMP_OLT: 7802 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT; 7803 break; 7804 case FCmpInst::FCMP_ULE: 7805 case FCmpInst::FCMP_OLE: 7806 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE; 7807 break; 7808 case FCmpInst::FCMP_UNE: 7809 case FCmpInst::FCMP_ONE: 7810 Pred = ICmpInst::ICMP_NE; 7811 break; 7812 case FCmpInst::FCMP_ORD: 7813 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7814 case FCmpInst::FCMP_UNO: 7815 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7816 } 7817 7818 // Now we know that the APFloat is a normal number, zero or inf. 7819 7820 // See if the FP constant is too large for the integer. For example, 7821 // comparing an i8 to 300.0. 7822 if (!LHSUnsigned) { 7823 // If the RHS value is > SignedMax, fold the comparison. This handles +INF 7824 // and large values. 7825 APFloat SMax(RHS->getSemantics()); 7826 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, 7827 APFloat::rmNearestTiesToEven); 7828 if (SMax < *RHS) { // smax < 13123.0 7829 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || 7830 Pred == ICmpInst::ICMP_SLE) 7831 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7832 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7833 } 7834 } else { 7835 // If the RHS value is > UnsignedMax, fold the comparison. This handles 7836 // +INF and large values. 7837 APFloat UMax(RHS->getSemantics()); 7838 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false, 7839 APFloat::rmNearestTiesToEven); 7840 if (UMax < *RHS) { // umax < 13123.0 7841 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || 7842 Pred == ICmpInst::ICMP_ULE) 7843 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7844 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7845 } 7846 } 7847 7848 if (!LHSUnsigned) { 7849 // See if the RHS value is < SignedMin. 7850 APFloat SMin(RHS->getSemantics()); 7851 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, 7852 APFloat::rmNearestTiesToEven); 7853 if (SMin > *RHS) { // smin > 12312.0 7854 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || 7855 Pred == ICmpInst::ICMP_SGE) 7856 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7857 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7858 } 7859 } else { 7860 // See if the RHS value is < UnsignedMin. 7861 APFloat UMin(RHS->getSemantics()); 7862 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false, 7863 APFloat::rmNearestTiesToEven); 7864 if (UMin > *RHS) { // umin > 12312.0 7865 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT || 7866 Pred == ICmpInst::ICMP_UGE) 7867 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7868 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7869 } 7870 } 7871 7872 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or 7873 // [0, UMAX], but it may still be fractional. Check whether this is the case 7874 // using the IsExact flag. 7875 // Don't do this for zero, because -0.0 is not fractional. 7876 APSInt RHSInt(IntWidth, LHSUnsigned); 7877 bool IsExact; 7878 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact); 7879 if (!RHS->isZero()) { 7880 if (!IsExact) { 7881 // If we had a comparison against a fractional value, we have to adjust 7882 // the compare predicate and sometimes the value. RHSC is rounded towards 7883 // zero at this point. 7884 switch (Pred) { 7885 default: llvm_unreachable("Unexpected integer comparison!"); 7886 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true 7887 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7888 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false 7889 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7890 case ICmpInst::ICMP_ULE: 7891 // (float)int <= 4.4 --> int <= 4 7892 // (float)int <= -4.4 --> false 7893 if (RHS->isNegative()) 7894 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7895 break; 7896 case ICmpInst::ICMP_SLE: 7897 // (float)int <= 4.4 --> int <= 4 7898 // (float)int <= -4.4 --> int < -4 7899 if (RHS->isNegative()) 7900 Pred = ICmpInst::ICMP_SLT; 7901 break; 7902 case ICmpInst::ICMP_ULT: 7903 // (float)int < -4.4 --> false 7904 // (float)int < 4.4 --> int <= 4 7905 if (RHS->isNegative()) 7906 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 7907 Pred = ICmpInst::ICMP_ULE; 7908 break; 7909 case ICmpInst::ICMP_SLT: 7910 // (float)int < -4.4 --> int < -4 7911 // (float)int < 4.4 --> int <= 4 7912 if (!RHS->isNegative()) 7913 Pred = ICmpInst::ICMP_SLE; 7914 break; 7915 case ICmpInst::ICMP_UGT: 7916 // (float)int > 4.4 --> int > 4 7917 // (float)int > -4.4 --> true 7918 if (RHS->isNegative()) 7919 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7920 break; 7921 case ICmpInst::ICMP_SGT: 7922 // (float)int > 4.4 --> int > 4 7923 // (float)int > -4.4 --> int >= -4 7924 if (RHS->isNegative()) 7925 Pred = ICmpInst::ICMP_SGE; 7926 break; 7927 case ICmpInst::ICMP_UGE: 7928 // (float)int >= -4.4 --> true 7929 // (float)int >= 4.4 --> int > 4 7930 if (RHS->isNegative()) 7931 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 7932 Pred = ICmpInst::ICMP_UGT; 7933 break; 7934 case ICmpInst::ICMP_SGE: 7935 // (float)int >= -4.4 --> int >= -4 7936 // (float)int >= 4.4 --> int > 4 7937 if (!RHS->isNegative()) 7938 Pred = ICmpInst::ICMP_SGT; 7939 break; 7940 } 7941 } 7942 } 7943 7944 // Lower this FP comparison into an appropriate integer version of the 7945 // comparison. 7946 return new ICmpInst(Pred, LHSI->getOperand(0), 7947 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt)); 7948 } 7949 7950 /// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary. 7951 static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI, 7952 Constant *RHSC) { 7953 // When C is not 0.0 and infinities are not allowed: 7954 // (C / X) < 0.0 is a sign-bit test of X 7955 // (C / X) < 0.0 --> X < 0.0 (if C is positive) 7956 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate) 7957 // 7958 // Proof: 7959 // Multiply (C / X) < 0.0 by X * X / C. 7960 // - X is non zero, if it is the flag 'ninf' is violated. 7961 // - C defines the sign of X * X * C. Thus it also defines whether to swap 7962 // the predicate. C is also non zero by definition. 7963 // 7964 // Thus X * X / C is non zero and the transformation is valid. [qed] 7965 7966 FCmpInst::Predicate Pred = I.getPredicate(); 7967 7968 // Check that predicates are valid. 7969 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) && 7970 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE)) 7971 return nullptr; 7972 7973 // Check that RHS operand is zero. 7974 if (!match(RHSC, m_AnyZeroFP())) 7975 return nullptr; 7976 7977 // Check fastmath flags ('ninf'). 7978 if (!LHSI->hasNoInfs() || !I.hasNoInfs()) 7979 return nullptr; 7980 7981 // Check the properties of the dividend. It must not be zero to avoid a 7982 // division by zero (see Proof). 7983 const APFloat *C; 7984 if (!match(LHSI->getOperand(0), m_APFloat(C))) 7985 return nullptr; 7986 7987 if (C->isZero()) 7988 return nullptr; 7989 7990 // Get swapped predicate if necessary. 7991 if (C->isNegative()) 7992 Pred = I.getSwappedPredicate(); 7993 7994 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I); 7995 } 7996 7997 /// Optimize fabs(X) compared with zero. 7998 static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) { 7999 Value *X; 8000 if (!match(I.getOperand(0), m_FAbs(m_Value(X)))) 8001 return nullptr; 8002 8003 const APFloat *C; 8004 if (!match(I.getOperand(1), m_APFloat(C))) 8005 return nullptr; 8006 8007 if (!C->isPosZero()) { 8008 if (!C->isSmallestNormalized()) 8009 return nullptr; 8010 8011 const Function *F = I.getFunction(); 8012 DenormalMode Mode = F->getDenormalMode(C->getSemantics()); 8013 if (Mode.Input == DenormalMode::PreserveSign || 8014 Mode.Input == DenormalMode::PositiveZero) { 8015 8016 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) { 8017 Constant *Zero = ConstantFP::getZero(X->getType()); 8018 return new FCmpInst(P, X, Zero, "", I); 8019 }; 8020 8021 switch (I.getPredicate()) { 8022 case FCmpInst::FCMP_OLT: 8023 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0 8024 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X); 8025 case FCmpInst::FCMP_UGE: 8026 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0 8027 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X); 8028 case FCmpInst::FCMP_OGE: 8029 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0 8030 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X); 8031 case FCmpInst::FCMP_ULT: 8032 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0 8033 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X); 8034 default: 8035 break; 8036 } 8037 } 8038 8039 return nullptr; 8040 } 8041 8042 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) { 8043 I->setPredicate(P); 8044 return IC.replaceOperand(*I, 0, X); 8045 }; 8046 8047 switch (I.getPredicate()) { 8048 case FCmpInst::FCMP_UGE: 8049 case FCmpInst::FCMP_OLT: 8050 // fabs(X) >= 0.0 --> true 8051 // fabs(X) < 0.0 --> false 8052 llvm_unreachable("fcmp should have simplified"); 8053 8054 case FCmpInst::FCMP_OGT: 8055 // fabs(X) > 0.0 --> X != 0.0 8056 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X); 8057 8058 case FCmpInst::FCMP_UGT: 8059 // fabs(X) u> 0.0 --> X u!= 0.0 8060 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X); 8061 8062 case FCmpInst::FCMP_OLE: 8063 // fabs(X) <= 0.0 --> X == 0.0 8064 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X); 8065 8066 case FCmpInst::FCMP_ULE: 8067 // fabs(X) u<= 0.0 --> X u== 0.0 8068 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X); 8069 8070 case FCmpInst::FCMP_OGE: 8071 // fabs(X) >= 0.0 --> !isnan(X) 8072 assert(!I.hasNoNaNs() && "fcmp should have simplified"); 8073 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X); 8074 8075 case FCmpInst::FCMP_ULT: 8076 // fabs(X) u< 0.0 --> isnan(X) 8077 assert(!I.hasNoNaNs() && "fcmp should have simplified"); 8078 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X); 8079 8080 case FCmpInst::FCMP_OEQ: 8081 case FCmpInst::FCMP_UEQ: 8082 case FCmpInst::FCMP_ONE: 8083 case FCmpInst::FCMP_UNE: 8084 case FCmpInst::FCMP_ORD: 8085 case FCmpInst::FCMP_UNO: 8086 // Look through the fabs() because it doesn't change anything but the sign. 8087 // fabs(X) == 0.0 --> X == 0.0, 8088 // fabs(X) != 0.0 --> X != 0.0 8089 // isnan(fabs(X)) --> isnan(X) 8090 // !isnan(fabs(X) --> !isnan(X) 8091 return replacePredAndOp0(&I, I.getPredicate(), X); 8092 8093 default: 8094 return nullptr; 8095 } 8096 } 8097 8098 /// Optimize sqrt(X) compared with zero. 8099 static Instruction *foldSqrtWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) { 8100 Value *X; 8101 if (!match(I.getOperand(0), m_Sqrt(m_Value(X)))) 8102 return nullptr; 8103 8104 if (!match(I.getOperand(1), m_PosZeroFP())) 8105 return nullptr; 8106 8107 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) { 8108 I.setPredicate(P); 8109 return IC.replaceOperand(I, 0, X); 8110 }; 8111 8112 // Clear ninf flag if sqrt doesn't have it. 8113 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs()) 8114 I.setHasNoInfs(false); 8115 8116 switch (I.getPredicate()) { 8117 case FCmpInst::FCMP_OLT: 8118 case FCmpInst::FCMP_UGE: 8119 // sqrt(X) < 0.0 --> false 8120 // sqrt(X) u>= 0.0 --> true 8121 llvm_unreachable("fcmp should have simplified"); 8122 case FCmpInst::FCMP_ULT: 8123 case FCmpInst::FCMP_ULE: 8124 case FCmpInst::FCMP_OGT: 8125 case FCmpInst::FCMP_OGE: 8126 case FCmpInst::FCMP_OEQ: 8127 case FCmpInst::FCMP_UNE: 8128 // sqrt(X) u< 0.0 --> X u< 0.0 8129 // sqrt(X) u<= 0.0 --> X u<= 0.0 8130 // sqrt(X) > 0.0 --> X > 0.0 8131 // sqrt(X) >= 0.0 --> X >= 0.0 8132 // sqrt(X) == 0.0 --> X == 0.0 8133 // sqrt(X) u!= 0.0 --> X u!= 0.0 8134 return IC.replaceOperand(I, 0, X); 8135 8136 case FCmpInst::FCMP_OLE: 8137 // sqrt(X) <= 0.0 --> X == 0.0 8138 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ); 8139 case FCmpInst::FCMP_UGT: 8140 // sqrt(X) u> 0.0 --> X u!= 0.0 8141 return ReplacePredAndOp0(FCmpInst::FCMP_UNE); 8142 case FCmpInst::FCMP_UEQ: 8143 // sqrt(X) u== 0.0 --> X u<= 0.0 8144 return ReplacePredAndOp0(FCmpInst::FCMP_ULE); 8145 case FCmpInst::FCMP_ONE: 8146 // sqrt(X) != 0.0 --> X > 0.0 8147 return ReplacePredAndOp0(FCmpInst::FCMP_OGT); 8148 case FCmpInst::FCMP_ORD: 8149 // !isnan(sqrt(X)) --> X >= 0.0 8150 return ReplacePredAndOp0(FCmpInst::FCMP_OGE); 8151 case FCmpInst::FCMP_UNO: 8152 // isnan(sqrt(X)) --> X u< 0.0 8153 return ReplacePredAndOp0(FCmpInst::FCMP_ULT); 8154 default: 8155 llvm_unreachable("Unexpected predicate!"); 8156 } 8157 } 8158 8159 static Instruction *foldFCmpFNegCommonOp(FCmpInst &I) { 8160 CmpInst::Predicate Pred = I.getPredicate(); 8161 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 8162 8163 // Canonicalize fneg as Op1. 8164 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) { 8165 std::swap(Op0, Op1); 8166 Pred = I.getSwappedPredicate(); 8167 } 8168 8169 if (!match(Op1, m_FNeg(m_Specific(Op0)))) 8170 return nullptr; 8171 8172 // Replace the negated operand with 0.0: 8173 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0 8174 Constant *Zero = ConstantFP::getZero(Op0->getType()); 8175 return new FCmpInst(Pred, Op0, Zero, "", &I); 8176 } 8177 8178 static Instruction *foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI, 8179 Constant *RHSC, InstCombinerImpl &CI) { 8180 const CmpInst::Predicate Pred = I.getPredicate(); 8181 Value *X = LHSI->getOperand(0); 8182 Value *Y = LHSI->getOperand(1); 8183 switch (Pred) { 8184 default: 8185 break; 8186 case FCmpInst::FCMP_UGT: 8187 case FCmpInst::FCMP_ULT: 8188 case FCmpInst::FCMP_UNE: 8189 case FCmpInst::FCMP_OEQ: 8190 case FCmpInst::FCMP_OGE: 8191 case FCmpInst::FCMP_OLE: 8192 // The optimization is not valid if X and Y are infinities of the same 8193 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan 8194 // flag then we can assume we do not have that case. Otherwise we might be 8195 // able to prove that either X or Y is not infinity. 8196 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() && 8197 !isKnownNeverInfinity(Y, /*Depth=*/0, 8198 CI.getSimplifyQuery().getWithInstruction(&I)) && 8199 !isKnownNeverInfinity(X, /*Depth=*/0, 8200 CI.getSimplifyQuery().getWithInstruction(&I))) 8201 break; 8202 8203 [[fallthrough]]; 8204 case FCmpInst::FCMP_OGT: 8205 case FCmpInst::FCMP_OLT: 8206 case FCmpInst::FCMP_ONE: 8207 case FCmpInst::FCMP_UEQ: 8208 case FCmpInst::FCMP_UGE: 8209 case FCmpInst::FCMP_ULE: 8210 // fcmp pred (x - y), 0 --> fcmp pred x, y 8211 if (match(RHSC, m_AnyZeroFP()) && 8212 I.getFunction()->getDenormalMode( 8213 LHSI->getType()->getScalarType()->getFltSemantics()) == 8214 DenormalMode::getIEEE()) { 8215 CI.replaceOperand(I, 0, X); 8216 CI.replaceOperand(I, 1, Y); 8217 return &I; 8218 } 8219 break; 8220 } 8221 8222 return nullptr; 8223 } 8224 8225 static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I, 8226 InstCombinerImpl &IC) { 8227 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); 8228 Type *OpType = LHS->getType(); 8229 CmpInst::Predicate Pred = I.getPredicate(); 8230 8231 bool FloorX = match(LHS, m_Intrinsic<Intrinsic::floor>(m_Specific(RHS))); 8232 bool CeilX = match(LHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(RHS))); 8233 8234 if (!FloorX && !CeilX) { 8235 if ((FloorX = match(RHS, m_Intrinsic<Intrinsic::floor>(m_Specific(LHS)))) || 8236 (CeilX = match(RHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(LHS))))) { 8237 std::swap(LHS, RHS); 8238 Pred = I.getSwappedPredicate(); 8239 } 8240 } 8241 8242 switch (Pred) { 8243 case FCmpInst::FCMP_OLE: 8244 // fcmp ole floor(x), x => fcmp ord x, 0 8245 if (FloorX) 8246 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType), 8247 "", &I); 8248 break; 8249 case FCmpInst::FCMP_OGT: 8250 // fcmp ogt floor(x), x => false 8251 if (FloorX) 8252 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 8253 break; 8254 case FCmpInst::FCMP_OGE: 8255 // fcmp oge ceil(x), x => fcmp ord x, 0 8256 if (CeilX) 8257 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType), 8258 "", &I); 8259 break; 8260 case FCmpInst::FCMP_OLT: 8261 // fcmp olt ceil(x), x => false 8262 if (CeilX) 8263 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 8264 break; 8265 case FCmpInst::FCMP_ULE: 8266 // fcmp ule floor(x), x => true 8267 if (FloorX) 8268 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 8269 break; 8270 case FCmpInst::FCMP_UGT: 8271 // fcmp ugt floor(x), x => fcmp uno x, 0 8272 if (FloorX) 8273 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(OpType), 8274 "", &I); 8275 break; 8276 case FCmpInst::FCMP_UGE: 8277 // fcmp uge ceil(x), x => true 8278 if (CeilX) 8279 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 8280 break; 8281 case FCmpInst::FCMP_ULT: 8282 // fcmp ult ceil(x), x => fcmp uno x, 0 8283 if (CeilX) 8284 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(OpType), 8285 "", &I); 8286 break; 8287 default: 8288 break; 8289 } 8290 8291 return nullptr; 8292 } 8293 8294 Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) { 8295 bool Changed = false; 8296 8297 /// Orders the operands of the compare so that they are listed from most 8298 /// complex to least complex. This puts constants before unary operators, 8299 /// before binary operators. 8300 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) { 8301 I.swapOperands(); 8302 Changed = true; 8303 } 8304 8305 const CmpInst::Predicate Pred = I.getPredicate(); 8306 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); 8307 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(), 8308 SQ.getWithInstruction(&I))) 8309 return replaceInstUsesWith(I, V); 8310 8311 // Simplify 'fcmp pred X, X' 8312 Type *OpType = Op0->getType(); 8313 assert(OpType == Op1->getType() && "fcmp with different-typed operands?"); 8314 if (Op0 == Op1) { 8315 switch (Pred) { 8316 default: break; 8317 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) 8318 case FCmpInst::FCMP_ULT: // True if unordered or less than 8319 case FCmpInst::FCMP_UGT: // True if unordered or greater than 8320 case FCmpInst::FCMP_UNE: // True if unordered or not equal 8321 // Canonicalize these to be 'fcmp uno %X, 0.0'. 8322 I.setPredicate(FCmpInst::FCMP_UNO); 8323 I.setOperand(1, Constant::getNullValue(OpType)); 8324 return &I; 8325 8326 case FCmpInst::FCMP_ORD: // True if ordered (no nans) 8327 case FCmpInst::FCMP_OEQ: // True if ordered and equal 8328 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal 8329 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal 8330 // Canonicalize these to be 'fcmp ord %X, 0.0'. 8331 I.setPredicate(FCmpInst::FCMP_ORD); 8332 I.setOperand(1, Constant::getNullValue(OpType)); 8333 return &I; 8334 } 8335 } 8336 8337 if (I.isCommutative()) { 8338 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) { 8339 replaceOperand(I, 0, Pair->first); 8340 replaceOperand(I, 1, Pair->second); 8341 return &I; 8342 } 8343 } 8344 8345 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand, 8346 // then canonicalize the operand to 0.0. 8347 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) { 8348 if (!match(Op0, m_PosZeroFP()) && 8349 isKnownNeverNaN(Op0, 0, getSimplifyQuery().getWithInstruction(&I))) 8350 return replaceOperand(I, 0, ConstantFP::getZero(OpType)); 8351 8352 if (!match(Op1, m_PosZeroFP()) && 8353 isKnownNeverNaN(Op1, 0, getSimplifyQuery().getWithInstruction(&I))) 8354 return replaceOperand(I, 1, ConstantFP::getZero(OpType)); 8355 } 8356 8357 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y 8358 Value *X, *Y; 8359 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) 8360 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I); 8361 8362 if (Instruction *R = foldFCmpFNegCommonOp(I)) 8363 return R; 8364 8365 // Test if the FCmpInst instruction is used exclusively by a select as 8366 // part of a minimum or maximum operation. If so, refrain from doing 8367 // any other folding. This helps out other analyses which understand 8368 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution 8369 // and CodeGen. And in this case, at least one of the comparison 8370 // operands has at least one user besides the compare (the select), 8371 // which would often largely negate the benefit of folding anyway. 8372 if (I.hasOneUse()) 8373 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) { 8374 Value *A, *B; 8375 SelectPatternResult SPR = matchSelectPattern(SI, A, B); 8376 if (SPR.Flavor != SPF_UNKNOWN) 8377 return nullptr; 8378 } 8379 8380 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0: 8381 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0 8382 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP())) 8383 return replaceOperand(I, 1, ConstantFP::getZero(OpType)); 8384 8385 // Canonicalize: 8386 // fcmp olt X, +inf -> fcmp one X, +inf 8387 // fcmp ole X, +inf -> fcmp ord X, 0 8388 // fcmp ogt X, +inf -> false 8389 // fcmp oge X, +inf -> fcmp oeq X, +inf 8390 // fcmp ult X, +inf -> fcmp une X, +inf 8391 // fcmp ule X, +inf -> true 8392 // fcmp ugt X, +inf -> fcmp uno X, 0 8393 // fcmp uge X, +inf -> fcmp ueq X, +inf 8394 // fcmp olt X, -inf -> false 8395 // fcmp ole X, -inf -> fcmp oeq X, -inf 8396 // fcmp ogt X, -inf -> fcmp one X, -inf 8397 // fcmp oge X, -inf -> fcmp ord X, 0 8398 // fcmp ult X, -inf -> fcmp uno X, 0 8399 // fcmp ule X, -inf -> fcmp ueq X, -inf 8400 // fcmp ugt X, -inf -> fcmp une X, -inf 8401 // fcmp uge X, -inf -> true 8402 const APFloat *C; 8403 if (match(Op1, m_APFloat(C)) && C->isInfinity()) { 8404 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) { 8405 default: 8406 break; 8407 case FCmpInst::FCMP_ORD: 8408 case FCmpInst::FCMP_UNO: 8409 case FCmpInst::FCMP_TRUE: 8410 case FCmpInst::FCMP_FALSE: 8411 case FCmpInst::FCMP_OGT: 8412 case FCmpInst::FCMP_ULE: 8413 llvm_unreachable("Should be simplified by InstSimplify"); 8414 case FCmpInst::FCMP_OLT: 8415 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I); 8416 case FCmpInst::FCMP_OLE: 8417 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType), 8418 "", &I); 8419 case FCmpInst::FCMP_OGE: 8420 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I); 8421 case FCmpInst::FCMP_ULT: 8422 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I); 8423 case FCmpInst::FCMP_UGT: 8424 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType), 8425 "", &I); 8426 case FCmpInst::FCMP_UGE: 8427 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I); 8428 } 8429 } 8430 8431 // Ignore signbit of bitcasted int when comparing equality to FP 0.0: 8432 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0 8433 if (match(Op1, m_PosZeroFP()) && 8434 match(Op0, m_OneUse(m_ElementWiseBitCast(m_Value(X))))) { 8435 ICmpInst::Predicate IntPred = ICmpInst::BAD_ICMP_PREDICATE; 8436 if (Pred == FCmpInst::FCMP_OEQ) 8437 IntPred = ICmpInst::ICMP_EQ; 8438 else if (Pred == FCmpInst::FCMP_UNE) 8439 IntPred = ICmpInst::ICMP_NE; 8440 8441 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) { 8442 Type *IntTy = X->getType(); 8443 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits()); 8444 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask)); 8445 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy)); 8446 } 8447 } 8448 8449 // Handle fcmp with instruction LHS and constant RHS. 8450 Instruction *LHSI; 8451 Constant *RHSC; 8452 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) { 8453 switch (LHSI->getOpcode()) { 8454 case Instruction::Select: 8455 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0 8456 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) && 8457 match(LHSI, m_c_Select(m_FNeg(m_Value(X)), m_Deferred(X)))) 8458 return replaceOperand(I, 0, X); 8459 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI))) 8460 return NV; 8461 break; 8462 case Instruction::FSub: 8463 if (LHSI->hasOneUse()) 8464 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this)) 8465 return NV; 8466 break; 8467 case Instruction::PHI: 8468 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI))) 8469 return NV; 8470 break; 8471 case Instruction::SIToFP: 8472 case Instruction::UIToFP: 8473 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC)) 8474 return NV; 8475 break; 8476 case Instruction::FDiv: 8477 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC)) 8478 return NV; 8479 break; 8480 case Instruction::Load: 8481 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) 8482 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) 8483 if (Instruction *Res = foldCmpLoadFromIndexedGlobal( 8484 cast<LoadInst>(LHSI), GEP, GV, I)) 8485 return Res; 8486 break; 8487 } 8488 } 8489 8490 if (Instruction *R = foldFabsWithFcmpZero(I, *this)) 8491 return R; 8492 8493 if (Instruction *R = foldSqrtWithFcmpZero(I, *this)) 8494 return R; 8495 8496 if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this)) 8497 return R; 8498 8499 if (match(Op0, m_FNeg(m_Value(X)))) { 8500 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C 8501 Constant *C; 8502 if (match(Op1, m_Constant(C))) 8503 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) 8504 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I); 8505 } 8506 8507 // fcmp (fadd X, 0.0), Y --> fcmp X, Y 8508 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP()))) 8509 return new FCmpInst(Pred, X, Op1, "", &I); 8510 8511 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y 8512 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP()))) 8513 return new FCmpInst(Pred, Op0, Y, "", &I); 8514 8515 if (match(Op0, m_FPExt(m_Value(X)))) { 8516 // fcmp (fpext X), (fpext Y) -> fcmp X, Y 8517 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType()) 8518 return new FCmpInst(Pred, X, Y, "", &I); 8519 8520 const APFloat *C; 8521 if (match(Op1, m_APFloat(C))) { 8522 const fltSemantics &FPSem = 8523 X->getType()->getScalarType()->getFltSemantics(); 8524 bool Lossy; 8525 APFloat TruncC = *C; 8526 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy); 8527 8528 if (Lossy) { 8529 // X can't possibly equal the higher-precision constant, so reduce any 8530 // equality comparison. 8531 // TODO: Other predicates can be handled via getFCmpCode(). 8532 switch (Pred) { 8533 case FCmpInst::FCMP_OEQ: 8534 // X is ordered and equal to an impossible constant --> false 8535 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); 8536 case FCmpInst::FCMP_ONE: 8537 // X is ordered and not equal to an impossible constant --> ordered 8538 return new FCmpInst(FCmpInst::FCMP_ORD, X, 8539 ConstantFP::getZero(X->getType())); 8540 case FCmpInst::FCMP_UEQ: 8541 // X is unordered or equal to an impossible constant --> unordered 8542 return new FCmpInst(FCmpInst::FCMP_UNO, X, 8543 ConstantFP::getZero(X->getType())); 8544 case FCmpInst::FCMP_UNE: 8545 // X is unordered or not equal to an impossible constant --> true 8546 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); 8547 default: 8548 break; 8549 } 8550 } 8551 8552 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless 8553 // Avoid lossy conversions and denormals. 8554 // Zero is a special case that's OK to convert. 8555 APFloat Fabs = TruncC; 8556 Fabs.clearSign(); 8557 if (!Lossy && 8558 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) { 8559 Constant *NewC = ConstantFP::get(X->getType(), TruncC); 8560 return new FCmpInst(Pred, X, NewC, "", &I); 8561 } 8562 } 8563 } 8564 8565 // Convert a sign-bit test of an FP value into a cast and integer compare. 8566 // TODO: Simplify if the copysign constant is 0.0 or NaN. 8567 // TODO: Handle non-zero compare constants. 8568 // TODO: Handle other predicates. 8569 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C), 8570 m_Value(X)))) && 8571 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) { 8572 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits()); 8573 if (auto *VecTy = dyn_cast<VectorType>(OpType)) 8574 IntType = VectorType::get(IntType, VecTy->getElementCount()); 8575 8576 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0 8577 if (Pred == FCmpInst::FCMP_OLT) { 8578 Value *IntX = Builder.CreateBitCast(X, IntType); 8579 return new ICmpInst(ICmpInst::ICMP_SLT, IntX, 8580 ConstantInt::getNullValue(IntType)); 8581 } 8582 } 8583 8584 { 8585 Value *CanonLHS = nullptr, *CanonRHS = nullptr; 8586 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS))); 8587 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS))); 8588 8589 // (canonicalize(x) == x) => (x == x) 8590 if (CanonLHS == Op1) 8591 return new FCmpInst(Pred, Op1, Op1, "", &I); 8592 8593 // (x == canonicalize(x)) => (x == x) 8594 if (CanonRHS == Op0) 8595 return new FCmpInst(Pred, Op0, Op0, "", &I); 8596 8597 // (canonicalize(x) == canonicalize(y)) => (x == y) 8598 if (CanonLHS && CanonRHS) 8599 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I); 8600 } 8601 8602 if (I.getType()->isVectorTy()) 8603 if (Instruction *Res = foldVectorCmp(I, Builder)) 8604 return Res; 8605 8606 return Changed ? &I : nullptr; 8607 } 8608