8993 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the visitICmp and visitFCmp functions.10//11//===----------------------------------------------------------------------===//12 13#include "InstCombineInternal.h"14#include "llvm/ADT/APFloat.h"15#include "llvm/ADT/APSInt.h"16#include "llvm/ADT/SetVector.h"17#include "llvm/ADT/Statistic.h"18#include "llvm/Analysis/CaptureTracking.h"19#include "llvm/Analysis/CmpInstAnalysis.h"20#include "llvm/Analysis/ConstantFolding.h"21#include "llvm/Analysis/InstructionSimplify.h"22#include "llvm/Analysis/Loads.h"23#include "llvm/Analysis/Utils/Local.h"24#include "llvm/Analysis/VectorUtils.h"25#include "llvm/IR/ConstantRange.h"26#include "llvm/IR/Constants.h"27#include "llvm/IR/DataLayout.h"28#include "llvm/IR/InstrTypes.h"29#include "llvm/IR/Instructions.h"30#include "llvm/IR/IntrinsicInst.h"31#include "llvm/IR/PatternMatch.h"32#include "llvm/Support/KnownBits.h"33#include "llvm/Transforms/InstCombine/InstCombiner.h"34#include <bitset>35 36using namespace llvm;37using namespace PatternMatch;38 39#define DEBUG_TYPE "instcombine"40 41// How many times is a select replaced by one of its operands?42STATISTIC(NumSel, "Number of select opts");43 44/// Compute Result = In1+In2, returning true if the result overflowed for this45/// type.46static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,47 bool IsSigned = false) {48 bool Overflow;49 if (IsSigned)50 Result = In1.sadd_ov(In2, Overflow);51 else52 Result = In1.uadd_ov(In2, Overflow);53 54 return Overflow;55}56 57/// Compute Result = In1-In2, returning true if the result overflowed for this58/// type.59static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,60 bool IsSigned = false) {61 bool Overflow;62 if (IsSigned)63 Result = In1.ssub_ov(In2, Overflow);64 else65 Result = In1.usub_ov(In2, Overflow);66 67 return Overflow;68}69 70/// Given an icmp instruction, return true if any use of this comparison is a71/// branch on sign bit comparison.72static bool hasBranchUse(ICmpInst &I) {73 for (auto *U : I.users())74 if (isa<BranchInst>(U))75 return true;76 return false;77}78 79/// Returns true if the exploded icmp can be expressed as a signed comparison80/// to zero and updates the predicate accordingly.81/// The signedness of the comparison is preserved.82/// TODO: Refactor with decomposeBitTestICmp()?83static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {84 if (!ICmpInst::isSigned(Pred))85 return false;86 87 if (C.isZero())88 return ICmpInst::isRelational(Pred);89 90 if (C.isOne()) {91 if (Pred == ICmpInst::ICMP_SLT) {92 Pred = ICmpInst::ICMP_SLE;93 return true;94 }95 } else if (C.isAllOnes()) {96 if (Pred == ICmpInst::ICMP_SGT) {97 Pred = ICmpInst::ICMP_SGE;98 return true;99 }100 }101 102 return false;103}104 105/// This is called when we see this pattern:106/// cmp pred (load (gep GV, ...)), cmpcst107/// where GV is a global variable with a constant initializer. Try to simplify108/// this into some simple computation that does not need the load. For example109/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".110///111/// If AndCst is non-null, then the loaded value is masked with that constant112/// before doing the comparison. This handles cases like "A[i]&4 == 0".113Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(114 LoadInst *LI, GetElementPtrInst *GEP, CmpInst &ICI, ConstantInt *AndCst) {115 auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(GEP));116 if (LI->isVolatile() || !GV || !GV->isConstant() ||117 !GV->hasDefinitiveInitializer())118 return nullptr;119 120 Type *EltTy = LI->getType();121 TypeSize EltSize = DL.getTypeStoreSize(EltTy);122 if (EltSize.isScalable())123 return nullptr;124 125 LinearExpression Expr = decomposeLinearExpression(DL, GEP);126 if (!Expr.Index || Expr.BasePtr != GV || Expr.Offset.getBitWidth() > 64)127 return nullptr;128 129 Constant *Init = GV->getInitializer();130 TypeSize GlobalSize = DL.getTypeAllocSize(Init->getType());131 132 Value *Idx = Expr.Index;133 const APInt &Stride = Expr.Scale;134 const APInt &ConstOffset = Expr.Offset;135 136 // Allow an additional context offset, but only within the stride.137 if (!ConstOffset.ult(Stride))138 return nullptr;139 140 // Don't handle overlapping loads for now.141 if (!Stride.uge(EltSize.getFixedValue()))142 return nullptr;143 144 // Don't blow up on huge arrays.145 uint64_t ArrayElementCount =146 divideCeil((GlobalSize.getFixedValue() - ConstOffset.getZExtValue()),147 Stride.getZExtValue());148 if (ArrayElementCount > MaxArraySizeForCombine)149 return nullptr;150 151 enum { Overdefined = -3, Undefined = -2 };152 153 // Variables for our state machines.154 155 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form156 // "i == 47 | i == 87", where 47 is the first index the condition is true for,157 // and 87 is the second (and last) index. FirstTrueElement is -2 when158 // undefined, otherwise set to the first true element. SecondTrueElement is159 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.160 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;161 162 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the163 // form "i != 47 & i != 87". Same state transitions as for true elements.164 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;165 166 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these167 /// define a state machine that triggers for ranges of values that the index168 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.169 /// This is -2 when undefined, -3 when overdefined, and otherwise the last170 /// index in the range (inclusive). We use -2 for undefined here because we171 /// use relative comparisons and don't want 0-1 to match -1.172 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;173 174 // MagicBitvector - This is a magic bitvector where we set a bit if the175 // comparison is true for element 'i'. If there are 64 elements or less in176 // the array, this will fully represent all the comparison results.177 uint64_t MagicBitvector = 0;178 179 // Scan the array and see if one of our patterns matches.180 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));181 APInt Offset = ConstOffset;182 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i, Offset += Stride) {183 Constant *Elt = ConstantFoldLoadFromConst(Init, EltTy, Offset, DL);184 if (!Elt)185 return nullptr;186 187 // If the element is masked, handle it.188 if (AndCst) {189 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);190 if (!Elt)191 return nullptr;192 }193 194 // Find out if the comparison would be true or false for the i'th element.195 Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,196 CompareRHS, DL, &TLI);197 if (!C)198 return nullptr;199 200 // If the result is undef for this element, ignore it.201 if (isa<UndefValue>(C)) {202 // Extend range state machines to cover this element in case there is an203 // undef in the middle of the range.204 if (TrueRangeEnd == (int)i - 1)205 TrueRangeEnd = i;206 if (FalseRangeEnd == (int)i - 1)207 FalseRangeEnd = i;208 continue;209 }210 211 // If we can't compute the result for any of the elements, we have to give212 // up evaluating the entire conditional.213 if (!isa<ConstantInt>(C))214 return nullptr;215 216 // Otherwise, we know if the comparison is true or false for this element,217 // update our state machines.218 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();219 220 // State machine for single/double/range index comparison.221 if (IsTrueForElt) {222 // Update the TrueElement state machine.223 if (FirstTrueElement == Undefined)224 FirstTrueElement = TrueRangeEnd = i; // First true element.225 else {226 // Update double-compare state machine.227 if (SecondTrueElement == Undefined)228 SecondTrueElement = i;229 else230 SecondTrueElement = Overdefined;231 232 // Update range state machine.233 if (TrueRangeEnd == (int)i - 1)234 TrueRangeEnd = i;235 else236 TrueRangeEnd = Overdefined;237 }238 } else {239 // Update the FalseElement state machine.240 if (FirstFalseElement == Undefined)241 FirstFalseElement = FalseRangeEnd = i; // First false element.242 else {243 // Update double-compare state machine.244 if (SecondFalseElement == Undefined)245 SecondFalseElement = i;246 else247 SecondFalseElement = Overdefined;248 249 // Update range state machine.250 if (FalseRangeEnd == (int)i - 1)251 FalseRangeEnd = i;252 else253 FalseRangeEnd = Overdefined;254 }255 }256 257 // If this element is in range, update our magic bitvector.258 if (i < 64 && IsTrueForElt)259 MagicBitvector |= 1ULL << i;260 261 // If all of our states become overdefined, bail out early. Since the262 // predicate is expensive, only check it every 8 elements. This is only263 // really useful for really huge arrays.264 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&265 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&266 FalseRangeEnd == Overdefined)267 return nullptr;268 }269 270 // Now that we've scanned the entire array, emit our new comparison(s). We271 // order the state machines in complexity of the generated code.272 273 // If inbounds keyword is not present, Idx * Stride can overflow.274 // Let's assume that Stride is 2 and the wanted value is at offset 0.275 // Then, there are two possible values for Idx to match offset 0:276 // 0x00..00, 0x80..00.277 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the278 // comparison is false if Idx was 0x80..00.279 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.280 auto MaskIdx = [&](Value *Idx) {281 if (!Expr.Flags.isInBounds() && Stride.countr_zero() != 0) {282 Value *Mask = Constant::getAllOnesValue(Idx->getType());283 Mask = Builder.CreateLShr(Mask, Stride.countr_zero());284 Idx = Builder.CreateAnd(Idx, Mask);285 }286 return Idx;287 };288 289 // If the comparison is only true for one or two elements, emit direct290 // comparisons.291 if (SecondTrueElement != Overdefined) {292 Idx = MaskIdx(Idx);293 // None true -> false.294 if (FirstTrueElement == Undefined)295 return replaceInstUsesWith(ICI, Builder.getFalse());296 297 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);298 299 // True for one element -> 'i == 47'.300 if (SecondTrueElement == Undefined)301 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);302 303 // True for two elements -> 'i == 47 | i == 72'.304 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx);305 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);306 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx);307 return BinaryOperator::CreateOr(C1, C2);308 }309 310 // If the comparison is only false for one or two elements, emit direct311 // comparisons.312 if (SecondFalseElement != Overdefined) {313 Idx = MaskIdx(Idx);314 // None false -> true.315 if (FirstFalseElement == Undefined)316 return replaceInstUsesWith(ICI, Builder.getTrue());317 318 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);319 320 // False for one element -> 'i != 47'.321 if (SecondFalseElement == Undefined)322 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);323 324 // False for two elements -> 'i != 47 & i != 72'.325 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx);326 Value *SecondFalseIdx =327 ConstantInt::get(Idx->getType(), SecondFalseElement);328 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx);329 return BinaryOperator::CreateAnd(C1, C2);330 }331 332 // If the comparison can be replaced with a range comparison for the elements333 // where it is true, emit the range check.334 if (TrueRangeEnd != Overdefined) {335 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");336 Idx = MaskIdx(Idx);337 338 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).339 if (FirstTrueElement) {340 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);341 Idx = Builder.CreateAdd(Idx, Offs);342 }343 344 Value *End =345 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1);346 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);347 }348 349 // False range check.350 if (FalseRangeEnd != Overdefined) {351 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");352 Idx = MaskIdx(Idx);353 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).354 if (FirstFalseElement) {355 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);356 Idx = Builder.CreateAdd(Idx, Offs);357 }358 359 Value *End =360 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement);361 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);362 }363 364 // If a magic bitvector captures the entire comparison state365 // of this load, replace it with computation that does:366 // ((magic_cst >> i) & 1) != 0367 {368 Type *Ty = nullptr;369 370 // Look for an appropriate type:371 // - The type of Idx if the magic fits372 // - The smallest fitting legal type373 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())374 Ty = Idx->getType();375 else376 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);377 378 if (Ty) {379 Idx = MaskIdx(Idx);380 Value *V = Builder.CreateIntCast(Idx, Ty, false);381 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);382 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V);383 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));384 }385 }386 387 return nullptr;388}389 390/// Returns true if we can rewrite Start as a GEP with pointer Base391/// and some integer offset. The nodes that need to be re-written392/// for this transformation will be added to Explored.393static bool canRewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags &NW,394 const DataLayout &DL,395 SetVector<Value *> &Explored) {396 SmallVector<Value *, 16> WorkList(1, Start);397 Explored.insert(Base);398 399 // The following traversal gives us an order which can be used400 // when doing the final transformation. Since in the final401 // transformation we create the PHI replacement instructions first,402 // we don't have to get them in any particular order.403 //404 // However, for other instructions we will have to traverse the405 // operands of an instruction first, which means that we have to406 // do a post-order traversal.407 while (!WorkList.empty()) {408 SetVector<PHINode *> PHIs;409 410 while (!WorkList.empty()) {411 if (Explored.size() >= 100)412 return false;413 414 Value *V = WorkList.back();415 416 if (Explored.contains(V)) {417 WorkList.pop_back();418 continue;419 }420 421 if (!isa<GetElementPtrInst>(V) && !isa<PHINode>(V))422 // We've found some value that we can't explore which is different from423 // the base. Therefore we can't do this transformation.424 return false;425 426 if (auto *GEP = dyn_cast<GEPOperator>(V)) {427 // Only allow inbounds GEPs with at most one variable offset.428 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); };429 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1)430 return false;431 432 NW = NW.intersectForOffsetAdd(GEP->getNoWrapFlags());433 if (!Explored.contains(GEP->getOperand(0)))434 WorkList.push_back(GEP->getOperand(0));435 }436 437 if (WorkList.back() == V) {438 WorkList.pop_back();439 // We've finished visiting this node, mark it as such.440 Explored.insert(V);441 }442 443 if (auto *PN = dyn_cast<PHINode>(V)) {444 // We cannot transform PHIs on unsplittable basic blocks.445 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))446 return false;447 Explored.insert(PN);448 PHIs.insert(PN);449 }450 }451 452 // Explore the PHI nodes further.453 for (auto *PN : PHIs)454 for (Value *Op : PN->incoming_values())455 if (!Explored.contains(Op))456 WorkList.push_back(Op);457 }458 459 // Make sure that we can do this. Since we can't insert GEPs in a basic460 // block before a PHI node, we can't easily do this transformation if461 // we have PHI node users of transformed instructions.462 for (Value *Val : Explored) {463 for (Value *Use : Val->uses()) {464 465 auto *PHI = dyn_cast<PHINode>(Use);466 auto *Inst = dyn_cast<Instruction>(Val);467 468 if (Inst == Base || Inst == PHI || !Inst || !PHI ||469 !Explored.contains(PHI))470 continue;471 472 if (PHI->getParent() == Inst->getParent())473 return false;474 }475 }476 return true;477}478 479// Sets the appropriate insert point on Builder where we can add480// a replacement Instruction for V (if that is possible).481static void setInsertionPoint(IRBuilder<> &Builder, Value *V,482 bool Before = true) {483 if (auto *PHI = dyn_cast<PHINode>(V)) {484 BasicBlock *Parent = PHI->getParent();485 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());486 return;487 }488 if (auto *I = dyn_cast<Instruction>(V)) {489 if (!Before)490 I = &*std::next(I->getIterator());491 Builder.SetInsertPoint(I);492 return;493 }494 if (auto *A = dyn_cast<Argument>(V)) {495 // Set the insertion point in the entry block.496 BasicBlock &Entry = A->getParent()->getEntryBlock();497 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt());498 return;499 }500 // Otherwise, this is a constant and we don't need to set a new501 // insertion point.502 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");503}504 505/// Returns a re-written value of Start as an indexed GEP using Base as a506/// pointer.507static Value *rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW,508 const DataLayout &DL,509 SetVector<Value *> &Explored,510 InstCombiner &IC) {511 // Perform all the substitutions. This is a bit tricky because we can512 // have cycles in our use-def chains.513 // 1. Create the PHI nodes without any incoming values.514 // 2. Create all the other values.515 // 3. Add the edges for the PHI nodes.516 // 4. Emit GEPs to get the original pointers.517 // 5. Remove the original instructions.518 Type *IndexType = IntegerType::get(519 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType()));520 521 DenseMap<Value *, Value *> NewInsts;522 NewInsts[Base] = ConstantInt::getNullValue(IndexType);523 524 // Create the new PHI nodes, without adding any incoming values.525 for (Value *Val : Explored) {526 if (Val == Base)527 continue;528 // Create empty phi nodes. This avoids cyclic dependencies when creating529 // the remaining instructions.530 if (auto *PHI = dyn_cast<PHINode>(Val))531 NewInsts[PHI] =532 PHINode::Create(IndexType, PHI->getNumIncomingValues(),533 PHI->getName() + ".idx", PHI->getIterator());534 }535 IRBuilder<> Builder(Base->getContext());536 537 // Create all the other instructions.538 for (Value *Val : Explored) {539 if (NewInsts.contains(Val))540 continue;541 542 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {543 setInsertionPoint(Builder, GEP);544 Value *Op = NewInsts[GEP->getOperand(0)];545 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);546 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero())547 NewInsts[GEP] = OffsetV;548 else549 NewInsts[GEP] = Builder.CreateAdd(550 Op, OffsetV, GEP->getOperand(0)->getName() + ".add",551 /*NUW=*/NW.hasNoUnsignedWrap(),552 /*NSW=*/NW.hasNoUnsignedSignedWrap());553 continue;554 }555 if (isa<PHINode>(Val))556 continue;557 558 llvm_unreachable("Unexpected instruction type");559 }560 561 // Add the incoming values to the PHI nodes.562 for (Value *Val : Explored) {563 if (Val == Base)564 continue;565 // All the instructions have been created, we can now add edges to the566 // phi nodes.567 if (auto *PHI = dyn_cast<PHINode>(Val)) {568 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);569 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {570 Value *NewIncoming = PHI->getIncomingValue(I);571 572 auto It = NewInsts.find(NewIncoming);573 if (It != NewInsts.end())574 NewIncoming = It->second;575 576 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));577 }578 }579 }580 581 for (Value *Val : Explored) {582 if (Val == Base)583 continue;584 585 setInsertionPoint(Builder, Val, false);586 // Create GEP for external users.587 Value *NewVal = Builder.CreateGEP(Builder.getInt8Ty(), Base, NewInsts[Val],588 Val->getName() + ".ptr", NW);589 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal);590 // Add old instruction to worklist for DCE. We don't directly remove it591 // here because the original compare is one of the users.592 IC.addToWorklist(cast<Instruction>(Val));593 }594 595 return NewInsts[Start];596}597 598/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.599/// We can look through PHIs, GEPs and casts in order to determine a common base600/// between GEPLHS and RHS.601static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,602 CmpPredicate Cond,603 const DataLayout &DL,604 InstCombiner &IC) {605 // FIXME: Support vector of pointers.606 if (GEPLHS->getType()->isVectorTy())607 return nullptr;608 609 if (!GEPLHS->hasAllConstantIndices())610 return nullptr;611 612 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);613 Value *PtrBase =614 GEPLHS->stripAndAccumulateConstantOffsets(DL, Offset,615 /*AllowNonInbounds*/ false);616 617 // Bail if we looked through addrspacecast.618 if (PtrBase->getType() != GEPLHS->getType())619 return nullptr;620 621 // The set of nodes that will take part in this transformation.622 SetVector<Value *> Nodes;623 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();624 if (!canRewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes))625 return nullptr;626 627 // We know we can re-write this as628 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)629 // Since we've only looked through inbouds GEPs we know that we630 // can't have overflow on either side. We can therefore re-write631 // this as:632 // OFFSET1 cmp OFFSET2633 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes, IC);634 635 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written636 // GEP having PtrBase as the pointer base, and has returned in NewRHS the637 // offset. Since Index is the offset of LHS to the base pointer, we will now638 // compare the offsets instead of comparing the pointers.639 return new ICmpInst(ICmpInst::getSignedPredicate(Cond),640 IC.Builder.getInt(Offset), NewRHS);641}642 643/// Fold comparisons between a GEP instruction and something else. At this point644/// we know that the GEP is on the LHS of the comparison.645Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,646 CmpPredicate Cond, Instruction &I) {647 // Don't transform signed compares of GEPs into index compares. Even if the648 // GEP is inbounds, the final add of the base pointer can have signed overflow649 // and would change the result of the icmp.650 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be651 // the maximum signed value for the pointer type.652 if (ICmpInst::isSigned(Cond))653 return nullptr;654 655 // Look through bitcasts and addrspacecasts. We do not however want to remove656 // 0 GEPs.657 if (!isa<GetElementPtrInst>(RHS))658 RHS = RHS->stripPointerCasts();659 660 auto CanFold = [Cond](GEPNoWrapFlags NW) {661 if (ICmpInst::isEquality(Cond))662 return true;663 664 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags.665 assert(ICmpInst::isUnsigned(Cond));666 return NW != GEPNoWrapFlags::none();667 };668 669 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) {670 if (!NW.hasNoUnsignedWrap()) {671 // Convert signed to unsigned comparison.672 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Op1, Op2);673 }674 675 auto *I = new ICmpInst(Cond, Op1, Op2);676 I->setSameSign(NW.hasNoUnsignedSignedWrap());677 return I;678 };679 680 CommonPointerBase Base = CommonPointerBase::compute(GEPLHS, RHS);681 if (Base.Ptr == RHS && CanFold(Base.LHSNW) && !Base.isExpensive()) {682 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).683 Type *IdxTy = DL.getIndexType(GEPLHS->getType());684 Value *Offset =685 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);686 return NewICmp(Base.LHSNW, Offset,687 Constant::getNullValue(Offset->getType()));688 }689 690 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&691 isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&692 !NullPointerIsDefined(I.getFunction(),693 RHS->getType()->getPointerAddressSpace())) {694 // For most address spaces, an allocation can't be placed at null, but null695 // itself is treated as a 0 size allocation in the in bounds rules. Thus,696 // the only valid inbounds address derived from null, is null itself.697 // Thus, we have four cases to consider:698 // 1) Base == nullptr, Offset == 0 -> inbounds, null699 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds700 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)701 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)702 //703 // (Note if we're indexing a type of size 0, that simply collapses into one704 // of the buckets above.)705 //706 // In general, we're allowed to make values less poison (i.e. remove707 // sources of full UB), so in this case, we just select between the two708 // non-poison cases (1 and 4 above).709 //710 // For vectors, we apply the same reasoning on a per-lane basis.711 auto *Base = GEPLHS->getPointerOperand();712 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {713 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount();714 Base = Builder.CreateVectorSplat(EC, Base);715 }716 return new ICmpInst(Cond, Base,717 ConstantExpr::getPointerBitCastOrAddrSpaceCast(718 cast<Constant>(RHS), Base->getType()));719 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {720 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();721 722 // If the base pointers are different, but the indices are the same, just723 // compare the base pointer.724 if (GEPLHS->getOperand(0) != GEPRHS->getOperand(0)) {725 bool IndicesTheSame =726 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&727 GEPLHS->getPointerOperand()->getType() ==728 GEPRHS->getPointerOperand()->getType() &&729 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();730 if (IndicesTheSame)731 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)732 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {733 IndicesTheSame = false;734 break;735 }736 737 // If all indices are the same, just compare the base pointers.738 Type *BaseType = GEPLHS->getOperand(0)->getType();739 if (IndicesTheSame &&740 CmpInst::makeCmpResultType(BaseType) == I.getType() && CanFold(NW))741 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));742 743 // If we're comparing GEPs with two base pointers that only differ in type744 // and both GEPs have only constant indices or just one use, then fold745 // the compare with the adjusted indices.746 // FIXME: Support vector of pointers.747 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&748 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&749 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&750 GEPLHS->getOperand(0)->stripPointerCasts() ==751 GEPRHS->getOperand(0)->stripPointerCasts() &&752 !GEPLHS->getType()->isVectorTy()) {753 Value *LOffset = EmitGEPOffset(GEPLHS);754 Value *ROffset = EmitGEPOffset(GEPRHS);755 756 // If we looked through an addrspacecast between different sized address757 // spaces, the LHS and RHS pointers are different sized758 // integers. Truncate to the smaller one.759 Type *LHSIndexTy = LOffset->getType();760 Type *RHSIndexTy = ROffset->getType();761 if (LHSIndexTy != RHSIndexTy) {762 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <763 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {764 ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy);765 } else766 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy);767 }768 769 Value *Cmp = Builder.CreateICmp(ICmpInst::getSignedPredicate(Cond),770 LOffset, ROffset);771 return replaceInstUsesWith(I, Cmp);772 }773 }774 775 if (GEPLHS->getOperand(0) == GEPRHS->getOperand(0) &&776 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&777 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {778 // If the GEPs only differ by one index, compare it.779 unsigned NumDifferences = 0; // Keep track of # differences.780 unsigned DiffOperand = 0; // The operand that differs.781 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)782 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {783 Type *LHSType = GEPLHS->getOperand(i)->getType();784 Type *RHSType = GEPRHS->getOperand(i)->getType();785 // FIXME: Better support for vector of pointers.786 if (LHSType->getPrimitiveSizeInBits() !=787 RHSType->getPrimitiveSizeInBits() ||788 (GEPLHS->getType()->isVectorTy() &&789 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {790 // Irreconcilable differences.791 NumDifferences = 2;792 break;793 }794 795 if (NumDifferences++)796 break;797 DiffOperand = i;798 }799 800 if (NumDifferences == 0) // SAME GEP?801 return replaceInstUsesWith(802 I, // No comparison is needed here.803 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));804 // If two GEPs only differ by an index, compare them.805 // Note that nowrap flags are always needed when comparing two indices.806 else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {807 Value *LHSV = GEPLHS->getOperand(DiffOperand);808 Value *RHSV = GEPRHS->getOperand(DiffOperand);809 return NewICmp(NW, LHSV, RHSV);810 }811 }812 813 if (Base.Ptr && CanFold(Base.LHSNW & Base.RHSNW) && !Base.isExpensive()) {814 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)815 Type *IdxTy = DL.getIndexType(GEPLHS->getType());816 Value *L =817 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEP=*/true);818 Value *R =819 EmitGEPOffsets(Base.RHSGEPs, Base.RHSNW, IdxTy, /*RewriteGEP=*/true);820 return NewICmp(Base.LHSNW & Base.RHSNW, L, R);821 }822 }823 824 // Try convert this to an indexed compare by looking through PHIs/casts as a825 // last resort.826 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);827}828 829bool InstCombinerImpl::foldAllocaCmp(AllocaInst *Alloca) {830 // It would be tempting to fold away comparisons between allocas and any831 // pointer not based on that alloca (e.g. an argument). However, even832 // though such pointers cannot alias, they can still compare equal.833 //834 // But LLVM doesn't specify where allocas get their memory, so if the alloca835 // doesn't escape we can argue that it's impossible to guess its value, and we836 // can therefore act as if any such guesses are wrong.837 //838 // However, we need to ensure that this folding is consistent: We can't fold839 // one comparison to false, and then leave a different comparison against the840 // same value alone (as it might evaluate to true at runtime, leading to a841 // contradiction). As such, this code ensures that all comparisons are folded842 // at the same time, and there are no other escapes.843 844 struct CmpCaptureTracker : public CaptureTracker {845 AllocaInst *Alloca;846 bool Captured = false;847 /// The value of the map is a bit mask of which icmp operands the alloca is848 /// used in.849 SmallMapVector<ICmpInst *, unsigned, 4> ICmps;850 851 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}852 853 void tooManyUses() override { Captured = true; }854 855 Action captured(const Use *U, UseCaptureInfo CI) override {856 // TODO(captures): Use UseCaptureInfo.857 auto *ICmp = dyn_cast<ICmpInst>(U->getUser());858 // We need to check that U is based *only* on the alloca, and doesn't859 // have other contributions from a select/phi operand.860 // TODO: We could check whether getUnderlyingObjects() reduces to one861 // object, which would allow looking through phi nodes.862 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) {863 // Collect equality icmps of the alloca, and don't treat them as864 // captures.865 ICmps[ICmp] |= 1u << U->getOperandNo();866 return Continue;867 }868 869 Captured = true;870 return Stop;871 }872 };873 874 CmpCaptureTracker Tracker(Alloca);875 PointerMayBeCaptured(Alloca, &Tracker);876 if (Tracker.Captured)877 return false;878 879 bool Changed = false;880 for (auto [ICmp, Operands] : Tracker.ICmps) {881 switch (Operands) {882 case 1:883 case 2: {884 // The alloca is only used in one icmp operand. Assume that the885 // equality is false.886 auto *Res = ConstantInt::get(ICmp->getType(),887 ICmp->getPredicate() == ICmpInst::ICMP_NE);888 replaceInstUsesWith(*ICmp, Res);889 eraseInstFromFunction(*ICmp);890 Changed = true;891 break;892 }893 case 3:894 // Both icmp operands are based on the alloca, so this is comparing895 // pointer offsets, without leaking any information about the address896 // of the alloca. Ignore such comparisons.897 break;898 default:899 llvm_unreachable("Cannot happen");900 }901 }902 903 return Changed;904}905 906/// Fold "icmp pred (X+C), X".907Instruction *InstCombinerImpl::foldICmpAddOpConst(Value *X, const APInt &C,908 CmpPredicate Pred) {909 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,910 // so the values can never be equal. Similarly for all other "or equals"911 // operators.912 assert(!!C && "C should not be zero!");913 914 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255915 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253916 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0917 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {918 Constant *R =919 ConstantInt::get(X->getType(), APInt::getMaxValue(C.getBitWidth()) - C);920 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);921 }922 923 // (X+1) >u X --> X <u (0-1) --> X != 255924 // (X+2) >u X --> X <u (0-2) --> X <u 254925 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0926 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)927 return new ICmpInst(ICmpInst::ICMP_ULT, X,928 ConstantInt::get(X->getType(), -C));929 930 APInt SMax = APInt::getSignedMaxValue(C.getBitWidth());931 932 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127933 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125934 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0935 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1936 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126937 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127938 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)939 return new ICmpInst(ICmpInst::ICMP_SGT, X,940 ConstantInt::get(X->getType(), SMax - C));941 942 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127943 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126944 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1945 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2946 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126947 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128948 949 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);950 return new ICmpInst(ICmpInst::ICMP_SLT, X,951 ConstantInt::get(X->getType(), SMax - (C - 1)));952}953 954/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->955/// (icmp eq/ne A, Log2(AP2/AP1)) ->956/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).957Instruction *InstCombinerImpl::foldICmpShrConstConst(ICmpInst &I, Value *A,958 const APInt &AP1,959 const APInt &AP2) {960 assert(I.isEquality() && "Cannot fold icmp gt/lt");961 962 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {963 if (I.getPredicate() == I.ICMP_NE)964 Pred = CmpInst::getInversePredicate(Pred);965 return new ICmpInst(Pred, LHS, RHS);966 };967 968 // Don't bother doing any work for cases which InstSimplify handles.969 if (AP2.isZero())970 return nullptr;971 972 bool IsAShr = isa<AShrOperator>(I.getOperand(0));973 if (IsAShr) {974 if (AP2.isAllOnes())975 return nullptr;976 if (AP2.isNegative() != AP1.isNegative())977 return nullptr;978 if (AP2.sgt(AP1))979 return nullptr;980 }981 982 if (!AP1)983 // 'A' must be large enough to shift out the highest set bit.984 return getICmp(I.ICMP_UGT, A,985 ConstantInt::get(A->getType(), AP2.logBase2()));986 987 if (AP1 == AP2)988 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));989 990 int Shift;991 if (IsAShr && AP1.isNegative())992 Shift = AP1.countl_one() - AP2.countl_one();993 else994 Shift = AP1.countl_zero() - AP2.countl_zero();995 996 if (Shift > 0) {997 if (IsAShr && AP1 == AP2.ashr(Shift)) {998 // There are multiple solutions if we are comparing against -1 and the LHS999 // of the ashr is not a power of two.1000 if (AP1.isAllOnes() && !AP2.isPowerOf2())1001 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));1002 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));1003 } else if (AP1 == AP2.lshr(Shift)) {1004 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));1005 }1006 }1007 1008 // Shifting const2 will never be equal to const1.1009 // FIXME: This should always be handled by InstSimplify?1010 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);1011 return replaceInstUsesWith(I, TorF);1012}1013 1014/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->1015/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).1016Instruction *InstCombinerImpl::foldICmpShlConstConst(ICmpInst &I, Value *A,1017 const APInt &AP1,1018 const APInt &AP2) {1019 assert(I.isEquality() && "Cannot fold icmp gt/lt");1020 1021 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {1022 if (I.getPredicate() == I.ICMP_NE)1023 Pred = CmpInst::getInversePredicate(Pred);1024 return new ICmpInst(Pred, LHS, RHS);1025 };1026 1027 // Don't bother doing any work for cases which InstSimplify handles.1028 if (AP2.isZero())1029 return nullptr;1030 1031 unsigned AP2TrailingZeros = AP2.countr_zero();1032 1033 if (!AP1 && AP2TrailingZeros != 0)1034 return getICmp(1035 I.ICMP_UGE, A,1036 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));1037 1038 if (AP1 == AP2)1039 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));1040 1041 // Get the distance between the lowest bits that are set.1042 int Shift = AP1.countr_zero() - AP2TrailingZeros;1043 1044 if (Shift > 0 && AP2.shl(Shift) == AP1)1045 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));1046 1047 // Shifting const2 will never be equal to const1.1048 // FIXME: This should always be handled by InstSimplify?1049 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);1050 return replaceInstUsesWith(I, TorF);1051}1052 1053/// The caller has matched a pattern of the form:1054/// I = icmp ugt (add (add A, B), CI2), CI11055/// If this is of the form:1056/// sum = a + b1057/// if (sum+128 >u 255)1058/// Then replace it with llvm.sadd.with.overflow.i8.1059///1060static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,1061 ConstantInt *CI2, ConstantInt *CI1,1062 InstCombinerImpl &IC) {1063 // The transformation we're trying to do here is to transform this into an1064 // llvm.sadd.with.overflow. To do this, we have to replace the original add1065 // with a narrower add, and discard the add-with-constant that is part of the1066 // range check (if we can't eliminate it, this isn't profitable).1067 1068 // In order to eliminate the add-with-constant, the compare can be its only1069 // use.1070 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));1071 if (!AddWithCst->hasOneUse())1072 return nullptr;1073 1074 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.1075 if (!CI2->getValue().isPowerOf2())1076 return nullptr;1077 unsigned NewWidth = CI2->getValue().countr_zero();1078 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)1079 return nullptr;1080 1081 // The width of the new add formed is 1 more than the bias.1082 ++NewWidth;1083 1084 // Check to see that CI1 is an all-ones value with NewWidth bits.1085 if (CI1->getBitWidth() == NewWidth ||1086 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))1087 return nullptr;1088 1089 // This is only really a signed overflow check if the inputs have been1090 // sign-extended; check for that condition. For example, if CI2 is 2^31 and1091 // the operands of the add are 64 bits wide, we need at least 33 sign bits.1092 if (IC.ComputeMaxSignificantBits(A, &I) > NewWidth ||1093 IC.ComputeMaxSignificantBits(B, &I) > NewWidth)1094 return nullptr;1095 1096 // In order to replace the original add with a narrower1097 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant1098 // and truncates that discard the high bits of the add. Verify that this is1099 // the case.1100 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));1101 for (User *U : OrigAdd->users()) {1102 if (U == AddWithCst)1103 continue;1104 1105 // Only accept truncates for now. We would really like a nice recursive1106 // predicate like SimplifyDemandedBits, but which goes downwards the use-def1107 // chain to see which bits of a value are actually demanded. If the1108 // original add had another add which was then immediately truncated, we1109 // could still do the transformation.1110 TruncInst *TI = dyn_cast<TruncInst>(U);1111 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)1112 return nullptr;1113 }1114 1115 // If the pattern matches, truncate the inputs to the narrower type and1116 // use the sadd_with_overflow intrinsic to efficiently compute both the1117 // result and the overflow bit.1118 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);1119 Function *F = Intrinsic::getOrInsertDeclaration(1120 I.getModule(), Intrinsic::sadd_with_overflow, NewType);1121 1122 InstCombiner::BuilderTy &Builder = IC.Builder;1123 1124 // Put the new code above the original add, in case there are any uses of the1125 // add between the add and the compare.1126 Builder.SetInsertPoint(OrigAdd);1127 1128 Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc");1129 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc");1130 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd");1131 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result");1132 Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType());1133 1134 // The inner add was the result of the narrow add, zero extended to the1135 // wider type. Replace it with the result computed by the intrinsic.1136 IC.replaceInstUsesWith(*OrigAdd, ZExt);1137 IC.eraseInstFromFunction(*OrigAdd);1138 1139 // The original icmp gets replaced with the overflow value.1140 return ExtractValueInst::Create(Call, 1, "sadd.overflow");1141}1142 1143/// If we have:1144/// icmp eq/ne (urem/srem %x, %y), 01145/// iff %y is a power-of-two, we can replace this with a bit test:1146/// icmp eq/ne (and %x, (add %y, -1)), 01147Instruction *InstCombinerImpl::foldIRemByPowerOfTwoToBitTest(ICmpInst &I) {1148 // This fold is only valid for equality predicates.1149 if (!I.isEquality())1150 return nullptr;1151 CmpPredicate Pred;1152 Value *X, *Y, *Zero;1153 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))),1154 m_CombineAnd(m_Zero(), m_Value(Zero)))))1155 return nullptr;1156 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, &I))1157 return nullptr;1158 // This may increase instruction count, we don't enforce that Y is a constant.1159 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType()));1160 Value *Masked = Builder.CreateAnd(X, Mask);1161 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, Zero);1162}1163 1164/// Fold equality-comparison between zero and any (maybe truncated) right-shift1165/// by one-less-than-bitwidth into a sign test on the original value.1166Instruction *InstCombinerImpl::foldSignBitTest(ICmpInst &I) {1167 Instruction *Val;1168 CmpPredicate Pred;1169 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero())))1170 return nullptr;1171 1172 Value *X;1173 Type *XTy;1174 1175 Constant *C;1176 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) {1177 XTy = X->getType();1178 unsigned XBitWidth = XTy->getScalarSizeInBits();1179 if (!match(C, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,1180 APInt(XBitWidth, XBitWidth - 1))))1181 return nullptr;1182 } else if (isa<BinaryOperator>(Val) &&1183 (X = reassociateShiftAmtsOfTwoSameDirectionShifts(1184 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val),1185 /*AnalyzeForSignBitExtraction=*/true))) {1186 XTy = X->getType();1187 } else1188 return nullptr;1189 1190 return ICmpInst::Create(Instruction::ICmp,1191 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SGE1192 : ICmpInst::ICMP_SLT,1193 X, ConstantInt::getNullValue(XTy));1194}1195 1196// Handle icmp pred X, 01197Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {1198 CmpInst::Predicate Pred = Cmp.getPredicate();1199 if (!match(Cmp.getOperand(1), m_Zero()))1200 return nullptr;1201 1202 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)1203 if (Pred == ICmpInst::ICMP_SGT) {1204 Value *A, *B;1205 if (match(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) {1206 if (isKnownPositive(A, SQ.getWithInstruction(&Cmp)))1207 return new ICmpInst(Pred, B, Cmp.getOperand(1));1208 if (isKnownPositive(B, SQ.getWithInstruction(&Cmp)))1209 return new ICmpInst(Pred, A, Cmp.getOperand(1));1210 }1211 }1212 1213 if (Instruction *New = foldIRemByPowerOfTwoToBitTest(Cmp))1214 return New;1215 1216 // Given:1217 // icmp eq/ne (urem %x, %y), 01218 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':1219 // icmp eq/ne %x, 01220 Value *X, *Y;1221 if (match(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) &&1222 ICmpInst::isEquality(Pred)) {1223 KnownBits XKnown = computeKnownBits(X, &Cmp);1224 KnownBits YKnown = computeKnownBits(Y, &Cmp);1225 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)1226 return new ICmpInst(Pred, X, Cmp.getOperand(1));1227 }1228 1229 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are1230 // odd/non-zero/there is no overflow.1231 if (match(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) &&1232 ICmpInst::isEquality(Pred)) {1233 1234 KnownBits XKnown = computeKnownBits(X, &Cmp);1235 // if X % 2 != 01236 // (icmp eq/ne Y)1237 if (XKnown.countMaxTrailingZeros() == 0)1238 return new ICmpInst(Pred, Y, Cmp.getOperand(1));1239 1240 KnownBits YKnown = computeKnownBits(Y, &Cmp);1241 // if Y % 2 != 01242 // (icmp eq/ne X)1243 if (YKnown.countMaxTrailingZeros() == 0)1244 return new ICmpInst(Pred, X, Cmp.getOperand(1));1245 1246 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0));1247 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {1248 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);1249 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`1250 // but to avoid unnecessary work, first just if this is an obvious case.1251 1252 // if X non-zero and NoOverflow(X * Y)1253 // (icmp eq/ne Y)1254 if (!XKnown.One.isZero() || isKnownNonZero(X, Q))1255 return new ICmpInst(Pred, Y, Cmp.getOperand(1));1256 1257 // if Y non-zero and NoOverflow(X * Y)1258 // (icmp eq/ne X)1259 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q))1260 return new ICmpInst(Pred, X, Cmp.getOperand(1));1261 }1262 // Note, we are skipping cases:1263 // if Y % 2 != 0 AND X % 2 != 01264 // (false/true)1265 // if X non-zero and Y non-zero and NoOverflow(X * Y)1266 // (false/true)1267 // Those can be simplified later as we would have already replaced the (icmp1268 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that1269 // will fold to a constant elsewhere.1270 }1271 1272 // (icmp eq/ne f(X), 0) -> (icmp eq/ne X, 0)1273 // where f(X) == 0 if and only if X == 01274 if (ICmpInst::isEquality(Pred))1275 if (Value *Stripped = stripNullTest(Cmp.getOperand(0)))1276 return new ICmpInst(Pred, Stripped,1277 Constant::getNullValue(Stripped->getType()));1278 1279 return nullptr;1280}1281 1282/// Fold icmp eq (num + mask) & ~mask, num1283/// to1284/// icmp eq (and num, mask), 01285/// Where mask is a low bit mask.1286Instruction *InstCombinerImpl::foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp) {1287 Value *Num;1288 CmpPredicate Pred;1289 const APInt *Mask, *Neg;1290 1291 if (!match(&Cmp,1292 m_c_ICmp(Pred, m_Value(Num),1293 m_OneUse(m_c_And(m_OneUse(m_c_Add(m_Deferred(Num),1294 m_LowBitMask(Mask))),1295 m_APInt(Neg))))))1296 return nullptr;1297 1298 if (*Neg != ~*Mask)1299 return nullptr;1300 1301 if (!ICmpInst::isEquality(Pred))1302 return nullptr;1303 1304 // Create new icmp eq (num & mask), 01305 auto *NewAnd = Builder.CreateAnd(Num, *Mask);1306 auto *Zero = Constant::getNullValue(Num->getType());1307 1308 return new ICmpInst(Pred, NewAnd, Zero);1309}1310 1311/// Fold icmp Pred X, C.1312/// TODO: This code structure does not make sense. The saturating add fold1313/// should be moved to some other helper and extended as noted below (it is also1314/// possible that code has been made unnecessary - do we canonicalize IR to1315/// overflow/saturating intrinsics or not?).1316Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) {1317 // Match the following pattern, which is a common idiom when writing1318 // overflow-safe integer arithmetic functions. The source performs an addition1319 // in wider type and explicitly checks for overflow using comparisons against1320 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.1321 //1322 // TODO: This could probably be generalized to handle other overflow-safe1323 // operations if we worked out the formulas to compute the appropriate magic1324 // constants.1325 //1326 // sum = a + b1327 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i81328 CmpInst::Predicate Pred = Cmp.getPredicate();1329 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);1330 Value *A, *B;1331 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI1332 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) &&1333 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))1334 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))1335 return Res;1336 1337 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).1338 Constant *C = dyn_cast<Constant>(Op1);1339 if (!C)1340 return nullptr;1341 1342 if (auto *Phi = dyn_cast<PHINode>(Op0))1343 if (all_of(Phi->operands(), IsaPred<Constant>)) {1344 SmallVector<Constant *> Ops;1345 for (Value *V : Phi->incoming_values()) {1346 Constant *Res =1347 ConstantFoldCompareInstOperands(Pred, cast<Constant>(V), C, DL);1348 if (!Res)1349 return nullptr;1350 Ops.push_back(Res);1351 }1352 Builder.SetInsertPoint(Phi);1353 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());1354 for (auto [V, Pred] : zip(Ops, Phi->blocks()))1355 NewPhi->addIncoming(V, Pred);1356 return replaceInstUsesWith(Cmp, NewPhi);1357 }1358 1359 if (Instruction *R = tryFoldInstWithCtpopWithNot(&Cmp))1360 return R;1361 1362 return nullptr;1363}1364 1365/// Canonicalize icmp instructions based on dominating conditions.1366Instruction *InstCombinerImpl::foldICmpWithDominatingICmp(ICmpInst &Cmp) {1367 // We already checked simple implication in InstSimplify, only handle complex1368 // cases here.1369 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1);1370 const APInt *C;1371 if (!match(Y, m_APInt(C)))1372 return nullptr;1373 1374 CmpInst::Predicate Pred = Cmp.getPredicate();1375 ConstantRange CR = ConstantRange::makeExactICmpRegion(Pred, *C);1376 1377 auto handleDomCond = [&](ICmpInst::Predicate DomPred,1378 const APInt *DomC) -> Instruction * {1379 // We have 2 compares of a variable with constants. Calculate the constant1380 // ranges of those compares to see if we can transform the 2nd compare:1381 // DomBB:1382 // DomCond = icmp DomPred X, DomC1383 // br DomCond, CmpBB, FalseBB1384 // CmpBB:1385 // Cmp = icmp Pred X, C1386 ConstantRange DominatingCR =1387 ConstantRange::makeExactICmpRegion(DomPred, *DomC);1388 ConstantRange Intersection = DominatingCR.intersectWith(CR);1389 ConstantRange Difference = DominatingCR.difference(CR);1390 if (Intersection.isEmptySet())1391 return replaceInstUsesWith(Cmp, Builder.getFalse());1392 if (Difference.isEmptySet())1393 return replaceInstUsesWith(Cmp, Builder.getTrue());1394 1395 // Canonicalizing a sign bit comparison that gets used in a branch,1396 // pessimizes codegen by generating branch on zero instruction instead1397 // of a test and branch. So we avoid canonicalizing in such situations1398 // because test and branch instruction has better branch displacement1399 // than compare and branch instruction.1400 bool UnusedBit;1401 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit);1402 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp)))1403 return nullptr;1404 1405 // Avoid an infinite loop with min/max canonicalization.1406 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.1407 if (Cmp.hasOneUse() &&1408 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value())))1409 return nullptr;1410 1411 if (const APInt *EqC = Intersection.getSingleElement())1412 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC));1413 if (const APInt *NeC = Difference.getSingleElement())1414 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC));1415 return nullptr;1416 };1417 1418 for (BranchInst *BI : DC.conditionsFor(X)) {1419 CmpPredicate DomPred;1420 const APInt *DomC;1421 if (!match(BI->getCondition(),1422 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC))))1423 continue;1424 1425 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));1426 if (DT.dominates(Edge0, Cmp.getParent())) {1427 if (auto *V = handleDomCond(DomPred, DomC))1428 return V;1429 } else {1430 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));1431 if (DT.dominates(Edge1, Cmp.getParent()))1432 if (auto *V =1433 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC))1434 return V;1435 }1436 }1437 1438 return nullptr;1439}1440 1441/// Fold icmp (trunc X), C.1442Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,1443 TruncInst *Trunc,1444 const APInt &C) {1445 ICmpInst::Predicate Pred = Cmp.getPredicate();1446 Value *X = Trunc->getOperand(0);1447 Type *SrcTy = X->getType();1448 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),1449 SrcBits = SrcTy->getScalarSizeInBits();1450 1451 // Match (icmp pred (trunc nuw/nsw X), C)1452 // Which we can convert to (icmp pred X, (sext/zext C))1453 if (shouldChangeType(Trunc->getType(), SrcTy)) {1454 if (Trunc->hasNoSignedWrap())1455 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));1456 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())1457 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));1458 }1459 1460 if (C.isOne() && C.getBitWidth() > 1) {1461 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 11462 Value *V = nullptr;1463 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))1464 return new ICmpInst(ICmpInst::ICMP_SLT, V,1465 ConstantInt::get(V->getType(), 1));1466 }1467 1468 // TODO: Handle non-equality predicates.1469 Value *Y;1470 const APInt *Pow2;1471 if (Cmp.isEquality() && match(X, m_Shl(m_Power2(Pow2), m_Value(Y))) &&1472 DstBits > Pow2->logBase2()) {1473 // (trunc (Pow2 << Y) to iN) == 0 --> Y u>= N - log2(Pow2)1474 // (trunc (Pow2 << Y) to iN) != 0 --> Y u< N - log2(Pow2)1475 // iff N > log2(Pow2)1476 if (C.isZero()) {1477 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;1478 return new ICmpInst(NewPred, Y,1479 ConstantInt::get(SrcTy, DstBits - Pow2->logBase2()));1480 }1481 // (trunc (Pow2 << Y) to iN) == 2**C --> Y == C - log2(Pow2)1482 // (trunc (Pow2 << Y) to iN) != 2**C --> Y != C - log2(Pow2)1483 if (C.isPowerOf2())1484 return new ICmpInst(1485 Pred, Y, ConstantInt::get(SrcTy, C.logBase2() - Pow2->logBase2()));1486 }1487 1488 if (Cmp.isEquality() && (Trunc->hasOneUse() || Trunc->hasNoUnsignedWrap())) {1489 // Canonicalize to a mask and wider compare if the wide type is suitable:1490 // (trunc X to i8) == C --> (X & 0xff) == (zext C)1491 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {1492 Constant *Mask =1493 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits));1494 Value *And = Trunc->hasNoUnsignedWrap() ? X : Builder.CreateAnd(X, Mask);1495 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits));1496 return new ICmpInst(Pred, And, WideC);1497 }1498 1499 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all1500 // of the high bits truncated out of x are known.1501 KnownBits Known = computeKnownBits(X, &Cmp);1502 1503 // If all the high bits are known, we can do this xform.1504 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {1505 // Pull in the high bits from known-ones set.1506 APInt NewRHS = C.zext(SrcBits);1507 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);1508 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS));1509 }1510 }1511 1512 // Look through truncated right-shift of the sign-bit for a sign-bit check:1513 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 01514 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -11515 Value *ShOp;1516 uint64_t ShAmt;1517 bool TrueIfSigned;1518 if (isSignBitCheck(Pred, C, TrueIfSigned) &&1519 match(X, m_Shr(m_Value(ShOp), m_ConstantInt(ShAmt))) &&1520 DstBits == SrcBits - ShAmt) {1521 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,1522 ConstantInt::getNullValue(SrcTy))1523 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,1524 ConstantInt::getAllOnesValue(SrcTy));1525 }1526 1527 return nullptr;1528}1529 1530/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).1531/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).1532Instruction *1533InstCombinerImpl::foldICmpTruncWithTruncOrExt(ICmpInst &Cmp,1534 const SimplifyQuery &Q) {1535 Value *X, *Y;1536 CmpPredicate Pred;1537 bool YIsSExt = false;1538 // Try to match icmp (trunc X), (trunc Y)1539 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {1540 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() &1541 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind();1542 if (Cmp.isSigned()) {1543 // For signed comparisons, both truncs must be nsw.1544 if (!(NoWrapFlags & TruncInst::NoSignedWrap))1545 return nullptr;1546 } else {1547 // For unsigned and equality comparisons, either both must be nuw or1548 // both must be nsw, we don't care which.1549 if (!NoWrapFlags)1550 return nullptr;1551 }1552 1553 if (X->getType() != Y->getType() &&1554 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))1555 return nullptr;1556 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) &&1557 isDesirableIntType(Y->getType()->getScalarSizeInBits())) {1558 std::swap(X, Y);1559 Pred = Cmp.getSwappedPredicate(Pred);1560 }1561 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);1562 }1563 // Try to match icmp (trunc nuw X), (zext Y)1564 else if (!Cmp.isSigned() &&1565 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),1566 m_OneUse(m_ZExt(m_Value(Y)))))) {1567 // Can fold trunc nuw + zext for unsigned and equality predicates.1568 }1569 // Try to match icmp (trunc nsw X), (sext Y)1570 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),1571 m_OneUse(m_ZExtOrSExt(m_Value(Y)))))) {1572 // Can fold trunc nsw + zext/sext for all predicates.1573 YIsSExt =1574 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));1575 } else1576 return nullptr;1577 1578 Type *TruncTy = Cmp.getOperand(0)->getType();1579 unsigned TruncBits = TruncTy->getScalarSizeInBits();1580 1581 // If this transform will end up changing from desirable types -> undesirable1582 // types skip it.1583 if (isDesirableIntType(TruncBits) &&1584 !isDesirableIntType(X->getType()->getScalarSizeInBits()))1585 return nullptr;1586 1587 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);1588 return new ICmpInst(Pred, X, NewY);1589}1590 1591/// Fold icmp (xor X, Y), C.1592Instruction *InstCombinerImpl::foldICmpXorConstant(ICmpInst &Cmp,1593 BinaryOperator *Xor,1594 const APInt &C) {1595 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))1596 return I;1597 1598 Value *X = Xor->getOperand(0);1599 Value *Y = Xor->getOperand(1);1600 const APInt *XorC;1601 if (!match(Y, m_APInt(XorC)))1602 return nullptr;1603 1604 // If this is a comparison that tests the signbit (X < 0) or (x > -1),1605 // fold the xor.1606 ICmpInst::Predicate Pred = Cmp.getPredicate();1607 bool TrueIfSigned = false;1608 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {1609 1610 // If the sign bit of the XorCst is not set, there is no change to1611 // the operation, just stop using the Xor.1612 if (!XorC->isNegative())1613 return replaceOperand(Cmp, 0, X);1614 1615 // Emit the opposite comparison.1616 if (TrueIfSigned)1617 return new ICmpInst(ICmpInst::ICMP_SGT, X,1618 ConstantInt::getAllOnesValue(X->getType()));1619 else1620 return new ICmpInst(ICmpInst::ICMP_SLT, X,1621 ConstantInt::getNullValue(X->getType()));1622 }1623 1624 if (Xor->hasOneUse()) {1625 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))1626 if (!Cmp.isEquality() && XorC->isSignMask()) {1627 Pred = Cmp.getFlippedSignednessPredicate();1628 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));1629 }1630 1631 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))1632 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {1633 Pred = Cmp.getFlippedSignednessPredicate();1634 Pred = Cmp.getSwappedPredicate(Pred);1635 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));1636 }1637 }1638 1639 // Mask constant magic can eliminate an 'xor' with unsigned compares.1640 if (Pred == ICmpInst::ICMP_UGT) {1641 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)1642 if (*XorC == ~C && (C + 1).isPowerOf2())1643 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);1644 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)1645 if (*XorC == C && (C + 1).isPowerOf2())1646 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);1647 }1648 if (Pred == ICmpInst::ICMP_ULT) {1649 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)1650 if (*XorC == -C && C.isPowerOf2())1651 return new ICmpInst(ICmpInst::ICMP_UGT, X,1652 ConstantInt::get(X->getType(), ~C));1653 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)1654 if (*XorC == C && (-C).isPowerOf2())1655 return new ICmpInst(ICmpInst::ICMP_UGT, X,1656 ConstantInt::get(X->getType(), ~C));1657 }1658 return nullptr;1659}1660 1661/// For power-of-2 C:1662/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)1663/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)1664Instruction *InstCombinerImpl::foldICmpXorShiftConst(ICmpInst &Cmp,1665 BinaryOperator *Xor,1666 const APInt &C) {1667 CmpInst::Predicate Pred = Cmp.getPredicate();1668 APInt PowerOf2;1669 if (Pred == ICmpInst::ICMP_ULT)1670 PowerOf2 = C;1671 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())1672 PowerOf2 = C + 1;1673 else1674 return nullptr;1675 if (!PowerOf2.isPowerOf2())1676 return nullptr;1677 Value *X;1678 const APInt *ShiftC;1679 if (!match(Xor, m_OneUse(m_c_Xor(m_Value(X),1680 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))1681 return nullptr;1682 uint64_t Shift = ShiftC->getLimitedValue();1683 Type *XType = X->getType();1684 if (Shift == 0 || PowerOf2.isMinSignedValue())1685 return nullptr;1686 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));1687 APInt Bound =1688 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);1689 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));1690}1691 1692/// Fold icmp (and (sh X, Y), C2), C1.1693Instruction *InstCombinerImpl::foldICmpAndShift(ICmpInst &Cmp,1694 BinaryOperator *And,1695 const APInt &C1,1696 const APInt &C2) {1697 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));1698 if (!Shift || !Shift->isShift())1699 return nullptr;1700 1701 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could1702 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in1703 // code produced by the clang front-end, for bitfield access.1704 // This seemingly simple opportunity to fold away a shift turns out to be1705 // rather complicated. See PR17827 for details.1706 unsigned ShiftOpcode = Shift->getOpcode();1707 bool IsShl = ShiftOpcode == Instruction::Shl;1708 const APInt *C3;1709 if (match(Shift->getOperand(1), m_APInt(C3))) {1710 APInt NewAndCst, NewCmpCst;1711 bool AnyCmpCstBitsShiftedOut;1712 if (ShiftOpcode == Instruction::Shl) {1713 // For a left shift, we can fold if the comparison is not signed. We can1714 // also fold a signed comparison if the mask value and comparison value1715 // are not negative. These constraints may not be obvious, but we can1716 // prove that they are correct using an SMT solver.1717 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))1718 return nullptr;1719 1720 NewCmpCst = C1.lshr(*C3);1721 NewAndCst = C2.lshr(*C3);1722 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;1723 } else if (ShiftOpcode == Instruction::LShr) {1724 // For a logical right shift, we can fold if the comparison is not signed.1725 // We can also fold a signed comparison if the shifted mask value and the1726 // shifted comparison value are not negative. These constraints may not be1727 // obvious, but we can prove that they are correct using an SMT solver.1728 NewCmpCst = C1.shl(*C3);1729 NewAndCst = C2.shl(*C3);1730 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;1731 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))1732 return nullptr;1733 } else {1734 // For an arithmetic shift, check that both constants don't use (in a1735 // signed sense) the top bits being shifted out.1736 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");1737 NewCmpCst = C1.shl(*C3);1738 NewAndCst = C2.shl(*C3);1739 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;1740 if (NewAndCst.ashr(*C3) != C2)1741 return nullptr;1742 }1743 1744 if (AnyCmpCstBitsShiftedOut) {1745 // If we shifted bits out, the fold is not going to work out. As a1746 // special case, check to see if this means that the result is always1747 // true or false now.1748 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)1749 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));1750 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)1751 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));1752 } else {1753 Value *NewAnd = Builder.CreateAnd(1754 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));1755 return new ICmpInst(Cmp.getPredicate(), NewAnd,1756 ConstantInt::get(And->getType(), NewCmpCst));1757 }1758 }1759 1760 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is1761 // preferable because it allows the C2 << Y expression to be hoisted out of a1762 // loop if Y is invariant and X is not.1763 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&1764 !Shift->isArithmeticShift() &&1765 ((!IsShl && C2.isOne()) || !isa<Constant>(Shift->getOperand(0)))) {1766 // Compute C2 << Y.1767 Value *NewShift =1768 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))1769 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));1770 1771 // Compute X & (C2 << Y).1772 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);1773 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));1774 }1775 1776 return nullptr;1777}1778 1779/// Fold icmp (and X, C2), C1.1780Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp,1781 BinaryOperator *And,1782 const APInt &C1) {1783 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;1784 1785 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i11786 // TODO: We canonicalize to the longer form for scalars because we have1787 // better analysis/folds for icmp, and codegen may be better with icmp.1788 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&1789 match(And->getOperand(1), m_One()))1790 return new TruncInst(And->getOperand(0), Cmp.getType());1791 1792 const APInt *C2;1793 Value *X;1794 if (!match(And, m_And(m_Value(X), m_APInt(C2))))1795 return nullptr;1796 1797 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask1798 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(~*C2) &&1799 C2->isNegatedPowerOf2())1800 return new ICmpInst(ICmpInst::ICMP_SGT, X,1801 ConstantInt::get(X->getType(), ~*C2));1802 // (and X, highmask) s< [1, -highmask] --> X s< -highmask1803 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() &&1804 (C1 - 1).ule(~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask())1805 return new ICmpInst(ICmpInst::ICMP_SLT, X,1806 ConstantInt::get(X->getType(), -*C2));1807 1808 // Don't perform the following transforms if the AND has multiple uses1809 if (!And->hasOneUse())1810 return nullptr;1811 1812 if (Cmp.isEquality() && C1.isZero()) {1813 // Restrict this fold to single-use 'and' (PR10267).1814 // Replace (and X, (1 << size(X)-1) != 0) with X s< 01815 if (C2->isSignMask()) {1816 Constant *Zero = Constant::getNullValue(X->getType());1817 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;1818 return new ICmpInst(NewPred, X, Zero);1819 }1820 1821 APInt NewC2 = *C2;1822 KnownBits Know = computeKnownBits(And->getOperand(0), And);1823 // Set high zeros of C2 to allow matching negated power-of-2.1824 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),1825 Know.countMinLeadingZeros());1826 1827 // Restrict this fold only for single-use 'and' (PR10267).1828 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.1829 if (NewC2.isNegatedPowerOf2()) {1830 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);1831 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;1832 return new ICmpInst(NewPred, X, NegBOC);1833 }1834 }1835 1836 // If the LHS is an 'and' of a truncate and we can widen the and/compare to1837 // the input width without changing the value produced, eliminate the cast:1838 //1839 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'1840 //1841 // We can do this transformation if the constants do not have their sign bits1842 // set or if it is an equality comparison. Extending a relational comparison1843 // when we're checking the sign bit would not work.1844 Value *W;1845 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&1846 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {1847 // TODO: Is this a good transform for vectors? Wider types may reduce1848 // throughput. Should this transform be limited (even for scalars) by using1849 // shouldChangeType()?1850 if (!Cmp.getType()->isVectorTy()) {1851 Type *WideType = W->getType();1852 unsigned WideScalarBits = WideType->getScalarSizeInBits();1853 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));1854 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));1855 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());1856 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);1857 }1858 }1859 1860 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))1861 return I;1862 1863 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->1864 // (icmp pred (and A, (or (shl 1, B), 1), 0))1865 //1866 // iff pred isn't signed1867 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&1868 match(And->getOperand(1), m_One())) {1869 Constant *One = cast<Constant>(And->getOperand(1));1870 Value *Or = And->getOperand(0);1871 Value *A, *B, *LShr;1872 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&1873 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {1874 unsigned UsesRemoved = 0;1875 if (And->hasOneUse())1876 ++UsesRemoved;1877 if (Or->hasOneUse())1878 ++UsesRemoved;1879 if (LShr->hasOneUse())1880 ++UsesRemoved;1881 1882 // Compute A & ((1 << B) | 1)1883 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;1884 if (UsesRemoved >= RequireUsesRemoved) {1885 Value *NewOr =1886 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),1887 /*HasNUW=*/true),1888 One, Or->getName());1889 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());1890 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));1891 }1892 }1893 }1894 1895 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->1896 // llvm.is.fpclass(X, fcInf|fcNan)1897 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->1898 // llvm.is.fpclass(X, ~(fcInf|fcNan))1899 // (icmp eq (and (bitcast X to int), ExponentMask), 0) -->1900 // llvm.is.fpclass(X, fcSubnormal|fcZero)1901 // (icmp ne (and (bitcast X to int), ExponentMask), 0) -->1902 // llvm.is.fpclass(X, ~(fcSubnormal|fcZero))1903 Value *V;1904 if (!Cmp.getParent()->getParent()->hasFnAttribute(1905 Attribute::NoImplicitFloat) &&1906 Cmp.isEquality() &&1907 match(X, m_OneUse(m_ElementWiseBitCast(m_Value(V))))) {1908 Type *FPType = V->getType()->getScalarType();1909 if (FPType->isIEEELikeFPTy() && (C1.isZero() || C1 == *C2)) {1910 APInt ExponentMask =1911 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt();1912 if (*C2 == ExponentMask) {1913 unsigned Mask = C1.isZero()1914 ? FPClassTest::fcZero | FPClassTest::fcSubnormal1915 : FPClassTest::fcNan | FPClassTest::fcInf;1916 if (isICMP_NE)1917 Mask = ~Mask & fcAllFlags;1918 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));1919 }1920 }1921 }1922 1923 return nullptr;1924}1925 1926/// Fold icmp (and X, Y), C.1927Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp,1928 BinaryOperator *And,1929 const APInt &C) {1930 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))1931 return I;1932 1933 const ICmpInst::Predicate Pred = Cmp.getPredicate();1934 bool TrueIfNeg;1935 if (isSignBitCheck(Pred, C, TrueIfNeg)) {1936 // ((X - 1) & ~X) < 0 --> X == 01937 // ((X - 1) & ~X) >= 0 --> X != 01938 Value *X;1939 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&1940 match(And->getOperand(1), m_Not(m_Specific(X)))) {1941 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;1942 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));1943 }1944 // (X & -X) < 0 --> X == MinSignedC1945 // (X & -X) > -1 --> X != MinSignedC1946 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {1947 Constant *MinSignedC = ConstantInt::get(1948 X->getType(),1949 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));1950 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;1951 return new ICmpInst(NewPred, X, MinSignedC);1952 }1953 }1954 1955 // TODO: These all require that Y is constant too, so refactor with the above.1956 1957 // Try to optimize things like "A[i] & 42 == 0" to index computations.1958 Value *X = And->getOperand(0);1959 Value *Y = And->getOperand(1);1960 if (auto *C2 = dyn_cast<ConstantInt>(Y))1961 if (auto *LI = dyn_cast<LoadInst>(X))1962 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))1963 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(LI, GEP, Cmp, C2))1964 return Res;1965 1966 if (!Cmp.isEquality())1967 return nullptr;1968 1969 // X & -C == -C -> X > u ~C1970 // X & -C != -C -> X <= u ~C1971 // iff C is a power of 21972 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {1973 auto NewPred =1974 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGT : CmpInst::ICMP_ULE;1975 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));1976 }1977 1978 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)1979 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)1980 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)1981 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)1982 if (match(And, m_OneUse(m_c_And(m_OneUse(m_ZExt(m_Value(X))), m_Value(Y)))) &&1983 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {1984 Value *TruncY = Builder.CreateTrunc(Y, X->getType());1985 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {1986 Value *And = Builder.CreateAnd(TruncY, X);1987 return BinaryOperator::CreateNot(And);1988 }1989 return BinaryOperator::CreateAnd(TruncY, X);1990 }1991 1992 // (icmp eq/ne (and (shl -1, X), Y), 0)1993 // -> (icmp eq/ne (lshr Y, X), 0)1994 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems1995 // highly unlikely the non-zero case will ever show up in code.1996 if (C.isZero() &&1997 match(And, m_OneUse(m_c_And(m_OneUse(m_Shl(m_AllOnes(), m_Value(X))),1998 m_Value(Y))))) {1999 Value *LShr = Builder.CreateLShr(Y, X);2000 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));2001 }2002 2003 // (icmp eq/ne (and (add A, Addend), Msk), C)2004 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk))2005 {2006 Value *A;2007 const APInt *Addend, *Msk;2008 if (match(And, m_And(m_OneUse(m_Add(m_Value(A), m_APInt(Addend))),2009 m_LowBitMask(Msk))) &&2010 C.ule(*Msk)) {2011 APInt NewComperand = (C - *Addend) & *Msk;2012 Value *MaskA = Builder.CreateAnd(A, ConstantInt::get(A->getType(), *Msk));2013 return new ICmpInst(Pred, MaskA,2014 ConstantInt::get(MaskA->getType(), NewComperand));2015 }2016 }2017 2018 return nullptr;2019}2020 2021/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.2022static Value *foldICmpOrXorSubChain(ICmpInst &Cmp, BinaryOperator *Or,2023 InstCombiner::BuilderTy &Builder) {2024 // Are we using xors or subs to bitwise check for a pair or pairs of2025 // (in)equalities? Convert to a shorter form that has more potential to be2026 // folded even further.2027 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)2028 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)2029 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->2030 // (X1 == X2) && (X3 == X4) && (X5 == X6)2031 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->2032 // (X1 != X2) || (X3 != X4) || (X5 != X6)2033 SmallVector<std::pair<Value *, Value *>, 2> CmpValues;2034 SmallVector<Value *, 16> WorkList(1, Or);2035 2036 while (!WorkList.empty()) {2037 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {2038 Value *Lhs, *Rhs;2039 2040 if (match(OrOperatorArgument,2041 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {2042 CmpValues.emplace_back(Lhs, Rhs);2043 return;2044 }2045 2046 if (match(OrOperatorArgument,2047 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {2048 CmpValues.emplace_back(Lhs, Rhs);2049 return;2050 }2051 2052 WorkList.push_back(OrOperatorArgument);2053 };2054 2055 Value *CurrentValue = WorkList.pop_back_val();2056 Value *OrOperatorLhs, *OrOperatorRhs;2057 2058 if (!match(CurrentValue,2059 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {2060 return nullptr;2061 }2062 2063 MatchOrOperatorArgument(OrOperatorRhs);2064 MatchOrOperatorArgument(OrOperatorLhs);2065 }2066 2067 ICmpInst::Predicate Pred = Cmp.getPredicate();2068 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;2069 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,2070 CmpValues.rbegin()->second);2071 2072 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {2073 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);2074 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);2075 }2076 2077 return LhsCmp;2078}2079 2080/// Fold icmp (or X, Y), C.2081Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,2082 BinaryOperator *Or,2083 const APInt &C) {2084 ICmpInst::Predicate Pred = Cmp.getPredicate();2085 if (C.isOne()) {2086 // icmp slt signum(V) 1 --> icmp slt V, 12087 Value *V = nullptr;2088 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))2089 return new ICmpInst(ICmpInst::ICMP_SLT, V,2090 ConstantInt::get(V->getType(), 1));2091 }2092 2093 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);2094 2095 // (icmp eq/ne (or disjoint x, C0), C1)2096 // -> (icmp eq/ne x, C0^C1)2097 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&2098 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {2099 Value *NewC =2100 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));2101 return new ICmpInst(Pred, OrOp0, NewC);2102 }2103 2104 const APInt *MaskC;2105 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {2106 if (*MaskC == C && (C + 1).isPowerOf2()) {2107 // X | C == C --> X <=u C2108 // X | C != C --> X >u C2109 // iff C+1 is a power of 2 (C is a bitmask of the low bits)2110 Pred = (Pred == CmpInst::ICMP_EQ) ? CmpInst::ICMP_ULE : CmpInst::ICMP_UGT;2111 return new ICmpInst(Pred, OrOp0, OrOp1);2112 }2113 2114 // More general: canonicalize 'equality with set bits mask' to2115 // 'equality with clear bits mask'.2116 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC2117 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC2118 if (Or->hasOneUse()) {2119 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));2120 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));2121 return new ICmpInst(Pred, And, NewC);2122 }2123 }2124 2125 // (X | (X-1)) s< 0 --> X s< 12126 // (X | (X-1)) s> -1 --> X s> 02127 Value *X;2128 bool TrueIfSigned;2129 if (isSignBitCheck(Pred, C, TrueIfSigned) &&2130 match(Or, m_c_Or(m_Add(m_Value(X), m_AllOnes()), m_Deferred(X)))) {2131 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;2132 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);2133 return new ICmpInst(NewPred, X, NewC);2134 }2135 2136 const APInt *OrC;2137 // icmp(X | OrC, C) --> icmp(X, 0)2138 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {2139 switch (Pred) {2140 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 02141 case ICmpInst::ICMP_SLT:2142 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 02143 case ICmpInst::ICMP_SGE:2144 if (OrC->sge(C))2145 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));2146 break;2147 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 02148 case ICmpInst::ICMP_SLE:2149 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 02150 case ICmpInst::ICMP_SGT:2151 if (OrC->sgt(C))2152 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(Pred), X,2153 ConstantInt::getNullValue(X->getType()));2154 break;2155 default:2156 break;2157 }2158 }2159 2160 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())2161 return nullptr;2162 2163 Value *P, *Q;2164 if (match(Or, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {2165 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 02166 // -> and (icmp eq P, null), (icmp eq Q, null).2167 Value *CmpP =2168 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));2169 Value *CmpQ =2170 Builder.CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType()));2171 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;2172 return BinaryOperator::Create(BOpc, CmpP, CmpQ);2173 }2174 2175 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))2176 return replaceInstUsesWith(Cmp, V);2177 2178 return nullptr;2179}2180 2181/// Fold icmp (mul X, Y), C.2182Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,2183 BinaryOperator *Mul,2184 const APInt &C) {2185 ICmpInst::Predicate Pred = Cmp.getPredicate();2186 Type *MulTy = Mul->getType();2187 Value *X = Mul->getOperand(0);2188 2189 // If there's no overflow:2190 // X * X == 0 --> X == 02191 // X * X != 0 --> X != 02192 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&2193 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))2194 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));2195 2196 const APInt *MulC;2197 if (!match(Mul->getOperand(1), m_APInt(MulC)))2198 return nullptr;2199 2200 // If this is a test of the sign bit and the multiply is sign-preserving with2201 // a constant operand, use the multiply LHS operand instead:2202 // (X * +MulC) < 0 --> X < 02203 // (X * -MulC) < 0 --> X > 02204 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {2205 if (MulC->isNegative())2206 Pred = ICmpInst::getSwappedPredicate(Pred);2207 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));2208 }2209 2210 if (MulC->isZero())2211 return nullptr;2212 2213 // If the multiply does not wrap or the constant is odd, try to divide the2214 // compare constant by the multiplication factor.2215 if (Cmp.isEquality()) {2216 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC2217 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {2218 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));2219 return new ICmpInst(Pred, X, NewC);2220 }2221 2222 // C % MulC == 0 is weaker than we could use if MulC is odd because it2223 // correct to transform if MulC * N == C including overflow. I.e with i82224 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we2225 // miss that case.2226 if (C.urem(*MulC).isZero()) {2227 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC2228 // (mul X, OddC) eq/ne N * C --> X eq/ne N2229 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {2230 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));2231 return new ICmpInst(Pred, X, NewC);2232 }2233 }2234 }2235 2236 // With a matching no-overflow guarantee, fold the constants:2237 // (X * MulC) < C --> X < (C / MulC)2238 // (X * MulC) > C --> X > (C / MulC)2239 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?2240 Constant *NewC = nullptr;2241 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {2242 // MININT / -1 --> overflow.2243 if (C.isMinSignedValue() && MulC->isAllOnes())2244 return nullptr;2245 if (MulC->isNegative())2246 Pred = ICmpInst::getSwappedPredicate(Pred);2247 2248 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {2249 NewC = ConstantInt::get(2250 MulTy, APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::UP));2251 } else {2252 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&2253 "Unexpected predicate");2254 NewC = ConstantInt::get(2255 MulTy, APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::DOWN));2256 }2257 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {2258 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {2259 NewC = ConstantInt::get(2260 MulTy, APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::UP));2261 } else {2262 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&2263 "Unexpected predicate");2264 NewC = ConstantInt::get(2265 MulTy, APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::DOWN));2266 }2267 }2268 2269 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;2270}2271 2272/// Fold icmp (shl nuw C2, Y), C.2273static Instruction *foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl,2274 const APInt &C) {2275 Value *Y;2276 const APInt *C2;2277 if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y))))2278 return nullptr;2279 2280 Type *ShiftType = Shl->getType();2281 unsigned TypeBits = C.getBitWidth();2282 ICmpInst::Predicate Pred = Cmp.getPredicate();2283 if (Cmp.isUnsigned()) {2284 if (C2->isZero() || C2->ugt(C))2285 return nullptr;2286 APInt Div, Rem;2287 APInt::udivrem(C, *C2, Div, Rem);2288 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();2289 2290 // (1 << Y) pred C -> Y pred Log2(C)2291 if (!CIsPowerOf2) {2292 // (1 << Y) < 30 -> Y <= 42293 // (1 << Y) <= 30 -> Y <= 42294 // (1 << Y) >= 30 -> Y > 42295 // (1 << Y) > 30 -> Y > 42296 if (Pred == ICmpInst::ICMP_ULT)2297 Pred = ICmpInst::ICMP_ULE;2298 else if (Pred == ICmpInst::ICMP_UGE)2299 Pred = ICmpInst::ICMP_UGT;2300 }2301 2302 unsigned CLog2 = Div.logBase2();2303 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));2304 } else if (Cmp.isSigned() && C2->isOne()) {2305 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);2306 // (1 << Y) > 0 -> Y != 312307 // (1 << Y) > C -> Y != 31 if C is negative.2308 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))2309 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);2310 2311 // (1 << Y) < 0 -> Y == 312312 // (1 << Y) < 1 -> Y == 312313 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.2314 // Exclude signed min by subtracting 1 and lower the upper bound to 0.2315 if (Pred == ICmpInst::ICMP_SLT && (C - 1).sle(0))2316 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);2317 }2318 2319 return nullptr;2320}2321 2322/// Fold icmp (shl X, Y), C.2323Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp,2324 BinaryOperator *Shl,2325 const APInt &C) {2326 const APInt *ShiftVal;2327 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))2328 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);2329 2330 ICmpInst::Predicate Pred = Cmp.getPredicate();2331 // (icmp pred (shl nuw&nsw X, Y), Csle0)2332 // -> (icmp pred X, Csle0)2333 //2334 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op2335 // so X's must be what is used.2336 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())2337 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));2338 2339 // (icmp eq/ne (shl nuw|nsw X, Y), 0)2340 // -> (icmp eq/ne X, 0)2341 if (ICmpInst::isEquality(Pred) && C.isZero() &&2342 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))2343 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));2344 2345 // (icmp slt (shl nsw X, Y), 0/1)2346 // -> (icmp slt X, 0/1)2347 // (icmp sgt (shl nsw X, Y), 0/-1)2348 // -> (icmp sgt X, 0/-1)2349 //2350 // NB: sge/sle with a constant will canonicalize to sgt/slt.2351 if (Shl->hasNoSignedWrap() &&2352 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))2353 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))2354 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));2355 2356 const APInt *ShiftAmt;2357 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))2358 return foldICmpShlLHSC(Cmp, Shl, C);2359 2360 // Check that the shift amount is in range. If not, don't perform undefined2361 // shifts. When the shift is visited, it will be simplified.2362 unsigned TypeBits = C.getBitWidth();2363 if (ShiftAmt->uge(TypeBits))2364 return nullptr;2365 2366 Value *X = Shl->getOperand(0);2367 Type *ShType = Shl->getType();2368 2369 // NSW guarantees that we are only shifting out sign bits from the high bits,2370 // so we can ASHR the compare constant without needing a mask and eliminate2371 // the shift.2372 if (Shl->hasNoSignedWrap()) {2373 if (Pred == ICmpInst::ICMP_SGT) {2374 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)2375 APInt ShiftedC = C.ashr(*ShiftAmt);2376 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));2377 }2378 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&2379 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {2380 APInt ShiftedC = C.ashr(*ShiftAmt);2381 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));2382 }2383 if (Pred == ICmpInst::ICMP_SLT) {2384 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:2385 // (X << S) <=s C is equiv to X <=s (C >> S) for all C2386 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX2387 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN2388 assert(!C.isMinSignedValue() && "Unexpected icmp slt");2389 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;2390 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));2391 }2392 }2393 2394 // NUW guarantees that we are only shifting out zero bits from the high bits,2395 // so we can LSHR the compare constant without needing a mask and eliminate2396 // the shift.2397 if (Shl->hasNoUnsignedWrap()) {2398 if (Pred == ICmpInst::ICMP_UGT) {2399 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)2400 APInt ShiftedC = C.lshr(*ShiftAmt);2401 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));2402 }2403 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&2404 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {2405 APInt ShiftedC = C.lshr(*ShiftAmt);2406 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));2407 }2408 if (Pred == ICmpInst::ICMP_ULT) {2409 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:2410 // (X << S) <=u C is equiv to X <=u (C >> S) for all C2411 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u2412 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 02413 assert(C.ugt(0) && "ult 0 should have been eliminated");2414 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;2415 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));2416 }2417 }2418 2419 if (Cmp.isEquality() && Shl->hasOneUse()) {2420 // Strength-reduce the shift into an 'and'.2421 Constant *Mask = ConstantInt::get(2422 ShType,2423 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));2424 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");2425 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));2426 return new ICmpInst(Pred, And, LShrC);2427 }2428 2429 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.2430 bool TrueIfSigned = false;2431 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {2432 // (X << 31) <s 0 --> (X & 1) != 02433 Constant *Mask = ConstantInt::get(2434 ShType,2435 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));2436 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");2437 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,2438 And, Constant::getNullValue(ShType));2439 }2440 2441 // Simplify 'shl' inequality test into 'and' equality test.2442 if (Cmp.isUnsigned() && Shl->hasOneUse()) {2443 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 02444 if ((C + 1).isPowerOf2() &&2445 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {2446 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));2447 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ2448 : ICmpInst::ICMP_NE,2449 And, Constant::getNullValue(ShType));2450 }2451 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 02452 if (C.isPowerOf2() &&2453 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {2454 Value *And =2455 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));2456 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ2457 : ICmpInst::ICMP_NE,2458 And, Constant::getNullValue(ShType));2459 }2460 }2461 2462 // Transform (icmp pred iM (shl iM %v, N), C)2463 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))2464 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.2465 // This enables us to get rid of the shift in favor of a trunc that may be2466 // free on the target. It has the additional benefit of comparing to a2467 // smaller constant that may be more target-friendly.2468 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);2469 if (Shl->hasOneUse() && Amt != 0 &&2470 shouldChangeType(ShType->getScalarSizeInBits(), TypeBits - Amt)) {2471 ICmpInst::Predicate CmpPred = Pred;2472 APInt RHSC = C;2473 2474 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(CmpPred)) {2475 // Try the flipped strictness predicate.2476 // e.g.:2477 // icmp ult i64 (shl X, 32), 8589934593 ->2478 // icmp ule i64 (shl X, 32), 8589934592 ->2479 // icmp ule i32 (trunc X, i32), 2 ->2480 // icmp ult i32 (trunc X, i32), 32481 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(2482 Pred, ConstantInt::get(ShType->getContext(), C))) {2483 CmpPred = FlippedStrictness->first;2484 RHSC = cast<ConstantInt>(FlippedStrictness->second)->getValue();2485 }2486 }2487 2488 if (RHSC.countr_zero() >= Amt) {2489 Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt);2490 Constant *NewC =2491 ConstantInt::get(TruncTy, RHSC.ashr(*ShiftAmt).trunc(TypeBits - Amt));2492 return new ICmpInst(CmpPred,2493 Builder.CreateTrunc(X, TruncTy, "", /*IsNUW=*/false,2494 Shl->hasNoSignedWrap()),2495 NewC);2496 }2497 }2498 2499 return nullptr;2500}2501 2502/// Fold icmp ({al}shr X, Y), C.2503Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,2504 BinaryOperator *Shr,2505 const APInt &C) {2506 // An exact shr only shifts out zero bits, so:2507 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 02508 Value *X = Shr->getOperand(0);2509 CmpInst::Predicate Pred = Cmp.getPredicate();2510 if (Cmp.isEquality() && Shr->isExact() && C.isZero())2511 return new ICmpInst(Pred, X, Cmp.getOperand(1));2512 2513 bool IsAShr = Shr->getOpcode() == Instruction::AShr;2514 const APInt *ShiftValC;2515 if (match(X, m_APInt(ShiftValC))) {2516 if (Cmp.isEquality())2517 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);2518 2519 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 02520 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 02521 bool TrueIfSigned;2522 if (!IsAShr && ShiftValC->isNegative() &&2523 isSignBitCheck(Pred, C, TrueIfSigned))2524 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,2525 Shr->getOperand(1),2526 ConstantInt::getNullValue(X->getType()));2527 2528 // If the shifted constant is a power-of-2, test the shift amount directly:2529 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))2530 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))2531 if (!IsAShr && ShiftValC->isPowerOf2() &&2532 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {2533 bool IsUGT = Pred == CmpInst::ICMP_UGT;2534 assert(ShiftValC->uge(C) && "Expected simplify of compare");2535 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");2536 2537 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();2538 unsigned ShiftLZ = ShiftValC->countl_zero();2539 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);2540 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;2541 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);2542 }2543 }2544 2545 const APInt *ShiftAmtC;2546 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))2547 return nullptr;2548 2549 // Check that the shift amount is in range. If not, don't perform undefined2550 // shifts. When the shift is visited it will be simplified.2551 unsigned TypeBits = C.getBitWidth();2552 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);2553 if (ShAmtVal >= TypeBits || ShAmtVal == 0)2554 return nullptr;2555 2556 bool IsExact = Shr->isExact();2557 Type *ShrTy = Shr->getType();2558 // TODO: If we could guarantee that InstSimplify would handle all of the2559 // constant-value-based preconditions in the folds below, then we could assert2560 // those conditions rather than checking them. This is difficult because of2561 // undef/poison (PR34838).2562 if (IsAShr && Shr->hasOneUse()) {2563 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&2564 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {2565 // When C - 1 is a power of two and the transform can be legally2566 // performed, prefer this form so the produced constant is close to a2567 // power of two.2568 // icmp slt/ult (ashr exact X, ShAmtC), C2569 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 12570 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;2571 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));2572 }2573 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {2574 // When ShAmtC can be shifted losslessly:2575 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)2576 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)2577 APInt ShiftedC = C.shl(ShAmtVal);2578 if (ShiftedC.ashr(ShAmtVal) == C)2579 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));2580 }2581 if (Pred == CmpInst::ICMP_SGT) {2582 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 12583 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;2584 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&2585 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))2586 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));2587 }2588 if (Pred == CmpInst::ICMP_UGT) {2589 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 12590 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd2591 // clause accounts for that pattern.2592 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;2593 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||2594 (C + 1).shl(ShAmtVal).isMinSignedValue())2595 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));2596 }2597 2598 // If the compare constant has significant bits above the lowest sign-bit,2599 // then convert an unsigned cmp to a test of the sign-bit:2600 // (ashr X, ShiftC) u> C --> X s< 02601 // (ashr X, ShiftC) u< C --> X s> -12602 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {2603 if (Pred == CmpInst::ICMP_UGT) {2604 return new ICmpInst(CmpInst::ICMP_SLT, X,2605 ConstantInt::getNullValue(ShrTy));2606 }2607 if (Pred == CmpInst::ICMP_ULT) {2608 return new ICmpInst(CmpInst::ICMP_SGT, X,2609 ConstantInt::getAllOnesValue(ShrTy));2610 }2611 }2612 } else if (!IsAShr) {2613 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {2614 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)2615 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)2616 APInt ShiftedC = C.shl(ShAmtVal);2617 if (ShiftedC.lshr(ShAmtVal) == C)2618 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));2619 }2620 if (Pred == CmpInst::ICMP_UGT) {2621 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 12622 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;2623 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))2624 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));2625 }2626 }2627 2628 if (!Cmp.isEquality())2629 return nullptr;2630 2631 // Handle equality comparisons of shift-by-constant.2632 2633 // If the comparison constant changes with the shift, the comparison cannot2634 // succeed (bits of the comparison constant cannot match the shifted value).2635 // This should be known by InstSimplify and already be folded to true/false.2636 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||2637 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&2638 "Expected icmp+shr simplify did not occur.");2639 2640 // If the bits shifted out are known zero, compare the unshifted value:2641 // (X & 4) >> 1 == 2 --> (X & 4) == 4.2642 if (Shr->isExact())2643 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));2644 2645 if (Shr->hasOneUse()) {2646 // Canonicalize the shift into an 'and':2647 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)2648 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));2649 Constant *Mask = ConstantInt::get(ShrTy, Val);2650 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");2651 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));2652 }2653 2654 return nullptr;2655}2656 2657Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,2658 BinaryOperator *SRem,2659 const APInt &C) {2660 const ICmpInst::Predicate Pred = Cmp.getPredicate();2661 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {2662 // Canonicalize unsigned predicates to signed:2663 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 02664 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-12665 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -12666 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-12667 2668 const APInt *DivisorC;2669 if (!match(SRem->getOperand(1), m_APInt(DivisorC)))2670 return nullptr;2671 2672 APInt NormalizedC = C;2673 if (Pred == ICmpInst::ICMP_ULT) {2674 assert(!NormalizedC.isZero() &&2675 "ult X, 0 should have been simplified already.");2676 --NormalizedC;2677 }2678 if (C.isNegative())2679 NormalizedC.flipAllBits();2680 assert(!DivisorC->isZero() &&2681 "srem X, 0 should have been simplified already.");2682 if (!NormalizedC.uge(DivisorC->abs() - 1))2683 return nullptr;2684 2685 Type *Ty = SRem->getType();2686 if (Pred == ICmpInst::ICMP_UGT)2687 return new ICmpInst(ICmpInst::ICMP_SLT, SRem,2688 ConstantInt::getNullValue(Ty));2689 return new ICmpInst(ICmpInst::ICMP_SGT, SRem,2690 ConstantInt::getAllOnesValue(Ty));2691 }2692 // Match an 'is positive' or 'is negative' comparison of remainder by a2693 // constant power-of-2 value:2694 // (X % pow2C) sgt/slt 02695 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&2696 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)2697 return nullptr;2698 2699 // TODO: The one-use check is standard because we do not typically want to2700 // create longer instruction sequences, but this might be a special-case2701 // because srem is not good for analysis or codegen.2702 if (!SRem->hasOneUse())2703 return nullptr;2704 2705 const APInt *DivisorC;2706 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))2707 return nullptr;2708 2709 // For cmp_sgt/cmp_slt only zero valued C is handled.2710 // For cmp_eq/cmp_ne only positive valued C is handled.2711 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&2712 !C.isZero()) ||2713 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&2714 !C.isStrictlyPositive()))2715 return nullptr;2716 2717 // Mask off the sign bit and the modulo bits (low-bits).2718 Type *Ty = SRem->getType();2719 APInt SignMask = APInt::getSignMask(Ty->getScalarSizeInBits());2720 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));2721 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);2722 2723 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)2724 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));2725 2726 // For 'is positive?' check that the sign-bit is clear and at least 1 masked2727 // bit is set. Example:2728 // (i8 X % 32) s> 0 --> (X & 159) s> 02729 if (Pred == ICmpInst::ICMP_SGT)2730 return new ICmpInst(ICmpInst::ICMP_SGT, And, ConstantInt::getNullValue(Ty));2731 2732 // For 'is negative?' check that the sign-bit is set and at least 1 masked2733 // bit is set. Example:2734 // (i16 X % 4) s< 0 --> (X & 32771) u> 327682735 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));2736}2737 2738/// Fold icmp (udiv X, Y), C.2739Instruction *InstCombinerImpl::foldICmpUDivConstant(ICmpInst &Cmp,2740 BinaryOperator *UDiv,2741 const APInt &C) {2742 ICmpInst::Predicate Pred = Cmp.getPredicate();2743 Value *X = UDiv->getOperand(0);2744 Value *Y = UDiv->getOperand(1);2745 Type *Ty = UDiv->getType();2746 2747 const APInt *C2;2748 if (!match(X, m_APInt(C2)))2749 return nullptr;2750 2751 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");2752 2753 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))2754 if (Pred == ICmpInst::ICMP_UGT) {2755 assert(!C.isMaxValue() &&2756 "icmp ugt X, UINT_MAX should have been simplified already.");2757 return new ICmpInst(ICmpInst::ICMP_ULE, Y,2758 ConstantInt::get(Ty, C2->udiv(C + 1)));2759 }2760 2761 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)2762 if (Pred == ICmpInst::ICMP_ULT) {2763 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");2764 return new ICmpInst(ICmpInst::ICMP_UGT, Y,2765 ConstantInt::get(Ty, C2->udiv(C)));2766 }2767 2768 return nullptr;2769}2770 2771/// Fold icmp ({su}div X, Y), C.2772Instruction *InstCombinerImpl::foldICmpDivConstant(ICmpInst &Cmp,2773 BinaryOperator *Div,2774 const APInt &C) {2775 ICmpInst::Predicate Pred = Cmp.getPredicate();2776 Value *X = Div->getOperand(0);2777 Value *Y = Div->getOperand(1);2778 Type *Ty = Div->getType();2779 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;2780 2781 // If unsigned division and the compare constant is bigger than2782 // UMAX/2 (negative), there's only one pair of values that satisfies an2783 // equality check, so eliminate the division:2784 // (X u/ Y) == C --> (X == C) && (Y == 1)2785 // (X u/ Y) != C --> (X != C) || (Y != 1)2786 // Similarly, if signed division and the compare constant is exactly SMIN:2787 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)2788 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)2789 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&2790 (!DivIsSigned || C.isMinSignedValue())) {2791 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));2792 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));2793 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;2794 return BinaryOperator::Create(Logic, XBig, YOne);2795 }2796 2797 // Fold: icmp pred ([us]div X, C2), C -> range test2798 // Fold this div into the comparison, producing a range check.2799 // Determine, based on the divide type, what the range is being2800 // checked. If there is an overflow on the low or high side, remember2801 // it, otherwise compute the range [low, hi) bounding the new value.2802 // See: InsertRangeTest above for the kinds of replacements possible.2803 const APInt *C2;2804 if (!match(Y, m_APInt(C2)))2805 return nullptr;2806 2807 // FIXME: If the operand types don't match the type of the divide2808 // then don't attempt this transform. The code below doesn't have the2809 // logic to deal with a signed divide and an unsigned compare (and2810 // vice versa). This is because (x /s C2) <s C produces different2811 // results than (x /s C2) <u C or (x /u C2) <s C or even2812 // (x /u C2) <u C. Simply casting the operands and result won't2813 // work. :( The if statement below tests that condition and bails2814 // if it finds it.2815 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())2816 return nullptr;2817 2818 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with2819 // INT_MIN will also fail if the divisor is 1. Although folds of all these2820 // division-by-constant cases should be present, we can not assert that they2821 // have happened before we reach this icmp instruction.2822 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))2823 return nullptr;2824 2825 // Compute Prod = C * C2. We are essentially solving an equation of2826 // form X / C2 = C. We solve for X by multiplying C2 and C.2827 // By solving for X, we can turn this into a range check instead of computing2828 // a divide.2829 APInt Prod = C * *C2;2830 2831 // Determine if the product overflows by seeing if the product is not equal to2832 // the divide. Make sure we do the same kind of divide as in the LHS2833 // instruction that we're folding.2834 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;2835 2836 // If the division is known to be exact, then there is no remainder from the2837 // divide, so the covered range size is unit, otherwise it is the divisor.2838 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;2839 2840 // Figure out the interval that is being checked. For example, a comparison2841 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).2842 // Compute this interval based on the constants involved and the signedness of2843 // the compare/divide. This computes a half-open interval, keeping track of2844 // whether either value in the interval overflows. After analysis each2845 // overflow variable is set to 0 if it's corresponding bound variable is valid2846 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.2847 int LoOverflow = 0, HiOverflow = 0;2848 APInt LoBound, HiBound;2849 2850 if (!DivIsSigned) { // udiv2851 // e.g. X/5 op 3 --> [15, 20)2852 LoBound = Prod;2853 HiOverflow = LoOverflow = ProdOV;2854 if (!HiOverflow) {2855 // If this is not an exact divide, then many values in the range collapse2856 // to the same result value.2857 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);2858 }2859 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.2860 if (C.isZero()) { // (X / pos) op 02861 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)2862 LoBound = -(RangeSize - 1);2863 HiBound = RangeSize;2864 } else if (C.isStrictlyPositive()) { // (X / pos) op pos2865 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)2866 HiOverflow = LoOverflow = ProdOV;2867 if (!HiOverflow)2868 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);2869 } else { // (X / pos) op neg2870 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)2871 HiBound = Prod + 1;2872 LoOverflow = HiOverflow = ProdOV ? -1 : 0;2873 if (!LoOverflow) {2874 APInt DivNeg = -RangeSize;2875 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;2876 }2877 }2878 } else if (C2->isNegative()) { // Divisor is < 0.2879 if (Div->isExact())2880 RangeSize.negate();2881 if (C.isZero()) { // (X / neg) op 02882 // e.g. X/-5 op 0 --> [-4, 5)2883 LoBound = RangeSize + 1;2884 HiBound = -RangeSize;2885 if (HiBound == *C2) { // -INTMIN = INTMIN2886 HiOverflow = 1; // [INTMIN+1, overflow)2887 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN2888 }2889 } else if (C.isStrictlyPositive()) { // (X / neg) op pos2890 // e.g. X/-5 op 3 --> [-19, -14)2891 HiBound = Prod + 1;2892 HiOverflow = LoOverflow = ProdOV ? -1 : 0;2893 if (!LoOverflow)2894 LoOverflow =2895 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;2896 } else { // (X / neg) op neg2897 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)2898 LoOverflow = HiOverflow = ProdOV;2899 if (!HiOverflow)2900 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);2901 }2902 2903 // Dividing by a negative swaps the condition. LT <-> GT2904 Pred = ICmpInst::getSwappedPredicate(Pred);2905 }2906 2907 switch (Pred) {2908 default:2909 llvm_unreachable("Unhandled icmp predicate!");2910 case ICmpInst::ICMP_EQ:2911 if (LoOverflow && HiOverflow)2912 return replaceInstUsesWith(Cmp, Builder.getFalse());2913 if (HiOverflow)2914 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,2915 X, ConstantInt::get(Ty, LoBound));2916 if (LoOverflow)2917 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,2918 X, ConstantInt::get(Ty, HiBound));2919 return replaceInstUsesWith(2920 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));2921 case ICmpInst::ICMP_NE:2922 if (LoOverflow && HiOverflow)2923 return replaceInstUsesWith(Cmp, Builder.getTrue());2924 if (HiOverflow)2925 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,2926 X, ConstantInt::get(Ty, LoBound));2927 if (LoOverflow)2928 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,2929 X, ConstantInt::get(Ty, HiBound));2930 return replaceInstUsesWith(2931 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));2932 case ICmpInst::ICMP_ULT:2933 case ICmpInst::ICMP_SLT:2934 if (LoOverflow == +1) // Low bound is greater than input range.2935 return replaceInstUsesWith(Cmp, Builder.getTrue());2936 if (LoOverflow == -1) // Low bound is less than input range.2937 return replaceInstUsesWith(Cmp, Builder.getFalse());2938 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));2939 case ICmpInst::ICMP_UGT:2940 case ICmpInst::ICMP_SGT:2941 if (HiOverflow == +1) // High bound greater than input range.2942 return replaceInstUsesWith(Cmp, Builder.getFalse());2943 if (HiOverflow == -1) // High bound less than input range.2944 return replaceInstUsesWith(Cmp, Builder.getTrue());2945 if (Pred == ICmpInst::ICMP_UGT)2946 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));2947 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));2948 }2949 2950 return nullptr;2951}2952 2953/// Fold icmp (sub X, Y), C.2954Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,2955 BinaryOperator *Sub,2956 const APInt &C) {2957 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);2958 ICmpInst::Predicate Pred = Cmp.getPredicate();2959 Type *Ty = Sub->getType();2960 2961 // (SubC - Y) == C) --> Y == (SubC - C)2962 // (SubC - Y) != C) --> Y != (SubC - C)2963 Constant *SubC;2964 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {2965 return new ICmpInst(Pred, Y,2966 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));2967 }2968 2969 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)2970 const APInt *C2;2971 APInt SubResult;2972 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();2973 bool HasNSW = Sub->hasNoSignedWrap();2974 bool HasNUW = Sub->hasNoUnsignedWrap();2975 if (match(X, m_APInt(C2)) &&2976 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&2977 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))2978 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));2979 2980 // X - Y == 0 --> X == Y.2981 // X - Y != 0 --> X != Y.2982 // TODO: We allow this with multiple uses as long as the other uses are not2983 // in phis. The phi use check is guarding against a codegen regression2984 // for a loop test. If the backend could undo this (and possibly2985 // subsequent transforms), we would not need this hack.2986 if (Cmp.isEquality() && C.isZero() &&2987 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))2988 return new ICmpInst(Pred, X, Y);2989 2990 // The following transforms are only worth it if the only user of the subtract2991 // is the icmp.2992 // TODO: This is an artificial restriction for all of the transforms below2993 // that only need a single replacement icmp. Can these use the phi test2994 // like the transform above here?2995 if (!Sub->hasOneUse())2996 return nullptr;2997 2998 if (Sub->hasNoSignedWrap()) {2999 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)3000 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())3001 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);3002 3003 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)3004 if (Pred == ICmpInst::ICMP_SGT && C.isZero())3005 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);3006 3007 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)3008 if (Pred == ICmpInst::ICMP_SLT && C.isZero())3009 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);3010 3011 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)3012 if (Pred == ICmpInst::ICMP_SLT && C.isOne())3013 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);3014 }3015 3016 if (!match(X, m_APInt(C2)))3017 return nullptr;3018 3019 // C2 - Y <u C -> (Y | (C - 1)) == C23020 // iff (C2 & (C - 1)) == C - 1 and C is a power of 23021 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&3022 (*C2 & (C - 1)) == (C - 1))3023 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);3024 3025 // C2 - Y >u C -> (Y | C) != C23026 // iff C2 & C == C and C + 1 is a power of 23027 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)3028 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);3029 3030 // We have handled special cases that reduce.3031 // Canonicalize any remaining sub to add as:3032 // (C2 - Y) > C --> (Y + ~C2) < ~C3033 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",3034 HasNUW, HasNSW);3035 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));3036}3037 3038static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,3039 Value *Op1, IRBuilderBase &Builder,3040 bool HasOneUse) {3041 auto FoldConstant = [&](bool Val) {3042 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();3043 if (Op0->getType()->isVectorTy())3044 Res = ConstantVector::getSplat(3045 cast<VectorType>(Op0->getType())->getElementCount(), Res);3046 return Res;3047 };3048 3049 switch (Table.to_ulong()) {3050 case 0: // 0 0 0 03051 return FoldConstant(false);3052 case 1: // 0 0 0 13053 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;3054 case 2: // 0 0 1 03055 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;3056 case 3: // 0 0 1 13057 return Builder.CreateNot(Op0);3058 case 4: // 0 1 0 03059 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;3060 case 5: // 0 1 0 13061 return Builder.CreateNot(Op1);3062 case 6: // 0 1 1 03063 return Builder.CreateXor(Op0, Op1);3064 case 7: // 0 1 1 13065 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;3066 case 8: // 1 0 0 03067 return Builder.CreateAnd(Op0, Op1);3068 case 9: // 1 0 0 13069 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;3070 case 10: // 1 0 1 03071 return Op1;3072 case 11: // 1 0 1 13073 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;3074 case 12: // 1 1 0 03075 return Op0;3076 case 13: // 1 1 0 13077 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;3078 case 14: // 1 1 1 03079 return Builder.CreateOr(Op0, Op1);3080 case 15: // 1 1 1 13081 return FoldConstant(true);3082 default:3083 llvm_unreachable("Invalid Operation");3084 }3085 return nullptr;3086}3087 3088Instruction *InstCombinerImpl::foldICmpBinOpWithConstantViaTruthTable(3089 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {3090 Value *A, *B;3091 Constant *C1, *C2, *C3, *C4;3092 if (!(match(BO->getOperand(0),3093 m_Select(m_Value(A), m_Constant(C1), m_Constant(C2)))) ||3094 !match(BO->getOperand(1),3095 m_Select(m_Value(B), m_Constant(C3), m_Constant(C4))) ||3096 Cmp.getType() != A->getType())3097 return nullptr;3098 3099 std::bitset<4> Table;3100 auto ComputeTable = [&](bool First, bool Second) -> std::optional<bool> {3101 Constant *L = First ? C1 : C2;3102 Constant *R = Second ? C3 : C4;3103 if (auto *Res = ConstantFoldBinaryOpOperands(BO->getOpcode(), L, R, DL)) {3104 auto *Val = Res->getType()->isVectorTy() ? Res->getSplatValue() : Res;3105 if (auto *CI = dyn_cast_or_null<ConstantInt>(Val))3106 return ICmpInst::compare(CI->getValue(), C, Cmp.getPredicate());3107 }3108 return std::nullopt;3109 };3110 3111 for (unsigned I = 0; I < 4; ++I) {3112 bool First = (I >> 1) & 1;3113 bool Second = I & 1;3114 if (auto Res = ComputeTable(First, Second))3115 Table[I] = *Res;3116 else3117 return nullptr;3118 }3119 3120 // Synthesize optimal logic.3121 if (auto *Cond = createLogicFromTable(Table, A, B, Builder, BO->hasOneUse()))3122 return replaceInstUsesWith(Cmp, Cond);3123 return nullptr;3124}3125 3126/// Fold icmp (add X, Y), C.3127Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,3128 BinaryOperator *Add,3129 const APInt &C) {3130 Value *Y = Add->getOperand(1);3131 Value *X = Add->getOperand(0);3132 3133 Value *Op0, *Op1;3134 Instruction *Ext0, *Ext1;3135 const CmpInst::Predicate Pred = Cmp.getPredicate();3136 if (match(Add,3137 m_Add(m_CombineAnd(m_Instruction(Ext0), m_ZExtOrSExt(m_Value(Op0))),3138 m_CombineAnd(m_Instruction(Ext1),3139 m_ZExtOrSExt(m_Value(Op1))))) &&3140 Op0->getType()->isIntOrIntVectorTy(1) &&3141 Op1->getType()->isIntOrIntVectorTy(1)) {3142 unsigned BW = C.getBitWidth();3143 std::bitset<4> Table;3144 auto ComputeTable = [&](bool Op0Val, bool Op1Val) {3145 APInt Res(BW, 0);3146 if (Op0Val)3147 Res += APInt(BW, isa<ZExtInst>(Ext0) ? 1 : -1, /*isSigned=*/true);3148 if (Op1Val)3149 Res += APInt(BW, isa<ZExtInst>(Ext1) ? 1 : -1, /*isSigned=*/true);3150 return ICmpInst::compare(Res, C, Pred);3151 };3152 3153 Table[0] = ComputeTable(false, false);3154 Table[1] = ComputeTable(false, true);3155 Table[2] = ComputeTable(true, false);3156 Table[3] = ComputeTable(true, true);3157 if (auto *Cond =3158 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))3159 return replaceInstUsesWith(Cmp, Cond);3160 }3161 const APInt *C2;3162 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))3163 return nullptr;3164 3165 // Fold icmp pred (add X, C2), C.3166 Type *Ty = Add->getType();3167 3168 // If the add does not wrap, we can always adjust the compare by subtracting3169 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE3170 // are canonicalized to SGT/SLT/UGT/ULT.3171 if ((Add->hasNoSignedWrap() &&3172 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||3173 (Add->hasNoUnsignedWrap() &&3174 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {3175 bool Overflow;3176 APInt NewC =3177 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);3178 // If there is overflow, the result must be true or false.3179 // TODO: Can we assert there is no overflow because InstSimplify always3180 // handles those cases?3181 if (!Overflow)3182 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)3183 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));3184 }3185 3186 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&3187 C.isNonNegative() && (C - *C2).isNonNegative() &&3188 computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())3189 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,3190 ConstantInt::get(Ty, C - *C2));3191 3192 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);3193 const APInt &Upper = CR.getUpper();3194 const APInt &Lower = CR.getLower();3195 if (Cmp.isSigned()) {3196 if (Lower.isSignMask())3197 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));3198 if (Upper.isSignMask())3199 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));3200 } else {3201 if (Lower.isMinValue())3202 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));3203 if (Upper.isMinValue())3204 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));3205 }3206 3207 // This set of folds is intentionally placed after folds that use no-wrapping3208 // flags because those folds are likely better for later analysis/codegen.3209 const APInt SMax = APInt::getSignedMaxValue(Ty->getScalarSizeInBits());3210 const APInt SMin = APInt::getSignedMinValue(Ty->getScalarSizeInBits());3211 3212 // Fold compare with offset to opposite sign compare if it eliminates offset:3213 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)3214 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)3215 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));3216 3217 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)3218 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)3219 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));3220 3221 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)3222 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)3223 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));3224 3225 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)3226 if (Pred == CmpInst::ICMP_SLT && C == *C2)3227 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));3228 3229 // (X + -1) <u C --> X <=u C (if X is never null)3230 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {3231 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);3232 if (llvm::isKnownNonZero(X, Q))3233 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));3234 }3235 3236 if (!Add->hasOneUse())3237 return nullptr;3238 3239 // X+C <u C2 -> (X & -C2) == C3240 // iff C & (C2-1) == 03241 // C2 is a power of 23242 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)3243 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(X, -C),3244 ConstantExpr::getNeg(cast<Constant>(Y)));3245 3246 // X+C2 <u C -> (X & C) == 2C3247 // iff C == -(C2)3248 // C2 is a power of 23249 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)3250 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, C),3251 ConstantInt::get(Ty, C * 2));3252 3253 // X+C >u C2 -> (X & ~C2) != C3254 // iff C & C2 == 03255 // C2+1 is a power of 23256 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)3257 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, ~C),3258 ConstantExpr::getNeg(cast<Constant>(Y)));3259 3260 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize3261 // to the ult form.3262 // X+C2 >u C -> X+(C2-C-1) <u ~C3263 if (Pred == ICmpInst::ICMP_UGT)3264 return new ICmpInst(ICmpInst::ICMP_ULT,3265 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),3266 ConstantInt::get(Ty, ~C));3267 3268 // zext(V) + C2 pred C -> V + C3 pred' C43269 Value *V;3270 if (match(X, m_ZExt(m_Value(V)))) {3271 Type *NewCmpTy = V->getType();3272 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();3273 if (shouldChangeType(Ty, NewCmpTy)) {3274 ConstantRange SrcCR = CR.truncate(NewCmpBW, TruncInst::NoUnsignedWrap);3275 CmpInst::Predicate EquivPred;3276 APInt EquivInt;3277 APInt EquivOffset;3278 3279 SrcCR.getEquivalentICmp(EquivPred, EquivInt, EquivOffset);3280 return new ICmpInst(3281 EquivPred,3282 EquivOffset.isZero()3283 ? V3284 : Builder.CreateAdd(V, ConstantInt::get(NewCmpTy, EquivOffset)),3285 ConstantInt::get(NewCmpTy, EquivInt));3286 }3287 }3288 3289 return nullptr;3290}3291 3292bool InstCombinerImpl::matchThreeWayIntCompare(SelectInst *SI, Value *&LHS,3293 Value *&RHS, ConstantInt *&Less,3294 ConstantInt *&Equal,3295 ConstantInt *&Greater) {3296 // TODO: Generalize this to work with other comparison idioms or ensure3297 // they get canonicalized into this form.3298 3299 // select i1 (a == b),3300 // i32 Equal,3301 // i32 (select i1 (a < b), i32 Less, i32 Greater)3302 // where Equal, Less and Greater are placeholders for any three constants.3303 CmpPredicate PredA;3304 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||3305 !ICmpInst::isEquality(PredA))3306 return false;3307 Value *EqualVal = SI->getTrueValue();3308 Value *UnequalVal = SI->getFalseValue();3309 // We still can get non-canonical predicate here, so canonicalize.3310 if (PredA == ICmpInst::ICMP_NE)3311 std::swap(EqualVal, UnequalVal);3312 if (!match(EqualVal, m_ConstantInt(Equal)))3313 return false;3314 CmpPredicate PredB;3315 Value *LHS2, *RHS2;3316 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),3317 m_ConstantInt(Less), m_ConstantInt(Greater))))3318 return false;3319 // We can get predicate mismatch here, so canonicalize if possible:3320 // First, ensure that 'LHS' match.3321 if (LHS2 != LHS) {3322 // x sgt y <--> y slt x3323 std::swap(LHS2, RHS2);3324 PredB = ICmpInst::getSwappedPredicate(PredB);3325 }3326 if (LHS2 != LHS)3327 return false;3328 // We also need to canonicalize 'RHS'.3329 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {3330 // x sgt C-1 <--> x sge C <--> not(x slt C)3331 auto FlippedStrictness =3332 getFlippedStrictnessPredicateAndConstant(PredB, cast<Constant>(RHS2));3333 if (!FlippedStrictness)3334 return false;3335 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&3336 "basic correctness failure");3337 RHS2 = FlippedStrictness->second;3338 // And kind-of perform the result swap.3339 std::swap(Less, Greater);3340 PredB = ICmpInst::ICMP_SLT;3341 }3342 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;3343}3344 3345Instruction *InstCombinerImpl::foldICmpSelectConstant(ICmpInst &Cmp,3346 SelectInst *Select,3347 ConstantInt *C) {3348 3349 assert(C && "Cmp RHS should be a constant int!");3350 // If we're testing a constant value against the result of a three way3351 // comparison, the result can be expressed directly in terms of the3352 // original values being compared. Note: We could possibly be more3353 // aggressive here and remove the hasOneUse test. The original select is3354 // really likely to simplify or sink when we remove a test of the result.3355 Value *OrigLHS, *OrigRHS;3356 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;3357 if (Cmp.hasOneUse() &&3358 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,3359 C3GreaterThan)) {3360 assert(C1LessThan && C2Equal && C3GreaterThan);3361 3362 bool TrueWhenLessThan = ICmpInst::compare(3363 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate());3364 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(),3365 Cmp.getPredicate());3366 bool TrueWhenGreaterThan = ICmpInst::compare(3367 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate());3368 3369 // This generates the new instruction that will replace the original Cmp3370 // Instruction. Instead of enumerating the various combinations when3371 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus3372 // false, we rely on chaining of ORs and future passes of InstCombine to3373 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).3374 3375 // When none of the three constants satisfy the predicate for the RHS (C),3376 // the entire original Cmp can be simplified to a false.3377 Value *Cond = Builder.getFalse();3378 if (TrueWhenLessThan)3379 Cond = Builder.CreateOr(3380 Cond, Builder.CreateICmp(ICmpInst::ICMP_SLT, OrigLHS, OrigRHS));3381 if (TrueWhenEqual)3382 Cond = Builder.CreateOr(3383 Cond, Builder.CreateICmp(ICmpInst::ICMP_EQ, OrigLHS, OrigRHS));3384 if (TrueWhenGreaterThan)3385 Cond = Builder.CreateOr(3386 Cond, Builder.CreateICmp(ICmpInst::ICMP_SGT, OrigLHS, OrigRHS));3387 3388 return replaceInstUsesWith(Cmp, Cond);3389 }3390 return nullptr;3391}3392 3393Instruction *InstCombinerImpl::foldICmpBitCast(ICmpInst &Cmp) {3394 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));3395 if (!Bitcast)3396 return nullptr;3397 3398 ICmpInst::Predicate Pred = Cmp.getPredicate();3399 Value *Op1 = Cmp.getOperand(1);3400 Value *BCSrcOp = Bitcast->getOperand(0);3401 Type *SrcType = Bitcast->getSrcTy();3402 Type *DstType = Bitcast->getType();3403 3404 // Make sure the bitcast doesn't change between scalar and vector and3405 // doesn't change the number of vector elements.3406 if (SrcType->isVectorTy() == DstType->isVectorTy() &&3407 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {3408 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.3409 Value *X;3410 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {3411 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 03412 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 03413 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 03414 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 03415 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||3416 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&3417 match(Op1, m_Zero()))3418 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));3419 3420 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 13421 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))3422 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));3423 3424 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -13425 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))3426 return new ICmpInst(Pred, X,3427 ConstantInt::getAllOnesValue(X->getType()));3428 }3429 3430 // Zero-equality checks are preserved through unsigned floating-point casts:3431 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 03432 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 03433 if (match(BCSrcOp, m_UIToFP(m_Value(X))))3434 if (Cmp.isEquality() && match(Op1, m_Zero()))3435 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));3436 3437 const APInt *C;3438 bool TrueIfSigned;3439 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {3440 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate3441 // the FP extend/truncate because that cast does not change the sign-bit.3442 // This is true for all standard IEEE-754 types and the X86 80-bit type.3443 // The sign-bit is always the most significant bit in those types.3444 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&3445 (match(BCSrcOp, m_FPExt(m_Value(X))) ||3446 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {3447 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 03448 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -13449 Type *XType = X->getType();3450 3451 // We can't currently handle Power style floating point operations here.3452 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {3453 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());3454 if (auto *XVTy = dyn_cast<VectorType>(XType))3455 NewType = VectorType::get(NewType, XVTy->getElementCount());3456 Value *NewBitcast = Builder.CreateBitCast(X, NewType);3457 if (TrueIfSigned)3458 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,3459 ConstantInt::getNullValue(NewType));3460 else3461 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,3462 ConstantInt::getAllOnesValue(NewType));3463 }3464 }3465 3466 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)3467 Type *FPType = SrcType->getScalarType();3468 if (!Cmp.getParent()->getParent()->hasFnAttribute(3469 Attribute::NoImplicitFloat) &&3470 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {3471 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();3472 if (Mask & (fcInf | fcZero)) {3473 if (Pred == ICmpInst::ICMP_NE)3474 Mask = ~Mask;3475 return replaceInstUsesWith(Cmp,3476 Builder.createIsFPClass(BCSrcOp, Mask));3477 }3478 }3479 }3480 }3481 3482 const APInt *C;3483 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||3484 !SrcType->isIntOrIntVectorTy())3485 return nullptr;3486 3487 // If this is checking if all elements of a vector compare are set or not,3488 // invert the casted vector equality compare and test if all compare3489 // elements are clear or not. Compare against zero is generally easier for3490 // analysis and codegen.3491 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 03492 // Example: are all elements equal? --> are zero elements not equal?3493 // TODO: Try harder to reduce compare of 2 freely invertible operands?3494 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {3495 if (Value *NotBCSrcOp =3496 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {3497 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);3498 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));3499 }3500 }3501 3502 // If this is checking if all elements of an extended vector are clear or not,3503 // compare in a narrow type to eliminate the extend:3504 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 03505 Value *X;3506 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&3507 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {3508 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {3509 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());3510 Value *NewCast = Builder.CreateBitCast(X, NewType);3511 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));3512 }3513 }3514 3515 // Folding: icmp <pred> iN X, C3516 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN3517 // and C is a splat of a K-bit pattern3518 // and SC is a constant vector = <C', C', C', ..., C'>3519 // Into:3520 // %E = extractelement <M x iK> %vec, i32 C'3521 // icmp <pred> iK %E, trunc(C)3522 Value *Vec;3523 ArrayRef<int> Mask;3524 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {3525 // Check whether every element of Mask is the same constant3526 if (all_equal(Mask)) {3527 auto *VecTy = cast<VectorType>(SrcType);3528 auto *EltTy = cast<IntegerType>(VecTy->getElementType());3529 if (C->isSplat(EltTy->getBitWidth())) {3530 // Fold the icmp based on the value of C3531 // If C is M copies of an iK sized bit pattern,3532 // then:3533 // => %E = extractelement <N x iK> %vec, i32 Elem3534 // icmp <pred> iK %SplatVal, <pattern>3535 Value *Elem = Builder.getInt32(Mask[0]);3536 Value *Extract = Builder.CreateExtractElement(Vec, Elem);3537 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));3538 return new ICmpInst(Pred, Extract, NewC);3539 }3540 }3541 }3542 return nullptr;3543}3544 3545/// Try to fold integer comparisons with a constant operand: icmp Pred X, C3546/// where X is some kind of instruction.3547Instruction *InstCombinerImpl::foldICmpInstWithConstant(ICmpInst &Cmp) {3548 const APInt *C;3549 3550 if (match(Cmp.getOperand(1), m_APInt(C))) {3551 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))3552 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))3553 return I;3554 3555 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))3556 // For now, we only support constant integers while folding the3557 // ICMP(SELECT)) pattern. We can extend this to support vector of integers3558 // similar to the cases handled by binary ops above.3559 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))3560 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))3561 return I;3562 3563 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))3564 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))3565 return I;3566 3567 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))3568 if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, II, *C))3569 return I;3570 3571 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y3572 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y3573 // TODO: This checks one-use, but that is not strictly necessary.3574 Value *Cmp0 = Cmp.getOperand(0);3575 Value *X, *Y;3576 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&3577 (match(Cmp0,3578 m_ExtractValue<0>(m_Intrinsic<Intrinsic::ssub_with_overflow>(3579 m_Value(X), m_Value(Y)))) ||3580 match(Cmp0,3581 m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>(3582 m_Value(X), m_Value(Y))))))3583 return new ICmpInst(Cmp.getPredicate(), X, Y);3584 }3585 3586 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))3587 return foldICmpInstWithConstantAllowPoison(Cmp, *C);3588 3589 return nullptr;3590}3591 3592/// Fold an icmp equality instruction with binary operator LHS and constant RHS:3593/// icmp eq/ne BO, C.3594Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(3595 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {3596 // TODO: Some of these folds could work with arbitrary constants, but this3597 // function is limited to scalar and vector splat constants.3598 if (!Cmp.isEquality())3599 return nullptr;3600 3601 ICmpInst::Predicate Pred = Cmp.getPredicate();3602 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;3603 Constant *RHS = cast<Constant>(Cmp.getOperand(1));3604 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);3605 3606 switch (BO->getOpcode()) {3607 case Instruction::SRem:3608 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.3609 if (C.isZero() && BO->hasOneUse()) {3610 const APInt *BOC;3611 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {3612 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());3613 return new ICmpInst(Pred, NewRem,3614 Constant::getNullValue(BO->getType()));3615 }3616 }3617 break;3618 case Instruction::Add: {3619 // (A + C2) == C --> A == (C - C2)3620 // (A + C2) != C --> A != (C - C2)3621 // TODO: Remove the one-use limitation? See discussion in D58633.3622 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {3623 if (BO->hasOneUse())3624 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));3625 } else if (C.isZero()) {3626 // Replace ((add A, B) != 0) with (A != -B) if A or B is3627 // efficiently invertible, or if the add has just this one use.3628 if (Value *NegVal = dyn_castNegVal(BOp1))3629 return new ICmpInst(Pred, BOp0, NegVal);3630 if (Value *NegVal = dyn_castNegVal(BOp0))3631 return new ICmpInst(Pred, NegVal, BOp1);3632 if (BO->hasOneUse()) {3633 // (add nuw A, B) != 0 -> (or A, B) != 03634 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {3635 Value *Or = Builder.CreateOr(BOp0, BOp1);3636 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));3637 }3638 Value *Neg = Builder.CreateNeg(BOp1);3639 Neg->takeName(BO);3640 return new ICmpInst(Pred, BOp0, Neg);3641 }3642 }3643 break;3644 }3645 case Instruction::Xor:3646 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {3647 // For the xor case, we can xor two constants together, eliminating3648 // the explicit xor.3649 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));3650 } else if (C.isZero()) {3651 // Replace ((xor A, B) != 0) with (A != B)3652 return new ICmpInst(Pred, BOp0, BOp1);3653 }3654 break;3655 case Instruction::Or: {3656 const APInt *BOC;3657 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {3658 // Comparing if all bits outside of a constant mask are set?3659 // Replace (X | C) == -1 with (X & ~C) == ~C.3660 // This removes the -1 constant.3661 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));3662 Value *And = Builder.CreateAnd(BOp0, NotBOC);3663 return new ICmpInst(Pred, And, NotBOC);3664 }3665 // (icmp eq (or (select cond, 0, NonZero), Other), 0)3666 // -> (and cond, (icmp eq Other, 0))3667 // (icmp ne (or (select cond, NonZero, 0), Other), 0)3668 // -> (or cond, (icmp ne Other, 0))3669 Value *Cond, *TV, *FV, *Other, *Sel;3670 if (C.isZero() &&3671 match(BO,3672 m_OneUse(m_c_Or(m_CombineAnd(m_Value(Sel),3673 m_Select(m_Value(Cond), m_Value(TV),3674 m_Value(FV))),3675 m_Value(Other)))) &&3676 Cond->getType() == Cmp.getType()) {3677 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);3678 // Easy case is if eq/ne matches whether 0 is trueval/falseval.3679 if (Pred == ICmpInst::ICMP_EQ3680 ? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))3681 : (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {3682 Value *Cmp = Builder.CreateICmp(3683 Pred, Other, Constant::getNullValue(Other->getType()));3684 return BinaryOperator::Create(3685 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,3686 Cond);3687 }3688 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this3689 // case we need to invert the select condition so we need to be careful to3690 // avoid creating extra instructions.3691 // (icmp ne (or (select cond, 0, NonZero), Other), 0)3692 // -> (or (not cond), (icmp ne Other, 0))3693 // (icmp eq (or (select cond, NonZero, 0), Other), 0)3694 // -> (and (not cond), (icmp eq Other, 0))3695 //3696 // Only do this if the inner select has one use, in which case we are3697 // replacing `select` with `(not cond)`. Otherwise, we will create more3698 // uses. NB: Trying to freely invert cond doesn't make sense here, as if3699 // cond was freely invertable, the select arms would have been inverted.3700 if (Sel->hasOneUse() &&3701 (Pred == ICmpInst::ICMP_EQ3702 ? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))3703 : (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {3704 Value *NotCond = Builder.CreateNot(Cond);3705 Value *Cmp = Builder.CreateICmp(3706 Pred, Other, Constant::getNullValue(Other->getType()));3707 return BinaryOperator::Create(3708 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,3709 NotCond);3710 }3711 }3712 break;3713 }3714 case Instruction::UDiv:3715 case Instruction::SDiv:3716 if (BO->isExact()) {3717 // div exact X, Y eq/ne 0 -> X eq/ne 03718 // div exact X, Y eq/ne 1 -> X eq/ne Y3719 // div exact X, Y eq/ne C ->3720 // if Y * C never-overflow && OneUse:3721 // -> Y * C eq/ne X3722 if (C.isZero())3723 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));3724 else if (C.isOne())3725 return new ICmpInst(Pred, BOp0, BOp1);3726 else if (BO->hasOneUse()) {3727 OverflowResult OR = computeOverflow(3728 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,3729 Cmp.getOperand(1), BO);3730 if (OR == OverflowResult::NeverOverflows) {3731 Value *YC =3732 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));3733 return new ICmpInst(Pred, YC, BOp0);3734 }3735 }3736 }3737 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {3738 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)3739 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;3740 return new ICmpInst(NewPred, BOp1, BOp0);3741 }3742 break;3743 default:3744 break;3745 }3746 return nullptr;3747}3748 3749static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,3750 const APInt &CRhs,3751 InstCombiner::BuilderTy &Builder,3752 const SimplifyQuery &Q) {3753 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&3754 "Non-ctpop intrin in ctpop fold");3755 if (!CtpopLhs->hasOneUse())3756 return nullptr;3757 3758 // Power of 2 test:3759 // isPow2OrZero : ctpop(X) u< 23760 // isPow2 : ctpop(X) == 13761 // NotPow2OrZero: ctpop(X) u> 13762 // NotPow2 : ctpop(X) != 13763 // If we know any bit of X can be folded to:3764 // IsPow2 : X & (~Bit) == 03765 // NotPow2 : X & (~Bit) != 03766 const ICmpInst::Predicate Pred = I.getPredicate();3767 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||3768 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {3769 Value *Op = CtpopLhs->getArgOperand(0);3770 KnownBits OpKnown = computeKnownBits(Op, Q.DL, Q.AC, Q.CxtI, Q.DT);3771 // No need to check for count > 1, that should be already constant folded.3772 if (OpKnown.countMinPopulation() == 1) {3773 Value *And = Builder.CreateAnd(3774 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));3775 return new ICmpInst(3776 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)3777 ? ICmpInst::ICMP_EQ3778 : ICmpInst::ICMP_NE,3779 And, Constant::getNullValue(Op->getType()));3780 }3781 }3782 3783 return nullptr;3784}3785 3786/// Fold an equality icmp with LLVM intrinsic and constant operand.3787Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant(3788 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {3789 Type *Ty = II->getType();3790 unsigned BitWidth = C.getBitWidth();3791 const ICmpInst::Predicate Pred = Cmp.getPredicate();3792 3793 switch (II->getIntrinsicID()) {3794 case Intrinsic::abs:3795 // abs(A) == 0 -> A == 03796 // abs(A) == INT_MIN -> A == INT_MIN3797 if (C.isZero() || C.isMinSignedValue())3798 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));3799 break;3800 3801 case Intrinsic::bswap:3802 // bswap(A) == C -> A == bswap(C)3803 return new ICmpInst(Pred, II->getArgOperand(0),3804 ConstantInt::get(Ty, C.byteSwap()));3805 3806 case Intrinsic::bitreverse:3807 // bitreverse(A) == C -> A == bitreverse(C)3808 return new ICmpInst(Pred, II->getArgOperand(0),3809 ConstantInt::get(Ty, C.reverseBits()));3810 3811 case Intrinsic::ctlz:3812 case Intrinsic::cttz: {3813 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=3814 if (C == BitWidth)3815 return new ICmpInst(Pred, II->getArgOperand(0),3816 ConstantInt::getNullValue(Ty));3817 3818 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set3819 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.3820 // Limit to one use to ensure we don't increase instruction count.3821 unsigned Num = C.getLimitedValue(BitWidth);3822 if (Num != BitWidth && II->hasOneUse()) {3823 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;3824 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)3825 : APInt::getHighBitsSet(BitWidth, Num + 1);3826 APInt Mask2 = IsTrailing3827 ? APInt::getOneBitSet(BitWidth, Num)3828 : APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);3829 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),3830 ConstantInt::get(Ty, Mask2));3831 }3832 break;3833 }3834 3835 case Intrinsic::ctpop: {3836 // popcount(A) == 0 -> A == 0 and likewise for !=3837 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=3838 bool IsZero = C.isZero();3839 if (IsZero || C == BitWidth)3840 return new ICmpInst(Pred, II->getArgOperand(0),3841 IsZero ? Constant::getNullValue(Ty)3842 : Constant::getAllOnesValue(Ty));3843 3844 break;3845 }3846 3847 case Intrinsic::fshl:3848 case Intrinsic::fshr:3849 if (II->getArgOperand(0) == II->getArgOperand(1)) {3850 const APInt *RotAmtC;3851 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)3852 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)3853 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))3854 return new ICmpInst(Pred, II->getArgOperand(0),3855 II->getIntrinsicID() == Intrinsic::fshl3856 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))3857 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));3858 }3859 break;3860 3861 case Intrinsic::umax:3862 case Intrinsic::uadd_sat: {3863 // uadd.sat(a, b) == 0 -> (a | b) == 03864 // umax(a, b) == 0 -> (a | b) == 03865 if (C.isZero() && II->hasOneUse()) {3866 Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1));3867 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));3868 }3869 break;3870 }3871 3872 case Intrinsic::ssub_sat:3873 // ssub.sat(a, b) == 0 -> a == b3874 if (C.isZero())3875 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));3876 break;3877 case Intrinsic::usub_sat: {3878 // usub.sat(a, b) == 0 -> a <= b3879 if (C.isZero()) {3880 ICmpInst::Predicate NewPred =3881 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;3882 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));3883 }3884 break;3885 }3886 default:3887 break;3888 }3889 3890 return nullptr;3891}3892 3893/// Fold an icmp with LLVM intrinsics3894static Instruction *3895foldICmpIntrinsicWithIntrinsic(ICmpInst &Cmp,3896 InstCombiner::BuilderTy &Builder) {3897 assert(Cmp.isEquality());3898 3899 ICmpInst::Predicate Pred = Cmp.getPredicate();3900 Value *Op0 = Cmp.getOperand(0);3901 Value *Op1 = Cmp.getOperand(1);3902 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);3903 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);3904 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())3905 return nullptr;3906 3907 switch (IIOp0->getIntrinsicID()) {3908 case Intrinsic::bswap:3909 case Intrinsic::bitreverse:3910 // If both operands are byte-swapped or bit-reversed, just compare the3911 // original values.3912 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));3913 case Intrinsic::fshl:3914 case Intrinsic::fshr: {3915 // If both operands are rotated by same amount, just compare the3916 // original values.3917 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))3918 break;3919 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))3920 break;3921 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))3922 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));3923 3924 // rotate(X, AmtX) == rotate(Y, AmtY)3925 // -> rotate(X, AmtX - AmtY) == Y3926 // Do this if either both rotates have one use or if only one has one use3927 // and AmtX/AmtY are constants.3928 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();3929 if (OneUses == 2 ||3930 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&3931 match(IIOp1->getOperand(2), m_ImmConstant()))) {3932 Value *SubAmt =3933 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));3934 Value *CombinedRotate = Builder.CreateIntrinsic(3935 Op0->getType(), IIOp0->getIntrinsicID(),3936 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});3937 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);3938 }3939 } break;3940 default:3941 break;3942 }3943 3944 return nullptr;3945}3946 3947/// Try to fold integer comparisons with a constant operand: icmp Pred X, C3948/// where X is some kind of instruction and C is AllowPoison.3949/// TODO: Move more folds which allow poison to this function.3950Instruction *3951InstCombinerImpl::foldICmpInstWithConstantAllowPoison(ICmpInst &Cmp,3952 const APInt &C) {3953 const ICmpInst::Predicate Pred = Cmp.getPredicate();3954 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {3955 switch (II->getIntrinsicID()) {3956 default:3957 break;3958 case Intrinsic::fshl:3959 case Intrinsic::fshr:3960 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {3961 // (rot X, ?) == 0/-1 --> X == 0/-13962 if (C.isZero() || C.isAllOnes())3963 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));3964 }3965 break;3966 }3967 }3968 3969 return nullptr;3970}3971 3972/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.3973Instruction *InstCombinerImpl::foldICmpBinOpWithConstant(ICmpInst &Cmp,3974 BinaryOperator *BO,3975 const APInt &C) {3976 switch (BO->getOpcode()) {3977 case Instruction::Xor:3978 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))3979 return I;3980 break;3981 case Instruction::And:3982 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))3983 return I;3984 break;3985 case Instruction::Or:3986 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))3987 return I;3988 break;3989 case Instruction::Mul:3990 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))3991 return I;3992 break;3993 case Instruction::Shl:3994 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))3995 return I;3996 break;3997 case Instruction::LShr:3998 case Instruction::AShr:3999 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))4000 return I;4001 break;4002 case Instruction::SRem:4003 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))4004 return I;4005 break;4006 case Instruction::UDiv:4007 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))4008 return I;4009 [[fallthrough]];4010 case Instruction::SDiv:4011 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))4012 return I;4013 break;4014 case Instruction::Sub:4015 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))4016 return I;4017 break;4018 case Instruction::Add:4019 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))4020 return I;4021 break;4022 default:4023 break;4024 }4025 4026 // TODO: These folds could be refactored to be part of the above calls.4027 if (Instruction *I = foldICmpBinOpEqualityWithConstant(Cmp, BO, C))4028 return I;4029 4030 // Fall back to handling `icmp pred (select A ? C1 : C2) binop (select B ? C34031 // : C4), C5` pattern, by computing a truth table of the four constant4032 // variants.4033 return foldICmpBinOpWithConstantViaTruthTable(Cmp, BO, C);4034}4035 4036static Instruction *4037foldICmpUSubSatOrUAddSatWithConstant(CmpPredicate Pred, SaturatingInst *II,4038 const APInt &C,4039 InstCombiner::BuilderTy &Builder) {4040 // This transform may end up producing more than one instruction for the4041 // intrinsic, so limit it to one user of the intrinsic.4042 if (!II->hasOneUse())4043 return nullptr;4044 4045 // Let Y = [add/sub]_sat(X, C) pred C24046 // SatVal = The saturating value for the operation4047 // WillWrap = Whether or not the operation will underflow / overflow4048 // => Y = (WillWrap ? SatVal : (X binop C)) pred C24049 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)4050 //4051 // When (SatVal pred C2) is true, then4052 // Y = WillWrap ? true : ((X binop C) pred C2)4053 // => Y = WillWrap || ((X binop C) pred C2)4054 // else4055 // Y = WillWrap ? false : ((X binop C) pred C2)4056 // => Y = !WillWrap ? ((X binop C) pred C2) : false4057 // => Y = !WillWrap && ((X binop C) pred C2)4058 Value *Op0 = II->getOperand(0);4059 Value *Op1 = II->getOperand(1);4060 4061 const APInt *COp1;4062 // This transform only works when the intrinsic has an integral constant or4063 // splat vector as the second operand.4064 if (!match(Op1, m_APInt(COp1)))4065 return nullptr;4066 4067 APInt SatVal;4068 switch (II->getIntrinsicID()) {4069 default:4070 llvm_unreachable(4071 "This function only works with usub_sat and uadd_sat for now!");4072 case Intrinsic::uadd_sat:4073 SatVal = APInt::getAllOnes(C.getBitWidth());4074 break;4075 case Intrinsic::usub_sat:4076 SatVal = APInt::getZero(C.getBitWidth());4077 break;4078 }4079 4080 // Check (SatVal pred C2)4081 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);4082 4083 // !WillWrap.4084 ConstantRange C1 = ConstantRange::makeExactNoWrapRegion(4085 II->getBinaryOp(), *COp1, II->getNoWrapKind());4086 4087 // WillWrap.4088 if (SatValCheck)4089 C1 = C1.inverse();4090 4091 ConstantRange C2 = ConstantRange::makeExactICmpRegion(Pred, C);4092 if (II->getBinaryOp() == Instruction::Add)4093 C2 = C2.sub(*COp1);4094 else4095 C2 = C2.add(*COp1);4096 4097 Instruction::BinaryOps CombiningOp =4098 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;4099 4100 std::optional<ConstantRange> Combination;4101 if (CombiningOp == Instruction::BinaryOps::Or)4102 Combination = C1.exactUnionWith(C2);4103 else /* CombiningOp == Instruction::BinaryOps::And */4104 Combination = C1.exactIntersectWith(C2);4105 4106 if (!Combination)4107 return nullptr;4108 4109 CmpInst::Predicate EquivPred;4110 APInt EquivInt;4111 APInt EquivOffset;4112 4113 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);4114 4115 return new ICmpInst(4116 EquivPred,4117 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),4118 ConstantInt::get(Op1->getType(), EquivInt));4119}4120 4121static Instruction *4122foldICmpOfCmpIntrinsicWithConstant(CmpPredicate Pred, IntrinsicInst *I,4123 const APInt &C,4124 InstCombiner::BuilderTy &Builder) {4125 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;4126 switch (Pred) {4127 case ICmpInst::ICMP_EQ:4128 case ICmpInst::ICMP_NE:4129 if (C.isZero())4130 NewPredicate = Pred;4131 else if (C.isOne())4132 NewPredicate =4133 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE;4134 else if (C.isAllOnes())4135 NewPredicate =4136 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;4137 break;4138 4139 case ICmpInst::ICMP_SGT:4140 if (C.isAllOnes())4141 NewPredicate = ICmpInst::ICMP_UGE;4142 else if (C.isZero())4143 NewPredicate = ICmpInst::ICMP_UGT;4144 break;4145 4146 case ICmpInst::ICMP_SLT:4147 if (C.isZero())4148 NewPredicate = ICmpInst::ICMP_ULT;4149 else if (C.isOne())4150 NewPredicate = ICmpInst::ICMP_ULE;4151 break;4152 4153 case ICmpInst::ICMP_ULT:4154 if (C.ugt(1))4155 NewPredicate = ICmpInst::ICMP_UGE;4156 break;4157 4158 case ICmpInst::ICMP_UGT:4159 if (!C.isZero() && !C.isAllOnes())4160 NewPredicate = ICmpInst::ICMP_ULT;4161 break;4162 4163 default:4164 break;4165 }4166 4167 if (!NewPredicate)4168 return nullptr;4169 4170 if (I->getIntrinsicID() == Intrinsic::scmp)4171 NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate);4172 Value *LHS = I->getOperand(0);4173 Value *RHS = I->getOperand(1);4174 return new ICmpInst(*NewPredicate, LHS, RHS);4175}4176 4177/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.4178Instruction *InstCombinerImpl::foldICmpIntrinsicWithConstant(ICmpInst &Cmp,4179 IntrinsicInst *II,4180 const APInt &C) {4181 ICmpInst::Predicate Pred = Cmp.getPredicate();4182 4183 // Handle folds that apply for any kind of icmp.4184 switch (II->getIntrinsicID()) {4185 default:4186 break;4187 case Intrinsic::uadd_sat:4188 case Intrinsic::usub_sat:4189 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(4190 Pred, cast<SaturatingInst>(II), C, Builder))4191 return Folded;4192 break;4193 case Intrinsic::ctpop: {4194 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);4195 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))4196 return R;4197 } break;4198 case Intrinsic::scmp:4199 case Intrinsic::ucmp:4200 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, II, C, Builder))4201 return Folded;4202 break;4203 }4204 4205 if (Cmp.isEquality())4206 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);4207 4208 Type *Ty = II->getType();4209 unsigned BitWidth = C.getBitWidth();4210 switch (II->getIntrinsicID()) {4211 case Intrinsic::ctpop: {4212 // (ctpop X > BitWidth - 1) --> X == -14213 Value *X = II->getArgOperand(0);4214 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)4215 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,4216 ConstantInt::getAllOnesValue(Ty));4217 // (ctpop X < BitWidth) --> X != -14218 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)4219 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,4220 ConstantInt::getAllOnesValue(Ty));4221 break;4222 }4223 case Intrinsic::ctlz: {4224 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b000100004225 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {4226 unsigned Num = C.getLimitedValue();4227 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);4228 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,4229 II->getArgOperand(0), ConstantInt::get(Ty, Limit));4230 }4231 4232 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b000111114233 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {4234 unsigned Num = C.getLimitedValue();4235 APInt Limit = APInt::getLowBitsSet(BitWidth, BitWidth - Num);4236 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,4237 II->getArgOperand(0), ConstantInt::get(Ty, Limit));4238 }4239 break;4240 }4241 case Intrinsic::cttz: {4242 // Limit to one use to ensure we don't increase instruction count.4243 if (!II->hasOneUse())4244 return nullptr;4245 4246 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 04247 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {4248 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);4249 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,4250 Builder.CreateAnd(II->getArgOperand(0), Mask),4251 ConstantInt::getNullValue(Ty));4252 }4253 4254 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 04255 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {4256 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());4257 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,4258 Builder.CreateAnd(II->getArgOperand(0), Mask),4259 ConstantInt::getNullValue(Ty));4260 }4261 break;4262 }4263 case Intrinsic::ssub_sat:4264 // ssub.sat(a, b) spred 0 -> a spred b4265 if (ICmpInst::isSigned(Pred)) {4266 if (C.isZero())4267 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));4268 // X s<= 0 is cannonicalized to X s< 14269 if (Pred == ICmpInst::ICMP_SLT && C.isOne())4270 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),4271 II->getArgOperand(1));4272 // X s>= 0 is cannonicalized to X s> -14273 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())4274 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),4275 II->getArgOperand(1));4276 }4277 break;4278 default:4279 break;4280 }4281 4282 return nullptr;4283}4284 4285/// Handle icmp with constant (but not simple integer constant) RHS.4286Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {4287 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);4288 Constant *RHSC = dyn_cast<Constant>(Op1);4289 Instruction *LHSI = dyn_cast<Instruction>(Op0);4290 if (!RHSC || !LHSI)4291 return nullptr;4292 4293 switch (LHSI->getOpcode()) {4294 case Instruction::IntToPtr:4295 // icmp pred inttoptr(X), null -> icmp pred X, 04296 if (RHSC->isNullValue() &&4297 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())4298 return new ICmpInst(4299 I.getPredicate(), LHSI->getOperand(0),4300 Constant::getNullValue(LHSI->getOperand(0)->getType()));4301 break;4302 4303 case Instruction::Load:4304 // Try to optimize things like "A[i] > 4" to index computations.4305 if (GetElementPtrInst *GEP =4306 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))4307 if (Instruction *Res =4308 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, I))4309 return Res;4310 break;4311 }4312 4313 return nullptr;4314}4315 4316Instruction *InstCombinerImpl::foldSelectICmp(CmpPredicate Pred, SelectInst *SI,4317 Value *RHS, const ICmpInst &I) {4318 // Try to fold the comparison into the select arms, which will cause the4319 // select to be converted into a logical and/or.4320 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {4321 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))4322 return Res;4323 if (std::optional<bool> Impl = isImpliedCondition(4324 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))4325 return ConstantInt::get(I.getType(), *Impl);4326 return nullptr;4327 };4328 4329 ConstantInt *CI = nullptr;4330 Value *Op1 = SimplifyOp(SI->getOperand(1), true);4331 if (Op1)4332 CI = dyn_cast<ConstantInt>(Op1);4333 4334 Value *Op2 = SimplifyOp(SI->getOperand(2), false);4335 if (Op2)4336 CI = dyn_cast<ConstantInt>(Op2);4337 4338 auto Simplifies = [&](Value *Op, unsigned Idx) {4339 // A comparison of ucmp/scmp with a constant will fold into an icmp.4340 const APInt *Dummy;4341 return Op ||4342 (isa<CmpIntrinsic>(SI->getOperand(Idx)) &&4343 SI->getOperand(Idx)->hasOneUse() && match(RHS, m_APInt(Dummy)));4344 };4345 4346 // We only want to perform this transformation if it will not lead to4347 // additional code. This is true if either both sides of the select4348 // fold to a constant (in which case the icmp is replaced with a select4349 // which will usually simplify) or this is the only user of the4350 // select (in which case we are trading a select+icmp for a simpler4351 // select+icmp) or all uses of the select can be replaced based on4352 // dominance information ("Global cases").4353 bool Transform = false;4354 if (Op1 && Op2)4355 Transform = true;4356 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {4357 // Local case4358 if (SI->hasOneUse())4359 Transform = true;4360 // Global cases4361 else if (CI && !CI->isZero())4362 // When Op1 is constant try replacing select with second operand.4363 // Otherwise Op2 is constant and try replacing select with first4364 // operand.4365 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);4366 }4367 if (Transform) {4368 if (!Op1)4369 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());4370 if (!Op2)4371 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());4372 return SelectInst::Create(SI->getOperand(0), Op1, Op2);4373 }4374 4375 return nullptr;4376}4377 4378// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)4379static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,4380 unsigned Depth = 0) {4381 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))4382 return true;4383 if (V->getType()->getScalarSizeInBits() == 1)4384 return true;4385 if (Depth++ >= MaxAnalysisRecursionDepth)4386 return false;4387 Value *X;4388 const Instruction *I = dyn_cast<Instruction>(V);4389 if (!I)4390 return false;4391 switch (I->getOpcode()) {4392 case Instruction::ZExt:4393 // ZExt(Mask) is a Mask.4394 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);4395 case Instruction::SExt:4396 // SExt(Mask) is a Mask.4397 // SExt(~Mask) is a ~Mask.4398 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);4399 case Instruction::And:4400 case Instruction::Or:4401 // Mask0 | Mask1 is a Mask.4402 // Mask0 & Mask1 is a Mask.4403 // ~Mask0 | ~Mask1 is a ~Mask.4404 // ~Mask0 & ~Mask1 is a ~Mask.4405 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&4406 isMaskOrZero(I->getOperand(0), Not, Q, Depth);4407 case Instruction::Xor:4408 if (match(V, m_Not(m_Value(X))))4409 return isMaskOrZero(X, !Not, Q, Depth);4410 4411 // (X ^ -X) is a ~Mask4412 if (Not)4413 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));4414 // (X ^ (X - 1)) is a Mask4415 else4416 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));4417 case Instruction::Select:4418 // c ? Mask0 : Mask1 is a Mask.4419 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&4420 isMaskOrZero(I->getOperand(2), Not, Q, Depth);4421 case Instruction::Shl:4422 // (~Mask) << X is a ~Mask.4423 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);4424 case Instruction::LShr:4425 // Mask >> X is a Mask.4426 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);4427 case Instruction::AShr:4428 // Mask s>> X is a Mask.4429 // ~Mask s>> X is a ~Mask.4430 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);4431 case Instruction::Add:4432 // Pow2 - 1 is a Mask.4433 if (!Not && match(I->getOperand(1), m_AllOnes()))4434 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,4435 Q.AC, Q.CxtI, Q.DT, Depth);4436 break;4437 case Instruction::Sub:4438 // -Pow2 is a ~Mask.4439 if (Not && match(I->getOperand(0), m_Zero()))4440 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,4441 Q.AC, Q.CxtI, Q.DT, Depth);4442 break;4443 case Instruction::Call: {4444 if (auto *II = dyn_cast<IntrinsicInst>(I)) {4445 switch (II->getIntrinsicID()) {4446 // min/max(Mask0, Mask1) is a Mask.4447 // min/max(~Mask0, ~Mask1) is a ~Mask.4448 case Intrinsic::umax:4449 case Intrinsic::smax:4450 case Intrinsic::umin:4451 case Intrinsic::smin:4452 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&4453 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);4454 4455 // In the context of masks, bitreverse(Mask) == ~Mask4456 case Intrinsic::bitreverse:4457 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);4458 default:4459 break;4460 }4461 }4462 break;4463 }4464 default:4465 break;4466 }4467 return false;4468}4469 4470/// Some comparisons can be simplified.4471/// In this case, we are looking for comparisons that look like4472/// a check for a lossy truncation.4473/// Folds:4474/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask4475/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask4476/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask4477/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask4478/// Where Mask is some pattern that produces all-ones in low bits:4479/// (-1 >> y)4480/// ((-1 << y) >> y) <- non-canonical, has extra uses4481/// ~(-1 << y)4482/// ((1 << y) + (-1)) <- non-canonical, has extra uses4483/// The Mask can be a constant, too.4484/// For some predicates, the operands are commutative.4485/// For others, x can only be on a specific side.4486static Value *foldICmpWithLowBitMaskedVal(CmpPredicate Pred, Value *Op0,4487 Value *Op1, const SimplifyQuery &Q,4488 InstCombiner &IC) {4489 4490 ICmpInst::Predicate DstPred;4491 switch (Pred) {4492 case ICmpInst::Predicate::ICMP_EQ:4493 // x & Mask == x4494 // x & ~Mask == 04495 // ~x | Mask == -14496 // -> x u<= Mask4497 // x & ~Mask == ~Mask4498 // -> ~Mask u<= x4499 DstPred = ICmpInst::Predicate::ICMP_ULE;4500 break;4501 case ICmpInst::Predicate::ICMP_NE:4502 // x & Mask != x4503 // x & ~Mask != 04504 // ~x | Mask != -14505 // -> x u> Mask4506 // x & ~Mask != ~Mask4507 // -> ~Mask u> x4508 DstPred = ICmpInst::Predicate::ICMP_UGT;4509 break;4510 case ICmpInst::Predicate::ICMP_ULT:4511 // x & Mask u< x4512 // -> x u> Mask4513 // x & ~Mask u< ~Mask4514 // -> ~Mask u> x4515 DstPred = ICmpInst::Predicate::ICMP_UGT;4516 break;4517 case ICmpInst::Predicate::ICMP_UGE:4518 // x & Mask u>= x4519 // -> x u<= Mask4520 // x & ~Mask u>= ~Mask4521 // -> ~Mask u<= x4522 DstPred = ICmpInst::Predicate::ICMP_ULE;4523 break;4524 case ICmpInst::Predicate::ICMP_SLT:4525 // x & Mask s< x [iff Mask s>= 0]4526 // -> x s> Mask4527 // x & ~Mask s< ~Mask [iff ~Mask != 0]4528 // -> ~Mask s> x4529 DstPred = ICmpInst::Predicate::ICMP_SGT;4530 break;4531 case ICmpInst::Predicate::ICMP_SGE:4532 // x & Mask s>= x [iff Mask s>= 0]4533 // -> x s<= Mask4534 // x & ~Mask s>= ~Mask [iff ~Mask != 0]4535 // -> ~Mask s<= x4536 DstPred = ICmpInst::Predicate::ICMP_SLE;4537 break;4538 default:4539 // We don't support sgt,sle4540 // ult/ugt are simplified to true/false respectively.4541 return nullptr;4542 }4543 4544 Value *X, *M;4545 // Put search code in lambda for early positive returns.4546 auto IsLowBitMask = [&]() {4547 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {4548 X = Op1;4549 // Look for: x & Mask pred x4550 if (isMaskOrZero(M, /*Not=*/false, Q)) {4551 return !ICmpInst::isSigned(Pred) ||4552 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));4553 }4554 4555 // Look for: x & ~Mask pred ~Mask4556 if (isMaskOrZero(X, /*Not=*/true, Q)) {4557 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);4558 }4559 return false;4560 }4561 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&4562 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {4563 4564 auto Check = [&]() {4565 // Look for: ~x | Mask == -14566 if (isMaskOrZero(M, /*Not=*/false, Q)) {4567 if (Value *NotX =4568 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {4569 X = NotX;4570 return true;4571 }4572 }4573 return false;4574 };4575 if (Check())4576 return true;4577 std::swap(X, M);4578 return Check();4579 }4580 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&4581 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {4582 auto Check = [&]() {4583 // Look for: x & ~Mask == 04584 if (isMaskOrZero(M, /*Not=*/true, Q)) {4585 if (Value *NotM =4586 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {4587 M = NotM;4588 return true;4589 }4590 }4591 return false;4592 };4593 if (Check())4594 return true;4595 std::swap(X, M);4596 return Check();4597 }4598 return false;4599 };4600 4601 if (!IsLowBitMask())4602 return nullptr;4603 4604 return IC.Builder.CreateICmp(DstPred, X, M);4605}4606 4607/// Some comparisons can be simplified.4608/// In this case, we are looking for comparisons that look like4609/// a check for a lossy signed truncation.4610/// Folds: (MaskedBits is a constant.)4611/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x4612/// Into:4613/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)4614/// Where KeptBits = bitwidth(%x) - MaskedBits4615static Value *4616foldICmpWithTruncSignExtendedVal(ICmpInst &I,4617 InstCombiner::BuilderTy &Builder) {4618 CmpPredicate SrcPred;4619 Value *X;4620 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.4621 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.4622 if (!match(&I, m_c_ICmp(SrcPred,4623 m_OneUse(m_AShr(m_Shl(m_Value(X), m_APInt(C0)),4624 m_APInt(C1))),4625 m_Deferred(X))))4626 return nullptr;4627 4628 // Potential handling of non-splats: for each element:4629 // * if both are undef, replace with constant 0.4630 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.4631 // * if both are not undef, and are different, bailout.4632 // * else, only one is undef, then pick the non-undef one.4633 4634 // The shift amount must be equal.4635 if (*C0 != *C1)4636 return nullptr;4637 const APInt &MaskedBits = *C0;4638 assert(MaskedBits != 0 && "shift by zero should be folded away already.");4639 4640 ICmpInst::Predicate DstPred;4641 switch (SrcPred) {4642 case ICmpInst::Predicate::ICMP_EQ:4643 // ((%x << MaskedBits) a>> MaskedBits) == %x4644 // =>4645 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)4646 DstPred = ICmpInst::Predicate::ICMP_ULT;4647 break;4648 case ICmpInst::Predicate::ICMP_NE:4649 // ((%x << MaskedBits) a>> MaskedBits) != %x4650 // =>4651 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)4652 DstPred = ICmpInst::Predicate::ICMP_UGE;4653 break;4654 // FIXME: are more folds possible?4655 default:4656 return nullptr;4657 }4658 4659 auto *XType = X->getType();4660 const unsigned XBitWidth = XType->getScalarSizeInBits();4661 const APInt BitWidth = APInt(XBitWidth, XBitWidth);4662 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");4663 4664 // KeptBits = bitwidth(%x) - MaskedBits4665 const APInt KeptBits = BitWidth - MaskedBits;4666 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");4667 // ICmpCst = (1 << KeptBits)4668 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);4669 assert(ICmpCst.isPowerOf2());4670 // AddCst = (1 << (KeptBits-1))4671 const APInt AddCst = ICmpCst.lshr(1);4672 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());4673 4674 // T0 = add %x, AddCst4675 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));4676 // T1 = T0 DstPred ICmpCst4677 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));4678 4679 return T1;4680}4681 4682// Given pattern:4683// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 04684// we should move shifts to the same hand of 'and', i.e. rewrite as4685// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)4686// We are only interested in opposite logical shifts here.4687// One of the shifts can be truncated.4688// If we can, we want to end up creating 'lshr' shift.4689static Value *4690foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ,4691 InstCombiner::BuilderTy &Builder) {4692 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||4693 !I.getOperand(0)->hasOneUse())4694 return nullptr;4695 4696 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());4697 4698 // Look for an 'and' of two logical shifts, one of which may be truncated.4699 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.4700 Instruction *XShift, *MaybeTruncation, *YShift;4701 if (!match(4702 I.getOperand(0),4703 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),4704 m_CombineAnd(m_TruncOrSelf(m_CombineAnd(4705 m_AnyLogicalShift, m_Instruction(YShift))),4706 m_Instruction(MaybeTruncation)))))4707 return nullptr;4708 4709 // We potentially looked past 'trunc', but only when matching YShift,4710 // therefore YShift must have the widest type.4711 Instruction *WidestShift = YShift;4712 // Therefore XShift must have the shallowest type.4713 // Or they both have identical types if there was no truncation.4714 Instruction *NarrowestShift = XShift;4715 4716 Type *WidestTy = WidestShift->getType();4717 Type *NarrowestTy = NarrowestShift->getType();4718 assert(NarrowestTy == I.getOperand(0)->getType() &&4719 "We did not look past any shifts while matching XShift though.");4720 bool HadTrunc = WidestTy != I.getOperand(0)->getType();4721 4722 // If YShift is a 'lshr', swap the shifts around.4723 if (match(YShift, m_LShr(m_Value(), m_Value())))4724 std::swap(XShift, YShift);4725 4726 // The shifts must be in opposite directions.4727 auto XShiftOpcode = XShift->getOpcode();4728 if (XShiftOpcode == YShift->getOpcode())4729 return nullptr; // Do not care about same-direction shifts here.4730 4731 Value *X, *XShAmt, *Y, *YShAmt;4732 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));4733 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));4734 4735 // If one of the values being shifted is a constant, then we will end with4736 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,4737 // however, we will need to ensure that we won't increase instruction count.4738 if (!isa<Constant>(X) && !isa<Constant>(Y)) {4739 // At least one of the hands of the 'and' should be one-use shift.4740 if (!match(I.getOperand(0),4741 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))4742 return nullptr;4743 if (HadTrunc) {4744 // Due to the 'trunc', we will need to widen X. For that either the old4745 // 'trunc' or the shift amt in the non-truncated shift should be one-use.4746 if (!MaybeTruncation->hasOneUse() &&4747 !NarrowestShift->getOperand(1)->hasOneUse())4748 return nullptr;4749 }4750 }4751 4752 // We have two shift amounts from two different shifts. The types of those4753 // shift amounts may not match. If that's the case let's bailout now.4754 if (XShAmt->getType() != YShAmt->getType())4755 return nullptr;4756 4757 // As input, we have the following pattern:4758 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 04759 // We want to rewrite that as:4760 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)4761 // While we know that originally (Q+K) would not overflow4762 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of4763 // shift amounts. so it may now overflow in smaller bitwidth.4764 // To ensure that does not happen, we need to ensure that the total maximal4765 // shift amount is still representable in that smaller bit width.4766 unsigned MaximalPossibleTotalShiftAmount =4767 (WidestTy->getScalarSizeInBits() - 1) +4768 (NarrowestTy->getScalarSizeInBits() - 1);4769 APInt MaximalRepresentableShiftAmount =4770 APInt::getAllOnes(XShAmt->getType()->getScalarSizeInBits());4771 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))4772 return nullptr;4773 4774 // Can we fold (XShAmt+YShAmt) ?4775 auto *NewShAmt = dyn_cast_or_null<Constant>(4776 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,4777 /*isNUW=*/false, SQ.getWithInstruction(&I)));4778 if (!NewShAmt)4779 return nullptr;4780 if (NewShAmt->getType() != WidestTy) {4781 NewShAmt =4782 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);4783 if (!NewShAmt)4784 return nullptr;4785 }4786 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();4787 4788 // Is the new shift amount smaller than the bit width?4789 // FIXME: could also rely on ConstantRange.4790 if (!match(NewShAmt,4791 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT,4792 APInt(WidestBitWidth, WidestBitWidth))))4793 return nullptr;4794 4795 // An extra legality check is needed if we had trunc-of-lshr.4796 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {4797 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,4798 WidestShift]() {4799 // It isn't obvious whether it's worth it to analyze non-constants here.4800 // Also, let's basically give up on non-splat cases, pessimizing vectors.4801 // If *any* of these preconditions matches we can perform the fold.4802 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()4803 ? NewShAmt->getSplatValue()4804 : NewShAmt;4805 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.4806 if (NewShAmtSplat &&4807 (NewShAmtSplat->isNullValue() ||4808 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))4809 return true;4810 // We consider *min* leading zeros so a single outlier4811 // blocks the transform as opposed to allowing it.4812 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {4813 KnownBits Known = computeKnownBits(C, SQ.DL);4814 unsigned MinLeadZero = Known.countMinLeadingZeros();4815 // If the value being shifted has at most lowest bit set we can fold.4816 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;4817 if (MaxActiveBits <= 1)4818 return true;4819 // Precondition: NewShAmt u<= countLeadingZeros(C)4820 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))4821 return true;4822 }4823 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {4824 KnownBits Known = computeKnownBits(C, SQ.DL);4825 unsigned MinLeadZero = Known.countMinLeadingZeros();4826 // If the value being shifted has at most lowest bit set we can fold.4827 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;4828 if (MaxActiveBits <= 1)4829 return true;4830 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)4831 if (NewShAmtSplat) {4832 APInt AdjNewShAmt =4833 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();4834 if (AdjNewShAmt.ule(MinLeadZero))4835 return true;4836 }4837 }4838 return false; // Can't tell if it's ok.4839 };4840 if (!CanFold())4841 return nullptr;4842 }4843 4844 // All good, we can do this fold.4845 X = Builder.CreateZExt(X, WidestTy);4846 Y = Builder.CreateZExt(Y, WidestTy);4847 // The shift is the same that was for X.4848 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr4849 ? Builder.CreateLShr(X, NewShAmt)4850 : Builder.CreateShl(X, NewShAmt);4851 Value *T1 = Builder.CreateAnd(T0, Y);4852 return Builder.CreateICmp(I.getPredicate(), T1,4853 Constant::getNullValue(WidestTy));4854}4855 4856/// Fold4857/// (-1 u/ x) u< y4858/// ((x * y) ?/ x) != y4859/// to4860/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit4861/// Note that the comparison is commutative, while inverted (u>=, ==) predicate4862/// will mean that we are looking for the opposite answer.4863Value *InstCombinerImpl::foldMultiplicationOverflowCheck(ICmpInst &I) {4864 CmpPredicate Pred;4865 Value *X, *Y;4866 Instruction *Mul;4867 Instruction *Div;4868 bool NeedNegation;4869 // Look for: (-1 u/ x) u</u>= y4870 if (!I.isEquality() &&4871 match(&I, m_c_ICmp(Pred,4872 m_CombineAnd(m_OneUse(m_UDiv(m_AllOnes(), m_Value(X))),4873 m_Instruction(Div)),4874 m_Value(Y)))) {4875 Mul = nullptr;4876 4877 // Are we checking that overflow does not happen, or does happen?4878 switch (Pred) {4879 case ICmpInst::Predicate::ICMP_ULT:4880 NeedNegation = false;4881 break; // OK4882 case ICmpInst::Predicate::ICMP_UGE:4883 NeedNegation = true;4884 break; // OK4885 default:4886 return nullptr; // Wrong predicate.4887 }4888 } else // Look for: ((x * y) / x) !=/== y4889 if (I.isEquality() &&4890 match(&I, m_c_ICmp(Pred, m_Value(Y),4891 m_CombineAnd(m_OneUse(m_IDiv(4892 m_CombineAnd(m_c_Mul(m_Deferred(Y),4893 m_Value(X)),4894 m_Instruction(Mul)),4895 m_Deferred(X))),4896 m_Instruction(Div))))) {4897 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;4898 } else4899 return nullptr;4900 4901 BuilderTy::InsertPointGuard Guard(Builder);4902 // If the pattern included (x * y), we'll want to insert new instructions4903 // right before that original multiplication so that we can replace it.4904 bool MulHadOtherUses = Mul && !Mul->hasOneUse();4905 if (MulHadOtherUses)4906 Builder.SetInsertPoint(Mul);4907 4908 CallInst *Call = Builder.CreateIntrinsic(4909 Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow4910 : Intrinsic::smul_with_overflow,4911 X->getType(), {X, Y}, /*FMFSource=*/nullptr, "mul");4912 4913 // If the multiplication was used elsewhere, to ensure that we don't leave4914 // "duplicate" instructions, replace uses of that original multiplication4915 // with the multiplication result from the with.overflow intrinsic.4916 if (MulHadOtherUses)4917 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));4918 4919 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");4920 if (NeedNegation) // This technically increases instruction count.4921 Res = Builder.CreateNot(Res, "mul.not.ov");4922 4923 // If we replaced the mul, erase it. Do this after all uses of Builder,4924 // as the mul is used as insertion point.4925 if (MulHadOtherUses)4926 eraseInstFromFunction(*Mul);4927 4928 return Res;4929}4930 4931static Instruction *foldICmpXNegX(ICmpInst &I,4932 InstCombiner::BuilderTy &Builder) {4933 CmpPredicate Pred;4934 Value *X;4935 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {4936 4937 if (ICmpInst::isSigned(Pred))4938 Pred = ICmpInst::getSwappedPredicate(Pred);4939 else if (ICmpInst::isUnsigned(Pred))4940 Pred = ICmpInst::getSignedPredicate(Pred);4941 // else for equality-comparisons just keep the predicate.4942 4943 return ICmpInst::Create(Instruction::ICmp, Pred, X,4944 Constant::getNullValue(X->getType()), I.getName());4945 }4946 4947 // A value is not equal to its negation unless that value is 0 or4948 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 04949 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&4950 ICmpInst::isEquality(Pred)) {4951 Type *Ty = X->getType();4952 uint32_t BitWidth = Ty->getScalarSizeInBits();4953 Constant *MaxSignedVal =4954 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));4955 Value *And = Builder.CreateAnd(X, MaxSignedVal);4956 Constant *Zero = Constant::getNullValue(Ty);4957 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);4958 }4959 4960 return nullptr;4961}4962 4963static Instruction *foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q,4964 InstCombinerImpl &IC) {4965 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;4966 // Normalize and operand as operand 0.4967 CmpInst::Predicate Pred = I.getPredicate();4968 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {4969 std::swap(Op0, Op1);4970 Pred = ICmpInst::getSwappedPredicate(Pred);4971 }4972 4973 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))4974 return nullptr;4975 4976 // (icmp (X & Y) u< X --> (X & Y) != X4977 if (Pred == ICmpInst::ICMP_ULT)4978 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);4979 4980 // (icmp (X & Y) u>= X --> (X & Y) == X4981 if (Pred == ICmpInst::ICMP_UGE)4982 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);4983 4984 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {4985 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and4986 // Y is non-constant. If Y is constant the `X & C == C` form is preferable4987 // so don't do this fold.4988 if (!match(Op1, m_ImmConstant()))4989 if (auto *NotOp1 =4990 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))4991 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),4992 Constant::getAllOnesValue(Op1->getType()));4993 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.4994 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))4995 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),4996 Constant::getNullValue(Op1->getType()));4997 }4998 4999 if (!ICmpInst::isSigned(Pred))5000 return nullptr;5001 5002 KnownBits KnownY = IC.computeKnownBits(A, &I);5003 // (X & NegY) spred X --> (X & NegY) upred X5004 if (KnownY.isNegative())5005 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);5006 5007 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)5008 return nullptr;5009 5010 if (KnownY.isNonNegative())5011 // (X & PosY) s<= X --> X s>= 05012 // (X & PosY) s> X --> X s< 05013 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,5014 Constant::getNullValue(Op1->getType()));5015 5016 if (isKnownNegative(Op1, IC.getSimplifyQuery().getWithInstruction(&I)))5017 // (NegX & Y) s<= NegX --> Y s< 05018 // (NegX & Y) s> NegX --> Y s>= 05019 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(Pred), A,5020 Constant::getNullValue(A->getType()));5021 5022 return nullptr;5023}5024 5025static Instruction *foldICmpOrXX(ICmpInst &I, const SimplifyQuery &Q,5026 InstCombinerImpl &IC) {5027 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;5028 5029 // Normalize or operand as operand 0.5030 CmpInst::Predicate Pred = I.getPredicate();5031 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {5032 std::swap(Op0, Op1);5033 Pred = ICmpInst::getSwappedPredicate(Pred);5034 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {5035 return nullptr;5036 }5037 5038 // icmp (X | Y) u<= X --> (X | Y) == X5039 if (Pred == ICmpInst::ICMP_ULE)5040 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);5041 5042 // icmp (X | Y) u> X --> (X | Y) != X5043 if (Pred == ICmpInst::ICMP_UGT)5044 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);5045 5046 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {5047 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible5048 if (Value *NotOp1 = IC.getFreelyInverted(5049 Op1, !isa<Constant>(Op1) && !Op1->hasNUsesOrMore(3), &IC.Builder))5050 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),5051 Constant::getNullValue(Op1->getType()));5052 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.5053 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))5054 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),5055 Constant::getAllOnesValue(Op1->getType()));5056 }5057 return nullptr;5058}5059 5060static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q,5061 InstCombinerImpl &IC) {5062 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;5063 // Normalize xor operand as operand 0.5064 CmpInst::Predicate Pred = I.getPredicate();5065 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {5066 std::swap(Op0, Op1);5067 Pred = ICmpInst::getSwappedPredicate(Pred);5068 }5069 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))5070 return nullptr;5071 5072 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X5073 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X5074 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X5075 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X5076 CmpInst::Predicate PredOut = CmpInst::getStrictPredicate(Pred);5077 if (PredOut != Pred && isKnownNonZero(A, Q))5078 return new ICmpInst(PredOut, Op0, Op1);5079 5080 // These transform work when A is negative.5081 // X s< X^A, X s<= X^A, X u> X^A, X u>= X^A --> X s< 05082 // X s> X^A, X s>= X^A, X u< X^A, X u<= X^A --> X s>= 05083 if (match(A, m_Negative())) {5084 CmpInst::Predicate NewPred;5085 switch (ICmpInst::getStrictPredicate(Pred)) {5086 default:5087 return nullptr;5088 case ICmpInst::ICMP_SLT:5089 case ICmpInst::ICMP_UGT:5090 NewPred = ICmpInst::ICMP_SLT;5091 break;5092 case ICmpInst::ICMP_SGT:5093 case ICmpInst::ICMP_ULT:5094 NewPred = ICmpInst::ICMP_SGE;5095 break;5096 }5097 Constant *Const = Constant::getNullValue(Op0->getType());5098 return new ICmpInst(NewPred, Op0, Const);5099 }5100 5101 return nullptr;5102}5103 5104/// Return true if X is a multiple of C.5105/// TODO: Handle non-power-of-2 factors.5106static bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q) {5107 if (C.isOne())5108 return true;5109 5110 if (!C.isPowerOf2())5111 return false;5112 5113 return MaskedValueIsZero(X, C - 1, Q);5114}5115 5116/// Try to fold icmp (binop), X or icmp X, (binop).5117/// TODO: A large part of this logic is duplicated in InstSimplify's5118/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code5119/// duplication.5120Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,5121 const SimplifyQuery &SQ) {5122 const SimplifyQuery Q = SQ.getWithInstruction(&I);5123 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);5124 5125 // Special logic for binary operators.5126 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);5127 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);5128 if (!BO0 && !BO1)5129 return nullptr;5130 5131 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))5132 return NewICmp;5133 5134 const CmpInst::Predicate Pred = I.getPredicate();5135 Value *X;5136 5137 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.5138 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X5139 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&5140 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))5141 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);5142 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op05143 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&5144 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))5145 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));5146 5147 {5148 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op15149 Constant *C;5150 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),5151 m_ImmConstant(C)))) &&5152 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {5153 Constant *C2 = ConstantExpr::getNot(C);5154 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);5155 }5156 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X5157 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),5158 m_ImmConstant(C)))) &&5159 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {5160 Constant *C2 = ConstantExpr::getNot(C);5161 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));5162 }5163 }5164 5165 // (icmp eq/ne (X, -P2), INT_MIN)5166 // -> (icmp slt/sge X, INT_MIN + P2)5167 if (ICmpInst::isEquality(Pred) && BO0 &&5168 match(I.getOperand(1), m_SignMask()) &&5169 match(BO0, m_And(m_Value(), m_NegatedPower2OrZero()))) {5170 // Will Constant fold.5171 Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));5172 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT5173 : ICmpInst::ICMP_SGE,5174 BO0->getOperand(0), NewC);5175 }5176 5177 {5178 // Similar to above: an unsigned overflow comparison may use offset + mask:5179 // ((Op1 + C) & C) u< Op1 --> Op1 != 05180 // ((Op1 + C) & C) u>= Op1 --> Op1 == 05181 // Op0 u> ((Op0 + C) & C) --> Op0 != 05182 // Op0 u<= ((Op0 + C) & C) --> Op0 == 05183 BinaryOperator *BO;5184 const APInt *C;5185 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&5186 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&5187 match(BO, m_Add(m_Specific(Op1), m_SpecificIntAllowPoison(*C)))) {5188 CmpInst::Predicate NewPred =5189 Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;5190 Constant *Zero = ConstantInt::getNullValue(Op1->getType());5191 return new ICmpInst(NewPred, Op1, Zero);5192 }5193 5194 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&5195 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&5196 match(BO, m_Add(m_Specific(Op0), m_SpecificIntAllowPoison(*C)))) {5197 CmpInst::Predicate NewPred =5198 Pred == ICmpInst::ICMP_UGT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;5199 Constant *Zero = ConstantInt::getNullValue(Op1->getType());5200 return new ICmpInst(NewPred, Op0, Zero);5201 }5202 }5203 5204 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;5205 bool Op0HasNUW = false, Op1HasNUW = false;5206 bool Op0HasNSW = false, Op1HasNSW = false;5207 // Analyze the case when either Op0 or Op1 is an add instruction.5208 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).5209 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,5210 bool &HasNSW, bool &HasNUW) -> bool {5211 if (isa<OverflowingBinaryOperator>(BO)) {5212 HasNUW = BO.hasNoUnsignedWrap();5213 HasNSW = BO.hasNoSignedWrap();5214 return ICmpInst::isEquality(Pred) ||5215 (CmpInst::isUnsigned(Pred) && HasNUW) ||5216 (CmpInst::isSigned(Pred) && HasNSW);5217 } else if (BO.getOpcode() == Instruction::Or) {5218 HasNUW = true;5219 HasNSW = true;5220 return true;5221 } else {5222 return false;5223 }5224 };5225 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;5226 5227 if (BO0) {5228 match(BO0, m_AddLike(m_Value(A), m_Value(B)));5229 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);5230 }5231 if (BO1) {5232 match(BO1, m_AddLike(m_Value(C), m_Value(D)));5233 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);5234 }5235 5236 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.5237 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.5238 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)5239 return new ICmpInst(Pred, A == Op1 ? B : A,5240 Constant::getNullValue(Op1->getType()));5241 5242 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.5243 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.5244 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)5245 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),5246 C == Op0 ? D : C);5247 5248 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.5249 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&5250 NoOp1WrapProblem) {5251 // Determine Y and Z in the form icmp (X+Y), (X+Z).5252 Value *Y, *Z;5253 if (A == C) {5254 // C + B == C + D -> B == D5255 Y = B;5256 Z = D;5257 } else if (A == D) {5258 // D + B == C + D -> B == C5259 Y = B;5260 Z = C;5261 } else if (B == C) {5262 // A + C == C + D -> A == D5263 Y = A;5264 Z = D;5265 } else {5266 assert(B == D);5267 // A + D == C + D -> A == C5268 Y = A;5269 Z = C;5270 }5271 return new ICmpInst(Pred, Y, Z);5272 }5273 5274 if (ICmpInst::isRelational(Pred)) {5275 // Return if both X and Y is divisible by Z/-Z.5276 // TODO: Generalize to check if (X - Y) is divisible by Z/-Z.5277 auto ShareCommonDivisor = [&Q](Value *X, Value *Y, Value *Z,5278 bool IsNegative) -> bool {5279 const APInt *OffsetC;5280 if (!match(Z, m_APInt(OffsetC)))5281 return false;5282 5283 // Fast path for Z == 1/-1.5284 if (IsNegative ? OffsetC->isAllOnes() : OffsetC->isOne())5285 return true;5286 5287 APInt C = *OffsetC;5288 if (IsNegative)5289 C.negate();5290 // Note: -INT_MIN is also negative.5291 if (!C.isStrictlyPositive())5292 return false;5293 5294 return isMultipleOf(X, C, Q) && isMultipleOf(Y, C, Q);5295 };5296 5297 // TODO: The subtraction-related identities shown below also hold, but5298 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations5299 // wouldn't happen even if they were implemented.5300 //5301 // icmp ult (A - 1), Op1 -> icmp ule A, Op15302 // icmp uge (A - 1), Op1 -> icmp ugt A, Op15303 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C5304 // icmp ule Op0, (C - 1) -> icmp ult Op0, C5305 5306 // icmp slt (A + -1), Op1 -> icmp sle A, Op15307 // icmp sge (A + -1), Op1 -> icmp sgt A, Op15308 // icmp sle (A + 1), Op1 -> icmp slt A, Op15309 // icmp sgt (A + 1), Op1 -> icmp sge A, Op15310 // icmp ule (A + 1), Op0 -> icmp ult A, Op15311 // icmp ugt (A + 1), Op0 -> icmp uge A, Op15312 if (A && NoOp0WrapProblem &&5313 ShareCommonDivisor(A, Op1, B,5314 ICmpInst::isLT(Pred) || ICmpInst::isGE(Pred)))5315 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(Pred), A,5316 Op1);5317 5318 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C5319 // icmp sle Op0, (C + -1) -> icmp slt Op0, C5320 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C5321 // icmp slt Op0, (C + 1) -> icmp sle Op0, C5322 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C5323 // icmp ult Op0, (C + 1) -> icmp ule Op0, C5324 if (C && NoOp1WrapProblem &&5325 ShareCommonDivisor(Op0, C, D,5326 ICmpInst::isGT(Pred) || ICmpInst::isLE(Pred)))5327 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(Pred), Op0,5328 C);5329 }5330 5331 // if C1 has greater magnitude than C2:5332 // icmp (A + C1), (C + C2) -> icmp (A + C3), C5333 // s.t. C3 = C1 - C25334 //5335 // if C2 has greater magnitude than C1:5336 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)5337 // s.t. C3 = C2 - C15338 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&5339 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {5340 const APInt *AP1, *AP2;5341 // TODO: Support non-uniform vectors.5342 // TODO: Allow poison passthrough if B or D's element is poison.5343 if (match(B, m_APIntAllowPoison(AP1)) &&5344 match(D, m_APIntAllowPoison(AP2)) &&5345 AP1->isNegative() == AP2->isNegative()) {5346 APInt AP1Abs = AP1->abs();5347 APInt AP2Abs = AP2->abs();5348 if (AP1Abs.uge(AP2Abs)) {5349 APInt Diff = *AP1 - *AP2;5350 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);5351 Value *NewAdd = Builder.CreateAdd(5352 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);5353 return new ICmpInst(Pred, NewAdd, C);5354 } else {5355 APInt Diff = *AP2 - *AP1;5356 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);5357 Value *NewAdd = Builder.CreateAdd(5358 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);5359 return new ICmpInst(Pred, A, NewAdd);5360 }5361 }5362 Constant *Cst1, *Cst2;5363 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&5364 ICmpInst::isEquality(Pred)) {5365 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);5366 Value *NewAdd = Builder.CreateAdd(C, Diff);5367 return new ICmpInst(Pred, A, NewAdd);5368 }5369 }5370 5371 // Analyze the case when either Op0 or Op1 is a sub instruction.5372 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).5373 A = nullptr;5374 B = nullptr;5375 C = nullptr;5376 D = nullptr;5377 if (BO0 && BO0->getOpcode() == Instruction::Sub) {5378 A = BO0->getOperand(0);5379 B = BO0->getOperand(1);5380 }5381 if (BO1 && BO1->getOpcode() == Instruction::Sub) {5382 C = BO1->getOperand(0);5383 D = BO1->getOperand(1);5384 }5385 5386 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.5387 if (A == Op1 && NoOp0WrapProblem)5388 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);5389 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.5390 if (C == Op0 && NoOp1WrapProblem)5391 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));5392 5393 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.5394 // (A - B) u>/u<= A --> B u>/u<= A5395 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))5396 return new ICmpInst(Pred, B, A);5397 // C u</u>= (C - D) --> C u</u>= D5398 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))5399 return new ICmpInst(Pred, C, D);5400 // (A - B) u>=/u< A --> B u>/u<= A iff B != 05401 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&5402 isKnownNonZero(B, Q))5403 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(Pred), B, A);5404 // C u<=/u> (C - D) --> C u</u>= D iff B != 05405 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&5406 isKnownNonZero(D, Q))5407 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(Pred), C, D);5408 5409 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.5410 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)5411 return new ICmpInst(Pred, A, C);5412 5413 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.5414 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)5415 return new ICmpInst(Pred, D, B);5416 5417 // icmp (0-X) < cst --> x > -cst5418 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {5419 Value *X;5420 if (match(BO0, m_Neg(m_Value(X))))5421 if (Constant *RHSC = dyn_cast<Constant>(Op1))5422 if (RHSC->isNotMinSignedValue())5423 return new ICmpInst(I.getSwappedPredicate(), X,5424 ConstantExpr::getNeg(RHSC));5425 }5426 5427 if (Instruction *R = foldICmpXorXX(I, Q, *this))5428 return R;5429 if (Instruction *R = foldICmpOrXX(I, Q, *this))5430 return R;5431 5432 {5433 // Try to remove shared multiplier from comparison:5434 // X * Z pred Y * Z5435 Value *X, *Y, *Z;5436 if ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&5437 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||5438 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&5439 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y))))) {5440 if (ICmpInst::isSigned(Pred)) {5441 if (Op0HasNSW && Op1HasNSW) {5442 KnownBits ZKnown = computeKnownBits(Z, &I);5443 if (ZKnown.isStrictlyPositive())5444 return new ICmpInst(Pred, X, Y);5445 if (ZKnown.isNegative())5446 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y);5447 Value *LessThan = simplifyICmpInst(ICmpInst::ICMP_SLT, X, Y,5448 SQ.getWithInstruction(&I));5449 if (LessThan && match(LessThan, m_One()))5450 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z,5451 Constant::getNullValue(Z->getType()));5452 Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y,5453 SQ.getWithInstruction(&I));5454 if (GreaterThan && match(GreaterThan, m_One()))5455 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));5456 }5457 } else {5458 bool NonZero;5459 if (ICmpInst::isEquality(Pred)) {5460 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 05461 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&5462 isKnownNonEqual(X, Y, SQ))5463 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));5464 5465 KnownBits ZKnown = computeKnownBits(Z, &I);5466 // if Z % 2 != 05467 // X * Z eq/ne Y * Z -> X eq/ne Y5468 if (ZKnown.countMaxTrailingZeros() == 0)5469 return new ICmpInst(Pred, X, Y);5470 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);5471 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)5472 // X * Z eq/ne Y * Z -> X eq/ne Y5473 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)5474 return new ICmpInst(Pred, X, Y);5475 } else5476 NonZero = isKnownNonZero(Z, Q);5477 5478 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)5479 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y5480 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)5481 return new ICmpInst(Pred, X, Y);5482 }5483 }5484 }5485 5486 BinaryOperator *SRem = nullptr;5487 // icmp (srem X, Y), Y5488 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))5489 SRem = BO0;5490 // icmp Y, (srem X, Y)5491 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&5492 Op0 == BO1->getOperand(1))5493 SRem = BO1;5494 if (SRem) {5495 // We don't check hasOneUse to avoid increasing register pressure because5496 // the value we use is the same value this instruction was already using.5497 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {5498 default:5499 break;5500 case ICmpInst::ICMP_EQ:5501 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));5502 case ICmpInst::ICMP_NE:5503 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));5504 case ICmpInst::ICMP_SGT:5505 case ICmpInst::ICMP_SGE:5506 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),5507 Constant::getAllOnesValue(SRem->getType()));5508 case ICmpInst::ICMP_SLT:5509 case ICmpInst::ICMP_SLE:5510 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),5511 Constant::getNullValue(SRem->getType()));5512 }5513 }5514 5515 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&5516 (BO0->hasOneUse() || BO1->hasOneUse()) &&5517 BO0->getOperand(1) == BO1->getOperand(1)) {5518 switch (BO0->getOpcode()) {5519 default:5520 break;5521 case Instruction::Add:5522 case Instruction::Sub:5523 case Instruction::Xor: {5524 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b5525 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));5526 5527 const APInt *C;5528 if (match(BO0->getOperand(1), m_APInt(C))) {5529 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b5530 if (C->isSignMask()) {5531 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();5532 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));5533 }5534 5535 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b5536 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {5537 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();5538 NewPred = I.getSwappedPredicate(NewPred);5539 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));5540 }5541 }5542 break;5543 }5544 case Instruction::Mul: {5545 if (!I.isEquality())5546 break;5547 5548 const APInt *C;5549 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&5550 !C->isOne()) {5551 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)5552 // Mask = -1 >> count-trailing-zeros(C).5553 if (unsigned TZs = C->countr_zero()) {5554 Constant *Mask = ConstantInt::get(5555 BO0->getType(),5556 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));5557 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);5558 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);5559 return new ICmpInst(Pred, And1, And2);5560 }5561 }5562 break;5563 }5564 case Instruction::UDiv:5565 case Instruction::LShr:5566 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())5567 break;5568 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));5569 5570 case Instruction::SDiv:5571 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||5572 !BO0->isExact() || !BO1->isExact())5573 break;5574 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));5575 5576 case Instruction::AShr:5577 if (!BO0->isExact() || !BO1->isExact())5578 break;5579 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));5580 5581 case Instruction::Shl: {5582 bool NUW = Op0HasNUW && Op1HasNUW;5583 bool NSW = Op0HasNSW && Op1HasNSW;5584 if (!NUW && !NSW)5585 break;5586 if (!NSW && I.isSigned())5587 break;5588 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));5589 }5590 }5591 }5592 5593 if (BO0) {5594 // Transform A & (L - 1) `ult` L --> L != 05595 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());5596 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);5597 5598 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {5599 auto *Zero = Constant::getNullValue(BO0->getType());5600 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);5601 }5602 }5603 5604 // For unsigned predicates / eq / ne:5605 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 05606 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x5607 if (!ICmpInst::isSigned(Pred)) {5608 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))5609 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,5610 Constant::getNullValue(Op1->getType()));5611 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))5612 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),5613 Constant::getNullValue(Op0->getType()), Op0);5614 }5615 5616 if (Value *V = foldMultiplicationOverflowCheck(I))5617 return replaceInstUsesWith(I, V);5618 5619 if (Instruction *R = foldICmpAndXX(I, Q, *this))5620 return R;5621 5622 if (Value *V = foldICmpWithTruncSignExtendedVal(I, Builder))5623 return replaceInstUsesWith(I, V);5624 5625 if (Value *V = foldShiftIntoShiftInAnotherHandOfAndInICmp(I, SQ, Builder))5626 return replaceInstUsesWith(I, V);5627 5628 return nullptr;5629}5630 5631/// Fold icmp Pred min|max(X, Y), Z.5632Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,5633 MinMaxIntrinsic *MinMax,5634 Value *Z, CmpPredicate Pred) {5635 Value *X = MinMax->getLHS();5636 Value *Y = MinMax->getRHS();5637 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())5638 return nullptr;5639 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {5640 // Revert the transform signed pred -> unsigned pred5641 // TODO: We can flip the signedness of predicate if both operands of icmp5642 // are negative.5643 if (isKnownNonNegative(Z, SQ.getWithInstruction(&I)) &&5644 isKnownNonNegative(MinMax, SQ.getWithInstruction(&I))) {5645 Pred = ICmpInst::getFlippedSignednessPredicate(Pred);5646 } else5647 return nullptr;5648 }5649 SimplifyQuery Q = SQ.getWithInstruction(&I);5650 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {5651 if (!Val)5652 return std::nullopt;5653 if (match(Val, m_One()))5654 return true;5655 if (match(Val, m_Zero()))5656 return false;5657 return std::nullopt;5658 };5659 // Remove samesign here since it is illegal to keep it when we speculatively5660 // execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`5661 // cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult5662 // -46, -32)`. `X` is allowed to be non-negative here.5663 Pred = Pred.dropSameSign();5664 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));5665 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));5666 if (!CmpXZ.has_value() && !CmpYZ.has_value())5667 return nullptr;5668 if (!CmpXZ.has_value()) {5669 std::swap(X, Y);5670 std::swap(CmpXZ, CmpYZ);5671 }5672 5673 auto FoldIntoCmpYZ = [&]() -> Instruction * {5674 if (CmpYZ.has_value())5675 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));5676 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);5677 };5678 5679 switch (Pred) {5680 case ICmpInst::ICMP_EQ:5681 case ICmpInst::ICMP_NE: {5682 // If X == Z:5683 // Expr Result5684 // min(X, Y) == Z X <= Y5685 // max(X, Y) == Z X >= Y5686 // min(X, Y) != Z X > Y5687 // max(X, Y) != Z X < Y5688 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {5689 ICmpInst::Predicate NewPred =5690 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());5691 if (Pred == ICmpInst::ICMP_NE)5692 NewPred = ICmpInst::getInversePredicate(NewPred);5693 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);5694 }5695 // Otherwise (X != Z):5696 ICmpInst::Predicate NewPred = MinMax->getPredicate();5697 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));5698 if (!MinMaxCmpXZ.has_value()) {5699 std::swap(X, Y);5700 std::swap(CmpXZ, CmpYZ);5701 // Re-check pre-condition X != Z5702 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)5703 break;5704 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));5705 }5706 if (!MinMaxCmpXZ.has_value())5707 break;5708 if (*MinMaxCmpXZ) {5709 // Expr Fact Result5710 // min(X, Y) == Z X < Z false5711 // max(X, Y) == Z X > Z false5712 // min(X, Y) != Z X < Z true5713 // max(X, Y) != Z X > Z true5714 return replaceInstUsesWith(5715 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));5716 } else {5717 // Expr Fact Result5718 // min(X, Y) == Z X > Z Y == Z5719 // max(X, Y) == Z X < Z Y == Z5720 // min(X, Y) != Z X > Z Y != Z5721 // max(X, Y) != Z X < Z Y != Z5722 return FoldIntoCmpYZ();5723 }5724 break;5725 }5726 case ICmpInst::ICMP_SLT:5727 case ICmpInst::ICMP_ULT:5728 case ICmpInst::ICMP_SLE:5729 case ICmpInst::ICMP_ULE:5730 case ICmpInst::ICMP_SGT:5731 case ICmpInst::ICMP_UGT:5732 case ICmpInst::ICMP_SGE:5733 case ICmpInst::ICMP_UGE: {5734 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);5735 if (*CmpXZ) {5736 if (IsSame) {5737 // Expr Fact Result5738 // min(X, Y) < Z X < Z true5739 // min(X, Y) <= Z X <= Z true5740 // max(X, Y) > Z X > Z true5741 // max(X, Y) >= Z X >= Z true5742 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));5743 } else {5744 // Expr Fact Result5745 // max(X, Y) < Z X < Z Y < Z5746 // max(X, Y) <= Z X <= Z Y <= Z5747 // min(X, Y) > Z X > Z Y > Z5748 // min(X, Y) >= Z X >= Z Y >= Z5749 return FoldIntoCmpYZ();5750 }5751 } else {5752 if (IsSame) {5753 // Expr Fact Result5754 // min(X, Y) < Z X >= Z Y < Z5755 // min(X, Y) <= Z X > Z Y <= Z5756 // max(X, Y) > Z X <= Z Y > Z5757 // max(X, Y) >= Z X < Z Y >= Z5758 return FoldIntoCmpYZ();5759 } else {5760 // Expr Fact Result5761 // max(X, Y) < Z X >= Z false5762 // max(X, Y) <= Z X > Z false5763 // min(X, Y) > Z X <= Z false5764 // min(X, Y) >= Z X < Z false5765 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));5766 }5767 }5768 break;5769 }5770 default:5771 break;5772 }5773 5774 return nullptr;5775}5776 5777/// Match and fold patterns like:5778/// icmp eq/ne X, min(max(X, Lo), Hi)5779/// which represents a range check and can be repsented as a ConstantRange.5780///5781/// For icmp eq, build ConstantRange [Lo, Hi + 1) and convert to:5782/// (X - Lo) u< (Hi + 1 - Lo)5783/// For icmp ne, build ConstantRange [Hi + 1, Lo) and convert to:5784/// (X - (Hi + 1)) u< (Lo - (Hi + 1))5785Instruction *InstCombinerImpl::foldICmpWithClamp(ICmpInst &I, Value *X,5786 MinMaxIntrinsic *Min) {5787 if (!I.isEquality() || !Min->hasOneUse() || !Min->isMin())5788 return nullptr;5789 5790 const APInt *Lo = nullptr, *Hi = nullptr;5791 if (Min->isSigned()) {5792 if (!match(Min->getLHS(), m_OneUse(m_SMax(m_Specific(X), m_APInt(Lo)))) ||5793 !match(Min->getRHS(), m_APInt(Hi)) || !Lo->slt(*Hi))5794 return nullptr;5795 } else {5796 if (!match(Min->getLHS(), m_OneUse(m_UMax(m_Specific(X), m_APInt(Lo)))) ||5797 !match(Min->getRHS(), m_APInt(Hi)) || !Lo->ult(*Hi))5798 return nullptr;5799 }5800 5801 ConstantRange CR = ConstantRange::getNonEmpty(*Lo, *Hi + 1);5802 ICmpInst::Predicate Pred;5803 APInt C, Offset;5804 if (I.getPredicate() == ICmpInst::ICMP_EQ)5805 CR.getEquivalentICmp(Pred, C, Offset);5806 else5807 CR.inverse().getEquivalentICmp(Pred, C, Offset);5808 5809 if (!Offset.isZero())5810 X = Builder.CreateAdd(X, ConstantInt::get(X->getType(), Offset));5811 5812 return replaceInstUsesWith(5813 I, Builder.CreateICmp(Pred, X, ConstantInt::get(X->getType(), C)));5814}5815 5816// Canonicalize checking for a power-of-2-or-zero value:5817static Instruction *foldICmpPow2Test(ICmpInst &I,5818 InstCombiner::BuilderTy &Builder) {5819 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);5820 const CmpInst::Predicate Pred = I.getPredicate();5821 Value *A = nullptr;5822 bool CheckIs;5823 if (I.isEquality()) {5824 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)5825 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)5826 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),5827 m_Deferred(A)))) ||5828 !match(Op1, m_ZeroInt()))5829 A = nullptr;5830 5831 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)5832 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)5833 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))5834 A = Op1;5835 else if (match(Op1,5836 m_OneUse(m_c_And(m_Neg(m_Specific(Op0)), m_Specific(Op0)))))5837 A = Op0;5838 5839 CheckIs = Pred == ICmpInst::ICMP_EQ;5840 } else if (ICmpInst::isUnsigned(Pred)) {5841 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)5842 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)5843 5844 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&5845 match(Op0, m_OneUse(m_c_Xor(m_Add(m_Specific(Op1), m_AllOnes()),5846 m_Specific(Op1))))) {5847 A = Op1;5848 CheckIs = Pred == ICmpInst::ICMP_UGE;5849 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&5850 match(Op1, m_OneUse(m_c_Xor(m_Add(m_Specific(Op0), m_AllOnes()),5851 m_Specific(Op0))))) {5852 A = Op0;5853 CheckIs = Pred == ICmpInst::ICMP_ULE;5854 }5855 }5856 5857 if (A) {5858 Type *Ty = A->getType();5859 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);5860 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,5861 ConstantInt::get(Ty, 2))5862 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,5863 ConstantInt::get(Ty, 1));5864 }5865 5866 return nullptr;5867}5868 5869/// Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.5870using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;5871static void collectOffsetOp(Value *V, SmallVectorImpl<OffsetOp> &Offsets,5872 bool AllowRecursion) {5873 Instruction *Inst = dyn_cast<Instruction>(V);5874 if (!Inst || !Inst->hasOneUse())5875 return;5876 5877 switch (Inst->getOpcode()) {5878 case Instruction::Add:5879 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(1));5880 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(0));5881 break;5882 case Instruction::Sub:5883 Offsets.emplace_back(Instruction::Add, Inst->getOperand(1));5884 break;5885 case Instruction::Xor:5886 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(1));5887 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(0));5888 break;5889 case Instruction::Shl:5890 if (Inst->hasNoSignedWrap())5891 Offsets.emplace_back(Instruction::AShr, Inst->getOperand(1));5892 if (Inst->hasNoUnsignedWrap())5893 Offsets.emplace_back(Instruction::LShr, Inst->getOperand(1));5894 break;5895 case Instruction::Select:5896 if (AllowRecursion) {5897 collectOffsetOp(Inst->getOperand(1), Offsets, /*AllowRecursion=*/false);5898 collectOffsetOp(Inst->getOperand(2), Offsets, /*AllowRecursion=*/false);5899 }5900 break;5901 default:5902 break;5903 }5904}5905 5906enum class OffsetKind { Invalid, Value, Select };5907 5908struct OffsetResult {5909 OffsetKind Kind;5910 Value *V0, *V1, *V2;5911 5912 static OffsetResult invalid() {5913 return {OffsetKind::Invalid, nullptr, nullptr, nullptr};5914 }5915 static OffsetResult value(Value *V) {5916 return {OffsetKind::Value, V, nullptr, nullptr};5917 }5918 static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV) {5919 return {OffsetKind::Select, Cond, TrueV, FalseV};5920 }5921 bool isValid() const { return Kind != OffsetKind::Invalid; }5922 Value *materialize(InstCombiner::BuilderTy &Builder) const {5923 switch (Kind) {5924 case OffsetKind::Invalid:5925 llvm_unreachable("Invalid offset result");5926 case OffsetKind::Value:5927 return V0;5928 case OffsetKind::Select:5929 return Builder.CreateSelect(V0, V1, V2);5930 }5931 llvm_unreachable("Unknown OffsetKind enum");5932 }5933};5934 5935/// Offset both sides of an equality icmp to see if we can save some5936/// instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.5937/// Note: This operation should not introduce poison.5938static Instruction *foldICmpEqualityWithOffset(ICmpInst &I,5939 InstCombiner::BuilderTy &Builder,5940 const SimplifyQuery &SQ) {5941 assert(I.isEquality() && "Expected an equality icmp");5942 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);5943 if (!Op0->getType()->isIntOrIntVectorTy())5944 return nullptr;5945 5946 SmallVector<OffsetOp, 4> OffsetOps;5947 collectOffsetOp(Op0, OffsetOps, /*AllowRecursion=*/true);5948 collectOffsetOp(Op1, OffsetOps, /*AllowRecursion=*/true);5949 5950 auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {5951 switch (BinOpc) {5952 // V = shl nsw X, RHS => X = ashr V, RHS5953 case Instruction::AShr: {5954 const APInt *CV, *CRHS;5955 if (!(match(V, m_APInt(CV)) && match(RHS, m_APInt(CRHS)) &&5956 CV->ashr(*CRHS).shl(*CRHS) == *CV) &&5957 !match(V, m_NSWShl(m_Value(), m_Specific(RHS))))5958 return nullptr;5959 break;5960 }5961 // V = shl nuw X, RHS => X = lshr V, RHS5962 case Instruction::LShr: {5963 const APInt *CV, *CRHS;5964 if (!(match(V, m_APInt(CV)) && match(RHS, m_APInt(CRHS)) &&5965 CV->lshr(*CRHS).shl(*CRHS) == *CV) &&5966 !match(V, m_NUWShl(m_Value(), m_Specific(RHS))))5967 return nullptr;5968 break;5969 }5970 default:5971 break;5972 }5973 5974 Value *Simplified = simplifyBinOp(BinOpc, V, RHS, SQ);5975 if (!Simplified)5976 return nullptr;5977 // Reject constant expressions as they don't simplify things.5978 if (isa<Constant>(Simplified) && !match(Simplified, m_ImmConstant()))5979 return nullptr;5980 // Check if the transformation introduces poison.5981 return impliesPoison(RHS, V) ? Simplified : nullptr;5982 };5983 5984 auto ApplyOffset = [&](Value *V, unsigned BinOpc,5985 Value *RHS) -> OffsetResult {5986 if (auto *Sel = dyn_cast<SelectInst>(V)) {5987 if (!Sel->hasOneUse())5988 return OffsetResult::invalid();5989 Value *TrueVal = ApplyOffsetImpl(Sel->getTrueValue(), BinOpc, RHS);5990 if (!TrueVal)5991 return OffsetResult::invalid();5992 Value *FalseVal = ApplyOffsetImpl(Sel->getFalseValue(), BinOpc, RHS);5993 if (!FalseVal)5994 return OffsetResult::invalid();5995 return OffsetResult::select(Sel->getCondition(), TrueVal, FalseVal);5996 }5997 if (Value *Simplified = ApplyOffsetImpl(V, BinOpc, RHS))5998 return OffsetResult::value(Simplified);5999 return OffsetResult::invalid();6000 };6001 6002 for (auto [BinOp, RHS] : OffsetOps) {6003 auto BinOpc = static_cast<unsigned>(BinOp);6004 6005 auto Op0Result = ApplyOffset(Op0, BinOpc, RHS);6006 if (!Op0Result.isValid())6007 continue;6008 auto Op1Result = ApplyOffset(Op1, BinOpc, RHS);6009 if (!Op1Result.isValid())6010 continue;6011 6012 Value *NewLHS = Op0Result.materialize(Builder);6013 Value *NewRHS = Op1Result.materialize(Builder);6014 return new ICmpInst(I.getPredicate(), NewLHS, NewRHS);6015 }6016 6017 return nullptr;6018}6019 6020Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {6021 if (!I.isEquality())6022 return nullptr;6023 6024 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);6025 const CmpInst::Predicate Pred = I.getPredicate();6026 Value *A, *B, *C, *D;6027 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {6028 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 06029 Value *OtherVal = A == Op1 ? B : A;6030 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));6031 }6032 6033 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {6034 // A^c1 == C^c2 --> A == C^(c1^c2)6035 ConstantInt *C1, *C2;6036 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&6037 Op1->hasOneUse()) {6038 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());6039 Value *Xor = Builder.CreateXor(C, NC);6040 return new ICmpInst(Pred, A, Xor);6041 }6042 6043 // A^B == A^D -> B == D6044 if (A == C)6045 return new ICmpInst(Pred, B, D);6046 if (A == D)6047 return new ICmpInst(Pred, B, C);6048 if (B == C)6049 return new ICmpInst(Pred, A, D);6050 if (B == D)6051 return new ICmpInst(Pred, A, C);6052 }6053 }6054 6055 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {6056 // A == (A^B) -> B == 06057 Value *OtherVal = A == Op0 ? B : A;6058 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));6059 }6060 6061 // (X&Z) == (Y&Z) -> (X^Y) & Z == 06062 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&6063 match(Op1, m_And(m_Value(C), m_Value(D)))) {6064 Value *X = nullptr, *Y = nullptr, *Z = nullptr;6065 6066 if (A == C) {6067 X = B;6068 Y = D;6069 Z = A;6070 } else if (A == D) {6071 X = B;6072 Y = C;6073 Z = A;6074 } else if (B == C) {6075 X = A;6076 Y = D;6077 Z = B;6078 } else if (B == D) {6079 X = A;6080 Y = C;6081 Z = B;6082 }6083 6084 if (X) {6085 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`6086 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional6087 // instructions.6088 const APInt *C0, *C1;6089 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&6090 (*C0 ^ *C1).isNegatedPowerOf2();6091 6092 // If either Op0/Op1 are both one use or X^Y will constant fold and one of6093 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral6094 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.6095 int UseCnt =6096 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +6097 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));6098 if (XorIsNegP2 || UseCnt >= 2) {6099 // Build (X^Y) & Z6100 Op1 = Builder.CreateXor(X, Y);6101 Op1 = Builder.CreateAnd(Op1, Z);6102 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));6103 }6104 }6105 }6106 6107 {6108 // Similar to above, but specialized for constant because invert is needed:6109 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 06110 Value *X, *Y;6111 Constant *C;6112 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&6113 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {6114 Value *Xor = Builder.CreateXor(X, Y);6115 Value *And = Builder.CreateAnd(Xor, ConstantExpr::getNot(C));6116 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));6117 }6118 }6119 6120 if (match(Op1, m_ZExt(m_Value(A))) &&6121 (Op0->hasOneUse() || Op1->hasOneUse())) {6122 // (B & (Pow2C-1)) == zext A --> A == trunc B6123 // (B & (Pow2C-1)) != zext A --> A != trunc B6124 const APInt *MaskC;6125 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&6126 MaskC->countr_one() == A->getType()->getScalarSizeInBits())6127 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));6128 }6129 6130 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)6131 // For lshr and ashr pairs.6132 const APInt *AP1, *AP2;6133 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&6134 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||6135 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&6136 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {6137 if (*AP1 != *AP2)6138 return nullptr;6139 unsigned TypeBits = AP1->getBitWidth();6140 unsigned ShAmt = AP1->getLimitedValue(TypeBits);6141 if (ShAmt < TypeBits && ShAmt != 0) {6142 ICmpInst::Predicate NewPred =6143 Pred == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;6144 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");6145 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);6146 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));6147 }6148 }6149 6150 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 06151 ConstantInt *Cst1;6152 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&6153 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {6154 unsigned TypeBits = Cst1->getBitWidth();6155 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);6156 if (ShAmt < TypeBits && ShAmt != 0) {6157 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");6158 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);6159 Value *And =6160 Builder.CreateAnd(Xor, Builder.getInt(AndVal), I.getName() + ".mask");6161 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));6162 }6163 }6164 6165 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to6166 // "icmp (and X, mask), cst"6167 uint64_t ShAmt = 0;6168 if (Op0->hasOneUse() &&6169 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&6170 match(Op1, m_ConstantInt(Cst1)) &&6171 // Only do this when A has multiple uses. This is most important to do6172 // when it exposes other optimizations.6173 !A->hasOneUse()) {6174 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();6175 6176 if (ShAmt < ASize) {6177 APInt MaskV =6178 APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits());6179 MaskV <<= ShAmt;6180 6181 APInt CmpV = Cst1->getValue().zext(ASize);6182 CmpV <<= ShAmt;6183 6184 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));6185 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));6186 }6187 }6188 6189 if (Instruction *ICmp = foldICmpIntrinsicWithIntrinsic(I, Builder))6190 return ICmp;6191 6192 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks6193 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s6194 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a6195 // few steps of instcombine.6196 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();6197 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&6198 match(Op1, m_Trunc(m_LShr(m_Specific(A), m_SpecificInt(BitWidth)))) &&6199 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&6200 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {6201 APInt C = APInt::getOneBitSet(BitWidth * 2, BitWidth - 1);6202 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));6203 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT6204 : ICmpInst::ICMP_UGE,6205 Add, ConstantInt::get(A->getType(), C.shl(1)));6206 }6207 6208 // Canonicalize:6209 // Assume B_Pow2 != 06210 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 06211 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 06212 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&6213 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, &I))6214 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,6215 ConstantInt::getNullValue(Op0->getType()));6216 6217 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&6218 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, &I))6219 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,6220 ConstantInt::getNullValue(Op1->getType()));6221 6222 // Canonicalize:6223 // icmp eq/ne X, OneUse(rotate-right(X))6224 // -> icmp eq/ne X, rotate-left(X)6225 // We generally try to convert rotate-right -> rotate-left, this just6226 // canonicalizes another case.6227 if (match(&I, m_c_ICmp(m_Value(A),6228 m_OneUse(m_Intrinsic<Intrinsic::fshr>(6229 m_Deferred(A), m_Deferred(A), m_Value(B))))))6230 return new ICmpInst(6231 Pred, A,6232 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));6233 6234 // Canonicalize:6235 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst6236 Constant *Cst;6237 if (match(&I, m_c_ICmp(m_OneUse(m_Xor(m_Value(A), m_ImmConstant(Cst))),6238 m_CombineAnd(m_Value(B), m_Unless(m_ImmConstant())))))6239 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);6240 6241 {6242 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)6243 auto m_Matcher =6244 m_CombineOr(m_CombineOr(m_c_Add(m_Value(B), m_Deferred(A)),6245 m_c_Xor(m_Value(B), m_Deferred(A))),6246 m_Sub(m_Value(B), m_Deferred(A)));6247 std::optional<bool> IsZero = std::nullopt;6248 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),6249 m_Deferred(A))))6250 IsZero = false;6251 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)6252 else if (match(&I,6253 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))6254 IsZero = true;6255 6256 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, &I))6257 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)6258 // -> (icmp eq/ne (and X, P2), 0)6259 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)6260 // -> (icmp eq/ne (and X, P2), P2)6261 return new ICmpInst(Pred, Builder.CreateAnd(B, A),6262 *IsZero ? A6263 : ConstantInt::getNullValue(A->getType()));6264 }6265 6266 if (auto *Res = foldICmpEqualityWithOffset(6267 I, Builder, getSimplifyQuery().getWithInstruction(&I)))6268 return Res;6269 6270 return nullptr;6271}6272 6273Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) {6274 ICmpInst::Predicate Pred = ICmp.getPredicate();6275 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);6276 6277 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.6278 // The trunc masks high bits while the compare may effectively mask low bits.6279 Value *X;6280 const APInt *C;6281 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))6282 return nullptr;6283 6284 // This matches patterns corresponding to tests of the signbit as well as:6285 // (trunc X) pred C2 --> (X & Mask) == C6286 if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*WithTrunc=*/true,6287 /*AllowNonZeroC=*/true)) {6288 Value *And = Builder.CreateAnd(Res->X, Res->Mask);6289 Constant *C = ConstantInt::get(Res->X->getType(), Res->C);6290 return new ICmpInst(Res->Pred, And, C);6291 }6292 6293 unsigned SrcBits = X->getType()->getScalarSizeInBits();6294 if (auto *II = dyn_cast<IntrinsicInst>(X)) {6295 if (II->getIntrinsicID() == Intrinsic::cttz ||6296 II->getIntrinsicID() == Intrinsic::ctlz) {6297 unsigned MaxRet = SrcBits;6298 // If the "is_zero_poison" argument is set, then we know at least6299 // one bit is set in the input, so the result is always at least one6300 // less than the full bitwidth of that input.6301 if (match(II->getArgOperand(1), m_One()))6302 MaxRet--;6303 6304 // Make sure the destination is wide enough to hold the largest output of6305 // the intrinsic.6306 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())6307 if (Instruction *I =6308 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))6309 return I;6310 }6311 }6312 6313 return nullptr;6314}6315 6316Instruction *InstCombinerImpl::foldICmpWithZextOrSext(ICmpInst &ICmp) {6317 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");6318 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));6319 Value *X;6320 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))6321 return nullptr;6322 6323 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;6324 bool IsSignedCmp = ICmp.isSigned();6325 6326 // icmp Pred (ext X), (ext Y)6327 Value *Y;6328 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {6329 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));6330 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));6331 6332 if (IsZext0 != IsZext1) {6333 // If X and Y and both i16334 // (icmp eq/ne (zext X) (sext Y))6335 // eq -> (icmp eq (or X, Y), 0)6336 // ne -> (icmp ne (or X, Y), 0)6337 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&6338 Y->getType()->isIntOrIntVectorTy(1))6339 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),6340 Constant::getNullValue(X->getType()));6341 6342 // If we have mismatched casts and zext has the nneg flag, we can6343 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.6344 6345 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));6346 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));6347 6348 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();6349 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();6350 6351 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))6352 IsSignedExt = true;6353 else6354 return nullptr;6355 }6356 6357 // Not an extension from the same type?6358 Type *XTy = X->getType(), *YTy = Y->getType();6359 if (XTy != YTy) {6360 // One of the casts must have one use because we are creating a new cast.6361 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())6362 return nullptr;6363 // Extend the narrower operand to the type of the wider operand.6364 CastInst::CastOps CastOpcode =6365 IsSignedExt ? Instruction::SExt : Instruction::ZExt;6366 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())6367 X = Builder.CreateCast(CastOpcode, X, YTy);6368 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())6369 Y = Builder.CreateCast(CastOpcode, Y, XTy);6370 else6371 return nullptr;6372 }6373 6374 // (zext X) == (zext Y) --> X == Y6375 // (sext X) == (sext Y) --> X == Y6376 if (ICmp.isEquality())6377 return new ICmpInst(ICmp.getPredicate(), X, Y);6378 6379 // A signed comparison of sign extended values simplifies into a6380 // signed comparison.6381 if (IsSignedCmp && IsSignedExt)6382 return new ICmpInst(ICmp.getPredicate(), X, Y);6383 6384 // The other three cases all fold into an unsigned comparison.6385 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);6386 }6387 6388 // Below here, we are only folding a compare with constant.6389 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));6390 if (!C)6391 return nullptr;6392 6393 // If a lossless truncate is possible...6394 Type *SrcTy = CastOp0->getSrcTy();6395 Constant *Res = getLosslessInvCast(C, SrcTy, CastOp0->getOpcode(), DL);6396 if (Res) {6397 if (ICmp.isEquality())6398 return new ICmpInst(ICmp.getPredicate(), X, Res);6399 6400 // A signed comparison of sign extended values simplifies into a6401 // signed comparison.6402 if (IsSignedExt && IsSignedCmp)6403 return new ICmpInst(ICmp.getPredicate(), X, Res);6404 6405 // The other three cases all fold into an unsigned comparison.6406 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);6407 }6408 6409 // The re-extended constant changed, partly changed (in the case of a vector),6410 // or could not be determined to be equal (in the case of a constant6411 // expression), so the constant cannot be represented in the shorter type.6412 // All the cases that fold to true or false will have already been handled6413 // by simplifyICmpInst, so only deal with the tricky case.6414 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))6415 return nullptr;6416 6417 // Is source op positive?6418 // icmp ult (sext X), C --> icmp sgt X, -16419 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)6420 return new ICmpInst(CmpInst::ICMP_SGT, X, Constant::getAllOnesValue(SrcTy));6421 6422 // Is source op negative?6423 // icmp ugt (sext X), C --> icmp slt X, 06424 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");6425 return new ICmpInst(CmpInst::ICMP_SLT, X, Constant::getNullValue(SrcTy));6426}6427 6428/// Handle icmp (cast x), (cast or constant).6429Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {6430 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as6431 // icmp compares only pointer's value.6432 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.6433 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));6434 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));6435 if (SimplifiedOp0 || SimplifiedOp1)6436 return new ICmpInst(ICmp.getPredicate(),6437 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),6438 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));6439 6440 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));6441 if (!CastOp0)6442 return nullptr;6443 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))6444 return nullptr;6445 6446 Value *Op0Src = CastOp0->getOperand(0);6447 Type *SrcTy = CastOp0->getSrcTy();6448 Type *DestTy = CastOp0->getDestTy();6449 6450 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the6451 // integer type is the same size as the pointer type.6452 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {6453 if (isa<VectorType>(PtrTy)) {6454 PtrTy = cast<VectorType>(PtrTy)->getElementType();6455 IntTy = cast<VectorType>(IntTy)->getElementType();6456 }6457 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();6458 };6459 if (CastOp0->getOpcode() == Instruction::PtrToInt &&6460 CompatibleSizes(SrcTy, DestTy)) {6461 Value *NewOp1 = nullptr;6462 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {6463 Value *PtrSrc = PtrToIntOp1->getOperand(0);6464 if (PtrSrc->getType() == Op0Src->getType())6465 NewOp1 = PtrToIntOp1->getOperand(0);6466 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {6467 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);6468 }6469 6470 if (NewOp1)6471 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);6472 }6473 6474 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).6475 if (CastOp0->getOpcode() == Instruction::IntToPtr &&6476 CompatibleSizes(DestTy, SrcTy)) {6477 Value *NewOp1 = nullptr;6478 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {6479 Value *IntSrc = IntToPtrOp1->getOperand(0);6480 if (IntSrc->getType() == Op0Src->getType())6481 NewOp1 = IntToPtrOp1->getOperand(0);6482 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {6483 NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);6484 }6485 6486 if (NewOp1)6487 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);6488 }6489 6490 if (Instruction *R = foldICmpWithTrunc(ICmp))6491 return R;6492 6493 return foldICmpWithZextOrSext(ICmp);6494}6495 6496static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS,6497 bool IsSigned) {6498 switch (BinaryOp) {6499 default:6500 llvm_unreachable("Unsupported binary op");6501 case Instruction::Add:6502 case Instruction::Sub:6503 return match(RHS, m_Zero());6504 case Instruction::Mul:6505 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&6506 match(RHS, m_One());6507 }6508}6509 6510OverflowResult6511InstCombinerImpl::computeOverflow(Instruction::BinaryOps BinaryOp,6512 bool IsSigned, Value *LHS, Value *RHS,6513 Instruction *CxtI) const {6514 switch (BinaryOp) {6515 default:6516 llvm_unreachable("Unsupported binary op");6517 case Instruction::Add:6518 if (IsSigned)6519 return computeOverflowForSignedAdd(LHS, RHS, CxtI);6520 else6521 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);6522 case Instruction::Sub:6523 if (IsSigned)6524 return computeOverflowForSignedSub(LHS, RHS, CxtI);6525 else6526 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);6527 case Instruction::Mul:6528 if (IsSigned)6529 return computeOverflowForSignedMul(LHS, RHS, CxtI);6530 else6531 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);6532 }6533}6534 6535bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,6536 bool IsSigned, Value *LHS,6537 Value *RHS, Instruction &OrigI,6538 Value *&Result,6539 Constant *&Overflow) {6540 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))6541 std::swap(LHS, RHS);6542 6543 // If the overflow check was an add followed by a compare, the insertion point6544 // may be pointing to the compare. We want to insert the new instructions6545 // before the add in case there are uses of the add between the add and the6546 // compare.6547 Builder.SetInsertPoint(&OrigI);6548 6549 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());6550 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))6551 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());6552 6553 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {6554 Result = LHS;6555 Overflow = ConstantInt::getFalse(OverflowTy);6556 return true;6557 }6558 6559 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {6560 case OverflowResult::MayOverflow:6561 return false;6562 case OverflowResult::AlwaysOverflowsLow:6563 case OverflowResult::AlwaysOverflowsHigh:6564 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);6565 Result->takeName(&OrigI);6566 Overflow = ConstantInt::getTrue(OverflowTy);6567 return true;6568 case OverflowResult::NeverOverflows:6569 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);6570 Result->takeName(&OrigI);6571 Overflow = ConstantInt::getFalse(OverflowTy);6572 if (auto *Inst = dyn_cast<Instruction>(Result)) {6573 if (IsSigned)6574 Inst->setHasNoSignedWrap();6575 else6576 Inst->setHasNoUnsignedWrap();6577 }6578 return true;6579 }6580 6581 llvm_unreachable("Unexpected overflow result");6582}6583 6584/// Recognize and process idiom involving test for multiplication6585/// overflow.6586///6587/// The caller has matched a pattern of the form:6588/// I = cmp u (mul(zext A, zext B), V6589/// The function checks if this is a test for overflow and if so replaces6590/// multiplication with call to 'mul.with.overflow' intrinsic.6591///6592/// \param I Compare instruction.6593/// \param MulVal Result of 'mult' instruction. It is one of the arguments of6594/// the compare instruction. Must be of integer type.6595/// \param OtherVal The other argument of compare instruction.6596/// \returns Instruction which must replace the compare instruction, NULL if no6597/// replacement required.6598static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal,6599 const APInt *OtherVal,6600 InstCombinerImpl &IC) {6601 // Don't bother doing this transformation for pointers, don't do it for6602 // vectors.6603 if (!isa<IntegerType>(MulVal->getType()))6604 return nullptr;6605 6606 auto *MulInstr = dyn_cast<Instruction>(MulVal);6607 if (!MulInstr)6608 return nullptr;6609 assert(MulInstr->getOpcode() == Instruction::Mul);6610 6611 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),6612 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));6613 assert(LHS->getOpcode() == Instruction::ZExt);6614 assert(RHS->getOpcode() == Instruction::ZExt);6615 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);6616 6617 // Calculate type and width of the result produced by mul.with.overflow.6618 Type *TyA = A->getType(), *TyB = B->getType();6619 unsigned WidthA = TyA->getPrimitiveSizeInBits(),6620 WidthB = TyB->getPrimitiveSizeInBits();6621 unsigned MulWidth;6622 Type *MulType;6623 if (WidthB > WidthA) {6624 MulWidth = WidthB;6625 MulType = TyB;6626 } else {6627 MulWidth = WidthA;6628 MulType = TyA;6629 }6630 6631 // In order to replace the original mul with a narrower mul.with.overflow,6632 // all uses must ignore upper bits of the product. The number of used low6633 // bits must be not greater than the width of mul.with.overflow.6634 if (MulVal->hasNUsesOrMore(2))6635 for (User *U : MulVal->users()) {6636 if (U == &I)6637 continue;6638 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {6639 // Check if truncation ignores bits above MulWidth.6640 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();6641 if (TruncWidth > MulWidth)6642 return nullptr;6643 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {6644 // Check if AND ignores bits above MulWidth.6645 if (BO->getOpcode() != Instruction::And)6646 return nullptr;6647 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {6648 const APInt &CVal = CI->getValue();6649 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)6650 return nullptr;6651 } else {6652 // In this case we could have the operand of the binary operation6653 // being defined in another block, and performing the replacement6654 // could break the dominance relation.6655 return nullptr;6656 }6657 } else {6658 // Other uses prohibit this transformation.6659 return nullptr;6660 }6661 }6662 6663 // Recognize patterns6664 switch (I.getPredicate()) {6665 case ICmpInst::ICMP_UGT: {6666 // Recognize pattern:6667 // mulval = mul(zext A, zext B)6668 // cmp ugt mulval, max6669 APInt MaxVal = APInt::getMaxValue(MulWidth);6670 MaxVal = MaxVal.zext(OtherVal->getBitWidth());6671 if (MaxVal.eq(*OtherVal))6672 break; // Recognized6673 return nullptr;6674 }6675 6676 case ICmpInst::ICMP_ULT: {6677 // Recognize pattern:6678 // mulval = mul(zext A, zext B)6679 // cmp ule mulval, max + 16680 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);6681 if (MaxVal.eq(*OtherVal))6682 break; // Recognized6683 return nullptr;6684 }6685 6686 default:6687 return nullptr;6688 }6689 6690 InstCombiner::BuilderTy &Builder = IC.Builder;6691 Builder.SetInsertPoint(MulInstr);6692 6693 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)6694 Value *MulA = A, *MulB = B;6695 if (WidthA < MulWidth)6696 MulA = Builder.CreateZExt(A, MulType);6697 if (WidthB < MulWidth)6698 MulB = Builder.CreateZExt(B, MulType);6699 CallInst *Call =6700 Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, MulType,6701 {MulA, MulB}, /*FMFSource=*/nullptr, "umul");6702 IC.addToWorklist(MulInstr);6703 6704 // If there are uses of mul result other than the comparison, we know that6705 // they are truncation or binary AND. Change them to use result of6706 // mul.with.overflow and adjust properly mask/size.6707 if (MulVal->hasNUsesOrMore(2)) {6708 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");6709 for (User *U : make_early_inc_range(MulVal->users())) {6710 if (U == &I)6711 continue;6712 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {6713 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)6714 IC.replaceInstUsesWith(*TI, Mul);6715 else6716 TI->setOperand(0, Mul);6717 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {6718 assert(BO->getOpcode() == Instruction::And);6719 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)6720 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));6721 APInt ShortMask = CI->getValue().trunc(MulWidth);6722 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);6723 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());6724 IC.replaceInstUsesWith(*BO, Zext);6725 } else {6726 llvm_unreachable("Unexpected Binary operation");6727 }6728 IC.addToWorklist(cast<Instruction>(U));6729 }6730 }6731 6732 // The original icmp gets replaced with the overflow value, maybe inverted6733 // depending on predicate.6734 if (I.getPredicate() == ICmpInst::ICMP_ULT) {6735 Value *Res = Builder.CreateExtractValue(Call, 1);6736 return BinaryOperator::CreateNot(Res);6737 }6738 6739 return ExtractValueInst::Create(Call, 1);6740}6741 6742/// When performing a comparison against a constant, it is possible that not all6743/// the bits in the LHS are demanded. This helper method computes the mask that6744/// IS demanded.6745static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth) {6746 const APInt *RHS;6747 if (!match(I.getOperand(1), m_APInt(RHS)))6748 return APInt::getAllOnes(BitWidth);6749 6750 // If this is a normal comparison, it demands all bits. If it is a sign bit6751 // comparison, it only demands the sign bit.6752 bool UnusedBit;6753 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))6754 return APInt::getSignMask(BitWidth);6755 6756 switch (I.getPredicate()) {6757 // For a UGT comparison, we don't care about any bits that6758 // correspond to the trailing ones of the comparand. The value of these6759 // bits doesn't impact the outcome of the comparison, because any value6760 // greater than the RHS must differ in a bit higher than these due to carry.6761 case ICmpInst::ICMP_UGT:6762 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());6763 6764 // Similarly, for a ULT comparison, we don't care about the trailing zeros.6765 // Any value less than the RHS must differ in a higher bit because of carries.6766 case ICmpInst::ICMP_ULT:6767 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());6768 6769 default:6770 return APInt::getAllOnes(BitWidth);6771 }6772}6773 6774/// Check that one use is in the same block as the definition and all6775/// other uses are in blocks dominated by a given block.6776///6777/// \param DI Definition6778/// \param UI Use6779/// \param DB Block that must dominate all uses of \p DI outside6780/// the parent block6781/// \return true when \p UI is the only use of \p DI in the parent block6782/// and all other uses of \p DI are in blocks dominated by \p DB.6783///6784bool InstCombinerImpl::dominatesAllUses(const Instruction *DI,6785 const Instruction *UI,6786 const BasicBlock *DB) const {6787 assert(DI && UI && "Instruction not defined\n");6788 // Ignore incomplete definitions.6789 if (!DI->getParent())6790 return false;6791 // DI and UI must be in the same block.6792 if (DI->getParent() != UI->getParent())6793 return false;6794 // Protect from self-referencing blocks.6795 if (DI->getParent() == DB)6796 return false;6797 for (const User *U : DI->users()) {6798 auto *Usr = cast<Instruction>(U);6799 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))6800 return false;6801 }6802 return true;6803}6804 6805/// Return true when the instruction sequence within a block is select-cmp-br.6806static bool isChainSelectCmpBranch(const SelectInst *SI) {6807 const BasicBlock *BB = SI->getParent();6808 if (!BB)6809 return false;6810 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());6811 if (!BI || BI->getNumSuccessors() != 2)6812 return false;6813 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());6814 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))6815 return false;6816 return true;6817}6818 6819/// True when a select result is replaced by one of its operands6820/// in select-icmp sequence. This will eventually result in the elimination6821/// of the select.6822///6823/// \param SI Select instruction6824/// \param Icmp Compare instruction6825/// \param SIOpd Operand that replaces the select6826///6827/// Notes:6828/// - The replacement is global and requires dominator information6829/// - The caller is responsible for the actual replacement6830///6831/// Example:6832///6833/// entry:6834/// %4 = select i1 %3, %C* %0, %C* null6835/// %5 = icmp eq %C* %4, null6836/// br i1 %5, label %9, label %76837/// ...6838/// ; <label>:7 ; preds = %entry6839/// %8 = getelementptr inbounds %C* %4, i64 0, i32 06840/// ...6841///6842/// can be transformed to6843///6844/// %5 = icmp eq %C* %0, null6845/// %6 = select i1 %3, i1 %5, i1 true6846/// br i1 %6, label %9, label %76847/// ...6848/// ; <label>:7 ; preds = %entry6849/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!6850///6851/// Similar when the first operand of the select is a constant or/and6852/// the compare is for not equal rather than equal.6853///6854/// NOTE: The function is only called when the select and compare constants6855/// are equal, the optimization can work only for EQ predicates. This is not a6856/// major restriction since a NE compare should be 'normalized' to an equal6857/// compare, which usually happens in the combiner and test case6858/// select-cmp-br.ll checks for it.6859bool InstCombinerImpl::replacedSelectWithOperand(SelectInst *SI,6860 const ICmpInst *Icmp,6861 const unsigned SIOpd) {6862 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");6863 if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) {6864 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);6865 // The check for the single predecessor is not the best that can be6866 // done. But it protects efficiently against cases like when SI's6867 // home block has two successors, Succ and Succ1, and Succ1 predecessor6868 // of Succ. Then SI can't be replaced by SIOpd because the use that gets6869 // replaced can be reached on either path. So the uniqueness check6870 // guarantees that the path all uses of SI (outside SI's parent) are on6871 // is disjoint from all other paths out of SI. But that information6872 // is more expensive to compute, and the trade-off here is in favor6873 // of compile-time. It should also be noticed that we check for a single6874 // predecessor and not only uniqueness. This to handle the situation when6875 // Succ and Succ1 points to the same basic block.6876 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {6877 NumSel++;6878 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());6879 return true;6880 }6881 }6882 return false;6883}6884 6885/// Try to fold the comparison based on range information we can get by checking6886/// whether bits are known to be zero or one in the inputs.6887Instruction *InstCombinerImpl::foldICmpUsingKnownBits(ICmpInst &I) {6888 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);6889 Type *Ty = Op0->getType();6890 ICmpInst::Predicate Pred = I.getPredicate();6891 6892 // Get scalar or pointer size.6893 unsigned BitWidth = Ty->isIntOrIntVectorTy()6894 ? Ty->getScalarSizeInBits()6895 : DL.getPointerTypeSizeInBits(Ty->getScalarType());6896 6897 if (!BitWidth)6898 return nullptr;6899 6900 KnownBits Op0Known(BitWidth);6901 KnownBits Op1Known(BitWidth);6902 6903 {6904 // Don't use dominating conditions when folding icmp using known bits. This6905 // may convert signed into unsigned predicates in ways that other passes6906 // (especially IndVarSimplify) may not be able to reliably undo.6907 SimplifyQuery Q = SQ.getWithoutDomCondCache().getWithInstruction(&I);6908 if (SimplifyDemandedBits(&I, 0, getDemandedBitsLHSMask(I, BitWidth),6909 Op0Known, Q))6910 return &I;6911 6912 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnes(BitWidth), Op1Known, Q))6913 return &I;6914 }6915 6916 if (!isa<Constant>(Op0) && Op0Known.isConstant())6917 return new ICmpInst(6918 Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1);6919 if (!isa<Constant>(Op1) && Op1Known.isConstant())6920 return new ICmpInst(6921 Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Known.getConstant()));6922 6923 if (std::optional<bool> Res = ICmpInst::compare(Op0Known, Op1Known, Pred))6924 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *Res));6925 6926 // Given the known and unknown bits, compute a range that the LHS could be6927 // in. Compute the Min, Max and RHS values based on the known bits. For the6928 // EQ and NE we use unsigned values.6929 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);6930 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);6931 if (I.isSigned()) {6932 Op0Min = Op0Known.getSignedMinValue();6933 Op0Max = Op0Known.getSignedMaxValue();6934 Op1Min = Op1Known.getSignedMinValue();6935 Op1Max = Op1Known.getSignedMaxValue();6936 } else {6937 Op0Min = Op0Known.getMinValue();6938 Op0Max = Op0Known.getMaxValue();6939 Op1Min = Op1Known.getMinValue();6940 Op1Max = Op1Known.getMaxValue();6941 }6942 6943 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a6944 // min/max canonical compare with some other compare. That could lead to6945 // conflict with select canonicalization and infinite looping.6946 // FIXME: This constraint may go away if min/max intrinsics are canonical.6947 auto isMinMaxCmp = [&](Instruction &Cmp) {6948 if (!Cmp.hasOneUse())6949 return false;6950 Value *A, *B;6951 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;6952 if (!SelectPatternResult::isMinOrMax(SPF))6953 return false;6954 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||6955 match(Op1, m_MaxOrMin(m_Value(), m_Value()));6956 };6957 if (!isMinMaxCmp(I)) {6958 switch (Pred) {6959 default:6960 break;6961 case ICmpInst::ICMP_ULT: {6962 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)6963 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);6964 const APInt *CmpC;6965 if (match(Op1, m_APInt(CmpC))) {6966 // A <u C -> A == C-1 if min(A)+1 == C6967 if (*CmpC == Op0Min + 1)6968 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,6969 ConstantInt::get(Op1->getType(), *CmpC - 1));6970 // X <u C --> X == 0, if the number of zero bits in the bottom of X6971 // exceeds the log2 of C.6972 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())6973 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,6974 Constant::getNullValue(Op1->getType()));6975 }6976 break;6977 }6978 case ICmpInst::ICMP_UGT: {6979 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)6980 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);6981 const APInt *CmpC;6982 if (match(Op1, m_APInt(CmpC))) {6983 // A >u C -> A == C+1 if max(a)-1 == C6984 if (*CmpC == Op0Max - 1)6985 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,6986 ConstantInt::get(Op1->getType(), *CmpC + 1));6987 // X >u C --> X != 0, if the number of zero bits in the bottom of X6988 // exceeds the log2 of C.6989 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())6990 return new ICmpInst(ICmpInst::ICMP_NE, Op0,6991 Constant::getNullValue(Op1->getType()));6992 }6993 break;6994 }6995 case ICmpInst::ICMP_SLT: {6996 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)6997 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);6998 const APInt *CmpC;6999 if (match(Op1, m_APInt(CmpC))) {7000 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C7001 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,7002 ConstantInt::get(Op1->getType(), *CmpC - 1));7003 }7004 break;7005 }7006 case ICmpInst::ICMP_SGT: {7007 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)7008 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);7009 const APInt *CmpC;7010 if (match(Op1, m_APInt(CmpC))) {7011 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C7012 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,7013 ConstantInt::get(Op1->getType(), *CmpC + 1));7014 }7015 break;7016 }7017 }7018 }7019 7020 // Based on the range information we know about the LHS, see if we can7021 // simplify this comparison. For example, (x&4) < 8 is always true.7022 switch (Pred) {7023 default:7024 break;7025 case ICmpInst::ICMP_EQ:7026 case ICmpInst::ICMP_NE: {7027 // If all bits are known zero except for one, then we know at most one bit7028 // is set. If the comparison is against zero, then this is a check to see if7029 // *that* bit is set.7030 APInt Op0KnownZeroInverted = ~Op0Known.Zero;7031 if (Op1Known.isZero()) {7032 // If the LHS is an AND with the same constant, look through it.7033 Value *LHS = nullptr;7034 const APInt *LHSC;7035 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||7036 *LHSC != Op0KnownZeroInverted)7037 LHS = Op0;7038 7039 Value *X;7040 const APInt *C1;7041 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {7042 Type *XTy = X->getType();7043 unsigned Log2C1 = C1->countr_zero();7044 APInt C2 = Op0KnownZeroInverted;7045 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;7046 if (C2Pow2.isPowerOf2()) {7047 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):7048 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))7049 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))7050 unsigned Log2C2 = C2Pow2.countr_zero();7051 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);7052 auto NewPred =7053 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT;7054 return new ICmpInst(NewPred, X, CmpC);7055 }7056 }7057 }7058 7059 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.7060 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&7061 (Op0Known & Op1Known) == Op0Known)7062 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,7063 ConstantInt::getNullValue(Op1->getType()));7064 break;7065 }7066 case ICmpInst::ICMP_SGE:7067 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)7068 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);7069 break;7070 case ICmpInst::ICMP_SLE:7071 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)7072 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);7073 break;7074 case ICmpInst::ICMP_UGE:7075 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)7076 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);7077 break;7078 case ICmpInst::ICMP_ULE:7079 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)7080 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);7081 break;7082 }7083 7084 // Turn a signed comparison into an unsigned one if both operands are known to7085 // have the same sign. Set samesign if possible (except for equality7086 // predicates).7087 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&7088 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||7089 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {7090 I.setPredicate(I.getUnsignedPredicate());7091 I.setSameSign();7092 return &I;7093 }7094 7095 return nullptr;7096}7097 7098/// If one operand of an icmp is effectively a bool (value range of {0,1}),7099/// then try to reduce patterns based on that limit.7100Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {7101 Value *X, *Y;7102 CmpPredicate Pred;7103 7104 // X must be 0 and bool must be true for "ULT":7105 // X <u (zext i1 Y) --> (X == 0) & Y7106 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&7107 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)7108 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);7109 7110 // X must be 0 or bool must be true for "ULE":7111 // X <=u (sext i1 Y) --> (X == 0) | Y7112 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&7113 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)7114 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);7115 7116 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))7117 CmpPredicate Pred1, Pred2;7118 const APInt *C;7119 Instruction *ExtI;7120 if (match(&I, m_c_ICmp(Pred1, m_Value(X),7121 m_CombineAnd(m_Instruction(ExtI),7122 m_ZExtOrSExt(m_ICmp(Pred2, m_Deferred(X),7123 m_APInt(C)))))) &&7124 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {7125 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;7126 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();7127 auto CreateRangeCheck = [&] {7128 Value *CmpV1 =7129 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));7130 Value *CmpV2 = Builder.CreateICmp(7131 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));7132 return BinaryOperator::Create(7133 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,7134 CmpV1, CmpV2);7135 };7136 if (C->isZero()) {7137 if (Pred2 == ICmpInst::ICMP_EQ) {7138 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false7139 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true7140 return replaceInstUsesWith(7141 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));7142 } else if (!IsSExt || HasOneUse) {7143 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 17144 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 17145 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -17146 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X != -17147 return CreateRangeCheck();7148 }7149 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {7150 if (Pred2 == ICmpInst::ICMP_NE) {7151 // icmp eq X, (zext (icmp ne X, 1)) --> false7152 // icmp ne X, (zext (icmp ne X, 1)) --> true7153 // icmp eq X, (sext (icmp ne X, -1)) --> false7154 // icmp ne X, (sext (icmp ne X, -1)) --> true7155 return replaceInstUsesWith(7156 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));7157 } else if (!IsSExt || HasOneUse) {7158 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 17159 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 17160 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -17161 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -17162 return CreateRangeCheck();7163 }7164 } else {7165 // when C != 0 && C != 1:7166 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 07167 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 17168 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 07169 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 17170 // when C != 0 && C != -1:7171 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 07172 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -17173 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 07174 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -17175 return ICmpInst::Create(7176 Instruction::ICmp, Pred1, X,7177 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE7178 ? (IsSExt ? -1 : 1)7179 : 0));7180 }7181 }7182 7183 return nullptr;7184}7185 7186/// If we have an icmp le or icmp ge instruction with a constant operand, turn7187/// it into the appropriate icmp lt or icmp gt instruction. This transform7188/// allows them to be folded in visitICmpInst.7189static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {7190 ICmpInst::Predicate Pred = I.getPredicate();7191 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||7192 InstCombiner::isCanonicalPredicate(Pred))7193 return nullptr;7194 7195 Value *Op0 = I.getOperand(0);7196 Value *Op1 = I.getOperand(1);7197 auto *Op1C = dyn_cast<Constant>(Op1);7198 if (!Op1C)7199 return nullptr;7200 7201 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);7202 if (!FlippedStrictness)7203 return nullptr;7204 7205 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);7206}7207 7208/// If we have a comparison with a non-canonical predicate, if we can update7209/// all the users, invert the predicate and adjust all the users.7210CmpInst *InstCombinerImpl::canonicalizeICmpPredicate(CmpInst &I) {7211 // Is the predicate already canonical?7212 CmpInst::Predicate Pred = I.getPredicate();7213 if (InstCombiner::isCanonicalPredicate(Pred))7214 return nullptr;7215 7216 // Can all users be adjusted to predicate inversion?7217 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))7218 return nullptr;7219 7220 // Ok, we can canonicalize comparison!7221 // Let's first invert the comparison's predicate.7222 I.setPredicate(CmpInst::getInversePredicate(Pred));7223 I.setName(I.getName() + ".not");7224 7225 // And, adapt users.7226 freelyInvertAllUsersOf(&I);7227 7228 return &I;7229}7230 7231/// Integer compare with boolean values can always be turned into bitwise ops.7232static Instruction *canonicalizeICmpBool(ICmpInst &I,7233 InstCombiner::BuilderTy &Builder) {7234 Value *A = I.getOperand(0), *B = I.getOperand(1);7235 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");7236 7237 // A boolean compared to true/false can be simplified to Op0/true/false in7238 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.7239 // Cases not handled by InstSimplify are always 'not' of Op0.7240 if (match(B, m_Zero())) {7241 switch (I.getPredicate()) {7242 case CmpInst::ICMP_EQ: // A == 0 -> !A7243 case CmpInst::ICMP_ULE: // A <=u 0 -> !A7244 case CmpInst::ICMP_SGE: // A >=s 0 -> !A7245 return BinaryOperator::CreateNot(A);7246 default:7247 llvm_unreachable("ICmp i1 X, C not simplified as expected.");7248 }7249 } else if (match(B, m_One())) {7250 switch (I.getPredicate()) {7251 case CmpInst::ICMP_NE: // A != 1 -> !A7252 case CmpInst::ICMP_ULT: // A <u 1 -> !A7253 case CmpInst::ICMP_SGT: // A >s -1 -> !A7254 return BinaryOperator::CreateNot(A);7255 default:7256 llvm_unreachable("ICmp i1 X, C not simplified as expected.");7257 }7258 }7259 7260 switch (I.getPredicate()) {7261 default:7262 llvm_unreachable("Invalid icmp instruction!");7263 case ICmpInst::ICMP_EQ:7264 // icmp eq i1 A, B -> ~(A ^ B)7265 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));7266 7267 case ICmpInst::ICMP_NE:7268 // icmp ne i1 A, B -> A ^ B7269 return BinaryOperator::CreateXor(A, B);7270 7271 case ICmpInst::ICMP_UGT:7272 // icmp ugt -> icmp ult7273 std::swap(A, B);7274 [[fallthrough]];7275 case ICmpInst::ICMP_ULT:7276 // icmp ult i1 A, B -> ~A & B7277 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);7278 7279 case ICmpInst::ICMP_SGT:7280 // icmp sgt -> icmp slt7281 std::swap(A, B);7282 [[fallthrough]];7283 case ICmpInst::ICMP_SLT:7284 // icmp slt i1 A, B -> A & ~B7285 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);7286 7287 case ICmpInst::ICMP_UGE:7288 // icmp uge -> icmp ule7289 std::swap(A, B);7290 [[fallthrough]];7291 case ICmpInst::ICMP_ULE:7292 // icmp ule i1 A, B -> ~A | B7293 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);7294 7295 case ICmpInst::ICMP_SGE:7296 // icmp sge -> icmp sle7297 std::swap(A, B);7298 [[fallthrough]];7299 case ICmpInst::ICMP_SLE:7300 // icmp sle i1 A, B -> A | ~B7301 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);7302 }7303}7304 7305// Transform pattern like:7306// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X7307// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X7308// Into:7309// (X l>> Y) != 07310// (X l>> Y) == 07311static Instruction *foldICmpWithHighBitMask(ICmpInst &Cmp,7312 InstCombiner::BuilderTy &Builder) {7313 CmpPredicate Pred, NewPred;7314 Value *X, *Y;7315 if (match(&Cmp,7316 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {7317 switch (Pred) {7318 case ICmpInst::ICMP_ULE:7319 NewPred = ICmpInst::ICMP_NE;7320 break;7321 case ICmpInst::ICMP_UGT:7322 NewPred = ICmpInst::ICMP_EQ;7323 break;7324 default:7325 return nullptr;7326 }7327 } else if (match(&Cmp, m_c_ICmp(Pred,7328 m_OneUse(m_CombineOr(7329 m_Not(m_Shl(m_AllOnes(), m_Value(Y))),7330 m_Add(m_Shl(m_One(), m_Value(Y)),7331 m_AllOnes()))),7332 m_Value(X)))) {7333 // The variant with 'add' is not canonical, (the variant with 'not' is)7334 // we only get it because it has extra uses, and can't be canonicalized,7335 7336 switch (Pred) {7337 case ICmpInst::ICMP_ULT:7338 NewPred = ICmpInst::ICMP_NE;7339 break;7340 case ICmpInst::ICMP_UGE:7341 NewPred = ICmpInst::ICMP_EQ;7342 break;7343 default:7344 return nullptr;7345 }7346 } else7347 return nullptr;7348 7349 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");7350 Constant *Zero = Constant::getNullValue(NewX->getType());7351 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);7352}7353 7354static Instruction *foldVectorCmp(CmpInst &Cmp,7355 InstCombiner::BuilderTy &Builder) {7356 const CmpInst::Predicate Pred = Cmp.getPredicate();7357 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);7358 Value *V1, *V2;7359 7360 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {7361 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());7362 if (auto *I = dyn_cast<Instruction>(V))7363 I->copyIRFlags(&Cmp);7364 Module *M = Cmp.getModule();7365 Function *F = Intrinsic::getOrInsertDeclaration(7366 M, Intrinsic::vector_reverse, V->getType());7367 return CallInst::Create(F, V);7368 };7369 7370 if (match(LHS, m_VecReverse(m_Value(V1)))) {7371 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)7372 if (match(RHS, m_VecReverse(m_Value(V2))) &&7373 (LHS->hasOneUse() || RHS->hasOneUse()))7374 return createCmpReverse(Pred, V1, V2);7375 7376 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)7377 if (LHS->hasOneUse() && isSplatValue(RHS))7378 return createCmpReverse(Pred, V1, RHS);7379 }7380 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)7381 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))7382 return createCmpReverse(Pred, LHS, V2);7383 7384 ArrayRef<int> M;7385 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))7386 return nullptr;7387 7388 // If both arguments of the cmp are shuffles that use the same mask and7389 // shuffle within a single vector, move the shuffle after the cmp:7390 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M7391 Type *V1Ty = V1->getType();7392 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&7393 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {7394 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);7395 return new ShuffleVectorInst(NewCmp, M);7396 }7397 7398 // Try to canonicalize compare with splatted operand and splat constant.7399 // TODO: We could generalize this for more than splats. See/use the code in7400 // InstCombiner::foldVectorBinop().7401 Constant *C;7402 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))7403 return nullptr;7404 7405 // Length-changing splats are ok, so adjust the constants as needed:7406 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M7407 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);7408 int MaskSplatIndex;7409 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {7410 // We allow poison in matching, but this transform removes it for safety.7411 // Demanded elements analysis should be able to recover some/all of that.7412 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),7413 ScalarC);7414 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);7415 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);7416 return new ShuffleVectorInst(NewCmp, NewM);7417 }7418 7419 return nullptr;7420}7421 7422// extract(uadd.with.overflow(A, B), 0) ult A7423// -> extract(uadd.with.overflow(A, B), 1)7424static Instruction *foldICmpOfUAddOv(ICmpInst &I) {7425 CmpInst::Predicate Pred = I.getPredicate();7426 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);7427 7428 Value *UAddOv;7429 Value *A, *B;7430 auto UAddOvResultPat = m_ExtractValue<0>(7431 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));7432 if (match(Op0, UAddOvResultPat) &&7433 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||7434 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&7435 (match(A, m_One()) || match(B, m_One()))) ||7436 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&7437 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))7438 // extract(uadd.with.overflow(A, B), 0) < A7439 // extract(uadd.with.overflow(A, 1), 0) == 07440 // extract(uadd.with.overflow(A, -1), 0) != -17441 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();7442 else if (match(Op1, UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&7443 (Op0 == A || Op0 == B))7444 // A > extract(uadd.with.overflow(A, B), 0)7445 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();7446 else7447 return nullptr;7448 7449 return ExtractValueInst::Create(UAddOv, 1);7450}7451 7452static Instruction *foldICmpInvariantGroup(ICmpInst &I) {7453 if (!I.getOperand(0)->getType()->isPointerTy() ||7454 NullPointerIsDefined(7455 I.getParent()->getParent(),7456 I.getOperand(0)->getType()->getPointerAddressSpace())) {7457 return nullptr;7458 }7459 Instruction *Op;7460 if (match(I.getOperand(0), m_Instruction(Op)) &&7461 match(I.getOperand(1), m_Zero()) &&7462 Op->isLaunderOrStripInvariantGroup()) {7463 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),7464 Op->getOperand(0), I.getOperand(1));7465 }7466 return nullptr;7467}7468 7469/// This function folds patterns produced by lowering of reduce idioms, such as7470/// llvm.vector.reduce.and which are lowered into instruction chains. This code7471/// attempts to generate fewer number of scalar comparisons instead of vector7472/// comparisons when possible.7473static Instruction *foldReductionIdiom(ICmpInst &I,7474 InstCombiner::BuilderTy &Builder,7475 const DataLayout &DL) {7476 if (I.getType()->isVectorTy())7477 return nullptr;7478 CmpPredicate OuterPred, InnerPred;7479 Value *LHS, *RHS;7480 7481 // Match lowering of @llvm.vector.reduce.and. Turn7482 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs7483 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i87484 /// %res = icmp <pred> i8 %scalar_ne, 07485 ///7486 /// into7487 ///7488 /// %lhs.scalar = bitcast <8 x i8> %lhs to i647489 /// %rhs.scalar = bitcast <8 x i8> %rhs to i647490 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar7491 ///7492 /// for <pred> in {ne, eq}.7493 if (!match(&I, m_ICmp(OuterPred,7494 m_OneUse(m_BitCast(m_OneUse(7495 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),7496 m_Zero())))7497 return nullptr;7498 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());7499 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())7500 return nullptr;7501 unsigned NumBits =7502 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();7503 // TODO: Relax this to "not wider than max legal integer type"?7504 if (!DL.isLegalInteger(NumBits))7505 return nullptr;7506 7507 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {7508 auto *ScalarTy = Builder.getIntNTy(NumBits);7509 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");7510 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");7511 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,7512 I.getName());7513 }7514 7515 return nullptr;7516}7517 7518// This helper will be called with icmp operands in both orders.7519Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,7520 Value *Op0, Value *Op1,7521 ICmpInst &CxtI) {7522 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.7523 if (auto *GEP = dyn_cast<GEPOperator>(Op0))7524 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))7525 return NI;7526 7527 if (auto *SI = dyn_cast<SelectInst>(Op0))7528 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))7529 return NI;7530 7531 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0)) {7532 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))7533 return Res;7534 7535 if (Instruction *Res = foldICmpWithClamp(CxtI, Op1, MinMax))7536 return Res;7537 }7538 7539 {7540 Value *X;7541 const APInt *C;7542 // icmp X+Cst, X7543 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)7544 return foldICmpAddOpConst(X, *C, Pred);7545 }7546 7547 // abs(X) >= X --> true7548 // abs(X) u<= X --> true7549 // abs(X) < X --> false7550 // abs(X) u> X --> false7551 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`7552 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`7553 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`7554 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`7555 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`7556 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`7557 {7558 Value *X;7559 Constant *C;7560 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&7561 match(Op1, m_Specific(X))) {7562 Value *NullValue = Constant::getNullValue(X->getType());7563 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());7564 const APInt SMin =7565 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());7566 bool IsIntMinPosion = C->isAllOnesValue();7567 switch (Pred) {7568 case CmpInst::ICMP_ULE:7569 case CmpInst::ICMP_SGE:7570 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));7571 case CmpInst::ICMP_UGT:7572 case CmpInst::ICMP_SLT:7573 return replaceInstUsesWith(CxtI, ConstantInt::getFalse(CxtI.getType()));7574 case CmpInst::ICMP_UGE:7575 case CmpInst::ICMP_SLE:7576 case CmpInst::ICMP_EQ: {7577 return replaceInstUsesWith(7578 CxtI, IsIntMinPosion7579 ? Builder.CreateICmpSGT(X, AllOnesValue)7580 : Builder.CreateICmpULT(7581 X, ConstantInt::get(X->getType(), SMin + 1)));7582 }7583 case CmpInst::ICMP_ULT:7584 case CmpInst::ICMP_SGT:7585 case CmpInst::ICMP_NE: {7586 return replaceInstUsesWith(7587 CxtI, IsIntMinPosion7588 ? Builder.CreateICmpSLT(X, NullValue)7589 : Builder.CreateICmpUGT(7590 X, ConstantInt::get(X->getType(), SMin)));7591 }7592 default:7593 llvm_unreachable("Invalid predicate!");7594 }7595 }7596 }7597 7598 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);7599 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))7600 return replaceInstUsesWith(CxtI, V);7601 7602 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 17603 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };7604 {7605 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {7606 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,7607 Constant::getNullValue(Op1->getType()));7608 }7609 7610 if (!ICmpInst::isUnsigned(Pred) &&7611 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {7612 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,7613 Constant::getNullValue(Op1->getType()));7614 }7615 }7616 7617 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 07618 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };7619 {7620 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {7621 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,7622 Constant::getNullValue(Op1->getType()));7623 }7624 7625 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&7626 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {7627 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,7628 Constant::getNullValue(Op1->getType()));7629 }7630 }7631 7632 return nullptr;7633}7634 7635Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {7636 bool Changed = false;7637 const SimplifyQuery Q = SQ.getWithInstruction(&I);7638 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);7639 unsigned Op0Cplxity = getComplexity(Op0);7640 unsigned Op1Cplxity = getComplexity(Op1);7641 7642 /// Orders the operands of the compare so that they are listed from most7643 /// complex to least complex. This puts constants before unary operators,7644 /// before binary operators.7645 if (Op0Cplxity < Op1Cplxity) {7646 I.swapOperands();7647 std::swap(Op0, Op1);7648 Changed = true;7649 }7650 7651 if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))7652 return replaceInstUsesWith(I, V);7653 7654 // Comparing -val or val with non-zero is the same as just comparing val7655 // ie, abs(val) != 0 -> val != 07656 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {7657 Value *Cond, *SelectTrue, *SelectFalse;7658 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),7659 m_Value(SelectFalse)))) {7660 if (Value *V = dyn_castNegVal(SelectTrue)) {7661 if (V == SelectFalse)7662 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);7663 } else if (Value *V = dyn_castNegVal(SelectFalse)) {7664 if (V == SelectTrue)7665 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);7666 }7667 }7668 }7669 7670 if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q))7671 return Res;7672 7673 if (Op0->getType()->isIntOrIntVectorTy(1))7674 if (Instruction *Res = canonicalizeICmpBool(I, Builder))7675 return Res;7676 7677 if (Instruction *Res = canonicalizeCmpWithConstant(I))7678 return Res;7679 7680 if (Instruction *Res = canonicalizeICmpPredicate(I))7681 return Res;7682 7683 if (Instruction *Res = foldICmpWithConstant(I))7684 return Res;7685 7686 if (Instruction *Res = foldICmpWithDominatingICmp(I))7687 return Res;7688 7689 if (Instruction *Res = foldICmpUsingBoolRange(I))7690 return Res;7691 7692 if (Instruction *Res = foldICmpUsingKnownBits(I))7693 return Res;7694 7695 if (Instruction *Res = foldIsMultipleOfAPowerOfTwo(I))7696 return Res;7697 7698 // Test if the ICmpInst instruction is used exclusively by a select as7699 // part of a minimum or maximum operation. If so, refrain from doing7700 // any other folding. This helps out other analyses which understand7701 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution7702 // and CodeGen. And in this case, at least one of the comparison7703 // operands has at least one user besides the compare (the select),7704 // which would often largely negate the benefit of folding anyway.7705 //7706 // Do the same for the other patterns recognized by matchSelectPattern.7707 if (I.hasOneUse())7708 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {7709 Value *A, *B;7710 SelectPatternResult SPR = matchSelectPattern(SI, A, B);7711 if (SPR.Flavor != SPF_UNKNOWN)7712 return nullptr;7713 }7714 7715 // Do this after checking for min/max to prevent infinite looping.7716 if (Instruction *Res = foldICmpWithZero(I))7717 return Res;7718 7719 // FIXME: We only do this after checking for min/max to prevent infinite7720 // looping caused by a reverse canonicalization of these patterns for min/max.7721 // FIXME: The organization of folds is a mess. These would naturally go into7722 // canonicalizeCmpWithConstant(), but we can't move all of the above folds7723 // down here after the min/max restriction.7724 ICmpInst::Predicate Pred = I.getPredicate();7725 const APInt *C;7726 if (match(Op1, m_APInt(C))) {7727 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set7728 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {7729 Constant *Zero = Constant::getNullValue(Op0->getType());7730 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);7731 }7732 7733 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear7734 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {7735 Constant *AllOnes = Constant::getAllOnesValue(Op0->getType());7736 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);7737 }7738 }7739 7740 // The folds in here may rely on wrapping flags and special constants, so7741 // they can break up min/max idioms in some cases but not seemingly similar7742 // patterns.7743 // FIXME: It may be possible to enhance select folding to make this7744 // unnecessary. It may also be moot if we canonicalize to min/max7745 // intrinsics.7746 if (Instruction *Res = foldICmpBinOp(I, Q))7747 return Res;7748 7749 if (Instruction *Res = foldICmpInstWithConstant(I))7750 return Res;7751 7752 // Try to match comparison as a sign bit test. Intentionally do this after7753 // foldICmpInstWithConstant() to potentially let other folds to happen first.7754 if (Instruction *New = foldSignBitTest(I))7755 return New;7756 7757 if (auto *PN = dyn_cast<PHINode>(Op0))7758 if (Instruction *NV = foldOpIntoPhi(I, PN))7759 return NV;7760 if (auto *PN = dyn_cast<PHINode>(Op1))7761 if (Instruction *NV = foldOpIntoPhi(I, PN))7762 return NV;7763 7764 if (Instruction *Res = foldICmpInstWithConstantNotInt(I))7765 return Res;7766 7767 if (Instruction *Res = foldICmpCommutative(I.getCmpPredicate(), Op0, Op1, I))7768 return Res;7769 if (Instruction *Res =7770 foldICmpCommutative(I.getSwappedCmpPredicate(), Op1, Op0, I))7771 return Res;7772 7773 if (I.isCommutative()) {7774 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {7775 replaceOperand(I, 0, Pair->first);7776 replaceOperand(I, 1, Pair->second);7777 return &I;7778 }7779 }7780 7781 // In case of a comparison with two select instructions having the same7782 // condition, check whether one of the resulting branches can be simplified.7783 // If so, just compare the other branch and select the appropriate result.7784 // For example:7785 // %tmp1 = select i1 %cmp, i32 %y, i32 %x7786 // %tmp2 = select i1 %cmp, i32 %z, i32 %x7787 // %cmp2 = icmp slt i32 %tmp2, %tmp17788 // The icmp will result false for the false value of selects and the result7789 // will depend upon the comparison of true values of selects if %cmp is7790 // true. Thus, transform this into:7791 // %cmp = icmp slt i32 %y, %z7792 // %sel = select i1 %cond, i1 %cmp, i1 false7793 // This handles similar cases to transform.7794 {7795 Value *Cond, *A, *B, *C, *D;7796 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&7797 match(Op1, m_Select(m_Specific(Cond), m_Value(C), m_Value(D))) &&7798 (Op0->hasOneUse() || Op1->hasOneUse())) {7799 // Check whether comparison of TrueValues can be simplified7800 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {7801 Value *NewICMP = Builder.CreateICmp(Pred, B, D);7802 return SelectInst::Create(Cond, Res, NewICMP);7803 }7804 // Check whether comparison of FalseValues can be simplified7805 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {7806 Value *NewICMP = Builder.CreateICmp(Pred, A, C);7807 return SelectInst::Create(Cond, NewICMP, Res);7808 }7809 }7810 }7811 7812 // icmp slt (sub nsw x, y), (add nsw x, y) --> icmp sgt y, 07813 // icmp ult (sub nuw x, y), (add nuw x, y) --> icmp ugt y, 07814 // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y) --> icmp eq y, 07815 {7816 Value *A, *B;7817 CmpPredicate CmpPred;7818 if (match(&I, m_c_ICmp(CmpPred, m_Sub(m_Value(A), m_Value(B)),7819 m_c_Add(m_Deferred(A), m_Deferred(B))))) {7820 auto *I0 = cast<OverflowingBinaryOperator>(Op0);7821 auto *I1 = cast<OverflowingBinaryOperator>(Op1);7822 bool I0NUW = I0->hasNoUnsignedWrap();7823 bool I1NUW = I1->hasNoUnsignedWrap();7824 bool I0NSW = I0->hasNoSignedWrap();7825 bool I1NSW = I1->hasNoSignedWrap();7826 if ((ICmpInst::isUnsigned(Pred) && I0NUW && I1NUW) ||7827 (ICmpInst::isSigned(Pred) && I0NSW && I1NSW) ||7828 (ICmpInst::isEquality(Pred) &&7829 ((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {7830 return new ICmpInst(CmpPredicate::getSwapped(CmpPred), B,7831 ConstantInt::get(Op0->getType(), 0));7832 }7833 }7834 }7835 7836 // Try to optimize equality comparisons against alloca-based pointers.7837 if (Op0->getType()->isPointerTy() && I.isEquality()) {7838 assert(Op1->getType()->isPointerTy() &&7839 "Comparing pointer with non-pointer?");7840 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))7841 if (foldAllocaCmp(Alloca))7842 return nullptr;7843 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))7844 if (foldAllocaCmp(Alloca))7845 return nullptr;7846 }7847 7848 if (Instruction *Res = foldICmpBitCast(I))7849 return Res;7850 7851 // TODO: Hoist this above the min/max bailout.7852 if (Instruction *R = foldICmpWithCastOp(I))7853 return R;7854 7855 {7856 Value *X, *Y;7857 // Transform (X & ~Y) == 0 --> (X & Y) != 07858 // and (X & ~Y) != 0 --> (X & Y) == 07859 // if A is a power of 2.7860 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&7861 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, &I) &&7862 I.isEquality())7863 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),7864 Op1);7865 7866 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.7867 if (Op0->getType()->isIntOrIntVectorTy()) {7868 bool ConsumesOp0, ConsumesOp1;7869 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&7870 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&7871 (ConsumesOp0 || ConsumesOp1)) {7872 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);7873 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);7874 assert(InvOp0 && InvOp1 &&7875 "Mismatch between isFreeToInvert and getFreelyInverted");7876 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);7877 }7878 }7879 7880 Instruction *AddI = nullptr;7881 if (match(&I, m_UAddWithOverflow(m_Value(X), m_Value(Y),7882 m_Instruction(AddI))) &&7883 isa<IntegerType>(X->getType())) {7884 Value *Result;7885 Constant *Overflow;7886 // m_UAddWithOverflow can match patterns that do not include an explicit7887 // "add" instruction, so check the opcode of the matched op.7888 if (AddI->getOpcode() == Instruction::Add &&7889 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,7890 Result, Overflow)) {7891 replaceInstUsesWith(*AddI, Result);7892 eraseInstFromFunction(*AddI);7893 return replaceInstUsesWith(I, Overflow);7894 }7895 }7896 7897 // (zext X) * (zext Y) --> llvm.umul.with.overflow.7898 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&7899 match(Op1, m_APInt(C))) {7900 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))7901 return R;7902 }7903 7904 // Signbit test folds7905 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i17906 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i17907 Instruction *ExtI;7908 if ((I.isUnsigned() || I.isEquality()) &&7909 match(Op1,7910 m_CombineAnd(m_Instruction(ExtI), m_ZExtOrSExt(m_Value(Y)))) &&7911 Y->getType()->getScalarSizeInBits() == 1 &&7912 (Op0->hasOneUse() || Op1->hasOneUse())) {7913 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();7914 Instruction *ShiftI;7915 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),7916 m_Shr(m_Value(X), m_SpecificIntAllowPoison(7917 OpWidth - 1))))) {7918 unsigned ExtOpc = ExtI->getOpcode();7919 unsigned ShiftOpc = ShiftI->getOpcode();7920 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||7921 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {7922 Value *SLTZero =7923 Builder.CreateICmpSLT(X, Constant::getNullValue(X->getType()));7924 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());7925 return replaceInstUsesWith(I, Cmp);7926 }7927 }7928 }7929 }7930 7931 if (Instruction *Res = foldICmpEquality(I))7932 return Res;7933 7934 if (Instruction *Res = foldICmpPow2Test(I, Builder))7935 return Res;7936 7937 if (Instruction *Res = foldICmpOfUAddOv(I))7938 return Res;7939 7940 // The 'cmpxchg' instruction returns an aggregate containing the old value and7941 // an i1 which indicates whether or not we successfully did the swap.7942 //7943 // Replace comparisons between the old value and the expected value with the7944 // indicator that 'cmpxchg' returns.7945 //7946 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to7947 // spuriously fail. In those cases, the old value may equal the expected7948 // value but it is possible for the swap to not occur.7949 if (I.getPredicate() == ICmpInst::ICMP_EQ)7950 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))7951 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))7952 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&7953 !ACXI->isWeak())7954 return ExtractValueInst::Create(ACXI, 1);7955 7956 if (Instruction *Res = foldICmpWithHighBitMask(I, Builder))7957 return Res;7958 7959 if (I.getType()->isVectorTy())7960 if (Instruction *Res = foldVectorCmp(I, Builder))7961 return Res;7962 7963 if (Instruction *Res = foldICmpInvariantGroup(I))7964 return Res;7965 7966 if (Instruction *Res = foldReductionIdiom(I, Builder, DL))7967 return Res;7968 7969 {7970 Value *A;7971 const APInt *C1, *C2;7972 ICmpInst::Predicate Pred = I.getPredicate();7973 if (ICmpInst::isEquality(Pred)) {7974 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)7975 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)7976 if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) &&7977 match(Op1, m_APInt(C2))) {7978 Type *InputTy = A->getType();7979 unsigned InputBitWidth = InputTy->getScalarSizeInBits();7980 // c2 must be non-negative at the bitwidth of a.7981 if (C2->getActiveBits() < InputBitWidth) {7982 APInt TruncC1 = C1->trunc(InputBitWidth);7983 // Check if there are 1s in C1 high bits of size InputBitWidth.7984 if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth)))7985 TruncC1.setBit(InputBitWidth - 1);7986 Value *AndInst = Builder.CreateAnd(A, TruncC1);7987 return new ICmpInst(7988 Pred, AndInst,7989 ConstantInt::get(InputTy, C2->trunc(InputBitWidth)));7990 }7991 }7992 }7993 }7994 7995 return Changed ? &I : nullptr;7996}7997 7998/// Fold fcmp ([us]itofp x, cst) if possible.7999Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,8000 Instruction *LHSI,8001 Constant *RHSC) {8002 const APFloat *RHS;8003 if (!match(RHSC, m_APFloat(RHS)))8004 return nullptr;8005 8006 // Get the width of the mantissa. We don't want to hack on conversions that8007 // might lose information from the integer, e.g. "i64 -> float"8008 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();8009 if (MantissaWidth == -1)8010 return nullptr; // Unknown.8011 8012 Type *IntTy = LHSI->getOperand(0)->getType();8013 unsigned IntWidth = IntTy->getScalarSizeInBits();8014 bool LHSUnsigned = isa<UIToFPInst>(LHSI);8015 8016 if (I.isEquality()) {8017 FCmpInst::Predicate P = I.getPredicate();8018 bool IsExact = false;8019 APSInt RHSCvt(IntWidth, LHSUnsigned);8020 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);8021 8022 // If the floating point constant isn't an integer value, we know if we will8023 // ever compare equal / not equal to it.8024 if (!IsExact) {8025 // TODO: Can never be -0.0 and other non-representable values8026 APFloat RHSRoundInt(*RHS);8027 RHSRoundInt.roundToIntegral(APFloat::rmNearestTiesToEven);8028 if (*RHS != RHSRoundInt) {8029 if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ)8030 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8031 8032 assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE);8033 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8034 }8035 }8036 8037 // TODO: If the constant is exactly representable, is it always OK to do8038 // equality compares as integer?8039 }8040 8041 // Check to see that the input is converted from an integer type that is small8042 // enough that preserves all bits. TODO: check here for "known" sign bits.8043 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.8044 8045 // Following test does NOT adjust IntWidth downwards for signed inputs,8046 // because the most negative value still requires all the mantissa bits8047 // to distinguish it from one less than that value.8048 if ((int)IntWidth > MantissaWidth) {8049 // Conversion would lose accuracy. Check if loss can impact comparison.8050 int Exp = ilogb(*RHS);8051 if (Exp == APFloat::IEK_Inf) {8052 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));8053 if (MaxExponent < (int)IntWidth - !LHSUnsigned)8054 // Conversion could create infinity.8055 return nullptr;8056 } else {8057 // Note that if RHS is zero or NaN, then Exp is negative8058 // and first condition is trivially false.8059 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)8060 // Conversion could affect comparison.8061 return nullptr;8062 }8063 }8064 8065 // Otherwise, we can potentially simplify the comparison. We know that it8066 // will always come through as an integer value and we know the constant is8067 // not a NAN (it would have been previously simplified).8068 assert(!RHS->isNaN() && "NaN comparison not already folded!");8069 8070 ICmpInst::Predicate Pred;8071 switch (I.getPredicate()) {8072 default:8073 llvm_unreachable("Unexpected predicate!");8074 case FCmpInst::FCMP_UEQ:8075 case FCmpInst::FCMP_OEQ:8076 Pred = ICmpInst::ICMP_EQ;8077 break;8078 case FCmpInst::FCMP_UGT:8079 case FCmpInst::FCMP_OGT:8080 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;8081 break;8082 case FCmpInst::FCMP_UGE:8083 case FCmpInst::FCMP_OGE:8084 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;8085 break;8086 case FCmpInst::FCMP_ULT:8087 case FCmpInst::FCMP_OLT:8088 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;8089 break;8090 case FCmpInst::FCMP_ULE:8091 case FCmpInst::FCMP_OLE:8092 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;8093 break;8094 case FCmpInst::FCMP_UNE:8095 case FCmpInst::FCMP_ONE:8096 Pred = ICmpInst::ICMP_NE;8097 break;8098 case FCmpInst::FCMP_ORD:8099 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8100 case FCmpInst::FCMP_UNO:8101 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8102 }8103 8104 // Now we know that the APFloat is a normal number, zero or inf.8105 8106 // See if the FP constant is too large for the integer. For example,8107 // comparing an i8 to 300.0.8108 if (!LHSUnsigned) {8109 // If the RHS value is > SignedMax, fold the comparison. This handles +INF8110 // and large values.8111 APFloat SMax(RHS->getSemantics());8112 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,8113 APFloat::rmNearestTiesToEven);8114 if (SMax < *RHS) { // smax < 13123.08115 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||8116 Pred == ICmpInst::ICMP_SLE)8117 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8118 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8119 }8120 } else {8121 // If the RHS value is > UnsignedMax, fold the comparison. This handles8122 // +INF and large values.8123 APFloat UMax(RHS->getSemantics());8124 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,8125 APFloat::rmNearestTiesToEven);8126 if (UMax < *RHS) { // umax < 13123.08127 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||8128 Pred == ICmpInst::ICMP_ULE)8129 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8130 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8131 }8132 }8133 8134 if (!LHSUnsigned) {8135 // See if the RHS value is < SignedMin.8136 APFloat SMin(RHS->getSemantics());8137 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,8138 APFloat::rmNearestTiesToEven);8139 if (SMin > *RHS) { // smin > 12312.08140 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||8141 Pred == ICmpInst::ICMP_SGE)8142 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8143 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8144 }8145 } else {8146 // See if the RHS value is < UnsignedMin.8147 APFloat UMin(RHS->getSemantics());8148 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,8149 APFloat::rmNearestTiesToEven);8150 if (UMin > *RHS) { // umin > 12312.08151 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||8152 Pred == ICmpInst::ICMP_UGE)8153 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8154 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8155 }8156 }8157 8158 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or8159 // [0, UMAX], but it may still be fractional. Check whether this is the case8160 // using the IsExact flag.8161 // Don't do this for zero, because -0.0 is not fractional.8162 APSInt RHSInt(IntWidth, LHSUnsigned);8163 bool IsExact;8164 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);8165 if (!RHS->isZero()) {8166 if (!IsExact) {8167 // If we had a comparison against a fractional value, we have to adjust8168 // the compare predicate and sometimes the value. RHSC is rounded towards8169 // zero at this point.8170 switch (Pred) {8171 default:8172 llvm_unreachable("Unexpected integer comparison!");8173 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true8174 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8175 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false8176 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8177 case ICmpInst::ICMP_ULE:8178 // (float)int <= 4.4 --> int <= 48179 // (float)int <= -4.4 --> false8180 if (RHS->isNegative())8181 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8182 break;8183 case ICmpInst::ICMP_SLE:8184 // (float)int <= 4.4 --> int <= 48185 // (float)int <= -4.4 --> int < -48186 if (RHS->isNegative())8187 Pred = ICmpInst::ICMP_SLT;8188 break;8189 case ICmpInst::ICMP_ULT:8190 // (float)int < -4.4 --> false8191 // (float)int < 4.4 --> int <= 48192 if (RHS->isNegative())8193 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8194 Pred = ICmpInst::ICMP_ULE;8195 break;8196 case ICmpInst::ICMP_SLT:8197 // (float)int < -4.4 --> int < -48198 // (float)int < 4.4 --> int <= 48199 if (!RHS->isNegative())8200 Pred = ICmpInst::ICMP_SLE;8201 break;8202 case ICmpInst::ICMP_UGT:8203 // (float)int > 4.4 --> int > 48204 // (float)int > -4.4 --> true8205 if (RHS->isNegative())8206 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8207 break;8208 case ICmpInst::ICMP_SGT:8209 // (float)int > 4.4 --> int > 48210 // (float)int > -4.4 --> int >= -48211 if (RHS->isNegative())8212 Pred = ICmpInst::ICMP_SGE;8213 break;8214 case ICmpInst::ICMP_UGE:8215 // (float)int >= -4.4 --> true8216 // (float)int >= 4.4 --> int > 48217 if (RHS->isNegative())8218 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8219 Pred = ICmpInst::ICMP_UGT;8220 break;8221 case ICmpInst::ICMP_SGE:8222 // (float)int >= -4.4 --> int >= -48223 // (float)int >= 4.4 --> int > 48224 if (!RHS->isNegative())8225 Pred = ICmpInst::ICMP_SGT;8226 break;8227 }8228 }8229 }8230 8231 // Lower this FP comparison into an appropriate integer version of the8232 // comparison.8233 return new ICmpInst(Pred, LHSI->getOperand(0),8234 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));8235}8236 8237/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.8238static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,8239 Constant *RHSC) {8240 // When C is not 0.0 and infinities are not allowed:8241 // (C / X) < 0.0 is a sign-bit test of X8242 // (C / X) < 0.0 --> X < 0.0 (if C is positive)8243 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)8244 //8245 // Proof:8246 // Multiply (C / X) < 0.0 by X * X / C.8247 // - X is non zero, if it is the flag 'ninf' is violated.8248 // - C defines the sign of X * X * C. Thus it also defines whether to swap8249 // the predicate. C is also non zero by definition.8250 //8251 // Thus X * X / C is non zero and the transformation is valid. [qed]8252 8253 FCmpInst::Predicate Pred = I.getPredicate();8254 8255 // Check that predicates are valid.8256 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&8257 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))8258 return nullptr;8259 8260 // Check that RHS operand is zero.8261 if (!match(RHSC, m_AnyZeroFP()))8262 return nullptr;8263 8264 // Check fastmath flags ('ninf').8265 if (!LHSI->hasNoInfs() || !I.hasNoInfs())8266 return nullptr;8267 8268 // Check the properties of the dividend. It must not be zero to avoid a8269 // division by zero (see Proof).8270 const APFloat *C;8271 if (!match(LHSI->getOperand(0), m_APFloat(C)))8272 return nullptr;8273 8274 if (C->isZero())8275 return nullptr;8276 8277 // Get swapped predicate if necessary.8278 if (C->isNegative())8279 Pred = I.getSwappedPredicate();8280 8281 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);8282}8283 8284// Transform 'fptrunc(x) cmp C' to 'x cmp ext(C)' if possible.8285// Patterns include:8286// fptrunc(x) < C --> x < ext(C)8287// fptrunc(x) <= C --> x <= ext(C)8288// fptrunc(x) > C --> x > ext(C)8289// fptrunc(x) >= C --> x >= ext(C)8290// where 'ext(C)' is the extension of 'C' to the type of 'x' with a small bias8291// due to precision loss.8292static Instruction *foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc,8293 const Constant &C) {8294 FCmpInst::Predicate Pred = I.getPredicate();8295 bool RoundDown = false;8296 8297 if (Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE ||8298 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT)8299 RoundDown = true;8300 else if (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT ||8301 Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)8302 RoundDown = false;8303 else8304 return nullptr;8305 8306 const APFloat *CValue;8307 if (!match(&C, m_APFloat(CValue)))8308 return nullptr;8309 8310 if (CValue->isNaN() || CValue->isInfinity())8311 return nullptr;8312 8313 auto ConvertFltSema = [](const APFloat &Src, const fltSemantics &Sema) {8314 bool LosesInfo;8315 APFloat Dest = Src;8316 Dest.convert(Sema, APFloat::rmNearestTiesToEven, &LosesInfo);8317 return Dest;8318 };8319 8320 auto NextValue = [](const APFloat &Value, bool RoundDown) {8321 APFloat NextValue = Value;8322 NextValue.next(RoundDown);8323 return NextValue;8324 };8325 8326 APFloat NextCValue = NextValue(*CValue, RoundDown);8327 8328 Type *DestType = FPTrunc.getOperand(0)->getType();8329 const fltSemantics &DestFltSema =8330 DestType->getScalarType()->getFltSemantics();8331 8332 APFloat ExtCValue = ConvertFltSema(*CValue, DestFltSema);8333 APFloat ExtNextCValue = ConvertFltSema(NextCValue, DestFltSema);8334 8335 // When 'NextCValue' is infinity, use an imaged 'NextCValue' that equals8336 // 'CValue + bias' to avoid the infinity after conversion. The bias is8337 // estimated as 'CValue - PrevCValue', where 'PrevCValue' is the previous8338 // value of 'CValue'.8339 if (NextCValue.isInfinity()) {8340 APFloat PrevCValue = NextValue(*CValue, !RoundDown);8341 APFloat Bias = ConvertFltSema(*CValue - PrevCValue, DestFltSema);8342 8343 ExtNextCValue = ExtCValue + Bias;8344 }8345 8346 APFloat ExtMidValue =8347 scalbn(ExtCValue + ExtNextCValue, -1, APFloat::rmNearestTiesToEven);8348 8349 const fltSemantics &SrcFltSema =8350 C.getType()->getScalarType()->getFltSemantics();8351 8352 // 'MidValue' might be rounded to 'NextCValue'. Correct it here.8353 APFloat MidValue = ConvertFltSema(ExtMidValue, SrcFltSema);8354 if (MidValue != *CValue)8355 ExtMidValue.next(!RoundDown);8356 8357 // Check whether 'ExtMidValue' is a valid result since the assumption on8358 // imaged 'NextCValue' might not hold for new float types.8359 // ppc_fp128 can't pass here when converting from max float because of8360 // APFloat implementation.8361 if (NextCValue.isInfinity()) {8362 // ExtMidValue --- narrowed ---> Finite8363 if (ConvertFltSema(ExtMidValue, SrcFltSema).isInfinity())8364 return nullptr;8365 8366 // NextExtMidValue --- narrowed ---> Infinity8367 APFloat NextExtMidValue = NextValue(ExtMidValue, RoundDown);8368 if (ConvertFltSema(NextExtMidValue, SrcFltSema).isFinite())8369 return nullptr;8370 }8371 8372 return new FCmpInst(Pred, FPTrunc.getOperand(0),8373 ConstantFP::get(DestType, ExtMidValue), "", &I);8374}8375 8376/// Optimize fabs(X) compared with zero.8377static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {8378 Value *X;8379 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))8380 return nullptr;8381 8382 const APFloat *C;8383 if (!match(I.getOperand(1), m_APFloat(C)))8384 return nullptr;8385 8386 if (!C->isPosZero()) {8387 if (!C->isSmallestNormalized())8388 return nullptr;8389 8390 const Function *F = I.getFunction();8391 DenormalMode Mode = F->getDenormalMode(C->getSemantics());8392 if (Mode.Input == DenormalMode::PreserveSign ||8393 Mode.Input == DenormalMode::PositiveZero) {8394 8395 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {8396 Constant *Zero = ConstantFP::getZero(X->getType());8397 return new FCmpInst(P, X, Zero, "", I);8398 };8399 8400 switch (I.getPredicate()) {8401 case FCmpInst::FCMP_OLT:8402 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.08403 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);8404 case FCmpInst::FCMP_UGE:8405 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.08406 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);8407 case FCmpInst::FCMP_OGE:8408 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.08409 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);8410 case FCmpInst::FCMP_ULT:8411 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.08412 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);8413 default:8414 break;8415 }8416 }8417 8418 return nullptr;8419 }8420 8421 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {8422 I->setPredicate(P);8423 return IC.replaceOperand(*I, 0, X);8424 };8425 8426 switch (I.getPredicate()) {8427 case FCmpInst::FCMP_UGE:8428 case FCmpInst::FCMP_OLT:8429 // fabs(X) >= 0.0 --> true8430 // fabs(X) < 0.0 --> false8431 llvm_unreachable("fcmp should have simplified");8432 8433 case FCmpInst::FCMP_OGT:8434 // fabs(X) > 0.0 --> X != 0.08435 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);8436 8437 case FCmpInst::FCMP_UGT:8438 // fabs(X) u> 0.0 --> X u!= 0.08439 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);8440 8441 case FCmpInst::FCMP_OLE:8442 // fabs(X) <= 0.0 --> X == 0.08443 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);8444 8445 case FCmpInst::FCMP_ULE:8446 // fabs(X) u<= 0.0 --> X u== 0.08447 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);8448 8449 case FCmpInst::FCMP_OGE:8450 // fabs(X) >= 0.0 --> !isnan(X)8451 assert(!I.hasNoNaNs() && "fcmp should have simplified");8452 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);8453 8454 case FCmpInst::FCMP_ULT:8455 // fabs(X) u< 0.0 --> isnan(X)8456 assert(!I.hasNoNaNs() && "fcmp should have simplified");8457 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);8458 8459 case FCmpInst::FCMP_OEQ:8460 case FCmpInst::FCMP_UEQ:8461 case FCmpInst::FCMP_ONE:8462 case FCmpInst::FCMP_UNE:8463 case FCmpInst::FCMP_ORD:8464 case FCmpInst::FCMP_UNO:8465 // Look through the fabs() because it doesn't change anything but the sign.8466 // fabs(X) == 0.0 --> X == 0.0,8467 // fabs(X) != 0.0 --> X != 0.08468 // isnan(fabs(X)) --> isnan(X)8469 // !isnan(fabs(X) --> !isnan(X)8470 return replacePredAndOp0(&I, I.getPredicate(), X);8471 8472 default:8473 return nullptr;8474 }8475}8476 8477/// Optimize sqrt(X) compared with zero.8478static Instruction *foldSqrtWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {8479 Value *X;8480 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))8481 return nullptr;8482 8483 if (!match(I.getOperand(1), m_PosZeroFP()))8484 return nullptr;8485 8486 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {8487 I.setPredicate(P);8488 return IC.replaceOperand(I, 0, X);8489 };8490 8491 // Clear ninf flag if sqrt doesn't have it.8492 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())8493 I.setHasNoInfs(false);8494 8495 switch (I.getPredicate()) {8496 case FCmpInst::FCMP_OLT:8497 case FCmpInst::FCMP_UGE:8498 // sqrt(X) < 0.0 --> false8499 // sqrt(X) u>= 0.0 --> true8500 llvm_unreachable("fcmp should have simplified");8501 case FCmpInst::FCMP_ULT:8502 case FCmpInst::FCMP_ULE:8503 case FCmpInst::FCMP_OGT:8504 case FCmpInst::FCMP_OGE:8505 case FCmpInst::FCMP_OEQ:8506 case FCmpInst::FCMP_UNE:8507 // sqrt(X) u< 0.0 --> X u< 0.08508 // sqrt(X) u<= 0.0 --> X u<= 0.08509 // sqrt(X) > 0.0 --> X > 0.08510 // sqrt(X) >= 0.0 --> X >= 0.08511 // sqrt(X) == 0.0 --> X == 0.08512 // sqrt(X) u!= 0.0 --> X u!= 0.08513 return IC.replaceOperand(I, 0, X);8514 8515 case FCmpInst::FCMP_OLE:8516 // sqrt(X) <= 0.0 --> X == 0.08517 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);8518 case FCmpInst::FCMP_UGT:8519 // sqrt(X) u> 0.0 --> X u!= 0.08520 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);8521 case FCmpInst::FCMP_UEQ:8522 // sqrt(X) u== 0.0 --> X u<= 0.08523 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);8524 case FCmpInst::FCMP_ONE:8525 // sqrt(X) != 0.0 --> X > 0.08526 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);8527 case FCmpInst::FCMP_ORD:8528 // !isnan(sqrt(X)) --> X >= 0.08529 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);8530 case FCmpInst::FCMP_UNO:8531 // isnan(sqrt(X)) --> X u< 0.08532 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);8533 default:8534 llvm_unreachable("Unexpected predicate!");8535 }8536}8537 8538static Instruction *foldFCmpFNegCommonOp(FCmpInst &I) {8539 CmpInst::Predicate Pred = I.getPredicate();8540 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);8541 8542 // Canonicalize fneg as Op1.8543 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {8544 std::swap(Op0, Op1);8545 Pred = I.getSwappedPredicate();8546 }8547 8548 if (!match(Op1, m_FNeg(m_Specific(Op0))))8549 return nullptr;8550 8551 // Replace the negated operand with 0.0:8552 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.08553 Constant *Zero = ConstantFP::getZero(Op0->getType());8554 return new FCmpInst(Pred, Op0, Zero, "", &I);8555}8556 8557static Instruction *foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI,8558 Constant *RHSC, InstCombinerImpl &CI) {8559 const CmpInst::Predicate Pred = I.getPredicate();8560 Value *X = LHSI->getOperand(0);8561 Value *Y = LHSI->getOperand(1);8562 switch (Pred) {8563 default:8564 break;8565 case FCmpInst::FCMP_UGT:8566 case FCmpInst::FCMP_ULT:8567 case FCmpInst::FCMP_UNE:8568 case FCmpInst::FCMP_OEQ:8569 case FCmpInst::FCMP_OGE:8570 case FCmpInst::FCMP_OLE:8571 // The optimization is not valid if X and Y are infinities of the same8572 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan8573 // flag then we can assume we do not have that case. Otherwise we might be8574 // able to prove that either X or Y is not infinity.8575 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&8576 !isKnownNeverInfinity(Y,8577 CI.getSimplifyQuery().getWithInstruction(&I)) &&8578 !isKnownNeverInfinity(X, CI.getSimplifyQuery().getWithInstruction(&I)))8579 break;8580 8581 [[fallthrough]];8582 case FCmpInst::FCMP_OGT:8583 case FCmpInst::FCMP_OLT:8584 case FCmpInst::FCMP_ONE:8585 case FCmpInst::FCMP_UEQ:8586 case FCmpInst::FCMP_UGE:8587 case FCmpInst::FCMP_ULE:8588 // fcmp pred (x - y), 0 --> fcmp pred x, y8589 if (match(RHSC, m_AnyZeroFP()) &&8590 I.getFunction()->getDenormalMode(8591 LHSI->getType()->getScalarType()->getFltSemantics()) ==8592 DenormalMode::getIEEE()) {8593 CI.replaceOperand(I, 0, X);8594 CI.replaceOperand(I, 1, Y);8595 I.setHasNoInfs(LHSI->hasNoInfs());8596 if (LHSI->hasNoNaNs())8597 I.setHasNoNaNs(true);8598 return &I;8599 }8600 break;8601 }8602 8603 return nullptr;8604}8605 8606static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,8607 InstCombinerImpl &IC) {8608 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);8609 Type *OpType = LHS->getType();8610 CmpInst::Predicate Pred = I.getPredicate();8611 8612 bool FloorX = match(LHS, m_Intrinsic<Intrinsic::floor>(m_Specific(RHS)));8613 bool CeilX = match(LHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(RHS)));8614 8615 if (!FloorX && !CeilX) {8616 if ((FloorX = match(RHS, m_Intrinsic<Intrinsic::floor>(m_Specific(LHS)))) ||8617 (CeilX = match(RHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(LHS))))) {8618 std::swap(LHS, RHS);8619 Pred = I.getSwappedPredicate();8620 }8621 }8622 8623 switch (Pred) {8624 case FCmpInst::FCMP_OLE:8625 // fcmp ole floor(x), x => fcmp ord x, 08626 if (FloorX)8627 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType),8628 "", &I);8629 break;8630 case FCmpInst::FCMP_OGT:8631 // fcmp ogt floor(x), x => false8632 if (FloorX)8633 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8634 break;8635 case FCmpInst::FCMP_OGE:8636 // fcmp oge ceil(x), x => fcmp ord x, 08637 if (CeilX)8638 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(OpType),8639 "", &I);8640 break;8641 case FCmpInst::FCMP_OLT:8642 // fcmp olt ceil(x), x => false8643 if (CeilX)8644 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8645 break;8646 case FCmpInst::FCMP_ULE:8647 // fcmp ule floor(x), x => true8648 if (FloorX)8649 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8650 break;8651 case FCmpInst::FCMP_UGT:8652 // fcmp ugt floor(x), x => fcmp uno x, 08653 if (FloorX)8654 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(OpType),8655 "", &I);8656 break;8657 case FCmpInst::FCMP_UGE:8658 // fcmp uge ceil(x), x => true8659 if (CeilX)8660 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8661 break;8662 case FCmpInst::FCMP_ULT:8663 // fcmp ult ceil(x), x => fcmp uno x, 08664 if (CeilX)8665 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(OpType),8666 "", &I);8667 break;8668 default:8669 break;8670 }8671 8672 return nullptr;8673}8674 8675Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {8676 bool Changed = false;8677 8678 /// Orders the operands of the compare so that they are listed from most8679 /// complex to least complex. This puts constants before unary operators,8680 /// before binary operators.8681 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {8682 I.swapOperands();8683 Changed = true;8684 }8685 8686 const CmpInst::Predicate Pred = I.getPredicate();8687 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);8688 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),8689 SQ.getWithInstruction(&I)))8690 return replaceInstUsesWith(I, V);8691 8692 // Simplify 'fcmp pred X, X'8693 Type *OpType = Op0->getType();8694 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");8695 if (Op0 == Op1) {8696 switch (Pred) {8697 default:8698 break;8699 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)8700 case FCmpInst::FCMP_ULT: // True if unordered or less than8701 case FCmpInst::FCMP_UGT: // True if unordered or greater than8702 case FCmpInst::FCMP_UNE: // True if unordered or not equal8703 // Canonicalize these to be 'fcmp uno %X, 0.0'.8704 I.setPredicate(FCmpInst::FCMP_UNO);8705 I.setOperand(1, Constant::getNullValue(OpType));8706 return &I;8707 8708 case FCmpInst::FCMP_ORD: // True if ordered (no nans)8709 case FCmpInst::FCMP_OEQ: // True if ordered and equal8710 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal8711 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal8712 // Canonicalize these to be 'fcmp ord %X, 0.0'.8713 I.setPredicate(FCmpInst::FCMP_ORD);8714 I.setOperand(1, Constant::getNullValue(OpType));8715 return &I;8716 }8717 }8718 8719 if (I.isCommutative()) {8720 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {8721 replaceOperand(I, 0, Pair->first);8722 replaceOperand(I, 1, Pair->second);8723 return &I;8724 }8725 }8726 8727 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,8728 // then canonicalize the operand to 0.0.8729 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {8730 if (!match(Op0, m_PosZeroFP()) &&8731 isKnownNeverNaN(Op0, getSimplifyQuery().getWithInstruction(&I)))8732 return replaceOperand(I, 0, ConstantFP::getZero(OpType));8733 8734 if (!match(Op1, m_PosZeroFP()) &&8735 isKnownNeverNaN(Op1, getSimplifyQuery().getWithInstruction(&I)))8736 return replaceOperand(I, 1, ConstantFP::getZero(OpType));8737 }8738 8739 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y8740 Value *X, *Y;8741 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))8742 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);8743 8744 if (Instruction *R = foldFCmpFNegCommonOp(I))8745 return R;8746 8747 // Test if the FCmpInst instruction is used exclusively by a select as8748 // part of a minimum or maximum operation. If so, refrain from doing8749 // any other folding. This helps out other analyses which understand8750 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution8751 // and CodeGen. And in this case, at least one of the comparison8752 // operands has at least one user besides the compare (the select),8753 // which would often largely negate the benefit of folding anyway.8754 if (I.hasOneUse())8755 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {8756 Value *A, *B;8757 SelectPatternResult SPR = matchSelectPattern(SI, A, B);8758 if (SPR.Flavor != SPF_UNKNOWN)8759 return nullptr;8760 }8761 8762 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:8763 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.08764 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))8765 return replaceOperand(I, 1, ConstantFP::getZero(OpType));8766 8767 // Canonicalize:8768 // fcmp olt X, +inf -> fcmp one X, +inf8769 // fcmp ole X, +inf -> fcmp ord X, 08770 // fcmp ogt X, +inf -> false8771 // fcmp oge X, +inf -> fcmp oeq X, +inf8772 // fcmp ult X, +inf -> fcmp une X, +inf8773 // fcmp ule X, +inf -> true8774 // fcmp ugt X, +inf -> fcmp uno X, 08775 // fcmp uge X, +inf -> fcmp ueq X, +inf8776 // fcmp olt X, -inf -> false8777 // fcmp ole X, -inf -> fcmp oeq X, -inf8778 // fcmp ogt X, -inf -> fcmp one X, -inf8779 // fcmp oge X, -inf -> fcmp ord X, 08780 // fcmp ult X, -inf -> fcmp uno X, 08781 // fcmp ule X, -inf -> fcmp ueq X, -inf8782 // fcmp ugt X, -inf -> fcmp une X, -inf8783 // fcmp uge X, -inf -> true8784 const APFloat *C;8785 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {8786 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {8787 default:8788 break;8789 case FCmpInst::FCMP_ORD:8790 case FCmpInst::FCMP_UNO:8791 case FCmpInst::FCMP_TRUE:8792 case FCmpInst::FCMP_FALSE:8793 case FCmpInst::FCMP_OGT:8794 case FCmpInst::FCMP_ULE:8795 llvm_unreachable("Should be simplified by InstSimplify");8796 case FCmpInst::FCMP_OLT:8797 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);8798 case FCmpInst::FCMP_OLE:8799 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),8800 "", &I);8801 case FCmpInst::FCMP_OGE:8802 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);8803 case FCmpInst::FCMP_ULT:8804 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);8805 case FCmpInst::FCMP_UGT:8806 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),8807 "", &I);8808 case FCmpInst::FCMP_UGE:8809 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);8810 }8811 }8812 8813 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:8814 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 08815 if (match(Op1, m_PosZeroFP()) &&8816 match(Op0, m_OneUse(m_ElementWiseBitCast(m_Value(X))))) {8817 ICmpInst::Predicate IntPred = ICmpInst::BAD_ICMP_PREDICATE;8818 if (Pred == FCmpInst::FCMP_OEQ)8819 IntPred = ICmpInst::ICMP_EQ;8820 else if (Pred == FCmpInst::FCMP_UNE)8821 IntPred = ICmpInst::ICMP_NE;8822 8823 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {8824 Type *IntTy = X->getType();8825 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());8826 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));8827 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));8828 }8829 }8830 8831 // Handle fcmp with instruction LHS and constant RHS.8832 Instruction *LHSI;8833 Constant *RHSC;8834 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {8835 switch (LHSI->getOpcode()) {8836 case Instruction::Select:8837 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 08838 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&8839 match(LHSI, m_c_Select(m_FNeg(m_Value(X)), m_Deferred(X))))8840 return replaceOperand(I, 0, X);8841 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))8842 return NV;8843 break;8844 case Instruction::FSub:8845 if (LHSI->hasOneUse())8846 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))8847 return NV;8848 break;8849 case Instruction::PHI:8850 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))8851 return NV;8852 break;8853 case Instruction::SIToFP:8854 case Instruction::UIToFP:8855 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))8856 return NV;8857 break;8858 case Instruction::FDiv:8859 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))8860 return NV;8861 break;8862 case Instruction::Load:8863 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))8864 if (Instruction *Res =8865 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, I))8866 return Res;8867 break;8868 case Instruction::FPTrunc:8869 if (Instruction *NV = foldFCmpFpTrunc(I, *LHSI, *RHSC))8870 return NV;8871 break;8872 }8873 }8874 8875 if (Instruction *R = foldFabsWithFcmpZero(I, *this))8876 return R;8877 8878 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))8879 return R;8880 8881 if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this))8882 return R;8883 8884 if (match(Op0, m_FNeg(m_Value(X)))) {8885 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C8886 Constant *C;8887 if (match(Op1, m_Constant(C)))8888 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))8889 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);8890 }8891 8892 // fcmp (fadd X, 0.0), Y --> fcmp X, Y8893 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))8894 return new FCmpInst(Pred, X, Op1, "", &I);8895 8896 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y8897 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))8898 return new FCmpInst(Pred, Op0, Y, "", &I);8899 8900 if (match(Op0, m_FPExt(m_Value(X)))) {8901 // fcmp (fpext X), (fpext Y) -> fcmp X, Y8902 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())8903 return new FCmpInst(Pred, X, Y, "", &I);8904 8905 const APFloat *C;8906 if (match(Op1, m_APFloat(C))) {8907 const fltSemantics &FPSem =8908 X->getType()->getScalarType()->getFltSemantics();8909 bool Lossy;8910 APFloat TruncC = *C;8911 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);8912 8913 if (Lossy) {8914 // X can't possibly equal the higher-precision constant, so reduce any8915 // equality comparison.8916 // TODO: Other predicates can be handled via getFCmpCode().8917 switch (Pred) {8918 case FCmpInst::FCMP_OEQ:8919 // X is ordered and equal to an impossible constant --> false8920 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));8921 case FCmpInst::FCMP_ONE:8922 // X is ordered and not equal to an impossible constant --> ordered8923 return new FCmpInst(FCmpInst::FCMP_ORD, X,8924 ConstantFP::getZero(X->getType()));8925 case FCmpInst::FCMP_UEQ:8926 // X is unordered or equal to an impossible constant --> unordered8927 return new FCmpInst(FCmpInst::FCMP_UNO, X,8928 ConstantFP::getZero(X->getType()));8929 case FCmpInst::FCMP_UNE:8930 // X is unordered or not equal to an impossible constant --> true8931 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));8932 default:8933 break;8934 }8935 }8936 8937 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless8938 // Avoid lossy conversions and denormals.8939 // Zero is a special case that's OK to convert.8940 APFloat Fabs = TruncC;8941 Fabs.clearSign();8942 if (!Lossy &&8943 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {8944 Constant *NewC = ConstantFP::get(X->getType(), TruncC);8945 return new FCmpInst(Pred, X, NewC, "", &I);8946 }8947 }8948 }8949 8950 // Convert a sign-bit test of an FP value into a cast and integer compare.8951 // TODO: Simplify if the copysign constant is 0.0 or NaN.8952 // TODO: Handle non-zero compare constants.8953 // TODO: Handle other predicates.8954 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),8955 m_Value(X)))) &&8956 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {8957 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());8958 if (auto *VecTy = dyn_cast<VectorType>(OpType))8959 IntType = VectorType::get(IntType, VecTy->getElementCount());8960 8961 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 08962 if (Pred == FCmpInst::FCMP_OLT) {8963 Value *IntX = Builder.CreateBitCast(X, IntType);8964 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,8965 ConstantInt::getNullValue(IntType));8966 }8967 }8968 8969 {8970 Value *CanonLHS = nullptr;8971 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));8972 // (canonicalize(x) == x) => (x == x)8973 if (CanonLHS == Op1)8974 return new FCmpInst(Pred, Op1, Op1, "", &I);8975 8976 Value *CanonRHS = nullptr;8977 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));8978 // (x == canonicalize(x)) => (x == x)8979 if (CanonRHS == Op0)8980 return new FCmpInst(Pred, Op0, Op0, "", &I);8981 8982 // (canonicalize(x) == canonicalize(y)) => (x == y)8983 if (CanonLHS && CanonRHS)8984 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);8985 }8986 8987 if (I.getType()->isVectorTy())8988 if (Instruction *Res = foldVectorCmp(I, Builder))8989 return Res;8990 8991 return Changed ? &I : nullptr;8992}8993