LLVM 20.0.0git
InstCombineCompares.cpp
Go to the documentation of this file.
1//===- InstCombineCompares.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the visitICmp and visitFCmp functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/APSInt.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/Statistic.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/InstrTypes.h"
31#include <bitset>
32
33using namespace llvm;
34using namespace PatternMatch;
35
36#define DEBUG_TYPE "instcombine"
37
38// How many times is a select replaced by one of its operands?
39STATISTIC(NumSel, "Number of select opts");
40
41
42/// Compute Result = In1+In2, returning true if the result overflowed for this
43/// type.
44static bool addWithOverflow(APInt &Result, const APInt &In1,
45 const APInt &In2, bool IsSigned = false) {
46 bool Overflow;
47 if (IsSigned)
48 Result = In1.sadd_ov(In2, Overflow);
49 else
50 Result = In1.uadd_ov(In2, Overflow);
51
52 return Overflow;
53}
54
55/// Compute Result = In1-In2, returning true if the result overflowed for this
56/// type.
57static bool subWithOverflow(APInt &Result, const APInt &In1,
58 const APInt &In2, bool IsSigned = false) {
59 bool Overflow;
60 if (IsSigned)
61 Result = In1.ssub_ov(In2, Overflow);
62 else
63 Result = In1.usub_ov(In2, Overflow);
64
65 return Overflow;
66}
67
68/// Given an icmp instruction, return true if any use of this comparison is a
69/// branch on sign bit comparison.
70static bool hasBranchUse(ICmpInst &I) {
71 for (auto *U : I.users())
72 if (isa<BranchInst>(U))
73 return true;
74 return false;
75}
76
77/// Returns true if the exploded icmp can be expressed as a signed comparison
78/// to zero and updates the predicate accordingly.
79/// The signedness of the comparison is preserved.
80/// TODO: Refactor with decomposeBitTestICmp()?
81static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
82 if (!ICmpInst::isSigned(Pred))
83 return false;
84
85 if (C.isZero())
86 return ICmpInst::isRelational(Pred);
87
88 if (C.isOne()) {
89 if (Pred == ICmpInst::ICMP_SLT) {
90 Pred = ICmpInst::ICMP_SLE;
91 return true;
92 }
93 } else if (C.isAllOnes()) {
94 if (Pred == ICmpInst::ICMP_SGT) {
95 Pred = ICmpInst::ICMP_SGE;
96 return true;
97 }
98 }
99
100 return false;
101}
102
103/// This is called when we see this pattern:
104/// cmp pred (load (gep GV, ...)), cmpcst
105/// where GV is a global variable with a constant initializer. Try to simplify
106/// this into some simple computation that does not need the load. For example
107/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
108///
109/// If AndCst is non-null, then the loaded value is masked with that constant
110/// before doing the comparison. This handles cases like "A[i]&4 == 0".
113 ConstantInt *AndCst) {
114 if (LI->isVolatile() || LI->getType() != GEP->getResultElementType() ||
115 GV->getValueType() != GEP->getSourceElementType() || !GV->isConstant() ||
117 return nullptr;
118
120 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
121 return nullptr;
122
123 uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
124 // Don't blow up on huge arrays.
125 if (ArrayElementCount > MaxArraySizeForCombine)
126 return nullptr;
127
128 // There are many forms of this optimization we can handle, for now, just do
129 // the simple index into a single-dimensional array.
130 //
131 // Require: GEP GV, 0, i {{, constant indices}}
132 if (GEP->getNumOperands() < 3 || !isa<ConstantInt>(GEP->getOperand(1)) ||
133 !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
134 isa<Constant>(GEP->getOperand(2)))
135 return nullptr;
136
137 // Check that indices after the variable are constants and in-range for the
138 // type they index. Collect the indices. This is typically for arrays of
139 // structs.
140 SmallVector<unsigned, 4> LaterIndices;
141
142 Type *EltTy = Init->getType()->getArrayElementType();
143 for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
144 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
145 if (!Idx)
146 return nullptr; // Variable index.
147
148 uint64_t IdxVal = Idx->getZExtValue();
149 if ((unsigned)IdxVal != IdxVal)
150 return nullptr; // Too large array index.
151
152 if (StructType *STy = dyn_cast<StructType>(EltTy))
153 EltTy = STy->getElementType(IdxVal);
154 else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
155 if (IdxVal >= ATy->getNumElements())
156 return nullptr;
157 EltTy = ATy->getElementType();
158 } else {
159 return nullptr; // Unknown type.
160 }
161
162 LaterIndices.push_back(IdxVal);
163 }
164
165 enum { Overdefined = -3, Undefined = -2 };
166
167 // Variables for our state machines.
168
169 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
170 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
171 // and 87 is the second (and last) index. FirstTrueElement is -2 when
172 // undefined, otherwise set to the first true element. SecondTrueElement is
173 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
174 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
175
176 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
177 // form "i != 47 & i != 87". Same state transitions as for true elements.
178 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
179
180 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
181 /// define a state machine that triggers for ranges of values that the index
182 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
183 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
184 /// index in the range (inclusive). We use -2 for undefined here because we
185 /// use relative comparisons and don't want 0-1 to match -1.
186 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
187
188 // MagicBitvector - This is a magic bitvector where we set a bit if the
189 // comparison is true for element 'i'. If there are 64 elements or less in
190 // the array, this will fully represent all the comparison results.
191 uint64_t MagicBitvector = 0;
192
193 // Scan the array and see if one of our patterns matches.
194 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
195 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
196 Constant *Elt = Init->getAggregateElement(i);
197 if (!Elt)
198 return nullptr;
199
200 // If this is indexing an array of structures, get the structure element.
201 if (!LaterIndices.empty()) {
202 Elt = ConstantFoldExtractValueInstruction(Elt, LaterIndices);
203 if (!Elt)
204 return nullptr;
205 }
206
207 // If the element is masked, handle it.
208 if (AndCst) {
209 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);
210 if (!Elt)
211 return nullptr;
212 }
213
214 // Find out if the comparison would be true or false for the i'th element.
216 CompareRHS, DL, &TLI);
217 if (!C)
218 return nullptr;
219
220 // If the result is undef for this element, ignore it.
221 if (isa<UndefValue>(C)) {
222 // Extend range state machines to cover this element in case there is an
223 // undef in the middle of the range.
224 if (TrueRangeEnd == (int)i - 1)
225 TrueRangeEnd = i;
226 if (FalseRangeEnd == (int)i - 1)
227 FalseRangeEnd = i;
228 continue;
229 }
230
231 // If we can't compute the result for any of the elements, we have to give
232 // up evaluating the entire conditional.
233 if (!isa<ConstantInt>(C))
234 return nullptr;
235
236 // Otherwise, we know if the comparison is true or false for this element,
237 // update our state machines.
238 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
239
240 // State machine for single/double/range index comparison.
241 if (IsTrueForElt) {
242 // Update the TrueElement state machine.
243 if (FirstTrueElement == Undefined)
244 FirstTrueElement = TrueRangeEnd = i; // First true element.
245 else {
246 // Update double-compare state machine.
247 if (SecondTrueElement == Undefined)
248 SecondTrueElement = i;
249 else
250 SecondTrueElement = Overdefined;
251
252 // Update range state machine.
253 if (TrueRangeEnd == (int)i - 1)
254 TrueRangeEnd = i;
255 else
256 TrueRangeEnd = Overdefined;
257 }
258 } else {
259 // Update the FalseElement state machine.
260 if (FirstFalseElement == Undefined)
261 FirstFalseElement = FalseRangeEnd = i; // First false element.
262 else {
263 // Update double-compare state machine.
264 if (SecondFalseElement == Undefined)
265 SecondFalseElement = i;
266 else
267 SecondFalseElement = Overdefined;
268
269 // Update range state machine.
270 if (FalseRangeEnd == (int)i - 1)
271 FalseRangeEnd = i;
272 else
273 FalseRangeEnd = Overdefined;
274 }
275 }
276
277 // If this element is in range, update our magic bitvector.
278 if (i < 64 && IsTrueForElt)
279 MagicBitvector |= 1ULL << i;
280
281 // If all of our states become overdefined, bail out early. Since the
282 // predicate is expensive, only check it every 8 elements. This is only
283 // really useful for really huge arrays.
284 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
285 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
286 FalseRangeEnd == Overdefined)
287 return nullptr;
288 }
289
290 // Now that we've scanned the entire array, emit our new comparison(s). We
291 // order the state machines in complexity of the generated code.
292 Value *Idx = GEP->getOperand(2);
293
294 // If the index is larger than the pointer offset size of the target, truncate
295 // the index down like the GEP would do implicitly. We don't have to do this
296 // for an inbounds GEP because the index can't be out of range.
297 if (!GEP->isInBounds()) {
298 Type *PtrIdxTy = DL.getIndexType(GEP->getType());
299 unsigned OffsetSize = PtrIdxTy->getIntegerBitWidth();
300 if (Idx->getType()->getPrimitiveSizeInBits().getFixedValue() > OffsetSize)
301 Idx = Builder.CreateTrunc(Idx, PtrIdxTy);
302 }
303
304 // If inbounds keyword is not present, Idx * ElementSize can overflow.
305 // Let's assume that ElementSize is 2 and the wanted value is at offset 0.
306 // Then, there are two possible values for Idx to match offset 0:
307 // 0x00..00, 0x80..00.
308 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
309 // comparison is false if Idx was 0x80..00.
310 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.
311 unsigned ElementSize =
312 DL.getTypeAllocSize(Init->getType()->getArrayElementType());
313 auto MaskIdx = [&](Value *Idx) {
314 if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) {
315 Value *Mask = Constant::getAllOnesValue(Idx->getType());
316 Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize));
317 Idx = Builder.CreateAnd(Idx, Mask);
318 }
319 return Idx;
320 };
321
322 // If the comparison is only true for one or two elements, emit direct
323 // comparisons.
324 if (SecondTrueElement != Overdefined) {
325 Idx = MaskIdx(Idx);
326 // None true -> false.
327 if (FirstTrueElement == Undefined)
328 return replaceInstUsesWith(ICI, Builder.getFalse());
329
330 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
331
332 // True for one element -> 'i == 47'.
333 if (SecondTrueElement == Undefined)
334 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
335
336 // True for two elements -> 'i == 47 | i == 72'.
337 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx);
338 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
339 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx);
340 return BinaryOperator::CreateOr(C1, C2);
341 }
342
343 // If the comparison is only false for one or two elements, emit direct
344 // comparisons.
345 if (SecondFalseElement != Overdefined) {
346 Idx = MaskIdx(Idx);
347 // None false -> true.
348 if (FirstFalseElement == Undefined)
349 return replaceInstUsesWith(ICI, Builder.getTrue());
350
351 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
352
353 // False for one element -> 'i != 47'.
354 if (SecondFalseElement == Undefined)
355 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
356
357 // False for two elements -> 'i != 47 & i != 72'.
358 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx);
359 Value *SecondFalseIdx =
360 ConstantInt::get(Idx->getType(), SecondFalseElement);
361 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx);
362 return BinaryOperator::CreateAnd(C1, C2);
363 }
364
365 // If the comparison can be replaced with a range comparison for the elements
366 // where it is true, emit the range check.
367 if (TrueRangeEnd != Overdefined) {
368 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
369 Idx = MaskIdx(Idx);
370
371 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
372 if (FirstTrueElement) {
373 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
374 Idx = Builder.CreateAdd(Idx, Offs);
375 }
376
377 Value *End =
378 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1);
379 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
380 }
381
382 // False range check.
383 if (FalseRangeEnd != Overdefined) {
384 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
385 Idx = MaskIdx(Idx);
386 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
387 if (FirstFalseElement) {
388 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
389 Idx = Builder.CreateAdd(Idx, Offs);
390 }
391
392 Value *End =
393 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement);
394 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
395 }
396
397 // If a magic bitvector captures the entire comparison state
398 // of this load, replace it with computation that does:
399 // ((magic_cst >> i) & 1) != 0
400 {
401 Type *Ty = nullptr;
402
403 // Look for an appropriate type:
404 // - The type of Idx if the magic fits
405 // - The smallest fitting legal type
406 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
407 Ty = Idx->getType();
408 else
409 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
410
411 if (Ty) {
412 Idx = MaskIdx(Idx);
413 Value *V = Builder.CreateIntCast(Idx, Ty, false);
414 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
415 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V);
416 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
417 }
418 }
419
420 return nullptr;
421}
422
423/// Returns true if we can rewrite Start as a GEP with pointer Base
424/// and some integer offset. The nodes that need to be re-written
425/// for this transformation will be added to Explored.
427 const DataLayout &DL,
428 SetVector<Value *> &Explored) {
429 SmallVector<Value *, 16> WorkList(1, Start);
430 Explored.insert(Base);
431
432 // The following traversal gives us an order which can be used
433 // when doing the final transformation. Since in the final
434 // transformation we create the PHI replacement instructions first,
435 // we don't have to get them in any particular order.
436 //
437 // However, for other instructions we will have to traverse the
438 // operands of an instruction first, which means that we have to
439 // do a post-order traversal.
440 while (!WorkList.empty()) {
442
443 while (!WorkList.empty()) {
444 if (Explored.size() >= 100)
445 return false;
446
447 Value *V = WorkList.back();
448
449 if (Explored.contains(V)) {
450 WorkList.pop_back();
451 continue;
452 }
453
454 if (!isa<GetElementPtrInst>(V) && !isa<PHINode>(V))
455 // We've found some value that we can't explore which is different from
456 // the base. Therefore we can't do this transformation.
457 return false;
458
459 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
460 // Only allow inbounds GEPs with at most one variable offset.
461 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); };
462 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1)
463 return false;
464
465 if (!Explored.contains(GEP->getOperand(0)))
466 WorkList.push_back(GEP->getOperand(0));
467 }
468
469 if (WorkList.back() == V) {
470 WorkList.pop_back();
471 // We've finished visiting this node, mark it as such.
472 Explored.insert(V);
473 }
474
475 if (auto *PN = dyn_cast<PHINode>(V)) {
476 // We cannot transform PHIs on unsplittable basic blocks.
477 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
478 return false;
479 Explored.insert(PN);
480 PHIs.insert(PN);
481 }
482 }
483
484 // Explore the PHI nodes further.
485 for (auto *PN : PHIs)
486 for (Value *Op : PN->incoming_values())
487 if (!Explored.contains(Op))
488 WorkList.push_back(Op);
489 }
490
491 // Make sure that we can do this. Since we can't insert GEPs in a basic
492 // block before a PHI node, we can't easily do this transformation if
493 // we have PHI node users of transformed instructions.
494 for (Value *Val : Explored) {
495 for (Value *Use : Val->uses()) {
496
497 auto *PHI = dyn_cast<PHINode>(Use);
498 auto *Inst = dyn_cast<Instruction>(Val);
499
500 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
501 !Explored.contains(PHI))
502 continue;
503
504 if (PHI->getParent() == Inst->getParent())
505 return false;
506 }
507 }
508 return true;
509}
510
511// Sets the appropriate insert point on Builder where we can add
512// a replacement Instruction for V (if that is possible).
513static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
514 bool Before = true) {
515 if (auto *PHI = dyn_cast<PHINode>(V)) {
516 BasicBlock *Parent = PHI->getParent();
517 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
518 return;
519 }
520 if (auto *I = dyn_cast<Instruction>(V)) {
521 if (!Before)
522 I = &*std::next(I->getIterator());
523 Builder.SetInsertPoint(I);
524 return;
525 }
526 if (auto *A = dyn_cast<Argument>(V)) {
527 // Set the insertion point in the entry block.
528 BasicBlock &Entry = A->getParent()->getEntryBlock();
529 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt());
530 return;
531 }
532 // Otherwise, this is a constant and we don't need to set a new
533 // insertion point.
534 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
535}
536
537/// Returns a re-written value of Start as an indexed GEP using Base as a
538/// pointer.
540 const DataLayout &DL,
541 SetVector<Value *> &Explored,
542 InstCombiner &IC) {
543 // Perform all the substitutions. This is a bit tricky because we can
544 // have cycles in our use-def chains.
545 // 1. Create the PHI nodes without any incoming values.
546 // 2. Create all the other values.
547 // 3. Add the edges for the PHI nodes.
548 // 4. Emit GEPs to get the original pointers.
549 // 5. Remove the original instructions.
550 Type *IndexType = IntegerType::get(
551 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType()));
552
554 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
555
556 // Create the new PHI nodes, without adding any incoming values.
557 for (Value *Val : Explored) {
558 if (Val == Base)
559 continue;
560 // Create empty phi nodes. This avoids cyclic dependencies when creating
561 // the remaining instructions.
562 if (auto *PHI = dyn_cast<PHINode>(Val))
563 NewInsts[PHI] =
564 PHINode::Create(IndexType, PHI->getNumIncomingValues(),
565 PHI->getName() + ".idx", PHI->getIterator());
566 }
567 IRBuilder<> Builder(Base->getContext());
568
569 // Create all the other instructions.
570 for (Value *Val : Explored) {
571 if (NewInsts.contains(Val))
572 continue;
573
574 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
575 setInsertionPoint(Builder, GEP);
576 Value *Op = NewInsts[GEP->getOperand(0)];
577 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);
578 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero())
579 NewInsts[GEP] = OffsetV;
580 else
581 NewInsts[GEP] = Builder.CreateNSWAdd(
582 Op, OffsetV, GEP->getOperand(0)->getName() + ".add");
583 continue;
584 }
585 if (isa<PHINode>(Val))
586 continue;
587
588 llvm_unreachable("Unexpected instruction type");
589 }
590
591 // Add the incoming values to the PHI nodes.
592 for (Value *Val : Explored) {
593 if (Val == Base)
594 continue;
595 // All the instructions have been created, we can now add edges to the
596 // phi nodes.
597 if (auto *PHI = dyn_cast<PHINode>(Val)) {
598 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
599 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
600 Value *NewIncoming = PHI->getIncomingValue(I);
601
602 if (NewInsts.contains(NewIncoming))
603 NewIncoming = NewInsts[NewIncoming];
604
605 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
606 }
607 }
608 }
609
610 for (Value *Val : Explored) {
611 if (Val == Base)
612 continue;
613
614 setInsertionPoint(Builder, Val, false);
615 // Create GEP for external users.
616 Value *NewVal = Builder.CreateInBoundsGEP(
617 Builder.getInt8Ty(), Base, NewInsts[Val], Val->getName() + ".ptr");
618 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal);
619 // Add old instruction to worklist for DCE. We don't directly remove it
620 // here because the original compare is one of the users.
621 IC.addToWorklist(cast<Instruction>(Val));
622 }
623
624 return NewInsts[Start];
625}
626
627/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
628/// We can look through PHIs, GEPs and casts in order to determine a common base
629/// between GEPLHS and RHS.
632 const DataLayout &DL,
633 InstCombiner &IC) {
634 // FIXME: Support vector of pointers.
635 if (GEPLHS->getType()->isVectorTy())
636 return nullptr;
637
638 if (!GEPLHS->hasAllConstantIndices())
639 return nullptr;
640
641 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
642 Value *PtrBase =
644 /*AllowNonInbounds*/ false);
645
646 // Bail if we looked through addrspacecast.
647 if (PtrBase->getType() != GEPLHS->getType())
648 return nullptr;
649
650 // The set of nodes that will take part in this transformation.
651 SetVector<Value *> Nodes;
652
653 if (!canRewriteGEPAsOffset(RHS, PtrBase, DL, Nodes))
654 return nullptr;
655
656 // We know we can re-write this as
657 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
658 // Since we've only looked through inbouds GEPs we know that we
659 // can't have overflow on either side. We can therefore re-write
660 // this as:
661 // OFFSET1 cmp OFFSET2
662 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, DL, Nodes, IC);
663
664 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
665 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
666 // offset. Since Index is the offset of LHS to the base pointer, we will now
667 // compare the offsets instead of comparing the pointers.
669 IC.Builder.getInt(Offset), NewRHS);
670}
671
672/// Fold comparisons between a GEP instruction and something else. At this point
673/// we know that the GEP is on the LHS of the comparison.
676 Instruction &I) {
677 // Don't transform signed compares of GEPs into index compares. Even if the
678 // GEP is inbounds, the final add of the base pointer can have signed overflow
679 // and would change the result of the icmp.
680 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
681 // the maximum signed value for the pointer type.
683 return nullptr;
684
685 // Look through bitcasts and addrspacecasts. We do not however want to remove
686 // 0 GEPs.
687 if (!isa<GetElementPtrInst>(RHS))
689
690 Value *PtrBase = GEPLHS->getOperand(0);
691 if (PtrBase == RHS && (GEPLHS->isInBounds() || ICmpInst::isEquality(Cond))) {
692 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
693 Value *Offset = EmitGEPOffset(GEPLHS);
695 Constant::getNullValue(Offset->getType()));
696 }
697
698 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
699 isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&
700 !NullPointerIsDefined(I.getFunction(),
702 // For most address spaces, an allocation can't be placed at null, but null
703 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
704 // the only valid inbounds address derived from null, is null itself.
705 // Thus, we have four cases to consider:
706 // 1) Base == nullptr, Offset == 0 -> inbounds, null
707 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
708 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
709 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
710 //
711 // (Note if we're indexing a type of size 0, that simply collapses into one
712 // of the buckets above.)
713 //
714 // In general, we're allowed to make values less poison (i.e. remove
715 // sources of full UB), so in this case, we just select between the two
716 // non-poison cases (1 and 4 above).
717 //
718 // For vectors, we apply the same reasoning on a per-lane basis.
719 auto *Base = GEPLHS->getPointerOperand();
720 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
721 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount();
723 }
724 return new ICmpInst(Cond, Base,
726 cast<Constant>(RHS), Base->getType()));
727 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
728 // If the base pointers are different, but the indices are the same, just
729 // compare the base pointer.
730 if (PtrBase != GEPRHS->getOperand(0)) {
731 bool IndicesTheSame =
732 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
733 GEPLHS->getPointerOperand()->getType() ==
734 GEPRHS->getPointerOperand()->getType() &&
735 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
736 if (IndicesTheSame)
737 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
738 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
739 IndicesTheSame = false;
740 break;
741 }
742
743 // If all indices are the same, just compare the base pointers.
744 Type *BaseType = GEPLHS->getOperand(0)->getType();
745 if (IndicesTheSame && CmpInst::makeCmpResultType(BaseType) == I.getType())
746 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(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 PtrBase->stripPointerCasts() ==
756 GEPRHS->getOperand(0)->stripPointerCasts() &&
757 !GEPLHS->getType()->isVectorTy()) {
758 Value *LOffset = EmitGEPOffset(GEPLHS);
759 Value *ROffset = EmitGEPOffset(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(ROffset, LHSIndexTy);
770 } else
771 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy);
772 }
773
775 LOffset, ROffset);
776 return replaceInstUsesWith(I, Cmp);
777 }
778
779 // Otherwise, the base pointers are different and the indices are
780 // different. Try convert this to an indexed compare by looking through
781 // PHIs/casts.
782 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
783 }
784
785 bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
786 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
787 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
788 // If the GEPs only differ by one index, compare it.
789 unsigned NumDifferences = 0; // Keep track of # differences.
790 unsigned DiffOperand = 0; // The operand that differs.
791 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
792 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
793 Type *LHSType = GEPLHS->getOperand(i)->getType();
794 Type *RHSType = GEPRHS->getOperand(i)->getType();
795 // FIXME: Better support for vector of pointers.
796 if (LHSType->getPrimitiveSizeInBits() !=
797 RHSType->getPrimitiveSizeInBits() ||
798 (GEPLHS->getType()->isVectorTy() &&
799 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
800 // Irreconcilable differences.
801 NumDifferences = 2;
802 break;
803 }
804
805 if (NumDifferences++) break;
806 DiffOperand = i;
807 }
808
809 if (NumDifferences == 0) // SAME GEP?
810 return replaceInstUsesWith(I, // No comparison is needed here.
811 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));
812
813 else if (NumDifferences == 1 && GEPsInBounds) {
814 Value *LHSV = GEPLHS->getOperand(DiffOperand);
815 Value *RHSV = GEPRHS->getOperand(DiffOperand);
816 // Make sure we do a signed comparison here.
817 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
818 }
819 }
820
821 if (GEPsInBounds || CmpInst::isEquality(Cond)) {
822 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
823 Value *L = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true);
824 Value *R = EmitGEPOffset(GEPRHS, /*RewriteGEP=*/true);
825 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), 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, *this);
832}
833
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.
855
856 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
857
858 void tooManyUses() override { Captured = true; }
859
860 bool captured(const Use *U) override {
861 auto *ICmp = dyn_cast<ICmpInst>(U->getUser());
862 // We need to check that U is based *only* on the alloca, and doesn't
863 // have other contributions from a select/phi operand.
864 // TODO: We could check whether getUnderlyingObjects() reduces to one
865 // object, which would allow looking through phi nodes.
866 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) {
867 // Collect equality icmps of the alloca, and don't treat them as
868 // captures.
869 auto Res = ICmps.insert({ICmp, 0});
870 Res.first->second |= 1u << U->getOperandNo();
871 return false;
872 }
873
874 Captured = true;
875 return true;
876 }
877 };
878
879 CmpCaptureTracker Tracker(Alloca);
880 PointerMayBeCaptured(Alloca, &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(
892 ICmp->getType(), ICmp->getPredicate() == ICmpInst::ICMP_NE);
893 replaceInstUsesWith(*ICmp, Res);
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".
913 ICmpInst::Predicate 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 = ConstantInt::get(X->getType(),
924 APInt::getMaxValue(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(X->getType(), -C));
934
935 APInt SMax = APInt::getSignedMaxValue(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(X->getType(), 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(X->getType(), 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)).
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);
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>(I.getOperand(0));
978 if (IsAShr) {
979 if (AP2.isAllOnes())
980 return nullptr;
981 if (AP2.isNegative() != AP1.isNegative())
982 return nullptr;
983 if (AP2.sgt(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(A->getType(), AP2.logBase2()));
991
992 if (AP1 == AP2)
993 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(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(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(A->getType(), Shift));
1007 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1008 } else if (AP1 == AP2.lshr(Shift)) {
1009 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), 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(I.getType(), I.getPredicate() == I.ICMP_NE);
1016 return replaceInstUsesWith(I, TorF);
1017}
1018
1019/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1020/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
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);
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(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
1042
1043 if (AP1 == AP2)
1044 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(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(Shift) == AP1)
1050 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), 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(I.getType(), I.getPredicate() == I.ICMP_NE);
1055 return replaceInstUsesWith(I, 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///
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>(I.getOperand(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(CI1->getBitWidth(), 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(A, 0, &I) > NewWidth ||
1098 IC.ComputeMaxSignificantBits(B, 0, &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>(AddWithCst->getOperand(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>(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(OrigAdd->getContext(), NewWidth);
1125 I.getModule(), Intrinsic::sadd_with_overflow, 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(A, NewType, A->getName() + ".trunc");
1134 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc");
1135 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd");
1136 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result");
1137 Value *ZExt = Builder.CreateZExt(Add, 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(*OrigAdd, ZExt);
1142 IC.eraseInstFromFunction(*OrigAdd);
1143
1144 // The original icmp gets replaced with the overflow value.
1145 return ExtractValueInst::Create(Call, 1, "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
1153 // This fold is only valid for equality predicates.
1154 if (!I.isEquality())
1155 return nullptr;
1157 Value *X, *Y, *Zero;
1158 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))),
1159 m_CombineAnd(m_Zero(), m_Value(Zero)))))
1160 return nullptr;
1161 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, 0, &I))
1162 return nullptr;
1163 // This may increase instruction count, we don't enforce that Y is a constant.
1164 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType()));
1165 Value *Masked = Builder.CreateAnd(X, Mask);
1166 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, 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.
1172 Instruction *Val;
1174 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero())))
1175 return nullptr;
1176
1177 Value *X;
1178 Type *XTy;
1179
1180 Constant *C;
1181 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) {
1182 XTy = X->getType();
1183 unsigned XBitWidth = XTy->getScalarSizeInBits();
1185 APInt(XBitWidth, XBitWidth - 1))))
1186 return nullptr;
1187 } else if (isa<BinaryOperator>(Val) &&
1189 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val),
1190 /*AnalyzeForSignBitExtraction=*/true))) {
1191 XTy = X->getType();
1192 } else
1193 return nullptr;
1194
1195 return ICmpInst::Create(Instruction::ICmp,
1199}
1200
1201// Handle icmp pred X, 0
1203 CmpInst::Predicate Pred = Cmp.getPredicate();
1204 if (!match(Cmp.getOperand(1), 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(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) {
1212 return new ICmpInst(Pred, B, Cmp.getOperand(1));
1214 return new ICmpInst(Pred, A, Cmp.getOperand(1));
1215 }
1216 }
1217
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(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) &&
1227 ICmpInst::isEquality(Pred)) {
1228 KnownBits XKnown = computeKnownBits(X, 0, &Cmp);
1229 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp);
1230 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1231 return new ICmpInst(Pred, X, Cmp.getOperand(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(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) &&
1237 ICmpInst::isEquality(Pred)) {
1238
1239 KnownBits XKnown = computeKnownBits(X, 0, &Cmp);
1240 // if X % 2 != 0
1241 // (icmp eq/ne Y)
1242 if (XKnown.countMaxTrailingZeros() == 0)
1243 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1244
1245 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp);
1246 // if Y % 2 != 0
1247 // (icmp eq/ne X)
1248 if (YKnown.countMaxTrailingZeros() == 0)
1249 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1250
1251 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0));
1252 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1253 const SimplifyQuery Q = SQ.getWithInstruction(&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(X, Q))
1260 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1261
1262 // if Y non-zero and NoOverflow(X * Y)
1263 // (icmp eq/ne X)
1264 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q))
1265 return new ICmpInst(Pred, X, Cmp.getOperand(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 return nullptr;
1277}
1278
1279/// Fold icmp Pred X, C.
1280/// TODO: This code structure does not make sense. The saturating add fold
1281/// should be moved to some other helper and extended as noted below (it is also
1282/// possible that code has been made unnecessary - do we canonicalize IR to
1283/// overflow/saturating intrinsics or not?).
1285 // Match the following pattern, which is a common idiom when writing
1286 // overflow-safe integer arithmetic functions. The source performs an addition
1287 // in wider type and explicitly checks for overflow using comparisons against
1288 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1289 //
1290 // TODO: This could probably be generalized to handle other overflow-safe
1291 // operations if we worked out the formulas to compute the appropriate magic
1292 // constants.
1293 //
1294 // sum = a + b
1295 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1296 CmpInst::Predicate Pred = Cmp.getPredicate();
1297 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
1298 Value *A, *B;
1299 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1300 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) &&
1301 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1302 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
1303 return Res;
1304
1305 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1306 Constant *C = dyn_cast<Constant>(Op1);
1307 if (!C)
1308 return nullptr;
1309
1310 if (auto *Phi = dyn_cast<PHINode>(Op0))
1311 if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
1313 for (Value *V : Phi->incoming_values()) {
1314 Constant *Res =
1315 ConstantFoldCompareInstOperands(Pred, cast<Constant>(V), C, DL);
1316 if (!Res)
1317 return nullptr;
1318 Ops.push_back(Res);
1319 }
1321 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());
1322 for (auto [V, Pred] : zip(Ops, Phi->blocks()))
1323 NewPhi->addIncoming(V, Pred);
1324 return replaceInstUsesWith(Cmp, NewPhi);
1325 }
1326
1328 return R;
1329
1330 return nullptr;
1331}
1332
1333/// Canonicalize icmp instructions based on dominating conditions.
1335 // We already checked simple implication in InstSimplify, only handle complex
1336 // cases here.
1337 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1);
1338 const APInt *C;
1339 if (!match(Y, m_APInt(C)))
1340 return nullptr;
1341
1342 CmpInst::Predicate Pred = Cmp.getPredicate();
1344
1345 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1346 const APInt *DomC) -> Instruction * {
1347 // We have 2 compares of a variable with constants. Calculate the constant
1348 // ranges of those compares to see if we can transform the 2nd compare:
1349 // DomBB:
1350 // DomCond = icmp DomPred X, DomC
1351 // br DomCond, CmpBB, FalseBB
1352 // CmpBB:
1353 // Cmp = icmp Pred X, C
1354 ConstantRange DominatingCR =
1355 ConstantRange::makeExactICmpRegion(DomPred, *DomC);
1356 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1357 ConstantRange Difference = DominatingCR.difference(CR);
1358 if (Intersection.isEmptySet())
1359 return replaceInstUsesWith(Cmp, Builder.getFalse());
1360 if (Difference.isEmptySet())
1361 return replaceInstUsesWith(Cmp, Builder.getTrue());
1362
1363 // Canonicalizing a sign bit comparison that gets used in a branch,
1364 // pessimizes codegen by generating branch on zero instruction instead
1365 // of a test and branch. So we avoid canonicalizing in such situations
1366 // because test and branch instruction has better branch displacement
1367 // than compare and branch instruction.
1368 bool UnusedBit;
1369 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit);
1370 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp)))
1371 return nullptr;
1372
1373 // Avoid an infinite loop with min/max canonicalization.
1374 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1375 if (Cmp.hasOneUse() &&
1376 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value())))
1377 return nullptr;
1378
1379 if (const APInt *EqC = Intersection.getSingleElement())
1380 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC));
1381 if (const APInt *NeC = Difference.getSingleElement())
1382 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC));
1383 return nullptr;
1384 };
1385
1386 for (BranchInst *BI : DC.conditionsFor(X)) {
1387 ICmpInst::Predicate DomPred;
1388 const APInt *DomC;
1389 if (!match(BI->getCondition(),
1390 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC))))
1391 continue;
1392
1393 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1394 if (DT.dominates(Edge0, Cmp.getParent())) {
1395 if (auto *V = handleDomCond(DomPred, DomC))
1396 return V;
1397 } else {
1398 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1399 if (DT.dominates(Edge1, Cmp.getParent()))
1400 if (auto *V =
1401 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC))
1402 return V;
1403 }
1404 }
1405
1406 return nullptr;
1407}
1408
1409/// Fold icmp (trunc X), C.
1411 TruncInst *Trunc,
1412 const APInt &C) {
1413 ICmpInst::Predicate Pred = Cmp.getPredicate();
1414 Value *X = Trunc->getOperand(0);
1415 Type *SrcTy = X->getType();
1416 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1417 SrcBits = SrcTy->getScalarSizeInBits();
1418
1419 // Match (icmp pred (trunc nuw/nsw X), C)
1420 // Which we can convert to (icmp pred X, (sext/zext C))
1421 if (shouldChangeType(Trunc->getType(), SrcTy)) {
1422 if (Trunc->hasNoSignedWrap())
1423 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
1424 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1425 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
1426 }
1427
1428 if (C.isOne() && C.getBitWidth() > 1) {
1429 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1430 Value *V = nullptr;
1431 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
1432 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1433 ConstantInt::get(V->getType(), 1));
1434 }
1435
1436 // TODO: Handle any shifted constant by subtracting trailing zeros.
1437 // TODO: Handle non-equality predicates.
1438 Value *Y;
1439 if (Cmp.isEquality() && match(X, m_Shl(m_One(), m_Value(Y)))) {
1440 // (trunc (1 << Y) to iN) == 0 --> Y u>= N
1441 // (trunc (1 << Y) to iN) != 0 --> Y u< N
1442 if (C.isZero()) {
1443 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1444 return new ICmpInst(NewPred, Y, ConstantInt::get(SrcTy, DstBits));
1445 }
1446 // (trunc (1 << Y) to iN) == 2**C --> Y == C
1447 // (trunc (1 << Y) to iN) != 2**C --> Y != C
1448 if (C.isPowerOf2())
1449 return new ICmpInst(Pred, Y, ConstantInt::get(SrcTy, C.logBase2()));
1450 }
1451
1452 if (Cmp.isEquality() && Trunc->hasOneUse()) {
1453 // Canonicalize to a mask and wider compare if the wide type is suitable:
1454 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1455 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {
1456 Constant *Mask =
1457 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits));
1458 Value *And = Builder.CreateAnd(X, Mask);
1459 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits));
1460 return new ICmpInst(Pred, And, WideC);
1461 }
1462
1463 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1464 // of the high bits truncated out of x are known.
1465 KnownBits Known = computeKnownBits(X, 0, &Cmp);
1466
1467 // If all the high bits are known, we can do this xform.
1468 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1469 // Pull in the high bits from known-ones set.
1470 APInt NewRHS = C.zext(SrcBits);
1471 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
1472 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS));
1473 }
1474 }
1475
1476 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1477 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1478 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1479 Value *ShOp;
1480 const APInt *ShAmtC;
1481 bool TrueIfSigned;
1482 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
1483 match(X, m_Shr(m_Value(ShOp), m_APInt(ShAmtC))) &&
1484 DstBits == SrcBits - ShAmtC->getZExtValue()) {
1485 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1487 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1489 }
1490
1491 return nullptr;
1492}
1493
1494/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1495/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1498 const SimplifyQuery &Q) {
1499 Value *X, *Y;
1501 bool YIsSExt = false;
1502 // Try to match icmp (trunc X), (trunc Y)
1503 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {
1504 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() &
1505 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind();
1506 if (Cmp.isSigned()) {
1507 // For signed comparisons, both truncs must be nsw.
1508 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1509 return nullptr;
1510 } else {
1511 // For unsigned and equality comparisons, either both must be nuw or
1512 // both must be nsw, we don't care which.
1513 if (!NoWrapFlags)
1514 return nullptr;
1515 }
1516
1517 if (X->getType() != Y->getType() &&
1518 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))
1519 return nullptr;
1520 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) &&
1521 isDesirableIntType(Y->getType()->getScalarSizeInBits())) {
1522 std::swap(X, Y);
1523 Pred = Cmp.getSwappedPredicate(Pred);
1524 }
1525 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1526 }
1527 // Try to match icmp (trunc nuw X), (zext Y)
1528 else if (!Cmp.isSigned() &&
1529 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),
1530 m_OneUse(m_ZExt(m_Value(Y)))))) {
1531 // Can fold trunc nuw + zext for unsigned and equality predicates.
1532 }
1533 // Try to match icmp (trunc nsw X), (sext Y)
1534 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),
1536 // Can fold trunc nsw + zext/sext for all predicates.
1537 YIsSExt =
1538 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));
1539 } else
1540 return nullptr;
1541
1542 Type *TruncTy = Cmp.getOperand(0)->getType();
1543 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1544
1545 // If this transform will end up changing from desirable types -> undesirable
1546 // types skip it.
1547 if (isDesirableIntType(TruncBits) &&
1548 !isDesirableIntType(X->getType()->getScalarSizeInBits()))
1549 return nullptr;
1550
1551 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);
1552 return new ICmpInst(Pred, X, NewY);
1553}
1554
1555/// Fold icmp (xor X, Y), C.
1558 const APInt &C) {
1559 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1560 return I;
1561
1562 Value *X = Xor->getOperand(0);
1563 Value *Y = Xor->getOperand(1);
1564 const APInt *XorC;
1565 if (!match(Y, m_APInt(XorC)))
1566 return nullptr;
1567
1568 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1569 // fold the xor.
1570 ICmpInst::Predicate Pred = Cmp.getPredicate();
1571 bool TrueIfSigned = false;
1572 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {
1573
1574 // If the sign bit of the XorCst is not set, there is no change to
1575 // the operation, just stop using the Xor.
1576 if (!XorC->isNegative())
1577 return replaceOperand(Cmp, 0, X);
1578
1579 // Emit the opposite comparison.
1580 if (TrueIfSigned)
1581 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1582 ConstantInt::getAllOnesValue(X->getType()));
1583 else
1584 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1585 ConstantInt::getNullValue(X->getType()));
1586 }
1587
1588 if (Xor->hasOneUse()) {
1589 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1590 if (!Cmp.isEquality() && XorC->isSignMask()) {
1591 Pred = Cmp.getFlippedSignednessPredicate();
1592 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1593 }
1594
1595 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1596 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1597 Pred = Cmp.getFlippedSignednessPredicate();
1598 Pred = Cmp.getSwappedPredicate(Pred);
1599 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1600 }
1601 }
1602
1603 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1604 if (Pred == ICmpInst::ICMP_UGT) {
1605 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1606 if (*XorC == ~C && (C + 1).isPowerOf2())
1607 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1608 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1609 if (*XorC == C && (C + 1).isPowerOf2())
1610 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1611 }
1612 if (Pred == ICmpInst::ICMP_ULT) {
1613 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1614 if (*XorC == -C && C.isPowerOf2())
1615 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1616 ConstantInt::get(X->getType(), ~C));
1617 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1618 if (*XorC == C && (-C).isPowerOf2())
1619 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1620 ConstantInt::get(X->getType(), ~C));
1621 }
1622 return nullptr;
1623}
1624
1625/// For power-of-2 C:
1626/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1627/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1630 const APInt &C) {
1631 CmpInst::Predicate Pred = Cmp.getPredicate();
1632 APInt PowerOf2;
1633 if (Pred == ICmpInst::ICMP_ULT)
1634 PowerOf2 = C;
1635 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1636 PowerOf2 = C + 1;
1637 else
1638 return nullptr;
1639 if (!PowerOf2.isPowerOf2())
1640 return nullptr;
1641 Value *X;
1642 const APInt *ShiftC;
1644 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))
1645 return nullptr;
1646 uint64_t Shift = ShiftC->getLimitedValue();
1647 Type *XType = X->getType();
1648 if (Shift == 0 || PowerOf2.isMinSignedValue())
1649 return nullptr;
1650 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));
1651 APInt Bound =
1652 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1653 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));
1654}
1655
1656/// Fold icmp (and (sh X, Y), C2), C1.
1659 const APInt &C1,
1660 const APInt &C2) {
1661 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1662 if (!Shift || !Shift->isShift())
1663 return nullptr;
1664
1665 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1666 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1667 // code produced by the clang front-end, for bitfield access.
1668 // This seemingly simple opportunity to fold away a shift turns out to be
1669 // rather complicated. See PR17827 for details.
1670 unsigned ShiftOpcode = Shift->getOpcode();
1671 bool IsShl = ShiftOpcode == Instruction::Shl;
1672 const APInt *C3;
1673 if (match(Shift->getOperand(1), m_APInt(C3))) {
1674 APInt NewAndCst, NewCmpCst;
1675 bool AnyCmpCstBitsShiftedOut;
1676 if (ShiftOpcode == Instruction::Shl) {
1677 // For a left shift, we can fold if the comparison is not signed. We can
1678 // also fold a signed comparison if the mask value and comparison value
1679 // are not negative. These constraints may not be obvious, but we can
1680 // prove that they are correct using an SMT solver.
1681 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1682 return nullptr;
1683
1684 NewCmpCst = C1.lshr(*C3);
1685 NewAndCst = C2.lshr(*C3);
1686 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;
1687 } else if (ShiftOpcode == Instruction::LShr) {
1688 // For a logical right shift, we can fold if the comparison is not signed.
1689 // We can also fold a signed comparison if the shifted mask value and the
1690 // shifted comparison value are not negative. These constraints may not be
1691 // obvious, but we can prove that they are correct using an SMT solver.
1692 NewCmpCst = C1.shl(*C3);
1693 NewAndCst = C2.shl(*C3);
1694 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;
1695 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1696 return nullptr;
1697 } else {
1698 // For an arithmetic shift, check that both constants don't use (in a
1699 // signed sense) the top bits being shifted out.
1700 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1701 NewCmpCst = C1.shl(*C3);
1702 NewAndCst = C2.shl(*C3);
1703 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;
1704 if (NewAndCst.ashr(*C3) != C2)
1705 return nullptr;
1706 }
1707
1708 if (AnyCmpCstBitsShiftedOut) {
1709 // If we shifted bits out, the fold is not going to work out. As a
1710 // special case, check to see if this means that the result is always
1711 // true or false now.
1712 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1713 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1714 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1715 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1716 } else {
1717 Value *NewAnd = Builder.CreateAnd(
1718 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));
1719 return new ICmpInst(Cmp.getPredicate(),
1720 NewAnd, ConstantInt::get(And->getType(), NewCmpCst));
1721 }
1722 }
1723
1724 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1725 // preferable because it allows the C2 << Y expression to be hoisted out of a
1726 // loop if Y is invariant and X is not.
1727 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1728 !Shift->isArithmeticShift() &&
1729 ((!IsShl && C2.isOne()) || !isa<Constant>(Shift->getOperand(0)))) {
1730 // Compute C2 << Y.
1731 Value *NewShift =
1732 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))
1733 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));
1734
1735 // Compute X & (C2 << Y).
1736 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1737 return replaceOperand(Cmp, 0, NewAnd);
1738 }
1739
1740 return nullptr;
1741}
1742
1743/// Fold icmp (and X, C2), C1.
1746 const APInt &C1) {
1747 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1748
1749 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
1750 // TODO: We canonicalize to the longer form for scalars because we have
1751 // better analysis/folds for icmp, and codegen may be better with icmp.
1752 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&
1753 match(And->getOperand(1), m_One()))
1754 return new TruncInst(And->getOperand(0), Cmp.getType());
1755
1756 const APInt *C2;
1757 Value *X;
1758 if (!match(And, m_And(m_Value(X), m_APInt(C2))))
1759 return nullptr;
1760
1761 // Don't perform the following transforms if the AND has multiple uses
1762 if (!And->hasOneUse())
1763 return nullptr;
1764
1765 if (Cmp.isEquality() && C1.isZero()) {
1766 // Restrict this fold to single-use 'and' (PR10267).
1767 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1768 if (C2->isSignMask()) {
1769 Constant *Zero = Constant::getNullValue(X->getType());
1770 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1771 return new ICmpInst(NewPred, X, Zero);
1772 }
1773
1774 APInt NewC2 = *C2;
1775 KnownBits Know = computeKnownBits(And->getOperand(0), 0, And);
1776 // Set high zeros of C2 to allow matching negated power-of-2.
1777 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
1778 Know.countMinLeadingZeros());
1779
1780 // Restrict this fold only for single-use 'and' (PR10267).
1781 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1782 if (NewC2.isNegatedPowerOf2()) {
1783 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);
1784 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1785 return new ICmpInst(NewPred, X, NegBOC);
1786 }
1787 }
1788
1789 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1790 // the input width without changing the value produced, eliminate the cast:
1791 //
1792 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1793 //
1794 // We can do this transformation if the constants do not have their sign bits
1795 // set or if it is an equality comparison. Extending a relational comparison
1796 // when we're checking the sign bit would not work.
1797 Value *W;
1798 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&
1799 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1800 // TODO: Is this a good transform for vectors? Wider types may reduce
1801 // throughput. Should this transform be limited (even for scalars) by using
1802 // shouldChangeType()?
1803 if (!Cmp.getType()->isVectorTy()) {
1804 Type *WideType = W->getType();
1805 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1806 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));
1807 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1808 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());
1809 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1810 }
1811 }
1812
1813 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))
1814 return I;
1815
1816 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1817 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1818 //
1819 // iff pred isn't signed
1820 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&
1821 match(And->getOperand(1), m_One())) {
1822 Constant *One = cast<Constant>(And->getOperand(1));
1823 Value *Or = And->getOperand(0);
1824 Value *A, *B, *LShr;
1825 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1826 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1827 unsigned UsesRemoved = 0;
1828 if (And->hasOneUse())
1829 ++UsesRemoved;
1830 if (Or->hasOneUse())
1831 ++UsesRemoved;
1832 if (LShr->hasOneUse())
1833 ++UsesRemoved;
1834
1835 // Compute A & ((1 << B) | 1)
1836 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;
1837 if (UsesRemoved >= RequireUsesRemoved) {
1838 Value *NewOr =
1839 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),
1840 /*HasNUW=*/true),
1841 One, Or->getName());
1842 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1843 return replaceOperand(Cmp, 0, NewAnd);
1844 }
1845 }
1846 }
1847
1848 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1849 // llvm.is.fpclass(X, fcInf|fcNan)
1850 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1851 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1852 Value *V;
1853 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1854 Attribute::NoImplicitFloat) &&
1855 Cmp.isEquality() &&
1857 Type *FPType = V->getType()->getScalarType();
1858 if (FPType->isIEEELikeFPTy() && C1 == *C2) {
1859 APInt ExponentMask =
1861 if (C1 == ExponentMask) {
1862 unsigned Mask = FPClassTest::fcNan | FPClassTest::fcInf;
1863 if (isICMP_NE)
1864 Mask = ~Mask & fcAllFlags;
1865 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));
1866 }
1867 }
1868 }
1869
1870 return nullptr;
1871}
1872
1873/// Fold icmp (and X, Y), C.
1876 const APInt &C) {
1877 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1878 return I;
1879
1880 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1881 bool TrueIfNeg;
1882 if (isSignBitCheck(Pred, C, TrueIfNeg)) {
1883 // ((X - 1) & ~X) < 0 --> X == 0
1884 // ((X - 1) & ~X) >= 0 --> X != 0
1885 Value *X;
1886 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&
1887 match(And->getOperand(1), m_Not(m_Specific(X)))) {
1888 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1889 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));
1890 }
1891 // (X & -X) < 0 --> X == MinSignedC
1892 // (X & -X) > -1 --> X != MinSignedC
1893 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {
1894 Constant *MinSignedC = ConstantInt::get(
1895 X->getType(),
1896 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
1897 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1898 return new ICmpInst(NewPred, X, MinSignedC);
1899 }
1900 }
1901
1902 // TODO: These all require that Y is constant too, so refactor with the above.
1903
1904 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1905 Value *X = And->getOperand(0);
1906 Value *Y = And->getOperand(1);
1907 if (auto *C2 = dyn_cast<ConstantInt>(Y))
1908 if (auto *LI = dyn_cast<LoadInst>(X))
1909 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1910 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1911 if (Instruction *Res =
1912 foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2))
1913 return Res;
1914
1915 if (!Cmp.isEquality())
1916 return nullptr;
1917
1918 // X & -C == -C -> X > u ~C
1919 // X & -C != -C -> X <= u ~C
1920 // iff C is a power of 2
1921 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {
1922 auto NewPred =
1924 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1925 }
1926
1927 // If we are testing the intersection of 2 select-of-nonzero-constants with no
1928 // common bits set, it's the same as checking if exactly one select condition
1929 // is set:
1930 // ((A ? TC : FC) & (B ? TC : FC)) == 0 --> xor A, B
1931 // ((A ? TC : FC) & (B ? TC : FC)) != 0 --> not(xor A, B)
1932 // TODO: Generalize for non-constant values.
1933 // TODO: Handle signed/unsigned predicates.
1934 // TODO: Handle other bitwise logic connectors.
1935 // TODO: Extend to handle a non-zero compare constant.
1936 if (C.isZero() && (Pred == CmpInst::ICMP_EQ || And->hasOneUse())) {
1937 assert(Cmp.isEquality() && "Not expecting non-equality predicates");
1938 Value *A, *B;
1939 const APInt *TC, *FC;
1940 if (match(X, m_Select(m_Value(A), m_APInt(TC), m_APInt(FC))) &&
1941 match(Y,
1942 m_Select(m_Value(B), m_SpecificInt(*TC), m_SpecificInt(*FC))) &&
1943 !TC->isZero() && !FC->isZero() && !TC->intersects(*FC)) {
1944 Value *R = Builder.CreateXor(A, B);
1945 if (Pred == CmpInst::ICMP_NE)
1946 R = Builder.CreateNot(R);
1947 return replaceInstUsesWith(Cmp, R);
1948 }
1949 }
1950
1951 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
1952 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1953 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1954 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
1956 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
1957 Value *TruncY = Builder.CreateTrunc(Y, X->getType());
1958 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
1959 Value *And = Builder.CreateAnd(TruncY, X);
1961 }
1962 return BinaryOperator::CreateAnd(TruncY, X);
1963 }
1964
1965 // (icmp eq/ne (and (shl -1, X), Y), 0)
1966 // -> (icmp eq/ne (lshr Y, X), 0)
1967 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
1968 // highly unlikely the non-zero case will ever show up in code.
1969 if (C.isZero() &&
1971 m_Value(Y))))) {
1972 Value *LShr = Builder.CreateLShr(Y, X);
1973 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));
1974 }
1975
1976 return nullptr;
1977}
1978
1979/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
1981 InstCombiner::BuilderTy &Builder) {
1982 // Are we using xors or subs to bitwise check for a pair or pairs of
1983 // (in)equalities? Convert to a shorter form that has more potential to be
1984 // folded even further.
1985 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
1986 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
1987 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
1988 // (X1 == X2) && (X3 == X4) && (X5 == X6)
1989 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
1990 // (X1 != X2) || (X3 != X4) || (X5 != X6)
1992 SmallVector<Value *, 16> WorkList(1, Or);
1993
1994 while (!WorkList.empty()) {
1995 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
1996 Value *Lhs, *Rhs;
1997
1998 if (match(OrOperatorArgument,
1999 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {
2000 CmpValues.emplace_back(Lhs, Rhs);
2001 return;
2002 }
2003
2004 if (match(OrOperatorArgument,
2005 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {
2006 CmpValues.emplace_back(Lhs, Rhs);
2007 return;
2008 }
2009
2010 WorkList.push_back(OrOperatorArgument);
2011 };
2012
2013 Value *CurrentValue = WorkList.pop_back_val();
2014 Value *OrOperatorLhs, *OrOperatorRhs;
2015
2016 if (!match(CurrentValue,
2017 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {
2018 return nullptr;
2019 }
2020
2021 MatchOrOperatorArgument(OrOperatorRhs);
2022 MatchOrOperatorArgument(OrOperatorLhs);
2023 }
2024
2025 ICmpInst::Predicate Pred = Cmp.getPredicate();
2026 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2027 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,
2028 CmpValues.rbegin()->second);
2029
2030 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2031 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);
2032 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);
2033 }
2034
2035 return LhsCmp;
2036}
2037
2038/// Fold icmp (or X, Y), C.
2041 const APInt &C) {
2042 ICmpInst::Predicate Pred = Cmp.getPredicate();
2043 if (C.isOne()) {
2044 // icmp slt signum(V) 1 --> icmp slt V, 1
2045 Value *V = nullptr;
2046 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
2047 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2048 ConstantInt::get(V->getType(), 1));
2049 }
2050
2051 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
2052
2053 // (icmp eq/ne (or disjoint x, C0), C1)
2054 // -> (icmp eq/ne x, C0^C1)
2055 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
2056 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {
2057 Value *NewC =
2058 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));
2059 return new ICmpInst(Pred, OrOp0, NewC);
2060 }
2061
2062 const APInt *MaskC;
2063 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {
2064 if (*MaskC == C && (C + 1).isPowerOf2()) {
2065 // X | C == C --> X <=u C
2066 // X | C != C --> X >u C
2067 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2069 return new ICmpInst(Pred, OrOp0, OrOp1);
2070 }
2071
2072 // More general: canonicalize 'equality with set bits mask' to
2073 // 'equality with clear bits mask'.
2074 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2075 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2076 if (Or->hasOneUse()) {
2077 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));
2078 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));
2079 return new ICmpInst(Pred, And, NewC);
2080 }
2081 }
2082
2083 // (X | (X-1)) s< 0 --> X s< 1
2084 // (X | (X-1)) s> -1 --> X s> 0
2085 Value *X;
2086 bool TrueIfSigned;
2087 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
2089 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2090 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
2091 return new ICmpInst(NewPred, X, NewC);
2092 }
2093
2094 const APInt *OrC;
2095 // icmp(X | OrC, C) --> icmp(X, 0)
2096 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {
2097 switch (Pred) {
2098 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2099 case ICmpInst::ICMP_SLT:
2100 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2101 case ICmpInst::ICMP_SGE:
2102 if (OrC->sge(C))
2103 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
2104 break;
2105 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2106 case ICmpInst::ICMP_SLE:
2107 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2108 case ICmpInst::ICMP_SGT:
2109 if (OrC->sgt(C))
2111 ConstantInt::getNullValue(X->getType()));
2112 break;
2113 default:
2114 break;
2115 }
2116 }
2117
2118 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2119 return nullptr;
2120
2121 Value *P, *Q;
2123 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2124 // -> and (icmp eq P, null), (icmp eq Q, null).
2125 Value *CmpP =
2126 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
2127 Value *CmpQ =
2129 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2130 return BinaryOperator::Create(BOpc, CmpP, CmpQ);
2131 }
2132
2133 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2134 return replaceInstUsesWith(Cmp, V);
2135
2136 return nullptr;
2137}
2138
2139/// Fold icmp (mul X, Y), C.
2142 const APInt &C) {
2143 ICmpInst::Predicate Pred = Cmp.getPredicate();
2144 Type *MulTy = Mul->getType();
2145 Value *X = Mul->getOperand(0);
2146
2147 // If there's no overflow:
2148 // X * X == 0 --> X == 0
2149 // X * X != 0 --> X != 0
2150 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
2151 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2152 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2153
2154 const APInt *MulC;
2155 if (!match(Mul->getOperand(1), m_APInt(MulC)))
2156 return nullptr;
2157
2158 // If this is a test of the sign bit and the multiply is sign-preserving with
2159 // a constant operand, use the multiply LHS operand instead:
2160 // (X * +MulC) < 0 --> X < 0
2161 // (X * -MulC) < 0 --> X > 0
2162 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2163 if (MulC->isNegative())
2164 Pred = ICmpInst::getSwappedPredicate(Pred);
2165 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2166 }
2167
2168 if (MulC->isZero())
2169 return nullptr;
2170
2171 // If the multiply does not wrap or the constant is odd, try to divide the
2172 // compare constant by the multiplication factor.
2173 if (Cmp.isEquality()) {
2174 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2175 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2176 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2177 return new ICmpInst(Pred, X, NewC);
2178 }
2179
2180 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2181 // correct to transform if MulC * N == C including overflow. I.e with i8
2182 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2183 // miss that case.
2184 if (C.urem(*MulC).isZero()) {
2185 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2186 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2187 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2188 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2189 return new ICmpInst(Pred, X, NewC);
2190 }
2191 }
2192 }
2193
2194 // With a matching no-overflow guarantee, fold the constants:
2195 // (X * MulC) < C --> X < (C / MulC)
2196 // (X * MulC) > C --> X > (C / MulC)
2197 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2198 Constant *NewC = nullptr;
2199 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2200 // MININT / -1 --> overflow.
2201 if (C.isMinSignedValue() && MulC->isAllOnes())
2202 return nullptr;
2203 if (MulC->isNegative())
2204 Pred = ICmpInst::getSwappedPredicate(Pred);
2205
2206 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2207 NewC = ConstantInt::get(
2209 } else {
2210 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2211 "Unexpected predicate");
2212 NewC = ConstantInt::get(
2214 }
2215 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2216 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2217 NewC = ConstantInt::get(
2219 } else {
2220 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2221 "Unexpected predicate");
2222 NewC = ConstantInt::get(
2224 }
2225 }
2226
2227 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2228}
2229
2230/// Fold icmp (shl 1, Y), C.
2232 const APInt &C) {
2233 Value *Y;
2234 if (!match(Shl, m_Shl(m_One(), m_Value(Y))))
2235 return nullptr;
2236
2237 Type *ShiftType = Shl->getType();
2238 unsigned TypeBits = C.getBitWidth();
2239 bool CIsPowerOf2 = C.isPowerOf2();
2240 ICmpInst::Predicate Pred = Cmp.getPredicate();
2241 if (Cmp.isUnsigned()) {
2242 // (1 << Y) pred C -> Y pred Log2(C)
2243 if (!CIsPowerOf2) {
2244 // (1 << Y) < 30 -> Y <= 4
2245 // (1 << Y) <= 30 -> Y <= 4
2246 // (1 << Y) >= 30 -> Y > 4
2247 // (1 << Y) > 30 -> Y > 4
2248 if (Pred == ICmpInst::ICMP_ULT)
2249 Pred = ICmpInst::ICMP_ULE;
2250 else if (Pred == ICmpInst::ICMP_UGE)
2251 Pred = ICmpInst::ICMP_UGT;
2252 }
2253
2254 unsigned CLog2 = C.logBase2();
2255 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2256 } else if (Cmp.isSigned()) {
2257 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
2258 // (1 << Y) > 0 -> Y != 31
2259 // (1 << Y) > C -> Y != 31 if C is negative.
2260 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))
2261 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2262
2263 // (1 << Y) < 0 -> Y == 31
2264 // (1 << Y) < 1 -> Y == 31
2265 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2266 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2267 if (Pred == ICmpInst::ICMP_SLT && (C-1).sle(0))
2268 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2269 }
2270
2271 return nullptr;
2272}
2273
2274/// Fold icmp (shl X, Y), C.
2276 BinaryOperator *Shl,
2277 const APInt &C) {
2278 const APInt *ShiftVal;
2279 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
2280 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);
2281
2282 ICmpInst::Predicate Pred = Cmp.getPredicate();
2283 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2284 // -> (icmp pred X, Csle0)
2285 //
2286 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2287 // so X's must be what is used.
2288 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2289 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2290
2291 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2292 // -> (icmp eq/ne X, 0)
2293 if (ICmpInst::isEquality(Pred) && C.isZero() &&
2294 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2295 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2296
2297 // (icmp slt (shl nsw X, Y), 0/1)
2298 // -> (icmp slt X, 0/1)
2299 // (icmp sgt (shl nsw X, Y), 0/-1)
2300 // -> (icmp sgt X, 0/-1)
2301 //
2302 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2303 if (Shl->hasNoSignedWrap() &&
2304 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2305 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2306 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2307
2308 const APInt *ShiftAmt;
2309 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2310 return foldICmpShlOne(Cmp, Shl, C);
2311
2312 // Check that the shift amount is in range. If not, don't perform undefined
2313 // shifts. When the shift is visited, it will be simplified.
2314 unsigned TypeBits = C.getBitWidth();
2315 if (ShiftAmt->uge(TypeBits))
2316 return nullptr;
2317
2318 Value *X = Shl->getOperand(0);
2319 Type *ShType = Shl->getType();
2320
2321 // NSW guarantees that we are only shifting out sign bits from the high bits,
2322 // so we can ASHR the compare constant without needing a mask and eliminate
2323 // the shift.
2324 if (Shl->hasNoSignedWrap()) {
2325 if (Pred == ICmpInst::ICMP_SGT) {
2326 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2327 APInt ShiftedC = C.ashr(*ShiftAmt);
2328 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2329 }
2330 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2331 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {
2332 APInt ShiftedC = C.ashr(*ShiftAmt);
2333 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2334 }
2335 if (Pred == ICmpInst::ICMP_SLT) {
2336 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2337 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2338 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2339 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2340 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2341 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;
2342 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2343 }
2344 }
2345
2346 // NUW guarantees that we are only shifting out zero bits from the high bits,
2347 // so we can LSHR the compare constant without needing a mask and eliminate
2348 // the shift.
2349 if (Shl->hasNoUnsignedWrap()) {
2350 if (Pred == ICmpInst::ICMP_UGT) {
2351 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2352 APInt ShiftedC = C.lshr(*ShiftAmt);
2353 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2354 }
2355 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2356 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {
2357 APInt ShiftedC = C.lshr(*ShiftAmt);
2358 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2359 }
2360 if (Pred == ICmpInst::ICMP_ULT) {
2361 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2362 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2363 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2364 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2365 assert(C.ugt(0) && "ult 0 should have been eliminated");
2366 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;
2367 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2368 }
2369 }
2370
2371 if (Cmp.isEquality() && Shl->hasOneUse()) {
2372 // Strength-reduce the shift into an 'and'.
2373 Constant *Mask = ConstantInt::get(
2374 ShType,
2375 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
2376 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2377 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));
2378 return new ICmpInst(Pred, And, LShrC);
2379 }
2380
2381 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2382 bool TrueIfSigned = false;
2383 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {
2384 // (X << 31) <s 0 --> (X & 1) != 0
2385 Constant *Mask = ConstantInt::get(
2386 ShType,
2387 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
2388 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2389 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2390 And, Constant::getNullValue(ShType));
2391 }
2392
2393 // Simplify 'shl' inequality test into 'and' equality test.
2394 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2395 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2396 if ((C + 1).isPowerOf2() &&
2397 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2398 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));
2399 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2401 And, Constant::getNullValue(ShType));
2402 }
2403 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2404 if (C.isPowerOf2() &&
2405 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2406 Value *And =
2407 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));
2408 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2410 And, Constant::getNullValue(ShType));
2411 }
2412 }
2413
2414 // Transform (icmp pred iM (shl iM %v, N), C)
2415 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2416 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2417 // This enables us to get rid of the shift in favor of a trunc that may be
2418 // free on the target. It has the additional benefit of comparing to a
2419 // smaller constant that may be more target-friendly.
2420 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
2421 if (Shl->hasOneUse() && Amt != 0 &&
2422 shouldChangeType(ShType->getScalarSizeInBits(), TypeBits - Amt)) {
2423 ICmpInst::Predicate CmpPred = Pred;
2424 APInt RHSC = C;
2425
2426 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(CmpPred)) {
2427 // Try the flipped strictness predicate.
2428 // e.g.:
2429 // icmp ult i64 (shl X, 32), 8589934593 ->
2430 // icmp ule i64 (shl X, 32), 8589934592 ->
2431 // icmp ule i32 (trunc X, i32), 2 ->
2432 // icmp ult i32 (trunc X, i32), 3
2433 if (auto FlippedStrictness =
2435 Pred, ConstantInt::get(ShType->getContext(), C))) {
2436 CmpPred = FlippedStrictness->first;
2437 RHSC = cast<ConstantInt>(FlippedStrictness->second)->getValue();
2438 }
2439 }
2440
2441 if (RHSC.countr_zero() >= Amt) {
2442 Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt);
2443 Constant *NewC =
2444 ConstantInt::get(TruncTy, RHSC.ashr(*ShiftAmt).trunc(TypeBits - Amt));
2445 return new ICmpInst(CmpPred,
2446 Builder.CreateTrunc(X, TruncTy, "", /*IsNUW=*/false,
2447 Shl->hasNoSignedWrap()),
2448 NewC);
2449 }
2450 }
2451
2452 return nullptr;
2453}
2454
2455/// Fold icmp ({al}shr X, Y), C.
2457 BinaryOperator *Shr,
2458 const APInt &C) {
2459 // An exact shr only shifts out zero bits, so:
2460 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2461 Value *X = Shr->getOperand(0);
2462 CmpInst::Predicate Pred = Cmp.getPredicate();
2463 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2464 return new ICmpInst(Pred, X, Cmp.getOperand(1));
2465
2466 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2467 const APInt *ShiftValC;
2468 if (match(X, m_APInt(ShiftValC))) {
2469 if (Cmp.isEquality())
2470 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
2471
2472 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2473 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2474 bool TrueIfSigned;
2475 if (!IsAShr && ShiftValC->isNegative() &&
2476 isSignBitCheck(Pred, C, TrueIfSigned))
2477 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2478 Shr->getOperand(1),
2479 ConstantInt::getNullValue(X->getType()));
2480
2481 // If the shifted constant is a power-of-2, test the shift amount directly:
2482 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2483 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2484 if (!IsAShr && ShiftValC->isPowerOf2() &&
2485 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2486 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2487 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2488 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2489
2490 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2491 unsigned ShiftLZ = ShiftValC->countl_zero();
2492 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
2493 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2494 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
2495 }
2496 }
2497
2498 const APInt *ShiftAmtC;
2499 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))
2500 return nullptr;
2501
2502 // Check that the shift amount is in range. If not, don't perform undefined
2503 // shifts. When the shift is visited it will be simplified.
2504 unsigned TypeBits = C.getBitWidth();
2505 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);
2506 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2507 return nullptr;
2508
2509 bool IsExact = Shr->isExact();
2510 Type *ShrTy = Shr->getType();
2511 // TODO: If we could guarantee that InstSimplify would handle all of the
2512 // constant-value-based preconditions in the folds below, then we could assert
2513 // those conditions rather than checking them. This is difficult because of
2514 // undef/poison (PR34838).
2515 if (IsAShr && Shr->hasOneUse()) {
2516 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2517 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2518 // When C - 1 is a power of two and the transform can be legally
2519 // performed, prefer this form so the produced constant is close to a
2520 // power of two.
2521 // icmp slt/ult (ashr exact X, ShAmtC), C
2522 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2523 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
2524 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2525 }
2526 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2527 // When ShAmtC can be shifted losslessly:
2528 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2529 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2530 APInt ShiftedC = C.shl(ShAmtVal);
2531 if (ShiftedC.ashr(ShAmtVal) == C)
2532 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2533 }
2534 if (Pred == CmpInst::ICMP_SGT) {
2535 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2536 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2537 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&
2538 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
2539 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2540 }
2541 if (Pred == CmpInst::ICMP_UGT) {
2542 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2543 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2544 // clause accounts for that pattern.
2545 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2546 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
2547 (C + 1).shl(ShAmtVal).isMinSignedValue())
2548 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2549 }
2550
2551 // If the compare constant has significant bits above the lowest sign-bit,
2552 // then convert an unsigned cmp to a test of the sign-bit:
2553 // (ashr X, ShiftC) u> C --> X s< 0
2554 // (ashr X, ShiftC) u< C --> X s> -1
2555 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2556 if (Pred == CmpInst::ICMP_UGT) {
2557 return new ICmpInst(CmpInst::ICMP_SLT, X,
2559 }
2560 if (Pred == CmpInst::ICMP_ULT) {
2561 return new ICmpInst(CmpInst::ICMP_SGT, X,
2563 }
2564 }
2565 } else if (!IsAShr) {
2566 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2567 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2568 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2569 APInt ShiftedC = C.shl(ShAmtVal);
2570 if (ShiftedC.lshr(ShAmtVal) == C)
2571 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2572 }
2573 if (Pred == CmpInst::ICMP_UGT) {
2574 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2575 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2576 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))
2577 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2578 }
2579 }
2580
2581 if (!Cmp.isEquality())
2582 return nullptr;
2583
2584 // Handle equality comparisons of shift-by-constant.
2585
2586 // If the comparison constant changes with the shift, the comparison cannot
2587 // succeed (bits of the comparison constant cannot match the shifted value).
2588 // This should be known by InstSimplify and already be folded to true/false.
2589 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2590 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2591 "Expected icmp+shr simplify did not occur.");
2592
2593 // If the bits shifted out are known zero, compare the unshifted value:
2594 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2595 if (Shr->isExact())
2596 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));
2597
2598 if (C.isZero()) {
2599 // == 0 is u< 1.
2600 if (Pred == CmpInst::ICMP_EQ)
2601 return new ICmpInst(CmpInst::ICMP_ULT, X,
2602 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal)));
2603 else
2604 return new ICmpInst(CmpInst::ICMP_UGT, X,
2605 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal) - 1));
2606 }
2607
2608 if (Shr->hasOneUse()) {
2609 // Canonicalize the shift into an 'and':
2610 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2611 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2612 Constant *Mask = ConstantInt::get(ShrTy, Val);
2613 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");
2614 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));
2615 }
2616
2617 return nullptr;
2618}
2619
2621 BinaryOperator *SRem,
2622 const APInt &C) {
2623 // Match an 'is positive' or 'is negative' comparison of remainder by a
2624 // constant power-of-2 value:
2625 // (X % pow2C) sgt/slt 0
2626 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2627 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2628 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2629 return nullptr;
2630
2631 // TODO: The one-use check is standard because we do not typically want to
2632 // create longer instruction sequences, but this might be a special-case
2633 // because srem is not good for analysis or codegen.
2634 if (!SRem->hasOneUse())
2635 return nullptr;
2636
2637 const APInt *DivisorC;
2638 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
2639 return nullptr;
2640
2641 // For cmp_sgt/cmp_slt only zero valued C is handled.
2642 // For cmp_eq/cmp_ne only positive valued C is handled.
2643 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2644 !C.isZero()) ||
2645 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2646 !C.isStrictlyPositive()))
2647 return nullptr;
2648
2649 // Mask off the sign bit and the modulo bits (low-bits).
2650 Type *Ty = SRem->getType();
2652 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
2653 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);
2654
2655 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2656 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));
2657
2658 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2659 // bit is set. Example:
2660 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2661 if (Pred == ICmpInst::ICMP_SGT)
2663
2664 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2665 // bit is set. Example:
2666 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2667 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
2668}
2669
2670/// Fold icmp (udiv X, Y), C.
2672 BinaryOperator *UDiv,
2673 const APInt &C) {
2674 ICmpInst::Predicate Pred = Cmp.getPredicate();
2675 Value *X = UDiv->getOperand(0);
2676 Value *Y = UDiv->getOperand(1);
2677 Type *Ty = UDiv->getType();
2678
2679 const APInt *C2;
2680 if (!match(X, m_APInt(C2)))
2681 return nullptr;
2682
2683 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2684
2685 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2686 if (Pred == ICmpInst::ICMP_UGT) {
2687 assert(!C.isMaxValue() &&
2688 "icmp ugt X, UINT_MAX should have been simplified already.");
2689 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2690 ConstantInt::get(Ty, C2->udiv(C + 1)));
2691 }
2692
2693 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2694 if (Pred == ICmpInst::ICMP_ULT) {
2695 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2696 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2697 ConstantInt::get(Ty, C2->udiv(C)));
2698 }
2699
2700 return nullptr;
2701}
2702
2703/// Fold icmp ({su}div X, Y), C.
2705 BinaryOperator *Div,
2706 const APInt &C) {
2707 ICmpInst::Predicate Pred = Cmp.getPredicate();
2708 Value *X = Div->getOperand(0);
2709 Value *Y = Div->getOperand(1);
2710 Type *Ty = Div->getType();
2711 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2712
2713 // If unsigned division and the compare constant is bigger than
2714 // UMAX/2 (negative), there's only one pair of values that satisfies an
2715 // equality check, so eliminate the division:
2716 // (X u/ Y) == C --> (X == C) && (Y == 1)
2717 // (X u/ Y) != C --> (X != C) || (Y != 1)
2718 // Similarly, if signed division and the compare constant is exactly SMIN:
2719 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2720 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2721 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2722 (!DivIsSigned || C.isMinSignedValue())) {
2723 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));
2724 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));
2725 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2726 return BinaryOperator::Create(Logic, XBig, YOne);
2727 }
2728
2729 // Fold: icmp pred ([us]div X, C2), C -> range test
2730 // Fold this div into the comparison, producing a range check.
2731 // Determine, based on the divide type, what the range is being
2732 // checked. If there is an overflow on the low or high side, remember
2733 // it, otherwise compute the range [low, hi) bounding the new value.
2734 // See: InsertRangeTest above for the kinds of replacements possible.
2735 const APInt *C2;
2736 if (!match(Y, m_APInt(C2)))
2737 return nullptr;
2738
2739 // FIXME: If the operand types don't match the type of the divide
2740 // then don't attempt this transform. The code below doesn't have the
2741 // logic to deal with a signed divide and an unsigned compare (and
2742 // vice versa). This is because (x /s C2) <s C produces different
2743 // results than (x /s C2) <u C or (x /u C2) <s C or even
2744 // (x /u C2) <u C. Simply casting the operands and result won't
2745 // work. :( The if statement below tests that condition and bails
2746 // if it finds it.
2747 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
2748 return nullptr;
2749
2750 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2751 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2752 // division-by-constant cases should be present, we can not assert that they
2753 // have happened before we reach this icmp instruction.
2754 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2755 return nullptr;
2756
2757 // Compute Prod = C * C2. We are essentially solving an equation of
2758 // form X / C2 = C. We solve for X by multiplying C2 and C.
2759 // By solving for X, we can turn this into a range check instead of computing
2760 // a divide.
2761 APInt Prod = C * *C2;
2762
2763 // Determine if the product overflows by seeing if the product is not equal to
2764 // the divide. Make sure we do the same kind of divide as in the LHS
2765 // instruction that we're folding.
2766 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;
2767
2768 // If the division is known to be exact, then there is no remainder from the
2769 // divide, so the covered range size is unit, otherwise it is the divisor.
2770 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2771
2772 // Figure out the interval that is being checked. For example, a comparison
2773 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2774 // Compute this interval based on the constants involved and the signedness of
2775 // the compare/divide. This computes a half-open interval, keeping track of
2776 // whether either value in the interval overflows. After analysis each
2777 // overflow variable is set to 0 if it's corresponding bound variable is valid
2778 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2779 int LoOverflow = 0, HiOverflow = 0;
2780 APInt LoBound, HiBound;
2781
2782 if (!DivIsSigned) { // udiv
2783 // e.g. X/5 op 3 --> [15, 20)
2784 LoBound = Prod;
2785 HiOverflow = LoOverflow = ProdOV;
2786 if (!HiOverflow) {
2787 // If this is not an exact divide, then many values in the range collapse
2788 // to the same result value.
2789 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2790 }
2791 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2792 if (C.isZero()) { // (X / pos) op 0
2793 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2794 LoBound = -(RangeSize - 1);
2795 HiBound = RangeSize;
2796 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2797 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2798 HiOverflow = LoOverflow = ProdOV;
2799 if (!HiOverflow)
2800 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2801 } else { // (X / pos) op neg
2802 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2803 HiBound = Prod + 1;
2804 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2805 if (!LoOverflow) {
2806 APInt DivNeg = -RangeSize;
2807 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2808 }
2809 }
2810 } else if (C2->isNegative()) { // Divisor is < 0.
2811 if (Div->isExact())
2812 RangeSize.negate();
2813 if (C.isZero()) { // (X / neg) op 0
2814 // e.g. X/-5 op 0 --> [-4, 5)
2815 LoBound = RangeSize + 1;
2816 HiBound = -RangeSize;
2817 if (HiBound == *C2) { // -INTMIN = INTMIN
2818 HiOverflow = 1; // [INTMIN+1, overflow)
2819 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2820 }
2821 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2822 // e.g. X/-5 op 3 --> [-19, -14)
2823 HiBound = Prod + 1;
2824 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2825 if (!LoOverflow)
2826 LoOverflow =
2827 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;
2828 } else { // (X / neg) op neg
2829 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2830 LoOverflow = HiOverflow = ProdOV;
2831 if (!HiOverflow)
2832 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2833 }
2834
2835 // Dividing by a negative swaps the condition. LT <-> GT
2836 Pred = ICmpInst::getSwappedPredicate(Pred);
2837 }
2838
2839 switch (Pred) {
2840 default:
2841 llvm_unreachable("Unhandled icmp predicate!");
2842 case ICmpInst::ICMP_EQ:
2843 if (LoOverflow && HiOverflow)
2844 return replaceInstUsesWith(Cmp, Builder.getFalse());
2845 if (HiOverflow)
2846 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2847 X, ConstantInt::get(Ty, LoBound));
2848 if (LoOverflow)
2849 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2850 X, ConstantInt::get(Ty, HiBound));
2851 return replaceInstUsesWith(
2852 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));
2853 case ICmpInst::ICMP_NE:
2854 if (LoOverflow && HiOverflow)
2855 return replaceInstUsesWith(Cmp, Builder.getTrue());
2856 if (HiOverflow)
2857 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2858 X, ConstantInt::get(Ty, LoBound));
2859 if (LoOverflow)
2860 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2861 X, ConstantInt::get(Ty, HiBound));
2862 return replaceInstUsesWith(
2863 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));
2864 case ICmpInst::ICMP_ULT:
2865 case ICmpInst::ICMP_SLT:
2866 if (LoOverflow == +1) // Low bound is greater than input range.
2867 return replaceInstUsesWith(Cmp, Builder.getTrue());
2868 if (LoOverflow == -1) // Low bound is less than input range.
2869 return replaceInstUsesWith(Cmp, Builder.getFalse());
2870 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));
2871 case ICmpInst::ICMP_UGT:
2872 case ICmpInst::ICMP_SGT:
2873 if (HiOverflow == +1) // High bound greater than input range.
2874 return replaceInstUsesWith(Cmp, Builder.getFalse());
2875 if (HiOverflow == -1) // High bound less than input range.
2876 return replaceInstUsesWith(Cmp, Builder.getTrue());
2877 if (Pred == ICmpInst::ICMP_UGT)
2878 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));
2879 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));
2880 }
2881
2882 return nullptr;
2883}
2884
2885/// Fold icmp (sub X, Y), C.
2887 BinaryOperator *Sub,
2888 const APInt &C) {
2889 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2890 ICmpInst::Predicate Pred = Cmp.getPredicate();
2891 Type *Ty = Sub->getType();
2892
2893 // (SubC - Y) == C) --> Y == (SubC - C)
2894 // (SubC - Y) != C) --> Y != (SubC - C)
2895 Constant *SubC;
2896 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {
2897 return new ICmpInst(Pred, Y,
2898 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));
2899 }
2900
2901 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
2902 const APInt *C2;
2903 APInt SubResult;
2904 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
2905 bool HasNSW = Sub->hasNoSignedWrap();
2906 bool HasNUW = Sub->hasNoUnsignedWrap();
2907 if (match(X, m_APInt(C2)) &&
2908 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
2909 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))
2910 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));
2911
2912 // X - Y == 0 --> X == Y.
2913 // X - Y != 0 --> X != Y.
2914 // TODO: We allow this with multiple uses as long as the other uses are not
2915 // in phis. The phi use check is guarding against a codegen regression
2916 // for a loop test. If the backend could undo this (and possibly
2917 // subsequent transforms), we would not need this hack.
2918 if (Cmp.isEquality() && C.isZero() &&
2919 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))
2920 return new ICmpInst(Pred, X, Y);
2921
2922 // The following transforms are only worth it if the only user of the subtract
2923 // is the icmp.
2924 // TODO: This is an artificial restriction for all of the transforms below
2925 // that only need a single replacement icmp. Can these use the phi test
2926 // like the transform above here?
2927 if (!Sub->hasOneUse())
2928 return nullptr;
2929
2930 if (Sub->hasNoSignedWrap()) {
2931 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
2932 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
2933 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
2934
2935 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
2936 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
2937 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
2938
2939 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
2940 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
2941 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
2942
2943 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
2944 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
2945 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
2946 }
2947
2948 if (!match(X, m_APInt(C2)))
2949 return nullptr;
2950
2951 // C2 - Y <u C -> (Y | (C - 1)) == C2
2952 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
2953 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
2954 (*C2 & (C - 1)) == (C - 1))
2955 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);
2956
2957 // C2 - Y >u C -> (Y | C) != C2
2958 // iff C2 & C == C and C + 1 is a power of 2
2959 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
2960 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);
2961
2962 // We have handled special cases that reduce.
2963 // Canonicalize any remaining sub to add as:
2964 // (C2 - Y) > C --> (Y + ~C2) < ~C
2965 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",
2966 HasNUW, HasNSW);
2967 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
2968}
2969
2970static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
2971 Value *Op1, IRBuilderBase &Builder,
2972 bool HasOneUse) {
2973 auto FoldConstant = [&](bool Val) {
2974 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
2975 if (Op0->getType()->isVectorTy())
2977 cast<VectorType>(Op0->getType())->getElementCount(), Res);
2978 return Res;
2979 };
2980
2981 switch (Table.to_ulong()) {
2982 case 0: // 0 0 0 0
2983 return FoldConstant(false);
2984 case 1: // 0 0 0 1
2985 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
2986 case 2: // 0 0 1 0
2987 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
2988 case 3: // 0 0 1 1
2989 return Builder.CreateNot(Op0);
2990 case 4: // 0 1 0 0
2991 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
2992 case 5: // 0 1 0 1
2993 return Builder.CreateNot(Op1);
2994 case 6: // 0 1 1 0
2995 return Builder.CreateXor(Op0, Op1);
2996 case 7: // 0 1 1 1
2997 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
2998 case 8: // 1 0 0 0
2999 return Builder.CreateAnd(Op0, Op1);
3000 case 9: // 1 0 0 1
3001 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
3002 case 10: // 1 0 1 0
3003 return Op1;
3004 case 11: // 1 0 1 1
3005 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
3006 case 12: // 1 1 0 0
3007 return Op0;
3008 case 13: // 1 1 0 1
3009 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
3010 case 14: // 1 1 1 0
3011 return Builder.CreateOr(Op0, Op1);
3012 case 15: // 1 1 1 1
3013 return FoldConstant(true);
3014 default:
3015 llvm_unreachable("Invalid Operation");
3016 }
3017 return nullptr;
3018}
3019
3020/// Fold icmp (add X, Y), C.
3023 const APInt &C) {
3024 Value *Y = Add->getOperand(1);
3025 Value *X = Add->getOperand(0);
3026
3027 Value *Op0, *Op1;
3028 Instruction *Ext0, *Ext1;
3029 const CmpInst::Predicate Pred = Cmp.getPredicate();
3030 if (match(Add,
3033 m_ZExtOrSExt(m_Value(Op1))))) &&
3034 Op0->getType()->isIntOrIntVectorTy(1) &&
3035 Op1->getType()->isIntOrIntVectorTy(1)) {
3036 unsigned BW = C.getBitWidth();
3037 std::bitset<4> Table;
3038 auto ComputeTable = [&](bool Op0Val, bool Op1Val) {
3039 int Res = 0;
3040 if (Op0Val)
3041 Res += isa<ZExtInst>(Ext0) ? 1 : -1;
3042 if (Op1Val)
3043 Res += isa<ZExtInst>(Ext1) ? 1 : -1;
3044 return ICmpInst::compare(APInt(BW, Res, true), C, Pred);
3045 };
3046
3047 Table[0] = ComputeTable(false, false);
3048 Table[1] = ComputeTable(false, true);
3049 Table[2] = ComputeTable(true, false);
3050 Table[3] = ComputeTable(true, true);
3051 if (auto *Cond =
3052 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))
3053 return replaceInstUsesWith(Cmp, Cond);
3054 }
3055 const APInt *C2;
3056 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3057 return nullptr;
3058
3059 // Fold icmp pred (add X, C2), C.
3060 Type *Ty = Add->getType();
3061
3062 // If the add does not wrap, we can always adjust the compare by subtracting
3063 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3064 // are canonicalized to SGT/SLT/UGT/ULT.
3065 if ((Add->hasNoSignedWrap() &&
3066 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
3067 (Add->hasNoUnsignedWrap() &&
3068 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
3069 bool Overflow;
3070 APInt NewC =
3071 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);
3072 // If there is overflow, the result must be true or false.
3073 // TODO: Can we assert there is no overflow because InstSimplify always
3074 // handles those cases?
3075 if (!Overflow)
3076 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3077 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
3078 }
3079
3080 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3081 C.isNonNegative() && (C - *C2).isNonNegative() &&
3082 computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())
3083 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
3084 ConstantInt::get(Ty, C - *C2));
3085
3086 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
3087 const APInt &Upper = CR.getUpper();
3088 const APInt &Lower = CR.getLower();
3089 if (Cmp.isSigned()) {
3090 if (Lower.isSignMask())
3091 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
3092 if (Upper.isSignMask())
3093 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
3094 } else {
3095 if (Lower.isMinValue())
3096 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
3097 if (Upper.isMinValue())
3098 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
3099 }
3100
3101 // This set of folds is intentionally placed after folds that use no-wrapping
3102 // flags because those folds are likely better for later analysis/codegen.
3105
3106 // Fold compare with offset to opposite sign compare if it eliminates offset:
3107 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3108 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3109 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));
3110
3111 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3112 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3113 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));
3114
3115 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3116 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3117 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));
3118
3119 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3120 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3121 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));
3122
3123 // (X + -1) <u C --> X <=u C (if X is never null)
3124 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3125 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3126 if (llvm::isKnownNonZero(X, Q))
3127 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));
3128 }
3129
3130 if (!Add->hasOneUse())
3131 return nullptr;
3132
3133 // X+C <u C2 -> (X & -C2) == C
3134 // iff C & (C2-1) == 0
3135 // C2 is a power of 2
3136 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3138 ConstantExpr::getNeg(cast<Constant>(Y)));
3139
3140 // X+C2 <u C -> (X & C) == 2C
3141 // iff C == -(C2)
3142 // C2 is a power of 2
3143 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3145 ConstantInt::get(Ty, C * 2));
3146
3147 // X+C >u C2 -> (X & ~C2) != C
3148 // iff C & C2 == 0
3149 // C2+1 is a power of 2
3150 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3152 ConstantExpr::getNeg(cast<Constant>(Y)));
3153
3154 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3155 // to the ult form.
3156 // X+C2 >u C -> X+(C2-C-1) <u ~C
3157 if (Pred == ICmpInst::ICMP_UGT)
3158 return new ICmpInst(ICmpInst::ICMP_ULT,
3159 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
3160 ConstantInt::get(Ty, ~C));
3161
3162 return nullptr;
3163}
3164
3166 Value *&RHS, ConstantInt *&Less,
3167 ConstantInt *&Equal,
3168 ConstantInt *&Greater) {
3169 // TODO: Generalize this to work with other comparison idioms or ensure
3170 // they get canonicalized into this form.
3171
3172 // select i1 (a == b),
3173 // i32 Equal,
3174 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3175 // where Equal, Less and Greater are placeholders for any three constants.
3176 ICmpInst::Predicate PredA;
3177 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||
3178 !ICmpInst::isEquality(PredA))
3179 return false;
3180 Value *EqualVal = SI->getTrueValue();
3181 Value *UnequalVal = SI->getFalseValue();
3182 // We still can get non-canonical predicate here, so canonicalize.
3183 if (PredA == ICmpInst::ICMP_NE)
3184 std::swap(EqualVal, UnequalVal);
3185 if (!match(EqualVal, m_ConstantInt(Equal)))
3186 return false;
3187 ICmpInst::Predicate PredB;
3188 Value *LHS2, *RHS2;
3189 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),
3190 m_ConstantInt(Less), m_ConstantInt(Greater))))
3191 return false;
3192 // We can get predicate mismatch here, so canonicalize if possible:
3193 // First, ensure that 'LHS' match.
3194 if (LHS2 != LHS) {
3195 // x sgt y <--> y slt x
3196 std::swap(LHS2, RHS2);
3197 PredB = ICmpInst::getSwappedPredicate(PredB);
3198 }
3199 if (LHS2 != LHS)
3200 return false;
3201 // We also need to canonicalize 'RHS'.
3202 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
3203 // x sgt C-1 <--> x sge C <--> not(x slt C)
3204 auto FlippedStrictness =
3206 PredB, cast<Constant>(RHS2));
3207 if (!FlippedStrictness)
3208 return false;
3209 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3210 "basic correctness failure");
3211 RHS2 = FlippedStrictness->second;
3212 // And kind-of perform the result swap.
3213 std::swap(Less, Greater);
3214 PredB = ICmpInst::ICMP_SLT;
3215 }
3216 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3217}
3218
3221 ConstantInt *C) {
3222
3223 assert(C && "Cmp RHS should be a constant int!");
3224 // If we're testing a constant value against the result of a three way
3225 // comparison, the result can be expressed directly in terms of the
3226 // original values being compared. Note: We could possibly be more
3227 // aggressive here and remove the hasOneUse test. The original select is
3228 // really likely to simplify or sink when we remove a test of the result.
3229 Value *OrigLHS, *OrigRHS;
3230 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3231 if (Cmp.hasOneUse() &&
3232 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,
3233 C3GreaterThan)) {
3234 assert(C1LessThan && C2Equal && C3GreaterThan);
3235
3236 bool TrueWhenLessThan = ICmpInst::compare(
3237 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate());
3238 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(),
3239 Cmp.getPredicate());
3240 bool TrueWhenGreaterThan = ICmpInst::compare(
3241 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate());
3242
3243 // This generates the new instruction that will replace the original Cmp
3244 // Instruction. Instead of enumerating the various combinations when
3245 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3246 // false, we rely on chaining of ORs and future passes of InstCombine to
3247 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3248
3249 // When none of the three constants satisfy the predicate for the RHS (C),
3250 // the entire original Cmp can be simplified to a false.
3252 if (TrueWhenLessThan)
3254 OrigLHS, OrigRHS));
3255 if (TrueWhenEqual)
3257 OrigLHS, OrigRHS));
3258 if (TrueWhenGreaterThan)
3260 OrigLHS, OrigRHS));
3261
3262 return replaceInstUsesWith(Cmp, Cond);
3263 }
3264 return nullptr;
3265}
3266
3268 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));
3269 if (!Bitcast)
3270 return nullptr;
3271
3272 ICmpInst::Predicate Pred = Cmp.getPredicate();
3273 Value *Op1 = Cmp.getOperand(1);
3274 Value *BCSrcOp = Bitcast->getOperand(0);
3275 Type *SrcType = Bitcast->getSrcTy();
3276 Type *DstType = Bitcast->getType();
3277
3278 // Make sure the bitcast doesn't change between scalar and vector and
3279 // doesn't change the number of vector elements.
3280 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3281 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3282 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3283 Value *X;
3284 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {
3285 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3286 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3287 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3288 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3289 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3290 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3291 match(Op1, m_Zero()))
3292 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3293
3294 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3295 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))
3296 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));
3297
3298 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3299 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))
3300 return new ICmpInst(Pred, X,
3301 ConstantInt::getAllOnesValue(X->getType()));
3302 }
3303
3304 // Zero-equality checks are preserved through unsigned floating-point casts:
3305 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3306 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3307 if (match(BCSrcOp, m_UIToFP(m_Value(X))))
3308 if (Cmp.isEquality() && match(Op1, m_Zero()))
3309 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3310
3311 const APInt *C;
3312 bool TrueIfSigned;
3313 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {
3314 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3315 // the FP extend/truncate because that cast does not change the sign-bit.
3316 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3317 // The sign-bit is always the most significant bit in those types.
3318 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&
3319 (match(BCSrcOp, m_FPExt(m_Value(X))) ||
3320 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {
3321 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3322 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3323 Type *XType = X->getType();
3324
3325 // We can't currently handle Power style floating point operations here.
3326 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3327 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());
3328 if (auto *XVTy = dyn_cast<VectorType>(XType))
3329 NewType = VectorType::get(NewType, XVTy->getElementCount());
3330 Value *NewBitcast = Builder.CreateBitCast(X, NewType);
3331 if (TrueIfSigned)
3332 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3333 ConstantInt::getNullValue(NewType));
3334 else
3335 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3337 }
3338 }
3339
3340 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3341 Type *FPType = SrcType->getScalarType();
3342 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3343 Attribute::NoImplicitFloat) &&
3344 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3345 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3346 if (Mask & (fcInf | fcZero)) {
3347 if (Pred == ICmpInst::ICMP_NE)
3348 Mask = ~Mask;
3349 return replaceInstUsesWith(Cmp,
3350 Builder.createIsFPClass(BCSrcOp, Mask));
3351 }
3352 }
3353 }
3354 }
3355
3356 const APInt *C;
3357 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||
3358 !SrcType->isIntOrIntVectorTy())
3359 return nullptr;
3360
3361 // If this is checking if all elements of a vector compare are set or not,
3362 // invert the casted vector equality compare and test if all compare
3363 // elements are clear or not. Compare against zero is generally easier for
3364 // analysis and codegen.
3365 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3366 // Example: are all elements equal? --> are zero elements not equal?
3367 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3368 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3369 if (Value *NotBCSrcOp =
3370 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {
3371 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);
3372 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));
3373 }
3374 }
3375
3376 // If this is checking if all elements of an extended vector are clear or not,
3377 // compare in a narrow type to eliminate the extend:
3378 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3379 Value *X;
3380 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3381 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {
3382 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {
3383 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());
3384 Value *NewCast = Builder.CreateBitCast(X, NewType);
3385 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));
3386 }
3387 }
3388
3389 // Folding: icmp <pred> iN X, C
3390 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3391 // and C is a splat of a K-bit pattern
3392 // and SC is a constant vector = <C', C', C', ..., C'>
3393 // Into:
3394 // %E = extractelement <M x iK> %vec, i32 C'
3395 // icmp <pred> iK %E, trunc(C)
3396 Value *Vec;
3397 ArrayRef<int> Mask;
3398 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {
3399 // Check whether every element of Mask is the same constant
3400 if (all_equal(Mask)) {
3401 auto *VecTy = cast<VectorType>(SrcType);
3402 auto *EltTy = cast<IntegerType>(VecTy->getElementType());
3403 if (C->isSplat(EltTy->getBitWidth())) {
3404 // Fold the icmp based on the value of C
3405 // If C is M copies of an iK sized bit pattern,
3406 // then:
3407 // => %E = extractelement <N x iK> %vec, i32 Elem
3408 // icmp <pred> iK %SplatVal, <pattern>
3409 Value *Elem = Builder.getInt32(Mask[0]);
3410 Value *Extract = Builder.CreateExtractElement(Vec, Elem);
3411 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));
3412 return new ICmpInst(Pred, Extract, NewC);
3413 }
3414 }
3415 }
3416 return nullptr;
3417}
3418
3419/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3420/// where X is some kind of instruction.
3422 const APInt *C;
3423
3424 if (match(Cmp.getOperand(1), m_APInt(C))) {
3425 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))
3426 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))
3427 return I;
3428
3429 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))
3430 // For now, we only support constant integers while folding the
3431 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3432 // similar to the cases handled by binary ops above.
3433 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))
3434 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))
3435 return I;
3436
3437 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))
3438 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))
3439 return I;
3440
3441 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))
3443 return I;
3444
3445 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3446 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3447 // TODO: This checks one-use, but that is not strictly necessary.
3448 Value *Cmp0 = Cmp.getOperand(0);
3449 Value *X, *Y;
3450 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3451 (match(Cmp0,
3452 m_ExtractValue<0>(m_Intrinsic<Intrinsic::ssub_with_overflow>(
3453 m_Value(X), m_Value(Y)))) ||
3454 match(Cmp0,
3455 m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>(
3456 m_Value(X), m_Value(Y))))))
3457 return new ICmpInst(Cmp.getPredicate(), X, Y);
3458 }
3459
3460 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))
3462
3463 return nullptr;
3464}
3465
3466/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3467/// icmp eq/ne BO, C.
3469 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3470 // TODO: Some of these folds could work with arbitrary constants, but this
3471 // function is limited to scalar and vector splat constants.
3472 if (!Cmp.isEquality())
3473 return nullptr;
3474
3475 ICmpInst::Predicate Pred = Cmp.getPredicate();
3476 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3477 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
3478 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
3479
3480 switch (BO->getOpcode()) {
3481 case Instruction::SRem:
3482 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3483 if (C.isZero() && BO->hasOneUse()) {
3484 const APInt *BOC;
3485 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
3486 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());
3487 return new ICmpInst(Pred, NewRem,
3489 }
3490 }
3491 break;
3492 case Instruction::Add: {
3493 // (A + C2) == C --> A == (C - C2)
3494 // (A + C2) != C --> A != (C - C2)
3495 // TODO: Remove the one-use limitation? See discussion in D58633.
3496 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {
3497 if (BO->hasOneUse())
3498 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));
3499 } else if (C.isZero()) {
3500 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3501 // efficiently invertible, or if the add has just this one use.
3502 if (Value *NegVal = dyn_castNegVal(BOp1))
3503 return new ICmpInst(Pred, BOp0, NegVal);
3504 if (Value *NegVal = dyn_castNegVal(BOp0))
3505 return new ICmpInst(Pred, NegVal, BOp1);
3506 if (BO->hasOneUse()) {
3507 // (add nuw A, B) != 0 -> (or A, B) != 0
3508 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
3509 Value *Or = Builder.CreateOr(BOp0, BOp1);
3510 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
3511 }
3512 Value *Neg = Builder.CreateNeg(BOp1);
3513 Neg->takeName(BO);
3514 return new ICmpInst(Pred, BOp0, Neg);
3515 }
3516 }
3517 break;
3518 }
3519 case Instruction::Xor:
3520 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
3521 // For the xor case, we can xor two constants together, eliminating
3522 // the explicit xor.
3523 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
3524 } else if (C.isZero()) {
3525 // Replace ((xor A, B) != 0) with (A != B)
3526 return new ICmpInst(Pred, BOp0, BOp1);
3527 }
3528 break;
3529 case Instruction::Or: {
3530 const APInt *BOC;
3531 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3532 // Comparing if all bits outside of a constant mask are set?
3533 // Replace (X | C) == -1 with (X & ~C) == ~C.
3534 // This removes the -1 constant.
3535 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));
3536 Value *And = Builder.CreateAnd(BOp0, NotBOC);
3537 return new ICmpInst(Pred, And, NotBOC);
3538 }
3539 // (icmp eq (or (select cond, 0, NonZero), Other), 0)
3540 // -> (and cond, (icmp eq Other, 0))
3541 // (icmp ne (or (select cond, NonZero, 0), Other), 0)
3542 // -> (or cond, (icmp ne Other, 0))
3543 Value *Cond, *TV, *FV, *Other, *Sel;
3544 if (C.isZero() &&
3545 match(BO,
3548 m_Value(FV))),
3549 m_Value(Other))))) {
3550 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3551 // Easy case is if eq/ne matches whether 0 is trueval/falseval.
3552 if (Pred == ICmpInst::ICMP_EQ
3553 ? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))
3554 : (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {
3555 Value *Cmp = Builder.CreateICmp(
3556 Pred, Other, Constant::getNullValue(Other->getType()));
3558 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3559 Cond);
3560 }
3561 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3562 // case we need to invert the select condition so we need to be careful to
3563 // avoid creating extra instructions.
3564 // (icmp ne (or (select cond, 0, NonZero), Other), 0)
3565 // -> (or (not cond), (icmp ne Other, 0))
3566 // (icmp eq (or (select cond, NonZero, 0), Other), 0)
3567 // -> (and (not cond), (icmp eq Other, 0))
3568 //
3569 // Only do this if the inner select has one use, in which case we are
3570 // replacing `select` with `(not cond)`. Otherwise, we will create more
3571 // uses. NB: Trying to freely invert cond doesn't make sense here, as if
3572 // cond was freely invertable, the select arms would have been inverted.
3573 if (Sel->hasOneUse() &&
3574 (Pred == ICmpInst::ICMP_EQ
3575 ? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))
3576 : (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {
3577 Value *NotCond = Builder.CreateNot(Cond);
3578 Value *Cmp = Builder.CreateICmp(
3579 Pred, Other, Constant::getNullValue(Other->getType()));
3581 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3582 NotCond);
3583 }
3584 }
3585 break;
3586 }
3587 case Instruction::UDiv:
3588 case Instruction::SDiv:
3589 if (BO->isExact()) {
3590 // div exact X, Y eq/ne 0 -> X eq/ne 0
3591 // div exact X, Y eq/ne 1 -> X eq/ne Y
3592 // div exact X, Y eq/ne C ->
3593 // if Y * C never-overflow && OneUse:
3594 // -> Y * C eq/ne X
3595 if (C.isZero())
3596 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));
3597 else if (C.isOne())
3598 return new ICmpInst(Pred, BOp0, BOp1);
3599 else if (BO->hasOneUse()) {
3601 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,
3602 Cmp.getOperand(1), BO);
3604 Value *YC =
3605 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));
3606 return new ICmpInst(Pred, YC, BOp0);
3607 }
3608 }
3609 }
3610 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3611 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3612 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3613 return new ICmpInst(NewPred, BOp1, BOp0);
3614 }
3615 break;
3616 default:
3617 break;
3618 }
3619 return nullptr;
3620}
3621
3623 const APInt &CRhs,
3624 InstCombiner::BuilderTy &Builder,
3625 const SimplifyQuery &Q) {
3626 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3627 "Non-ctpop intrin in ctpop fold");
3628 if (!CtpopLhs->hasOneUse())
3629 return nullptr;
3630
3631 // Power of 2 test:
3632 // isPow2OrZero : ctpop(X) u< 2
3633 // isPow2 : ctpop(X) == 1
3634 // NotPow2OrZero: ctpop(X) u> 1
3635 // NotPow2 : ctpop(X) != 1
3636 // If we know any bit of X can be folded to:
3637 // IsPow2 : X & (~Bit) == 0
3638 // NotPow2 : X & (~Bit) != 0
3639 const ICmpInst::Predicate Pred = I.getPredicate();
3640 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3641 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3642 Value *Op = CtpopLhs->getArgOperand(0);
3643 KnownBits OpKnown = computeKnownBits(Op, Q.DL,
3644 /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT);
3645 // No need to check for count > 1, that should be already constant folded.
3646 if (OpKnown.countMinPopulation() == 1) {
3647 Value *And = Builder.CreateAnd(
3648 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));
3649 return new ICmpInst(
3650 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3653 And, Constant::getNullValue(Op->getType()));
3654 }
3655 }
3656
3657 return nullptr;
3658}
3659
3660/// Fold an equality icmp with LLVM intrinsic and constant operand.
3662 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3663 Type *Ty = II->getType();
3664 unsigned BitWidth = C.getBitWidth();
3665 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3666
3667 switch (II->getIntrinsicID()) {
3668 case Intrinsic::abs:
3669 // abs(A) == 0 -> A == 0
3670 // abs(A) == INT_MIN -> A == INT_MIN
3671 if (C.isZero() || C.isMinSignedValue())
3672 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));
3673 break;
3674
3675 case Intrinsic::bswap:
3676 // bswap(A) == C -> A == bswap(C)
3677 return new ICmpInst(Pred, II->getArgOperand(0),
3678 ConstantInt::get(Ty, C.byteSwap()));
3679
3680 case Intrinsic::bitreverse:
3681 // bitreverse(A) == C -> A == bitreverse(C)
3682 return new ICmpInst(Pred, II->getArgOperand(0),
3683 ConstantInt::get(Ty, C.reverseBits()));
3684
3685 case Intrinsic::ctlz:
3686 case Intrinsic::cttz: {
3687 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3688 if (C == BitWidth)
3689 return new ICmpInst(Pred, II->getArgOperand(0),
3691
3692 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3693 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3694 // Limit to one use to ensure we don't increase instruction count.
3695 unsigned Num = C.getLimitedValue(BitWidth);
3696 if (Num != BitWidth && II->hasOneUse()) {
3697 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3698 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)
3699 : APInt::getHighBitsSet(BitWidth, Num + 1);
3700 APInt Mask2 = IsTrailing
3703 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),
3704 ConstantInt::get(Ty, Mask2));
3705 }
3706 break;
3707 }
3708
3709 case Intrinsic::ctpop: {
3710 // popcount(A) == 0 -> A == 0 and likewise for !=
3711 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3712 bool IsZero = C.isZero();
3713 if (IsZero || C == BitWidth)
3714 return new ICmpInst(Pred, II->getArgOperand(0),
3715 IsZero ? Constant::getNullValue(Ty)
3717
3718 break;
3719 }
3720
3721 case Intrinsic::fshl:
3722 case Intrinsic::fshr:
3723 if (II->getArgOperand(0) == II->getArgOperand(1)) {
3724 const APInt *RotAmtC;
3725 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3726 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3727 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))
3728 return new ICmpInst(Pred, II->getArgOperand(0),
3729 II->getIntrinsicID() == Intrinsic::fshl
3730 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))
3731 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));
3732 }
3733 break;
3734
3735 case Intrinsic::umax:
3736 case Intrinsic::uadd_sat: {
3737 // uadd.sat(a, b) == 0 -> (a | b) == 0
3738 // umax(a, b) == 0 -> (a | b) == 0
3739 if (C.isZero() && II->hasOneUse()) {
3740 Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1));
3741 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3742 }
3743 break;
3744 }
3745
3746 case Intrinsic::ssub_sat:
3747 // ssub.sat(a, b) == 0 -> a == b
3748 if (C.isZero())
3749 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3750 break;
3751 case Intrinsic::usub_sat: {
3752 // usub.sat(a, b) == 0 -> a <= b
3753 if (C.isZero()) {
3754 ICmpInst::Predicate NewPred =
3756 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));
3757 }
3758 break;
3759 }
3760 default:
3761 break;
3762 }
3763
3764 return nullptr;
3765}
3766
3767/// Fold an icmp with LLVM intrinsics
3768static Instruction *
3770 InstCombiner::BuilderTy &Builder) {
3771 assert(Cmp.isEquality());
3772
3773 ICmpInst::Predicate Pred = Cmp.getPredicate();
3774 Value *Op0 = Cmp.getOperand(0);
3775 Value *Op1 = Cmp.getOperand(1);
3776 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);
3777 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);
3778 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3779 return nullptr;
3780
3781 switch (IIOp0->getIntrinsicID()) {
3782 case Intrinsic::bswap:
3783 case Intrinsic::bitreverse:
3784 // If both operands are byte-swapped or bit-reversed, just compare the
3785 // original values.
3786 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3787 case Intrinsic::fshl:
3788 case Intrinsic::fshr: {
3789 // If both operands are rotated by same amount, just compare the
3790 // original values.
3791 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))
3792 break;
3793 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))
3794 break;
3795 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))
3796 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3797
3798 // rotate(X, AmtX) == rotate(Y, AmtY)
3799 // -> rotate(X, AmtX - AmtY) == Y
3800 // Do this if either both rotates have one use or if only one has one use
3801 // and AmtX/AmtY are constants.
3802 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3803 if (OneUses == 2 ||
3804 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&
3805 match(IIOp1->getOperand(2), m_ImmConstant()))) {
3806 Value *SubAmt =
3807 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));
3808 Value *CombinedRotate = Builder.CreateIntrinsic(
3809 Op0->getType(), IIOp0->getIntrinsicID(),
3810 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});
3811 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);
3812 }
3813 } break;
3814 default:
3815 break;
3816 }
3817
3818 return nullptr;
3819}
3820
3821/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3822/// where X is some kind of instruction and C is AllowPoison.
3823/// TODO: Move more folds which allow poison to this function.
3826 const APInt &C) {
3827 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3828 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {
3829 switch (II->getIntrinsicID()) {
3830 default:
3831 break;
3832 case Intrinsic::fshl:
3833 case Intrinsic::fshr:
3834 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {
3835 // (rot X, ?) == 0/-1 --> X == 0/-1
3836 if (C.isZero() || C.isAllOnes())
3837 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
3838 }
3839 break;
3840 }
3841 }
3842
3843 return nullptr;
3844}
3845
3846/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
3848 BinaryOperator *BO,
3849 const APInt &C) {
3850 switch (BO->getOpcode()) {
3851 case Instruction::Xor:
3852 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
3853 return I;
3854 break;
3855 case Instruction::And:
3856 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
3857 return I;
3858 break;
3859 case Instruction::Or:
3860 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
3861 return I;
3862 break;
3863 case Instruction::Mul:
3864 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
3865 return I;
3866 break;
3867 case Instruction::Shl:
3868 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
3869 return I;
3870 break;
3871 case Instruction::LShr:
3872 case Instruction::AShr:
3873 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
3874 return I;
3875 break;
3876 case Instruction::SRem:
3877 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
3878 return I;
3879 break;
3880 case Instruction::UDiv:
3881 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
3882 return I;
3883 [[fallthrough]];
3884 case Instruction::SDiv:
3885 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
3886 return I;
3887 break;
3888 case Instruction::Sub:
3889 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
3890 return I;
3891 break;
3892 case Instruction::Add:
3893 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
3894 return I;
3895 break;
3896 default:
3897 break;
3898 }
3899
3900 // TODO: These folds could be refactored to be part of the above calls.
3901 return foldICmpBinOpEqualityWithConstant(Cmp, BO, C);
3902}
3903
3904static Instruction *
3906 SaturatingInst *II, const APInt &C,
3907 InstCombiner::BuilderTy &Builder) {
3908 // This transform may end up producing more than one instruction for the
3909 // intrinsic, so limit it to one user of the intrinsic.
3910 if (!II->hasOneUse())
3911 return nullptr;
3912
3913 // Let Y = [add/sub]_sat(X, C) pred C2
3914 // SatVal = The saturating value for the operation
3915 // WillWrap = Whether or not the operation will underflow / overflow
3916 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
3917 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
3918 //
3919 // When (SatVal pred C2) is true, then
3920 // Y = WillWrap ? true : ((X binop C) pred C2)
3921 // => Y = WillWrap || ((X binop C) pred C2)
3922 // else
3923 // Y = WillWrap ? false : ((X binop C) pred C2)
3924 // => Y = !WillWrap ? ((X binop C) pred C2) : false
3925 // => Y = !WillWrap && ((X binop C) pred C2)
3926 Value *Op0 = II->getOperand(0);
3927 Value *Op1 = II->getOperand(1);
3928
3929 const APInt *COp1;
3930 // This transform only works when the intrinsic has an integral constant or
3931 // splat vector as the second operand.
3932 if (!match(Op1, m_APInt(COp1)))
3933 return nullptr;
3934
3935 APInt SatVal;
3936 switch (II->getIntrinsicID()) {
3937 default:
3939 "This function only works with usub_sat and uadd_sat for now!");
3940 case Intrinsic::uadd_sat:
3941 SatVal = APInt::getAllOnes(C.getBitWidth());
3942 break;
3943 case Intrinsic::usub_sat:
3944 SatVal = APInt::getZero(C.getBitWidth());
3945 break;
3946 }
3947
3948 // Check (SatVal pred C2)
3949 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);
3950
3951 // !WillWrap.
3953 II->getBinaryOp(), *COp1, II->getNoWrapKind());
3954
3955 // WillWrap.
3956 if (SatValCheck)
3957 C1 = C1.inverse();
3958
3960 if (II->getBinaryOp() == Instruction::Add)
3961 C2 = C2.sub(*COp1);
3962 else
3963 C2 = C2.add(*COp1);
3964
3965 Instruction::BinaryOps CombiningOp =
3966 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
3967
3968 std::optional<ConstantRange> Combination;
3969 if (CombiningOp == Instruction::BinaryOps::Or)
3970 Combination = C1.exactUnionWith(C2);
3971 else /* CombiningOp == Instruction::BinaryOps::And */
3972 Combination = C1.exactIntersectWith(C2);
3973
3974 if (!Combination)
3975 return nullptr;
3976
3977 CmpInst::Predicate EquivPred;
3978 APInt EquivInt;
3979 APInt EquivOffset;
3980
3981 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
3982
3983 return new ICmpInst(
3984 EquivPred,
3985 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),
3986 ConstantInt::get(Op1->getType(), EquivInt));
3987}
3988
3989static Instruction *
3991 const APInt &C,
3992 InstCombiner::BuilderTy &Builder) {
3993 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
3994 switch (Pred) {
3995 case ICmpInst::ICMP_EQ:
3996 case ICmpInst::ICMP_NE:
3997 if (C.isZero())
3998 NewPredicate = Pred;
3999 else if (C.isOne())
4000 NewPredicate =
4002 else if (C.isAllOnes())
4003 NewPredicate =
4005 break;
4006
4007 case ICmpInst::ICMP_SGT:
4008 if (C.isAllOnes())
4009 NewPredicate = ICmpInst::ICMP_UGE;
4010 else if (C.isZero())
4011 NewPredicate = ICmpInst::ICMP_UGT;
4012 break;
4013
4014 case ICmpInst::ICMP_SLT:
4015 if (C.isZero())
4016 NewPredicate = ICmpInst::ICMP_ULT;
4017 else if (C.isOne())
4018 NewPredicate = ICmpInst::ICMP_ULE;
4019 break;
4020
4021 case ICmpInst::ICMP_ULT:
4022 if (C.ugt(1))
4023 NewPredicate = ICmpInst::ICMP_UGE;
4024 break;
4025
4026 case ICmpInst::ICMP_UGT:
4027 if (!C.isZero() && !C.isAllOnes())
4028 NewPredicate = ICmpInst::ICMP_ULT;
4029 break;
4030
4031 default:
4032 break;
4033 }
4034
4035 if (!NewPredicate)
4036 return nullptr;
4037
4038 if (I->getIntrinsicID() == Intrinsic::scmp)
4039 NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate);
4040 Value *LHS = I->getOperand(0);
4041 Value *RHS = I->getOperand(1);
4042 return new ICmpInst(*NewPredicate, LHS, RHS);
4043}
4044
4045/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
4048 const APInt &C) {
4049 ICmpInst::Predicate Pred = Cmp.getPredicate();
4050
4051 // Handle folds that apply for any kind of icmp.
4052 switch (II->getIntrinsicID()) {
4053 default:
4054 break;
4055 case Intrinsic::uadd_sat:
4056 case Intrinsic::usub_sat:
4057 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
4058 Pred, cast<SaturatingInst>(II), C, Builder))
4059 return Folded;
4060 break;
4061 case Intrinsic::ctpop: {
4062 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
4063 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))
4064 return R;
4065 } break;
4066 case Intrinsic::scmp:
4067 case Intrinsic::ucmp:
4068 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, II, C, Builder))
4069 return Folded;
4070 break;
4071 }
4072
4073 if (Cmp.isEquality())
4074 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
4075
4076 Type *Ty = II->getType();
4077 unsigned BitWidth = C.getBitWidth();
4078 switch (II->getIntrinsicID()) {
4079 case Intrinsic::ctpop: {
4080 // (ctpop X > BitWidth - 1) --> X == -1
4081 Value *X = II->getArgOperand(0);
4082 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
4083 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,
4085 // (ctpop X < BitWidth) --> X != -1
4086 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
4087 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,
4089 break;
4090 }
4091 case Intrinsic::ctlz: {
4092 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
4093 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4094 unsigned Num = C.getLimitedValue();
4095 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);
4096 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,
4097 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4098 }
4099
4100 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
4101 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4102 unsigned Num = C.getLimitedValue();
4104 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,
4105 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4106 }
4107 break;
4108 }
4109 case Intrinsic::cttz: {
4110 // Limit to one use to ensure we don't increase instruction count.
4111 if (!II->hasOneUse())
4112 return nullptr;
4113
4114 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
4115 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4116 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);
4117 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,
4118 Builder.CreateAnd(II->getArgOperand(0), Mask),
4120 }
4121
4122 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
4123 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4124 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());
4125 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,
4126 Builder.CreateAnd(II->getArgOperand(0), Mask),
4128 }
4129 break;
4130 }
4131 case Intrinsic::ssub_sat:
4132 // ssub.sat(a, b) spred 0 -> a spred b
4133 if (ICmpInst::isSigned(Pred)) {
4134 if (C.isZero())
4135 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
4136 // X s<= 0 is cannonicalized to X s< 1
4137 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
4138 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),
4139 II->getArgOperand(1));
4140 // X s>= 0 is cannonicalized to X s> -1
4141 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
4142 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),
4143 II->getArgOperand(1));
4144 }
4145 break;
4146 default:
4147 break;
4148 }
4149
4150 return nullptr;
4151}
4152
4153/// Handle icmp with constant (but not simple integer constant) RHS.
4155 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4156 Constant *RHSC = dyn_cast<Constant>(Op1);
4157 Instruction *LHSI = dyn_cast<Instruction>(Op0);
4158 if (!RHSC || !LHSI)
4159 return nullptr;
4160
4161 switch (LHSI->getOpcode()) {
4162 case Instruction::PHI:
4163 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
4164 return NV;
4165 break;
4166 case Instruction::IntToPtr:
4167 // icmp pred inttoptr(X), null -> icmp pred X, 0
4168 if (RHSC->isNullValue() &&
4169 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
4170 return new ICmpInst(
4171 I.getPredicate(), LHSI->getOperand(0),
4173 break;
4174
4175 case Instruction::Load:
4176 // Try to optimize things like "A[i] > 4" to index computations.
4177 if (GetElementPtrInst *GEP =
4178 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
4179 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
4180 if (Instruction *Res =
4181 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, GV, I))
4182 return Res;
4183 break;
4184 }
4185
4186 return nullptr;
4187}
4188
4190 SelectInst *SI, Value *RHS,
4191 const ICmpInst &I) {
4192 // Try to fold the comparison into the select arms, which will cause the
4193 // select to be converted into a logical and/or.
4194 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4195 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))
4196 return Res;
4197 if (std::optional<bool> Impl = isImpliedCondition(
4198 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))
4199 return ConstantInt::get(I.getType(), *Impl);
4200 return nullptr;
4201 };
4202
4203 ConstantInt *CI = nullptr;
4204 Value *Op1 = SimplifyOp(SI->getOperand(1), true);
4205 if (Op1)
4206 CI = dyn_cast<ConstantInt>(Op1);
4207
4208 Value *Op2 = SimplifyOp(SI->getOperand(2), false);
4209 if (Op2)
4210 CI = dyn_cast<ConstantInt>(Op2);
4211
4212 // We only want to perform this transformation if it will not lead to
4213 // additional code. This is true if either both sides of the select
4214 // fold to a constant (in which case the icmp is replaced with a select
4215 // which will usually simplify) or this is the only user of the
4216 // select (in which case we are trading a select+icmp for a simpler
4217 // select+icmp) or all uses of the select can be replaced based on
4218 // dominance information ("Global cases").
4219 bool Transform = false;
4220 if (Op1 && Op2)
4221 Transform = true;
4222 else if (Op1 || Op2) {
4223 // Local case
4224 if (SI->hasOneUse())
4225 Transform = true;
4226 // Global cases
4227 else if (CI && !CI->isZero())
4228 // When Op1 is constant try replacing select with second operand.
4229 // Otherwise Op2 is constant and try replacing select with first
4230 // operand.
4231 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4232 }
4233 if (Transform) {
4234 if (!Op1)
4235 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4236 if (!Op2)
4237 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4238 return SelectInst::Create(SI->getOperand(0), Op1, Op2);
4239 }
4240
4241 return nullptr;
4242}
4243
4244// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4245static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4246 unsigned Depth = 0) {
4247 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4248 return true;
4249 if (V->getType()->getScalarSizeInBits() == 1)
4250 return true;
4252 return false;
4253 Value *X;
4254 const Instruction *I = dyn_cast<Instruction>(V);
4255 if (!I)
4256 return false;
4257 switch (I->getOpcode()) {
4258 case Instruction::ZExt:
4259 // ZExt(Mask) is a Mask.
4260 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4261 case Instruction::SExt:
4262 // SExt(Mask) is a Mask.
4263 // SExt(~Mask) is a ~Mask.
4264 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4265 case Instruction::And:
4266 case Instruction::Or:
4267 // Mask0 | Mask1 is a Mask.
4268 // Mask0 & Mask1 is a Mask.
4269 // ~Mask0 | ~Mask1 is a ~Mask.
4270 // ~Mask0 & ~Mask1 is a ~Mask.
4271 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4272 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4273 case Instruction::Xor:
4274 if (match(V, m_Not(m_Value(X))))
4275 return isMaskOrZero(X, !Not, Q, Depth);
4276
4277 // (X ^ -X) is a ~Mask
4278 if (Not)
4279 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4280 // (X ^ (X - 1)) is a Mask
4281 else
4282 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4283 case Instruction::Select:
4284 // c ? Mask0 : Mask1 is a Mask.
4285 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4286 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4287 case Instruction::Shl:
4288 // (~Mask) << X is a ~Mask.
4289 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4290 case Instruction::LShr:
4291 // Mask >> X is a Mask.
4292 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4293 case Instruction::AShr:
4294 // Mask s>> X is a Mask.
4295 // ~Mask s>> X is a ~Mask.
4296 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4297 case Instruction::Add:
4298 // Pow2 - 1 is a Mask.
4299 if (!Not && match(I->getOperand(1), m_AllOnes()))
4300 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4301 Depth, Q.AC, Q.CxtI, Q.DT);
4302 break;
4303 case Instruction::Sub:
4304 // -Pow2 is a ~Mask.
4305 if (Not && match(I->getOperand(0), m_Zero()))
4306 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4307 Depth, Q.AC, Q.CxtI, Q.DT);
4308 break;
4309 case Instruction::Call: {
4310 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4311 switch (II->getIntrinsicID()) {
4312 // min/max(Mask0, Mask1) is a Mask.
4313 // min/max(~Mask0, ~Mask1) is a ~Mask.
4314 case Intrinsic::umax:
4315 case Intrinsic::smax:
4316 case Intrinsic::umin:
4317 case Intrinsic::smin:
4318 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4319 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4320
4321 // In the context of masks, bitreverse(Mask) == ~Mask
4322 case Intrinsic::bitreverse:
4323 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4324 default:
4325 break;
4326 }
4327 }
4328 break;
4329 }
4330 default:
4331 break;
4332 }
4333 return false;
4334}
4335
4336/// Some comparisons can be simplified.
4337/// In this case, we are looking for comparisons that look like
4338/// a check for a lossy truncation.
4339/// Folds:
4340/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4341/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4342/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4343/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4344/// Where Mask is some pattern that produces all-ones in low bits:
4345/// (-1 >> y)
4346/// ((-1 << y) >> y) <- non-canonical, has extra uses
4347/// ~(-1 << y)
4348/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4349/// The Mask can be a constant, too.
4350/// For some predicates, the operands are commutative.
4351/// For others, x can only be on a specific side.
4353 Value *Op1, const SimplifyQuery &Q,
4354 InstCombiner &IC) {
4355
4356 ICmpInst::Predicate DstPred;
4357 switch (Pred) {
4359 // x & Mask == x
4360 // x & ~Mask == 0
4361 // ~x | Mask == -1
4362 // -> x u<= Mask
4363 // x & ~Mask == ~Mask
4364 // -> ~Mask u<= x
4366 break;
4368 // x & Mask != x
4369 // x & ~Mask != 0
4370 // ~x | Mask != -1
4371 // -> x u> Mask
4372 // x & ~Mask != ~Mask
4373 // -> ~Mask u> x
4375 break;
4377 // x & Mask u< x
4378 // -> x u> Mask
4379 // x & ~Mask u< ~Mask
4380 // -> ~Mask u> x
4382 break;
4384 // x & Mask u>= x
4385 // -> x u<= Mask
4386 // x & ~Mask u>= ~Mask
4387 // -> ~Mask u<= x
4389 break;
4391 // x & Mask s< x [iff Mask s>= 0]
4392 // -> x s> Mask
4393 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4394 // -> ~Mask s> x
4396 break;
4398 // x & Mask s>= x [iff Mask s>= 0]
4399 // -> x s<= Mask
4400 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4401 // -> ~Mask s<= x
4403 break;
4404 default:
4405 // We don't support sgt,sle
4406 // ult/ugt are simplified to true/false respectively.
4407 return nullptr;
4408 }
4409
4410 Value *X, *M;
4411 // Put search code in lambda for early positive returns.
4412 auto IsLowBitMask = [&]() {
4413 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4414 X = Op1;
4415 // Look for: x & Mask pred x
4416 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4417 return !ICmpInst::isSigned(Pred) ||
4418 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4419 }
4420
4421 // Look for: x & ~Mask pred ~Mask
4422 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4423 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4424 }
4425 return false;
4426 }
4427 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4428 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4429
4430 auto Check = [&]() {
4431 // Look for: ~x | Mask == -1
4432 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4433 if (Value *NotX =
4434 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4435 X = NotX;
4436 return true;
4437 }
4438 }
4439 return false;
4440 };
4441 if (Check())
4442 return true;
4443 std::swap(X, M);
4444 return Check();
4445 }
4446 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4447 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4448 auto Check = [&]() {
4449 // Look for: x & ~Mask == 0
4450 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4451 if (Value *NotM =
4452 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4453 M = NotM;
4454 return true;
4455 }
4456 }
4457 return false;
4458 };
4459 if (Check())
4460 return true;
4461 std::swap(X, M);
4462 return Check();
4463 }
4464 return false;
4465 };
4466
4467 if (!IsLowBitMask())
4468 return nullptr;
4469
4470 return IC.Builder.CreateICmp(DstPred, X, M);
4471}
4472
4473/// Some comparisons can be simplified.
4474/// In this case, we are looking for comparisons that look like
4475/// a check for a lossy signed truncation.
4476/// Folds: (MaskedBits is a constant.)
4477/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4478/// Into:
4479/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4480/// Where KeptBits = bitwidth(%x) - MaskedBits
4481static Value *
4483 InstCombiner::BuilderTy &Builder) {
4484 ICmpInst::Predicate SrcPred;
4485 Value *X;
4486 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4487 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4488 if (!match(&I, m_c_ICmp(SrcPred,
4490 m_APInt(C1))),
4491 m_Deferred(X))))
4492 return nullptr;
4493
4494 // Potential handling of non-splats: for each element:
4495 // * if both are undef, replace with constant 0.
4496 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4497 // * if both are not undef, and are different, bailout.
4498 // * else, only one is undef, then pick the non-undef one.
4499
4500 // The shift amount must be equal.
4501 if (*C0 != *C1)
4502 return nullptr;
4503 const APInt &MaskedBits = *C0;
4504 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4505
4506 ICmpInst::Predicate DstPred;
4507 switch (SrcPred) {
4509 // ((%x << MaskedBits) a>> MaskedBits) == %x
4510 // =>
4511 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4513 break;
4515 // ((%x << MaskedBits) a>> MaskedBits) != %x
4516 // =>
4517 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4519 break;
4520 // FIXME: are more folds possible?
4521 default:
4522 return nullptr;
4523 }
4524
4525 auto *XType = X->getType();
4526 const unsigned XBitWidth = XType->getScalarSizeInBits();
4527 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4528 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4529
4530 // KeptBits = bitwidth(%x) - MaskedBits
4531 const APInt KeptBits = BitWidth - MaskedBits;
4532 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4533 // ICmpCst = (1 << KeptBits)
4534 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4535 assert(ICmpCst.isPowerOf2());
4536 // AddCst = (1 << (KeptBits-1))
4537 const APInt AddCst = ICmpCst.lshr(1);
4538 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4539
4540 // T0 = add %x, AddCst
4541 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4542 // T1 = T0 DstPred ICmpCst
4543 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4544
4545 return T1;
4546}
4547
4548// Given pattern:
4549// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4550// we should move shifts to the same hand of 'and', i.e. rewrite as
4551// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4552// We are only interested in opposite logical shifts here.
4553// One of the shifts can be truncated.
4554// If we can, we want to end up creating 'lshr' shift.
4555static Value *
4557 InstCombiner::BuilderTy &Builder) {
4558 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4559 !I.getOperand(0)->hasOneUse())
4560 return nullptr;
4561
4562 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4563
4564 // Look for an 'and' of two logical shifts, one of which may be truncated.
4565 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4566 Instruction *XShift, *MaybeTruncation, *YShift;
4567 if (!match(
4568 I.getOperand(0),
4569 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4571 m_AnyLogicalShift, m_Instruction(YShift))),
4572 m_Instruction(MaybeTruncation)))))
4573 return nullptr;
4574
4575 // We potentially looked past 'trunc', but only when matching YShift,
4576 // therefore YShift must have the widest type.
4577 Instruction *WidestShift = YShift;
4578 // Therefore XShift must have the shallowest type.
4579 // Or they both have identical types if there was no truncation.
4580 Instruction *NarrowestShift = XShift;
4581
4582 Type *WidestTy = WidestShift->getType();
4583 Type *NarrowestTy = NarrowestShift->getType();
4584 assert(NarrowestTy == I.getOperand(0)->getType() &&
4585 "We did not look past any shifts while matching XShift though.");
4586 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4587
4588 // If YShift is a 'lshr', swap the shifts around.
4589 if (match(YShift, m_LShr(m_Value(), m_Value())))
4590 std::swap(XShift, YShift);
4591
4592 // The shifts must be in opposite directions.
4593 auto XShiftOpcode = XShift->getOpcode();
4594 if (XShiftOpcode == YShift->getOpcode())
4595 return nullptr; // Do not care about same-direction shifts here.
4596
4597 Value *X, *XShAmt, *Y, *YShAmt;
4598 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4599 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4600
4601 // If one of the values being shifted is a constant, then we will end with
4602 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4603 // however, we will need to ensure that we won't increase instruction count.
4604 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4605 // At least one of the hands of the 'and' should be one-use shift.
4606 if (!match(I.getOperand(0),
4607 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4608 return nullptr;
4609 if (HadTrunc) {
4610 // Due to the 'trunc', we will need to widen X. For that either the old
4611 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4612 if (!MaybeTruncation->hasOneUse() &&
4613 !NarrowestShift->getOperand(1)->hasOneUse())
4614 return nullptr;
4615 }
4616 }
4617
4618 // We have two shift amounts from two different shifts. The types of those
4619 // shift amounts may not match. If that's the case let's bailout now.
4620 if (XShAmt->getType() != YShAmt->getType())
4621 return nullptr;
4622
4623 // As input, we have the following pattern:
4624 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4625 // We want to rewrite that as:
4626 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4627 // While we know that originally (Q+K) would not overflow
4628 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4629 // shift amounts. so it may now overflow in smaller bitwidth.
4630 // To ensure that does not happen, we need to ensure that the total maximal
4631 // shift amount is still representable in that smaller bit width.
4632 unsigned MaximalPossibleTotalShiftAmount =
4633 (WidestTy->getScalarSizeInBits() - 1) +
4634 (NarrowestTy->getScalarSizeInBits() - 1);
4635 APInt MaximalRepresentableShiftAmount =
4637 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4638 return nullptr;
4639
4640 // Can we fold (XShAmt+YShAmt) ?
4641 auto *NewShAmt = dyn_cast_or_null<Constant>(
4642 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4643 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4644 if (!NewShAmt)
4645 return nullptr;
4646 if (NewShAmt->getType() != WidestTy) {
4647 NewShAmt =
4648 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4649 if (!NewShAmt)
4650 return nullptr;
4651 }
4652 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4653
4654 // Is the new shift amount smaller than the bit width?
4655 // FIXME: could also rely on ConstantRange.
4656 if (!match(NewShAmt,
4658 APInt(WidestBitWidth, WidestBitWidth))))
4659 return nullptr;
4660
4661 // An extra legality check is needed if we had trunc-of-lshr.
4662 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4663 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4664 WidestShift]() {
4665 // It isn't obvious whether it's worth it to analyze non-constants here.
4666 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4667 // If *any* of these preconditions matches we can perform the fold.
4668 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4669 ? NewShAmt->getSplatValue()
4670 : NewShAmt;
4671 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4672 if (NewShAmtSplat &&
4673 (NewShAmtSplat->isNullValue() ||
4674 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4675 return true;
4676 // We consider *min* leading zeros so a single outlier
4677 // blocks the transform as opposed to allowing it.
4678 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4679 KnownBits Known = computeKnownBits(C, SQ.DL);
4680 unsigned MinLeadZero = Known.countMinLeadingZeros();
4681 // If the value being shifted has at most lowest bit set we can fold.
4682 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4683 if (MaxActiveBits <= 1)
4684 return true;
4685 // Precondition: NewShAmt u<= countLeadingZeros(C)
4686 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4687 return true;
4688 }
4689 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4690 KnownBits Known = computeKnownBits(C, SQ.DL);
4691 unsigned MinLeadZero = Known.countMinLeadingZeros();
4692 // If the value being shifted has at most lowest bit set we can fold.
4693 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4694 if (MaxActiveBits <= 1)
4695 return true;
4696 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4697 if (NewShAmtSplat) {
4698 APInt AdjNewShAmt =
4699 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4700 if (AdjNewShAmt.ule(MinLeadZero))
4701 return true;
4702 }
4703 }
4704 return false; // Can't tell if it's ok.
4705 };
4706 if (!CanFold())
4707 return nullptr;
4708 }
4709
4710 // All good, we can do this fold.
4711 X = Builder.CreateZExt(X, WidestTy);
4712 Y = Builder.CreateZExt(Y, WidestTy);
4713 // The shift is the same that was for X.
4714 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4715 ? Builder.CreateLShr(X, NewShAmt)
4716 : Builder.CreateShl(X, NewShAmt);
4717 Value *T1 = Builder.CreateAnd(T0, Y);
4718 return Builder.CreateICmp(I.getPredicate(), T1,
4719 Constant::getNullValue(WidestTy));
4720}
4721
4722/// Fold
4723/// (-1 u/ x) u< y
4724/// ((x * y) ?/ x) != y
4725/// to
4726/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4727/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4728/// will mean that we are looking for the opposite answer.
4731 Value *X, *Y;
4733 Instruction *Div;
4734 bool NeedNegation;
4735 // Look for: (-1 u/ x) u</u>= y
4736 if (!I.isEquality() &&
4737 match(&I, m_c_ICmp(Pred,
4739 m_Instruction(Div)),
4740 m_Value(Y)))) {
4741 Mul = nullptr;
4742
4743 // Are we checking that overflow does not happen, or does happen?
4744 switch (Pred) {
4746 NeedNegation = false;
4747 break; // OK
4749 NeedNegation = true;
4750 break; // OK
4751 default:
4752 return nullptr; // Wrong predicate.
4753 }
4754 } else // Look for: ((x * y) / x) !=/== y
4755 if (I.isEquality() &&
4756 match(&I,
4757 m_c_ICmp(Pred, m_Value(Y),
4760 m_Value(X)),
4762 m_Deferred(X))),
4763 m_Instruction(Div))))) {
4764 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4765 } else
4766 return nullptr;
4767
4769 // If the pattern included (x * y), we'll want to insert new instructions
4770 // right before that original multiplication so that we can replace it.
4771 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4772 if (MulHadOtherUses)
4774
4775 Function *F = Intrinsic::getDeclaration(I.getModule(),
4776 Div->getOpcode() == Instruction::UDiv
4777 ? Intrinsic::umul_with_overflow
4778 : Intrinsic::smul_with_overflow,
4779 X->getType());
4780 CallInst *Call = Builder.CreateCall(F, {X, Y}, "mul");
4781
4782 // If the multiplication was used elsewhere, to ensure that we don't leave
4783 // "duplicate" instructions, replace uses of that original multiplication
4784 // with the multiplication result from the with.overflow intrinsic.
4785 if (MulHadOtherUses)
4786 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4787
4788 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4789 if (NeedNegation) // This technically increases instruction count.
4790 Res = Builder.CreateNot(Res, "mul.not.ov");
4791
4792 // If we replaced the mul, erase it. Do this after all uses of Builder,
4793 // as the mul is used as insertion point.
4794 if (MulHadOtherUses)
4796
4797 return Res;
4798}
4799
4801 InstCombiner::BuilderTy &Builder) {
4802 CmpInst::Predicate Pred;
4803 Value *X;
4804 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4805
4806 if (ICmpInst::isSigned(Pred))
4807 Pred = ICmpInst::getSwappedPredicate(Pred);
4808 else if (ICmpInst::isUnsigned(Pred))
4809 Pred = ICmpInst::getSignedPredicate(Pred);
4810 // else for equality-comparisons just keep the predicate.
4811
4812 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4813 Constant::getNullValue(X->getType()), I.getName());
4814 }
4815
4816 // A value is not equal to its negation unless that value is 0 or
4817 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4818 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
4819 ICmpInst::isEquality(Pred)) {
4820 Type *Ty = X->getType();
4822 Constant *MaxSignedVal =
4823 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
4824 Value *And = Builder.CreateAnd(X, MaxSignedVal);
4825 Constant *Zero = Constant::getNullValue(Ty);
4826 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
4827 }
4828
4829 return nullptr;
4830}
4831
4833 InstCombinerImpl &IC) {
4834 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4835 // Normalize and operand as operand 0.
4836 CmpInst::Predicate Pred = I.getPredicate();
4837 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
4838 std::swap(Op0, Op1);
4839 Pred = ICmpInst::getSwappedPredicate(Pred);
4840 }
4841
4842 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
4843 return nullptr;
4844
4845 // (icmp (X & Y) u< X --> (X & Y) != X
4846 if (Pred == ICmpInst::ICMP_ULT)
4847 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4848
4849 // (icmp (X & Y) u>= X --> (X & Y) == X
4850 if (Pred == ICmpInst::ICMP_UGE)
4851 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4852
4853 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4854 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
4855 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
4856 // so don't do this fold.
4857 if (!match(Op1, m_ImmConstant()))
4858 if (auto *NotOp1 =
4859 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
4860 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),
4861 Constant::getAllOnesValue(Op1->getType()));
4862 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
4863 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4864 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),
4865 Constant::getNullValue(Op1->getType()));
4866 }
4867
4868 if (!ICmpInst::isSigned(Pred))
4869 return nullptr;
4870
4871 KnownBits KnownY = IC.computeKnownBits(A, /*Depth=*/0, &I);
4872 // (X & NegY) spred X --> (X & NegY) upred X
4873 if (KnownY.isNegative())
4874 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
4875
4876 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
4877 return nullptr;
4878
4879 if (KnownY.isNonNegative())
4880 // (X & PosY) s<= X --> X s>= 0
4881 // (X & PosY) s> X --> X s< 0
4882 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
4883 Constant::getNullValue(Op1->getType()));
4884
4886 // (NegX & Y) s<= NegX --> Y s< 0
4887 // (NegX & Y) s> NegX --> Y s>= 0
4889 Constant::getNullValue(A->getType()));
4890
4891 return nullptr;
4892}
4893
4895 InstCombinerImpl &IC) {
4896 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4897
4898 // Normalize or operand as operand 0.
4899 CmpInst::Predicate Pred = I.getPredicate();
4900 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
4901 std::swap(Op0, Op1);
4902 Pred = ICmpInst::getSwappedPredicate(Pred);
4903 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
4904 return nullptr;
4905 }
4906
4907 // icmp (X | Y) u<= X --> (X | Y) == X
4908 if (Pred == ICmpInst::ICMP_ULE)
4909 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4910
4911 // icmp (X | Y) u> X --> (X | Y) != X
4912 if (Pred == ICmpInst::ICMP_UGT)
4913 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4914
4915 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4916 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
4917 if (Value *NotOp1 =
4918 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
4919 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
4920 Constant::getNullValue(Op1->getType()));
4921 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
4922 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4923 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
4924 Constant::getAllOnesValue(Op1->getType()));
4925 }
4926 return nullptr;
4927}
4928
4930 InstCombinerImpl &IC) {
4931 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4932 // Normalize xor operand as operand 0.
4933 CmpInst::Predicate Pred = I.getPredicate();
4934 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
4935 std::swap(Op0, Op1);
4936 Pred = ICmpInst::getSwappedPredicate(Pred);
4937 }
4938 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
4939 return nullptr;
4940
4941 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
4942 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
4943 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
4944 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
4946 if (PredOut != Pred && isKnownNonZero(A, Q))
4947 return new ICmpInst(PredOut, Op0, Op1);
4948
4949 return nullptr;
4950}
4951
4952/// Try to fold icmp (binop), X or icmp X, (binop).
4953/// TODO: A large part of this logic is duplicated in InstSimplify's
4954/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
4955/// duplication.
4957 const SimplifyQuery &SQ) {
4959 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4960
4961 // Special logic for binary operators.
4962 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
4963 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
4964 if (!BO0 && !BO1)
4965 return nullptr;
4966
4967 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
4968 return NewICmp;
4969
4970 const CmpInst::Predicate Pred = I.getPredicate();
4971 Value *X;
4972
4973 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
4974 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
4975 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
4976 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
4977 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
4978 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
4979 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
4980 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
4981 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
4982
4983 {
4984 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
4985 Constant *C;
4986 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
4987 m_ImmConstant(C)))) &&
4988 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
4990 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
4991 }
4992 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
4993 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
4994 m_ImmConstant(C)))) &&
4995 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
4997 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
4998 }
4999 }
5000
5001 {
5002 // Similar to above: an unsigned overflow comparison may use offset + mask:
5003 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5004 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5005 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5006 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5007 BinaryOperator *BO;
5008 const APInt *C;
5009 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5010 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5012 CmpInst::Predicate NewPred =
5014 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5015 return new ICmpInst(NewPred, Op1, Zero);
5016 }
5017
5018 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5019 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5021 CmpInst::Predicate NewPred =
5023 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5024 return new ICmpInst(NewPred, Op0, Zero);
5025 }
5026 }
5027
5028 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5029 bool Op0HasNUW = false, Op1HasNUW = false;
5030 bool Op0HasNSW = false, Op1HasNSW = false;
5031 // Analyze the case when either Op0 or Op1 is an add instruction.
5032 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5033 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5034 bool &HasNSW, bool &HasNUW) -> bool {
5035 if (isa<OverflowingBinaryOperator>(BO)) {
5036 HasNUW = BO.hasNoUnsignedWrap();
5037 HasNSW = BO.hasNoSignedWrap();
5038 return ICmpInst::isEquality(Pred) ||
5039 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5040 (CmpInst::isSigned(Pred) && HasNSW);
5041 } else if (BO.getOpcode() == Instruction::Or) {
5042 HasNUW = true;
5043 HasNSW = true;
5044 return true;
5045 } else {
5046 return false;
5047 }
5048 };
5049 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5050
5051 if (BO0) {
5052 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
5053 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5054 }
5055 if (BO1) {
5056 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
5057 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5058 }
5059
5060 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5061 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5062 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5063 return new ICmpInst(Pred, A == Op1 ? B : A,
5064 Constant::getNullValue(Op1->getType()));
5065
5066 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5067 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5068 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5069 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
5070 C == Op0 ? D : C);
5071
5072 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5073 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5074 NoOp1WrapProblem) {
5075 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5076 Value *Y, *Z;
5077 if (A == C) {
5078 // C + B == C + D -> B == D
5079 Y = B;
5080 Z = D;
5081 } else if (A == D) {
5082 // D + B == C + D -> B == C
5083 Y = B;
5084 Z = C;
5085 } else if (B == C) {
5086 // A + C == C + D -> A == D
5087 Y = A;
5088 Z = D;
5089 } else {
5090 assert(B == D);
5091 // A + D == C + D -> A == C
5092 Y = A;
5093 Z = C;
5094 }
5095 return new ICmpInst(Pred, Y, Z);
5096 }
5097
5098 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5099 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
5100 match(B, m_AllOnes()))
5101 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
5102
5103 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5104 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
5105 match(B, m_AllOnes()))
5106 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
5107
5108 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5109 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
5110 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
5111
5112 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5113 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
5114 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
5115
5116 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5117 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
5118 match(D, m_AllOnes()))
5119 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
5120
5121 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5122 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
5123 match(D, m_AllOnes()))
5124 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
5125
5126 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5127 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
5128 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
5129
5130 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5131 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
5132 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
5133
5134 // TODO: The subtraction-related identities shown below also hold, but
5135 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5136 // wouldn't happen even if they were implemented.
5137 //
5138 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5139 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5140 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5141 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5142
5143 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5144 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One()))
5145 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1);
5146
5147 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5148 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One()))
5149 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1);
5150
5151 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5152 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One()))
5153 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C);
5154
5155 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5156 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One()))
5157 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C);
5158
5159 // if C1 has greater magnitude than C2:
5160 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5161 // s.t. C3 = C1 - C2
5162 //
5163 // if C2 has greater magnitude than C1:
5164 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5165 // s.t. C3 = C2 - C1
5166 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5167 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5168 const APInt *AP1, *AP2;
5169 // TODO: Support non-uniform vectors.
5170 // TODO: Allow poison passthrough if B or D's element is poison.
5171 if (match(B, m_APIntAllowPoison(AP1)) &&
5172 match(D, m_APIntAllowPoison(AP2)) &&
5173 AP1->isNegative() == AP2->isNegative()) {
5174 APInt AP1Abs = AP1->abs();
5175 APInt AP2Abs = AP2->abs();
5176 if (AP1Abs.uge(AP2Abs)) {
5177 APInt Diff = *AP1 - *AP2;
5178 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5179 Value *NewAdd = Builder.CreateAdd(
5180 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
5181 return new ICmpInst(Pred, NewAdd, C);
5182 } else {
5183 APInt Diff = *AP2 - *AP1;
5184 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5185 Value *NewAdd = Builder.CreateAdd(
5186 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
5187 return new ICmpInst(Pred, A, NewAdd);
5188 }
5189 }
5190 Constant *Cst1, *Cst2;
5191 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5192 ICmpInst::isEquality(Pred)) {
5193 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5194 Value *NewAdd = Builder.CreateAdd(C, Diff);
5195 return new ICmpInst(Pred, A, NewAdd);
5196 }
5197 }
5198
5199 // Analyze the case when either Op0 or Op1 is a sub instruction.
5200 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5201 A = nullptr;
5202 B = nullptr;
5203 C = nullptr;
5204 D = nullptr;
5205 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5206 A = BO0->getOperand(0);
5207 B = BO0->getOperand(1);
5208 }
5209 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5210 C = BO1->getOperand(0);
5211 D = BO1->getOperand(1);
5212 }
5213
5214 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5215 if (A == Op1 && NoOp0WrapProblem)
5216 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5217 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5218 if (C == Op0 && NoOp1WrapProblem)
5219 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5220
5221 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5222 // (A - B) u>/u<= A --> B u>/u<= A
5223 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5224 return new ICmpInst(Pred, B, A);
5225 // C u</u>= (C - D) --> C u</u>= D
5226 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5227 return new ICmpInst(Pred, C, D);
5228 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5229 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5230 isKnownNonZero(B, Q))
5232 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5233 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5234 isKnownNonZero(D, Q))
5236
5237 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5238 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5239 return new ICmpInst(Pred, A, C);
5240
5241 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5242 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5243 return new ICmpInst(Pred, D, B);
5244
5245 // icmp (0-X) < cst --> x > -cst
5246 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5247 Value *X;
5248 if (match(BO0, m_Neg(m_Value(X))))
5249 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5250 if (RHSC->isNotMinSignedValue())
5251 return new ICmpInst(I.getSwappedPredicate(), X,
5252 ConstantExpr::getNeg(RHSC));
5253 }
5254
5255 if (Instruction * R = foldICmpXorXX(I, Q, *this))
5256 return R;
5257 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5258 return R;
5259
5260 {
5261 // Try to remove shared multiplier from comparison:
5262 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z
5263 Value *X, *Y, *Z;
5264 if (Pred == ICmpInst::getUnsignedPredicate(Pred) &&
5265 ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5266 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5267 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5268 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))))) {
5269 bool NonZero;
5270 if (ICmpInst::isEquality(Pred)) {
5271 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5272 // if Z % 2 != 0
5273 // X * Z eq/ne Y * Z -> X eq/ne Y
5274 if (ZKnown.countMaxTrailingZeros() == 0)
5275 return new ICmpInst(Pred, X, Y);
5276 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5277 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5278 // X * Z eq/ne Y * Z -> X eq/ne Y
5279 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5280 return new ICmpInst(Pred, X, Y);
5281 } else
5282 NonZero = isKnownNonZero(Z, Q);
5283
5284 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5285 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5286 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5287 return new ICmpInst(Pred, X, Y);
5288 }
5289 }
5290
5291 BinaryOperator *SRem = nullptr;
5292 // icmp (srem X, Y), Y
5293 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5294 SRem = BO0;
5295 // icmp Y, (srem X, Y)
5296 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5297 Op0 == BO1->getOperand(1))
5298 SRem = BO1;
5299 if (SRem) {
5300 // We don't check hasOneUse to avoid increasing register pressure because
5301 // the value we use is the same value this instruction was already using.
5302 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5303 default:
5304 break;
5305 case ICmpInst::ICMP_EQ:
5306 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5307 case ICmpInst::ICMP_NE:
5308 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5309 case ICmpInst::ICMP_SGT:
5310 case ICmpInst::ICMP_SGE:
5311 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5313 case ICmpInst::ICMP_SLT:
5314 case ICmpInst::ICMP_SLE:
5315 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5317 }
5318 }
5319
5320 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5321 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5322 BO0->getOperand(1) == BO1->getOperand(1)) {
5323 switch (BO0->getOpcode()) {
5324 default:
5325 break;
5326 case Instruction::Add:
5327 case Instruction::Sub:
5328 case Instruction::Xor: {
5329 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5330 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5331
5332 const APInt *C;
5333 if (match(BO0->getOperand(1), m_APInt(C))) {
5334 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5335 if (C->isSignMask()) {
5336 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5337 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5338 }
5339
5340 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5341 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5342 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5343 NewPred = I.getSwappedPredicate(NewPred);
5344 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5345 }
5346 }
5347 break;
5348 }
5349 case Instruction::Mul: {
5350 if (!I.isEquality())
5351 break;
5352
5353 const APInt *C;
5354 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5355 !C->isOne()) {
5356 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5357 // Mask = -1 >> count-trailing-zeros(C).
5358 if (unsigned TZs = C->countr_zero()) {
5359 Constant *Mask = ConstantInt::get(
5360 BO0->getType(),
5361 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5362 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5363 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5364 return new ICmpInst(Pred, And1, And2);
5365 }
5366 }
5367 break;
5368 }
5369 case Instruction::UDiv:
5370 case Instruction::LShr:
5371 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5372 break;
5373 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5374
5375 case Instruction::SDiv:
5376 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5377 !BO0->isExact() || !BO1->isExact())
5378 break;
5379 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5380
5381 case Instruction::AShr:
5382 if (!BO0->isExact() || !BO1->isExact())
5383 break;
5384 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5385
5386 case Instruction::Shl: {
5387 bool NUW = Op0HasNUW && Op1HasNUW;
5388 bool NSW = Op0HasNSW && Op1HasNSW;
5389 if (!NUW && !NSW)
5390 break;
5391 if (!NSW && I.isSigned())
5392 break;
5393 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5394 }
5395 }
5396 }
5397
5398 if (BO0) {
5399 // Transform A & (L - 1) `ult` L --> L != 0
5400 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5401 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5402
5403 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5404 auto *Zero = Constant::getNullValue(BO0->getType());
5405 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5406 }
5407 }
5408
5409 // For unsigned predicates / eq / ne:
5410 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5411 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5412 if (!ICmpInst::isSigned(Pred)) {
5413 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5414 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5415 Constant::getNullValue(Op1->getType()));
5416 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5417 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5418 Constant::getNullValue(Op0->getType()), Op0);
5419 }
5420
5422 return replaceInstUsesWith(I, V);
5423
5424 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5425 return R;
5426
5428 return replaceInstUsesWith(I, V);
5429
5431 return replaceInstUsesWith(I, V);
5432
5433 return nullptr;
5434}
5435
5436/// Fold icmp Pred min|max(X, Y), Z.
5439 Value *Z,
5440 ICmpInst::Predicate Pred) {
5441 Value *X = MinMax->getLHS();
5442 Value *Y = MinMax->getRHS();
5443 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5444 return nullptr;
5445 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5446 // Revert the transform signed pred -> unsigned pred
5447 // TODO: We can flip the signedness of predicate if both operands of icmp
5448 // are negative.
5452 } else
5453 return nullptr;
5454 }
5456 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5457 if (!Val)
5458 return std::nullopt;
5459 if (match(Val, m_One()))
5460 return true;
5461 if (match(Val, m_Zero()))
5462 return false;
5463 return std::nullopt;
5464 };
5465 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5466 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5467 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5468 return nullptr;
5469 if (!CmpXZ.has_value()) {
5470 std::swap(X, Y);
5471 std::swap(CmpXZ, CmpYZ);
5472 }
5473
5474 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5475 if (CmpYZ.has_value())
5476 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5477 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5478 };
5479
5480 switch (Pred) {
5481 case ICmpInst::ICMP_EQ:
5482 case ICmpInst::ICMP_NE: {
5483 // If X == Z:
5484 // Expr Result
5485 // min(X, Y) == Z X <= Y
5486 // max(X, Y) == Z X >= Y
5487 // min(X, Y) != Z X > Y
5488 // max(X, Y) != Z X < Y
5489 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5490 ICmpInst::Predicate NewPred =
5491 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5492 if (Pred == ICmpInst::ICMP_NE)
5493 NewPred = ICmpInst::getInversePredicate(NewPred);
5494 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5495 }
5496 // Otherwise (X != Z):
5497 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5498 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5499 if (!MinMaxCmpXZ.has_value()) {
5500 std::swap(X, Y);
5501 std::swap(CmpXZ, CmpYZ);
5502 // Re-check pre-condition X != Z
5503 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5504 break;
5505 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5506 }
5507 if (!MinMaxCmpXZ.has_value())
5508 break;
5509 if (*MinMaxCmpXZ) {
5510 // Expr Fact Result
5511 // min(X, Y) == Z X < Z false
5512 // max(X, Y) == Z X > Z false
5513 // min(X, Y) != Z X < Z true
5514 // max(X, Y) != Z X > Z true
5515 return replaceInstUsesWith(
5516 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5517 } else {
5518 // Expr Fact Result
5519 // min(X, Y) == Z X > Z Y == Z
5520 // max(X, Y) == Z X < Z Y == Z
5521 // min(X, Y) != Z X > Z Y != Z
5522 // max(X, Y) != Z X < Z Y != Z
5523 return FoldIntoCmpYZ();
5524 }
5525 break;
5526 }
5527 case ICmpInst::ICMP_SLT:
5528 case ICmpInst::ICMP_ULT:
5529 case ICmpInst::ICMP_SLE:
5530 case ICmpInst::ICMP_ULE:
5531 case ICmpInst::ICMP_SGT:
5532 case ICmpInst::ICMP_UGT:
5533 case ICmpInst::ICMP_SGE:
5534 case ICmpInst::ICMP_UGE: {
5535 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5536 if (*CmpXZ) {
5537 if (IsSame) {
5538 // Expr Fact Result
5539 // min(X, Y) < Z X < Z true
5540 // min(X, Y) <= Z X <= Z true
5541 // max(X, Y) > Z X > Z true
5542 // max(X, Y) >= Z X >= Z true
5543 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5544 } else {
5545 // Expr Fact Result
5546 // max(X, Y) < Z X < Z Y < Z
5547 // max(X, Y) <= Z X <= Z Y <= Z
5548 // min(X, Y) > Z X > Z Y > Z
5549 // min(X, Y) >= Z X >= Z Y >= Z
5550 return FoldIntoCmpYZ();
5551 }
5552 } else {
5553 if (IsSame) {
5554 // Expr Fact Result
5555 // min(X, Y) < Z X >= Z Y < Z
5556 // min(X, Y) <= Z X > Z Y <= Z
5557 // max(X, Y) > Z X <= Z Y > Z
5558 // max(X, Y) >= Z X < Z Y >= Z
5559 return FoldIntoCmpYZ();
5560 } else {
5561 // Expr Fact Result
5562 // max(X, Y) < Z X >= Z false
5563 // max(X, Y) <= Z X > Z false
5564 // min(X, Y) > Z X <= Z false
5565 // min(X, Y) >= Z X < Z false
5566 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5567 }
5568 }
5569 break;
5570 }
5571 default:
5572 break;
5573 }
5574
5575 return nullptr;
5576}
5577
5578// Canonicalize checking for a power-of-2-or-zero value:
5580 InstCombiner::BuilderTy &Builder) {
5581 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5582 const CmpInst::Predicate Pred = I.getPredicate();
5583 Value *A = nullptr;
5584 bool CheckIs;
5585 if (I.isEquality()) {
5586 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5587 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5588 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5589 m_Deferred(A)))) ||
5590 !match(Op1, m_ZeroInt()))
5591 A = nullptr;
5592
5593 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5594 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5595 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5596 A = Op1;
5597 else if (match(Op1,
5599 A = Op0;
5600
5601 CheckIs = Pred == ICmpInst::ICMP_EQ;
5602 } else if (ICmpInst::isUnsigned(Pred)) {
5603 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5604 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5605
5606 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5608 m_Specific(Op1))))) {
5609 A = Op1;
5610 CheckIs = Pred == ICmpInst::ICMP_UGE;
5611 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5613 m_Specific(Op0))))) {
5614 A = Op0;
5615 CheckIs = Pred == ICmpInst::ICMP_ULE;
5616 }
5617 }
5618
5619 if (A) {
5620 Type *Ty = A->getType();
5621 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5622 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5623 ConstantInt::get(Ty, 2))
5624 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5625 ConstantInt::get(Ty, 1));
5626 }
5627
5628 return nullptr;
5629}
5630
5632 if (!I.isEquality())
5633 return nullptr;
5634
5635 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5636 const CmpInst::Predicate Pred = I.getPredicate();
5637 Value *A, *B, *C, *D;
5638 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5639 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5640 Value *OtherVal = A == Op1 ? B : A;
5641 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5642 }
5643
5644 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
5645 // A^c1 == C^c2 --> A == C^(c1^c2)
5646 ConstantInt *C1, *C2;
5647 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
5648 Op1->hasOneUse()) {
5649 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
5651 return new ICmpInst(Pred, A, Xor);
5652 }
5653
5654 // A^B == A^D -> B == D
5655 if (A == C)
5656 return new ICmpInst(Pred, B, D);
5657 if (A == D)
5658 return new ICmpInst(Pred, B, C);
5659 if (B == C)
5660 return new ICmpInst(Pred, A, D);
5661 if (B == D)
5662 return new ICmpInst(Pred, A, C);
5663 }
5664 }
5665
5666 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
5667 // A == (A^B) -> B == 0
5668 Value *OtherVal = A == Op0 ? B : A;
5669 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5670 }
5671
5672 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
5673 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
5674 match(Op1, m_And(m_Value(C), m_Value(D)))) {
5675 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
5676
5677 if (A == C) {
5678 X = B;
5679 Y = D;
5680 Z = A;
5681 } else if (A == D) {
5682 X = B;
5683 Y = C;
5684 Z = A;
5685 } else if (B == C) {
5686 X = A;
5687 Y = D;
5688 Z = B;
5689 } else if (B == D) {
5690 X = A;
5691 Y = C;
5692 Z = B;
5693 }
5694
5695 if (X) {
5696 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
5697 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
5698 // instructions.
5699 const APInt *C0, *C1;
5700 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
5701 (*C0 ^ *C1).isNegatedPowerOf2();
5702
5703 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
5704 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
5705 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
5706 int UseCnt =
5707 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
5708 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
5709 if (XorIsNegP2 || UseCnt >= 2) {
5710 // Build (X^Y) & Z
5711 Op1 = Builder.CreateXor(X, Y);
5712 Op1 = Builder.CreateAnd(Op1, Z);
5713 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
5714 }
5715 }
5716 }
5717
5718 {
5719 // Similar to above, but specialized for constant because invert is needed:
5720 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
5721 Value *X, *Y;
5722 Constant *C;
5723 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
5724 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
5727 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
5728 }
5729 }
5730
5731 if (match(Op1, m_ZExt(m_Value(A))) &&
5732 (Op0->hasOneUse() || Op1->hasOneUse())) {
5733 // (B & (Pow2C-1)) == zext A --> A == trunc B
5734 // (B & (Pow2C-1)) != zext A --> A != trunc B
5735 const APInt *MaskC;
5736 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
5737 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
5738 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
5739 }
5740
5741 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
5742 // For lshr and ashr pairs.
5743 const APInt *AP1, *AP2;
5744 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5745 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
5746 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5747 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
5748 if (AP1 != AP2)
5749 return nullptr;
5750 unsigned TypeBits = AP1->getBitWidth();
5751 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
5752 if (ShAmt < TypeBits && ShAmt != 0) {
5753 ICmpInst::Predicate NewPred =
5755 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5756 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
5757 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
5758 }
5759 }
5760
5761 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
5762 ConstantInt *Cst1;
5763 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
5764 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
5765 unsigned TypeBits = Cst1->getBitWidth();
5766 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
5767 if (ShAmt < TypeBits && ShAmt != 0) {
5768 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5769 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
5771 I.getName() + ".mask");
5772 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
5773 }
5774 }
5775
5776 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
5777 // "icmp (and X, mask), cst"
5778 uint64_t ShAmt = 0;
5779 if (Op0->hasOneUse() &&
5780 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
5781 match(Op1, m_ConstantInt(Cst1)) &&
5782 // Only do this when A has multiple uses. This is most important to do
5783 // when it exposes other optimizations.
5784 !A->hasOneUse()) {
5785 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
5786
5787 if (ShAmt < ASize) {
5788 APInt MaskV =
5790 MaskV <<= ShAmt;
5791
5792 APInt CmpV = Cst1->getValue().zext(ASize);
5793 CmpV <<= ShAmt;
5794
5795 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
5796 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
5797 }
5798 }
5799
5801 return ICmp;
5802
5803 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks the
5804 // top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s INT_MAX",
5805 // which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a few steps
5806 // of instcombine.
5807 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
5808 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
5810 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
5811 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
5813 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
5814 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
5816 Add, ConstantInt::get(A->getType(), C.shl(1)));
5817 }
5818
5819 // Canonicalize:
5820 // Assume B_Pow2 != 0
5821 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
5822 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
5823 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
5824 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, 0, &I))
5825 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
5827
5828 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
5829 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, 0, &I))
5830 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
5831 ConstantInt::getNullValue(Op1->getType()));
5832
5833 // Canonicalize:
5834 // icmp eq/ne X, OneUse(rotate-right(X))
5835 // -> icmp eq/ne X, rotate-left(X)
5836 // We generally try to convert rotate-right -> rotate-left, this just
5837 // canonicalizes another case.
5838 if (match(&I, m_c_ICmp(m_Value(A),
5839 m_OneUse(m_Intrinsic<Intrinsic::fshr>(
5840 m_Deferred(A), m_Deferred(A), m_Value(B))))))
5841 return new ICmpInst(
5842 Pred, A,
5843 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
5844
5845 // Canonicalize:
5846 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
5847 Constant *Cst;
5850 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
5851
5852 {
5853 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5854 auto m_Matcher =
5857 m_Sub(m_Value(B), m_Deferred(A)));
5858 std::optional<bool> IsZero = std::nullopt;
5859 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),
5860 m_Deferred(A))))
5861 IsZero = false;
5862 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5863 else if (match(&I,
5864 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))
5865 IsZero = true;
5866
5867 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, /*Depth*/ 0, &I))
5868 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5869 // -> (icmp eq/ne (and X, P2), 0)
5870 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5871 // -> (icmp eq/ne (and X, P2), P2)
5872 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
5873 *IsZero ? A
5874 : ConstantInt::getNullValue(A->getType()));
5875 }
5876
5877 return nullptr;
5878}
5879
5881 ICmpInst::Predicate Pred = ICmp.getPredicate();
5882 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
5883
5884 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
5885 // The trunc masks high bits while the compare may effectively mask low bits.
5886 Value *X;
5887 const APInt *C;
5888 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
5889 return nullptr;
5890
5891 // This matches patterns corresponding to tests of the signbit as well as:
5892 // (trunc X) u< C --> (X & -C) == 0 (are all masked-high-bits clear?)
5893 // (trunc X) u> C --> (X & ~C) != 0 (are any masked-high-bits set?)
5894 APInt Mask;
5895 if (decomposeBitTestICmp(Op0, Op1, Pred, X, Mask, true /* WithTrunc */)) {
5896 Value *And = Builder.CreateAnd(X, Mask);
5897 Constant *Zero = ConstantInt::getNullValue(X->getType());
5898 return new ICmpInst(Pred, And, Zero);
5899 }
5900
5901 unsigned SrcBits = X->getType()->getScalarSizeInBits();
5902 if (Pred == ICmpInst::ICMP_ULT && C->isNegatedPowerOf2()) {
5903 // If C is a negative power-of-2 (high-bit mask):
5904 // (trunc X) u< C --> (X & C) != C (are any masked-high-bits clear?)
5905 Constant *MaskC = ConstantInt::get(X->getType(), C->zext(SrcBits));
5906 Value *And = Builder.CreateAnd(X, MaskC);
5907 return new ICmpInst(ICmpInst::ICMP_NE, And, MaskC);
5908 }
5909
5910 if (Pred == ICmpInst::ICMP_UGT && (~*C).isPowerOf2()) {
5911 // If C is not-of-power-of-2 (one clear bit):
5912 // (trunc X) u> C --> (X & (C+1)) == C+1 (are all masked-high-bits set?)
5913 Constant *MaskC = ConstantInt::get(X->getType(), (*C + 1).zext(SrcBits));
5914 Value *And = Builder.CreateAnd(X, MaskC);
5915 return new ICmpInst(ICmpInst::ICMP_EQ, And, MaskC);
5916 }
5917
5918 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
5919 if (II->getIntrinsicID() == Intrinsic::cttz ||
5920 II->getIntrinsicID() == Intrinsic::ctlz) {
5921 unsigned MaxRet = SrcBits;
5922 // If the "is_zero_poison" argument is set, then we know at least
5923 // one bit is set in the input, so the result is always at least one
5924 // less than the full bitwidth of that input.
5925 if (match(II->getArgOperand(1), m_One()))
5926 MaxRet--;
5927
5928 // Make sure the destination is wide enough to hold the largest output of
5929 // the intrinsic.
5930 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
5931 if (Instruction *I =
5932 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
5933 return I;
5934 }
5935 }
5936
5937 return nullptr;
5938}
5939
5941 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
5942 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
5943 Value *X;
5944 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
5945 return nullptr;
5946
5947 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
5948 bool IsSignedCmp = ICmp.isSigned();
5949
5950 // icmp Pred (ext X), (ext Y)
5951 Value *Y;
5952 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
5953 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
5954 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
5955
5956 if (IsZext0 != IsZext1) {
5957 // If X and Y and both i1
5958 // (icmp eq/ne (zext X) (sext Y))
5959 // eq -> (icmp eq (or X, Y), 0)
5960 // ne -> (icmp ne (or X, Y), 0)
5961 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
5962 Y->getType()->isIntOrIntVectorTy(1))
5963 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
5964 Constant::getNullValue(X->getType()));
5965
5966 // If we have mismatched casts and zext has the nneg flag, we can
5967 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
5968
5969 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
5970 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
5971
5972 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
5973 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
5974
5975 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
5976 IsSignedExt = true;
5977 else
5978 return nullptr;
5979 }
5980
5981 // Not an extension from the same type?
5982 Type *XTy = X->getType(), *YTy = Y->getType();
5983 if (XTy != YTy) {
5984 // One of the casts must have one use because we are creating a new cast.
5985 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
5986 return nullptr;
5987 // Extend the narrower operand to the type of the wider operand.
5988 CastInst::CastOps CastOpcode =
5989 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
5990 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
5991 X = Builder.CreateCast(CastOpcode, X, YTy);
5992 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
5993 Y = Builder.CreateCast(CastOpcode, Y, XTy);
5994 else
5995 return nullptr;
5996 }
5997
5998 // (zext X) == (zext Y) --> X == Y
5999 // (sext X) == (sext Y) --> X == Y
6000 if (ICmp.isEquality())
6001 return new ICmpInst(ICmp.getPredicate(), X, Y);
6002
6003 // A signed comparison of sign extended values simplifies into a
6004 // signed comparison.
6005 if (IsSignedCmp && IsSignedExt)
6006 return new ICmpInst(ICmp.getPredicate(), X, Y);
6007
6008 // The other three cases all fold into an unsigned comparison.
6009 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6010 }
6011
6012 // Below here, we are only folding a compare with constant.
6013 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
6014 if (!C)
6015 return nullptr;
6016
6017 // If a lossless truncate is possible...
6018 Type *SrcTy = CastOp0->getSrcTy();
6019 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
6020 if (Res) {
6021 if (ICmp.isEquality())
6022 return new ICmpInst(ICmp.getPredicate(), X, Res);
6023
6024 // A signed comparison of sign extended values simplifies into a
6025 // signed comparison.
6026 if (IsSignedExt && IsSignedCmp)
6027 return new ICmpInst(ICmp.getPredicate(), X, Res);
6028
6029 // The other three cases all fold into an unsigned comparison.
6030 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6031 }
6032
6033 // The re-extended constant changed, partly changed (in the case of a vector),
6034 // or could not be determined to be equal (in the case of a constant
6035 // expression), so the constant cannot be represented in the shorter type.
6036 // All the cases that fold to true or false will have already been handled
6037 // by simplifyICmpInst, so only deal with the tricky case.
6038 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
6039 return nullptr;
6040
6041 // Is source op positive?
6042 // icmp ult (sext X), C --> icmp sgt X, -1
6043 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6045
6046 // Is source op negative?
6047 // icmp ugt (sext X), C --> icmp slt X, 0
6048 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6050}
6051
6052/// Handle icmp (cast x), (cast or constant).
6054 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6055 // icmp compares only pointer's value.
6056 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6057 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
6058 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
6059 if (SimplifiedOp0 || SimplifiedOp1)
6060 return new ICmpInst(ICmp.getPredicate(),
6061 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
6062 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
6063
6064 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
6065 if (!CastOp0)
6066 return nullptr;
6067 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
6068 return nullptr;
6069
6070 Value *Op0Src = CastOp0->getOperand(0);
6071 Type *SrcTy = CastOp0->getSrcTy();
6072 Type *DestTy = CastOp0->getDestTy();
6073
6074 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6075 // integer type is the same size as the pointer type.
6076 auto CompatibleSizes = [&](Type *SrcTy, Type *DestTy) {
6077 if (isa<VectorType>(SrcTy)) {
6078 SrcTy = cast<VectorType>(SrcTy)->getElementType();
6079 DestTy = cast<VectorType>(DestTy)->getElementType();
6080 }
6081 return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
6082 };
6083 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6084 CompatibleSizes(SrcTy, DestTy)) {
6085 Value *NewOp1 = nullptr;
6086 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
6087 Value *PtrSrc = PtrToIntOp1->getOperand(0);
6088 if (PtrSrc->getType() == Op0Src->getType())
6089 NewOp1 = PtrToIntOp1->getOperand(0);
6090 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6091 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
6092 }
6093
6094 if (NewOp1)
6095 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6096 }
6097
6098 if (Instruction *R = foldICmpWithTrunc(ICmp))
6099 return R;
6100
6101 return foldICmpWithZextOrSext(ICmp);
6102}
6103
6104static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned) {
6105 switch (BinaryOp) {
6106 default:
6107 llvm_unreachable("Unsupported binary op");
6108 case Instruction::Add:
6109 case Instruction::Sub:
6110 return match(RHS, m_Zero());
6111 case Instruction::Mul:
6112 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
6113 match(RHS, m_One());
6114 }
6115}
6116
6119 bool IsSigned, Value *LHS, Value *RHS,
6120 Instruction *CxtI) const {
6121 switch (BinaryOp) {
6122 default:
6123 llvm_unreachable("Unsupported binary op");
6124 case Instruction::Add:
6125 if (IsSigned)
6126 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6127 else
6128 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6129 case Instruction::Sub:
6130 if (IsSigned)
6131 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6132 else
6133 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6134 case Instruction::Mul:
6135 if (IsSigned)
6136 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6137 else
6138 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6139 }
6140}
6141
6142bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6143 bool IsSigned, Value *LHS,
6144 Value *RHS, Instruction &OrigI,
6145 Value *&Result,
6146 Constant *&Overflow) {
6147 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
6148 std::swap(LHS, RHS);
6149
6150 // If the overflow check was an add followed by a compare, the insertion point
6151 // may be pointing to the compare. We want to insert the new instructions
6152 // before the add in case there are uses of the add between the add and the
6153 // compare.
6154 Builder.SetInsertPoint(&OrigI);
6155
6156 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
6157 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
6158 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
6159
6160 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6161 Result = LHS;
6162 Overflow = ConstantInt::getFalse(OverflowTy);
6163 return true;
6164 }
6165
6166 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
6168 return false;
6171 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6172 Result->takeName(&OrigI);
6173 Overflow = ConstantInt::getTrue(OverflowTy);
6174 return true;
6176 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6177 Result->takeName(&OrigI);
6178 Overflow = ConstantInt::getFalse(OverflowTy);
6179 if (auto *Inst = dyn_cast<Instruction>(Result)) {
6180 if (IsSigned)
6181 Inst->setHasNoSignedWrap();
6182 else
6183 Inst->setHasNoUnsignedWrap();
6184 }
6185 return true;
6186 }
6187
6188 llvm_unreachable("Unexpected overflow result");
6189}
6190
6191/// Recognize and process idiom involving test for multiplication
6192/// overflow.
6193///
6194/// The caller has matched a pattern of the form:
6195/// I = cmp u (mul(zext A, zext B), V
6196/// The function checks if this is a test for overflow and if so replaces
6197/// multiplication with call to 'mul.with.overflow' intrinsic.
6198///
6199/// \param I Compare instruction.
6200/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6201/// the compare instruction. Must be of integer type.
6202/// \param OtherVal The other argument of compare instruction.
6203/// \returns Instruction which must replace the compare instruction, NULL if no
6204/// replacement required.
6206 const APInt *OtherVal,
6207 InstCombinerImpl &IC) {
6208 // Don't bother doing this transformation for pointers, don't do it for
6209 // vectors.
6210 if (!isa<IntegerType>(MulVal->getType()))
6211 return nullptr;
6212
6213 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6214 if (!MulInstr)
6215 return nullptr;
6216 assert(MulInstr->getOpcode() == Instruction::Mul);
6217
6218 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6219 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6220 assert(LHS->getOpcode() == Instruction::ZExt);
6221 assert(RHS->getOpcode() == Instruction::ZExt);
6222 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6223
6224 // Calculate type and width of the result produced by mul.with.overflow.
6225 Type *TyA = A->getType(), *TyB = B->getType();
6226 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6227 WidthB = TyB->getPrimitiveSizeInBits();
6228 unsigned MulWidth;
6229 Type *MulType;
6230 if (WidthB > WidthA) {
6231 MulWidth = WidthB;
6232 MulType = TyB;
6233 } else {
6234 MulWidth = WidthA;
6235 MulType = TyA;
6236 }
6237
6238 // In order to replace the original mul with a narrower mul.with.overflow,
6239 // all uses must ignore upper bits of the product. The number of used low
6240 // bits must be not greater than the width of mul.with.overflow.
6241 if (MulVal->hasNUsesOrMore(2))
6242 for (User *U : MulVal->users()) {
6243 if (U == &I)
6244 continue;
6245 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6246 // Check if truncation ignores bits above MulWidth.
6247 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6248 if (TruncWidth > MulWidth)
6249 return nullptr;
6250 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6251 // Check if AND ignores bits above MulWidth.
6252 if (BO->getOpcode() != Instruction::And)
6253 return nullptr;
6254 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6255 const APInt &CVal = CI->getValue();
6256 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6257 return nullptr;
6258 } else {
6259 // In this case we could have the operand of the binary operation
6260 // being defined in another block, and performing the replacement
6261 // could break the dominance relation.
6262 return nullptr;
6263 }
6264 } else {
6265 // Other uses prohibit this transformation.
6266 return nullptr;
6267 }
6268 }
6269
6270 // Recognize patterns
6271 switch (I.getPredicate()) {
6272 case ICmpInst::ICMP_UGT: {
6273 // Recognize pattern:
6274 // mulval = mul(zext A, zext B)
6275 // cmp ugt mulval, max
6276 APInt MaxVal = APInt::getMaxValue(MulWidth);
6277 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6278 if (MaxVal.eq(*OtherVal))
6279 break; // Recognized
6280 return nullptr;
6281 }
6282
6283 case ICmpInst::ICMP_ULT: {
6284 // Recognize pattern:
6285 // mulval = mul(zext A, zext B)
6286 // cmp ule mulval, max + 1
6287 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6288 if (MaxVal.eq(*OtherVal))
6289 break; // Recognized
6290 return nullptr;
6291 }
6292
6293 default:
6294 return nullptr;
6295 }
6296
6297 InstCombiner::BuilderTy &Builder = IC.Builder;
6298 Builder.SetInsertPoint(MulInstr);
6299
6300 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6301 Value *MulA = A, *MulB = B;
6302 if (WidthA < MulWidth)
6303 MulA = Builder.CreateZExt(A, MulType);
6304 if (WidthB < MulWidth)
6305 MulB = Builder.CreateZExt(B, MulType);
6307 I.getModule(), Intrinsic::umul_with_overflow, MulType);
6308 CallInst *Call = Builder.CreateCall(F, {MulA, MulB}, "umul");
6309 IC.addToWorklist(MulInstr);
6310
6311 // If there are uses of mul result other than the comparison, we know that
6312 // they are truncation or binary AND. Change them to use result of
6313 // mul.with.overflow and adjust properly mask/size.
6314 if (MulVal->hasNUsesOrMore(2)) {
6315 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6316 for (User *U : make_early_inc_range(MulVal->users())) {
6317 if (U == &I)
6318 continue;
6319 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6320 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6321 IC.replaceInstUsesWith(*TI, Mul);
6322 else
6323 TI->setOperand(0, Mul);
6324 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6325 assert(BO->getOpcode() == Instruction::And);
6326 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6327 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6328 APInt ShortMask = CI->getValue().trunc(MulWidth);
6329 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6330 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6331 IC.replaceInstUsesWith(*BO, Zext);
6332 } else {
6333 llvm_unreachable("Unexpected Binary operation");
6334 }
6335 IC.addToWorklist(cast<Instruction>(U));
6336 }
6337 }
6338
6339 // The original icmp gets replaced with the overflow value, maybe inverted
6340 // depending on predicate.
6341 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6342 Value *Res = Builder.CreateExtractValue(Call, 1);
6343 return BinaryOperator::CreateNot(Res);
6344 }
6345
6346 return ExtractValueInst::Create(Call, 1);
6347}
6348
6349/// When performing a comparison against a constant, it is possible that not all
6350/// the bits in the LHS are demanded. This helper method computes the mask that
6351/// IS demanded.
6353 const APInt *RHS;
6354 if (!match(I.getOperand(1), m_APInt(RHS)))
6356
6357 // If this is a normal comparison, it demands all bits. If it is a sign bit
6358 // comparison, it only demands the sign bit.
6359 bool UnusedBit;
6360 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6362
6363 switch (I.getPredicate()) {
6364 // For a UGT comparison, we don't care about any bits that
6365 // correspond to the trailing ones of the comparand. The value of these
6366 // bits doesn't impact the outcome of the comparison, because any value
6367 // greater than the RHS must differ in a bit higher than these due to carry.
6368 case ICmpInst::ICMP_UGT:
6369 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6370
6371 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6372 // Any value less than the RHS must differ in a higher bit because of carries.
6373 case ICmpInst::ICMP_ULT:
6374 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6375
6376 default:
6378 }
6379}
6380
6381/// Check that one use is in the same block as the definition and all
6382/// other uses are in blocks dominated by a given block.
6383///
6384/// \param DI Definition
6385/// \param UI Use
6386/// \param DB Block that must dominate all uses of \p DI outside
6387/// the parent block
6388/// \return true when \p UI is the only use of \p DI in the parent block
6389/// and all other uses of \p DI are in blocks dominated by \p DB.
6390///
6392 const Instruction *UI,
6393 const BasicBlock *DB) const {
6394 assert(DI && UI && "Instruction not defined\n");
6395 // Ignore incomplete definitions.
6396 if (!DI->getParent())
6397 return false;
6398 // DI and UI must be in the same block.
6399 if (DI->getParent() != UI->getParent())
6400 return false;
6401 // Protect from self-referencing blocks.
6402 if (DI->getParent() == DB)
6403 return false;
6404 for (const User *U : DI->users()) {
6405 auto *Usr = cast<Instruction>(U);
6406 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6407 return false;
6408 }
6409 return true;
6410}
6411
6412/// Return true when the instruction sequence within a block is select-cmp-br.
6413static bool isChainSelectCmpBranch(const SelectInst *SI) {
6414 const BasicBlock *BB = SI->getParent();
6415 if (!BB)
6416 return false;
6417 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
6418 if (!BI || BI->getNumSuccessors() != 2)
6419 return false;
6420 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6421 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6422 return false;
6423 return true;
6424}
6425
6426/// True when a select result is replaced by one of its operands
6427/// in select-icmp sequence. This will eventually result in the elimination
6428/// of the select.
6429///
6430/// \param SI Select instruction
6431/// \param Icmp Compare instruction
6432/// \param SIOpd Operand that replaces the select
6433///
6434/// Notes:
6435/// - The replacement is global and requires dominator information
6436/// - The caller is responsible for the actual replacement
6437///
6438/// Example:
6439///
6440/// entry:
6441/// %4 = select i1 %3, %C* %0, %C* null
6442/// %5 = icmp eq %C* %4, null
6443/// br i1 %5, label %9, label %7
6444/// ...
6445/// ; <label>:7 ; preds = %entry
6446/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6447/// ...
6448///
6449/// can be transformed to
6450///
6451/// %5 = icmp eq %C* %0, null
6452/// %6 = select i1 %3, i1 %5, i1 true
6453/// br i1 %6, label %9, label %7
6454/// ...
6455/// ; <label>:7 ; preds = %entry
6456/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6457///
6458/// Similar when the first operand of the select is a constant or/and
6459/// the compare is for not equal rather than equal.
6460///
6461/// NOTE: The function is only called when the select and compare constants
6462/// are equal, the optimization can work only for EQ predicates. This is not a
6463/// major restriction since a NE compare should be 'normalized' to an equal
6464/// compare, which usually happens in the combiner and test case
6465/// select-cmp-br.ll checks for it.
6467 const ICmpInst *Icmp,
6468 const unsigned SIOpd) {
6469 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6471 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6472 // The check for the single predecessor is not the best that can be
6473 // done. But it protects efficiently against cases like when SI's
6474 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6475 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6476 // replaced can be reached on either path. So the uniqueness check
6477 // guarantees that the path all uses of SI (outside SI's parent) are on
6478 // is disjoint from all other paths out of SI. But that information
6479 // is more expensive to compute, and the trade-off here is in favor
6480 // of compile-time. It should also be noticed that we check for a single
6481 // predecessor and not only uniqueness. This to handle the situation when
6482 // Succ and Succ1 points to the same basic block.
6483 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6484 NumSel++;
6485 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6486 return true;
6487 }
6488 }
6489 return false;
6490}
6491
6492/// Try to fold the comparison based on range information we can get by checking
6493/// whether bits are known to be zero or one in the inputs.
6495 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6496 Type *Ty = Op0->getType();
6497 ICmpInst::Predicate Pred = I.getPredicate();
6498
6499 // Get scalar or pointer size.
6500 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6501 ? Ty->getScalarSizeInBits()
6503
6504 if (!BitWidth)
6505 return nullptr;
6506
6507 KnownBits Op0Known(BitWidth);
6508 KnownBits Op1Known(BitWidth);
6509
6510 {
6511 // Don't use dominating conditions when folding icmp using known bits. This
6512 // may convert signed into unsigned predicates in ways that other passes
6513 // (especially IndVarSimplify) may not be able to reliably undo.
6516 Op0Known, /*Depth=*/0, Q))
6517 return &I;
6518
6520 /*Depth=*/0, Q))
6521 return &I;
6522 }
6523
6524 // Given the known and unknown bits, compute a range that the LHS could be
6525 // in. Compute the Min, Max and RHS values based on the known bits. For the
6526 // EQ and NE we use unsigned values.
6527 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6528 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6529 if (I.isSigned()) {
6530 Op0Min = Op0Known.getSignedMinValue();
6531 Op0Max = Op0Known.getSignedMaxValue();
6532 Op1Min = Op1Known.getSignedMinValue();
6533 Op1Max = Op1Known.getSignedMaxValue();
6534 } else {
6535 Op0Min = Op0Known.getMinValue();
6536 Op0Max = Op0Known.getMaxValue();
6537 Op1Min = Op1Known.getMinValue();
6538 Op1Max = Op1Known.getMaxValue();
6539 }
6540
6541 // If Min and Max are known to be the same, then SimplifyDemandedBits figured
6542 // out that the LHS or RHS is a constant. Constant fold this now, so that
6543 // code below can assume that Min != Max.
6544 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
6545 return new ICmpInst(Pred, ConstantExpr::getIntegerValue(Ty, Op0Min), Op1);
6546 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
6547 return new ICmpInst(Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Min));
6548
6549 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6550 // min/max canonical compare with some other compare. That could lead to
6551 // conflict with select canonicalization and infinite looping.
6552 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6553 auto isMinMaxCmp = [&](Instruction &Cmp) {
6554 if (!Cmp.hasOneUse())
6555 return false;
6556 Value *A, *B;
6557 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6559 return false;
6560 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6561 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6562 };
6563 if (!isMinMaxCmp(I)) {
6564 switch (Pred) {
6565 default:
6566 break;
6567 case ICmpInst::ICMP_ULT: {
6568 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6569 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6570 const APInt *CmpC;
6571 if (match(Op1, m_APInt(CmpC))) {
6572 // A <u C -> A == C-1 if min(A)+1 == C
6573 if (*CmpC == Op0Min + 1)
6574 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6575 ConstantInt::get(Op1->getType(), *CmpC - 1));
6576 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6577 // exceeds the log2 of C.
6578 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6579 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6580 Constant::getNullValue(Op1->getType()));
6581 }
6582 break;
6583 }
6584 case ICmpInst::ICMP_UGT: {
6585 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6586 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6587 const APInt *CmpC;
6588 if (match(Op1, m_APInt(CmpC))) {
6589 // A >u C -> A == C+1 if max(a)-1 == C
6590 if (*CmpC == Op0Max - 1)
6591 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6592 ConstantInt::get(Op1->getType(), *CmpC + 1));
6593 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6594 // exceeds the log2 of C.
6595 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6596 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6597 Constant::getNullValue(Op1->getType()));
6598 }
6599 break;
6600 }
6601 case ICmpInst::ICMP_SLT: {
6602 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6603 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6604 const APInt *CmpC;
6605 if (match(Op1, m_APInt(CmpC))) {
6606 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6607 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6608 ConstantInt::get(Op1->getType(), *CmpC - 1));
6609 }
6610 break;
6611 }
6612 case ICmpInst::ICMP_SGT: {
6613 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6614 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6615 const APInt *CmpC;
6616 if (match(Op1, m_APInt(CmpC))) {
6617 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6618 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6619 ConstantInt::get(Op1->getType(), *CmpC + 1));
6620 }
6621 break;
6622 }
6623 }
6624 }
6625
6626 // Based on the range information we know about the LHS, see if we can
6627 // simplify this comparison. For example, (x&4) < 8 is always true.
6628 switch (Pred) {
6629 default:
6630 llvm_unreachable("Unknown icmp opcode!");
6631 case ICmpInst::ICMP_EQ:
6632 case ICmpInst::ICMP_NE: {
6633 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
6634 return replaceInstUsesWith(
6635 I, ConstantInt::getBool(I.getType(), Pred == CmpInst::ICMP_NE));
6636
6637 // If all bits are known zero except for one, then we know at most one bit
6638 // is set. If the comparison is against zero, then this is a check to see if
6639 // *that* bit is set.
6640 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
6641 if (Op1Known.isZero()) {
6642 // If the LHS is an AND with the same constant, look through it.
6643 Value *LHS = nullptr;
6644 const APInt *LHSC;
6645 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
6646 *LHSC != Op0KnownZeroInverted)
6647 LHS = Op0;
6648
6649 Value *X;
6650 const APInt *C1;
6651 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
6652 Type *XTy = X->getType();
6653 unsigned Log2C1 = C1->countr_zero();
6654 APInt C2 = Op0KnownZeroInverted;
6655 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
6656 if (C2Pow2.isPowerOf2()) {
6657 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
6658 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
6659 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
6660 unsigned Log2C2 = C2Pow2.countr_zero();
6661 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
6662 auto NewPred =
6664 return new ICmpInst(NewPred, X, CmpC);
6665 }
6666 }
6667 }
6668
6669 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
6670 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
6671 (Op0Known & Op1Known) == Op0Known)
6672 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6673 ConstantInt::getNullValue(Op1->getType()));
6674 break;
6675 }
6676 case ICmpInst::ICMP_ULT: {
6677 if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
6678 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6679 if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
6680 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6681 break;
6682 }
6683 case ICmpInst::ICMP_UGT: {
6684 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
6685 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6686 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
6687 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6688 break;
6689 }
6690 case ICmpInst::ICMP_SLT: {
6691 if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
6692 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6693 if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
6694 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6695 break;
6696 }
6697 case ICmpInst::ICMP_SGT: {
6698 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
6699 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6700 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
6701 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6702 break;
6703 }
6704 case ICmpInst::ICMP_SGE:
6705 assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
6706 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
6707 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6708 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
6709 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6710 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
6711 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6712 break;
6713 case ICmpInst::ICMP_SLE:
6714 assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
6715 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
6716 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6717 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
6718 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6719 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
6720 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6721 break;
6722 case ICmpInst::ICMP_UGE:
6723 assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
6724 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
6725 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6726 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
6727 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6728 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
6729 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6730 break;
6731 case ICmpInst::ICMP_ULE:
6732 assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
6733 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
6734 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6735 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
6736 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6737 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
6738 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6739 break;
6740 }
6741
6742 // Turn a signed comparison into an unsigned one if both operands are known to
6743 // have the same sign.
6744 if (I.isSigned() &&
6745 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
6746 (Op0Known.One.isNegative() && Op1Known.One.isNegative())))
6747 return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
6748
6749 return nullptr;
6750}
6751
6752/// If one operand of an icmp is effectively a bool (value range of {0,1}),
6753/// then try to reduce patterns based on that limit.
6755 Value *X, *Y;
6757
6758 // X must be 0 and bool must be true for "ULT":
6759 // X <u (zext i1 Y) --> (X == 0) & Y
6760 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
6761 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
6762 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
6763
6764 // X must be 0 or bool must be true for "ULE":
6765 // X <=u (sext i1 Y) --> (X == 0) | Y
6766 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
6767 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
6768 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
6769
6770 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
6771 ICmpInst::Predicate Pred1, Pred2;
6772 const APInt *C;
6773 Instruction *ExtI;
6774 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
6777 m_APInt(C)))))) &&
6778 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
6779 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
6780 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
6781 auto CreateRangeCheck = [&] {
6782 Value *CmpV1 =
6783 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
6784 Value *CmpV2 = Builder.CreateICmp(
6785 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
6787 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
6788 CmpV1, CmpV2);
6789 };
6790 if (C->isZero()) {
6791 if (Pred2 == ICmpInst::ICMP_EQ) {
6792 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
6793 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
6794 return replaceInstUsesWith(
6795 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6796 } else if (!IsSExt || HasOneUse) {
6797 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
6798 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
6799 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
6800 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X == -1
6801 return CreateRangeCheck();
6802 }
6803 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
6804 if (Pred2 == ICmpInst::ICMP_NE) {
6805 // icmp eq X, (zext (icmp ne X, 1)) --> false
6806 // icmp ne X, (zext (icmp ne X, 1)) --> true
6807 // icmp eq X, (sext (icmp ne X, -1)) --> false
6808 // icmp ne X, (sext (icmp ne X, -1)) --> true
6809 return replaceInstUsesWith(
6810 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6811 } else if (!IsSExt || HasOneUse) {
6812 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
6813 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
6814 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
6815 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
6816 return CreateRangeCheck();
6817 }
6818 } else {
6819 // when C != 0 && C != 1:
6820 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
6821 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
6822 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
6823 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
6824 // when C != 0 && C != -1:
6825 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
6826 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
6827 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
6828 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
6829 return ICmpInst::Create(
6830 Instruction::ICmp, Pred1, X,
6831 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
6832 ? (IsSExt ? -1 : 1)
6833 : 0));
6834 }
6835 }
6836
6837 return nullptr;
6838}
6839
6840std::optional<std::pair<CmpInst::Predicate, Constant *>>
6842 Constant *C) {
6844 "Only for relational integer predicates.");
6845
6846 Type *Type = C->getType();
6847 bool IsSigned = ICmpInst::isSigned(Pred);
6848
6850 bool WillIncrement =
6851 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
6852
6853 // Check if the constant operand can be safely incremented/decremented
6854 // without overflowing/underflowing.
6855 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
6856 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
6857 };
6858
6859 Constant *SafeReplacementConstant = nullptr;
6860 if (auto *CI = dyn_cast<ConstantInt>(C)) {
6861 // Bail out if the constant can't be safely incremented/decremented.
6862 if (!ConstantIsOk(CI))
6863 return std::nullopt;
6864 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
6865 unsigned NumElts = FVTy->getNumElements();
6866 for (unsigned i = 0; i != NumElts; ++i) {
6867 Constant *Elt = C->getAggregateElement(i);
6868 if (!Elt)
6869 return std::nullopt;
6870
6871 if (isa<UndefValue>(Elt))
6872 continue;
6873
6874 // Bail out if we can't determine if this constant is min/max or if we
6875 // know that this constant is min/max.
6876 auto *CI = dyn_cast<ConstantInt>(Elt);
6877 if (!CI || !ConstantIsOk(CI))
6878 return std::nullopt;
6879
6880 if (!SafeReplacementConstant)
6881 SafeReplacementConstant = CI;
6882 }
6883 } else if (isa<VectorType>(C->getType())) {
6884 // Handle scalable splat
6885 Value *SplatC = C->getSplatValue();
6886 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
6887 // Bail out if the constant can't be safely incremented/decremented.
6888 if (!CI || !ConstantIsOk(CI))
6889 return std::nullopt;
6890 } else {
6891 // ConstantExpr?
6892 return std::nullopt;
6893 }
6894
6895 // It may not be safe to change a compare predicate in the presence of
6896 // undefined elements, so replace those elements with the first safe constant
6897 // that we found.
6898 // TODO: in case of poison, it is safe; let's replace undefs only.
6899 if (C->containsUndefOrPoisonElement()) {
6900 assert(SafeReplacementConstant && "Replacement constant not set");
6901 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
6902 }
6903
6905
6906 // Increment or decrement the constant.
6907 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
6908 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
6909
6910 return std::make_pair(NewPred, NewC);
6911}
6912
6913/// If we have an icmp le or icmp ge instruction with a constant operand, turn
6914/// it into the appropriate icmp lt or icmp gt instruction. This transform
6915/// allows them to be folded in visitICmpInst.
6917 ICmpInst::Predicate Pred = I.getPredicate();
6918 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
6920 return nullptr;
6921
6922 Value *Op0 = I.getOperand(0);
6923 Value *Op1 = I.getOperand(1);
6924 auto *Op1C = dyn_cast<Constant>(Op1);
6925 if (!Op1C)
6926 return nullptr;
6927
6928 auto FlippedStrictness =
6930 if (!FlippedStrictness)
6931 return nullptr;
6932
6933 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
6934}
6935
6936/// If we have a comparison with a non-canonical predicate, if we can update
6937/// all the users, invert the predicate and adjust all the users.
6939 // Is the predicate already canonical?
6940 CmpInst::Predicate Pred = I.getPredicate();
6942 return nullptr;
6943
6944 // Can all users be adjusted to predicate inversion?
6945 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
6946 return nullptr;
6947
6948 // Ok, we can canonicalize comparison!
6949 // Let's first invert the comparison's predicate.
6950 I.setPredicate(CmpInst::getInversePredicate(Pred));
6951 I.setName(I.getName() + ".not");
6952
6953 // And, adapt users.
6955
6956 return &I;
6957}
6958
6959/// Integer compare with boolean values can always be turned into bitwise ops.
6961 InstCombiner::BuilderTy &Builder) {
6962 Value *A = I.getOperand(0), *B = I.getOperand(1);
6963 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
6964
6965 // A boolean compared to true/false can be simplified to Op0/true/false in
6966 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
6967 // Cases not handled by InstSimplify are always 'not' of Op0.
6968 if (match(B, m_Zero())) {
6969 switch (I.getPredicate()) {
6970 case CmpInst::ICMP_EQ: // A == 0 -> !A
6971 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
6972 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
6974 default:
6975 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6976 }
6977 } else if (match(B, m_One())) {
6978 switch (I.getPredicate()) {
6979 case CmpInst::ICMP_NE: // A != 1 -> !A
6980 case CmpInst::ICMP_ULT: // A <u 1 -> !A
6981 case CmpInst::ICMP_SGT: // A >s -1 -> !A
6983 default:
6984 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6985 }
6986 }
6987
6988 switch (I.getPredicate()) {
6989 default:
6990 llvm_unreachable("Invalid icmp instruction!");
6991 case ICmpInst::ICMP_EQ:
6992 // icmp eq i1 A, B -> ~(A ^ B)
6993 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
6994
6995 case ICmpInst::ICMP_NE:
6996 // icmp ne i1 A, B -> A ^ B
6997 return BinaryOperator::CreateXor(A, B);
6998
6999 case ICmpInst::ICMP_UGT:
7000 // icmp ugt -> icmp ult
7001 std::swap(A, B);
7002 [[fallthrough]];
7003 case ICmpInst::ICMP_ULT:
7004 // icmp ult i1 A, B -> ~A & B
7005 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
7006
7007 case ICmpInst::ICMP_SGT:
7008 // icmp sgt -> icmp slt
7009 std::swap(A, B);
7010 [[fallthrough]];
7011 case ICmpInst::ICMP_SLT:
7012 // icmp slt i1 A, B -> A & ~B
7013 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
7014
7015 case ICmpInst::ICMP_UGE:
7016 // icmp uge -> icmp ule
7017 std::swap(A, B);
7018 [[fallthrough]];
7019 case ICmpInst::ICMP_ULE:
7020 // icmp ule i1 A, B -> ~A | B
7021 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
7022
7023 case ICmpInst::ICMP_SGE:
7024 // icmp sge -> icmp sle
7025 std::swap(A, B);
7026 [[fallthrough]];
7027 case ICmpInst::ICMP_SLE:
7028 // icmp sle i1 A, B -> A | ~B
7029 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
7030 }
7031}
7032
7033// Transform pattern like:
7034// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7035// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7036// Into:
7037// (X l>> Y) != 0
7038// (X l>> Y) == 0
7040 InstCombiner::BuilderTy &Builder) {
7041 ICmpInst::Predicate Pred, NewPred;
7042 Value *X, *Y;
7043 if (match(&Cmp,
7044 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
7045 switch (Pred) {
7046 case ICmpInst::ICMP_ULE:
7047 NewPred = ICmpInst::ICMP_NE;
7048 break;
7049 case ICmpInst::ICMP_UGT:
7050 NewPred = ICmpInst::ICMP_EQ;
7051 break;
7052 default:
7053 return nullptr;
7054 }
7055 } else if (match(&Cmp, m_c_ICmp(Pred,
7058 m_Add(m_Shl(m_One(), m_Value(Y)),
7059 m_AllOnes()))),
7060 m_Value(X)))) {
7061 // The variant with 'add' is not canonical, (the variant with 'not' is)
7062 // we only get it because it has extra uses, and can't be canonicalized,
7063
7064 switch (Pred) {
7065 case ICmpInst::ICMP_ULT:
7066 NewPred = ICmpInst::ICMP_NE;
7067 break;
7068 case ICmpInst::ICMP_UGE:
7069 NewPred = ICmpInst::ICMP_EQ;
7070 break;
7071 default:
7072 return nullptr;
7073 }
7074 } else
7075 return nullptr;
7076
7077 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
7078 Constant *Zero = Constant::getNullValue(NewX->getType());
7079 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
7080}
7081
7083 InstCombiner::BuilderTy &Builder) {
7084 const CmpInst::Predicate Pred = Cmp.getPredicate();
7085 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
7086 Value *V1, *V2;
7087
7088 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7089 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
7090 if (auto *I = dyn_cast<Instruction>(V))
7091 I->copyIRFlags(&Cmp);
7092 Module *M = Cmp.getModule();
7093 Function *F =
7094 Intrinsic::getDeclaration(M, Intrinsic::vector_reverse, V->getType());
7095 return CallInst::Create(F, V);
7096 };
7097
7098 if (match(LHS, m_VecReverse(m_Value(V1)))) {
7099 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7100 if (match(RHS, m_VecReverse(m_Value(V2))) &&
7101 (LHS->hasOneUse() || RHS->hasOneUse()))
7102 return createCmpReverse(Pred, V1, V2);
7103
7104 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7105 if (LHS->hasOneUse() && isSplatValue(RHS))
7106 return createCmpReverse(Pred, V1, RHS);
7107 }
7108 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7109 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
7110 return createCmpReverse(Pred, LHS, V2);
7111
7112 ArrayRef<int> M;
7113 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
7114 return nullptr;
7115
7116 // If both arguments of the cmp are shuffles that use the same mask and
7117 // shuffle within a single vector, move the shuffle after the cmp:
7118 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7119 Type *V1Ty = V1->getType();
7120 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
7121 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7122 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
7123 return new ShuffleVectorInst(NewCmp, M);
7124 }
7125
7126 // Try to canonicalize compare with splatted operand and splat constant.
7127 // TODO: We could generalize this for more than splats. See/use the code in
7128 // InstCombiner::foldVectorBinop().
7129 Constant *C;
7130 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
7131 return nullptr;
7132
7133 // Length-changing splats are ok, so adjust the constants as needed:
7134 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7135 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7136 int MaskSplatIndex;
7137 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
7138 // We allow poison in matching, but this transform removes it for safety.
7139 // Demanded elements analysis should be able to recover some/all of that.
7140 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
7141 ScalarC);
7142 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7143 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
7144 return new ShuffleVectorInst(NewCmp, NewM);
7145 }
7146
7147 return nullptr;
7148}
7149
7150// extract(uadd.with.overflow(A, B), 0) ult A
7151// -> extract(uadd.with.overflow(A, B), 1)
7153 CmpInst::Predicate Pred = I.getPredicate();
7154 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7155
7156 Value *UAddOv;
7157 Value *A, *B;
7158 auto UAddOvResultPat = m_ExtractValue<0>(
7159 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));
7160 if (match(Op0, UAddOvResultPat) &&
7161 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7162 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
7163 (match(A, m_One()) || match(B, m_One()))) ||
7164 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
7165 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
7166 // extract(uadd.with.overflow(A, B), 0) < A
7167 // extract(uadd.with.overflow(A, 1), 0) == 0
7168 // extract(uadd.with.overflow(A, -1), 0) != -1
7169 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
7170 else if (match(Op1, UAddOvResultPat) &&
7171 Pred == ICmpInst::ICMP_UGT && (Op0 == A || Op0 == B))
7172 // A > extract(uadd.with.overflow(A, B), 0)
7173 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
7174 else
7175 return nullptr;
7176
7177 return ExtractValueInst::Create(UAddOv, 1);
7178}
7179
7181 if (!I.getOperand(0)->getType()->isPointerTy() ||
7183 I.getParent()->getParent(),
7184 I.getOperand(0)->getType()->getPointerAddressSpace())) {
7185 return nullptr;
7186 }
7187 Instruction *Op;
7188 if (match(I.getOperand(0), m_Instruction(Op)) &&
7189 match(I.getOperand(1), m_Zero()) &&
7190 Op->isLaunderOrStripInvariantGroup()) {
7191 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7192 Op->getOperand(0), I.getOperand(1));
7193 }
7194 return nullptr;
7195}
7196
7197/// This function folds patterns produced by lowering of reduce idioms, such as
7198/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7199/// attempts to generate fewer number of scalar comparisons instead of vector
7200/// comparisons when possible.
7202 InstCombiner::BuilderTy &Builder,
7203 const DataLayout &DL) {
7204 if (I.getType()->isVectorTy())
7205 return nullptr;
7206 ICmpInst::Predicate OuterPred, InnerPred;
7207 Value *LHS, *RHS;
7208
7209 // Match lowering of @llvm.vector.reduce.and. Turn
7210 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7211 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7212 /// %res = icmp <pred> i8 %scalar_ne, 0
7213 ///
7214 /// into
7215 ///
7216 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7217 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7218 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7219 ///
7220 /// for <pred> in {ne, eq}.
7221 if (!match(&I, m_ICmp(OuterPred,
7223 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7224 m_Zero())))
7225 return nullptr;
7226 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7227 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7228 return nullptr;
7229 unsigned NumBits =
7230 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7231 // TODO: Relax this to "not wider than max legal integer type"?
7232 if (!DL.isLegalInteger(NumBits))
7233 return nullptr;
7234
7235 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7236 auto *ScalarTy = Builder.getIntNTy(NumBits);
7237 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7238 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7239 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7240 I.getName());
7241 }
7242
7243 return nullptr;
7244}
7245
7246// This helper will be called with icmp operands in both orders.
7248 Value *Op0, Value *Op1,
7249 ICmpInst &CxtI) {
7250 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7251 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7252 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7253 return NI;
7254
7255 if (auto *SI = dyn_cast<SelectInst>(Op0))
7256 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7257 return NI;
7258
7259 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0))
7260 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7261 return Res;
7262
7263 {
7264 Value *X;
7265 const APInt *C;
7266 // icmp X+Cst, X
7267 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7268 return foldICmpAddOpConst(X, *C, Pred);
7269 }
7270
7271 // abs(X) >= X --> true
7272 // abs(X) u<= X --> true
7273 // abs(X) < X --> false
7274 // abs(X) u> X --> false
7275 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7276 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7277 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7278 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7279 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7280 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7281 {
7282 Value *X;
7283 Constant *C;
7284 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
7285 match(Op1, m_Specific(X))) {
7286 Value *NullValue = Constant::getNullValue(X->getType());
7287 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7288 const APInt SMin =
7289 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7290 bool IsIntMinPosion = C->isAllOnesValue();
7291 switch (Pred) {
7292 case CmpInst::ICMP_ULE:
7293 case CmpInst::ICMP_SGE:
7294 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7295 case CmpInst::ICMP_UGT:
7296 case CmpInst::ICMP_SLT:
7298 case CmpInst::ICMP_UGE:
7299 case CmpInst::ICMP_SLE:
7300 case CmpInst::ICMP_EQ: {
7301 return replaceInstUsesWith(
7302 CxtI, IsIntMinPosion
7303 ? Builder.CreateICmpSGT(X, AllOnesValue)
7305 X, ConstantInt::get(X->getType(), SMin + 1)));
7306 }
7307 case CmpInst::ICMP_ULT:
7308 case CmpInst::ICMP_SGT:
7309 case CmpInst::ICMP_NE: {
7310 return replaceInstUsesWith(
7311 CxtI, IsIntMinPosion
7312 ? Builder.CreateICmpSLT(X, NullValue)
7314 X, ConstantInt::get(X->getType(), SMin)));
7315 }
7316 default:
7317 llvm_unreachable("Invalid predicate!");
7318 }
7319 }
7320 }
7321
7322 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7323 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7324 return replaceInstUsesWith(CxtI, V);
7325
7326 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7327 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7328 {
7329 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7330 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7332 }
7333
7334 if (!ICmpInst::isUnsigned(Pred) &&
7335 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7336 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7338 }
7339 }
7340
7341 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7342 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7343 {
7344 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7345 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7347 }
7348
7349 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7350 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7351 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7353 }
7354 }
7355
7356 return nullptr;
7357}
7358
7360 bool Changed = false;
7362 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7363 unsigned Op0Cplxity = getComplexity(Op0);
7364 unsigned Op1Cplxity = getComplexity(Op1);
7365
7366 /// Orders the operands of the compare so that they are listed from most
7367 /// complex to least complex. This puts constants before unary operators,
7368 /// before binary operators.
7369 if (Op0Cplxity < Op1Cplxity) {
7370 I.swapOperands();
7371 std::swap(Op0, Op1);
7372 Changed = true;
7373 }
7374
7375 if (Value *V = simplifyICmpInst(I.getPredicate(), Op0, Op1, Q))
7376 return replaceInstUsesWith(I, V);
7377
7378 // Comparing -val or val with non-zero is the same as just comparing val
7379 // ie, abs(val) != 0 -> val != 0
7380 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7381 Value *Cond, *SelectTrue, *SelectFalse;
7382 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7383 m_Value(SelectFalse)))) {
7384 if (Value *V = dyn_castNegVal(SelectTrue)) {
7385 if (V == SelectFalse)
7386 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7387 }
7388 else if (Value *V = dyn_castNegVal(SelectFalse)) {
7389 if (V == SelectTrue)
7390 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7391 }
7392 }
7393 }
7394
7395 if (Op0->getType()->isIntOrIntVectorTy(1))
7397 return Res;
7398
7400 return Res;
7401
7403 return Res;
7404
7406 return Res;
7407
7409 return Res;
7410
7412 return Res;
7413
7415 return Res;
7416
7418 return Res;
7419
7420 // Test if the ICmpInst instruction is used exclusively by a select as
7421 // part of a minimum or maximum operation. If so, refrain from doing
7422 // any other folding. This helps out other analyses which understand
7423 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7424 // and CodeGen. And in this case, at least one of the comparison
7425 // operands has at least one user besides the compare (the select),
7426 // which would often largely negate the benefit of folding anyway.
7427 //
7428 // Do the same for the other patterns recognized by matchSelectPattern.
7429 if (I.hasOneUse())
7430 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7431 Value *A, *B;
7433 if (SPR.Flavor != SPF_UNKNOWN)
7434 return nullptr;
7435 }
7436
7437 // Do this after checking for min/max to prevent infinite looping.
7438 if (Instruction *Res = foldICmpWithZero(I))
7439 return Res;
7440
7441 // FIXME: We only do this after checking for min/max to prevent infinite
7442 // looping caused by a reverse canonicalization of these patterns for min/max.
7443 // FIXME: The organization of folds is a mess. These would naturally go into
7444 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7445 // down here after the min/max restriction.
7446 ICmpInst::Predicate Pred = I.getPredicate();
7447 const APInt *C;
7448 if (match(Op1, m_APInt(C))) {
7449 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7450 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7451 Constant *Zero = Constant::getNullValue(Op0->getType());
7452 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7453 }
7454
7455 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7456 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7458 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7459 }
7460 }
7461
7462 // The folds in here may rely on wrapping flags and special constants, so
7463 // they can break up min/max idioms in some cases but not seemingly similar
7464 // patterns.
7465 // FIXME: It may be possible to enhance select folding to make this
7466 // unnecessary. It may also be moot if we canonicalize to min/max
7467 // intrinsics.
7468 if (Instruction *Res = foldICmpBinOp(I, Q))
7469 return Res;
7470
7472 return Res;
7473
7474 // Try to match comparison as a sign bit test. Intentionally do this after
7475 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7476 if (Instruction *New = foldSignBitTest(I))
7477 return New;
7478
7480 return Res;
7481
7482 if (Instruction *Res = foldICmpCommutative(I.getPredicate(), Op0, Op1, I))
7483 return Res;
7484 if (Instruction *Res =
7485 foldICmpCommutative(I.getSwappedPredicate(), Op1, Op0, I))
7486 return Res;
7487
7488 if (I.isCommutative()) {
7489 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7490 replaceOperand(I, 0, Pair->first);
7491 replaceOperand(I, 1, Pair->second);
7492 return &I;
7493 }
7494 }
7495
7496 // In case of a comparison with two select instructions having the same
7497 // condition, check whether one of the resulting branches can be simplified.
7498 // If so, just compare the other branch and select the appropriate result.
7499 // For example:
7500 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7501 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7502 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7503 // The icmp will result false for the false value of selects and the result
7504 // will depend upon the comparison of true values of selects if %cmp is
7505 // true. Thus, transform this into:
7506 // %cmp = icmp slt i32 %y, %z
7507 // %sel = select i1 %cond, i1 %cmp, i1 false
7508 // This handles similar cases to transform.
7509 {
7510 Value *Cond, *A, *B, *C, *D;
7511 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7513 (Op0->hasOneUse() || Op1->hasOneUse())) {
7514 // Check whether comparison of TrueValues can be simplified
7515 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7516 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7517 return SelectInst::Create(Cond, Res, NewICMP);
7518 }
7519 // Check whether comparison of FalseValues can be simplified
7520 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7521 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7522 return SelectInst::Create(Cond, NewICMP, Res);
7523 }
7524 }
7525 }
7526
7527 // Try to optimize equality comparisons against alloca-based pointers.
7528 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7529 assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?");
7530 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7531 if (foldAllocaCmp(Alloca))
7532 return nullptr;
7533 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7534 if (foldAllocaCmp(Alloca))
7535 return nullptr;
7536 }
7537
7538 if (Instruction *Res = foldICmpBitCast(I))
7539 return Res;
7540
7541 // TODO: Hoist this above the min/max bailout.
7543 return R;
7544
7545 {
7546 Value *X, *Y;
7547 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7548 // and (X & ~Y) != 0 --> (X & Y) == 0
7549 // if A is a power of 2.
7550 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7551 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, 0, &I) &&
7552 I.isEquality())
7553 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7554 Op1);
7555
7556 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7557 if (Op0->getType()->isIntOrIntVectorTy()) {
7558 bool ConsumesOp0, ConsumesOp1;
7559 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7560 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7561 (ConsumesOp0 || ConsumesOp1)) {
7562 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7563 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7564 assert(InvOp0 && InvOp1 &&
7565 "Mismatch between isFreeToInvert and getFreelyInverted");
7566 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7567 }
7568 }
7569
7570 Instruction *AddI = nullptr;
7572 m_Instruction(AddI))) &&
7573 isa<IntegerType>(X->getType())) {
7574 Value *Result;
7575 Constant *Overflow;
7576 // m_UAddWithOverflow can match patterns that do not include an explicit
7577 // "add" instruction, so check the opcode of the matched op.
7578 if (AddI->getOpcode() == Instruction::Add &&
7579 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7580 Result, Overflow)) {
7581 replaceInstUsesWith(*AddI, Result);
7582 eraseInstFromFunction(*AddI);
7583 return replaceInstUsesWith(I, Overflow);
7584 }
7585 }
7586
7587 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7588 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7589 match(Op1, m_APInt(C))) {
7590 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7591 return R;
7592 }
7593
7594 // Signbit test folds
7595 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7596 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7597 Instruction *ExtI;
7598 if ((I.isUnsigned() || I.isEquality()) &&
7599 match(Op1,
7601 Y->getType()->getScalarSizeInBits() == 1 &&
7602 (Op0->hasOneUse() || Op1->hasOneUse())) {
7603 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7604 Instruction *ShiftI;
7605 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7607 OpWidth - 1))))) {
7608 unsigned ExtOpc = ExtI->getOpcode();
7609 unsigned ShiftOpc = ShiftI->getOpcode();
7610 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7611 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7612 Value *SLTZero =
7614 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7615 return replaceInstUsesWith(I, Cmp);
7616 }
7617 }
7618 }
7619 }
7620
7621 if (Instruction *Res = foldICmpEquality(I))
7622 return Res;
7623
7625 return Res;
7626
7627 if (Instruction *Res = foldICmpOfUAddOv(I))
7628 return Res;
7629
7630 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7631 // an i1 which indicates whether or not we successfully did the swap.
7632 //
7633 // Replace comparisons between the old value and the expected value with the
7634 // indicator that 'cmpxchg' returns.
7635 //
7636 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7637 // spuriously fail. In those cases, the old value may equal the expected
7638 // value but it is possible for the swap to not occur.
7639 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7640 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7641 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7642 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7643 !ACXI->isWeak())
7644 return ExtractValueInst::Create(ACXI, 1);
7645
7647 return Res;
7648
7649 if (I.getType()->isVectorTy())
7650 if (Instruction *Res = foldVectorCmp(I, Builder))
7651 return Res;
7652
7654 return Res;
7655
7657 return Res;
7658
7659 return Changed ? &I : nullptr;
7660}
7661
7662/// Fold fcmp ([us]itofp x, cst) if possible.
7664 Instruction *LHSI,
7665 Constant *RHSC) {
7666 const APFloat *RHS;
7667 if (!match(RHSC, m_APFloat(RHS)))
7668 return nullptr;
7669
7670 // Get the width of the mantissa. We don't want to hack on conversions that
7671 // might lose information from the integer, e.g. "i64 -> float"
7672 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7673 if (MantissaWidth == -1) return nullptr; // Unknown.
7674
7675 Type *IntTy = LHSI->getOperand(0)->getType();
7676 unsigned IntWidth = IntTy->getScalarSizeInBits();
7677 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7678
7679 if (I.isEquality()) {
7680 FCmpInst::Predicate P = I.getPredicate();
7681 bool IsExact = false;
7682 APSInt RHSCvt(IntWidth, LHSUnsigned);
7683 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7684
7685 // If the floating point constant isn't an integer value, we know if we will
7686 // ever compare equal / not equal to it.
7687 if (!IsExact) {
7688 // TODO: Can never be -0.0 and other non-representable values
7689 APFloat RHSRoundInt(*RHS);
7691 if (*RHS != RHSRoundInt) {
7693 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7694
7696 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7697 }
7698 }
7699
7700 // TODO: If the constant is exactly representable, is it always OK to do
7701 // equality compares as integer?
7702 }
7703
7704 // Check to see that the input is converted from an integer type that is small
7705 // enough that preserves all bits. TODO: check here for "known" sign bits.
7706 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
7707
7708 // Following test does NOT adjust IntWidth downwards for signed inputs,
7709 // because the most negative value still requires all the mantissa bits
7710 // to distinguish it from one less than that value.
7711 if ((int)IntWidth > MantissaWidth) {
7712 // Conversion would lose accuracy. Check if loss can impact comparison.
7713 int Exp = ilogb(*RHS);
7714 if (Exp == APFloat::IEK_Inf) {
7715 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
7716 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
7717 // Conversion could create infinity.
7718 return nullptr;
7719 } else {
7720 // Note that if RHS is zero or NaN, then Exp is negative
7721 // and first condition is trivially false.
7722 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
7723 // Conversion could affect comparison.
7724 return nullptr;
7725 }
7726 }
7727
7728 // Otherwise, we can potentially simplify the comparison. We know that it
7729 // will always come through as an integer value and we know the constant is
7730 // not a NAN (it would have been previously simplified).
7731 assert(!RHS->isNaN() && "NaN comparison not already folded!");
7732
7734 switch (I.getPredicate()) {
7735 default: llvm_unreachable("Unexpected predicate!");
7736 case FCmpInst::FCMP_UEQ:
7737 case FCmpInst::FCMP_OEQ:
7738 Pred = ICmpInst::ICMP_EQ;
7739 break;
7740 case FCmpInst::FCMP_UGT:
7741 case FCmpInst::FCMP_OGT:
7742 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
7743 break;
7744 case FCmpInst::FCMP_UGE:
7745 case FCmpInst::FCMP_OGE:
7746 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
7747 break;
7748 case FCmpInst::FCMP_ULT:
7749 case FCmpInst::FCMP_OLT:
7750 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
7751 break;
7752 case FCmpInst::FCMP_ULE:
7753 case FCmpInst::FCMP_OLE:
7754 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
7755 break;
7756 case FCmpInst::FCMP_UNE:
7757 case FCmpInst::FCMP_ONE:
7758 Pred = ICmpInst::ICMP_NE;
7759 break;
7760 case FCmpInst::FCMP_ORD:
7761 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7762 case FCmpInst::FCMP_UNO:
7763 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7764 }
7765
7766 // Now we know that the APFloat is a normal number, zero or inf.
7767
7768 // See if the FP constant is too large for the integer. For example,
7769 // comparing an i8 to 300.0.
7770 if (!LHSUnsigned) {
7771 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
7772 // and large values.
7773 APFloat SMax(RHS->getSemantics());
7774 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
7776 if (SMax < *RHS) { // smax < 13123.0
7777 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
7778 Pred == ICmpInst::ICMP_SLE)
7779 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7780 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7781 }
7782 } else {
7783 // If the RHS value is > UnsignedMax, fold the comparison. This handles
7784 // +INF and large values.
7785 APFloat UMax(RHS->getSemantics());
7786 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
7788 if (UMax < *RHS) { // umax < 13123.0
7789 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
7790 Pred == ICmpInst::ICMP_ULE)
7791 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7792 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7793 }
7794 }
7795
7796 if (!LHSUnsigned) {
7797 // See if the RHS value is < SignedMin.
7798 APFloat SMin(RHS->getSemantics());
7799 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
7801 if (SMin > *RHS) { // smin > 12312.0
7802 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
7803 Pred == ICmpInst::ICMP_SGE)
7804 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7805 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7806 }
7807 } else {
7808 // See if the RHS value is < UnsignedMin.
7809 APFloat UMin(RHS->getSemantics());
7810 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
7812 if (UMin > *RHS) { // umin > 12312.0
7813 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
7814 Pred == ICmpInst::ICMP_UGE)
7815 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7816 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7817 }
7818 }
7819
7820 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
7821 // [0, UMAX], but it may still be fractional. Check whether this is the case
7822 // using the IsExact flag.
7823 // Don't do this for zero, because -0.0 is not fractional.
7824 APSInt RHSInt(IntWidth, LHSUnsigned);
7825 bool IsExact;
7826 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
7827 if (!RHS->isZero()) {
7828 if (!IsExact) {
7829 // If we had a comparison against a fractional value, we have to adjust
7830 // the compare predicate and sometimes the value. RHSC is rounded towards
7831 // zero at this point.
7832 switch (Pred) {
7833 default: llvm_unreachable("Unexpected integer comparison!");
7834 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
7835 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7836 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
7837 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7838 case ICmpInst::ICMP_ULE:
7839 // (float)int <= 4.4 --> int <= 4
7840 // (float)int <= -4.4 --> false
7841 if (RHS->isNegative())
7842 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7843 break;
7844 case ICmpInst::ICMP_SLE:
7845 // (float)int <= 4.4 --> int <= 4
7846 // (float)int <= -4.4 --> int < -4
7847 if (RHS->isNegative())
7848 Pred = ICmpInst::ICMP_SLT;
7849 break;
7850 case ICmpInst::ICMP_ULT:
7851 // (float)int < -4.4 --> false
7852 // (float)int < 4.4 --> int <= 4
7853 if (RHS->isNegative())
7854 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7855 Pred = ICmpInst::ICMP_ULE;
7856 break;
7857 case ICmpInst::ICMP_SLT:
7858 // (float)int < -4.4 --> int < -4
7859 // (float)int < 4.4 --> int <= 4
7860 if (!RHS->isNegative())
7861 Pred = ICmpInst::ICMP_SLE;
7862 break;
7863 case ICmpInst::ICMP_UGT:
7864 // (float)int > 4.4 --> int > 4
7865 // (float)int > -4.4 --> true
7866 if (RHS->isNegative())
7867 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7868 break;
7869 case ICmpInst::ICMP_SGT:
7870 // (float)int > 4.4 --> int > 4
7871 // (float)int > -4.4 --> int >= -4
7872 if (RHS->isNegative())
7873 Pred = ICmpInst::ICMP_SGE;
7874 break;
7875 case ICmpInst::ICMP_UGE:
7876 // (float)int >= -4.4 --> true
7877 // (float)int >= 4.4 --> int > 4
7878 if (RHS->isNegative())
7879 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7880 Pred = ICmpInst::ICMP_UGT;
7881 break;
7882 case ICmpInst::ICMP_SGE:
7883 // (float)int >= -4.4 --> int >= -4
7884 // (float)int >= 4.4 --> int > 4
7885 if (!RHS->isNegative())
7886 Pred = ICmpInst::ICMP_SGT;
7887 break;
7888 }
7889 }
7890 }
7891
7892 // Lower this FP comparison into an appropriate integer version of the
7893 // comparison.
7894 return new ICmpInst(Pred, LHSI->getOperand(0),
7895 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
7896}
7897
7898/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
7900 Constant *RHSC) {
7901 // When C is not 0.0 and infinities are not allowed:
7902 // (C / X) < 0.0 is a sign-bit test of X
7903 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
7904 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
7905 //
7906 // Proof:
7907 // Multiply (C / X) < 0.0 by X * X / C.
7908 // - X is non zero, if it is the flag 'ninf' is violated.
7909 // - C defines the sign of X * X * C. Thus it also defines whether to swap
7910 // the predicate. C is also non zero by definition.
7911 //
7912 // Thus X * X / C is non zero and the transformation is valid. [qed]
7913
7914 FCmpInst::Predicate Pred = I.getPredicate();
7915
7916 // Check that predicates are valid.
7917 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
7918 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
7919 return nullptr;
7920
7921 // Check that RHS operand is zero.
7922 if (!match(RHSC, m_AnyZeroFP()))
7923 return nullptr;
7924
7925 // Check fastmath flags ('ninf').
7926 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
7927 return nullptr;
7928
7929 // Check the properties of the dividend. It must not be zero to avoid a
7930 // division by zero (see Proof).
7931 const APFloat *C;
7932 if (!match(LHSI->getOperand(0), m_APFloat(C)))
7933 return nullptr;
7934
7935 if (C->isZero())
7936 return nullptr;
7937
7938 // Get swapped predicate if necessary.
7939 if (C->isNegative())
7940 Pred = I.getSwappedPredicate();
7941
7942 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
7943}
7944
7945/// Optimize fabs(X) compared with zero.
7947 Value *X;
7948 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
7949 return nullptr;
7950
7951 const APFloat *C;
7952 if (!match(I.getOperand(1), m_APFloat(C)))
7953 return nullptr;
7954
7955 if (!C->isPosZero()) {
7956 if (!C->isSmallestNormalized())
7957 return nullptr;
7958
7959 const Function *F = I.getFunction();
7960 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
7961 if (Mode.Input == DenormalMode::PreserveSign ||
7962 Mode.Input == DenormalMode::PositiveZero) {
7963
7964 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7965 Constant *Zero = ConstantFP::getZero(X->getType());
7966 return new FCmpInst(P, X, Zero, "", I);
7967 };
7968
7969 switch (I.getPredicate()) {
7970 case FCmpInst::FCMP_OLT:
7971 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
7972 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
7973 case FCmpInst::FCMP_UGE:
7974 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
7975 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
7976 case FCmpInst::FCMP_OGE:
7977 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
7978 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
7979 case FCmpInst::FCMP_ULT:
7980 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
7981 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
7982 default:
7983 break;
7984 }
7985 }
7986
7987 return nullptr;
7988 }
7989
7990 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7991 I->setPredicate(P);
7992 return IC.replaceOperand(*I, 0, X);
7993 };
7994
7995 switch (I.getPredicate()) {
7996 case FCmpInst::FCMP_UGE:
7997 case FCmpInst::FCMP_OLT:
7998 // fabs(X) >= 0.0 --> true
7999 // fabs(X) < 0.0 --> false
8000 llvm_unreachable("fcmp should have simplified");
8001
8002 case FCmpInst::FCMP_OGT:
8003 // fabs(X) > 0.0 --> X != 0.0
8004 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8005
8006 case FCmpInst::FCMP_UGT:
8007 // fabs(X) u> 0.0 --> X u!= 0.0
8008 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8009
8010 case FCmpInst::FCMP_OLE:
8011 // fabs(X) <= 0.0 --> X == 0.0
8012 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8013
8014 case FCmpInst::FCMP_ULE:
8015 // fabs(X) u<= 0.0 --> X u== 0.0
8016 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8017
8018 case FCmpInst::FCMP_OGE:
8019 // fabs(X) >= 0.0 --> !isnan(X)
8020 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8021 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8022
8023 case FCmpInst::FCMP_ULT:
8024 // fabs(X) u< 0.0 --> isnan(X)
8025 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8026 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8027
8028 case FCmpInst::FCMP_OEQ:
8029 case FCmpInst::FCMP_UEQ:
8030 case FCmpInst::FCMP_ONE:
8031 case FCmpInst::FCMP_UNE:
8032 case FCmpInst::FCMP_ORD:
8033 case FCmpInst::FCMP_UNO:
8034 // Look through the fabs() because it doesn't change anything but the sign.
8035 // fabs(X) == 0.0 --> X == 0.0,
8036 // fabs(X) != 0.0 --> X != 0.0
8037 // isnan(fabs(X)) --> isnan(X)
8038 // !isnan(fabs(X) --> !isnan(X)
8039 return replacePredAndOp0(&I, I.getPredicate(), X);
8040
8041 default:
8042 return nullptr;
8043 }
8044}
8045
8046/// Optimize sqrt(X) compared with zero.
8048 Value *X;
8049 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))
8050 return nullptr;
8051
8052 if (!match(I.getOperand(1), m_PosZeroFP()))
8053 return nullptr;
8054
8055 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8056 I.setPredicate(P);
8057 return IC.replaceOperand(I, 0, X);
8058 };
8059
8060 // Clear ninf flag if sqrt doesn't have it.
8061 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())
8062 I.setHasNoInfs(false);
8063
8064 switch (I.getPredicate()) {
8065 case FCmpInst::FCMP_OLT:
8066 case FCmpInst::FCMP_UGE:
8067 // sqrt(X) < 0.0 --> false
8068 // sqrt(X) u>= 0.0 --> true
8069 llvm_unreachable("fcmp should have simplified");
8070 case FCmpInst::FCMP_ULT:
8071 case FCmpInst::FCMP_ULE:
8072 case FCmpInst::FCMP_OGT:
8073 case FCmpInst::FCMP_OGE:
8074 case FCmpInst::FCMP_OEQ:
8075 case FCmpInst::FCMP_UNE:
8076 // sqrt(X) u< 0.0 --> X u< 0.0
8077 // sqrt(X) u<= 0.0 --> X u<= 0.0
8078 // sqrt(X) > 0.0 --> X > 0.0
8079 // sqrt(X) >= 0.0 --> X >= 0.0
8080 // sqrt(X) == 0.0 --> X == 0.0
8081 // sqrt(X) u!= 0.0 --> X u!= 0.0
8082 return IC.replaceOperand(I, 0, X);
8083
8084 case FCmpInst::FCMP_OLE:
8085 // sqrt(X) <= 0.0 --> X == 0.0
8086 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8087 case FCmpInst::FCMP_UGT:
8088 // sqrt(X) u> 0.0 --> X u!= 0.0
8089 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8090 case FCmpInst::FCMP_UEQ:
8091 // sqrt(X) u== 0.0 --> X u<= 0.0
8092 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8093 case FCmpInst::FCMP_ONE:
8094 // sqrt(X) != 0.0 --> X > 0.0
8095 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8096 case FCmpInst::FCMP_ORD:
8097 // !isnan(sqrt(X)) --> X >= 0.0
8098 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8099 case FCmpInst::FCMP_UNO:
8100 // isnan(sqrt(X)) --> X u< 0.0
8101 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8102 default:
8103 llvm_unreachable("Unexpected predicate!");
8104 }
8105}
8106
8108 CmpInst::Predicate Pred = I.getPredicate();
8109 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8110
8111 // Canonicalize fneg as Op1.
8112 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
8113 std::swap(Op0, Op1);
8114 Pred = I.getSwappedPredicate();
8115 }
8116
8117 if (!match(Op1, m_FNeg(m_Specific(Op0))))
8118 return nullptr;
8119
8120 // Replace the negated operand with 0.0:
8121 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8122 Constant *Zero = ConstantFP::getZero(Op0->getType());
8123 return new FCmpInst(Pred, Op0, Zero, "", &I);
8124}
8125
8127 Constant *RHSC, InstCombinerImpl &CI) {
8128 const CmpInst::Predicate Pred = I.getPredicate();
8129 Value *X = LHSI->getOperand(0);
8130 Value *Y = LHSI->getOperand(1);
8131 switch (Pred) {
8132 default:
8133 break;
8134 case FCmpInst::FCMP_UGT:
8135 case FCmpInst::FCMP_ULT:
8136 case FCmpInst::FCMP_UNE:
8137 case FCmpInst::FCMP_OEQ:
8138 case FCmpInst::FCMP_OGE:
8139 case FCmpInst::FCMP_OLE:
8140 // The optimization is not valid if X and Y are infinities of the same
8141 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8142 // flag then we can assume we do not have that case. Otherwise we might be
8143 // able to prove that either X or Y is not infinity.
8144 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8145 !isKnownNeverInfinity(Y, /*Depth=*/0,
8147 !isKnownNeverInfinity(X, /*Depth=*/0,
8149 break;
8150
8151 [[fallthrough]];
8152 case FCmpInst::FCMP_OGT:
8153 case FCmpInst::FCMP_OLT:
8154 case FCmpInst::FCMP_ONE:
8155 case FCmpInst::FCMP_UEQ:
8156 case FCmpInst::FCMP_UGE:
8157 case FCmpInst::FCMP_ULE:
8158 // fcmp pred (x - y), 0 --> fcmp pred x, y
8159 if (match(RHSC, m_AnyZeroFP()) &&
8160 I.getFunction()->getDenormalMode(
8161 LHSI->getType()->getScalarType()->getFltSemantics()) ==
8163 CI.replaceOperand(I, 0, X);
8164 CI.replaceOperand(I, 1, Y);
8165 return &I;
8166 }
8167 break;
8168 }
8169
8170 return nullptr;
8171}
8172
8174 bool Changed = false;
8175
8176 /// Orders the operands of the compare so that they are listed from most
8177 /// complex to least complex. This puts constants before unary operators,
8178 /// before binary operators.
8179 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
8180 I.swapOperands();
8181 Changed = true;
8182 }
8183
8184 const CmpInst::Predicate Pred = I.getPredicate();
8185 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8186 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
8188 return replaceInstUsesWith(I, V);
8189
8190 // Simplify 'fcmp pred X, X'
8191 Type *OpType = Op0->getType();
8192 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
8193 if (Op0 == Op1) {
8194 switch (Pred) {
8195 default: break;
8196 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
8197 case FCmpInst::FCMP_ULT: // True if unordered or less than
8198 case FCmpInst::FCMP_UGT: // True if unordered or greater than
8199 case FCmpInst::FCMP_UNE: // True if unordered or not equal
8200 // Canonicalize these to be 'fcmp uno %X, 0.0'.
8201 I.setPredicate(FCmpInst::FCMP_UNO);
8202 I.setOperand(1, Constant::getNullValue(OpType));
8203 return &I;
8204
8205 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
8206 case FCmpInst::FCMP_OEQ: // True if ordered and equal
8207 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
8208 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
8209 // Canonicalize these to be 'fcmp ord %X, 0.0'.
8210 I.setPredicate(FCmpInst::FCMP_ORD);
8211 I.setOperand(1, Constant::getNullValue(OpType));
8212 return &I;
8213 }
8214 }
8215
8216 if (I.isCommutative()) {
8217 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
8218 replaceOperand(I, 0, Pair->first);
8219 replaceOperand(I, 1, Pair->second);
8220 return &I;
8221 }
8222 }
8223
8224 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
8225 // then canonicalize the operand to 0.0.
8226 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
8227 if (!match(Op0, m_PosZeroFP()) &&
8228 isKnownNeverNaN(Op0, 0, getSimplifyQuery().getWithInstruction(&I)))
8229 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
8230
8231 if (!match(Op1, m_PosZeroFP()) &&
8232 isKnownNeverNaN(Op1, 0, getSimplifyQuery().getWithInstruction(&I)))
8233 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8234 }
8235
8236 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
8237 Value *X, *Y;
8238 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
8239 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
8240
8242 return R;
8243
8244 // Test if the FCmpInst instruction is used exclusively by a select as
8245 // part of a minimum or maximum operation. If so, refrain from doing
8246 // any other folding. This helps out other analyses which understand
8247 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
8248 // and CodeGen. And in this case, at least one of the comparison
8249 // operands has at least one user besides the compare (the select),
8250 // which would often largely negate the benefit of folding anyway.
8251 if (I.hasOneUse())
8252 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
8253 Value *A, *B;
8255 if (SPR.Flavor != SPF_UNKNOWN)
8256 return nullptr;
8257 }
8258
8259 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
8260 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
8261 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
8262 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8263
8264 // Canonicalize:
8265 // fcmp olt X, +inf -> fcmp one X, +inf
8266 // fcmp ole X, +inf -> fcmp ord X, 0
8267 // fcmp ogt X, +inf -> false
8268 // fcmp oge X, +inf -> fcmp oeq X, +inf
8269 // fcmp ult X, +inf -> fcmp une X, +inf
8270 // fcmp ule X, +inf -> true
8271 // fcmp ugt X, +inf -> fcmp uno X, 0
8272 // fcmp uge X, +inf -> fcmp ueq X, +inf
8273 // fcmp olt X, -inf -> false
8274 // fcmp ole X, -inf -> fcmp oeq X, -inf
8275 // fcmp ogt X, -inf -> fcmp one X, -inf
8276 // fcmp oge X, -inf -> fcmp ord X, 0
8277 // fcmp ult X, -inf -> fcmp uno X, 0
8278 // fcmp ule X, -inf -> fcmp ueq X, -inf
8279 // fcmp ugt X, -inf -> fcmp une X, -inf
8280 // fcmp uge X, -inf -> true
8281 const APFloat *C;
8282 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
8283 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
8284 default:
8285 break;
8286 case FCmpInst::FCMP_ORD:
8287 case FCmpInst::FCMP_UNO:
8290 case FCmpInst::FCMP_OGT:
8291 case FCmpInst::FCMP_ULE:
8292 llvm_unreachable("Should be simplified by InstSimplify");
8293 case FCmpInst::FCMP_OLT:
8294 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
8295 case FCmpInst::FCMP_OLE:
8296 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
8297 "", &I);
8298 case FCmpInst::FCMP_OGE:
8299 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
8300 case FCmpInst::FCMP_ULT:
8301 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
8302 case FCmpInst::FCMP_UGT:
8303 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8304 "", &I);
8305 case FCmpInst::FCMP_UGE:
8306 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8307 }
8308 }
8309
8310 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8311 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8312 if (match(Op1, m_PosZeroFP()) &&
8315 if (Pred == FCmpInst::FCMP_OEQ)
8316 IntPred = ICmpInst::ICMP_EQ;
8317 else if (Pred == FCmpInst::FCMP_UNE)
8318 IntPred = ICmpInst::ICMP_NE;
8319
8320 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8321 Type *IntTy = X->getType();
8322 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8323 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8324 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8325 }
8326 }
8327
8328 // Handle fcmp with instruction LHS and constant RHS.
8329 Instruction *LHSI;
8330 Constant *RHSC;
8331 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8332 switch (LHSI->getOpcode()) {
8333 case Instruction::Select:
8334 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8335 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8336 (match(LHSI,
8338 match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
8339 return replaceOperand(I, 0, X);
8340 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
8341 return NV;
8342 break;
8343 case Instruction::FSub:
8344 if (LHSI->hasOneUse())
8345 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
8346 return NV;
8347 break;
8348 case Instruction::PHI:
8349 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8350 return NV;
8351 break;
8352 case Instruction::SIToFP:
8353 case Instruction::UIToFP:
8354 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8355 return NV;
8356 break;
8357 case Instruction::FDiv:
8358 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8359 return NV;
8360 break;
8361 case Instruction::Load:
8362 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8363 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
8365 cast<LoadInst>(LHSI), GEP, GV, I))
8366 return Res;
8367 break;
8368 }
8369 }
8370
8371 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8372 return R;
8373
8374 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))
8375 return R;
8376
8377 if (match(Op0, m_FNeg(m_Value(X)))) {
8378 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8379 Constant *C;
8380 if (match(Op1, m_Constant(C)))
8381 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8382 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8383 }
8384
8385 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8386 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8387 return new FCmpInst(Pred, X, Op1, "", &I);
8388
8389 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8390 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8391 return new FCmpInst(Pred, Op0, Y, "", &I);
8392
8393 if (match(Op0, m_FPExt(m_Value(X)))) {
8394 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8395 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8396 return new FCmpInst(Pred, X, Y, "", &I);
8397
8398 const APFloat *C;
8399 if (match(Op1, m_APFloat(C))) {
8400 const fltSemantics &FPSem =
8401 X->getType()->getScalarType()->getFltSemantics();
8402 bool Lossy;
8403 APFloat TruncC = *C;
8404 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8405
8406 if (Lossy) {
8407 // X can't possibly equal the higher-precision constant, so reduce any
8408 // equality comparison.
8409 // TODO: Other predicates can be handled via getFCmpCode().
8410 switch (Pred) {
8411 case FCmpInst::FCMP_OEQ:
8412 // X is ordered and equal to an impossible constant --> false
8413 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8414 case FCmpInst::FCMP_ONE:
8415 // X is ordered and not equal to an impossible constant --> ordered
8416 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8417 ConstantFP::getZero(X->getType()));
8418 case FCmpInst::FCMP_UEQ:
8419 // X is unordered or equal to an impossible constant --> unordered
8420 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8421 ConstantFP::getZero(X->getType()));
8422 case FCmpInst::FCMP_UNE:
8423 // X is unordered or not equal to an impossible constant --> true
8424 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8425 default:
8426 break;
8427 }
8428 }
8429
8430 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8431 // Avoid lossy conversions and denormals.
8432 // Zero is a special case that's OK to convert.
8433 APFloat Fabs = TruncC;
8434 Fabs.clearSign();
8435 if (!Lossy &&
8436 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8437 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8438 return new FCmpInst(Pred, X, NewC, "", &I);
8439 }
8440 }
8441 }
8442
8443 // Convert a sign-bit test of an FP value into a cast and integer compare.
8444 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8445 // TODO: Handle non-zero compare constants.
8446 // TODO: Handle other predicates.
8447 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),
8448 m_Value(X)))) &&
8449 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8450 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8451 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8452 IntType = VectorType::get(IntType, VecTy->getElementCount());
8453
8454 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8455 if (Pred == FCmpInst::FCMP_OLT) {
8456 Value *IntX = Builder.CreateBitCast(X, IntType);
8457 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8458 ConstantInt::getNullValue(IntType));
8459 }
8460 }
8461
8462 {
8463 Value *CanonLHS = nullptr, *CanonRHS = nullptr;
8464 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));
8465 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));
8466
8467 // (canonicalize(x) == x) => (x == x)
8468 if (CanonLHS == Op1)
8469 return new FCmpInst(Pred, Op1, Op1, "", &I);
8470
8471 // (x == canonicalize(x)) => (x == x)
8472 if (CanonRHS == Op0)
8473 return new FCmpInst(Pred, Op0, Op0, "", &I);
8474
8475 // (canonicalize(x) == canonicalize(y)) => (x == y)
8476 if (CanonLHS && CanonRHS)
8477 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8478 }
8479
8480 if (I.getType()->isVectorTy())
8481 if (Instruction *Res = foldVectorCmp(I, Builder))
8482 return Res;
8483
8484 return Changed ? &I : nullptr;
8485}
amdgpu AMDGPU Register Bank Select
Rewrite undef for PHI
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define Check(C,...)
Hexagon Common GEP
static Instruction * foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
static Instruction * foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC)
Optimize fabs(X) compared with zero.
static Instruction * foldICmpUSubSatOrUAddSatWithConstant(ICmpInst::Predicate Pred, SaturatingInst *II, const APInt &C, InstCombiner::BuilderTy &Builder)
static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2, bool IsSigned=false)
Compute Result = In1+In2, returning true if the result overflowed for this type.
static Value * foldICmpWithLowBitMaskedVal(ICmpInst::Predicate Pred, Value *Op0, Value *Op1, const SimplifyQuery &Q, InstCombiner &IC)
Some comparisons can be simplified.
static Instruction * foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * foldVectorCmp(CmpInst &Cmp, InstCombiner::BuilderTy &Builder)
static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q, unsigned Depth=0)
static Value * createLogicFromTable(const std::bitset< 4 > &Table, Value *Op0, Value *Op1, IRBuilderBase &Builder, bool HasOneUse)
static Instruction * foldICmpOfUAddOv(ICmpInst &I)
static Instruction * foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl, const APInt &C)
Fold icmp (shl 1, Y), C.
static bool isChainSelectCmpBranch(const SelectInst *SI)
Return true when the instruction sequence within a block is select-cmp-br.
static Instruction * foldICmpInvariantGroup(ICmpInst &I)
static Instruction * foldReductionIdiom(ICmpInst &I, InstCombiner::BuilderTy &Builder, const DataLayout &DL)
This function folds patterns produced by lowering of reduce idioms, such as llvm.vector....
static Instruction * canonicalizeICmpBool(ICmpInst &I, InstCombiner::BuilderTy &Builder)
Integer compare with boolean values can always be turned into bitwise ops.
static Instruction * foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI, Constant *RHSC, InstCombinerImpl &CI)
static Value * foldICmpOrXorSubChain(ICmpInst &Cmp, BinaryOperator *Or, InstCombiner::BuilderTy &Builder)
Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
static bool hasBranchUse(ICmpInst &I)
Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparis...
static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth)
When performing a comparison against a constant, it is possible that not all the bits in the LHS are ...
static Instruction * foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * processUMulZExtIdiom(ICmpInst &I, Value *MulVal, const APInt *OtherVal, InstCombinerImpl &IC)
Recognize and process idiom involving test for multiplication overflow.
static Instruction * transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, const DataLayout &DL, InstCombiner &IC)
Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
static Instruction * foldSqrtWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC)
Optimize sqrt(X) compared with zero.
static Instruction * foldFCmpFNegCommonOp(FCmpInst &I)
static Instruction * foldICmpOfCmpIntrinsicWithConstant(ICmpInst::Predicate Pred, IntrinsicInst *I, const APInt &C, InstCombiner::BuilderTy &Builder)
static bool canRewriteGEPAsOffset(Value *Start, Value *Base, const DataLayout &DL, SetVector< Value * > &Explored)
Returns true if we can rewrite Start as a GEP with pointer Base and some integer offset.
static Instruction * foldICmpWithHighBitMask(ICmpInst &Cmp, InstCombiner::BuilderTy &Builder)
static ICmpInst * canonicalizeCmpWithConstant(ICmpInst &I)
If we have an icmp le or icmp ge instruction with a constant operand, turn it into the appropriate ic...
static Instruction * foldICmpIntrinsicWithIntrinsic(ICmpInst &Cmp, InstCombiner::BuilderTy &Builder)
Fold an icmp with LLVM intrinsics.
static Instruction * foldICmpPow2Test(ICmpInst &I, InstCombiner::BuilderTy &Builder)
static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2, bool IsSigned=false)
Compute Result = In1-In2, returning true if the result overflowed for this type.
static Instruction * foldICmpXNegX(ICmpInst &I, InstCombiner::BuilderTy &Builder)
static Instruction * processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, ConstantInt *CI2, ConstantInt *CI1, InstCombinerImpl &IC)
The caller has matched a pattern of the form: I = icmp ugt (add (add A, B), CI2), CI1 If this is of t...
static Value * foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ, InstCombiner::BuilderTy &Builder)
static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C)
Returns true if the exploded icmp can be expressed as a signed comparison to zero and updates the pre...
static Instruction * foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs, const APInt &CRhs, InstCombiner::BuilderTy &Builder, const SimplifyQuery &Q)
static void setInsertionPoint(IRBuilder<> &Builder, Value *V, bool Before=true)
static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned)
static Value * foldICmpWithTruncSignExtendedVal(ICmpInst &I, InstCombiner::BuilderTy &Builder)
Some comparisons can be simplified.
static Value * rewriteGEPAsOffset(Value *Start, Value *Base, const DataLayout &DL, SetVector< Value * > &Explored, InstCombiner &IC)
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
static Instruction * foldICmpOrXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
This file provides internal interfaces used to implement the InstCombine.
This file provides the interface for the instcombine pass implementation.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:512
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
#define T1
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
Value * RHS
Value * LHS
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5337
void clearSign()
Definition: APFloat.h:1215
bool isZero() const
Definition: APFloat.h:1356
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1070
APInt bitcastToAPInt() const
Definition: APFloat.h:1266
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1050
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1010
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5324
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1165
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:214
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition: APInt.h:429
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:209
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:403
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1500
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1472
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:186
APInt abs() const
Get the absolute value.
Definition: APInt.h:1753
unsigned ceilLogBase2() const
Definition: APInt.h:1722
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1181
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:351
APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1918
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1162
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:360
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition: APInt.h:446
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1448
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1091
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:189
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:196
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:309
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1898
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition: APInt.h:1229
bool eq(const APInt &RHS) const
Equality comparison.
Definition: APInt.h:1059
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1146
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1905
void negate()
Negate this APInt in place.
Definition: APInt.h:1430
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1598
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1557
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:199
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:336
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1574
unsigned logBase2() const
Definition: APInt.h:1719
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition: APInt.h:455
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:807
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:385
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1130
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:853
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:420
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:286
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1110
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:276
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:180
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1217
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1911
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:369
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:266
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:219
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:831
unsigned countr_one() const
Count the number of trailing one bits.
Definition: APInt.h:1615
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1201
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
an instruction to allocate memory on the stack
Definition: Instructions.h:61
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Class to represent array types.
Definition: DerivedTypes.h:371
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:416
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:459
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:239
BinaryOps getOpcode() const
Definition: InstrTypes.h:442
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Conditional or Unconditional Branch instruction.
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1410
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:747
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1104
Predicate getStrictPredicate() const
For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
Definition: InstrTypes.h:940
bool isEquality() const
Determine if this is an equals/not equals predicate.
Definition: InstrTypes.h:997
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:757
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:760
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:774
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:786
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:787
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:763
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:772
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:761
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:762
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:781
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:780
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:784
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:771
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:765
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:768
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:782
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:769
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:764
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:766
@ ICMP_EQ
equal
Definition: InstrTypes.h:778
@ ICMP_NE
not equal
Definition: InstrTypes.h:779
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:785
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:773
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:783
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:770
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:759
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:767
bool isSigned() const
Definition: InstrTypes.h:1007
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:909
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:1056
static CmpInst * Create(OtherOps Op, Predicate Pred, Value *S1, Value *S2, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition: InstrTypes.h:953
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:871
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:847
bool isStrictPredicate() const
Definition: InstrTypes.h:925
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:975
Predicate getFlippedSignednessPredicate()
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert.
Definition: InstrTypes.h:1050
bool isIntPredicate() const
Definition: InstrTypes.h:865
bool isUnsigned() const
Definition: InstrTypes.h:1013
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2281
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2242
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2618
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2605
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2632
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2611
static Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2599
static Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1038
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition: Constants.h:256
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:850
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:206
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:124
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:857
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:149
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:146
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:864
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
std::optional< ConstantRange > exactUnionWith(const ConstantRange &CR) const
Union the two ranges and return the result if it can be represented exactly, otherwise return std::nu...
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
ConstantRange difference(const ConstantRange &CR) const
Subtract the specified range from this range (aka relative complement of the sets).
bool isEmptySet() const
Return true if this set contains no members.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
std::optional< ConstantRange > exactIntersectWith(const ConstantRange &CR) const
Intersect the two ranges and return the result if it can be represented exactly, otherwise return std...
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1450
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
Definition: Constants.cpp:400
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:768
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:417
const APInt & getUniqueInteger() const
If C is a constant integer then return its value, otherwise C must be a vector of constant integers,...
Definition: Constants.cpp:1745
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
Definition: DataLayout.cpp:869
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:761
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
Definition: DataLayout.cpp:896
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:462
Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
Definition: DataLayout.cpp:884
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:146
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This instruction compares its operands according to the predicate given to the constructor.
bool isEquality() const
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:411
Type * getSourceElementType() const
Definition: Operator.cpp:68
Value * getPointerOperand()
Definition: Operator.h:438
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
Definition: Operator.h:485
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:915
Type * getValueType() const
Definition: GlobalValue.h:296
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:91
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:914
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2277
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2480
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:536
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2285
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1193
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2536
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:463
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:933
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1454
Value * CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1353
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1891
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2265
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1738
Value * createIsFPClass(Value *FPNum, unsigned Test)
Definition: IRBuilder.cpp:1288
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:483
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2386
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2417
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1766
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2261
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1361
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2147
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2269
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1433
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2041
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1492
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1344
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:468
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2027
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1514
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1683
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2293
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2181
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2216
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition: IRBuilder.h:2564
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:177
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2432
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1536
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2371
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:513
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:499
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1421
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1378
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
Instruction * foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr, const APInt &C)
Fold icmp ({al}shr X, Y), C.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * foldICmpWithZextOrSext(ICmpInst &ICmp)
Instruction * foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select, ConstantInt *C)
Instruction * foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Instruction * foldICmpBinOpWithConstant(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
Instruction * foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, const APInt &C)
Fold icmp (or X, Y), C.
Instruction * foldICmpTruncWithTruncOrExt(ICmpInst &Cmp, const SimplifyQuery &Q)
Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
Instruction * foldSignBitTest(ICmpInst &I)
Fold equality-comparison between zero and any (maybe truncated) right-shift by one-less-than-bitwidth...
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
Value * insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, bool isSigned, bool Inside)
Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise (V < Lo || V >= Hi).
Instruction * foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ)
Try to fold icmp (binop), X or icmp X, (binop).
Instruction * foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub, const APInt &C)
Fold icmp (sub X, Y), C.
Instruction * foldICmpInstWithConstantNotInt(ICmpInst &Cmp)
Handle icmp with constant (but not simple integer constant) RHS.
Instruction * foldICmpWithMinMax(Instruction &I, MinMaxIntrinsic *MinMax, Value *Z, ICmpInst::Predicate Pred)
Fold icmp Pred min|max(X, Y), Z.
Instruction * foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I)
Fold comparisons between a GEP instruction and something else.
Instruction * foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, const APInt &C2)
Handle "(icmp eq/ne (shl AP2, A), AP1)" -> (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
Value * reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0, const SimplifyQuery &SQ, bool AnalyzeForSignBitExtraction=false)
Instruction * foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, const APInt &C)
Fold an equality icmp with LLVM intrinsic and constant operand.
Value * foldMultiplicationOverflowCheck(ICmpInst &Cmp)
Fold (-1 u/ x) u< y ((x * y) ?/ x) != y to @llvm.
Instruction * foldICmpWithConstant(ICmpInst &Cmp)
Fold icmp Pred X, C.
CmpInst * canonicalizeICmpPredicate(CmpInst &I)
If we have a comparison with a non-canonical predicate, if we can update all the users,...
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * foldICmpWithZero(ICmpInst &Cmp)
Instruction * foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Fold an icmp equality instruction with binary operator LHS and constant RHS: icmp eq/ne BO,...
Instruction * foldICmpUsingBoolRange(ICmpInst &I)
If one operand of an icmp is effectively a bool (value range of {0,1}), then try to reduce patterns b...
Instruction * foldICmpWithTrunc(ICmpInst &Cmp)
Instruction * foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, const APInt &C)
Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
bool SimplifyDemandedBits(Instruction *I, unsigned Op, const APInt &DemandedMask, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q) override
This form of SimplifyDemandedBits simplifies the specified instruction operand if possible,...
bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS, ConstantInt *&Less, ConstantInt *&Equal, ConstantInt *&Greater)
Match a select chain which produces one of three values based on whether the LHS is less than,...
Instruction * foldCmpLoadFromIndexedGlobal(LoadInst *LI, GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
This is called when we see this pattern: cmp pred (load (gep GV, ...)), cmpcst where GV is a global v...
Instruction * visitFCmpInst(FCmpInst &I)
Instruction * foldICmpUsingKnownBits(ICmpInst &Cmp)
Try to fold the comparison based on range information we can get by checking whether bits are known t...
Instruction * foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, const APInt &C)
Fold icmp ({su}div X, Y), C.
Instruction * foldIRemByPowerOfTwoToBitTest(ICmpInst &I)
If we have: icmp eq/ne (urem/srem x, y), 0 iff y is a power-of-two, we can replace this with a bit te...
Instruction * foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold fcmp ([us]itofp x, cst) if possible.
Instruction * foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Fold icmp (udiv X, Y), C.
Constant * getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp)
Instruction * foldICmpWithCastOp(ICmpInst &ICmp)
Handle icmp (cast x), (cast or constant).
Instruction * foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc, const APInt &C)
Fold icmp (trunc X), C.
Instruction * foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add, const APInt &C)
Fold icmp (add X, Y), C.
Instruction * foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul, const APInt &C)
Fold icmp (mul X, Y), C.
Instruction * tryFoldInstWithCtpopWithNot(Instruction *I)
Instruction * foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor, const APInt &C)
Fold icmp (xor X, Y), C.
Instruction * foldICmpAddOpConst(Value *X, const APInt &C, ICmpInst::Predicate Pred)
Fold "icmp pred (X+C), X".
Instruction * foldICmpInstWithConstantAllowPoison(ICmpInst &Cmp, const APInt &C)
Try to fold integer comparisons with a constant operand: icmp Pred X, C where X is some kind of instr...
Instruction * foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, const APInt &C1, const APInt &C2)
Fold icmp (and (sh X, Y), C2), C1.
Instruction * foldICmpInstWithConstant(ICmpInst &Cmp)
Try to fold integer comparisons with a constant operand: icmp Pred X, C where X is some kind of instr...
Instruction * foldICmpXorShiftConst(ICmpInst &Cmp, BinaryOperator *Xor, const APInt &C)
For power-of-2 C: ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1) ((X s>> ShiftC) ^ X) u> (C - 1) -...
Instruction * foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl, const APInt &C)
Fold icmp (shl X, Y), C.
Instruction * foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And, const APInt &C)
Fold icmp (and X, Y), C.
Instruction * foldICmpEquality(ICmpInst &Cmp)
bool dominatesAllUses(const Instruction *DI, const Instruction *UI, const BasicBlock *DB) const
True when DB dominates all uses of DI except UI.
bool foldAllocaCmp(AllocaInst *Alloca)
Instruction * foldICmpCommutative(ICmpInst::Predicate Pred, Value *Op0, Value *Op1, ICmpInst &CxtI)
Instruction * visitICmpInst(ICmpInst &I)
Instruction * foldSelectICmp(ICmpInst::Predicate Pred, SelectInst *SI, Value *RHS, const ICmpInst &I)
OverflowResult computeOverflow(Instruction::BinaryOps BinaryOp, bool IsSigned, Value *LHS, Value *RHS, Instruction *CxtI) const
Instruction * foldICmpWithDominatingICmp(ICmpInst &Cmp)
Canonicalize icmp instructions based on dominating conditions.
bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp, const unsigned SIOpd)
Try to replace select with select operand SIOpd in SI-ICmp sequence.
Instruction * foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, const APInt &C2)
Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" -> (icmp eq/ne A, Log2(AP2/AP1)) -> (icmp eq/ne A,...
void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser=nullptr)
Freely adapt every user of V as-if V was changed to !V.
Instruction * foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And, const APInt &C1)
Fold icmp (and X, C2), C1.
Instruction * foldICmpBitCast(ICmpInst &Cmp)
The core instruction combiner logic.
Definition: InstCombiner.h:47
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:499
SimplifyQuery SQ
Definition: InstCombiner.h:76
static bool isCanonicalPredicate(CmpInst::Predicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
Definition: InstCombiner.h:157
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:232
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI, bool IsNSW=false) const
Definition: InstCombiner.h:462
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
Definition: InstCombiner.h:139
TargetLibraryInfo & TLI
Definition: InstCombiner.h:73
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
Definition: InstCombiner.h:441
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:386
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
Definition: InstCombiner.h:55
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:485
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
Definition: InstCombiner.h:180
static std::optional< std::pair< CmpInst::Predicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpInst::Predicate Pred, Constant *C)
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:492
const DataLayout & DL
Definition: InstCombiner.h:75
DomConditionCache DC
Definition: InstCombiner.h:81
bool canFreelyInvertAllUsersOf(Instruction *V, Value *IgnoredUser)
Given i1 V, can every user of V be freely adapted if V is changed to !V ? InstCombine's freelyInvertA...
Definition: InstCombiner.h:248
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:336
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:410
DominatorTree & DT
Definition: InstCombiner.h:74
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:470
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:431
BuilderTy & Builder
Definition: InstCombiner.h:60
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:477
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:213
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:342
unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:457
bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoInfs() const LLVM_READONLY
Determine whether the no-infs flag is set.
bool isArithmeticShift() const
Return true if this is an arithmetic shift right.
Definition: Instruction.h:318
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:274
bool isShift() const
Definition: Instruction.h:281
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:266
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:55
An instruction for reading from memory.
Definition: Instructions.h:174
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:141
This class represents min/max intrinsics.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Represents a saturating add/sub intrinsic.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
A vector that has set insertion semantics.
Definition: SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition: SetVector.h:254
This instruction constructs a fixed permutation of two input vectors.
bool empty() const
Definition: SmallVector.h:95
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:951
void push_back(const T &Elt)
Definition: SmallVector.h:427
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
Class to represent struct types.
Definition: DerivedTypes.h:216
This class represents a truncation of integer types.
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:261
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:230
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:251
static IntegerType * getInt1Ty(LLVMContext &C)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition: Type.h:165
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
int getFPMantissaWidth() const
Return the width of the mantissa of this type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:224
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout as defined b...
Definition: Type.h:170
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:343
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr) const
Accumulate the constant offset this value has compared to a base pointer.
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
iterator_range< user_iterator > users()
Definition: Value.h:421
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition: Value.cpp:153
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:694
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1075
iterator_range< use_iterator > uses()
Definition: Value.h:376
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:664
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
const ParentTy * getParent() const
Definition: ilist_node.h:32
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A unsign-divided by B, rounded by the given rounding mode.
Definition: APInt.cpp:2732
APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A sign-divided by B, rounded by the given rounding mode.
Definition: APInt.cpp:2750
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1539
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:972
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:816
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:875
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
Definition: PatternMatch.h:980
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:893
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:854
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:639
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
Definition: PatternMatch.h:481
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:683
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Definition: PatternMatch.h:773
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
Definition: PatternMatch.h:203
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
Definition: PatternMatch.h:698
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition: STLExtras.h:853
OverflowResult
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
bool isKnownNeverInfinity(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:215
Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:340
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: bit.h:281
Value * emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
Definition: Local.cpp:22
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:48
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_UNKNOWN
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:2132
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
Value * simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
@ Other
Any other memory.
bool isKnownNegative(const Value *V, const SimplifyQuery &DL, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ And
Bitwise or logical AND of integers.
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ Add
Sum of integers.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1921
bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, Value *&X, APInt &Mask, bool LookThroughTrunc=true)
Decompose an icmp into the form ((X & Mask) pred 0) if possible.
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition: STLExtras.h:2045
bool isKnownNeverNaN(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
Value * simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define NC
Definition: regutils.h:42
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:254
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:258
This callback is used in conjunction with PointerMayBeCaptured.
Represent subnormal handling kind for floating point instruction inputs and outputs.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
static constexpr DenormalMode getIEEE()
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:97
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:76
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:231
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:263
APInt getSignedMaxValue() const
Return the maximal signed value possible given these KnownBits.
Definition: KnownBits.h:140
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:278
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:50
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:237
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:134
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:118
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:94
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:275
APInt getSignedMinValue() const
Return the minimal signed value possible given these KnownBits.
Definition: KnownBits.h:124
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:56
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
Definition: SimplifyQuery.h:71
const Instruction * CxtI
Definition: SimplifyQuery.h:75
const DominatorTree * DT
Definition: SimplifyQuery.h:73
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:74
SimplifyQuery getWithoutDomCondCache() const
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:254