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/APSInt.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/Analysis/CaptureTracking.h"
19#include "llvm/Analysis/CmpInstAnalysis.h"
20#include "llvm/Analysis/ConstantFolding.h"
21#include "llvm/Analysis/InstructionSimplify.h"
22#include "llvm/Analysis/Loads.h"
23#include "llvm/Analysis/Utils/Local.h"
24#include "llvm/Analysis/VectorUtils.h"
25#include "llvm/IR/ConstantRange.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/IntrinsicInst.h"
32#include "llvm/IR/PatternMatch.h"
33#include "llvm/Support/KnownBits.h"
34#include "llvm/Transforms/InstCombine/InstCombiner.h"
35#include <bitset>
36
37using namespace llvm;
38using namespace PatternMatch;
39
40#define DEBUG_TYPE "instcombine"
41
42// How many times is a select replaced by one of its operands?
43STATISTIC(NumSel, "Number of select opts");
44
45namespace llvm {
46extern cl::opt<bool> ProfcheckDisableMetadataFixes;
47}
48
49/// Compute Result = In1+In2, returning true if the result overflowed for this
50/// type.
51static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
52 bool IsSigned = false) {
53 bool Overflow;
54 if (IsSigned)
55 Result = In1.sadd_ov(RHS: In2, Overflow);
56 else
57 Result = In1.uadd_ov(RHS: In2, Overflow);
58
59 return Overflow;
60}
61
62/// Compute Result = In1-In2, returning true if the result overflowed for this
63/// type.
64static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
65 bool IsSigned = false) {
66 bool Overflow;
67 if (IsSigned)
68 Result = In1.ssub_ov(RHS: In2, Overflow);
69 else
70 Result = In1.usub_ov(RHS: In2, Overflow);
71
72 return Overflow;
73}
74
75/// Given an icmp instruction, return true if any use of this comparison is a
76/// branch on sign bit comparison.
77static bool hasBranchUse(ICmpInst &I) {
78 for (auto *U : I.users())
79 if (isa<CondBrInst>(Val: U))
80 return true;
81 return false;
82}
83
84/// Returns true if the exploded icmp can be expressed as a signed comparison
85/// to zero and updates the predicate accordingly.
86/// The signedness of the comparison is preserved.
87/// TODO: Refactor with decomposeBitTestICmp()?
88static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
89 if (!ICmpInst::isSigned(Pred))
90 return false;
91
92 if (C.isZero())
93 return ICmpInst::isRelational(P: Pred);
94
95 if (C.isOne()) {
96 if (Pred == ICmpInst::ICMP_SLT) {
97 Pred = ICmpInst::ICMP_SLE;
98 return true;
99 }
100 } else if (C.isAllOnes()) {
101 if (Pred == ICmpInst::ICMP_SGT) {
102 Pred = ICmpInst::ICMP_SGE;
103 return true;
104 }
105 }
106
107 return false;
108}
109
110/// This is called when we see this pattern:
111/// cmp pred (load (gep GV, ...)), cmpcst
112/// where GV is a global variable with a constant initializer. Try to simplify
113/// this into some simple computation that does not need the load. For example
114/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
115///
116/// If AndCst is non-null, then the loaded value is masked with that constant
117/// before doing the comparison. This handles cases like "A[i]&4 == 0".
118Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
119 LoadInst *LI, GetElementPtrInst *GEP, CmpInst &ICI, ConstantInt *AndCst) {
120 auto *GV = dyn_cast<GlobalVariable>(Val: getUnderlyingObject(V: GEP));
121 if (LI->isVolatile() || !GV || !GV->isConstant() ||
122 !GV->hasDefinitiveInitializer())
123 return nullptr;
124
125 Type *EltTy = LI->getType();
126 TypeSize EltSize = DL.getTypeStoreSize(Ty: EltTy);
127 if (EltSize.isScalable())
128 return nullptr;
129
130 LinearExpression Expr = decomposeLinearExpression(DL, Ptr: GEP);
131 if (!Expr.Index || Expr.BasePtr != GV || Expr.Offset.getBitWidth() > 64)
132 return nullptr;
133
134 Constant *Init = GV->getInitializer();
135 TypeSize GlobalSize = DL.getTypeAllocSize(Ty: Init->getType());
136
137 Value *Idx = Expr.Index;
138 const APInt &Stride = Expr.Scale;
139 const APInt &ConstOffset = Expr.Offset;
140
141 // Allow an additional context offset, but only within the stride.
142 if (!ConstOffset.ult(RHS: Stride))
143 return nullptr;
144
145 // Don't handle overlapping loads for now.
146 if (!Stride.uge(RHS: EltSize.getFixedValue()))
147 return nullptr;
148
149 // Don't blow up on huge arrays.
150 uint64_t ArrayElementCount =
151 divideCeil(Numerator: (GlobalSize.getFixedValue() - ConstOffset.getZExtValue()),
152 Denominator: Stride.getZExtValue());
153 if (ArrayElementCount > MaxArraySizeForCombine)
154 return nullptr;
155
156 enum { Overdefined = -3, Undefined = -2 };
157
158 // Variables for our state machines.
159
160 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
161 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
162 // and 87 is the second (and last) index. FirstTrueElement is -2 when
163 // undefined, otherwise set to the first true element. SecondTrueElement is
164 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
165 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
166
167 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
168 // form "i != 47 & i != 87". Same state transitions as for true elements.
169 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
170
171 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
172 /// define a state machine that triggers for ranges of values that the index
173 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
174 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
175 /// index in the range (inclusive). We use -2 for undefined here because we
176 /// use relative comparisons and don't want 0-1 to match -1.
177 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
178
179 // MagicBitvector - This is a magic bitvector where we set a bit if the
180 // comparison is true for element 'i'. If there are 64 elements or less in
181 // the array, this will fully represent all the comparison results.
182 uint64_t MagicBitvector = 0;
183
184 // Scan the array and see if one of our patterns matches.
185 Constant *CompareRHS = cast<Constant>(Val: ICI.getOperand(i_nocapture: 1));
186 APInt Offset = ConstOffset;
187 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i, Offset += Stride) {
188 Constant *Elt = ConstantFoldLoadFromConst(C: Init, Ty: EltTy, Offset, DL);
189 if (!Elt)
190 return nullptr;
191
192 // If the element is masked, handle it.
193 if (AndCst) {
194 Elt = ConstantFoldBinaryOpOperands(Opcode: Instruction::And, LHS: Elt, RHS: AndCst, DL);
195 if (!Elt)
196 return nullptr;
197 }
198
199 // Find out if the comparison would be true or false for the i'th element.
200 Constant *C = ConstantFoldCompareInstOperands(Predicate: ICI.getPredicate(), LHS: Elt,
201 RHS: CompareRHS, DL, TLI: &TLI);
202 if (!C)
203 return nullptr;
204
205 // If the result is undef for this element, ignore it.
206 if (isa<UndefValue>(Val: C)) {
207 // Extend range state machines to cover this element in case there is an
208 // undef in the middle of the range.
209 if (TrueRangeEnd == (int)i - 1)
210 TrueRangeEnd = i;
211 if (FalseRangeEnd == (int)i - 1)
212 FalseRangeEnd = i;
213 continue;
214 }
215
216 // If we can't compute the result for any of the elements, we have to give
217 // up evaluating the entire conditional.
218 if (!isa<ConstantInt>(Val: C))
219 return nullptr;
220
221 // Otherwise, we know if the comparison is true or false for this element,
222 // update our state machines.
223 bool IsTrueForElt = !cast<ConstantInt>(Val: C)->isZero();
224
225 // State machine for single/double/range index comparison.
226 if (IsTrueForElt) {
227 // Update the TrueElement state machine.
228 if (FirstTrueElement == Undefined)
229 FirstTrueElement = TrueRangeEnd = i; // First true element.
230 else {
231 // Update double-compare state machine.
232 if (SecondTrueElement == Undefined)
233 SecondTrueElement = i;
234 else
235 SecondTrueElement = Overdefined;
236
237 // Update range state machine.
238 if (TrueRangeEnd == (int)i - 1)
239 TrueRangeEnd = i;
240 else
241 TrueRangeEnd = Overdefined;
242 }
243 } else {
244 // Update the FalseElement state machine.
245 if (FirstFalseElement == Undefined)
246 FirstFalseElement = FalseRangeEnd = i; // First false element.
247 else {
248 // Update double-compare state machine.
249 if (SecondFalseElement == Undefined)
250 SecondFalseElement = i;
251 else
252 SecondFalseElement = Overdefined;
253
254 // Update range state machine.
255 if (FalseRangeEnd == (int)i - 1)
256 FalseRangeEnd = i;
257 else
258 FalseRangeEnd = Overdefined;
259 }
260 }
261
262 // If this element is in range, update our magic bitvector.
263 if (i < 64 && IsTrueForElt)
264 MagicBitvector |= 1ULL << i;
265
266 // If all of our states become overdefined, bail out early. Since the
267 // predicate is expensive, only check it every 8 elements. This is only
268 // really useful for really huge arrays.
269 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
270 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
271 FalseRangeEnd == Overdefined)
272 return nullptr;
273 }
274
275 // Now that we've scanned the entire array, emit our new comparison(s). We
276 // order the state machines in complexity of the generated code.
277
278 // If inbounds keyword is not present, Idx * Stride can overflow.
279 // Let's assume that Stride is 2 and the wanted value is at offset 0.
280 // Then, there are two possible values for Idx to match offset 0:
281 // 0x00..00, 0x80..00.
282 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
283 // comparison is false if Idx was 0x80..00.
284 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.
285 auto MaskIdx = [&](Value *Idx) {
286 if (!Expr.Flags.isInBounds() && Stride.countr_zero() != 0) {
287 Value *Mask = Constant::getAllOnesValue(Ty: Idx->getType());
288 Mask = Builder.CreateLShr(LHS: Mask, RHS: Stride.countr_zero());
289 Idx = Builder.CreateAnd(LHS: Idx, RHS: Mask);
290 }
291 return Idx;
292 };
293
294 // If the comparison is only true for one or two elements, emit direct
295 // comparisons.
296 if (SecondTrueElement != Overdefined) {
297 Idx = MaskIdx(Idx);
298 // None true -> false.
299 if (FirstTrueElement == Undefined)
300 return replaceInstUsesWith(I&: ICI, V: Builder.getFalse());
301
302 Value *FirstTrueIdx = ConstantInt::get(Ty: Idx->getType(), V: FirstTrueElement);
303
304 // True for one element -> 'i == 47'.
305 if (SecondTrueElement == Undefined)
306 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
307
308 // True for two elements -> 'i == 47 | i == 72'.
309 Value *C1 = Builder.CreateICmpEQ(LHS: Idx, RHS: FirstTrueIdx);
310 Value *SecondTrueIdx = ConstantInt::get(Ty: Idx->getType(), V: SecondTrueElement);
311 Value *C2 = Builder.CreateICmpEQ(LHS: Idx, RHS: SecondTrueIdx);
312 return BinaryOperator::CreateOr(V1: C1, V2: C2);
313 }
314
315 // If the comparison is only false for one or two elements, emit direct
316 // comparisons.
317 if (SecondFalseElement != Overdefined) {
318 Idx = MaskIdx(Idx);
319 // None false -> true.
320 if (FirstFalseElement == Undefined)
321 return replaceInstUsesWith(I&: ICI, V: Builder.getTrue());
322
323 Value *FirstFalseIdx = ConstantInt::get(Ty: Idx->getType(), V: FirstFalseElement);
324
325 // False for one element -> 'i != 47'.
326 if (SecondFalseElement == Undefined)
327 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
328
329 // False for two elements -> 'i != 47 & i != 72'.
330 Value *C1 = Builder.CreateICmpNE(LHS: Idx, RHS: FirstFalseIdx);
331 Value *SecondFalseIdx =
332 ConstantInt::get(Ty: Idx->getType(), V: SecondFalseElement);
333 Value *C2 = Builder.CreateICmpNE(LHS: Idx, RHS: SecondFalseIdx);
334 return BinaryOperator::CreateAnd(V1: C1, V2: C2);
335 }
336
337 // If the comparison can be replaced with a range comparison for the elements
338 // where it is true, emit the range check.
339 if (TrueRangeEnd != Overdefined) {
340 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
341 Idx = MaskIdx(Idx);
342
343 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
344 if (FirstTrueElement) {
345 Value *Offs = ConstantInt::getSigned(Ty: Idx->getType(), V: -FirstTrueElement);
346 Idx = Builder.CreateAdd(LHS: Idx, RHS: Offs);
347 }
348
349 Value *End =
350 ConstantInt::get(Ty: Idx->getType(), V: TrueRangeEnd - FirstTrueElement + 1);
351 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
352 }
353
354 // False range check.
355 if (FalseRangeEnd != Overdefined) {
356 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
357 Idx = MaskIdx(Idx);
358 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
359 if (FirstFalseElement) {
360 Value *Offs = ConstantInt::getSigned(Ty: Idx->getType(), V: -FirstFalseElement);
361 Idx = Builder.CreateAdd(LHS: Idx, RHS: Offs);
362 }
363
364 Value *End =
365 ConstantInt::get(Ty: Idx->getType(), V: FalseRangeEnd - FirstFalseElement);
366 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
367 }
368
369 // If a magic bitvector captures the entire comparison state
370 // of this load, replace it with computation that does:
371 // ((magic_cst >> i) & 1) != 0
372 {
373 Type *Ty = nullptr;
374
375 // Look for an appropriate type:
376 // - The type of Idx if the magic fits
377 // - The smallest fitting legal type
378 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
379 Ty = Idx->getType();
380 else
381 Ty = DL.getSmallestLegalIntType(C&: Init->getContext(), Width: ArrayElementCount);
382
383 if (Ty) {
384 Idx = MaskIdx(Idx);
385 Value *V = Builder.CreateIntCast(V: Idx, DestTy: Ty, isSigned: false);
386 V = Builder.CreateLShr(LHS: ConstantInt::get(Ty, V: MagicBitvector), RHS: V);
387 V = Builder.CreateAnd(LHS: ConstantInt::get(Ty, V: 1), RHS: V);
388 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, V: 0));
389 }
390 }
391
392 return nullptr;
393}
394
395/// Returns true if we can rewrite Start as a GEP with pointer Base
396/// and some integer offset. The nodes that need to be re-written
397/// for this transformation will be added to Explored.
398static bool canRewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags &NW,
399 const DataLayout &DL,
400 SetVector<Value *> &Explored) {
401 SmallVector<Value *, 16> WorkList(1, Start);
402 Explored.insert(X: Base);
403
404 // The following traversal gives us an order which can be used
405 // when doing the final transformation. Since in the final
406 // transformation we create the PHI replacement instructions first,
407 // we don't have to get them in any particular order.
408 //
409 // However, for other instructions we will have to traverse the
410 // operands of an instruction first, which means that we have to
411 // do a post-order traversal.
412 while (!WorkList.empty()) {
413 SetVector<PHINode *> PHIs;
414
415 while (!WorkList.empty()) {
416 if (Explored.size() >= 100)
417 return false;
418
419 Value *V = WorkList.back();
420
421 if (Explored.contains(key: V)) {
422 WorkList.pop_back();
423 continue;
424 }
425
426 if (!isa<GetElementPtrInst>(Val: V) && !isa<PHINode>(Val: V))
427 // We've found some value that we can't explore which is different from
428 // the base. Therefore we can't do this transformation.
429 return false;
430
431 if (auto *GEP = dyn_cast<GEPOperator>(Val: V)) {
432 // Only allow inbounds GEPs with at most one variable offset.
433 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(Val: V); };
434 if (!GEP->isInBounds() || count_if(Range: GEP->indices(), P: IsNonConst) > 1)
435 return false;
436
437 NW = NW.intersectForOffsetAdd(Other: GEP->getNoWrapFlags());
438 if (!Explored.contains(key: GEP->getOperand(i_nocapture: 0)))
439 WorkList.push_back(Elt: GEP->getOperand(i_nocapture: 0));
440 }
441
442 if (WorkList.back() == V) {
443 WorkList.pop_back();
444 // We've finished visiting this node, mark it as such.
445 Explored.insert(X: V);
446 }
447
448 if (auto *PN = dyn_cast<PHINode>(Val: V)) {
449 // We cannot transform PHIs on unsplittable basic blocks.
450 if (isa<CatchSwitchInst>(Val: PN->getParent()->getTerminator()))
451 return false;
452 Explored.insert(X: PN);
453 PHIs.insert(X: PN);
454 }
455 }
456
457 // Explore the PHI nodes further.
458 for (auto *PN : PHIs)
459 for (Value *Op : PN->incoming_values())
460 if (!Explored.contains(key: Op))
461 WorkList.push_back(Elt: Op);
462 }
463
464 // Make sure that we can do this. Since we can't insert GEPs in a basic
465 // block before a PHI node, we can't easily do this transformation if
466 // we have PHI node users of transformed instructions.
467 for (Value *Val : Explored) {
468 for (Value *Use : Val->uses()) {
469
470 auto *PHI = dyn_cast<PHINode>(Val: Use);
471 auto *Inst = dyn_cast<Instruction>(Val);
472
473 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
474 !Explored.contains(key: PHI))
475 continue;
476
477 if (PHI->getParent() == Inst->getParent())
478 return false;
479 }
480 }
481 return true;
482}
483
484// Sets the appropriate insert point on Builder where we can add
485// a replacement Instruction for V (if that is possible).
486static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
487 bool Before = true) {
488 if (auto *PHI = dyn_cast<PHINode>(Val: V)) {
489 BasicBlock *Parent = PHI->getParent();
490 Builder.SetInsertPoint(TheBB: Parent, IP: Parent->getFirstInsertionPt());
491 return;
492 }
493 if (auto *I = dyn_cast<Instruction>(Val: V)) {
494 if (!Before)
495 I = &*std::next(x: I->getIterator());
496 Builder.SetInsertPoint(I);
497 return;
498 }
499 if (auto *A = dyn_cast<Argument>(Val: V)) {
500 // Set the insertion point in the entry block.
501 BasicBlock &Entry = A->getParent()->getEntryBlock();
502 Builder.SetInsertPoint(TheBB: &Entry, IP: Entry.getFirstInsertionPt());
503 return;
504 }
505 // Otherwise, this is a constant and we don't need to set a new
506 // insertion point.
507 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
508}
509
510/// Returns a re-written value of Start as an indexed GEP using Base as a
511/// pointer.
512static Value *rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW,
513 const DataLayout &DL,
514 SetVector<Value *> &Explored,
515 InstCombiner &IC) {
516 // Perform all the substitutions. This is a bit tricky because we can
517 // have cycles in our use-def chains.
518 // 1. Create the PHI nodes without any incoming values.
519 // 2. Create all the other values.
520 // 3. Add the edges for the PHI nodes.
521 // 4. Emit GEPs to get the original pointers.
522 // 5. Remove the original instructions.
523 Type *IndexType = IntegerType::get(
524 C&: Base->getContext(), NumBits: DL.getIndexTypeSizeInBits(Ty: Start->getType()));
525
526 DenseMap<Value *, Value *> NewInsts;
527 NewInsts[Base] = ConstantInt::getNullValue(Ty: IndexType);
528
529 // Create the new PHI nodes, without adding any incoming values.
530 for (Value *Val : Explored) {
531 if (Val == Base)
532 continue;
533 // Create empty phi nodes. This avoids cyclic dependencies when creating
534 // the remaining instructions.
535 if (auto *PHI = dyn_cast<PHINode>(Val))
536 NewInsts[PHI] =
537 PHINode::Create(Ty: IndexType, NumReservedValues: PHI->getNumIncomingValues(),
538 NameStr: PHI->getName() + ".idx", InsertBefore: PHI->getIterator());
539 }
540 IRBuilder<> Builder(Base->getContext());
541
542 // Create all the other instructions.
543 for (Value *Val : Explored) {
544 if (NewInsts.contains(Val))
545 continue;
546
547 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
548 setInsertionPoint(Builder, V: GEP);
549 Value *Op = NewInsts[GEP->getOperand(i_nocapture: 0)];
550 Value *OffsetV = emitGEPOffset(Builder: &Builder, DL, GEP);
551 if (isa<ConstantInt>(Val: Op) && cast<ConstantInt>(Val: Op)->isZero())
552 NewInsts[GEP] = OffsetV;
553 else
554 NewInsts[GEP] = Builder.CreateAdd(
555 LHS: Op, RHS: OffsetV, Name: GEP->getOperand(i_nocapture: 0)->getName() + ".add",
556 /*NUW=*/HasNUW: NW.hasNoUnsignedWrap(),
557 /*NSW=*/HasNSW: NW.hasNoUnsignedSignedWrap());
558 continue;
559 }
560 if (isa<PHINode>(Val))
561 continue;
562
563 llvm_unreachable("Unexpected instruction type");
564 }
565
566 // Add the incoming values to the PHI nodes.
567 for (Value *Val : Explored) {
568 if (Val == Base)
569 continue;
570 // All the instructions have been created, we can now add edges to the
571 // phi nodes.
572 if (auto *PHI = dyn_cast<PHINode>(Val)) {
573 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
574 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
575 Value *NewIncoming = PHI->getIncomingValue(i: I);
576
577 auto It = NewInsts.find(Val: NewIncoming);
578 if (It != NewInsts.end())
579 NewIncoming = It->second;
580
581 NewPhi->addIncoming(V: NewIncoming, BB: PHI->getIncomingBlock(i: I));
582 }
583 }
584 }
585
586 for (Value *Val : Explored) {
587 if (Val == Base)
588 continue;
589
590 setInsertionPoint(Builder, V: Val, Before: false);
591 // Create GEP for external users.
592 Value *NewVal = Builder.CreateGEP(Ty: Builder.getInt8Ty(), Ptr: Base, IdxList: NewInsts[Val],
593 Name: Val->getName() + ".ptr", NW);
594 IC.replaceInstUsesWith(I&: *cast<Instruction>(Val), V: NewVal);
595 // Add old instruction to worklist for DCE. We don't directly remove it
596 // here because the original compare is one of the users.
597 IC.addToWorklist(I: cast<Instruction>(Val));
598 }
599
600 return NewInsts[Start];
601}
602
603/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
604/// We can look through PHIs, GEPs and casts in order to determine a common base
605/// between GEPLHS and RHS.
606static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
607 CmpPredicate Cond,
608 const DataLayout &DL,
609 InstCombiner &IC) {
610 // FIXME: Support vector of pointers.
611 if (GEPLHS->getType()->isVectorTy())
612 return nullptr;
613
614 if (!GEPLHS->hasAllConstantIndices())
615 return nullptr;
616
617 APInt Offset(DL.getIndexTypeSizeInBits(Ty: GEPLHS->getType()), 0);
618 Value *PtrBase =
619 GEPLHS->stripAndAccumulateConstantOffsets(DL, Offset,
620 /*AllowNonInbounds*/ false);
621
622 // Bail if we looked through addrspacecast.
623 if (PtrBase->getType() != GEPLHS->getType())
624 return nullptr;
625
626 // The set of nodes that will take part in this transformation.
627 SetVector<Value *> Nodes;
628 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
629 if (!canRewriteGEPAsOffset(Start: RHS, Base: PtrBase, NW, DL, Explored&: Nodes))
630 return nullptr;
631
632 // We know we can re-write this as
633 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
634 // Since we've only looked through inbouds GEPs we know that we
635 // can't have overflow on either side. We can therefore re-write
636 // this as:
637 // OFFSET1 cmp OFFSET2
638 Value *NewRHS = rewriteGEPAsOffset(Start: RHS, Base: PtrBase, NW, DL, Explored&: Nodes, IC);
639
640 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
641 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
642 // offset. Since Index is the offset of LHS to the base pointer, we will now
643 // compare the offsets instead of comparing the pointers.
644 return new ICmpInst(ICmpInst::getSignedPredicate(Pred: Cond),
645 IC.Builder.getInt(AI: Offset), NewRHS);
646}
647
648/// Fold comparisons between a GEP instruction and something else. At this point
649/// we know that the GEP is on the LHS of the comparison.
650Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
651 CmpPredicate Cond, Instruction &I) {
652 // Don't transform signed compares of GEPs into index compares. Even if the
653 // GEP is inbounds, the final add of the base pointer can have signed overflow
654 // and would change the result of the icmp.
655 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
656 // the maximum signed value for the pointer type.
657 if (ICmpInst::isSigned(Pred: Cond))
658 return nullptr;
659
660 // Look through bitcasts and addrspacecasts. We do not however want to remove
661 // 0 GEPs.
662 if (!isa<GetElementPtrInst>(Val: RHS))
663 RHS = RHS->stripPointerCasts();
664
665 auto CanFold = [Cond](GEPNoWrapFlags NW) {
666 if (ICmpInst::isEquality(P: Cond))
667 return true;
668
669 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags.
670 assert(ICmpInst::isUnsigned(Cond));
671 return NW != GEPNoWrapFlags::none();
672 };
673
674 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) {
675 if (!NW.hasNoUnsignedWrap()) {
676 // Convert signed to unsigned comparison.
677 return new ICmpInst(ICmpInst::getSignedPredicate(Pred: Cond), Op1, Op2);
678 }
679
680 auto *I = new ICmpInst(Cond, Op1, Op2);
681 I->setSameSign(NW.hasNoUnsignedSignedWrap());
682 return I;
683 };
684
685 CommonPointerBase Base = CommonPointerBase::compute(LHS: GEPLHS, RHS);
686 if (Base.Ptr == RHS && CanFold(Base.LHSNW) && !Base.isExpensive()) {
687 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
688 Type *IdxTy = DL.getIndexType(PtrTy: GEPLHS->getType());
689 Value *Offset =
690 EmitGEPOffsets(GEPs: Base.LHSGEPs, NW: Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);
691 return NewICmp(Base.LHSNW, Offset,
692 Constant::getNullValue(Ty: Offset->getType()));
693 }
694
695 if (GEPLHS->isInBounds() && ICmpInst::isEquality(P: Cond) &&
696 isa<ConstantPointerNull>(Val: RHS) &&
697 !NullPointerIsDefined(F: I.getFunction(),
698 AS: RHS->getType()->getPointerAddressSpace())) {
699 // For most address spaces, an allocation can't be placed at null, but null
700 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
701 // the only valid inbounds address derived from null, is null itself.
702 // Thus, we have four cases to consider:
703 // 1) Base == nullptr, Offset == 0 -> inbounds, null
704 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
705 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
706 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
707 //
708 // (Note if we're indexing a type of size 0, that simply collapses into one
709 // of the buckets above.)
710 //
711 // In general, we're allowed to make values less poison (i.e. remove
712 // sources of full UB), so in this case, we just select between the two
713 // non-poison cases (1 and 4 above).
714 //
715 // For vectors, we apply the same reasoning on a per-lane basis.
716 auto *Base = GEPLHS->getPointerOperand();
717 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
718 auto EC = cast<VectorType>(Val: GEPLHS->getType())->getElementCount();
719 Base = Builder.CreateVectorSplat(EC, V: Base);
720 }
721 return new ICmpInst(Cond, Base,
722 ConstantExpr::getPointerBitCastOrAddrSpaceCast(
723 C: cast<Constant>(Val: RHS), Ty: Base->getType()));
724 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(Val: RHS)) {
725 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
726
727 // If the base pointers are different, but the indices are the same, just
728 // compare the base pointer.
729 if (GEPLHS->getOperand(i_nocapture: 0) != GEPRHS->getOperand(i_nocapture: 0)) {
730 bool IndicesTheSame =
731 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
732 GEPLHS->getPointerOperand()->getType() ==
733 GEPRHS->getPointerOperand()->getType() &&
734 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
735 if (IndicesTheSame)
736 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
737 if (GEPLHS->getOperand(i_nocapture: i) != GEPRHS->getOperand(i_nocapture: i)) {
738 IndicesTheSame = false;
739 break;
740 }
741
742 // If all indices are the same, just compare the base pointers.
743 Type *BaseType = GEPLHS->getOperand(i_nocapture: 0)->getType();
744 if (IndicesTheSame &&
745 CmpInst::makeCmpResultType(opnd_type: BaseType) == I.getType() && CanFold(NW))
746 return new ICmpInst(Cond, GEPLHS->getOperand(i_nocapture: 0), GEPRHS->getOperand(i_nocapture: 0));
747
748 // If we're comparing GEPs with two base pointers that only differ in type
749 // and both GEPs have only constant indices or just one use, then fold
750 // the compare with the adjusted indices.
751 // FIXME: Support vector of pointers.
752 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
753 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
754 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
755 GEPLHS->getOperand(i_nocapture: 0)->stripPointerCasts() ==
756 GEPRHS->getOperand(i_nocapture: 0)->stripPointerCasts() &&
757 !GEPLHS->getType()->isVectorTy()) {
758 Value *LOffset = EmitGEPOffset(GEP: GEPLHS);
759 Value *ROffset = EmitGEPOffset(GEP: GEPRHS);
760
761 // If we looked through an addrspacecast between different sized address
762 // spaces, the LHS and RHS pointers are different sized
763 // integers. Truncate to the smaller one.
764 Type *LHSIndexTy = LOffset->getType();
765 Type *RHSIndexTy = ROffset->getType();
766 if (LHSIndexTy != RHSIndexTy) {
767 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <
768 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {
769 ROffset = Builder.CreateTrunc(V: ROffset, DestTy: LHSIndexTy);
770 } else
771 LOffset = Builder.CreateTrunc(V: LOffset, DestTy: RHSIndexTy);
772 }
773
774 Value *Cmp = Builder.CreateICmp(P: ICmpInst::getSignedPredicate(Pred: Cond),
775 LHS: LOffset, RHS: ROffset);
776 return replaceInstUsesWith(I, V: Cmp);
777 }
778 }
779
780 if (GEPLHS->getOperand(i_nocapture: 0) == GEPRHS->getOperand(i_nocapture: 0) &&
781 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
782 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
783 // If the GEPs only differ by one index, compare it.
784 unsigned NumDifferences = 0; // Keep track of # differences.
785 unsigned DiffOperand = 0; // The operand that differs.
786 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
787 if (GEPLHS->getOperand(i_nocapture: i) != GEPRHS->getOperand(i_nocapture: i)) {
788 Type *LHSType = GEPLHS->getOperand(i_nocapture: i)->getType();
789 Type *RHSType = GEPRHS->getOperand(i_nocapture: i)->getType();
790 // FIXME: Better support for vector of pointers.
791 if (LHSType->getPrimitiveSizeInBits() !=
792 RHSType->getPrimitiveSizeInBits() ||
793 (GEPLHS->getType()->isVectorTy() &&
794 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
795 // Irreconcilable differences.
796 NumDifferences = 2;
797 break;
798 }
799
800 if (NumDifferences++)
801 break;
802 DiffOperand = i;
803 }
804
805 if (NumDifferences == 0) // SAME GEP?
806 return replaceInstUsesWith(
807 I, // No comparison is needed here.
808 V: ConstantInt::get(Ty: I.getType(), V: ICmpInst::isTrueWhenEqual(predicate: Cond)));
809 // If two GEPs only differ by an index, compare them.
810 // Note that nowrap flags are always needed when comparing two indices.
811 else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {
812 Value *LHSV = GEPLHS->getOperand(i_nocapture: DiffOperand);
813 Value *RHSV = GEPRHS->getOperand(i_nocapture: DiffOperand);
814 return NewICmp(NW, LHSV, RHSV);
815 }
816 }
817
818 if (Base.Ptr && CanFold(Base.LHSNW & Base.RHSNW) && !Base.isExpensive()) {
819 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
820 Type *IdxTy = DL.getIndexType(PtrTy: GEPLHS->getType());
821 Value *L =
822 EmitGEPOffsets(GEPs: Base.LHSGEPs, NW: Base.LHSNW, IdxTy, /*RewriteGEP=*/RewriteGEPs: true);
823 Value *R =
824 EmitGEPOffsets(GEPs: Base.RHSGEPs, NW: Base.RHSNW, IdxTy, /*RewriteGEP=*/RewriteGEPs: true);
825 return NewICmp(Base.LHSNW & Base.RHSNW, L, R);
826 }
827 }
828
829 // Try convert this to an indexed compare by looking through PHIs/casts as a
830 // last resort.
831 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, IC&: *this);
832}
833
834bool InstCombinerImpl::foldAllocaCmp(AllocaInst *Alloca) {
835 // It would be tempting to fold away comparisons between allocas and any
836 // pointer not based on that alloca (e.g. an argument). However, even
837 // though such pointers cannot alias, they can still compare equal.
838 //
839 // But LLVM doesn't specify where allocas get their memory, so if the alloca
840 // doesn't escape we can argue that it's impossible to guess its value, and we
841 // can therefore act as if any such guesses are wrong.
842 //
843 // However, we need to ensure that this folding is consistent: We can't fold
844 // one comparison to false, and then leave a different comparison against the
845 // same value alone (as it might evaluate to true at runtime, leading to a
846 // contradiction). As such, this code ensures that all comparisons are folded
847 // at the same time, and there are no other escapes.
848
849 struct CmpCaptureTracker : public CaptureTracker {
850 AllocaInst *Alloca;
851 bool Captured = false;
852 /// The value of the map is a bit mask of which icmp operands the alloca is
853 /// used in.
854 SmallMapVector<ICmpInst *, unsigned, 4> ICmps;
855
856 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
857
858 void tooManyUses() override { Captured = true; }
859
860 Action captured(const Use *U, UseCaptureInfo CI) override {
861 // TODO(captures): Use UseCaptureInfo.
862 auto *ICmp = dyn_cast<ICmpInst>(Val: U->getUser());
863 // We need to check that U is based *only* on the alloca, and doesn't
864 // have other contributions from a select/phi operand.
865 // TODO: We could check whether getUnderlyingObjects() reduces to one
866 // object, which would allow looking through phi nodes.
867 if (ICmp && ICmp->isEquality() && getUnderlyingObject(V: *U) == Alloca) {
868 // Collect equality icmps of the alloca, and don't treat them as
869 // captures.
870 ICmps[ICmp] |= 1u << U->getOperandNo();
871 return Continue;
872 }
873
874 Captured = true;
875 return Stop;
876 }
877 };
878
879 CmpCaptureTracker Tracker(Alloca);
880 PointerMayBeCaptured(V: Alloca, Tracker: &Tracker);
881 if (Tracker.Captured)
882 return false;
883
884 bool Changed = false;
885 for (auto [ICmp, Operands] : Tracker.ICmps) {
886 switch (Operands) {
887 case 1:
888 case 2: {
889 // The alloca is only used in one icmp operand. Assume that the
890 // equality is false.
891 auto *Res = ConstantInt::get(Ty: ICmp->getType(),
892 V: ICmp->getPredicate() == ICmpInst::ICMP_NE);
893 replaceInstUsesWith(I&: *ICmp, V: Res);
894 eraseInstFromFunction(I&: *ICmp);
895 Changed = true;
896 break;
897 }
898 case 3:
899 // Both icmp operands are based on the alloca, so this is comparing
900 // pointer offsets, without leaking any information about the address
901 // of the alloca. Ignore such comparisons.
902 break;
903 default:
904 llvm_unreachable("Cannot happen");
905 }
906 }
907
908 return Changed;
909}
910
911/// Fold "icmp pred (X+C), X".
912Instruction *InstCombinerImpl::foldICmpAddOpConst(Value *X, const APInt &C,
913 CmpPredicate Pred) {
914 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
915 // so the values can never be equal. Similarly for all other "or equals"
916 // operators.
917 assert(!!C && "C should not be zero!");
918
919 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
920 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
921 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
922 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
923 Constant *R =
924 ConstantInt::get(Ty: X->getType(), V: APInt::getMaxValue(numBits: C.getBitWidth()) - C);
925 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
926 }
927
928 // (X+1) >u X --> X <u (0-1) --> X != 255
929 // (X+2) >u X --> X <u (0-2) --> X <u 254
930 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
931 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
932 return new ICmpInst(ICmpInst::ICMP_ULT, X,
933 ConstantInt::get(Ty: X->getType(), V: -C));
934
935 APInt SMax = APInt::getSignedMaxValue(numBits: C.getBitWidth());
936
937 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
938 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
939 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
940 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
941 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
942 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
943 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
944 return new ICmpInst(ICmpInst::ICMP_SGT, X,
945 ConstantInt::get(Ty: X->getType(), V: SMax - C));
946
947 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
948 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
949 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
950 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
951 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
952 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
953
954 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
955 return new ICmpInst(ICmpInst::ICMP_SLT, X,
956 ConstantInt::get(Ty: X->getType(), V: SMax - (C - 1)));
957}
958
959/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
960/// (icmp eq/ne A, Log2(AP2/AP1)) ->
961/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
962Instruction *InstCombinerImpl::foldICmpShrConstConst(ICmpInst &I, Value *A,
963 const APInt &AP1,
964 const APInt &AP2) {
965 assert(I.isEquality() && "Cannot fold icmp gt/lt");
966
967 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
968 if (I.getPredicate() == I.ICMP_NE)
969 Pred = CmpInst::getInversePredicate(pred: Pred);
970 return new ICmpInst(Pred, LHS, RHS);
971 };
972
973 // Don't bother doing any work for cases which InstSimplify handles.
974 if (AP2.isZero())
975 return nullptr;
976
977 bool IsAShr = isa<AShrOperator>(Val: I.getOperand(i_nocapture: 0));
978 if (IsAShr) {
979 if (AP2.isAllOnes())
980 return nullptr;
981 if (AP2.isNegative() != AP1.isNegative())
982 return nullptr;
983 if (AP2.sgt(RHS: AP1))
984 return nullptr;
985 }
986
987 if (!AP1)
988 // 'A' must be large enough to shift out the highest set bit.
989 return getICmp(I.ICMP_UGT, A,
990 ConstantInt::get(Ty: A->getType(), V: AP2.logBase2()));
991
992 if (AP1 == AP2)
993 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(Ty: A->getType()));
994
995 int Shift;
996 if (IsAShr && AP1.isNegative())
997 Shift = AP1.countl_one() - AP2.countl_one();
998 else
999 Shift = AP1.countl_zero() - AP2.countl_zero();
1000
1001 if (Shift > 0) {
1002 if (IsAShr && AP1 == AP2.ashr(ShiftAmt: Shift)) {
1003 // There are multiple solutions if we are comparing against -1 and the LHS
1004 // of the ashr is not a power of two.
1005 if (AP1.isAllOnes() && !AP2.isPowerOf2())
1006 return getICmp(I.ICMP_UGE, A, ConstantInt::get(Ty: A->getType(), V: Shift));
1007 return getICmp(I.ICMP_EQ, A, ConstantInt::get(Ty: A->getType(), V: Shift));
1008 } else if (AP1 == AP2.lshr(shiftAmt: Shift)) {
1009 return getICmp(I.ICMP_EQ, A, ConstantInt::get(Ty: A->getType(), V: Shift));
1010 }
1011 }
1012
1013 // Shifting const2 will never be equal to const1.
1014 // FIXME: This should always be handled by InstSimplify?
1015 auto *TorF = ConstantInt::get(Ty: I.getType(), V: I.getPredicate() == I.ICMP_NE);
1016 return replaceInstUsesWith(I, V: TorF);
1017}
1018
1019/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1020/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1021Instruction *InstCombinerImpl::foldICmpShlConstConst(ICmpInst &I, Value *A,
1022 const APInt &AP1,
1023 const APInt &AP2) {
1024 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1025
1026 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1027 if (I.getPredicate() == I.ICMP_NE)
1028 Pred = CmpInst::getInversePredicate(pred: Pred);
1029 return new ICmpInst(Pred, LHS, RHS);
1030 };
1031
1032 // Don't bother doing any work for cases which InstSimplify handles.
1033 if (AP2.isZero())
1034 return nullptr;
1035
1036 unsigned AP2TrailingZeros = AP2.countr_zero();
1037
1038 if (!AP1 && AP2TrailingZeros != 0)
1039 return getICmp(
1040 I.ICMP_UGE, A,
1041 ConstantInt::get(Ty: A->getType(), V: AP2.getBitWidth() - AP2TrailingZeros));
1042
1043 if (AP1 == AP2)
1044 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(Ty: A->getType()));
1045
1046 // Get the distance between the lowest bits that are set.
1047 int Shift = AP1.countr_zero() - AP2TrailingZeros;
1048
1049 if (Shift > 0 && AP2.shl(shiftAmt: Shift) == AP1)
1050 return getICmp(I.ICMP_EQ, A, ConstantInt::get(Ty: A->getType(), V: Shift));
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/// The caller has matched a pattern of the form:
1059/// I = icmp ugt (add (add A, B), CI2), CI1
1060/// If this is of the form:
1061/// sum = a + b
1062/// if (sum+128 >u 255)
1063/// Then replace it with llvm.sadd.with.overflow.i8.
1064///
1065static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
1066 ConstantInt *CI2, ConstantInt *CI1,
1067 InstCombinerImpl &IC) {
1068 // The transformation we're trying to do here is to transform this into an
1069 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1070 // with a narrower add, and discard the add-with-constant that is part of the
1071 // range check (if we can't eliminate it, this isn't profitable).
1072
1073 // In order to eliminate the add-with-constant, the compare can be its only
1074 // use.
1075 Instruction *AddWithCst = cast<Instruction>(Val: I.getOperand(i_nocapture: 0));
1076 if (!AddWithCst->hasOneUse())
1077 return nullptr;
1078
1079 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1080 if (!CI2->getValue().isPowerOf2())
1081 return nullptr;
1082 unsigned NewWidth = CI2->getValue().countr_zero();
1083 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1084 return nullptr;
1085
1086 // The width of the new add formed is 1 more than the bias.
1087 ++NewWidth;
1088
1089 // Check to see that CI1 is an all-ones value with NewWidth bits.
1090 if (CI1->getBitWidth() == NewWidth ||
1091 CI1->getValue() != APInt::getLowBitsSet(numBits: CI1->getBitWidth(), loBitsSet: NewWidth))
1092 return nullptr;
1093
1094 // This is only really a signed overflow check if the inputs have been
1095 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1096 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1097 if (IC.ComputeMaxSignificantBits(Op: A, CxtI: &I) > NewWidth ||
1098 IC.ComputeMaxSignificantBits(Op: B, CxtI: &I) > NewWidth)
1099 return nullptr;
1100
1101 // In order to replace the original add with a narrower
1102 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1103 // and truncates that discard the high bits of the add. Verify that this is
1104 // the case.
1105 Instruction *OrigAdd = cast<Instruction>(Val: AddWithCst->getOperand(i: 0));
1106 for (User *U : OrigAdd->users()) {
1107 if (U == AddWithCst)
1108 continue;
1109
1110 // Only accept truncates for now. We would really like a nice recursive
1111 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1112 // chain to see which bits of a value are actually demanded. If the
1113 // original add had another add which was then immediately truncated, we
1114 // could still do the transformation.
1115 TruncInst *TI = dyn_cast<TruncInst>(Val: U);
1116 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1117 return nullptr;
1118 }
1119
1120 // If the pattern matches, truncate the inputs to the narrower type and
1121 // use the sadd_with_overflow intrinsic to efficiently compute both the
1122 // result and the overflow bit.
1123 Type *NewType = IntegerType::get(C&: OrigAdd->getContext(), NumBits: NewWidth);
1124 Function *F = Intrinsic::getOrInsertDeclaration(
1125 M: I.getModule(), id: Intrinsic::sadd_with_overflow, OverloadTys: NewType);
1126
1127 InstCombiner::BuilderTy &Builder = IC.Builder;
1128
1129 // Put the new code above the original add, in case there are any uses of the
1130 // add between the add and the compare.
1131 Builder.SetInsertPoint(OrigAdd);
1132
1133 Value *TruncA = Builder.CreateTrunc(V: A, DestTy: NewType, Name: A->getName() + ".trunc");
1134 Value *TruncB = Builder.CreateTrunc(V: B, DestTy: NewType, Name: B->getName() + ".trunc");
1135 CallInst *Call = Builder.CreateCall(Callee: F, Args: {TruncA, TruncB}, Name: "sadd");
1136 Value *Add = Builder.CreateExtractValue(Agg: Call, Idxs: 0, Name: "sadd.result");
1137 Value *ZExt = Builder.CreateZExt(V: Add, DestTy: OrigAdd->getType());
1138
1139 // The inner add was the result of the narrow add, zero extended to the
1140 // wider type. Replace it with the result computed by the intrinsic.
1141 IC.replaceInstUsesWith(I&: *OrigAdd, V: ZExt);
1142 IC.eraseInstFromFunction(I&: *OrigAdd);
1143
1144 // The original icmp gets replaced with the overflow value.
1145 return ExtractValueInst::Create(Agg: Call, Idxs: 1, NameStr: "sadd.overflow");
1146}
1147
1148/// If we have:
1149/// icmp eq/ne (urem/srem %x, %y), 0
1150/// iff %y is a power-of-two, we can replace this with a bit test:
1151/// icmp eq/ne (and %x, (add %y, -1)), 0
1152Instruction *InstCombinerImpl::foldIRemByPowerOfTwoToBitTest(ICmpInst &I) {
1153 // This fold is only valid for equality predicates.
1154 if (!I.isEquality())
1155 return nullptr;
1156 CmpPredicate Pred;
1157 Value *X, *Y, *Zero;
1158 if (!match(V: &I, P: m_ICmp(Pred, L: m_OneUse(SubPattern: m_IRem(L: m_Value(V&: X), R: m_Value(V&: Y))),
1159 R: m_CombineAnd(Ps: m_Zero(), Ps: m_Value(V&: Zero)))))
1160 return nullptr;
1161 if (!isKnownToBeAPowerOfTwo(V: Y, /*OrZero*/ true, CxtI: &I))
1162 return nullptr;
1163 // This may increase instruction count, we don't enforce that Y is a constant.
1164 Value *Mask = Builder.CreateAdd(LHS: Y, RHS: Constant::getAllOnesValue(Ty: Y->getType()));
1165 Value *Masked = Builder.CreateAnd(LHS: X, RHS: Mask);
1166 return ICmpInst::Create(Op: Instruction::ICmp, Pred, S1: Masked, S2: Zero);
1167}
1168
1169/// Fold equality-comparison between zero and any (maybe truncated) right-shift
1170/// by one-less-than-bitwidth into a sign test on the original value.
1171Instruction *InstCombinerImpl::foldSignBitTest(ICmpInst &I) {
1172 Instruction *Val;
1173 CmpPredicate Pred;
1174 if (!I.isEquality() || !match(V: &I, P: m_ICmp(Pred, L: m_Instruction(I&: Val), R: m_Zero())))
1175 return nullptr;
1176
1177 Value *X;
1178 Type *XTy;
1179
1180 Constant *C;
1181 if (match(V: Val, P: m_TruncOrSelf(Op: m_Shr(L: m_Value(V&: X), R: m_Constant(C))))) {
1182 XTy = X->getType();
1183 unsigned XBitWidth = XTy->getScalarSizeInBits();
1184 if (!match(V: C, P: m_SpecificInt_ICMP(Predicate: ICmpInst::Predicate::ICMP_EQ,
1185 Threshold: APInt(XBitWidth, XBitWidth - 1))))
1186 return nullptr;
1187 } else if (isa<BinaryOperator>(Val) &&
1188 (X = reassociateShiftAmtsOfTwoSameDirectionShifts(
1189 Sh0: cast<BinaryOperator>(Val), SQ: SQ.getWithInstruction(I: Val),
1190 /*AnalyzeForSignBitExtraction=*/true))) {
1191 XTy = X->getType();
1192 } else
1193 return nullptr;
1194
1195 return ICmpInst::Create(Op: Instruction::ICmp,
1196 Pred: Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SGE
1197 : ICmpInst::ICMP_SLT,
1198 S1: X, S2: ConstantInt::getNullValue(Ty: XTy));
1199}
1200
1201// Handle icmp pred X, 0
1202Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {
1203 CmpInst::Predicate Pred = Cmp.getPredicate();
1204 if (!match(V: Cmp.getOperand(i_nocapture: 1), P: m_Zero()))
1205 return nullptr;
1206
1207 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1208 if (Pred == ICmpInst::ICMP_SGT) {
1209 Value *A, *B;
1210 if (match(V: Cmp.getOperand(i_nocapture: 0), P: m_SMin(Op0: m_Value(V&: A), Op1: m_Value(V&: B)))) {
1211 if (isKnownPositive(V: A, SQ: SQ.getWithInstruction(I: &Cmp)))
1212 return new ICmpInst(Pred, B, Cmp.getOperand(i_nocapture: 1));
1213 if (isKnownPositive(V: B, SQ: SQ.getWithInstruction(I: &Cmp)))
1214 return new ICmpInst(Pred, A, Cmp.getOperand(i_nocapture: 1));
1215 }
1216 }
1217
1218 if (Instruction *New = foldIRemByPowerOfTwoToBitTest(I&: Cmp))
1219 return New;
1220
1221 // Given:
1222 // icmp eq/ne (urem %x, %y), 0
1223 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
1224 // icmp eq/ne %x, 0
1225 Value *X, *Y;
1226 if (match(V: Cmp.getOperand(i_nocapture: 0), P: m_URem(L: m_Value(V&: X), R: m_Value(V&: Y))) &&
1227 ICmpInst::isEquality(P: Pred)) {
1228 KnownBits XKnown = computeKnownBits(V: X, CxtI: &Cmp);
1229 KnownBits YKnown = computeKnownBits(V: Y, CxtI: &Cmp);
1230 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1231 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
1232 }
1233
1234 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are
1235 // odd/non-zero/there is no overflow.
1236 if (match(V: Cmp.getOperand(i_nocapture: 0), P: m_Mul(L: m_Value(V&: X), R: m_Value(V&: Y))) &&
1237 ICmpInst::isEquality(P: Pred)) {
1238
1239 KnownBits XKnown = computeKnownBits(V: X, CxtI: &Cmp);
1240 // if X % 2 != 0
1241 // (icmp eq/ne Y)
1242 if (XKnown.countMaxTrailingZeros() == 0)
1243 return new ICmpInst(Pred, Y, Cmp.getOperand(i_nocapture: 1));
1244
1245 KnownBits YKnown = computeKnownBits(V: Y, CxtI: &Cmp);
1246 // if Y % 2 != 0
1247 // (icmp eq/ne X)
1248 if (YKnown.countMaxTrailingZeros() == 0)
1249 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
1250
1251 auto *BO0 = cast<OverflowingBinaryOperator>(Val: Cmp.getOperand(i_nocapture: 0));
1252 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1253 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
1254 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`
1255 // but to avoid unnecessary work, first just if this is an obvious case.
1256
1257 // if X non-zero and NoOverflow(X * Y)
1258 // (icmp eq/ne Y)
1259 if (!XKnown.One.isZero() || isKnownNonZero(V: X, Q))
1260 return new ICmpInst(Pred, Y, Cmp.getOperand(i_nocapture: 1));
1261
1262 // if Y non-zero and NoOverflow(X * Y)
1263 // (icmp eq/ne X)
1264 if (!YKnown.One.isZero() || isKnownNonZero(V: Y, Q))
1265 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
1266 }
1267 // Note, we are skipping cases:
1268 // if Y % 2 != 0 AND X % 2 != 0
1269 // (false/true)
1270 // if X non-zero and Y non-zero and NoOverflow(X * Y)
1271 // (false/true)
1272 // Those can be simplified later as we would have already replaced the (icmp
1273 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
1274 // will fold to a constant elsewhere.
1275 }
1276
1277 // (icmp eq/ne f(X), 0) -> (icmp eq/ne X, 0)
1278 // where f(X) == 0 if and only if X == 0
1279 if (ICmpInst::isEquality(P: Pred))
1280 if (Value *Stripped = stripNullTest(V: Cmp.getOperand(i_nocapture: 0)))
1281 return new ICmpInst(Pred, Stripped,
1282 Constant::getNullValue(Ty: Stripped->getType()));
1283
1284 return nullptr;
1285}
1286
1287/// Fold icmp eq (num + mask) & ~mask, num
1288/// to
1289/// icmp eq (and num, mask), 0
1290/// Where mask is a low bit mask.
1291Instruction *InstCombinerImpl::foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp) {
1292 Value *Num;
1293 CmpPredicate Pred;
1294 const APInt *Mask, *Neg;
1295
1296 if (!match(V: &Cmp,
1297 P: m_c_ICmp(Pred, L: m_Value(V&: Num),
1298 R: m_OneUse(SubPattern: m_c_And(L: m_OneUse(SubPattern: m_c_Add(L: m_Deferred(V: Num),
1299 R: m_LowBitMask(V&: Mask))),
1300 R: m_APInt(Res&: Neg))))))
1301 return nullptr;
1302
1303 if (*Neg != ~*Mask)
1304 return nullptr;
1305
1306 if (!ICmpInst::isEquality(P: Pred))
1307 return nullptr;
1308
1309 // Create new icmp eq (num & mask), 0
1310 auto *NewAnd = Builder.CreateAnd(LHS: Num, RHS: *Mask);
1311 auto *Zero = Constant::getNullValue(Ty: Num->getType());
1312
1313 return new ICmpInst(Pred, NewAnd, Zero);
1314}
1315
1316/// Fold icmp Pred X, C.
1317/// TODO: This code structure does not make sense. The saturating add fold
1318/// should be moved to some other helper and extended as noted below (it is also
1319/// possible that code has been made unnecessary - do we canonicalize IR to
1320/// overflow/saturating intrinsics or not?).
1321Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) {
1322 // Match the following pattern, which is a common idiom when writing
1323 // overflow-safe integer arithmetic functions. The source performs an addition
1324 // in wider type and explicitly checks for overflow using comparisons against
1325 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1326 //
1327 // TODO: This could probably be generalized to handle other overflow-safe
1328 // operations if we worked out the formulas to compute the appropriate magic
1329 // constants.
1330 //
1331 // sum = a + b
1332 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1333 CmpInst::Predicate Pred = Cmp.getPredicate();
1334 Value *Op0 = Cmp.getOperand(i_nocapture: 0), *Op1 = Cmp.getOperand(i_nocapture: 1);
1335 Value *A, *B;
1336 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1337 if (Pred == ICmpInst::ICMP_UGT && match(V: Op1, P: m_ConstantInt(CI)) &&
1338 match(V: Op0, P: m_Add(L: m_Add(L: m_Value(V&: A), R: m_Value(V&: B)), R: m_ConstantInt(CI&: CI2))))
1339 if (Instruction *Res = processUGT_ADDCST_ADD(I&: Cmp, A, B, CI2, CI1: CI, IC&: *this))
1340 return Res;
1341
1342 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1343 Constant *C = dyn_cast<Constant>(Val: Op1);
1344 if (!C)
1345 return nullptr;
1346
1347 if (auto *Phi = dyn_cast<PHINode>(Val: Op0))
1348 if (all_of(Range: Phi->operands(), P: IsaPred<Constant>)) {
1349 SmallVector<Constant *> Ops;
1350 for (Value *V : Phi->incoming_values()) {
1351 Constant *Res =
1352 ConstantFoldCompareInstOperands(Predicate: Pred, LHS: cast<Constant>(Val: V), RHS: C, DL);
1353 if (!Res)
1354 return nullptr;
1355 Ops.push_back(Elt: Res);
1356 }
1357 Builder.SetInsertPoint(Phi);
1358 PHINode *NewPhi = Builder.CreatePHI(Ty: Cmp.getType(), NumReservedValues: Phi->getNumOperands());
1359 for (auto [V, Pred] : zip(t&: Ops, u: Phi->blocks()))
1360 NewPhi->addIncoming(V, BB: Pred);
1361 return replaceInstUsesWith(I&: Cmp, V: NewPhi);
1362 }
1363
1364 if (Instruction *R = tryFoldInstWithCtpopWithNot(I: &Cmp))
1365 return R;
1366
1367 return nullptr;
1368}
1369
1370/// Canonicalize icmp instructions based on dominating conditions.
1371Instruction *InstCombinerImpl::foldICmpWithDominatingICmp(ICmpInst &Cmp) {
1372 // We already checked simple implication in InstSimplify, only handle complex
1373 // cases here.
1374 Value *X = Cmp.getOperand(i_nocapture: 0), *Y = Cmp.getOperand(i_nocapture: 1);
1375 const APInt *C;
1376 if (!match(V: Y, P: m_APInt(Res&: C)))
1377 return nullptr;
1378
1379 CmpInst::Predicate Pred = Cmp.getPredicate();
1380 ConstantRange CR = ConstantRange::makeExactICmpRegion(Pred, Other: *C);
1381
1382 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1383 const APInt *DomC) -> Instruction * {
1384 // We have 2 compares of a variable with constants. Calculate the constant
1385 // ranges of those compares to see if we can transform the 2nd compare:
1386 // DomBB:
1387 // DomCond = icmp DomPred X, DomC
1388 // br DomCond, CmpBB, FalseBB
1389 // CmpBB:
1390 // Cmp = icmp Pred X, C
1391 ConstantRange DominatingCR =
1392 ConstantRange::makeExactICmpRegion(Pred: DomPred, Other: *DomC);
1393 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1394 ConstantRange Difference = DominatingCR.difference(CR);
1395 if (Intersection.isEmptySet())
1396 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
1397 if (Difference.isEmptySet())
1398 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
1399
1400 // Canonicalizing a sign bit comparison that gets used in a branch,
1401 // pessimizes codegen by generating branch on zero instruction instead
1402 // of a test and branch. So we avoid canonicalizing in such situations
1403 // because test and branch instruction has better branch displacement
1404 // than compare and branch instruction.
1405 bool UnusedBit;
1406 bool IsSignBit = isSignBitCheck(Pred, RHS: *C, TrueIfSigned&: UnusedBit);
1407 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(I&: Cmp)))
1408 return nullptr;
1409
1410 // Avoid an infinite loop with min/max canonicalization.
1411 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1412 if (Cmp.hasOneUse() &&
1413 match(V: Cmp.user_back(), P: m_MaxOrMin(Op0: m_Value(), Op1: m_Value())))
1414 return nullptr;
1415
1416 if (const APInt *EqC = Intersection.getSingleElement())
1417 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(AI: *EqC));
1418 if (const APInt *NeC = Difference.getSingleElement())
1419 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(AI: *NeC));
1420 return nullptr;
1421 };
1422
1423 for (CondBrInst *BI : DC.conditionsFor(V: X)) {
1424 CmpPredicate DomPred;
1425 const APInt *DomC;
1426 if (!match(V: BI->getCondition(),
1427 P: m_ICmp(Pred&: DomPred, L: m_Specific(V: X), R: m_APInt(Res&: DomC))))
1428 continue;
1429
1430 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(i: 0));
1431 if (DT.dominates(BBE: Edge0, BB: Cmp.getParent())) {
1432 if (auto *V = handleDomCond(DomPred, DomC))
1433 return V;
1434 } else {
1435 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(i: 1));
1436 if (DT.dominates(BBE: Edge1, BB: Cmp.getParent()))
1437 if (auto *V =
1438 handleDomCond(CmpInst::getInversePredicate(pred: DomPred), DomC))
1439 return V;
1440 }
1441 }
1442
1443 return nullptr;
1444}
1445
1446/// Fold icmp (trunc X), C.
1447Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
1448 TruncInst *Trunc,
1449 const APInt &C) {
1450 ICmpInst::Predicate Pred = Cmp.getPredicate();
1451 Value *X = Trunc->getOperand(i_nocapture: 0);
1452 Type *SrcTy = X->getType();
1453 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1454 SrcBits = SrcTy->getScalarSizeInBits();
1455
1456 // Match (icmp pred (trunc nuw/nsw X), C)
1457 // Which we can convert to (icmp pred X, (sext/zext C))
1458 if (shouldChangeType(From: Trunc->getType(), To: SrcTy)) {
1459 if (Trunc->hasNoSignedWrap())
1460 return new ICmpInst(Pred, X, ConstantInt::get(Ty: SrcTy, V: C.sext(width: SrcBits)));
1461 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1462 return new ICmpInst(Pred, X, ConstantInt::get(Ty: SrcTy, V: C.zext(width: SrcBits)));
1463 }
1464
1465 if (C.isOne() && C.getBitWidth() > 1) {
1466 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1467 Value *V = nullptr;
1468 if (Pred == ICmpInst::ICMP_SLT && match(V: X, P: m_Signum(V: m_Value(V))))
1469 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1470 ConstantInt::get(Ty: V->getType(), V: 1));
1471 }
1472
1473 // TODO: Handle non-equality predicates.
1474 Value *Y;
1475 const APInt *Pow2;
1476 if (Cmp.isEquality() && match(V: X, P: m_Shl(L: m_Power2(V&: Pow2), R: m_Value(V&: Y))) &&
1477 DstBits > Pow2->logBase2()) {
1478 // (trunc (Pow2 << Y) to iN) == 0 --> Y u>= N - log2(Pow2)
1479 // (trunc (Pow2 << Y) to iN) != 0 --> Y u< N - log2(Pow2)
1480 // iff N > log2(Pow2)
1481 if (C.isZero()) {
1482 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1483 return new ICmpInst(NewPred, Y,
1484 ConstantInt::get(Ty: SrcTy, V: DstBits - Pow2->logBase2()));
1485 }
1486 // (trunc (Pow2 << Y) to iN) == 2**C --> Y == C - log2(Pow2)
1487 // (trunc (Pow2 << Y) to iN) != 2**C --> Y != C - log2(Pow2)
1488 if (C.isPowerOf2())
1489 return new ICmpInst(
1490 Pred, Y, ConstantInt::get(Ty: SrcTy, V: C.logBase2() - Pow2->logBase2()));
1491 }
1492
1493 if (Cmp.isEquality() && (Trunc->hasOneUse() || Trunc->hasNoUnsignedWrap())) {
1494 // Canonicalize to a mask and wider compare if the wide type is suitable:
1495 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1496 if (!SrcTy->isVectorTy() && shouldChangeType(FromBitWidth: DstBits, ToBitWidth: SrcBits)) {
1497 Constant *Mask =
1498 ConstantInt::get(Ty: SrcTy, V: APInt::getLowBitsSet(numBits: SrcBits, loBitsSet: DstBits));
1499 Value *And = Trunc->hasNoUnsignedWrap() ? X : Builder.CreateAnd(LHS: X, RHS: Mask);
1500 Constant *WideC = ConstantInt::get(Ty: SrcTy, V: C.zext(width: SrcBits));
1501 return new ICmpInst(Pred, And, WideC);
1502 }
1503
1504 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1505 // of the high bits truncated out of x are known.
1506 KnownBits Known = computeKnownBits(V: X, CxtI: &Cmp);
1507
1508 // If all the high bits are known, we can do this xform.
1509 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1510 // Pull in the high bits from known-ones set.
1511 APInt NewRHS = C.zext(width: SrcBits);
1512 NewRHS |= Known.One & APInt::getHighBitsSet(numBits: SrcBits, hiBitsSet: SrcBits - DstBits);
1513 return new ICmpInst(Pred, X, ConstantInt::get(Ty: SrcTy, V: NewRHS));
1514 }
1515 }
1516
1517 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1518 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1519 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1520 Value *ShOp;
1521 uint64_t ShAmt;
1522 bool TrueIfSigned;
1523 if (isSignBitCheck(Pred, RHS: C, TrueIfSigned) &&
1524 match(V: X, P: m_Shr(L: m_Value(V&: ShOp), R: m_ConstantInt(V&: ShAmt))) &&
1525 DstBits == SrcBits - ShAmt) {
1526 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1527 ConstantInt::getNullValue(Ty: SrcTy))
1528 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1529 ConstantInt::getAllOnesValue(Ty: SrcTy));
1530 }
1531
1532 return nullptr;
1533}
1534
1535/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1536/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1537Instruction *
1538InstCombinerImpl::foldICmpTruncWithTruncOrExt(ICmpInst &Cmp,
1539 const SimplifyQuery &Q) {
1540 Value *X, *Y;
1541 CmpPredicate Pred;
1542 bool YIsSExt = false;
1543 // Try to match icmp (trunc X), (trunc Y)
1544 if (match(V: &Cmp, P: m_ICmp(Pred, L: m_Trunc(Op: m_Value(V&: X)), R: m_Trunc(Op: m_Value(V&: Y))))) {
1545 unsigned NoWrapFlags = cast<TruncInst>(Val: Cmp.getOperand(i_nocapture: 0))->getNoWrapKind() &
1546 cast<TruncInst>(Val: Cmp.getOperand(i_nocapture: 1))->getNoWrapKind();
1547 if (Cmp.isSigned()) {
1548 // For signed comparisons, both truncs must be nsw.
1549 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1550 return nullptr;
1551 } else {
1552 // For unsigned and equality comparisons, either both must be nuw or
1553 // both must be nsw, we don't care which.
1554 if (!NoWrapFlags)
1555 return nullptr;
1556 }
1557
1558 if (X->getType() != Y->getType() &&
1559 (!Cmp.getOperand(i_nocapture: 0)->hasOneUse() || !Cmp.getOperand(i_nocapture: 1)->hasOneUse()))
1560 return nullptr;
1561 if (!isDesirableIntType(BitWidth: X->getType()->getScalarSizeInBits()) &&
1562 isDesirableIntType(BitWidth: Y->getType()->getScalarSizeInBits())) {
1563 std::swap(a&: X, b&: Y);
1564 Pred = Cmp.getSwappedPredicate(pred: Pred);
1565 }
1566 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1567 }
1568 // Try to match icmp (trunc nuw X), (zext Y)
1569 else if (!Cmp.isSigned() &&
1570 match(V: &Cmp, P: m_c_ICmp(Pred, L: m_NUWTrunc(Op: m_Value(V&: X)),
1571 R: m_OneUse(SubPattern: m_ZExt(Op: m_Value(V&: Y)))))) {
1572 // Can fold trunc nuw + zext for unsigned and equality predicates.
1573 }
1574 // Try to match icmp (trunc nsw X), (sext Y)
1575 else if (match(V: &Cmp, P: m_c_ICmp(Pred, L: m_NSWTrunc(Op: m_Value(V&: X)),
1576 R: m_OneUse(SubPattern: m_ZExtOrSExt(Op: m_Value(V&: Y)))))) {
1577 // Can fold trunc nsw + zext/sext for all predicates.
1578 YIsSExt =
1579 isa<SExtInst>(Val: Cmp.getOperand(i_nocapture: 0)) || isa<SExtInst>(Val: Cmp.getOperand(i_nocapture: 1));
1580 } else
1581 return nullptr;
1582
1583 Type *TruncTy = Cmp.getOperand(i_nocapture: 0)->getType();
1584 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1585
1586 // If this transform will end up changing from desirable types -> undesirable
1587 // types skip it.
1588 if (isDesirableIntType(BitWidth: TruncBits) &&
1589 !isDesirableIntType(BitWidth: X->getType()->getScalarSizeInBits()))
1590 return nullptr;
1591
1592 Value *NewY = Builder.CreateIntCast(V: Y, DestTy: X->getType(), isSigned: YIsSExt);
1593 return new ICmpInst(Pred, X, NewY);
1594}
1595
1596/// Fold icmp (xor X, Y), C.
1597Instruction *InstCombinerImpl::foldICmpXorConstant(ICmpInst &Cmp,
1598 BinaryOperator *Xor,
1599 const APInt &C) {
1600 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1601 return I;
1602
1603 Value *X = Xor->getOperand(i_nocapture: 0);
1604 Value *Y = Xor->getOperand(i_nocapture: 1);
1605 const APInt *XorC;
1606 if (!match(V: Y, P: m_APInt(Res&: XorC)))
1607 return nullptr;
1608
1609 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1610 // fold the xor.
1611 ICmpInst::Predicate Pred = Cmp.getPredicate();
1612 bool TrueIfSigned = false;
1613 if (isSignBitCheck(Pred: Cmp.getPredicate(), RHS: C, TrueIfSigned)) {
1614
1615 // If the sign bit of the XorCst is not set, there is no change to
1616 // the operation, just stop using the Xor.
1617 if (!XorC->isNegative())
1618 return replaceOperand(I&: Cmp, OpNum: 0, V: X);
1619
1620 // Emit the opposite comparison.
1621 if (TrueIfSigned)
1622 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1623 ConstantInt::getAllOnesValue(Ty: X->getType()));
1624 else
1625 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1626 ConstantInt::getNullValue(Ty: X->getType()));
1627 }
1628
1629 if (Xor->hasOneUse()) {
1630 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1631 if (!Cmp.isEquality() && XorC->isSignMask()) {
1632 Pred = Cmp.getFlippedSignednessPredicate();
1633 return new ICmpInst(Pred, X, ConstantInt::get(Ty: X->getType(), V: C ^ *XorC));
1634 }
1635
1636 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1637 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1638 Pred = Cmp.getFlippedSignednessPredicate();
1639 Pred = Cmp.getSwappedPredicate(pred: Pred);
1640 return new ICmpInst(Pred, X, ConstantInt::get(Ty: X->getType(), V: C ^ *XorC));
1641 }
1642 }
1643
1644 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1645 if (Pred == ICmpInst::ICMP_UGT) {
1646 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1647 if (*XorC == ~C && (C + 1).isPowerOf2())
1648 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1649 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1650 if (*XorC == C && (C + 1).isPowerOf2())
1651 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1652 }
1653 if (Pred == ICmpInst::ICMP_ULT) {
1654 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1655 if (*XorC == -C && C.isPowerOf2())
1656 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1657 ConstantInt::get(Ty: X->getType(), V: ~C));
1658 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1659 if (*XorC == C && (-C).isPowerOf2())
1660 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1661 ConstantInt::get(Ty: X->getType(), V: ~C));
1662 }
1663 return nullptr;
1664}
1665
1666/// For power-of-2 C:
1667/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1668/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1669Instruction *InstCombinerImpl::foldICmpXorShiftConst(ICmpInst &Cmp,
1670 BinaryOperator *Xor,
1671 const APInt &C) {
1672 CmpInst::Predicate Pred = Cmp.getPredicate();
1673 APInt PowerOf2;
1674 if (Pred == ICmpInst::ICMP_ULT)
1675 PowerOf2 = C;
1676 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1677 PowerOf2 = C + 1;
1678 else
1679 return nullptr;
1680 if (!PowerOf2.isPowerOf2())
1681 return nullptr;
1682 Value *X;
1683 const APInt *ShiftC;
1684 if (!match(V: Xor, P: m_OneUse(SubPattern: m_c_Xor(L: m_Value(V&: X),
1685 R: m_AShr(L: m_Deferred(V: X), R: m_APInt(Res&: ShiftC))))))
1686 return nullptr;
1687 uint64_t Shift = ShiftC->getLimitedValue();
1688 Type *XType = X->getType();
1689 if (Shift == 0 || PowerOf2.isMinSignedValue())
1690 return nullptr;
1691 Value *Add = Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty: XType, V: PowerOf2));
1692 APInt Bound =
1693 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1694 return new ICmpInst(Pred, Add, ConstantInt::get(Ty: XType, V: Bound));
1695}
1696
1697/// Fold icmp (and (sh X, Y), C2), C1.
1698Instruction *InstCombinerImpl::foldICmpAndShift(ICmpInst &Cmp,
1699 BinaryOperator *And,
1700 const APInt &C1,
1701 const APInt &C2) {
1702 BinaryOperator *Shift = dyn_cast<BinaryOperator>(Val: And->getOperand(i_nocapture: 0));
1703 if (!Shift || !Shift->isShift())
1704 return nullptr;
1705
1706 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1707 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1708 // code produced by the clang front-end, for bitfield access.
1709 // This seemingly simple opportunity to fold away a shift turns out to be
1710 // rather complicated. See PR17827 for details.
1711 unsigned ShiftOpcode = Shift->getOpcode();
1712 bool IsShl = ShiftOpcode == Instruction::Shl;
1713 const APInt *C3;
1714 if (match(V: Shift->getOperand(i_nocapture: 1), P: m_APInt(Res&: C3))) {
1715 APInt NewAndCst, NewCmpCst;
1716 bool AnyCmpCstBitsShiftedOut;
1717 if (ShiftOpcode == Instruction::Shl) {
1718 // For a left shift, we can fold if the comparison is not signed. We can
1719 // also fold a signed comparison if the mask value and comparison value
1720 // are not negative. These constraints may not be obvious, but we can
1721 // prove that they are correct using an SMT solver.
1722 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1723 return nullptr;
1724
1725 NewCmpCst = C1.lshr(ShiftAmt: *C3);
1726 NewAndCst = C2.lshr(ShiftAmt: *C3);
1727 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(ShiftAmt: *C3) != C1;
1728 } else if (ShiftOpcode == Instruction::LShr) {
1729 // For a logical right shift, we can fold if the comparison is not signed.
1730 // We can also fold a signed comparison if the shifted mask value and the
1731 // shifted comparison value are not negative. These constraints may not be
1732 // obvious, but we can prove that they are correct using an SMT solver.
1733 NewCmpCst = C1.shl(ShiftAmt: *C3);
1734 NewAndCst = C2.shl(ShiftAmt: *C3);
1735 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(ShiftAmt: *C3) != C1;
1736 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1737 return nullptr;
1738 } else {
1739 // For an arithmetic shift, check that both constants don't use (in a
1740 // signed sense) the top bits being shifted out.
1741 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1742 NewCmpCst = C1.shl(ShiftAmt: *C3);
1743 NewAndCst = C2.shl(ShiftAmt: *C3);
1744 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(ShiftAmt: *C3) != C1;
1745 if (NewAndCst.ashr(ShiftAmt: *C3) != C2)
1746 return nullptr;
1747 }
1748
1749 if (AnyCmpCstBitsShiftedOut) {
1750 // If we shifted bits out, the fold is not going to work out. As a
1751 // special case, check to see if this means that the result is always
1752 // true or false now.
1753 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1754 return replaceInstUsesWith(I&: Cmp, V: ConstantInt::getFalse(Ty: Cmp.getType()));
1755 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1756 return replaceInstUsesWith(I&: Cmp, V: ConstantInt::getTrue(Ty: Cmp.getType()));
1757 } else {
1758 Value *NewAnd = Builder.CreateAnd(
1759 LHS: Shift->getOperand(i_nocapture: 0), RHS: ConstantInt::get(Ty: And->getType(), V: NewAndCst));
1760 return new ICmpInst(Cmp.getPredicate(), NewAnd,
1761 ConstantInt::get(Ty: And->getType(), V: NewCmpCst));
1762 }
1763 }
1764
1765 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1766 // preferable because it allows the C2 << Y expression to be hoisted out of a
1767 // loop if Y is invariant and X is not.
1768 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1769 !Shift->isArithmeticShift() &&
1770 ((!IsShl && C2.isOne()) || !isa<Constant>(Val: Shift->getOperand(i_nocapture: 0)))) {
1771 // Compute C2 << Y.
1772 Value *NewShift =
1773 IsShl ? Builder.CreateLShr(LHS: And->getOperand(i_nocapture: 1), RHS: Shift->getOperand(i_nocapture: 1))
1774 : Builder.CreateShl(LHS: And->getOperand(i_nocapture: 1), RHS: Shift->getOperand(i_nocapture: 1));
1775
1776 // Compute X & (C2 << Y).
1777 Value *NewAnd = Builder.CreateAnd(LHS: Shift->getOperand(i_nocapture: 0), RHS: NewShift);
1778 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(i_nocapture: 1));
1779 }
1780
1781 return nullptr;
1782}
1783
1784/// Fold icmp (and X, C2), C1.
1785Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp,
1786 BinaryOperator *And,
1787 const APInt &C1) {
1788 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1789
1790 // icmp ne (and X, 1), 0 --> trunc X to i1
1791 if (isICMP_NE && C1.isZero() && match(V: And->getOperand(i_nocapture: 1), P: m_One()))
1792 return new TruncInst(And->getOperand(i_nocapture: 0), Cmp.getType());
1793
1794 const APInt *C2;
1795 Value *X;
1796 if (!match(V: And, P: m_And(L: m_Value(V&: X), R: m_APInt(Res&: C2))))
1797 return nullptr;
1798
1799 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask
1800 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(RHS: ~*C2) &&
1801 C2->isNegatedPowerOf2())
1802 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1803 ConstantInt::get(Ty: X->getType(), V: ~*C2));
1804 // (and X, highmask) s< [1, -highmask] --> X s< -highmask
1805 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() &&
1806 (C1 - 1).ule(RHS: ~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask())
1807 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1808 ConstantInt::get(Ty: X->getType(), V: -*C2));
1809
1810 // Don't perform the following transforms if the AND has multiple uses
1811 if (!And->hasOneUse())
1812 return nullptr;
1813
1814 if (Cmp.isEquality() && C1.isZero()) {
1815 // Restrict this fold to single-use 'and' (PR10267).
1816 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1817 if (C2->isSignMask()) {
1818 Constant *Zero = Constant::getNullValue(Ty: X->getType());
1819 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1820 return new ICmpInst(NewPred, X, Zero);
1821 }
1822
1823 APInt NewC2 = *C2;
1824 KnownBits Know = computeKnownBits(V: And->getOperand(i_nocapture: 0), CxtI: And);
1825 // Set high zeros of C2 to allow matching negated power-of-2.
1826 NewC2 = *C2 | APInt::getHighBitsSet(numBits: C2->getBitWidth(),
1827 hiBitsSet: Know.countMinLeadingZeros());
1828
1829 // Restrict this fold only for single-use 'and' (PR10267).
1830 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1831 if (NewC2.isNegatedPowerOf2()) {
1832 Constant *NegBOC = ConstantInt::get(Ty: And->getType(), V: -NewC2);
1833 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1834 return new ICmpInst(NewPred, X, NegBOC);
1835 }
1836 }
1837
1838 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1839 // the input width without changing the value produced, eliminate the cast:
1840 //
1841 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1842 //
1843 // We can do this transformation if the constants do not have their sign bits
1844 // set or if it is an equality comparison. Extending a relational comparison
1845 // when we're checking the sign bit would not work.
1846 Value *W;
1847 if (match(V: And->getOperand(i_nocapture: 0), P: m_OneUse(SubPattern: m_Trunc(Op: m_Value(V&: W)))) &&
1848 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1849 // TODO: Is this a good transform for vectors? Wider types may reduce
1850 // throughput. Should this transform be limited (even for scalars) by using
1851 // shouldChangeType()?
1852 if (!Cmp.getType()->isVectorTy()) {
1853 Type *WideType = W->getType();
1854 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1855 Constant *ZextC1 = ConstantInt::get(Ty: WideType, V: C1.zext(width: WideScalarBits));
1856 Constant *ZextC2 = ConstantInt::get(Ty: WideType, V: C2->zext(width: WideScalarBits));
1857 Value *NewAnd = Builder.CreateAnd(LHS: W, RHS: ZextC2, Name: And->getName());
1858 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1859 }
1860 }
1861
1862 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, C2: *C2))
1863 return I;
1864
1865 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1866 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1867 //
1868 // iff pred isn't signed
1869 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(i_nocapture: 0)->hasOneUse() &&
1870 match(V: And->getOperand(i_nocapture: 1), P: m_One())) {
1871 Constant *One = cast<Constant>(Val: And->getOperand(i_nocapture: 1));
1872 Value *Or = And->getOperand(i_nocapture: 0);
1873 Value *A, *B, *LShr;
1874 if (match(V: Or, P: m_Or(L: m_Value(V&: LShr), R: m_Value(V&: A))) &&
1875 match(V: LShr, P: m_LShr(L: m_Specific(V: A), R: m_Value(V&: B)))) {
1876 unsigned UsesRemoved = 0;
1877 if (And->hasOneUse())
1878 ++UsesRemoved;
1879 if (Or->hasOneUse())
1880 ++UsesRemoved;
1881 if (LShr->hasOneUse())
1882 ++UsesRemoved;
1883
1884 // Compute A & ((1 << B) | 1)
1885 unsigned RequireUsesRemoved = match(V: B, P: m_ImmConstant()) ? 1 : 3;
1886 if (UsesRemoved >= RequireUsesRemoved) {
1887 Value *NewOr =
1888 Builder.CreateOr(LHS: Builder.CreateShl(LHS: One, RHS: B, Name: LShr->getName(),
1889 /*HasNUW=*/true),
1890 RHS: One, Name: Or->getName());
1891 Value *NewAnd = Builder.CreateAnd(LHS: A, RHS: NewOr, Name: And->getName());
1892 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(i_nocapture: 1));
1893 }
1894 }
1895 }
1896
1897 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1898 // llvm.is.fpclass(X, fcInf|fcNan)
1899 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1900 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1901 // (icmp eq (and (bitcast X to int), ExponentMask), 0) -->
1902 // llvm.is.fpclass(X, fcSubnormal|fcZero)
1903 // (icmp ne (and (bitcast X to int), ExponentMask), 0) -->
1904 // llvm.is.fpclass(X, ~(fcSubnormal|fcZero))
1905 Value *V;
1906 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1907 Kind: Attribute::NoImplicitFloat) &&
1908 Cmp.isEquality() &&
1909 match(V: X, P: m_OneUse(SubPattern: m_ElementWiseBitCast(Op: m_Value(V))))) {
1910 Type *FPType = V->getType()->getScalarType();
1911 if (FPType->isIEEELikeFPTy() && (C1.isZero() || C1 == *C2)) {
1912 APInt ExponentMask =
1913 APFloat::getInf(Sem: FPType->getFltSemantics()).bitcastToAPInt();
1914 if (*C2 == ExponentMask) {
1915 unsigned Mask = C1.isZero()
1916 ? FPClassTest::fcZero | FPClassTest::fcSubnormal
1917 : FPClassTest::fcNan | FPClassTest::fcInf;
1918 if (isICMP_NE)
1919 Mask = ~Mask & fcAllFlags;
1920 return replaceInstUsesWith(I&: Cmp, V: Builder.createIsFPClass(FPNum: V, Test: Mask));
1921 }
1922 }
1923 }
1924
1925 return nullptr;
1926}
1927
1928/// Fold icmp (and X, Y), C.
1929Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp,
1930 BinaryOperator *And,
1931 const APInt &C) {
1932 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C1: C))
1933 return I;
1934
1935 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1936 bool TrueIfNeg;
1937 if (isSignBitCheck(Pred, RHS: C, TrueIfSigned&: TrueIfNeg)) {
1938 // ((X - 1) & ~X) < 0 --> X == 0
1939 // ((X - 1) & ~X) >= 0 --> X != 0
1940 Value *X;
1941 if (match(V: And->getOperand(i_nocapture: 0), P: m_Add(L: m_Value(V&: X), R: m_AllOnes())) &&
1942 match(V: And->getOperand(i_nocapture: 1), P: m_Not(V: m_Specific(V: X)))) {
1943 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1944 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(Ty: X->getType()));
1945 }
1946 // (X & -X) < 0 --> X == MinSignedC
1947 // (X & -X) > -1 --> X != MinSignedC
1948 if (match(V: And, P: m_c_And(L: m_Neg(V: m_Value(V&: X)), R: m_Deferred(V: X)))) {
1949 Constant *MinSignedC = ConstantInt::get(
1950 Ty: X->getType(),
1951 V: APInt::getSignedMinValue(numBits: X->getType()->getScalarSizeInBits()));
1952 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1953 return new ICmpInst(NewPred, X, MinSignedC);
1954 }
1955 }
1956
1957 // TODO: These all require that Y is constant too, so refactor with the above.
1958
1959 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1960 Value *X = And->getOperand(i_nocapture: 0);
1961 Value *Y = And->getOperand(i_nocapture: 1);
1962 if (auto *C2 = dyn_cast<ConstantInt>(Val: Y))
1963 if (auto *LI = dyn_cast<LoadInst>(Val: X))
1964 if (auto *GEP = dyn_cast<GetElementPtrInst>(Val: LI->getOperand(i_nocapture: 0)))
1965 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(LI, GEP, ICI&: Cmp, AndCst: C2))
1966 return Res;
1967
1968 if (!Cmp.isEquality())
1969 return nullptr;
1970
1971 // X & -C == -C -> X > u ~C
1972 // X & -C != -C -> X <= u ~C
1973 // iff C is a power of 2
1974 if (Cmp.getOperand(i_nocapture: 1) == Y && C.isNegatedPowerOf2()) {
1975 auto NewPred =
1976 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGT : CmpInst::ICMP_ULE;
1977 return new ICmpInst(NewPred, X, SubOne(C: cast<Constant>(Val: Cmp.getOperand(i_nocapture: 1))));
1978 }
1979
1980 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
1981 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1982 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1983 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
1984 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)))) &&
1985 X->getType()->isIntOrIntVectorTy(BitWidth: 1) && (C.isZero() || C.isOne())) {
1986 Value *TruncY = Builder.CreateTrunc(V: Y, DestTy: X->getType());
1987 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
1988 Value *And = Builder.CreateAnd(LHS: TruncY, RHS: X);
1989 return BinaryOperator::CreateNot(Op: And);
1990 }
1991 return BinaryOperator::CreateAnd(V1: TruncY, V2: X);
1992 }
1993
1994 // (icmp eq/ne (and (shl -1, X), Y), 0)
1995 // -> (icmp eq/ne (lshr Y, X), 0)
1996 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
1997 // highly unlikely the non-zero case will ever show up in code.
1998 if (C.isZero() &&
1999 match(V: And, P: m_OneUse(SubPattern: m_c_And(L: m_OneUse(SubPattern: m_Shl(L: m_AllOnes(), R: m_Value(V&: X))),
2000 R: m_Value(V&: Y))))) {
2001 Value *LShr = Builder.CreateLShr(LHS: Y, RHS: X);
2002 return new ICmpInst(Pred, LShr, Constant::getNullValue(Ty: LShr->getType()));
2003 }
2004
2005 // (icmp eq/ne (and (add A, Addend), Msk), C)
2006 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk))
2007 {
2008 Value *A;
2009 const APInt *Addend, *Msk;
2010 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))),
2011 R: m_LowBitMask(V&: Msk)))) &&
2012 C.ule(RHS: *Msk)) {
2013 APInt NewComperand = (C - *Addend) & *Msk;
2014 Value *MaskA = Builder.CreateAnd(LHS: A, RHS: ConstantInt::get(Ty: A->getType(), V: *Msk));
2015 return new ICmpInst(Pred, MaskA,
2016 ConstantInt::get(Ty: MaskA->getType(), V: NewComperand));
2017 }
2018 }
2019
2020 return nullptr;
2021}
2022
2023/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
2024static Value *foldICmpOrXorSubChain(ICmpInst &Cmp, BinaryOperator *Or,
2025 InstCombiner::BuilderTy &Builder) {
2026 // Are we using xors or subs to bitwise check for a pair or pairs of
2027 // (in)equalities? Convert to a shorter form that has more potential to be
2028 // folded even further.
2029 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
2030 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
2031 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
2032 // (X1 == X2) && (X3 == X4) && (X5 == X6)
2033 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
2034 // (X1 != X2) || (X3 != X4) || (X5 != X6)
2035 SmallVector<std::pair<Value *, Value *>, 2> CmpValues;
2036 SmallVector<Value *, 16> WorkList(1, Or);
2037
2038 while (!WorkList.empty()) {
2039 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
2040 Value *Lhs, *Rhs;
2041
2042 if (match(V: OrOperatorArgument,
2043 P: m_OneUse(SubPattern: m_Xor(L: m_Value(V&: Lhs), R: m_Value(V&: Rhs))))) {
2044 CmpValues.emplace_back(Args&: Lhs, Args&: Rhs);
2045 return;
2046 }
2047
2048 if (match(V: OrOperatorArgument,
2049 P: m_OneUse(SubPattern: m_Sub(L: m_Value(V&: Lhs), R: m_Value(V&: Rhs))))) {
2050 CmpValues.emplace_back(Args&: Lhs, Args&: Rhs);
2051 return;
2052 }
2053
2054 WorkList.push_back(Elt: OrOperatorArgument);
2055 };
2056
2057 Value *CurrentValue = WorkList.pop_back_val();
2058 Value *OrOperatorLhs, *OrOperatorRhs;
2059
2060 if (!match(V: CurrentValue,
2061 P: m_Or(L: m_Value(V&: OrOperatorLhs), R: m_Value(V&: OrOperatorRhs)))) {
2062 return nullptr;
2063 }
2064
2065 MatchOrOperatorArgument(OrOperatorRhs);
2066 MatchOrOperatorArgument(OrOperatorLhs);
2067 }
2068
2069 ICmpInst::Predicate Pred = Cmp.getPredicate();
2070 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2071 Value *LhsCmp = Builder.CreateICmp(P: Pred, LHS: CmpValues.rbegin()->first,
2072 RHS: CmpValues.rbegin()->second);
2073
2074 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2075 Value *RhsCmp = Builder.CreateICmp(P: Pred, LHS: It->first, RHS: It->second);
2076 LhsCmp = Builder.CreateBinOp(Opc: BOpc, LHS: LhsCmp, RHS: RhsCmp);
2077 }
2078
2079 return LhsCmp;
2080}
2081
2082/// Fold icmp (or X, Y), C.
2083Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,
2084 BinaryOperator *Or,
2085 const APInt &C) {
2086 ICmpInst::Predicate Pred = Cmp.getPredicate();
2087 if (C.isOne()) {
2088 // icmp slt signum(V) 1 --> icmp slt V, 1
2089 Value *V = nullptr;
2090 if (Pred == ICmpInst::ICMP_SLT && match(V: Or, P: m_Signum(V: m_Value(V))))
2091 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2092 ConstantInt::get(Ty: V->getType(), V: 1));
2093 }
2094
2095 Value *OrOp0 = Or->getOperand(i_nocapture: 0), *OrOp1 = Or->getOperand(i_nocapture: 1);
2096
2097 // (icmp eq/ne (or disjoint x, C0), C1)
2098 // -> (icmp eq/ne x, C0^C1)
2099 if (Cmp.isEquality() && match(V: OrOp1, P: m_ImmConstant()) &&
2100 cast<PossiblyDisjointInst>(Val: Or)->isDisjoint()) {
2101 Value *NewC =
2102 Builder.CreateXor(LHS: OrOp1, RHS: ConstantInt::get(Ty: OrOp1->getType(), V: C));
2103 return new ICmpInst(Pred, OrOp0, NewC);
2104 }
2105
2106 const APInt *MaskC;
2107 if (match(V: OrOp1, P: m_APInt(Res&: MaskC)) && Cmp.isEquality()) {
2108 if (*MaskC == C && (C + 1).isPowerOf2()) {
2109 // X | C == C --> X <=u C
2110 // X | C != C --> X >u C
2111 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2112 Pred = (Pred == CmpInst::ICMP_EQ) ? CmpInst::ICMP_ULE : CmpInst::ICMP_UGT;
2113 return new ICmpInst(Pred, OrOp0, OrOp1);
2114 }
2115
2116 // More general: canonicalize 'equality with set bits mask' to
2117 // 'equality with clear bits mask'.
2118 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2119 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2120 if (Or->hasOneUse()) {
2121 Value *And = Builder.CreateAnd(LHS: OrOp0, RHS: ~(*MaskC));
2122 Constant *NewC = ConstantInt::get(Ty: Or->getType(), V: C ^ (*MaskC));
2123 return new ICmpInst(Pred, And, NewC);
2124 }
2125 }
2126
2127 // (X | (X-1)) s< 0 --> X s< 1
2128 // (X | (X-1)) s> -1 --> X s> 0
2129 Value *X;
2130 bool TrueIfSigned;
2131 if (isSignBitCheck(Pred, RHS: C, TrueIfSigned) &&
2132 match(V: Or, P: m_c_Or(L: m_Add(L: m_Value(V&: X), R: m_AllOnes()), R: m_Deferred(V: X)))) {
2133 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2134 Constant *NewC = ConstantInt::get(Ty: X->getType(), V: TrueIfSigned ? 1 : 0);
2135 return new ICmpInst(NewPred, X, NewC);
2136 }
2137
2138 const APInt *OrC;
2139 // icmp(X | OrC, C) --> icmp(X, 0)
2140 if (C.isNonNegative() && match(V: Or, P: m_Or(L: m_Value(V&: X), R: m_APInt(Res&: OrC)))) {
2141 switch (Pred) {
2142 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2143 case ICmpInst::ICMP_SLT:
2144 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2145 case ICmpInst::ICMP_SGE:
2146 if (OrC->sge(RHS: C))
2147 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: X->getType()));
2148 break;
2149 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2150 case ICmpInst::ICMP_SLE:
2151 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2152 case ICmpInst::ICMP_SGT:
2153 if (OrC->sgt(RHS: C))
2154 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), X,
2155 ConstantInt::getNullValue(Ty: X->getType()));
2156 break;
2157 default:
2158 break;
2159 }
2160 }
2161
2162 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2163 return nullptr;
2164
2165 Value *P, *Q;
2166 if (match(V: Or, P: m_Or(L: m_PtrToInt(Op: m_Value(V&: P)), R: m_PtrToInt(Op: m_Value(V&: Q))))) {
2167 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2168 // -> and (icmp eq P, null), (icmp eq Q, null).
2169 Value *CmpP =
2170 Builder.CreateICmp(P: Pred, LHS: P, RHS: ConstantInt::getNullValue(Ty: P->getType()));
2171 Value *CmpQ =
2172 Builder.CreateICmp(P: Pred, LHS: Q, RHS: ConstantInt::getNullValue(Ty: Q->getType()));
2173 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2174 return BinaryOperator::Create(Op: BOpc, S1: CmpP, S2: CmpQ);
2175 }
2176
2177 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2178 return replaceInstUsesWith(I&: Cmp, V);
2179
2180 return nullptr;
2181}
2182
2183/// Fold icmp (mul X, Y), C.
2184Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
2185 BinaryOperator *Mul,
2186 const APInt &C) {
2187 ICmpInst::Predicate Pred = Cmp.getPredicate();
2188 Type *MulTy = Mul->getType();
2189 Value *X = Mul->getOperand(i_nocapture: 0);
2190
2191 // If comparing a square with a constant, try simplifying to comparing square
2192 // roots.
2193 if (X == Mul->getOperand(i_nocapture: 1) && !Cmp.isSigned()) {
2194 APInt R = C.sqrtFloor();
2195 bool IsSqr = C == R * R;
2196
2197 // X * X eq/ne C
2198 if (Cmp.isEquality() &&
2199 (Mul->hasNoUnsignedWrap() || (Mul->hasNoSignedWrap() && C.isZero()))) {
2200
2201 // If constant is not a square, eq/ne is false/true respectively
2202 if (!IsSqr)
2203 return replaceInstUsesWith(
2204 I&: Cmp,
2205 V: ConstantInt::getBool(Ty: Cmp.getType(), V: Pred == ICmpInst::ICMP_NE));
2206
2207 return new ICmpInst(Pred, X, ConstantInt::get(Ty: MulTy, V: R));
2208 }
2209
2210 // If the multiply does not wrap
2211 // X * X pred C --> X pred R
2212 if (Mul->hasNoUnsignedWrap()) {
2213
2214 if (IsSqr)
2215 return new ICmpInst(Pred, X, ConstantInt::get(Ty: MulTy, V: R));
2216
2217 // If C is not a square, we use floor/ceil of sqrt(C).
2218 //
2219 // If LT or LE, we need R to be an overestimate of sqrt(C),
2220 // then use the strict predicate (LT->LT, LE->LT).
2221 //
2222 // If GT or GE, we need R to be an underestimate of sqrt(C),
2223 // then use the strict predicate (GT->GT, GE->GT).
2224 //
2225 // R is already an underestimate of sqrt(C) due to sqrtFloor.
2226 if (ICmpInst::isLT(P: Pred) || ICmpInst::isLE(P: Pred))
2227 ++R;
2228
2229 return new ICmpInst(Cmp.getStrictPredicate(), X,
2230 ConstantInt::get(Ty: MulTy, V: R));
2231 }
2232 }
2233
2234 const APInt *MulC;
2235 if (!match(V: Mul->getOperand(i_nocapture: 1), P: m_APInt(Res&: MulC)))
2236 return nullptr;
2237
2238 // If this is a test of the sign bit and the multiply is sign-preserving with
2239 // a constant operand, use the multiply LHS operand instead:
2240 // (X * +MulC) < 0 --> X < 0
2241 // (X * -MulC) < 0 --> X > 0
2242 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2243 if (MulC->isNegative())
2244 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
2245 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: MulTy));
2246 }
2247
2248 if (MulC->isZero())
2249 return nullptr;
2250
2251 // If the multiply does not wrap or the constant is odd, try to divide the
2252 // compare constant by the multiplication factor.
2253 if (Cmp.isEquality()) {
2254 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2255 if (Mul->hasNoSignedWrap() && C.srem(RHS: *MulC).isZero()) {
2256 Constant *NewC = ConstantInt::get(Ty: MulTy, V: C.sdiv(RHS: *MulC));
2257 return new ICmpInst(Pred, X, NewC);
2258 }
2259
2260 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2261 // correct to transform if MulC * N == C including overflow. I.e with i8
2262 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2263 // miss that case.
2264 if (C.urem(RHS: *MulC).isZero()) {
2265 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2266 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2267 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2268 Constant *NewC = ConstantInt::get(Ty: MulTy, V: C.udiv(RHS: *MulC));
2269 return new ICmpInst(Pred, X, NewC);
2270 }
2271 }
2272 }
2273
2274 // With a matching no-overflow guarantee, fold the constants:
2275 // (X * MulC) < C --> X < (C / MulC)
2276 // (X * MulC) > C --> X > (C / MulC)
2277 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2278 Constant *NewC = nullptr;
2279 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2280 // MININT / -1 --> overflow.
2281 if (C.isMinSignedValue() && MulC->isAllOnes())
2282 return nullptr;
2283 if (MulC->isNegative())
2284 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
2285
2286 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2287 NewC = ConstantInt::get(
2288 Ty: MulTy, V: APIntOps::RoundingSDiv(A: C, B: *MulC, RM: APInt::Rounding::UP));
2289 } else {
2290 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2291 "Unexpected predicate");
2292 NewC = ConstantInt::get(
2293 Ty: MulTy, V: APIntOps::RoundingSDiv(A: C, B: *MulC, RM: APInt::Rounding::DOWN));
2294 }
2295 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2296 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2297 NewC = ConstantInt::get(
2298 Ty: MulTy, V: APIntOps::RoundingUDiv(A: C, B: *MulC, RM: APInt::Rounding::UP));
2299 } else {
2300 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2301 "Unexpected predicate");
2302 NewC = ConstantInt::get(
2303 Ty: MulTy, V: APIntOps::RoundingUDiv(A: C, B: *MulC, RM: APInt::Rounding::DOWN));
2304 }
2305 }
2306
2307 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2308}
2309
2310/// Fold icmp (shl nuw C2, Y), C.
2311static Instruction *foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl,
2312 const APInt &C) {
2313 Value *Y;
2314 const APInt *C2;
2315 if (!match(V: Shl, P: m_NUWShl(L: m_APInt(Res&: C2), R: m_Value(V&: Y))))
2316 return nullptr;
2317
2318 Type *ShiftType = Shl->getType();
2319 unsigned TypeBits = C.getBitWidth();
2320 ICmpInst::Predicate Pred = Cmp.getPredicate();
2321 if (Cmp.isUnsigned()) {
2322 if (C2->isZero() || C2->ugt(RHS: C))
2323 return nullptr;
2324 APInt Div, Rem;
2325 APInt::udivrem(LHS: C, RHS: *C2, Quotient&: Div, Remainder&: Rem);
2326 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();
2327
2328 // (1 << Y) pred C -> Y pred Log2(C)
2329 if (!CIsPowerOf2) {
2330 // (1 << Y) < 30 -> Y <= 4
2331 // (1 << Y) <= 30 -> Y <= 4
2332 // (1 << Y) >= 30 -> Y > 4
2333 // (1 << Y) > 30 -> Y > 4
2334 if (Pred == ICmpInst::ICMP_ULT)
2335 Pred = ICmpInst::ICMP_ULE;
2336 else if (Pred == ICmpInst::ICMP_UGE)
2337 Pred = ICmpInst::ICMP_UGT;
2338 }
2339
2340 unsigned CLog2 = Div.logBase2();
2341 return new ICmpInst(Pred, Y, ConstantInt::get(Ty: ShiftType, V: CLog2));
2342 } else if (Cmp.isSigned() && C2->isOne()) {
2343 Constant *BitWidthMinusOne = ConstantInt::get(Ty: ShiftType, V: TypeBits - 1);
2344 // (1 << Y) > 0 -> Y != 31
2345 // (1 << Y) > C -> Y != 31 if C is negative.
2346 if (Pred == ICmpInst::ICMP_SGT && C.sle(RHS: 0))
2347 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2348
2349 // (1 << Y) < 0 -> Y == 31
2350 // (1 << Y) < 1 -> Y == 31
2351 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2352 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2353 if (Pred == ICmpInst::ICMP_SLT && (C - 1).sle(RHS: 0))
2354 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2355 }
2356
2357 return nullptr;
2358}
2359
2360/// Fold icmp (shl X, Y), C.
2361Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp,
2362 BinaryOperator *Shl,
2363 const APInt &C) {
2364 const APInt *ShiftVal;
2365 if (Cmp.isEquality() && match(V: Shl->getOperand(i_nocapture: 0), P: m_APInt(Res&: ShiftVal)))
2366 return foldICmpShlConstConst(I&: Cmp, A: Shl->getOperand(i_nocapture: 1), AP1: C, AP2: *ShiftVal);
2367
2368 ICmpInst::Predicate Pred = Cmp.getPredicate();
2369 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2370 // -> (icmp pred X, Csle0)
2371 //
2372 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2373 // so X's must be what is used.
2374 if (C.sle(RHS: 0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2375 return new ICmpInst(Pred, Shl->getOperand(i_nocapture: 0), Cmp.getOperand(i_nocapture: 1));
2376
2377 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2378 // -> (icmp eq/ne X, 0)
2379 if (ICmpInst::isEquality(P: Pred) && C.isZero() &&
2380 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2381 return new ICmpInst(Pred, Shl->getOperand(i_nocapture: 0), Cmp.getOperand(i_nocapture: 1));
2382
2383 // (icmp slt (shl nsw X, Y), 0/1)
2384 // -> (icmp slt X, 0/1)
2385 // (icmp sgt (shl nsw X, Y), 0/-1)
2386 // -> (icmp sgt X, 0/-1)
2387 //
2388 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2389 if (Shl->hasNoSignedWrap() &&
2390 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2391 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2392 return new ICmpInst(Pred, Shl->getOperand(i_nocapture: 0), Cmp.getOperand(i_nocapture: 1));
2393
2394 const APInt *ShiftAmt;
2395 if (!match(V: Shl->getOperand(i_nocapture: 1), P: m_APInt(Res&: ShiftAmt)))
2396 return foldICmpShlLHSC(Cmp, Shl, C);
2397
2398 // Check that the shift amount is in range. If not, don't perform undefined
2399 // shifts. When the shift is visited, it will be simplified.
2400 unsigned TypeBits = C.getBitWidth();
2401 if (ShiftAmt->uge(RHS: TypeBits))
2402 return nullptr;
2403
2404 Value *X = Shl->getOperand(i_nocapture: 0);
2405 Type *ShType = Shl->getType();
2406
2407 // NSW guarantees that we are only shifting out sign bits from the high bits,
2408 // so we can ASHR the compare constant without needing a mask and eliminate
2409 // the shift.
2410 if (Shl->hasNoSignedWrap()) {
2411 if (Pred == ICmpInst::ICMP_SGT) {
2412 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2413 APInt ShiftedC = C.ashr(ShiftAmt: *ShiftAmt);
2414 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2415 }
2416 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2417 C.ashr(ShiftAmt: *ShiftAmt).shl(ShiftAmt: *ShiftAmt) == C) {
2418 APInt ShiftedC = C.ashr(ShiftAmt: *ShiftAmt);
2419 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2420 }
2421 if (Pred == ICmpInst::ICMP_SLT) {
2422 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2423 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2424 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2425 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2426 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2427 APInt ShiftedC = (C - 1).ashr(ShiftAmt: *ShiftAmt) + 1;
2428 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2429 }
2430 }
2431
2432 // NUW guarantees that we are only shifting out zero bits from the high bits,
2433 // so we can LSHR the compare constant without needing a mask and eliminate
2434 // the shift.
2435 if (Shl->hasNoUnsignedWrap()) {
2436 if (Pred == ICmpInst::ICMP_UGT) {
2437 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2438 APInt ShiftedC = C.lshr(ShiftAmt: *ShiftAmt);
2439 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2440 }
2441 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2442 C.lshr(ShiftAmt: *ShiftAmt).shl(ShiftAmt: *ShiftAmt) == C) {
2443 APInt ShiftedC = C.lshr(ShiftAmt: *ShiftAmt);
2444 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2445 }
2446 if (Pred == ICmpInst::ICMP_ULT) {
2447 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2448 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2449 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2450 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2451 assert(C.ugt(0) && "ult 0 should have been eliminated");
2452 APInt ShiftedC = (C - 1).lshr(ShiftAmt: *ShiftAmt) + 1;
2453 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShType, V: ShiftedC));
2454 }
2455 }
2456
2457 if (Cmp.isEquality() && Shl->hasOneUse()) {
2458 // Strength-reduce the shift into an 'and'.
2459 Constant *Mask = ConstantInt::get(
2460 Ty: ShType,
2461 V: APInt::getLowBitsSet(numBits: TypeBits, loBitsSet: TypeBits - ShiftAmt->getZExtValue()));
2462 Value *And = Builder.CreateAnd(LHS: X, RHS: Mask, Name: Shl->getName() + ".mask");
2463 Constant *LShrC = ConstantInt::get(Ty: ShType, V: C.lshr(ShiftAmt: *ShiftAmt));
2464 return new ICmpInst(Pred, And, LShrC);
2465 }
2466
2467 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2468 bool TrueIfSigned = false;
2469 if (Shl->hasOneUse() && isSignBitCheck(Pred, RHS: C, TrueIfSigned)) {
2470 // (X << 31) <s 0 --> (X & 1) != 0
2471 Constant *Mask = ConstantInt::get(
2472 Ty: ShType,
2473 V: APInt::getOneBitSet(numBits: TypeBits, BitNo: TypeBits - ShiftAmt->getZExtValue() - 1));
2474 Value *And = Builder.CreateAnd(LHS: X, RHS: Mask, Name: Shl->getName() + ".mask");
2475 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2476 And, Constant::getNullValue(Ty: ShType));
2477 }
2478
2479 // Simplify 'shl' inequality test into 'and' equality test.
2480 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2481 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2482 if ((C + 1).isPowerOf2() &&
2483 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2484 Value *And = Builder.CreateAnd(LHS: X, RHS: (~C).lshr(shiftAmt: ShiftAmt->getZExtValue()));
2485 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2486 : ICmpInst::ICMP_NE,
2487 And, Constant::getNullValue(Ty: ShType));
2488 }
2489 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2490 if (C.isPowerOf2() &&
2491 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2492 Value *And =
2493 Builder.CreateAnd(LHS: X, RHS: (~(C - 1)).lshr(shiftAmt: ShiftAmt->getZExtValue()));
2494 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2495 : ICmpInst::ICMP_NE,
2496 And, Constant::getNullValue(Ty: ShType));
2497 }
2498 }
2499
2500 // Transform (icmp pred iM (shl iM %v, N), C)
2501 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2502 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2503 // This enables us to get rid of the shift in favor of a trunc that may be
2504 // free on the target. It has the additional benefit of comparing to a
2505 // smaller constant that may be more target-friendly.
2506 unsigned Amt = ShiftAmt->getLimitedValue(Limit: TypeBits - 1);
2507 if (Shl->hasOneUse() && Amt != 0 &&
2508 shouldChangeType(FromBitWidth: ShType->getScalarSizeInBits(), ToBitWidth: TypeBits - Amt)) {
2509 ICmpInst::Predicate CmpPred = Pred;
2510 APInt RHSC = C;
2511
2512 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(predicate: CmpPred)) {
2513 // Try the flipped strictness predicate.
2514 // e.g.:
2515 // icmp ult i64 (shl X, 32), 8589934593 ->
2516 // icmp ule i64 (shl X, 32), 8589934592 ->
2517 // icmp ule i32 (trunc X, i32), 2 ->
2518 // icmp ult i32 (trunc X, i32), 3
2519 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(
2520 Pred, C: ConstantInt::get(Context&: ShType->getContext(), V: C))) {
2521 CmpPred = FlippedStrictness->first;
2522 RHSC = cast<ConstantInt>(Val: FlippedStrictness->second)->getValue();
2523 }
2524 }
2525
2526 if (RHSC.countr_zero() >= Amt) {
2527 Type *TruncTy = ShType->getWithNewBitWidth(NewBitWidth: TypeBits - Amt);
2528 Constant *NewC =
2529 ConstantInt::get(Ty: TruncTy, V: RHSC.ashr(ShiftAmt: *ShiftAmt).trunc(width: TypeBits - Amt));
2530 return new ICmpInst(CmpPred,
2531 Builder.CreateTrunc(V: X, DestTy: TruncTy, Name: "", /*IsNUW=*/false,
2532 IsNSW: Shl->hasNoSignedWrap()),
2533 NewC);
2534 }
2535 }
2536
2537 return nullptr;
2538}
2539
2540/// Fold icmp ({al}shr X, Y), C.
2541Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,
2542 BinaryOperator *Shr,
2543 const APInt &C) {
2544 // An exact shr only shifts out zero bits, so:
2545 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2546 Value *X = Shr->getOperand(i_nocapture: 0);
2547 CmpInst::Predicate Pred = Cmp.getPredicate();
2548 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2549 return new ICmpInst(Pred, X, Cmp.getOperand(i_nocapture: 1));
2550
2551 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2552 const APInt *ShiftValC;
2553 if (match(V: X, P: m_APInt(Res&: ShiftValC))) {
2554 if (Cmp.isEquality())
2555 return foldICmpShrConstConst(I&: Cmp, A: Shr->getOperand(i_nocapture: 1), AP1: C, AP2: *ShiftValC);
2556
2557 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2558 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2559 bool TrueIfSigned;
2560 if (!IsAShr && ShiftValC->isNegative() &&
2561 isSignBitCheck(Pred, RHS: C, TrueIfSigned))
2562 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2563 Shr->getOperand(i_nocapture: 1),
2564 ConstantInt::getNullValue(Ty: X->getType()));
2565
2566 // If the shifted constant is a power-of-2, test the shift amount directly:
2567 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2568 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2569 if (!IsAShr && ShiftValC->isPowerOf2() &&
2570 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2571 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2572 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2573 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2574
2575 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2576 unsigned ShiftLZ = ShiftValC->countl_zero();
2577 Constant *NewC = ConstantInt::get(Ty: Shr->getType(), V: CmpLZ - ShiftLZ);
2578 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2579 return new ICmpInst(NewPred, Shr->getOperand(i_nocapture: 1), NewC);
2580 }
2581 }
2582
2583 const APInt *ShiftAmtC;
2584 if (!match(V: Shr->getOperand(i_nocapture: 1), P: m_APInt(Res&: ShiftAmtC)))
2585 return nullptr;
2586
2587 // Check that the shift amount is in range. If not, don't perform undefined
2588 // shifts. When the shift is visited it will be simplified.
2589 unsigned TypeBits = C.getBitWidth();
2590 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(Limit: TypeBits);
2591 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2592 return nullptr;
2593
2594 bool IsExact = Shr->isExact();
2595 Type *ShrTy = Shr->getType();
2596 // TODO: If we could guarantee that InstSimplify would handle all of the
2597 // constant-value-based preconditions in the folds below, then we could assert
2598 // those conditions rather than checking them. This is difficult because of
2599 // undef/poison (PR34838).
2600 if (IsAShr && Shr->hasOneUse()) {
2601 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2602 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2603 // When C - 1 is a power of two and the transform can be legally
2604 // performed, prefer this form so the produced constant is close to a
2605 // power of two.
2606 // icmp slt/ult (ashr exact X, ShAmtC), C
2607 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2608 APInt ShiftedC = (C - 1).shl(shiftAmt: ShAmtVal) + 1;
2609 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2610 }
2611 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2612 // When ShAmtC can be shifted losslessly:
2613 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2614 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2615 APInt ShiftedC = C.shl(shiftAmt: ShAmtVal);
2616 if (ShiftedC.ashr(ShiftAmt: ShAmtVal) == C)
2617 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2618 }
2619 if (Pred == CmpInst::ICMP_SGT) {
2620 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2621 APInt ShiftedC = (C + 1).shl(shiftAmt: ShAmtVal) - 1;
2622 if (!C.isMaxSignedValue() && !(C + 1).shl(shiftAmt: ShAmtVal).isMinSignedValue() &&
2623 (ShiftedC + 1).ashr(ShiftAmt: ShAmtVal) == (C + 1))
2624 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2625 }
2626 if (Pred == CmpInst::ICMP_UGT) {
2627 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2628 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2629 // clause accounts for that pattern.
2630 APInt ShiftedC = (C + 1).shl(shiftAmt: ShAmtVal) - 1;
2631 if ((ShiftedC + 1).ashr(ShiftAmt: ShAmtVal) == (C + 1) ||
2632 (C + 1).shl(shiftAmt: ShAmtVal).isMinSignedValue())
2633 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2634 }
2635
2636 // If the compare constant has significant bits above the lowest sign-bit,
2637 // then convert an unsigned cmp to a test of the sign-bit:
2638 // (ashr X, ShiftC) u> C --> X s< 0
2639 // (ashr X, ShiftC) u< C --> X s> -1
2640 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2641 if (Pred == CmpInst::ICMP_UGT) {
2642 return new ICmpInst(CmpInst::ICMP_SLT, X,
2643 ConstantInt::getNullValue(Ty: ShrTy));
2644 }
2645 if (Pred == CmpInst::ICMP_ULT) {
2646 return new ICmpInst(CmpInst::ICMP_SGT, X,
2647 ConstantInt::getAllOnesValue(Ty: ShrTy));
2648 }
2649 }
2650 } else if (!IsAShr) {
2651 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2652 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2653 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2654 APInt ShiftedC = C.shl(shiftAmt: ShAmtVal);
2655 if (ShiftedC.lshr(shiftAmt: ShAmtVal) == C)
2656 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2657 }
2658 if (Pred == CmpInst::ICMP_UGT) {
2659 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2660 APInt ShiftedC = (C + 1).shl(shiftAmt: ShAmtVal) - 1;
2661 if ((ShiftedC + 1).lshr(shiftAmt: ShAmtVal) == (C + 1))
2662 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: ShiftedC));
2663 }
2664 }
2665
2666 if (!Cmp.isEquality())
2667 return nullptr;
2668
2669 // Handle equality comparisons of shift-by-constant.
2670
2671 // If the comparison constant changes with the shift, the comparison cannot
2672 // succeed (bits of the comparison constant cannot match the shifted value).
2673 // This should be known by InstSimplify and already be folded to true/false.
2674 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2675 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2676 "Expected icmp+shr simplify did not occur.");
2677
2678 // If the bits shifted out are known zero, compare the unshifted value:
2679 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2680 if (Shr->isExact())
2681 return new ICmpInst(Pred, X, ConstantInt::get(Ty: ShrTy, V: C << ShAmtVal));
2682
2683 if (Shr->hasOneUse()) {
2684 // Canonicalize the shift into an 'and':
2685 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2686 APInt Val(APInt::getHighBitsSet(numBits: TypeBits, hiBitsSet: TypeBits - ShAmtVal));
2687 Constant *Mask = ConstantInt::get(Ty: ShrTy, V: Val);
2688 Value *And = Builder.CreateAnd(LHS: X, RHS: Mask, Name: Shr->getName() + ".mask");
2689 return new ICmpInst(Pred, And, ConstantInt::get(Ty: ShrTy, V: C << ShAmtVal));
2690 }
2691
2692 return nullptr;
2693}
2694
2695Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
2696 BinaryOperator *SRem,
2697 const APInt &C) {
2698 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2699 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
2700 // Canonicalize unsigned predicates to signed:
2701 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 0
2702 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1
2703 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1
2704 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1
2705
2706 const APInt *DivisorC;
2707 if (!match(V: SRem->getOperand(i_nocapture: 1), P: m_APInt(Res&: DivisorC)))
2708 return nullptr;
2709 if (DivisorC->isZero())
2710 return nullptr;
2711
2712 APInt NormalizedC = C;
2713 if (Pred == ICmpInst::ICMP_ULT) {
2714 assert(!NormalizedC.isZero() &&
2715 "ult X, 0 should have been simplified already.");
2716 --NormalizedC;
2717 }
2718 if (C.isNegative())
2719 NormalizedC.flipAllBits();
2720 if (!NormalizedC.uge(RHS: DivisorC->abs() - 1))
2721 return nullptr;
2722
2723 Type *Ty = SRem->getType();
2724 if (Pred == ICmpInst::ICMP_UGT)
2725 return new ICmpInst(ICmpInst::ICMP_SLT, SRem,
2726 ConstantInt::getNullValue(Ty));
2727 return new ICmpInst(ICmpInst::ICMP_SGT, SRem,
2728 ConstantInt::getAllOnesValue(Ty));
2729 }
2730 // Match an 'is positive' or 'is negative' comparison of remainder by a
2731 // constant power-of-2 value:
2732 // (X % pow2C) sgt/slt 0
2733 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2734 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2735 return nullptr;
2736
2737 // TODO: The one-use check is standard because we do not typically want to
2738 // create longer instruction sequences, but this might be a special-case
2739 // because srem is not good for analysis or codegen.
2740 if (!SRem->hasOneUse())
2741 return nullptr;
2742
2743 const APInt *DivisorC;
2744 if (!match(V: SRem->getOperand(i_nocapture: 1), P: m_Power2(V&: DivisorC)))
2745 return nullptr;
2746
2747 // For cmp_sgt/cmp_slt only zero valued C is handled.
2748 // For cmp_eq/cmp_ne only positive valued C is handled.
2749 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2750 !C.isZero()) ||
2751 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2752 !C.isStrictlyPositive()))
2753 return nullptr;
2754
2755 // Mask off the sign bit and the modulo bits (low-bits).
2756 Type *Ty = SRem->getType();
2757 APInt SignMask = APInt::getSignMask(BitWidth: Ty->getScalarSizeInBits());
2758 Constant *MaskC = ConstantInt::get(Ty, V: SignMask | (*DivisorC - 1));
2759 Value *And = Builder.CreateAnd(LHS: SRem->getOperand(i_nocapture: 0), RHS: MaskC);
2760
2761 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2762 return new ICmpInst(Pred, And, ConstantInt::get(Ty, V: C));
2763
2764 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2765 // bit is set. Example:
2766 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2767 if (Pred == ICmpInst::ICMP_SGT)
2768 return new ICmpInst(ICmpInst::ICMP_SGT, And, ConstantInt::getNullValue(Ty));
2769
2770 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2771 // bit is set. Example:
2772 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2773 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, V: SignMask));
2774}
2775
2776/// Fold icmp (udiv X, Y), C.
2777Instruction *InstCombinerImpl::foldICmpUDivConstant(ICmpInst &Cmp,
2778 BinaryOperator *UDiv,
2779 const APInt &C) {
2780 ICmpInst::Predicate Pred = Cmp.getPredicate();
2781 Value *X = UDiv->getOperand(i_nocapture: 0);
2782 Value *Y = UDiv->getOperand(i_nocapture: 1);
2783 Type *Ty = UDiv->getType();
2784
2785 const APInt *C2;
2786 if (!match(V: X, P: m_APInt(Res&: C2)))
2787 return nullptr;
2788
2789 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2790
2791 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2792 if (Pred == ICmpInst::ICMP_UGT) {
2793 assert(!C.isMaxValue() &&
2794 "icmp ugt X, UINT_MAX should have been simplified already.");
2795 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2796 ConstantInt::get(Ty, V: C2->udiv(RHS: C + 1)));
2797 }
2798
2799 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2800 if (Pred == ICmpInst::ICMP_ULT) {
2801 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2802 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2803 ConstantInt::get(Ty, V: C2->udiv(RHS: C)));
2804 }
2805
2806 return nullptr;
2807}
2808
2809/// Fold icmp ({su}div X, Y), C.
2810Instruction *InstCombinerImpl::foldICmpDivConstant(ICmpInst &Cmp,
2811 BinaryOperator *Div,
2812 const APInt &C) {
2813 ICmpInst::Predicate Pred = Cmp.getPredicate();
2814 Value *X = Div->getOperand(i_nocapture: 0);
2815 Value *Y = Div->getOperand(i_nocapture: 1);
2816 Type *Ty = Div->getType();
2817 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2818
2819 // If unsigned division and the compare constant is bigger than
2820 // UMAX/2 (negative), there's only one pair of values that satisfies an
2821 // equality check, so eliminate the division:
2822 // (X u/ Y) == C --> (X == C) && (Y == 1)
2823 // (X u/ Y) != C --> (X != C) || (Y != 1)
2824 // Similarly, if signed division and the compare constant is exactly SMIN:
2825 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2826 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2827 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2828 (!DivIsSigned || C.isMinSignedValue())) {
2829 Value *XBig = Builder.CreateICmp(P: Pred, LHS: X, RHS: ConstantInt::get(Ty, V: C));
2830 Value *YOne = Builder.CreateICmp(P: Pred, LHS: Y, RHS: ConstantInt::get(Ty, V: 1));
2831 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2832 return BinaryOperator::Create(Op: Logic, S1: XBig, S2: YOne);
2833 }
2834
2835 // Fold: icmp pred ([us]div X, C2), C -> range test
2836 // Fold this div into the comparison, producing a range check.
2837 // Determine, based on the divide type, what the range is being
2838 // checked. If there is an overflow on the low or high side, remember
2839 // it, otherwise compute the range [low, hi) bounding the new value.
2840 // See: InsertRangeTest above for the kinds of replacements possible.
2841 const APInt *C2;
2842 if (!match(V: Y, P: m_APInt(Res&: C2)))
2843 return nullptr;
2844
2845 // FIXME: If the operand types don't match the type of the divide
2846 // then don't attempt this transform. The code below doesn't have the
2847 // logic to deal with a signed divide and an unsigned compare (and
2848 // vice versa). This is because (x /s C2) <s C produces different
2849 // results than (x /s C2) <u C or (x /u C2) <s C or even
2850 // (x /u C2) <u C. Simply casting the operands and result won't
2851 // work. :( The if statement below tests that condition and bails
2852 // if it finds it.
2853 // However, when the divisor is a positive constant and the dividend is
2854 // known non-negative, sdiv is equivalent to udiv, so we can lower
2855 // DivIsSigned and proceed through the unsigned path.
2856 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned()) {
2857 if (!DivIsSigned || !C2->isStrictlyPositive() ||
2858 !isKnownNonNegative(V: X, SQ: SQ.getWithInstruction(I: &Cmp)))
2859 return nullptr;
2860 DivIsSigned = false;
2861 }
2862
2863 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2864 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2865 // division-by-constant cases should be present, we can not assert that they
2866 // have happened before we reach this icmp instruction.
2867 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2868 return nullptr;
2869
2870 // Compute Prod = C * C2. We are essentially solving an equation of
2871 // form X / C2 = C. We solve for X by multiplying C2 and C.
2872 // By solving for X, we can turn this into a range check instead of computing
2873 // a divide.
2874 APInt Prod = C * *C2;
2875
2876 // Determine if the product overflows by seeing if the product is not equal to
2877 // the divide. Make sure we do the same kind of divide as in the LHS
2878 // instruction that we're folding.
2879 bool ProdOV = (DivIsSigned ? Prod.sdiv(RHS: *C2) : Prod.udiv(RHS: *C2)) != C;
2880
2881 // If the division is known to be exact, then there is no remainder from the
2882 // divide, so the covered range size is unit, otherwise it is the divisor.
2883 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2884
2885 // Figure out the interval that is being checked. For example, a comparison
2886 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2887 // Compute this interval based on the constants involved and the signedness of
2888 // the compare/divide. This computes a half-open interval, keeping track of
2889 // whether either value in the interval overflows. After analysis each
2890 // overflow variable is set to 0 if it's corresponding bound variable is valid
2891 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2892 int LoOverflow = 0, HiOverflow = 0;
2893 APInt LoBound, HiBound;
2894
2895 if (!DivIsSigned) { // udiv
2896 // e.g. X/5 op 3 --> [15, 20)
2897 LoBound = Prod;
2898 HiOverflow = LoOverflow = ProdOV;
2899 if (!HiOverflow) {
2900 // If this is not an exact divide, then many values in the range collapse
2901 // to the same result value.
2902 HiOverflow = addWithOverflow(Result&: HiBound, In1: LoBound, In2: RangeSize, IsSigned: false);
2903 }
2904 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2905 if (C.isZero()) { // (X / pos) op 0
2906 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2907 LoBound = -(RangeSize - 1);
2908 HiBound = RangeSize;
2909 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2910 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2911 HiOverflow = LoOverflow = ProdOV;
2912 if (!HiOverflow)
2913 HiOverflow = addWithOverflow(Result&: HiBound, In1: Prod, In2: RangeSize, IsSigned: true);
2914 } else { // (X / pos) op neg
2915 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2916 HiBound = Prod + 1;
2917 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2918 if (!LoOverflow) {
2919 APInt DivNeg = -RangeSize;
2920 LoOverflow = addWithOverflow(Result&: LoBound, In1: HiBound, In2: DivNeg, IsSigned: true) ? -1 : 0;
2921 }
2922 }
2923 } else if (C2->isNegative()) { // Divisor is < 0.
2924 if (Div->isExact())
2925 RangeSize.negate();
2926 if (C.isZero()) { // (X / neg) op 0
2927 // e.g. X/-5 op 0 --> [-4, 5)
2928 LoBound = RangeSize + 1;
2929 HiBound = -RangeSize;
2930 if (HiBound == *C2) { // -INTMIN = INTMIN
2931 HiOverflow = 1; // [INTMIN+1, overflow)
2932 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2933 }
2934 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2935 // e.g. X/-5 op 3 --> [-19, -14)
2936 HiBound = Prod + 1;
2937 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2938 if (!LoOverflow)
2939 LoOverflow =
2940 addWithOverflow(Result&: LoBound, In1: HiBound, In2: RangeSize, IsSigned: true) ? -1 : 0;
2941 } else { // (X / neg) op neg
2942 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2943 LoOverflow = HiOverflow = ProdOV;
2944 if (!HiOverflow)
2945 HiOverflow = subWithOverflow(Result&: HiBound, In1: Prod, In2: RangeSize, IsSigned: true);
2946 }
2947
2948 // Dividing by a negative swaps the condition. LT <-> GT
2949 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
2950 }
2951
2952 switch (Pred) {
2953 default:
2954 llvm_unreachable("Unhandled icmp predicate!");
2955 case ICmpInst::ICMP_EQ:
2956 if (LoOverflow && HiOverflow)
2957 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
2958 if (HiOverflow)
2959 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2960 X, ConstantInt::get(Ty, V: LoBound));
2961 if (LoOverflow)
2962 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2963 X, ConstantInt::get(Ty, V: HiBound));
2964 return replaceInstUsesWith(
2965 I&: Cmp, V: insertRangeTest(V: X, Lo: LoBound, Hi: HiBound, isSigned: DivIsSigned, Inside: true));
2966 case ICmpInst::ICMP_NE:
2967 if (LoOverflow && HiOverflow)
2968 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
2969 if (HiOverflow)
2970 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2971 X, ConstantInt::get(Ty, V: LoBound));
2972 if (LoOverflow)
2973 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2974 X, ConstantInt::get(Ty, V: HiBound));
2975 return replaceInstUsesWith(
2976 I&: Cmp, V: insertRangeTest(V: X, Lo: LoBound, Hi: HiBound, isSigned: DivIsSigned, Inside: false));
2977 case ICmpInst::ICMP_ULT:
2978 case ICmpInst::ICMP_SLT:
2979 if (LoOverflow == +1) // Low bound is greater than input range.
2980 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
2981 if (LoOverflow == -1) // Low bound is less than input range.
2982 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
2983 return new ICmpInst(Pred, X, ConstantInt::get(Ty, V: LoBound));
2984 case ICmpInst::ICMP_UGT:
2985 case ICmpInst::ICMP_SGT:
2986 if (HiOverflow == +1) // High bound greater than input range.
2987 return replaceInstUsesWith(I&: Cmp, V: Builder.getFalse());
2988 if (HiOverflow == -1) // High bound less than input range.
2989 return replaceInstUsesWith(I&: Cmp, V: Builder.getTrue());
2990 if (Pred == ICmpInst::ICMP_UGT)
2991 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, V: HiBound));
2992 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, V: HiBound));
2993 }
2994
2995 return nullptr;
2996}
2997
2998/// Fold icmp (sub X, Y), C.
2999Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,
3000 BinaryOperator *Sub,
3001 const APInt &C) {
3002 Value *X = Sub->getOperand(i_nocapture: 0), *Y = Sub->getOperand(i_nocapture: 1);
3003 ICmpInst::Predicate Pred = Cmp.getPredicate();
3004 Type *Ty = Sub->getType();
3005
3006 // (SubC - Y) == C) --> Y == (SubC - C)
3007 // (SubC - Y) != C) --> Y != (SubC - C)
3008 Constant *SubC;
3009 if (Cmp.isEquality() && match(V: X, P: m_ImmConstant(C&: SubC))) {
3010 return new ICmpInst(Pred, Y,
3011 ConstantExpr::getSub(C1: SubC, C2: ConstantInt::get(Ty, V: C)));
3012 }
3013
3014 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
3015 const APInt *C2;
3016 APInt SubResult;
3017 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
3018 bool HasNSW = Sub->hasNoSignedWrap();
3019 bool HasNUW = Sub->hasNoUnsignedWrap();
3020 if (match(V: X, P: m_APInt(Res&: C2)) &&
3021 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
3022 !subWithOverflow(Result&: SubResult, In1: *C2, In2: C, IsSigned: Cmp.isSigned()))
3023 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, V: SubResult));
3024
3025 // X - Y == 0 --> X == Y.
3026 // X - Y != 0 --> X != Y.
3027 // TODO: We allow this with multiple uses as long as the other uses are not
3028 // in phis. The phi use check is guarding against a codegen regression
3029 // for a loop test. If the backend could undo this (and possibly
3030 // subsequent transforms), we would not need this hack.
3031 if (Cmp.isEquality() && C.isZero() &&
3032 none_of(Range: (Sub->users()), P: [](const User *U) { return isa<PHINode>(Val: U); }))
3033 return new ICmpInst(Pred, X, Y);
3034
3035 // The following transforms are only worth it if the only user of the subtract
3036 // is the icmp.
3037 // TODO: This is an artificial restriction for all of the transforms below
3038 // that only need a single replacement icmp. Can these use the phi test
3039 // like the transform above here?
3040 if (!Sub->hasOneUse())
3041 return nullptr;
3042
3043 if (Sub->hasNoSignedWrap()) {
3044 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
3045 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
3046 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
3047
3048 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
3049 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
3050 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3051
3052 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
3053 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
3054 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
3055
3056 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
3057 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3058 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3059 }
3060
3061 if (!match(V: X, P: m_APInt(Res&: C2)))
3062 return nullptr;
3063
3064 // C2 - Y <u C -> (Y | (C - 1)) == C2
3065 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
3066 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
3067 (*C2 & (C - 1)) == (C - 1))
3068 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(LHS: Y, RHS: C - 1), X);
3069
3070 // C2 - Y >u C -> (Y | C) != C2
3071 // iff C2 & C == C and C + 1 is a power of 2
3072 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
3073 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(LHS: Y, RHS: C), X);
3074
3075 // We have handled special cases that reduce.
3076 // Canonicalize any remaining sub to add as:
3077 // (C2 - Y) > C --> (Y + ~C2) < ~C
3078 Value *Add = Builder.CreateAdd(LHS: Y, RHS: ConstantInt::get(Ty, V: ~(*C2)), Name: "notsub",
3079 HasNUW, HasNSW);
3080 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, V: ~C));
3081}
3082
3083static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
3084 Value *Op1, IRBuilderBase &Builder,
3085 bool HasOneUse) {
3086 auto FoldConstant = [&](bool Val) {
3087 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
3088 if (Op0->getType()->isVectorTy())
3089 Res = ConstantVector::getSplat(
3090 EC: cast<VectorType>(Val: Op0->getType())->getElementCount(), Elt: Res);
3091 return Res;
3092 };
3093
3094 switch (Table.to_ulong()) {
3095 case 0: // 0 0 0 0
3096 return FoldConstant(false);
3097 case 1: // 0 0 0 1
3098 return HasOneUse ? Builder.CreateNot(V: Builder.CreateOr(LHS: Op0, RHS: Op1)) : nullptr;
3099 case 2: // 0 0 1 0
3100 return HasOneUse ? Builder.CreateAnd(LHS: Builder.CreateNot(V: Op0), RHS: Op1) : nullptr;
3101 case 3: // 0 0 1 1
3102 return Builder.CreateNot(V: Op0);
3103 case 4: // 0 1 0 0
3104 return HasOneUse ? Builder.CreateAnd(LHS: Op0, RHS: Builder.CreateNot(V: Op1)) : nullptr;
3105 case 5: // 0 1 0 1
3106 return Builder.CreateNot(V: Op1);
3107 case 6: // 0 1 1 0
3108 return Builder.CreateXor(LHS: Op0, RHS: Op1);
3109 case 7: // 0 1 1 1
3110 return HasOneUse ? Builder.CreateNot(V: Builder.CreateAnd(LHS: Op0, RHS: Op1)) : nullptr;
3111 case 8: // 1 0 0 0
3112 return Builder.CreateAnd(LHS: Op0, RHS: Op1);
3113 case 9: // 1 0 0 1
3114 return HasOneUse ? Builder.CreateNot(V: Builder.CreateXor(LHS: Op0, RHS: Op1)) : nullptr;
3115 case 10: // 1 0 1 0
3116 return Op1;
3117 case 11: // 1 0 1 1
3118 return HasOneUse ? Builder.CreateOr(LHS: Builder.CreateNot(V: Op0), RHS: Op1) : nullptr;
3119 case 12: // 1 1 0 0
3120 return Op0;
3121 case 13: // 1 1 0 1
3122 return HasOneUse ? Builder.CreateOr(LHS: Op0, RHS: Builder.CreateNot(V: Op1)) : nullptr;
3123 case 14: // 1 1 1 0
3124 return Builder.CreateOr(LHS: Op0, RHS: Op1);
3125 case 15: // 1 1 1 1
3126 return FoldConstant(true);
3127 default:
3128 llvm_unreachable("Invalid Operation");
3129 }
3130 return nullptr;
3131}
3132
3133Instruction *InstCombinerImpl::foldICmpBinOpWithConstantViaTruthTable(
3134 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3135 Value *A, *B;
3136 Constant *C1, *C2, *C3, *C4;
3137 if (!match(V: BO->getOperand(i_nocapture: 0),
3138 P: m_SelectLike(C: m_Value(V&: A), TrueC: m_Constant(C&: C1), FalseC: m_Constant(C&: C2))) ||
3139 !match(V: BO->getOperand(i_nocapture: 1),
3140 P: m_SelectLike(C: m_Value(V&: B), TrueC: m_Constant(C&: C3), FalseC: m_Constant(C&: C4))) ||
3141 Cmp.getType() != A->getType() || Cmp.getType() != B->getType())
3142 return nullptr;
3143
3144 std::bitset<4> Table;
3145 auto ComputeTable = [&](bool First, bool Second) -> std::optional<bool> {
3146 Constant *L = First ? C1 : C2;
3147 Constant *R = Second ? C3 : C4;
3148 if (auto *Res = ConstantFoldBinaryOpOperands(Opcode: BO->getOpcode(), LHS: L, RHS: R, DL)) {
3149 auto *Val = Res->getType()->isVectorTy() ? Res->getSplatValue() : Res;
3150 if (auto *CI = dyn_cast_or_null<ConstantInt>(Val))
3151 return ICmpInst::compare(LHS: CI->getValue(), RHS: C, Pred: Cmp.getPredicate());
3152 }
3153 return std::nullopt;
3154 };
3155
3156 for (unsigned I = 0; I < 4; ++I) {
3157 bool First = (I >> 1) & 1;
3158 bool Second = I & 1;
3159 if (auto Res = ComputeTable(First, Second))
3160 Table[I] = *Res;
3161 else
3162 return nullptr;
3163 }
3164
3165 // Synthesize optimal logic.
3166 if (auto *Cond = createLogicFromTable(Table, Op0: A, Op1: B, Builder, HasOneUse: BO->hasOneUse()))
3167 return replaceInstUsesWith(I&: Cmp, V: Cond);
3168 return nullptr;
3169}
3170
3171/// Fold icmp (add X, Y), C.
3172Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
3173 BinaryOperator *Add,
3174 const APInt &C) {
3175 Value *Y = Add->getOperand(i_nocapture: 1);
3176 Value *X = Add->getOperand(i_nocapture: 0);
3177 const CmpPredicate Pred = Cmp.getCmpPredicate();
3178
3179 // icmp ult (add nuw A, (lshr A, ShAmtC)), C --> icmp ult A, C
3180 // when C <= (1 << ShAmtC).
3181 const APInt *ShAmtC;
3182 Value *A;
3183 unsigned BitWidth = C.getBitWidth();
3184 if (Pred == ICmpInst::ICMP_ULT &&
3185 match(V: Add,
3186 P: m_c_NUWAdd(L: m_Value(V&: A), R: m_LShr(L: m_Deferred(V: A), R: m_APInt(Res&: ShAmtC)))) &&
3187 ShAmtC->ult(RHS: BitWidth) &&
3188 C.ule(RHS: APInt::getOneBitSet(numBits: BitWidth, BitNo: ShAmtC->getZExtValue())))
3189 return new ICmpInst(Pred, A, ConstantInt::get(Ty: A->getType(), V: C));
3190
3191 const APInt *C2;
3192 if (Cmp.isEquality() || !match(V: Y, P: m_APInt(Res&: C2)))
3193 return nullptr;
3194
3195 // Fold icmp pred (add X, C2), C.
3196 Type *Ty = Add->getType();
3197
3198 // If the add does not wrap, we can always adjust the compare by subtracting
3199 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3200 // have been canonicalized to SGT/SLT/UGT/ULT.
3201 if (Add->hasNoUnsignedWrap() &&
3202 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT)) {
3203 bool Overflow;
3204 APInt NewC = C.usub_ov(RHS: *C2, Overflow);
3205 // If there is overflow, the result must be true or false.
3206 if (!Overflow)
3207 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3208 return new ICmpInst(Pred, X, ConstantInt::get(Ty, V: NewC));
3209 }
3210
3211 CmpInst::Predicate ChosenPred = Pred.getPreferredSignedPredicate();
3212
3213 if (Add->hasNoSignedWrap() &&
3214 (ChosenPred == ICmpInst::ICMP_SGT || ChosenPred == ICmpInst::ICMP_SLT)) {
3215 bool Overflow;
3216 APInt NewC = C.ssub_ov(RHS: *C2, Overflow);
3217 if (!Overflow)
3218 // icmp samesign ugt/ult (add nsw X, C2), C
3219 // -> icmp sgt/slt X, (C - C2)
3220 return new ICmpInst(ChosenPred, X, ConstantInt::get(Ty, V: NewC));
3221 }
3222
3223 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3224 C.isNonNegative() && (C - *C2).isNonNegative() &&
3225 computeConstantRange(V: X, /*ForSigned=*/true, SQ: SQ.getWithInstruction(I: &Cmp))
3226 .add(Other: *C2)
3227 .isAllNonNegative())
3228 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
3229 ConstantInt::get(Ty, V: C - *C2));
3230
3231 auto CR = ConstantRange::makeExactICmpRegion(Pred, Other: C).subtract(CI: *C2);
3232 const APInt &Upper = CR.getUpper();
3233 const APInt &Lower = CR.getLower();
3234 if (Cmp.isSigned()) {
3235 if (Lower.isSignMask())
3236 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, V: Upper));
3237 if (Upper.isSignMask())
3238 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, V: Lower));
3239 } else {
3240 if (Lower.isMinValue())
3241 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, V: Upper));
3242 if (Upper.isMinValue())
3243 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, V: Lower));
3244 }
3245
3246 // This set of folds is intentionally placed after folds that use no-wrapping
3247 // flags because those folds are likely better for later analysis/codegen.
3248 const APInt SMax = APInt::getSignedMaxValue(numBits: Ty->getScalarSizeInBits());
3249 const APInt SMin = APInt::getSignedMinValue(numBits: Ty->getScalarSizeInBits());
3250
3251 // Fold compare with offset to opposite sign compare if it eliminates offset:
3252 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3253 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3254 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, V: -(*C2)));
3255
3256 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3257 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3258 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, V: ~(*C2)));
3259
3260 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3261 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3262 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, V: SMax - C));
3263
3264 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3265 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3266 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, V: C ^ SMax));
3267
3268 // (X + -1) <u C --> X <=u C (if X is never null)
3269 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3270 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
3271 if (llvm::isKnownNonZero(V: X, Q))
3272 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, V: C));
3273 }
3274
3275 if (!Add->hasOneUse())
3276 return nullptr;
3277
3278 // X+C <u C2 -> (X & -C2) == C
3279 // iff C & (C2-1) == 0
3280 // C2 is a power of 2
3281 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3282 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(LHS: X, RHS: -C),
3283 ConstantExpr::getNeg(C: cast<Constant>(Val: Y)));
3284
3285 // X+C2 <u C -> (X & C) == 2C
3286 // iff C == -(C2)
3287 // C2 is a power of 2
3288 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3289 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(LHS: X, RHS: C),
3290 ConstantInt::get(Ty, V: C * 2));
3291
3292 // X+C >u C2 -> (X & ~C2) != C
3293 // iff C & C2 == 0
3294 // C2+1 is a power of 2
3295 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3296 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(LHS: X, RHS: ~C),
3297 ConstantExpr::getNeg(C: cast<Constant>(Val: Y)));
3298
3299 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3300 // to the ult form.
3301 // X+C2 >u C -> X+(C2-C-1) <u ~C
3302 if (Pred == ICmpInst::ICMP_UGT)
3303 return new ICmpInst(ICmpInst::ICMP_ULT,
3304 Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty, V: *C2 - C - 1)),
3305 ConstantInt::get(Ty, V: ~C));
3306
3307 // zext(V) + C2 pred C -> V + C3 pred' C4
3308 Value *V;
3309 if (match(V: X, P: m_ZExt(Op: m_Value(V)))) {
3310 Type *NewCmpTy = V->getType();
3311 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();
3312 if (shouldChangeType(From: Ty, To: NewCmpTy)) {
3313 ConstantRange SrcCR = CR.truncate(BitWidth: NewCmpBW, NoWrapKind: TruncInst::NoUnsignedWrap);
3314 CmpInst::Predicate EquivPred;
3315 APInt EquivInt;
3316 APInt EquivOffset;
3317
3318 SrcCR.getEquivalentICmp(Pred&: EquivPred, RHS&: EquivInt, Offset&: EquivOffset);
3319 return new ICmpInst(
3320 EquivPred,
3321 EquivOffset.isZero()
3322 ? V
3323 : Builder.CreateAdd(LHS: V, RHS: ConstantInt::get(Ty: NewCmpTy, V: EquivOffset)),
3324 ConstantInt::get(Ty: NewCmpTy, V: EquivInt));
3325 }
3326 }
3327
3328 return nullptr;
3329}
3330
3331bool InstCombinerImpl::matchThreeWayIntCompare(SelectInst *SI, Value *&LHS,
3332 Value *&RHS, ConstantInt *&Less,
3333 ConstantInt *&Equal,
3334 ConstantInt *&Greater) {
3335 // TODO: Generalize this to work with other comparison idioms or ensure
3336 // they get canonicalized into this form.
3337
3338 // select i1 (a == b),
3339 // i32 Equal,
3340 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3341 // where Equal, Less and Greater are placeholders for any three constants.
3342 CmpPredicate PredA;
3343 if (!match(V: SI->getCondition(), P: m_ICmp(Pred&: PredA, L: m_Value(V&: LHS), R: m_Value(V&: RHS))) ||
3344 !ICmpInst::isEquality(P: PredA))
3345 return false;
3346 Value *EqualVal = SI->getTrueValue();
3347 Value *UnequalVal = SI->getFalseValue();
3348 // We still can get non-canonical predicate here, so canonicalize.
3349 if (PredA == ICmpInst::ICMP_NE)
3350 std::swap(a&: EqualVal, b&: UnequalVal);
3351 if (!match(V: EqualVal, P: m_ConstantInt(CI&: Equal)))
3352 return false;
3353 CmpPredicate PredB;
3354 Value *LHS2, *RHS2;
3355 if (!match(V: UnequalVal, P: m_Select(C: m_ICmp(Pred&: PredB, L: m_Value(V&: LHS2), R: m_Value(V&: RHS2)),
3356 L: m_ConstantInt(CI&: Less), R: m_ConstantInt(CI&: Greater))))
3357 return false;
3358 // We can get predicate mismatch here, so canonicalize if possible:
3359 // First, ensure that 'LHS' match.
3360 if (LHS2 != LHS) {
3361 // x sgt y <--> y slt x
3362 std::swap(a&: LHS2, b&: RHS2);
3363 PredB = ICmpInst::getSwappedPredicate(pred: PredB);
3364 }
3365 if (LHS2 != LHS)
3366 return false;
3367 // We also need to canonicalize 'RHS'.
3368 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(Val: RHS2)) {
3369 // x sgt C-1 <--> x sge C <--> not(x slt C)
3370 auto FlippedStrictness =
3371 getFlippedStrictnessPredicateAndConstant(Pred: PredB, C: cast<Constant>(Val: RHS2));
3372 if (!FlippedStrictness)
3373 return false;
3374 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3375 "basic correctness failure");
3376 RHS2 = FlippedStrictness->second;
3377 // And kind-of perform the result swap.
3378 std::swap(a&: Less, b&: Greater);
3379 PredB = ICmpInst::ICMP_SLT;
3380 }
3381 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3382}
3383
3384Instruction *InstCombinerImpl::foldICmpSelectConstant(ICmpInst &Cmp,
3385 SelectInst *Select,
3386 ConstantInt *C) {
3387
3388 assert(C && "Cmp RHS should be a constant int!");
3389 // If we're testing a constant value against the result of a three way
3390 // comparison, the result can be expressed directly in terms of the
3391 // original values being compared. Note: We could possibly be more
3392 // aggressive here and remove the hasOneUse test. The original select is
3393 // really likely to simplify or sink when we remove a test of the result.
3394 Value *OrigLHS, *OrigRHS;
3395 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3396 if (Cmp.hasOneUse() &&
3397 matchThreeWayIntCompare(SI: Select, LHS&: OrigLHS, RHS&: OrigRHS, Less&: C1LessThan, Equal&: C2Equal,
3398 Greater&: C3GreaterThan)) {
3399 assert(C1LessThan && C2Equal && C3GreaterThan);
3400
3401 bool TrueWhenLessThan = ICmpInst::compare(
3402 LHS: C1LessThan->getValue(), RHS: C->getValue(), Pred: Cmp.getPredicate());
3403 bool TrueWhenEqual = ICmpInst::compare(LHS: C2Equal->getValue(), RHS: C->getValue(),
3404 Pred: Cmp.getPredicate());
3405 bool TrueWhenGreaterThan = ICmpInst::compare(
3406 LHS: C3GreaterThan->getValue(), RHS: C->getValue(), Pred: Cmp.getPredicate());
3407
3408 // This generates the new instruction that will replace the original Cmp
3409 // Instruction. Instead of enumerating the various combinations when
3410 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3411 // false, we rely on chaining of ORs and future passes of InstCombine to
3412 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3413
3414 // When none of the three constants satisfy the predicate for the RHS (C),
3415 // the entire original Cmp can be simplified to a false.
3416 Value *Cond = Builder.getFalse();
3417 if (TrueWhenLessThan)
3418 Cond = Builder.CreateOr(
3419 LHS: Cond, RHS: Builder.CreateICmp(P: ICmpInst::ICMP_SLT, LHS: OrigLHS, RHS: OrigRHS));
3420 if (TrueWhenEqual)
3421 Cond = Builder.CreateOr(
3422 LHS: Cond, RHS: Builder.CreateICmp(P: ICmpInst::ICMP_EQ, LHS: OrigLHS, RHS: OrigRHS));
3423 if (TrueWhenGreaterThan)
3424 Cond = Builder.CreateOr(
3425 LHS: Cond, RHS: Builder.CreateICmp(P: ICmpInst::ICMP_SGT, LHS: OrigLHS, RHS: OrigRHS));
3426
3427 return replaceInstUsesWith(I&: Cmp, V: Cond);
3428 }
3429 return nullptr;
3430}
3431
3432Instruction *InstCombinerImpl::foldICmpBitCast(ICmpInst &Cmp) {
3433 auto *Bitcast = dyn_cast<BitCastInst>(Val: Cmp.getOperand(i_nocapture: 0));
3434 if (!Bitcast)
3435 return nullptr;
3436
3437 ICmpInst::Predicate Pred = Cmp.getPredicate();
3438 Value *Op1 = Cmp.getOperand(i_nocapture: 1);
3439 Value *BCSrcOp = Bitcast->getOperand(i_nocapture: 0);
3440 Type *SrcType = Bitcast->getSrcTy();
3441 Type *DstType = Bitcast->getType();
3442
3443 // Make sure the bitcast doesn't change between scalar and vector and
3444 // doesn't change the number of vector elements.
3445 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3446 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3447 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3448 Value *X;
3449 if (match(V: BCSrcOp, P: m_SIToFP(Op: m_Value(V&: X)))) {
3450 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3451 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3452 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3453 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3454 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3455 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3456 match(V: Op1, P: m_Zero()))
3457 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: X->getType()));
3458
3459 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3460 if (Pred == ICmpInst::ICMP_SLT && match(V: Op1, P: m_One()))
3461 return new ICmpInst(Pred, X, ConstantInt::get(Ty: X->getType(), V: 1));
3462
3463 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3464 if (Pred == ICmpInst::ICMP_SGT && match(V: Op1, P: m_AllOnes()))
3465 return new ICmpInst(Pred, X,
3466 ConstantInt::getAllOnesValue(Ty: X->getType()));
3467 }
3468
3469 // Zero-equality checks are preserved through unsigned floating-point casts:
3470 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3471 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3472 if (match(V: BCSrcOp, P: m_UIToFP(Op: m_Value(V&: X))))
3473 if (Cmp.isEquality() && match(V: Op1, P: m_Zero()))
3474 return new ICmpInst(Pred, X, ConstantInt::getNullValue(Ty: X->getType()));
3475
3476 const APInt *C;
3477 bool TrueIfSigned;
3478 if (match(V: Op1, P: m_APInt(Res&: C)) && Bitcast->hasOneUse()) {
3479 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3480 // the FP extend/truncate because that cast does not change the sign-bit.
3481 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3482 // The sign-bit is always the most significant bit in those types.
3483 if (isSignBitCheck(Pred, RHS: *C, TrueIfSigned) &&
3484 (match(V: BCSrcOp, P: m_FPExt(Op: m_Value(V&: X))) ||
3485 match(V: BCSrcOp, P: m_FPTrunc(Op: m_Value(V&: X))))) {
3486 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3487 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3488 Type *XType = X->getType();
3489
3490 // We can't currently handle Power style floating point operations here.
3491 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3492 Type *NewType = Builder.getIntNTy(N: XType->getScalarSizeInBits());
3493 if (auto *XVTy = dyn_cast<VectorType>(Val: XType))
3494 NewType = VectorType::get(ElementType: NewType, EC: XVTy->getElementCount());
3495 Value *NewBitcast = Builder.CreateBitCast(V: X, DestTy: NewType);
3496 if (TrueIfSigned)
3497 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3498 ConstantInt::getNullValue(Ty: NewType));
3499 else
3500 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3501 ConstantInt::getAllOnesValue(Ty: NewType));
3502 }
3503 }
3504
3505 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3506 Type *FPType = SrcType->getScalarType();
3507 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3508 Kind: Attribute::NoImplicitFloat) &&
3509 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3510 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3511 if (Mask & (fcInf | fcZero)) {
3512 if (Pred == ICmpInst::ICMP_NE)
3513 Mask = ~Mask;
3514 return replaceInstUsesWith(I&: Cmp,
3515 V: Builder.createIsFPClass(FPNum: BCSrcOp, Test: Mask));
3516 }
3517 }
3518 }
3519 }
3520
3521 const APInt *C;
3522 if (!match(V: Cmp.getOperand(i_nocapture: 1), P: m_APInt(Res&: C)) || !DstType->isIntegerTy() ||
3523 !SrcType->isIntOrIntVectorTy())
3524 return nullptr;
3525
3526 // If this is checking if all elements of a vector compare are set or not,
3527 // invert the casted vector equality compare and test if all compare
3528 // elements are clear or not. Compare against zero is generally easier for
3529 // analysis and codegen.
3530 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3531 // Example: are all elements equal? --> are zero elements not equal?
3532 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3533 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3534 if (Value *NotBCSrcOp =
3535 getFreelyInverted(V: BCSrcOp, WillInvertAllUses: BCSrcOp->hasOneUse(), Builder: &Builder)) {
3536 Value *Cast = Builder.CreateBitCast(V: NotBCSrcOp, DestTy: DstType);
3537 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(Ty: DstType));
3538 }
3539 }
3540
3541 // If this is checking if all elements of an extended vector are clear or not,
3542 // compare in a narrow type to eliminate the extend:
3543 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3544 Value *X;
3545 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3546 match(V: BCSrcOp, P: m_ZExtOrSExt(Op: m_Value(V&: X)))) {
3547 if (auto *VecTy = dyn_cast<FixedVectorType>(Val: X->getType())) {
3548 Type *NewType = Builder.getIntNTy(N: VecTy->getPrimitiveSizeInBits());
3549 Value *NewCast = Builder.CreateBitCast(V: X, DestTy: NewType);
3550 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(Ty: NewType));
3551 }
3552 }
3553
3554 // Folding: icmp <pred> iN X, C
3555 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3556 // and C is a splat of a K-bit pattern
3557 // and SC is a constant vector = <C', C', C', ..., C'>
3558 // Into:
3559 // %E = extractelement <M x iK> %vec, i32 C'
3560 // icmp <pred> iK %E, trunc(C)
3561 Value *Vec;
3562 ArrayRef<int> Mask;
3563 if (match(V: BCSrcOp, P: m_Shuffle(v1: m_Value(V&: Vec), v2: m_Undef(), mask: m_Mask(Mask)))) {
3564 // Check whether every element of Mask is the same constant
3565 if (all_equal(Range&: Mask)) {
3566 auto *VecTy = cast<VectorType>(Val: SrcType);
3567 auto *EltTy = cast<IntegerType>(Val: VecTy->getElementType());
3568 if (C->isSplat(SplatSizeInBits: EltTy->getBitWidth())) {
3569 // Fold the icmp based on the value of C
3570 // If C is M copies of an iK sized bit pattern,
3571 // then:
3572 // => %E = extractelement <N x iK> %vec, i32 Elem
3573 // icmp <pred> iK %SplatVal, <pattern>
3574 Value *Elem = Builder.getInt32(C: Mask[0]);
3575 Value *Extract = Builder.CreateExtractElement(Vec, Idx: Elem);
3576 Value *NewC = ConstantInt::get(Ty: EltTy, V: C->trunc(width: EltTy->getBitWidth()));
3577 return new ICmpInst(Pred, Extract, NewC);
3578 }
3579 }
3580 }
3581 return nullptr;
3582}
3583
3584/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3585/// where X is some kind of instruction.
3586Instruction *InstCombinerImpl::foldICmpInstWithConstant(ICmpInst &Cmp) {
3587 const APInt *C;
3588
3589 if (match(V: Cmp.getOperand(i_nocapture: 1), P: m_APInt(Res&: C))) {
3590 if (auto *BO = dyn_cast<BinaryOperator>(Val: Cmp.getOperand(i_nocapture: 0)))
3591 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, C: *C))
3592 return I;
3593
3594 if (auto *SI = dyn_cast<SelectInst>(Val: Cmp.getOperand(i_nocapture: 0)))
3595 // For now, we only support constant integers while folding the
3596 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3597 // similar to the cases handled by binary ops above.
3598 if (auto *ConstRHS = dyn_cast<ConstantInt>(Val: Cmp.getOperand(i_nocapture: 1)))
3599 if (Instruction *I = foldICmpSelectConstant(Cmp, Select: SI, C: ConstRHS))
3600 return I;
3601
3602 if (auto *TI = dyn_cast<TruncInst>(Val: Cmp.getOperand(i_nocapture: 0)))
3603 if (Instruction *I = foldICmpTruncConstant(Cmp, Trunc: TI, C: *C))
3604 return I;
3605
3606 if (auto *II = dyn_cast<IntrinsicInst>(Val: Cmp.getOperand(i_nocapture: 0)))
3607 if (Instruction *I = foldICmpIntrinsicWithConstant(ICI&: Cmp, II, C: *C))
3608 return I;
3609
3610 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3611 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3612 // TODO: This checks one-use, but that is not strictly necessary.
3613 Value *Cmp0 = Cmp.getOperand(i_nocapture: 0);
3614 Value *X, *Y;
3615 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3616 (match(V: Cmp0,
3617 P: m_ExtractValue<0>(V: m_Intrinsic<Intrinsic::ssub_with_overflow>(
3618 Op0: m_Value(V&: X), Op1: m_Value(V&: Y)))) ||
3619 match(V: Cmp0,
3620 P: m_ExtractValue<0>(V: m_Intrinsic<Intrinsic::usub_with_overflow>(
3621 Op0: m_Value(V&: X), Op1: m_Value(V&: Y))))))
3622 return new ICmpInst(Cmp.getPredicate(), X, Y);
3623 }
3624
3625 if (match(V: Cmp.getOperand(i_nocapture: 1), P: m_APIntAllowPoison(Res&: C)))
3626 return foldICmpInstWithConstantAllowPoison(Cmp, C: *C);
3627
3628 return nullptr;
3629}
3630
3631/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3632/// icmp eq/ne BO, C.
3633Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
3634 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3635 // TODO: Some of these folds could work with arbitrary constants, but this
3636 // function is limited to scalar and vector splat constants.
3637 if (!Cmp.isEquality())
3638 return nullptr;
3639
3640 ICmpInst::Predicate Pred = Cmp.getPredicate();
3641 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3642 Constant *RHS = cast<Constant>(Val: Cmp.getOperand(i_nocapture: 1));
3643 Value *BOp0 = BO->getOperand(i_nocapture: 0), *BOp1 = BO->getOperand(i_nocapture: 1);
3644
3645 switch (BO->getOpcode()) {
3646 case Instruction::SRem:
3647 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3648 if (C.isZero() && BO->hasOneUse()) {
3649 const APInt *BOC;
3650 if (match(V: BOp1, P: m_APInt(Res&: BOC)) && BOC->sgt(RHS: 1) && BOC->isPowerOf2()) {
3651 Value *NewRem = Builder.CreateURem(LHS: BOp0, RHS: BOp1, Name: BO->getName());
3652 return new ICmpInst(Pred, NewRem,
3653 Constant::getNullValue(Ty: BO->getType()));
3654 }
3655 }
3656 break;
3657 case Instruction::Add: {
3658 // (A + C2) == C --> A == (C - C2)
3659 // (A + C2) != C --> A != (C - C2)
3660 // TODO: Remove the one-use limitation? See discussion in D58633.
3661 if (Constant *C2 = dyn_cast<Constant>(Val: BOp1)) {
3662 if (BO->hasOneUse())
3663 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(C1: RHS, C2));
3664 } else if (C.isZero()) {
3665 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3666 // efficiently invertible, or if the add has just this one use.
3667 if (Value *NegVal = dyn_castNegVal(V: BOp1))
3668 return new ICmpInst(Pred, BOp0, NegVal);
3669 if (Value *NegVal = dyn_castNegVal(V: BOp0))
3670 return new ICmpInst(Pred, NegVal, BOp1);
3671 if (BO->hasOneUse()) {
3672 // (add nuw A, B) != 0 -> (or A, B) != 0
3673 if (match(V: BO, P: m_NUWAdd(L: m_Value(), R: m_Value()))) {
3674 Value *Or = Builder.CreateOr(LHS: BOp0, RHS: BOp1);
3675 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty: BO->getType()));
3676 }
3677 Value *Neg = Builder.CreateNeg(V: BOp1);
3678 Neg->takeName(V: BO);
3679 return new ICmpInst(Pred, BOp0, Neg);
3680 }
3681 }
3682 break;
3683 }
3684 case Instruction::Xor:
3685 if (Constant *BOC = dyn_cast<Constant>(Val: BOp1)) {
3686 // For the xor case, we can xor two constants together, eliminating
3687 // the explicit xor.
3688 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(C1: RHS, C2: BOC));
3689 } else if (C.isZero()) {
3690 // Replace ((xor A, B) != 0) with (A != B)
3691 return new ICmpInst(Pred, BOp0, BOp1);
3692 }
3693 break;
3694 case Instruction::Or: {
3695 const APInt *BOC;
3696 if (match(V: BOp1, P: m_APInt(Res&: BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3697 // Comparing if all bits outside of a constant mask are set?
3698 // Replace (X | C) == -1 with (X & ~C) == ~C.
3699 // This removes the -1 constant.
3700 Constant *NotBOC = ConstantExpr::getNot(C: cast<Constant>(Val: BOp1));
3701 Value *And = Builder.CreateAnd(LHS: BOp0, RHS: NotBOC);
3702 return new ICmpInst(Pred, And, NotBOC);
3703 }
3704 // (icmp eq (or (select cond, 0, NonZero), Other), 0)
3705 // -> (and cond, (icmp eq Other, 0))
3706 // (icmp ne (or (select cond, NonZero, 0), Other), 0)
3707 // -> (or cond, (icmp ne Other, 0))
3708 Value *Cond, *TV, *FV, *Other, *Sel;
3709 if (C.isZero() &&
3710 match(V: BO,
3711 P: m_OneUse(SubPattern: m_c_Or(L: m_CombineAnd(Ps: m_Value(V&: Sel),
3712 Ps: m_Select(C: m_Value(V&: Cond), L: m_Value(V&: TV),
3713 R: m_Value(V&: FV))),
3714 R: m_Value(V&: Other)))) &&
3715 Cond->getType() == Cmp.getType()) {
3716 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
3717 // Easy case is if eq/ne matches whether 0 is trueval/falseval.
3718 if (Pred == ICmpInst::ICMP_EQ
3719 ? (match(V: TV, P: m_Zero()) && isKnownNonZero(V: FV, Q))
3720 : (match(V: FV, P: m_Zero()) && isKnownNonZero(V: TV, Q))) {
3721 Value *Cmp = Builder.CreateICmp(
3722 P: Pred, LHS: Other, RHS: Constant::getNullValue(Ty: Other->getType()));
3723 return BinaryOperator::Create(
3724 Op: Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, S1: Cmp,
3725 S2: Cond);
3726 }
3727 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3728 // case we need to invert the select condition so we need to be careful to
3729 // avoid creating extra instructions.
3730 // (icmp ne (or (select cond, 0, NonZero), Other), 0)
3731 // -> (or (not cond), (icmp ne Other, 0))
3732 // (icmp eq (or (select cond, NonZero, 0), Other), 0)
3733 // -> (and (not cond), (icmp eq Other, 0))
3734 //
3735 // Only do this if the inner select has one use, in which case we are
3736 // replacing `select` with `(not cond)`. Otherwise, we will create more
3737 // uses. NB: Trying to freely invert cond doesn't make sense here, as if
3738 // cond was freely invertable, the select arms would have been inverted.
3739 if (Sel->hasOneUse() &&
3740 (Pred == ICmpInst::ICMP_EQ
3741 ? (match(V: FV, P: m_Zero()) && isKnownNonZero(V: TV, Q))
3742 : (match(V: TV, P: m_Zero()) && isKnownNonZero(V: FV, Q)))) {
3743 Value *NotCond = Builder.CreateNot(V: Cond);
3744 Value *Cmp = Builder.CreateICmp(
3745 P: Pred, LHS: Other, RHS: Constant::getNullValue(Ty: Other->getType()));
3746 return BinaryOperator::Create(
3747 Op: Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, S1: Cmp,
3748 S2: NotCond);
3749 }
3750 }
3751 break;
3752 }
3753 case Instruction::UDiv:
3754 case Instruction::SDiv:
3755 if (BO->isExact()) {
3756 // div exact X, Y eq/ne 0 -> X eq/ne 0
3757 // div exact X, Y eq/ne 1 -> X eq/ne Y
3758 // div exact X, Y eq/ne C ->
3759 // if Y * C never-overflow && OneUse:
3760 // -> Y * C eq/ne X
3761 if (C.isZero())
3762 return new ICmpInst(Pred, BOp0, Constant::getNullValue(Ty: BO->getType()));
3763 else if (C.isOne())
3764 return new ICmpInst(Pred, BOp0, BOp1);
3765 else if (BO->hasOneUse()) {
3766 OverflowResult OR = computeOverflow(
3767 BinaryOp: Instruction::Mul, IsSigned: BO->getOpcode() == Instruction::SDiv, LHS: BOp1,
3768 RHS: Cmp.getOperand(i_nocapture: 1), CxtI: BO);
3769 if (OR == OverflowResult::NeverOverflows) {
3770 Value *YC =
3771 Builder.CreateMul(LHS: BOp1, RHS: ConstantInt::get(Ty: BO->getType(), V: C));
3772 return new ICmpInst(Pred, YC, BOp0);
3773 }
3774 }
3775 }
3776 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3777 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3778 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3779 return new ICmpInst(NewPred, BOp1, BOp0);
3780 }
3781 break;
3782 default:
3783 break;
3784 }
3785 return nullptr;
3786}
3787
3788static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
3789 const APInt &CRhs,
3790 InstCombiner::BuilderTy &Builder,
3791 const SimplifyQuery &Q) {
3792 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3793 "Non-ctpop intrin in ctpop fold");
3794 if (!CtpopLhs->hasOneUse())
3795 return nullptr;
3796
3797 // Power of 2 test:
3798 // isPow2OrZero : ctpop(X) u< 2
3799 // isPow2 : ctpop(X) == 1
3800 // NotPow2OrZero: ctpop(X) u> 1
3801 // NotPow2 : ctpop(X) != 1
3802 // If we know any bit of X can be folded to:
3803 // IsPow2 : X & (~Bit) == 0
3804 // NotPow2 : X & (~Bit) != 0
3805 const ICmpInst::Predicate Pred = I.getPredicate();
3806 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3807 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3808 Value *Op = CtpopLhs->getArgOperand(i: 0);
3809 KnownBits OpKnown = computeKnownBits(V: Op, DL: Q.DL, AC: Q.AC, CxtI: Q.CxtI, DT: Q.DT);
3810 // No need to check for count > 1, that should be already constant folded.
3811 if (OpKnown.countMinPopulation() == 1) {
3812 Value *And = Builder.CreateAnd(
3813 LHS: Op, RHS: Constant::getIntegerValue(Ty: Op->getType(), V: ~(OpKnown.One)));
3814 return new ICmpInst(
3815 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3816 ? ICmpInst::ICMP_EQ
3817 : ICmpInst::ICMP_NE,
3818 And, Constant::getNullValue(Ty: Op->getType()));
3819 }
3820 }
3821
3822 return nullptr;
3823}
3824
3825/// Fold an equality icmp with LLVM intrinsic and constant operand.
3826Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant(
3827 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3828 Type *Ty = II->getType();
3829 unsigned BitWidth = C.getBitWidth();
3830 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3831
3832 switch (II->getIntrinsicID()) {
3833 case Intrinsic::abs:
3834 // abs(A) == 0 -> A == 0
3835 // abs(A) == INT_MIN -> A == INT_MIN
3836 if (C.isZero() || C.isMinSignedValue())
3837 return new ICmpInst(Pred, II->getArgOperand(i: 0), ConstantInt::get(Ty, V: C));
3838 break;
3839
3840 case Intrinsic::bswap:
3841 // bswap(A) == C -> A == bswap(C)
3842 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3843 ConstantInt::get(Ty, V: C.byteSwap()));
3844
3845 case Intrinsic::bitreverse:
3846 // bitreverse(A) == C -> A == bitreverse(C)
3847 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3848 ConstantInt::get(Ty, V: C.reverseBits()));
3849
3850 case Intrinsic::ctlz:
3851 case Intrinsic::cttz: {
3852 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3853 if (C == BitWidth)
3854 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3855 ConstantInt::getNullValue(Ty));
3856
3857 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3858 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3859 // Limit to one use to ensure we don't increase instruction count.
3860 unsigned Num = C.getLimitedValue(Limit: BitWidth);
3861 if (Num != BitWidth && II->hasOneUse()) {
3862 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3863 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: Num + 1)
3864 : APInt::getHighBitsSet(numBits: BitWidth, hiBitsSet: Num + 1);
3865 APInt Mask2 = IsTrailing
3866 ? APInt::getOneBitSet(numBits: BitWidth, BitNo: Num)
3867 : APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - Num - 1);
3868 return new ICmpInst(Pred, Builder.CreateAnd(LHS: II->getArgOperand(i: 0), RHS: Mask1),
3869 ConstantInt::get(Ty, V: Mask2));
3870 }
3871 break;
3872 }
3873
3874 case Intrinsic::ctpop: {
3875 // popcount(A) == 0 -> A == 0 and likewise for !=
3876 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3877 bool IsZero = C.isZero();
3878 if (IsZero || C == BitWidth)
3879 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3880 IsZero ? Constant::getNullValue(Ty)
3881 : Constant::getAllOnesValue(Ty));
3882
3883 break;
3884 }
3885
3886 case Intrinsic::fshl:
3887 case Intrinsic::fshr:
3888 if (II->getArgOperand(i: 0) == II->getArgOperand(i: 1)) {
3889 const APInt *RotAmtC;
3890 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3891 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3892 if (match(V: II->getArgOperand(i: 2), P: m_APInt(Res&: RotAmtC)))
3893 return new ICmpInst(Pred, II->getArgOperand(i: 0),
3894 II->getIntrinsicID() == Intrinsic::fshl
3895 ? ConstantInt::get(Ty, V: C.rotr(rotateAmt: *RotAmtC))
3896 : ConstantInt::get(Ty, V: C.rotl(rotateAmt: *RotAmtC)));
3897 }
3898 break;
3899
3900 case Intrinsic::umax:
3901 case Intrinsic::uadd_sat: {
3902 // uadd.sat(a, b) == 0 -> (a | b) == 0
3903 // umax(a, b) == 0 -> (a | b) == 0
3904 if (C.isZero() && II->hasOneUse()) {
3905 Value *Or = Builder.CreateOr(LHS: II->getArgOperand(i: 0), RHS: II->getArgOperand(i: 1));
3906 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3907 }
3908 break;
3909 }
3910
3911 case Intrinsic::ssub_sat:
3912 // ssub.sat(a, b) == 0 -> a == b
3913 //
3914 // Note this doesn't work for ssub.sat.i1 because ssub.sat.i1 0, -1 = 0
3915 // (because 1 saturates to 0). Just skip the optimization for i1.
3916 if (C.isZero() && II->getType()->getScalarSizeInBits() > 1)
3917 return new ICmpInst(Pred, II->getArgOperand(i: 0), II->getArgOperand(i: 1));
3918 break;
3919 case Intrinsic::usub_sat: {
3920 // usub.sat(a, b) == 0 -> a <= b
3921 if (C.isZero()) {
3922 ICmpInst::Predicate NewPred =
3923 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3924 return new ICmpInst(NewPred, II->getArgOperand(i: 0), II->getArgOperand(i: 1));
3925 }
3926 break;
3927 }
3928 default:
3929 break;
3930 }
3931
3932 return nullptr;
3933}
3934
3935/// Fold an icmp with LLVM intrinsics
3936static Instruction *
3937foldICmpIntrinsicWithIntrinsic(ICmpInst &Cmp,
3938 InstCombiner::BuilderTy &Builder) {
3939 assert(Cmp.isEquality());
3940
3941 ICmpInst::Predicate Pred = Cmp.getPredicate();
3942 Value *Op0 = Cmp.getOperand(i_nocapture: 0);
3943 Value *Op1 = Cmp.getOperand(i_nocapture: 1);
3944 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Val: Op0);
3945 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Val: Op1);
3946 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3947 return nullptr;
3948
3949 switch (IIOp0->getIntrinsicID()) {
3950 case Intrinsic::bswap:
3951 case Intrinsic::bitreverse:
3952 // If both operands are byte-swapped or bit-reversed, just compare the
3953 // original values.
3954 return new ICmpInst(Pred, IIOp0->getOperand(i_nocapture: 0), IIOp1->getOperand(i_nocapture: 0));
3955 case Intrinsic::fshl:
3956 case Intrinsic::fshr: {
3957 // If both operands are rotated by same amount, just compare the
3958 // original values.
3959 if (IIOp0->getOperand(i_nocapture: 0) != IIOp0->getOperand(i_nocapture: 1))
3960 break;
3961 if (IIOp1->getOperand(i_nocapture: 0) != IIOp1->getOperand(i_nocapture: 1))
3962 break;
3963 if (IIOp0->getOperand(i_nocapture: 2) == IIOp1->getOperand(i_nocapture: 2))
3964 return new ICmpInst(Pred, IIOp0->getOperand(i_nocapture: 0), IIOp1->getOperand(i_nocapture: 0));
3965
3966 // rotate(X, AmtX) == rotate(Y, AmtY)
3967 // -> rotate(X, AmtX - AmtY) == Y
3968 // Do this if either both rotates have one use or if only one has one use
3969 // and AmtX/AmtY are constants.
3970 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3971 if (OneUses == 2 ||
3972 (OneUses == 1 && match(V: IIOp0->getOperand(i_nocapture: 2), P: m_ImmConstant()) &&
3973 match(V: IIOp1->getOperand(i_nocapture: 2), P: m_ImmConstant()))) {
3974 Value *SubAmt =
3975 Builder.CreateSub(LHS: IIOp0->getOperand(i_nocapture: 2), RHS: IIOp1->getOperand(i_nocapture: 2));
3976 Value *CombinedRotate = Builder.CreateIntrinsic(
3977 RetTy: Op0->getType(), ID: IIOp0->getIntrinsicID(),
3978 Args: {IIOp0->getOperand(i_nocapture: 0), IIOp0->getOperand(i_nocapture: 0), SubAmt});
3979 return new ICmpInst(Pred, IIOp1->getOperand(i_nocapture: 0), CombinedRotate);
3980 }
3981 } break;
3982 default:
3983 break;
3984 }
3985
3986 return nullptr;
3987}
3988
3989/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3990/// where X is some kind of instruction and C is AllowPoison.
3991/// TODO: Move more folds which allow poison to this function.
3992Instruction *
3993InstCombinerImpl::foldICmpInstWithConstantAllowPoison(ICmpInst &Cmp,
3994 const APInt &C) {
3995 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3996 if (auto *II = dyn_cast<IntrinsicInst>(Val: Cmp.getOperand(i_nocapture: 0))) {
3997 switch (II->getIntrinsicID()) {
3998 default:
3999 break;
4000 case Intrinsic::fshl:
4001 case Intrinsic::fshr:
4002 if (Cmp.isEquality() && II->getArgOperand(i: 0) == II->getArgOperand(i: 1)) {
4003 // (rot X, ?) == 0/-1 --> X == 0/-1
4004 if (C.isZero() || C.isAllOnes())
4005 return new ICmpInst(Pred, II->getArgOperand(i: 0), Cmp.getOperand(i_nocapture: 1));
4006 }
4007 break;
4008 }
4009 }
4010
4011 return nullptr;
4012}
4013
4014/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
4015Instruction *InstCombinerImpl::foldICmpBinOpWithConstant(ICmpInst &Cmp,
4016 BinaryOperator *BO,
4017 const APInt &C) {
4018 switch (BO->getOpcode()) {
4019 case Instruction::Xor:
4020 if (Instruction *I = foldICmpXorConstant(Cmp, Xor: BO, C))
4021 return I;
4022 break;
4023 case Instruction::And:
4024 if (Instruction *I = foldICmpAndConstant(Cmp, And: BO, C))
4025 return I;
4026 break;
4027 case Instruction::Or:
4028 if (Instruction *I = foldICmpOrConstant(Cmp, Or: BO, C))
4029 return I;
4030 break;
4031 case Instruction::Mul:
4032 if (Instruction *I = foldICmpMulConstant(Cmp, Mul: BO, C))
4033 return I;
4034 break;
4035 case Instruction::Shl:
4036 if (Instruction *I = foldICmpShlConstant(Cmp, Shl: BO, C))
4037 return I;
4038 break;
4039 case Instruction::LShr:
4040 case Instruction::AShr:
4041 if (Instruction *I = foldICmpShrConstant(Cmp, Shr: BO, C))
4042 return I;
4043 break;
4044 case Instruction::SRem:
4045 if (Instruction *I = foldICmpSRemConstant(Cmp, SRem: BO, C))
4046 return I;
4047 break;
4048 case Instruction::UDiv:
4049 if (Instruction *I = foldICmpUDivConstant(Cmp, UDiv: BO, C))
4050 return I;
4051 [[fallthrough]];
4052 case Instruction::SDiv:
4053 if (Instruction *I = foldICmpDivConstant(Cmp, Div: BO, C))
4054 return I;
4055 break;
4056 case Instruction::Sub:
4057 if (Instruction *I = foldICmpSubConstant(Cmp, Sub: BO, C))
4058 return I;
4059 break;
4060 case Instruction::Add:
4061 if (Instruction *I = foldICmpAddConstant(Cmp, Add: BO, C))
4062 return I;
4063 break;
4064 default:
4065 break;
4066 }
4067
4068 // TODO: These folds could be refactored to be part of the above calls.
4069 if (Instruction *I = foldICmpBinOpEqualityWithConstant(Cmp, BO, C))
4070 return I;
4071
4072 // Fall back to handling `icmp pred (select A ? C1 : C2) binop (select B ? C3
4073 // : C4), C5` pattern, by computing a truth table of the four constant
4074 // variants.
4075 return foldICmpBinOpWithConstantViaTruthTable(Cmp, BO, C);
4076}
4077
4078static Instruction *
4079foldICmpUSubSatOrUAddSatWithConstant(CmpPredicate Pred, SaturatingInst *II,
4080 const APInt &C,
4081 InstCombiner::BuilderTy &Builder) {
4082 // This transform may end up producing more than one instruction for the
4083 // intrinsic, so limit it to one user of the intrinsic.
4084 if (!II->hasOneUse())
4085 return nullptr;
4086
4087 // Let Y = [add/sub]_sat(X, C) pred C2
4088 // SatVal = The saturating value for the operation
4089 // WillWrap = Whether or not the operation will underflow / overflow
4090 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
4091 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
4092 //
4093 // When (SatVal pred C2) is true, then
4094 // Y = WillWrap ? true : ((X binop C) pred C2)
4095 // => Y = WillWrap || ((X binop C) pred C2)
4096 // else
4097 // Y = WillWrap ? false : ((X binop C) pred C2)
4098 // => Y = !WillWrap ? ((X binop C) pred C2) : false
4099 // => Y = !WillWrap && ((X binop C) pred C2)
4100 Value *Op0 = II->getOperand(i_nocapture: 0);
4101 Value *Op1 = II->getOperand(i_nocapture: 1);
4102
4103 const APInt *COp1;
4104 // This transform only works when the intrinsic has an integral constant or
4105 // splat vector as the second operand.
4106 if (!match(V: Op1, P: m_APInt(Res&: COp1)))
4107 return nullptr;
4108
4109 APInt SatVal;
4110 switch (II->getIntrinsicID()) {
4111 default:
4112 llvm_unreachable(
4113 "This function only works with usub_sat and uadd_sat for now!");
4114 case Intrinsic::uadd_sat:
4115 SatVal = APInt::getAllOnes(numBits: C.getBitWidth());
4116 break;
4117 case Intrinsic::usub_sat:
4118 SatVal = APInt::getZero(numBits: C.getBitWidth());
4119 break;
4120 }
4121
4122 // Check (SatVal pred C2)
4123 bool SatValCheck = ICmpInst::compare(LHS: SatVal, RHS: C, Pred);
4124
4125 // !WillWrap.
4126 ConstantRange C1 = ConstantRange::makeExactNoWrapRegion(
4127 BinOp: II->getBinaryOp(), Other: *COp1, NoWrapKind: II->getNoWrapKind());
4128
4129 // WillWrap.
4130 if (SatValCheck)
4131 C1 = C1.inverse();
4132
4133 ConstantRange C2 = ConstantRange::makeExactICmpRegion(Pred, Other: C);
4134 if (II->getBinaryOp() == Instruction::Add)
4135 C2 = C2.sub(Other: *COp1);
4136 else
4137 C2 = C2.add(Other: *COp1);
4138
4139 Instruction::BinaryOps CombiningOp =
4140 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
4141
4142 std::optional<ConstantRange> Combination;
4143 if (CombiningOp == Instruction::BinaryOps::Or)
4144 Combination = C1.exactUnionWith(CR: C2);
4145 else /* CombiningOp == Instruction::BinaryOps::And */
4146 Combination = C1.exactIntersectWith(CR: C2);
4147
4148 if (!Combination)
4149 return nullptr;
4150
4151 CmpInst::Predicate EquivPred;
4152 APInt EquivInt;
4153 APInt EquivOffset;
4154
4155 Combination->getEquivalentICmp(Pred&: EquivPred, RHS&: EquivInt, Offset&: EquivOffset);
4156
4157 return new ICmpInst(
4158 EquivPred,
4159 Builder.CreateAdd(LHS: Op0, RHS: ConstantInt::get(Ty: Op1->getType(), V: EquivOffset)),
4160 ConstantInt::get(Ty: Op1->getType(), V: EquivInt));
4161}
4162
4163static Instruction *
4164foldICmpOfCmpIntrinsicWithConstant(CmpPredicate Pred, IntrinsicInst *I,
4165 const APInt &C,
4166 InstCombiner::BuilderTy &Builder) {
4167 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
4168 switch (Pred) {
4169 case ICmpInst::ICMP_EQ:
4170 case ICmpInst::ICMP_NE:
4171 if (C.isZero())
4172 NewPredicate = Pred;
4173 else if (C.isOne())
4174 NewPredicate =
4175 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE;
4176 else if (C.isAllOnes())
4177 NewPredicate =
4178 Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
4179 break;
4180
4181 case ICmpInst::ICMP_SGT:
4182 if (C.isAllOnes())
4183 NewPredicate = ICmpInst::ICMP_UGE;
4184 else if (C.isZero())
4185 NewPredicate = ICmpInst::ICMP_UGT;
4186 break;
4187
4188 case ICmpInst::ICMP_SLT:
4189 if (C.isZero())
4190 NewPredicate = ICmpInst::ICMP_ULT;
4191 else if (C.isOne())
4192 NewPredicate = ICmpInst::ICMP_ULE;
4193 break;
4194
4195 case ICmpInst::ICMP_ULT:
4196 if (C.ugt(RHS: 1))
4197 NewPredicate = ICmpInst::ICMP_UGE;
4198 break;
4199
4200 case ICmpInst::ICMP_UGT:
4201 if (!C.isZero() && !C.isAllOnes())
4202 NewPredicate = ICmpInst::ICMP_ULT;
4203 break;
4204
4205 default:
4206 break;
4207 }
4208
4209 if (!NewPredicate)
4210 return nullptr;
4211
4212 if (I->getIntrinsicID() == Intrinsic::scmp)
4213 NewPredicate = ICmpInst::getSignedPredicate(Pred: *NewPredicate);
4214 Value *LHS = I->getOperand(i_nocapture: 0);
4215 Value *RHS = I->getOperand(i_nocapture: 1);
4216 return new ICmpInst(*NewPredicate, LHS, RHS);
4217}
4218
4219/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
4220Instruction *InstCombinerImpl::foldICmpIntrinsicWithConstant(ICmpInst &Cmp,
4221 IntrinsicInst *II,
4222 const APInt &C) {
4223 ICmpInst::Predicate Pred = Cmp.getPredicate();
4224
4225 // Handle folds that apply for any kind of icmp.
4226 switch (II->getIntrinsicID()) {
4227 default:
4228 break;
4229 case Intrinsic::uadd_sat:
4230 case Intrinsic::usub_sat:
4231 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
4232 Pred, II: cast<SaturatingInst>(Val: II), C, Builder))
4233 return Folded;
4234 break;
4235 case Intrinsic::ctpop: {
4236 const SimplifyQuery Q = SQ.getWithInstruction(I: &Cmp);
4237 if (Instruction *R = foldCtpopPow2Test(I&: Cmp, CtpopLhs: II, CRhs: C, Builder, Q))
4238 return R;
4239 } break;
4240 case Intrinsic::scmp:
4241 case Intrinsic::ucmp:
4242 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, I: II, C, Builder))
4243 return Folded;
4244 break;
4245 }
4246
4247 if (Cmp.isEquality())
4248 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
4249
4250 Type *Ty = II->getType();
4251 unsigned BitWidth = C.getBitWidth();
4252 switch (II->getIntrinsicID()) {
4253 case Intrinsic::ctpop: {
4254 // (ctpop X > BitWidth - 1) --> X == -1
4255 Value *X = II->getArgOperand(i: 0);
4256 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
4257 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_EQ, S1: X,
4258 S2: ConstantInt::getAllOnesValue(Ty));
4259 // (ctpop X < BitWidth) --> X != -1
4260 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
4261 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_NE, S1: X,
4262 S2: ConstantInt::getAllOnesValue(Ty));
4263 break;
4264 }
4265 case Intrinsic::ctlz: {
4266 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
4267 if (Pred == ICmpInst::ICMP_UGT && C.ult(RHS: BitWidth)) {
4268 unsigned Num = C.getLimitedValue();
4269 APInt Limit = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - Num - 1);
4270 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_ULT,
4271 S1: II->getArgOperand(i: 0), S2: ConstantInt::get(Ty, V: Limit));
4272 }
4273
4274 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
4275 if (Pred == ICmpInst::ICMP_ULT && C.uge(RHS: 1) && C.ule(RHS: BitWidth)) {
4276 unsigned Num = C.getLimitedValue();
4277 APInt Limit = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: BitWidth - Num);
4278 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_UGT,
4279 S1: II->getArgOperand(i: 0), S2: ConstantInt::get(Ty, V: Limit));
4280 }
4281 break;
4282 }
4283 case Intrinsic::cttz: {
4284 // Limit to one use to ensure we don't increase instruction count.
4285 if (!II->hasOneUse())
4286 return nullptr;
4287
4288 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
4289 if (Pred == ICmpInst::ICMP_UGT && C.ult(RHS: BitWidth)) {
4290 APInt Mask = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: C.getLimitedValue() + 1);
4291 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_EQ,
4292 S1: Builder.CreateAnd(LHS: II->getArgOperand(i: 0), RHS: Mask),
4293 S2: ConstantInt::getNullValue(Ty));
4294 }
4295
4296 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
4297 if (Pred == ICmpInst::ICMP_ULT && C.uge(RHS: 1) && C.ule(RHS: BitWidth)) {
4298 APInt Mask = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: C.getLimitedValue());
4299 return CmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_NE,
4300 S1: Builder.CreateAnd(LHS: II->getArgOperand(i: 0), RHS: Mask),
4301 S2: ConstantInt::getNullValue(Ty));
4302 }
4303 break;
4304 }
4305 case Intrinsic::ssub_sat:
4306 // ssub.sat(a, b) spred 0 -> a spred b
4307 //
4308 // Note this doesn't work for ssub.sat.i1 because ssub.sat.i1 0, -1 = 0
4309 // (because 1 saturates to 0). Just skip the optimization for i1.
4310 if (ICmpInst::isSigned(Pred) && C.getBitWidth() > 1) {
4311 if (C.isZero())
4312 return new ICmpInst(Pred, II->getArgOperand(i: 0), II->getArgOperand(i: 1));
4313 // X s<= 0 is cannonicalized to X s< 1
4314 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
4315 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(i: 0),
4316 II->getArgOperand(i: 1));
4317 // X s>= 0 is cannonicalized to X s> -1
4318 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
4319 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(i: 0),
4320 II->getArgOperand(i: 1));
4321 }
4322 break;
4323 case Intrinsic::abs: {
4324 if (!II->hasOneUse())
4325 return nullptr;
4326
4327 Value *X = II->getArgOperand(i: 0);
4328 bool IsIntMinPoison =
4329 cast<ConstantInt>(Val: II->getArgOperand(i: 1))->getValue().isOne();
4330
4331 // If C >= 0:
4332 // abs(X) u> C --> X + C u> 2 * C
4333 if (Pred == CmpInst::ICMP_UGT && C.isNonNegative()) {
4334 return new ICmpInst(ICmpInst::ICMP_UGT,
4335 Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty, V: C)),
4336 ConstantInt::get(Ty, V: 2 * C));
4337 }
4338
4339 // If abs(INT_MIN) is poison and C >= 1:
4340 // abs(X) u< C --> X + (C - 1) u<= 2 * (C - 1)
4341 if (IsIntMinPoison && Pred == CmpInst::ICMP_ULT && C.sge(RHS: 1)) {
4342 return new ICmpInst(ICmpInst::ICMP_ULE,
4343 Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty, V: C - 1)),
4344 ConstantInt::get(Ty, V: 2 * (C - 1)));
4345 }
4346
4347 break;
4348 }
4349 default:
4350 break;
4351 }
4352
4353 return nullptr;
4354}
4355
4356/// Handle icmp with constant (but not simple integer constant) RHS.
4357Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {
4358 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
4359 Constant *RHSC = dyn_cast<Constant>(Val: Op1);
4360 Instruction *LHSI = dyn_cast<Instruction>(Val: Op0);
4361 if (!RHSC || !LHSI)
4362 return nullptr;
4363
4364 switch (LHSI->getOpcode()) {
4365 case Instruction::IntToPtr:
4366 // icmp pred inttoptr(X), null -> icmp pred X, null pointer value
4367 if (isa<ConstantPointerNull>(Val: RHSC)) {
4368 Type *IntPtrTy = DL.getIntPtrType(RHSC->getType());
4369 if (IntPtrTy == LHSI->getOperand(i: 0)->getType()) {
4370 APInt NullPtrValue =
4371 DL.getNullPtrValue(AS: RHSC->getType()->getPointerAddressSpace());
4372 return new ICmpInst(I.getPredicate(), LHSI->getOperand(i: 0),
4373 Constant::getIntegerValue(Ty: IntPtrTy, V: NullPtrValue));
4374 }
4375 }
4376 break;
4377
4378 case Instruction::Load:
4379 // Try to optimize things like "A[i] > 4" to index computations.
4380 if (GetElementPtrInst *GEP =
4381 dyn_cast<GetElementPtrInst>(Val: LHSI->getOperand(i: 0)))
4382 if (Instruction *Res =
4383 foldCmpLoadFromIndexedGlobal(LI: cast<LoadInst>(Val: LHSI), GEP, ICI&: I))
4384 return Res;
4385 break;
4386 }
4387
4388 return nullptr;
4389}
4390
4391Instruction *InstCombinerImpl::foldSelectICmp(CmpPredicate Pred, SelectInst *SI,
4392 Value *RHS, const ICmpInst &I) {
4393 // Try to fold the comparison into the select arms, which will cause the
4394 // select to be converted into a logical and/or.
4395 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4396 if (Value *Res = simplifyICmpInst(Pred, LHS: Op, RHS, Q: SQ))
4397 return Res;
4398 if (std::optional<bool> Impl = isImpliedCondition(
4399 LHS: SI->getCondition(), RHSPred: Pred, RHSOp0: Op, RHSOp1: RHS, DL, LHSIsTrue: SelectCondIsTrue))
4400 return ConstantInt::get(Ty: I.getType(), V: *Impl);
4401 return nullptr;
4402 };
4403
4404 ConstantInt *CI = nullptr;
4405 Value *Op1 = SimplifyOp(SI->getOperand(i_nocapture: 1), true);
4406 if (Op1)
4407 CI = dyn_cast<ConstantInt>(Val: Op1);
4408
4409 Value *Op2 = SimplifyOp(SI->getOperand(i_nocapture: 2), false);
4410 if (Op2)
4411 CI = dyn_cast<ConstantInt>(Val: Op2);
4412
4413 auto Simplifies = [&](Value *Op, unsigned Idx) {
4414 // A comparison of ucmp/scmp with a constant will fold into an icmp.
4415 const APInt *Dummy;
4416 return Op ||
4417 (isa<CmpIntrinsic>(Val: SI->getOperand(i_nocapture: Idx)) &&
4418 SI->getOperand(i_nocapture: Idx)->hasOneUse() && match(V: RHS, P: m_APInt(Res&: Dummy)));
4419 };
4420
4421 // We only want to perform this transformation if it will not lead to
4422 // additional code. This is true if either both sides of the select
4423 // fold to a constant (in which case the icmp is replaced with a select
4424 // which will usually simplify) or this is the only user of the
4425 // select (in which case we are trading a select+icmp for a simpler
4426 // select+icmp) or all uses of the select can be replaced based on
4427 // dominance information ("Global cases").
4428 bool Transform = false;
4429 if (Op1 && Op2)
4430 Transform = true;
4431 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {
4432 // Local case
4433 if (SI->hasOneUse())
4434 Transform = true;
4435 // Global cases
4436 else if (CI && !CI->isZero())
4437 // When Op1 is constant try replacing select with second operand.
4438 // Otherwise Op2 is constant and try replacing select with first
4439 // operand.
4440 Transform = replacedSelectWithOperand(SI, Icmp: &I, SIOpd: Op1 ? 2 : 1);
4441 }
4442 if (Transform) {
4443 if (!Op1)
4444 Op1 = Builder.CreateICmp(P: Pred, LHS: SI->getOperand(i_nocapture: 1), RHS, Name: I.getName());
4445 if (!Op2)
4446 Op2 = Builder.CreateICmp(P: Pred, LHS: SI->getOperand(i_nocapture: 2), RHS, Name: I.getName());
4447 return SelectInst::Create(C: SI->getOperand(i_nocapture: 0), S1: Op1, S2: Op2, NameStr: "", InsertBefore: nullptr,
4448 MDFrom: ProfcheckDisableMetadataFixes ? nullptr : SI);
4449 }
4450
4451 return nullptr;
4452}
4453
4454// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4455static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4456 unsigned Depth = 0) {
4457 if (Not ? match(V, P: m_NegatedPower2OrZero()) : match(V, P: m_LowBitMaskOrZero()))
4458 return true;
4459 if (V->getType()->getScalarSizeInBits() == 1)
4460 return true;
4461 if (Depth++ >= MaxAnalysisRecursionDepth)
4462 return false;
4463 Value *X;
4464 const Instruction *I = dyn_cast<Instruction>(Val: V);
4465 if (!I)
4466 return false;
4467 switch (I->getOpcode()) {
4468 case Instruction::ZExt:
4469 // ZExt(Mask) is a Mask.
4470 return !Not && isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4471 case Instruction::SExt:
4472 // SExt(Mask) is a Mask.
4473 // SExt(~Mask) is a ~Mask.
4474 return isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4475 case Instruction::And:
4476 case Instruction::Or:
4477 // Mask0 | Mask1 is a Mask.
4478 // Mask0 & Mask1 is a Mask.
4479 // ~Mask0 | ~Mask1 is a ~Mask.
4480 // ~Mask0 & ~Mask1 is a ~Mask.
4481 return isMaskOrZero(V: I->getOperand(i: 1), Not, Q, Depth) &&
4482 isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4483 case Instruction::Xor:
4484 if (match(V, P: m_Not(V: m_Value(V&: X))))
4485 return isMaskOrZero(V: X, Not: !Not, Q, Depth);
4486
4487 // (X ^ -X) is a ~Mask
4488 if (Not)
4489 return match(V, P: m_c_Xor(L: m_Value(V&: X), R: m_Neg(V: m_Deferred(V: X))));
4490 // (X ^ (X - 1)) is a Mask
4491 else
4492 return match(V, P: m_c_Xor(L: m_Value(V&: X), R: m_Add(L: m_Deferred(V: X), R: m_AllOnes())));
4493 case Instruction::Select:
4494 // c ? Mask0 : Mask1 is a Mask.
4495 return isMaskOrZero(V: I->getOperand(i: 1), Not, Q, Depth) &&
4496 isMaskOrZero(V: I->getOperand(i: 2), Not, Q, Depth);
4497 case Instruction::Shl:
4498 // (~Mask) << X is a ~Mask.
4499 return Not && isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4500 case Instruction::LShr:
4501 // Mask >> X is a Mask.
4502 return !Not && isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4503 case Instruction::AShr:
4504 // Mask s>> X is a Mask.
4505 // ~Mask s>> X is a ~Mask.
4506 return isMaskOrZero(V: I->getOperand(i: 0), Not, Q, Depth);
4507 case Instruction::Add:
4508 // Pow2 - 1 is a Mask.
4509 if (!Not && match(V: I->getOperand(i: 1), P: m_AllOnes()))
4510 return isKnownToBeAPowerOfTwo(V: I->getOperand(i: 0), DL: Q.DL, /*OrZero*/ true,
4511 AC: Q.AC, CxtI: Q.CxtI, DT: Q.DT, UseInstrInfo: Depth);
4512 break;
4513 case Instruction::Sub:
4514 // -Pow2 is a ~Mask.
4515 if (Not && match(V: I->getOperand(i: 0), P: m_Zero()))
4516 return isKnownToBeAPowerOfTwo(V: I->getOperand(i: 1), DL: Q.DL, /*OrZero*/ true,
4517 AC: Q.AC, CxtI: Q.CxtI, DT: Q.DT, UseInstrInfo: Depth);
4518 break;
4519 case Instruction::Call: {
4520 if (auto *II = dyn_cast<IntrinsicInst>(Val: I)) {
4521 switch (II->getIntrinsicID()) {
4522 // min/max(Mask0, Mask1) is a Mask.
4523 // min/max(~Mask0, ~Mask1) is a ~Mask.
4524 case Intrinsic::umax:
4525 case Intrinsic::smax:
4526 case Intrinsic::umin:
4527 case Intrinsic::smin:
4528 return isMaskOrZero(V: II->getArgOperand(i: 1), Not, Q, Depth) &&
4529 isMaskOrZero(V: II->getArgOperand(i: 0), Not, Q, Depth);
4530
4531 // In the context of masks, bitreverse(Mask) == ~Mask
4532 case Intrinsic::bitreverse:
4533 return isMaskOrZero(V: II->getArgOperand(i: 0), Not: !Not, Q, Depth);
4534 default:
4535 break;
4536 }
4537 }
4538 break;
4539 }
4540 default:
4541 break;
4542 }
4543 return false;
4544}
4545
4546/// Some comparisons can be simplified.
4547/// In this case, we are looking for comparisons that look like
4548/// a check for a lossy truncation.
4549/// Folds:
4550/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4551/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4552/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4553/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4554/// Where Mask is some pattern that produces all-ones in low bits:
4555/// (-1 >> y)
4556/// ((-1 << y) >> y) <- non-canonical, has extra uses
4557/// ~(-1 << y)
4558/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4559/// The Mask can be a constant, too.
4560/// For some predicates, the operands are commutative.
4561/// For others, x can only be on a specific side.
4562static Value *foldICmpWithLowBitMaskedVal(CmpPredicate Pred, Value *Op0,
4563 Value *Op1, const SimplifyQuery &Q,
4564 InstCombiner &IC) {
4565
4566 ICmpInst::Predicate DstPred;
4567 switch (Pred) {
4568 case ICmpInst::Predicate::ICMP_EQ:
4569 // x & Mask == x
4570 // x & ~Mask == 0
4571 // ~x | Mask == -1
4572 // -> x u<= Mask
4573 // x & ~Mask == ~Mask
4574 // -> ~Mask u<= x
4575 DstPred = ICmpInst::Predicate::ICMP_ULE;
4576 break;
4577 case ICmpInst::Predicate::ICMP_NE:
4578 // x & Mask != x
4579 // x & ~Mask != 0
4580 // ~x | Mask != -1
4581 // -> x u> Mask
4582 // x & ~Mask != ~Mask
4583 // -> ~Mask u> x
4584 DstPred = ICmpInst::Predicate::ICMP_UGT;
4585 break;
4586 case ICmpInst::Predicate::ICMP_ULT:
4587 // x & Mask u< x
4588 // -> x u> Mask
4589 // x & ~Mask u< ~Mask
4590 // -> ~Mask u> x
4591 DstPred = ICmpInst::Predicate::ICMP_UGT;
4592 break;
4593 case ICmpInst::Predicate::ICMP_UGE:
4594 // x & Mask u>= x
4595 // -> x u<= Mask
4596 // x & ~Mask u>= ~Mask
4597 // -> ~Mask u<= x
4598 DstPred = ICmpInst::Predicate::ICMP_ULE;
4599 break;
4600 case ICmpInst::Predicate::ICMP_SLT:
4601 // x & Mask s< x [iff Mask s>= 0]
4602 // -> x s> Mask
4603 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4604 // -> ~Mask s> x
4605 DstPred = ICmpInst::Predicate::ICMP_SGT;
4606 break;
4607 case ICmpInst::Predicate::ICMP_SGE:
4608 // x & Mask s>= x [iff Mask s>= 0]
4609 // -> x s<= Mask
4610 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4611 // -> ~Mask s<= x
4612 DstPred = ICmpInst::Predicate::ICMP_SLE;
4613 break;
4614 default:
4615 // We don't support sgt,sle
4616 // ult/ugt are simplified to true/false respectively.
4617 return nullptr;
4618 }
4619
4620 Value *X, *M;
4621 // Put search code in lambda for early positive returns.
4622 auto IsLowBitMask = [&]() {
4623 if (match(V: Op0, P: m_c_And(L: m_Specific(V: Op1), R: m_Value(V&: M)))) {
4624 X = Op1;
4625 // Look for: x & Mask pred x
4626 if (isMaskOrZero(V: M, /*Not=*/false, Q)) {
4627 return !ICmpInst::isSigned(Pred) ||
4628 (match(V: M, P: m_NonNegative()) || isKnownNonNegative(V: M, SQ: Q));
4629 }
4630
4631 // Look for: x & ~Mask pred ~Mask
4632 if (isMaskOrZero(V: X, /*Not=*/true, Q)) {
4633 return !ICmpInst::isSigned(Pred) || isKnownNonZero(V: X, Q);
4634 }
4635 return false;
4636 }
4637 if (ICmpInst::isEquality(P: Pred) && match(V: Op1, P: m_AllOnes()) &&
4638 match(V: Op0, P: m_OneUse(SubPattern: m_Or(L: m_Value(V&: X), R: m_Value(V&: M))))) {
4639
4640 auto Check = [&]() {
4641 // Look for: ~x | Mask == -1
4642 if (isMaskOrZero(V: M, /*Not=*/false, Q)) {
4643 if (Value *NotX =
4644 IC.getFreelyInverted(V: X, WillInvertAllUses: X->hasOneUse(), Builder: &IC.Builder)) {
4645 X = NotX;
4646 return true;
4647 }
4648 }
4649 return false;
4650 };
4651 if (Check())
4652 return true;
4653 std::swap(a&: X, b&: M);
4654 return Check();
4655 }
4656 if (ICmpInst::isEquality(P: Pred) && match(V: Op1, P: m_Zero()) &&
4657 match(V: Op0, P: m_OneUse(SubPattern: m_And(L: m_Value(V&: X), R: m_Value(V&: M))))) {
4658 auto Check = [&]() {
4659 // Look for: x & ~Mask == 0
4660 if (isMaskOrZero(V: M, /*Not=*/true, Q)) {
4661 if (Value *NotM =
4662 IC.getFreelyInverted(V: M, WillInvertAllUses: M->hasOneUse(), Builder: &IC.Builder)) {
4663 M = NotM;
4664 return true;
4665 }
4666 }
4667 return false;
4668 };
4669 if (Check())
4670 return true;
4671 std::swap(a&: X, b&: M);
4672 return Check();
4673 }
4674 return false;
4675 };
4676
4677 if (!IsLowBitMask())
4678 return nullptr;
4679
4680 return IC.Builder.CreateICmp(P: DstPred, LHS: X, RHS: M);
4681}
4682
4683/// Some comparisons can be simplified.
4684/// In this case, we are looking for comparisons that look like
4685/// a check for a lossy signed truncation.
4686/// Folds: (MaskedBits is a constant.)
4687/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4688/// Into:
4689/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4690/// Where KeptBits = bitwidth(%x) - MaskedBits
4691static Value *
4692foldICmpWithTruncSignExtendedVal(ICmpInst &I,
4693 InstCombiner::BuilderTy &Builder) {
4694 CmpPredicate SrcPred;
4695 Value *X;
4696 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4697 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4698 if (!match(V: &I, P: m_c_ICmp(Pred&: SrcPred,
4699 L: m_OneUse(SubPattern: m_AShr(L: m_Shl(L: m_Value(V&: X), R: m_APInt(Res&: C0)),
4700 R: m_APInt(Res&: C1))),
4701 R: m_Deferred(V: X))))
4702 return nullptr;
4703
4704 // Potential handling of non-splats: for each element:
4705 // * if both are undef, replace with constant 0.
4706 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4707 // * if both are not undef, and are different, bailout.
4708 // * else, only one is undef, then pick the non-undef one.
4709
4710 // The shift amount must be equal.
4711 if (*C0 != *C1)
4712 return nullptr;
4713 const APInt &MaskedBits = *C0;
4714 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4715
4716 ICmpInst::Predicate DstPred;
4717 switch (SrcPred) {
4718 case ICmpInst::Predicate::ICMP_EQ:
4719 // ((%x << MaskedBits) a>> MaskedBits) == %x
4720 // =>
4721 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4722 DstPred = ICmpInst::Predicate::ICMP_ULT;
4723 break;
4724 case ICmpInst::Predicate::ICMP_NE:
4725 // ((%x << MaskedBits) a>> MaskedBits) != %x
4726 // =>
4727 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4728 DstPred = ICmpInst::Predicate::ICMP_UGE;
4729 break;
4730 // FIXME: are more folds possible?
4731 default:
4732 return nullptr;
4733 }
4734
4735 auto *XType = X->getType();
4736 const unsigned XBitWidth = XType->getScalarSizeInBits();
4737 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4738 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4739
4740 // KeptBits = bitwidth(%x) - MaskedBits
4741 const APInt KeptBits = BitWidth - MaskedBits;
4742 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4743 // ICmpCst = (1 << KeptBits)
4744 const APInt ICmpCst = APInt(XBitWidth, 1).shl(ShiftAmt: KeptBits);
4745 assert(ICmpCst.isPowerOf2());
4746 // AddCst = (1 << (KeptBits-1))
4747 const APInt AddCst = ICmpCst.lshr(shiftAmt: 1);
4748 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4749
4750 // T0 = add %x, AddCst
4751 Value *T0 = Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty: XType, V: AddCst));
4752 // T1 = T0 DstPred ICmpCst
4753 Value *T1 = Builder.CreateICmp(P: DstPred, LHS: T0, RHS: ConstantInt::get(Ty: XType, V: ICmpCst));
4754
4755 return T1;
4756}
4757
4758// Given pattern:
4759// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4760// we should move shifts to the same hand of 'and', i.e. rewrite as
4761// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4762// We are only interested in opposite logical shifts here.
4763// One of the shifts can be truncated.
4764// If we can, we want to end up creating 'lshr' shift.
4765static Value *
4766foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ,
4767 InstCombiner::BuilderTy &Builder) {
4768 if (!I.isEquality() || !match(V: I.getOperand(i_nocapture: 1), P: m_Zero()) ||
4769 !I.getOperand(i_nocapture: 0)->hasOneUse())
4770 return nullptr;
4771
4772 auto m_AnyLogicalShift = m_LogicalShift(L: m_Value(), R: m_Value());
4773
4774 // Look for an 'and' of two logical shifts, one of which may be truncated.
4775 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4776 Instruction *XShift, *MaybeTruncation, *YShift;
4777 if (!match(
4778 V: I.getOperand(i_nocapture: 0),
4779 P: m_c_And(L: m_CombineAnd(Ps: m_AnyLogicalShift, Ps: m_Instruction(I&: XShift)),
4780 R: m_CombineAnd(Ps: m_TruncOrSelf(Op: m_CombineAnd(
4781 Ps: m_AnyLogicalShift, Ps: m_Instruction(I&: YShift))),
4782 Ps: m_Instruction(I&: MaybeTruncation)))))
4783 return nullptr;
4784
4785 // We potentially looked past 'trunc', but only when matching YShift,
4786 // therefore YShift must have the widest type.
4787 Instruction *WidestShift = YShift;
4788 // Therefore XShift must have the shallowest type.
4789 // Or they both have identical types if there was no truncation.
4790 Instruction *NarrowestShift = XShift;
4791
4792 Type *WidestTy = WidestShift->getType();
4793 Type *NarrowestTy = NarrowestShift->getType();
4794 assert(NarrowestTy == I.getOperand(0)->getType() &&
4795 "We did not look past any shifts while matching XShift though.");
4796 bool HadTrunc = WidestTy != I.getOperand(i_nocapture: 0)->getType();
4797
4798 // If YShift is a 'lshr', swap the shifts around.
4799 if (match(V: YShift, P: m_LShr(L: m_Value(), R: m_Value())))
4800 std::swap(a&: XShift, b&: YShift);
4801
4802 // The shifts must be in opposite directions.
4803 auto XShiftOpcode = XShift->getOpcode();
4804 if (XShiftOpcode == YShift->getOpcode())
4805 return nullptr; // Do not care about same-direction shifts here.
4806
4807 Value *X, *XShAmt, *Y, *YShAmt;
4808 match(V: XShift, P: m_BinOp(L: m_Value(V&: X), R: m_ZExtOrSelf(Op: m_Value(V&: XShAmt))));
4809 match(V: YShift, P: m_BinOp(L: m_Value(V&: Y), R: m_ZExtOrSelf(Op: m_Value(V&: YShAmt))));
4810
4811 // If one of the values being shifted is a constant, then we will end with
4812 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4813 // however, we will need to ensure that we won't increase instruction count.
4814 if (!isa<Constant>(Val: X) && !isa<Constant>(Val: Y)) {
4815 // At least one of the hands of the 'and' should be one-use shift.
4816 if (!match(V: I.getOperand(i_nocapture: 0),
4817 P: m_c_And(L: m_OneUse(SubPattern: m_AnyLogicalShift), R: m_Value())))
4818 return nullptr;
4819 if (HadTrunc) {
4820 // Due to the 'trunc', we will need to widen X. For that either the old
4821 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4822 if (!MaybeTruncation->hasOneUse() &&
4823 !NarrowestShift->getOperand(i: 1)->hasOneUse())
4824 return nullptr;
4825 }
4826 }
4827
4828 // We have two shift amounts from two different shifts. The types of those
4829 // shift amounts may not match. If that's the case let's bailout now.
4830 if (XShAmt->getType() != YShAmt->getType())
4831 return nullptr;
4832
4833 // As input, we have the following pattern:
4834 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4835 // We want to rewrite that as:
4836 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4837 // While we know that originally (Q+K) would not overflow
4838 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4839 // shift amounts. so it may now overflow in smaller bitwidth.
4840 // To ensure that does not happen, we need to ensure that the total maximal
4841 // shift amount is still representable in that smaller bit width.
4842 unsigned MaximalPossibleTotalShiftAmount =
4843 (WidestTy->getScalarSizeInBits() - 1) +
4844 (NarrowestTy->getScalarSizeInBits() - 1);
4845 APInt MaximalRepresentableShiftAmount =
4846 APInt::getAllOnes(numBits: XShAmt->getType()->getScalarSizeInBits());
4847 if (MaximalRepresentableShiftAmount.ult(RHS: MaximalPossibleTotalShiftAmount))
4848 return nullptr;
4849
4850 // Can we fold (XShAmt+YShAmt) ?
4851 auto *NewShAmt = dyn_cast_or_null<Constant>(
4852 Val: simplifyAddInst(LHS: XShAmt, RHS: YShAmt, /*isNSW=*/IsNSW: false,
4853 /*isNUW=*/IsNUW: false, Q: SQ.getWithInstruction(I: &I)));
4854 if (!NewShAmt)
4855 return nullptr;
4856 if (NewShAmt->getType() != WidestTy) {
4857 NewShAmt =
4858 ConstantFoldCastOperand(Opcode: Instruction::ZExt, C: NewShAmt, DestTy: WidestTy, DL: SQ.DL);
4859 if (!NewShAmt)
4860 return nullptr;
4861 }
4862 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4863
4864 // Is the new shift amount smaller than the bit width?
4865 // FIXME: could also rely on ConstantRange.
4866 if (!match(V: NewShAmt,
4867 P: m_SpecificInt_ICMP(Predicate: ICmpInst::Predicate::ICMP_ULT,
4868 Threshold: APInt(WidestBitWidth, WidestBitWidth))))
4869 return nullptr;
4870
4871 // An extra legality check is needed if we had trunc-of-lshr.
4872 if (HadTrunc && match(V: WidestShift, P: m_LShr(L: m_Value(), R: m_Value()))) {
4873 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4874 WidestShift]() {
4875 // It isn't obvious whether it's worth it to analyze non-constants here.
4876 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4877 // If *any* of these preconditions matches we can perform the fold.
4878 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4879 ? NewShAmt->getSplatValue()
4880 : NewShAmt;
4881 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4882 if (NewShAmtSplat &&
4883 (NewShAmtSplat->isNullValue() ||
4884 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4885 return true;
4886 // We consider *min* leading zeros so a single outlier
4887 // blocks the transform as opposed to allowing it.
4888 if (auto *C = dyn_cast<Constant>(Val: NarrowestShift->getOperand(i: 0))) {
4889 KnownBits Known = computeKnownBits(V: C, DL: SQ.DL);
4890 unsigned MinLeadZero = Known.countMinLeadingZeros();
4891 // If the value being shifted has at most lowest bit set we can fold.
4892 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4893 if (MaxActiveBits <= 1)
4894 return true;
4895 // Precondition: NewShAmt u<= countLeadingZeros(C)
4896 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(RHS: MinLeadZero))
4897 return true;
4898 }
4899 if (auto *C = dyn_cast<Constant>(Val: WidestShift->getOperand(i: 0))) {
4900 KnownBits Known = computeKnownBits(V: C, DL: SQ.DL);
4901 unsigned MinLeadZero = Known.countMinLeadingZeros();
4902 // If the value being shifted has at most lowest bit set we can fold.
4903 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4904 if (MaxActiveBits <= 1)
4905 return true;
4906 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4907 if (NewShAmtSplat) {
4908 APInt AdjNewShAmt =
4909 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4910 if (AdjNewShAmt.ule(RHS: MinLeadZero))
4911 return true;
4912 }
4913 }
4914 return false; // Can't tell if it's ok.
4915 };
4916 if (!CanFold())
4917 return nullptr;
4918 }
4919
4920 // All good, we can do this fold.
4921 X = Builder.CreateZExt(V: X, DestTy: WidestTy);
4922 Y = Builder.CreateZExt(V: Y, DestTy: WidestTy);
4923 // The shift is the same that was for X.
4924 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4925 ? Builder.CreateLShr(LHS: X, RHS: NewShAmt)
4926 : Builder.CreateShl(LHS: X, RHS: NewShAmt);
4927 Value *T1 = Builder.CreateAnd(LHS: T0, RHS: Y);
4928 return Builder.CreateICmp(P: I.getPredicate(), LHS: T1,
4929 RHS: Constant::getNullValue(Ty: WidestTy));
4930}
4931
4932/// Fold
4933/// (-1 u/ x) u< y
4934/// ((x * y) ?/ x) != y
4935/// to
4936/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4937/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4938/// will mean that we are looking for the opposite answer.
4939Value *InstCombinerImpl::foldMultiplicationOverflowCheck(ICmpInst &I) {
4940 CmpPredicate Pred;
4941 Value *X, *Y;
4942 Instruction *Mul;
4943 Instruction *Div;
4944 bool NeedNegation;
4945 // Look for: (-1 u/ x) u</u>= y
4946 if (!I.isEquality() &&
4947 match(V: &I, P: m_c_ICmp(Pred,
4948 L: m_CombineAnd(Ps: m_OneUse(SubPattern: m_UDiv(L: m_AllOnes(), R: m_Value(V&: X))),
4949 Ps: m_Instruction(I&: Div)),
4950 R: m_Value(V&: Y)))) {
4951 Mul = nullptr;
4952
4953 // Are we checking that overflow does not happen, or does happen?
4954 switch (Pred) {
4955 case ICmpInst::Predicate::ICMP_ULT:
4956 NeedNegation = false;
4957 break; // OK
4958 case ICmpInst::Predicate::ICMP_UGE:
4959 NeedNegation = true;
4960 break; // OK
4961 default:
4962 return nullptr; // Wrong predicate.
4963 }
4964 } else // Look for: ((x * y) / x) !=/== y
4965 if (I.isEquality() &&
4966 match(V: &I, P: m_c_ICmp(Pred, L: m_Value(V&: Y),
4967 R: m_CombineAnd(Ps: m_OneUse(SubPattern: m_IDiv(
4968 L: m_CombineAnd(Ps: m_c_Mul(L: m_Deferred(V: Y),
4969 R: m_Value(V&: X)),
4970 Ps: m_Instruction(I&: Mul)),
4971 R: m_Deferred(V: X))),
4972 Ps: m_Instruction(I&: Div))))) {
4973 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4974 } else
4975 return nullptr;
4976
4977 BuilderTy::InsertPointGuard Guard(Builder);
4978 // If the pattern included (x * y), we'll want to insert new instructions
4979 // right before that original multiplication so that we can replace it.
4980 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4981 if (MulHadOtherUses)
4982 Builder.SetInsertPoint(Mul);
4983
4984 Value *Call = Builder.CreateIntrinsic(
4985 ID: Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow
4986 : Intrinsic::smul_with_overflow,
4987 OverloadTypes: X->getType(), Args: {X, Y}, /*FMFSource=*/nullptr, Name: "mul");
4988
4989 // If the multiplication was used elsewhere, to ensure that we don't leave
4990 // "duplicate" instructions, replace uses of that original multiplication
4991 // with the multiplication result from the with.overflow intrinsic.
4992 if (MulHadOtherUses)
4993 replaceInstUsesWith(I&: *Mul, V: Builder.CreateExtractValue(Agg: Call, Idxs: 0, Name: "mul.val"));
4994
4995 Value *Res = Builder.CreateExtractValue(Agg: Call, Idxs: 1, Name: "mul.ov");
4996 if (NeedNegation) // This technically increases instruction count.
4997 Res = Builder.CreateNot(V: Res, Name: "mul.not.ov");
4998
4999 // If we replaced the mul, erase it. Do this after all uses of Builder,
5000 // as the mul is used as insertion point.
5001 if (MulHadOtherUses)
5002 eraseInstFromFunction(I&: *Mul);
5003
5004 return Res;
5005}
5006
5007static Instruction *foldICmpXNegX(ICmpInst &I,
5008 InstCombiner::BuilderTy &Builder) {
5009 CmpPredicate Pred;
5010 Value *X;
5011 if (match(V: &I, P: m_c_ICmp(Pred, L: m_NSWNeg(V: m_Value(V&: X)), R: m_Deferred(V: X)))) {
5012
5013 if (ICmpInst::isSigned(Pred))
5014 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5015 else if (ICmpInst::isUnsigned(Pred))
5016 Pred = ICmpInst::getSignedPredicate(Pred);
5017 // else for equality-comparisons just keep the predicate.
5018
5019 return ICmpInst::Create(Op: Instruction::ICmp, Pred, S1: X,
5020 S2: Constant::getNullValue(Ty: X->getType()), Name: I.getName());
5021 }
5022
5023 // A value is not equal to its negation unless that value is 0 or
5024 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
5025 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))) &&
5026 ICmpInst::isEquality(P: Pred)) {
5027 Type *Ty = X->getType();
5028 uint32_t BitWidth = Ty->getScalarSizeInBits();
5029 Constant *MaxSignedVal =
5030 ConstantInt::get(Ty, V: APInt::getSignedMaxValue(numBits: BitWidth));
5031 Value *And = Builder.CreateAnd(LHS: X, RHS: MaxSignedVal);
5032 Constant *Zero = Constant::getNullValue(Ty);
5033 return CmpInst::Create(Op: Instruction::ICmp, Pred, S1: And, S2: Zero);
5034 }
5035
5036 return nullptr;
5037}
5038
5039static Instruction *foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q,
5040 InstCombinerImpl &IC) {
5041 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1), *A;
5042 // Normalize and operand as operand 0.
5043 CmpInst::Predicate Pred = I.getPredicate();
5044 if (match(V: Op1, P: m_c_And(L: m_Specific(V: Op0), R: m_Value()))) {
5045 std::swap(a&: Op0, b&: Op1);
5046 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5047 }
5048
5049 if (!match(V: Op0, P: m_c_And(L: m_Specific(V: Op1), R: m_Value(V&: A))))
5050 return nullptr;
5051
5052 // (icmp (X & Y) u< X --> (X & Y) != X
5053 if (Pred == ICmpInst::ICMP_ULT)
5054 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5055
5056 // (icmp (X & Y) u>= X --> (X & Y) == X
5057 if (Pred == ICmpInst::ICMP_UGE)
5058 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5059
5060 if (ICmpInst::isEquality(P: Pred) && Op0->hasOneUse()) {
5061 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
5062 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
5063 // so don't do this fold.
5064 if (!match(V: Op1, P: m_ImmConstant()))
5065 if (auto *NotOp1 =
5066 IC.getFreelyInverted(V: Op1, WillInvertAllUses: !Op1->hasNUsesOrMore(N: 3), Builder: &IC.Builder))
5067 return new ICmpInst(Pred, IC.Builder.CreateOr(LHS: A, RHS: NotOp1),
5068 Constant::getAllOnesValue(Ty: Op1->getType()));
5069 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
5070 if (auto *NotA = IC.getFreelyInverted(V: A, WillInvertAllUses: A->hasOneUse(), Builder: &IC.Builder))
5071 return new ICmpInst(Pred, IC.Builder.CreateAnd(LHS: Op1, RHS: NotA),
5072 Constant::getNullValue(Ty: Op1->getType()));
5073 }
5074
5075 if (!ICmpInst::isSigned(Pred))
5076 return nullptr;
5077
5078 KnownBits KnownY = IC.computeKnownBits(V: A, CxtI: &I);
5079 // (X & NegY) spred X --> (X & NegY) upred X
5080 if (KnownY.isNegative())
5081 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
5082
5083 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
5084 return nullptr;
5085
5086 if (KnownY.isNonNegative())
5087 // (X & PosY) s<= X --> X s>= 0
5088 // (X & PosY) s> X --> X s< 0
5089 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
5090 Constant::getNullValue(Ty: Op1->getType()));
5091
5092 if (isKnownNegative(V: Op1, SQ: IC.getSimplifyQuery().getWithInstruction(I: &I)))
5093 // (NegX & Y) s<= NegX --> Y s< 0
5094 // (NegX & Y) s> NegX --> Y s>= 0
5095 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), A,
5096 Constant::getNullValue(Ty: A->getType()));
5097
5098 return nullptr;
5099}
5100
5101static Instruction *foldICmpOrXX(ICmpInst &I, const SimplifyQuery &Q,
5102 InstCombinerImpl &IC) {
5103 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1), *A;
5104
5105 // Normalize or operand as operand 0.
5106 CmpInst::Predicate Pred = I.getPredicate();
5107 if (match(V: Op1, P: m_c_Or(L: m_Specific(V: Op0), R: m_Value(V&: A)))) {
5108 std::swap(a&: Op0, b&: Op1);
5109 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5110 } else if (!match(V: Op0, P: m_c_Or(L: m_Specific(V: Op1), R: m_Value(V&: A)))) {
5111 return nullptr;
5112 }
5113
5114 // icmp (X | Y) u<= X --> (X | Y) == X
5115 if (Pred == ICmpInst::ICMP_ULE)
5116 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5117
5118 // icmp (X | Y) u> X --> (X | Y) != X
5119 if (Pred == ICmpInst::ICMP_UGT)
5120 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5121
5122 if (ICmpInst::isEquality(P: Pred) && Op0->hasOneUse()) {
5123 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
5124 if (Value *NotOp1 = IC.getFreelyInverted(
5125 V: Op1, WillInvertAllUses: !isa<Constant>(Val: Op1) && !Op1->hasNUsesOrMore(N: 3), Builder: &IC.Builder))
5126 return new ICmpInst(Pred, IC.Builder.CreateAnd(LHS: A, RHS: NotOp1),
5127 Constant::getNullValue(Ty: Op1->getType()));
5128 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
5129 if (Value *NotA = IC.getFreelyInverted(V: A, WillInvertAllUses: A->hasOneUse(), Builder: &IC.Builder))
5130 return new ICmpInst(Pred, IC.Builder.CreateOr(LHS: Op1, RHS: NotA),
5131 Constant::getAllOnesValue(Ty: Op1->getType()));
5132 }
5133 return nullptr;
5134}
5135
5136static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q,
5137 InstCombinerImpl &IC) {
5138 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1), *A;
5139 // Normalize xor operand as operand 0.
5140 CmpInst::Predicate Pred = I.getPredicate();
5141 if (match(V: Op1, P: m_c_Xor(L: m_Specific(V: Op0), R: m_Value()))) {
5142 std::swap(a&: Op0, b&: Op1);
5143 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
5144 }
5145 if (!match(V: Op0, P: m_c_Xor(L: m_Specific(V: Op1), R: m_Value(V&: A))))
5146 return nullptr;
5147
5148 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
5149 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
5150 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
5151 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
5152 CmpInst::Predicate PredOut = CmpInst::getStrictPredicate(pred: Pred);
5153 if (PredOut != Pred && isKnownNonZero(V: A, Q))
5154 return new ICmpInst(PredOut, Op0, Op1);
5155
5156 // These transform work when A is negative.
5157 // X s< X^A, X s<= X^A, X u> X^A, X u>= X^A --> X s< 0
5158 // X s> X^A, X s>= X^A, X u< X^A, X u<= X^A --> X s>= 0
5159 if (match(V: A, P: m_Negative())) {
5160 CmpInst::Predicate NewPred;
5161 switch (ICmpInst::getStrictPredicate(pred: Pred)) {
5162 default:
5163 return nullptr;
5164 case ICmpInst::ICMP_SLT:
5165 case ICmpInst::ICMP_UGT:
5166 NewPred = ICmpInst::ICMP_SLT;
5167 break;
5168 case ICmpInst::ICMP_SGT:
5169 case ICmpInst::ICMP_ULT:
5170 NewPred = ICmpInst::ICMP_SGE;
5171 break;
5172 }
5173 Constant *Const = Constant::getNullValue(Ty: Op0->getType());
5174 return new ICmpInst(NewPred, Op0, Const);
5175 }
5176
5177 return nullptr;
5178}
5179
5180/// Return true if X is a multiple of C.
5181/// TODO: Handle non-power-of-2 factors.
5182static bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q) {
5183 if (C.isOne())
5184 return true;
5185
5186 if (!C.isPowerOf2())
5187 return false;
5188
5189 return MaskedValueIsZero(V: X, Mask: C - 1, SQ: Q);
5190}
5191
5192/// Try to fold icmp (binop), X or icmp X, (binop).
5193/// TODO: A large part of this logic is duplicated in InstSimplify's
5194/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
5195/// duplication.
5196Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
5197 const SimplifyQuery &SQ) {
5198 const SimplifyQuery Q = SQ.getWithInstruction(I: &I);
5199 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
5200
5201 // Special logic for binary operators.
5202 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Val: Op0);
5203 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Val: Op1);
5204 if (!BO0 && !BO1)
5205 return nullptr;
5206
5207 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
5208 return NewICmp;
5209
5210 const CmpInst::Predicate Pred = I.getPredicate();
5211 Value *X;
5212
5213 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
5214 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
5215 if (match(V: Op0, P: m_OneUse(SubPattern: m_c_Add(L: m_Specific(V: Op1), R: m_Value(V&: X)))) &&
5216 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5217 return new ICmpInst(Pred, Builder.CreateNot(V: Op1), X);
5218 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
5219 if (match(V: Op1, P: m_OneUse(SubPattern: m_c_Add(L: m_Specific(V: Op0), R: m_Value(V&: X)))) &&
5220 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5221 return new ICmpInst(Pred, X, Builder.CreateNot(V: Op0));
5222
5223 {
5224 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
5225 Constant *C;
5226 if (match(V: Op0, P: m_OneUse(SubPattern: m_Add(L: m_c_Add(L: m_Specific(V: Op1), R: m_Value(V&: X)),
5227 R: m_ImmConstant(C)))) &&
5228 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
5229 Constant *C2 = ConstantExpr::getNot(C);
5230 return new ICmpInst(Pred, Builder.CreateSub(LHS: C2, RHS: X), Op1);
5231 }
5232 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5233 if (match(V: Op1, P: m_OneUse(SubPattern: m_Add(L: m_c_Add(L: m_Specific(V: Op0), R: m_Value(V&: X)),
5234 R: m_ImmConstant(C)))) &&
5235 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5236 Constant *C2 = ConstantExpr::getNot(C);
5237 return new ICmpInst(Pred, Op0, Builder.CreateSub(LHS: C2, RHS: X));
5238 }
5239 }
5240
5241 // (icmp eq/ne (X, -P2), INT_MIN)
5242 // -> (icmp slt/sge X, INT_MIN + P2)
5243 if (ICmpInst::isEquality(P: Pred) && BO0 &&
5244 match(V: I.getOperand(i_nocapture: 1), P: m_SignMask()) &&
5245 match(V: BO0, P: m_And(L: m_Value(), R: m_NegatedPower2OrZero()))) {
5246 // Will Constant fold.
5247 Value *NewC = Builder.CreateSub(LHS: I.getOperand(i_nocapture: 1), RHS: BO0->getOperand(i_nocapture: 1));
5248 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5249 : ICmpInst::ICMP_SGE,
5250 BO0->getOperand(i_nocapture: 0), NewC);
5251 }
5252
5253 {
5254 // Similar to above: an unsigned overflow comparison may use offset + mask:
5255 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5256 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5257 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5258 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5259 BinaryOperator *BO;
5260 const APInt *C;
5261 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5262 match(V: Op0, P: m_And(L: m_BinOp(I&: BO), R: m_LowBitMask(V&: C))) &&
5263 match(V: BO, P: m_Add(L: m_Specific(V: Op1), R: m_SpecificIntAllowPoison(V: *C)))) {
5264 CmpInst::Predicate NewPred =
5265 Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
5266 Constant *Zero = ConstantInt::getNullValue(Ty: Op1->getType());
5267 return new ICmpInst(NewPred, Op1, Zero);
5268 }
5269
5270 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5271 match(V: Op1, P: m_And(L: m_BinOp(I&: BO), R: m_LowBitMask(V&: C))) &&
5272 match(V: BO, P: m_Add(L: m_Specific(V: Op0), R: m_SpecificIntAllowPoison(V: *C)))) {
5273 CmpInst::Predicate NewPred =
5274 Pred == ICmpInst::ICMP_UGT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
5275 Constant *Zero = ConstantInt::getNullValue(Ty: Op1->getType());
5276 return new ICmpInst(NewPred, Op0, Zero);
5277 }
5278 }
5279
5280 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5281 bool Op0HasNUW = false, Op1HasNUW = false;
5282 bool Op0HasNSW = false, Op1HasNSW = false;
5283 // Analyze the case when either Op0 or Op1 is an add instruction.
5284 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5285 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5286 bool &HasNSW, bool &HasNUW) -> bool {
5287 if (isa<OverflowingBinaryOperator>(Val: BO)) {
5288 HasNUW = BO.hasNoUnsignedWrap();
5289 HasNSW = BO.hasNoSignedWrap();
5290 return ICmpInst::isEquality(P: Pred) ||
5291 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5292 (CmpInst::isSigned(Pred) && HasNSW);
5293 } else if (BO.getOpcode() == Instruction::Or) {
5294 HasNUW = true;
5295 HasNSW = true;
5296 return true;
5297 } else {
5298 return false;
5299 }
5300 };
5301 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5302
5303 if (BO0) {
5304 match(V: BO0, P: m_AddLike(L: m_Value(V&: A), R: m_Value(V&: B)));
5305 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5306 }
5307 if (BO1) {
5308 match(V: BO1, P: m_AddLike(L: m_Value(V&: C), R: m_Value(V&: D)));
5309 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5310 }
5311
5312 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5313 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5314 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5315 return new ICmpInst(Pred, A == Op1 ? B : A,
5316 Constant::getNullValue(Ty: Op1->getType()));
5317
5318 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5319 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5320 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5321 return new ICmpInst(Pred, Constant::getNullValue(Ty: Op0->getType()),
5322 C == Op0 ? D : C);
5323
5324 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5325 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5326 NoOp1WrapProblem) {
5327 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5328 Value *Y, *Z;
5329 if (A == C) {
5330 // C + B == C + D -> B == D
5331 Y = B;
5332 Z = D;
5333 } else if (A == D) {
5334 // D + B == C + D -> B == C
5335 Y = B;
5336 Z = C;
5337 } else if (B == C) {
5338 // A + C == C + D -> A == D
5339 Y = A;
5340 Z = D;
5341 } else {
5342 assert(B == D);
5343 // A + D == C + D -> A == C
5344 Y = A;
5345 Z = C;
5346 }
5347 return new ICmpInst(Pred, Y, Z);
5348 }
5349
5350 if (ICmpInst::isRelational(P: Pred)) {
5351 // Return if both X and Y is divisible by Z/-Z.
5352 // TODO: Generalize to check if (X - Y) is divisible by Z/-Z.
5353 auto ShareCommonDivisor = [&Q](Value *X, Value *Y, Value *Z,
5354 bool IsNegative) -> bool {
5355 const APInt *OffsetC;
5356 if (!match(V: Z, P: m_APInt(Res&: OffsetC)))
5357 return false;
5358
5359 // Fast path for Z == 1/-1.
5360 if (IsNegative ? OffsetC->isAllOnes() : OffsetC->isOne())
5361 return true;
5362
5363 APInt C = *OffsetC;
5364 if (IsNegative)
5365 C.negate();
5366 // Note: -INT_MIN is also negative.
5367 if (!C.isStrictlyPositive())
5368 return false;
5369
5370 return isMultipleOf(X, C, Q) && isMultipleOf(X: Y, C, Q);
5371 };
5372
5373 // TODO: The subtraction-related identities shown below also hold, but
5374 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5375 // wouldn't happen even if they were implemented.
5376 //
5377 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5378 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5379 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5380 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5381
5382 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5383 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5384 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5385 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5386 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5387 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5388 if (A && NoOp0WrapProblem &&
5389 ShareCommonDivisor(A, Op1, B,
5390 ICmpInst::isLT(P: Pred) || ICmpInst::isGE(P: Pred)))
5391 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), A,
5392 Op1);
5393
5394 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5395 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5396 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5397 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5398 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5399 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5400 if (C && NoOp1WrapProblem &&
5401 ShareCommonDivisor(Op0, C, D,
5402 ICmpInst::isGT(P: Pred) || ICmpInst::isLE(P: Pred)))
5403 return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(pred: Pred), Op0,
5404 C);
5405 }
5406
5407 // if C1 has greater magnitude than C2:
5408 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5409 // s.t. C3 = C1 - C2
5410 //
5411 // if C2 has greater magnitude than C1:
5412 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5413 // s.t. C3 = C2 - C1
5414 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5415 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5416 const APInt *AP1, *AP2;
5417 // TODO: Support non-uniform vectors.
5418 // TODO: Allow poison passthrough if B or D's element is poison.
5419 if (match(V: B, P: m_APIntAllowPoison(Res&: AP1)) &&
5420 match(V: D, P: m_APIntAllowPoison(Res&: AP2)) &&
5421 AP1->isNegative() == AP2->isNegative()) {
5422 APInt AP1Abs = AP1->abs();
5423 APInt AP2Abs = AP2->abs();
5424 if (AP1Abs.uge(RHS: AP2Abs)) {
5425 APInt Diff = *AP1 - *AP2;
5426 Constant *C3 = Constant::getIntegerValue(Ty: BO0->getType(), V: Diff);
5427 Value *NewAdd = Builder.CreateAdd(
5428 LHS: A, RHS: C3, Name: "", HasNUW: Op0HasNUW && Diff.ule(RHS: *AP1), HasNSW: Op0HasNSW);
5429 return new ICmpInst(Pred, NewAdd, C);
5430 } else {
5431 APInt Diff = *AP2 - *AP1;
5432 Constant *C3 = Constant::getIntegerValue(Ty: BO0->getType(), V: Diff);
5433 Value *NewAdd = Builder.CreateAdd(
5434 LHS: C, RHS: C3, Name: "", HasNUW: Op1HasNUW && Diff.ule(RHS: *AP2), HasNSW: Op1HasNSW);
5435 return new ICmpInst(Pred, A, NewAdd);
5436 }
5437 }
5438 Constant *Cst1, *Cst2;
5439 if (match(V: B, P: m_ImmConstant(C&: Cst1)) && match(V: D, P: m_ImmConstant(C&: Cst2)) &&
5440 ICmpInst::isEquality(P: Pred)) {
5441 Constant *Diff = ConstantExpr::getSub(C1: Cst2, C2: Cst1);
5442 Value *NewAdd = Builder.CreateAdd(LHS: C, RHS: Diff);
5443 return new ICmpInst(Pred, A, NewAdd);
5444 }
5445 }
5446
5447 // Analyze the case when either Op0 or Op1 is a sub instruction.
5448 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5449 A = nullptr;
5450 B = nullptr;
5451 C = nullptr;
5452 D = nullptr;
5453 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5454 A = BO0->getOperand(i_nocapture: 0);
5455 B = BO0->getOperand(i_nocapture: 1);
5456 }
5457 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5458 C = BO1->getOperand(i_nocapture: 0);
5459 D = BO1->getOperand(i_nocapture: 1);
5460 }
5461
5462 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5463 if (A == Op1 && NoOp0WrapProblem)
5464 return new ICmpInst(Pred, Constant::getNullValue(Ty: Op1->getType()), B);
5465 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5466 if (C == Op0 && NoOp1WrapProblem)
5467 return new ICmpInst(Pred, D, Constant::getNullValue(Ty: Op0->getType()));
5468
5469 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5470 // (A - B) u>/u<= A --> B u>/u<= A
5471 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5472 return new ICmpInst(Pred, B, A);
5473 // C u</u>= (C - D) --> C u</u>= D
5474 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5475 return new ICmpInst(Pred, C, D);
5476 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5477 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5478 isKnownNonZero(V: B, Q))
5479 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(pred: Pred), B, A);
5480 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5481 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5482 isKnownNonZero(V: D, Q))
5483 return new ICmpInst(CmpInst::getFlippedStrictnessPredicate(pred: Pred), C, D);
5484
5485 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5486 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5487 return new ICmpInst(Pred, A, C);
5488
5489 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5490 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5491 return new ICmpInst(Pred, D, B);
5492
5493 // icmp (0-X) < cst --> x > -cst
5494 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5495 Value *X;
5496 if (match(V: BO0, P: m_Neg(V: m_Value(V&: X))))
5497 if (Constant *RHSC = dyn_cast<Constant>(Val: Op1))
5498 if (RHSC->isNotMinSignedValue())
5499 return new ICmpInst(I.getSwappedPredicate(), X,
5500 ConstantExpr::getNeg(C: RHSC));
5501 }
5502
5503 if (Instruction *R = foldICmpXorXX(I, Q, IC&: *this))
5504 return R;
5505 if (Instruction *R = foldICmpOrXX(I, Q, IC&: *this))
5506 return R;
5507
5508 {
5509 // Try to remove shared multiplier from comparison:
5510 // X * Z pred Y * Z
5511 Value *X, *Y, *Z;
5512 if ((match(V: Op0, P: m_Mul(L: m_Value(V&: X), R: m_Value(V&: Z))) &&
5513 match(V: Op1, P: m_c_Mul(L: m_Specific(V: Z), R: m_Value(V&: Y)))) ||
5514 (match(V: Op0, P: m_Mul(L: m_Value(V&: Z), R: m_Value(V&: X))) &&
5515 match(V: Op1, P: m_c_Mul(L: m_Specific(V: Z), R: m_Value(V&: Y))))) {
5516 if (ICmpInst::isSigned(Pred)) {
5517 if (Op0HasNSW && Op1HasNSW) {
5518 KnownBits ZKnown = computeKnownBits(V: Z, CxtI: &I);
5519 if (ZKnown.isStrictlyPositive())
5520 return new ICmpInst(Pred, X, Y);
5521 if (ZKnown.isNegative())
5522 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), X, Y);
5523 Value *LessThan = simplifyICmpInst(Pred: ICmpInst::ICMP_SLT, LHS: X, RHS: Y,
5524 Q: SQ.getWithInstruction(I: &I));
5525 if (LessThan && match(V: LessThan, P: m_One()))
5526 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Z,
5527 Constant::getNullValue(Ty: Z->getType()));
5528 Value *GreaterThan = simplifyICmpInst(Pred: ICmpInst::ICMP_SGT, LHS: X, RHS: Y,
5529 Q: SQ.getWithInstruction(I: &I));
5530 if (GreaterThan && match(V: GreaterThan, P: m_One()))
5531 return new ICmpInst(Pred, Z, Constant::getNullValue(Ty: Z->getType()));
5532 }
5533 } else {
5534 bool NonZero;
5535 if (ICmpInst::isEquality(P: Pred)) {
5536 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0
5537 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&
5538 isKnownNonEqual(V1: X, V2: Y, SQ))
5539 return new ICmpInst(Pred, Z, Constant::getNullValue(Ty: Z->getType()));
5540
5541 KnownBits ZKnown = computeKnownBits(V: Z, CxtI: &I);
5542 // if Z % 2 != 0
5543 // X * Z eq/ne Y * Z -> X eq/ne Y
5544 if (ZKnown.countMaxTrailingZeros() == 0)
5545 return new ICmpInst(Pred, X, Y);
5546 NonZero = !ZKnown.One.isZero() || isKnownNonZero(V: Z, Q);
5547 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5548 // X * Z eq/ne Y * Z -> X eq/ne Y
5549 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5550 return new ICmpInst(Pred, X, Y);
5551 } else
5552 NonZero = isKnownNonZero(V: Z, Q);
5553
5554 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5555 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5556 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5557 return new ICmpInst(Pred, X, Y);
5558 }
5559 }
5560 }
5561
5562 BinaryOperator *SRem = nullptr;
5563 // icmp (srem X, Y), Y
5564 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(i_nocapture: 1))
5565 SRem = BO0;
5566 // icmp Y, (srem X, Y)
5567 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5568 Op0 == BO1->getOperand(i_nocapture: 1))
5569 SRem = BO1;
5570 if (SRem) {
5571 // We don't check hasOneUse to avoid increasing register pressure because
5572 // the value we use is the same value this instruction was already using.
5573 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(pred: Pred) : Pred) {
5574 default:
5575 break;
5576 case ICmpInst::ICMP_EQ:
5577 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
5578 case ICmpInst::ICMP_NE:
5579 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
5580 case ICmpInst::ICMP_SGT:
5581 case ICmpInst::ICMP_SGE:
5582 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(i_nocapture: 1),
5583 Constant::getAllOnesValue(Ty: SRem->getType()));
5584 case ICmpInst::ICMP_SLT:
5585 case ICmpInst::ICMP_SLE:
5586 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(i_nocapture: 1),
5587 Constant::getNullValue(Ty: SRem->getType()));
5588 }
5589 }
5590
5591 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5592 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5593 BO0->getOperand(i_nocapture: 1) == BO1->getOperand(i_nocapture: 1)) {
5594 switch (BO0->getOpcode()) {
5595 default:
5596 break;
5597 case Instruction::Add:
5598 case Instruction::Sub:
5599 case Instruction::Xor: {
5600 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5601 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5602
5603 const APInt *C;
5604 if (match(V: BO0->getOperand(i_nocapture: 1), P: m_APInt(Res&: C))) {
5605 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5606 if (C->isSignMask()) {
5607 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5608 return new ICmpInst(NewPred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5609 }
5610
5611 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5612 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5613 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5614 NewPred = I.getSwappedPredicate(pred: NewPred);
5615 return new ICmpInst(NewPred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5616 }
5617 }
5618 break;
5619 }
5620 case Instruction::Mul: {
5621 if (!I.isEquality())
5622 break;
5623
5624 const APInt *C;
5625 if (match(V: BO0->getOperand(i_nocapture: 1), P: m_APInt(Res&: C)) && !C->isZero() &&
5626 !C->isOne()) {
5627 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5628 // Mask = -1 >> count-trailing-zeros(C).
5629 if (unsigned TZs = C->countr_zero()) {
5630 Constant *Mask = ConstantInt::get(
5631 Ty: BO0->getType(),
5632 V: APInt::getLowBitsSet(numBits: C->getBitWidth(), loBitsSet: C->getBitWidth() - TZs));
5633 Value *And1 = Builder.CreateAnd(LHS: BO0->getOperand(i_nocapture: 0), RHS: Mask);
5634 Value *And2 = Builder.CreateAnd(LHS: BO1->getOperand(i_nocapture: 0), RHS: Mask);
5635 return new ICmpInst(Pred, And1, And2);
5636 }
5637 }
5638 break;
5639 }
5640 case Instruction::UDiv:
5641 case Instruction::LShr:
5642 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5643 break;
5644 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5645
5646 case Instruction::SDiv:
5647 if (!(I.isEquality() || match(V: BO0->getOperand(i_nocapture: 1), P: m_NonNegative())) ||
5648 !BO0->isExact() || !BO1->isExact())
5649 break;
5650 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5651
5652 case Instruction::AShr:
5653 if (!BO0->isExact() || !BO1->isExact())
5654 break;
5655 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5656
5657 case Instruction::Shl: {
5658 bool NUW = Op0HasNUW && Op1HasNUW;
5659 bool NSW = Op0HasNSW && Op1HasNSW;
5660 if (!NUW && !NSW)
5661 break;
5662 if (!NSW && I.isSigned())
5663 break;
5664 return new ICmpInst(Pred, BO0->getOperand(i_nocapture: 0), BO1->getOperand(i_nocapture: 0));
5665 }
5666 }
5667 }
5668
5669 if (BO0) {
5670 // Transform A & (L - 1) `ult` L --> L != 0
5671 auto LSubOne = m_Add(L: m_Specific(V: Op1), R: m_AllOnes());
5672 auto BitwiseAnd = m_c_And(L: m_Value(), R: LSubOne);
5673
5674 if (match(V: BO0, P: BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5675 auto *Zero = Constant::getNullValue(Ty: BO0->getType());
5676 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5677 }
5678 }
5679
5680 // For unsigned predicates / eq / ne:
5681 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5682 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5683 if (!ICmpInst::isSigned(Pred)) {
5684 if (match(V: Op0, P: m_Shl(L: m_Specific(V: Op1), R: m_One())))
5685 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5686 Constant::getNullValue(Ty: Op1->getType()));
5687 else if (match(V: Op1, P: m_Shl(L: m_Specific(V: Op0), R: m_One())))
5688 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5689 Constant::getNullValue(Ty: Op0->getType()), Op0);
5690 }
5691
5692 if (Value *V = foldMultiplicationOverflowCheck(I))
5693 return replaceInstUsesWith(I, V);
5694
5695 if (Instruction *R = foldICmpAndXX(I, Q, IC&: *this))
5696 return R;
5697
5698 if (Value *V = foldICmpWithTruncSignExtendedVal(I, Builder))
5699 return replaceInstUsesWith(I, V);
5700
5701 if (Value *V = foldShiftIntoShiftInAnotherHandOfAndInICmp(I, SQ, Builder))
5702 return replaceInstUsesWith(I, V);
5703
5704 return nullptr;
5705}
5706
5707/// Fold icmp Pred min|max(X, Y), Z.
5708Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
5709 MinMaxIntrinsic *MinMax,
5710 Value *Z, CmpPredicate Pred) {
5711 Value *X = MinMax->getLHS();
5712 Value *Y = MinMax->getRHS();
5713 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5714 return nullptr;
5715 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5716 // Revert the transform signed pred -> unsigned pred
5717 // TODO: We can flip the signedness of predicate if both operands of icmp
5718 // are negative.
5719 if (isKnownNonNegative(V: Z, SQ: SQ.getWithInstruction(I: &I)) &&
5720 isKnownNonNegative(V: MinMax, SQ: SQ.getWithInstruction(I: &I))) {
5721 Pred = ICmpInst::getFlippedSignednessPredicate(Pred);
5722 } else
5723 return nullptr;
5724 }
5725 SimplifyQuery Q = SQ.getWithInstruction(I: &I);
5726 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5727 if (!Val)
5728 return std::nullopt;
5729 if (match(V: Val, P: m_One()))
5730 return true;
5731 if (match(V: Val, P: m_Zero()))
5732 return false;
5733 return std::nullopt;
5734 };
5735 // Remove samesign here since it is illegal to keep it when we speculatively
5736 // execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
5737 // cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
5738 // -46, -32)`. `X` is allowed to be non-negative here.
5739 Pred = Pred.dropSameSign();
5740 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, LHS: X, RHS: Z, Q));
5741 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, LHS: Y, RHS: Z, Q));
5742 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5743 return nullptr;
5744 if (!CmpXZ.has_value()) {
5745 std::swap(a&: X, b&: Y);
5746 std::swap(lhs&: CmpXZ, rhs&: CmpYZ);
5747 }
5748
5749 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5750 if (CmpYZ.has_value())
5751 return replaceInstUsesWith(I, V: ConstantInt::getBool(Ty: I.getType(), V: *CmpYZ));
5752 return ICmpInst::Create(Op: Instruction::ICmp, Pred, S1: Y, S2: Z);
5753 };
5754
5755 switch (Pred) {
5756 case ICmpInst::ICMP_EQ:
5757 case ICmpInst::ICMP_NE: {
5758 // If X == Z:
5759 // Expr Result
5760 // min(X, Y) == Z X <= Y
5761 // max(X, Y) == Z X >= Y
5762 // min(X, Y) != Z X > Y
5763 // max(X, Y) != Z X < Y
5764 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5765 ICmpInst::Predicate NewPred =
5766 ICmpInst::getNonStrictPredicate(pred: MinMax->getPredicate());
5767 if (Pred == ICmpInst::ICMP_NE)
5768 NewPred = ICmpInst::getInversePredicate(pred: NewPred);
5769 return ICmpInst::Create(Op: Instruction::ICmp, Pred: NewPred, S1: X, S2: Y);
5770 }
5771 // Otherwise (X != Z):
5772 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5773 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred: NewPred, LHS: X, RHS: Z, Q));
5774 if (!MinMaxCmpXZ.has_value()) {
5775 std::swap(a&: X, b&: Y);
5776 std::swap(lhs&: CmpXZ, rhs&: CmpYZ);
5777 // Re-check pre-condition X != Z
5778 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5779 break;
5780 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred: NewPred, LHS: X, RHS: Z, Q));
5781 }
5782 if (!MinMaxCmpXZ.has_value())
5783 break;
5784 if (*MinMaxCmpXZ) {
5785 // Expr Fact Result
5786 // min(X, Y) == Z X < Z false
5787 // max(X, Y) == Z X > Z false
5788 // min(X, Y) != Z X < Z true
5789 // max(X, Y) != Z X > Z true
5790 return replaceInstUsesWith(
5791 I, V: ConstantInt::getBool(Ty: I.getType(), V: Pred == ICmpInst::ICMP_NE));
5792 } else {
5793 // Expr Fact Result
5794 // min(X, Y) == Z X > Z Y == Z
5795 // max(X, Y) == Z X < Z Y == Z
5796 // min(X, Y) != Z X > Z Y != Z
5797 // max(X, Y) != Z X < Z Y != Z
5798 return FoldIntoCmpYZ();
5799 }
5800 break;
5801 }
5802 case ICmpInst::ICMP_SLT:
5803 case ICmpInst::ICMP_ULT:
5804 case ICmpInst::ICMP_SLE:
5805 case ICmpInst::ICMP_ULE:
5806 case ICmpInst::ICMP_SGT:
5807 case ICmpInst::ICMP_UGT:
5808 case ICmpInst::ICMP_SGE:
5809 case ICmpInst::ICMP_UGE: {
5810 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(pred: Pred);
5811 if (*CmpXZ) {
5812 if (IsSame) {
5813 // Expr Fact Result
5814 // min(X, Y) < Z X < Z true
5815 // min(X, Y) <= Z X <= Z true
5816 // max(X, Y) > Z X > Z true
5817 // max(X, Y) >= Z X >= Z true
5818 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
5819 } else {
5820 // Expr Fact Result
5821 // max(X, Y) < Z X < Z Y < Z
5822 // max(X, Y) <= Z X <= Z Y <= Z
5823 // min(X, Y) > Z X > Z Y > Z
5824 // min(X, Y) >= Z X >= Z Y >= Z
5825 return FoldIntoCmpYZ();
5826 }
5827 } else {
5828 if (IsSame) {
5829 // Expr Fact Result
5830 // min(X, Y) < Z X >= Z Y < Z
5831 // min(X, Y) <= Z X > Z Y <= Z
5832 // max(X, Y) > Z X <= Z Y > Z
5833 // max(X, Y) >= Z X < Z Y >= Z
5834 return FoldIntoCmpYZ();
5835 } else {
5836 // Expr Fact Result
5837 // max(X, Y) < Z X >= Z false
5838 // max(X, Y) <= Z X > Z false
5839 // min(X, Y) > Z X <= Z false
5840 // min(X, Y) >= Z X < Z false
5841 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
5842 }
5843 }
5844 break;
5845 }
5846 default:
5847 break;
5848 }
5849
5850 return nullptr;
5851}
5852
5853/// Match and fold patterns like:
5854/// icmp eq/ne X, min(max(X, Lo), Hi)
5855/// which represents a range check and can be represented as a ConstantRange.
5856///
5857/// For icmp eq, build ConstantRange [Lo, Hi + 1) and convert to:
5858/// (X - Lo) u< (Hi + 1 - Lo)
5859/// For icmp ne, build ConstantRange [Hi + 1, Lo) and convert to:
5860/// (X - (Hi + 1)) u< (Lo - (Hi + 1))
5861Instruction *InstCombinerImpl::foldICmpWithClamp(ICmpInst &I, Value *X,
5862 MinMaxIntrinsic *Min) {
5863 if (!I.isEquality() || !Min->hasOneUse() || !Min->isMin())
5864 return nullptr;
5865
5866 const APInt *Lo = nullptr, *Hi = nullptr;
5867 if (Min->isSigned()) {
5868 if (!match(V: Min->getLHS(), P: m_OneUse(SubPattern: m_SMax(Op0: m_Specific(V: X), Op1: m_APInt(Res&: Lo)))) ||
5869 !match(V: Min->getRHS(), P: m_APInt(Res&: Hi)) || !Lo->slt(RHS: *Hi))
5870 return nullptr;
5871 } else {
5872 if (!match(V: Min->getLHS(), P: m_OneUse(SubPattern: m_UMax(Op0: m_Specific(V: X), Op1: m_APInt(Res&: Lo)))) ||
5873 !match(V: Min->getRHS(), P: m_APInt(Res&: Hi)) || !Lo->ult(RHS: *Hi))
5874 return nullptr;
5875 }
5876
5877 ConstantRange CR = ConstantRange::getNonEmpty(Lower: *Lo, Upper: *Hi + 1);
5878 ICmpInst::Predicate Pred;
5879 APInt C, Offset;
5880 if (I.getPredicate() == ICmpInst::ICMP_EQ)
5881 CR.getEquivalentICmp(Pred, RHS&: C, Offset);
5882 else
5883 CR.inverse().getEquivalentICmp(Pred, RHS&: C, Offset);
5884
5885 if (!Offset.isZero())
5886 X = Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: Offset));
5887
5888 return replaceInstUsesWith(
5889 I, V: Builder.CreateICmp(P: Pred, LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: C)));
5890}
5891
5892// Canonicalize checking for a power-of-2-or-zero value:
5893static Instruction *foldICmpPow2Test(ICmpInst &I,
5894 InstCombiner::BuilderTy &Builder) {
5895 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
5896 const CmpInst::Predicate Pred = I.getPredicate();
5897 Value *A = nullptr;
5898 bool CheckIs;
5899 if (I.isEquality()) {
5900 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5901 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5902 if (!match(V: Op0, P: m_OneUse(SubPattern: m_c_And(L: m_Add(L: m_Value(V&: A), R: m_AllOnes()),
5903 R: m_Deferred(V: A)))) ||
5904 !match(V: Op1, P: m_ZeroInt()))
5905 A = nullptr;
5906
5907 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5908 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5909 if (match(V: Op0, P: m_OneUse(SubPattern: m_c_And(L: m_Neg(V: m_Specific(V: Op1)), R: m_Specific(V: Op1)))))
5910 A = Op1;
5911 else if (match(V: Op1,
5912 P: m_OneUse(SubPattern: m_c_And(L: m_Neg(V: m_Specific(V: Op0)), R: m_Specific(V: Op0)))))
5913 A = Op0;
5914
5915 CheckIs = Pred == ICmpInst::ICMP_EQ;
5916 } else if (ICmpInst::isUnsigned(Pred)) {
5917 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5918 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5919
5920 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5921 match(V: Op0, P: m_OneUse(SubPattern: m_c_Xor(L: m_Add(L: m_Specific(V: Op1), R: m_AllOnes()),
5922 R: m_Specific(V: Op1))))) {
5923 A = Op1;
5924 CheckIs = Pred == ICmpInst::ICMP_UGE;
5925 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5926 match(V: Op1, P: m_OneUse(SubPattern: m_c_Xor(L: m_Add(L: m_Specific(V: Op0), R: m_AllOnes()),
5927 R: m_Specific(V: Op0))))) {
5928 A = Op0;
5929 CheckIs = Pred == ICmpInst::ICMP_ULE;
5930 }
5931 }
5932
5933 if (A) {
5934 Type *Ty = A->getType();
5935 Value *CtPop = Builder.CreateUnaryIntrinsic(ID: Intrinsic::ctpop, Op: A);
5936 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5937 ConstantInt::get(Ty, V: 2))
5938 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5939 ConstantInt::get(Ty, V: 1));
5940 }
5941
5942 return nullptr;
5943}
5944
5945/// Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
5946using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;
5947static void collectOffsetOp(Value *V, SmallVectorImpl<OffsetOp> &Offsets,
5948 bool AllowRecursion) {
5949 Instruction *Inst = dyn_cast<Instruction>(Val: V);
5950 if (!Inst || !Inst->hasOneUse())
5951 return;
5952
5953 switch (Inst->getOpcode()) {
5954 case Instruction::Add:
5955 Offsets.emplace_back(Args: Instruction::Sub, Args: Inst->getOperand(i: 1));
5956 Offsets.emplace_back(Args: Instruction::Sub, Args: Inst->getOperand(i: 0));
5957 break;
5958 case Instruction::Sub:
5959 Offsets.emplace_back(Args: Instruction::Add, Args: Inst->getOperand(i: 1));
5960 break;
5961 case Instruction::Xor:
5962 Offsets.emplace_back(Args: Instruction::Xor, Args: Inst->getOperand(i: 1));
5963 Offsets.emplace_back(Args: Instruction::Xor, Args: Inst->getOperand(i: 0));
5964 break;
5965 case Instruction::Shl:
5966 if (Inst->hasNoSignedWrap())
5967 Offsets.emplace_back(Args: Instruction::AShr, Args: Inst->getOperand(i: 1));
5968 if (Inst->hasNoUnsignedWrap())
5969 Offsets.emplace_back(Args: Instruction::LShr, Args: Inst->getOperand(i: 1));
5970 break;
5971 case Instruction::Select:
5972 if (AllowRecursion) {
5973 collectOffsetOp(V: Inst->getOperand(i: 1), Offsets, /*AllowRecursion=*/false);
5974 collectOffsetOp(V: Inst->getOperand(i: 2), Offsets, /*AllowRecursion=*/false);
5975 }
5976 break;
5977 default:
5978 break;
5979 }
5980}
5981
5982enum class OffsetKind { Invalid, Value, Select };
5983
5984struct OffsetResult {
5985 OffsetKind Kind;
5986 Value *V0, *V1, *V2;
5987 Instruction *MDFrom;
5988
5989 static OffsetResult invalid() {
5990 return {.Kind: OffsetKind::Invalid, .V0: nullptr, .V1: nullptr, .V2: nullptr, .MDFrom: nullptr};
5991 }
5992 static OffsetResult value(Value *V) {
5993 return {.Kind: OffsetKind::Value, .V0: V, .V1: nullptr, .V2: nullptr, .MDFrom: nullptr};
5994 }
5995 static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV,
5996 Instruction *MDFrom) {
5997 return {.Kind: OffsetKind::Select, .V0: Cond, .V1: TrueV, .V2: FalseV, .MDFrom: MDFrom};
5998 }
5999 bool isValid() const { return Kind != OffsetKind::Invalid; }
6000 Value *materialize(InstCombiner::BuilderTy &Builder) const {
6001 switch (Kind) {
6002 case OffsetKind::Invalid:
6003 llvm_unreachable("Invalid offset result");
6004 case OffsetKind::Value:
6005 return V0;
6006 case OffsetKind::Select:
6007 return Builder.CreateSelect(
6008 C: V0, True: V1, False: V2, Name: "", MDFrom: ProfcheckDisableMetadataFixes ? nullptr : MDFrom);
6009 }
6010 llvm_unreachable("Unknown OffsetKind enum");
6011 }
6012};
6013
6014/// Offset both sides of an equality icmp to see if we can save some
6015/// instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.
6016/// Note: This operation should not introduce poison.
6017static Instruction *foldICmpEqualityWithOffset(ICmpInst &I,
6018 InstCombiner::BuilderTy &Builder,
6019 const SimplifyQuery &SQ) {
6020 assert(I.isEquality() && "Expected an equality icmp");
6021 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
6022 if (!Op0->getType()->isIntOrIntVectorTy())
6023 return nullptr;
6024
6025 SmallVector<OffsetOp, 4> OffsetOps;
6026 collectOffsetOp(V: Op0, Offsets&: OffsetOps, /*AllowRecursion=*/true);
6027 collectOffsetOp(V: Op1, Offsets&: OffsetOps, /*AllowRecursion=*/true);
6028
6029 auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {
6030 switch (BinOpc) {
6031 // V = shl nsw X, RHS => X = ashr V, RHS
6032 case Instruction::AShr: {
6033 const APInt *CV, *CRHS;
6034 if (!(match(V, P: m_APInt(Res&: CV)) && match(V: RHS, P: m_APInt(Res&: CRHS)) &&
6035 CV->ashr(ShiftAmt: *CRHS).shl(ShiftAmt: *CRHS) == *CV) &&
6036 !match(V, P: m_NSWShl(L: m_Value(), R: m_Specific(V: RHS))))
6037 return nullptr;
6038 break;
6039 }
6040 // V = shl nuw X, RHS => X = lshr V, RHS
6041 case Instruction::LShr: {
6042 const APInt *CV, *CRHS;
6043 if (!(match(V, P: m_APInt(Res&: CV)) && match(V: RHS, P: m_APInt(Res&: CRHS)) &&
6044 CV->lshr(ShiftAmt: *CRHS).shl(ShiftAmt: *CRHS) == *CV) &&
6045 !match(V, P: m_NUWShl(L: m_Value(), R: m_Specific(V: RHS))))
6046 return nullptr;
6047 break;
6048 }
6049 default:
6050 break;
6051 }
6052
6053 Value *Simplified = simplifyBinOp(Opcode: BinOpc, LHS: V, RHS, Q: SQ);
6054 if (!Simplified)
6055 return nullptr;
6056 // Reject constant expressions as they don't simplify things.
6057 if (isa<Constant>(Val: Simplified) && !match(V: Simplified, P: m_ImmConstant()))
6058 return nullptr;
6059 // Check if the transformation introduces poison.
6060 return impliesPoison(ValAssumedPoison: RHS, V) ? Simplified : nullptr;
6061 };
6062
6063 auto ApplyOffset = [&](Value *V, unsigned BinOpc,
6064 Value *RHS) -> OffsetResult {
6065 if (auto *Sel = dyn_cast<SelectInst>(Val: V)) {
6066 if (!Sel->hasOneUse())
6067 return OffsetResult::invalid();
6068 Value *TrueVal = ApplyOffsetImpl(Sel->getTrueValue(), BinOpc, RHS);
6069 if (!TrueVal)
6070 return OffsetResult::invalid();
6071 Value *FalseVal = ApplyOffsetImpl(Sel->getFalseValue(), BinOpc, RHS);
6072 if (!FalseVal)
6073 return OffsetResult::invalid();
6074 return OffsetResult::select(Cond: Sel->getCondition(), TrueV: TrueVal, FalseV: FalseVal, MDFrom: Sel);
6075 }
6076 if (Value *Simplified = ApplyOffsetImpl(V, BinOpc, RHS))
6077 return OffsetResult::value(V: Simplified);
6078 return OffsetResult::invalid();
6079 };
6080
6081 for (auto [BinOp, RHS] : OffsetOps) {
6082 auto BinOpc = static_cast<unsigned>(BinOp);
6083
6084 auto Op0Result = ApplyOffset(Op0, BinOpc, RHS);
6085 if (!Op0Result.isValid())
6086 continue;
6087 auto Op1Result = ApplyOffset(Op1, BinOpc, RHS);
6088 if (!Op1Result.isValid())
6089 continue;
6090
6091 Value *NewLHS = Op0Result.materialize(Builder);
6092 Value *NewRHS = Op1Result.materialize(Builder);
6093 return new ICmpInst(I.getPredicate(), NewLHS, NewRHS);
6094 }
6095
6096 return nullptr;
6097}
6098
6099Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
6100 if (!I.isEquality())
6101 return nullptr;
6102
6103 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
6104 const CmpInst::Predicate Pred = I.getPredicate();
6105 Value *A, *B, *C, *D;
6106 if (match(V: Op0, P: m_Xor(L: m_Value(V&: A), R: m_Value(V&: B)))) {
6107 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
6108 Value *OtherVal = A == Op1 ? B : A;
6109 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(Ty: A->getType()));
6110 }
6111
6112 if (match(V: Op1, P: m_Xor(L: m_Value(V&: C), R: m_Value(V&: D)))) {
6113 // A^c1 == C^c2 --> A == C^(c1^c2)
6114 ConstantInt *C1, *C2;
6115 if (match(V: B, P: m_ConstantInt(CI&: C1)) && match(V: D, P: m_ConstantInt(CI&: C2)) &&
6116 Op1->hasOneUse()) {
6117 Constant *NC = Builder.getInt(AI: C1->getValue() ^ C2->getValue());
6118 Value *Xor = Builder.CreateXor(LHS: C, RHS: NC);
6119 return new ICmpInst(Pred, A, Xor);
6120 }
6121
6122 // A^B == A^D -> B == D
6123 if (A == C)
6124 return new ICmpInst(Pred, B, D);
6125 if (A == D)
6126 return new ICmpInst(Pred, B, C);
6127 if (B == C)
6128 return new ICmpInst(Pred, A, D);
6129 if (B == D)
6130 return new ICmpInst(Pred, A, C);
6131 }
6132 }
6133
6134 if (match(V: Op1, P: m_Xor(L: m_Value(V&: A), R: m_Value(V&: B))) && (A == Op0 || B == Op0)) {
6135 // A == (A^B) -> B == 0
6136 Value *OtherVal = A == Op0 ? B : A;
6137 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(Ty: A->getType()));
6138 }
6139
6140 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
6141 if (match(V: Op0, P: m_And(L: m_Value(V&: A), R: m_Value(V&: B))) &&
6142 match(V: Op1, P: m_And(L: m_Value(V&: C), R: m_Value(V&: D)))) {
6143 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
6144
6145 if (A == C) {
6146 X = B;
6147 Y = D;
6148 Z = A;
6149 } else if (A == D) {
6150 X = B;
6151 Y = C;
6152 Z = A;
6153 } else if (B == C) {
6154 X = A;
6155 Y = D;
6156 Z = B;
6157 } else if (B == D) {
6158 X = A;
6159 Y = C;
6160 Z = B;
6161 }
6162
6163 if (X) {
6164 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
6165 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
6166 // instructions.
6167 const APInt *C0, *C1;
6168 bool XorIsNegP2 = match(V: X, P: m_APInt(Res&: C0)) && match(V: Y, P: m_APInt(Res&: C1)) &&
6169 (*C0 ^ *C1).isNegatedPowerOf2();
6170
6171 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
6172 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
6173 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
6174 int UseCnt =
6175 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
6176 (int(match(V: X, P: m_ImmConstant()) && match(V: Y, P: m_ImmConstant())));
6177 if (XorIsNegP2 || UseCnt >= 2) {
6178 // Build (X^Y) & Z
6179 Op1 = Builder.CreateXor(LHS: X, RHS: Y);
6180 Op1 = Builder.CreateAnd(LHS: Op1, RHS: Z);
6181 return new ICmpInst(Pred, Op1, Constant::getNullValue(Ty: Op1->getType()));
6182 }
6183 }
6184 }
6185
6186 {
6187 // Similar to above, but specialized for constant because invert is needed:
6188 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
6189 Value *X, *Y;
6190 Constant *C;
6191 if (match(V: Op0, P: m_OneUse(SubPattern: m_Or(L: m_Value(V&: X), R: m_Constant(C)))) &&
6192 match(V: Op1, P: m_OneUse(SubPattern: m_Or(L: m_Value(V&: Y), R: m_Specific(V: C))))) {
6193 Value *Xor = Builder.CreateXor(LHS: X, RHS: Y);
6194 Value *And = Builder.CreateAnd(LHS: Xor, RHS: ConstantExpr::getNot(C));
6195 return new ICmpInst(Pred, And, Constant::getNullValue(Ty: And->getType()));
6196 }
6197 }
6198
6199 if (match(V: Op1, P: m_ZExt(Op: m_Value(V&: A))) &&
6200 (Op0->hasOneUse() || Op1->hasOneUse())) {
6201 // (B & (Pow2C-1)) == zext A --> A == trunc B
6202 // (B & (Pow2C-1)) != zext A --> A != trunc B
6203 const APInt *MaskC;
6204 if (match(V: Op0, P: m_And(L: m_Value(V&: B), R: m_LowBitMask(V&: MaskC))) &&
6205 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
6206 return new ICmpInst(Pred, A, Builder.CreateTrunc(V: B, DestTy: A->getType()));
6207 }
6208
6209 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
6210 // For lshr and ashr pairs.
6211 const APInt *AP1, *AP2;
6212 if ((match(V: Op0, P: m_OneUse(SubPattern: m_LShr(L: m_Value(V&: A), R: m_APIntAllowPoison(Res&: AP1)))) &&
6213 match(V: Op1, P: m_OneUse(SubPattern: m_LShr(L: m_Value(V&: B), R: m_APIntAllowPoison(Res&: AP2))))) ||
6214 (match(V: Op0, P: m_OneUse(SubPattern: m_AShr(L: m_Value(V&: A), R: m_APIntAllowPoison(Res&: AP1)))) &&
6215 match(V: Op1, P: m_OneUse(SubPattern: m_AShr(L: m_Value(V&: B), R: m_APIntAllowPoison(Res&: AP2)))))) {
6216 if (*AP1 != *AP2)
6217 return nullptr;
6218 unsigned TypeBits = AP1->getBitWidth();
6219 unsigned ShAmt = AP1->getLimitedValue(Limit: TypeBits);
6220 if (ShAmt < TypeBits && ShAmt != 0) {
6221 ICmpInst::Predicate NewPred =
6222 Pred == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
6223 Value *Xor = Builder.CreateXor(LHS: A, RHS: B, Name: I.getName() + ".unshifted");
6224 APInt CmpVal = APInt::getOneBitSet(numBits: TypeBits, BitNo: ShAmt);
6225 return new ICmpInst(NewPred, Xor, ConstantInt::get(Ty: A->getType(), V: CmpVal));
6226 }
6227 }
6228
6229 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
6230 ConstantInt *Cst1;
6231 if (match(V: Op0, P: m_OneUse(SubPattern: m_Shl(L: m_Value(V&: A), R: m_ConstantInt(CI&: Cst1)))) &&
6232 match(V: Op1, P: m_OneUse(SubPattern: m_Shl(L: m_Value(V&: B), R: m_Specific(V: Cst1))))) {
6233 unsigned TypeBits = Cst1->getBitWidth();
6234 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(Limit: TypeBits);
6235 if (ShAmt < TypeBits && ShAmt != 0) {
6236 Value *Xor = Builder.CreateXor(LHS: A, RHS: B, Name: I.getName() + ".unshifted");
6237 APInt AndVal = APInt::getLowBitsSet(numBits: TypeBits, loBitsSet: TypeBits - ShAmt);
6238 Value *And =
6239 Builder.CreateAnd(LHS: Xor, RHS: Builder.getInt(AI: AndVal), Name: I.getName() + ".mask");
6240 return new ICmpInst(Pred, And, Constant::getNullValue(Ty: Cst1->getType()));
6241 }
6242 }
6243
6244 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
6245 // "icmp (and X, mask), cst"
6246 uint64_t ShAmt = 0;
6247 if (Op0->hasOneUse() &&
6248 match(V: Op0, P: m_Trunc(Op: m_OneUse(SubPattern: m_LShr(L: m_Value(V&: A), R: m_ConstantInt(V&: ShAmt))))) &&
6249 match(V: Op1, P: m_ConstantInt(CI&: Cst1)) &&
6250 // Only do this when A has multiple uses. This is most important to do
6251 // when it exposes other optimizations.
6252 !A->hasOneUse()) {
6253 unsigned ASize = cast<IntegerType>(Val: A->getType())->getPrimitiveSizeInBits();
6254
6255 if (ShAmt < ASize) {
6256 APInt MaskV =
6257 APInt::getLowBitsSet(numBits: ASize, loBitsSet: Op0->getType()->getPrimitiveSizeInBits());
6258 MaskV <<= ShAmt;
6259
6260 APInt CmpV = Cst1->getValue().zext(width: ASize);
6261 CmpV <<= ShAmt;
6262
6263 Value *Mask = Builder.CreateAnd(LHS: A, RHS: Builder.getInt(AI: MaskV));
6264 return new ICmpInst(Pred, Mask, Builder.getInt(AI: CmpV));
6265 }
6266 }
6267
6268 if (Instruction *ICmp = foldICmpIntrinsicWithIntrinsic(Cmp&: I, Builder))
6269 return ICmp;
6270
6271 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks
6272 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s
6273 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a
6274 // few steps of instcombine.
6275 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6276 if (match(V: Op0, P: m_AShr(L: m_Trunc(Op: m_Value(V&: A)), R: m_SpecificInt(V: BitWidth - 1))) &&
6277 match(V: Op1, P: m_Trunc(Op: m_LShr(L: m_Specific(V: A), R: m_SpecificInt(V: BitWidth)))) &&
6278 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
6279 (I.getOperand(i_nocapture: 0)->hasOneUse() || I.getOperand(i_nocapture: 1)->hasOneUse())) {
6280 APInt C = APInt::getOneBitSet(numBits: BitWidth * 2, BitNo: BitWidth - 1);
6281 Value *Add = Builder.CreateAdd(LHS: A, RHS: ConstantInt::get(Ty: A->getType(), V: C));
6282 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
6283 : ICmpInst::ICMP_UGE,
6284 Add, ConstantInt::get(Ty: A->getType(), V: C.shl(shiftAmt: 1)));
6285 }
6286
6287 // Canonicalize:
6288 // Assume B_Pow2 != 0
6289 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
6290 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
6291 if (match(V: Op0, P: m_c_And(L: m_Specific(V: Op1), R: m_Value())) &&
6292 isKnownToBeAPowerOfTwo(V: Op1, /* OrZero */ false, CxtI: &I))
6293 return new ICmpInst(CmpInst::getInversePredicate(pred: Pred), Op0,
6294 ConstantInt::getNullValue(Ty: Op0->getType()));
6295
6296 if (match(V: Op1, P: m_c_And(L: m_Specific(V: Op0), R: m_Value())) &&
6297 isKnownToBeAPowerOfTwo(V: Op0, /* OrZero */ false, CxtI: &I))
6298 return new ICmpInst(CmpInst::getInversePredicate(pred: Pred), Op1,
6299 ConstantInt::getNullValue(Ty: Op1->getType()));
6300
6301 // Canonicalize:
6302 // icmp eq/ne X, OneUse(rotate-right(X))
6303 // -> icmp eq/ne X, rotate-left(X)
6304 // We generally try to convert rotate-right -> rotate-left, this just
6305 // canonicalizes another case.
6306 if (match(V: &I, P: m_c_ICmp(L: m_Value(V&: A),
6307 R: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::fshr>(
6308 Op0: m_Deferred(V: A), Op1: m_Deferred(V: A), Op2: m_Value(V&: B))))))
6309 return new ICmpInst(
6310 Pred, A,
6311 Builder.CreateIntrinsic(RetTy: Op0->getType(), ID: Intrinsic::fshl, Args: {A, A, B}));
6312
6313 // Canonicalize:
6314 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
6315 Constant *Cst;
6316 if (match(V: &I, P: m_c_ICmp(L: m_OneUse(SubPattern: m_Xor(L: m_Value(V&: A), R: m_ImmConstant(C&: Cst))),
6317 R: m_CombineAnd(Ps: m_Value(V&: B), Ps: m_Unless(M: m_ImmConstant())))))
6318 return new ICmpInst(Pred, Builder.CreateXor(LHS: A, RHS: B), Cst);
6319
6320 {
6321 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6322 auto m_Matcher =
6323 m_CombineOr(Ps: m_CombineOr(Ps: m_c_Add(L: m_Value(V&: B), R: m_Deferred(V: A)),
6324 Ps: m_c_Xor(L: m_Value(V&: B), R: m_Deferred(V: A))),
6325 Ps: m_Sub(L: m_Value(V&: B), R: m_Deferred(V: A)));
6326 std::optional<bool> IsZero = std::nullopt;
6327 if (match(V: &I, P: m_c_ICmp(L: m_OneUse(SubPattern: m_c_And(L: m_Value(V&: A), R: m_Matcher)),
6328 R: m_Deferred(V: A))))
6329 IsZero = false;
6330 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6331 else if (match(V: &I,
6332 P: m_ICmp(L: m_OneUse(SubPattern: m_c_And(L: m_Value(V&: A), R: m_Matcher)), R: m_Zero())))
6333 IsZero = true;
6334
6335 if (IsZero && isKnownToBeAPowerOfTwo(V: A, /* OrZero */ true, CxtI: &I))
6336 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6337 // -> (icmp eq/ne (and X, P2), 0)
6338 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6339 // -> (icmp eq/ne (and X, P2), P2)
6340 return new ICmpInst(Pred, Builder.CreateAnd(LHS: B, RHS: A),
6341 *IsZero ? A
6342 : ConstantInt::getNullValue(Ty: A->getType()));
6343 }
6344
6345 if (auto *Res = foldICmpEqualityWithOffset(
6346 I, Builder, SQ: getSimplifyQuery().getWithInstruction(I: &I)))
6347 return Res;
6348
6349 return nullptr;
6350}
6351
6352Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) {
6353 ICmpInst::Predicate Pred = ICmp.getPredicate();
6354 Value *Op0 = ICmp.getOperand(i_nocapture: 0), *Op1 = ICmp.getOperand(i_nocapture: 1);
6355
6356 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
6357 // The trunc masks high bits while the compare may effectively mask low bits.
6358 Value *X;
6359 const APInt *C;
6360 if (!match(V: Op0, P: m_OneUse(SubPattern: m_Trunc(Op: m_Value(V&: X)))) || !match(V: Op1, P: m_APInt(Res&: C)))
6361 return nullptr;
6362
6363 // This matches patterns corresponding to tests of the signbit as well as:
6364 // (trunc X) pred C2 --> (X & Mask) == C
6365 if (auto Res = decomposeBitTestICmp(LHS: Op0, RHS: Op1, Pred, /*LookThroughTrunc=*/true,
6366 /*AllowNonZeroC=*/true)) {
6367 Value *And = Builder.CreateAnd(LHS: Res->X, RHS: Res->Mask);
6368 Constant *C = ConstantInt::get(Ty: Res->X->getType(), V: Res->C);
6369 return new ICmpInst(Res->Pred, And, C);
6370 }
6371
6372 unsigned SrcBits = X->getType()->getScalarSizeInBits();
6373 if (auto *II = dyn_cast<IntrinsicInst>(Val: X)) {
6374 if (II->getIntrinsicID() == Intrinsic::cttz ||
6375 II->getIntrinsicID() == Intrinsic::ctlz) {
6376 unsigned MaxRet = SrcBits;
6377 // If the "is_zero_poison" argument is set, then we know at least
6378 // one bit is set in the input, so the result is always at least one
6379 // less than the full bitwidth of that input.
6380 if (match(V: II->getArgOperand(i: 1), P: m_One()))
6381 MaxRet--;
6382
6383 // Make sure the destination is wide enough to hold the largest output of
6384 // the intrinsic.
6385 if (llvm::Log2_32(Value: MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
6386 if (Instruction *I =
6387 foldICmpIntrinsicWithConstant(Cmp&: ICmp, II, C: C->zext(width: SrcBits)))
6388 return I;
6389 }
6390 }
6391
6392 return nullptr;
6393}
6394
6395Instruction *InstCombinerImpl::foldICmpWithZextOrSext(ICmpInst &ICmp) {
6396 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
6397 auto *CastOp0 = cast<CastInst>(Val: ICmp.getOperand(i_nocapture: 0));
6398 Value *X;
6399 if (!match(V: CastOp0, P: m_ZExtOrSExt(Op: m_Value(V&: X))))
6400 return nullptr;
6401
6402 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
6403 bool IsSignedCmp = ICmp.isSigned();
6404
6405 // icmp Pred (ext X), (ext Y)
6406 Value *Y;
6407 if (match(V: ICmp.getOperand(i_nocapture: 1), P: m_ZExtOrSExt(Op: m_Value(V&: Y)))) {
6408 bool IsZext0 = isa<ZExtInst>(Val: ICmp.getOperand(i_nocapture: 0));
6409 bool IsZext1 = isa<ZExtInst>(Val: ICmp.getOperand(i_nocapture: 1));
6410
6411 if (IsZext0 != IsZext1) {
6412 // If X and Y and both i1
6413 // (icmp eq/ne (zext X) (sext Y))
6414 // eq -> (icmp eq (or X, Y), 0)
6415 // ne -> (icmp ne (or X, Y), 0)
6416 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(BitWidth: 1) &&
6417 Y->getType()->isIntOrIntVectorTy(BitWidth: 1))
6418 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(LHS: X, RHS: Y),
6419 Constant::getNullValue(Ty: X->getType()));
6420
6421 // If we have mismatched casts and zext has the nneg flag, we can
6422 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
6423
6424 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(Val: ICmp.getOperand(i_nocapture: 0));
6425 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(Val: ICmp.getOperand(i_nocapture: 1));
6426
6427 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
6428 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
6429
6430 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
6431 IsSignedExt = true;
6432 else
6433 return nullptr;
6434 }
6435
6436 // Not an extension from the same type?
6437 Type *XTy = X->getType(), *YTy = Y->getType();
6438 if (XTy != YTy) {
6439 // One of the casts must have one use because we are creating a new cast.
6440 if (!ICmp.getOperand(i_nocapture: 0)->hasOneUse() && !ICmp.getOperand(i_nocapture: 1)->hasOneUse())
6441 return nullptr;
6442 // Extend the narrower operand to the type of the wider operand.
6443 CastInst::CastOps CastOpcode =
6444 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
6445 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
6446 X = Builder.CreateCast(Op: CastOpcode, V: X, DestTy: YTy);
6447 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6448 Y = Builder.CreateCast(Op: CastOpcode, V: Y, DestTy: XTy);
6449 else
6450 return nullptr;
6451 }
6452
6453 // (zext X) == (zext Y) --> X == Y
6454 // (sext X) == (sext Y) --> X == Y
6455 if (ICmp.isEquality())
6456 return new ICmpInst(ICmp.getPredicate(), X, Y);
6457
6458 // A signed comparison of sign extended values simplifies into a
6459 // signed comparison.
6460 if (IsSignedCmp && IsSignedExt)
6461 return new ICmpInst(ICmp.getPredicate(), X, Y);
6462
6463 // The other three cases all fold into an unsigned comparison.
6464 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6465 }
6466
6467 // Below here, we are only folding a compare with constant.
6468 auto *C = dyn_cast<Constant>(Val: ICmp.getOperand(i_nocapture: 1));
6469 if (!C)
6470 return nullptr;
6471
6472 // If a lossless truncate is possible...
6473 Type *SrcTy = CastOp0->getSrcTy();
6474 Constant *Res = getLosslessInvCast(C, InvCastTo: SrcTy, CastOp: CastOp0->getOpcode(), DL);
6475 if (Res) {
6476 if (ICmp.isEquality())
6477 return new ICmpInst(ICmp.getPredicate(), X, Res);
6478
6479 // A signed comparison of sign extended values simplifies into a
6480 // signed comparison.
6481 if (IsSignedExt && IsSignedCmp)
6482 return new ICmpInst(ICmp.getPredicate(), X, Res);
6483
6484 // The other three cases all fold into an unsigned comparison.
6485 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6486 }
6487
6488 // The re-extended constant changed, partly changed (in the case of a vector),
6489 // or could not be determined to be equal (in the case of a constant
6490 // expression), so the constant cannot be represented in the shorter type.
6491 // All the cases that fold to true or false will have already been handled
6492 // by simplifyICmpInst, so only deal with the tricky case.
6493 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(Val: C))
6494 return nullptr;
6495
6496 // Is source op positive?
6497 // icmp ult (sext X), C --> icmp sgt X, -1
6498 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6499 return new ICmpInst(CmpInst::ICMP_SGT, X, Constant::getAllOnesValue(Ty: SrcTy));
6500
6501 // Is source op negative?
6502 // icmp ugt (sext X), C --> icmp slt X, 0
6503 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6504 return new ICmpInst(CmpInst::ICMP_SLT, X, Constant::getNullValue(Ty: SrcTy));
6505}
6506
6507/// Handle icmp (cast x), (cast or constant).
6508Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
6509 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6510 // icmp compares only pointer's value.
6511 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6512 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(Val: ICmp.getOperand(i_nocapture: 0));
6513 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(Val: ICmp.getOperand(i_nocapture: 1));
6514 if (SimplifiedOp0 || SimplifiedOp1)
6515 return new ICmpInst(ICmp.getPredicate(),
6516 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(i_nocapture: 0),
6517 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(i_nocapture: 1));
6518
6519 auto *CastOp0 = dyn_cast<CastInst>(Val: ICmp.getOperand(i_nocapture: 0));
6520 if (!CastOp0)
6521 return nullptr;
6522 if (!isa<Constant>(Val: ICmp.getOperand(i_nocapture: 1)) && !isa<CastInst>(Val: ICmp.getOperand(i_nocapture: 1)))
6523 return nullptr;
6524
6525 Value *Op0Src = CastOp0->getOperand(i_nocapture: 0);
6526 Type *SrcTy = CastOp0->getSrcTy();
6527 Type *DestTy = CastOp0->getDestTy();
6528
6529 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6530 // integer type is the same size as the pointer type.
6531 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6532 if (isa<VectorType>(Val: PtrTy)) {
6533 PtrTy = cast<VectorType>(Val: PtrTy)->getElementType();
6534 IntTy = cast<VectorType>(Val: IntTy)->getElementType();
6535 }
6536 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
6537 };
6538 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6539 CompatibleSizes(SrcTy, DestTy)) {
6540 Value *NewOp1 = nullptr;
6541 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(Val: ICmp.getOperand(i_nocapture: 1))) {
6542 Value *PtrSrc = PtrToIntOp1->getOperand(i_nocapture: 0);
6543 if (PtrSrc->getType() == Op0Src->getType())
6544 NewOp1 = PtrToIntOp1->getOperand(i_nocapture: 0);
6545 } else if (auto *RHSC = dyn_cast<Constant>(Val: ICmp.getOperand(i_nocapture: 1))) {
6546 NewOp1 = ConstantExpr::getIntToPtr(C: RHSC, Ty: SrcTy);
6547 }
6548
6549 if (NewOp1)
6550 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6551 }
6552
6553 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6554 if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6555 CompatibleSizes(DestTy, SrcTy)) {
6556 Value *NewOp1 = nullptr;
6557 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(Val: ICmp.getOperand(i_nocapture: 1))) {
6558 Value *IntSrc = IntToPtrOp1->getOperand(i_nocapture: 0);
6559 if (IntSrc->getType() == Op0Src->getType())
6560 NewOp1 = IntToPtrOp1->getOperand(i_nocapture: 0);
6561 } else if (auto *RHSC = dyn_cast<Constant>(Val: ICmp.getOperand(i_nocapture: 1))) {
6562 NewOp1 = ConstantFoldConstant(C: ConstantExpr::getPtrToInt(C: RHSC, Ty: SrcTy), DL);
6563 }
6564
6565 if (NewOp1)
6566 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6567 }
6568
6569 if (Instruction *R = foldICmpWithTrunc(ICmp))
6570 return R;
6571
6572 return foldICmpWithZextOrSext(ICmp);
6573}
6574
6575static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS,
6576 bool IsSigned) {
6577 switch (BinaryOp) {
6578 default:
6579 llvm_unreachable("Unsupported binary op");
6580 case Instruction::Add:
6581 case Instruction::Sub:
6582 return match(V: RHS, P: m_Zero());
6583 case Instruction::Mul:
6584 return !(RHS->getType()->isIntOrIntVectorTy(BitWidth: 1) && IsSigned) &&
6585 match(V: RHS, P: m_One());
6586 }
6587}
6588
6589OverflowResult
6590InstCombinerImpl::computeOverflow(Instruction::BinaryOps BinaryOp,
6591 bool IsSigned, Value *LHS, Value *RHS,
6592 Instruction *CxtI) const {
6593 switch (BinaryOp) {
6594 default:
6595 llvm_unreachable("Unsupported binary op");
6596 case Instruction::Add:
6597 if (IsSigned)
6598 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6599 else
6600 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6601 case Instruction::Sub:
6602 if (IsSigned)
6603 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6604 else
6605 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6606 case Instruction::Mul:
6607 if (IsSigned)
6608 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6609 else
6610 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6611 }
6612}
6613
6614bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6615 bool IsSigned, Value *LHS,
6616 Value *RHS, Instruction &OrigI,
6617 Value *&Result,
6618 Constant *&Overflow) {
6619 if (OrigI.isCommutative() && isa<Constant>(Val: LHS) && !isa<Constant>(Val: RHS))
6620 std::swap(a&: LHS, b&: RHS);
6621
6622 // If the overflow check was an add followed by a compare, the insertion point
6623 // may be pointing to the compare. We want to insert the new instructions
6624 // before the add in case there are uses of the add between the add and the
6625 // compare.
6626 Builder.SetInsertPoint(&OrigI);
6627
6628 Type *OverflowTy = Type::getInt1Ty(C&: LHS->getContext());
6629 if (auto *LHSTy = dyn_cast<VectorType>(Val: LHS->getType()))
6630 OverflowTy = VectorType::get(ElementType: OverflowTy, EC: LHSTy->getElementCount());
6631
6632 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6633 Result = LHS;
6634 Overflow = ConstantInt::getFalse(Ty: OverflowTy);
6635 return true;
6636 }
6637
6638 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, CxtI: &OrigI)) {
6639 case OverflowResult::MayOverflow:
6640 return false;
6641 case OverflowResult::AlwaysOverflowsLow:
6642 case OverflowResult::AlwaysOverflowsHigh:
6643 Result = Builder.CreateBinOp(Opc: BinaryOp, LHS, RHS);
6644 Result->takeName(V: &OrigI);
6645 Overflow = ConstantInt::getTrue(Ty: OverflowTy);
6646 return true;
6647 case OverflowResult::NeverOverflows:
6648 Result = Builder.CreateBinOp(Opc: BinaryOp, LHS, RHS);
6649 Result->takeName(V: &OrigI);
6650 Overflow = ConstantInt::getFalse(Ty: OverflowTy);
6651 if (auto *Inst = dyn_cast<Instruction>(Val: Result)) {
6652 if (IsSigned)
6653 Inst->setHasNoSignedWrap();
6654 else
6655 Inst->setHasNoUnsignedWrap();
6656 }
6657 return true;
6658 }
6659
6660 llvm_unreachable("Unexpected overflow result");
6661}
6662
6663/// Recognize and process idiom involving test for multiplication
6664/// overflow.
6665///
6666/// The caller has matched a pattern of the form:
6667/// I = cmp u (mul(zext A, zext B), V
6668/// The function checks if this is a test for overflow and if so replaces
6669/// multiplication with call to 'mul.with.overflow' intrinsic.
6670///
6671/// \param I Compare instruction.
6672/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6673/// the compare instruction. Must be of integer type.
6674/// \param OtherVal The other argument of compare instruction.
6675/// \returns Instruction which must replace the compare instruction, NULL if no
6676/// replacement required.
6677static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal,
6678 const APInt *OtherVal,
6679 InstCombinerImpl &IC) {
6680 // Don't bother doing this transformation for pointers, don't do it for
6681 // vectors.
6682 if (!isa<IntegerType>(Val: MulVal->getType()))
6683 return nullptr;
6684
6685 auto *MulInstr = dyn_cast<Instruction>(Val: MulVal);
6686 if (!MulInstr)
6687 return nullptr;
6688 assert(MulInstr->getOpcode() == Instruction::Mul);
6689
6690 auto *LHS = cast<ZExtInst>(Val: MulInstr->getOperand(i: 0)),
6691 *RHS = cast<ZExtInst>(Val: MulInstr->getOperand(i: 1));
6692 assert(LHS->getOpcode() == Instruction::ZExt);
6693 assert(RHS->getOpcode() == Instruction::ZExt);
6694 Value *A = LHS->getOperand(i_nocapture: 0), *B = RHS->getOperand(i_nocapture: 0);
6695
6696 // Calculate type and width of the result produced by mul.with.overflow.
6697 Type *TyA = A->getType(), *TyB = B->getType();
6698 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6699 WidthB = TyB->getPrimitiveSizeInBits();
6700 unsigned MulWidth;
6701 Type *MulType;
6702 if (WidthB > WidthA) {
6703 MulWidth = WidthB;
6704 MulType = TyB;
6705 } else {
6706 MulWidth = WidthA;
6707 MulType = TyA;
6708 }
6709
6710 // In order to replace the original mul with a narrower mul.with.overflow,
6711 // all uses must ignore upper bits of the product. The number of used low
6712 // bits must be not greater than the width of mul.with.overflow.
6713 if (MulVal->hasNUsesOrMore(N: 2))
6714 for (User *U : MulVal->users()) {
6715 if (U == &I)
6716 continue;
6717 if (TruncInst *TI = dyn_cast<TruncInst>(Val: U)) {
6718 // Check if truncation ignores bits above MulWidth.
6719 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6720 if (TruncWidth > MulWidth)
6721 return nullptr;
6722 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Val: U)) {
6723 // Check if AND ignores bits above MulWidth.
6724 if (BO->getOpcode() != Instruction::And)
6725 return nullptr;
6726 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: BO->getOperand(i_nocapture: 1))) {
6727 const APInt &CVal = CI->getValue();
6728 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6729 return nullptr;
6730 } else {
6731 // In this case we could have the operand of the binary operation
6732 // being defined in another block, and performing the replacement
6733 // could break the dominance relation.
6734 return nullptr;
6735 }
6736 } else {
6737 // Other uses prohibit this transformation.
6738 return nullptr;
6739 }
6740 }
6741
6742 // Recognize patterns
6743 switch (I.getPredicate()) {
6744 case ICmpInst::ICMP_UGT: {
6745 // Recognize pattern:
6746 // mulval = mul(zext A, zext B)
6747 // cmp ugt mulval, max
6748 APInt MaxVal = APInt::getMaxValue(numBits: MulWidth);
6749 MaxVal = MaxVal.zext(width: OtherVal->getBitWidth());
6750 if (MaxVal.eq(RHS: *OtherVal))
6751 break; // Recognized
6752 return nullptr;
6753 }
6754
6755 case ICmpInst::ICMP_ULT: {
6756 // Recognize pattern:
6757 // mulval = mul(zext A, zext B)
6758 // cmp ule mulval, max + 1
6759 APInt MaxVal = APInt::getOneBitSet(numBits: OtherVal->getBitWidth(), BitNo: MulWidth);
6760 if (MaxVal.eq(RHS: *OtherVal))
6761 break; // Recognized
6762 return nullptr;
6763 }
6764
6765 default:
6766 return nullptr;
6767 }
6768
6769 InstCombiner::BuilderTy &Builder = IC.Builder;
6770 Builder.SetInsertPoint(MulInstr);
6771
6772 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6773 Value *MulA = A, *MulB = B;
6774 if (WidthA < MulWidth)
6775 MulA = Builder.CreateZExt(V: A, DestTy: MulType);
6776 if (WidthB < MulWidth)
6777 MulB = Builder.CreateZExt(V: B, DestTy: MulType);
6778 Value *Call =
6779 Builder.CreateIntrinsic(ID: Intrinsic::umul_with_overflow, OverloadTypes: MulType,
6780 Args: {MulA, MulB}, /*FMFSource=*/nullptr, Name: "umul");
6781 IC.addToWorklist(I: MulInstr);
6782
6783 // If there are uses of mul result other than the comparison, we know that
6784 // they are truncation or binary AND. Change them to use result of
6785 // mul.with.overflow and adjust properly mask/size.
6786 if (MulVal->hasNUsesOrMore(N: 2)) {
6787 Value *Mul = Builder.CreateExtractValue(Agg: Call, Idxs: 0, Name: "umul.value");
6788 for (User *U : make_early_inc_range(Range: MulVal->users())) {
6789 if (U == &I)
6790 continue;
6791 if (TruncInst *TI = dyn_cast<TruncInst>(Val: U)) {
6792 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6793 IC.replaceInstUsesWith(I&: *TI, V: Mul);
6794 else
6795 TI->setOperand(i_nocapture: 0, Val_nocapture: Mul);
6796 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Val: U)) {
6797 assert(BO->getOpcode() == Instruction::And);
6798 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6799 ConstantInt *CI = cast<ConstantInt>(Val: BO->getOperand(i_nocapture: 1));
6800 APInt ShortMask = CI->getValue().trunc(width: MulWidth);
6801 Value *ShortAnd = Builder.CreateAnd(LHS: Mul, RHS: ShortMask);
6802 Value *Zext = Builder.CreateZExt(V: ShortAnd, DestTy: BO->getType());
6803 IC.replaceInstUsesWith(I&: *BO, V: Zext);
6804 } else {
6805 llvm_unreachable("Unexpected Binary operation");
6806 }
6807 IC.addToWorklist(I: cast<Instruction>(Val: U));
6808 }
6809 }
6810
6811 // The original icmp gets replaced with the overflow value, maybe inverted
6812 // depending on predicate.
6813 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6814 Value *Res = Builder.CreateExtractValue(Agg: Call, Idxs: 1);
6815 return BinaryOperator::CreateNot(Op: Res);
6816 }
6817
6818 return ExtractValueInst::Create(Agg: Call, Idxs: 1);
6819}
6820
6821/// When performing a comparison against a constant, it is possible that not all
6822/// the bits in the LHS are demanded. This helper method computes the mask that
6823/// IS demanded.
6824static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth) {
6825 const APInt *RHS;
6826 if (!match(V: I.getOperand(i_nocapture: 1), P: m_APInt(Res&: RHS)))
6827 return APInt::getAllOnes(numBits: BitWidth);
6828
6829 // If this is a normal comparison, it demands all bits. If it is a sign bit
6830 // comparison, it only demands the sign bit.
6831 bool UnusedBit;
6832 if (isSignBitCheck(Pred: I.getPredicate(), RHS: *RHS, TrueIfSigned&: UnusedBit))
6833 return APInt::getSignMask(BitWidth);
6834
6835 switch (I.getPredicate()) {
6836 // For a UGT comparison, we don't care about any bits that
6837 // correspond to the trailing ones of the comparand. The value of these
6838 // bits doesn't impact the outcome of the comparison, because any value
6839 // greater than the RHS must differ in a bit higher than these due to carry.
6840 case ICmpInst::ICMP_UGT:
6841 return APInt::getBitsSetFrom(numBits: BitWidth, loBit: RHS->countr_one());
6842
6843 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6844 // Any value less than the RHS must differ in a higher bit because of carries.
6845 case ICmpInst::ICMP_ULT:
6846 return APInt::getBitsSetFrom(numBits: BitWidth, loBit: RHS->countr_zero());
6847
6848 default:
6849 return APInt::getAllOnes(numBits: BitWidth);
6850 }
6851}
6852
6853/// Check that one use is in the same block as the definition and all
6854/// other uses are in blocks dominated by a given block.
6855///
6856/// \param DI Definition
6857/// \param UI Use
6858/// \param DB Block that must dominate all uses of \p DI outside
6859/// the parent block
6860/// \return true when \p UI is the only use of \p DI in the parent block
6861/// and all other uses of \p DI are in blocks dominated by \p DB.
6862///
6863bool InstCombinerImpl::dominatesAllUses(const Instruction *DI,
6864 const Instruction *UI,
6865 const BasicBlock *DB) const {
6866 assert(DI && UI && "Instruction not defined\n");
6867 // Ignore incomplete definitions.
6868 if (!DI->getParent())
6869 return false;
6870 // DI and UI must be in the same block.
6871 if (DI->getParent() != UI->getParent())
6872 return false;
6873 // Protect from self-referencing blocks.
6874 if (DI->getParent() == DB)
6875 return false;
6876 for (const User *U : DI->users()) {
6877 auto *Usr = cast<Instruction>(Val: U);
6878 if (Usr != UI && !DT.dominates(A: DB, B: Usr->getParent()))
6879 return false;
6880 }
6881 return true;
6882}
6883
6884/// Return true when the instruction sequence within a block is select-cmp-br.
6885static bool isChainSelectCmpBranch(const SelectInst *SI) {
6886 const BasicBlock *BB = SI->getParent();
6887 if (!BB)
6888 return false;
6889 auto *BI = dyn_cast_or_null<CondBrInst>(Val: BB->getTerminator());
6890 if (!BI)
6891 return false;
6892 auto *IC = dyn_cast<ICmpInst>(Val: BI->getCondition());
6893 if (!IC || (IC->getOperand(i_nocapture: 0) != SI && IC->getOperand(i_nocapture: 1) != SI))
6894 return false;
6895 return true;
6896}
6897
6898/// True when a select result is replaced by one of its operands
6899/// in select-icmp sequence. This will eventually result in the elimination
6900/// of the select.
6901///
6902/// \param SI Select instruction
6903/// \param Icmp Compare instruction
6904/// \param SIOpd Operand that replaces the select
6905///
6906/// Notes:
6907/// - The replacement is global and requires dominator information
6908/// - The caller is responsible for the actual replacement
6909///
6910/// Example:
6911///
6912/// entry:
6913/// %4 = select i1 %3, %C* %0, %C* null
6914/// %5 = icmp eq %C* %4, null
6915/// br i1 %5, label %9, label %7
6916/// ...
6917/// ; <label>:7 ; preds = %entry
6918/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6919/// ...
6920///
6921/// can be transformed to
6922///
6923/// %5 = icmp eq %C* %0, null
6924/// %6 = select i1 %3, i1 %5, i1 true
6925/// br i1 %6, label %9, label %7
6926/// ...
6927/// ; <label>:7 ; preds = %entry
6928/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6929///
6930/// Similar when the first operand of the select is a constant or/and
6931/// the compare is for not equal rather than equal.
6932///
6933/// NOTE: The function is only called when the select and compare constants
6934/// are equal, the optimization can work only for EQ predicates. This is not a
6935/// major restriction since a NE compare should be 'normalized' to an equal
6936/// compare, which usually happens in the combiner and test case
6937/// select-cmp-br.ll checks for it.
6938bool InstCombinerImpl::replacedSelectWithOperand(SelectInst *SI,
6939 const ICmpInst *Icmp,
6940 const unsigned SIOpd) {
6941 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6942 if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) {
6943 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(Idx: 1);
6944 // The check for the single predecessor is not the best that can be
6945 // done. But it protects efficiently against cases like when SI's
6946 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6947 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6948 // replaced can be reached on either path. So the uniqueness check
6949 // guarantees that the path all uses of SI (outside SI's parent) are on
6950 // is disjoint from all other paths out of SI. But that information
6951 // is more expensive to compute, and the trade-off here is in favor
6952 // of compile-time. It should also be noticed that we check for a single
6953 // predecessor and not only uniqueness. This to handle the situation when
6954 // Succ and Succ1 points to the same basic block.
6955 if (Succ->getSinglePredecessor() && dominatesAllUses(DI: SI, UI: Icmp, DB: Succ)) {
6956 NumSel++;
6957 SI->replaceUsesOutsideBlock(V: SI->getOperand(i_nocapture: SIOpd), BB: SI->getParent());
6958 return true;
6959 }
6960 }
6961 return false;
6962}
6963
6964/// Try to fold the comparison based on range information we can get by checking
6965/// whether bits are known to be zero or one in the inputs.
6966Instruction *InstCombinerImpl::foldICmpUsingKnownBits(ICmpInst &I) {
6967 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
6968 Type *Ty = Op0->getType();
6969 ICmpInst::Predicate Pred = I.getPredicate();
6970
6971 // Get scalar or pointer size.
6972 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6973 ? Ty->getScalarSizeInBits()
6974 : DL.getPointerTypeSizeInBits(Ty->getScalarType());
6975
6976 if (!BitWidth)
6977 return nullptr;
6978
6979 KnownBits Op0Known(BitWidth);
6980 KnownBits Op1Known(BitWidth);
6981
6982 {
6983 // Don't use dominating conditions when folding icmp using known bits. This
6984 // may convert signed into unsigned predicates in ways that other passes
6985 // (especially IndVarSimplify) may not be able to reliably undo.
6986 SimplifyQuery Q = SQ.getWithoutDomCondCache().getWithInstruction(I: &I);
6987 if (SimplifyDemandedBits(I: &I, Op: 0, DemandedMask: getDemandedBitsLHSMask(I, BitWidth),
6988 Known&: Op0Known, Q))
6989 return &I;
6990
6991 if (SimplifyDemandedBits(I: &I, Op: 1, DemandedMask: APInt::getAllOnes(numBits: BitWidth), Known&: Op1Known, Q))
6992 return &I;
6993 }
6994
6995 if (!isa<Constant>(Val: Op0) && Op0Known.isConstant())
6996 return new ICmpInst(
6997 Pred, ConstantExpr::getIntegerValue(Ty, V: Op0Known.getConstant()), Op1);
6998 if (!isa<Constant>(Val: Op1) && Op1Known.isConstant())
6999 return new ICmpInst(
7000 Pred, Op0, ConstantExpr::getIntegerValue(Ty, V: Op1Known.getConstant()));
7001
7002 if (std::optional<bool> Res = ICmpInst::compare(LHS: Op0Known, RHS: Op1Known, Pred))
7003 return replaceInstUsesWith(I, V: ConstantInt::getBool(Ty: I.getType(), V: *Res));
7004
7005 // Given the known and unknown bits, compute a range that the LHS could be
7006 // in. Compute the Min, Max and RHS values based on the known bits. For the
7007 // EQ and NE we use unsigned values.
7008 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
7009 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
7010 if (I.isSigned()) {
7011 Op0Min = Op0Known.getSignedMinValue();
7012 Op0Max = Op0Known.getSignedMaxValue();
7013 Op1Min = Op1Known.getSignedMinValue();
7014 Op1Max = Op1Known.getSignedMaxValue();
7015 } else {
7016 Op0Min = Op0Known.getMinValue();
7017 Op0Max = Op0Known.getMaxValue();
7018 Op1Min = Op1Known.getMinValue();
7019 Op1Max = Op1Known.getMaxValue();
7020 }
7021
7022 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
7023 // min/max canonical compare with some other compare. That could lead to
7024 // conflict with select canonicalization and infinite looping.
7025 // FIXME: This constraint may go away if min/max intrinsics are canonical.
7026 auto isMinMaxCmp = [&](Instruction &Cmp) {
7027 if (!Cmp.hasOneUse())
7028 return false;
7029 Value *A, *B;
7030 SelectPatternFlavor SPF = matchSelectPattern(V: Cmp.user_back(), LHS&: A, RHS&: B).Flavor;
7031 if (!SelectPatternResult::isMinOrMax(SPF))
7032 return false;
7033 return match(V: Op0, P: m_MaxOrMin(Op0: m_Value(), Op1: m_Value())) ||
7034 match(V: Op1, P: m_MaxOrMin(Op0: m_Value(), Op1: m_Value()));
7035 };
7036 if (!isMinMaxCmp(I)) {
7037 switch (Pred) {
7038 default:
7039 break;
7040 case ICmpInst::ICMP_ULT: {
7041 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
7042 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7043 const APInt *CmpC;
7044 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7045 // A <u C -> A == C-1 if min(A)+1 == C
7046 if (*CmpC == Op0Min + 1)
7047 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7048 ConstantInt::get(Ty: Op1->getType(), V: *CmpC - 1));
7049 // X <u C --> X == 0, if the number of zero bits in the bottom of X
7050 // exceeds the log2 of C.
7051 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
7052 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7053 Constant::getNullValue(Ty: Op1->getType()));
7054 }
7055 break;
7056 }
7057 case ICmpInst::ICMP_UGT: {
7058 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
7059 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7060 const APInt *CmpC;
7061 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7062 // A >u C -> A == C+1 if max(a)-1 == C
7063 if (*CmpC == Op0Max - 1)
7064 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7065 ConstantInt::get(Ty: Op1->getType(), V: *CmpC + 1));
7066 // X >u C --> X != 0, if the number of zero bits in the bottom of X
7067 // exceeds the log2 of C.
7068 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
7069 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
7070 Constant::getNullValue(Ty: Op1->getType()));
7071 }
7072 break;
7073 }
7074 case ICmpInst::ICMP_SLT: {
7075 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
7076 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7077 const APInt *CmpC;
7078 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7079 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
7080 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7081 ConstantInt::get(Ty: Op1->getType(), V: *CmpC - 1));
7082 }
7083 break;
7084 }
7085 case ICmpInst::ICMP_SGT: {
7086 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
7087 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7088 const APInt *CmpC;
7089 if (match(V: Op1, P: m_APInt(Res&: CmpC))) {
7090 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
7091 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7092 ConstantInt::get(Ty: Op1->getType(), V: *CmpC + 1));
7093 }
7094 break;
7095 }
7096 }
7097 }
7098
7099 // Based on the range information we know about the LHS, see if we can
7100 // simplify this comparison. For example, (x&4) < 8 is always true.
7101 switch (Pred) {
7102 default:
7103 break;
7104 case ICmpInst::ICMP_EQ:
7105 case ICmpInst::ICMP_NE: {
7106 // If all bits are known zero except for one, then we know at most one bit
7107 // is set. If the comparison is against zero, then this is a check to see if
7108 // *that* bit is set.
7109 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
7110 if (Op1Known.isZero()) {
7111 // If the LHS is an AND with the same constant, look through it.
7112 Value *LHS = nullptr;
7113 const APInt *LHSC;
7114 if (!match(V: Op0, P: m_And(L: m_Value(V&: LHS), R: m_APInt(Res&: LHSC))) ||
7115 *LHSC != Op0KnownZeroInverted)
7116 LHS = Op0;
7117
7118 Value *X;
7119 const APInt *C1;
7120 if (match(V: LHS, P: m_Shl(L: m_Power2(V&: C1), R: m_Value(V&: X)))) {
7121 Type *XTy = X->getType();
7122 unsigned Log2C1 = C1->countr_zero();
7123 APInt C2 = Op0KnownZeroInverted;
7124 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
7125 if (C2Pow2.isPowerOf2()) {
7126 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
7127 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
7128 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
7129 unsigned Log2C2 = C2Pow2.countr_zero();
7130 auto *CmpC = ConstantInt::get(Ty: XTy, V: Log2C2 - Log2C1);
7131 auto NewPred =
7132 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT;
7133 return new ICmpInst(NewPred, X, CmpC);
7134 }
7135 }
7136 }
7137
7138 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
7139 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
7140 (Op0Known & Op1Known) == Op0Known)
7141 return new ICmpInst(CmpInst::getInversePredicate(pred: Pred), Op0,
7142 ConstantInt::getNullValue(Ty: Op1->getType()));
7143 break;
7144 }
7145 case ICmpInst::ICMP_SGE:
7146 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
7147 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7148 break;
7149 case ICmpInst::ICMP_SLE:
7150 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
7151 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7152 break;
7153 case ICmpInst::ICMP_UGE:
7154 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
7155 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7156 break;
7157 case ICmpInst::ICMP_ULE:
7158 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
7159 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7160 break;
7161 }
7162
7163 // Turn a signed comparison into an unsigned one if both operands are known to
7164 // have the same sign. Set samesign if possible (except for equality
7165 // predicates).
7166 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&
7167 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
7168 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {
7169 I.setPredicate(I.getUnsignedPredicate());
7170 I.setSameSign();
7171 return &I;
7172 }
7173
7174 return nullptr;
7175}
7176
7177/// If one operand of an icmp is effectively a bool (value range of {0,1}),
7178/// then try to reduce patterns based on that limit.
7179Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
7180 Value *X, *Y;
7181 CmpPredicate Pred;
7182
7183 // X must be 0 and bool must be true for "ULT":
7184 // X <u (zext i1 Y) --> (X == 0) & Y
7185 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))))) &&
7186 Y->getType()->isIntOrIntVectorTy(BitWidth: 1) && Pred == ICmpInst::ICMP_ULT)
7187 return BinaryOperator::CreateAnd(V1: Builder.CreateIsNull(Arg: X), V2: Y);
7188
7189 // X must be 0 or bool must be true for "ULE":
7190 // X <=u (sext i1 Y) --> (X == 0) | Y
7191 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))))) &&
7192 Y->getType()->isIntOrIntVectorTy(BitWidth: 1) && Pred == ICmpInst::ICMP_ULE)
7193 return BinaryOperator::CreateOr(V1: Builder.CreateIsNull(Arg: X), V2: Y);
7194
7195 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
7196 CmpPredicate Pred1, Pred2;
7197 const APInt *C;
7198 Instruction *ExtI;
7199 if (match(V: &I, P: m_c_ICmp(Pred&: Pred1, L: m_Value(V&: X),
7200 R: m_CombineAnd(Ps: m_Instruction(I&: ExtI),
7201 Ps: m_ZExtOrSExt(Op: m_ICmp(Pred&: Pred2, L: m_Deferred(V: X),
7202 R: m_APInt(Res&: C)))))) &&
7203 ICmpInst::isEquality(P: Pred1) && ICmpInst::isEquality(P: Pred2)) {
7204 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
7205 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(i: 0)->hasOneUse();
7206 auto CreateRangeCheck = [&] {
7207 Value *CmpV1 =
7208 Builder.CreateICmp(P: Pred1, LHS: X, RHS: Constant::getNullValue(Ty: X->getType()));
7209 Value *CmpV2 = Builder.CreateICmp(
7210 P: Pred1, LHS: X, RHS: ConstantInt::getSigned(Ty: X->getType(), V: IsSExt ? -1 : 1));
7211 return BinaryOperator::Create(
7212 Op: Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
7213 S1: CmpV1, S2: CmpV2);
7214 };
7215 if (C->isZero()) {
7216 if (Pred2 == ICmpInst::ICMP_EQ) {
7217 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
7218 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
7219 return replaceInstUsesWith(
7220 I, V: ConstantInt::getBool(Ty: I.getType(), V: Pred1 == ICmpInst::ICMP_NE));
7221 } else if (!IsSExt || HasOneUse) {
7222 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
7223 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
7224 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
7225 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X != -1
7226 return CreateRangeCheck();
7227 }
7228 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
7229 if (Pred2 == ICmpInst::ICMP_NE) {
7230 // icmp eq X, (zext (icmp ne X, 1)) --> false
7231 // icmp ne X, (zext (icmp ne X, 1)) --> true
7232 // icmp eq X, (sext (icmp ne X, -1)) --> false
7233 // icmp ne X, (sext (icmp ne X, -1)) --> true
7234 return replaceInstUsesWith(
7235 I, V: ConstantInt::getBool(Ty: I.getType(), V: Pred1 == ICmpInst::ICMP_NE));
7236 } else if (!IsSExt || HasOneUse) {
7237 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
7238 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
7239 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
7240 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
7241 return CreateRangeCheck();
7242 }
7243 } else {
7244 // when C != 0 && C != 1:
7245 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
7246 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
7247 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
7248 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
7249 // when C != 0 && C != -1:
7250 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
7251 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
7252 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
7253 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
7254 return ICmpInst::Create(
7255 Op: Instruction::ICmp, Pred: Pred1, S1: X,
7256 S2: ConstantInt::getSigned(Ty: X->getType(), V: Pred2 == ICmpInst::ICMP_NE
7257 ? (IsSExt ? -1 : 1)
7258 : 0));
7259 }
7260 }
7261
7262 return nullptr;
7263}
7264
7265/// If we have an icmp le or icmp ge instruction with a constant operand, turn
7266/// it into the appropriate icmp lt or icmp gt instruction. This transform
7267/// allows them to be folded in visitICmpInst.
7268static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
7269 ICmpInst::Predicate Pred = I.getPredicate();
7270 if (ICmpInst::isEquality(P: Pred) || !ICmpInst::isIntPredicate(P: Pred) ||
7271 InstCombiner::isCanonicalPredicate(Pred))
7272 return nullptr;
7273
7274 Value *Op0 = I.getOperand(i_nocapture: 0);
7275 Value *Op1 = I.getOperand(i_nocapture: 1);
7276 auto *Op1C = dyn_cast<Constant>(Val: Op1);
7277 if (!Op1C)
7278 return nullptr;
7279
7280 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, C: Op1C);
7281 if (!FlippedStrictness)
7282 return nullptr;
7283
7284 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
7285}
7286
7287/// If we have a comparison with a non-canonical predicate, if we can update
7288/// all the users, invert the predicate and adjust all the users.
7289CmpInst *InstCombinerImpl::canonicalizeICmpPredicate(CmpInst &I) {
7290 // Is the predicate already canonical?
7291 CmpInst::Predicate Pred = I.getPredicate();
7292 if (InstCombiner::isCanonicalPredicate(Pred))
7293 return nullptr;
7294
7295 // Can all users be adjusted to predicate inversion?
7296 if (!InstCombiner::canFreelyInvertAllUsersOf(V: &I, /*IgnoredUser=*/nullptr))
7297 return nullptr;
7298
7299 // Ok, we can canonicalize comparison!
7300 // Let's first invert the comparison's predicate.
7301 I.setPredicate(CmpInst::getInversePredicate(pred: Pred));
7302 I.setName(I.getName() + ".not");
7303
7304 // And, adapt users.
7305 freelyInvertAllUsersOf(V: &I);
7306
7307 return &I;
7308}
7309
7310/// Integer compare with boolean values can always be turned into bitwise ops.
7311static Instruction *canonicalizeICmpBool(ICmpInst &I,
7312 InstCombiner::BuilderTy &Builder) {
7313 Value *A = I.getOperand(i_nocapture: 0), *B = I.getOperand(i_nocapture: 1);
7314 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
7315
7316 // A boolean compared to true/false can be simplified to Op0/true/false in
7317 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
7318 // Cases not handled by InstSimplify are always 'not' of Op0.
7319 if (match(V: B, P: m_Zero())) {
7320 switch (I.getPredicate()) {
7321 case CmpInst::ICMP_EQ: // A == 0 -> !A
7322 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
7323 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
7324 return BinaryOperator::CreateNot(Op: A);
7325 default:
7326 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7327 }
7328 } else if (match(V: B, P: m_One())) {
7329 switch (I.getPredicate()) {
7330 case CmpInst::ICMP_NE: // A != 1 -> !A
7331 case CmpInst::ICMP_ULT: // A <u 1 -> !A
7332 case CmpInst::ICMP_SGT: // A >s -1 -> !A
7333 return BinaryOperator::CreateNot(Op: A);
7334 default:
7335 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7336 }
7337 }
7338
7339 switch (I.getPredicate()) {
7340 default:
7341 llvm_unreachable("Invalid icmp instruction!");
7342 case ICmpInst::ICMP_EQ:
7343 // icmp eq i1 A, B -> ~(A ^ B)
7344 return BinaryOperator::CreateNot(Op: Builder.CreateXor(LHS: A, RHS: B));
7345
7346 case ICmpInst::ICMP_NE:
7347 // icmp ne i1 A, B -> A ^ B
7348 return BinaryOperator::CreateXor(V1: A, V2: B);
7349
7350 case ICmpInst::ICMP_UGT:
7351 // icmp ugt -> icmp ult
7352 std::swap(a&: A, b&: B);
7353 [[fallthrough]];
7354 case ICmpInst::ICMP_ULT:
7355 // icmp ult i1 A, B -> ~A & B
7356 return BinaryOperator::CreateAnd(V1: Builder.CreateNot(V: A), V2: B);
7357
7358 case ICmpInst::ICMP_SGT:
7359 // icmp sgt -> icmp slt
7360 std::swap(a&: A, b&: B);
7361 [[fallthrough]];
7362 case ICmpInst::ICMP_SLT:
7363 // icmp slt i1 A, B -> A & ~B
7364 return BinaryOperator::CreateAnd(V1: Builder.CreateNot(V: B), V2: A);
7365
7366 case ICmpInst::ICMP_UGE:
7367 // icmp uge -> icmp ule
7368 std::swap(a&: A, b&: B);
7369 [[fallthrough]];
7370 case ICmpInst::ICMP_ULE:
7371 // icmp ule i1 A, B -> ~A | B
7372 return BinaryOperator::CreateOr(V1: Builder.CreateNot(V: A), V2: B);
7373
7374 case ICmpInst::ICMP_SGE:
7375 // icmp sge -> icmp sle
7376 std::swap(a&: A, b&: B);
7377 [[fallthrough]];
7378 case ICmpInst::ICMP_SLE:
7379 // icmp sle i1 A, B -> A | ~B
7380 return BinaryOperator::CreateOr(V1: Builder.CreateNot(V: B), V2: A);
7381 }
7382}
7383
7384// Transform pattern like:
7385// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7386// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7387// Into:
7388// (X l>> Y) != 0
7389// (X l>> Y) == 0
7390static Instruction *foldICmpWithHighBitMask(ICmpInst &Cmp,
7391 InstCombiner::BuilderTy &Builder) {
7392 CmpPredicate Pred, NewPred;
7393 Value *X, *Y;
7394 if (match(V: &Cmp,
7395 P: m_c_ICmp(Pred, L: m_OneUse(SubPattern: m_Shl(L: m_One(), R: m_Value(V&: Y))), R: m_Value(V&: X)))) {
7396 switch (Pred) {
7397 case ICmpInst::ICMP_ULE:
7398 NewPred = ICmpInst::ICMP_NE;
7399 break;
7400 case ICmpInst::ICMP_UGT:
7401 NewPred = ICmpInst::ICMP_EQ;
7402 break;
7403 default:
7404 return nullptr;
7405 }
7406 } else if (match(V: &Cmp, P: m_c_ICmp(Pred,
7407 L: m_OneUse(SubPattern: m_CombineOr(
7408 Ps: m_Not(V: m_Shl(L: m_AllOnes(), R: m_Value(V&: Y))),
7409 Ps: m_Add(L: m_Shl(L: m_One(), R: m_Value(V&: Y)),
7410 R: m_AllOnes()))),
7411 R: m_Value(V&: X)))) {
7412 // The variant with 'add' is not canonical, (the variant with 'not' is)
7413 // we only get it because it has extra uses, and can't be canonicalized,
7414
7415 switch (Pred) {
7416 case ICmpInst::ICMP_ULT:
7417 NewPred = ICmpInst::ICMP_NE;
7418 break;
7419 case ICmpInst::ICMP_UGE:
7420 NewPred = ICmpInst::ICMP_EQ;
7421 break;
7422 default:
7423 return nullptr;
7424 }
7425 } else
7426 return nullptr;
7427
7428 Value *NewX = Builder.CreateLShr(LHS: X, RHS: Y, Name: X->getName() + ".highbits");
7429 Constant *Zero = Constant::getNullValue(Ty: NewX->getType());
7430 return CmpInst::Create(Op: Instruction::ICmp, Pred: NewPred, S1: NewX, S2: Zero);
7431}
7432
7433static Instruction *foldVectorCmp(CmpInst &Cmp,
7434 InstCombiner::BuilderTy &Builder) {
7435 const CmpInst::Predicate Pred = Cmp.getPredicate();
7436 Value *LHS = Cmp.getOperand(i_nocapture: 0), *RHS = Cmp.getOperand(i_nocapture: 1);
7437 Value *V1, *V2;
7438
7439 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7440 Value *V = Builder.CreateCmp(Pred, LHS: X, RHS: Y, Name: Cmp.getName());
7441 if (auto *I = dyn_cast<Instruction>(Val: V))
7442 I->copyIRFlags(V: &Cmp);
7443 Module *M = Cmp.getModule();
7444 Function *F = Intrinsic::getOrInsertDeclaration(
7445 M, id: Intrinsic::vector_reverse, OverloadTys: V->getType());
7446 return CallInst::Create(Func: F, Args: V);
7447 };
7448
7449 if (match(V: LHS, P: m_VecReverse(Op0: m_Value(V&: V1)))) {
7450 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7451 if (match(V: RHS, P: m_VecReverse(Op0: m_Value(V&: V2))) &&
7452 (LHS->hasOneUse() || RHS->hasOneUse()))
7453 return createCmpReverse(Pred, V1, V2);
7454
7455 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7456 if (LHS->hasOneUse() && isSplatValue(V: RHS))
7457 return createCmpReverse(Pred, V1, RHS);
7458 }
7459 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7460 else if (isSplatValue(V: LHS) && match(V: RHS, P: m_OneUse(SubPattern: m_VecReverse(Op0: m_Value(V&: V2)))))
7461 return createCmpReverse(Pred, LHS, V2);
7462
7463 ArrayRef<int> M;
7464 if (!match(V: LHS, P: m_Shuffle(v1: m_Value(V&: V1), v2: m_Undef(), mask: m_Mask(M))))
7465 return nullptr;
7466
7467 // If both arguments of the cmp are shuffles that use the same mask and
7468 // shuffle within a single vector, move the shuffle after the cmp:
7469 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7470 Type *V1Ty = V1->getType();
7471 if (match(V: RHS, P: m_Shuffle(v1: m_Value(V&: V2), v2: m_Undef(), mask: m_SpecificMask(M))) &&
7472 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7473 Value *NewCmp = Builder.CreateCmp(Pred, LHS: V1, RHS: V2);
7474 return new ShuffleVectorInst(NewCmp, M);
7475 }
7476
7477 // Try to canonicalize compare with splatted operand and splat constant.
7478 // TODO: We could generalize this for more than splats. See/use the code in
7479 // InstCombiner::foldVectorBinop().
7480 Constant *C;
7481 if (!LHS->hasOneUse() || !match(V: RHS, P: m_Constant(C)))
7482 return nullptr;
7483
7484 // Length-changing splats are ok, so adjust the constants as needed:
7485 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7486 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7487 int MaskSplatIndex;
7488 if (ScalarC && match(Mask: M, P: m_SplatOrPoisonMask(MaskSplatIndex))) {
7489 // We allow poison in matching, but this transform removes it for safety.
7490 // Demanded elements analysis should be able to recover some/all of that.
7491 C = ConstantVector::getSplat(EC: cast<VectorType>(Val: V1Ty)->getElementCount(),
7492 Elt: ScalarC);
7493 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7494 Value *NewCmp = Builder.CreateCmp(Pred, LHS: V1, RHS: C);
7495 return new ShuffleVectorInst(NewCmp, NewM);
7496 }
7497
7498 return nullptr;
7499}
7500
7501// extract(uadd.with.overflow(A, B), 0) ult A
7502// -> extract(uadd.with.overflow(A, B), 1)
7503static Instruction *foldICmpOfUAddOv(ICmpInst &I) {
7504 CmpInst::Predicate Pred = I.getPredicate();
7505 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
7506
7507 Value *UAddOv;
7508 Value *A, *B;
7509 auto UAddOvResultPat = m_ExtractValue<0>(
7510 V: m_Intrinsic<Intrinsic::uadd_with_overflow>(Op0: m_Value(V&: A), Op1: m_Value(V&: B)));
7511 if (match(V: Op0, P: UAddOvResultPat) &&
7512 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7513 (Pred == ICmpInst::ICMP_EQ && match(V: Op1, P: m_ZeroInt()) &&
7514 (match(V: A, P: m_One()) || match(V: B, P: m_One()))) ||
7515 (Pred == ICmpInst::ICMP_NE && match(V: Op1, P: m_AllOnes()) &&
7516 (match(V: A, P: m_AllOnes()) || match(V: B, P: m_AllOnes())))))
7517 // extract(uadd.with.overflow(A, B), 0) < A
7518 // extract(uadd.with.overflow(A, 1), 0) == 0
7519 // extract(uadd.with.overflow(A, -1), 0) != -1
7520 UAddOv = cast<ExtractValueInst>(Val: Op0)->getAggregateOperand();
7521 else if (match(V: Op1, P: UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&
7522 (Op0 == A || Op0 == B))
7523 // A > extract(uadd.with.overflow(A, B), 0)
7524 UAddOv = cast<ExtractValueInst>(Val: Op1)->getAggregateOperand();
7525 else
7526 return nullptr;
7527
7528 return ExtractValueInst::Create(Agg: UAddOv, Idxs: 1);
7529}
7530
7531static Instruction *foldICmpInvariantGroup(ICmpInst &I) {
7532 if (!I.getOperand(i_nocapture: 0)->getType()->isPointerTy() ||
7533 NullPointerIsDefined(
7534 F: I.getParent()->getParent(),
7535 AS: I.getOperand(i_nocapture: 0)->getType()->getPointerAddressSpace())) {
7536 return nullptr;
7537 }
7538 Instruction *Op;
7539 if (match(V: I.getOperand(i_nocapture: 0), P: m_Instruction(I&: Op)) &&
7540 match(V: I.getOperand(i_nocapture: 1), P: m_Zero()) &&
7541 Op->isLaunderOrStripInvariantGroup()) {
7542 return ICmpInst::Create(Op: Instruction::ICmp, Pred: I.getPredicate(),
7543 S1: Op->getOperand(i: 0), S2: I.getOperand(i_nocapture: 1));
7544 }
7545 return nullptr;
7546}
7547
7548static Instruction *foldICmpOfVectorReduce(ICmpInst &I, const DataLayout &DL,
7549 IRBuilderBase &Builder) {
7550 if (!ICmpInst::isEquality(P: I.getPredicate()))
7551 return nullptr;
7552
7553 // The caller puts constants after non-constants.
7554 Value *Op = I.getOperand(i_nocapture: 0);
7555 Value *Const = I.getOperand(i_nocapture: 1);
7556
7557 // For Cond an equality condition, fold
7558 //
7559 // icmp (eq|ne) (vreduce_(or|and) Op), (Zero|AllOnes) ->
7560 // icmp (eq|ne) Op, (Zero|AllOnes)
7561 //
7562 // with a bitcast.
7563 Value *Vec;
7564 if ((match(V: Const, P: m_ZeroInt()) &&
7565 match(V: Op, P: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::vector_reduce_or>(
7566 Op0: m_Value(V&: Vec))))) ||
7567 (match(V: Const, P: m_AllOnes()) &&
7568 match(V: Op, P: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::vector_reduce_and>(
7569 Op0: m_Value(V&: Vec)))))) {
7570 auto *VecTy = dyn_cast<FixedVectorType>(Val: Vec->getType());
7571 if (!VecTy)
7572 return nullptr;
7573 Type *VecEltTy = VecTy->getElementType();
7574 unsigned ScalarBW =
7575 DL.getTypeSizeInBits(Ty: VecEltTy) * VecTy->getNumElements();
7576 if (!DL.fitsInLegalInteger(Width: ScalarBW))
7577 return nullptr;
7578 Type *ScalarTy = IntegerType::get(C&: I.getContext(), NumBits: ScalarBW);
7579 Value *NewConst = match(V: Const, P: m_ZeroInt())
7580 ? ConstantInt::get(Ty: ScalarTy, V: 0)
7581 : ConstantInt::getAllOnesValue(Ty: ScalarTy);
7582 return CmpInst::Create(Op: Instruction::ICmp, Pred: I.getPredicate(),
7583 S1: Builder.CreateBitCast(V: Vec, DestTy: ScalarTy), S2: NewConst);
7584 }
7585 return nullptr;
7586}
7587
7588/// This function folds patterns produced by lowering of reduce idioms, such as
7589/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7590/// attempts to generate fewer number of scalar comparisons instead of vector
7591/// comparisons when possible.
7592static Instruction *foldReductionIdiom(ICmpInst &I,
7593 InstCombiner::BuilderTy &Builder,
7594 const DataLayout &DL) {
7595 if (I.getType()->isVectorTy())
7596 return nullptr;
7597 CmpPredicate OuterPred, InnerPred;
7598 Value *LHS, *RHS;
7599
7600 // Match lowering of @llvm.vector.reduce.and. Turn
7601 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7602 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7603 /// %res = icmp <pred> i8 %scalar_ne, 0
7604 ///
7605 /// into
7606 ///
7607 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7608 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7609 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7610 ///
7611 /// for <pred> in {ne, eq}.
7612 if (!match(V: &I, P: m_ICmp(Pred&: OuterPred,
7613 L: m_OneUse(SubPattern: m_BitCast(Op: m_OneUse(
7614 SubPattern: m_ICmp(Pred&: InnerPred, L: m_Value(V&: LHS), R: m_Value(V&: RHS))))),
7615 R: m_Zero())))
7616 return nullptr;
7617 auto *LHSTy = dyn_cast<FixedVectorType>(Val: LHS->getType());
7618 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7619 return nullptr;
7620 unsigned NumBits =
7621 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7622 // TODO: Relax this to "not wider than max legal integer type"?
7623 if (!DL.isLegalInteger(Width: NumBits))
7624 return nullptr;
7625
7626 if (ICmpInst::isEquality(P: OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7627 auto *ScalarTy = Builder.getIntNTy(N: NumBits);
7628 LHS = Builder.CreateBitCast(V: LHS, DestTy: ScalarTy, Name: LHS->getName() + ".scalar");
7629 RHS = Builder.CreateBitCast(V: RHS, DestTy: ScalarTy, Name: RHS->getName() + ".scalar");
7630 return ICmpInst::Create(Op: Instruction::ICmp, Pred: OuterPred, S1: LHS, S2: RHS,
7631 Name: I.getName());
7632 }
7633
7634 return nullptr;
7635}
7636
7637// This helper will be called with icmp operands in both orders.
7638Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
7639 Value *Op0, Value *Op1,
7640 ICmpInst &CxtI) {
7641 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7642 if (auto *GEP = dyn_cast<GEPOperator>(Val: Op0))
7643 if (Instruction *NI = foldGEPICmp(GEPLHS: GEP, RHS: Op1, Cond: Pred, I&: CxtI))
7644 return NI;
7645
7646 if (auto *SI = dyn_cast<SelectInst>(Val: Op0))
7647 if (Instruction *NI = foldSelectICmp(Pred, SI, RHS: Op1, I: CxtI))
7648 return NI;
7649
7650 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Val: Op0)) {
7651 if (Instruction *Res = foldICmpWithMinMax(I&: CxtI, MinMax, Z: Op1, Pred))
7652 return Res;
7653
7654 if (Instruction *Res = foldICmpWithClamp(I&: CxtI, X: Op1, Min: MinMax))
7655 return Res;
7656 }
7657
7658 {
7659 Value *X;
7660 const APInt *C;
7661 // icmp X+Cst, X
7662 if (match(V: Op0, P: m_Add(L: m_Value(V&: X), R: m_APInt(Res&: C))) && Op1 == X)
7663 return foldICmpAddOpConst(X, C: *C, Pred);
7664 }
7665
7666 // abs(X) >= X --> true
7667 // abs(X) u<= X --> true
7668 // abs(X) < X --> false
7669 // abs(X) u> X --> false
7670 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7671 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7672 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7673 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7674 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7675 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7676 {
7677 Value *X;
7678 Constant *C;
7679 if (match(V: Op0, P: m_Intrinsic<Intrinsic::abs>(Op0: m_Value(V&: X), Op1: m_Constant(C))) &&
7680 match(V: Op1, P: m_Specific(V: X))) {
7681 Value *NullValue = Constant::getNullValue(Ty: X->getType());
7682 Value *AllOnesValue = Constant::getAllOnesValue(Ty: X->getType());
7683 const APInt SMin =
7684 APInt::getSignedMinValue(numBits: X->getType()->getScalarSizeInBits());
7685 bool IsIntMinPosion = C->isAllOnesValue();
7686 switch (Pred) {
7687 case CmpInst::ICMP_ULE:
7688 case CmpInst::ICMP_SGE:
7689 return replaceInstUsesWith(I&: CxtI, V: ConstantInt::getTrue(Ty: CxtI.getType()));
7690 case CmpInst::ICMP_UGT:
7691 case CmpInst::ICMP_SLT:
7692 return replaceInstUsesWith(I&: CxtI, V: ConstantInt::getFalse(Ty: CxtI.getType()));
7693 case CmpInst::ICMP_UGE:
7694 case CmpInst::ICMP_SLE:
7695 case CmpInst::ICMP_EQ: {
7696 return replaceInstUsesWith(
7697 I&: CxtI, V: IsIntMinPosion
7698 ? Builder.CreateICmpSGT(LHS: X, RHS: AllOnesValue)
7699 : Builder.CreateICmpULT(
7700 LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: SMin + 1)));
7701 }
7702 case CmpInst::ICMP_ULT:
7703 case CmpInst::ICMP_SGT:
7704 case CmpInst::ICMP_NE: {
7705 return replaceInstUsesWith(
7706 I&: CxtI, V: IsIntMinPosion
7707 ? Builder.CreateICmpSLT(LHS: X, RHS: NullValue)
7708 : Builder.CreateICmpUGT(
7709 LHS: X, RHS: ConstantInt::get(Ty: X->getType(), V: SMin)));
7710 }
7711 default:
7712 llvm_unreachable("Invalid predicate!");
7713 }
7714 }
7715 }
7716
7717 const SimplifyQuery Q = SQ.getWithInstruction(I: &CxtI);
7718 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, IC&: *this))
7719 return replaceInstUsesWith(I&: CxtI, V);
7720
7721 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7722 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(RHS: 1); };
7723 {
7724 if (match(V: Op0, P: m_UDiv(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckUGT1)))) {
7725 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
7726 Constant::getNullValue(Ty: Op1->getType()));
7727 }
7728
7729 if (!ICmpInst::isUnsigned(Pred) &&
7730 match(V: Op0, P: m_SDiv(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckUGT1)))) {
7731 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
7732 Constant::getNullValue(Ty: Op1->getType()));
7733 }
7734 }
7735
7736 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7737 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7738 {
7739 if (match(V: Op0, P: m_LShr(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckNE0)))) {
7740 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
7741 Constant::getNullValue(Ty: Op1->getType()));
7742 }
7743
7744 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7745 match(V: Op0, P: m_AShr(L: m_Specific(V: Op1), R: m_CheckedInt(CheckFn: CheckNE0)))) {
7746 return new ICmpInst(ICmpInst::getSwappedPredicate(pred: Pred), Op1,
7747 Constant::getNullValue(Ty: Op1->getType()));
7748 }
7749 }
7750
7751 // icmp (shl nsw/nuw X, L), (add nsw/nuw (shl nsw/nuw Y, L), K)
7752 // -> icmp X, (add nsw/nuw Y, K >> L)
7753 // We use AShr for nsw and LShr for nuw to safely peel off the shift.
7754 Value *X;
7755 uint64_t ShAmt;
7756 if (match(V: Op0, P: m_NUWShl(L: m_Value(V&: X), R: m_ConstantInt(V&: ShAmt))) &&
7757 !CxtI.isSigned()) {
7758 if (ShAmt >= X->getType()->getScalarSizeInBits())
7759 return nullptr;
7760 if (canEvaluateShifted(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
7761 Semantics: ShiftSemantics::Unsigned, CxtI: &CxtI)) {
7762 Value *NewOp1 = getShiftedValue(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
7763 Semantics: ShiftSemantics::Unsigned);
7764 return new ICmpInst(Pred, X, NewOp1);
7765 }
7766 }
7767
7768 if (match(V: Op0, P: m_NSWShl(L: m_Value(V&: X), R: m_ConstantInt(V&: ShAmt))) &&
7769 !CxtI.isUnsigned()) {
7770 if (ShAmt >= X->getType()->getScalarSizeInBits())
7771 return nullptr;
7772 if (canEvaluateShifted(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
7773 Semantics: ShiftSemantics::Signed, CxtI: &CxtI)) {
7774 Value *NewOp1 = getShiftedValue(V: Op1, NumBits: ShAmt, /*IsLeftShift=*/false,
7775 Semantics: ShiftSemantics::Signed);
7776 return new ICmpInst(Pred, X, NewOp1);
7777 }
7778 }
7779 return nullptr;
7780}
7781
7782Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
7783 bool Changed = false;
7784 const SimplifyQuery Q = SQ.getWithInstruction(I: &I);
7785 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
7786 unsigned Op0Cplxity = getComplexity(V: Op0);
7787 unsigned Op1Cplxity = getComplexity(V: Op1);
7788
7789 /// Orders the operands of the compare so that they are listed from most
7790 /// complex to least complex. This puts constants before unary operators,
7791 /// before binary operators.
7792 if (Op0Cplxity < Op1Cplxity) {
7793 I.swapOperands();
7794 std::swap(a&: Op0, b&: Op1);
7795 Changed = true;
7796 }
7797
7798 if (Value *V = simplifyICmpInst(Pred: I.getCmpPredicate(), LHS: Op0, RHS: Op1, Q))
7799 return replaceInstUsesWith(I, V);
7800
7801 // Comparing -val or val with non-zero is the same as just comparing val
7802 // ie, abs(val) != 0 -> val != 0
7803 if (I.getPredicate() == ICmpInst::ICMP_NE && match(V: Op1, P: m_Zero())) {
7804 Value *Cond, *SelectTrue, *SelectFalse;
7805 if (match(V: Op0, P: m_Select(C: m_Value(V&: Cond), L: m_Value(V&: SelectTrue),
7806 R: m_Value(V&: SelectFalse)))) {
7807 if (Value *V = dyn_castNegVal(V: SelectTrue)) {
7808 if (V == SelectFalse)
7809 return CmpInst::Create(Op: Instruction::ICmp, Pred: I.getPredicate(), S1: V, S2: Op1);
7810 } else if (Value *V = dyn_castNegVal(V: SelectFalse)) {
7811 if (V == SelectTrue)
7812 return CmpInst::Create(Op: Instruction::ICmp, Pred: I.getPredicate(), S1: V, S2: Op1);
7813 }
7814 }
7815 }
7816
7817 if (Instruction *Res = foldICmpTruncWithTruncOrExt(Cmp&: I, Q))
7818 return Res;
7819
7820 if (Op0->getType()->isIntOrIntVectorTy(BitWidth: 1))
7821 if (Instruction *Res = canonicalizeICmpBool(I, Builder))
7822 return Res;
7823
7824 if (Instruction *Res = canonicalizeCmpWithConstant(I))
7825 return Res;
7826
7827 if (Instruction *Res = canonicalizeICmpPredicate(I))
7828 return Res;
7829
7830 if (Instruction *Res = foldICmpWithConstant(Cmp&: I))
7831 return Res;
7832
7833 if (Instruction *Res = foldICmpWithDominatingICmp(Cmp&: I))
7834 return Res;
7835
7836 if (Instruction *Res = foldICmpUsingBoolRange(I))
7837 return Res;
7838
7839 if (Instruction *Res = foldICmpUsingKnownBits(I))
7840 return Res;
7841
7842 if (Instruction *Res = foldIsMultipleOfAPowerOfTwo(Cmp&: I))
7843 return Res;
7844
7845 // Test if the ICmpInst instruction is used exclusively by a select as
7846 // part of a minimum or maximum operation. If so, refrain from doing
7847 // any other folding. This helps out other analyses which understand
7848 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7849 // and CodeGen. And in this case, at least one of the comparison
7850 // operands has at least one user besides the compare (the select),
7851 // which would often largely negate the benefit of folding anyway.
7852 //
7853 // Do the same for the other patterns recognized by matchSelectPattern.
7854 if (I.hasOneUse())
7855 if (SelectInst *SI = dyn_cast<SelectInst>(Val: I.user_back())) {
7856 Value *A, *B;
7857 SelectPatternResult SPR = matchSelectPattern(V: SI, LHS&: A, RHS&: B);
7858 if (SPR.Flavor != SPF_UNKNOWN)
7859 return nullptr;
7860 }
7861
7862 // Do this after checking for min/max to prevent infinite looping.
7863 if (Instruction *Res = foldICmpWithZero(Cmp&: I))
7864 return Res;
7865
7866 Value *X;
7867 const APInt *C;
7868 if (I.getPredicate() == ICmpInst::ICMP_UGT &&
7869 match(V: Op0, P: m_UMax(Op0: m_Value(V&: X), Op1: m_APInt(Res&: C))) &&
7870 match(V: Op1, P: m_Not(V: m_Specific(V: X)))) {
7871 if (C->isNonNegative())
7872 return new ICmpInst(ICmpInst::ICMP_SLT, X,
7873 Constant::getNullValue(Ty: X->getType()));
7874 return new ICmpInst(ICmpInst::ICMP_UGT, X,
7875 ConstantInt::get(Ty: X->getType(), V: ~*C));
7876 }
7877
7878 if (I.getPredicate() == ICmpInst::ICMP_ULT &&
7879 match(V: Op0, P: m_UMax(Op0: m_Value(V&: X), Op1: m_APInt(Res&: C))) &&
7880 match(V: Op1, P: m_Not(V: m_Specific(V: X)))) {
7881 if (C->isNonNegative())
7882 return new ICmpInst(ICmpInst::ICMP_SGT, X,
7883 Constant::getAllOnesValue(Ty: X->getType()));
7884 return new ICmpInst(ICmpInst::ICMP_ULT, X,
7885 ConstantInt::get(Ty: X->getType(), V: ~*C));
7886 }
7887
7888 // FIXME: We only do this after checking for min/max to prevent infinite
7889 // looping caused by a reverse canonicalization of these patterns for min/max.
7890 // FIXME: The organization of folds is a mess. These would naturally go into
7891 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7892 // down here after the min/max restriction.
7893 ICmpInst::Predicate Pred = I.getPredicate();
7894 if (match(V: Op1, P: m_APInt(Res&: C))) {
7895 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7896 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7897 Constant *Zero = Constant::getNullValue(Ty: Op0->getType());
7898 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7899 }
7900
7901 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7902 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7903 Constant *AllOnes = Constant::getAllOnesValue(Ty: Op0->getType());
7904 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7905 }
7906 }
7907
7908 // The folds in here may rely on wrapping flags and special constants, so
7909 // they can break up min/max idioms in some cases but not seemingly similar
7910 // patterns.
7911 // FIXME: It may be possible to enhance select folding to make this
7912 // unnecessary. It may also be moot if we canonicalize to min/max
7913 // intrinsics.
7914 if (Instruction *Res = foldICmpBinOp(I, SQ: Q))
7915 return Res;
7916
7917 if (Instruction *Res = foldICmpInstWithConstant(Cmp&: I))
7918 return Res;
7919
7920 // Try to match comparison as a sign bit test. Intentionally do this after
7921 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7922 if (Instruction *New = foldSignBitTest(I))
7923 return New;
7924
7925 if (auto *PN = dyn_cast<PHINode>(Val: Op0))
7926 if (Instruction *NV = foldOpIntoPhi(I, PN))
7927 return NV;
7928 if (auto *PN = dyn_cast<PHINode>(Val: Op1))
7929 if (Instruction *NV = foldOpIntoPhi(I, PN))
7930 return NV;
7931
7932 if (Instruction *Res = foldICmpInstWithConstantNotInt(I))
7933 return Res;
7934
7935 if (Instruction *Res = foldICmpCommutative(Pred: I.getCmpPredicate(), Op0, Op1, CxtI&: I))
7936 return Res;
7937 if (Instruction *Res =
7938 foldICmpCommutative(Pred: I.getSwappedCmpPredicate(), Op0: Op1, Op1: Op0, CxtI&: I))
7939 return Res;
7940
7941 if (I.isCommutative()) {
7942 if (auto Pair = matchSymmetricPair(LHS: I.getOperand(i_nocapture: 0), RHS: I.getOperand(i_nocapture: 1))) {
7943 replaceOperand(I, OpNum: 0, V: Pair->first);
7944 replaceOperand(I, OpNum: 1, V: Pair->second);
7945 return &I;
7946 }
7947 }
7948
7949 // Fold icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2)
7950 // when all select arms are constants, via truth table.
7951 if (Instruction *R = foldCmpSelectOfConstants(I))
7952 return R;
7953
7954 // In case of a comparison with two select instructions having the same
7955 // condition, check whether one of the resulting branches can be simplified.
7956 // If so, just compare the other branch and select the appropriate result.
7957 // For example:
7958 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7959 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7960 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7961 // The icmp will result false for the false value of selects and the result
7962 // will depend upon the comparison of true values of selects if %cmp is
7963 // true. Thus, transform this into:
7964 // %cmp = icmp slt i32 %y, %z
7965 // %sel = select i1 %cond, i1 %cmp, i1 false
7966 // This handles similar cases to transform.
7967 {
7968 Value *Cond, *A, *B, *C, *D;
7969 if (match(V: Op0, P: m_Select(C: m_Value(V&: Cond), L: m_Value(V&: A), R: m_Value(V&: B))) &&
7970 match(V: Op1, P: m_Select(C: m_Specific(V: Cond), L: m_Value(V&: C), R: m_Value(V&: D))) &&
7971 (Op0->hasOneUse() || Op1->hasOneUse())) {
7972 // Check whether comparison of TrueValues can be simplified
7973 if (Value *Res = simplifyICmpInst(Pred, LHS: A, RHS: C, Q: SQ)) {
7974 Value *NewICMP = Builder.CreateICmp(P: Pred, LHS: B, RHS: D);
7975 return SelectInst::Create(
7976 C: Cond, S1: Res, S2: NewICMP, /*NameStr=*/"", /*InsertBefore=*/nullptr,
7977 MDFrom: ProfcheckDisableMetadataFixes ? nullptr : cast<Instruction>(Val: Op0));
7978 }
7979 // Check whether comparison of FalseValues can be simplified
7980 if (Value *Res = simplifyICmpInst(Pred, LHS: B, RHS: D, Q: SQ)) {
7981 Value *NewICMP = Builder.CreateICmp(P: Pred, LHS: A, RHS: C);
7982 return SelectInst::Create(
7983 C: Cond, S1: NewICMP, S2: Res, /*NameStr=*/"", /*InsertBefore=*/nullptr,
7984 MDFrom: ProfcheckDisableMetadataFixes ? nullptr : cast<Instruction>(Val: Op0));
7985 }
7986 }
7987 }
7988
7989 // icmp slt (sub nsw x, y), (add nsw x, y) --> icmp sgt y, 0
7990 // icmp ult (sub nuw x, y), (add nuw x, y) --> icmp ugt y, 0
7991 // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y) --> icmp eq y, 0
7992 {
7993 Value *A, *B;
7994 CmpPredicate CmpPred;
7995 if (match(V: &I, P: m_c_ICmp(Pred&: CmpPred, L: m_Sub(L: m_Value(V&: A), R: m_Value(V&: B)),
7996 R: m_c_Add(L: m_Deferred(V: A), R: m_Deferred(V: B))))) {
7997 auto *I0 = cast<OverflowingBinaryOperator>(Val: Op0);
7998 auto *I1 = cast<OverflowingBinaryOperator>(Val: Op1);
7999 bool I0NUW = I0->hasNoUnsignedWrap();
8000 bool I1NUW = I1->hasNoUnsignedWrap();
8001 bool I0NSW = I0->hasNoSignedWrap();
8002 bool I1NSW = I1->hasNoSignedWrap();
8003 if ((ICmpInst::isUnsigned(Pred) && I0NUW && I1NUW) ||
8004 (ICmpInst::isSigned(Pred) && I0NSW && I1NSW) ||
8005 (ICmpInst::isEquality(P: Pred) &&
8006 ((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {
8007 return new ICmpInst(CmpPredicate::getSwapped(P: CmpPred), B,
8008 ConstantInt::get(Ty: Op0->getType(), V: 0));
8009 }
8010 }
8011 }
8012
8013 // Try to optimize equality comparisons against alloca-based pointers.
8014 if (Op0->getType()->isPointerTy() && I.isEquality()) {
8015 assert(Op1->getType()->isPointerTy() &&
8016 "Comparing pointer with non-pointer?");
8017 if (auto *Alloca = dyn_cast<AllocaInst>(Val: getUnderlyingObject(V: Op0)))
8018 if (foldAllocaCmp(Alloca))
8019 return nullptr;
8020 if (auto *Alloca = dyn_cast<AllocaInst>(Val: getUnderlyingObject(V: Op1)))
8021 if (foldAllocaCmp(Alloca))
8022 return nullptr;
8023 }
8024
8025 if (Instruction *Res = foldICmpBitCast(Cmp&: I))
8026 return Res;
8027
8028 // TODO: Hoist this above the min/max bailout.
8029 if (Instruction *R = foldICmpWithCastOp(ICmp&: I))
8030 return R;
8031
8032 {
8033 Value *X, *Y;
8034 // Transform (X & ~Y) == 0 --> (X & Y) != 0
8035 // and (X & ~Y) != 0 --> (X & Y) == 0
8036 // if A is a power of 2.
8037 if (match(V: Op0, P: m_And(L: m_Value(V&: X), R: m_Not(V: m_Value(V&: Y)))) &&
8038 match(V: Op1, P: m_Zero()) && isKnownToBeAPowerOfTwo(V: X, OrZero: false, CxtI: &I) &&
8039 I.isEquality())
8040 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(LHS: X, RHS: Y),
8041 Op1);
8042
8043 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
8044 if (Op0->getType()->isIntOrIntVectorTy()) {
8045 bool ConsumesOp0, ConsumesOp1;
8046 if (isFreeToInvert(V: Op0, WillInvertAllUses: Op0->hasOneUse(), DoesConsume&: ConsumesOp0) &&
8047 isFreeToInvert(V: Op1, WillInvertAllUses: Op1->hasOneUse(), DoesConsume&: ConsumesOp1) &&
8048 (ConsumesOp0 || ConsumesOp1)) {
8049 Value *InvOp0 = getFreelyInverted(V: Op0, WillInvertAllUses: Op0->hasOneUse(), Builder: &Builder);
8050 Value *InvOp1 = getFreelyInverted(V: Op1, WillInvertAllUses: Op1->hasOneUse(), Builder: &Builder);
8051 assert(InvOp0 && InvOp1 &&
8052 "Mismatch between isFreeToInvert and getFreelyInverted");
8053 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
8054 }
8055 }
8056
8057 Instruction *AddI = nullptr;
8058 if (match(V: &I, P: m_UAddWithOverflow(L: m_Value(V&: X), R: m_Value(V&: Y),
8059 S: m_Instruction(I&: AddI))) &&
8060 isa<IntegerType>(Val: X->getType())) {
8061 Value *Result;
8062 Constant *Overflow;
8063 // m_UAddWithOverflow can match patterns that do not include an explicit
8064 // "add" instruction, so check the opcode of the matched op.
8065 if (AddI->getOpcode() == Instruction::Add &&
8066 OptimizeOverflowCheck(BinaryOp: Instruction::Add, /*Signed*/ IsSigned: false, LHS: X, RHS: Y, OrigI&: *AddI,
8067 Result, Overflow)) {
8068 replaceInstUsesWith(I&: *AddI, V: Result);
8069 eraseInstFromFunction(I&: *AddI);
8070 return replaceInstUsesWith(I, V: Overflow);
8071 }
8072 }
8073
8074 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
8075 if (match(V: Op0, P: m_NUWMul(L: m_ZExt(Op: m_Value(V&: X)), R: m_ZExt(Op: m_Value(V&: Y)))) &&
8076 match(V: Op1, P: m_APInt(Res&: C))) {
8077 if (Instruction *R = processUMulZExtIdiom(I, MulVal: Op0, OtherVal: C, IC&: *this))
8078 return R;
8079 }
8080
8081 // Signbit test folds
8082 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
8083 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
8084 Instruction *ExtI;
8085 if ((I.isUnsigned() || I.isEquality()) &&
8086 match(V: Op1,
8087 P: m_CombineAnd(Ps: m_Instruction(I&: ExtI), Ps: m_ZExtOrSExt(Op: m_Value(V&: Y)))) &&
8088 Y->getType()->getScalarSizeInBits() == 1 &&
8089 (Op0->hasOneUse() || Op1->hasOneUse())) {
8090 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
8091 Instruction *ShiftI;
8092 if (match(V: Op0, P: m_CombineAnd(Ps: m_Instruction(I&: ShiftI),
8093 Ps: m_Shr(L: m_Value(V&: X), R: m_SpecificIntAllowPoison(
8094 V: OpWidth - 1))))) {
8095 unsigned ExtOpc = ExtI->getOpcode();
8096 unsigned ShiftOpc = ShiftI->getOpcode();
8097 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
8098 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
8099 Value *SLTZero =
8100 Builder.CreateICmpSLT(LHS: X, RHS: Constant::getNullValue(Ty: X->getType()));
8101 Value *Cmp = Builder.CreateICmp(P: Pred, LHS: SLTZero, RHS: Y, Name: I.getName());
8102 return replaceInstUsesWith(I, V: Cmp);
8103 }
8104 }
8105 }
8106 }
8107
8108 if (Instruction *Res = foldICmpEquality(I))
8109 return Res;
8110
8111 if (Instruction *Res = foldICmpPow2Test(I, Builder))
8112 return Res;
8113
8114 if (Instruction *Res = foldICmpOfUAddOv(I))
8115 return Res;
8116
8117 if (Instruction *Res = foldICmpOfVectorReduce(I, DL, Builder))
8118 return Res;
8119
8120 // The 'cmpxchg' instruction returns an aggregate containing the old value and
8121 // an i1 which indicates whether or not we successfully did the swap.
8122 //
8123 // Replace comparisons between the old value and the expected value with the
8124 // indicator that 'cmpxchg' returns.
8125 //
8126 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
8127 // spuriously fail. In those cases, the old value may equal the expected
8128 // value but it is possible for the swap to not occur.
8129 if (I.getPredicate() == ICmpInst::ICMP_EQ)
8130 if (auto *EVI = dyn_cast<ExtractValueInst>(Val: Op0))
8131 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(Val: EVI->getAggregateOperand()))
8132 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
8133 !ACXI->isWeak())
8134 return ExtractValueInst::Create(Agg: ACXI, Idxs: 1);
8135
8136 if (Instruction *Res = foldICmpWithHighBitMask(Cmp&: I, Builder))
8137 return Res;
8138
8139 if (I.getType()->isVectorTy())
8140 if (Instruction *Res = foldVectorCmp(Cmp&: I, Builder))
8141 return Res;
8142
8143 if (Instruction *Res = foldICmpInvariantGroup(I))
8144 return Res;
8145
8146 if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
8147 return Res;
8148
8149 {
8150 Value *A;
8151 const APInt *C1, *C2;
8152 ICmpInst::Predicate Pred = I.getPredicate();
8153 if (ICmpInst::isEquality(P: Pred)) {
8154 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
8155 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
8156 if (match(V: Op0, P: m_And(L: m_SExt(Op: m_Value(V&: A)), R: m_APInt(Res&: C1))) &&
8157 match(V: Op1, P: m_APInt(Res&: C2))) {
8158 Type *InputTy = A->getType();
8159 unsigned InputBitWidth = InputTy->getScalarSizeInBits();
8160 // c2 must be non-negative at the bitwidth of a.
8161 if (C2->getActiveBits() < InputBitWidth) {
8162 APInt TruncC1 = C1->trunc(width: InputBitWidth);
8163 // Check if there are 1s in C1 high bits of size InputBitWidth.
8164 if (C1->uge(RHS: APInt::getOneBitSet(numBits: C1->getBitWidth(), BitNo: InputBitWidth)))
8165 TruncC1.setBit(InputBitWidth - 1);
8166 Value *AndInst = Builder.CreateAnd(LHS: A, RHS: TruncC1);
8167 return new ICmpInst(
8168 Pred, AndInst,
8169 ConstantInt::get(Ty: InputTy, V: C2->trunc(width: InputBitWidth)));
8170 }
8171 }
8172 }
8173 }
8174
8175 return Changed ? &I : nullptr;
8176}
8177
8178/// Fold fcmp ([us]itofp x, cst) if possible.
8179Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
8180 Instruction *LHSI,
8181 Constant *RHSC) {
8182 const APFloat *RHS;
8183 if (!match(V: RHSC, P: m_APFloat(Res&: RHS)))
8184 return nullptr;
8185
8186 // Get the width of the mantissa. We don't want to hack on conversions that
8187 // might lose information from the integer, e.g. "i64 -> float"
8188 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
8189 if (MantissaWidth == -1)
8190 return nullptr; // Unknown.
8191
8192 Type *IntTy = LHSI->getOperand(i: 0)->getType();
8193 unsigned IntWidth = IntTy->getScalarSizeInBits();
8194 bool LHSUnsigned = isa<UIToFPInst>(Val: LHSI);
8195
8196 if (I.isEquality()) {
8197 FCmpInst::Predicate P = I.getPredicate();
8198 bool IsExact = false;
8199 APSInt RHSCvt(IntWidth, LHSUnsigned);
8200 RHS->convertToInteger(Result&: RHSCvt, RM: APFloat::rmNearestTiesToEven, IsExact: &IsExact);
8201
8202 // If the floating point constant isn't an integer value, we know if we will
8203 // ever compare equal / not equal to it.
8204 if (!IsExact) {
8205 // TODO: Can never be -0.0 and other non-representable values
8206 APFloat RHSRoundInt(*RHS);
8207 RHSRoundInt.roundToIntegral(RM: APFloat::rmNearestTiesToEven);
8208 if (*RHS != RHSRoundInt) {
8209 if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ)
8210 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8211
8212 assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE);
8213 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8214 }
8215 }
8216
8217 // TODO: If the constant is exactly representable, is it always OK to do
8218 // equality compares as integer?
8219 }
8220
8221 // Check to see that the input is converted from an integer type that is small
8222 // enough that preserves all bits. TODO: check here for "known" sign bits.
8223 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
8224
8225 // Following test does NOT adjust IntWidth downwards for signed inputs,
8226 // because the most negative value still requires all the mantissa bits
8227 // to distinguish it from one less than that value.
8228 if ((int)IntWidth > MantissaWidth) {
8229 // Conversion would lose accuracy. Check if loss can impact comparison.
8230 int Exp = ilogb(Arg: *RHS);
8231 if (Exp == APFloat::IEK_Inf) {
8232 int MaxExponent = ilogb(Arg: APFloat::getLargest(Sem: RHS->getSemantics()));
8233 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
8234 // Conversion could create infinity.
8235 return nullptr;
8236 } else {
8237 // Note that if RHS is zero or NaN, then Exp is negative
8238 // and first condition is trivially false.
8239 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
8240 // Conversion could affect comparison.
8241 return nullptr;
8242 }
8243 }
8244
8245 // Otherwise, we can potentially simplify the comparison. We know that it
8246 // will always come through as an integer value and we know the constant is
8247 // not a NAN (it would have been previously simplified).
8248 assert(!RHS->isNaN() && "NaN comparison not already folded!");
8249
8250 ICmpInst::Predicate Pred;
8251 switch (I.getPredicate()) {
8252 default:
8253 llvm_unreachable("Unexpected predicate!");
8254 case FCmpInst::FCMP_UEQ:
8255 case FCmpInst::FCMP_OEQ:
8256 Pred = ICmpInst::ICMP_EQ;
8257 break;
8258 case FCmpInst::FCMP_UGT:
8259 case FCmpInst::FCMP_OGT:
8260 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
8261 break;
8262 case FCmpInst::FCMP_UGE:
8263 case FCmpInst::FCMP_OGE:
8264 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
8265 break;
8266 case FCmpInst::FCMP_ULT:
8267 case FCmpInst::FCMP_OLT:
8268 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
8269 break;
8270 case FCmpInst::FCMP_ULE:
8271 case FCmpInst::FCMP_OLE:
8272 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
8273 break;
8274 case FCmpInst::FCMP_UNE:
8275 case FCmpInst::FCMP_ONE:
8276 Pred = ICmpInst::ICMP_NE;
8277 break;
8278 case FCmpInst::FCMP_ORD:
8279 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8280 case FCmpInst::FCMP_UNO:
8281 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8282 }
8283
8284 // Now we know that the APFloat is a normal number, zero or inf.
8285
8286 // See if the FP constant is too large for the integer. For example,
8287 // comparing an i8 to 300.0.
8288 if (!LHSUnsigned) {
8289 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
8290 // and large values.
8291 APFloat SMax(RHS->getSemantics());
8292 SMax.convertFromAPInt(Input: APInt::getSignedMaxValue(numBits: IntWidth), IsSigned: true,
8293 RM: APFloat::rmNearestTiesToEven);
8294 if (SMax < *RHS) { // smax < 13123.0
8295 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
8296 Pred == ICmpInst::ICMP_SLE)
8297 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8298 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8299 }
8300 } else {
8301 // If the RHS value is > UnsignedMax, fold the comparison. This handles
8302 // +INF and large values.
8303 APFloat UMax(RHS->getSemantics());
8304 UMax.convertFromAPInt(Input: APInt::getMaxValue(numBits: IntWidth), IsSigned: false,
8305 RM: APFloat::rmNearestTiesToEven);
8306 if (UMax < *RHS) { // umax < 13123.0
8307 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
8308 Pred == ICmpInst::ICMP_ULE)
8309 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8310 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8311 }
8312 }
8313
8314 if (!LHSUnsigned) {
8315 // See if the RHS value is < SignedMin.
8316 APFloat SMin(RHS->getSemantics());
8317 SMin.convertFromAPInt(Input: APInt::getSignedMinValue(numBits: IntWidth), IsSigned: true,
8318 RM: APFloat::rmNearestTiesToEven);
8319 if (SMin > *RHS) { // smin > 12312.0
8320 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
8321 Pred == ICmpInst::ICMP_SGE)
8322 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8323 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8324 }
8325 } else {
8326 // See if the RHS value is < UnsignedMin.
8327 APFloat UMin(RHS->getSemantics());
8328 UMin.convertFromAPInt(Input: APInt::getMinValue(numBits: IntWidth), IsSigned: false,
8329 RM: APFloat::rmNearestTiesToEven);
8330 if (UMin > *RHS) { // umin > 12312.0
8331 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
8332 Pred == ICmpInst::ICMP_UGE)
8333 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8334 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8335 }
8336 }
8337
8338 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
8339 // [0, UMAX], but it may still be fractional. Check whether this is the case
8340 // using the IsExact flag.
8341 // Don't do this for zero, because -0.0 is not fractional.
8342 APSInt RHSInt(IntWidth, LHSUnsigned);
8343 bool IsExact;
8344 RHS->convertToInteger(Result&: RHSInt, RM: APFloat::rmTowardZero, IsExact: &IsExact);
8345 if (!RHS->isZero()) {
8346 if (!IsExact) {
8347 // If we had a comparison against a fractional value, we have to adjust
8348 // the compare predicate and sometimes the value. RHSC is rounded towards
8349 // zero at this point.
8350 switch (Pred) {
8351 default:
8352 llvm_unreachable("Unexpected integer comparison!");
8353 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
8354 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8355 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
8356 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8357 case ICmpInst::ICMP_ULE:
8358 // (float)int <= 4.4 --> int <= 4
8359 // (float)int <= -4.4 --> false
8360 if (RHS->isNegative())
8361 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8362 break;
8363 case ICmpInst::ICMP_SLE:
8364 // (float)int <= 4.4 --> int <= 4
8365 // (float)int <= -4.4 --> int < -4
8366 if (RHS->isNegative())
8367 Pred = ICmpInst::ICMP_SLT;
8368 break;
8369 case ICmpInst::ICMP_ULT:
8370 // (float)int < -4.4 --> false
8371 // (float)int < 4.4 --> int <= 4
8372 if (RHS->isNegative())
8373 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8374 Pred = ICmpInst::ICMP_ULE;
8375 break;
8376 case ICmpInst::ICMP_SLT:
8377 // (float)int < -4.4 --> int < -4
8378 // (float)int < 4.4 --> int <= 4
8379 if (!RHS->isNegative())
8380 Pred = ICmpInst::ICMP_SLE;
8381 break;
8382 case ICmpInst::ICMP_UGT:
8383 // (float)int > 4.4 --> int > 4
8384 // (float)int > -4.4 --> true
8385 if (RHS->isNegative())
8386 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8387 break;
8388 case ICmpInst::ICMP_SGT:
8389 // (float)int > 4.4 --> int > 4
8390 // (float)int > -4.4 --> int >= -4
8391 if (RHS->isNegative())
8392 Pred = ICmpInst::ICMP_SGE;
8393 break;
8394 case ICmpInst::ICMP_UGE:
8395 // (float)int >= -4.4 --> true
8396 // (float)int >= 4.4 --> int > 4
8397 if (RHS->isNegative())
8398 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8399 Pred = ICmpInst::ICMP_UGT;
8400 break;
8401 case ICmpInst::ICMP_SGE:
8402 // (float)int >= -4.4 --> int >= -4
8403 // (float)int >= 4.4 --> int > 4
8404 if (!RHS->isNegative())
8405 Pred = ICmpInst::ICMP_SGT;
8406 break;
8407 }
8408 }
8409 }
8410
8411 // Lower this FP comparison into an appropriate integer version of the
8412 // comparison.
8413 return new ICmpInst(Pred, LHSI->getOperand(i: 0),
8414 ConstantInt::get(Ty: LHSI->getOperand(i: 0)->getType(), V: RHSInt));
8415}
8416
8417/// Fold fcmp/icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2)
8418/// where all true/false values are constants that allow the compare to be
8419/// constant-folded for every combination of C1 and C2.
8420/// We compute a 4-entry truth table and use createLogicFromTable to
8421/// synthesize a boolean expression of C1 and C2.
8422Instruction *InstCombinerImpl::foldCmpSelectOfConstants(CmpInst &I) {
8423 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
8424 Value *C1, *C2;
8425 Constant *TV1, *FV1, *TV2, *FV2;
8426
8427 if (!match(V: Op0, P: m_Select(C: m_Value(V&: C1), L: m_Constant(C&: TV1), R: m_Constant(C&: FV1))) ||
8428 !match(V: Op1, P: m_Select(C: m_Value(V&: C2), L: m_Constant(C&: TV2), R: m_Constant(C&: FV2))))
8429 return nullptr;
8430
8431 if (I.getType() != C1->getType() || I.getType() != C2->getType())
8432 return nullptr;
8433
8434 unsigned Pred = I.getPredicate();
8435 const DataLayout &DL = I.getDataLayout();
8436
8437 Constant *Res00 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: FV1, RHS: FV2, DL);
8438 Constant *Res01 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: FV1, RHS: TV2, DL);
8439 Constant *Res10 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: TV1, RHS: FV2, DL);
8440 Constant *Res11 = ConstantFoldCompareInstOperands(Predicate: Pred, LHS: TV1, RHS: TV2, DL);
8441
8442 if (!Res00 || !Res01 || !Res10 || !Res11)
8443 return nullptr;
8444
8445 if ((!Res00->isNullValue() && !Res00->isAllOnesValue()) ||
8446 (!Res01->isNullValue() && !Res01->isAllOnesValue()) ||
8447 (!Res10->isNullValue() && !Res10->isAllOnesValue()) ||
8448 (!Res11->isNullValue() && !Res11->isAllOnesValue()))
8449 return nullptr;
8450
8451 std::bitset<4> Table;
8452 if (!Res00->isNullValue())
8453 Table.set(position: 0);
8454 if (!Res01->isNullValue())
8455 Table.set(position: 1);
8456 if (!Res10->isNullValue())
8457 Table.set(position: 2);
8458 if (!Res11->isNullValue())
8459 Table.set(position: 3);
8460
8461 Value *Res = createLogicFromTable(Table, Op0: C1, Op1: C2, Builder,
8462 HasOneUse: Op0->hasOneUse() && Op1->hasOneUse());
8463 if (!Res)
8464 return nullptr;
8465 return replaceInstUsesWith(I, V: Res);
8466}
8467
8468/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
8469static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,
8470 Constant *RHSC) {
8471 // When C is not 0.0 and infinities are not allowed:
8472 // (C / X) < 0.0 is a sign-bit test of X
8473 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
8474 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
8475 //
8476 // Proof:
8477 // Multiply (C / X) < 0.0 by X * X / C.
8478 // - X is non zero, if it is the flag 'ninf' is violated.
8479 // - C defines the sign of X * X * C. Thus it also defines whether to swap
8480 // the predicate. C is also non zero by definition.
8481 //
8482 // Thus X * X / C is non zero and the transformation is valid. [qed]
8483
8484 FCmpInst::Predicate Pred = I.getPredicate();
8485
8486 // Check that predicates are valid.
8487 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
8488 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
8489 return nullptr;
8490
8491 // Check that RHS operand is zero.
8492 if (!match(V: RHSC, P: m_AnyZeroFP()))
8493 return nullptr;
8494
8495 // Check fastmath flags ('ninf').
8496 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
8497 return nullptr;
8498
8499 // Check the properties of the dividend. It must not be zero to avoid a
8500 // division by zero (see Proof).
8501 const APFloat *C;
8502 if (!match(V: LHSI->getOperand(i: 0), P: m_APFloat(Res&: C)))
8503 return nullptr;
8504
8505 if (C->isZero())
8506 return nullptr;
8507
8508 // Get swapped predicate if necessary.
8509 if (C->isNegative())
8510 Pred = I.getSwappedPredicate();
8511
8512 return new FCmpInst(Pred, LHSI->getOperand(i: 1), RHSC, "", &I);
8513}
8514
8515// Transform 'fptrunc(x) cmp C' to 'x cmp ext(C)' if possible.
8516// Patterns include:
8517// fptrunc(x) < C --> x < ext(C)
8518// fptrunc(x) <= C --> x <= ext(C)
8519// fptrunc(x) > C --> x > ext(C)
8520// fptrunc(x) >= C --> x >= ext(C)
8521// fptrunc(x) ord/uno C --> x ord/uno 0
8522// where 'ext(C)' is the extension of 'C' to the type of 'x' with a small bias
8523// due to precision loss.
8524static Instruction *foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc,
8525 const Constant &C) {
8526 FCmpInst::Predicate Pred = I.getPredicate();
8527 Type *DestType = FPTrunc.getOperand(i: 0)->getType();
8528
8529 const APFloat *CValue;
8530 // TODO: support vec
8531 if (!match(V: &C, P: m_APFloat(Res&: CValue)))
8532 return nullptr;
8533
8534 // Handle ord/uno
8535 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
8536 assert(!CValue->isNaN() &&
8537 "X ord/uno NaN should be folded away by simplifyFCmpInst()");
8538 return new FCmpInst(Pred, FPTrunc.getOperand(i: 0),
8539 ConstantFP::getZero(Ty: DestType), "", &I);
8540 }
8541
8542 // Handle <, >, <=, >=
8543 bool RoundDown = false;
8544
8545 if (Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE ||
8546 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT)
8547 RoundDown = true;
8548 else if (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT ||
8549 Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)
8550 RoundDown = false;
8551 else
8552 return nullptr;
8553
8554 if (CValue->isNaN() || CValue->isInfinity())
8555 return nullptr;
8556
8557 auto ConvertFltSema = [](const APFloat &Src, const fltSemantics &Sema) {
8558 bool LosesInfo;
8559 APFloat Dest = Src;
8560 Dest.convert(ToSemantics: Sema, RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
8561 return Dest;
8562 };
8563
8564 auto NextValue = [](const APFloat &Value, bool RoundDown) {
8565 APFloat NextValue = Value;
8566 NextValue.next(nextDown: RoundDown);
8567 return NextValue;
8568 };
8569
8570 APFloat NextCValue = NextValue(*CValue, RoundDown);
8571
8572 const fltSemantics &DestFltSema =
8573 DestType->getScalarType()->getFltSemantics();
8574
8575 APFloat ExtCValue = ConvertFltSema(*CValue, DestFltSema);
8576 APFloat ExtNextCValue = ConvertFltSema(NextCValue, DestFltSema);
8577
8578 // When 'NextCValue' is infinity, use an imaged 'NextCValue' that equals
8579 // 'CValue + bias' to avoid the infinity after conversion. The bias is
8580 // estimated as 'CValue - PrevCValue', where 'PrevCValue' is the previous
8581 // value of 'CValue'.
8582 if (NextCValue.isInfinity()) {
8583 APFloat PrevCValue = NextValue(*CValue, !RoundDown);
8584 APFloat Bias = ConvertFltSema(*CValue - PrevCValue, DestFltSema);
8585
8586 ExtNextCValue = ExtCValue + Bias;
8587 }
8588
8589 APFloat ExtMidValue =
8590 scalbn(X: ExtCValue + ExtNextCValue, Exp: -1, RM: APFloat::rmNearestTiesToEven);
8591
8592 const fltSemantics &SrcFltSema =
8593 C.getType()->getScalarType()->getFltSemantics();
8594
8595 // 'MidValue' might be rounded to 'NextCValue'. Correct it here.
8596 APFloat MidValue = ConvertFltSema(ExtMidValue, SrcFltSema);
8597 if (MidValue != *CValue)
8598 ExtMidValue.next(nextDown: !RoundDown);
8599
8600 // Check whether 'ExtMidValue' is a valid result since the assumption on
8601 // imaged 'NextCValue' might not hold for new float types.
8602 // ppc_fp128 can't pass here when converting from max float because of
8603 // APFloat implementation.
8604 if (NextCValue.isInfinity()) {
8605 // ExtMidValue --- narrowed ---> Finite
8606 if (ConvertFltSema(ExtMidValue, SrcFltSema).isInfinity())
8607 return nullptr;
8608
8609 // NextExtMidValue --- narrowed ---> Infinity
8610 APFloat NextExtMidValue = NextValue(ExtMidValue, RoundDown);
8611 if (ConvertFltSema(NextExtMidValue, SrcFltSema).isFinite())
8612 return nullptr;
8613 }
8614
8615 return new FCmpInst(Pred, FPTrunc.getOperand(i: 0),
8616 ConstantFP::get(Ty: DestType, V: ExtMidValue), "", &I);
8617}
8618
8619/// Optimize fabs(X) compared with zero.
8620static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {
8621 Value *X;
8622 if (!match(V: I.getOperand(i_nocapture: 0), P: m_FAbs(Op0: m_Value(V&: X))))
8623 return nullptr;
8624
8625 const APFloat *C;
8626 if (!match(V: I.getOperand(i_nocapture: 1), P: m_APFloat(Res&: C)))
8627 return nullptr;
8628
8629 if (!C->isPosZero()) {
8630 if (!C->isSmallestNormalized())
8631 return nullptr;
8632
8633 const Function *F = I.getFunction();
8634 DenormalMode Mode = F->getDenormalMode(FPType: C->getSemantics());
8635 if (Mode.Input == DenormalMode::PreserveSign ||
8636 Mode.Input == DenormalMode::PositiveZero) {
8637
8638 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8639 Constant *Zero = ConstantFP::getZero(Ty: X->getType());
8640 return new FCmpInst(P, X, Zero, "", I);
8641 };
8642
8643 switch (I.getPredicate()) {
8644 case FCmpInst::FCMP_OLT:
8645 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
8646 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
8647 case FCmpInst::FCMP_UGE:
8648 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
8649 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
8650 case FCmpInst::FCMP_OGE:
8651 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
8652 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
8653 case FCmpInst::FCMP_ULT:
8654 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
8655 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
8656 default:
8657 break;
8658 }
8659 }
8660
8661 return nullptr;
8662 }
8663
8664 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8665 I->setPredicate(P);
8666 return IC.replaceOperand(I&: *I, OpNum: 0, V: X);
8667 };
8668
8669 switch (I.getPredicate()) {
8670 case FCmpInst::FCMP_UGE:
8671 case FCmpInst::FCMP_OLT:
8672 // fabs(X) >= 0.0 --> true
8673 // fabs(X) < 0.0 --> false
8674 llvm_unreachable("fcmp should have simplified");
8675
8676 case FCmpInst::FCMP_OGT:
8677 // fabs(X) > 0.0 --> X != 0.0
8678 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8679
8680 case FCmpInst::FCMP_UGT:
8681 // fabs(X) u> 0.0 --> X u!= 0.0
8682 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8683
8684 case FCmpInst::FCMP_OLE:
8685 // fabs(X) <= 0.0 --> X == 0.0
8686 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8687
8688 case FCmpInst::FCMP_ULE:
8689 // fabs(X) u<= 0.0 --> X u== 0.0
8690 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8691
8692 case FCmpInst::FCMP_OGE:
8693 // fabs(X) >= 0.0 --> !isnan(X)
8694 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8695 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8696
8697 case FCmpInst::FCMP_ULT:
8698 // fabs(X) u< 0.0 --> isnan(X)
8699 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8700 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8701
8702 case FCmpInst::FCMP_OEQ:
8703 case FCmpInst::FCMP_UEQ:
8704 case FCmpInst::FCMP_ONE:
8705 case FCmpInst::FCMP_UNE:
8706 case FCmpInst::FCMP_ORD:
8707 case FCmpInst::FCMP_UNO:
8708 // Look through the fabs() because it doesn't change anything but the sign.
8709 // fabs(X) == 0.0 --> X == 0.0,
8710 // fabs(X) != 0.0 --> X != 0.0
8711 // isnan(fabs(X)) --> isnan(X)
8712 // !isnan(fabs(X) --> !isnan(X)
8713 return replacePredAndOp0(&I, I.getPredicate(), X);
8714
8715 default:
8716 return nullptr;
8717 }
8718}
8719
8720/// Optimize sqrt(X) compared with zero.
8721static Instruction *foldSqrtWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {
8722 Value *X;
8723 if (!match(V: I.getOperand(i_nocapture: 0), P: m_Sqrt(Op0: m_Value(V&: X))))
8724 return nullptr;
8725
8726 if (!match(V: I.getOperand(i_nocapture: 1), P: m_PosZeroFP()))
8727 return nullptr;
8728
8729 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8730 I.setPredicate(P);
8731 return IC.replaceOperand(I, OpNum: 0, V: X);
8732 };
8733
8734 // Clear ninf flag if sqrt doesn't have it.
8735 if (!cast<Instruction>(Val: I.getOperand(i_nocapture: 0))->hasNoInfs())
8736 I.setHasNoInfs(false);
8737
8738 switch (I.getPredicate()) {
8739 case FCmpInst::FCMP_OLT:
8740 case FCmpInst::FCMP_UGE:
8741 // sqrt(X) < 0.0 --> false
8742 // sqrt(X) u>= 0.0 --> true
8743 llvm_unreachable("fcmp should have simplified");
8744 case FCmpInst::FCMP_ULT:
8745 case FCmpInst::FCMP_ULE:
8746 case FCmpInst::FCMP_OGT:
8747 case FCmpInst::FCMP_OGE:
8748 case FCmpInst::FCMP_OEQ:
8749 case FCmpInst::FCMP_UNE:
8750 // sqrt(X) u< 0.0 --> X u< 0.0
8751 // sqrt(X) u<= 0.0 --> X u<= 0.0
8752 // sqrt(X) > 0.0 --> X > 0.0
8753 // sqrt(X) >= 0.0 --> X >= 0.0
8754 // sqrt(X) == 0.0 --> X == 0.0
8755 // sqrt(X) u!= 0.0 --> X u!= 0.0
8756 return IC.replaceOperand(I, OpNum: 0, V: X);
8757
8758 case FCmpInst::FCMP_OLE:
8759 // sqrt(X) <= 0.0 --> X == 0.0
8760 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8761 case FCmpInst::FCMP_UGT:
8762 // sqrt(X) u> 0.0 --> X u!= 0.0
8763 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8764 case FCmpInst::FCMP_UEQ:
8765 // sqrt(X) u== 0.0 --> X u<= 0.0
8766 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8767 case FCmpInst::FCMP_ONE:
8768 // sqrt(X) != 0.0 --> X > 0.0
8769 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8770 case FCmpInst::FCMP_ORD:
8771 // !isnan(sqrt(X)) --> X >= 0.0
8772 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8773 case FCmpInst::FCMP_UNO:
8774 // isnan(sqrt(X)) --> X u< 0.0
8775 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8776 default:
8777 llvm_unreachable("Unexpected predicate!");
8778 }
8779}
8780
8781static Instruction *foldFCmpFNegCommonOp(FCmpInst &I) {
8782 CmpInst::Predicate Pred = I.getPredicate();
8783 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
8784
8785 // Canonicalize fneg as Op1.
8786 if (match(V: Op0, P: m_FNeg(X: m_Value())) && !match(V: Op1, P: m_FNeg(X: m_Value()))) {
8787 std::swap(a&: Op0, b&: Op1);
8788 Pred = I.getSwappedPredicate();
8789 }
8790
8791 if (!match(V: Op1, P: m_FNeg(X: m_Specific(V: Op0))))
8792 return nullptr;
8793
8794 // Replace the negated operand with 0.0:
8795 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8796 Constant *Zero = ConstantFP::getZero(Ty: Op0->getType());
8797 return new FCmpInst(Pred, Op0, Zero, "", &I);
8798}
8799
8800static Instruction *foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI,
8801 Constant *RHSC, InstCombinerImpl &CI) {
8802 const CmpInst::Predicate Pred = I.getPredicate();
8803 Value *X = LHSI->getOperand(i: 0);
8804 Value *Y = LHSI->getOperand(i: 1);
8805 switch (Pred) {
8806 default:
8807 break;
8808 case FCmpInst::FCMP_UGT:
8809 case FCmpInst::FCMP_ULT:
8810 case FCmpInst::FCMP_UNE:
8811 case FCmpInst::FCMP_OEQ:
8812 case FCmpInst::FCMP_OGE:
8813 case FCmpInst::FCMP_OLE:
8814 // The optimization is not valid if X and Y are infinities of the same
8815 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8816 // flag then we can assume we do not have that case. Otherwise we might be
8817 // able to prove that either X or Y is not infinity.
8818 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8819 !isKnownNeverInfinity(V: Y,
8820 SQ: CI.getSimplifyQuery().getWithInstruction(I: &I)) &&
8821 !isKnownNeverInfinity(V: X, SQ: CI.getSimplifyQuery().getWithInstruction(I: &I)))
8822 break;
8823
8824 [[fallthrough]];
8825 case FCmpInst::FCMP_OGT:
8826 case FCmpInst::FCMP_OLT:
8827 case FCmpInst::FCMP_ONE:
8828 case FCmpInst::FCMP_UEQ:
8829 case FCmpInst::FCMP_UGE:
8830 case FCmpInst::FCMP_ULE:
8831 // fcmp pred (x - y), 0 --> fcmp pred x, y
8832 if (match(V: RHSC, P: m_AnyZeroFP()) &&
8833 I.getFunction()->getDenormalMode(
8834 FPType: LHSI->getType()->getScalarType()->getFltSemantics()) ==
8835 DenormalMode::getIEEE()) {
8836 CI.replaceOperand(I, OpNum: 0, V: X);
8837 CI.replaceOperand(I, OpNum: 1, V: Y);
8838 I.setHasNoInfs(LHSI->hasNoInfs());
8839 if (LHSI->hasNoNaNs())
8840 I.setHasNoNaNs(true);
8841 return &I;
8842 }
8843 // fcmp `pred (C - Y), C` -> `fcmp swap(pred), Y, 0`
8844 // where C and Y can't be arbitrary floating-point values.
8845 // For example, with `C = 1.0f` and `Y = 0x1p-149`, `1.0f - Y` rounds back
8846 // to `1.0f`, so the source compare is false while the rewritten compare is
8847 // true.
8848 // We need to make sure (C - Y) never rounds back to C
8849 const APFloat *C;
8850 Value *IntSrc;
8851 if (match(V: RHSC, P: m_APFloat(Res&: C)) &&
8852 match(V: LHSI, P: m_FSub(L: m_Specific(V: RHSC), R: m_IToFP(Op: m_Value(V&: IntSrc)))) &&
8853 C->isNormal()) {
8854 // Requirements on C and Y:
8855 // 1. C is finite, nonzero, normal.
8856 // 2. C shouldn't be too large, that is, ULP(C) <= 1.
8857 // 3. Y must be the form of `[su]itofp`, so the finite nonzero result of Y
8858 // must be integer-valued with an absolute value of at least 1;
8859 // as long as the step size near C does not exceed 1,
8860 // C - Y cannot be rounded back to C when Y != 0.
8861 // 4. If Y = 0, `fcmp pred (C - 0), C` are equivalent to `fcmp swap(pred)
8862 // 0, 0` for ordered and unordered predicates as long as C is finite and
8863 // nonzero.
8864 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
8865 if (MantissaWidth != -1 && ilogb(Arg: *C) < MantissaWidth) {
8866 Constant *ZeroC = ConstantFP::getZero(Ty: LHSI->getType());
8867 I.setPredicate(I.getSwappedPredicate());
8868 CI.replaceOperand(I, OpNum: 0, V: Y);
8869 CI.replaceOperand(I, OpNum: 1, V: ZeroC);
8870 return &I;
8871 }
8872 }
8873 break;
8874 }
8875
8876 return nullptr;
8877}
8878
8879/// Fold: fabs(uitofp(a) - uitofp(b)) pred C --> a == b
8880/// where 'pred' is olt, ult, ogt, ugt, oge or uge and C is a positive, Non-NaN
8881/// float when the uitofp casts are exact and C is in the valid range.
8882///
8883/// Since exact uitofp means distinct integers map to distinct floats, the only
8884/// values fabs(uitofp(a) - uitofp(b)) can take are {0.0, 1.0, 2.0, ...}.
8885/// There are no values in the open interval (0, 1), so:
8886/// fabs(...) < C where 0 < C <= 1.0 --> a == b (strict lt: C=1.0 ok)
8887// fabs(..) >= C where C >= 1.0 -> a != b
8888///
8889/// The same logic applies to sitofp.
8890static Instruction *foldFCmpFAbsFSubIntToFP(FCmpInst &I, InstCombinerImpl &IC) {
8891 Value *FAbsArg;
8892 if (!match(V: I.getOperand(i_nocapture: 0), P: m_FAbs(Op0: m_Value(V&: FAbsArg))))
8893 return nullptr;
8894
8895 const APFloat *C;
8896 if (!match(V: I.getOperand(i_nocapture: 1), P: PatternMatch::m_FiniteNonZero(V&: C)))
8897 return nullptr;
8898
8899 FCmpInst::Predicate Pred = I.getPredicate();
8900 bool IsStrictLt = Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT;
8901 bool IsLe = Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE;
8902 bool IsStrictGt = Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT;
8903 bool IsGe = Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE;
8904 if (!IsStrictLt && !IsStrictGt && !IsGe)
8905 return nullptr;
8906
8907 APFloat One = APFloat::getOne(Sem: C->getSemantics());
8908 APFloat::cmpResult Cmp = C->compare(RHS: One);
8909
8910 // For strict-lt (olt/ult): C must be in (0, 1.0] -- C == 1.0 is fine since
8911 // the next possible value after 0.0 is 1.0, and < 1.0 excludes it.
8912 if (IsStrictLt && Cmp == APFloat::cmpGreaterThan)
8913 return nullptr;
8914 if (IsGe && Cmp == APFloat::cmpGreaterThan)
8915 return nullptr;
8916 if (IsLe && Cmp != APFloat::cmpGreaterThan)
8917 return nullptr;
8918 if (IsStrictGt && Cmp != APFloat::cmpLessThan)
8919 return nullptr;
8920
8921 // Match: fsub(uitofp(A), uitofp(B)) where both casts are uitofp or sitofp
8922 Value *A, *B;
8923 bool IsSigned;
8924 if (match(V: FAbsArg, P: m_FSub(L: m_UIToFP(Op: m_Value(V&: A)), R: m_UIToFP(Op: m_Value(V&: B))))) {
8925 IsSigned = false;
8926 } else if (match(V: FAbsArg,
8927 P: m_FSub(L: m_SIToFP(Op: m_Value(V&: A)), R: m_SIToFP(Op: m_Value(V&: B))))) {
8928 IsSigned = true;
8929 } else {
8930 return nullptr;
8931 }
8932
8933 // A and B must have the same integer type
8934 if (A->getType() != B->getType())
8935 return nullptr;
8936
8937 Type *FPTy = FAbsArg->getType();
8938 if (!IC.canBeCastedExactlyIntToFP(V: A, FPTy, IsSigned, CxtI: &I) ||
8939 !IC.canBeCastedExactlyIntToFP(V: B, FPTy, IsSigned, CxtI: &I))
8940 return nullptr;
8941 ICmpInst::Predicate ResultPred =
8942 IsStrictLt || IsLe ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
8943 return new ICmpInst(ResultPred, A, B);
8944}
8945
8946static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,
8947 InstCombinerImpl &IC) {
8948 Value *LHS = I.getOperand(i_nocapture: 0), *RHS = I.getOperand(i_nocapture: 1);
8949 Type *OpType = LHS->getType();
8950 CmpInst::Predicate Pred = I.getPredicate();
8951
8952 bool FloorX = match(V: LHS, P: m_Intrinsic<Intrinsic::floor>(Op0: m_Specific(V: RHS)));
8953 bool CeilX = match(V: LHS, P: m_Intrinsic<Intrinsic::ceil>(Op0: m_Specific(V: RHS)));
8954
8955 if (!FloorX && !CeilX) {
8956 if ((FloorX = match(V: RHS, P: m_Intrinsic<Intrinsic::floor>(Op0: m_Specific(V: LHS)))) ||
8957 (CeilX = match(V: RHS, P: m_Intrinsic<Intrinsic::ceil>(Op0: m_Specific(V: LHS))))) {
8958 std::swap(a&: LHS, b&: RHS);
8959 Pred = I.getSwappedPredicate();
8960 }
8961 }
8962
8963 if ((FloorX || CeilX) && FCmpInst::isCommutative(Pred) && LHS->hasOneUse()) {
8964 // fcmp pred floor(x), x => fcmp pred trunc(x), x
8965 // fcmp pred ceil(x), x => fcmp pred trunc(x), x
8966 // where pred is oeq, one, ord, ueq, une, uno.
8967 Value *TruncX = IC.Builder.CreateUnaryIntrinsic(ID: Intrinsic::trunc, Op: RHS);
8968 return new FCmpInst(Pred, TruncX, RHS, "", &I);
8969 }
8970
8971 switch (Pred) {
8972 case FCmpInst::FCMP_OLE:
8973 // fcmp ole floor(x), x => fcmp ord x, 0
8974 if (FloorX)
8975 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(Ty: OpType),
8976 "", &I);
8977 break;
8978 case FCmpInst::FCMP_OGT:
8979 // fcmp ogt floor(x), x => false
8980 if (FloorX)
8981 return IC.replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8982 break;
8983 case FCmpInst::FCMP_OGE:
8984 // fcmp oge ceil(x), x => fcmp ord x, 0
8985 if (CeilX)
8986 return new FCmpInst(FCmpInst::FCMP_ORD, RHS, ConstantFP::getZero(Ty: OpType),
8987 "", &I);
8988 break;
8989 case FCmpInst::FCMP_OLT:
8990 // fcmp olt ceil(x), x => false
8991 if (CeilX)
8992 return IC.replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
8993 break;
8994 case FCmpInst::FCMP_ULE:
8995 // fcmp ule floor(x), x => true
8996 if (FloorX)
8997 return IC.replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
8998 break;
8999 case FCmpInst::FCMP_UGT:
9000 // fcmp ugt floor(x), x => fcmp uno x, 0
9001 if (FloorX)
9002 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(Ty: OpType),
9003 "", &I);
9004 break;
9005 case FCmpInst::FCMP_UGE:
9006 // fcmp uge ceil(x), x => true
9007 if (CeilX)
9008 return IC.replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
9009 break;
9010 case FCmpInst::FCMP_ULT:
9011 // fcmp ult ceil(x), x => fcmp uno x, 0
9012 if (CeilX)
9013 return new FCmpInst(FCmpInst::FCMP_UNO, RHS, ConstantFP::getZero(Ty: OpType),
9014 "", &I);
9015 break;
9016 default:
9017 break;
9018 }
9019
9020 return nullptr;
9021}
9022
9023/// Returns true if a select that implements a min/max is redundant and
9024/// select result can be replaced with its non-constant operand, e.g.,
9025/// select ( (si/ui-to-fp A) <= C ), C, (si/ui-to-fp A)
9026/// where C is the FP constant equal to the minimum integer value
9027/// representable by A.
9028static bool isMinMaxCmpSelectEliminable(SelectPatternFlavor Flavor, Value *A,
9029 Value *B) {
9030 const APFloat *APF;
9031 if (!match(V: B, P: m_APFloat(Res&: APF)))
9032 return false;
9033
9034 auto *I = dyn_cast<Instruction>(Val: A);
9035 if (!I || !(I->getOpcode() == Instruction::SIToFP ||
9036 I->getOpcode() == Instruction::UIToFP))
9037 return false;
9038
9039 bool IsUnsigned = I->getOpcode() == Instruction::UIToFP;
9040 unsigned BitWidth = I->getOperand(i: 0)->getType()->getScalarSizeInBits();
9041 APSInt IntBoundary = (Flavor == SPF_FMAXNUM)
9042 ? APSInt::getMinValue(numBits: BitWidth, Unsigned: IsUnsigned)
9043 : APSInt::getMaxValue(numBits: BitWidth, Unsigned: IsUnsigned);
9044 APSInt ConvertedInt(BitWidth, IsUnsigned);
9045 bool IsExact;
9046 APFloat::opStatus Status =
9047 APF->convertToInteger(Result&: ConvertedInt, RM: APFloat::rmTowardZero, IsExact: &IsExact);
9048 return Status == APFloat::opOK && IsExact && ConvertedInt == IntBoundary;
9049}
9050
9051Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
9052 bool Changed = false;
9053
9054 /// Orders the operands of the compare so that they are listed from most
9055 /// complex to least complex. This puts constants before unary operators,
9056 /// before binary operators.
9057 if (getComplexity(V: I.getOperand(i_nocapture: 0)) < getComplexity(V: I.getOperand(i_nocapture: 1))) {
9058 I.swapOperands();
9059 Changed = true;
9060 }
9061
9062 const CmpInst::Predicate Pred = I.getPredicate();
9063 Value *Op0 = I.getOperand(i_nocapture: 0), *Op1 = I.getOperand(i_nocapture: 1);
9064 if (Value *V = simplifyFCmpInst(Predicate: Pred, LHS: Op0, RHS: Op1, FMF: I.getFastMathFlags(),
9065 Q: SQ.getWithInstruction(I: &I)))
9066 return replaceInstUsesWith(I, V);
9067
9068 // Simplify 'fcmp pred X, X'
9069 Type *OpType = Op0->getType();
9070 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
9071 if (Op0 == Op1) {
9072 switch (Pred) {
9073 default:
9074 break;
9075 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
9076 case FCmpInst::FCMP_ULT: // True if unordered or less than
9077 case FCmpInst::FCMP_UGT: // True if unordered or greater than
9078 case FCmpInst::FCMP_UNE: // True if unordered or not equal
9079 // Canonicalize these to be 'fcmp uno %X, 0.0'.
9080 I.setPredicate(FCmpInst::FCMP_UNO);
9081 I.setOperand(i_nocapture: 1, Val_nocapture: Constant::getNullValue(Ty: OpType));
9082 return &I;
9083
9084 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
9085 case FCmpInst::FCMP_OEQ: // True if ordered and equal
9086 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
9087 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
9088 // Canonicalize these to be 'fcmp ord %X, 0.0'.
9089 I.setPredicate(FCmpInst::FCMP_ORD);
9090 I.setOperand(i_nocapture: 1, Val_nocapture: Constant::getNullValue(Ty: OpType));
9091 return &I;
9092 }
9093 }
9094
9095 if (I.isCommutative()) {
9096 if (auto Pair = matchSymmetricPair(LHS: I.getOperand(i_nocapture: 0), RHS: I.getOperand(i_nocapture: 1))) {
9097 replaceOperand(I, OpNum: 0, V: Pair->first);
9098 replaceOperand(I, OpNum: 1, V: Pair->second);
9099 return &I;
9100 }
9101 }
9102
9103 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
9104 // then canonicalize the operand to 0.0.
9105 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
9106 if (!match(V: Op0, P: m_PosZeroFP()) &&
9107 isKnownNeverNaN(V: Op0, SQ: getSimplifyQuery().getWithInstruction(I: &I)))
9108 return replaceOperand(I, OpNum: 0, V: ConstantFP::getZero(Ty: OpType));
9109
9110 if (!match(V: Op1, P: m_PosZeroFP()) &&
9111 isKnownNeverNaN(V: Op1, SQ: getSimplifyQuery().getWithInstruction(I: &I)))
9112 return replaceOperand(I, OpNum: 1, V: ConstantFP::getZero(Ty: OpType));
9113 }
9114
9115 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
9116 Value *X, *Y;
9117 if (match(V: Op0, P: m_FNeg(X: m_Value(V&: X))) && match(V: Op1, P: m_FNeg(X: m_Value(V&: Y))))
9118 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
9119
9120 if (Instruction *R = foldFCmpFNegCommonOp(I))
9121 return R;
9122
9123 // Test if the FCmpInst instruction is used exclusively by a select as
9124 // part of a minimum or maximum operation. If so, refrain from doing
9125 // any other folding. This helps out other analyses which understand
9126 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
9127 // and CodeGen. And in this case, at least one of the comparison
9128 // operands has at least one user besides the compare (the select),
9129 // which would often largely negate the benefit of folding anyway.
9130 if (I.hasOneUse())
9131 if (SelectInst *SI = dyn_cast<SelectInst>(Val: I.user_back())) {
9132 Value *A, *B;
9133 SelectPatternResult SPR = matchSelectPattern(V: SI, LHS&: A, RHS&: B);
9134 bool IsRedundantMinMaxClamp =
9135 (SPR.Flavor == SPF_FMAXNUM || SPR.Flavor == SPF_FMINNUM) &&
9136 isMinMaxCmpSelectEliminable(Flavor: SPR.Flavor, A, B);
9137 if (SPR.Flavor != SPF_UNKNOWN && !IsRedundantMinMaxClamp)
9138 return nullptr;
9139 }
9140
9141 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
9142 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
9143 if (match(V: Op1, P: m_AnyZeroFP()) && !match(V: Op1, P: m_PosZeroFP()))
9144 return replaceOperand(I, OpNum: 1, V: ConstantFP::getZero(Ty: OpType));
9145
9146 // Canonicalize:
9147 // fcmp olt X, +inf -> fcmp one X, +inf
9148 // fcmp ole X, +inf -> fcmp ord X, 0
9149 // fcmp ogt X, +inf -> false
9150 // fcmp oge X, +inf -> fcmp oeq X, +inf
9151 // fcmp ult X, +inf -> fcmp une X, +inf
9152 // fcmp ule X, +inf -> true
9153 // fcmp ugt X, +inf -> fcmp uno X, 0
9154 // fcmp uge X, +inf -> fcmp ueq X, +inf
9155 // fcmp olt X, -inf -> false
9156 // fcmp ole X, -inf -> fcmp oeq X, -inf
9157 // fcmp ogt X, -inf -> fcmp one X, -inf
9158 // fcmp oge X, -inf -> fcmp ord X, 0
9159 // fcmp ult X, -inf -> fcmp uno X, 0
9160 // fcmp ule X, -inf -> fcmp ueq X, -inf
9161 // fcmp ugt X, -inf -> fcmp une X, -inf
9162 // fcmp uge X, -inf -> true
9163 const APFloat *C;
9164 if (match(V: Op1, P: m_APFloat(Res&: C)) && C->isInfinity()) {
9165 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(pred: Pred) : Pred) {
9166 default:
9167 break;
9168 case FCmpInst::FCMP_ORD:
9169 case FCmpInst::FCMP_UNO:
9170 case FCmpInst::FCMP_TRUE:
9171 case FCmpInst::FCMP_FALSE:
9172 case FCmpInst::FCMP_OGT:
9173 case FCmpInst::FCMP_ULE:
9174 llvm_unreachable("Should be simplified by InstSimplify");
9175 case FCmpInst::FCMP_OLT:
9176 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
9177 case FCmpInst::FCMP_OLE:
9178 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(Ty: OpType),
9179 "", &I);
9180 case FCmpInst::FCMP_OGE:
9181 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
9182 case FCmpInst::FCMP_ULT:
9183 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
9184 case FCmpInst::FCMP_UGT:
9185 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(Ty: OpType),
9186 "", &I);
9187 case FCmpInst::FCMP_UGE:
9188 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
9189 }
9190 }
9191
9192 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
9193 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
9194 if (match(V: Op1, P: m_PosZeroFP()) &&
9195 match(V: Op0, P: m_OneUse(SubPattern: m_ElementWiseBitCast(Op: m_Value(V&: X)))) &&
9196 X->getType()->isIntOrIntVectorTy() &&
9197 !F.getDenormalMode(FPType: Op1->getType()->getScalarType()->getFltSemantics())
9198 .inputsMayBeZero()) {
9199 ICmpInst::Predicate IntPred = ICmpInst::BAD_ICMP_PREDICATE;
9200 if (Pred == FCmpInst::FCMP_OEQ)
9201 IntPred = ICmpInst::ICMP_EQ;
9202 else if (Pred == FCmpInst::FCMP_UNE)
9203 IntPred = ICmpInst::ICMP_NE;
9204
9205 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
9206 Type *IntTy = X->getType();
9207 const APInt &SignMask = ~APInt::getSignMask(BitWidth: IntTy->getScalarSizeInBits());
9208 Value *MaskX = Builder.CreateAnd(LHS: X, RHS: ConstantInt::get(Ty: IntTy, V: SignMask));
9209 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(Ty: IntTy));
9210 }
9211 }
9212
9213 // Handle fcmp with instruction LHS and constant RHS.
9214 Instruction *LHSI;
9215 Constant *RHSC;
9216 if (match(V: Op0, P: m_Instruction(I&: LHSI)) && match(V: Op1, P: m_Constant(C&: RHSC))) {
9217 switch (LHSI->getOpcode()) {
9218 case Instruction::Select:
9219 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
9220 if (FCmpInst::isEquality(Pred) && match(V: RHSC, P: m_AnyZeroFP()) &&
9221 match(V: LHSI, P: m_c_Select(L: m_FNeg(X: m_Value(V&: X)), R: m_Deferred(V: X))))
9222 return replaceOperand(I, OpNum: 0, V: X);
9223 if (Instruction *NV = FoldOpIntoSelect(Op&: I, SI: cast<SelectInst>(Val: LHSI)))
9224 return NV;
9225 break;
9226 case Instruction::FSub:
9227 if (LHSI->hasOneUse())
9228 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, CI&: *this))
9229 return NV;
9230 break;
9231 case Instruction::PHI:
9232 if (Instruction *NV = foldOpIntoPhi(I, PN: cast<PHINode>(Val: LHSI)))
9233 return NV;
9234 break;
9235 case Instruction::SIToFP:
9236 case Instruction::UIToFP:
9237 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
9238 return NV;
9239 break;
9240 case Instruction::FDiv:
9241 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
9242 return NV;
9243 break;
9244 case Instruction::Load:
9245 if (auto *GEP = dyn_cast<GetElementPtrInst>(Val: LHSI->getOperand(i: 0)))
9246 if (Instruction *Res =
9247 foldCmpLoadFromIndexedGlobal(LI: cast<LoadInst>(Val: LHSI), GEP, ICI&: I))
9248 return Res;
9249 break;
9250 case Instruction::FPTrunc:
9251 if (Instruction *NV = foldFCmpFpTrunc(I, FPTrunc: *LHSI, C: *RHSC))
9252 return NV;
9253 break;
9254 }
9255 }
9256
9257 if (Instruction *R = foldFabsWithFcmpZero(I, IC&: *this))
9258 return R;
9259
9260 if (Instruction *R = foldFCmpFAbsFSubIntToFP(I, IC&: *this))
9261 return R;
9262
9263 if (Instruction *R = foldSqrtWithFcmpZero(I, IC&: *this))
9264 return R;
9265
9266 if (Instruction *R = foldFCmpWithFloorAndCeil(I, IC&: *this))
9267 return R;
9268
9269 if (Instruction *R = foldCmpSelectOfConstants(I))
9270 return R;
9271
9272 if (match(V: Op0, P: m_FNeg(X: m_Value(V&: X)))) {
9273 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
9274 Constant *C;
9275 if (match(V: Op1, P: m_Constant(C)))
9276 if (Constant *NegC = ConstantFoldUnaryOpOperand(Opcode: Instruction::FNeg, Op: C, DL))
9277 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
9278 }
9279
9280 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
9281 if (match(V: Op0, P: m_FAdd(L: m_Value(V&: X), R: m_AnyZeroFP())))
9282 return new FCmpInst(Pred, X, Op1, "", &I);
9283
9284 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
9285 if (match(V: Op1, P: m_FAdd(L: m_Value(V&: Y), R: m_AnyZeroFP())))
9286 return new FCmpInst(Pred, Op0, Y, "", &I);
9287
9288 // fcmp ord/uno (fptrunc X), (fptrunc Y) -> fcmp ord/uno X, Y
9289 if ((Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) &&
9290 match(V: Op0, P: m_FPTrunc(Op: m_Value(V&: X))) && match(V: Op1, P: m_FPTrunc(Op: m_Value(V&: Y))) &&
9291 X->getType() == Y->getType())
9292 return new FCmpInst(Pred, X, Y, "", &I);
9293
9294 if (match(V: Op0, P: m_FPExt(Op: m_Value(V&: X)))) {
9295 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
9296 if (match(V: Op1, P: m_FPExt(Op: m_Value(V&: Y))) && X->getType() == Y->getType())
9297 return new FCmpInst(Pred, X, Y, "", &I);
9298
9299 const APFloat *C;
9300 if (match(V: Op1, P: m_APFloat(Res&: C))) {
9301 const fltSemantics &FPSem =
9302 X->getType()->getScalarType()->getFltSemantics();
9303 bool Lossy;
9304 APFloat TruncC = *C;
9305 TruncC.convert(ToSemantics: FPSem, RM: APFloat::rmNearestTiesToEven, losesInfo: &Lossy);
9306
9307 if (Lossy) {
9308 // X can't possibly equal the higher-precision constant, so reduce any
9309 // equality comparison.
9310 // TODO: Other predicates can be handled via getFCmpCode().
9311 switch (Pred) {
9312 case FCmpInst::FCMP_OEQ:
9313 // X is ordered and equal to an impossible constant --> false
9314 return replaceInstUsesWith(I, V: ConstantInt::getFalse(Ty: I.getType()));
9315 case FCmpInst::FCMP_ONE:
9316 // X is ordered and not equal to an impossible constant --> ordered
9317 return new FCmpInst(FCmpInst::FCMP_ORD, X,
9318 ConstantFP::getZero(Ty: X->getType()));
9319 case FCmpInst::FCMP_UEQ:
9320 // X is unordered or equal to an impossible constant --> unordered
9321 return new FCmpInst(FCmpInst::FCMP_UNO, X,
9322 ConstantFP::getZero(Ty: X->getType()));
9323 case FCmpInst::FCMP_UNE:
9324 // X is unordered or not equal to an impossible constant --> true
9325 return replaceInstUsesWith(I, V: ConstantInt::getTrue(Ty: I.getType()));
9326 default:
9327 break;
9328 }
9329 }
9330
9331 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
9332 // Avoid lossy conversions and denormals.
9333 // Zero is a special case that's OK to convert.
9334 APFloat Fabs = TruncC;
9335 Fabs.clearSign();
9336 if (!Lossy &&
9337 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(Sem: FPSem)))) {
9338 Constant *NewC = ConstantFP::get(Ty: X->getType(), V: TruncC);
9339 return new FCmpInst(Pred, X, NewC, "", &I);
9340 }
9341 }
9342 }
9343
9344 // Convert a sign-bit test of an FP value into a cast and integer compare.
9345 // TODO: Simplify if the copysign constant is 0.0 or NaN.
9346 // TODO: Handle non-zero compare constants.
9347 // TODO: Handle other predicates.
9348 if (match(V: Op0, P: m_OneUse(SubPattern: m_Intrinsic<Intrinsic::copysign>(Op0: m_APFloat(Res&: C),
9349 Op1: m_Value(V&: X)))) &&
9350 match(V: Op1, P: m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
9351 Type *IntType = Builder.getIntNTy(N: X->getType()->getScalarSizeInBits());
9352 if (auto *VecTy = dyn_cast<VectorType>(Val: OpType))
9353 IntType = VectorType::get(ElementType: IntType, EC: VecTy->getElementCount());
9354
9355 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
9356 if (Pred == FCmpInst::FCMP_OLT) {
9357 Value *IntX = Builder.CreateBitCast(V: X, DestTy: IntType);
9358 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
9359 ConstantInt::getNullValue(Ty: IntType));
9360 }
9361 }
9362
9363 {
9364 Value *CanonLHS = nullptr;
9365 match(V: Op0, P: m_Intrinsic<Intrinsic::canonicalize>(Op0: m_Value(V&: CanonLHS)));
9366 // (canonicalize(x) == x) => (x == x)
9367 if (CanonLHS == Op1)
9368 return new FCmpInst(Pred, Op1, Op1, "", &I);
9369
9370 Value *CanonRHS = nullptr;
9371 match(V: Op1, P: m_Intrinsic<Intrinsic::canonicalize>(Op0: m_Value(V&: CanonRHS)));
9372 // (x == canonicalize(x)) => (x == x)
9373 if (CanonRHS == Op0)
9374 return new FCmpInst(Pred, Op0, Op0, "", &I);
9375
9376 // (canonicalize(x) == canonicalize(y)) => (x == y)
9377 if (CanonLHS && CanonRHS)
9378 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
9379 }
9380
9381 if (I.getType()->isVectorTy())
9382 if (Instruction *Res = foldVectorCmp(Cmp&: I, Builder))
9383 return Res;
9384
9385 return Changed ? &I : nullptr;
9386}
9387