1//===- InstCombineCompares.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the visitICmp and visitFCmp functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/APFloat.h"
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/APSInt.h"
17#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/Analysis/CaptureTracking.h"
20#include "llvm/Analysis/CmpInstAnalysis.h"
21#include "llvm/Analysis/ConstantFolding.h"
22#include "llvm/Analysis/InstructionSimplify.h"
23#include "llvm/Analysis/Loads.h"
24#include "llvm/Analysis/Utils/Local.h"
25#include "llvm/Analysis/ValueTracking.h"
26#include "llvm/Analysis/VectorUtils.h"
27#include "llvm/IR/ConstantRange.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/InstrTypes.h"
31#include "llvm/IR/Instruction.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/PatternMatch.h"
35#include "llvm/Support/KnownBits.h"
36#include "llvm/Transforms/InstCombine/InstCombiner.h"
37#include <bitset>
38
39using namespace llvm;
40using namespace PatternMatch;
41
42#define DEBUG_TYPE "instcombine"
43
44// How many times is a select replaced by one of its operands?
45STATISTIC(NumSel, "Number of select opts");
46
47/// Compute Result = In1+In2, returning true if the result overflowed for this
48/// type.
49static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
50 bool IsSigned = false) {
51 bool Overflow;
52 if (IsSigned)
53 Result = In1.sadd_ov(RHS: In2, Overflow);
54 else
55 Result = In1.uadd_ov(RHS: In2, Overflow);
56
57 return Overflow;
58}
59
60/// Compute Result = In1-In2, returning true if the result overflowed for this
61/// type.
62static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
63 bool IsSigned = false) {
64 bool Overflow;
65 if (IsSigned)
66 Result = In1.ssub_ov(RHS: In2, Overflow);
67 else
68 Result = In1.usub_ov(RHS: In2, Overflow);
69
70 return Overflow;
71}
72
73/// Given an icmp instruction, return true if any use of this comparison is a
74/// branch on sign bit comparison.
75static bool hasBranchUse(ICmpInst &I) {
76 for (auto *U : I.users())
77 if (isa<CondBrInst>(Val: U))
78 return true;
79 return false;
80}
81
82/// Returns true if the exploded icmp can be expressed as a signed comparison
83/// to zero and updates the predicate accordingly.
84/// The signedness of the comparison is preserved.
85/// TODO: Refactor with decomposeBitTestICmp()?
86static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
87 if (!ICmpInst::isSigned(Pred))
88 return false;
89
90 if (C.isZero())
91 return ICmpInst::isRelational(P: Pred);
92
93 if (C.isOne()) {
94 if (Pred == ICmpInst::ICMP_SLT) {
95 Pred = ICmpInst::ICMP_SLE;
96 return true;
97 }
98 } else if (C.isAllOnes()) {
99 if (Pred == ICmpInst::ICMP_SGT) {
100 Pred = ICmpInst::ICMP_SGE;
101 return true;
102 }
103 }
104
105 return false;
106}
107
108/// Try to fold
109/// cmp pred (load (gep GV, ...)), cmpcst
110/// where GV is a global variable with a constant initializer. Try to simplify
111/// this into some simple computation that does not need the load. For example
112/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
113///
114/// If AndCst is non-null, then the loaded value is masked with that constant
115/// before doing the comparison. This handles cases like "A[i]&4 == 0".
116///
117/// We allow multi-use cases in this fold, even though it can increase
118/// instruction count, because it appears to be mostly beneficial in practice.
119/// Even if there are multiple uses, they can often be sunk into the block
120/// guarded by the icmp.
121Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
122 LoadInst *LI, CmpPredicate Pred, Constant *RHSC, Instruction &CtxI,
123 ConstantInt *AndCst) {
124 Value *Ptr = LI->getPointerOperand();
125 auto *GV = dyn_cast<GlobalVariable>(Val: getUnderlyingObject(V: Ptr));
126 if (LI->isVolatile() || !GV || !GV->isConstant() ||
127 !GV->hasDefinitiveInitializer())
128 return nullptr;
129
130 Type *EltTy = LI->getType();
131 TypeSize EltSize = DL.getTypeStoreSize(Ty: EltTy);
132 if (EltSize.isScalable())
133 return nullptr;
134
135 LinearExpression Expr = decomposeLinearExpression(DL, Ptr);
136 if (!Expr.Index || Expr.BasePtr != GV || Expr.Offset.getBitWidth() > 64)
137 return nullptr;
138
139 Constant *Init = GV->getInitializer();
140 TypeSize GlobalSize = DL.getTypeAllocSize(Ty: Init->getType());
141
142 Value *Idx = Expr.Index;
143 const APInt &Stride = Expr.Scale;
144 const APInt &ConstOffset = Expr.Offset;
145
146 // Allow an additional context offset, but only within the stride.
147 if (!ConstOffset.ult(RHS: Stride))
148 return nullptr;
149
150 // Don't handle overlapping loads for now.
151 if (!Stride.uge(RHS: EltSize.getFixedValue()))
152 return nullptr;
153
154 // - With nuw: `Idx * Stride` does not wrap and all strides are valid.
155 // - With nusw: `Idx * Stride` does not wrap as signed, and the following
156 // enumeration over ArrayElementCount is valid so long as the stride is
157 // positive.
158 // - Without nusw or nuw: `Idx * Stride` is computed modulo 2^N (see MaskIdx
159 // below), and the masking in MaskIdx below is only exact when the stride
160 // is a power of two.
161 bool StrideIsValid =
162 Expr.Flags.hasNoUnsignedWrap() ||
163 (Expr.Flags.hasNoUnsignedSignedWrap() ? !Stride.isNegative()
164 : Stride.isPowerOf2());
165 if (!StrideIsValid)
166 return nullptr;
167
168 // Don't blow up on huge arrays.
169 uint64_t ArrayElementCount =
170 divideCeil(Numerator: (GlobalSize.getFixedValue() - ConstOffset.getZExtValue()),
171 Denominator: Stride.getZExtValue());
172 if (ArrayElementCount > CLOpts.maxarray_size)
173 return nullptr;
174
175 enum { Overdefined = -3, Undefined = -2 };
176
177 // Variables for our state machines.
178
179 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
180 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
181 // and 87 is the second (and last) index. FirstTrueElement is -2 when
182 // undefined, otherwise set to the first true element. SecondTrueElement is
183 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
184 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
185
186 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
187 // form "i != 47 & i != 87". Same state transitions as for true elements.
188 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
189
190 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
191 /// define a state machine that triggers for ranges of values that the index
192 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
193 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
194 /// index in the range (inclusive). We use -2 for undefined here because we
195 /// use relative comparisons and don't want 0-1 to match -1.
196 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
197
198 // MagicBitvector - This is a magic bitvector where we set a bit if the
199 // comparison is true for element 'i'. If there are 64 elements or less in
200 // the array, this will fully represent all the comparison results.
201 uint64_t MagicBitvector = 0;
202
203 // Scan the array and see if one of our patterns matches.
204 APInt Offset = ConstOffset;
205 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i, Offset += Stride) {
206 Constant *Elt = ConstantFoldLoadFromConst(C: Init, Ty: EltTy, Offset, DL);
207 if (!Elt)
208 return nullptr;
209
210 // If the element is masked, handle it.
211 if (AndCst) {
212 Elt = ConstantFoldBinaryOpOperands(Opcode: Instruction::And, LHS: Elt, RHS: AndCst, DL);
213 if (!Elt)
214 return nullptr;
215 }
216
217 // Find out if the comparison would be true or false for the i'th element.
218 Constant *C = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: Elt, RHS: RHSC, DL, TLI: &TLI);
219 if (!C)
220 return nullptr;
221
222 // If the result is undef for this element, ignore it.
223 if (isa<UndefValue>(Val: C)) {
224 // Extend range state machines to cover this element in case there is an
225 // undef in the middle of the range.
226 if (TrueRangeEnd == (int)i - 1)
227 TrueRangeEnd = i;
228 if (FalseRangeEnd == (int)i - 1)
229 FalseRangeEnd = i;
230 continue;
231 }
232
233 // If we can't compute the result for any of the elements, we have to give
234 // up evaluating the entire conditional.
235 if (!isa<ConstantInt>(Val: C))
236 return nullptr;
237
238 // Otherwise, we know if the comparison is true or false for this element,
239 // update our state machines.
240 bool IsTrueForElt = !cast<ConstantInt>(Val: C)->isZero();
241
242 // State machine for single/double/range index comparison.
243 if (IsTrueForElt) {
244 // Update the TrueElement state machine.
245 if (FirstTrueElement == Undefined)
246 FirstTrueElement = TrueRangeEnd = i; // First true element.
247 else {
248 // Update double-compare state machine.
249 if (SecondTrueElement == Undefined)
250 SecondTrueElement = i;
251 else
252 SecondTrueElement = Overdefined;
253
254 // Update range state machine.
255 if (TrueRangeEnd == (int)i - 1)
256 TrueRangeEnd = i;
257 else
258 TrueRangeEnd = Overdefined;
259 }
260 } else {
261 // Update the FalseElement state machine.
262 if (FirstFalseElement == Undefined)
263 FirstFalseElement = FalseRangeEnd = i; // First false element.
264 else {
265 // Update double-compare state machine.
266 if (SecondFalseElement == Undefined)
267 SecondFalseElement = i;
268 else
269 SecondFalseElement = Overdefined;
270
271 // Update range state machine.
272 if (FalseRangeEnd == (int)i - 1)
273 FalseRangeEnd = i;
274 else
275 FalseRangeEnd = Overdefined;
276 }
277 }
278
279 // If this element is in range, update our magic bitvector.
280 if (i < 64 && IsTrueForElt)
281 MagicBitvector |= 1ULL << i;
282
283 // If all of our states become overdefined and we need more than 64 bits in
284 // the bit vector, bail out.
285 if (i >= 64 && SecondTrueElement == Overdefined &&
286 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
287 FalseRangeEnd == Overdefined)
288 return nullptr;
289 }
290
291 // Now that we've scanned the entire array, emit our new comparison(s). We
292 // order the state machines in complexity of the generated code.
293
294 // If none of nusw (implied by inbounds) or nuw is present, Idx * Stride can
295 // overflow.
296 // Let's assume that Stride is 2 and the wanted value is at offset 0.
297 // Then, there are two possible values for Idx to match offset 0:
298 // 0x00..00, 0x80..00.
299 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
300 // comparison is false if Idx was 0x80..00.
301 // We need to erase the highest countTrailingZeros(Stride) bits of Idx.
302 auto MaskIdx = [&](Value *Idx) {
303 if (!Expr.Flags.hasNoUnsignedWrap() &&
304 !Expr.Flags.hasNoUnsignedSignedWrap() && Stride.countr_zero() != 0) {
305 Value *Mask = Constant::getAllOnesValue(Ty: Idx->getType());
306 Mask = Builder.CreateLShr(LHS: Mask, RHS: Stride.countr_zero());
307 Idx = Builder.CreateAnd(LHS: Idx, RHS: Mask);
308 }
309 return Idx;
310 };
311
312 // If the comparison is only true for one or two elements, emit direct
313 // comparisons.
314 if (SecondTrueElement != Overdefined) {
315 Idx = MaskIdx(Idx);
316 // None true -> false.
317 if (FirstTrueElement == Undefined)
318 return replaceInstUsesWith(I&: CtxI, V: Builder.getFalse());
319
320 Value *FirstTrueIdx = ConstantInt::get(Ty: Idx->getType(), V: FirstTrueElement);
321
322 // True for one element -> 'i == 47'.
323 if (SecondTrueElement == Undefined)
324 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
325
326 // True for two elements -> 'i == 47 | i == 72'.
327 Value *C1 = Builder.CreateICmpEQ(LHS: Idx, RHS: FirstTrueIdx);
328 Value *SecondTrueIdx = ConstantInt::get(Ty: Idx->getType(), V: SecondTrueElement);
329 Value *C2 = Builder.CreateICmpEQ(LHS: Idx, RHS: SecondTrueIdx);
330 return BinaryOperator::CreateOr(V1: C1, V2: C2);
331 }
332
333 // If the comparison is only false for one or two elements, emit direct
334 // comparisons.
335 if (SecondFalseElement != Overdefined) {
336 Idx = MaskIdx(Idx);
337 // None false -> true.
338 if (FirstFalseElement == Undefined)
339 return replaceInstUsesWith(I&: CtxI, V: Builder.getTrue());
340
341 Value *FirstFalseIdx = ConstantInt::get(Ty: Idx->getType(), V: FirstFalseElement);
342
343 // False for one element -> 'i != 47'.
344 if (SecondFalseElement == Undefined)
345 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
346
347 // False for two elements -> 'i != 47 & i != 72'.
348 Value *C1 = Builder.CreateICmpNE(LHS: Idx, RHS: FirstFalseIdx);
349 Value *SecondFalseIdx =
350 ConstantInt::get(Ty: Idx->getType(), V: SecondFalseElement);
351 Value *C2 = Builder.CreateICmpNE(LHS: Idx, RHS: SecondFalseIdx);
352 return BinaryOperator::CreateAnd(V1: C1, V2: C2);
353 }
354
355 // If the comparison can be replaced with a range comparison for the elements
356 // where it is true, emit the range check.
357 if (TrueRangeEnd != Overdefined) {
358 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
359 Idx = MaskIdx(Idx);
360
361 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
362 if (FirstTrueElement) {
363 Value *Offs = ConstantInt::getSigned(Ty: Idx->getType(), V: -FirstTrueElement);
364 Idx = Builder.CreateAdd(LHS: Idx, RHS: Offs);
365 }
366
367 Value *End =
368 ConstantInt::get(Ty: Idx->getType(), V: TrueRangeEnd - FirstTrueElement + 1);
369 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
370 }
371
372 // False range check.
373 if (FalseRangeEnd != Overdefined) {
374 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
375 Idx = MaskIdx(Idx);
376 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
377 if (FirstFalseElement) {
378 Value *Offs = ConstantInt::getSigned(Ty: Idx->getType(), V: -FirstFalseElement);
379 Idx = Builder.CreateAdd(LHS: Idx, RHS: Offs);
380 }
381
382 Value *End =
383 ConstantInt::get(Ty: Idx->getType(), V: FalseRangeEnd - FirstFalseElement);
384 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
385 }
386
387 // If a magic bitvector captures the entire comparison state
388 // of this load, replace it with computation that does:
389 // ((magic_cst >> i) & 1) != 0
390 {
391 Type *Ty = nullptr;
392
393 // Look for an appropriate type:
394 // - The type of Idx if the magic fits
395 // - The smallest fitting legal type
396 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
397 Ty = Idx->getType();
398 else
399 Ty = DL.getSmallestLegalIntType(C&: Init->getContext(), Width: ArrayElementCount);
400
401 if (Ty) {
402 Idx = MaskIdx(Idx);
403 Value *V = Builder.CreateIntCast(V: Idx, DestTy: Ty, isSigned: false);
404 V = Builder.CreateLShr(LHS: ConstantInt::get(Ty, V: MagicBitvector), RHS: V);
405 V = Builder.CreateAnd(LHS: ConstantInt::get(Ty, V: 1), RHS: V);
406 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, V: 0));
407 }
408 }
409
410 return nullptr;
411}
412
413/// Returns true if we can rewrite Start as a GEP with pointer Base
414/// and some integer offset. The nodes that need to be re-written
415/// for this transformation will be added to Explored.
416static bool canRewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags &NW,
417 const DataLayout &DL,
418 SetVector<Value *> &Explored) {
419 SmallVector<Value *, 16> WorkList(1, Start);
420 Explored.insert(X: Base);
421
422 // The following traversal gives us an order which can be used
423 // when doing the final transformation. Since in the final
424 // transformation we create the PHI replacement instructions first,
425 // we don't have to get them in any particular order.
426 //
427 // However, for other instructions we will have to traverse the
428 // operands of an instruction first, which means that we have to
429 // do a post-order traversal.
430 while (!WorkList.empty()) {
431 SetVector<PHINode *> PHIs;
432
433 while (!WorkList.empty()) {
434 if (Explored.size() >= 100)
435 return false;
436
437 Value *V = WorkList.back();
438
439 if (Explored.contains(key: V)) {
440 WorkList.pop_back();
441 continue;
442 }
443
444 if (!isa<GetElementPtrInst>(Val: V) && !isa<PHINode>(Val: V))
445 // We've found some value that we can't explore which is different from
446 // the base. Therefore we can't do this transformation.
447 return false;
448
449 if (auto *GEP = dyn_cast<GEPOperator>(Val: V)) {
450 // Only allow inbounds GEPs with at most one variable offset.
451 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(Val: V); };
452 if (!GEP->isInBounds() || count_if(Range: GEP->indices(), P: IsNonConst) > 1)
453 return false;
454
455 NW = NW.intersectForOffsetAdd(Other: GEP->getNoWrapFlags());
456 if (!Explored.contains(key: GEP->getOperand(i_nocapture: 0)))
457 WorkList.push_back(Elt: GEP->getOperand(i_nocapture: 0));
458 }
459
460 if (WorkList.back() == V) {
461 WorkList.pop_back();
462 // We've finished visiting this node, mark it as such.
463 Explored.insert(X: V);
464 }
465
466 if (auto *PN = dyn_cast<PHINode>(Val: V)) {
467 // We cannot transform PHIs on unsplittable basic blocks.
468 if (isa<CatchSwitchInst>(Val: PN->getParent()->getTerminator()))
469 return false;
470 Explored.insert(X: PN);
471 PHIs.insert(X: PN);
472 }
473 }
474
475 // Explore the PHI nodes further.
476 for (auto *PN : PHIs)
477 for (Value *Op : PN->incoming_values())
478 if (!Explored.contains(key: Op))
479 WorkList.push_back(Elt: Op);
480 }
481
482 // Make sure that we can do this. Since we can't insert GEPs in a basic
483 // block before a PHI node, we can't easily do this transformation if
484 // we have PHI node users of transformed instructions.
485 for (Value *Val : Explored) {
486 for (Value *Use : Val->uses()) {
487
488 auto *PHI = dyn_cast<PHINode>(Val: Use);
489 auto *Inst = dyn_cast<Instruction>(Val);
490
491 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
492 !Explored.contains(key: PHI))
493 continue;
494
495 if (PHI->getParent() == Inst->getParent())
496 return false;
497 }
498 }
499 return true;
500}
501
502// Sets the appropriate insert point on Builder where we can add
503// a replacement Instruction for V (if that is possible).
504static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
505 bool Before = true) {
506 if (auto *PHI = dyn_cast<PHINode>(Val: V)) {
507 BasicBlock *Parent = PHI->getParent();
508 Builder.SetInsertPoint(Parent->getFirstInsertionPt());
509 return;
510 }
511 if (auto *I = dyn_cast<Instruction>(Val: V)) {
512 if (!Before)
513 I = &*std::next(x: I->getIterator());
514 Builder.SetInsertPoint(I);
515 return;
516 }
517 if (auto *A = dyn_cast<Argument>(Val: V)) {
518 // Set the insertion point in the entry block.
519 BasicBlock &Entry = A->getParent()->getEntryBlock();
520 Builder.SetInsertPoint(Entry.getFirstInsertionPt());
521 return;
522 }
523 // Otherwise, this is a constant and we don't need to set a new
524 // insertion point.
525 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
526}
527
528/// Returns a re-written value of Start as an indexed GEP using Base as a
529/// pointer.
530static Value *rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW,
531 const DataLayout &DL,
532 SetVector<Value *> &Explored,
533 InstCombiner &IC) {
534 // Perform all the substitutions. This is a bit tricky because we can
535 // have cycles in our use-def chains.
536 // 1. Create the PHI nodes without any incoming values.
537 // 2. Create all the other values.
538 // 3. Add the edges for the PHI nodes.
539 // 4. Emit GEPs to get the original pointers.
540 // 5. Remove the original instructions.
541 Type *IndexType = IntegerType::get(
542 C&: Base->getContext(), NumBits: DL.getIndexTypeSizeInBits(Ty: Start->getType()));
543
544 DenseMap<Value *, Value *> NewInsts;
545 NewInsts[Base] = ConstantInt::getNullValue(Ty: IndexType);
546
547 // Create the new PHI nodes, without adding any incoming values.
548 for (Value *Val : Explored) {
549 if (Val == Base)
550 continue;
551 // Create empty phi nodes. This avoids cyclic dependencies when creating
552 // the remaining instructions.
553 if (auto *PHI = dyn_cast<PHINode>(Val))
554 NewInsts[PHI] =
555 PHINode::Create(Ty: IndexType, NumReservedValues: PHI->getNumIncomingValues(),
556 NameStr: PHI->getName() + ".idx", InsertBefore: PHI->getIterator());
557 }
558 IRBuilder<> Builder(IC.getModule());
559
560 // Create all the other instructions.
561 for (Value *Val : Explored) {
562 if (NewInsts.contains(Val))
563 continue;
564
565 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
566 setInsertionPoint(Builder, V: GEP);
567 Value *Op = NewInsts[GEP->getOperand(i_nocapture: 0)];
568 Value *OffsetV = emitGEPOffset(Builder: &Builder, DL, GEP);
569 if (isa<ConstantInt>(Val: Op) && cast<ConstantInt>(Val: Op)->isZero())
570 NewInsts[GEP] = OffsetV;
571 else
572 NewInsts[GEP] = Builder.CreateAdd(
573 LHS: Op, RHS: OffsetV, Name: GEP->getOperand(i_nocapture: 0)->getName() + ".add",
574 /*NUW=*/HasNUW: NW.hasNoUnsignedWrap(),
575 /*NSW=*/HasNSW: NW.hasNoUnsignedSignedWrap());
576 continue;
577 }
578 if (isa<PHINode>(Val))
579 continue;
580
581 llvm_unreachable("Unexpected instruction type");
582 }
583
584 // Add the incoming values to the PHI nodes.
585 for (Value *Val : Explored) {
586 if (Val == Base)
587 continue;
588 // All the instructions have been created, we can now add edges to the
589 // phi nodes.
590 if (auto *PHI = dyn_cast<PHINode>(Val)) {
591 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
592 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
593 Value *NewIncoming = PHI->getIncomingValue(i: I);
594
595 auto It = NewInsts.find(Val: NewIncoming);
596 if (It != NewInsts.end())
597 NewIncoming = It->second;
598
599 NewPhi->addIncoming(V: NewIncoming, BB: PHI->getIncomingBlock(i: I));
600 }
601 }
602 }
603
604 for (Value *Val : Explored) {
605 if (Val == Base)
606 continue;
607
608 setInsertionPoint(Builder, V: Val, Before: false);
609 // Create GEP for external users.
610 Value *NewVal = Builder.CreateGEP(Ty: Builder.getInt8Ty(), Ptr: Base, IdxList: NewInsts[Val],
611 Name: Val->getName() + ".ptr", NW);
612 IC.replaceInstUsesWith(I&: *cast<Instruction>(Val), V: NewVal);
613 // Add old instruction to worklist for DCE. We don't directly remove it
614 // here because the original compare is one of the users.
615 IC.addToWorklist(I: cast<Instruction>(Val));
616 }
617
618 return NewInsts[Start];
619}
620
621/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
622/// We can look through PHIs, GEPs and casts in order to determine a common base
623/// between GEPLHS and RHS.
624static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
625 CmpPredicate Cond,
626 const DataLayout &DL,
627 InstCombiner &IC) {
628 // FIXME: Support vector of pointers.
629 if (GEPLHS->getType()->isVectorTy())
630 return nullptr;
631
632 if (!GEPLHS->hasAllConstantIndices())
633 return nullptr;
634
635 APInt Offset(DL.getIndexTypeSizeInBits(Ty: GEPLHS->getType()), 0);
636 Value *PtrBase =
637 GEPLHS->stripAndAccumulateConstantOffsets(DL, Offset,
638 /*AllowNonInbounds*/ false);
639
640 // Bail if we looked through addrspacecast.
641 if (PtrBase->getType() != GEPLHS->getType())
642 return nullptr;
643
644 // The set of nodes that will take part in this transformation.
645 SetVector<Value *> Nodes;
646 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
647 if (!canRewriteGEPAsOffset(Start: RHS, Base: PtrBase, NW, DL, Explored&: Nodes))
648 return nullptr;
649
650 // We know we can re-write this as
651 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
652 // Since we've only looked through inbouds GEPs we know that we
653 // can't have overflow on either side. We can therefore re-write
654 // this as:
655 // OFFSET1 cmp OFFSET2
656 Value *NewRHS = rewriteGEPAsOffset(Start: RHS, Base: PtrBase, NW, DL, Explored&: Nodes, IC);
657
658 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
659 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
660 // offset. Since Index is the offset of LHS to the base pointer, we will now
661 // compare the offsets instead of comparing the pointers.
662 return new ICmpInst(ICmpInst::getSignedPredicate(Pred: Cond),
663 IC.Builder.getInt(AI: Offset), NewRHS);
664}
665
666/// Fold comparisons between a GEP instruction and something else. At this point
667/// we know that the GEP is on the LHS of the comparison.
668Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
669 CmpPredicate Cond, Instruction &I) {
670 // Don't transform signed compares of GEPs into index compares. Even if the
671 // GEP is inbounds, the final add of the base pointer can have signed overflow
672 // and would change the result of the icmp.
673 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
674 // the maximum signed value for the pointer type.
675 if (ICmpInst::isSigned(Pred: Cond))
676 return nullptr;
677
678 // Look through bitcasts and addrspacecasts. We do not however want to remove
679 // 0 GEPs.
680 if (!isa<GetElementPtrInst>(Val: RHS))
681 RHS = RHS->stripPointerCasts();
682
683 auto CanFold = [Cond](GEPNoWrapFlags NW) {
684 if (ICmpInst::isEquality(P: Cond))
685 return true;
686
687 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags.
688 assert(ICmpInst::isUnsigned(Cond));
689 return NW != GEPNoWrapFlags::none();
690 };
691
692 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) {
693 if (!NW.hasNoUnsignedWrap()) {
694 // Convert signed to unsigned comparison.
695 return new ICmpInst(ICmpInst::getSignedPredicate(Pred: Cond), Op1, Op2);
696 }
697
698 auto *I = new ICmpInst(Cond, Op1, Op2);
699 I->setSameSign(NW.hasNoUnsignedSignedWrap());
700 return I;
701 };
702
703 CommonPointerBase Base = CommonPointerBase::compute(LHS: GEPLHS, RHS);
704 if (Base.Ptr == RHS && CanFold(Base.LHSNW) && !Base.isExpensive()) {
705 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
706 Type *IdxTy = DL.getIndexType(PtrTy: GEPLHS->getType());
707 Value *Offset =
708 EmitGEPOffsets(GEPs: Base.LHSGEPs, NW: Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);
709 return NewICmp(Base.LHSNW, Offset,
710 Constant::getNullValue(Ty: Offset->getType()));
711 }
712
713 if (GEPLHS->isInBounds() && ICmpInst::isEquality(P: Cond) &&
714 isa<ConstantPointerNull>(Val: RHS) &&
715 !NullPointerIsDefined(F: I.getFunction(),
716 AS: RHS->getType()->getPointerAddressSpace())) {
717 // For most address spaces, an allocation can't be placed at null, but null
718 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
719 // the only valid inbounds address derived from null, is null itself.
720 // Thus, we have four cases to consider:
721 // 1) Base == nullptr, Offset == 0 -> inbounds, null
722 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
723 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
724 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
725 //
726 // (Note if we're indexing a type of size 0, that simply collapses into one
727 // of the buckets above.)
728 //
729 // In general, we're allowed to make values less poison (i.e. remove
730 // sources of full UB), so in this case, we just select between the two
731 // non-poison cases (1 and 4 above).
732 //
733 // For vectors, we apply the same reasoning on a per-lane basis.
734 auto *Base = GEPLHS->getPointerOperand();
735 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
736 auto EC = cast<VectorType>(Val: GEPLHS->getType())->getElementCount();
737 Base = Builder.CreateVectorSplat(EC, V: Base);
738 }
739 return new ICmpInst(Cond, Base,
740 ConstantExpr::getPointerBitCastOrAddrSpaceCast(
741 C: cast<Constant>(Val: RHS), Ty: Base->getType()));
742 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(Val: RHS)) {
743 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
744
745 // If the base pointers are different, but the indices are the same, just
746 // compare the base pointer.
747 if (GEPLHS->getOperand(i_nocapture: 0) != GEPRHS->getOperand(i_nocapture: 0)) {
748 bool IndicesTheSame =
749 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
750 GEPLHS->getPointerOperand()->getType() ==
751 GEPRHS->getPointerOperand()->getType() &&
752 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
753 if (IndicesTheSame)
754 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
755 if (GEPLHS->getOperand(i_nocapture: i) != GEPRHS->getOperand(i_nocapture: i)) {
756 IndicesTheSame = false;
757 break;
758 }
759
760 // If all indices are the same, just compare the base pointers.
761 Type *BaseType = GEPLHS->getOperand(i_nocapture: 0)->getType();
762 if (IndicesTheSame &&
763 CmpInst::makeCmpResultType(opnd_type: BaseType) == I.getType() && CanFold(NW))
764 return new ICmpInst(Cond, GEPLHS->getOperand(i_nocapture: 0), GEPRHS->getOperand(i_nocapture: 0));
765
766 // If we're comparing GEPs with two base pointers that only differ in type
767 // and both GEPs have only constant indices or just one use, then fold
768 // the compare with the adjusted indices.
769 // FIXME: Support vector of pointers.
770 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
771 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
772 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
773 GEPLHS->getOperand(i_nocapture: 0)->stripPointerCasts() ==
774 GEPRHS->getOperand(i_nocapture: 0)->stripPointerCasts() &&
775 !GEPLHS->getType()->isVectorTy()) {
776 Value *LOffset = EmitGEPOffset(GEP: GEPLHS);
777 Value *ROffset = EmitGEPOffset(GEP: GEPRHS);
778
779 // If we looked through an addrspacecast between different sized address
780 // spaces, the LHS and RHS pointers are different sized
781 // integers. Truncate to the smaller one.
782 Type *LHSIndexTy = LOffset->getType();
783 Type *RHSIndexTy = ROffset->getType();
784 if (LHSIndexTy != RHSIndexTy) {
785 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <
786 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {
787 ROffset = Builder.CreateTrunc(V: ROffset, DestTy: LHSIndexTy);
788 } else
789 LOffset = Builder.CreateTrunc(V: LOffset, DestTy: RHSIndexTy);
790 }
791
792 Value *Cmp = Builder.CreateICmp(P: ICmpInst::getSignedPredicate(Pred: Cond),
793 LHS: LOffset, RHS: ROffset);
794 return replaceInstUsesWith(I, V: Cmp);
795 }
796 }
797
798 if (GEPLHS->getOperand(i_nocapture: 0) == GEPRHS->getOperand(i_nocapture: 0) &&
799 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
800 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
801 // If the GEPs only differ by one index, compare it.
802 unsigned NumDifferences = 0; // Keep track of # differences.
803 unsigned DiffOperand = 0; // The operand that differs.
804 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
805 if (GEPLHS->getOperand(i_nocapture: i) != GEPRHS->getOperand(i_nocapture: i)) {
806 Type *LHSType = GEPLHS->getOperand(i_nocapture: i)->getType();
807 Type *RHSType = GEPRHS->getOperand(i_nocapture: i)->getType();
808 // FIXME: Better support for vector of pointers.
809 if (LHSType->getPrimitiveSizeInBits() !=
810 RHSType->getPrimitiveSizeInBits() ||
811 (GEPLHS->getType()->isVectorTy() &&
812 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
813 // Irreconcilable differences.
814 NumDifferences = 2;
815 break;
816 }
817
818 if (NumDifferences++)
819 break;
820 DiffOperand = i;
821 }
822
823 if (NumDifferences == 0) // SAME GEP?
824 return replaceInstUsesWith(
825 I, // No comparison is needed here.
826 V: ConstantInt::get(Ty: I.getType(), V: ICmpInst::isTrueWhenEqual(predicate: Cond)));
827 // If two GEPs only differ by an index, compare them.
828 // Note that nowrap flags are always needed when comparing two indices.
829 else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {
830 Value *LHSV = GEPLHS->getOperand(i_nocapture: DiffOperand);
831 Value *RHSV = GEPRHS->getOperand(i_nocapture: DiffOperand);
832 return NewICmp(NW, LHSV, RHSV);
833 }
834 }
835
836 if (Base.Ptr && !Base.isExpensive()) {
837 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
838 bool DoFold = CanFold(Base.LHSNW & Base.RHSNW);
839
840 if (!DoFold && Base.Ptr->getType()->isPointerTy()) {
841 // Without the flags, we can still fold if the offsets are constant and
842 // they cross the base's alignment boundary the same number of times, so
843 // either both arguments will wrap, or none of them will.
844 unsigned BW = DL.getIndexTypeSizeInBits(Ty: GEPLHS->getType());
845 APInt Alignment = APInt(BW, Base.Ptr->getPointerAlignment(DL).value());
846 APInt LOff(BW, 0);
847 APInt ROff(BW, 0);
848 if (GEPLHS->stripAndAccumulateConstantOffsets(
849 DL, Offset&: LOff, /*AllowNonInbounds=*/true) == Base.Ptr &&
850 RHS->stripAndAccumulateConstantOffsets(
851 DL, Offset&: ROff, /*AllowNonInbounds=*/true) == Base.Ptr)
852 DoFold =
853 APIntOps::RoundingSDiv(A: LOff, B: Alignment, RM: APInt::Rounding::DOWN) ==
854 APIntOps::RoundingSDiv(A: ROff, B: Alignment, RM: APInt::Rounding::DOWN);
855 }
856
857 if (DoFold) {
858 Type *IdxTy = DL.getIndexType(PtrTy: GEPLHS->getType());
859 Value *L = EmitGEPOffsets(GEPs: Base.LHSGEPs, NW: Base.LHSNW, IdxTy,
860 /*RewriteGEP=*/RewriteGEPs: true);
861 Value *R = EmitGEPOffsets(GEPs: Base.RHSGEPs, NW: Base.RHSNW, IdxTy,
862 /*RewriteGEP=*/RewriteGEPs: true);
863 return NewICmp(Base.LHSNW & Base.RHSNW, L, R);
864 }
865 }
866 }
867
868 // Try convert this to an indexed compare by looking through PHIs/casts as a
869 // last resort.
870 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, IC&: *this);
871}
872
873bool InstCombinerImpl::foldAllocaCmp(AllocaInst *Alloca) {
874 // It would be tempting to fold away comparisons between allocas and any
875 // pointer not based on that alloca (e.g. an argument). However, even
876 // though such pointers cannot alias, they can still compare equal.
877 //
878 // But LLVM doesn't specify where allocas get their memory, so if the alloca
879 // doesn't escape we can argue that it's impossible to guess its value, and we
880 // can therefore act as if any such guesses are wrong.
881 //
882 // However, we need to ensure that this folding is consistent: We can't fold
883 // one comparison to false, and then leave a different comparison against the
884 // same value alone (as it might evaluate to true at runtime, leading to a
885 // contradiction). As such, this code ensures that all comparisons are folded
886 // at the same time, and there are no other escapes.
887
888 struct CmpCaptureTracker : public CaptureTracker {
889 AllocaInst *Alloca;
890 bool Captured = false;
891 /// The value of the map is a bit mask of which icmp operands the alloca is
892 /// used in.
893 SmallMapVector<ICmpInst *, unsigned, 4> ICmps;
894
895 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
896
897 void tooManyUses() override { Captured = true; }
898
899 Action captured(const Use *U, UseCaptureInfo CI) override {
900 // TODO(captures): Use UseCaptureInfo.
901 auto *ICmp = dyn_cast<ICmpInst>(Val: U->getUser());
902 // We need to check that U is based *only* on the alloca, and doesn't
903 // have other contributions from a select/phi operand.
904 // TODO: We could check whether getUnderlyingObjects() reduces to one
905 // object, which would allow looking through phi nodes.
906 if (ICmp && ICmp->isEquality() && getUnderlyingObject(V: *U) == Alloca) {
907 // Collect equality icmps of the alloca, and don't treat them as
908 // captures.
909 ICmps[ICmp] |= 1u << U->getOperandNo();
910 return Continue;
911 }
912
913 Captured = true;
914 return Stop;
915 }
916 };
917
918 CmpCaptureTracker Tracker(Alloca);
919 PointerMayBeCaptured(V: Alloca, Tracker: &Tracker);
920 if (Tracker.Captured)
921 return false;
922
923 bool Changed = false;
924 for (auto [ICmp, Operands] : Tracker.ICmps) {
925 switch (Operands) {
926 case 1:
927 case 2: {
928 // The alloca is only used in one icmp operand. Assume that the
929 // equality is false.
930 auto *Res = ConstantInt::get(Ty: ICmp->getType(),
931 V: ICmp->getPredicate() == ICmpInst::ICMP_NE);
932 replaceInstUsesWith(I&: *ICmp, V: Res);
933 eraseInstFromFunction(I&: *ICmp);
934 Changed = true;
935 break;
936 }
937 case 3:
938 // Both icmp operands are based on the alloca, so this is comparing
939 // pointer offsets, without leaking any information about the address
940 // of the alloca. Ignore such comparisons.
941 break;
942 default:
943 llvm_unreachable("Cannot happen");
944 }
945 }
946
947 return Changed;
948}
949
950/// Fold "icmp pred (X+C), X".
951Instruction *InstCombinerImpl::foldICmpAddOpConst(Value *X, const APInt &C,
952 CmpPredicate Pred) {
953 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
954 // so the values can never be equal. Similarly for all other "or equals"
955 // operators.
956 assert(!!C && "C should not be zero!");
957
958 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
959 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
960 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
961 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
962 Constant *R =
963 ConstantInt::get(Ty: X->getType(), V: APInt::getMaxValue(numBits: C.getBitWidth()) - C);
964 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
965 }
966
967 // (X+1) >u X --> X <u (0-1) --> X != 255
968 // (X+2) >u X --> X <u (0-2) --> X <u 254
969 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
970 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
971 return new ICmpInst(ICmpInst::ICMP_ULT, X,
972 ConstantInt::get(Ty: X->getType(), V: -C));
973
974 APInt SMax = APInt::getSignedMaxValue(numBits: C.getBitWidth());
975
976 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
977 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
978 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
979 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
980 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
981 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
982 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
983 return new ICmpInst(ICmpInst::ICMP_SGT, X,
984 ConstantInt::get(Ty: X->getType(), V: SMax - C));
985
986 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
987 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
988 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
989 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
990 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
991 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
992
993 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
994 return new ICmpInst(ICmpInst::ICMP_SLT, X,
995 ConstantInt::get(Ty: X->getType(), V: SMax - (C - 1)));
996}
997
998/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
999/// (icmp eq/ne A, Log2(AP2/AP1)) ->
1000/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
1001Instruction *InstCombinerImpl::foldICmpShrConstConst(ICmpInst &I, Value *A,
1002 const APInt &AP1,
1003 const APInt &AP2) {
1004 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1005
1006 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1007 if (I.getPredicate() == I.ICMP_NE)
1008 Pred = CmpInst::getInversePredicate(pred: Pred);
1009 return new ICmpInst(Pred, LHS, RHS);
1010 };
1011
1012 // Don't bother doing any work for cases which InstSimplify handles.
1013 if (AP2.isZero())
1014 return nullptr;
1015
1016 bool IsAShr = isa<AShrOperator>(Val: I.getOperand(i_nocapture: 0));
1017 if (IsAShr) {
1018 if (AP2.isAllOnes())
1019 return nullptr;
1020 if (AP2.isNegative() != AP1.isNegative())
1021 return nullptr;
1022 if (AP2.sgt(RHS: AP1))
1023 return nullptr;
1024 }
1025
1026 if (!AP1)
1027 // 'A' must be large enough to shift out the highest set bit.
1028 return getICmp(I.ICMP_UGT, A,
1029 ConstantInt::get(Ty: A->getType(), V: AP2.logBase2()));
1030
1031 if (AP1 == AP2)
1032 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(Ty: A->getType()));
1033
1034 int Shift;
1035 if (IsAShr && AP1.isNegative())
1036 Shift = AP1.countl_one() - AP2.countl_one();
1037 else
1038 Shift = AP1.countl_zero() - AP2.countl_zero();
1039
1040 if (Shift > 0) {
1041 if (IsAShr && AP1 == AP2.ashr(ShiftAmt: Shift)) {
1042 // There are multiple solutions if we are comparing against -1 and the LHS
1043 // of the ashr is not a power of two.
1044 if (AP1.isAllOnes() && !AP2.isPowerOf2())
1045 return getICmp(I.ICMP_UGE, A, ConstantInt::get(Ty: A->getType(), V: Shift));
1046 return getICmp(I.ICMP_EQ, A, ConstantInt::get(Ty: A->getType(), V: Shift));
1047 } else if (AP1 == AP2.lshr(shiftAmt: Shift)) {
1048 return getICmp(I.ICMP_EQ, A, ConstantInt::get(Ty: A->getType(), V: Shift));
1049 }
1050 }
1051
1052 // Shifting const2 will never be equal to const1.
1053 // FIXME: This should always be handled by InstSimplify?
1054 auto *TorF = ConstantInt::get(Ty: I.getType(), V: I.getPredicate() == I.ICMP_NE);
1055 return replaceInstUsesWith(I, V: TorF);
1056}
1057
1058/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1059/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1060Instruction *InstCombinerImpl::foldICmpShlConstConst(ICmpInst &I, Value *A,
1061 const APInt &AP1,
1062 const APInt &AP2) {
1063 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1064
1065 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1066 if (I.getPredicate() == I.ICMP_NE)
1067 Pred = CmpInst::getInversePredicate(pred: Pred);
1068 return new ICmpInst(Pred, LHS, RHS);
1069 };
1070
1071 // Don't bother doing any work for cases which InstSimplify handles.
1072 if (AP2.isZero())
1073 return nullptr;
1074
1075 unsigned AP2TrailingZeros = AP2.countr_zero();
1076
1077 if (!AP1 && AP2TrailingZeros != 0)
1078 return getICmp(
1079 I.ICMP_UGE, A,
1080 ConstantInt::get(Ty: A->getType(), V: AP2.getBitWidth() - AP2TrailingZeros));
1081
1082 if (AP1 == AP2)
1083 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(Ty: A->getType()));
1084
1085 // Get the distance between the lowest bits that are set.
1086 int Shift = AP1.countr_zero() - AP2TrailingZeros;
1087
1088 if (Shift > 0 && AP2.shl(shiftAmt: Shift) == AP1)
1089 return getICmp(I.ICMP_EQ, A, ConstantInt::get(Ty: A->getType(), V: Shift));
1090
1091 // Shifting const2 will never be equal to const1.
1092 // FIXME: This should always be handled by InstSimplify?
1093 auto *TorF = ConstantInt::get(Ty: I.getType(), V: I.getPredicate() == I.ICMP_NE);
1094 return replaceInstUsesWith(I, V: TorF);
1095}
1096
1097/// The caller has matched a pattern of the form:
1098/// I = icmp ugt (add (add A, B), CI2), CI1
1099/// If this is of the form:
1100/// sum = a + b
1101/// if (sum+128 >u 255)
1102/// Then replace it with llvm.sadd.with.overflow.i8.
1103///
1104static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
1105 ConstantInt *CI2, ConstantInt *CI1,
1106 InstCombinerImpl &IC) {
1107 // The transformation we're trying to do here is to transform this into an
1108 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1109 // with a narrower add, and discard the add-with-constant that is part of the
1110 // range check (if we can't eliminate it, this isn't profitable).
1111
1112 // In order to eliminate the add-with-constant, the compare can be its only
1113 // use.
1114 Instruction *AddWithCst = cast<Instruction>(Val: I.getOperand(i_nocapture: 0));
1115 if (!AddWithCst->hasOneUse())
1116 return nullptr;
1117
1118 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1119 if (!CI2->getValue().isPowerOf2())
1120 return nullptr;
1121 unsigned NewWidth = CI2->getValue().countr_zero();
1122 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1123 return nullptr;
1124
1125 // The width of the new add formed is 1 more than the bias.
1126 ++NewWidth;
1127
1128 // Check to see that CI1 is an all-ones value with NewWidth bits.
1129 if (CI1->getBitWidth() == NewWidth ||
1130 CI1->getValue() != APInt::getLowBitsSet(numBits: CI1->getBitWidth(), loBitsSet: NewWidth))
1131 return nullptr;
1132
1133 // This is only really a signed overflow check if the inputs have been
1134 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1135 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1136 if (IC.ComputeMaxSignificantBits(Op: A, CtxI: &I) > NewWidth ||
1137 IC.ComputeMaxSignificantBits(Op: B, CtxI: &I) > NewWidth)
1138 return nullptr;
1139
1140 // In order to replace the original add with a narrower
1141 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1142 // and truncates that discard the high bits of the add. Verify that this is
1143 // the case.
1144 Instruction *OrigAdd = cast<Instruction>(Val: AddWithCst->getOperand(i: 0));
1145 for (User *U : OrigAdd->users()) {
1146 if (U == AddWithCst)
1147 continue;
1148
1149 // Only accept truncates for now. We would really like a nice recursive
1150 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1151 // chain to see which bits of a value are actually demanded. If the
1152 // original add had another add which was then immediately truncated, we
1153 // could still do the transformation.
1154 TruncInst *TI = dyn_cast<TruncInst>(Val: U);
1155 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1156 return nullptr;
1157 }
1158
1159 // If the pattern matches, truncate the inputs to the narrower type and
1160 // use the sadd_with_overflow intrinsic to efficiently compute both the
1161 // result and the overflow bit.
1162 Type *NewType = IntegerType::get(C&: OrigAdd->getContext(), NumBits: NewWidth);
1163 Function *F = Intrinsic::getOrInsertDeclaration(
1164 M: I.getModule(), id: Intrinsic::sadd_with_overflow, OverloadTys: NewType);
1165
1166 InstCombiner::BuilderTy &Builder = IC.Builder;
1167
1168 // Put the new code above the original add, in case there are any uses of the
1169 // add between the add and the compare.
1170 Builder.SetInsertPoint(OrigAdd);
1171
1172 Value *TruncA = Builder.CreateTrunc(V: A, DestTy: NewType, Name: A->getName() + ".trunc");
1173 Value *TruncB = Builder.CreateTrunc(V: B, DestTy: NewType, Name: B->getName() + ".trunc");
1174 CallInst *Call = Builder.CreateCall(Callee: F, Args: {TruncA, TruncB}, Name: "sadd");
1175 Value *Add = Builder.CreateExtractValue(Agg: Call, Idxs: 0, Name: "sadd.result");
1176 Value *ZExt = Builder.CreateZExt(V: Add, DestTy: OrigAdd->getType());
1177
1178 // The inner add was the result of the narrow add, zero extended to the
1179 // wider type. Replace it with the result computed by the intrinsic.
1180 IC.replaceInstUsesWith(I&: *OrigAdd, V: ZExt);
1181 IC.eraseInstFromFunction(I&: *OrigAdd);
1182
1183 // The original icmp gets replaced with the overflow value.
1184 return ExtractValueInst::Create(Agg: Call, Idxs: 1, NameStr: "sadd.overflow");
1185}
1186
1187/// If we have:
1188/// icmp eq/ne (urem/srem %x, %y), 0
1189/// iff %y is a power-of-two, we can replace this with a bit test:
1190/// icmp eq/ne (and %x, (add %y, -1)), 0
1191Instruction *InstCombinerImpl::foldIRemByPowerOfTwoToBitTest(ICmpInst &I) {
1192 // This fold is only valid for equality predicates.
1193 if (!I.isEquality())
1194 return nullptr;
1195 CmpPredicate Pred;
1196 Value *X, *Y, *Zero;
1197 if (!match(V: &I, P: m_ICmp(Pred, L: m_OneUse(SubPattern: m_IRem(L: m_Value(V&: X), R: m_Value(V&: Y))),
1198 R: m_CombineAnd(Ps: m_Zero(), Ps: m_Value(V&: Zero)))))
1199 return nullptr;
1200 if (!isKnownToBeAPowerOfTwo(V: Y, /*OrZero*/ true, CtxI: &I))
1201 return nullptr;
1202 // This may increase instruction count, we don't enforce that Y is a constant.
1203 Value *Mask = Builder.CreateAdd(LHS: Y, RHS: Constant::getAllOnesValue(Ty: Y->getType()));
1204 Value *Masked = Builder.CreateAnd(LHS: X, RHS: Mask);
1205 return ICmpInst::Create(Op: Instruction::ICmp, Pred, S1: Masked, S2: Zero);
1206}
1207
1208/// Fold equality-comparison between zero and any (maybe truncated) right-shift
1209/// by one-less-than-bitwidth into a sign test on the original value.
1210Instruction *InstCombinerImpl::foldSignBitTest(ICmpInst &I) {
1211 Instruction *Val;
1212 CmpPredicate Pred;
1213 if (!I.isEquality() || !match(V: &I, P: m_ICmp(Pred, L: m_Instruction(I&: Val), R: m_Zero())))
1214 return nullptr;
1215
1216 Value *X;
1217 Type *XTy;
1218
1219 Constant *C;
1220 if (match(V: Val, P: m_TruncOrSelf(Op: m_Shr(L: m_Value(V&: X), R: m_Constant(C))))) {
1221 XTy = X->getType();
1222 unsigned XBitWidth = XTy->getScalarSizeInBits();
1223 if (!match(V: C, P: m_SpecificInt_ICMP(Predicate: ICmpInst::Predicate::ICMP_EQ,
1224 Threshold: APInt(XBitWidth, XBitWidth - 1))))
1225 return nullptr;
1226 } else if (isa<BinaryOperator>(Val) &&
1227 (X = reassociateShiftAmtsOfTwoSameDirectionShifts(
1228 Sh0: cast<BinaryOperator>(Val), SQ: SQ.getWithInstruction(I: Val),
1229 /*AnalyzeForSignBitExtraction=*/true))) {
1230 XTy = X->getType();
1231 } else
1232 return nullptr;
1233
1234 return ICmpInst::Create(Op: Instruction::ICmp,
1235 Pred: Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SGE
1236 : ICmpInst::ICMP_SLT,
1237 S1: X, S2: ConstantInt::getNullValue(Ty: XTy));
1238}
1239
1240// Handle icmp pred X, 0
1241Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {
1242 CmpInst::Predicate Pred = Cmp.getPredicate();
1243 if (!match(V: Cmp.getOperand(i_nocapture: 1), P: m_Zero()))
1244 return nullptr;
1245
1246 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1247 if (Pred == ICmpInst::ICMP_SGT) {
1248 Value *A, *B;
1249 if (match(V: Cmp.getOperand(i_nocapture: 0), P: m_SMin(Op0: m_Value(V&: A), Op1: m_Value(V&: B)))) {
1250 if (isKnownPositive(V: A, SQ: SQ.getWithInstruction(I: &Cmp)))
1251 return new ICmpInst(Pred, B, Cmp.getOperand(i_nocapture: 1));
1252 if (isKnownPositive(V: B, SQ: SQ.getWithInstruction(I: &Cmp)))
1253 return new ICmpInst(Pred, A, Cmp.getOperand(i_nocapture: 1));
1254 }
1255 }
1256
1257 if (Instruction *New = foldIRemByPowerOfTwoToBitTest(I&: Cmp))
1258 return New;
1259
1260 // Given:
1261 // icmp eq/ne (urem %x, %y), 0
1262 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
1263 // icmp eq/ne %x, 0
1264 Value *X, *Y;
1265 if (match(V: Cmp.getOperand(i_nocapture: 0), P: m_URem(L: m_Value(V&: X), R: m_Value(V&: Y))) &&
1266 ICmpInst::isEquality(P: Pred)) {
1267 KnownBits XKnown = computeKnownBits(V: X, CtxI: &Cmp);
1268 KnownBits YKnown = computeKnownBits(V: Y, CtxI: &Cmp);
1269 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1270 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
1271 }
1272
1273 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are
1274 // odd/non-zero/there is no overflow.
1275 if (match(V: Cmp.getOperand(i_nocapture: 0), P: m_Mul(L: m_Value(V&: X), R: m_Value(V&: Y))) &&
1276 ICmpInst::isEquality(P: Pred)) {
1277
1278 KnownBits XKnown = computeKnownBits(V: X, CtxI: &Cmp);
1279 // if X % 2 != 0
1280 // (icmp eq/ne Y)
1281 if (XKnown.countMaxTrailingZeros() == 0)
1282 return new ICmpInst(Pred, Y, Cmp.getOperand(i_nocapture: 1));
1283
1284 KnownBits YKnown = computeKnownBits(V: Y, CtxI: &Cmp);
1285 // if Y % 2 != 0
1286 // (icmp eq/ne X)
1287 if (YKnown.countMaxTrailingZeros() == 0)
1288 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
1289
1290 auto *BO0 = cast<OverflowingBinaryOperator>(Val: Cmp.getOperand(i_nocapture: 0));
1291 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1292 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
1293 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`
1294 // but to avoid unnecessary work, first just if this is an obvious case.
1295
1296 // if X non-zero and NoOverflow(X * Y)
1297 // (icmp eq/ne Y)
1298 if (!XKnown.One.isZero() || isKnownNonZero(V: X, Q))
1299 return new ICmpInst(Pred, Y, Cmp.getOperand(i_nocapture: 1));
1300
1301 // if Y non-zero and NoOverflow(X * Y)
1302 // (icmp eq/ne X)
1303 if (!YKnown.One.isZero() || isKnownNonZero(V: Y, Q))
1304 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
1305 }
1306 // Note, we are skipping cases:
1307 // if Y % 2 != 0 AND X % 2 != 0
1308 // (false/true)
1309 // if X non-zero and Y non-zero and NoOverflow(X * Y)
1310 // (false/true)
1311 // Those can be simplified later as we would have already replaced the (icmp
1312 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
1313 // will fold to a constant elsewhere.
1314 }
1315
1316 // (icmp eq/ne f(X), 0) -> (icmp eq/ne X, 0)
1317 // where f(X) == 0 if and only if X == 0
1318 if (ICmpInst::isEquality(P: Pred))
1319 if (Value *Stripped = stripNullTest(V: Cmp.getOperand(i_nocapture: 0)))
1320 return new ICmpInst(Pred, Stripped,
1321 Constant::getNullValue(Ty: Stripped->getType()));
1322
1323 return nullptr;
1324}
1325
1326/// Fold icmp eq (num + mask) & ~mask, num
1327/// to
1328/// icmp eq (and num, mask), 0
1329/// Where mask is a low bit mask.
1330Instruction *InstCombinerImpl::foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp) {
1331 Value *Num;
1332 CmpPredicate Pred;
1333 const APInt *Mask, *Neg;
1334
1335 if (!match(V: &Cmp,
1336 P: m_c_ICmp(Pred, L: m_Value(V&: Num),
1337 R: m_OneUse(SubPattern: m_c_And(L: m_OneUse(SubPattern: m_c_Add(L: m_Deferred(V: Num),
1338 R: m_LowBitMask(V&: Mask))),
1339 R: m_APInt(Res&: Neg))))))
1340 return nullptr;
1341
1342 if (*Neg != ~*Mask)
1343 return nullptr;
1344
1345 if (!ICmpInst::isEquality(P: Pred))
1346 return nullptr;
1347
1348 // Create new icmp eq (num & mask), 0
1349 auto *NewAnd = Builder.CreateAnd(LHS: Num, RHS: *Mask);
1350 auto *Zero = Constant::getNullValue(Ty: Num->getType());
1351
1352 return new ICmpInst(Pred, NewAnd, Zero);
1353}
1354
1355/// Fold icmp Pred X, C.
1356/// TODO: This code structure does not make sense. The saturating add fold
1357/// should be moved to some other helper and extended as noted below (it is also
1358/// possible that code has been made unnecessary - do we canonicalize IR to
1359/// overflow/saturating intrinsics or not?).
1360Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) {
1361 // Match the following pattern, which is a common idiom when writing
1362 // overflow-safe integer arithmetic functions. The source performs an addition
1363 // in wider type and explicitly checks for overflow using comparisons against
1364 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1365 //
1366 // TODO: This could probably be generalized to handle other overflow-safe
1367 // operations if we worked out the formulas to compute the appropriate magic
1368 // constants.
1369 //
1370 // sum = a + b
1371 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1372 CmpInst::Predicate Pred = Cmp.getPredicate();
1373 Value *Op0 = Cmp.getOperand(i_nocapture: 0), *Op1 = Cmp.getOperand(i_nocapture: 1);
1374 Value *A, *B;
1375 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1376 if (Pred == ICmpInst::ICMP_UGT && match(V: Op1, P: m_ConstantInt(CI)) &&
1377 match(V: Op0, P: m_Add(L: m_Add(L: m_Value(V&: A), R: m_Value(V&: B)), R: m_ConstantInt(CI&: CI2))))
1378 if (Instruction *Res = processUGT_ADDCST_ADD(I&: Cmp, A, B, CI2, CI1: CI, IC&: *this))
1379 return Res;
1380
1381 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1382 Constant *C = dyn_cast<Constant>(Val: Op1);
1383 if (!C)
1384 return nullptr;
1385
1386 if (auto *Phi = dyn_cast<PHINode>(Val: Op0))
1387 if (all_of(Range: Phi->operands(), P: IsaPred<Constant>)) {
1388 SmallVector<Constant *> Ops;
1389 for (Value *V : Phi->incoming_values()) {
1390 Constant *Res =
1391 ConstantFoldCompareInstOperands(Predicate: Pred, LHS: cast<Constant>(Val: V), RHS: C, DL);
1392 if (!Res)
1393 return nullptr;
1394 Ops.push_back(Elt: Res);
1395 }
1396 Builder.SetInsertPoint(Phi);
1397 PHINode *NewPhi = Builder.CreatePHI(Ty: Cmp.getType(), NumReservedValues: Phi->getNumOperands());
1398 for (auto [V, Pred] : zip(t&: Ops, u: Phi->blocks()))
1399 NewPhi->addIncoming(V, BB: Pred);
1400 return replaceInstUsesWith(I&: Cmp, V: NewPhi);
1401 }
1402
1403 if (Instruction *R = tryFoldInstWithCtpopWithNot(I: &Cmp))
1404 return R;
1405
1406 return nullptr;
1407}
1408
1409/// Canonicalize icmp instructions based on dominating conditions.
1410Instruction *InstCombinerImpl::foldICmpWithDominatingICmp(ICmpInst &Cmp) {
1411 // We already checked simple implication in InstSimplify, only handle complex
1412 // cases here.
1413 Value *X = Cmp.getOperand(i_nocapture: 0), *Y = Cmp.getOperand(i_nocapture: 1);
1414 const APInt *C;
1415 if (!match(V: Y, P: m_APInt(Res&: C)))
1416 return nullptr;
1417
1418 CmpInst::Predicate Pred = Cmp.getPredicate();
1419 ConstantRange CR = ConstantRange::makeExactICmpRegion(Pred, Other: *C);
1420
1421 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1422 const APInt *DomC) -> Instruction * {
1423 // We have 2 compares of a variable with constants. Calculate the constant
1424 // ranges of those compares to see if we can transform the 2nd compare:
1425 // DomBB:
1426 // DomCond = icmp DomPred X, DomC
1427 // br DomCond, CmpBB, FalseBB
1428 // CmpBB:
1429 // Cmp = icmp Pred X, C
1430 ConstantRange DominatingCR =
1431 ConstantRange::makeExactICmpRegion(Pred: DomPred, Other: *DomC);
1432 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1433 ConstantRange Difference = DominatingCR.difference(CR);
1434 if (Intersection.isEmptySet())
1435 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
1436 if (Difference.isEmptySet())
1437 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
1438
1439 // Canonicalizing a sign bit comparison that gets used in a branch,
1440 // pessimizes codegen by generating branch on zero instruction instead
1441 // of a test and branch. So we avoid canonicalizing in such situations
1442 // because test and branch instruction has better branch displacement
1443 // than compare and branch instruction.
1444 bool UnusedBit;
1445 bool IsSignBit = isSignBitCheck(Pred, RHS: *C, TrueIfSigned&: UnusedBit);
1446 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(I&: Cmp)))
1447 return nullptr;
1448
1449 // Avoid an infinite loop with min/max canonicalization.
1450 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1451 if (Cmp.hasOneUse() &&
1452 match(V: Cmp.user_back(), P: m_MaxOrMin(Op0: m_Value(), Op1: m_Value())))
1453 return nullptr;
1454
1455 if (const APInt *EqC = Intersection.getSingleElement())
1456 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(AI: *EqC));
1457 if (const APInt *NeC = Difference.getSingleElement())
1458 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(AI: *NeC));
1459 return nullptr;
1460 };
1461
1462 for (CondBrInst *BI : DC.conditionsFor(V: X)) {
1463 CmpPredicate DomPred;
1464 const APInt *DomC;
1465 if (!match(V: BI->getCondition(),
1466 P: m_ICmp(Pred&: DomPred, L: m_Specific(V: X), R: m_APInt(Res&: DomC))))
1467 continue;
1468
1469 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(i: 0));
1470 if (DT.dominates(BBE: Edge0, BB: Cmp.getParent())) {
1471 if (auto *V = handleDomCond(DomPred, DomC))
1472 return V;
1473 } else {
1474 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(i: 1));
1475 if (DT.dominates(BBE: Edge1, BB: Cmp.getParent()))
1476 if (auto *V =
1477 handleDomCond(CmpInst::getInversePredicate(pred: DomPred), DomC))
1478 return V;
1479 }
1480 }
1481
1482 return nullptr;
1483}
1484
1485/// Fold icmp (trunc X), C.
1486Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
1487 TruncInst *Trunc,
1488 const APInt &C) {
1489 ICmpInst::Predicate Pred = Cmp.getPredicate();
1490 Value *X = Trunc->getOperand(i_nocapture: 0);
1491 Type *SrcTy = X->getType();
1492 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1493 SrcBits = SrcTy->getScalarSizeInBits();
1494
1495 // Match (icmp pred (trunc nuw/nsw X), C)
1496 // Which we can convert to (icmp pred X, (sext/zext C))
1497 if (shouldChangeType(From: Trunc->getType(), To: SrcTy)) {
1498 if (Trunc->hasNoSignedWrap())
1499 return new ICmpInst(Pred, X, ConstantInt::get(Ty: SrcTy, V: C.sext(width: SrcBits)));
1500 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1501 return new ICmpInst(Pred, X, ConstantInt::get(Ty: SrcTy, V: C.zext(width: SrcBits)));
1502 }
1503
1504 if (C.isOne() && C.getBitWidth() > 1) {
1505 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1506 Value *V = nullptr;
1507 if (Pred == ICmpInst::ICMP_SLT && match(V: X, P: m_Signum(V: m_Value(V))))
1508 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1509 ConstantInt::get(Ty: V->getType(), V: 1));
1510 }
1511
1512 // TODO: Handle non-equality predicates.
1513 Value *Y;
1514 const APInt *Pow2;
1515 if (Cmp.isEquality() && match(V: X, P: m_Shl(L: m_Power2(V&: Pow2), R: m_Value(V&: Y))) &&
1516 DstBits > Pow2->logBase2()) {
1517 // (trunc (Pow2 << Y) to iN) == 0 --> Y u>= N - log2(Pow2)
1518 // (trunc (Pow2 << Y) to iN) != 0 --> Y u< N - log2(Pow2)
1519 // iff N > log2(Pow2)
1520 if (C.isZero()) {
1521 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1522 return new ICmpInst(NewPred, Y,
1523 ConstantInt::get(Ty: SrcTy, V: DstBits - Pow2->logBase2()));
1524 }
1525 // (trunc (Pow2 << Y) to iN) == 2**C --> Y == C - log2(Pow2)
1526 // (trunc (Pow2 << Y) to iN) != 2**C --> Y != C - log2(Pow2)
1527 if (C.isPowerOf2())
1528 return new ICmpInst(
1529 Pred, Y, ConstantInt::get(Ty: SrcTy, V: C.logBase2() - Pow2->logBase2()));
1530 }
1531
1532 if (Cmp.isEquality() && (Trunc->hasOneUse() || Trunc->hasNoUnsignedWrap())) {
1533 // Canonicalize to a mask and wider compare if the wide type is suitable:
1534 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1535 if (!SrcTy->isVectorTy() && shouldChangeType(FromBitWidth: DstBits, ToBitWidth: SrcBits)) {
1536 Constant *Mask =
1537 ConstantInt::get(Ty: SrcTy, V: APInt::getLowBitsSet(numBits: SrcBits, loBitsSet: DstBits));
1538 Value *And = Trunc->hasNoUnsignedWrap() ? X : Builder.CreateAnd(LHS: X, RHS: Mask);
1539 Constant *WideC = ConstantInt::get(Ty: SrcTy, V: C.zext(width: SrcBits));
1540 return new ICmpInst(Pred, And, WideC);
1541 }
1542
1543 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1544 // of the high bits truncated out of x are known.
1545 KnownBits Known = computeKnownBits(V: X, CtxI: &Cmp);
1546
1547 // If all the high bits are known, we can do this xform.
1548 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1549 // Pull in the high bits from known-ones set.
1550 APInt NewRHS = C.zext(width: SrcBits);
1551 NewRHS |= Known.One & APInt::getHighBitsSet(numBits: SrcBits, hiBitsSet: SrcBits - DstBits);
1552 return new ICmpInst(Pred, X, ConstantInt::get(Ty: SrcTy, V: NewRHS));
1553 }
1554 }
1555
1556 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1557 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1558 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1559 Value *ShOp;
1560 uint64_t ShAmt;
1561 bool TrueIfSigned;
1562 if (isSignBitCheck(Pred, RHS: C, TrueIfSigned) &&
1563 match(V: X, P: m_Shr(L: m_Value(V&: ShOp), R: m_ConstantInt(V&: ShAmt))) &&
1564 DstBits == SrcBits - ShAmt) {
1565 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1566 ConstantInt::getNullValue(Ty: SrcTy))
1567 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1568 ConstantInt::getAllOnesValue(Ty: SrcTy));
1569 }
1570
1571 return nullptr;
1572}
1573
1574/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1575/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1576Instruction *
1577InstCombinerImpl::foldICmpTruncWithTruncOrExt(ICmpInst &Cmp,
1578 const SimplifyQuery &Q) {
1579 Value *X, *Y;
1580 CmpPredicate Pred;
1581 bool YIsSExt = false;
1582 // Try to match icmp (trunc X), (trunc Y)
1583 if (match(V: &Cmp, P: m_ICmp(Pred, L: m_Trunc(Op: m_Value(V&: X)), R: m_Trunc(Op: m_Value(V&: Y))))) {
1584 unsigned NoWrapFlags = cast<TruncInst>(Val: Cmp.getOperand(i_nocapture: 0))->getNoWrapKind() &
1585 cast<TruncInst>(Val: Cmp.getOperand(i_nocapture: 1))->getNoWrapKind();
1586 if (Cmp.isSigned()) {
1587 // For signed comparisons, both truncs must be nsw.
1588 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1589 return nullptr;
1590 } else {
1591 // For unsigned and equality comparisons, either both must be nuw or
1592 // both must be nsw, we don't care which.
1593 if (!NoWrapFlags)
1594 return nullptr;
1595 }
1596
1597 if (X->getType() != Y->getType() &&
1598 (!Cmp.getOperand(i_nocapture: 0)->hasOneUse() || !Cmp.getOperand(i_nocapture: 1)->hasOneUse()))
1599 return nullptr;
1600 if (!isDesirableIntType(BitWidth: X->getType()->getScalarSizeInBits()) &&
1601 isDesirableIntType(BitWidth: Y->getType()->getScalarSizeInBits())) {
1602 std::swap(a&: X, b&: Y);
1603 Pred = Cmp.getSwappedPredicate(pred: Pred);
1604 }
1605 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1606 }
1607 // Try to match icmp (trunc nuw X), (zext Y)
1608 else if (!Cmp.isSigned() &&
1609 match(V: &Cmp, P: m_c_ICmp(Pred, L: m_NUWTrunc(Op: m_Value(V&: X)),
1610 R: m_OneUse(SubPattern: m_ZExt(Op: m_Value(V&: Y)))))) {
1611 // Can fold trunc nuw + zext for unsigned and equality predicates.
1612 }
1613 // Try to match icmp (trunc nsw X), (sext Y)
1614 else if (match(V: &Cmp, P: m_c_ICmp(Pred, L: m_NSWTrunc(Op: m_Value(V&: X)),
1615 R: m_OneUse(SubPattern: m_ZExtOrSExt(Op: m_Value(V&: Y)))))) {
1616 // Can fold trunc nsw + zext/sext for all predicates.
1617 YIsSExt =
1618 isa<SExtInst>(Val: Cmp.getOperand(i_nocapture: 0)) || isa<SExtInst>(Val: Cmp.getOperand(i_nocapture: 1));
1619 } else
1620 return nullptr;
1621
1622 Type *TruncTy = Cmp.getOperand(i_nocapture: 0)->getType();
1623 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1624
1625 // If this transform will end up changing from desirable types -> undesirable
1626 // types skip it.
1627 if (isDesirableIntType(BitWidth: TruncBits) &&
1628 !isDesirableIntType(BitWidth: X->getType()->getScalarSizeInBits()))
1629 return nullptr;
1630
1631 Value *NewY = Builder.CreateIntCast(V: Y, DestTy: X->getType(), isSigned: YIsSExt);
1632 return new ICmpInst(Pred, X, NewY);
1633}
1634
1635/// Fold icmp (xor X, Y), C.
1636Instruction *InstCombinerImpl::foldICmpXorConstant(ICmpInst &Cmp,
1637 BinaryOperator *Xor,
1638 const APInt &C) {
1639 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1640 return I;
1641
1642 Value *X = Xor->getOperand(i_nocapture: 0);
1643 Value *Y = Xor->getOperand(i_nocapture: 1);
1644 const APInt *XorC;
1645 if (!match(V: Y, P: m_APInt(Res&: XorC)))
1646 return nullptr;
1647
1648 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1649 // fold the xor.
1650 ICmpInst::Predicate Pred = Cmp.getPredicate();
1651 bool TrueIfSigned = false;
1652 if (isSignBitCheck(Pred: Cmp.getPredicate(), RHS: C, TrueIfSigned)) {
1653
1654 // If the sign bit of the XorCst is not set, there is no change to
1655 // the operation, just stop using the Xor.
1656 if (!XorC->isNegative())
1657 return replaceOperand(I&: Cmp, OpNum: 0, V: X);
1658
1659 // Emit the opposite comparison.
1660 if (TrueIfSigned)
1661 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1662 ConstantInt::getAllOnesValue(Ty: X->getType()));
1663 else
1664 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1665 ConstantInt::getNullValue(Ty: X->getType()));
1666 }
1667
1668 if (Xor->hasOneUse()) {
1669 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1670 if (!Cmp.isEquality() && XorC->isSignMask()) {
1671 Pred = Cmp.getFlippedSignednessPredicate();
1672 return new ICmpInst(Pred, X, ConstantInt::get(Ty: X->getType(), V: C ^ *XorC));
1673 }
1674
1675 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1676 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1677 Pred = Cmp.getFlippedSignednessPredicate();
1678 Pred = Cmp.getSwappedPredicate(pred: Pred);
1679 return new ICmpInst(Pred, X, ConstantInt::get(Ty: X->getType(), V: C ^ *XorC));
1680 }
1681 }
1682
1683 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1684 if (Pred == ICmpInst::ICMP_UGT) {
1685 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1686 if (*XorC == ~C && (C + 1).isPowerOf2())
1687 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1688 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1689 if (*XorC == C && (C + 1).isPowerOf2())
1690 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1691 }
1692 if (Pred == ICmpInst::ICMP_ULT) {
1693 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1694 if (*XorC == -C && C.isPowerOf2())
1695 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1696 ConstantInt::get(Ty: X->getType(), V: ~C));
1697 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1698 if (*XorC == C && (-C).isPowerOf2())
1699 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1700 ConstantInt::get(Ty: X->getType(), V: ~C));
1701 }
1702 return nullptr;
1703}
1704
1705/// For power-of-2 C:
1706/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1707/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1708Instruction *InstCombinerImpl::foldICmpXorShiftConst(ICmpInst &Cmp,
1709 BinaryOperator *Xor,
1710 const APInt &C) {
1711 CmpInst::Predicate Pred = Cmp.getPredicate();
1712 APInt PowerOf2;
1713 if (Pred == ICmpInst::ICMP_ULT)
1714 PowerOf2 = C;
1715 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1716 PowerOf2 = C + 1;
1717 else
1718 return nullptr;
1719 if (!PowerOf2.isPowerOf2())
1720 return nullptr;
1721 Value *X;
1722 const APInt *ShiftC;
1723 if (!match(V: Xor, P: m_OneUse(SubPattern: m_c_Xor(L: m_Value(V&: X),
1724 R: m_AShr(L: m_Deferred(V: X), R: m_APInt(Res&: ShiftC))))))
1725 return nullptr;
1726 uint64_t Shift = ShiftC->getLimitedValue();
1727 Type *XType = X->getType();
1728 if (Shift == 0 || PowerOf2.isMinSignedValue())
1729 return nullptr;
1730 Value *Add = Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty: XType, V: PowerOf2));
1731 APInt Bound =
1732 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1733 return new ICmpInst(Pred, Add, ConstantInt::get(Ty: XType, V: Bound));
1734}
1735
1736/// Fold icmp (and (sh X, Y), C2), C1.
1737Instruction *InstCombinerImpl::foldICmpAndShift(ICmpInst &Cmp,
1738 BinaryOperator *And,
1739 const APInt &C1,
1740 const APInt &C2) {
1741 BinaryOperator *Shift = dyn_cast<BinaryOperator>(Val: And->getOperand(i_nocapture: 0));
1742 if (!Shift || !Shift->isShift())
1743 return nullptr;
1744
1745 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1746 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1747 // code produced by the clang front-end, for bitfield access.
1748 // This seemingly simple opportunity to fold away a shift turns out to be
1749 // rather complicated. See PR17827 for details.
1750 unsigned ShiftOpcode = Shift->getOpcode();
1751 bool IsShl = ShiftOpcode == Instruction::Shl;
1752 const APInt *C3;
1753 if (match(V: Shift->getOperand(i_nocapture: 1), P: m_APInt(Res&: C3))) {
1754 APInt NewAndCst, NewCmpCst;
1755 bool AnyCmpCstBitsShiftedOut;
1756 if (ShiftOpcode == Instruction::Shl) {
1757 // For a left shift, we can fold if the comparison is not signed. We can
1758 // also fold a signed comparison if the mask value and comparison value
1759 // are not negative. These constraints may not be obvious, but we can
1760 // prove that they are correct using an SMT solver.
1761 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1762 return nullptr;
1763
1764 NewCmpCst = C1.lshr(ShiftAmt: *C3);
1765 NewAndCst = C2.lshr(ShiftAmt: *C3);
1766 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(ShiftAmt: *C3) != C1;
1767 } else if (ShiftOpcode == Instruction::LShr) {
1768 // For a logical right shift, we can fold if the comparison is not signed.
1769 // We can also fold a signed comparison if the shifted mask value and the
1770 // shifted comparison value are not negative. These constraints may not be
1771 // obvious, but we can prove that they are correct using an SMT solver.
1772 NewCmpCst = C1.shl(ShiftAmt: *C3);
1773 NewAndCst = C2.shl(ShiftAmt: *C3);
1774 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(ShiftAmt: *C3) != C1;
1775 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1776 return nullptr;
1777 } else {
1778 // For an arithmetic shift, check that both constants don't use (in a
1779 // signed sense) the top bits being shifted out.
1780 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1781 NewCmpCst = C1.shl(ShiftAmt: *C3);
1782 NewAndCst = C2.shl(ShiftAmt: *C3);
1783 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(ShiftAmt: *C3) != C1;
1784 if (NewAndCst.ashr(ShiftAmt: *C3) != C2)
1785 return nullptr;
1786 }
1787
1788 if (AnyCmpCstBitsShiftedOut) {
1789 // If we shifted bits out, the fold is not going to work out. As a
1790 // special case, check to see if this means that the result is always
1791 // true or false now.
1792 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1793 return replaceInstUsesWith(I&: Cmp, V: ConstantInt::getFalse(Ty: Cmp.getType()));
1794 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1795 return replaceInstUsesWith(I&: Cmp, V: ConstantInt::getTrue(Ty: Cmp.getType()));
1796 } else {
1797 Value *NewAnd = Builder.CreateAnd(
1798 LHS: Shift->getOperand(i_nocapture: 0), RHS: ConstantInt::get(Ty: And->getType(), V: NewAndCst));
1799 return new ICmpInst(Cmp.getPredicate(), NewAnd,
1800 ConstantInt::get(Ty: And->getType(), V: NewCmpCst));
1801 }
1802 }
1803
1804 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1805 // preferable because it allows the C2 << Y expression to be hoisted out of a
1806 // loop if Y is invariant and X is not.
1807 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1808 !Shift->isArithmeticShift() &&
1809 ((!IsShl && C2.isOne()) || !isa<Constant>(Val: Shift->getOperand(i_nocapture: 0)))) {
1810 // Compute C2 << Y.
1811 Value *NewShift =
1812 IsShl ? Builder.CreateLShr(LHS: And->getOperand(i_nocapture: 1), RHS: Shift->getOperand(i_nocapture: 1))
1813 : Builder.CreateShl(LHS: And->getOperand(i_nocapture: 1), RHS: Shift->getOperand(i_nocapture: 1));
1814
1815 // Compute X & (C2 << Y).
1816 Value *NewAnd = Builder.CreateAnd(LHS: Shift->getOperand(i_nocapture: 0), RHS: NewShift);
1817 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(i_nocapture: 1));
1818 }
1819
1820 return nullptr;
1821}
1822
1823/// Fold icmp (and X, C2), C1.
1824Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp,
1825 BinaryOperator *And,
1826 const APInt &C1) {
1827 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1828
1829 // icmp ne (and X, 1), 0 --> trunc X to i1
1830 if (isICMP_NE && C1.isZero() && match(V: And->getOperand(i_nocapture: 1), P: m_One()))
1831 return new TruncInst(And->getOperand(i_nocapture: 0), Cmp.getType());
1832
1833 const APInt *C2;
1834 Value *X;
1835 if (!match(V: And, P: m_And(L: m_Value(V&: X), R: m_APInt(Res&: C2))))
1836 return nullptr;
1837
1838 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask
1839 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(RHS: ~*C2) &&
1840 C2->isNegatedPowerOf2())
1841 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1842 ConstantInt::get(Ty: X->getType(), V: ~*C2));
1843 // (and X, highmask) s< [1, -highmask] --> X s< -highmask
1844 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() &&
1845 (C1 - 1).ule(RHS: ~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask())
1846 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1847 ConstantInt::get(Ty: X->getType(), V: -*C2));
1848
1849 // Don't perform the following transforms if the AND has multiple uses
1850 if (!And->hasOneUse())
1851 return nullptr;
1852
1853 if (Cmp.isEquality() && C1.isZero()) {
1854 // Restrict this fold to single-use 'and' (PR10267).
1855 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1856 if (C2->isSignMask()) {
1857 Constant *Zero = Constant::getNullValue(Ty: X->getType());
1858 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1859 return new ICmpInst(NewPred, X, Zero);
1860 }
1861
1862 APInt NewC2 = *C2;
1863 KnownBits Know = computeKnownBits(V: And->getOperand(i_nocapture: 0), CtxI: And);
1864 // Set high zeros of C2 to allow matching negated power-of-2.
1865 NewC2 = *C2 | APInt::getHighBitsSet(numBits: C2->getBitWidth(),
1866 hiBitsSet: Know.countMinLeadingZeros());
1867
1868 // Restrict this fold only for single-use 'and' (PR10267).
1869 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1870 if (NewC2.isNegatedPowerOf2()) {
1871 Constant *NegBOC = ConstantInt::get(Ty: And->getType(), V: -NewC2);
1872 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1873 return new ICmpInst(NewPred, X, NegBOC);
1874 }
1875 }
1876
1877 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1878 // the input width without changing the value produced, eliminate the cast:
1879 //
1880 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1881 //
1882 // We can do this transformation if the constants do not have their sign bits
1883 // set or if it is an equality comparison. Extending a relational comparison
1884 // when we're checking the sign bit would not work.
1885 Value *W;
1886 if (match(V: And->getOperand(i_nocapture: 0), P: m_OneUse(SubPattern: m_Trunc(Op: m_Value(V&: W)))) &&
1887 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1888 // TODO: Is this a good transform for vectors? Wider types may reduce
1889 // throughput. Should this transform be limited (even for scalars) by using
1890 // shouldChangeType()?
1891 if (!Cmp.getType()->isVectorTy()) {
1892 Type *WideType = W->getType();
1893 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1894 Constant *ZextC1 = ConstantInt::get(Ty: WideType, V: C1.zext(width: WideScalarBits));
1895 Constant *ZextC2 = ConstantInt::get(Ty: WideType, V: C2->zext(width: WideScalarBits));
1896 Value *NewAnd = Builder.CreateAnd(LHS: W, RHS: ZextC2, Name: And->getName());
1897 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1898 }
1899 }
1900
1901 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, C2: *C2))
1902 return I;
1903
1904 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1905 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1906 //
1907 // iff pred isn't signed
1908 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(i_nocapture: 0)->hasOneUse() &&
1909 match(V: And->getOperand(i_nocapture: 1), P: m_One())) {
1910 Constant *One = cast<Constant>(Val: And->getOperand(i_nocapture: 1));
1911 Value *Or = And->getOperand(i_nocapture: 0);
1912 Value *A, *B, *LShr;
1913 if (match(V: Or, P: m_Or(L: m_Value(V&: LShr), R: m_Value(V&: A))) &&
1914 match(V: LShr, P: m_LShr(L: m_Specific(V: A), R: m_Value(V&: B)))) {
1915 unsigned UsesRemoved = 0;
1916 if (And->hasOneUse())
1917 ++UsesRemoved;
1918 if (Or->hasOneUse())
1919 ++UsesRemoved;
1920 if (LShr->hasOneUse())
1921 ++UsesRemoved;
1922
1923 // Compute A & ((1 << B) | 1)
1924 unsigned RequireUsesRemoved = match(V: B, P: m_ImmConstant()) ? 1 : 3;
1925 if (UsesRemoved >= RequireUsesRemoved) {
1926 Value *NewOr =
1927 Builder.CreateOr(LHS: Builder.CreateShl(LHS: One, RHS: B, Name: LShr->getName(),
1928 /*HasNUW=*/true),
1929 RHS: One, Name: Or->getName());
1930 Value *NewAnd = Builder.CreateAnd(LHS: A, RHS: NewOr, Name: And->getName());
1931 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(i_nocapture: 1));
1932 }
1933 }
1934 }
1935
1936 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1937 // llvm.is.fpclass(X, fcInf|fcNan)
1938 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1939 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1940 // (icmp eq (and (bitcast X to int), ExponentMask), 0) -->
1941 // llvm.is.fpclass(X, fcSubnormal|fcZero)
1942 // (icmp ne (and (bitcast X to int), ExponentMask), 0) -->
1943 // llvm.is.fpclass(X, ~(fcSubnormal|fcZero))
1944 Value *V;
1945 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1946 Kind: Attribute::NoImplicitFloat) &&
1947 Cmp.isEquality() &&
1948 match(V: X, P: m_OneUse(SubPattern: m_ElementWiseBitCast(Op: m_Value(V))))) {
1949 Type *FPType = V->getType()->getScalarType();
1950 if (FPType->isIEEELikeFPTy() && (C1.isZero() || C1 == *C2)) {
1951 APInt ExponentMask =
1952 APFloat::getInf(Sem: FPType->getFltSemantics()).bitcastToAPInt();
1953 if (*C2 == ExponentMask) {
1954 unsigned Mask = C1.isZero()
1955 ? FPClassTest::fcZero | FPClassTest::fcSubnormal
1956 : FPClassTest::fcNan | FPClassTest::fcInf;
1957 if (isICMP_NE)
1958 Mask = ~Mask & fcAllFlags;
1959 return replaceInstUsesWith(I&: Cmp, V: Builder.createIsFPClass(FPNum: V, Test: Mask));
1960 }
1961 }
1962 }
1963
1964 return nullptr;
1965}
1966
1967/// Fold icmp (and X, Y), C.
1968Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp,
1969 BinaryOperator *And,
1970 const APInt &C) {
1971 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C1: C))
1972 return I;
1973
1974 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1975 bool TrueIfNeg;
1976 if (isSignBitCheck(Pred, RHS: C, TrueIfSigned&: TrueIfNeg)) {
1977 // ((X - 1) & ~X) < 0 --> X == 0
1978 // ((X - 1) & ~X) >= 0 --> X != 0
1979 Value *X;
1980 if (match(V: And->getOperand(i_nocapture: 0), P: m_Add(L: m_Value(V&: X), R: m_AllOnes())) &&
1981 match(V: And->getOperand(i_nocapture: 1), P: m_Not(V: m_Specific(V: X)))) {
1982 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1983 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(Ty: X->getType()));
1984 }
1985 // (X & -X) < 0 --> X == MinSignedC
1986 // (X & -X) > -1 --> X != MinSignedC
1987 if (match(V: And, P: m_c_And(L: m_Neg(V: m_Value(V&: X)), R: m_Deferred(V: X)))) {
1988 Constant *MinSignedC = ConstantInt::get(
1989 Ty: X->getType(),
1990 V: APInt::getSignedMinValue(numBits: X->getType()->getScalarSizeInBits()));
1991 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1992 return new ICmpInst(NewPred, X, MinSignedC);
1993 }
1994 }
1995
1996 // TODO: These all require that Y is constant too, so refactor with the above.
1997
1998 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1999 Value *X = And->getOperand(i_nocapture: 0);
2000 Value *Y = And->getOperand(i_nocapture: 1);
2001 if (auto *C2 = dyn_cast<ConstantInt>(Val: Y))
2002 if (auto *LI = dyn_cast<LoadInst>(Val: X))
2003 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(
2004 LI, Pred: Cmp.getPredicate(), RHSC: cast<Constant>(Val: Cmp.getOperand(i_nocapture: 1)), CtxI&: Cmp,
2005 AndCst: C2))
2006 return Res;
2007
2008 if (!Cmp.isEquality())
2009 return nullptr;
2010
2011 // (X & -X) == 0 --> X == 0
2012 // (X & -X) != 0 --> X != 0
2013 // (X & -X) == 1 --> trunc X to i1
2014 // (X & -X) != 1 --> !(trunc X to i1)
2015 // Cmp is == or != by the check above.
2016 Value *MatchedX;
2017 // Match X & -X in either operand order.
2018 if (C.getBitWidth() > 1 && (C.isZero() || C.isOne()) &&
2019 match(V: And, P: m_c_And(L: m_Neg(V: m_Value(V&: MatchedX)), R: m_Deferred(V: MatchedX)))) {
2020 // Preserve the predicate: (X & -X) ==/!= 0 --> X ==/!= 0.
2021 if (C.isZero())
2022 return new ICmpInst(Pred, MatchedX, Cmp.getOperand(i_nocapture: 1));
2023
2024 // (X & -X) == 1 iff the low bit of X is set.
2025 if (Pred == CmpInst::ICMP_EQ)
2026 return new TruncInst(MatchedX, Cmp.getType());
2027
2028 // The remaining case needs a trunc and not. Require the original and
2029 // to become dead to avoid increasing the instruction count.
2030 if (And->hasOneUse()) {
2031 Value *Trunc = Builder.CreateTrunc(V: MatchedX, DestTy: Cmp.getType());
2032 return BinaryOperator::CreateNot(Op: Trunc);
2033 }
2034 }
2035
2036 // X & -C == -C -> X > u ~C
2037 // X & -C != -C -> X <= u ~C
2038 // iff C is a power of 2
2039 if (Cmp.getOperand(i_nocapture: 1) == Y && C.isNegatedPowerOf2()) {
2040 auto NewPred =
2041 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGT : CmpInst::ICMP_ULE;
2042 return new ICmpInst(NewPred, X, SubOne(C: cast<Constant>(Val: Cmp.getOperand(i_nocapture: 1))));
2043 }
2044
2045 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
2046 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
2047 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
2048 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
2049 if (match(V: And, P: m_OneUse(SubPattern: m_c_And(L: m_OneUse(SubPattern: m_ZExt(Op: m_Value(V&: X))), R: m_Value(V&: Y)))) &&
2050 X->getType()->isIntOrIntVectorTy(BitWidth: 1) && (C.isZero() || C.isOne())) {
2051 Value *TruncY = Builder.CreateTrunc(V: Y, DestTy: X->getType());
2052 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
2053 Value *And = Builder.CreateAnd(LHS: TruncY, RHS: X);
2054 return BinaryOperator::CreateNot(Op: And);
2055 }
2056 return BinaryOperator::CreateAnd(V1: TruncY, V2: X);
2057 }
2058
2059 // (icmp eq/ne (and (shl -1, X), Y), 0)
2060 // -> (icmp eq/ne (lshr Y, X), 0)
2061 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
2062 // highly unlikely the non-zero case will ever show up in code.
2063 if (C.isZero() &&
2064 match(V: And, P: m_OneUse(SubPattern: m_c_And(L: m_OneUse(SubPattern: m_Shl(L: m_AllOnes(), R: m_Value(V&: X))),
2065 R: m_Value(V&: Y))))) {
2066 Value *LShr = Builder.CreateLShr(LHS: Y, RHS: X);
2067 return new ICmpInst(Pred, LShr, Constant::getNullValue(Ty: LShr->getType()));
2068 }
2069
2070 // (icmp eq/ne (and (add A, Addend), Msk), C)
2071 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk))
2072 {
2073 Value *A;
2074 const APInt *Addend, *Msk;
2075 if (match(V: And, P: m_OneUse(SubPattern: m_And(L: m_OneUse(SubPattern: m_Add(L: m_Value(V&: A), R: m_APInt(Res&: Addend))),
2076 R: m_LowBitMask(V&: Msk)))) &&
2077 C.ule(RHS: *Msk)) {
2078 APInt NewComperand = (C - *Addend) & *Msk;
2079 Value *MaskA = Builder.CreateAnd(LHS: A, RHS: ConstantInt::get(Ty: A->getType(), V: *Msk));
2080 return new ICmpInst(Pred, MaskA,
2081 ConstantInt::get(Ty: MaskA->getType(), V: NewComperand));
2082 }
2083 }
2084
2085 return nullptr;
2086}
2087
2088/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
2089static Value *foldICmpOrXorSubChain(ICmpInst &Cmp, BinaryOperator *Or,
2090 InstCombiner::BuilderTy &Builder) {
2091 // Are we using xors or subs to bitwise check for a pair or pairs of
2092 // (in)equalities? Convert to a shorter form that has more potential to be
2093 // folded even further.
2094 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
2095 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
2096 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
2097 // (X1 == X2) && (X3 == X4) && (X5 == X6)
2098 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
2099 // (X1 != X2) || (X3 != X4) || (X5 != X6)
2100 SmallVector<std::pair<Value *, Value *>, 2> CmpValues;
2101 SmallVector<Value *, 16> WorkList(1, Or);
2102
2103 while (!WorkList.empty()) {
2104 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
2105 Value *Lhs, *Rhs;
2106
2107 if (match(V: OrOperatorArgument,
2108 P: m_OneUse(SubPattern: m_Xor(L: m_Value(V&: Lhs), R: m_Value(V&: Rhs))))) {
2109 CmpValues.emplace_back(Args&: Lhs, Args&: Rhs);
2110 return;
2111 }
2112
2113 if (match(V: OrOperatorArgument,
2114 P: m_OneUse(SubPattern: m_Sub(L: m_Value(V&: Lhs), R: m_Value(V&: Rhs))))) {
2115 CmpValues.emplace_back(Args&: Lhs, Args&: Rhs);
2116 return;
2117 }
2118
2119 WorkList.push_back(Elt: OrOperatorArgument);
2120 };
2121
2122 Value *CurrentValue = WorkList.pop_back_val();
2123 Value *OrOperatorLhs, *OrOperatorRhs;
2124
2125 if (!match(V: CurrentValue,
2126 P: m_Or(L: m_Value(V&: OrOperatorLhs), R: m_Value(V&: OrOperatorRhs)))) {
2127 return nullptr;
2128 }
2129
2130 MatchOrOperatorArgument(OrOperatorRhs);
2131 MatchOrOperatorArgument(OrOperatorLhs);
2132 }
2133
2134 ICmpInst::Predicate Pred = Cmp.getPredicate();
2135 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2136 Value *LhsCmp = Builder.CreateICmp(P: Pred, LHS: CmpValues.rbegin()->first,
2137 RHS: CmpValues.rbegin()->second);
2138
2139 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2140 Value *RhsCmp = Builder.CreateICmp(P: Pred, LHS: It->first, RHS: It->second);
2141 LhsCmp = Builder.CreateBinOp(Opc: BOpc, LHS: LhsCmp, RHS: RhsCmp);
2142 }
2143
2144 return LhsCmp;
2145}
2146
2147/// Fold icmp (or X, Y), C.
2148Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,
2149 BinaryOperator *Or,
2150 const APInt &C) {
2151 ICmpInst::Predicate Pred = Cmp.getPredicate();
2152 if (C.isOne()) {
2153 // icmp slt signum(V) 1 --> icmp slt V, 1
2154 Value *V = nullptr;
2155 if (Pred == ICmpInst::ICMP_SLT && match(V: Or, P: m_Signum(V: m_Value(V))))
2156 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2157 ConstantInt::get(Ty: V->getType(), V: 1));
2158 }
2159
2160 Value *OrOp0 = Or->getOperand(i_nocapture: 0), *OrOp1 = Or->getOperand(i_nocapture: 1);
2161
2162 // (icmp eq/ne (or disjoint x, C0), C1)
2163 // -> (icmp eq/ne x, C0^C1)
2164 if (Cmp.isEquality() && match(V: OrOp1, P: m_ImmConstant()) &&
2165 cast<PossiblyDisjointInst>(Val: Or)->isDisjoint()) {
2166 Value *NewC =
2167 Builder.CreateXor(LHS: OrOp1, RHS: ConstantInt::get(Ty: OrOp1->getType(), V: C));
2168 return new ICmpInst(Pred, OrOp0, NewC);
2169 }
2170
2171 const APInt *MaskC;
2172 if (match(V: OrOp1, P: m_APInt(Res&: MaskC)) && Cmp.isEquality()) {
2173 if (*MaskC == C && (C + 1).isPowerOf2()) {
2174 // X | C == C --> X <=u C
2175 // X | C != C --> X >u C
2176 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2177 Pred = (Pred == CmpInst::ICMP_EQ) ? CmpInst::ICMP_ULE : CmpInst::ICMP_UGT;
2178 return new ICmpInst(Pred, OrOp0, OrOp1);
2179 }
2180
2181 // More general: canonicalize 'equality with set bits mask' to
2182 // 'equality with clear bits mask'.
2183 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2184 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2185 if (Or->hasOneUse()) {
2186 Value *And = Builder.CreateAnd(LHS: OrOp0, RHS: ~(*MaskC));
2187 Constant *NewC = ConstantInt::get(Ty: Or->getType(), V: C ^ (*MaskC));
2188 return new ICmpInst(Pred, And, NewC);
2189 }
2190 }
2191
2192 // (X | (X-1)) s< 0 --> X s< 1
2193 // (X | (X-1)) s> -1 --> X s> 0
2194 Value *X;
2195 bool TrueIfSigned;
2196 if (isSignBitCheck(Pred, RHS: C, TrueIfSigned) &&
2197 match(V: Or, P: m_c_Or(L: m_Add(L: m_Value(V&: X), R: m_AllOnes()), R: m_Deferred(V: X)))) {
2198 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2199 Constant *NewC = ConstantInt::get(Ty: X->getType(), V: TrueIfSigned ? 1 : 0);
2200 return new ICmpInst(NewPred, X, NewC);
2201 }
2202
2203 const APInt *OrC;
2204 // icmp(X | OrC, C) --> icmp(X, 0)
2205 if (C.isNonNegative() && match(V: Or, P: m_Or(L: m_Value(V&: X), R: m_APInt(Res&: OrC)))) {
2206 switch (Pred) {
2207 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2208 case ICmpInst::ICMP_SLT:
2209 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2210 case ICmpInst::ICMP_SGE:
2211 if (OrC->sge(RHS: C))
2212 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: X->getType()));
2213 break;
2214 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2215 case ICmpInst::ICMP_SLE:
2216 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2217 case ICmpInst::ICMP_SGT:
2218 if (OrC->sgt(RHS: C))
2219 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), X,
2220 ConstantInt::getNullValue(Ty: X->getType()));
2221 break;
2222 default:
2223 break;
2224 }
2225 }
2226
2227 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2228 return nullptr;
2229
2230 Value *P, *Q;
2231 if (match(V: Or, P: m_Or(L: m_PtrToInt(Op: m_Value(V&: P)), R: m_PtrToInt(Op: m_Value(V&: Q))))) {
2232 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2233 // -> and (icmp eq P, null), (icmp eq Q, null).
2234 Value *CmpP =
2235 Builder.CreateICmp(P: Pred, LHS: P, RHS: ConstantInt::getNullValue(Ty: P->getType()));
2236 Value *CmpQ =
2237 Builder.CreateICmp(P: Pred, LHS: Q, RHS: ConstantInt::getNullValue(Ty: Q->getType()));
2238 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2239 return BinaryOperator::Create(Op: BOpc, S1: CmpP, S2: CmpQ);
2240 }
2241
2242 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2243 return replaceInstUsesWith(I&: Cmp, V);
2244
2245 return nullptr;
2246}
2247
2248/// Fold icmp (mul X, Y), C.
2249Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
2250 BinaryOperator *Mul,
2251 const APInt &C) {
2252 ICmpInst::Predicate Pred = Cmp.getPredicate();
2253 Type *MulTy = Mul->getType();
2254 Value *X = Mul->getOperand(i_nocapture: 0);
2255
2256 // If comparing a square with a constant, try simplifying to comparing square
2257 // roots.
2258 if (X == Mul->getOperand(i_nocapture: 1) && !Cmp.isSigned()) {
2259 APInt R = C.sqrtFloor();
2260 bool IsSqr = C == R * R;
2261
2262 // X * X eq/ne C
2263 if (Cmp.isEquality() &&
2264 (Mul->hasNoUnsignedWrap() || (Mul->hasNoSignedWrap() && C.isZero()))) {
2265
2266 // If constant is not a square, eq/ne is false/true respectively
2267 if (!IsSqr)
2268 return replaceInstUsesWith(
2269 I&: Cmp,
2270 V: ConstantInt::getBool(Ty: Cmp.getType(), V: Pred == ICmpInst::ICMP_NE));
2271
2272 return new ICmpInst(Pred, X, ConstantInt::get(Ty: MulTy, V: R));
2273 }
2274
2275 // If the multiply does not wrap
2276 // X * X pred C --> X pred R
2277 if (Mul->hasNoUnsignedWrap()) {
2278
2279 if (IsSqr)
2280 return new ICmpInst(Pred, X, ConstantInt::get(Ty: MulTy, V: R));
2281
2282 // If C is not a square, we use floor/ceil of sqrt(C).
2283 //
2284 // If LT or LE, we need R to be an overestimate of sqrt(C),
2285 // then use the strict predicate (LT->LT, LE->LT).
2286 //
2287 // If GT or GE, we need R to be an underestimate of sqrt(C),
2288 // then use the strict predicate (GT->GT, GE->GT).
2289 //
2290 // R is already an underestimate of sqrt(C) due to sqrtFloor.
2291 if (ICmpInst::isLT(P: Pred) || ICmpInst::isLE(P: Pred))
2292 ++R;
2293
2294 return new ICmpInst(Cmp.getStrictPredicate(), X,
2295 ConstantInt::get(Ty: MulTy, V: R));
2296 }
2297 }
2298
2299 const APInt *MulC;
2300 if (!match(V: Mul->getOperand(i_nocapture: 1), P: m_APInt(Res&: MulC)))
2301 return nullptr;
2302
2303 // If this is a test of the sign bit and the multiply is sign-preserving with
2304 // a constant operand, use the multiply LHS operand instead:
2305 // (X * +MulC) < 0 --> X < 0
2306 // (X * -MulC) < 0 --> X > 0
2307 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2308 if (MulC->isNegative())
2309 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
2310 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: MulTy));
2311 }
2312
2313 if (MulC->isZero())
2314 return nullptr;
2315
2316 // If the multiply does not wrap or the constant is odd, try to divide the
2317 // compare constant by the multiplication factor.
2318 if (Cmp.isEquality()) {
2319 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2320 if (Mul->hasNoSignedWrap() && C.srem(RHS: *MulC).isZero()) {
2321 Constant *NewC = ConstantInt::get(Ty: MulTy, V: C.sdiv(RHS: *MulC));
2322 return new ICmpInst(Pred, X, NewC);
2323 }
2324
2325 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2326 // correct to transform if MulC * N == C including overflow. I.e with i8
2327 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2328 // miss that case.
2329 if (C.urem(RHS: *MulC).isZero()) {
2330 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2331 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2332 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2333 Constant *NewC = ConstantInt::get(Ty: MulTy, V: C.udiv(RHS: *MulC));
2334 return new ICmpInst(Pred, X, NewC);
2335 }
2336 }
2337 }
2338
2339 // With a matching no-overflow guarantee, fold the constants:
2340 // (X * MulC) < C --> X < (C / MulC)
2341 // (X * MulC) > C --> X > (C / MulC)
2342 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2343 Constant *NewC = nullptr;
2344 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2345 // MININT / -1 --> overflow.
2346 if (C.isMinSignedValue() && MulC->isAllOnes())
2347 return nullptr;
2348 if (MulC->isNegative())
2349 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
2350
2351 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2352 NewC = ConstantInt::get(
2353 Ty: MulTy, V: APIntOps::RoundingSDiv(A: C, B: *MulC, RM: APInt::Rounding::UP));
2354 } else {
2355 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2356 "Unexpected predicate");
2357 NewC = ConstantInt::get(
2358 Ty: MulTy, V: APIntOps::RoundingSDiv(A: C, B: *MulC, RM: APInt::Rounding::DOWN));
2359 }
2360 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2361 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2362 NewC = ConstantInt::get(
2363 Ty: MulTy, V: APIntOps::RoundingUDiv(A: C, B: *MulC, RM: APInt::Rounding::UP));
2364 } else {
2365 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2366 "Unexpected predicate");
2367 NewC = ConstantInt::get(
2368 Ty: MulTy, V: APIntOps::RoundingUDiv(A: C, B: *MulC, RM: APInt::Rounding::DOWN));
2369 }
2370 }
2371
2372 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2373}
2374
2375/// Fold icmp (shl nuw C2, Y), C.
2376static Instruction *foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl,
2377 const APInt &C) {
2378 Value *Y;
2379 const APInt *C2;
2380 if (!match(V: Shl, P: m_NUWShl(L: m_APInt(Res&: C2), R: m_Value(V&: Y))))
2381 return nullptr;
2382
2383 Type *ShiftType = Shl->getType();
2384 unsigned TypeBits = C.getBitWidth();
2385 ICmpInst::Predicate Pred = Cmp.getPredicate();
2386 if (Cmp.isUnsigned()) {
2387 if (C2->isZero() || C2->ugt(RHS: C))
2388 return nullptr;
2389 APInt Div, Rem;
2390 APInt::udivrem(LHS: C, RHS: *C2, Quotient&: Div, Remainder&: Rem);
2391 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();
2392
2393 // (1 << Y) pred C -> Y pred Log2(C)
2394 if (!CIsPowerOf2) {
2395 // (1 << Y) < 30 -> Y <= 4
2396 // (1 << Y) <= 30 -> Y <= 4
2397 // (1 << Y) >= 30 -> Y > 4
2398 // (1 << Y) > 30 -> Y > 4
2399 if (Pred == ICmpInst::ICMP_ULT)
2400 Pred = ICmpInst::ICMP_ULE;
2401 else if (Pred == ICmpInst::ICMP_UGE)
2402 Pred = ICmpInst::ICMP_UGT;
2403 }
2404
2405 unsigned CLog2 = Div.logBase2();
2406 return new ICmpInst(Pred, Y, ConstantInt::get(Ty: ShiftType, V: CLog2));
2407 } else if (Cmp.isSigned() && C2->isOne()) {
2408 Constant *BitWidthMinusOne = ConstantInt::get(Ty: ShiftType, V: TypeBits - 1);
2409 // (1 << Y) > 0 -> Y != 31
2410 // (1 << Y) > C -> Y != 31 if C is negative.
2411 if (Pred == ICmpInst::ICMP_SGT && C.sle(RHS: 0))
2412 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2413
2414 // (1 << Y) < 0 -> Y == 31
2415 // (1 << Y) < 1 -> Y == 31
2416 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2417 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2418 if (Pred == ICmpInst::ICMP_SLT && (C - 1).sle(RHS: 0))
2419 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2420 }
2421
2422 return nullptr;
2423}
2424
2425/// Fold icmp (shl X, Y), C.
2426Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp,
2427 BinaryOperator *Shl,
2428 const APInt &C) {
2429 const APInt *ShiftVal;
2430 if (Cmp.isEquality() && match(V: Shl->getOperand(i_nocapture: 0), P: m_APInt(Res&: ShiftVal)))
2431 return foldICmpShlConstConst(I&: Cmp, A: Shl->getOperand(i_nocapture: 1), AP1: C, AP2: *ShiftVal);
2432
2433 ICmpInst::Predicate Pred = Cmp.getPredicate();
2434 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2435 // -> (icmp pred X, Csle0)
2436 //
2437 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2438 // so X's must be what is used.
2439 if (C.sle(RHS: 0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2440 return new ICmpInst(Pred, Shl->getOperand(i_nocapture: 0), Cmp.getOperand(i_nocapture: 1));
2441
2442 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2443 // -> (icmp eq/ne X, 0)
2444 if (ICmpInst::isEquality(P: Pred) && C.isZero() &&
2445 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2446 return new ICmpInst(Pred, Shl->getOperand(i_nocapture: 0), Cmp.getOperand(i_nocapture: 1));
2447
2448 // (icmp slt (shl nsw X, Y), 0/1)
2449 // -> (icmp slt X, 0/1)
2450 // (icmp sgt (shl nsw X, Y), 0/-1)
2451 // -> (icmp sgt X, 0/-1)
2452 //
2453 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2454 if (Shl->hasNoSignedWrap() &&
2455 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2456 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2457 return new ICmpInst(Pred, Shl->getOperand(i_nocapture: 0), Cmp.getOperand(i_nocapture: 1));
2458
2459 const APInt *ShiftAmt;
2460 if (!match(V: Shl->getOperand(i_nocapture: 1), P: m_APInt(Res&: ShiftAmt)))
2461 return foldICmpShlLHSC(Cmp, Shl, C);
2462
2463 // Check that the shift amount is in range. If not, don't perform undefined
2464 // shifts. When the shift is visited, it will be simplified.
2465 unsigned TypeBits = C.getBitWidth();
2466 if (ShiftAmt->uge(RHS: TypeBits))
2467 return nullptr;
2468
2469 Value *X = Shl->getOperand(i_nocapture: 0);
2470 Type *ShType = Shl->getType();
2471
2472 // NSW guarantees that we are only shifting out sign bits from the high bits,
2473 // so we can ASHR the compare constant without needing a mask and eliminate
2474 // the shift.
2475 if (Shl->hasNoSignedWrap()) {
2476 if (Pred == ICmpInst::ICMP_SGT) {
2477 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2478 APInt ShiftedC = C.ashr(ShiftAmt: *ShiftAmt);
2479 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2480 }
2481 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2482 C.ashr(ShiftAmt: *ShiftAmt).shl(ShiftAmt: *ShiftAmt) == C) {
2483 APInt ShiftedC = C.ashr(ShiftAmt: *ShiftAmt);
2484 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2485 }
2486 if (Pred == ICmpInst::ICMP_SLT) {
2487 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2488 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2489 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2490 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2491 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2492 APInt ShiftedC = (C - 1).ashr(ShiftAmt: *ShiftAmt) + 1;
2493 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2494 }
2495 }
2496
2497 // NUW guarantees that we are only shifting out zero bits from the high bits,
2498 // so we can LSHR the compare constant without needing a mask and eliminate
2499 // the shift.
2500 if (Shl->hasNoUnsignedWrap()) {
2501 if (Pred == ICmpInst::ICMP_UGT) {
2502 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2503 APInt ShiftedC = C.lshr(ShiftAmt: *ShiftAmt);
2504 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2505 }
2506 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2507 C.lshr(ShiftAmt: *ShiftAmt).shl(ShiftAmt: *ShiftAmt) == C) {
2508 APInt ShiftedC = C.lshr(ShiftAmt: *ShiftAmt);
2509 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2510 }
2511 if (Pred == ICmpInst::ICMP_ULT) {
2512 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2513 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2514 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2515 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2516 assert(C.ugt(0) && "ult 0 should have been eliminated");
2517 APInt ShiftedC = (C - 1).lshr(ShiftAmt: *ShiftAmt) + 1;
2518 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2519 }
2520 }
2521
2522 if (Cmp.isEquality() && Shl->hasOneUse()) {
2523 // Strength-reduce the shift into an 'and'.
2524 Constant *Mask = ConstantInt::get(
2525 Ty: ShType,
2526 V: APInt::getLowBitsSet(numBits: TypeBits, loBitsSet: TypeBits - ShiftAmt->getZExtValue()));
2527 Value *And = Builder.CreateAnd(LHS: X, RHS: Mask, Name: Shl->getName() + ".mask");
2528 Constant *LShrC = ConstantInt::get(Ty: ShType, V: C.lshr(ShiftAmt: *ShiftAmt));
2529 return new ICmpInst(Pred, And, LShrC);
2530 }
2531
2532 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2533 bool TrueIfSigned = false;
2534 if (Shl->hasOneUse() && isSignBitCheck(Pred, RHS: C, TrueIfSigned)) {
2535 // (X << 31) <s 0 --> (X & 1) != 0
2536 Constant *Mask = ConstantInt::get(
2537 Ty: ShType,
2538 V: APInt::getOneBitSet(numBits: TypeBits, BitNo: TypeBits - ShiftAmt->getZExtValue() - 1));
2539 Value *And = Builder.CreateAnd(LHS: X, RHS: Mask, Name: Shl->getName() + ".mask");
2540 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2541 And, Constant::getNullValue(Ty: ShType));
2542 }
2543
2544 // Simplify 'shl' inequality test into 'and' equality test.
2545 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2546 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2547 if ((C + 1).isPowerOf2() &&
2548 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2549 Value *And = Builder.CreateAnd(LHS: X, RHS: (~C).lshr(shiftAmt: ShiftAmt->getZExtValue()));
2550 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2551 : ICmpInst::ICMP_NE,
2552 And, Constant::getNullValue(Ty: ShType));
2553 }
2554 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2555 if (C.isPowerOf2() &&
2556 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2557 Value *And =
2558 Builder.CreateAnd(LHS: X, RHS: (~(C - 1)).lshr(shiftAmt: ShiftAmt->getZExtValue()));
2559 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2560 : ICmpInst::ICMP_NE,
2561 And, Constant::getNullValue(Ty: ShType));
2562 }
2563 }
2564
2565 // Transform (icmp pred iM (shl iM %v, N), C)
2566 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2567 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2568 // This enables us to get rid of the shift in favor of a trunc that may be
2569 // free on the target. It has the additional benefit of comparing to a
2570 // smaller constant that may be more target-friendly.
2571 unsigned Amt = ShiftAmt->getLimitedValue(Limit: TypeBits - 1);
2572 if (Shl->hasOneUse() && Amt != 0 &&
2573 shouldChangeType(FromBitWidth: ShType->getScalarSizeInBits(), ToBitWidth: TypeBits - Amt)) {
2574 ICmpInst::Predicate CmpPred = Pred;
2575 APInt RHSC = C;
2576
2577 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(predicate: CmpPred)) {
2578 // Try the flipped strictness predicate.
2579 // e.g.:
2580 // icmp ult i64 (shl X, 32), 8589934593 ->
2581 // icmp ule i64 (shl X, 32), 8589934592 ->
2582 // icmp ule i32 (trunc X, i32), 2 ->
2583 // icmp ult i32 (trunc X, i32), 3
2584 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(
2585 Pred, C: ConstantInt::get(Context&: ShType->getContext(), V: C))) {
2586 CmpPred = FlippedStrictness->first;
2587 RHSC = cast<ConstantInt>(Val: FlippedStrictness->second)->getValue();
2588 }
2589 }
2590
2591 if (RHSC.countr_zero() >= Amt) {
2592 Type *TruncTy = ShType->getWithNewBitWidth(NewBitWidth: TypeBits - Amt);
2593 Constant *NewC =
2594 ConstantInt::get(Ty: TruncTy, V: RHSC.ashr(ShiftAmt: *ShiftAmt).trunc(width: TypeBits - Amt));
2595 return new ICmpInst(CmpPred,
2596 Builder.CreateTrunc(V: X, DestTy: TruncTy, Name: "", /*IsNUW=*/false,
2597 IsNSW: Shl->hasNoSignedWrap()),
2598 NewC);
2599 }
2600 }
2601
2602 return nullptr;
2603}
2604
2605/// Fold icmp ({al}shr X, Y), C.
2606Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,
2607 BinaryOperator *Shr,
2608 const APInt &C) {
2609 // An exact shr only shifts out zero bits, so:
2610 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2611 Value *X = Shr->getOperand(i_nocapture: 0);
2612 CmpInst::Predicate Pred = Cmp.getPredicate();
2613 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2614 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
2615
2616 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2617 const APInt *ShiftValC;
2618 if (match(V: X, P: m_APInt(Res&: ShiftValC))) {
2619 if (Cmp.isEquality())
2620 return foldICmpShrConstConst(I&: Cmp, A: Shr->getOperand(i_nocapture: 1), AP1: C, AP2: *ShiftValC);
2621
2622 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2623 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2624 bool TrueIfSigned;
2625 if (!IsAShr && ShiftValC->isNegative() &&
2626 isSignBitCheck(Pred, RHS: C, TrueIfSigned))
2627 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2628 Shr->getOperand(i_nocapture: 1),
2629 ConstantInt::getNullValue(Ty: X->getType()));
2630
2631 // If the shifted constant is a power-of-2, test the shift amount directly:
2632 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2633 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2634 if (!IsAShr && ShiftValC->isPowerOf2() &&
2635 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2636 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2637 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2638 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2639
2640 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2641 unsigned ShiftLZ = ShiftValC->countl_zero();
2642 Constant *NewC = ConstantInt::get(Ty: Shr->getType(), V: CmpLZ - ShiftLZ);
2643 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2644 return new ICmpInst(NewPred, Shr->getOperand(i_nocapture: 1), NewC);
2645 }
2646 }
2647
2648 const APInt *ShiftAmtC;
2649 if (!match(V: Shr->getOperand(i_nocapture: 1), P: m_APInt(Res&: ShiftAmtC)))
2650 return nullptr;
2651
2652 // Check that the shift amount is in range. If not, don't perform undefined
2653 // shifts. When the shift is visited it will be simplified.
2654 unsigned TypeBits = C.getBitWidth();
2655 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(Limit: TypeBits);
2656 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2657 return nullptr;
2658
2659 bool IsExact = Shr->isExact();
2660 Type *ShrTy = Shr->getType();
2661 // TODO: If we could guarantee that InstSimplify would handle all of the
2662 // constant-value-based preconditions in the folds below, then we could assert
2663 // those conditions rather than checking them. This is difficult because of
2664 // undef/poison (PR34838).
2665 if (IsAShr && Shr->hasOneUse()) {
2666 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2667 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2668 // When C - 1 is a power of two and the transform can be legally
2669 // performed, prefer this form so the produced constant is close to a
2670 // power of two.
2671 // icmp slt/ult (ashr exact X, ShAmtC), C
2672 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2673 APInt ShiftedC = (C - 1).shl(shiftAmt: ShAmtVal) + 1;
2674 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2675 }
2676 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2677 // When ShAmtC can be shifted losslessly:
2678 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2679 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2680 APInt ShiftedC = C.shl(shiftAmt: ShAmtVal);
2681 if (ShiftedC.ashr(ShiftAmt: ShAmtVal) == C)
2682 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2683 }
2684 if (Pred == CmpInst::ICMP_SGT) {
2685 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2686 APInt ShiftedC = (C + 1).shl(shiftAmt: ShAmtVal) - 1;
2687 if (!C.isMaxSignedValue() && !(C + 1).shl(shiftAmt: ShAmtVal).isMinSignedValue() &&
2688 (ShiftedC + 1).ashr(ShiftAmt: ShAmtVal) == (C + 1))
2689 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2690 }
2691 if (Pred == CmpInst::ICMP_UGT) {
2692 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2693 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2694 // clause accounts for that pattern.
2695 APInt ShiftedC = (C + 1).shl(shiftAmt: ShAmtVal) - 1;
2696 if ((ShiftedC + 1).ashr(ShiftAmt: ShAmtVal) == (C + 1) ||
2697 (C + 1).shl(shiftAmt: ShAmtVal).isMinSignedValue())
2698 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2699 }
2700
2701 // If the compare constant has significant bits above the lowest sign-bit,
2702 // then convert an unsigned cmp to a test of the sign-bit:
2703 // (ashr X, ShiftC) u> C --> X s< 0
2704 // (ashr X, ShiftC) u< C --> X s> -1
2705 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2706 if (Pred == CmpInst::ICMP_UGT) {
2707 return new ICmpInst(CmpInst::ICMP_SLT, X,
2708 ConstantInt::getNullValue(Ty: ShrTy));
2709 }
2710 if (Pred == CmpInst::ICMP_ULT) {
2711 return new ICmpInst(CmpInst::ICMP_SGT, X,
2712 ConstantInt::getAllOnesValue(Ty: ShrTy));
2713 }
2714 }
2715 } else if (!IsAShr) {
2716 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2717 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2718 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2719 APInt ShiftedC = C.shl(shiftAmt: ShAmtVal);
2720 if (ShiftedC.lshr(shiftAmt: ShAmtVal) == C)
2721 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2722 }
2723 if (Pred == CmpInst::ICMP_UGT) {
2724 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2725 APInt ShiftedC = (C + 1).shl(shiftAmt: ShAmtVal) - 1;
2726 if ((ShiftedC + 1).lshr(shiftAmt: ShAmtVal) == (C + 1))
2727 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2728 }
2729 }
2730
2731 if (!Cmp.isEquality())
2732 return nullptr;
2733
2734 // Handle equality comparisons of shift-by-constant.
2735
2736 // If the comparison constant changes with the shift, the comparison cannot
2737 // succeed (bits of the comparison constant cannot match the shifted value).
2738 // This should be known by InstSimplify and already be folded to true/false.
2739 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2740 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2741 "Expected icmp+shr simplify did not occur.");
2742
2743 // If the bits shifted out are known zero, compare the unshifted value:
2744 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2745 if (Shr->isExact())
2746 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: C << ShAmtVal));
2747
2748 if (Shr->hasOneUse()) {
2749 // Canonicalize the shift into an 'and':
2750 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2751 APInt Val(APInt::getHighBitsSet(numBits: TypeBits, hiBitsSet: TypeBits - ShAmtVal));
2752 Constant *Mask = ConstantInt::get(Ty: ShrTy, V: Val);
2753 Value *And = Builder.CreateAnd(LHS: X, RHS: Mask, Name: Shr->getName() + ".mask");
2754 return new ICmpInst(Pred, And, ConstantInt::get(Ty: ShrTy, V: C << ShAmtVal));
2755 }
2756
2757 return nullptr;
2758}
2759
2760Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
2761 BinaryOperator *SRem,
2762 const APInt &C) {
2763 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2764 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
2765 // Canonicalize unsigned predicates to signed:
2766 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 0
2767 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1
2768 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1
2769 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1
2770
2771 const APInt *DivisorC;
2772 if (!match(V: SRem->getOperand(i_nocapture: 1), P: m_APInt(Res&: DivisorC)))
2773 return nullptr;
2774 if (DivisorC->isZero())
2775 return nullptr;
2776
2777 APInt NormalizedC = C;
2778 if (Pred == ICmpInst::ICMP_ULT) {
2779 assert(!NormalizedC.isZero() &&
2780 "ult X, 0 should have been simplified already.");
2781 --NormalizedC;
2782 }
2783 if (C.isNegative())
2784 NormalizedC.flipAllBits();
2785 if (!NormalizedC.uge(RHS: DivisorC->abs() - 1))
2786 return nullptr;
2787
2788 Type *Ty = SRem->getType();
2789 if (Pred == ICmpInst::ICMP_UGT)
2790 return new ICmpInst(ICmpInst::ICMP_SLT, SRem,
2791 ConstantInt::getNullValue(Ty));
2792 return new ICmpInst(ICmpInst::ICMP_SGT, SRem,
2793 ConstantInt::getAllOnesValue(Ty));
2794 }
2795 // Match an 'is positive' or 'is negative' comparison of remainder by a
2796 // constant power-of-2 value:
2797 // (X % pow2C) sgt/slt 0
2798 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2799 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2800 return nullptr;
2801
2802 // TODO: The one-use check is standard because we do not typically want to
2803 // create longer instruction sequences, but this might be a special-case
2804 // because srem is not good for analysis or codegen.
2805 if (!SRem->hasOneUse())
2806 return nullptr;
2807
2808 const APInt *DivisorC;
2809 if (!match(V: SRem->getOperand(i_nocapture: 1), P: m_Power2(V&: DivisorC)))
2810 return nullptr;
2811
2812 // For cmp_sgt/cmp_slt only zero valued C is handled.
2813 // For cmp_eq/cmp_ne only positive valued C is handled.
2814 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2815 !C.isZero()) ||
2816 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2817 !C.isStrictlyPositive()))
2818 return nullptr;
2819
2820 // Mask off the sign bit and the modulo bits (low-bits).
2821 Type *Ty = SRem->getType();
2822 APInt SignMask = APInt::getSignMask(BitWidth: Ty->getScalarSizeInBits());
2823 Constant *MaskC = ConstantInt::get(Ty, V: SignMask | (*DivisorC - 1));
2824 Value *And = Builder.CreateAnd(LHS: SRem->getOperand(i_nocapture: 0), RHS: MaskC);
2825
2826 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2827 return new ICmpInst(Pred, And, ConstantInt::get(Ty, V: C));
2828
2829 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2830 // bit is set. Example:
2831 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2832 if (Pred == ICmpInst::ICMP_SGT)
2833 return new ICmpInst(ICmpInst::ICMP_SGT, And, ConstantInt::getNullValue(Ty));
2834
2835 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2836 // bit is set. Example:
2837 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2838 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, V: SignMask));
2839}
2840
2841/// Fold icmp (udiv X, Y), C.
2842Instruction *InstCombinerImpl::foldICmpUDivConstant(ICmpInst &Cmp,
2843 BinaryOperator *UDiv,
2844 const APInt &C) {
2845 ICmpInst::Predicate Pred = Cmp.getPredicate();
2846 Value *X = UDiv->getOperand(i_nocapture: 0);
2847 Value *Y = UDiv->getOperand(i_nocapture: 1);
2848 Type *Ty = UDiv->getType();
2849
2850 const APInt *C2;
2851 if (!match(V: X, P: m_APInt(Res&: C2)))
2852 return nullptr;
2853
2854 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2855
2856 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2857 if (Pred == ICmpInst::ICMP_UGT) {
2858 assert(!C.isMaxValue() &&
2859 "icmp ugt X, UINT_MAX should have been simplified already.");
2860 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2861 ConstantInt::get(Ty, V: C2->udiv(RHS: C + 1)));
2862 }
2863
2864 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2865 if (Pred == ICmpInst::ICMP_ULT) {
2866 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2867 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2868 ConstantInt::get(Ty, V: C2->udiv(RHS: C)));
2869 }
2870
2871 return nullptr;
2872}
2873
2874/// Fold icmp ({su}div X, Y), C.
2875Instruction *InstCombinerImpl::foldICmpDivConstant(ICmpInst &Cmp,
2876 BinaryOperator *Div,
2877 const APInt &C) {
2878 ICmpInst::Predicate Pred = Cmp.getPredicate();
2879 Value *X = Div->getOperand(i_nocapture: 0);
2880 Value *Y = Div->getOperand(i_nocapture: 1);
2881 Type *Ty = Div->getType();
2882 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2883
2884 // If unsigned division and the compare constant is bigger than
2885 // UMAX/2 (negative), there's only one pair of values that satisfies an
2886 // equality check, so eliminate the division:
2887 // (X u/ Y) == C --> (X == C) && (Y == 1)
2888 // (X u/ Y) != C --> (X != C) || (Y != 1)
2889 // Similarly, if signed division and the compare constant is exactly SMIN:
2890 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2891 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2892 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2893 (!DivIsSigned || C.isMinSignedValue())) {
2894 Value *XBig = Builder.CreateICmp(P: Pred, LHS: X, RHS: ConstantInt::get(Ty, V: C));
2895 Value *YOne = Builder.CreateICmp(P: Pred, LHS: Y, RHS: ConstantInt::get(Ty, V: 1));
2896 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2897 return BinaryOperator::Create(Op: Logic, S1: XBig, S2: YOne);
2898 }
2899
2900 // Fold: icmp pred ([us]div X, C2), C -> range test
2901 // Fold this div into the comparison, producing a range check.
2902 // Determine, based on the divide type, what the range is being
2903 // checked. If there is an overflow on the low or high side, remember
2904 // it, otherwise compute the range [low, hi) bounding the new value.
2905 // See: InsertRangeTest above for the kinds of replacements possible.
2906 const APInt *C2;
2907 if (!match(V: Y, P: m_APInt(Res&: C2)))
2908 return nullptr;
2909
2910 // FIXME: If the operand types don't match the type of the divide
2911 // then don't attempt this transform. The code below doesn't have the
2912 // logic to deal with a signed divide and an unsigned compare (and
2913 // vice versa). This is because (x /s C2) <s C produces different
2914 // results than (x /s C2) <u C or (x /u C2) <s C or even
2915 // (x /u C2) <u C. Simply casting the operands and result won't
2916 // work. :( The if statement below tests that condition and bails
2917 // if it finds it.
2918 // However, when the divisor is a positive constant and the dividend is
2919 // known non-negative, sdiv is equivalent to udiv, so we can lower
2920 // DivIsSigned and proceed through the unsigned path.
2921 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned()) {
2922 if (!DivIsSigned || !C2->isStrictlyPositive() ||
2923 !isKnownNonNegative(V: X, SQ: SQ.getWithInstruction(I: &Cmp)))
2924 return nullptr;
2925 DivIsSigned = false;
2926 }
2927
2928 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2929 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2930 // division-by-constant cases should be present, we can not assert that they
2931 // have happened before we reach this icmp instruction.
2932 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2933 return nullptr;
2934
2935 // Compute Prod = C * C2. We are essentially solving an equation of
2936 // form X / C2 = C. We solve for X by multiplying C2 and C.
2937 // By solving for X, we can turn this into a range check instead of computing
2938 // a divide.
2939 APInt Prod = C * *C2;
2940
2941 // Determine if the product overflows by seeing if the product is not equal to
2942 // the divide. Make sure we do the same kind of divide as in the LHS
2943 // instruction that we're folding.
2944 bool ProdOV = (DivIsSigned ? Prod.sdiv(RHS: *C2) : Prod.udiv(RHS: *C2)) != C;
2945
2946 // If the division is known to be exact, then there is no remainder from the
2947 // divide, so the covered range size is unit, otherwise it is the divisor.
2948 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2949
2950 // Figure out the interval that is being checked. For example, a comparison
2951 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2952 // Compute this interval based on the constants involved and the signedness of
2953 // the compare/divide. This computes a half-open interval, keeping track of
2954 // whether either value in the interval overflows. After analysis each
2955 // overflow variable is set to 0 if it's corresponding bound variable is valid
2956 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2957 int LoOverflow = 0, HiOverflow = 0;
2958 APInt LoBound, HiBound;
2959
2960 if (!DivIsSigned) { // udiv
2961 // e.g. X/5 op 3 --> [15, 20)
2962 LoBound = Prod;
2963 HiOverflow = LoOverflow = ProdOV;
2964 if (!HiOverflow) {
2965 // If this is not an exact divide, then many values in the range collapse
2966 // to the same result value.
2967 HiOverflow = addWithOverflow(Result&: HiBound, In1: LoBound, In2: RangeSize, IsSigned: false);
2968 }
2969 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2970 if (C.isZero()) { // (X / pos) op 0
2971 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2972 LoBound = -(RangeSize - 1);
2973 HiBound = RangeSize;
2974 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2975 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2976 HiOverflow = LoOverflow = ProdOV;
2977 if (!HiOverflow)
2978 HiOverflow = addWithOverflow(Result&: HiBound, In1: Prod, In2: RangeSize, IsSigned: true);
2979 } else { // (X / pos) op neg
2980 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2981 HiBound = Prod + 1;
2982 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2983 if (!LoOverflow) {
2984 APInt DivNeg = -RangeSize;
2985 LoOverflow = addWithOverflow(Result&: LoBound, In1: HiBound, In2: DivNeg, IsSigned: true) ? -1 : 0;
2986 }
2987 }
2988 } else if (C2->isNegative()) { // Divisor is < 0.
2989 if (Div->isExact())
2990 RangeSize.negate();
2991 if (C.isZero()) { // (X / neg) op 0
2992 // e.g. X/-5 op 0 --> [-4, 5)
2993 LoBound = RangeSize + 1;
2994 HiBound = -RangeSize;
2995 if (HiBound == *C2) { // -INTMIN = INTMIN
2996 HiOverflow = 1; // [INTMIN+1, overflow)
2997 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2998 }
2999 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
3000 // e.g. X/-5 op 3 --> [-19, -14)
3001 HiBound = Prod + 1;
3002 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
3003 if (!LoOverflow)
3004 LoOverflow =
3005 addWithOverflow(Result&: LoBound, In1: HiBound, In2: RangeSize, IsSigned: true) ? -1 : 0;
3006 } else { // (X / neg) op neg
3007 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
3008 LoOverflow = HiOverflow = ProdOV;
3009 if (!HiOverflow)
3010 HiOverflow = subWithOverflow(Result&: HiBound, In1: Prod, In2: RangeSize, IsSigned: true);
3011 }
3012
3013 // Dividing by a negative swaps the condition. LT <-> GT
3014 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
3015 }
3016
3017 switch (Pred) {
3018 default:
3019 llvm_unreachable("Unhandled icmp predicate!");
3020 case ICmpInst::ICMP_EQ:
3021 if (LoOverflow && HiOverflow)
3022 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
3023 if (HiOverflow)
3024 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
3025 X, ConstantInt::get(Ty, V: LoBound));
3026 if (LoOverflow)
3027 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
3028 X, ConstantInt::get(Ty, V: HiBound));
3029 return replaceInstUsesWith(
3030 I&: Cmp, V: insertRangeTest(V: X, Lo: LoBound, Hi: HiBound, isSigned: DivIsSigned, Inside: true));
3031 case ICmpInst::ICMP_NE:
3032 if (LoOverflow && HiOverflow)
3033 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
3034 if (HiOverflow)
3035 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
3036 X, ConstantInt::get(Ty, V: LoBound));
3037 if (LoOverflow)
3038 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
3039 X, ConstantInt::get(Ty, V: HiBound));
3040 return replaceInstUsesWith(
3041 I&: Cmp, V: insertRangeTest(V: X, Lo: LoBound, Hi: HiBound, isSigned: DivIsSigned, Inside: false));
3042 case ICmpInst::ICMP_ULT:
3043 case ICmpInst::ICMP_SLT:
3044 if (LoOverflow == +1) // Low bound is greater than input range.
3045 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
3046 if (LoOverflow == -1) // Low bound is less than input range.
3047 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
3048 return new ICmpInst(Pred, X, ConstantInt::get(Ty, V: LoBound));
3049 case ICmpInst::ICMP_UGT:
3050 case ICmpInst::ICMP_SGT:
3051 if (HiOverflow == +1) // High bound greater than input range.
3052 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
3053 if (HiOverflow == -1) // High bound less than input range.
3054 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
3055 if (Pred == ICmpInst::ICMP_UGT)
3056 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, V: HiBound));
3057 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, V: HiBound));
3058 }
3059
3060 return nullptr;
3061}
3062
3063/// Fold icmp (sub X, Y), C.
3064Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,
3065 BinaryOperator *Sub,
3066 const APInt &C) {
3067 Value *X = Sub->getOperand(i_nocapture: 0), *Y = Sub->getOperand(i_nocapture: 1);
3068 ICmpInst::Predicate Pred = Cmp.getPredicate();
3069 Type *Ty = Sub->getType();
3070
3071 // (X - (X urem D)) is D*(X/D), a multiple of D, so it is u> C exactly when
3072 // X u>= D (for C u< D), and u< C exactly when X u< D (for 0 u< C u<= D):
3073 // icmp ugt (sub X, (urem X, D)), C --> icmp ugt X, D-1
3074 // icmp ult (sub X, (urem X, D)), C --> icmp ult X, D
3075 const APInt *D;
3076 if (match(V: Y, P: m_URem(L: m_Specific(V: X), R: m_APInt(Res&: D))) && !D->isZero()) {
3077 if (Pred == ICmpInst::ICMP_UGT && C.ult(RHS: *D))
3078 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, V: *D - 1));
3079 if (Pred == ICmpInst::ICMP_ULT && !C.isZero() && C.ule(RHS: *D))
3080 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, V: *D));
3081 }
3082
3083 // (SubC - Y) == C) --> Y == (SubC - C)
3084 // (SubC - Y) != C) --> Y != (SubC - C)
3085 Constant *SubC;
3086 if (Cmp.isEquality() && match(V: X, P: m_ImmConstant(C&: SubC))) {
3087 return new ICmpInst(Pred, Y,
3088 ConstantExpr::getSub(C1: SubC, C2: ConstantInt::get(Ty, V: C)));
3089 }
3090
3091 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
3092 const APInt *C2;
3093 APInt SubResult;
3094 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
3095 bool HasNSW = Sub->hasNoSignedWrap();
3096 bool HasNUW = Sub->hasNoUnsignedWrap();
3097 if (match(V: X, P: m_APInt(Res&: C2)) &&
3098 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
3099 !subWithOverflow(Result&: SubResult, In1: *C2, In2: C, IsSigned: Cmp.isSigned()))
3100 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, V: SubResult));
3101
3102 // X - Y == 0 --> X == Y.
3103 // X - Y != 0 --> X != Y.
3104 // TODO: We allow this with multiple uses as long as the other uses are not
3105 // in phis. The phi use check is guarding against a codegen regression
3106 // for a loop test. If the backend could undo this (and possibly
3107 // subsequent transforms), we would not need this hack.
3108 if (Cmp.isEquality() && C.isZero() &&
3109 none_of(Range: (Sub->users()), P: [](const User *U) { return isa<PHINode>(Val: U); }))
3110 return new ICmpInst(Pred, X, Y);
3111
3112 // The following transforms are only worth it if the only user of the subtract
3113 // is the icmp.
3114 // TODO: This is an artificial restriction for all of the transforms below
3115 // that only need a single replacement icmp. Can these use the phi test
3116 // like the transform above here?
3117 if (!Sub->hasOneUse())
3118 return nullptr;
3119
3120 if (Sub->hasNoSignedWrap()) {
3121 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
3122 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
3123 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
3124
3125 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
3126 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
3127 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3128
3129 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
3130 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
3131 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
3132
3133 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
3134 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3135 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3136 }
3137
3138 if (!match(V: X, P: m_APInt(Res&: C2)))
3139 return nullptr;
3140
3141 // C2 - Y <u C -> (Y | (C - 1)) == C2
3142 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
3143 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
3144 (*C2 & (C - 1)) == (C - 1))
3145 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(LHS: Y, RHS: C - 1), X);
3146
3147 // C2 - Y >u C -> (Y | C) != C2
3148 // iff C2 & C == C and C + 1 is a power of 2
3149 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
3150 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(LHS: Y, RHS: C), X);
3151
3152 // We have handled special cases that reduce.
3153 // Canonicalize any remaining sub to add as:
3154 // (C2 - Y) > C --> (Y + ~C2) < ~C
3155 Value *Add = Builder.CreateAdd(LHS: Y, RHS: ConstantInt::get(Ty, V: ~(*C2)), Name: "notsub",
3156 HasNUW, HasNSW);
3157 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, V: ~C));
3158}
3159
3160static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
3161 Value *Op1, IRBuilderBase &Builder,
3162 bool HasOneUse) {
3163 auto FoldConstant = [&](bool Val) {
3164 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
3165 if (Op0->getType()->isVectorTy())
3166 Res = ConstantVector::getSplat(
3167 EC: cast<VectorType>(Val: Op0->getType())->getElementCount(), Elt: Res);
3168 return Res;
3169 };
3170
3171 switch (Table.to_ulong()) {
3172 case 0: // 0 0 0 0
3173 return FoldConstant(false);
3174 case 1: // 0 0 0 1
3175 return HasOneUse ? Builder.CreateNot(V: Builder.CreateOr(LHS: Op0, RHS: Op1)) : nullptr;
3176 case 2: // 0 0 1 0
3177 return HasOneUse ? Builder.CreateAnd(LHS: Builder.CreateNot(V: Op0), RHS: Op1) : nullptr;
3178 case 3: // 0 0 1 1
3179 return Builder.CreateNot(V: Op0);
3180 case 4: // 0 1 0 0
3181 return HasOneUse ? Builder.CreateAnd(LHS: Op0, RHS: Builder.CreateNot(V: Op1)) : nullptr;
3182 case 5: // 0 1 0 1
3183 return Builder.CreateNot(V: Op1);
3184 case 6: // 0 1 1 0
3185 return Builder.CreateXor(LHS: Op0, RHS: Op1);
3186 case 7: // 0 1 1 1
3187 return HasOneUse ? Builder.CreateNot(V: Builder.CreateAnd(LHS: Op0, RHS: Op1)) : nullptr;
3188 case 8: // 1 0 0 0
3189 return Builder.CreateAnd(LHS: Op0, RHS: Op1);
3190 case 9: // 1 0 0 1
3191 return HasOneUse ? Builder.CreateNot(V: Builder.CreateXor(LHS: Op0, RHS: Op1)) : nullptr;
3192 case 10: // 1 0 1 0
3193 return Op1;
3194 case 11: // 1 0 1 1
3195 return HasOneUse ? Builder.CreateOr(LHS: Builder.CreateNot(V: Op0), RHS: Op1) : nullptr;
3196 case 12: // 1 1 0 0
3197 return Op0;
3198 case 13: // 1 1 0 1
3199 return HasOneUse ? Builder.CreateOr(LHS: Op0, RHS: Builder.CreateNot(V: Op1)) : nullptr;
3200 case 14: // 1 1 1 0
3201 return Builder.CreateOr(LHS: Op0, RHS: Op1);
3202 case 15: // 1 1 1 1
3203 return FoldConstant(true);
3204 default:
3205 llvm_unreachable("Invalid Operation");
3206 }
3207 return nullptr;
3208}
3209
3210Instruction *InstCombinerImpl::foldICmpBinOpWithConstantViaTruthTable(
3211 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3212 Value *A, *B;
3213 Constant *C1, *C2, *C3, *C4;
3214 if (!match(V: BO->getOperand(i_nocapture: 0),
3215 P: m_SelectLike(C: m_Value(V&: A), TrueC: m_Constant(C&: C1), FalseC: m_Constant(C&: C2))) ||
3216 !match(V: BO->getOperand(i_nocapture: 1),
3217 P: m_SelectLike(C: m_Value(V&: B), TrueC: m_Constant(C&: C3), FalseC: m_Constant(C&: C4))) ||
3218 Cmp.getType() != A->getType() || Cmp.getType() != B->getType())
3219 return nullptr;
3220
3221 std::bitset<4> Table;
3222 auto ComputeTable = [&](bool First, bool Second) -> std::optional<bool> {
3223 Constant *L = First ? C1 : C2;
3224 Constant *R = Second ? C3 : C4;
3225 if (auto *Res = ConstantFoldBinaryOpOperands(Opcode: BO->getOpcode(), LHS: L, RHS: R, DL)) {
3226 auto *Val = Res->getType()->isVectorTy() ? Res->getSplatValue() : Res;
3227 if (auto *CI = dyn_cast_or_null<ConstantInt>(Val))
3228 return ICmpInst::compare(LHS: CI->getValue(), RHS: C, Pred: Cmp.getPredicate());
3229 }
3230 return std::nullopt;
3231 };
3232
3233 for (unsigned I = 0; I < 4; ++I) {
3234 bool First = (I >> 1) & 1;
3235 bool Second = I & 1;
3236 if (auto Res = ComputeTable(First, Second))
3237 Table[I] = *Res;
3238 else
3239 return nullptr;
3240 }
3241
3242 // Synthesize optimal logic.
3243 if (auto *Cond = createLogicFromTable(Table, Op0: A, Op1: B, Builder, HasOneUse: BO->hasOneUse()))
3244 return replaceInstUsesWith(I&: Cmp, V: Cond);
3245 return nullptr;
3246}
3247
3248/// Fold icmp (add X, Y), C.
3249Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
3250 BinaryOperator *Add,
3251 const APInt &C) {
3252 Value *Y = Add->getOperand(i_nocapture: 1);
3253 Value *X = Add->getOperand(i_nocapture: 0);
3254 const CmpPredicate Pred = Cmp.getCmpPredicate();
3255
3256 // icmp ult (add nuw A, (lshr A, ShAmtC)), C --> icmp ult A, C
3257 // when C <= (1 << ShAmtC).
3258 const APInt *ShAmtC;
3259 Value *A;
3260 unsigned BitWidth = C.getBitWidth();
3261 if (Pred == ICmpInst::ICMP_ULT &&
3262 match(V: Add,
3263 P: m_c_NUWAdd(L: m_Value(V&: A), R: m_LShr(L: m_Deferred(V: A), R: m_APInt(Res&: ShAmtC)))) &&
3264 ShAmtC->ult(RHS: BitWidth) &&
3265 C.ule(RHS: APInt::getOneBitSet(numBits: BitWidth, BitNo: ShAmtC->getZExtValue())))
3266 return new ICmpInst(Pred, A, ConstantInt::get(Ty: A->getType(), V: C));
3267
3268 const APInt *C2;
3269 if (Cmp.isEquality() || !match(V: Y, P: m_APInt(Res&: C2)))
3270 return nullptr;
3271
3272 // Fold icmp pred (add X, C2), C.
3273 Type *Ty = Add->getType();
3274
3275 // If the add does not wrap, we can always adjust the compare by subtracting
3276 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3277 // have been canonicalized to SGT/SLT/UGT/ULT.
3278 if (Add->hasNoUnsignedWrap() &&
3279 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT)) {
3280 bool Overflow;
3281 APInt NewC = C.usub_ov(RHS: *C2, Overflow);
3282 // If there is overflow, the result must be true or false.
3283 if (!Overflow)
3284 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3285 return new ICmpInst(Pred, X, ConstantInt::get(Ty, V: NewC));
3286 }
3287
3288 CmpInst::Predicate ChosenPred = Pred.getPreferredSignedPredicate();
3289
3290 if (Add->hasNoSignedWrap() &&
3291 (ChosenPred == ICmpInst::ICMP_SGT || ChosenPred == ICmpInst::ICMP_SLT)) {
3292 bool Overflow;
3293 APInt NewC = C.ssub_ov(RHS: *C2, Overflow);
3294 if (!Overflow)
3295 // icmp samesign ugt/ult (add nsw X, C2), C
3296 // -> icmp sgt/slt X, (C - C2)
3297 return new ICmpInst(ChosenPred, X, ConstantInt::get(Ty, V: NewC));
3298 }
3299
3300 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3301 C.isNonNegative() && (C - *C2).isNonNegative() &&
3302 computeConstantRange(V: X, /*ForSigned=*/true, SQ: SQ.getWithInstruction(I: &Cmp))
3303 .add(Other: *C2)
3304 .isAllNonNegative())
3305 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
3306 ConstantInt::get(Ty, V: C - *C2));
3307
3308 auto CR = ConstantRange::makeExactICmpRegion(Pred, Other: C).subtract(CI: *C2);
3309 const APInt &Upper = CR.getUpper();
3310 const APInt &Lower = CR.getLower();
3311 if (Cmp.isSigned()) {
3312 if (Lower.isSignMask())
3313 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, V: Upper));
3314 if (Upper.isSignMask())
3315 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, V: Lower));
3316 } else {
3317 if (Lower.isMinValue())
3318 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, V: Upper));
3319 if (Upper.isMinValue())
3320 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, V: Lower));
3321 }
3322
3323 // This set of folds is intentionally placed after folds that use no-wrapping
3324 // flags because those folds are likely better for later analysis/codegen.
3325 const APInt SMax = APInt::getSignedMaxValue(numBits: Ty->getScalarSizeInBits());
3326 const APInt SMin = APInt::getSignedMinValue(numBits: Ty->getScalarSizeInBits());
3327
3328 // Fold compare with offset to opposite sign compare if it eliminates offset:
3329 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3330 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3331 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, V: -(*C2)));
3332
3333 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3334 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3335 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, V: ~(*C2)));
3336
3337 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3338 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3339 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, V: SMax - C));
3340
3341 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3342 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3343 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, V: C ^ SMax));
3344
3345 // (X + -1) <u C --> X <=u C (if X is never null)
3346 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3347 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
3348 if (llvm::isKnownNonZero(V: X, Q))
3349 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, V: C));
3350 }
3351
3352 if (!Add->hasOneUse())
3353 return nullptr;
3354
3355 // X+C <u C2 -> (X & -C2) == C
3356 // iff C & (C2-1) == 0
3357 // C2 is a power of 2
3358 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3359 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(LHS: X, RHS: -C),
3360 ConstantExpr::getNeg(C: cast<Constant>(Val: Y)));
3361
3362 // X+C2 <u C -> (X & C) == 2C
3363 // iff C == -(C2)
3364 // C2 is a power of 2
3365 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3366 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(LHS: X, RHS: C),
3367 ConstantInt::get(Ty, V: C * 2));
3368
3369 // X+C >u C2 -> (X & ~C2) != C
3370 // iff C & C2 == 0
3371 // C2+1 is a power of 2
3372 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3373 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(LHS: X, RHS: ~C),
3374 ConstantExpr::getNeg(C: cast<Constant>(Val: Y)));
3375
3376 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3377 // to the ult form.
3378 // X+C2 >u C -> X+(C2-C-1) <u ~C
3379 if (Pred == ICmpInst::ICMP_UGT)
3380 return new ICmpInst(ICmpInst::ICMP_ULT,
3381 Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty, V: *C2 - C - 1)),
3382 ConstantInt::get(Ty, V: ~C));
3383
3384 // zext(V) + C2 pred C -> V + C3 pred' C4
3385 Value *V;
3386 if (match(V: X, P: m_ZExt(Op: m_Value(V)))) {
3387 Type *NewCmpTy = V->getType();
3388 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();
3389 if (shouldChangeType(From: Ty, To: NewCmpTy)) {
3390 ConstantRange SrcCR = CR.truncate(BitWidth: NewCmpBW, NoWrapKind: TruncInst::NoUnsignedWrap);
3391 CmpInst::Predicate EquivPred;
3392 APInt EquivInt;
3393 APInt EquivOffset;
3394
3395 SrcCR.getEquivalentICmp(Pred&: EquivPred, RHS&: EquivInt, Offset&: EquivOffset);
3396 return new ICmpInst(
3397 EquivPred,
3398 EquivOffset.isZero()
3399 ? V
3400 : Builder.CreateAdd(LHS: V, RHS: ConstantInt::get(Ty: NewCmpTy, V: EquivOffset)),
3401 ConstantInt::get(Ty: NewCmpTy, V: EquivInt));
3402 }
3403 }
3404
3405 return nullptr;
3406}
3407
3408bool InstCombinerImpl::matchThreeWayIntCompare(SelectInst *SI, Value *&LHS,
3409 Value *&RHS, ConstantInt *&Less,
3410 ConstantInt *&Equal,
3411 ConstantInt *&Greater) {
3412 // TODO: Generalize this to work with other comparison idioms or ensure
3413 // they get canonicalized into this form.
3414
3415 // select i1 (a == b),
3416 // i32 Equal,
3417 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3418 // where Equal, Less and Greater are placeholders for any three constants.
3419 CmpPredicate PredA;
3420 if (!match(V: SI->getCondition(), P: m_ICmp(Pred&: PredA, L: m_Value(V&: LHS), R: m_Value(V&: RHS))) ||
3421 !ICmpInst::isEquality(P: PredA))
3422 return false;
3423 Value *EqualVal = SI->getTrueValue();
3424 Value *UnequalVal = SI->getFalseValue();
3425 // We still can get non-canonical predicate here, so canonicalize.
3426 if (PredA == ICmpInst::ICMP_NE)
3427 std::swap(a&: EqualVal, b&: UnequalVal);
3428 if (!match(V: EqualVal, P: m_ConstantInt(CI&: Equal)))
3429 return false;
3430 CmpPredicate PredB;
3431 Value *LHS2, *RHS2;
3432 if (!match(V: UnequalVal, P: m_Select(C: m_ICmp(Pred&: PredB, L: m_Value(V&: LHS2), R: m_Value(V&: RHS2)),
3433 L: m_ConstantInt(CI&: Less), R: m_ConstantInt(CI&: Greater))))
3434 return false;
3435 // We can get predicate mismatch here, so canonicalize if possible:
3436 // First, ensure that 'LHS' match.
3437 if (LHS2 != LHS) {
3438 // x sgt y <--> y slt x
3439 std::swap(a&: LHS2, b&: RHS2);
3440 PredB = ICmpInst::getSwappedPredicate(pred: PredB);
3441 }
3442 if (LHS2 != LHS)
3443 return false;
3444 // We also need to canonicalize 'RHS'.
3445 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(Val: RHS2)) {
3446 // x sgt C-1 <--> x sge C <--> not(x slt C)
3447 auto FlippedStrictness =
3448 getFlippedStrictnessPredicateAndConstant(Pred: PredB, C: cast<Constant>(Val: RHS2));
3449 if (!FlippedStrictness)
3450 return false;
3451 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3452 "basic correctness failure");
3453 RHS2 = FlippedStrictness->second;
3454 // And kind-of perform the result swap.
3455 std::swap(a&: Less, b&: Greater);
3456 PredB = ICmpInst::ICMP_SLT;
3457 }
3458 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3459}
3460
3461Instruction *InstCombinerImpl::foldICmpSelectConstant(ICmpInst &Cmp,
3462 SelectInst *Select,
3463 ConstantInt *C) {
3464
3465 assert(C && "Cmp RHS should be a constant int!");
3466 // If we're testing a constant value against the result of a three way
3467 // comparison, the result can be expressed directly in terms of the
3468 // original values being compared. Note: We could possibly be more
3469 // aggressive here and remove the hasOneUse test. The original select is
3470 // really likely to simplify or sink when we remove a test of the result.
3471 Value *OrigLHS, *OrigRHS;
3472 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3473 if (Cmp.hasOneUse() &&
3474 matchThreeWayIntCompare(SI: Select, LHS&: OrigLHS, RHS&: OrigRHS, Less&: C1LessThan, Equal&: C2Equal,
3475 Greater&: C3GreaterThan)) {
3476 assert(C1LessThan && C2Equal && C3GreaterThan);
3477
3478 bool TrueWhenLessThan = ICmpInst::compare(
3479 LHS: C1LessThan->getValue(), RHS: C->getValue(), Pred: Cmp.getPredicate());
3480 bool TrueWhenEqual = ICmpInst::compare(LHS: C2Equal->getValue(), RHS: C->getValue(),
3481 Pred: Cmp.getPredicate());
3482 bool TrueWhenGreaterThan = ICmpInst::compare(
3483 LHS: C3GreaterThan->getValue(), RHS: C->getValue(), Pred: Cmp.getPredicate());
3484
3485 // This generates the new instruction that will replace the original Cmp
3486 // Instruction. Instead of enumerating the various combinations when
3487 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3488 // false, we rely on chaining of ORs and future passes of InstCombine to
3489 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3490
3491 // When none of the three constants satisfy the predicate for the RHS (C),
3492 // the entire original Cmp can be simplified to a false.
3493 Value *Cond = Builder.getFalse();
3494 if (TrueWhenLessThan)
3495 Cond = Builder.CreateOr(
3496 LHS: Cond, RHS: Builder.CreateICmp(P: ICmpInst::ICMP_SLT, LHS: OrigLHS, RHS: OrigRHS));
3497 if (TrueWhenEqual)
3498 Cond = Builder.CreateOr(
3499 LHS: Cond, RHS: Builder.CreateICmp(P: ICmpInst::ICMP_EQ, LHS: OrigLHS, RHS: OrigRHS));
3500 if (TrueWhenGreaterThan)
3501 Cond = Builder.CreateOr(
3502 LHS: Cond, RHS: Builder.CreateICmp(P: ICmpInst::ICMP_SGT, LHS: OrigLHS, RHS: OrigRHS));
3503
3504 return replaceInstUsesWith(I&: Cmp, V: Cond);
3505 }
3506 return nullptr;
3507}
3508
3509Instruction *InstCombinerImpl::foldICmpBitCast(ICmpInst &Cmp) {
3510 auto *Bitcast = dyn_cast<BitCastInst>(Val: Cmp.getOperand(i_nocapture: 0));
3511 if (!Bitcast)
3512 return nullptr;
3513
3514 ICmpInst::Predicate Pred = Cmp.getPredicate();
3515 Value *Op1 = Cmp.getOperand(i_nocapture: 1);
3516 Value *BCSrcOp = Bitcast->getOperand(i_nocapture: 0);
3517 Type *SrcType = Bitcast->getSrcTy();
3518 Type *DstType = Bitcast->getType();
3519
3520 // Make sure the bitcast doesn't change between scalar and vector and
3521 // doesn't change the number of vector elements.
3522 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3523 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3524 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3525 Value *X;
3526 if (match(V: BCSrcOp, P: m_SIToFP(Op: m_Value(V&: X)))) {
3527 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3528 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3529 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3530 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3531 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3532 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3533 match(V: Op1, P: m_Zero()))
3534 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: X->getType()));
3535
3536 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3537 if (Pred == ICmpInst::ICMP_SLT && match(V: Op1, P: m_One()))
3538 return new ICmpInst(Pred, X, ConstantInt::get(Ty: X->getType(), V: 1));
3539
3540 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3541 if (Pred == ICmpInst::ICMP_SGT && match(V: Op1, P: m_AllOnes()))
3542 return new ICmpInst(Pred, X,
3543 ConstantInt::getAllOnesValue(Ty: X->getType()));
3544 }
3545
3546 // Zero-equality checks are preserved through unsigned floating-point casts:
3547 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3548 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3549 if (match(V: BCSrcOp, P: m_UIToFP(Op: m_Value(V&: X))))
3550 if (Cmp.isEquality() && match(V: Op1, P: m_Zero()))
3551 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: X->getType()));
3552
3553 const APInt *C;
3554 bool TrueIfSigned;
3555 if (match(V: Op1, P: m_APInt(Res&: C)) && Bitcast->hasOneUse()) {
3556 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3557 // the FP extend/truncate because that cast does not change the sign-bit.
3558 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3559 // The sign-bit is always the most significant bit in those types.
3560 if (isSignBitCheck(Pred, RHS: *C, TrueIfSigned) &&
3561 (match(V: BCSrcOp, P: m_FPExt(Op: m_Value(V&: X))) ||
3562 match(V: BCSrcOp, P: m_FPTrunc(Op: m_Value(V&: X))))) {
3563 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3564 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3565 Type *XType = X->getType();
3566
3567 // We can't currently handle Power style floating point operations here.
3568 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3569 Type *NewType = Builder.getIntNTy(N: XType->getScalarSizeInBits());
3570 if (auto *XVTy = dyn_cast<VectorType>(Val: XType))
3571 NewType = VectorType::get(ElementType: NewType, EC: XVTy->getElementCount());
3572 Value *NewBitcast = Builder.CreateBitCast(V: X, DestTy: NewType);
3573 if (TrueIfSigned)
3574 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3575 ConstantInt::getNullValue(Ty: NewType));
3576 else
3577 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3578 ConstantInt::getAllOnesValue(Ty: NewType));
3579 }
3580 }
3581
3582 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3583 Type *FPType = SrcType->getScalarType();
3584 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3585 Kind: Attribute::NoImplicitFloat) &&
3586 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3587 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3588 if (Mask & (fcInf | fcZero)) {
3589 if (Pred == ICmpInst::ICMP_NE)
3590 Mask = ~Mask;
3591 return replaceInstUsesWith(I&: Cmp,
3592 V: Builder.createIsFPClass(FPNum: BCSrcOp, Test: Mask));
3593 }
3594 }
3595 }
3596 }
3597
3598 // Fold the canonicalized form of vector_reduce_or if the arg is
3599 // get_active_lane mask.
3600 // icmp ne (bitcast <N x i1> to iN (get_active_lane_mask(l, h))), 0 ->
3601 // icmp ult l, h
3602 // icmp eq (bitcast <N x i1> to iN (get_active_lane_mask(l, h))), 0 ->
3603 // icmp uge l, h
3604 Value *Upper, *Lower;
3605 if (match(V: BCSrcOp, P: m_Intrinsic<Intrinsic::get_active_lane_mask>(
3606 Ops: m_Value(V&: Lower), Ops: m_Value(V&: Upper))) &&
3607 match(V: Op1, P: m_Zero()) && DstType->isIntegerTy()) {
3608 if (Pred == ICmpInst::ICMP_NE)
3609 return new ICmpInst(ICmpInst::ICMP_ULT, Lower, Upper);
3610 if (Pred == ICmpInst::ICMP_EQ)
3611 return new ICmpInst(ICmpInst::ICMP_UGE, Lower, Upper);
3612 }
3613
3614 const APInt *C;
3615 if (!match(V: Cmp.getOperand(i_nocapture: 1), P: m_APInt(Res&: C)) || !DstType->isIntegerTy() ||
3616 !SrcType->isIntOrIntVectorTy())
3617 return nullptr;
3618
3619 // If this is checking if all elements of a vector compare are set or not,
3620 // invert the casted vector equality compare and test if all compare
3621 // elements are clear or not. Compare against zero is generally easier for
3622 // analysis and codegen.
3623 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3624 // Example: are all elements equal? --> are zero elements not equal?
3625 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3626 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3627 if (Value *NotBCSrcOp =
3628 getFreelyInverted(V: BCSrcOp, WillInvertAllUses: BCSrcOp->hasOneUse(), Builder: &Builder)) {
3629 Value *Cast = Builder.CreateBitCast(V: NotBCSrcOp, DestTy: DstType);
3630 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(Ty: DstType));
3631 }
3632 }
3633
3634 // If this is checking if all elements of an extended vector are clear or not,
3635 // compare in a narrow type to eliminate the extend:
3636 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3637 Value *X;
3638 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3639 match(V: BCSrcOp, P: m_ZExtOrSExt(Op: m_Value(V&: X)))) {
3640 if (auto *VecTy = dyn_cast<FixedVectorType>(Val: X->getType())) {
3641 Type *NewType = Builder.getIntNTy(N: VecTy->getPrimitiveSizeInBits());
3642 Value *NewCast = Builder.CreateBitCast(V: X, DestTy: NewType);
3643 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(Ty: NewType));
3644 }
3645 }
3646
3647 // Folding: icmp <pred> iN X, C
3648 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3649 // and C is a splat of a K-bit pattern
3650 // and SC is a constant vector = <C', C', C', ..., C'>
3651 // Into:
3652 // %E = extractelement <M x iK> %vec, i32 C'
3653 // icmp <pred> iK %E, trunc(C)
3654 Value *Vec;
3655 ArrayRef<int> Mask;
3656 if (match(V: BCSrcOp, P: m_Shuffle(v1: m_Value(V&: Vec), v2: m_Undef(), mask: m_Mask(Mask)))) {
3657 // Check whether every element of Mask is the same constant
3658 if (all_equal(Range&: Mask)) {
3659 auto *VecTy = cast<VectorType>(Val: SrcType);
3660 auto *EltTy = cast<IntegerType>(Val: VecTy->getElementType());
3661 if (C->isSplat(SplatSizeInBits: EltTy->getBitWidth())) {
3662 // Fold the icmp based on the value of C
3663 // If C is M copies of an iK sized bit pattern,
3664 // then:
3665 // => %E = extractelement <N x iK> %vec, i64 Elem
3666 // icmp <pred> iK %SplatVal, <pattern>
3667 Value *Extract = Builder.CreateExtractElement(Vec, Idx: Mask[0]);
3668 Value *NewC = ConstantInt::get(Ty: EltTy, V: C->trunc(width: EltTy->getBitWidth()));
3669 return new ICmpInst(Pred, Extract, NewC);
3670 }
3671 }
3672 }
3673 return nullptr;
3674}
3675
3676/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3677/// where X is some kind of instruction.
3678Instruction *InstCombinerImpl::foldICmpInstWithConstant(ICmpInst &Cmp) {
3679 const APInt *C;
3680
3681 if (match(V: Cmp.getOperand(i_nocapture: 1), P: m_APInt(Res&: C))) {
3682 if (auto *BO = dyn_cast<BinaryOperator>(Val: Cmp.getOperand(i_nocapture: 0)))
3683 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, C: *C))
3684 return I;
3685
3686 if (auto *SI = dyn_cast<SelectInst>(Val: Cmp.getOperand(i_nocapture: 0)))
3687 // For now, we only support constant integers while folding the
3688 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3689 // similar to the cases handled by binary ops above.
3690 if (auto *ConstRHS = dyn_cast<ConstantInt>(Val: Cmp.getOperand(i_nocapture: 1)))
3691 if (Instruction *I = foldICmpSelectConstant(Cmp, Select: SI, C: ConstRHS))
3692 return I;
3693
3694 if (auto *TI = dyn_cast<TruncInst>(Val: Cmp.getOperand(i_nocapture: 0)))
3695 if (Instruction *I = foldICmpTruncConstant(Cmp, Trunc: TI, C: *C))
3696 return I;
3697
3698 if (auto *II = dyn_cast<IntrinsicInst>(Val: Cmp.getOperand(i_nocapture: 0)))
3699 if (Instruction *I = foldICmpIntrinsicWithConstant(ICI&: Cmp, II, C: *C))
3700 return I;
3701
3702 {
3703 // icmp slt/sgt (extractvalue (frexp X), 1), C -->
3704 // fcmp olt/oge (fabs X), 2^ExpVal
3705 // slt -> olt, ExpVal = C-1; sgt -> oge, ExpVal = C.
3706 Value *X;
3707 if (match(V: Cmp.getOperand(i_nocapture: 0),
3708 P: m_OneUse(SubPattern: m_ExtractValue<1>(
3709 V: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::frexp>(Ops: m_Value(V&: X))))))) {
3710 ICmpInst::Predicate Pred = Cmp.getPredicate();
3711 APInt Exp;
3712 FCmpInst::Predicate NewPred;
3713 bool ValidPred = true;
3714
3715 switch (Pred) {
3716 case ICmpInst::ICMP_SLT:
3717 NewPred = FCmpInst::FCMP_OLT;
3718 Exp = *C - 1;
3719 break;
3720 case ICmpInst::ICMP_SGT:
3721 NewPred = FCmpInst::FCMP_OGE;
3722 Exp = *C;
3723 break;
3724 default:
3725 ValidPred = false;
3726 break;
3727 }
3728
3729 if (ValidPred) {
3730 const fltSemantics &Sem =
3731 X->getType()->getScalarType()->getFltSemantics();
3732 int MaxExp = APFloat::semanticsMaxExponent(Sem);
3733
3734 if (!Exp.isNegative() && Exp.sle(RHS: MaxExp + 1) &&
3735 isKnownNeverInfOrNaN(V: X, SQ: SQ.getWithInstruction(I: &Cmp))) {
3736 int ExpVal = static_cast<int>(Exp.getSExtValue());
3737 APFloat CmpConst = scalbn(X: APFloat::getOne(Sem), Exp: ExpVal,
3738 RM: APFloat::rmNearestTiesToEven);
3739 Value *Fabs = Builder.CreateFAbs(V: X);
3740 return new FCmpInst(NewPred, Fabs,
3741 ConstantFP::get(Ty: X->getType(), V: CmpConst));
3742 }
3743 }
3744 }
3745 }
3746
3747 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3748 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3749 // TODO: This checks one-use, but that is not strictly necessary.
3750 Value *Cmp0 = Cmp.getOperand(i_nocapture: 0);
3751 Value *X, *Y;
3752 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3753 (match(V: Cmp0,
3754 P: m_ExtractValue<0>(V: m_Intrinsic<Intrinsic::ssub_with_overflow>(
3755 Ops: m_Value(V&: X), Ops: m_Value(V&: Y)))) ||
3756 match(V: Cmp0,
3757 P: m_ExtractValue<0>(V: m_Intrinsic<Intrinsic::usub_with_overflow>(
3758 Ops: m_Value(V&: X), Ops: m_Value(V&: Y))))))
3759 return new ICmpInst(Cmp.getPredicate(), X, Y);
3760 }
3761
3762 if (match(V: Cmp.getOperand(i_nocapture: 1), P: m_APIntAllowPoison(Res&: C)))
3763 return foldICmpInstWithConstantAllowPoison(Cmp, C: *C);
3764
3765 return nullptr;
3766}
3767
3768/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3769/// icmp eq/ne BO, C.
3770Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
3771 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3772 // TODO: Some of these folds could work with arbitrary constants, but this
3773 // function is limited to scalar and vector splat constants.
3774 if (!Cmp.isEquality())
3775 return nullptr;
3776
3777 ICmpInst::Predicate Pred = Cmp.getPredicate();
3778 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3779 Constant *RHS = cast<Constant>(Val: Cmp.getOperand(i_nocapture: 1));
3780 Value *BOp0 = BO->getOperand(i_nocapture: 0), *BOp1 = BO->getOperand(i_nocapture: 1);
3781
3782 switch (BO->getOpcode()) {
3783 case Instruction::SRem:
3784 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3785 if (C.isZero() && BO->hasOneUse()) {
3786 const APInt *BOC;
3787 if (match(V: BOp1, P: m_APInt(Res&: BOC)) && BOC->sgt(RHS: 1) && BOC->isPowerOf2()) {
3788 Value *NewRem = Builder.CreateURem(LHS: BOp0, RHS: BOp1, Name: BO->getName());
3789 return new ICmpInst(Pred, NewRem,
3790 Constant::getNullValue(Ty: BO->getType()));
3791 }
3792 }
3793 break;
3794 case Instruction::Add: {
3795 // (A + C2) == C --> A == (C - C2)
3796 // (A + C2) != C --> A != (C - C2)
3797 // TODO: Remove the one-use limitation? See discussion in D58633.
3798 if (Constant *C2 = dyn_cast<Constant>(Val: BOp1)) {
3799 if (BO->hasOneUse())
3800 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(C1: RHS, C2));
3801 } else if (C.isZero()) {
3802 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3803 // efficiently invertible, or if the add has just this one use.
3804 if (Value *NegVal = dyn_castNegVal(V: BOp1))
3805 return new ICmpInst(Pred, BOp0, NegVal);
3806 if (Value *NegVal = dyn_castNegVal(V: BOp0))
3807 return new ICmpInst(Pred, NegVal, BOp1);
3808 if (BO->hasOneUse()) {
3809 // (add nuw A, B) != 0 -> (or A, B) != 0
3810 if (match(V: BO, P: m_NUWAdd(L: m_Value(), R: m_Value()))) {
3811 Value *Or = Builder.CreateOr(LHS: BOp0, RHS: BOp1);
3812 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty: BO->getType()));
3813 }
3814 Value *Neg = Builder.CreateNeg(V: BOp1);
3815 Neg->takeName(V: BO);
3816 return new ICmpInst(Pred, BOp0, Neg);
3817 }
3818 }
3819 break;
3820 }
3821 case Instruction::Xor:
3822 if (Constant *BOC = dyn_cast<Constant>(Val: BOp1)) {
3823 // For the xor case, we can xor two constants together, eliminating
3824 // the explicit xor.
3825 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(C1: RHS, C2: BOC));
3826 } else if (C.isZero()) {
3827 // Replace ((xor A, B) != 0) with (A != B)
3828 return new ICmpInst(Pred, BOp0, BOp1);
3829 }
3830 break;
3831 case Instruction::Or: {
3832 const APInt *BOC;
3833 if (match(V: BOp1, P: m_APInt(Res&: BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3834 // Comparing if all bits outside of a constant mask are set?
3835 // Replace (X | C) == -1 with (X & ~C) == ~C.
3836 // This removes the -1 constant.
3837 Constant *NotBOC = ConstantExpr::getNot(C: cast<Constant>(Val: BOp1));
3838 Value *And = Builder.CreateAnd(LHS: BOp0, RHS: NotBOC);
3839 return new ICmpInst(Pred, And, NotBOC);
3840 }
3841 // (icmp eq (or (select cond, 0, NonZero), Other), 0)
3842 // -> (and cond, (icmp eq Other, 0))
3843 // (icmp ne (or (select cond, NonZero, 0), Other), 0)
3844 // -> (or cond, (icmp ne Other, 0))
3845 Value *Cond, *TV, *FV, *Other, *Sel;
3846 if (C.isZero() &&
3847 match(V: BO,
3848 P: m_OneUse(SubPattern: m_c_Or(L: m_CombineAnd(Ps: m_Value(V&: Sel),
3849 Ps: m_Select(C: m_Value(V&: Cond), L: m_Value(V&: TV),
3850 R: m_Value(V&: FV))),
3851 R: m_Value(V&: Other)))) &&
3852 Cond->getType() == Cmp.getType()) {
3853 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
3854 // Easy case is if eq/ne matches whether 0 is trueval/falseval.
3855 if (Pred == ICmpInst::ICMP_EQ
3856 ? (match(V: TV, P: m_Zero()) && isKnownNonZero(V: FV, Q))
3857 : (match(V: FV, P: m_Zero()) && isKnownNonZero(V: TV, Q))) {
3858 Value *Cmp = Builder.CreateICmp(
3859 P: Pred, LHS: Other, RHS: Constant::getNullValue(Ty: Other->getType()));
3860 return BinaryOperator::Create(
3861 Op: Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, S1: Cmp,
3862 S2: Cond);
3863 }
3864 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3865 // case we need to invert the select condition so we need to be careful to
3866 // avoid creating extra instructions.
3867 // (icmp ne (or (select cond, 0, NonZero), Other), 0)
3868 // -> (or (not cond), (icmp ne Other, 0))
3869 // (icmp eq (or (select cond, NonZero, 0), Other), 0)
3870 // -> (and (not cond), (icmp eq Other, 0))
3871 //
3872 // Only do this if the inner select has one use, in which case we are
3873 // replacing `select` with `(not cond)`. Otherwise, we will create more
3874 // uses. NB: Trying to freely invert cond doesn't make sense here, as if
3875 // cond was freely invertable, the select arms would have been inverted.
3876 if (Sel->hasOneUse() &&
3877 (Pred == ICmpInst::ICMP_EQ
3878 ? (match(V: FV, P: m_Zero()) && isKnownNonZero(V: TV, Q))
3879 : (match(V: TV, P: m_Zero()) && isKnownNonZero(V: FV, Q)))) {
3880 Value *NotCond = Builder.CreateNot(V: Cond);
3881 Value *Cmp = Builder.CreateICmp(
3882 P: Pred, LHS: Other, RHS: Constant::getNullValue(Ty: Other->getType()));
3883 return BinaryOperator::Create(
3884 Op: Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, S1: Cmp,
3885 S2: NotCond);
3886 }
3887 }
3888 break;
3889 }
3890 case Instruction::UDiv:
3891 case Instruction::SDiv:
3892 if (BO->isExact()) {
3893 // div exact X, Y eq/ne 0 -> X eq/ne 0
3894 // div exact X, Y eq/ne 1 -> X eq/ne Y
3895 // div exact X, Y eq/ne C ->
3896 // if Y * C never-overflow && OneUse:
3897 // -> Y * C eq/ne X
3898 if (C.isZero())
3899 return new ICmpInst(Pred, BOp0, Constant::getNullValue(Ty: BO->getType()));
3900 else if (C.isOne())
3901 return new ICmpInst(Pred, BOp0, BOp1);
3902 else if (BO->hasOneUse()) {
3903 OverflowResult OR = computeOverflow(
3904 BinaryOp: Instruction::Mul, IsSigned: BO->getOpcode() == Instruction::SDiv, LHS: BOp1,
3905 RHS: Cmp.getOperand(i_nocapture: 1), CtxI: BO);
3906 if (OR == OverflowResult::NeverOverflows) {
3907 Value *YC =
3908 Builder.CreateMul(LHS: BOp1, RHS: ConstantInt::get(Ty: BO->getType(), V: C));
3909 return new ICmpInst(Pred, YC, BOp0);
3910 }
3911 }
3912 }
3913 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3914 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3915 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3916 return new ICmpInst(NewPred, BOp1, BOp0);
3917 }
3918 break;
3919 default:
3920 break;
3921 }
3922 return nullptr;
3923}
3924
3925static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
3926 const APInt &CRhs,
3927 InstCombiner::BuilderTy &Builder,
3928 const SimplifyQuery &Q) {
3929 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3930 "Non-ctpop intrin in ctpop fold");
3931 if (!CtpopLhs->hasOneUse())
3932 return nullptr;
3933
3934 // Power of 2 test:
3935 // isPow2OrZero : ctpop(X) u< 2
3936 // isPow2 : ctpop(X) == 1
3937 // NotPow2OrZero: ctpop(X) u> 1
3938 // NotPow2 : ctpop(X) != 1
3939 // If we know any bit of X can be folded to:
3940 // IsPow2 : X & (~Bit) == 0
3941 // NotPow2 : X & (~Bit) != 0
3942 const ICmpInst::Predicate Pred = I.getPredicate();
3943 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3944 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3945 Value *Op = CtpopLhs->getArgOperand(i: 0);
3946 KnownBits OpKnown = computeKnownBits(V: Op, DL: Q.DL, AC: Q.AC, CtxI: Q.CtxI, DT: Q.DT);
3947 // No need to check for count > 1, that should be already constant folded.
3948 if (OpKnown.countMinPopulation() == 1) {
3949 Value *And = Builder.CreateAnd(
3950 LHS: Op, RHS: Constant::getIntegerValue(Ty: Op->getType(), V: ~(OpKnown.One)));
3951 return new ICmpInst(
3952 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3953 ? ICmpInst::ICMP_EQ
3954 : ICmpInst::ICMP_NE,
3955 And, Constant::getNullValue(Ty: Op->getType()));
3956 }
3957 }
3958
3959 return nullptr;
3960}
3961
3962/// Fold an equality icmp with LLVM intrinsic and constant operand.
3963Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant(
3964 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3965 Type *Ty = II->getType();
3966 unsigned BitWidth = C.getBitWidth();
3967 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3968
3969 switch (II->getIntrinsicID()) {
3970 case Intrinsic::abs:
3971 // abs(A) == 0 -> A == 0
3972 // abs(A) == INT_MIN -> A == INT_MIN
3973 if (C.isZero() || C.isMinSignedValue())
3974 return new ICmpInst(Pred, II->getArgOperand(i: 0), ConstantInt::get(Ty, V: C));
3975 break;
3976
3977 case Intrinsic::bswap:
3978 // bswap(A) == C -> A == bswap(C)
3979 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3980 ConstantInt::get(Ty, V: C.byteSwap()));
3981
3982 case Intrinsic::bitreverse:
3983 // bitreverse(A) == C -> A == bitreverse(C)
3984 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3985 ConstantInt::get(Ty, V: C.reverseBits()));
3986
3987 case Intrinsic::ctlz:
3988 case Intrinsic::cttz: {
3989 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3990 if (C == BitWidth)
3991 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3992 ConstantInt::getNullValue(Ty));
3993
3994 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3995 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3996 // Limit to one use to ensure we don't increase instruction count.
3997 unsigned Num = C.getLimitedValue(Limit: BitWidth);
3998 if (Num != BitWidth && II->hasOneUse()) {
3999 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
4000 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: Num + 1)
4001 : APInt::getHighBitsSet(numBits: BitWidth, hiBitsSet: Num + 1);
4002 APInt Mask2 = IsTrailing
4003 ? APInt::getOneBitSet(numBits: BitWidth, BitNo: Num)
4004 : APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - Num - 1);
4005 return new ICmpInst(Pred, Builder.CreateAnd(LHS: II->getArgOperand(i: 0), RHS: Mask1),
4006 ConstantInt::get(Ty, V: Mask2));
4007 }
4008 break;
4009 }
4010
4011 case Intrinsic::ctpop: {
4012 // popcount(A) == 0 -> A == 0 and likewise for !=
4013 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
4014 bool IsZero = C.isZero();
4015 if (IsZero || C == BitWidth)
4016 return new ICmpInst(Pred, II->getArgOperand(i: 0),
4017 IsZero ? Constant::getNullValue(Ty)
4018 : Constant::getAllOnesValue(Ty));
4019
4020 break;
4021 }
4022
4023 case Intrinsic::fshl:
4024 case Intrinsic::fshr:
4025 if (II->getArgOperand(i: 0) == II->getArgOperand(i: 1)) {
4026 const APInt *RotAmtC;
4027 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
4028 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
4029 if (match(V: II->getArgOperand(i: 2), P: m_APInt(Res&: RotAmtC)))
4030 return new ICmpInst(Pred, II->getArgOperand(i: 0),
4031 II->getIntrinsicID() == Intrinsic::fshl
4032 ? ConstantInt::get(Ty, V: C.rotr(rotateAmt: *RotAmtC))
4033 : ConstantInt::get(Ty, V: C.rotl(rotateAmt: *RotAmtC)));
4034 }
4035 break;
4036
4037 case Intrinsic::umax:
4038 case Intrinsic::uadd_sat: {
4039 // uadd.sat(a, b) == 0 -> (a | b) == 0
4040 // umax(a, b) == 0 -> (a | b) == 0
4041 if (C.isZero() && II->hasOneUse()) {
4042 Value *Or = Builder.CreateOr(LHS: II->getArgOperand(i: 0), RHS: II->getArgOperand(i: 1));
4043 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
4044 }
4045 break;
4046 }
4047
4048 case Intrinsic::ssub_sat:
4049 // ssub.sat(a, b) == 0 -> a == b
4050 //
4051 // Note this doesn't work for ssub.sat.i1 because ssub.sat.i1 0, -1 = 0
4052 // (because 1 saturates to 0). Just skip the optimization for i1.
4053 if (C.isZero() && II->getType()->getScalarSizeInBits() > 1)
4054 return new ICmpInst(Pred, II->getArgOperand(i: 0), II->getArgOperand(i: 1));
4055 break;
4056 case Intrinsic::usub_sat: {
4057 // usub.sat(a, b) == 0 -> a <= b
4058 if (C.isZero()) {
4059 ICmpInst::Predicate NewPred =
4060 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
4061 return new ICmpInst(NewPred, II->getArgOperand(i: 0), II->getArgOperand(i: 1));
4062 }
4063 break;
4064 }
4065 default:
4066 break;
4067 }
4068
4069 return nullptr;
4070}
4071
4072/// Fold an icmp with LLVM intrinsics
4073static Instruction *
4074foldICmpIntrinsicWithIntrinsic(ICmpInst &Cmp,
4075 InstCombiner::BuilderTy &Builder) {
4076 assert(Cmp.isEquality());
4077
4078 ICmpInst::Predicate Pred = Cmp.getPredicate();
4079 Value *Op0 = Cmp.getOperand(i_nocapture: 0);
4080 Value *Op1 = Cmp.getOperand(i_nocapture: 1);
4081 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Val: Op0);
4082 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Val: Op1);
4083 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
4084 return nullptr;
4085
4086 switch (IIOp0->getIntrinsicID()) {
4087 case Intrinsic::bswap:
4088 case Intrinsic::bitreverse:
4089 // If both operands are byte-swapped or bit-reversed, just compare the
4090 // original values.
4091 return new ICmpInst(Pred, IIOp0->getOperand(i_nocapture: 0), IIOp1->getOperand(i_nocapture: 0));
4092 case Intrinsic::fshl:
4093 case Intrinsic::fshr: {
4094 // If both operands are rotated by same amount, just compare the
4095 // original values.
4096 if (IIOp0->getOperand(i_nocapture: 0) != IIOp0->getOperand(i_nocapture: 1))
4097 break;
4098 if (IIOp1->getOperand(i_nocapture: 0) != IIOp1->getOperand(i_nocapture: 1))
4099 break;
4100 if (IIOp0->getOperand(i_nocapture: 2) == IIOp1->getOperand(i_nocapture: 2))
4101 return new ICmpInst(Pred, IIOp0->getOperand(i_nocapture: 0), IIOp1->getOperand(i_nocapture: 0));
4102
4103 // rotate(X, AmtX) == rotate(Y, AmtY)
4104 // -> rotate(X, AmtX - AmtY) == Y
4105 // Do this if either both rotates have one use or if only one has one use
4106 // and AmtX/AmtY are constants.
4107 const unsigned BW = IIOp0->getType()->getScalarSizeInBits();
4108 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
4109 if (OneUses == 2 ||
4110 (OneUses == 1 && match(V: IIOp0->getOperand(i_nocapture: 2), P: m_ImmConstant()) &&
4111 match(V: IIOp1->getOperand(i_nocapture: 2), P: m_ImmConstant()))) {
4112
4113 // Only valid assuming (2**BW) % BW == 0, which only holds for powers
4114 // of two.
4115 if (isPowerOf2_32(Value: BW)) {
4116 Value *SubAmt =
4117 Builder.CreateSub(LHS: IIOp0->getOperand(i_nocapture: 2), RHS: IIOp1->getOperand(i_nocapture: 2));
4118 Value *CombinedRotate = Builder.CreateIntrinsic(
4119 RetTy: Op0->getType(), ID: IIOp0->getIntrinsicID(),
4120 Args: {IIOp0->getOperand(i_nocapture: 0), IIOp0->getOperand(i_nocapture: 0), SubAmt});
4121 return new ICmpInst(Pred, IIOp1->getOperand(i_nocapture: 0), CombinedRotate);
4122 }
4123 }
4124 } break;
4125 default:
4126 break;
4127 }
4128
4129 return nullptr;
4130}
4131
4132/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
4133/// where X is some kind of instruction and C is AllowPoison.
4134/// TODO: Move more folds which allow poison to this function.
4135Instruction *
4136InstCombinerImpl::foldICmpInstWithConstantAllowPoison(ICmpInst &Cmp,
4137 const APInt &C) {
4138 const ICmpInst::Predicate Pred = Cmp.getPredicate();
4139 if (auto *II = dyn_cast<IntrinsicInst>(Val: Cmp.getOperand(i_nocapture: 0))) {
4140 switch (II->getIntrinsicID()) {
4141 default:
4142 break;
4143 case Intrinsic::fshl:
4144 case Intrinsic::fshr:
4145 if (Cmp.isEquality() && II->getArgOperand(i: 0) == II->getArgOperand(i: 1)) {
4146 // (rot X, ?) == 0/-1 --> X == 0/-1
4147 if (C.isZero() || C.isAllOnes())
4148 return new ICmpInst(Pred, II->getArgOperand(i: 0), Cmp.getOperand(i_nocapture: 1));
4149 }
4150 break;
4151 }
4152 }
4153
4154 return nullptr;
4155}
4156
4157/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
4158Instruction *InstCombinerImpl::foldICmpBinOpWithConstant(ICmpInst &Cmp,
4159 BinaryOperator *BO,
4160 const APInt &C) {
4161 switch (BO->getOpcode()) {
4162 case Instruction::Xor:
4163 if (Instruction *I = foldICmpXorConstant(Cmp, Xor: BO, C))
4164 return I;
4165 break;
4166 case Instruction::And:
4167 if (Instruction *I = foldICmpAndConstant(Cmp, And: BO, C))
4168 return I;
4169 break;
4170 case Instruction::Or:
4171 if (Instruction *I = foldICmpOrConstant(Cmp, Or: BO, C))
4172 return I;
4173 break;
4174 case Instruction::Mul:
4175 if (Instruction *I = foldICmpMulConstant(Cmp, Mul: BO, C))
4176 return I;
4177 break;
4178 case Instruction::Shl:
4179 if (Instruction *I = foldICmpShlConstant(Cmp, Shl: BO, C))
4180 return I;
4181 break;
4182 case Instruction::LShr:
4183 case Instruction::AShr:
4184 if (Instruction *I = foldICmpShrConstant(Cmp, Shr: BO, C))
4185 return I;
4186 break;
4187 case Instruction::SRem:
4188 if (Instruction *I = foldICmpSRemConstant(Cmp, SRem: BO, C))
4189 return I;
4190 break;
4191 case Instruction::UDiv:
4192 if (Instruction *I = foldICmpUDivConstant(Cmp, UDiv: BO, C))
4193 return I;
4194 [[fallthrough]];
4195 case Instruction::SDiv:
4196 if (Instruction *I = foldICmpDivConstant(Cmp, Div: BO, C))
4197 return I;
4198 break;
4199 case Instruction::Sub:
4200 if (Instruction *I = foldICmpSubConstant(Cmp, Sub: BO, C))
4201 return I;
4202 break;
4203 case Instruction::Add:
4204 if (Instruction *I = foldICmpAddConstant(Cmp, Add: BO, C))
4205 return I;
4206 break;
4207 default:
4208 break;
4209 }
4210
4211 // TODO: These folds could be refactored to be part of the above calls.
4212 if (Instruction *I = foldICmpBinOpEqualityWithConstant(Cmp, BO, C))
4213 return I;
4214
4215 // Fall back to handling `icmp pred (select A ? C1 : C2) binop (select B ? C3
4216 // : C4), C5` pattern, by computing a truth table of the four constant
4217 // variants.
4218 return foldICmpBinOpWithConstantViaTruthTable(Cmp, BO, C);
4219}
4220
4221static Instruction *
4222foldICmpUSubSatOrUAddSatWithConstant(CmpPredicate Pred, SaturatingInst *II,
4223 const APInt &C,
4224 InstCombiner::BuilderTy &Builder) {
4225 // This transform may end up producing more than one instruction for the
4226 // intrinsic, so limit it to one user of the intrinsic.
4227 if (!II->hasOneUse())
4228 return nullptr;
4229
4230 // Let Y = [add/sub]_sat(X, C) pred C2
4231 // SatVal = The saturating value for the operation
4232 // WillWrap = Whether or not the operation will underflow / overflow
4233 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
4234 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
4235 //
4236 // When (SatVal pred C2) is true, then
4237 // Y = WillWrap ? true : ((X binop C) pred C2)
4238 // => Y = WillWrap || ((X binop C) pred C2)
4239 // else
4240 // Y = WillWrap ? false : ((X binop C) pred C2)
4241 // => Y = !WillWrap ? ((X binop C) pred C2) : false
4242 // => Y = !WillWrap && ((X binop C) pred C2)
4243 Value *Op0 = II->getOperand(i_nocapture: 0);
4244 Value *Op1 = II->getOperand(i_nocapture: 1);
4245
4246 const APInt *COp1;
4247 // This transform only works when the intrinsic has an integral constant or
4248 // splat vector as the second operand.
4249 if (!match(V: Op1, P: m_APInt(Res&: COp1)))
4250 return nullptr;
4251
4252 APInt SatVal;
4253 switch (II->getIntrinsicID()) {
4254 default:
4255 llvm_unreachable(
4256 "This function only works with usub_sat and uadd_sat for now!");
4257 case Intrinsic::uadd_sat:
4258 SatVal = APInt::getAllOnes(numBits: C.getBitWidth());
4259 break;
4260 case Intrinsic::usub_sat:
4261 SatVal = APInt::getZero(numBits: C.getBitWidth());
4262 break;
4263 }
4264
4265 // Check (SatVal pred C2)
4266 bool SatValCheck = ICmpInst::compare(LHS: SatVal, RHS: C, Pred);
4267
4268 // !WillWrap.
4269 ConstantRange C1 = ConstantRange::makeExactNoWrapRegion(
4270 BinOp: II->getBinaryOp(), Other: *COp1, NoWrapKind: II->getNoWrapKind());
4271
4272 // WillWrap.
4273 if (SatValCheck)
4274 C1 = C1.inverse();
4275
4276 ConstantRange C2 = ConstantRange::makeExactICmpRegion(Pred, Other: C);
4277 if (II->getBinaryOp() == Instruction::Add)
4278 C2 = C2.sub(Other: *COp1);
4279 else
4280 C2 = C2.add(Other: *COp1);
4281
4282 Instruction::BinaryOps CombiningOp =
4283 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
4284
4285 std::optional<ConstantRange> Combination;
4286 if (CombiningOp == Instruction::BinaryOps::Or)
4287 Combination = C1.exactUnionWith(CR: C2);
4288 else /* CombiningOp == Instruction::BinaryOps::And */
4289 Combination = C1.exactIntersectWith(CR: C2);
4290
4291 if (!Combination)
4292 return nullptr;
4293
4294 CmpInst::Predicate EquivPred;
4295 APInt EquivInt;
4296 APInt EquivOffset;
4297
4298 Combination->getEquivalentICmp(Pred&: EquivPred, RHS&: EquivInt, Offset&: EquivOffset);
4299
4300 return new ICmpInst(
4301 EquivPred,
4302 Builder.CreateAdd(LHS: Op0, RHS: ConstantInt::get(Ty: Op1->getType(), V: EquivOffset)),
4303 ConstantInt::get(Ty: Op1->getType(), V: EquivInt));
4304}
4305
4306static Instruction *
4307foldICmpOfCmpIntrinsicWithConstant(CmpPredicate Pred, IntrinsicInst *I,
4308 const APInt &C,
4309 InstCombiner::BuilderTy &Builder) {
4310 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
4311 switch (Pred) {
4312 case ICmpInst::ICMP_EQ:
4313 case ICmpInst::ICMP_NE:
4314 if (C.isZero())
4315 NewPredicate = Pred;
4316 else if (C.isOne())
4317 NewPredicate =
4318 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE;
4319 else if (C.isAllOnes())
4320 NewPredicate =
4321 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
4322 break;
4323
4324 case ICmpInst::ICMP_SGT:
4325 if (C.isAllOnes())
4326 NewPredicate = ICmpInst::ICMP_UGE;
4327 else if (C.isZero())
4328 NewPredicate = ICmpInst::ICMP_UGT;
4329 break;
4330
4331 case ICmpInst::ICMP_SLT:
4332 if (C.isZero())
4333 NewPredicate = ICmpInst::ICMP_ULT;
4334 else if (C.isOne())
4335 NewPredicate = ICmpInst::ICMP_ULE;
4336 break;
4337
4338 case ICmpInst::ICMP_ULT:
4339 if (C.ugt(RHS: 1))
4340 NewPredicate = ICmpInst::ICMP_UGE;
4341 break;
4342
4343 case ICmpInst::ICMP_UGT:
4344 if (!C.isZero() && !C.isAllOnes())
4345 NewPredicate = ICmpInst::ICMP_ULT;
4346 break;
4347
4348 default:
4349 break;
4350 }
4351
4352 if (!NewPredicate)
4353 return nullptr;
4354
4355 if (I->getIntrinsicID() == Intrinsic::scmp)
4356 NewPredicate = ICmpInst::getSignedPredicate(Pred: *NewPredicate);
4357 Value *LHS = I->getOperand(i_nocapture: 0);
4358 Value *RHS = I->getOperand(i_nocapture: 1);
4359 return new ICmpInst(*NewPredicate, LHS, RHS);
4360}
4361
4362/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
4363Instruction *InstCombinerImpl::foldICmpIntrinsicWithConstant(ICmpInst &Cmp,
4364 IntrinsicInst *II,
4365 const APInt &C) {
4366 ICmpInst::Predicate Pred = Cmp.getPredicate();
4367
4368 // Handle folds that apply for any kind of icmp.
4369 switch (II->getIntrinsicID()) {
4370 default:
4371 break;
4372 case Intrinsic::uadd_sat:
4373 case Intrinsic::usub_sat:
4374 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
4375 Pred, II: cast<SaturatingInst>(Val: II), C, Builder))
4376 return Folded;
4377 break;
4378 case Intrinsic::ctpop: {
4379 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
4380 if (Instruction *R = foldCtpopPow2Test(I&: Cmp, CtpopLhs: II, CRhs: C, Builder, Q))
4381 return R;
4382 } break;
4383 case Intrinsic::scmp:
4384 case Intrinsic::ucmp:
4385 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, I: II, C, Builder))
4386 return Folded;
4387 break;
4388 }
4389
4390 if (Cmp.isEquality())
4391 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
4392
4393 Type *Ty = II->getType();
4394 unsigned BitWidth = C.getBitWidth();
4395 switch (II->getIntrinsicID()) {
4396 case Intrinsic::ctpop: {
4397 // (ctpop X > BitWidth - 1) --> X == -1
4398 Value *X = II->getArgOperand(i: 0);
4399 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
4400 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_EQ, S1: X,
4401 S2: ConstantInt::getAllOnesValue(Ty));
4402 // (ctpop X < BitWidth) --> X != -1
4403 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
4404 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_NE, S1: X,
4405 S2: ConstantInt::getAllOnesValue(Ty));
4406 break;
4407 }
4408 case Intrinsic::ctlz: {
4409 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
4410 if (Pred == ICmpInst::ICMP_UGT && C.ult(RHS: BitWidth)) {
4411 unsigned Num = C.getLimitedValue();
4412 APInt Limit = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - Num - 1);
4413 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_ULT,
4414 S1: II->getArgOperand(i: 0), S2: ConstantInt::get(Ty, V: Limit));
4415 }
4416
4417 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
4418 if (Pred == ICmpInst::ICMP_ULT && C.uge(RHS: 1) && C.ule(RHS: BitWidth)) {
4419 unsigned Num = C.getLimitedValue();
4420 APInt Limit = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: BitWidth - Num);
4421 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_UGT,
4422 S1: II->getArgOperand(i: 0), S2: ConstantInt::get(Ty, V: Limit));
4423 }
4424 break;
4425 }
4426 case Intrinsic::cttz: {
4427 // Limit to one use to ensure we don't increase instruction count.
4428 if (!II->hasOneUse())
4429 return nullptr;
4430
4431 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
4432 if (Pred == ICmpInst::ICMP_UGT && C.ult(RHS: BitWidth)) {
4433 APInt Mask = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: C.getLimitedValue() + 1);
4434 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_EQ,
4435 S1: Builder.CreateAnd(LHS: II->getArgOperand(i: 0), RHS: Mask),
4436 S2: ConstantInt::getNullValue(Ty));
4437 }
4438
4439 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
4440 if (Pred == ICmpInst::ICMP_ULT && C.uge(RHS: 1) && C.ule(RHS: BitWidth)) {
4441 APInt Mask = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: C.getLimitedValue());
4442 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_NE,
4443 S1: Builder.CreateAnd(LHS: II->getArgOperand(i: 0), RHS: Mask),
4444 S2: ConstantInt::getNullValue(Ty));
4445 }
4446 break;
4447 }
4448 case Intrinsic::ssub_sat:
4449 // ssub.sat(a, b) spred 0 -> a spred b
4450 //
4451 // Note this doesn't work for ssub.sat.i1 because ssub.sat.i1 0, -1 = 0
4452 // (because 1 saturates to 0). Just skip the optimization for i1.
4453 if (ICmpInst::isSigned(Pred) && C.getBitWidth() > 1) {
4454 if (C.isZero())
4455 return new ICmpInst(Pred, II->getArgOperand(i: 0), II->getArgOperand(i: 1));
4456 // X s<= 0 is cannonicalized to X s< 1
4457 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
4458 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(i: 0),
4459 II->getArgOperand(i: 1));
4460 // X s>= 0 is cannonicalized to X s> -1
4461 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
4462 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(i: 0),
4463 II->getArgOperand(i: 1));
4464 }
4465 break;
4466 case Intrinsic::abs: {
4467 if (!II->hasOneUse())
4468 return nullptr;
4469
4470 Value *X = II->getArgOperand(i: 0);
4471
4472 // If C >= 0:
4473 // abs(X) u> C --> X + C u> 2 * C
4474 if (Pred == CmpInst::ICMP_UGT && C.isNonNegative()) {
4475 return new ICmpInst(ICmpInst::ICMP_UGT,
4476 Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty, V: C)),
4477 ConstantInt::get(Ty, V: 2 * C));
4478 }
4479
4480 // If C >= 1:
4481 // abs(X) u< C --> X + (C - 1) u<= 2 * (C - 1)
4482 if (Pred == CmpInst::ICMP_ULT && C.sge(RHS: 1))
4483 return new ICmpInst(ICmpInst::ICMP_ULE,
4484 Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty, V: C - 1)),
4485 ConstantInt::get(Ty, V: 2 * (C - 1)));
4486
4487 break;
4488 }
4489 default:
4490 break;
4491 }
4492
4493 return nullptr;
4494}
4495
4496/// Handle icmp with constant (but not simple integer constant) RHS.
4497Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {
4498 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
4499 Constant *RHSC = dyn_cast<Constant>(Val: Op1);
4500 Instruction *LHSI = dyn_cast<Instruction>(Val: Op0);
4501 if (!RHSC || !LHSI)
4502 return nullptr;
4503
4504 switch (LHSI->getOpcode()) {
4505 // umax(X, Y) == null -> (X == null) && (Y == null).
4506 // umax(X, Y) != null -> (X != null) || (Y != null).
4507 case Instruction::Select: {
4508 CmpInst::Predicate Pred = I.getPredicate();
4509 if (!ICmpInst::isEquality(P: Pred) || !RHSC->getType()->isPtrOrPtrVectorTy())
4510 break;
4511
4512 // Require zero address bits, the minimum unsigned pointer value.
4513 Constant *RHSIndexBits = ConstantFoldCastOperand(
4514 Opcode: Instruction::PtrToAddr, C: RHSC, DestTy: DL.getIndexType(PtrTy: RHSC->getType()), DL);
4515 if (!RHSIndexBits || !RHSIndexBits->isNullValue())
4516 break;
4517
4518 auto *SI = cast<SelectInst>(Val: LHSI);
4519 // Both the select and its comparison must die to avoid adding instructions.
4520 if (!SI->hasOneUse() || !SI->getCondition()->hasOneUse())
4521 break;
4522
4523 // Require the condition to compare the select operands directly.
4524 Value *A = SI->getTrueValue();
4525 Value *B = SI->getFalseValue();
4526 CmpPredicate SelectPred;
4527 if (!match(V: SI->getCondition(),
4528 P: m_c_ICmp(Pred&: SelectPred, L: m_Specific(V: A), R: m_Specific(V: B))) ||
4529 (SelectPred != ICmpInst::ICMP_UGT && SelectPred != ICmpInst::ICMP_UGE))
4530 break;
4531
4532 Value *ACmp = Builder.CreateICmp(P: Pred, LHS: A, RHS: RHSC);
4533 Value *BCmp = Builder.CreateICmp(P: Pred, LHS: B, RHS: RHSC);
4534 // The original icmp propagates poison from both operands; and/or is safe.
4535 return BinaryOperator::Create(Op: Pred == ICmpInst::ICMP_EQ ? Instruction::And
4536 : Instruction::Or,
4537 S1: ACmp, S2: BCmp);
4538 }
4539
4540 case Instruction::IntToPtr:
4541 // icmp pred inttoptr(X), null -> icmp pred X, null pointer value
4542 if (isa<ConstantPointerNull>(Val: RHSC)) {
4543 Type *IntPtrTy = DL.getIntPtrType(RHSC->getType());
4544 if (IntPtrTy == LHSI->getOperand(i: 0)->getType()) {
4545 APInt NullPtrValue =
4546 DL.getNullPtrValue(AS: RHSC->getType()->getPointerAddressSpace());
4547 return new ICmpInst(I.getPredicate(), LHSI->getOperand(i: 0),
4548 Constant::getIntegerValue(Ty: IntPtrTy, V: NullPtrValue));
4549 }
4550 }
4551 break;
4552
4553 case Instruction::Load:
4554 // Try to optimize things like "A[i] > 4" to index computations.
4555 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(
4556 LI: cast<LoadInst>(Val: LHSI), Pred: I.getPredicate(), RHSC, CtxI&: I))
4557 return Res;
4558 break;
4559 }
4560
4561 return nullptr;
4562}
4563
4564Instruction *InstCombinerImpl::foldSelectICmp(CmpPredicate Pred, SelectInst *SI,
4565 Value *RHS, const ICmpInst &I) {
4566 // Try to fold the comparison into the select arms, which will cause the
4567 // select to be converted into a logical and/or.
4568 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4569 if (Value *Res = simplifyICmpInst(Pred, LHS: Op, RHS, Q: SQ))
4570 return Res;
4571 if (std::optional<bool> Impl = isImpliedCondition(
4572 LHS: SI->getCondition(), RHSPred: Pred, RHSOp0: Op, RHSOp1: RHS, DL, LHSIsTrue: SelectCondIsTrue))
4573 return ConstantInt::get(Ty: I.getType(), V: *Impl);
4574 return nullptr;
4575 };
4576
4577 ConstantInt *CI = nullptr;
4578 Value *Op1 = SimplifyOp(SI->getOperand(i_nocapture: 1), true);
4579 if (Op1)
4580 CI = dyn_cast<ConstantInt>(Val: Op1);
4581
4582 Value *Op2 = SimplifyOp(SI->getOperand(i_nocapture: 2), false);
4583 if (Op2)
4584 CI = dyn_cast<ConstantInt>(Val: Op2);
4585
4586 auto Simplifies = [&](Value *Op, unsigned Idx) {
4587 // A comparison of ucmp/scmp with a constant will fold into an icmp.
4588 const APInt *Dummy;
4589 return Op ||
4590 (isa<CmpIntrinsic>(Val: SI->getOperand(i_nocapture: Idx)) &&
4591 SI->getOperand(i_nocapture: Idx)->hasOneUse() && match(V: RHS, P: m_APInt(Res&: Dummy)));
4592 };
4593
4594 // We only want to perform this transformation if it will not lead to
4595 // additional code. This is true if either both sides of the select
4596 // fold to a constant (in which case the icmp is replaced with a select
4597 // which will usually simplify) or this is the only user of the
4598 // select (in which case we are trading a select+icmp for a simpler
4599 // select+icmp) or all uses of the select can be replaced based on
4600 // dominance information ("Global cases").
4601 bool Transform = false;
4602 if (Op1 && Op2)
4603 Transform = true;
4604 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {
4605 // Local case
4606 if (SI->hasOneUse())
4607 Transform = true;
4608 // Global cases
4609 else if (CI && !CI->isZero())
4610 // When Op1 is constant try replacing select with second operand.
4611 // Otherwise Op2 is constant and try replacing select with first
4612 // operand.
4613 Transform = replacedSelectWithOperand(SI, Icmp: &I, SIOpd: Op1 ? 2 : 1);
4614 }
4615 if (Transform) {
4616 if (!Op1)
4617 Op1 = Builder.CreateICmp(P: Pred, LHS: SI->getOperand(i_nocapture: 1), RHS, Name: I.getName());
4618 if (!Op2)
4619 Op2 = Builder.CreateICmp(P: Pred, LHS: SI->getOperand(i_nocapture: 2), RHS, Name: I.getName());
4620 return SelectInst::Create(C: SI->getOperand(i_nocapture: 0), S1: Op1, S2: Op2, NameStr: "", InsertBefore: nullptr, MDFrom: SI);
4621 }
4622
4623 // Fold icmp eq/ne X, select(icmp pred X, P, C1, C2)
4624 // When the select condition compares X with a constant P and the select
4625 // arms are constants C1/C2, we can fold to a set membership test.
4626 // Example: X == select(X >s 0, 2, 0) -> (X == 2) | (X == 0)
4627 // This is valid when C1 satisfies the condition (C1 >s 0) and C2 does not.
4628 if (ICmpInst::isEquality(P: Pred)) {
4629 CmpPredicate CondPred;
4630 const APInt *C1, *C2, *P;
4631 if (match(V: SI,
4632 P: m_OneUse(SubPattern: m_Select(C: m_ICmp(Pred&: CondPred, L: m_Specific(V: RHS), R: m_APInt(Res&: P)),
4633 L: m_APInt(Res&: C1), R: m_APInt(Res&: C2))))) {
4634 bool C1SatisfiesCond = ICmpInst::compare(LHS: *C1, RHS: *P, Pred: CondPred);
4635 bool C2SatisfiesCond = ICmpInst::compare(LHS: *C2, RHS: *P, Pred: CondPred);
4636
4637 if (C1SatisfiesCond && !C2SatisfiesCond) {
4638 // X == select(cond, C1, C2) -> (X == C1) | (X == C2)
4639 // X != select(cond, C1, C2) -> (X != C1) & (X != C2)
4640 Value *Cmp1 = Builder.CreateICmp(P: Pred, LHS: RHS, RHS: SI->getTrueValue());
4641 Value *Cmp2 = Builder.CreateICmp(P: Pred, LHS: RHS, RHS: SI->getFalseValue());
4642 if (Pred == ICmpInst::ICMP_EQ)
4643 return BinaryOperator::CreateOr(V1: Cmp1, V2: Cmp2);
4644 return BinaryOperator::CreateAnd(V1: Cmp1, V2: Cmp2);
4645 }
4646 }
4647 }
4648
4649 return nullptr;
4650}
4651
4652// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4653static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4654 unsigned Depth = 0) {
4655 if (Not ? match(V, P: m_NegatedPower2OrZero()) : match(V, P: m_LowBitMaskOrZero()))
4656 return true;
4657 if (V->getType()->getScalarSizeInBits() == 1)
4658 return true;
4659 if (Depth++ >= MaxAnalysisRecursionDepth)
4660 return false;
4661 Value *X;
4662 const Instruction *I = dyn_cast<Instruction>(Val: V);
4663 if (!I)
4664 return false;
4665 switch (I->getOpcode()) {
4666 case Instruction::ZExt:
4667 // ZExt(Mask) is a Mask.
4668 return !Not && isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4669 case Instruction::SExt:
4670 // SExt(Mask) is a Mask.
4671 // SExt(~Mask) is a ~Mask.
4672 return isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4673 case Instruction::And:
4674 case Instruction::Or:
4675 // Mask0 | Mask1 is a Mask.
4676 // Mask0 & Mask1 is a Mask.
4677 // ~Mask0 | ~Mask1 is a ~Mask.
4678 // ~Mask0 & ~Mask1 is a ~Mask.
4679 return isMaskOrZero(V: I->getOperand(i: 1), Not, Q, Depth) &&
4680 isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4681 case Instruction::Xor:
4682 if (match(V, P: m_Not(V: m_Value(V&: X))))
4683 return isMaskOrZero(V: X, Not: !Not, Q, Depth);
4684
4685 // (X ^ -X) is a ~Mask
4686 if (Not)
4687 return match(V, P: m_c_Xor(L: m_Value(V&: X), R: m_Neg(V: m_Deferred(V: X))));
4688 // (X ^ (X - 1)) is a Mask
4689 else
4690 return match(V, P: m_c_Xor(L: m_Value(V&: X), R: m_Add(L: m_Deferred(V: X), R: m_AllOnes())));
4691 case Instruction::Select:
4692 // c ? Mask0 : Mask1 is a Mask.
4693 return isMaskOrZero(V: I->getOperand(i: 1), Not, Q, Depth) &&
4694 isMaskOrZero(V: I->getOperand(i: 2), Not, Q, Depth);
4695 case Instruction::Shl:
4696 // (~Mask) << X is a ~Mask.
4697 return Not && isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4698 case Instruction::LShr:
4699 // Mask >> X is a Mask.
4700 return !Not && isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4701 case Instruction::AShr:
4702 // Mask s>> X is a Mask.
4703 // ~Mask s>> X is a ~Mask.
4704 return isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4705 case Instruction::Add:
4706 // Pow2 - 1 is a Mask.
4707 if (!Not && match(V: I->getOperand(i: 1), P: m_AllOnes()))
4708 return isKnownToBeAPowerOfTwo(V: I->getOperand(i: 0), DL: Q.DL, /*OrZero*/ true,
4709 AC: Q.AC, CtxI: Q.CtxI, DT: Q.DT, UseInstrInfo: Depth);
4710 break;
4711 case Instruction::Sub:
4712 // -Pow2 is a ~Mask.
4713 if (Not && match(V: I->getOperand(i: 0), P: m_Zero()))
4714 return isKnownToBeAPowerOfTwo(V: I->getOperand(i: 1), DL: Q.DL, /*OrZero*/ true,
4715 AC: Q.AC, CtxI: Q.CtxI, DT: Q.DT, UseInstrInfo: Depth);
4716 break;
4717 case Instruction::Call: {
4718 if (auto *II = dyn_cast<IntrinsicInst>(Val: I)) {
4719 switch (II->getIntrinsicID()) {
4720 // min/max(Mask0, Mask1) is a Mask.
4721 // min/max(~Mask0, ~Mask1) is a ~Mask.
4722 case Intrinsic::umax:
4723 case Intrinsic::smax:
4724 case Intrinsic::umin:
4725 case Intrinsic::smin:
4726 return isMaskOrZero(V: II->getArgOperand(i: 1), Not, Q, Depth) &&
4727 isMaskOrZero(V: II->getArgOperand(i: 0), Not, Q, Depth);
4728
4729 // In the context of masks, bitreverse(Mask) == ~Mask
4730 case Intrinsic::bitreverse:
4731 return isMaskOrZero(V: II->getArgOperand(i: 0), Not: !Not, Q, Depth);
4732 default:
4733 break;
4734 }
4735 }
4736 break;
4737 }
4738 default:
4739 break;
4740 }
4741 return false;
4742}
4743
4744/// Some comparisons can be simplified.
4745/// In this case, we are looking for comparisons that look like
4746/// a check for a lossy truncation.
4747/// Folds:
4748/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4749/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4750/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4751/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4752/// Where Mask is some pattern that produces all-ones in low bits:
4753/// (-1 >> y)
4754/// ((-1 << y) >> y) <- non-canonical, has extra uses
4755/// ~(-1 << y)
4756/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4757/// The Mask can be a constant, too.
4758/// For some predicates, the operands are commutative.
4759/// For others, x can only be on a specific side.
4760static Value *foldICmpWithLowBitMaskedVal(CmpPredicate Pred, Value *Op0,
4761 Value *Op1, const SimplifyQuery &Q,
4762 InstCombiner &IC) {
4763
4764 ICmpInst::Predicate DstPred;
4765 switch (Pred) {
4766 case ICmpInst::Predicate::ICMP_EQ:
4767 // x & Mask == x
4768 // x & ~Mask == 0
4769 // ~x | Mask == -1
4770 // -> x u<= Mask
4771 // x & ~Mask == ~Mask
4772 // -> ~Mask u<= x
4773 DstPred = ICmpInst::Predicate::ICMP_ULE;
4774 break;
4775 case ICmpInst::Predicate::ICMP_NE:
4776 // x & Mask != x
4777 // x & ~Mask != 0
4778 // ~x | Mask != -1
4779 // -> x u> Mask
4780 // x & ~Mask != ~Mask
4781 // -> ~Mask u> x
4782 DstPred = ICmpInst::Predicate::ICMP_UGT;
4783 break;
4784 case ICmpInst::Predicate::ICMP_ULT:
4785 // x & Mask u< x
4786 // -> x u> Mask
4787 // x & ~Mask u< ~Mask
4788 // -> ~Mask u> x
4789 DstPred = ICmpInst::Predicate::ICMP_UGT;
4790 break;
4791 case ICmpInst::Predicate::ICMP_UGE:
4792 // x & Mask u>= x
4793 // -> x u<= Mask
4794 // x & ~Mask u>= ~Mask
4795 // -> ~Mask u<= x
4796 DstPred = ICmpInst::Predicate::ICMP_ULE;
4797 break;
4798 case ICmpInst::Predicate::ICMP_SLT:
4799 // x & Mask s< x [iff Mask s>= 0]
4800 // -> x s> Mask
4801 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4802 // -> ~Mask s> x
4803 DstPred = ICmpInst::Predicate::ICMP_SGT;
4804 break;
4805 case ICmpInst::Predicate::ICMP_SGE:
4806 // x & Mask s>= x [iff Mask s>= 0]
4807 // -> x s<= Mask
4808 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4809 // -> ~Mask s<= x
4810 DstPred = ICmpInst::Predicate::ICMP_SLE;
4811 break;
4812 default:
4813 // We don't support sgt,sle
4814 // ult/ugt are simplified to true/false respectively.
4815 return nullptr;
4816 }
4817
4818 Value *X, *M;
4819 // Put search code in lambda for early positive returns.
4820 auto IsLowBitMask = [&]() {
4821 if (match(V: Op0, P: m_c_And(L: m_Specific(V: Op1), R: m_Value(V&: M)))) {
4822 X = Op1;
4823 // Look for: x & Mask pred x
4824 if (isMaskOrZero(V: M, /*Not=*/false, Q)) {
4825 return !ICmpInst::isSigned(Pred) ||
4826 (match(V: M, P: m_NonNegative()) || isKnownNonNegative(V: M, SQ: Q));
4827 }
4828
4829 // Look for: x & ~Mask pred ~Mask
4830 if (isMaskOrZero(V: X, /*Not=*/true, Q)) {
4831 return !ICmpInst::isSigned(Pred) || isKnownNonZero(V: X, Q);
4832 }
4833 return false;
4834 }
4835 if (ICmpInst::isEquality(P: Pred) && match(V: Op1, P: m_AllOnes()) &&
4836 match(V: Op0, P: m_OneUse(SubPattern: m_Or(L: m_Value(V&: X), R: m_Value(V&: M))))) {
4837
4838 auto Check = [&]() {
4839 // Look for: ~x | Mask == -1
4840 if (isMaskOrZero(V: M, /*Not=*/false, Q)) {
4841 if (Value *NotX =
4842 IC.getFreelyInverted(V: X, WillInvertAllUses: X->hasOneUse(), Builder: &IC.Builder)) {
4843 X = NotX;
4844 return true;
4845 }
4846 }
4847 return false;
4848 };
4849 if (Check())
4850 return true;
4851 std::swap(a&: X, b&: M);
4852 return Check();
4853 }
4854 if (ICmpInst::isEquality(P: Pred) && match(V: Op1, P: m_Zero()) &&
4855 match(V: Op0, P: m_OneUse(SubPattern: m_And(L: m_Value(V&: X), R: m_Value(V&: M))))) {
4856 auto Check = [&]() {
4857 // Look for: x & ~Mask == 0
4858 if (isMaskOrZero(V: M, /*Not=*/true, Q)) {
4859 if (Value *NotM =
4860 IC.getFreelyInverted(V: M, WillInvertAllUses: M->hasOneUse(), Builder: &IC.Builder)) {
4861 M = NotM;
4862 return true;
4863 }
4864 }
4865 return false;
4866 };
4867 if (Check())
4868 return true;
4869 std::swap(a&: X, b&: M);
4870 return Check();
4871 }
4872 return false;
4873 };
4874
4875 if (!IsLowBitMask())
4876 return nullptr;
4877
4878 return IC.Builder.CreateICmp(P: DstPred, LHS: X, RHS: M);
4879}
4880
4881/// Some comparisons can be simplified.
4882/// In this case, we are looking for comparisons that look like
4883/// a check for a lossy signed truncation.
4884/// Folds: (MaskedBits is a constant.)
4885/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4886/// Into:
4887/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4888/// Where KeptBits = bitwidth(%x) - MaskedBits
4889static Value *
4890foldICmpWithTruncSignExtendedVal(ICmpInst &I,
4891 InstCombiner::BuilderTy &Builder) {
4892 CmpPredicate SrcPred;
4893 Value *X;
4894 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4895 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4896 if (!match(V: &I, P: m_c_ICmp(Pred&: SrcPred,
4897 L: m_OneUse(SubPattern: m_AShr(L: m_Shl(L: m_Value(V&: X), R: m_APInt(Res&: C0)),
4898 R: m_APInt(Res&: C1))),
4899 R: m_Deferred(V: X))))
4900 return nullptr;
4901
4902 // Potential handling of non-splats: for each element:
4903 // * if both are undef, replace with constant 0.
4904 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4905 // * if both are not undef, and are different, bailout.
4906 // * else, only one is undef, then pick the non-undef one.
4907
4908 // The shift amount must be equal.
4909 if (*C0 != *C1)
4910 return nullptr;
4911 const APInt &MaskedBits = *C0;
4912 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4913
4914 ICmpInst::Predicate DstPred;
4915 switch (SrcPred) {
4916 case ICmpInst::Predicate::ICMP_EQ:
4917 // ((%x << MaskedBits) a>> MaskedBits) == %x
4918 // =>
4919 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4920 DstPred = ICmpInst::Predicate::ICMP_ULT;
4921 break;
4922 case ICmpInst::Predicate::ICMP_NE:
4923 // ((%x << MaskedBits) a>> MaskedBits) != %x
4924 // =>
4925 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4926 DstPred = ICmpInst::Predicate::ICMP_UGE;
4927 break;
4928 // FIXME: are more folds possible?
4929 default:
4930 return nullptr;
4931 }
4932
4933 auto *XType = X->getType();
4934 const unsigned XBitWidth = XType->getScalarSizeInBits();
4935 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4936 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4937
4938 // KeptBits = bitwidth(%x) - MaskedBits
4939 const APInt KeptBits = BitWidth - MaskedBits;
4940 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4941 // ICmpCst = (1 << KeptBits)
4942 const APInt ICmpCst = APInt(XBitWidth, 1).shl(ShiftAmt: KeptBits);
4943 assert(ICmpCst.isPowerOf2());
4944 // AddCst = (1 << (KeptBits-1))
4945 const APInt AddCst = ICmpCst.lshr(shiftAmt: 1);
4946 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4947
4948 // T0 = add %x, AddCst
4949 Value *T0 = Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty: XType, V: AddCst));
4950 // T1 = T0 DstPred ICmpCst
4951 Value *T1 = Builder.CreateICmp(P: DstPred, LHS: T0, RHS: ConstantInt::get(Ty: XType, V: ICmpCst));
4952
4953 return T1;
4954}
4955
4956// Given pattern:
4957// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4958// we should move shifts to the same hand of 'and', i.e. rewrite as
4959// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4960// We are only interested in opposite logical shifts here.
4961// One of the shifts can be truncated.
4962// If we can, we want to end up creating 'lshr' shift.
4963static Value *
4964foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ,
4965 InstCombiner::BuilderTy &Builder) {
4966 if (!I.isEquality() || !match(V: I.getOperand(i_nocapture: 1), P: m_Zero()) ||
4967 !I.getOperand(i_nocapture: 0)->hasOneUse())
4968 return nullptr;
4969
4970 auto m_AnyLogicalShift = m_LogicalShift(L: m_Value(), R: m_Value());
4971
4972 // Look for an 'and' of two logical shifts, one of which may be truncated.
4973 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4974 Instruction *XShift, *MaybeTruncation, *YShift;
4975 if (!match(
4976 V: I.getOperand(i_nocapture: 0),
4977 P: m_c_And(L: m_CombineAnd(Ps: m_AnyLogicalShift, Ps: m_Instruction(I&: XShift)),
4978 R: m_CombineAnd(Ps: m_TruncOrSelf(Op: m_CombineAnd(
4979 Ps: m_AnyLogicalShift, Ps: m_Instruction(I&: YShift))),
4980 Ps: m_Instruction(I&: MaybeTruncation)))))
4981 return nullptr;
4982
4983 // We potentially looked past 'trunc', but only when matching YShift,
4984 // therefore YShift must have the widest type.
4985 Instruction *WidestShift = YShift;
4986 // Therefore XShift must have the shallowest type.
4987 // Or they both have identical types if there was no truncation.
4988 Instruction *NarrowestShift = XShift;
4989
4990 Type *WidestTy = WidestShift->getType();
4991 Type *NarrowestTy = NarrowestShift->getType();
4992 assert(NarrowestTy == I.getOperand(0)->getType() &&
4993 "We did not look past any shifts while matching XShift though.");
4994 bool HadTrunc = WidestTy != I.getOperand(i_nocapture: 0)->getType();
4995
4996 // If YShift is a 'lshr', swap the shifts around.
4997 if (match(V: YShift, P: m_LShr(L: m_Value(), R: m_Value())))
4998 std::swap(a&: XShift, b&: YShift);
4999
5000 // The shifts must be in opposite directions.
5001 auto XShiftOpcode = XShift->getOpcode();
5002 if (XShiftOpcode == YShift->getOpcode())
5003 return nullptr; // Do not care about same-direction shifts here.
5004
5005 Value *X, *XShAmt, *Y, *YShAmt;
5006 match(V: XShift, P: m_BinOp(L: m_Value(V&: X), R: m_ZExtOrSelf(Op: m_Value(V&: XShAmt))));
5007 match(V: YShift, P: m_BinOp(L: m_Value(V&: Y), R: m_ZExtOrSelf(Op: m_Value(V&: YShAmt))));
5008
5009 // If one of the values being shifted is a constant, then we will end with
5010 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
5011 // however, we will need to ensure that we won't increase instruction count.
5012 if (!isa<Constant>(Val: X) && !isa<Constant>(Val: Y)) {
5013 // At least one of the hands of the 'and' should be one-use shift.
5014 if (!match(V: I.getOperand(i_nocapture: 0),
5015 P: m_c_And(L: m_OneUse(SubPattern: m_AnyLogicalShift), R: m_Value())))
5016 return nullptr;
5017 if (HadTrunc) {
5018 // Due to the 'trunc', we will need to widen X. For that either the old
5019 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
5020 if (!MaybeTruncation->hasOneUse() &&
5021 !NarrowestShift->getOperand(i: 1)->hasOneUse())
5022 return nullptr;
5023 }
5024 }
5025
5026 // We have two shift amounts from two different shifts. The types of those
5027 // shift amounts may not match. If that's the case let's bailout now.
5028 if (XShAmt->getType() != YShAmt->getType())
5029 return nullptr;
5030
5031 // As input, we have the following pattern:
5032 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
5033 // We want to rewrite that as:
5034 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
5035 // While we know that originally (Q+K) would not overflow
5036 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
5037 // shift amounts. so it may now overflow in smaller bitwidth.
5038 // To ensure that does not happen, we need to ensure that the total maximal
5039 // shift amount is still representable in that smaller bit width.
5040 unsigned MaximalPossibleTotalShiftAmount =
5041 (WidestTy->getScalarSizeInBits() - 1) +
5042 (NarrowestTy->getScalarSizeInBits() - 1);
5043 APInt MaximalRepresentableShiftAmount =
5044 APInt::getAllOnes(numBits: XShAmt->getType()->getScalarSizeInBits());
5045 if (MaximalRepresentableShiftAmount.ult(RHS: MaximalPossibleTotalShiftAmount))
5046 return nullptr;
5047
5048 // Can we fold (XShAmt+YShAmt) ?
5049 auto *NewShAmt = dyn_cast_or_null<Constant>(
5050 Val: simplifyAddInst(LHS: XShAmt, RHS: YShAmt, /*isNSW=*/IsNSW: false,
5051 /*isNUW=*/IsNUW: false, Q: SQ.getWithInstruction(I: &I)));
5052 if (!NewShAmt)
5053 return nullptr;
5054 if (NewShAmt->getType() != WidestTy) {
5055 NewShAmt =
5056 ConstantFoldCastOperand(Opcode: Instruction::ZExt, C: NewShAmt, DestTy: WidestTy, DL: SQ.DL);
5057 if (!NewShAmt)
5058 return nullptr;
5059 }
5060 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
5061
5062 // Is the new shift amount smaller than the bit width?
5063 // FIXME: could also rely on ConstantRange.
5064 if (!match(V: NewShAmt,
5065 P: m_SpecificInt_ICMP(Predicate: ICmpInst::Predicate::ICMP_ULT,
5066 Threshold: APInt(WidestBitWidth, WidestBitWidth))))
5067 return nullptr;
5068
5069 // An extra legality check is needed if we had trunc-of-lshr.
5070 if (HadTrunc && match(V: WidestShift, P: m_LShr(L: m_Value(), R: m_Value()))) {
5071 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
5072 WidestShift]() {
5073 // It isn't obvious whether it's worth it to analyze non-constants here.
5074 // Also, let's basically give up on non-splat cases, pessimizing vectors.
5075 // If *any* of these preconditions matches we can perform the fold.
5076 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
5077 ? NewShAmt->getSplatValue()
5078 : NewShAmt;
5079 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
5080 if (NewShAmtSplat &&
5081 (NewShAmtSplat->isNullValue() ||
5082 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
5083 return true;
5084 // We consider *min* leading zeros so a single outlier
5085 // blocks the transform as opposed to allowing it.
5086 if (auto *C = dyn_cast<Constant>(Val: NarrowestShift->getOperand(i: 0))) {
5087 KnownBits Known = computeKnownBits(V: C, DL: SQ.DL);
5088 unsigned MinLeadZero = Known.countMinLeadingZeros();
5089 // If the value being shifted has at most lowest bit set we can fold.
5090 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
5091 if (MaxActiveBits <= 1)
5092 return true;
5093 // Precondition: NewShAmt u<= countLeadingZeros(C)
5094 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(RHS: MinLeadZero))
5095 return true;
5096 }
5097 if (auto *C = dyn_cast<Constant>(Val: WidestShift->getOperand(i: 0))) {
5098 KnownBits Known = computeKnownBits(V: C, DL: SQ.DL);
5099 unsigned MinLeadZero = Known.countMinLeadingZeros();
5100 // If the value being shifted has at most lowest bit set we can fold.
5101 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
5102 if (MaxActiveBits <= 1)
5103 return true;
5104 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
5105 if (NewShAmtSplat) {
5106 APInt AdjNewShAmt =
5107 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
5108 if (AdjNewShAmt.ule(RHS: MinLeadZero))
5109 return true;
5110 }
5111 }
5112 return false; // Can't tell if it's ok.
5113 };
5114 if (!CanFold())
5115 return nullptr;
5116 }
5117
5118 // All good, we can do this fold.
5119 X = Builder.CreateZExt(V: X, DestTy: WidestTy);
5120 Y = Builder.CreateZExt(V: Y, DestTy: WidestTy);
5121 // The shift is the same that was for X.
5122 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
5123 ? Builder.CreateLShr(LHS: X, RHS: NewShAmt)
5124 : Builder.CreateShl(LHS: X, RHS: NewShAmt);
5125 Value *T1 = Builder.CreateAnd(LHS: T0, RHS: Y);
5126 return Builder.CreateICmp(P: I.getPredicate(), LHS: T1,
5127 RHS: Constant::getNullValue(Ty: WidestTy));
5128}
5129
5130/// Fold
5131/// (-1 u/ x) u< y
5132/// ((x * y) ?/ x) != y
5133/// to
5134/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
5135/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
5136/// will mean that we are looking for the opposite answer.
5137Value *InstCombinerImpl::foldMultiplicationOverflowCheck(ICmpInst &I) {
5138 CmpPredicate Pred;
5139 Value *X, *Y;
5140 Instruction *Mul;
5141 Instruction *Div;
5142 bool NeedNegation;
5143 // Look for: (-1 u/ x) u</u>= y
5144 if (!I.isEquality() &&
5145 match(V: &I, P: m_c_ICmp(Pred,
5146 L: m_CombineAnd(Ps: m_OneUse(SubPattern: m_UDiv(L: m_AllOnes(), R: m_Value(V&: X))),
5147 Ps: m_Instruction(I&: Div)),
5148 R: m_Value(V&: Y)))) {
5149 Mul = nullptr;
5150
5151 // Are we checking that overflow does not happen, or does happen?
5152 switch (Pred) {
5153 case ICmpInst::Predicate::ICMP_ULT:
5154 NeedNegation = false;
5155 break; // OK
5156 case ICmpInst::Predicate::ICMP_UGE:
5157 NeedNegation = true;
5158 break; // OK
5159 default:
5160 return nullptr; // Wrong predicate.
5161 }
5162 } else // Look for: ((x * y) / x) !=/== y
5163 if (I.isEquality() &&
5164 match(V: &I, P: m_c_ICmp(Pred, L: m_Value(V&: Y),
5165 R: m_CombineAnd(Ps: m_OneUse(SubPattern: m_IDiv(
5166 L: m_CombineAnd(Ps: m_c_Mul(L: m_Deferred(V: Y),
5167 R: m_Value(V&: X)),
5168 Ps: m_Instruction(I&: Mul)),
5169 R: m_Deferred(V: X))),
5170 Ps: m_Instruction(I&: Div))))) {
5171 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
5172 } else
5173 return nullptr;
5174
5175 BuilderTy::InsertPointGuard Guard(Builder);
5176 // If the pattern included (x * y), we'll want to insert new instructions
5177 // right before that original multiplication so that we can replace it.
5178 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
5179 if (MulHadOtherUses)
5180 Builder.SetInsertPoint(Mul);
5181
5182 Value *Call = Builder.CreateIntrinsic(
5183 ID: Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow
5184 : Intrinsic::smul_with_overflow,
5185 OverloadTypes: X->getType(), Args: {X, Y}, /*FMFSource=*/nullptr, Name: "mul");
5186
5187 // If the multiplication was used elsewhere, to ensure that we don't leave
5188 // "duplicate" instructions, replace uses of that original multiplication
5189 // with the multiplication result from the with.overflow intrinsic.
5190 if (MulHadOtherUses)
5191 replaceInstUsesWith(I&: *Mul, V: Builder.CreateExtractValue(Agg: Call, Idxs: 0, Name: "mul.val"));
5192
5193 Value *Res = Builder.CreateExtractValue(Agg: Call, Idxs: 1, Name: "mul.ov");
5194 if (NeedNegation) // This technically increases instruction count.
5195 Res = Builder.CreateNot(V: Res, Name: "mul.not.ov");
5196
5197 // If we replaced the mul, erase it. Do this after all uses of Builder,
5198 // as the mul is used as insertion point.
5199 if (MulHadOtherUses)
5200 eraseInstFromFunction(I&: *Mul);
5201
5202 return Res;
5203}
5204
5205static Instruction *foldICmpXNegX(ICmpInst &I,
5206 InstCombiner::BuilderTy &Builder) {
5207 CmpPredicate Pred;
5208 Value *X;
5209 if (match(V: &I, P: m_c_ICmp(Pred, L: m_NSWNeg(V: m_Value(V&: X)), R: m_Deferred(V: X)))) {
5210
5211 if (ICmpInst::isSigned(Pred))
5212 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5213 else if (ICmpInst::isUnsigned(Pred))
5214 Pred = ICmpInst::getSignedPredicate(Pred);
5215 // else for equality-comparisons just keep the predicate.
5216
5217 return ICmpInst::Create(Op: Instruction::ICmp, Pred, S1: X,
5218 S2: Constant::getNullValue(Ty: X->getType()), Name: I.getName());
5219 }
5220
5221 // A value is not equal to its negation unless that value is 0 or
5222 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
5223 if (match(V: &I, P: m_c_ICmp(Pred, L: m_OneUse(SubPattern: m_Neg(V: m_Value(V&: X))), R: m_Deferred(V: X))) &&
5224 ICmpInst::isEquality(P: Pred)) {
5225 Type *Ty = X->getType();
5226 uint32_t BitWidth = Ty->getScalarSizeInBits();
5227 Constant *MaxSignedVal =
5228 ConstantInt::get(Ty, V: APInt::getSignedMaxValue(numBits: BitWidth));
5229 Value *And = Builder.CreateAnd(LHS: X, RHS: MaxSignedVal);
5230 Constant *Zero = Constant::getNullValue(Ty);
5231 return CmpInst::Create(Op: Instruction::ICmp, Pred, S1: And, S2: Zero);
5232 }
5233
5234 return nullptr;
5235}
5236
5237static Instruction *foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q,
5238 InstCombinerImpl &IC) {
5239 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1), *A;
5240 // Normalize and operand as operand 0.
5241 CmpInst::Predicate Pred = I.getPredicate();
5242 if (match(V: Op1, P: m_c_And(L: m_Specific(V: Op0), R: m_Value()))) {
5243 std::swap(a&: Op0, b&: Op1);
5244 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5245 }
5246
5247 if (!match(V: Op0, P: m_c_And(L: m_Specific(V: Op1), R: m_Value(V&: A))))
5248 return nullptr;
5249
5250 // (icmp (X & Y) u< X --> (X & Y) != X
5251 if (Pred == ICmpInst::ICMP_ULT)
5252 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5253
5254 // (icmp (X & Y) u>= X --> (X & Y) == X
5255 if (Pred == ICmpInst::ICMP_UGE)
5256 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5257
5258 if (ICmpInst::isEquality(P: Pred) && Op0->hasOneUse()) {
5259 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
5260 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
5261 // so don't do this fold.
5262 if (!match(V: Op1, P: m_ImmConstant()))
5263 if (auto *NotOp1 =
5264 IC.getFreelyInverted(V: Op1, WillInvertAllUses: !Op1->hasNUsesOrMore(N: 3), Builder: &IC.Builder))
5265 return new ICmpInst(Pred, IC.Builder.CreateOr(LHS: A, RHS: NotOp1),
5266 Constant::getAllOnesValue(Ty: Op1->getType()));
5267 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
5268 if (auto *NotA = IC.getFreelyInverted(V: A, WillInvertAllUses: A->hasOneUse(), Builder: &IC.Builder))
5269 return new ICmpInst(Pred, IC.Builder.CreateAnd(LHS: Op1, RHS: NotA),
5270 Constant::getNullValue(Ty: Op1->getType()));
5271 }
5272
5273 if (!ICmpInst::isSigned(Pred))
5274 return nullptr;
5275
5276 KnownBits KnownY = IC.computeKnownBits(V: A, CtxI: &I);
5277 // (X & NegY) spred X --> (X & NegY) upred X
5278 if (KnownY.isNegative())
5279 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
5280
5281 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
5282 return nullptr;
5283
5284 if (KnownY.isNonNegative())
5285 // (X & PosY) s<= X --> X s>= 0
5286 // (X & PosY) s> X --> X s< 0
5287 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
5288 Constant::getNullValue(Ty: Op1->getType()));
5289
5290 if (isKnownNegative(V: Op1, SQ: IC.getSimplifyQuery().getWithInstruction(I: &I)))
5291 // (NegX & Y) s<= NegX --> Y s< 0
5292 // (NegX & Y) s> NegX --> Y s>= 0
5293 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), A,
5294 Constant::getNullValue(Ty: A->getType()));
5295
5296 return nullptr;
5297}
5298
5299static Instruction *foldICmpOrXX(ICmpInst &I, const SimplifyQuery &Q,
5300 InstCombinerImpl &IC) {
5301 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1), *A;
5302
5303 // Normalize or operand as operand 0.
5304 CmpInst::Predicate Pred = I.getPredicate();
5305 if (match(V: Op1, P: m_c_Or(L: m_Specific(V: Op0), R: m_Value(V&: A)))) {
5306 std::swap(a&: Op0, b&: Op1);
5307 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5308 } else if (!match(V: Op0, P: m_c_Or(L: m_Specific(V: Op1), R: m_Value(V&: A)))) {
5309 return nullptr;
5310 }
5311
5312 // icmp (X | Y) u<= X --> (X | Y) == X
5313 if (Pred == ICmpInst::ICMP_ULE)
5314 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5315
5316 // icmp (X | Y) u> X --> (X | Y) != X
5317 if (Pred == ICmpInst::ICMP_UGT)
5318 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5319
5320 if (ICmpInst::isEquality(P: Pred) && Op0->hasOneUse()) {
5321 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
5322 if (Value *NotOp1 = IC.getFreelyInverted(
5323 V: Op1, WillInvertAllUses: !isa<Constant>(Val: Op1) && !Op1->hasNUsesOrMore(N: 3), Builder: &IC.Builder))
5324 return new ICmpInst(Pred, IC.Builder.CreateAnd(LHS: A, RHS: NotOp1),
5325 Constant::getNullValue(Ty: Op1->getType()));
5326 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
5327 if (Value *NotA = IC.getFreelyInverted(V: A, WillInvertAllUses: A->hasOneUse(), Builder: &IC.Builder))
5328 return new ICmpInst(Pred, IC.Builder.CreateOr(LHS: Op1, RHS: NotA),
5329 Constant::getAllOnesValue(Ty: Op1->getType()));
5330 }
5331 return nullptr;
5332}
5333
5334static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q,
5335 InstCombinerImpl &IC) {
5336 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1), *A;
5337 // Normalize xor operand as operand 0.
5338 CmpInst::Predicate Pred = I.getPredicate();
5339 if (match(V: Op1, P: m_c_Xor(L: m_Specific(V: Op0), R: m_Value()))) {
5340 std::swap(a&: Op0, b&: Op1);
5341 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5342 }
5343 if (!match(V: Op0, P: m_c_Xor(L: m_Specific(V: Op1), R: m_Value(V&: A))))
5344 return nullptr;
5345
5346 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
5347 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
5348 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
5349 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
5350 CmpInst::Predicate PredOut = CmpInst::getStrictPredicate(pred: Pred);
5351 if (PredOut != Pred && isKnownNonZero(V: A, Q))
5352 return new ICmpInst(PredOut, Op0, Op1);
5353
5354 // These transform work when A is negative.
5355 // X s< X^A, X s<= X^A, X u> X^A, X u>= X^A --> X s< 0
5356 // X s> X^A, X s>= X^A, X u< X^A, X u<= X^A --> X s>= 0
5357 if (match(V: A, P: m_Negative())) {
5358 CmpInst::Predicate NewPred;
5359 switch (ICmpInst::getStrictPredicate(pred: Pred)) {
5360 default:
5361 return nullptr;
5362 case ICmpInst::ICMP_SLT:
5363 case ICmpInst::ICMP_UGT:
5364 NewPred = ICmpInst::ICMP_SLT;
5365 break;
5366 case ICmpInst::ICMP_SGT:
5367 case ICmpInst::ICMP_ULT:
5368 NewPred = ICmpInst::ICMP_SGE;
5369 break;
5370 }
5371 Constant *Const = Constant::getNullValue(Ty: Op0->getType());
5372 return new ICmpInst(NewPred, Op0, Const);
5373 }
5374
5375 return nullptr;
5376}
5377
5378/// Return true if X is a multiple of C.
5379/// TODO: Handle non-power-of-2 factors.
5380static bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q) {
5381 if (C.isOne())
5382 return true;
5383
5384 if (!C.isPowerOf2())
5385 return false;
5386
5387 return MaskedValueIsZero(V: X, Mask: C - 1, SQ: Q);
5388}
5389
5390static bool hasNoWrapProblem(const BinaryOperator &BO, CmpInst::Predicate Pred,
5391 bool &HasNSW, bool &HasNUW) {
5392 if (isa<OverflowingBinaryOperator>(Val: BO)) {
5393 HasNUW = BO.hasNoUnsignedWrap();
5394 HasNSW = BO.hasNoSignedWrap();
5395 return ICmpInst::isEquality(P: Pred) ||
5396 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5397 (CmpInst::isSigned(Pred) && HasNSW);
5398 }
5399 if (BO.getOpcode() == Instruction::Or) {
5400 // The callers only use this result for an or matched by m_AddLike, which
5401 // must be an or disjoint and is equivalent to an add nuw nsw.
5402 HasNUW = true;
5403 HasNSW = true;
5404 return true;
5405 }
5406 return false;
5407}
5408
5409bool InstCombinerImpl::matchCommonBinOpOperands(Value *Op0, Value *Op1,
5410 CmpInst::Predicate Pred,
5411 Value *&LHS, Value *&RHS,
5412 const SimplifyQuery &Q) {
5413 auto *BO0 = dyn_cast<BinaryOperator>(Val: Op0);
5414 auto *BO1 = dyn_cast<BinaryOperator>(Val: Op1);
5415 if (!BO0 || !BO1)
5416 return false;
5417
5418 bool IsEquality = ICmpInst::isEquality(P: Pred);
5419 bool IsSigned = ICmpInst::isSigned(Pred);
5420
5421 bool Op0HasNSW = false, Op0HasNUW = false;
5422 bool Op1HasNSW = false, Op1HasNUW = false;
5423 bool NoOp0WrapProblem = hasNoWrapProblem(BO: *BO0, Pred, HasNSW&: Op0HasNSW, HasNUW&: Op0HasNUW);
5424 bool NoOp1WrapProblem = hasNoWrapProblem(BO: *BO1, Pred, HasNSW&: Op1HasNSW, HasNUW&: Op1HasNUW);
5425
5426 Value *A, *B, *C, *D;
5427
5428 // Handle all add-like operations.
5429 if (match(V: BO0, P: m_AddLike(L: m_Value(V&: A), R: m_Value(V&: B))) &&
5430 match(V: BO1, P: m_AddLike(L: m_Value(V&: C), R: m_Value(V&: D)))) {
5431 if (!NoOp0WrapProblem || !NoOp1WrapProblem)
5432 return false;
5433
5434 if (A == C) {
5435 // (A + B) cmp (A + D) -> B cmp D
5436 LHS = B;
5437 RHS = D;
5438 return true;
5439 }
5440 if (A == D) {
5441 // (A + B) cmp (C + A) -> B cmp C
5442 LHS = B;
5443 RHS = C;
5444 return true;
5445 }
5446 if (B == C) {
5447 // (A + B) cmp (B + D) -> A cmp D
5448 LHS = A;
5449 RHS = D;
5450 return true;
5451 }
5452 if (B == D) {
5453 // (A + B) cmp (C + B) -> A cmp C
5454 LHS = A;
5455 RHS = C;
5456 return true;
5457 }
5458 return false;
5459 }
5460
5461 if (BO0->getOpcode() != BO1->getOpcode())
5462 return false;
5463
5464 A = BO0->getOperand(i_nocapture: 0);
5465 B = BO0->getOperand(i_nocapture: 1);
5466 C = BO1->getOperand(i_nocapture: 0);
5467 D = BO1->getOperand(i_nocapture: 1);
5468
5469 switch (BO0->getOpcode()) {
5470 case Instruction::Sub:
5471 if (!NoOp0WrapProblem || !NoOp1WrapProblem)
5472 return false;
5473
5474 // (A - B) cmp (C - B) -> A cmp C
5475 if (B == D) {
5476 LHS = A;
5477 RHS = C;
5478 return true;
5479 }
5480 // (A - B) cmp (A - D) -> D cmp B
5481 if (A == C) {
5482 LHS = D;
5483 RHS = B;
5484 return true;
5485 }
5486 return false;
5487
5488 case Instruction::Mul: {
5489 // Equality comparisons have additional modular-arithmetic folds and are
5490 // handled by the existing multiplication logic in foldICmpBinOp().
5491 if (IsEquality || !NoOp0WrapProblem || !NoOp1WrapProblem)
5492 return false;
5493
5494 Value *Z;
5495
5496 if (A == C) {
5497 // (A * B) cmp (A * D) -> B cmp D
5498 Z = A;
5499 LHS = B;
5500 RHS = D;
5501 } else if (A == D) {
5502 // (A * B) cmp (C * A) -> B cmp C
5503 Z = A;
5504 LHS = B;
5505 RHS = C;
5506 } else if (B == C) {
5507 // (A * B) cmp (B * D) -> A cmp D
5508 Z = B;
5509 LHS = A;
5510 RHS = D;
5511 } else if (B == D) {
5512 // (A * B) cmp (C * B) -> A cmp C
5513 Z = B;
5514 LHS = A;
5515 RHS = C;
5516 } else {
5517 return false;
5518 }
5519
5520 if (!IsSigned)
5521 return isKnownNonZero(V: Z, Q);
5522
5523 WithCache<const Value *> ZCache(Z);
5524 if (isKnownPositive(V: ZCache, SQ: Q))
5525 return true;
5526
5527 if (isKnownNegative(V: ZCache, SQ: Q)) {
5528 std::swap(a&: LHS, b&: RHS);
5529 return true;
5530 }
5531
5532 return false;
5533 }
5534 default:
5535 return false;
5536 }
5537}
5538
5539/// Try to fold icmp (binop), X or icmp X, (binop).
5540/// TODO: A large part of this logic is duplicated in InstSimplify's
5541/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
5542/// duplication.
5543Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
5544 const SimplifyQuery &SQ) {
5545 const SimplifyQuery Q = SQ.getWithInstruction(I: &I);
5546 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
5547
5548 // Special logic for binary operators.
5549 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Val: Op0);
5550 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Val: Op1);
5551 if (!BO0 && !BO1)
5552 return nullptr;
5553
5554 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
5555 return NewICmp;
5556
5557 const CmpInst::Predicate Pred = I.getPredicate();
5558
5559 // (X urem Y) == X --> X u< Y
5560 // (X urem Y) != X --> X u>= Y
5561 Value *Dividend, *Divisor;
5562 if (I.isEquality() &&
5563 match(V: &I, P: m_c_ICmp(L: m_URem(L: m_Value(V&: Dividend), R: m_Value(V&: Divisor)),
5564 R: m_Deferred(V: Dividend)))) {
5565 CmpInst::Predicate NewPred =
5566 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
5567 return new ICmpInst(NewPred, Dividend, Divisor);
5568 }
5569
5570 Value *X;
5571
5572 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
5573 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
5574 if (match(V: Op0, P: m_OneUse(SubPattern: m_c_Add(L: m_Specific(V: Op1), R: m_Value(V&: X)))) &&
5575 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5576 return new ICmpInst(Pred, Builder.CreateNot(V: Op1), X);
5577 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
5578 if (match(V: Op1, P: m_OneUse(SubPattern: m_c_Add(L: m_Specific(V: Op0), R: m_Value(V&: X)))) &&
5579 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5580 return new ICmpInst(Pred, X, Builder.CreateNot(V: Op0));
5581
5582 {
5583 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
5584 Constant *C;
5585 if (match(V: Op0, P: m_OneUse(SubPattern: m_Add(L: m_c_Add(L: m_Specific(V: Op1), R: m_Value(V&: X)),
5586 R: m_ImmConstant(C)))) &&
5587 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
5588 Constant *C2 = ConstantExpr::getNot(C);
5589 return new ICmpInst(Pred, Builder.CreateSub(LHS: C2, RHS: X), Op1);
5590 }
5591 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5592 if (match(V: Op1, P: m_OneUse(SubPattern: m_Add(L: m_c_Add(L: m_Specific(V: Op0), R: m_Value(V&: X)),
5593 R: m_ImmConstant(C)))) &&
5594 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5595 Constant *C2 = ConstantExpr::getNot(C);
5596 return new ICmpInst(Pred, Op0, Builder.CreateSub(LHS: C2, RHS: X));
5597 }
5598 }
5599
5600 // (icmp eq/ne (X, -P2), INT_MIN)
5601 // -> (icmp slt/sge X, INT_MIN + P2)
5602 if (ICmpInst::isEquality(P: Pred) && BO0 &&
5603 match(V: I.getOperand(i_nocapture: 1), P: m_SignMask()) &&
5604 match(V: BO0, P: m_And(L: m_Value(), R: m_NegatedPower2OrZero()))) {
5605 // Will Constant fold.
5606 Value *NewC = Builder.CreateSub(LHS: I.getOperand(i_nocapture: 1), RHS: BO0->getOperand(i_nocapture: 1));
5607 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5608 : ICmpInst::ICMP_SGE,
5609 BO0->getOperand(i_nocapture: 0), NewC);
5610 }
5611
5612 {
5613 // Similar to above: an unsigned overflow comparison may use offset + mask:
5614 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5615 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5616 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5617 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5618 BinaryOperator *BO;
5619 const APInt *C;
5620 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5621 match(V: Op0, P: m_And(L: m_BinOp(I&: BO), R: m_LowBitMask(V&: C))) &&
5622 match(V: BO, P: m_Add(L: m_Specific(V: Op1), R: m_SpecificIntAllowPoison(V: *C)))) {
5623 CmpInst::Predicate NewPred =
5624 Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
5625 Constant *Zero = ConstantInt::getNullValue(Ty: Op1->getType());
5626 return new ICmpInst(NewPred, Op1, Zero);
5627 }
5628
5629 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5630 match(V: Op1, P: m_And(L: m_BinOp(I&: BO), R: m_LowBitMask(V&: C))) &&
5631 match(V: BO, P: m_Add(L: m_Specific(V: Op0), R: m_SpecificIntAllowPoison(V: *C)))) {
5632 CmpInst::Predicate NewPred =
5633 Pred == ICmpInst::ICMP_UGT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
5634 Constant *Zero = ConstantInt::getNullValue(Ty: Op1->getType());
5635 return new ICmpInst(NewPred, Op0, Zero);
5636 }
5637 }
5638
5639 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5640 bool Op0HasNUW = false, Op1HasNUW = false;
5641 bool Op0HasNSW = false, Op1HasNSW = false;
5642 // Analyze the case when either Op0 or Op1 is an add instruction.
5643 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5644 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5645
5646 if (BO0) {
5647 match(V: BO0, P: m_AddLike(L: m_Value(V&: A), R: m_Value(V&: B)));
5648 NoOp0WrapProblem = hasNoWrapProblem(BO: *BO0, Pred, HasNSW&: Op0HasNSW, HasNUW&: Op0HasNUW);
5649 }
5650 if (BO1) {
5651 match(V: BO1, P: m_AddLike(L: m_Value(V&: C), R: m_Value(V&: D)));
5652 NoOp1WrapProblem = hasNoWrapProblem(BO: *BO1, Pred, HasNSW&: Op1HasNSW, HasNUW&: Op1HasNUW);
5653 }
5654
5655 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5656 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5657 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5658 return new ICmpInst(Pred, A == Op1 ? B : A,
5659 Constant::getNullValue(Ty: Op1->getType()));
5660
5661 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5662 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5663 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5664 return new ICmpInst(Pred, Constant::getNullValue(Ty: Op0->getType()),
5665 C == Op0 ? D : C);
5666
5667 // Fold comparisons of binops with a removable common operand.
5668 Value *LHS;
5669 Value *RHS;
5670 if (matchCommonBinOpOperands(Op0, Op1, Pred, LHS, RHS, Q))
5671 return new ICmpInst(Pred, LHS, RHS);
5672
5673 if (ICmpInst::isRelational(P: Pred)) {
5674 // Return if both X and Y is divisible by Z/-Z.
5675 // TODO: Generalize to check if (X - Y) is divisible by Z/-Z.
5676 auto ShareCommonDivisor = [&Q](Value *X, Value *Y, Value *Z,
5677 bool IsNegative) -> bool {
5678 const APInt *OffsetC;
5679 if (!match(V: Z, P: m_APInt(Res&: OffsetC)))
5680 return false;
5681
5682 // Fast path for Z == 1/-1.
5683 if (IsNegative ? OffsetC->isAllOnes() : OffsetC->isOne())
5684 return true;
5685
5686 APInt C = *OffsetC;
5687 if (IsNegative)
5688 C.negate();
5689 // Note: -INT_MIN is also negative.
5690 if (!C.isStrictlyPositive())
5691 return false;
5692
5693 return isMultipleOf(X, C, Q) && isMultipleOf(X: Y, C, Q);
5694 };
5695
5696 // The subtraction-related identities (A -nuw B) shown below require that
5697 // the subtraction does not wrap unsigned (i.e., A >=u B). Canonicalization
5698 // from (A -nuw 1) to (A + -1) means that such combinations ought to never
5699 // occur, as sub nuw ops should have been canonicalized to add ones. It may
5700 // however appear in the form of a or disjoint. Though, or disjoint A, -B
5701 // requires proving A <u B, for which the nowrap precondition can never be
5702 // satisfied. These are therefore skipped.
5703 //
5704 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5705 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5706 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5707 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5708
5709 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5710 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5711 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5712 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5713 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5714 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5715 bool IsNegative = ICmpInst::isLT(P: Pred) || ICmpInst::isGE(P: Pred);
5716 bool IsAddOrSignedPred = !IsNegative || ICmpInst::isSigned(Pred);
5717 if (A && NoOp0WrapProblem && IsAddOrSignedPred &&
5718 ShareCommonDivisor(A, Op1, B, IsNegative))
5719 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), A,
5720 Op1);
5721
5722 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5723 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5724 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5725 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5726 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5727 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5728 if (C && NoOp1WrapProblem &&
5729 ShareCommonDivisor(Op0, C, D,
5730 ICmpInst::isGT(P: Pred) || ICmpInst::isLE(P: Pred)))
5731 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), Op0,
5732 C);
5733 }
5734
5735 // if C1 has greater magnitude than C2:
5736 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5737 // s.t. C3 = C1 - C2
5738 //
5739 // if C2 has greater magnitude than C1:
5740 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5741 // s.t. C3 = C2 - C1
5742 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5743 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5744 const APInt *AP1, *AP2;
5745 // TODO: Support non-uniform vectors.
5746 // TODO: Allow poison passthrough if B or D's element is poison.
5747 if (match(V: B, P: m_APIntAllowPoison(Res&: AP1)) &&
5748 match(V: D, P: m_APIntAllowPoison(Res&: AP2)) &&
5749 AP1->isNegative() == AP2->isNegative()) {
5750 APInt AP1Abs = AP1->abs();
5751 APInt AP2Abs = AP2->abs();
5752 if (AP1Abs.uge(RHS: AP2Abs)) {
5753 APInt Diff = *AP1 - *AP2;
5754 Constant *C3 = Constant::getIntegerValue(Ty: BO0->getType(), V: Diff);
5755 Value *NewAdd = Builder.CreateAdd(
5756 LHS: A, RHS: C3, Name: "", HasNUW: Op0HasNUW && Diff.ule(RHS: *AP1), HasNSW: Op0HasNSW);
5757 return new ICmpInst(Pred, NewAdd, C);
5758 } else {
5759 APInt Diff = *AP2 - *AP1;
5760 Constant *C3 = Constant::getIntegerValue(Ty: BO0->getType(), V: Diff);
5761 Value *NewAdd = Builder.CreateAdd(
5762 LHS: C, RHS: C3, Name: "", HasNUW: Op1HasNUW && Diff.ule(RHS: *AP2), HasNSW: Op1HasNSW);
5763 return new ICmpInst(Pred, A, NewAdd);
5764 }
5765 }
5766 Constant *Cst1, *Cst2;
5767 if (match(V: B, P: m_ImmConstant(C&: Cst1)) && match(V: D, P: m_ImmConstant(C&: Cst2)) &&
5768 ICmpInst::isEquality(P: Pred)) {
5769 Constant *Diff = ConstantExpr::getSub(C1: Cst2, C2: Cst1);
5770 Value *NewAdd = Builder.CreateAdd(LHS: C, RHS: Diff);
5771 return new ICmpInst(Pred, A, NewAdd);
5772 }
5773 }
5774
5775 // Analyze the case when either Op0 or Op1 is a sub instruction.
5776 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5777 A = nullptr;
5778 B = nullptr;
5779 C = nullptr;
5780 D = nullptr;
5781 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5782 A = BO0->getOperand(i_nocapture: 0);
5783 B = BO0->getOperand(i_nocapture: 1);
5784 }
5785 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5786 C = BO1->getOperand(i_nocapture: 0);
5787 D = BO1->getOperand(i_nocapture: 1);
5788 }
5789
5790 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5791 if (A == Op1 && NoOp0WrapProblem)
5792 return new ICmpInst(Pred, Constant::getNullValue(Ty: Op1->getType()), B);
5793 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5794 if (C == Op0 && NoOp1WrapProblem)
5795 return new ICmpInst(Pred, D, Constant::getNullValue(Ty: Op0->getType()));
5796
5797 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5798 // (A - B) u>/u<= A --> B u>/u<= A
5799 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5800 return new ICmpInst(Pred, B, A);
5801 // C u</u>= (C - D) --> C u</u>= D
5802 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5803 return new ICmpInst(Pred, C, D);
5804 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5805 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5806 isKnownNonZero(V: B, Q))
5807 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(pred: Pred), B, A);
5808 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5809 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5810 isKnownNonZero(V: D, Q))
5811 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(pred: Pred), C, D);
5812
5813 // icmp (0-X) < cst --> x > -cst
5814 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5815 Value *X;
5816 if (match(V: BO0, P: m_Neg(V: m_Value(V&: X))))
5817 if (Constant *RHSC = dyn_cast<Constant>(Val: Op1))
5818 if (RHSC->isNotMinSignedValue())
5819 return new ICmpInst(I.getSwappedPredicate(), X,
5820 ConstantExpr::getNeg(C: RHSC));
5821 }
5822
5823 if (Instruction *R = foldICmpXorXX(I, Q, IC&: *this))
5824 return R;
5825 if (Instruction *R = foldICmpOrXX(I, Q, IC&: *this))
5826 return R;
5827
5828 {
5829 // Try to remove shared multiplier from comparison:
5830 // X * Z pred Y * Z
5831 Value *X, *Y, *Z;
5832 if ((match(V: Op0, P: m_Mul(L: m_Value(V&: X), R: m_Value(V&: Z))) &&
5833 match(V: Op1, P: m_c_Mul(L: m_Specific(V: Z), R: m_Value(V&: Y)))) ||
5834 (match(V: Op0, P: m_Mul(L: m_Value(V&: Z), R: m_Value(V&: X))) &&
5835 match(V: Op1, P: m_c_Mul(L: m_Specific(V: Z), R: m_Value(V&: Y))))) {
5836 if (ICmpInst::isSigned(Pred)) {
5837 if (Op0HasNSW && Op1HasNSW) {
5838 Value *LessThan = simplifyICmpInst(Pred: ICmpInst::ICMP_SLT, LHS: X, RHS: Y,
5839 Q: SQ.getWithInstruction(I: &I));
5840 if (LessThan && match(V: LessThan, P: m_One()))
5841 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Z,
5842 Constant::getNullValue(Ty: Z->getType()));
5843 Value *GreaterThan = simplifyICmpInst(Pred: ICmpInst::ICMP_SGT, LHS: X, RHS: Y,
5844 Q: SQ.getWithInstruction(I: &I));
5845 if (GreaterThan && match(V: GreaterThan, P: m_One()))
5846 return new ICmpInst(Pred, Z, Constant::getNullValue(Ty: Z->getType()));
5847 }
5848 } else if (ICmpInst::isEquality(P: Pred)) {
5849 // Handle equality folds beyond direct multiplier cancellation.
5850 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0
5851 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&
5852 isKnownNonEqual(V1: X, V2: Y, SQ))
5853 return new ICmpInst(Pred, Z, Constant::getNullValue(Ty: Z->getType()));
5854
5855 KnownBits ZKnown = computeKnownBits(V: Z, CtxI: &I);
5856 // if Z % 2 != 0
5857 // X * Z eq/ne Y * Z -> X eq/ne Y
5858 if (ZKnown.countMaxTrailingZeros() == 0)
5859 return new ICmpInst(Pred, X, Y);
5860 bool NonZero = !ZKnown.One.isZero() || isKnownNonZero(V: Z, Q);
5861 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5862 // X * Z eq/ne Y * Z -> X eq/ne Y
5863 if (NonZero && Op0HasNSW && Op1HasNSW)
5864 return new ICmpInst(Pred, X, Y);
5865
5866 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5867 // X * Z eq/ne Y * Z -> X eq/ne Y
5868 if (NonZero && Op0HasNUW && Op1HasNUW)
5869 return new ICmpInst(Pred, X, Y);
5870 }
5871 }
5872 }
5873
5874 BinaryOperator *SRem = nullptr;
5875 // icmp (srem X, Y), Y
5876 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(i_nocapture: 1))
5877 SRem = BO0;
5878 // icmp Y, (srem X, Y)
5879 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5880 Op0 == BO1->getOperand(i_nocapture: 1))
5881 SRem = BO1;
5882 if (SRem) {
5883 // We don't check hasOneUse to avoid increasing register pressure because
5884 // the value we use is the same value this instruction was already using.
5885 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(pred: Pred) : Pred) {
5886 default:
5887 break;
5888 case ICmpInst::ICMP_EQ:
5889 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
5890 case ICmpInst::ICMP_NE:
5891 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
5892 case ICmpInst::ICMP_SGT:
5893 case ICmpInst::ICMP_SGE:
5894 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(i_nocapture: 1),
5895 Constant::getAllOnesValue(Ty: SRem->getType()));
5896 case ICmpInst::ICMP_SLT:
5897 case ICmpInst::ICMP_SLE:
5898 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(i_nocapture: 1),
5899 Constant::getNullValue(Ty: SRem->getType()));
5900 }
5901 }
5902
5903 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5904 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5905 BO0->getOperand(i_nocapture: 1) == BO1->getOperand(i_nocapture: 1)) {
5906 switch (BO0->getOpcode()) {
5907 default:
5908 break;
5909 case Instruction::Add:
5910 case Instruction::Sub:
5911 case Instruction::Xor: {
5912 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5913 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5914
5915 const APInt *C;
5916 if (match(V: BO0->getOperand(i_nocapture: 1), P: m_APInt(Res&: C))) {
5917 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5918 if (C->isSignMask()) {
5919 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5920 return new ICmpInst(NewPred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5921 }
5922
5923 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5924 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5925 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5926 NewPred = I.getSwappedPredicate(pred: NewPred);
5927 return new ICmpInst(NewPred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5928 }
5929 }
5930 break;
5931 }
5932 case Instruction::Mul: {
5933 if (!I.isEquality())
5934 break;
5935
5936 const APInt *C;
5937 if (match(V: BO0->getOperand(i_nocapture: 1), P: m_APInt(Res&: C)) && !C->isZero() &&
5938 !C->isOne()) {
5939 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5940 // Mask = -1 >> count-trailing-zeros(C).
5941 if (unsigned TZs = C->countr_zero()) {
5942 Constant *Mask = ConstantInt::get(
5943 Ty: BO0->getType(),
5944 V: APInt::getLowBitsSet(numBits: C->getBitWidth(), loBitsSet: C->getBitWidth() - TZs));
5945 Value *And1 = Builder.CreateAnd(LHS: BO0->getOperand(i_nocapture: 0), RHS: Mask);
5946 Value *And2 = Builder.CreateAnd(LHS: BO1->getOperand(i_nocapture: 0), RHS: Mask);
5947 return new ICmpInst(Pred, And1, And2);
5948 }
5949 }
5950 break;
5951 }
5952 case Instruction::UDiv:
5953 case Instruction::LShr:
5954 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5955 break;
5956 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5957
5958 case Instruction::SDiv:
5959 if (!(I.isEquality() || match(V: BO0->getOperand(i_nocapture: 1), P: m_NonNegative())) ||
5960 !BO0->isExact() || !BO1->isExact())
5961 break;
5962 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5963
5964 case Instruction::AShr:
5965 if (!BO0->isExact() || !BO1->isExact())
5966 break;
5967 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5968
5969 case Instruction::Shl: {
5970 bool NUW = Op0HasNUW && Op1HasNUW;
5971 bool NSW = Op0HasNSW && Op1HasNSW;
5972 if (!NUW && !NSW)
5973 break;
5974 if (!NSW && I.isSigned())
5975 break;
5976 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5977 }
5978 }
5979 }
5980
5981 if (BO0) {
5982 // Transform A & (L - 1) `ult` L --> L != 0
5983 auto LSubOne = m_Add(L: m_Specific(V: Op1), R: m_AllOnes());
5984 auto BitwiseAnd = m_c_And(L: m_Value(), R: LSubOne);
5985
5986 if (match(V: BO0, P: BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5987 auto *Zero = Constant::getNullValue(Ty: BO0->getType());
5988 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5989 }
5990 }
5991
5992 // For unsigned predicates / eq / ne:
5993 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5994 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5995 if (!ICmpInst::isSigned(Pred)) {
5996 if (match(V: Op0, P: m_Shl(L: m_Specific(V: Op1), R: m_One())))
5997 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5998 Constant::getNullValue(Ty: Op1->getType()));
5999 else if (match(V: Op1, P: m_Shl(L: m_Specific(V: Op0), R: m_One())))
6000 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
6001 Constant::getNullValue(Ty: Op0->getType()), Op0);
6002 }
6003
6004 if (Value *V = foldMultiplicationOverflowCheck(I))
6005 return replaceInstUsesWith(I, V);
6006
6007 if (Instruction *R = foldICmpAndXX(I, Q, IC&: *this))
6008 return R;
6009
6010 if (Value *V = foldICmpWithTruncSignExtendedVal(I, Builder))
6011 return replaceInstUsesWith(I, V);
6012
6013 if (Value *V = foldShiftIntoShiftInAnotherHandOfAndInICmp(I, SQ, Builder))
6014 return replaceInstUsesWith(I, V);
6015
6016 return nullptr;
6017}
6018
6019/// Fold icmp Pred min|max(X, Y), Z.
6020Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
6021 MinMaxIntrinsic *MinMax,
6022 Value *Z, CmpPredicate Pred) {
6023 Value *X = MinMax->getLHS();
6024 Value *Y = MinMax->getRHS();
6025 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
6026 return nullptr;
6027 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
6028 // Revert the transform signed pred -> unsigned pred
6029 // TODO: We can flip the signedness of predicate if both operands of icmp
6030 // are negative.
6031 if (isKnownNonNegative(V: Z, SQ: SQ.getWithInstruction(I: &I)) &&
6032 isKnownNonNegative(V: MinMax, SQ: SQ.getWithInstruction(I: &I))) {
6033 Pred = ICmpInst::getFlippedSignednessPredicate(Pred);
6034 } else
6035 return nullptr;
6036 }
6037 SimplifyQuery Q = SQ.getWithInstruction(I: &I);
6038 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
6039 if (!Val)
6040 return std::nullopt;
6041 if (match(V: Val, P: m_One()))
6042 return true;
6043 if (match(V: Val, P: m_Zero()))
6044 return false;
6045 return std::nullopt;
6046 };
6047 // Remove samesign here since it is illegal to keep it when we speculatively
6048 // execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
6049 // cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
6050 // -46, -32)`. `X` is allowed to be non-negative here.
6051 Pred = Pred.dropSameSign();
6052 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, LHS: X, RHS: Z, Q));
6053 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, LHS: Y, RHS: Z, Q));
6054 if (!CmpXZ.has_value() && !CmpYZ.has_value())
6055 return nullptr;
6056 if (!CmpXZ.has_value()) {
6057 std::swap(a&: X, b&: Y);
6058 std::swap(lhs&: CmpXZ, rhs&: CmpYZ);
6059 }
6060
6061 auto FoldIntoCmpYZ = [&]() -> Instruction * {
6062 if (CmpYZ.has_value())
6063 return replaceInstUsesWith(I, V: ConstantInt::getBool(Ty: I.getType(), V: *CmpYZ));
6064 return ICmpInst::Create(Op: Instruction::ICmp, Pred, S1: Y, S2: Z);
6065 };
6066
6067 switch (Pred) {
6068 case ICmpInst::ICMP_EQ:
6069 case ICmpInst::ICMP_NE: {
6070 // If X == Z:
6071 // Expr Result
6072 // min(X, Y) == Z X <= Y
6073 // max(X, Y) == Z X >= Y
6074 // min(X, Y) != Z X > Y
6075 // max(X, Y) != Z X < Y
6076 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
6077 ICmpInst::Predicate NewPred =
6078 ICmpInst::getNonStrictPredicate(pred: MinMax->getPredicate());
6079 if (Pred == ICmpInst::ICMP_NE)
6080 NewPred = ICmpInst::getInversePredicate(pred: NewPred);
6081 return ICmpInst::Create(Op: Instruction::ICmp, Pred: NewPred, S1: X, S2: Y);
6082 }
6083 // Otherwise (X != Z):
6084 ICmpInst::Predicate NewPred = MinMax->getPredicate();
6085 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred: NewPred, LHS: X, RHS: Z, Q));
6086 if (!MinMaxCmpXZ.has_value()) {
6087 std::swap(a&: X, b&: Y);
6088 std::swap(lhs&: CmpXZ, rhs&: CmpYZ);
6089 // Re-check pre-condition X != Z
6090 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
6091 break;
6092 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred: NewPred, LHS: X, RHS: Z, Q));
6093 }
6094 if (!MinMaxCmpXZ.has_value())
6095 break;
6096 if (*MinMaxCmpXZ) {
6097 // Expr Fact Result
6098 // min(X, Y) == Z X < Z false
6099 // max(X, Y) == Z X > Z false
6100 // min(X, Y) != Z X < Z true
6101 // max(X, Y) != Z X > Z true
6102 return replaceInstUsesWith(
6103 I, V: ConstantInt::getBool(Ty: I.getType(), V: Pred == ICmpInst::ICMP_NE));
6104 } else {
6105 // Expr Fact Result
6106 // min(X, Y) == Z X > Z Y == Z
6107 // max(X, Y) == Z X < Z Y == Z
6108 // min(X, Y) != Z X > Z Y != Z
6109 // max(X, Y) != Z X < Z Y != Z
6110 return FoldIntoCmpYZ();
6111 }
6112 break;
6113 }
6114 case ICmpInst::ICMP_SLT:
6115 case ICmpInst::ICMP_ULT:
6116 case ICmpInst::ICMP_SLE:
6117 case ICmpInst::ICMP_ULE:
6118 case ICmpInst::ICMP_SGT:
6119 case ICmpInst::ICMP_UGT:
6120 case ICmpInst::ICMP_SGE:
6121 case ICmpInst::ICMP_UGE: {
6122 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(pred: Pred);
6123 if (*CmpXZ) {
6124 if (IsSame) {
6125 // Expr Fact Result
6126 // min(X, Y) < Z X < Z true
6127 // min(X, Y) <= Z X <= Z true
6128 // max(X, Y) > Z X > Z true
6129 // max(X, Y) >= Z X >= Z true
6130 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
6131 } else {
6132 // Expr Fact Result
6133 // max(X, Y) < Z X < Z Y < Z
6134 // max(X, Y) <= Z X <= Z Y <= Z
6135 // min(X, Y) > Z X > Z Y > Z
6136 // min(X, Y) >= Z X >= Z Y >= Z
6137 return FoldIntoCmpYZ();
6138 }
6139 } else {
6140 if (IsSame) {
6141 // Expr Fact Result
6142 // min(X, Y) < Z X >= Z Y < Z
6143 // min(X, Y) <= Z X > Z Y <= Z
6144 // max(X, Y) > Z X <= Z Y > Z
6145 // max(X, Y) >= Z X < Z Y >= Z
6146 return FoldIntoCmpYZ();
6147 } else {
6148 // Expr Fact Result
6149 // max(X, Y) < Z X >= Z false
6150 // max(X, Y) <= Z X > Z false
6151 // min(X, Y) > Z X <= Z false
6152 // min(X, Y) >= Z X < Z false
6153 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
6154 }
6155 }
6156 break;
6157 }
6158 default:
6159 break;
6160 }
6161
6162 return nullptr;
6163}
6164
6165/// Match and fold patterns like:
6166/// icmp eq/ne X, min(max(X, Lo), Hi)
6167/// which represents a range check and can be represented as a ConstantRange.
6168///
6169/// For icmp eq, build ConstantRange [Lo, Hi + 1) and convert to:
6170/// (X - Lo) u< (Hi + 1 - Lo)
6171/// For icmp ne, build ConstantRange [Hi + 1, Lo) and convert to:
6172/// (X - (Hi + 1)) u< (Lo - (Hi + 1))
6173Instruction *InstCombinerImpl::foldICmpWithClamp(ICmpInst &I, Value *X,
6174 MinMaxIntrinsic *Min) {
6175 if (!I.isEquality() || !Min->hasOneUse() || !Min->isMin())
6176 return nullptr;
6177
6178 const APInt *Lo = nullptr, *Hi = nullptr;
6179 if (Min->isSigned()) {
6180 if (!match(V: Min->getLHS(), P: m_OneUse(SubPattern: m_SMax(Op0: m_Specific(V: X), Op1: m_APInt(Res&: Lo)))) ||
6181 !match(V: Min->getRHS(), P: m_APInt(Res&: Hi)) || !Lo->slt(RHS: *Hi))
6182 return nullptr;
6183 } else {
6184 if (!match(V: Min->getLHS(), P: m_OneUse(SubPattern: m_UMax(Op0: m_Specific(V: X), Op1: m_APInt(Res&: Lo)))) ||
6185 !match(V: Min->getRHS(), P: m_APInt(Res&: Hi)) || !Lo->ult(RHS: *Hi))
6186 return nullptr;
6187 }
6188
6189 ConstantRange CR = ConstantRange::getNonEmpty(Lower: *Lo, Upper: *Hi + 1);
6190 ICmpInst::Predicate Pred;
6191 APInt C, Offset;
6192 if (I.getPredicate() == ICmpInst::ICMP_EQ)
6193 CR.getEquivalentICmp(Pred, RHS&: C, Offset);
6194 else
6195 CR.inverse().getEquivalentICmp(Pred, RHS&: C, Offset);
6196
6197 if (!Offset.isZero())
6198 X = Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: Offset));
6199
6200 return replaceInstUsesWith(
6201 I, V: Builder.CreateICmp(P: Pred, LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: C)));
6202}
6203
6204// Canonicalize checking for a power-of-2-or-zero value:
6205static Instruction *foldICmpPow2Test(ICmpInst &I,
6206 InstCombiner::BuilderTy &Builder) {
6207 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
6208 const CmpInst::Predicate Pred = I.getPredicate();
6209 Value *A = nullptr;
6210 bool CheckIs;
6211 if (I.isEquality()) {
6212 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
6213 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
6214 if (!match(V: Op0, P: m_OneUse(SubPattern: m_c_And(L: m_Add(L: m_Value(V&: A), R: m_AllOnes()),
6215 R: m_Deferred(V: A)))) ||
6216 !match(V: Op1, P: m_ZeroInt()))
6217 A = nullptr;
6218
6219 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
6220 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
6221 if (match(V: Op0, P: m_OneUse(SubPattern: m_c_And(L: m_Neg(V: m_Specific(V: Op1)), R: m_Specific(V: Op1)))))
6222 A = Op1;
6223 else if (match(V: Op1,
6224 P: m_OneUse(SubPattern: m_c_And(L: m_Neg(V: m_Specific(V: Op0)), R: m_Specific(V: Op0)))))
6225 A = Op0;
6226
6227 CheckIs = Pred == ICmpInst::ICMP_EQ;
6228 } else if (ICmpInst::isUnsigned(Pred)) {
6229 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
6230 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
6231
6232 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
6233 match(V: Op0, P: m_OneUse(SubPattern: m_c_Xor(L: m_Add(L: m_Specific(V: Op1), R: m_AllOnes()),
6234 R: m_Specific(V: Op1))))) {
6235 A = Op1;
6236 CheckIs = Pred == ICmpInst::ICMP_UGE;
6237 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
6238 match(V: Op1, P: m_OneUse(SubPattern: m_c_Xor(L: m_Add(L: m_Specific(V: Op0), R: m_AllOnes()),
6239 R: m_Specific(V: Op0))))) {
6240 A = Op0;
6241 CheckIs = Pred == ICmpInst::ICMP_ULE;
6242 }
6243 }
6244
6245 if (A) {
6246 Type *Ty = A->getType();
6247 Value *CtPop = Builder.CreateUnaryIntrinsic(ID: Intrinsic::ctpop, Op: A);
6248 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
6249 ConstantInt::get(Ty, V: 2))
6250 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
6251 ConstantInt::get(Ty, V: 1));
6252 }
6253
6254 return nullptr;
6255}
6256
6257/// Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
6258using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;
6259static void collectOffsetOp(Value *V, SmallVectorImpl<OffsetOp> &Offsets,
6260 bool AllowRecursion) {
6261 Instruction *Inst = dyn_cast<Instruction>(Val: V);
6262 if (!Inst || !Inst->hasOneUse())
6263 return;
6264
6265 switch (Inst->getOpcode()) {
6266 case Instruction::Add:
6267 Offsets.emplace_back(Args: Instruction::Sub, Args: Inst->getOperand(i: 1));
6268 Offsets.emplace_back(Args: Instruction::Sub, Args: Inst->getOperand(i: 0));
6269 break;
6270 case Instruction::Sub:
6271 Offsets.emplace_back(Args: Instruction::Add, Args: Inst->getOperand(i: 1));
6272 break;
6273 case Instruction::Xor:
6274 Offsets.emplace_back(Args: Instruction::Xor, Args: Inst->getOperand(i: 1));
6275 Offsets.emplace_back(Args: Instruction::Xor, Args: Inst->getOperand(i: 0));
6276 break;
6277 case Instruction::Shl:
6278 if (Inst->hasNoSignedWrap())
6279 Offsets.emplace_back(Args: Instruction::AShr, Args: Inst->getOperand(i: 1));
6280 if (Inst->hasNoUnsignedWrap())
6281 Offsets.emplace_back(Args: Instruction::LShr, Args: Inst->getOperand(i: 1));
6282 break;
6283 case Instruction::Select:
6284 if (AllowRecursion) {
6285 collectOffsetOp(V: Inst->getOperand(i: 1), Offsets, /*AllowRecursion=*/false);
6286 collectOffsetOp(V: Inst->getOperand(i: 2), Offsets, /*AllowRecursion=*/false);
6287 }
6288 break;
6289 default:
6290 break;
6291 }
6292}
6293
6294enum class OffsetKind { Invalid, Value, Select };
6295
6296struct OffsetResult {
6297 OffsetKind Kind;
6298 Value *V0, *V1, *V2;
6299 Instruction *MDFrom;
6300
6301 static OffsetResult invalid() {
6302 return {.Kind: OffsetKind::Invalid, .V0: nullptr, .V1: nullptr, .V2: nullptr, .MDFrom: nullptr};
6303 }
6304 static OffsetResult value(Value *V) {
6305 return {.Kind: OffsetKind::Value, .V0: V, .V1: nullptr, .V2: nullptr, .MDFrom: nullptr};
6306 }
6307 static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV,
6308 Instruction *MDFrom) {
6309 return {.Kind: OffsetKind::Select, .V0: Cond, .V1: TrueV, .V2: FalseV, .MDFrom: MDFrom};
6310 }
6311 bool isValid() const { return Kind != OffsetKind::Invalid; }
6312 Value *materialize(InstCombiner::BuilderTy &Builder) const {
6313 switch (Kind) {
6314 case OffsetKind::Invalid:
6315 llvm_unreachable("Invalid offset result");
6316 case OffsetKind::Value:
6317 return V0;
6318 case OffsetKind::Select:
6319 return Builder.CreateSelect(C: V0, True: V1, False: V2, Name: "", MDFrom);
6320 }
6321 llvm_unreachable("Unknown OffsetKind enum");
6322 }
6323};
6324
6325/// Offset both sides of an equality icmp to see if we can save some
6326/// instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.
6327/// Note: This operation should not introduce poison.
6328static Instruction *foldICmpEqualityWithOffset(ICmpInst &I,
6329 InstCombiner::BuilderTy &Builder,
6330 const SimplifyQuery &SQ) {
6331 assert(I.isEquality() && "Expected an equality icmp");
6332 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
6333 if (!Op0->getType()->isIntOrIntVectorTy())
6334 return nullptr;
6335
6336 SmallVector<OffsetOp, 4> OffsetOps;
6337 collectOffsetOp(V: Op0, Offsets&: OffsetOps, /*AllowRecursion=*/true);
6338 collectOffsetOp(V: Op1, Offsets&: OffsetOps, /*AllowRecursion=*/true);
6339
6340 auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {
6341 switch (BinOpc) {
6342 // V = shl nsw X, RHS => X = ashr V, RHS
6343 case Instruction::AShr: {
6344 const APInt *CV, *CRHS;
6345 if (!(match(V, P: m_APInt(Res&: CV)) && match(V: RHS, P: m_APInt(Res&: CRHS)) &&
6346 CV->ashr(ShiftAmt: *CRHS).shl(ShiftAmt: *CRHS) == *CV) &&
6347 !match(V, P: m_NSWShl(L: m_Value(), R: m_Specific(V: RHS))))
6348 return nullptr;
6349 break;
6350 }
6351 // V = shl nuw X, RHS => X = lshr V, RHS
6352 case Instruction::LShr: {
6353 const APInt *CV, *CRHS;
6354 if (!(match(V, P: m_APInt(Res&: CV)) && match(V: RHS, P: m_APInt(Res&: CRHS)) &&
6355 CV->lshr(ShiftAmt: *CRHS).shl(ShiftAmt: *CRHS) == *CV) &&
6356 !match(V, P: m_NUWShl(L: m_Value(), R: m_Specific(V: RHS))))
6357 return nullptr;
6358 break;
6359 }
6360 default:
6361 break;
6362 }
6363
6364 Value *Simplified = simplifyBinOp(Opcode: BinOpc, LHS: V, RHS, Q: SQ);
6365 if (!Simplified)
6366 return nullptr;
6367 // Reject constant expressions as they don't simplify things.
6368 if (isa<Constant>(Val: Simplified) && !match(V: Simplified, P: m_ImmConstant()))
6369 return nullptr;
6370 // Check if the transformation introduces poison.
6371 return impliesPoison(ValAssumedPoison: RHS, V) ? Simplified : nullptr;
6372 };
6373
6374 auto ApplyOffset = [&](Value *V, unsigned BinOpc,
6375 Value *RHS) -> OffsetResult {
6376 if (auto *Sel = dyn_cast<SelectInst>(Val: V)) {
6377 if (!Sel->hasOneUse())
6378 return OffsetResult::invalid();
6379 Value *TrueVal = ApplyOffsetImpl(Sel->getTrueValue(), BinOpc, RHS);
6380 if (!TrueVal)
6381 return OffsetResult::invalid();
6382 Value *FalseVal = ApplyOffsetImpl(Sel->getFalseValue(), BinOpc, RHS);
6383 if (!FalseVal)
6384 return OffsetResult::invalid();
6385 return OffsetResult::select(Cond: Sel->getCondition(), TrueV: TrueVal, FalseV: FalseVal, MDFrom: Sel);
6386 }
6387 if (Value *Simplified = ApplyOffsetImpl(V, BinOpc, RHS))
6388 return OffsetResult::value(V: Simplified);
6389 return OffsetResult::invalid();
6390 };
6391
6392 for (auto [BinOp, RHS] : OffsetOps) {
6393 auto BinOpc = static_cast<unsigned>(BinOp);
6394
6395 auto Op0Result = ApplyOffset(Op0, BinOpc, RHS);
6396 if (!Op0Result.isValid())
6397 continue;
6398 auto Op1Result = ApplyOffset(Op1, BinOpc, RHS);
6399 if (!Op1Result.isValid())
6400 continue;
6401
6402 Value *NewLHS = Op0Result.materialize(Builder);
6403 Value *NewRHS = Op1Result.materialize(Builder);
6404 return new ICmpInst(I.getPredicate(), NewLHS, NewRHS);
6405 }
6406
6407 return nullptr;
6408}
6409
6410Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
6411 if (!I.isEquality())
6412 return nullptr;
6413
6414 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
6415 const CmpInst::Predicate Pred = I.getPredicate();
6416 Value *A, *B, *C, *D;
6417 if (match(V: Op0, P: m_Xor(L: m_Value(V&: A), R: m_Value(V&: B)))) {
6418 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
6419 Value *OtherVal = A == Op1 ? B : A;
6420 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(Ty: A->getType()));
6421 }
6422
6423 if (match(V: Op1, P: m_Xor(L: m_Value(V&: C), R: m_Value(V&: D)))) {
6424 // A^c1 == C^c2 --> A == C^(c1^c2)
6425 ConstantInt *C1, *C2;
6426 if (match(V: B, P: m_ConstantInt(CI&: C1)) && match(V: D, P: m_ConstantInt(CI&: C2)) &&
6427 Op1->hasOneUse()) {
6428 Constant *NC = Builder.getInt(AI: C1->getValue() ^ C2->getValue());
6429 Value *Xor = Builder.CreateXor(LHS: C, RHS: NC);
6430 return new ICmpInst(Pred, A, Xor);
6431 }
6432
6433 // A^B == A^D -> B == D
6434 if (A == C)
6435 return new ICmpInst(Pred, B, D);
6436 if (A == D)
6437 return new ICmpInst(Pred, B, C);
6438 if (B == C)
6439 return new ICmpInst(Pred, A, D);
6440 if (B == D)
6441 return new ICmpInst(Pred, A, C);
6442 }
6443 }
6444
6445 if (match(V: Op1, P: m_Xor(L: m_Value(V&: A), R: m_Value(V&: B))) && (A == Op0 || B == Op0)) {
6446 // A == (A^B) -> B == 0
6447 Value *OtherVal = A == Op0 ? B : A;
6448 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(Ty: A->getType()));
6449 }
6450
6451 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
6452 if (match(V: Op0, P: m_And(L: m_Value(V&: A), R: m_Value(V&: B))) &&
6453 match(V: Op1, P: m_And(L: m_Value(V&: C), R: m_Value(V&: D)))) {
6454 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
6455
6456 if (A == C) {
6457 X = B;
6458 Y = D;
6459 Z = A;
6460 } else if (A == D) {
6461 X = B;
6462 Y = C;
6463 Z = A;
6464 } else if (B == C) {
6465 X = A;
6466 Y = D;
6467 Z = B;
6468 } else if (B == D) {
6469 X = A;
6470 Y = C;
6471 Z = B;
6472 }
6473
6474 if (X) {
6475 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
6476 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
6477 // instructions.
6478 const APInt *C0, *C1;
6479 bool XorIsNegP2 = match(V: X, P: m_APInt(Res&: C0)) && match(V: Y, P: m_APInt(Res&: C1)) &&
6480 (*C0 ^ *C1).isNegatedPowerOf2();
6481
6482 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
6483 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
6484 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
6485 int UseCnt =
6486 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
6487 (int(match(V: X, P: m_ImmConstant()) && match(V: Y, P: m_ImmConstant())));
6488 if (XorIsNegP2 || UseCnt >= 2) {
6489 // Build (X^Y) & Z
6490 Op1 = Builder.CreateXor(LHS: X, RHS: Y);
6491 Op1 = Builder.CreateAnd(LHS: Op1, RHS: Z);
6492 return new ICmpInst(Pred, Op1, Constant::getNullValue(Ty: Op1->getType()));
6493 }
6494 }
6495 }
6496
6497 {
6498 // Similar to above, but specialized for constant because invert is needed:
6499 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
6500 Value *X, *Y;
6501 Constant *C;
6502 if (match(V: Op0, P: m_OneUse(SubPattern: m_Or(L: m_Value(V&: X), R: m_Constant(C)))) &&
6503 match(V: Op1, P: m_OneUse(SubPattern: m_Or(L: m_Value(V&: Y), R: m_Specific(V: C))))) {
6504 Value *Xor = Builder.CreateXor(LHS: X, RHS: Y);
6505 Value *And = Builder.CreateAnd(LHS: Xor, RHS: ConstantExpr::getNot(C));
6506 return new ICmpInst(Pred, And, Constant::getNullValue(Ty: And->getType()));
6507 }
6508 }
6509
6510 if (match(V: Op1, P: m_ZExt(Op: m_Value(V&: A))) &&
6511 (Op0->hasOneUse() || Op1->hasOneUse())) {
6512 // (B & (Pow2C-1)) == zext A --> A == trunc B
6513 // (B & (Pow2C-1)) != zext A --> A != trunc B
6514 const APInt *MaskC;
6515 if (match(V: Op0, P: m_And(L: m_Value(V&: B), R: m_LowBitMask(V&: MaskC))) &&
6516 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
6517 return new ICmpInst(Pred, A, Builder.CreateTrunc(V: B, DestTy: A->getType()));
6518 }
6519
6520 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
6521 // For lshr and ashr pairs.
6522 const APInt *AP1, *AP2;
6523 if ((match(V: Op0, P: m_OneUse(SubPattern: m_LShr(L: m_Value(V&: A), R: m_APIntAllowPoison(Res&: AP1)))) &&
6524 match(V: Op1, P: m_OneUse(SubPattern: m_LShr(L: m_Value(V&: B), R: m_APIntAllowPoison(Res&: AP2))))) ||
6525 (match(V: Op0, P: m_OneUse(SubPattern: m_AShr(L: m_Value(V&: A), R: m_APIntAllowPoison(Res&: AP1)))) &&
6526 match(V: Op1, P: m_OneUse(SubPattern: m_AShr(L: m_Value(V&: B), R: m_APIntAllowPoison(Res&: AP2)))))) {
6527 if (*AP1 != *AP2)
6528 return nullptr;
6529 unsigned TypeBits = AP1->getBitWidth();
6530 unsigned ShAmt = AP1->getLimitedValue(Limit: TypeBits);
6531 if (ShAmt < TypeBits && ShAmt != 0) {
6532 ICmpInst::Predicate NewPred =
6533 Pred == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
6534 Value *Xor = Builder.CreateXor(LHS: A, RHS: B, Name: I.getName() + ".unshifted");
6535 APInt CmpVal = APInt::getOneBitSet(numBits: TypeBits, BitNo: ShAmt);
6536 return new ICmpInst(NewPred, Xor, ConstantInt::get(Ty: A->getType(), V: CmpVal));
6537 }
6538 }
6539
6540 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
6541 ConstantInt *Cst1;
6542 if (match(V: Op0, P: m_OneUse(SubPattern: m_Shl(L: m_Value(V&: A), R: m_ConstantInt(CI&: Cst1)))) &&
6543 match(V: Op1, P: m_OneUse(SubPattern: m_Shl(L: m_Value(V&: B), R: m_Specific(V: Cst1))))) {
6544 unsigned TypeBits = Cst1->getBitWidth();
6545 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(Limit: TypeBits);
6546 if (ShAmt < TypeBits && ShAmt != 0) {
6547 Value *Xor = Builder.CreateXor(LHS: A, RHS: B, Name: I.getName() + ".unshifted");
6548 APInt AndVal = APInt::getLowBitsSet(numBits: TypeBits, loBitsSet: TypeBits - ShAmt);
6549 Value *And =
6550 Builder.CreateAnd(LHS: Xor, RHS: Builder.getInt(AI: AndVal), Name: I.getName() + ".mask");
6551 return new ICmpInst(Pred, And, Constant::getNullValue(Ty: Cst1->getType()));
6552 }
6553 }
6554
6555 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
6556 // "icmp (and X, mask), cst"
6557 uint64_t ShAmt = 0;
6558 if (Op0->hasOneUse() &&
6559 match(V: Op0, P: m_Trunc(Op: m_OneUse(SubPattern: m_LShr(L: m_Value(V&: A), R: m_ConstantInt(V&: ShAmt))))) &&
6560 match(V: Op1, P: m_ConstantInt(CI&: Cst1)) &&
6561 // Only do this when A has multiple uses. This is most important to do
6562 // when it exposes other optimizations.
6563 !A->hasOneUse()) {
6564 unsigned ASize = cast<IntegerType>(Val: A->getType())->getPrimitiveSizeInBits();
6565
6566 if (ShAmt < ASize) {
6567 APInt MaskV =
6568 APInt::getLowBitsSet(numBits: ASize, loBitsSet: Op0->getType()->getPrimitiveSizeInBits());
6569 MaskV <<= ShAmt;
6570
6571 APInt CmpV = Cst1->getValue().zext(width: ASize);
6572 CmpV <<= ShAmt;
6573
6574 Value *Mask = Builder.CreateAnd(LHS: A, RHS: Builder.getInt(AI: MaskV));
6575 return new ICmpInst(Pred, Mask, Builder.getInt(AI: CmpV));
6576 }
6577 }
6578
6579 if (Instruction *ICmp = foldICmpIntrinsicWithIntrinsic(Cmp&: I, Builder))
6580 return ICmp;
6581
6582 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks
6583 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s
6584 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a
6585 // few steps of instcombine.
6586 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6587 if (match(V: Op0, P: m_AShr(L: m_Trunc(Op: m_Value(V&: A)), R: m_SpecificInt(V: BitWidth - 1))) &&
6588 match(V: Op1, P: m_Trunc(Op: m_LShr(L: m_Specific(V: A), R: m_SpecificInt(V: BitWidth)))) &&
6589 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
6590 (I.getOperand(i_nocapture: 0)->hasOneUse() || I.getOperand(i_nocapture: 1)->hasOneUse())) {
6591 APInt C = APInt::getOneBitSet(numBits: BitWidth * 2, BitNo: BitWidth - 1);
6592 Value *Add = Builder.CreateAdd(LHS: A, RHS: ConstantInt::get(Ty: A->getType(), V: C));
6593 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
6594 : ICmpInst::ICMP_UGE,
6595 Add, ConstantInt::get(Ty: A->getType(), V: C.shl(shiftAmt: 1)));
6596 }
6597
6598 // Canonicalize:
6599 // Assume B_Pow2 != 0
6600 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
6601 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
6602 if (match(V: Op0, P: m_c_And(L: m_Specific(V: Op1), R: m_Value())) &&
6603 isKnownToBeAPowerOfTwo(V: Op1, /* OrZero */ false, CtxI: &I))
6604 return new ICmpInst(CmpInst::getInversePredicate(pred: Pred), Op0,
6605 ConstantInt::getNullValue(Ty: Op0->getType()));
6606
6607 if (match(V: Op1, P: m_c_And(L: m_Specific(V: Op0), R: m_Value())) &&
6608 isKnownToBeAPowerOfTwo(V: Op0, /* OrZero */ false, CtxI: &I))
6609 return new ICmpInst(CmpInst::getInversePredicate(pred: Pred), Op1,
6610 ConstantInt::getNullValue(Ty: Op1->getType()));
6611
6612 // Canonicalize:
6613 // icmp eq/ne X, OneUse(rotate-right(X))
6614 // -> icmp eq/ne X, rotate-left(X)
6615 // We generally try to convert rotate-right -> rotate-left, this just
6616 // canonicalizes another case.
6617 if (match(V: &I, P: m_c_ICmp(L: m_Value(V&: A),
6618 R: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::fshr>(
6619 Ops: m_Deferred(V: A), Ops: m_Deferred(V: A), Ops: m_Value(V&: B))))))
6620 return new ICmpInst(
6621 Pred, A,
6622 Builder.CreateIntrinsic(RetTy: Op0->getType(), ID: Intrinsic::fshl, Args: {A, A, B}));
6623
6624 // Canonicalize:
6625 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
6626 Constant *Cst;
6627 if (match(V: &I, P: m_c_ICmp(L: m_OneUse(SubPattern: m_Xor(L: m_Value(V&: A), R: m_ImmConstant(C&: Cst))),
6628 R: m_CombineAnd(Ps: m_Value(V&: B), Ps: m_Unless(P: m_ImmConstant())))))
6629 return new ICmpInst(Pred, Builder.CreateXor(LHS: A, RHS: B), Cst);
6630
6631 {
6632 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6633 auto m_Matcher =
6634 m_CombineOr(Ps: m_CombineOr(Ps: m_c_Add(L: m_Value(V&: B), R: m_Deferred(V: A)),
6635 Ps: m_c_Xor(L: m_Value(V&: B), R: m_Deferred(V: A))),
6636 Ps: m_Sub(L: m_Value(V&: B), R: m_Deferred(V: A)));
6637 std::optional<bool> IsZero = std::nullopt;
6638 if (match(V: &I, P: m_c_ICmp(L: m_OneUse(SubPattern: m_c_And(L: m_Value(V&: A), R: m_Matcher)),
6639 R: m_Deferred(V: A))))
6640 IsZero = false;
6641 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6642 else if (match(V: &I,
6643 P: m_ICmp(L: m_OneUse(SubPattern: m_c_And(L: m_Value(V&: A), R: m_Matcher)), R: m_Zero())))
6644 IsZero = true;
6645
6646 if (IsZero && isKnownToBeAPowerOfTwo(V: A, /* OrZero */ true, CtxI: &I))
6647 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6648 // -> (icmp eq/ne (and X, P2), 0)
6649 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6650 // -> (icmp eq/ne (and X, P2), P2)
6651 return new ICmpInst(Pred, Builder.CreateAnd(LHS: B, RHS: A),
6652 *IsZero ? A
6653 : ConstantInt::getNullValue(Ty: A->getType()));
6654 }
6655
6656 if (auto *Res = foldICmpEqualityWithOffset(
6657 I, Builder, SQ: getSimplifyQuery().getWithInstruction(I: &I)))
6658 return Res;
6659
6660 return nullptr;
6661}
6662
6663Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) {
6664 ICmpInst::Predicate Pred = ICmp.getPredicate();
6665 Value *Op0 = ICmp.getOperand(i_nocapture: 0), *Op1 = ICmp.getOperand(i_nocapture: 1);
6666
6667 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
6668 // The trunc masks high bits while the compare may effectively mask low bits.
6669 Value *X;
6670 const APInt *C;
6671 if (!match(V: Op0, P: m_OneUse(SubPattern: m_Trunc(Op: m_Value(V&: X)))) || !match(V: Op1, P: m_APInt(Res&: C)))
6672 return nullptr;
6673
6674 // This matches patterns corresponding to tests of the signbit as well as:
6675 // (trunc X) pred C2 --> (X & Mask) == C
6676 if (auto Res = decomposeBitTestICmp(LHS: Op0, RHS: Op1, Pred, /*LookThroughTrunc=*/true,
6677 /*AllowNonZeroC=*/true)) {
6678 Value *And = Builder.CreateAnd(LHS: Res->X, RHS: Res->Mask);
6679 Constant *C = ConstantInt::get(Ty: Res->X->getType(), V: Res->C);
6680 return new ICmpInst(Res->Pred, And, C);
6681 }
6682
6683 unsigned SrcBits = X->getType()->getScalarSizeInBits();
6684 if (auto *II = dyn_cast<IntrinsicInst>(Val: X)) {
6685 if (II->getIntrinsicID() == Intrinsic::cttz ||
6686 II->getIntrinsicID() == Intrinsic::ctlz) {
6687 unsigned MaxRet = SrcBits;
6688 // If the "is_zero_poison" argument is set, then we know at least
6689 // one bit is set in the input, so the result is always at least one
6690 // less than the full bitwidth of that input.
6691 if (match(V: II->getArgOperand(i: 1), P: m_One()))
6692 MaxRet--;
6693
6694 // Make sure the destination is wide enough to hold the largest output of
6695 // the intrinsic.
6696 if (llvm::Log2_32(Value: MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
6697 if (Instruction *I =
6698 foldICmpIntrinsicWithConstant(Cmp&: ICmp, II, C: C->zext(width: SrcBits)))
6699 return I;
6700 }
6701 }
6702
6703 return nullptr;
6704}
6705
6706Instruction *InstCombinerImpl::foldICmpWithZextOrSext(ICmpInst &ICmp) {
6707 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
6708 auto *CastOp0 = cast<CastInst>(Val: ICmp.getOperand(i_nocapture: 0));
6709 Value *X;
6710 if (!match(V: CastOp0, P: m_ZExtOrSExt(Op: m_Value(V&: X))))
6711 return nullptr;
6712
6713 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
6714 bool IsSignedCmp = ICmp.isSigned();
6715
6716 // icmp Pred (ext X), (ext Y)
6717 Value *Y;
6718 if (match(V: ICmp.getOperand(i_nocapture: 1), P: m_ZExtOrSExt(Op: m_Value(V&: Y)))) {
6719 bool IsZext0 = isa<ZExtInst>(Val: ICmp.getOperand(i_nocapture: 0));
6720 bool IsZext1 = isa<ZExtInst>(Val: ICmp.getOperand(i_nocapture: 1));
6721
6722 if (IsZext0 != IsZext1) {
6723 // If X and Y and both i1
6724 // (icmp eq/ne (zext X) (sext Y))
6725 // eq -> (icmp eq (or X, Y), 0)
6726 // ne -> (icmp ne (or X, Y), 0)
6727 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(BitWidth: 1) &&
6728 Y->getType()->isIntOrIntVectorTy(BitWidth: 1))
6729 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(LHS: X, RHS: Y),
6730 Constant::getNullValue(Ty: X->getType()));
6731
6732 // If we have mismatched casts and zext has the nneg flag, we can
6733 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
6734
6735 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(Val: ICmp.getOperand(i_nocapture: 0));
6736 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(Val: ICmp.getOperand(i_nocapture: 1));
6737
6738 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
6739 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
6740
6741 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
6742 IsSignedExt = true;
6743 else
6744 return nullptr;
6745 }
6746
6747 // Not an extension from the same type?
6748 Type *XTy = X->getType(), *YTy = Y->getType();
6749 if (XTy != YTy) {
6750 // One of the casts must have one use because we are creating a new cast.
6751 if (!ICmp.getOperand(i_nocapture: 0)->hasOneUse() && !ICmp.getOperand(i_nocapture: 1)->hasOneUse())
6752 return nullptr;
6753 // Extend the narrower operand to the type of the wider operand.
6754 CastInst::CastOps CastOpcode =
6755 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
6756 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
6757 X = Builder.CreateCast(Op: CastOpcode, V: X, DestTy: YTy);
6758 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6759 Y = Builder.CreateCast(Op: CastOpcode, V: Y, DestTy: XTy);
6760 else
6761 return nullptr;
6762 }
6763
6764 // (zext X) == (zext Y) --> X == Y
6765 // (sext X) == (sext Y) --> X == Y
6766 if (ICmp.isEquality())
6767 return new ICmpInst(ICmp.getPredicate(), X, Y);
6768
6769 // A signed comparison of sign extended values simplifies into a
6770 // signed comparison.
6771 if (IsSignedCmp && IsSignedExt)
6772 return new ICmpInst(ICmp.getPredicate(), X, Y);
6773
6774 // The other three cases all fold into an unsigned comparison.
6775 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6776 }
6777
6778 // Below here, we are only folding a compare with constant.
6779 auto *C = dyn_cast<Constant>(Val: ICmp.getOperand(i_nocapture: 1));
6780 if (!C)
6781 return nullptr;
6782
6783 // If a lossless truncate is possible...
6784 Type *SrcTy = CastOp0->getSrcTy();
6785 Constant *Res = getLosslessInvCast(C, InvCastTo: SrcTy, CastOp: CastOp0->getOpcode(), DL);
6786 if (Res) {
6787 if (ICmp.isEquality())
6788 return new ICmpInst(ICmp.getPredicate(), X, Res);
6789
6790 // A signed comparison of sign extended values simplifies into a
6791 // signed comparison.
6792 if (IsSignedExt && IsSignedCmp)
6793 return new ICmpInst(ICmp.getPredicate(), X, Res);
6794
6795 // The other three cases all fold into an unsigned comparison.
6796 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6797 }
6798
6799 // The re-extended constant changed, partly changed (in the case of a vector),
6800 // or could not be determined to be equal (in the case of a constant
6801 // expression), so the constant cannot be represented in the shorter type.
6802 // All the cases that fold to true or false will have already been handled
6803 // by simplifyICmpInst, so only deal with the tricky case.
6804 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(Val: C))
6805 return nullptr;
6806
6807 // Is source op positive?
6808 // icmp ult (sext X), C --> icmp sgt X, -1
6809 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6810 return new ICmpInst(CmpInst::ICMP_SGT, X, Constant::getAllOnesValue(Ty: SrcTy));
6811
6812 // Is source op negative?
6813 // icmp ugt (sext X), C --> icmp slt X, 0
6814 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6815 return new ICmpInst(CmpInst::ICMP_SLT, X, Constant::getNullValue(Ty: SrcTy));
6816}
6817
6818/// Handle icmp (cast x), (cast or constant).
6819Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
6820 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6821 // icmp compares only pointer's value.
6822 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6823 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(Val: ICmp.getOperand(i_nocapture: 0));
6824 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(Val: ICmp.getOperand(i_nocapture: 1));
6825 if (SimplifiedOp0 || SimplifiedOp1)
6826 return new ICmpInst(ICmp.getPredicate(),
6827 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(i_nocapture: 0),
6828 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(i_nocapture: 1));
6829
6830 auto *CastOp0 = dyn_cast<CastInst>(Val: ICmp.getOperand(i_nocapture: 0));
6831 Value *Op1 = ICmp.getOperand(i_nocapture: 1);
6832 if (!CastOp0)
6833 return nullptr;
6834 if (!isa<Constant>(Val: ICmp.getOperand(i_nocapture: 1)) && !isa<CastInst>(Val: ICmp.getOperand(i_nocapture: 1)))
6835 return nullptr;
6836
6837 Value *Op0Src = CastOp0->getOperand(i_nocapture: 0);
6838 Type *SrcTy = CastOp0->getSrcTy();
6839 Type *DestTy = CastOp0->getDestTy();
6840
6841 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6842 // integer type is the same size as the pointer type.
6843 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6844 unsigned IntWidth = IntTy->getScalarType()->getIntegerBitWidth();
6845 unsigned IndexWidth = DL.getAddressSizeInBits(Ty: PtrTy);
6846 unsigned PtrWidth = DL.getPointerTypeSizeInBits(PtrTy);
6847 // For ptrtoint/inttoptr, we must check that IntWidth == IndexWidth and also
6848 // IndexWidth == PtrWidth to (not) handle non-integral pointers.
6849 return IntWidth == IndexWidth && IndexWidth == PtrWidth;
6850 };
6851 if (isa<PtrToIntInst, PtrToAddrInst>(Val: CastOp0)) {
6852 bool HasPtrToInt = isa<PtrToIntInst>(Val: CastOp0);
6853 Value *NewOp1 = nullptr;
6854 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(Val: Op1)) {
6855 NewOp1 = PtrToIntOp1->getOperand(i_nocapture: 0);
6856 HasPtrToInt = true;
6857 } else if (auto *PtrToAddrOp1 = dyn_cast<PtrToAddrOperator>(Val: Op1)) {
6858 NewOp1 = PtrToAddrOp1->getOperand(i_nocapture: 0);
6859 } else if (auto *RHSC = dyn_cast<Constant>(Val: Op1)) {
6860 NewOp1 = ConstantExpr::getIntToPtr(C: RHSC, Ty: SrcTy);
6861 }
6862
6863 // For ptrtoaddr, IntWidth == IndexWidth is implied and we don't need to
6864 // check PtrWidth.
6865 if ((!HasPtrToInt || CompatibleSizes(SrcTy, DestTy)) &&
6866 (NewOp1 && NewOp1->getType() == Op0Src->getType()))
6867 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6868 }
6869
6870 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6871 if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6872 CompatibleSizes(DestTy, SrcTy)) {
6873 Value *NewOp1 = nullptr;
6874 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(Val: Op1)) {
6875 Value *IntSrc = IntToPtrOp1->getOperand(i_nocapture: 0);
6876 if (IntSrc->getType() == Op0Src->getType())
6877 NewOp1 = IntToPtrOp1->getOperand(i_nocapture: 0);
6878 } else if (auto *RHSC = dyn_cast<Constant>(Val: Op1)) {
6879 NewOp1 = ConstantFoldConstant(C: ConstantExpr::getPtrToInt(C: RHSC, Ty: SrcTy), DL);
6880 }
6881
6882 if (NewOp1)
6883 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6884 }
6885
6886 if (Instruction *R = foldICmpWithTrunc(ICmp))
6887 return R;
6888
6889 return foldICmpWithZextOrSext(ICmp);
6890}
6891
6892static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS,
6893 bool IsSigned) {
6894 switch (BinaryOp) {
6895 default:
6896 llvm_unreachable("Unsupported binary op");
6897 case Instruction::Add:
6898 case Instruction::Sub:
6899 return match(V: RHS, P: m_Zero());
6900 case Instruction::Mul:
6901 return !(RHS->getType()->isIntOrIntVectorTy(BitWidth: 1) && IsSigned) &&
6902 match(V: RHS, P: m_One());
6903 }
6904}
6905
6906OverflowResult
6907InstCombinerImpl::computeOverflow(Instruction::BinaryOps BinaryOp,
6908 bool IsSigned, Value *LHS, Value *RHS,
6909 Instruction *CtxI) const {
6910 switch (BinaryOp) {
6911 default:
6912 llvm_unreachable("Unsupported binary op");
6913 case Instruction::Add:
6914 if (IsSigned)
6915 return computeOverflowForSignedAdd(LHS, RHS, CtxI);
6916 else
6917 return computeOverflowForUnsignedAdd(LHS, RHS, CtxI);
6918 case Instruction::Sub:
6919 if (IsSigned)
6920 return computeOverflowForSignedSub(LHS, RHS, CtxI);
6921 else
6922 return computeOverflowForUnsignedSub(LHS, RHS, CtxI);
6923 case Instruction::Mul:
6924 if (IsSigned)
6925 return computeOverflowForSignedMul(LHS, RHS, CtxI);
6926 else
6927 return computeOverflowForUnsignedMul(LHS, RHS, CtxI);
6928 }
6929}
6930
6931bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6932 bool IsSigned, Value *LHS,
6933 Value *RHS, Instruction &OrigI,
6934 Value *&Result,
6935 Constant *&Overflow) {
6936 if (OrigI.isCommutative() && isa<Constant>(Val: LHS) && !isa<Constant>(Val: RHS))
6937 std::swap(a&: LHS, b&: RHS);
6938
6939 // If the overflow check was an add followed by a compare, the insertion point
6940 // may be pointing to the compare. We want to insert the new instructions
6941 // before the add in case there are uses of the add between the add and the
6942 // compare.
6943 Builder.SetInsertPoint(&OrigI);
6944
6945 Type *OverflowTy = Type::getInt1Ty(C&: LHS->getContext());
6946 if (auto *LHSTy = dyn_cast<VectorType>(Val: LHS->getType()))
6947 OverflowTy = VectorType::get(ElementType: OverflowTy, EC: LHSTy->getElementCount());
6948
6949 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6950 Result = LHS;
6951 Overflow = ConstantInt::getFalse(Ty: OverflowTy);
6952 return true;
6953 }
6954
6955 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, CtxI: &OrigI)) {
6956 case OverflowResult::MayOverflow:
6957 return false;
6958 case OverflowResult::AlwaysOverflowsLow:
6959 case OverflowResult::AlwaysOverflowsHigh:
6960 Result = Builder.CreateBinOp(Opc: BinaryOp, LHS, RHS);
6961 Result->takeName(V: &OrigI);
6962 Overflow = ConstantInt::getTrue(Ty: OverflowTy);
6963 return true;
6964 case OverflowResult::NeverOverflows:
6965 Result = Builder.CreateBinOp(Opc: BinaryOp, LHS, RHS);
6966 Result->takeName(V: &OrigI);
6967 Overflow = ConstantInt::getFalse(Ty: OverflowTy);
6968 if (auto *Inst = dyn_cast<Instruction>(Val: Result)) {
6969 if (IsSigned)
6970 Inst->setHasNoSignedWrap();
6971 else
6972 Inst->setHasNoUnsignedWrap();
6973 }
6974 return true;
6975 }
6976
6977 llvm_unreachable("Unexpected overflow result");
6978}
6979
6980/// Recognize and process idiom involving test for unsigned
6981/// overflow.
6982///
6983/// The caller has matched a pattern of the form:
6984/// I = cmp u (add(zext A, zext B), V
6985/// I = cmp u (mul(zext A, zext B), V
6986/// The function checks if this is a test for overflow and if so replaces
6987/// addition/multiplication with call to the umul intrinsic or the canonical
6988/// form of uadd overflow.
6989///
6990/// \param I Compare instruction.
6991/// \param Val Result of add/mul instruction. It is one of the arguments of
6992/// the compare instruction. Must be of integer type.
6993/// \param OtherVal The other argument of compare instruction.
6994/// \returns Instruction which must replace the compare instruction, NULL if no
6995/// replacement required.
6996static Instruction *processUZExtIdiom(ICmpInst &I, Value *Val,
6997 const APInt *OtherVal,
6998 InstCombinerImpl &IC) {
6999 // Don't bother doing this transformation for pointers, don't do it for
7000 // vectors.
7001 if (!isa<IntegerType>(Val: Val->getType()))
7002 return nullptr;
7003
7004 auto *Instr = cast<Instruction>(Val);
7005 unsigned Opcode = Instr->getOpcode();
7006 assert(Opcode == Instruction::Add || Opcode == Instruction::Mul);
7007
7008 auto *LHS = cast<ZExtInst>(Val: Instr->getOperand(i: 0)),
7009 *RHS = cast<ZExtInst>(Val: Instr->getOperand(i: 1));
7010 Value *A = LHS->getOperand(i_nocapture: 0), *B = RHS->getOperand(i_nocapture: 0);
7011
7012 // Calculate type and width of the result produced by add/mul.with.overflow.
7013 Type *TyA = A->getType(), *TyB = B->getType();
7014 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
7015 WidthB = TyB->getPrimitiveSizeInBits();
7016 unsigned ResultWidth;
7017 Type *ResultType;
7018 if (WidthB > WidthA) {
7019 ResultWidth = WidthB;
7020 ResultType = TyB;
7021 } else {
7022 ResultWidth = WidthA;
7023 ResultType = TyA;
7024 }
7025
7026 // In order to replace the original result with a narrower one, all uses must
7027 // ignore upper bits of the result. The number of used low bits must be not
7028 // greater than the width of add or mul.with.overflow.
7029 if (Val->hasNUsesOrMore(N: 2))
7030 for (User *U : Val->users()) {
7031 if (U == &I)
7032 continue;
7033 if (TruncInst *TI = dyn_cast<TruncInst>(Val: U)) {
7034 // Check if truncation ignores bits above ResultWidth.
7035 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
7036 if (TruncWidth > ResultWidth)
7037 return nullptr;
7038 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Val: U)) {
7039 // Check if AND ignores bits above ResultWidth.
7040 if (BO->getOpcode() != Instruction::And)
7041 return nullptr;
7042 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: BO->getOperand(i_nocapture: 1))) {
7043 const APInt &CVal = CI->getValue();
7044 if (CVal.getBitWidth() - CVal.countl_zero() > ResultWidth)
7045 return nullptr;
7046 } else {
7047 // In this case we could have the operand of the binary operation
7048 // being defined in another block, and performing the replacement
7049 // could break the dominance relation.
7050 return nullptr;
7051 }
7052 } else {
7053 // Other uses prohibit this transformation.
7054 return nullptr;
7055 }
7056 }
7057
7058 // Recognize patterns
7059 switch (I.getPredicate()) {
7060 case ICmpInst::ICMP_UGT: {
7061 // Recognize pattern:
7062 // val = add/mul(zext A, zext B)
7063 // cmp ugt val, max
7064 APInt MaxVal = APInt::getMaxValue(numBits: ResultWidth);
7065 MaxVal = MaxVal.zext(width: OtherVal->getBitWidth());
7066 if (MaxVal.eq(RHS: *OtherVal))
7067 break; // Recognized
7068 return nullptr;
7069 }
7070
7071 case ICmpInst::ICMP_ULT: {
7072 // Recognize pattern:
7073 // val = add/mul(zext A, zext B)
7074 // cmp ult val, max + 1
7075 APInt MaxVal = APInt::getOneBitSet(numBits: OtherVal->getBitWidth(), BitNo: ResultWidth);
7076 if (MaxVal.eq(RHS: *OtherVal))
7077 break; // Recognized
7078 return nullptr;
7079 }
7080
7081 default:
7082 return nullptr;
7083 }
7084
7085 InstCombiner::BuilderTy &Builder = IC.Builder;
7086 Builder.SetInsertPoint(Instr);
7087
7088 // Replace: add/mul(zext A, zext B) --> canonical add/mul + overflow check
7089 Value *ResultA = A, *ResultB = B;
7090 if (WidthA < ResultWidth)
7091 ResultA = Builder.CreateZExt(V: A, DestTy: ResultType);
7092 if (WidthB < ResultWidth)
7093 ResultB = Builder.CreateZExt(V: B, DestTy: ResultType);
7094
7095 Value *ArithResult;
7096 Value *OverflowCheck;
7097
7098 if (Opcode == Instruction::Add) {
7099 // Canonical add overflow check: add + compare
7100 ArithResult = Builder.CreateAdd(LHS: ResultA, RHS: ResultB, Name: "add");
7101 // Overflow if result < either operand (for unsigned add)
7102 if (I.getPredicate() == ICmpInst::ICMP_ULT)
7103 OverflowCheck =
7104 Builder.CreateICmpUGE(LHS: ArithResult, RHS: ResultA, Name: "not.add.overflow");
7105 else
7106 OverflowCheck =
7107 Builder.CreateICmpULT(LHS: ArithResult, RHS: ResultA, Name: "add.overflow");
7108 } else {
7109 // For multiplication, the intrinsic is actually the canonical form
7110 Value *Call = Builder.CreateIntrinsic(ID: Intrinsic::umul_with_overflow,
7111 OverloadTypes: ResultType, Args: {ResultA, ResultB},
7112 /*FMFSource=*/nullptr, Name: "umul");
7113 ArithResult = Builder.CreateExtractValue(Agg: Call, Idxs: 0, Name: "umul.value");
7114 OverflowCheck = Builder.CreateExtractValue(Agg: Call, Idxs: 1, Name: "umul.overflow");
7115 if (I.getPredicate() == ICmpInst::ICMP_ULT)
7116 OverflowCheck = Builder.CreateNot(V: OverflowCheck);
7117 }
7118
7119 IC.addToWorklist(I: Instr);
7120
7121 // Replace uses of the original add/mul result with the new arithmetic result
7122 if (Val->hasNUsesOrMore(N: 2)) {
7123 for (User *U : make_early_inc_range(Range: Val->users())) {
7124 if (U == &I)
7125 continue;
7126 if (TruncInst *TI = dyn_cast<TruncInst>(Val: U)) {
7127 if (TI->getType()->getPrimitiveSizeInBits() == ResultWidth)
7128 IC.replaceInstUsesWith(I&: *TI, V: ArithResult);
7129 else
7130 TI->setOperand(i_nocapture: 0, Val_nocapture: ArithResult);
7131 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Val: U)) {
7132 assert(BO->getOpcode() == Instruction::And);
7133 // Replace (ArithResult & mask) --> zext (ArithResult & short_mask)
7134 ConstantInt *CI = cast<ConstantInt>(Val: BO->getOperand(i_nocapture: 1));
7135 APInt ShortMask = CI->getValue().trunc(width: ResultWidth);
7136 Value *ShortAnd = Builder.CreateAnd(LHS: ArithResult, RHS: ShortMask);
7137 Value *Zext = Builder.CreateZExt(V: ShortAnd, DestTy: BO->getType());
7138 IC.replaceInstUsesWith(I&: *BO, V: Zext);
7139 } else {
7140 llvm_unreachable("Unexpected Binary operation");
7141 }
7142 IC.addToWorklist(I: cast<Instruction>(Val: U));
7143 }
7144 }
7145
7146 return IC.replaceInstUsesWith(I, V: OverflowCheck);
7147}
7148
7149/// When performing a comparison against a constant, it is possible that not all
7150/// the bits in the LHS are demanded. This helper method computes the mask that
7151/// IS demanded.
7152static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth) {
7153 const APInt *RHS;
7154 if (!match(V: I.getOperand(i_nocapture: 1), P: m_APInt(Res&: RHS)))
7155 return APInt::getAllOnes(numBits: BitWidth);
7156
7157 // If this is a normal comparison, it demands all bits. If it is a sign bit
7158 // comparison, it only demands the sign bit.
7159 bool UnusedBit;
7160 if (isSignBitCheck(Pred: I.getPredicate(), RHS: *RHS, TrueIfSigned&: UnusedBit))
7161 return APInt::getSignMask(BitWidth);
7162
7163 switch (I.getPredicate()) {
7164 // For a UGT comparison, we don't care about any bits that
7165 // correspond to the trailing ones of the comparand. The value of these
7166 // bits doesn't impact the outcome of the comparison, because any value
7167 // greater than the RHS must differ in a bit higher than these due to carry.
7168 case ICmpInst::ICMP_UGT:
7169 return APInt::getBitsSetFrom(numBits: BitWidth, loBit: RHS->countr_one());
7170
7171 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
7172 // Any value less than the RHS must differ in a higher bit because of carries.
7173 case ICmpInst::ICMP_ULT:
7174 return APInt::getBitsSetFrom(numBits: BitWidth, loBit: RHS->countr_zero());
7175
7176 default:
7177 return APInt::getAllOnes(numBits: BitWidth);
7178 }
7179}
7180
7181/// Check that one use is in the same block as the definition and all
7182/// other uses are in blocks dominated by a given block.
7183///
7184/// \param DI Definition
7185/// \param UI Use
7186/// \param DB Block that must dominate all uses of \p DI outside
7187/// the parent block
7188/// \return true when \p UI is the only use of \p DI in the parent block
7189/// and all other uses of \p DI are in blocks dominated by \p DB.
7190///
7191bool InstCombinerImpl::dominatesAllUses(const Instruction *DI,
7192 const Instruction *UI,
7193 const BasicBlock *DB) const {
7194 assert(DI && UI && "Instruction not defined\n");
7195 // Ignore incomplete definitions.
7196 if (!DI->getParent())
7197 return false;
7198 // DI and UI must be in the same block.
7199 if (DI->getParent() != UI->getParent())
7200 return false;
7201 // Protect from self-referencing blocks.
7202 if (DI->getParent() == DB)
7203 return false;
7204 for (const User *U : DI->users()) {
7205 auto *Usr = cast<Instruction>(Val: U);
7206 if (Usr != UI && !DT.dominates(A: DB, B: Usr->getParent()))
7207 return false;
7208 }
7209 return true;
7210}
7211
7212/// Return true when the instruction sequence within a block is select-cmp-br.
7213static bool isChainSelectCmpBranch(const SelectInst *SI) {
7214 const BasicBlock *BB = SI->getParent();
7215 if (!BB)
7216 return false;
7217 auto *BI = dyn_cast<CondBrInst>(Val: BB->getTerminator());
7218 if (!BI)
7219 return false;
7220 auto *IC = dyn_cast<ICmpInst>(Val: BI->getCondition());
7221 if (!IC || (IC->getOperand(i_nocapture: 0) != SI && IC->getOperand(i_nocapture: 1) != SI))
7222 return false;
7223 return true;
7224}
7225
7226/// True when a select result is replaced by one of its operands
7227/// in select-icmp sequence. This will eventually result in the elimination
7228/// of the select.
7229///
7230/// \param SI Select instruction
7231/// \param Icmp Compare instruction
7232/// \param SIOpd Operand that replaces the select
7233///
7234/// Notes:
7235/// - The replacement is global and requires dominator information
7236/// - The caller is responsible for the actual replacement
7237///
7238/// Example:
7239///
7240/// entry:
7241/// %4 = select i1 %3, %C* %0, %C* null
7242/// %5 = icmp eq %C* %4, null
7243/// br i1 %5, label %9, label %7
7244/// ...
7245/// ; <label>:7 ; preds = %entry
7246/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
7247/// ...
7248///
7249/// can be transformed to
7250///
7251/// %5 = icmp eq %C* %0, null
7252/// %6 = select i1 %3, i1 %5, i1 true
7253/// br i1 %6, label %9, label %7
7254/// ...
7255/// ; <label>:7 ; preds = %entry
7256/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
7257///
7258/// Similar when the first operand of the select is a constant or/and
7259/// the compare is for not equal rather than equal.
7260///
7261/// NOTE: The function is only called when the select and compare constants
7262/// are equal, the optimization can work only for EQ predicates. This is not a
7263/// major restriction since a NE compare should be 'normalized' to an equal
7264/// compare, which usually happens in the combiner and test case
7265/// select-cmp-br.ll checks for it.
7266bool InstCombinerImpl::replacedSelectWithOperand(SelectInst *SI,
7267 const ICmpInst *Icmp,
7268 const unsigned SIOpd) {
7269 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
7270 if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) {
7271 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(Idx: 1);
7272 // The check for the single predecessor is not the best that can be
7273 // done. But it protects efficiently against cases like when SI's
7274 // home block has two successors, Succ and Succ1, and Succ1 predecessor
7275 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
7276 // replaced can be reached on either path. So the uniqueness check
7277 // guarantees that the path all uses of SI (outside SI's parent) are on
7278 // is disjoint from all other paths out of SI. But that information
7279 // is more expensive to compute, and the trade-off here is in favor
7280 // of compile-time. It should also be noticed that we check for a single
7281 // predecessor and not only uniqueness. This to handle the situation when
7282 // Succ and Succ1 points to the same basic block.
7283 if (Succ->getSinglePredecessor() && dominatesAllUses(DI: SI, UI: Icmp, DB: Succ)) {
7284 NumSel++;
7285 SI->replaceUsesOutsideBlock(V: SI->getOperand(i_nocapture: SIOpd), BB: SI->getParent());
7286 return true;
7287 }
7288 }
7289 return false;
7290}
7291
7292/// Try to fold the comparison based on range information we can get by checking
7293/// whether bits are known to be zero or one in the inputs.
7294Instruction *InstCombinerImpl::foldICmpUsingKnownBits(ICmpInst &I) {
7295 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
7296 Type *Ty = Op0->getType();
7297 ICmpInst::Predicate Pred = I.getPredicate();
7298
7299 // Get scalar or pointer size.
7300 unsigned BitWidth = Ty->isIntOrIntVectorTy()
7301 ? Ty->getScalarSizeInBits()
7302 : DL.getPointerTypeSizeInBits(Ty->getScalarType());
7303
7304 if (!BitWidth)
7305 return nullptr;
7306
7307 KnownBits Op0Known(BitWidth);
7308 KnownBits Op1Known(BitWidth);
7309
7310 {
7311 // Don't use dominating conditions when folding icmp using known bits. This
7312 // may convert signed into unsigned predicates in ways that other passes
7313 // (especially IndVarSimplify) may not be able to reliably undo.
7314 SimplifyQuery Q = SQ.getWithoutDomCondCache().getWithInstruction(I: &I);
7315 if (SimplifyDemandedBits(I: &I, Op: 0, DemandedMask: getDemandedBitsLHSMask(I, BitWidth),
7316 Known&: Op0Known, Q))
7317 return &I;
7318
7319 if (SimplifyDemandedBits(I: &I, Op: 1, DemandedMask: APInt::getAllOnes(numBits: BitWidth), Known&: Op1Known, Q))
7320 return &I;
7321 }
7322
7323 // If an unsigned samesign comparison is not poison, both operands have the
7324 // same sign bit. Propagate a known sign bit between the temporary KnownBits
7325 // values so the existing range folds can use that constraint.
7326 if (I.hasSameSign() && I.isUnsigned()) {
7327 auto PropagateSignBit = [](const KnownBits &From, KnownBits &To) {
7328 if (To.isNegative() || To.isNonNegative())
7329 return;
7330 if (From.isNegative())
7331 To.makeNegative();
7332 else if (From.isNonNegative())
7333 To.makeNonNegative();
7334 };
7335 PropagateSignBit(Op0Known, Op1Known);
7336 PropagateSignBit(Op1Known, Op0Known);
7337 }
7338
7339 if (!isa<Constant>(Val: Op0) && Op0Known.isConstant())
7340 return new ICmpInst(
7341 Pred, ConstantExpr::getIntegerValue(Ty, V: Op0Known.getConstant()), Op1);
7342 if (!isa<Constant>(Val: Op1) && Op1Known.isConstant())
7343 return new ICmpInst(
7344 Pred, Op0, ConstantExpr::getIntegerValue(Ty, V: Op1Known.getConstant()));
7345
7346 if (std::optional<bool> Res = ICmpInst::compare(LHS: Op0Known, RHS: Op1Known, Pred))
7347 return replaceInstUsesWith(I, V: ConstantInt::getBool(Ty: I.getType(), V: *Res));
7348
7349 // Given the known and unknown bits, compute a range that the LHS could be
7350 // in. Compute the Min, Max and RHS values based on the known bits. For the
7351 // EQ and NE we use unsigned values.
7352 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
7353 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
7354 if (I.isSigned()) {
7355 Op0Min = Op0Known.getSignedMinValue();
7356 Op0Max = Op0Known.getSignedMaxValue();
7357 Op1Min = Op1Known.getSignedMinValue();
7358 Op1Max = Op1Known.getSignedMaxValue();
7359 } else {
7360 Op0Min = Op0Known.getMinValue();
7361 Op0Max = Op0Known.getMaxValue();
7362 Op1Min = Op1Known.getMinValue();
7363 Op1Max = Op1Known.getMaxValue();
7364 }
7365
7366 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
7367 // min/max canonical compare with some other compare. That could lead to
7368 // conflict with select canonicalization and infinite looping.
7369 // FIXME: This constraint may go away if min/max intrinsics are canonical.
7370 auto isMinMaxCmp = [&](Instruction &Cmp) {
7371 if (!Cmp.hasOneUse())
7372 return false;
7373 Value *A, *B;
7374 SelectPatternFlavor SPF = matchSelectPattern(V: Cmp.user_back(), LHS&: A, RHS&: B).Flavor;
7375 if (!SelectPatternResult::isMinOrMax(SPF))
7376 return false;
7377 return match(V: Op0, P: m_MaxOrMin(Op0: m_Value(), Op1: m_Value())) ||
7378 match(V: Op1, P: m_MaxOrMin(Op0: m_Value(), Op1: m_Value()));
7379 };
7380 if (!isMinMaxCmp(I)) {
7381 switch (Pred) {
7382 default:
7383 break;
7384 case ICmpInst::ICMP_ULT: {
7385 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
7386 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7387 const APInt *CmpC;
7388 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7389 // A <u C -> A == C-1 if min(A)+1 == C
7390 if (*CmpC == Op0Min + 1)
7391 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7392 ConstantInt::get(Ty: Op1->getType(), V: *CmpC - 1));
7393 // X <u C --> X == 0, if the number of zero bits in the bottom of X
7394 // exceeds the log2 of C.
7395 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
7396 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7397 Constant::getNullValue(Ty: Op1->getType()));
7398 }
7399 break;
7400 }
7401 case ICmpInst::ICMP_UGT: {
7402 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
7403 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7404 const APInt *CmpC;
7405 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7406 // A >u C -> A == C+1 if max(a)-1 == C
7407 if (*CmpC == Op0Max - 1)
7408 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7409 ConstantInt::get(Ty: Op1->getType(), V: *CmpC + 1));
7410 // X >u C --> X != 0, if the number of zero bits in the bottom of X
7411 // exceeds the log2 of C.
7412 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
7413 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
7414 Constant::getNullValue(Ty: Op1->getType()));
7415 }
7416 break;
7417 }
7418 case ICmpInst::ICMP_SLT: {
7419 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
7420 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7421 const APInt *CmpC;
7422 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7423 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
7424 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7425 ConstantInt::get(Ty: Op1->getType(), V: *CmpC - 1));
7426 }
7427 break;
7428 }
7429 case ICmpInst::ICMP_SGT: {
7430 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
7431 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7432 const APInt *CmpC;
7433 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7434 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
7435 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7436 ConstantInt::get(Ty: Op1->getType(), V: *CmpC + 1));
7437 }
7438 break;
7439 }
7440 }
7441 }
7442
7443 // Based on the range information we know about the LHS, see if we can
7444 // simplify this comparison. For example, (x&4) < 8 is always true.
7445 switch (Pred) {
7446 default:
7447 break;
7448 case ICmpInst::ICMP_EQ:
7449 case ICmpInst::ICMP_NE: {
7450 // If all bits are known zero except for one, then we know at most one bit
7451 // is set. If the comparison is against zero, then this is a check to see if
7452 // *that* bit is set.
7453 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
7454 if (Op1Known.isZero()) {
7455 // If the LHS is an AND with the same constant, look through it.
7456 Value *LHS = nullptr;
7457 const APInt *LHSC;
7458 if (!match(V: Op0, P: m_And(L: m_Value(V&: LHS), R: m_APInt(Res&: LHSC))) ||
7459 *LHSC != Op0KnownZeroInverted)
7460 LHS = Op0;
7461
7462 Value *X;
7463 const APInt *C1;
7464 if (match(V: LHS, P: m_Shl(L: m_Power2(V&: C1), R: m_Value(V&: X)))) {
7465 Type *XTy = X->getType();
7466 unsigned Log2C1 = C1->countr_zero();
7467 APInt C2 = Op0KnownZeroInverted;
7468 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
7469 if (C2Pow2.isPowerOf2()) {
7470 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
7471 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
7472 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
7473 unsigned Log2C2 = C2Pow2.countr_zero();
7474 auto *CmpC = ConstantInt::get(Ty: XTy, V: Log2C2 - Log2C1);
7475 auto NewPred =
7476 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT;
7477 return new ICmpInst(NewPred, X, CmpC);
7478 }
7479 }
7480 }
7481
7482 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
7483 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
7484 (Op0Known & Op1Known) == Op0Known)
7485 return new ICmpInst(CmpInst::getInversePredicate(pred: Pred), Op0,
7486 ConstantInt::getNullValue(Ty: Op1->getType()));
7487 break;
7488 }
7489 case ICmpInst::ICMP_SGE:
7490 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
7491 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7492 break;
7493 case ICmpInst::ICMP_SLE:
7494 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
7495 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7496 break;
7497 case ICmpInst::ICMP_UGE:
7498 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
7499 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7500 break;
7501 case ICmpInst::ICMP_ULE:
7502 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
7503 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7504 break;
7505 }
7506
7507 // Turn a signed comparison into an unsigned one if both operands are known to
7508 // have the same sign. Set samesign if possible (except for equality
7509 // predicates).
7510 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&
7511 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
7512 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {
7513 I.setPredicate(I.getUnsignedPredicate());
7514 I.setSameSign();
7515 return &I;
7516 }
7517
7518 return nullptr;
7519}
7520
7521/// If one operand of an icmp is effectively a bool (value range of {0,1}),
7522/// then try to reduce patterns based on that limit.
7523Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
7524 Value *X, *Y;
7525 CmpPredicate Pred;
7526
7527 // X must be 0 and bool must be true for "ULT":
7528 // X <u (zext i1 Y) --> (X == 0) & Y
7529 if (match(V: &I, P: m_c_ICmp(Pred, L: m_Value(V&: X), R: m_OneUse(SubPattern: m_ZExt(Op: m_Value(V&: Y))))) &&
7530 Y->getType()->isIntOrIntVectorTy(BitWidth: 1) && Pred == ICmpInst::ICMP_ULT)
7531 return BinaryOperator::CreateAnd(V1: Builder.CreateIsNull(Arg: X), V2: Y);
7532
7533 // X must be 0 or bool must be true for "ULE":
7534 // X <=u (sext i1 Y) --> (X == 0) | Y
7535 if (match(V: &I, P: m_c_ICmp(Pred, L: m_Value(V&: X), R: m_OneUse(SubPattern: m_SExt(Op: m_Value(V&: Y))))) &&
7536 Y->getType()->isIntOrIntVectorTy(BitWidth: 1) && Pred == ICmpInst::ICMP_ULE)
7537 return BinaryOperator::CreateOr(V1: Builder.CreateIsNull(Arg: X), V2: Y);
7538
7539 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
7540 CmpPredicate Pred1, Pred2;
7541 const APInt *C;
7542 Instruction *ExtI;
7543 if (match(V: &I, P: m_c_ICmp(Pred&: Pred1, L: m_Value(V&: X),
7544 R: m_CombineAnd(Ps: m_Instruction(I&: ExtI),
7545 Ps: m_ZExtOrSExt(Op: m_ICmp(Pred&: Pred2, L: m_Deferred(V: X),
7546 R: m_APInt(Res&: C)))))) &&
7547 ICmpInst::isEquality(P: Pred1) && ICmpInst::isEquality(P: Pred2)) {
7548 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
7549 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(i: 0)->hasOneUse();
7550 auto CreateRangeCheck = [&] {
7551 Value *CmpV1 =
7552 Builder.CreateICmp(P: Pred1, LHS: X, RHS: Constant::getNullValue(Ty: X->getType()));
7553 Value *CmpV2 = Builder.CreateICmp(
7554 P: Pred1, LHS: X, RHS: ConstantInt::getSigned(Ty: X->getType(), V: IsSExt ? -1 : 1));
7555 return BinaryOperator::Create(
7556 Op: Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
7557 S1: CmpV1, S2: CmpV2);
7558 };
7559 if (C->isZero()) {
7560 if (Pred2 == ICmpInst::ICMP_EQ) {
7561 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
7562 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
7563 return replaceInstUsesWith(
7564 I, V: ConstantInt::getBool(Ty: I.getType(), V: Pred1 == ICmpInst::ICMP_NE));
7565 } else if (!IsSExt || HasOneUse) {
7566 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
7567 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
7568 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
7569 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X != -1
7570 return CreateRangeCheck();
7571 }
7572 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
7573 if (Pred2 == ICmpInst::ICMP_NE) {
7574 // icmp eq X, (zext (icmp ne X, 1)) --> false
7575 // icmp ne X, (zext (icmp ne X, 1)) --> true
7576 // icmp eq X, (sext (icmp ne X, -1)) --> false
7577 // icmp ne X, (sext (icmp ne X, -1)) --> true
7578 return replaceInstUsesWith(
7579 I, V: ConstantInt::getBool(Ty: I.getType(), V: Pred1 == ICmpInst::ICMP_NE));
7580 } else if (!IsSExt || HasOneUse) {
7581 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
7582 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
7583 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
7584 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
7585 return CreateRangeCheck();
7586 }
7587 } else {
7588 // when C != 0 && C != 1:
7589 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
7590 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
7591 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
7592 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
7593 // when C != 0 && C != -1:
7594 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
7595 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
7596 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
7597 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
7598 return ICmpInst::Create(
7599 Op: Instruction::ICmp, Pred: Pred1, S1: X,
7600 S2: ConstantInt::getSigned(Ty: X->getType(), V: Pred2 == ICmpInst::ICMP_NE
7601 ? (IsSExt ? -1 : 1)
7602 : 0));
7603 }
7604 }
7605
7606 return nullptr;
7607}
7608
7609/// If we have an icmp le or icmp ge instruction with a constant operand, turn
7610/// it into the appropriate icmp lt or icmp gt instruction. This transform
7611/// allows them to be folded in visitICmpInst.
7612static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
7613 CmpPredicate Pred = I.getCmpPredicate();
7614 if (ICmpInst::isEquality(P: Pred) || !ICmpInst::isIntPredicate(P: Pred) ||
7615 InstCombiner::isCanonicalPredicate(Pred))
7616 return nullptr;
7617
7618 Value *Op0 = I.getOperand(i_nocapture: 0);
7619 Value *Op1 = I.getOperand(i_nocapture: 1);
7620 auto *Op1C = dyn_cast<Constant>(Val: Op1);
7621 if (!Op1C)
7622 return nullptr;
7623
7624 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, C: Op1C);
7625 if (!FlippedStrictness)
7626 return nullptr;
7627
7628 auto *NewCmp =
7629 new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
7630 NewCmp->setSameSign(FlippedStrictness->first.hasSameSign());
7631 return NewCmp;
7632}
7633
7634/// If we have a comparison with a non-canonical predicate, if we can update
7635/// all the users, invert the predicate and adjust all the users.
7636CmpInst *InstCombinerImpl::canonicalizeICmpPredicate(CmpInst &I) {
7637 // Is the predicate already canonical?
7638 CmpInst::Predicate Pred = I.getPredicate();
7639 if (InstCombiner::isCanonicalPredicate(Pred))
7640 return nullptr;
7641
7642 // Can all users be adjusted to predicate inversion?
7643 if (!InstCombiner::canFreelyInvertAllUsersOf(V: &I, /*IgnoredUser=*/nullptr))
7644 return nullptr;
7645
7646 // Ok, we can canonicalize comparison!
7647 // Let's first invert the comparison's predicate.
7648 I.setPredicate(CmpInst::getInversePredicate(pred: Pred));
7649 I.setName(I.getName() + ".not");
7650
7651 // And, adapt users.
7652 freelyInvertAllUsersOf(V: &I);
7653
7654 return &I;
7655}
7656
7657/// Integer compare with boolean values can always be turned into bitwise ops.
7658static Instruction *canonicalizeICmpBool(ICmpInst &I,
7659 InstCombiner::BuilderTy &Builder) {
7660 Value *A = I.getOperand(i_nocapture: 0), *B = I.getOperand(i_nocapture: 1);
7661 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
7662
7663 // A boolean compared to true/false can be simplified to Op0/true/false in
7664 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
7665 // Cases not handled by InstSimplify are always 'not' of Op0.
7666 if (match(V: B, P: m_Zero())) {
7667 switch (I.getPredicate()) {
7668 case CmpInst::ICMP_EQ: // A == 0 -> !A
7669 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
7670 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
7671 return BinaryOperator::CreateNot(Op: A);
7672 default:
7673 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7674 }
7675 } else if (match(V: B, P: m_One())) {
7676 switch (I.getPredicate()) {
7677 case CmpInst::ICMP_NE: // A != 1 -> !A
7678 case CmpInst::ICMP_ULT: // A <u 1 -> !A
7679 case CmpInst::ICMP_SGT: // A >s -1 -> !A
7680 return BinaryOperator::CreateNot(Op: A);
7681 default:
7682 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7683 }
7684 }
7685
7686 switch (I.getPredicate()) {
7687 default:
7688 llvm_unreachable("Invalid icmp instruction!");
7689 case ICmpInst::ICMP_EQ:
7690 // icmp eq i1 A, B -> ~(A ^ B)
7691 return BinaryOperator::CreateNot(Op: Builder.CreateXor(LHS: A, RHS: B));
7692
7693 case ICmpInst::ICMP_NE:
7694 // icmp ne i1 A, B -> A ^ B
7695 return BinaryOperator::CreateXor(V1: A, V2: B);
7696
7697 case ICmpInst::ICMP_UGT:
7698 // icmp ugt -> icmp ult
7699 std::swap(a&: A, b&: B);
7700 [[fallthrough]];
7701 case ICmpInst::ICMP_ULT:
7702 // icmp ult i1 A, B -> ~A & B
7703 return BinaryOperator::CreateAnd(V1: Builder.CreateNot(V: A), V2: B);
7704
7705 case ICmpInst::ICMP_SGT:
7706 // icmp sgt -> icmp slt
7707 std::swap(a&: A, b&: B);
7708 [[fallthrough]];
7709 case ICmpInst::ICMP_SLT:
7710 // icmp slt i1 A, B -> A & ~B
7711 return BinaryOperator::CreateAnd(V1: Builder.CreateNot(V: B), V2: A);
7712
7713 case ICmpInst::ICMP_UGE:
7714 // icmp uge -> icmp ule
7715 std::swap(a&: A, b&: B);
7716 [[fallthrough]];
7717 case ICmpInst::ICMP_ULE:
7718 // icmp ule i1 A, B -> ~A | B
7719 return BinaryOperator::CreateOr(V1: Builder.CreateNot(V: A), V2: B);
7720
7721 case ICmpInst::ICMP_SGE:
7722 // icmp sge -> icmp sle
7723 std::swap(a&: A, b&: B);
7724 [[fallthrough]];
7725 case ICmpInst::ICMP_SLE:
7726 // icmp sle i1 A, B -> A | ~B
7727 return BinaryOperator::CreateOr(V1: Builder.CreateNot(V: B), V2: A);
7728 }
7729}
7730
7731// Transform pattern like:
7732// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7733// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7734// Into:
7735// (X l>> Y) != 0
7736// (X l>> Y) == 0
7737static Instruction *foldICmpWithHighBitMask(ICmpInst &Cmp,
7738 InstCombiner::BuilderTy &Builder) {
7739 CmpPredicate Pred, NewPred;
7740 Value *X, *Y;
7741 if (match(V: &Cmp,
7742 P: m_c_ICmp(Pred, L: m_OneUse(SubPattern: m_Shl(L: m_One(), R: m_Value(V&: Y))), R: m_Value(V&: X)))) {
7743 switch (Pred) {
7744 case ICmpInst::ICMP_ULE:
7745 NewPred = ICmpInst::ICMP_NE;
7746 break;
7747 case ICmpInst::ICMP_UGT:
7748 NewPred = ICmpInst::ICMP_EQ;
7749 break;
7750 default:
7751 return nullptr;
7752 }
7753 } else if (match(V: &Cmp, P: m_c_ICmp(Pred,
7754 L: m_OneUse(SubPattern: m_CombineOr(
7755 Ps: m_Not(V: m_Shl(L: m_AllOnes(), R: m_Value(V&: Y))),
7756 Ps: m_Add(L: m_Shl(L: m_One(), R: m_Value(V&: Y)),
7757 R: m_AllOnes()))),
7758 R: m_Value(V&: X)))) {
7759 // The variant with 'add' is not canonical, (the variant with 'not' is)
7760 // we only get it because it has extra uses, and can't be canonicalized,
7761
7762 switch (Pred) {
7763 case ICmpInst::ICMP_ULT:
7764 NewPred = ICmpInst::ICMP_NE;
7765 break;
7766 case ICmpInst::ICMP_UGE:
7767 NewPred = ICmpInst::ICMP_EQ;
7768 break;
7769 default:
7770 return nullptr;
7771 }
7772 } else
7773 return nullptr;
7774
7775 Value *NewX = Builder.CreateLShr(LHS: X, RHS: Y, Name: X->getName() + ".highbits");
7776 Constant *Zero = Constant::getNullValue(Ty: NewX->getType());
7777 return CmpInst::Create(Op: Instruction::ICmp, Pred: NewPred, S1: NewX, S2: Zero);
7778}
7779
7780static Instruction *foldVectorCmp(CmpInst &Cmp,
7781 InstCombiner::BuilderTy &Builder) {
7782 const CmpInst::Predicate Pred = Cmp.getPredicate();
7783 Value *LHS = Cmp.getOperand(i_nocapture: 0), *RHS = Cmp.getOperand(i_nocapture: 1);
7784 Value *V1, *V2;
7785
7786 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7787 Value *V = Builder.CreateCmp(Pred, LHS: X, RHS: Y, Name: Cmp.getName());
7788 if (auto *I = dyn_cast<Instruction>(Val: V))
7789 I->copyIRFlags(V: &Cmp);
7790 Module *M = Cmp.getModule();
7791 Function *F = Intrinsic::getOrInsertDeclaration(
7792 M, id: Intrinsic::vector_reverse, OverloadTys: V->getType());
7793 return CallInst::Create(Func: F, Args: V);
7794 };
7795
7796 if (match(V: LHS, P: m_VecReverse(Op0: m_Value(V&: V1)))) {
7797 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7798 if (match(V: RHS, P: m_VecReverse(Op0: m_Value(V&: V2))) &&
7799 (LHS->hasOneUse() || RHS->hasOneUse()))
7800 return createCmpReverse(Pred, V1, V2);
7801
7802 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7803 if (LHS->hasOneUse() && isSplatValue(V: RHS))
7804 return createCmpReverse(Pred, V1, RHS);
7805 }
7806 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7807 else if (isSplatValue(V: LHS) && match(V: RHS, P: m_OneUse(SubPattern: m_VecReverse(Op0: m_Value(V&: V2)))))
7808 return createCmpReverse(Pred, LHS, V2);
7809
7810 ArrayRef<int> M;
7811 if (!match(V: LHS, P: m_Shuffle(v1: m_Value(V&: V1), v2: m_Undef(), mask: m_Mask(M))))
7812 return nullptr;
7813
7814 // If both arguments of the cmp are shuffles that use the same mask and
7815 // shuffle within a single vector, move the shuffle after the cmp:
7816 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7817 Type *V1Ty = V1->getType();
7818 if (match(V: RHS, P: m_Shuffle(v1: m_Value(V&: V2), v2: m_Undef(), mask: m_SpecificMask(M))) &&
7819 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7820 Value *NewCmp = Builder.CreateCmp(Pred, LHS: V1, RHS: V2);
7821 return new ShuffleVectorInst(NewCmp, M);
7822 }
7823
7824 // Try to canonicalize compare with splatted operand and splat constant.
7825 // TODO: We could generalize this for more than splats. See/use the code in
7826 // InstCombiner::foldVectorBinop().
7827 Constant *C;
7828 if (!LHS->hasOneUse() || !match(V: RHS, P: m_Constant(C)))
7829 return nullptr;
7830
7831 // Length-changing splats are ok, so adjust the constants as needed:
7832 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7833 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7834 int MaskSplatIndex;
7835 if (ScalarC && match(Mask: M, P: m_SplatOrPoisonMask(MaskSplatIndex))) {
7836 // We allow poison in matching, but this transform removes it for safety.
7837 // Demanded elements analysis should be able to recover some/all of that.
7838 C = ConstantVector::getSplat(EC: cast<VectorType>(Val: V1Ty)->getElementCount(),
7839 Elt: ScalarC);
7840 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7841 Value *NewCmp = Builder.CreateCmp(Pred, LHS: V1, RHS: C);
7842 return new ShuffleVectorInst(NewCmp, NewM);
7843 }
7844
7845 return nullptr;
7846}
7847
7848// extract(uadd.with.overflow(A, B), 0) ult A
7849// -> extract(uadd.with.overflow(A, B), 1)
7850static Instruction *foldICmpOfUAddOv(ICmpInst &I) {
7851 CmpInst::Predicate Pred = I.getPredicate();
7852 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
7853
7854 Value *UAddOv;
7855 Value *A, *B;
7856 auto UAddOvResultPat = m_ExtractValue<0>(
7857 V: m_Intrinsic<Intrinsic::uadd_with_overflow>(Ops: m_Value(V&: A), Ops: m_Value(V&: B)));
7858 if (match(V: Op0, P: UAddOvResultPat) &&
7859 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7860 (Pred == ICmpInst::ICMP_EQ && match(V: Op1, P: m_ZeroInt()) &&
7861 (match(V: A, P: m_One()) || match(V: B, P: m_One()))) ||
7862 (Pred == ICmpInst::ICMP_NE && match(V: Op1, P: m_AllOnes()) &&
7863 (match(V: A, P: m_AllOnes()) || match(V: B, P: m_AllOnes())))))
7864 // extract(uadd.with.overflow(A, B), 0) < A
7865 // extract(uadd.with.overflow(A, 1), 0) == 0
7866 // extract(uadd.with.overflow(A, -1), 0) != -1
7867 UAddOv = cast<ExtractValueInst>(Val: Op0)->getAggregateOperand();
7868 else if (match(V: Op1, P: UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&
7869 (Op0 == A || Op0 == B))
7870 // A > extract(uadd.with.overflow(A, B), 0)
7871 UAddOv = cast<ExtractValueInst>(Val: Op1)->getAggregateOperand();
7872 else
7873 return nullptr;
7874
7875 return ExtractValueInst::Create(Agg: UAddOv, Idxs: 1);
7876}
7877
7878static Instruction *foldICmpInvariantGroup(ICmpInst &I) {
7879 if (!I.getOperand(i_nocapture: 0)->getType()->isPointerTy() ||
7880 NullPointerIsDefined(
7881 F: I.getParent()->getParent(),
7882 AS: I.getOperand(i_nocapture: 0)->getType()->getPointerAddressSpace())) {
7883 return nullptr;
7884 }
7885 Value *Ptr;
7886 if (match(V: I.getOperand(i_nocapture: 0),
7887 P: m_Intrinsic<Intrinsic::launder_invariant_group>(Ops: m_Value(V&: Ptr))) &&
7888 match(V: I.getOperand(i_nocapture: 1), P: m_Zero())) {
7889 return ICmpInst::Create(Op: Instruction::ICmp, Pred: I.getPredicate(), S1: Ptr,
7890 S2: I.getOperand(i_nocapture: 1));
7891 }
7892 return nullptr;
7893}
7894
7895static Instruction *foldICmpOfVectorReduce(ICmpInst &I, const DataLayout &DL,
7896 IRBuilderBase &Builder) {
7897 if (!ICmpInst::isEquality(P: I.getPredicate()))
7898 return nullptr;
7899
7900 // The caller puts constants after non-constants.
7901 Value *Op = I.getOperand(i_nocapture: 0);
7902 Value *Const = I.getOperand(i_nocapture: 1);
7903
7904 // For Cond an equality condition, fold
7905 //
7906 // icmp (eq|ne) (vreduce_(or|and) Op), (Zero|AllOnes) ->
7907 // icmp (eq|ne) Op, (Zero|AllOnes)
7908 //
7909 // with a bitcast.
7910 Value *Vec;
7911 if ((match(V: Const, P: m_ZeroInt()) &&
7912 match(V: Op, P: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::vector_reduce_or>(
7913 Ops: m_Value(V&: Vec))))) ||
7914 (match(V: Const, P: m_AllOnes()) &&
7915 match(V: Op, P: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::vector_reduce_and>(
7916 Ops: m_Value(V&: Vec)))))) {
7917 auto *VecTy = dyn_cast<FixedVectorType>(Val: Vec->getType());
7918 if (!VecTy)
7919 return nullptr;
7920 Type *VecEltTy = VecTy->getElementType();
7921 unsigned ScalarBW =
7922 DL.getTypeSizeInBits(Ty: VecEltTy) * VecTy->getNumElements();
7923 if (!DL.fitsInLegalInteger(Width: ScalarBW))
7924 return nullptr;
7925 Type *ScalarTy = IntegerType::get(C&: I.getContext(), NumBits: ScalarBW);
7926 Value *NewConst = match(V: Const, P: m_ZeroInt())
7927 ? ConstantInt::get(Ty: ScalarTy, V: 0)
7928 : ConstantInt::getAllOnesValue(Ty: ScalarTy);
7929 return CmpInst::Create(Op: Instruction::ICmp, Pred: I.getPredicate(),
7930 S1: Builder.CreateBitCast(V: Vec, DestTy: ScalarTy), S2: NewConst);
7931 }
7932 return nullptr;
7933}
7934
7935/// This function folds patterns produced by lowering of reduce idioms, such as
7936/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7937/// attempts to generate fewer number of scalar comparisons instead of vector
7938/// comparisons when possible.
7939static Instruction *foldReductionIdiom(ICmpInst &I,
7940 InstCombiner::BuilderTy &Builder,
7941 const DataLayout &DL) {
7942 if (I.getType()->isVectorTy())
7943 return nullptr;
7944 CmpPredicate OuterPred, InnerPred;
7945 Value *LHS, *RHS;
7946
7947 // Match lowering of @llvm.vector.reduce.and. Turn
7948 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7949 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7950 /// %res = icmp <pred> i8 %scalar_ne, 0
7951 ///
7952 /// into
7953 ///
7954 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7955 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7956 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7957 ///
7958 /// for <pred> in {ne, eq}.
7959 if (!match(V: &I, P: m_ICmp(Pred&: OuterPred,
7960 L: m_OneUse(SubPattern: m_BitCast(Op: m_OneUse(
7961 SubPattern: m_ICmp(Pred&: InnerPred, L: m_Value(V&: LHS), R: m_Value(V&: RHS))))),
7962 R: m_Zero())))
7963 return nullptr;
7964 auto *LHSTy = dyn_cast<FixedVectorType>(Val: LHS->getType());
7965 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7966 return nullptr;
7967 unsigned NumBits =
7968 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7969 // TODO: Relax this to "not wider than max legal integer type"?
7970 if (!DL.isLegalInteger(Width: NumBits))
7971 return nullptr;
7972
7973 if (ICmpInst::isEquality(P: OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7974 auto *ScalarTy = Builder.getIntNTy(N: NumBits);
7975 LHS = Builder.CreateBitCast(V: LHS, DestTy: ScalarTy, Name: LHS->getName() + ".scalar");
7976 RHS = Builder.CreateBitCast(V: RHS, DestTy: ScalarTy, Name: RHS->getName() + ".scalar");
7977 return ICmpInst::Create(Op: Instruction::ICmp, Pred: OuterPred, S1: LHS, S2: RHS,
7978 Name: I.getName());
7979 }
7980
7981 return nullptr;
7982}
7983
7984// This helper will be called with icmp operands in both orders.
7985Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
7986 Value *Op0, Value *Op1,
7987 ICmpInst &CtxI) {
7988 // Each fold below requires Op0 to be a particular operator, so dispatch on
7989 // its opcode and only try the folds that can apply.
7990 switch (Operator::getOpcode(V: Op0)) {
7991 default:
7992 return nullptr;
7993
7994 case Instruction::GetElementPtr:
7995 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7996 return foldGEPICmp(GEPLHS: cast<GEPOperator>(Val: Op0), RHS: Op1, Cond: Pred, I&: CtxI);
7997
7998 case Instruction::Select:
7999 return foldSelectICmp(Pred, SI: cast<SelectInst>(Val: Op0), RHS: Op1, I: CtxI);
8000
8001 case Instruction::Call: {
8002 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Val: Op0)) {
8003 if (Instruction *Res = foldICmpWithMinMax(I&: CtxI, MinMax, Z: Op1, Pred))
8004 return Res;
8005
8006 if (Instruction *Res = foldICmpWithClamp(I&: CtxI, X: Op1, Min: MinMax))
8007 return Res;
8008 }
8009
8010 // abs(X) >= X --> true
8011 // abs(X) u<= X --> true
8012 // abs(X) < X --> false
8013 // abs(X) u> X --> false
8014 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
8015 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
8016 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
8017 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
8018 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
8019 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
8020 Value *X;
8021 Constant *C;
8022 if (match(V: Op0, P: m_Intrinsic<Intrinsic::abs>(Ops: m_Value(V&: X), Ops: m_Constant(C))) &&
8023 match(V: Op1, P: m_Specific(V: X))) {
8024 Value *NullValue = Constant::getNullValue(Ty: X->getType());
8025 Value *AllOnesValue = Constant::getAllOnesValue(Ty: X->getType());
8026 const APInt SMin =
8027 APInt::getSignedMinValue(numBits: X->getType()->getScalarSizeInBits());
8028 bool IsIntMinPosion = C->isAllOnesValue();
8029 switch (Pred) {
8030 case CmpInst::ICMP_ULE:
8031 case CmpInst::ICMP_SGE:
8032 return replaceInstUsesWith(I&: CtxI, V: ConstantInt::getTrue(Ty: CtxI.getType()));
8033 case CmpInst::ICMP_UGT:
8034 case CmpInst::ICMP_SLT:
8035 return replaceInstUsesWith(I&: CtxI, V: ConstantInt::getFalse(Ty: CtxI.getType()));
8036 case CmpInst::ICMP_UGE:
8037 case CmpInst::ICMP_SLE:
8038 case CmpInst::ICMP_EQ: {
8039 return replaceInstUsesWith(
8040 I&: CtxI, V: IsIntMinPosion
8041 ? Builder.CreateICmpSGT(LHS: X, RHS: AllOnesValue)
8042 : Builder.CreateICmpULT(
8043 LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: SMin + 1)));
8044 }
8045 case CmpInst::ICMP_ULT:
8046 case CmpInst::ICMP_SGT:
8047 case CmpInst::ICMP_NE: {
8048 return replaceInstUsesWith(
8049 I&: CtxI, V: IsIntMinPosion
8050 ? Builder.CreateICmpSLT(LHS: X, RHS: NullValue)
8051 : Builder.CreateICmpUGT(
8052 LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: SMin)));
8053 }
8054 default:
8055 llvm_unreachable("Invalid predicate!");
8056 }
8057 }
8058
8059 // For a nonzero constant C:
8060 // usub.sat(X, C) == X --> X == 0
8061 // usub.sat(X, C) != X --> X != 0
8062 // usub.sat(X, C) < X --> X != 0
8063 if (match(V: Op0, P: m_Intrinsic<Intrinsic::usub_sat>(Ops: m_Specific(V: Op1),
8064 Ops: m_NonZeroInt())) &&
8065 (CmpInst::isEquality(pred: Pred) || Pred == ICmpInst::ICMP_ULT)) {
8066 ICmpInst::Predicate NewPred =
8067 CmpInst::isEquality(pred: Pred) ? Pred.dropSameSign() : ICmpInst::ICMP_NE;
8068 return new ICmpInst(NewPred, Op1, Constant::getNullValue(Ty: Op1->getType()));
8069 }
8070 return nullptr;
8071 }
8072
8073 case Instruction::Add: {
8074 Value *X;
8075 const APInt *C;
8076 // icmp X+Cst, X
8077 if (match(V: Op0, P: m_Add(L: m_Value(V&: X), R: m_APInt(Res&: C))) && Op1 == X)
8078 return foldICmpAddOpConst(X, C: *C, Pred);
8079 return nullptr;
8080 }
8081
8082 case Instruction::And:
8083 case Instruction::Or: {
8084 const SimplifyQuery Q = SQ.getWithInstruction(I: &CtxI);
8085 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, IC&: *this))
8086 return replaceInstUsesWith(I&: CtxI, V);
8087 return nullptr;
8088 }
8089
8090 case Instruction::UDiv:
8091 case Instruction::SDiv: {
8092 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
8093 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(RHS: 1); };
8094 if (match(V: Op0, P: m_UDiv(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckUGT1)))) {
8095 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
8096 Constant::getNullValue(Ty: Op1->getType()));
8097 }
8098
8099 if (!ICmpInst::isUnsigned(Pred) &&
8100 match(V: Op0, P: m_SDiv(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckUGT1)))) {
8101 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
8102 Constant::getNullValue(Ty: Op1->getType()));
8103 }
8104 return nullptr;
8105 }
8106
8107 case Instruction::LShr:
8108 case Instruction::AShr: {
8109 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
8110 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
8111 if (match(V: Op0, P: m_LShr(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckNE0)))) {
8112 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
8113 Constant::getNullValue(Ty: Op1->getType()));
8114 }
8115
8116 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
8117 match(V: Op0, P: m_AShr(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckNE0)))) {
8118 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
8119 Constant::getNullValue(Ty: Op1->getType()));
8120 }
8121 return nullptr;
8122 }
8123
8124 case Instruction::Shl: {
8125 // icmp (shl nsw/nuw X, L), (add nsw/nuw (shl nsw/nuw Y, L), K)
8126 // -> icmp X, (add nsw/nuw Y, K >> L)
8127 // We use AShr for nsw and LShr for nuw to safely peel off the shift.
8128 Value *X;
8129 uint64_t ShAmt;
8130 if (match(V: Op0, P: m_NUWShl(L: m_Value(V&: X), R: m_ConstantInt(V&: ShAmt))) &&
8131 !CtxI.isSigned()) {
8132 if (ShAmt >= X->getType()->getScalarSizeInBits())
8133 return nullptr;
8134 if (canEvaluateShifted(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
8135 Semantics: ShiftSemantics::Unsigned, CtxI: &CtxI)) {
8136 Value *NewOp1 = getShiftedValue(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
8137 Semantics: ShiftSemantics::Unsigned);
8138 return new ICmpInst(Pred, X, NewOp1);
8139 }
8140 }
8141
8142 if (match(V: Op0, P: m_NSWShl(L: m_Value(V&: X), R: m_ConstantInt(V&: ShAmt))) &&
8143 !CtxI.isUnsigned()) {
8144 if (ShAmt >= X->getType()->getScalarSizeInBits())
8145 return nullptr;
8146 if (canEvaluateShifted(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
8147 Semantics: ShiftSemantics::Signed, CtxI: &CtxI)) {
8148 Value *NewOp1 = getShiftedValue(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
8149 Semantics: ShiftSemantics::Signed);
8150 return new ICmpInst(Pred, X, NewOp1);
8151 }
8152 }
8153 return nullptr;
8154 }
8155 }
8156}
8157
8158Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
8159 bool Changed = false;
8160 const SimplifyQuery Q = SQ.getWithInstruction(I: &I);
8161 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
8162 unsigned Op0Cplxity = getComplexity(V: Op0);
8163 unsigned Op1Cplxity = getComplexity(V: Op1);
8164
8165 /// Orders the operands of the compare so that they are listed from most
8166 /// complex to least complex. This puts constants before unary operators,
8167 /// before binary operators.
8168 if (Op0Cplxity < Op1Cplxity) {
8169 I.swapOperands();
8170 std::swap(a&: Op0, b&: Op1);
8171 Changed = true;
8172 }
8173
8174 if (Value *V = simplifyICmpInst(Pred: I.getCmpPredicate(), LHS: Op0, RHS: Op1, Q))
8175 return replaceInstUsesWith(I, V);
8176
8177 if (Instruction *Res = foldICmpTruncWithTruncOrExt(Cmp&: I, Q))
8178 return Res;
8179
8180 if (Op0->getType()->isIntOrIntVectorTy(BitWidth: 1))
8181 if (Instruction *Res = canonicalizeICmpBool(I, Builder))
8182 return Res;
8183
8184 if (Instruction *Res = canonicalizeCmpWithConstant(I))
8185 return Res;
8186
8187 if (Instruction *Res = canonicalizeICmpPredicate(I))
8188 return Res;
8189
8190 if (Instruction *Res = foldICmpWithConstant(Cmp&: I))
8191 return Res;
8192
8193 if (Instruction *Res = foldICmpWithDominatingICmp(Cmp&: I))
8194 return Res;
8195
8196 if (Instruction *Res = foldICmpUsingBoolRange(I))
8197 return Res;
8198
8199 if (Instruction *Res = foldICmpUsingKnownBits(I))
8200 return Res;
8201
8202 if (Instruction *Res = foldIsMultipleOfAPowerOfTwo(Cmp&: I))
8203 return Res;
8204
8205 // Test if the ICmpInst instruction is used exclusively by a select as
8206 // part of a minimum or maximum operation. If so, refrain from doing
8207 // any other folding. This helps out other analyses which understand
8208 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
8209 // and CodeGen. And in this case, at least one of the comparison
8210 // operands has at least one user besides the compare (the select),
8211 // which would often largely negate the benefit of folding anyway.
8212 //
8213 // Do the same for the other patterns recognized by matchSelectPattern.
8214 if (I.hasOneUse())
8215 if (SelectInst *SI = dyn_cast<SelectInst>(Val: I.user_back())) {
8216 Value *A, *B;
8217 SelectPatternResult SPR = matchSelectPattern(V: SI, LHS&: A, RHS&: B);
8218 if (SPR.Flavor != SPF_UNKNOWN)
8219 return nullptr;
8220 }
8221
8222 // Do this after checking for min/max to prevent infinite looping.
8223 if (Instruction *Res = foldICmpWithZero(Cmp&: I))
8224 return Res;
8225
8226 Value *X;
8227 const APInt *C;
8228 if (I.getPredicate() == ICmpInst::ICMP_UGT &&
8229 match(V: Op0, P: m_UMax(Op0: m_Value(V&: X), Op1: m_APInt(Res&: C))) &&
8230 match(V: Op1, P: m_Not(V: m_Specific(V: X)))) {
8231 if (C->isNonNegative())
8232 return new ICmpInst(ICmpInst::ICMP_SLT, X,
8233 Constant::getNullValue(Ty: X->getType()));
8234 return new ICmpInst(ICmpInst::ICMP_UGT, X,
8235 ConstantInt::get(Ty: X->getType(), V: ~*C));
8236 }
8237
8238 if (I.getPredicate() == ICmpInst::ICMP_ULT &&
8239 match(V: Op0, P: m_UMax(Op0: m_Value(V&: X), Op1: m_APInt(Res&: C))) &&
8240 match(V: Op1, P: m_Not(V: m_Specific(V: X)))) {
8241 if (C->isNonNegative())
8242 return new ICmpInst(ICmpInst::ICMP_SGT, X,
8243 Constant::getAllOnesValue(Ty: X->getType()));
8244 return new ICmpInst(ICmpInst::ICMP_ULT, X,
8245 ConstantInt::get(Ty: X->getType(), V: ~*C));
8246 }
8247
8248 // FIXME: We only do this after checking for min/max to prevent infinite
8249 // looping caused by a reverse canonicalization of these patterns for min/max.
8250 // FIXME: The organization of folds is a mess. These would naturally go into
8251 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
8252 // down here after the min/max restriction.
8253 ICmpInst::Predicate Pred = I.getPredicate();
8254 if (match(V: Op1, P: m_APInt(Res&: C))) {
8255 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
8256 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
8257 Constant *Zero = Constant::getNullValue(Ty: Op0->getType());
8258 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
8259 }
8260
8261 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
8262 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
8263 Constant *AllOnes = Constant::getAllOnesValue(Ty: Op0->getType());
8264 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
8265 }
8266 }
8267
8268 // The folds in here may rely on wrapping flags and special constants, so
8269 // they can break up min/max idioms in some cases but not seemingly similar
8270 // patterns.
8271 // FIXME: It may be possible to enhance select folding to make this
8272 // unnecessary. It may also be moot if we canonicalize to min/max
8273 // intrinsics.
8274 if (Instruction *Res = foldICmpBinOp(I, SQ: Q))
8275 return Res;
8276
8277 if (Instruction *Res = foldICmpInstWithConstant(Cmp&: I))
8278 return Res;
8279
8280 // Try to match comparison as a sign bit test. Intentionally do this after
8281 // foldICmpInstWithConstant() to potentially let other folds to happen first.
8282 if (Instruction *New = foldSignBitTest(I))
8283 return New;
8284
8285 if (auto *PN = dyn_cast<PHINode>(Val: Op0))
8286 if (Instruction *NV = foldOpIntoPhi(I, PN))
8287 return NV;
8288 if (auto *PN = dyn_cast<PHINode>(Val: Op1))
8289 if (Instruction *NV = foldOpIntoPhi(I, PN))
8290 return NV;
8291
8292 if (Instruction *Res = foldICmpInstWithConstantNotInt(I))
8293 return Res;
8294
8295 if (Instruction *Res = foldICmpCommutative(Pred: I.getCmpPredicate(), Op0, Op1, CtxI&: I))
8296 return Res;
8297 if (Instruction *Res =
8298 foldICmpCommutative(Pred: I.getSwappedCmpPredicate(), Op0: Op1, Op1: Op0, CtxI&: I))
8299 return Res;
8300
8301 if (I.isCommutative()) {
8302 if (auto Pair = matchSymmetricPair(LHS: I.getOperand(i_nocapture: 0), RHS: I.getOperand(i_nocapture: 1))) {
8303 replaceOperand(I, OpNum: 0, V: Pair->first);
8304 replaceOperand(I, OpNum: 1, V: Pair->second);
8305 return &I;
8306 }
8307 }
8308
8309 // Fold icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2)
8310 // when all select arms are constants, via truth table.
8311 if (Instruction *R = foldCmpSelectOfConstants(I))
8312 return R;
8313
8314 // In case of a comparison with two select instructions having the same
8315 // condition, check whether one of the resulting branches can be simplified.
8316 // If so, just compare the other branch and select the appropriate result.
8317 // For example:
8318 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
8319 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
8320 // %cmp2 = icmp slt i32 %tmp2, %tmp1
8321 // The icmp will result false for the false value of selects and the result
8322 // will depend upon the comparison of true values of selects if %cmp is
8323 // true. Thus, transform this into:
8324 // %cmp = icmp slt i32 %y, %z
8325 // %sel = select i1 %cond, i1 %cmp, i1 false
8326 // This handles similar cases to transform.
8327 {
8328 Value *Cond, *A, *B, *C, *D;
8329 if (match(V: Op0, P: m_Select(C: m_Value(V&: Cond), L: m_Value(V&: A), R: m_Value(V&: B))) &&
8330 match(V: Op1, P: m_Select(C: m_Specific(V: Cond), L: m_Value(V&: C), R: m_Value(V&: D))) &&
8331 (Op0->hasOneUse() || Op1->hasOneUse())) {
8332 // Check whether comparison of TrueValues can be simplified
8333 if (Value *Res = simplifyICmpInst(Pred, LHS: A, RHS: C, Q: SQ)) {
8334 Value *NewICMP = Builder.CreateICmp(P: Pred, LHS: B, RHS: D);
8335 return SelectInst::Create(C: Cond, S1: Res, S2: NewICMP, /*NameStr=*/"",
8336 /*InsertBefore=*/nullptr,
8337 MDFrom: cast<Instruction>(Val: Op0));
8338 }
8339 // Check whether comparison of FalseValues can be simplified
8340 if (Value *Res = simplifyICmpInst(Pred, LHS: B, RHS: D, Q: SQ)) {
8341 Value *NewICMP = Builder.CreateICmp(P: Pred, LHS: A, RHS: C);
8342 return SelectInst::Create(C: Cond, S1: NewICMP, S2: Res, /*NameStr=*/"",
8343 /*InsertBefore=*/nullptr,
8344 MDFrom: cast<Instruction>(Val: Op0));
8345 }
8346 }
8347 }
8348
8349 // icmp slt (sub nsw x, y), (add nsw x, y) --> icmp sgt y, 0
8350 // icmp ult (sub nuw x, y), (add nuw x, y) --> icmp ugt y, 0
8351 // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y) --> icmp eq y, 0
8352 {
8353 Value *A, *B;
8354 CmpPredicate CmpPred;
8355 if (match(V: &I, P: m_c_ICmp(Pred&: CmpPred, L: m_Sub(L: m_Value(V&: A), R: m_Value(V&: B)),
8356 R: m_c_Add(L: m_Deferred(V: A), R: m_Deferred(V: B))))) {
8357 auto *I0 = cast<OverflowingBinaryOperator>(Val: Op0);
8358 auto *I1 = cast<OverflowingBinaryOperator>(Val: Op1);
8359 bool I0NUW = I0->hasNoUnsignedWrap();
8360 bool I1NUW = I1->hasNoUnsignedWrap();
8361 bool I0NSW = I0->hasNoSignedWrap();
8362 bool I1NSW = I1->hasNoSignedWrap();
8363 if ((ICmpInst::isUnsigned(Pred) && I0NUW && I1NUW) ||
8364 (ICmpInst::isSigned(Pred) && I0NSW && I1NSW) ||
8365 (ICmpInst::isEquality(P: Pred) &&
8366 ((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {
8367 return new ICmpInst(CmpPredicate::getSwapped(P: CmpPred), B,
8368 ConstantInt::get(Ty: Op0->getType(), V: 0));
8369 }
8370 }
8371 }
8372
8373 // Try to optimize equality comparisons against alloca-based pointers.
8374 if (Op0->getType()->isPointerTy() && I.isEquality()) {
8375 assert(Op1->getType()->isPointerTy() &&
8376 "Comparing pointer with non-pointer?");
8377 if (auto *Alloca = dyn_cast<AllocaInst>(Val: getUnderlyingObject(V: Op0)))
8378 if (foldAllocaCmp(Alloca))
8379 return nullptr;
8380 if (auto *Alloca = dyn_cast<AllocaInst>(Val: getUnderlyingObject(V: Op1)))
8381 if (foldAllocaCmp(Alloca))
8382 return nullptr;
8383 }
8384
8385 if (Instruction *Res = foldICmpBitCast(Cmp&: I))
8386 return Res;
8387
8388 // TODO: Hoist this above the min/max bailout.
8389 if (Instruction *R = foldICmpWithCastOp(ICmp&: I))
8390 return R;
8391
8392 // icmp (zext X), (and (trunc Y), Mask) -> icmp X, trunc Y IFF Mask exactly
8393 // covers the bits of X
8394 {
8395 Value *Y;
8396 const APInt *Mask;
8397 if (match(V: I.getOperand(i_nocapture: 1), P: m_ZExt(Op: m_Value(V&: X))) &&
8398 match(V: I.getOperand(i_nocapture: 0),
8399 P: m_OneUse(SubPattern: m_And(L: m_Trunc(Op: m_Value(V&: Y)), R: m_APInt(Res&: Mask))))) {
8400 Type *SmallType = X->getType();
8401 unsigned SmallWidth = SmallType->getScalarSizeInBits();
8402 if (Mask->isMask(numBits: SmallWidth) &&
8403 shouldChangeType(From: I.getOperand(i_nocapture: 0)->getType(), To: SmallType)) {
8404 Value *NewTrunc = Builder.CreateTrunc(V: Y, DestTy: SmallType);
8405 return new ICmpInst(I.getUnsignedPredicate(), NewTrunc, X);
8406 }
8407 }
8408 }
8409
8410 {
8411 Value *X, *Y;
8412 // Transform (X & ~Y) == 0 --> (X & Y) != 0
8413 // and (X & ~Y) != 0 --> (X & Y) == 0
8414 // if A is a power of 2.
8415 if (match(V: Op0, P: m_And(L: m_Value(V&: X), R: m_Not(V: m_Value(V&: Y)))) &&
8416 match(V: Op1, P: m_Zero()) && isKnownToBeAPowerOfTwo(V: X, OrZero: false, CtxI: &I) &&
8417 I.isEquality())
8418 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(LHS: X, RHS: Y),
8419 Op1);
8420
8421 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
8422 if (Op0->getType()->isIntOrIntVectorTy()) {
8423 bool ConsumesOp0, ConsumesOp1;
8424 if (isFreeToInvert(V: Op0, WillInvertAllUses: Op0->hasOneUse(), DoesConsume&: ConsumesOp0) &&
8425 isFreeToInvert(V: Op1, WillInvertAllUses: Op1->hasOneUse(), DoesConsume&: ConsumesOp1) &&
8426 (ConsumesOp0 || ConsumesOp1)) {
8427 Value *InvOp0 = getFreelyInverted(V: Op0, WillInvertAllUses: Op0->hasOneUse(), Builder: &Builder);
8428 Value *InvOp1 = getFreelyInverted(V: Op1, WillInvertAllUses: Op1->hasOneUse(), Builder: &Builder);
8429 assert(InvOp0 && InvOp1 &&
8430 "Mismatch between isFreeToInvert and getFreelyInverted");
8431 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
8432 }
8433 }
8434
8435 Instruction *AddI = nullptr;
8436 if (match(V: &I, P: m_UAddWithOverflow(L: m_Value(V&: X), R: m_Value(V&: Y),
8437 S: m_Instruction(I&: AddI))) &&
8438 isa<IntegerType>(Val: X->getType())) {
8439 Value *Result;
8440 Constant *Overflow;
8441 // m_UAddWithOverflow can match patterns that do not include an explicit
8442 // "add" instruction, so check the opcode of the matched op.
8443 if (AddI->getOpcode() == Instruction::Add &&
8444 OptimizeOverflowCheck(BinaryOp: Instruction::Add, /*Signed*/ IsSigned: false, LHS: X, RHS: Y, OrigI&: *AddI,
8445 Result, Overflow)) {
8446 replaceInstUsesWith(I&: *AddI, V: Result);
8447 eraseInstFromFunction(I&: *AddI);
8448 return replaceInstUsesWith(I, V: Overflow);
8449 }
8450 }
8451
8452 // (zext X) + (zext Y) --> add + overflow check.
8453 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
8454 if ((match(V: Op0, P: m_NUWAdd(L: m_ZExt(Op: m_Value(V&: X)), R: m_ZExt(Op: m_Value(V&: Y)))) ||
8455 match(V: Op0, P: m_NUWMul(L: m_ZExt(Op: m_Value(V&: X)), R: m_ZExt(Op: m_Value(V&: Y))))) &&
8456 match(V: Op1, P: m_APInt(Res&: C))) {
8457 if (Instruction *R = processUZExtIdiom(I, Val: Op0, OtherVal: C, IC&: *this))
8458 return R;
8459 }
8460
8461 // Signbit test folds
8462 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
8463 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
8464 Instruction *ExtI;
8465 if ((I.isUnsigned() || I.isEquality()) &&
8466 match(V: Op1,
8467 P: m_CombineAnd(Ps: m_Instruction(I&: ExtI), Ps: m_ZExtOrSExt(Op: m_Value(V&: Y)))) &&
8468 Y->getType()->getScalarSizeInBits() == 1 &&
8469 (Op0->hasOneUse() || Op1->hasOneUse())) {
8470 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
8471 Instruction *ShiftI;
8472 if (match(V: Op0, P: m_CombineAnd(Ps: m_Instruction(I&: ShiftI),
8473 Ps: m_Shr(L: m_Value(V&: X), R: m_SpecificIntAllowPoison(
8474 V: OpWidth - 1))))) {
8475 unsigned ExtOpc = ExtI->getOpcode();
8476 unsigned ShiftOpc = ShiftI->getOpcode();
8477 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
8478 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
8479 Value *SLTZero =
8480 Builder.CreateICmpSLT(LHS: X, RHS: Constant::getNullValue(Ty: X->getType()));
8481 Value *Cmp = Builder.CreateICmp(P: Pred, LHS: SLTZero, RHS: Y, Name: I.getName());
8482 return replaceInstUsesWith(I, V: Cmp);
8483 }
8484 }
8485 }
8486 }
8487
8488 if (Instruction *Res = foldICmpEquality(I))
8489 return Res;
8490
8491 if (Instruction *Res = foldICmpPow2Test(I, Builder))
8492 return Res;
8493
8494 if (Instruction *Res = foldICmpOfUAddOv(I))
8495 return Res;
8496
8497 if (Instruction *Res = foldICmpOfVectorReduce(I, DL, Builder))
8498 return Res;
8499
8500 // The 'cmpxchg' instruction returns an aggregate containing the old value and
8501 // an i1 which indicates whether or not we successfully did the swap.
8502 //
8503 // Replace comparisons between the old value and the expected value with the
8504 // indicator that 'cmpxchg' returns.
8505 //
8506 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
8507 // spuriously fail. In those cases, the old value may equal the expected
8508 // value but it is possible for the swap to not occur.
8509 if (I.getPredicate() == ICmpInst::ICMP_EQ)
8510 if (auto *EVI = dyn_cast<ExtractValueInst>(Val: Op0))
8511 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(Val: EVI->getAggregateOperand()))
8512 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
8513 !ACXI->isWeak())
8514 return ExtractValueInst::Create(Agg: ACXI, Idxs: 1);
8515
8516 if (Instruction *Res = foldICmpWithHighBitMask(Cmp&: I, Builder))
8517 return Res;
8518
8519 if (I.getType()->isVectorTy())
8520 if (Instruction *Res = foldVectorCmp(Cmp&: I, Builder))
8521 return Res;
8522
8523 if (Instruction *Res = foldICmpInvariantGroup(I))
8524 return Res;
8525
8526 if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
8527 return Res;
8528
8529 {
8530 Value *A;
8531 const APInt *C1, *C2;
8532 ICmpInst::Predicate Pred = I.getPredicate();
8533 if (ICmpInst::isEquality(P: Pred)) {
8534 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
8535 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
8536 if (match(V: Op0, P: m_And(L: m_SExt(Op: m_Value(V&: A)), R: m_APInt(Res&: C1))) &&
8537 match(V: Op1, P: m_APInt(Res&: C2))) {
8538 Type *InputTy = A->getType();
8539 unsigned InputBitWidth = InputTy->getScalarSizeInBits();
8540 // c2 must be non-negative at the bitwidth of a.
8541 if (C2->getActiveBits() < InputBitWidth) {
8542 APInt TruncC1 = C1->trunc(width: InputBitWidth);
8543 // Check if there are 1s in C1 high bits of size InputBitWidth.
8544 if (C1->uge(RHS: APInt::getOneBitSet(numBits: C1->getBitWidth(), BitNo: InputBitWidth)))
8545 TruncC1.setBit(InputBitWidth - 1);
8546 Value *AndInst = Builder.CreateAnd(LHS: A, RHS: TruncC1);
8547 return new ICmpInst(
8548 Pred, AndInst,
8549 ConstantInt::get(Ty: InputTy, V: C2->trunc(width: InputBitWidth)));
8550 }
8551 }
8552 }
8553 }
8554
8555 return Changed ? &I : nullptr;
8556}
8557
8558/// Fold fcmp ([us]itofp x, cst) if possible.
8559Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
8560 Instruction *LHSI,
8561 Constant *RHSC) {
8562 const APFloat *RHS;
8563 if (!match(V: RHSC, P: m_APFloat(Res&: RHS)))
8564 return nullptr;
8565
8566 // Get the width of the mantissa. We don't want to hack on conversions that
8567 // might lose information from the integer, e.g. "i64 -> float"
8568 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
8569 if (MantissaWidth == -1)
8570 return nullptr; // Unknown.
8571
8572 Type *IntTy = LHSI->getOperand(i: 0)->getType();
8573 unsigned IntWidth = IntTy->getScalarSizeInBits();
8574 bool LHSUnsigned = isa<UIToFPInst>(Val: LHSI);
8575
8576 if (I.isEquality()) {
8577 FCmpInst::Predicate P = I.getPredicate();
8578 bool IsExact = false;
8579 APSInt RHSCvt(IntWidth, LHSUnsigned);
8580 RHS->convertToInteger(Result&: RHSCvt, RM: APFloat::rmNearestTiesToEven, IsExact: &IsExact);
8581
8582 // If the floating point constant isn't an integer value, we know if we will
8583 // ever compare equal / not equal to it.
8584 if (!IsExact) {
8585 // TODO: Can never be -0.0 and other non-representable values
8586 APFloat RHSRoundInt(*RHS);
8587 RHSRoundInt.roundToIntegral(RM: APFloat::rmNearestTiesToEven);
8588 if (*RHS != RHSRoundInt) {
8589 if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ)
8590 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8591
8592 assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE);
8593 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8594 }
8595 }
8596
8597 // TODO: If the constant is exactly representable, is it always OK to do
8598 // equality compares as integer?
8599 }
8600
8601 // Check to see that the input is converted from an integer type that is small
8602 // enough that preserves all bits. TODO: check here for "known" sign bits.
8603 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
8604
8605 // Following test does NOT adjust IntWidth downwards for signed inputs,
8606 // because the most negative value still requires all the mantissa bits
8607 // to distinguish it from one less than that value.
8608 if ((int)IntWidth > MantissaWidth) {
8609 // Conversion would lose accuracy. Check if loss can impact comparison.
8610 int Exp = ilogb(Arg: *RHS);
8611 if (Exp == APFloat::IEK_Inf) {
8612 int MaxExponent = ilogb(Arg: APFloat::getLargest(Sem: RHS->getSemantics()));
8613 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
8614 // Conversion could create infinity.
8615 return nullptr;
8616 } else {
8617 // Note that if RHS is zero or NaN, then Exp is negative
8618 // and first condition is trivially false.
8619 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
8620 // Conversion could affect comparison.
8621 return nullptr;
8622 }
8623 }
8624
8625 // Otherwise, we can potentially simplify the comparison. We know that it
8626 // will always come through as an integer value and we know the constant is
8627 // not a NAN (it would have been previously simplified).
8628 assert(!RHS->isNaN() && "NaN comparison not already folded!");
8629
8630 ICmpInst::Predicate Pred;
8631 switch (I.getPredicate()) {
8632 default:
8633 llvm_unreachable("Unexpected predicate!");
8634 case FCmpInst::FCMP_UEQ:
8635 case FCmpInst::FCMP_OEQ:
8636 Pred = ICmpInst::ICMP_EQ;
8637 break;
8638 case FCmpInst::FCMP_UGT:
8639 case FCmpInst::FCMP_OGT:
8640 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
8641 break;
8642 case FCmpInst::FCMP_UGE:
8643 case FCmpInst::FCMP_OGE:
8644 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
8645 break;
8646 case FCmpInst::FCMP_ULT:
8647 case FCmpInst::FCMP_OLT:
8648 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
8649 break;
8650 case FCmpInst::FCMP_ULE:
8651 case FCmpInst::FCMP_OLE:
8652 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
8653 break;
8654 case FCmpInst::FCMP_UNE:
8655 case FCmpInst::FCMP_ONE:
8656 Pred = ICmpInst::ICMP_NE;
8657 break;
8658 case FCmpInst::FCMP_ORD:
8659 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8660 case FCmpInst::FCMP_UNO:
8661 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8662 }
8663
8664 // Now we know that the APFloat is a normal number, zero or inf.
8665
8666 // See if the FP constant is too large for the integer. For example,
8667 // comparing an i8 to 300.0.
8668 if (!LHSUnsigned) {
8669 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
8670 // and large values.
8671 APFloat SMax(RHS->getSemantics());
8672 SMax.convertFromAPInt(Input: APInt::getSignedMaxValue(numBits: IntWidth), IsSigned: true,
8673 RM: APFloat::rmNearestTiesToEven);
8674 if (SMax < *RHS) { // smax < 13123.0
8675 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
8676 Pred == ICmpInst::ICMP_SLE)
8677 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8678 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8679 }
8680 } else {
8681 // If the RHS value is > UnsignedMax, fold the comparison. This handles
8682 // +INF and large values.
8683 APFloat UMax(RHS->getSemantics());
8684 UMax.convertFromAPInt(Input: APInt::getMaxValue(numBits: IntWidth), IsSigned: false,
8685 RM: APFloat::rmNearestTiesToEven);
8686 if (UMax < *RHS) { // umax < 13123.0
8687 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
8688 Pred == ICmpInst::ICMP_ULE)
8689 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8690 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8691 }
8692 }
8693
8694 if (!LHSUnsigned) {
8695 // See if the RHS value is < SignedMin.
8696 APFloat SMin(RHS->getSemantics());
8697 SMin.convertFromAPInt(Input: APInt::getSignedMinValue(numBits: IntWidth), IsSigned: true,
8698 RM: APFloat::rmNearestTiesToEven);
8699 if (SMin > *RHS) { // smin > 12312.0
8700 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
8701 Pred == ICmpInst::ICMP_SGE)
8702 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8703 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8704 }
8705 } else {
8706 // See if the RHS value is < UnsignedMin.
8707 APFloat UMin(RHS->getSemantics());
8708 UMin.convertFromAPInt(Input: APInt::getMinValue(numBits: IntWidth), IsSigned: false,
8709 RM: APFloat::rmNearestTiesToEven);
8710 if (UMin > *RHS) { // umin > 12312.0
8711 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
8712 Pred == ICmpInst::ICMP_UGE)
8713 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8714 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8715 }
8716 }
8717
8718 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
8719 // [0, UMAX], but it may still be fractional. Check whether this is the case
8720 // using the IsExact flag.
8721 // Don't do this for zero, because -0.0 is not fractional.
8722 APSInt RHSInt(IntWidth, LHSUnsigned);
8723 bool IsExact;
8724 RHS->convertToInteger(Result&: RHSInt, RM: APFloat::rmTowardZero, IsExact: &IsExact);
8725 if (!RHS->isZero()) {
8726 if (!IsExact) {
8727 // If we had a comparison against a fractional value, we have to adjust
8728 // the compare predicate and sometimes the value. RHSC is rounded towards
8729 // zero at this point.
8730 switch (Pred) {
8731 default:
8732 llvm_unreachable("Unexpected integer comparison!");
8733 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
8734 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8735 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
8736 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8737 case ICmpInst::ICMP_ULE:
8738 // (float)int <= 4.4 --> int <= 4
8739 // (float)int <= -4.4 --> false
8740 if (RHS->isNegative())
8741 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8742 break;
8743 case ICmpInst::ICMP_SLE:
8744 // (float)int <= 4.4 --> int <= 4
8745 // (float)int <= -4.4 --> int < -4
8746 if (RHS->isNegative())
8747 Pred = ICmpInst::ICMP_SLT;
8748 break;
8749 case ICmpInst::ICMP_ULT:
8750 // (float)int < -4.4 --> false
8751 // (float)int < 4.4 --> int <= 4
8752 if (RHS->isNegative())
8753 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8754 Pred = ICmpInst::ICMP_ULE;
8755 break;
8756 case ICmpInst::ICMP_SLT:
8757 // (float)int < -4.4 --> int < -4
8758 // (float)int < 4.4 --> int <= 4
8759 if (!RHS->isNegative())
8760 Pred = ICmpInst::ICMP_SLE;
8761 break;
8762 case ICmpInst::ICMP_UGT:
8763 // (float)int > 4.4 --> int > 4
8764 // (float)int > -4.4 --> true
8765 if (RHS->isNegative())
8766 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8767 break;
8768 case ICmpInst::ICMP_SGT:
8769 // (float)int > 4.4 --> int > 4
8770 // (float)int > -4.4 --> int >= -4
8771 if (RHS->isNegative())
8772 Pred = ICmpInst::ICMP_SGE;
8773 break;
8774 case ICmpInst::ICMP_UGE:
8775 // (float)int >= -4.4 --> true
8776 // (float)int >= 4.4 --> int > 4
8777 if (RHS->isNegative())
8778 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8779 Pred = ICmpInst::ICMP_UGT;
8780 break;
8781 case ICmpInst::ICMP_SGE:
8782 // (float)int >= -4.4 --> int >= -4
8783 // (float)int >= 4.4 --> int > 4
8784 if (!RHS->isNegative())
8785 Pred = ICmpInst::ICMP_SGT;
8786 break;
8787 }
8788 }
8789 }
8790
8791 // Lower this FP comparison into an appropriate integer version of the
8792 // comparison.
8793 return new ICmpInst(Pred, LHSI->getOperand(i: 0),
8794 ConstantInt::get(Ty: LHSI->getOperand(i: 0)->getType(), V: RHSInt));
8795}
8796
8797/// Fold fcmp/icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2)
8798/// where all true/false values are constants that allow the compare to be
8799/// constant-folded for every combination of C1 and C2.
8800/// We compute a 4-entry truth table and use createLogicFromTable to
8801/// synthesize a boolean expression of C1 and C2.
8802Instruction *InstCombinerImpl::foldCmpSelectOfConstants(CmpInst &I) {
8803 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
8804 Value *C1, *C2;
8805 Constant *TV1, *FV1, *TV2, *FV2;
8806
8807 if (!match(V: Op0, P: m_Select(C: m_Value(V&: C1), L: m_Constant(C&: TV1), R: m_Constant(C&: FV1))) ||
8808 !match(V: Op1, P: m_Select(C: m_Value(V&: C2), L: m_Constant(C&: TV2), R: m_Constant(C&: FV2))))
8809 return nullptr;
8810
8811 if (I.getType() != C1->getType() || I.getType() != C2->getType())
8812 return nullptr;
8813
8814 unsigned Pred = I.getPredicate();
8815 const DataLayout &DL = I.getDataLayout();
8816
8817 Constant *Res00 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: FV1, RHS: FV2, DL);
8818 Constant *Res01 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: FV1, RHS: TV2, DL);
8819 Constant *Res10 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: TV1, RHS: FV2, DL);
8820 Constant *Res11 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: TV1, RHS: TV2, DL);
8821
8822 if (!Res00 || !Res01 || !Res10 || !Res11)
8823 return nullptr;
8824
8825 if ((!Res00->isNullValue() && !Res00->isAllOnesValue()) ||
8826 (!Res01->isNullValue() && !Res01->isAllOnesValue()) ||
8827 (!Res10->isNullValue() && !Res10->isAllOnesValue()) ||
8828 (!Res11->isNullValue() && !Res11->isAllOnesValue()))
8829 return nullptr;
8830
8831 std::bitset<4> Table;
8832 if (!Res00->isNullValue())
8833 Table.set(position: 0);
8834 if (!Res01->isNullValue())
8835 Table.set(position: 1);
8836 if (!Res10->isNullValue())
8837 Table.set(position: 2);
8838 if (!Res11->isNullValue())
8839 Table.set(position: 3);
8840
8841 Value *Res = createLogicFromTable(Table, Op0: C1, Op1: C2, Builder,
8842 HasOneUse: Op0->hasOneUse() && Op1->hasOneUse());
8843 if (!Res)
8844 return nullptr;
8845 return replaceInstUsesWith(I, V: Res);
8846}
8847
8848/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
8849static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,
8850 Constant *RHSC) {
8851 // When C is not 0.0 and infinities are not allowed:
8852 // (C / X) < 0.0 is a sign-bit test of X
8853 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
8854 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
8855 //
8856 // Proof:
8857 // Multiply (C / X) < 0.0 by X * X / C.
8858 // - X is non zero, if it is the flag 'ninf' is violated.
8859 // - C defines the sign of X * X * C. Thus it also defines whether to swap
8860 // the predicate. C is also non zero by definition.
8861 //
8862 // Thus X * X / C is non zero and the transformation is valid. [qed]
8863
8864 FCmpInst::Predicate Pred = I.getPredicate();
8865
8866 // Check that predicates are valid.
8867 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
8868 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
8869 return nullptr;
8870
8871 // Check that RHS operand is zero.
8872 if (!match(V: RHSC, P: m_AnyZeroFP()))
8873 return nullptr;
8874
8875 // Check fastmath flags ('ninf').
8876 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
8877 return nullptr;
8878
8879 // Check the properties of the dividend. It must not be zero to avoid a
8880 // division by zero (see Proof).
8881 const APFloat *C;
8882 if (!match(V: LHSI->getOperand(i: 0), P: m_APFloat(Res&: C)))
8883 return nullptr;
8884
8885 if (C->isZero())
8886 return nullptr;
8887
8888 // Get swapped predicate if necessary.
8889 if (C->isNegative())
8890 Pred = I.getSwappedPredicate();
8891
8892 return new FCmpInst(Pred, LHSI->getOperand(i: 1), RHSC, "", &I);
8893}
8894
8895// Transform 'fptrunc(x) cmp C' to 'x cmp ext(C)' if possible.
8896// Patterns include:
8897// fptrunc(x) < C --> x < ext(C)
8898// fptrunc(x) <= C --> x <= ext(C)
8899// fptrunc(x) > C --> x > ext(C)
8900// fptrunc(x) >= C --> x >= ext(C)
8901// fptrunc(x) ord/uno C --> x ord/uno 0
8902// where 'ext(C)' is the extension of 'C' to the type of 'x' with a small bias
8903// due to precision loss.
8904static Instruction *foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc,
8905 const Constant &C) {
8906 FCmpInst::Predicate Pred = I.getPredicate();
8907 Type *DestType = FPTrunc.getOperand(i: 0)->getType();
8908
8909 const APFloat *CValue;
8910 // TODO: support vec
8911 if (!match(V: &C, P: m_APFloat(Res&: CValue)))
8912 return nullptr;
8913
8914 // Handle ord/uno
8915 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
8916 assert(!CValue->isNaN() &&
8917 "X ord/uno NaN should be folded away by simplifyFCmpInst()");
8918 return new FCmpInst(Pred, FPTrunc.getOperand(i: 0),
8919 ConstantFP::getZero(Ty: DestType), "", &I);
8920 }
8921
8922 // Handle <, >, <=, >=
8923 bool RoundDown = false;
8924
8925 if (Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE ||
8926 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT)
8927 RoundDown = true;
8928 else if (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT ||
8929 Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)
8930 RoundDown = false;
8931 else
8932 return nullptr;
8933
8934 if (CValue->isNaN() || CValue->isInfinity())
8935 return nullptr;
8936
8937 auto ConvertFltSema = [](const APFloat &Src, const fltSemantics &Sema) {
8938 bool LosesInfo;
8939 APFloat Dest = Src;
8940 Dest.convert(ToSemantics: Sema, RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
8941 return Dest;
8942 };
8943
8944 auto NextValue = [](const APFloat &Value, bool RoundDown) {
8945 APFloat NextValue = Value;
8946 NextValue.next(nextDown: RoundDown);
8947 return NextValue;
8948 };
8949
8950 APFloat NextCValue = NextValue(*CValue, RoundDown);
8951
8952 const fltSemantics &DestFltSema =
8953 DestType->getScalarType()->getFltSemantics();
8954
8955 APFloat ExtCValue = ConvertFltSema(*CValue, DestFltSema);
8956 APFloat ExtNextCValue = ConvertFltSema(NextCValue, DestFltSema);
8957
8958 // When 'NextCValue' is infinity, use an imaged 'NextCValue' that equals
8959 // 'CValue + bias' to avoid the infinity after conversion. The bias is
8960 // estimated as 'CValue - PrevCValue', where 'PrevCValue' is the previous
8961 // value of 'CValue'.
8962 if (NextCValue.isInfinity()) {
8963 APFloat PrevCValue = NextValue(*CValue, !RoundDown);
8964 APFloat Bias = ConvertFltSema(*CValue - PrevCValue, DestFltSema);
8965
8966 ExtNextCValue = ExtCValue + Bias;
8967 }
8968
8969 APFloat ExtMidValue =
8970 scalbn(X: ExtCValue + ExtNextCValue, Exp: -1, RM: APFloat::rmNearestTiesToEven);
8971
8972 const fltSemantics &SrcFltSema =
8973 C.getType()->getScalarType()->getFltSemantics();
8974
8975 // 'MidValue' might be rounded to 'NextCValue'. Correct it here.
8976 APFloat MidValue = ConvertFltSema(ExtMidValue, SrcFltSema);
8977 if (MidValue != *CValue)
8978 ExtMidValue.next(nextDown: !RoundDown);
8979
8980 // Check whether 'ExtMidValue' is a valid result since the assumption on
8981 // imaged 'NextCValue' might not hold for new float types.
8982 // ppc_fp128 can't pass here when converting from max float because of
8983 // APFloat implementation.
8984 if (NextCValue.isInfinity()) {
8985 // ExtMidValue --- narrowed ---> Finite
8986 if (ConvertFltSema(ExtMidValue, SrcFltSema).isInfinity())
8987 return nullptr;
8988
8989 // NextExtMidValue --- narrowed ---> Infinity
8990 APFloat NextExtMidValue = NextValue(ExtMidValue, RoundDown);
8991 if (ConvertFltSema(NextExtMidValue, SrcFltSema).isFinite())
8992 return nullptr;
8993 }
8994
8995 return new FCmpInst(Pred, FPTrunc.getOperand(i: 0),
8996 ConstantFP::get(Ty: DestType, V: ExtMidValue), "", &I);
8997}
8998
8999/// Optimize fabs(X) compared with zero.
9000static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {
9001 Value *X;
9002 if (!match(V: I.getOperand(i_nocapture: 0), P: m_FAbs(Op0: m_Value(V&: X))))
9003 return nullptr;
9004
9005 const APFloat *C;
9006 if (!match(V: I.getOperand(i_nocapture: 1), P: m_APFloat(Res&: C)))
9007 return nullptr;
9008
9009 if (!C->isPosZero()) {
9010 if (!C->isSmallestNormalized())
9011 return nullptr;
9012
9013 const Function *F = I.getFunction();
9014 DenormalMode Mode = F->getDenormalMode(FPType: C->getSemantics());
9015 if (Mode.Input == DenormalMode::PreserveSign ||
9016 Mode.Input == DenormalMode::PositiveZero) {
9017
9018 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
9019 Constant *Zero = ConstantFP::getZero(Ty: X->getType());
9020 return new FCmpInst(P, X, Zero, "", I);
9021 };
9022
9023 switch (I.getPredicate()) {
9024 case FCmpInst::FCMP_OLT:
9025 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
9026 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
9027 case FCmpInst::FCMP_UGE:
9028 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
9029 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
9030 case FCmpInst::FCMP_OGE:
9031 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
9032 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
9033 case FCmpInst::FCMP_ULT:
9034 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
9035 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
9036 default:
9037 break;
9038 }
9039 }
9040
9041 return nullptr;
9042 }
9043
9044 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
9045 I->setPredicate(P);
9046 return IC.replaceOperand(I&: *I, OpNum: 0, V: X);
9047 };
9048
9049 switch (I.getPredicate()) {
9050 case FCmpInst::FCMP_UGE:
9051 case FCmpInst::FCMP_OLT:
9052 // fabs(X) >= 0.0 --> true
9053 // fabs(X) < 0.0 --> false
9054 llvm_unreachable("fcmp should have simplified");
9055
9056 case FCmpInst::FCMP_OGT:
9057 // fabs(X) > 0.0 --> X != 0.0
9058 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
9059
9060 case FCmpInst::FCMP_UGT:
9061 // fabs(X) u> 0.0 --> X u!= 0.0
9062 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
9063
9064 case FCmpInst::FCMP_OLE:
9065 // fabs(X) <= 0.0 --> X == 0.0
9066 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
9067
9068 case FCmpInst::FCMP_ULE:
9069 // fabs(X) u<= 0.0 --> X u== 0.0
9070 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
9071
9072 case FCmpInst::FCMP_OGE:
9073 // fabs(X) >= 0.0 --> !isnan(X)
9074 assert(!I.hasNoNaNs() && "fcmp should have simplified");
9075 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
9076
9077 case FCmpInst::FCMP_ULT:
9078 // fabs(X) u< 0.0 --> isnan(X)
9079 assert(!I.hasNoNaNs() && "fcmp should have simplified");
9080 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
9081
9082 case FCmpInst::FCMP_OEQ:
9083 case FCmpInst::FCMP_UEQ:
9084 case FCmpInst::FCMP_ONE:
9085 case FCmpInst::FCMP_UNE:
9086 case FCmpInst::FCMP_ORD:
9087 case FCmpInst::FCMP_UNO:
9088 // Look through the fabs() because it doesn't change anything but the sign.
9089 // fabs(X) == 0.0 --> X == 0.0,
9090 // fabs(X) != 0.0 --> X != 0.0
9091 // isnan(fabs(X)) --> isnan(X)
9092 // !isnan(fabs(X) --> !isnan(X)
9093 return replacePredAndOp0(&I, I.getPredicate(), X);
9094
9095 default:
9096 return nullptr;
9097 }
9098}
9099
9100/// Optimize sqrt(X) compared with zero.
9101static Instruction *foldSqrtWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {
9102 Value *X;
9103 if (!match(V: I.getOperand(i_nocapture: 0), P: m_Sqrt(Op0: m_Value(V&: X))))
9104 return nullptr;
9105
9106 if (!match(V: I.getOperand(i_nocapture: 1), P: m_PosZeroFP()))
9107 return nullptr;
9108
9109 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
9110 I.setPredicate(P);
9111 return IC.replaceOperand(I, OpNum: 0, V: X);
9112 };
9113
9114 // Clear ninf flag if sqrt doesn't have it.
9115 if (!cast<Instruction>(Val: I.getOperand(i_nocapture: 0))->hasNoInfs())
9116 I.setHasNoInfs(false);
9117
9118 switch (I.getPredicate()) {
9119 case FCmpInst::FCMP_OLT:
9120 case FCmpInst::FCMP_UGE:
9121 // sqrt(X) < 0.0 --> false
9122 // sqrt(X) u>= 0.0 --> true
9123 llvm_unreachable("fcmp should have simplified");
9124 case FCmpInst::FCMP_ULT:
9125 case FCmpInst::FCMP_ULE:
9126 case FCmpInst::FCMP_OGT:
9127 case FCmpInst::FCMP_OGE:
9128 case FCmpInst::FCMP_OEQ:
9129 case FCmpInst::FCMP_UNE:
9130 // sqrt(X) u< 0.0 --> X u< 0.0
9131 // sqrt(X) u<= 0.0 --> X u<= 0.0
9132 // sqrt(X) > 0.0 --> X > 0.0
9133 // sqrt(X) >= 0.0 --> X >= 0.0
9134 // sqrt(X) == 0.0 --> X == 0.0
9135 // sqrt(X) u!= 0.0 --> X u!= 0.0
9136 return IC.replaceOperand(I, OpNum: 0, V: X);
9137
9138 case FCmpInst::FCMP_OLE:
9139 // sqrt(X) <= 0.0 --> X == 0.0
9140 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
9141 case FCmpInst::FCMP_UGT:
9142 // sqrt(X) u> 0.0 --> X u!= 0.0
9143 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
9144 case FCmpInst::FCMP_UEQ:
9145 // sqrt(X) u== 0.0 --> X u<= 0.0
9146 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
9147 case FCmpInst::FCMP_ONE:
9148 // sqrt(X) != 0.0 --> X > 0.0
9149 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
9150 case FCmpInst::FCMP_ORD:
9151 // !isnan(sqrt(X)) --> X >= 0.0
9152 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
9153 case FCmpInst::FCMP_UNO:
9154 // isnan(sqrt(X)) --> X u< 0.0
9155 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
9156 default:
9157 llvm_unreachable("Unexpected predicate!");
9158 }
9159}
9160
9161static Instruction *foldFCmpFNegCommonOp(FCmpInst &I) {
9162 CmpInst::Predicate Pred = I.getPredicate();
9163 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
9164
9165 // Canonicalize fneg as Op1.
9166 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
9167 if (match(V: Op0, P: m_FNegOrFSubNegZero(X: m_Value())) &&
9168 !match(V: Op1, P: m_FNegOrFSubNegZero(X: m_Value()))) {
9169 std::swap(a&: Op0, b&: Op1);
9170 Pred = I.getSwappedPredicate();
9171 }
9172
9173 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
9174 if (!match(V: Op1, P: m_FNegOrFSubNegZero(X: m_Specific(V: Op0))))
9175 return nullptr;
9176
9177 // Replace the negated operand with 0.0:
9178 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
9179 Constant *Zero = ConstantFP::getZero(Ty: Op0->getType());
9180 return new FCmpInst(Pred, Op0, Zero, "", &I);
9181}
9182
9183static Instruction *foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI,
9184 Constant *RHSC, InstCombinerImpl &CI) {
9185 const CmpInst::Predicate Pred = I.getPredicate();
9186 Value *X = LHSI->getOperand(i: 0);
9187 Value *Y = LHSI->getOperand(i: 1);
9188 switch (Pred) {
9189 default:
9190 break;
9191 case FCmpInst::FCMP_UGT:
9192 case FCmpInst::FCMP_ULT:
9193 case FCmpInst::FCMP_UNE:
9194 case FCmpInst::FCMP_OEQ:
9195 case FCmpInst::FCMP_OGE:
9196 case FCmpInst::FCMP_OLE:
9197 // The optimization is not valid if X and Y are infinities of the same
9198 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
9199 // flag then we can assume we do not have that case. Otherwise we might be
9200 // able to prove that either X or Y is not infinity.
9201 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
9202 !isKnownNeverInfinity(V: Y,
9203 SQ: CI.getSimplifyQuery().getWithInstruction(I: &I)) &&
9204 !isKnownNeverInfinity(V: X, SQ: CI.getSimplifyQuery().getWithInstruction(I: &I)))
9205 break;
9206
9207 [[fallthrough]];
9208 case FCmpInst::FCMP_OGT:
9209 case FCmpInst::FCMP_OLT:
9210 case FCmpInst::FCMP_ONE:
9211 case FCmpInst::FCMP_UEQ:
9212 case FCmpInst::FCMP_UGE:
9213 case FCmpInst::FCMP_ULE:
9214 // fcmp pred (x - y), 0 --> fcmp pred x, y
9215 if (match(V: RHSC, P: m_AnyZeroFP()) &&
9216 I.getFunction()->getDenormalMode(
9217 FPType: LHSI->getType()->getScalarType()->getFltSemantics()) ==
9218 DenormalMode::getIEEE()) {
9219 CI.replaceOperand(I, OpNum: 0, V: X);
9220 CI.replaceOperand(I, OpNum: 1, V: Y);
9221 I.setHasNoInfs(LHSI->hasNoInfs());
9222 if (LHSI->hasNoNaNs())
9223 I.setHasNoNaNs(true);
9224 return &I;
9225 }
9226 // fcmp `pred (C - Y), C` -> `fcmp swap(pred), Y, 0`
9227 // where C and Y can't be arbitrary floating-point values.
9228 // For example, with `C = 1.0f` and `Y = 0x1p-149`, `1.0f - Y` rounds back
9229 // to `1.0f`, so the source compare is false while the rewritten compare is
9230 // true.
9231 // We need to make sure (C - Y) never rounds back to C
9232 const APFloat *C;
9233 Value *IntSrc;
9234 if (match(V: RHSC, P: m_APFloat(Res&: C)) &&
9235 match(V: LHSI, P: m_FSub(L: m_Specific(V: RHSC), R: m_IToFP(Op: m_Value(V&: IntSrc)))) &&
9236 C->isNormal()) {
9237 // Requirements on C and Y:
9238 // 1. C is finite, nonzero, normal.
9239 // 2. C shouldn't be too large, that is, ULP(C) <= 1.
9240 // 3. Y must be the form of `[su]itofp`, so the finite nonzero result of Y
9241 // must be integer-valued with an absolute value of at least 1;
9242 // as long as the step size near C does not exceed 1,
9243 // C - Y cannot be rounded back to C when Y != 0.
9244 // 4. If Y = 0, `fcmp pred (C - 0), C` are equivalent to `fcmp swap(pred)
9245 // 0, 0` for ordered and unordered predicates as long as C is finite and
9246 // nonzero.
9247 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
9248 if (MantissaWidth != -1 && ilogb(Arg: *C) < MantissaWidth) {
9249 Constant *ZeroC = ConstantFP::getZero(Ty: LHSI->getType());
9250 I.setPredicate(I.getSwappedPredicate());
9251 CI.replaceOperand(I, OpNum: 0, V: Y);
9252 CI.replaceOperand(I, OpNum: 1, V: ZeroC);
9253 return &I;
9254 }
9255 }
9256 break;
9257 }
9258
9259 return nullptr;
9260}
9261
9262/// Fold: fabs(uitofp(a) - uitofp(b)) pred C --> a == b
9263/// where 'pred' is olt, ult, ogt, ugt, oge or uge and C is a positive, Non-NaN
9264/// float when the uitofp casts are exact and C is in the valid range.
9265///
9266/// Since exact uitofp means distinct integers map to distinct floats, the only
9267/// values fabs(uitofp(a) - uitofp(b)) can take are {0.0, 1.0, 2.0, ...}.
9268/// There are no values in the open interval (0, 1), so:
9269/// fabs(...) < C where 0 < C <= 1.0 --> a == b (strict lt: C=1.0 ok)
9270// fabs(..) >= C where C >= 1.0 -> a != b
9271///
9272/// The same logic applies to sitofp.
9273static Instruction *foldFCmpFAbsFSubIntToFP(FCmpInst &I, InstCombinerImpl &IC) {
9274 Value *FAbsArg;
9275 if (!match(V: I.getOperand(i_nocapture: 0), P: m_FAbs(Op0: m_Value(V&: FAbsArg))))
9276 return nullptr;
9277
9278 const APFloat *C;
9279 if (!match(V: I.getOperand(i_nocapture: 1), P: PatternMatch::m_FiniteNonZero(V&: C)))
9280 return nullptr;
9281
9282 FCmpInst::Predicate Pred = I.getPredicate();
9283 bool IsStrictLt = Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT;
9284 bool IsLe = Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE;
9285 bool IsStrictGt = Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT;
9286 bool IsGe = Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE;
9287 if (!IsStrictLt && !IsStrictGt && !IsGe)
9288 return nullptr;
9289
9290 APFloat One = APFloat::getOne(Sem: C->getSemantics());
9291 APFloat::cmpResult Cmp = C->compare(RHS: One);
9292
9293 // For strict-lt (olt/ult): C must be in (0, 1.0] -- C == 1.0 is fine since
9294 // the next possible value after 0.0 is 1.0, and < 1.0 excludes it.
9295 if (IsStrictLt && Cmp == APFloat::cmpGreaterThan)
9296 return nullptr;
9297 if (IsGe && Cmp == APFloat::cmpGreaterThan)
9298 return nullptr;
9299 if (IsLe && Cmp != APFloat::cmpGreaterThan)
9300 return nullptr;
9301 if (IsStrictGt && Cmp != APFloat::cmpLessThan)
9302 return nullptr;
9303
9304 // Match: fsub(uitofp(A), uitofp(B)) where both casts are uitofp or sitofp
9305 Value *A, *B;
9306 bool IsSigned;
9307 if (match(V: FAbsArg, P: m_FSub(L: m_UIToFP(Op: m_Value(V&: A)), R: m_UIToFP(Op: m_Value(V&: B))))) {
9308 IsSigned = false;
9309 } else if (match(V: FAbsArg,
9310 P: m_FSub(L: m_SIToFP(Op: m_Value(V&: A)), R: m_SIToFP(Op: m_Value(V&: B))))) {
9311 IsSigned = true;
9312 } else {
9313 return nullptr;
9314 }
9315
9316 // A and B must have the same integer type
9317 if (A->getType() != B->getType())
9318 return nullptr;
9319
9320 Type *FPTy = FAbsArg->getType();
9321 if (!IC.canBeCastedExactlyIntToFP(V: A, FPTy, IsSigned, CtxI: &I) ||
9322 !IC.canBeCastedExactlyIntToFP(V: B, FPTy, IsSigned, CtxI: &I))
9323 return nullptr;
9324 ICmpInst::Predicate ResultPred =
9325 IsStrictLt || IsLe ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
9326 return new ICmpInst(ResultPred, A, B);
9327}
9328
9329static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,
9330 InstCombinerImpl &IC) {
9331 Value *LHS = I.getOperand(i_nocapture: 0), *RHS = I.getOperand(i_nocapture: 1);
9332 Type *OpType = LHS->getType();
9333 CmpInst::Predicate Pred = I.getPredicate();
9334
9335 bool FloorX = match(V: LHS, P: m_Intrinsic<Intrinsic::floor>(Ops: m_Specific(V: RHS)));
9336 bool CeilX = match(V: LHS, P: m_Intrinsic<Intrinsic::ceil>(Ops: m_Specific(V: RHS)));
9337
9338 if (!FloorX && !CeilX) {
9339 if ((FloorX = match(V: RHS, P: m_Intrinsic<Intrinsic::floor>(Ops: m_Specific(V: LHS)))) ||
9340 (CeilX = match(V: RHS, P: m_Intrinsic<Intrinsic::ceil>(Ops: m_Specific(V: LHS))))) {
9341 std::swap(a&: LHS, b&: RHS);
9342 Pred = I.getSwappedPredicate();
9343 }
9344 }
9345
9346 if ((FloorX || CeilX) && FCmpInst::isCommutative(Pred) && LHS->hasOneUse()) {
9347 // fcmp pred floor(x), x => fcmp pred trunc(x), x
9348 // fcmp pred ceil(x), x => fcmp pred trunc(x), x
9349 // where pred is oeq, one, ord, ueq, une, uno.
9350 Value *TruncX = IC.Builder.CreateUnaryIntrinsic(ID: Intrinsic::trunc, Op: RHS);
9351 return new FCmpInst(Pred, TruncX, RHS, "", &I);
9352 }
9353
9354 switch (Pred) {
9355 case FCmpInst::FCMP_OLE:
9356 // fcmp ole floor(x), x => fcmp ord x, 0
9357 if (FloorX)
9358 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(Ty: OpType),
9359 "", &I);
9360 break;
9361 case FCmpInst::FCMP_OGT:
9362 // fcmp ogt floor(x), x => false
9363 if (FloorX)
9364 return IC.replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
9365 break;
9366 case FCmpInst::FCMP_OGE:
9367 // fcmp oge ceil(x), x => fcmp ord x, 0
9368 if (CeilX)
9369 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(Ty: OpType),
9370 "", &I);
9371 break;
9372 case FCmpInst::FCMP_OLT:
9373 // fcmp olt ceil(x), x => false
9374 if (CeilX)
9375 return IC.replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
9376 break;
9377 case FCmpInst::FCMP_ULE:
9378 // fcmp ule floor(x), x => true
9379 if (FloorX)
9380 return IC.replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
9381 break;
9382 case FCmpInst::FCMP_UGT:
9383 // fcmp ugt floor(x), x => fcmp uno x, 0
9384 if (FloorX)
9385 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(Ty: OpType),
9386 "", &I);
9387 break;
9388 case FCmpInst::FCMP_UGE:
9389 // fcmp uge ceil(x), x => true
9390 if (CeilX)
9391 return IC.replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
9392 break;
9393 case FCmpInst::FCMP_ULT:
9394 // fcmp ult ceil(x), x => fcmp uno x, 0
9395 if (CeilX)
9396 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(Ty: OpType),
9397 "", &I);
9398 break;
9399 default:
9400 break;
9401 }
9402
9403 return nullptr;
9404}
9405
9406/// Returns true if a select that implements a min/max is redundant and
9407/// select result can be replaced with its non-constant operand, e.g.,
9408/// select ( (si/ui-to-fp A) <= C ), C, (si/ui-to-fp A)
9409/// where C is the FP constant equal to the minimum integer value
9410/// representable by A.
9411static bool isMinMaxCmpSelectEliminable(SelectPatternFlavor Flavor, Value *A,
9412 Value *B) {
9413 const APFloat *APF;
9414 if (!match(V: B, P: m_APFloat(Res&: APF)))
9415 return false;
9416
9417 auto *I = dyn_cast<Instruction>(Val: A);
9418 if (!I || !(I->getOpcode() == Instruction::SIToFP ||
9419 I->getOpcode() == Instruction::UIToFP))
9420 return false;
9421
9422 bool IsUnsigned = I->getOpcode() == Instruction::UIToFP;
9423 unsigned BitWidth = I->getOperand(i: 0)->getType()->getScalarSizeInBits();
9424 APSInt IntBoundary = (Flavor == SPF_FMAXNUM)
9425 ? APSInt::getMinValue(numBits: BitWidth, Unsigned: IsUnsigned)
9426 : APSInt::getMaxValue(numBits: BitWidth, Unsigned: IsUnsigned);
9427 APSInt ConvertedInt(BitWidth, IsUnsigned);
9428 bool IsExact;
9429 APFloat::opStatus Status =
9430 APF->convertToInteger(Result&: ConvertedInt, RM: APFloat::rmTowardZero, IsExact: &IsExact);
9431 return Status == APFloat::opOK && IsExact && ConvertedInt == IntBoundary;
9432}
9433
9434Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
9435 bool Changed = false;
9436
9437 /// Orders the operands of the compare so that they are listed from most
9438 /// complex to least complex. This puts constants before unary operators,
9439 /// before binary operators.
9440 if (getComplexity(V: I.getOperand(i_nocapture: 0)) < getComplexity(V: I.getOperand(i_nocapture: 1))) {
9441 I.swapOperands();
9442 Changed = true;
9443 }
9444
9445 const CmpInst::Predicate Pred = I.getPredicate();
9446 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
9447 if (Value *V = simplifyFCmpInst(Predicate: Pred, LHS: Op0, RHS: Op1, FMF: I.getFastMathFlags(),
9448 Q: SQ.getWithInstruction(I: &I)))
9449 return replaceInstUsesWith(I, V);
9450
9451 // Simplify 'fcmp pred X, X'
9452 Type *OpType = Op0->getType();
9453 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
9454 if (Op0 == Op1) {
9455 switch (Pred) {
9456 default:
9457 break;
9458 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
9459 case FCmpInst::FCMP_ULT: // True if unordered or less than
9460 case FCmpInst::FCMP_UGT: // True if unordered or greater than
9461 case FCmpInst::FCMP_UNE: // True if unordered or not equal
9462 // Canonicalize these to be 'fcmp uno %X, 0.0'.
9463 I.setPredicate(FCmpInst::FCMP_UNO);
9464 I.setOperand(i_nocapture: 1, Val_nocapture: Constant::getNullValue(Ty: OpType));
9465 return &I;
9466
9467 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
9468 case FCmpInst::FCMP_OEQ: // True if ordered and equal
9469 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
9470 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
9471 // Canonicalize these to be 'fcmp ord %X, 0.0'.
9472 I.setPredicate(FCmpInst::FCMP_ORD);
9473 I.setOperand(i_nocapture: 1, Val_nocapture: Constant::getNullValue(Ty: OpType));
9474 return &I;
9475 }
9476 }
9477
9478 if (I.isCommutative()) {
9479 if (auto Pair = matchSymmetricPair(LHS: I.getOperand(i_nocapture: 0), RHS: I.getOperand(i_nocapture: 1))) {
9480 replaceOperand(I, OpNum: 0, V: Pair->first);
9481 replaceOperand(I, OpNum: 1, V: Pair->second);
9482 return &I;
9483 }
9484 }
9485
9486 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
9487 // then canonicalize the operand to 0.0.
9488 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
9489 if (!match(V: Op0, P: m_PosZeroFP()) &&
9490 isKnownNeverNaN(V: Op0, SQ: getSimplifyQuery().getWithInstruction(I: &I)))
9491 return replaceOperand(I, OpNum: 0, V: ConstantFP::getZero(Ty: OpType));
9492
9493 if (!match(V: Op1, P: m_PosZeroFP()) &&
9494 isKnownNeverNaN(V: Op1, SQ: getSimplifyQuery().getWithInstruction(I: &I)))
9495 return replaceOperand(I, OpNum: 1, V: ConstantFP::getZero(Ty: OpType));
9496 }
9497
9498 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
9499 Value *X, *Y;
9500 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
9501 if (match(V: Op0, P: m_FNegOrFSubNegZero(X: m_Value(V&: X))) &&
9502 match(V: Op1, P: m_FNegOrFSubNegZero(X: m_Value(V&: Y))))
9503 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
9504
9505 if (Instruction *R = foldFCmpFNegCommonOp(I))
9506 return R;
9507
9508 // Test if the FCmpInst instruction is used exclusively by a select as
9509 // part of a minimum or maximum operation. If so, refrain from doing
9510 // any other folding. This helps out other analyses which understand
9511 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
9512 // and CodeGen. And in this case, at least one of the comparison
9513 // operands has at least one user besides the compare (the select),
9514 // which would often largely negate the benefit of folding anyway.
9515 if (I.hasOneUse())
9516 if (SelectInst *SI = dyn_cast<SelectInst>(Val: I.user_back())) {
9517 Value *A, *B;
9518 SelectPatternResult SPR = matchSelectPattern(V: SI, LHS&: A, RHS&: B);
9519 bool IsRedundantMinMaxClamp =
9520 (SPR.Flavor == SPF_FMAXNUM || SPR.Flavor == SPF_FMINNUM) &&
9521 isMinMaxCmpSelectEliminable(Flavor: SPR.Flavor, A, B);
9522 if (SPR.Flavor != SPF_UNKNOWN && !IsRedundantMinMaxClamp)
9523 return nullptr;
9524 }
9525
9526 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
9527 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
9528 if (match(V: Op1, P: m_AnyZeroFP()) && !match(V: Op1, P: m_PosZeroFP()))
9529 return replaceOperand(I, OpNum: 1, V: ConstantFP::getZero(Ty: OpType));
9530
9531 // Canonicalize:
9532 // fcmp olt X, +inf -> fcmp one X, +inf
9533 // fcmp ole X, +inf -> fcmp ord X, 0
9534 // fcmp ogt X, +inf -> false
9535 // fcmp oge X, +inf -> fcmp oeq X, +inf
9536 // fcmp ult X, +inf -> fcmp une X, +inf
9537 // fcmp ule X, +inf -> true
9538 // fcmp ugt X, +inf -> fcmp uno X, 0
9539 // fcmp uge X, +inf -> fcmp ueq X, +inf
9540 // fcmp olt X, -inf -> false
9541 // fcmp ole X, -inf -> fcmp oeq X, -inf
9542 // fcmp ogt X, -inf -> fcmp one X, -inf
9543 // fcmp oge X, -inf -> fcmp ord X, 0
9544 // fcmp ult X, -inf -> fcmp uno X, 0
9545 // fcmp ule X, -inf -> fcmp ueq X, -inf
9546 // fcmp ugt X, -inf -> fcmp une X, -inf
9547 // fcmp uge X, -inf -> true
9548 const APFloat *C;
9549 if (match(V: Op1, P: m_APFloat(Res&: C)) && C->isInfinity()) {
9550 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(pred: Pred) : Pred) {
9551 default:
9552 break;
9553 case FCmpInst::FCMP_ORD:
9554 case FCmpInst::FCMP_UNO:
9555 case FCmpInst::FCMP_TRUE:
9556 case FCmpInst::FCMP_FALSE:
9557 case FCmpInst::FCMP_OGT:
9558 case FCmpInst::FCMP_ULE:
9559 llvm_unreachable("Should be simplified by InstSimplify");
9560 case FCmpInst::FCMP_OLT:
9561 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
9562 case FCmpInst::FCMP_OLE:
9563 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(Ty: OpType),
9564 "", &I);
9565 case FCmpInst::FCMP_OGE:
9566 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
9567 case FCmpInst::FCMP_ULT:
9568 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
9569 case FCmpInst::FCMP_UGT:
9570 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(Ty: OpType),
9571 "", &I);
9572 case FCmpInst::FCMP_UGE:
9573 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
9574 }
9575 }
9576
9577 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
9578 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
9579 if (match(V: Op1, P: m_PosZeroFP()) &&
9580 match(V: Op0, P: m_OneUse(SubPattern: m_ElementWiseBitCast(Op: m_Value(V&: X)))) &&
9581 X->getType()->isIntOrIntVectorTy() &&
9582 !F.getDenormalMode(FPType: Op1->getType()->getScalarType()->getFltSemantics())
9583 .inputsMayBeZero()) {
9584 ICmpInst::Predicate IntPred = ICmpInst::BAD_ICMP_PREDICATE;
9585 if (Pred == FCmpInst::FCMP_OEQ)
9586 IntPred = ICmpInst::ICMP_EQ;
9587 else if (Pred == FCmpInst::FCMP_UNE)
9588 IntPred = ICmpInst::ICMP_NE;
9589
9590 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
9591 Type *IntTy = X->getType();
9592 const APInt &SignMask = ~APInt::getSignMask(BitWidth: IntTy->getScalarSizeInBits());
9593 Value *MaskX = Builder.CreateAnd(LHS: X, RHS: ConstantInt::get(Ty: IntTy, V: SignMask));
9594 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(Ty: IntTy));
9595 }
9596 }
9597
9598 // Handle fcmp with instruction LHS and constant RHS.
9599 Instruction *LHSI;
9600 Constant *RHSC;
9601 if (match(V: Op0, P: m_Instruction(I&: LHSI)) && match(V: Op1, P: m_Constant(C&: RHSC))) {
9602 switch (LHSI->getOpcode()) {
9603 case Instruction::Select:
9604 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
9605 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
9606 if (FCmpInst::isEquality(Pred) && match(V: RHSC, P: m_AnyZeroFP()) &&
9607 match(V: LHSI,
9608 P: m_c_Select(L: m_FNegOrFSubNegZero(X: m_Value(V&: X)), R: m_Deferred(V: X))))
9609 return replaceOperand(I, OpNum: 0, V: X);
9610 if (Instruction *NV = FoldOpIntoSelect(Op&: I, SI: cast<SelectInst>(Val: LHSI)))
9611 return NV;
9612 break;
9613 case Instruction::FSub:
9614 if (LHSI->hasOneUse())
9615 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, CI&: *this))
9616 return NV;
9617 break;
9618 case Instruction::PHI:
9619 if (Instruction *NV = foldOpIntoPhi(I, PN: cast<PHINode>(Val: LHSI)))
9620 return NV;
9621 break;
9622 case Instruction::SIToFP:
9623 case Instruction::UIToFP:
9624 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
9625 return NV;
9626 break;
9627 case Instruction::FDiv:
9628 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
9629 return NV;
9630 break;
9631 case Instruction::Load:
9632 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(
9633 LI: cast<LoadInst>(Val: LHSI), Pred: I.getPredicate(), RHSC, CtxI&: I))
9634 return Res;
9635 break;
9636 case Instruction::FPTrunc:
9637 if (Instruction *NV = foldFCmpFpTrunc(I, FPTrunc: *LHSI, C: *RHSC))
9638 return NV;
9639 break;
9640 }
9641 }
9642
9643 if (Instruction *R = foldFabsWithFcmpZero(I, IC&: *this))
9644 return R;
9645
9646 if (Instruction *R = foldFCmpFAbsFSubIntToFP(I, IC&: *this))
9647 return R;
9648
9649 if (Instruction *R = foldSqrtWithFcmpZero(I, IC&: *this))
9650 return R;
9651
9652 if (Instruction *R = foldFCmpWithFloorAndCeil(I, IC&: *this))
9653 return R;
9654
9655 if (Instruction *R = foldCmpSelectOfConstants(I))
9656 return R;
9657
9658 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
9659 if (match(V: Op0, P: m_FNegOrFSubNegZero(X: m_Value(V&: X)))) {
9660 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
9661 Constant *C;
9662 if (match(V: Op1, P: m_Constant(C)))
9663 if (Constant *NegC = ConstantFoldUnaryOpOperand(Opcode: Instruction::FNeg, Op: C, DL))
9664 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
9665 }
9666
9667 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
9668 if (match(V: Op0, P: m_FAdd(L: m_Value(V&: X), R: m_AnyZeroFP())))
9669 return new FCmpInst(Pred, X, Op1, "", &I);
9670
9671 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
9672 if (match(V: Op1, P: m_FAdd(L: m_Value(V&: Y), R: m_AnyZeroFP())))
9673 return new FCmpInst(Pred, Op0, Y, "", &I);
9674
9675 // fcmp ord/uno (fptrunc X), (fptrunc Y) -> fcmp ord/uno X, Y
9676 if ((Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) &&
9677 match(V: Op0, P: m_FPTrunc(Op: m_Value(V&: X))) && match(V: Op1, P: m_FPTrunc(Op: m_Value(V&: Y))) &&
9678 X->getType() == Y->getType())
9679 return new FCmpInst(Pred, X, Y, "", &I);
9680
9681 if (match(V: Op0, P: m_FPExt(Op: m_Value(V&: X)))) {
9682 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
9683 if (match(V: Op1, P: m_FPExt(Op: m_Value(V&: Y))) && X->getType() == Y->getType())
9684 return new FCmpInst(Pred, X, Y, "", &I);
9685
9686 const APFloat *C;
9687 if (match(V: Op1, P: m_APFloat(Res&: C))) {
9688 const fltSemantics &FPSem =
9689 X->getType()->getScalarType()->getFltSemantics();
9690 bool Lossy;
9691 APFloat TruncC = *C;
9692 TruncC.convert(ToSemantics: FPSem, RM: APFloat::rmNearestTiesToEven, losesInfo: &Lossy);
9693
9694 if (Lossy) {
9695 // X can't possibly equal the higher-precision constant, so reduce any
9696 // equality comparison.
9697 // TODO: Other predicates can be handled via getFCmpCode().
9698 switch (Pred) {
9699 case FCmpInst::FCMP_OEQ:
9700 // X is ordered and equal to an impossible constant --> false
9701 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
9702 case FCmpInst::FCMP_ONE:
9703 // X is ordered and not equal to an impossible constant --> ordered
9704 return new FCmpInst(FCmpInst::FCMP_ORD, X,
9705 ConstantFP::getZero(Ty: X->getType()));
9706 case FCmpInst::FCMP_UEQ:
9707 // X is unordered or equal to an impossible constant --> unordered
9708 return new FCmpInst(FCmpInst::FCMP_UNO, X,
9709 ConstantFP::getZero(Ty: X->getType()));
9710 case FCmpInst::FCMP_UNE:
9711 // X is unordered or not equal to an impossible constant --> true
9712 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
9713 default:
9714 break;
9715 }
9716 }
9717
9718 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
9719 // Avoid lossy conversions and denormals.
9720 // Zero is a special case that's OK to convert.
9721 APFloat Fabs = TruncC;
9722 Fabs.clearSign();
9723 if (!Lossy &&
9724 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(Sem: FPSem)))) {
9725 Constant *NewC = ConstantFP::get(Ty: X->getType(), V: TruncC);
9726 return new FCmpInst(Pred, X, NewC, "", &I);
9727 }
9728 }
9729 }
9730
9731 // Convert a sign-bit test of an FP value into a cast and integer compare.
9732 // TODO: Simplify if the copysign constant is 0.0 or NaN.
9733 // TODO: Handle non-zero compare constants.
9734 // TODO: Handle other predicates.
9735 if (match(V: Op0, P: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::copysign>(Ops: m_APFloat(Res&: C),
9736 Ops: m_Value(V&: X)))) &&
9737 match(V: Op1, P: m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
9738 Type *IntType = Builder.getIntNTy(N: X->getType()->getScalarSizeInBits());
9739 if (auto *VecTy = dyn_cast<VectorType>(Val: OpType))
9740 IntType = VectorType::get(ElementType: IntType, EC: VecTy->getElementCount());
9741
9742 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
9743 if (Pred == FCmpInst::FCMP_OLT) {
9744 Value *IntX = Builder.CreateBitCast(V: X, DestTy: IntType);
9745 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
9746 ConstantInt::getNullValue(Ty: IntType));
9747 }
9748 }
9749
9750 {
9751 Value *CanonLHS = nullptr;
9752 match(V: Op0, P: m_Intrinsic<Intrinsic::canonicalize>(Ops: m_Value(V&: CanonLHS)));
9753 // (canonicalize(x) == x) => (x == x)
9754 if (CanonLHS == Op1)
9755 return new FCmpInst(Pred, Op1, Op1, "", &I);
9756
9757 Value *CanonRHS = nullptr;
9758 match(V: Op1, P: m_Intrinsic<Intrinsic::canonicalize>(Ops: m_Value(V&: CanonRHS)));
9759 // (x == canonicalize(x)) => (x == x)
9760 if (CanonRHS == Op0)
9761 return new FCmpInst(Pred, Op0, Op0, "", &I);
9762
9763 // (canonicalize(x) == canonicalize(y)) => (x == y)
9764 if (CanonLHS && CanonRHS)
9765 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
9766 }
9767
9768 if (I.getType()->isVectorTy())
9769 if (Instruction *Res = foldVectorCmp(Cmp&: I, Builder))
9770 return Res;
9771
9772 return Changed ? &I : nullptr;
9773}
9774