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))
688 RHS = RHS->stripPointerCasts();
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(),
701 RHS->getType()->getPointerAddressSpace())) {
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 auto Simplifies = [&](Value *Op, unsigned Idx) {
4213 // A comparison of ucmp/scmp with a constant will fold into an icmp.
4214 const APInt *Dummy;
4215 return Op ||
4216 (isa<CmpIntrinsic>(SI->getOperand(Idx)) &&
4217 SI->getOperand(Idx)->hasOneUse() && match(RHS, m_APInt(Dummy)));
4218 };
4219
4220 // We only want to perform this transformation if it will not lead to
4221 // additional code. This is true if either both sides of the select
4222 // fold to a constant (in which case the icmp is replaced with a select
4223 // which will usually simplify) or this is the only user of the
4224 // select (in which case we are trading a select+icmp for a simpler
4225 // select+icmp) or all uses of the select can be replaced based on
4226 // dominance information ("Global cases").
4227 bool Transform = false;
4228 if (Op1 && Op2)
4229 Transform = true;
4230 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {
4231 // Local case
4232 if (SI->hasOneUse())
4233 Transform = true;
4234 // Global cases
4235 else if (CI && !CI->isZero())
4236 // When Op1 is constant try replacing select with second operand.
4237 // Otherwise Op2 is constant and try replacing select with first
4238 // operand.
4239 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4240 }
4241 if (Transform) {
4242 if (!Op1)
4243 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4244 if (!Op2)
4245 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4246 return SelectInst::Create(SI->getOperand(0), Op1, Op2);
4247 }
4248
4249 return nullptr;
4250}
4251
4252// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4253static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4254 unsigned Depth = 0) {
4255 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4256 return true;
4257 if (V->getType()->getScalarSizeInBits() == 1)
4258 return true;
4260 return false;
4261 Value *X;
4262 const Instruction *I = dyn_cast<Instruction>(V);
4263 if (!I)
4264 return false;
4265 switch (I->getOpcode()) {
4266 case Instruction::ZExt:
4267 // ZExt(Mask) is a Mask.
4268 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4269 case Instruction::SExt:
4270 // SExt(Mask) is a Mask.
4271 // SExt(~Mask) is a ~Mask.
4272 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4273 case Instruction::And:
4274 case Instruction::Or:
4275 // Mask0 | Mask1 is a Mask.
4276 // Mask0 & Mask1 is a Mask.
4277 // ~Mask0 | ~Mask1 is a ~Mask.
4278 // ~Mask0 & ~Mask1 is a ~Mask.
4279 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4280 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4281 case Instruction::Xor:
4282 if (match(V, m_Not(m_Value(X))))
4283 return isMaskOrZero(X, !Not, Q, Depth);
4284
4285 // (X ^ -X) is a ~Mask
4286 if (Not)
4287 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4288 // (X ^ (X - 1)) is a Mask
4289 else
4290 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4291 case Instruction::Select:
4292 // c ? Mask0 : Mask1 is a Mask.
4293 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4294 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4295 case Instruction::Shl:
4296 // (~Mask) << X is a ~Mask.
4297 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4298 case Instruction::LShr:
4299 // Mask >> X is a Mask.
4300 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4301 case Instruction::AShr:
4302 // Mask s>> X is a Mask.
4303 // ~Mask s>> X is a ~Mask.
4304 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4305 case Instruction::Add:
4306 // Pow2 - 1 is a Mask.
4307 if (!Not && match(I->getOperand(1), m_AllOnes()))
4308 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4309 Depth, Q.AC, Q.CxtI, Q.DT);
4310 break;
4311 case Instruction::Sub:
4312 // -Pow2 is a ~Mask.
4313 if (Not && match(I->getOperand(0), m_Zero()))
4314 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4315 Depth, Q.AC, Q.CxtI, Q.DT);
4316 break;
4317 case Instruction::Call: {
4318 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4319 switch (II->getIntrinsicID()) {
4320 // min/max(Mask0, Mask1) is a Mask.
4321 // min/max(~Mask0, ~Mask1) is a ~Mask.
4322 case Intrinsic::umax:
4323 case Intrinsic::smax:
4324 case Intrinsic::umin:
4325 case Intrinsic::smin:
4326 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4327 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4328
4329 // In the context of masks, bitreverse(Mask) == ~Mask
4330 case Intrinsic::bitreverse:
4331 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4332 default:
4333 break;
4334 }
4335 }
4336 break;
4337 }
4338 default:
4339 break;
4340 }
4341 return false;
4342}
4343
4344/// Some comparisons can be simplified.
4345/// In this case, we are looking for comparisons that look like
4346/// a check for a lossy truncation.
4347/// Folds:
4348/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4349/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4350/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4351/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4352/// Where Mask is some pattern that produces all-ones in low bits:
4353/// (-1 >> y)
4354/// ((-1 << y) >> y) <- non-canonical, has extra uses
4355/// ~(-1 << y)
4356/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4357/// The Mask can be a constant, too.
4358/// For some predicates, the operands are commutative.
4359/// For others, x can only be on a specific side.
4361 Value *Op1, const SimplifyQuery &Q,
4362 InstCombiner &IC) {
4363
4364 ICmpInst::Predicate DstPred;
4365 switch (Pred) {
4367 // x & Mask == x
4368 // x & ~Mask == 0
4369 // ~x | Mask == -1
4370 // -> x u<= Mask
4371 // x & ~Mask == ~Mask
4372 // -> ~Mask u<= x
4374 break;
4376 // x & Mask != x
4377 // x & ~Mask != 0
4378 // ~x | Mask != -1
4379 // -> x u> Mask
4380 // x & ~Mask != ~Mask
4381 // -> ~Mask u> x
4383 break;
4385 // x & Mask u< x
4386 // -> x u> Mask
4387 // x & ~Mask u< ~Mask
4388 // -> ~Mask u> x
4390 break;
4392 // x & Mask u>= x
4393 // -> x u<= Mask
4394 // x & ~Mask u>= ~Mask
4395 // -> ~Mask u<= x
4397 break;
4399 // x & Mask s< x [iff Mask s>= 0]
4400 // -> x s> Mask
4401 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4402 // -> ~Mask s> x
4404 break;
4406 // x & Mask s>= x [iff Mask s>= 0]
4407 // -> x s<= Mask
4408 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4409 // -> ~Mask s<= x
4411 break;
4412 default:
4413 // We don't support sgt,sle
4414 // ult/ugt are simplified to true/false respectively.
4415 return nullptr;
4416 }
4417
4418 Value *X, *M;
4419 // Put search code in lambda for early positive returns.
4420 auto IsLowBitMask = [&]() {
4421 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4422 X = Op1;
4423 // Look for: x & Mask pred x
4424 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4425 return !ICmpInst::isSigned(Pred) ||
4426 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4427 }
4428
4429 // Look for: x & ~Mask pred ~Mask
4430 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4431 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4432 }
4433 return false;
4434 }
4435 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4436 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4437
4438 auto Check = [&]() {
4439 // Look for: ~x | Mask == -1
4440 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4441 if (Value *NotX =
4442 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4443 X = NotX;
4444 return true;
4445 }
4446 }
4447 return false;
4448 };
4449 if (Check())
4450 return true;
4451 std::swap(X, M);
4452 return Check();
4453 }
4454 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4455 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4456 auto Check = [&]() {
4457 // Look for: x & ~Mask == 0
4458 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4459 if (Value *NotM =
4460 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4461 M = NotM;
4462 return true;
4463 }
4464 }
4465 return false;
4466 };
4467 if (Check())
4468 return true;
4469 std::swap(X, M);
4470 return Check();
4471 }
4472 return false;
4473 };
4474
4475 if (!IsLowBitMask())
4476 return nullptr;
4477
4478 return IC.Builder.CreateICmp(DstPred, X, M);
4479}
4480
4481/// Some comparisons can be simplified.
4482/// In this case, we are looking for comparisons that look like
4483/// a check for a lossy signed truncation.
4484/// Folds: (MaskedBits is a constant.)
4485/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4486/// Into:
4487/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4488/// Where KeptBits = bitwidth(%x) - MaskedBits
4489static Value *
4491 InstCombiner::BuilderTy &Builder) {
4492 ICmpInst::Predicate SrcPred;
4493 Value *X;
4494 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4495 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4496 if (!match(&I, m_c_ICmp(SrcPred,
4498 m_APInt(C1))),
4499 m_Deferred(X))))
4500 return nullptr;
4501
4502 // Potential handling of non-splats: for each element:
4503 // * if both are undef, replace with constant 0.
4504 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4505 // * if both are not undef, and are different, bailout.
4506 // * else, only one is undef, then pick the non-undef one.
4507
4508 // The shift amount must be equal.
4509 if (*C0 != *C1)
4510 return nullptr;
4511 const APInt &MaskedBits = *C0;
4512 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4513
4514 ICmpInst::Predicate DstPred;
4515 switch (SrcPred) {
4517 // ((%x << MaskedBits) a>> MaskedBits) == %x
4518 // =>
4519 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4521 break;
4523 // ((%x << MaskedBits) a>> MaskedBits) != %x
4524 // =>
4525 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4527 break;
4528 // FIXME: are more folds possible?
4529 default:
4530 return nullptr;
4531 }
4532
4533 auto *XType = X->getType();
4534 const unsigned XBitWidth = XType->getScalarSizeInBits();
4535 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4536 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4537
4538 // KeptBits = bitwidth(%x) - MaskedBits
4539 const APInt KeptBits = BitWidth - MaskedBits;
4540 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4541 // ICmpCst = (1 << KeptBits)
4542 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4543 assert(ICmpCst.isPowerOf2());
4544 // AddCst = (1 << (KeptBits-1))
4545 const APInt AddCst = ICmpCst.lshr(1);
4546 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4547
4548 // T0 = add %x, AddCst
4549 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4550 // T1 = T0 DstPred ICmpCst
4551 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4552
4553 return T1;
4554}
4555
4556// Given pattern:
4557// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4558// we should move shifts to the same hand of 'and', i.e. rewrite as
4559// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4560// We are only interested in opposite logical shifts here.
4561// One of the shifts can be truncated.
4562// If we can, we want to end up creating 'lshr' shift.
4563static Value *
4565 InstCombiner::BuilderTy &Builder) {
4566 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4567 !I.getOperand(0)->hasOneUse())
4568 return nullptr;
4569
4570 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4571
4572 // Look for an 'and' of two logical shifts, one of which may be truncated.
4573 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4574 Instruction *XShift, *MaybeTruncation, *YShift;
4575 if (!match(
4576 I.getOperand(0),
4577 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4579 m_AnyLogicalShift, m_Instruction(YShift))),
4580 m_Instruction(MaybeTruncation)))))
4581 return nullptr;
4582
4583 // We potentially looked past 'trunc', but only when matching YShift,
4584 // therefore YShift must have the widest type.
4585 Instruction *WidestShift = YShift;
4586 // Therefore XShift must have the shallowest type.
4587 // Or they both have identical types if there was no truncation.
4588 Instruction *NarrowestShift = XShift;
4589
4590 Type *WidestTy = WidestShift->getType();
4591 Type *NarrowestTy = NarrowestShift->getType();
4592 assert(NarrowestTy == I.getOperand(0)->getType() &&
4593 "We did not look past any shifts while matching XShift though.");
4594 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4595
4596 // If YShift is a 'lshr', swap the shifts around.
4597 if (match(YShift, m_LShr(m_Value(), m_Value())))
4598 std::swap(XShift, YShift);
4599
4600 // The shifts must be in opposite directions.
4601 auto XShiftOpcode = XShift->getOpcode();
4602 if (XShiftOpcode == YShift->getOpcode())
4603 return nullptr; // Do not care about same-direction shifts here.
4604
4605 Value *X, *XShAmt, *Y, *YShAmt;
4606 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4607 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4608
4609 // If one of the values being shifted is a constant, then we will end with
4610 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4611 // however, we will need to ensure that we won't increase instruction count.
4612 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4613 // At least one of the hands of the 'and' should be one-use shift.
4614 if (!match(I.getOperand(0),
4615 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4616 return nullptr;
4617 if (HadTrunc) {
4618 // Due to the 'trunc', we will need to widen X. For that either the old
4619 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4620 if (!MaybeTruncation->hasOneUse() &&
4621 !NarrowestShift->getOperand(1)->hasOneUse())
4622 return nullptr;
4623 }
4624 }
4625
4626 // We have two shift amounts from two different shifts. The types of those
4627 // shift amounts may not match. If that's the case let's bailout now.
4628 if (XShAmt->getType() != YShAmt->getType())
4629 return nullptr;
4630
4631 // As input, we have the following pattern:
4632 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4633 // We want to rewrite that as:
4634 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4635 // While we know that originally (Q+K) would not overflow
4636 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4637 // shift amounts. so it may now overflow in smaller bitwidth.
4638 // To ensure that does not happen, we need to ensure that the total maximal
4639 // shift amount is still representable in that smaller bit width.
4640 unsigned MaximalPossibleTotalShiftAmount =
4641 (WidestTy->getScalarSizeInBits() - 1) +
4642 (NarrowestTy->getScalarSizeInBits() - 1);
4643 APInt MaximalRepresentableShiftAmount =
4645 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4646 return nullptr;
4647
4648 // Can we fold (XShAmt+YShAmt) ?
4649 auto *NewShAmt = dyn_cast_or_null<Constant>(
4650 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4651 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4652 if (!NewShAmt)
4653 return nullptr;
4654 if (NewShAmt->getType() != WidestTy) {
4655 NewShAmt =
4656 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4657 if (!NewShAmt)
4658 return nullptr;
4659 }
4660 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4661
4662 // Is the new shift amount smaller than the bit width?
4663 // FIXME: could also rely on ConstantRange.
4664 if (!match(NewShAmt,
4666 APInt(WidestBitWidth, WidestBitWidth))))
4667 return nullptr;
4668
4669 // An extra legality check is needed if we had trunc-of-lshr.
4670 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4671 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4672 WidestShift]() {
4673 // It isn't obvious whether it's worth it to analyze non-constants here.
4674 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4675 // If *any* of these preconditions matches we can perform the fold.
4676 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4677 ? NewShAmt->getSplatValue()
4678 : NewShAmt;
4679 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4680 if (NewShAmtSplat &&
4681 (NewShAmtSplat->isNullValue() ||
4682 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4683 return true;
4684 // We consider *min* leading zeros so a single outlier
4685 // blocks the transform as opposed to allowing it.
4686 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4687 KnownBits Known = computeKnownBits(C, SQ.DL);
4688 unsigned MinLeadZero = Known.countMinLeadingZeros();
4689 // If the value being shifted has at most lowest bit set we can fold.
4690 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4691 if (MaxActiveBits <= 1)
4692 return true;
4693 // Precondition: NewShAmt u<= countLeadingZeros(C)
4694 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4695 return true;
4696 }
4697 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4698 KnownBits Known = computeKnownBits(C, SQ.DL);
4699 unsigned MinLeadZero = Known.countMinLeadingZeros();
4700 // If the value being shifted has at most lowest bit set we can fold.
4701 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4702 if (MaxActiveBits <= 1)
4703 return true;
4704 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4705 if (NewShAmtSplat) {
4706 APInt AdjNewShAmt =
4707 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4708 if (AdjNewShAmt.ule(MinLeadZero))
4709 return true;
4710 }
4711 }
4712 return false; // Can't tell if it's ok.
4713 };
4714 if (!CanFold())
4715 return nullptr;
4716 }
4717
4718 // All good, we can do this fold.
4719 X = Builder.CreateZExt(X, WidestTy);
4720 Y = Builder.CreateZExt(Y, WidestTy);
4721 // The shift is the same that was for X.
4722 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4723 ? Builder.CreateLShr(X, NewShAmt)
4724 : Builder.CreateShl(X, NewShAmt);
4725 Value *T1 = Builder.CreateAnd(T0, Y);
4726 return Builder.CreateICmp(I.getPredicate(), T1,
4727 Constant::getNullValue(WidestTy));
4728}
4729
4730/// Fold
4731/// (-1 u/ x) u< y
4732/// ((x * y) ?/ x) != y
4733/// to
4734/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4735/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4736/// will mean that we are looking for the opposite answer.
4739 Value *X, *Y;
4741 Instruction *Div;
4742 bool NeedNegation;
4743 // Look for: (-1 u/ x) u</u>= y
4744 if (!I.isEquality() &&
4745 match(&I, m_c_ICmp(Pred,
4747 m_Instruction(Div)),
4748 m_Value(Y)))) {
4749 Mul = nullptr;
4750
4751 // Are we checking that overflow does not happen, or does happen?
4752 switch (Pred) {
4754 NeedNegation = false;
4755 break; // OK
4757 NeedNegation = true;
4758 break; // OK
4759 default:
4760 return nullptr; // Wrong predicate.
4761 }
4762 } else // Look for: ((x * y) / x) !=/== y
4763 if (I.isEquality() &&
4764 match(&I,
4765 m_c_ICmp(Pred, m_Value(Y),
4768 m_Value(X)),
4770 m_Deferred(X))),
4771 m_Instruction(Div))))) {
4772 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4773 } else
4774 return nullptr;
4775
4777 // If the pattern included (x * y), we'll want to insert new instructions
4778 // right before that original multiplication so that we can replace it.
4779 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4780 if (MulHadOtherUses)
4782
4783 Function *F = Intrinsic::getDeclaration(I.getModule(),
4784 Div->getOpcode() == Instruction::UDiv
4785 ? Intrinsic::umul_with_overflow
4786 : Intrinsic::smul_with_overflow,
4787 X->getType());
4788 CallInst *Call = Builder.CreateCall(F, {X, Y}, "mul");
4789
4790 // If the multiplication was used elsewhere, to ensure that we don't leave
4791 // "duplicate" instructions, replace uses of that original multiplication
4792 // with the multiplication result from the with.overflow intrinsic.
4793 if (MulHadOtherUses)
4794 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4795
4796 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4797 if (NeedNegation) // This technically increases instruction count.
4798 Res = Builder.CreateNot(Res, "mul.not.ov");
4799
4800 // If we replaced the mul, erase it. Do this after all uses of Builder,
4801 // as the mul is used as insertion point.
4802 if (MulHadOtherUses)
4804
4805 return Res;
4806}
4807
4809 InstCombiner::BuilderTy &Builder) {
4810 CmpInst::Predicate Pred;
4811 Value *X;
4812 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4813
4814 if (ICmpInst::isSigned(Pred))
4815 Pred = ICmpInst::getSwappedPredicate(Pred);
4816 else if (ICmpInst::isUnsigned(Pred))
4817 Pred = ICmpInst::getSignedPredicate(Pred);
4818 // else for equality-comparisons just keep the predicate.
4819
4820 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4821 Constant::getNullValue(X->getType()), I.getName());
4822 }
4823
4824 // A value is not equal to its negation unless that value is 0 or
4825 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4826 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
4827 ICmpInst::isEquality(Pred)) {
4828 Type *Ty = X->getType();
4830 Constant *MaxSignedVal =
4831 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
4832 Value *And = Builder.CreateAnd(X, MaxSignedVal);
4833 Constant *Zero = Constant::getNullValue(Ty);
4834 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
4835 }
4836
4837 return nullptr;
4838}
4839
4841 InstCombinerImpl &IC) {
4842 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4843 // Normalize and operand as operand 0.
4844 CmpInst::Predicate Pred = I.getPredicate();
4845 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
4846 std::swap(Op0, Op1);
4847 Pred = ICmpInst::getSwappedPredicate(Pred);
4848 }
4849
4850 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
4851 return nullptr;
4852
4853 // (icmp (X & Y) u< X --> (X & Y) != X
4854 if (Pred == ICmpInst::ICMP_ULT)
4855 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4856
4857 // (icmp (X & Y) u>= X --> (X & Y) == X
4858 if (Pred == ICmpInst::ICMP_UGE)
4859 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4860
4861 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4862 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
4863 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
4864 // so don't do this fold.
4865 if (!match(Op1, m_ImmConstant()))
4866 if (auto *NotOp1 =
4867 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
4868 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),
4869 Constant::getAllOnesValue(Op1->getType()));
4870 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
4871 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4872 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),
4873 Constant::getNullValue(Op1->getType()));
4874 }
4875
4876 if (!ICmpInst::isSigned(Pred))
4877 return nullptr;
4878
4879 KnownBits KnownY = IC.computeKnownBits(A, /*Depth=*/0, &I);
4880 // (X & NegY) spred X --> (X & NegY) upred X
4881 if (KnownY.isNegative())
4882 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
4883
4884 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
4885 return nullptr;
4886
4887 if (KnownY.isNonNegative())
4888 // (X & PosY) s<= X --> X s>= 0
4889 // (X & PosY) s> X --> X s< 0
4890 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
4891 Constant::getNullValue(Op1->getType()));
4892
4894 // (NegX & Y) s<= NegX --> Y s< 0
4895 // (NegX & Y) s> NegX --> Y s>= 0
4897 Constant::getNullValue(A->getType()));
4898
4899 return nullptr;
4900}
4901
4903 InstCombinerImpl &IC) {
4904 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4905
4906 // Normalize or operand as operand 0.
4907 CmpInst::Predicate Pred = I.getPredicate();
4908 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
4909 std::swap(Op0, Op1);
4910 Pred = ICmpInst::getSwappedPredicate(Pred);
4911 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
4912 return nullptr;
4913 }
4914
4915 // icmp (X | Y) u<= X --> (X | Y) == X
4916 if (Pred == ICmpInst::ICMP_ULE)
4917 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4918
4919 // icmp (X | Y) u> X --> (X | Y) != X
4920 if (Pred == ICmpInst::ICMP_UGT)
4921 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4922
4923 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4924 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
4925 if (Value *NotOp1 =
4926 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
4927 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
4928 Constant::getNullValue(Op1->getType()));
4929 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
4930 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4931 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
4932 Constant::getAllOnesValue(Op1->getType()));
4933 }
4934 return nullptr;
4935}
4936
4938 InstCombinerImpl &IC) {
4939 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4940 // Normalize xor operand as operand 0.
4941 CmpInst::Predicate Pred = I.getPredicate();
4942 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
4943 std::swap(Op0, Op1);
4944 Pred = ICmpInst::getSwappedPredicate(Pred);
4945 }
4946 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
4947 return nullptr;
4948
4949 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
4950 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
4951 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
4952 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
4954 if (PredOut != Pred && isKnownNonZero(A, Q))
4955 return new ICmpInst(PredOut, Op0, Op1);
4956
4957 return nullptr;
4958}
4959
4960/// Try to fold icmp (binop), X or icmp X, (binop).
4961/// TODO: A large part of this logic is duplicated in InstSimplify's
4962/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
4963/// duplication.
4965 const SimplifyQuery &SQ) {
4967 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4968
4969 // Special logic for binary operators.
4970 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
4971 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
4972 if (!BO0 && !BO1)
4973 return nullptr;
4974
4975 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
4976 return NewICmp;
4977
4978 const CmpInst::Predicate Pred = I.getPredicate();
4979 Value *X;
4980
4981 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
4982 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
4983 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
4984 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
4985 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
4986 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
4987 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
4988 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
4989 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
4990
4991 {
4992 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
4993 Constant *C;
4994 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
4995 m_ImmConstant(C)))) &&
4996 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
4998 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
4999 }
5000 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5001 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
5002 m_ImmConstant(C)))) &&
5003 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5005 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
5006 }
5007 }
5008
5009 {
5010 // Similar to above: an unsigned overflow comparison may use offset + mask:
5011 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5012 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5013 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5014 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5015 BinaryOperator *BO;
5016 const APInt *C;
5017 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5018 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5020 CmpInst::Predicate NewPred =
5022 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5023 return new ICmpInst(NewPred, Op1, Zero);
5024 }
5025
5026 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5027 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5029 CmpInst::Predicate NewPred =
5031 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5032 return new ICmpInst(NewPred, Op0, Zero);
5033 }
5034 }
5035
5036 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5037 bool Op0HasNUW = false, Op1HasNUW = false;
5038 bool Op0HasNSW = false, Op1HasNSW = false;
5039 // Analyze the case when either Op0 or Op1 is an add instruction.
5040 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5041 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5042 bool &HasNSW, bool &HasNUW) -> bool {
5043 if (isa<OverflowingBinaryOperator>(BO)) {
5044 HasNUW = BO.hasNoUnsignedWrap();
5045 HasNSW = BO.hasNoSignedWrap();
5046 return ICmpInst::isEquality(Pred) ||
5047 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5048 (CmpInst::isSigned(Pred) && HasNSW);
5049 } else if (BO.getOpcode() == Instruction::Or) {
5050 HasNUW = true;
5051 HasNSW = true;
5052 return true;
5053 } else {
5054 return false;
5055 }
5056 };
5057 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5058
5059 if (BO0) {
5060 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
5061 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5062 }
5063 if (BO1) {
5064 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
5065 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5066 }
5067
5068 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5069 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5070 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5071 return new ICmpInst(Pred, A == Op1 ? B : A,
5072 Constant::getNullValue(Op1->getType()));
5073
5074 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5075 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5076 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5077 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
5078 C == Op0 ? D : C);
5079
5080 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5081 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5082 NoOp1WrapProblem) {
5083 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5084 Value *Y, *Z;
5085 if (A == C) {
5086 // C + B == C + D -> B == D
5087 Y = B;
5088 Z = D;
5089 } else if (A == D) {
5090 // D + B == C + D -> B == C
5091 Y = B;
5092 Z = C;
5093 } else if (B == C) {
5094 // A + C == C + D -> A == D
5095 Y = A;
5096 Z = D;
5097 } else {
5098 assert(B == D);
5099 // A + D == C + D -> A == C
5100 Y = A;
5101 Z = C;
5102 }
5103 return new ICmpInst(Pred, Y, Z);
5104 }
5105
5106 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5107 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
5108 match(B, m_AllOnes()))
5109 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
5110
5111 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5112 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
5113 match(B, m_AllOnes()))
5114 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
5115
5116 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5117 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
5118 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
5119
5120 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5121 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
5122 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
5123
5124 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5125 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
5126 match(D, m_AllOnes()))
5127 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
5128
5129 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5130 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
5131 match(D, m_AllOnes()))
5132 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
5133
5134 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5135 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
5136 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
5137
5138 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5139 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
5140 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
5141
5142 // TODO: The subtraction-related identities shown below also hold, but
5143 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5144 // wouldn't happen even if they were implemented.
5145 //
5146 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5147 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5148 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5149 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5150
5151 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5152 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One()))
5153 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1);
5154
5155 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5156 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One()))
5157 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1);
5158
5159 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5160 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One()))
5161 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C);
5162
5163 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5164 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One()))
5165 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C);
5166
5167 // if C1 has greater magnitude than C2:
5168 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5169 // s.t. C3 = C1 - C2
5170 //
5171 // if C2 has greater magnitude than C1:
5172 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5173 // s.t. C3 = C2 - C1
5174 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5175 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5176 const APInt *AP1, *AP2;
5177 // TODO: Support non-uniform vectors.
5178 // TODO: Allow poison passthrough if B or D's element is poison.
5179 if (match(B, m_APIntAllowPoison(AP1)) &&
5180 match(D, m_APIntAllowPoison(AP2)) &&
5181 AP1->isNegative() == AP2->isNegative()) {
5182 APInt AP1Abs = AP1->abs();
5183 APInt AP2Abs = AP2->abs();
5184 if (AP1Abs.uge(AP2Abs)) {
5185 APInt Diff = *AP1 - *AP2;
5186 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5187 Value *NewAdd = Builder.CreateAdd(
5188 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
5189 return new ICmpInst(Pred, NewAdd, C);
5190 } else {
5191 APInt Diff = *AP2 - *AP1;
5192 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5193 Value *NewAdd = Builder.CreateAdd(
5194 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
5195 return new ICmpInst(Pred, A, NewAdd);
5196 }
5197 }
5198 Constant *Cst1, *Cst2;
5199 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5200 ICmpInst::isEquality(Pred)) {
5201 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5202 Value *NewAdd = Builder.CreateAdd(C, Diff);
5203 return new ICmpInst(Pred, A, NewAdd);
5204 }
5205 }
5206
5207 // Analyze the case when either Op0 or Op1 is a sub instruction.
5208 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5209 A = nullptr;
5210 B = nullptr;
5211 C = nullptr;
5212 D = nullptr;
5213 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5214 A = BO0->getOperand(0);
5215 B = BO0->getOperand(1);
5216 }
5217 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5218 C = BO1->getOperand(0);
5219 D = BO1->getOperand(1);
5220 }
5221
5222 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5223 if (A == Op1 && NoOp0WrapProblem)
5224 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5225 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5226 if (C == Op0 && NoOp1WrapProblem)
5227 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5228
5229 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5230 // (A - B) u>/u<= A --> B u>/u<= A
5231 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5232 return new ICmpInst(Pred, B, A);
5233 // C u</u>= (C - D) --> C u</u>= D
5234 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5235 return new ICmpInst(Pred, C, D);
5236 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5237 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5238 isKnownNonZero(B, Q))
5240 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5241 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5242 isKnownNonZero(D, Q))
5244
5245 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5246 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5247 return new ICmpInst(Pred, A, C);
5248
5249 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5250 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5251 return new ICmpInst(Pred, D, B);
5252
5253 // icmp (0-X) < cst --> x > -cst
5254 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5255 Value *X;
5256 if (match(BO0, m_Neg(m_Value(X))))
5257 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5258 if (RHSC->isNotMinSignedValue())
5259 return new ICmpInst(I.getSwappedPredicate(), X,
5260 ConstantExpr::getNeg(RHSC));
5261 }
5262
5263 if (Instruction * R = foldICmpXorXX(I, Q, *this))
5264 return R;
5265 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5266 return R;
5267
5268 {
5269 // Try to remove shared multiplier from comparison:
5270 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z
5271 Value *X, *Y, *Z;
5272 if (Pred == ICmpInst::getUnsignedPredicate(Pred) &&
5273 ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5274 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5275 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5276 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))))) {
5277 bool NonZero;
5278 if (ICmpInst::isEquality(Pred)) {
5279 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5280 // if Z % 2 != 0
5281 // X * Z eq/ne Y * Z -> X eq/ne Y
5282 if (ZKnown.countMaxTrailingZeros() == 0)
5283 return new ICmpInst(Pred, X, Y);
5284 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5285 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5286 // X * Z eq/ne Y * Z -> X eq/ne Y
5287 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5288 return new ICmpInst(Pred, X, Y);
5289 } else
5290 NonZero = isKnownNonZero(Z, Q);
5291
5292 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5293 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5294 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5295 return new ICmpInst(Pred, X, Y);
5296 }
5297 }
5298
5299 BinaryOperator *SRem = nullptr;
5300 // icmp (srem X, Y), Y
5301 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5302 SRem = BO0;
5303 // icmp Y, (srem X, Y)
5304 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5305 Op0 == BO1->getOperand(1))
5306 SRem = BO1;
5307 if (SRem) {
5308 // We don't check hasOneUse to avoid increasing register pressure because
5309 // the value we use is the same value this instruction was already using.
5310 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5311 default:
5312 break;
5313 case ICmpInst::ICMP_EQ:
5314 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5315 case ICmpInst::ICMP_NE:
5316 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5317 case ICmpInst::ICMP_SGT:
5318 case ICmpInst::ICMP_SGE:
5319 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5321 case ICmpInst::ICMP_SLT:
5322 case ICmpInst::ICMP_SLE:
5323 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5325 }
5326 }
5327
5328 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5329 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5330 BO0->getOperand(1) == BO1->getOperand(1)) {
5331 switch (BO0->getOpcode()) {
5332 default:
5333 break;
5334 case Instruction::Add:
5335 case Instruction::Sub:
5336 case Instruction::Xor: {
5337 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5338 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5339
5340 const APInt *C;
5341 if (match(BO0->getOperand(1), m_APInt(C))) {
5342 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5343 if (C->isSignMask()) {
5344 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5345 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5346 }
5347
5348 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5349 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5350 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5351 NewPred = I.getSwappedPredicate(NewPred);
5352 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5353 }
5354 }
5355 break;
5356 }
5357 case Instruction::Mul: {
5358 if (!I.isEquality())
5359 break;
5360
5361 const APInt *C;
5362 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5363 !C->isOne()) {
5364 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5365 // Mask = -1 >> count-trailing-zeros(C).
5366 if (unsigned TZs = C->countr_zero()) {
5367 Constant *Mask = ConstantInt::get(
5368 BO0->getType(),
5369 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5370 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5371 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5372 return new ICmpInst(Pred, And1, And2);
5373 }
5374 }
5375 break;
5376 }
5377 case Instruction::UDiv:
5378 case Instruction::LShr:
5379 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5380 break;
5381 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5382
5383 case Instruction::SDiv:
5384 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5385 !BO0->isExact() || !BO1->isExact())
5386 break;
5387 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5388
5389 case Instruction::AShr:
5390 if (!BO0->isExact() || !BO1->isExact())
5391 break;
5392 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5393
5394 case Instruction::Shl: {
5395 bool NUW = Op0HasNUW && Op1HasNUW;
5396 bool NSW = Op0HasNSW && Op1HasNSW;
5397 if (!NUW && !NSW)
5398 break;
5399 if (!NSW && I.isSigned())
5400 break;
5401 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5402 }
5403 }
5404 }
5405
5406 if (BO0) {
5407 // Transform A & (L - 1) `ult` L --> L != 0
5408 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5409 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5410
5411 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5412 auto *Zero = Constant::getNullValue(BO0->getType());
5413 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5414 }
5415 }
5416
5417 // For unsigned predicates / eq / ne:
5418 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5419 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5420 if (!ICmpInst::isSigned(Pred)) {
5421 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5422 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5423 Constant::getNullValue(Op1->getType()));
5424 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5425 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5426 Constant::getNullValue(Op0->getType()), Op0);
5427 }
5428
5430 return replaceInstUsesWith(I, V);
5431
5432 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5433 return R;
5434
5436 return replaceInstUsesWith(I, V);
5437
5439 return replaceInstUsesWith(I, V);
5440
5441 return nullptr;
5442}
5443
5444/// Fold icmp Pred min|max(X, Y), Z.
5447 Value *Z,
5448 ICmpInst::Predicate Pred) {
5449 Value *X = MinMax->getLHS();
5450 Value *Y = MinMax->getRHS();
5451 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5452 return nullptr;
5453 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5454 // Revert the transform signed pred -> unsigned pred
5455 // TODO: We can flip the signedness of predicate if both operands of icmp
5456 // are negative.
5460 } else
5461 return nullptr;
5462 }
5464 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5465 if (!Val)
5466 return std::nullopt;
5467 if (match(Val, m_One()))
5468 return true;
5469 if (match(Val, m_Zero()))
5470 return false;
5471 return std::nullopt;
5472 };
5473 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5474 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5475 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5476 return nullptr;
5477 if (!CmpXZ.has_value()) {
5478 std::swap(X, Y);
5479 std::swap(CmpXZ, CmpYZ);
5480 }
5481
5482 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5483 if (CmpYZ.has_value())
5484 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5485 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5486 };
5487
5488 switch (Pred) {
5489 case ICmpInst::ICMP_EQ:
5490 case ICmpInst::ICMP_NE: {
5491 // If X == Z:
5492 // Expr Result
5493 // min(X, Y) == Z X <= Y
5494 // max(X, Y) == Z X >= Y
5495 // min(X, Y) != Z X > Y
5496 // max(X, Y) != Z X < Y
5497 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5498 ICmpInst::Predicate NewPred =
5499 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5500 if (Pred == ICmpInst::ICMP_NE)
5501 NewPred = ICmpInst::getInversePredicate(NewPred);
5502 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5503 }
5504 // Otherwise (X != Z):
5505 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5506 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5507 if (!MinMaxCmpXZ.has_value()) {
5508 std::swap(X, Y);
5509 std::swap(CmpXZ, CmpYZ);
5510 // Re-check pre-condition X != Z
5511 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5512 break;
5513 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5514 }
5515 if (!MinMaxCmpXZ.has_value())
5516 break;
5517 if (*MinMaxCmpXZ) {
5518 // Expr Fact Result
5519 // min(X, Y) == Z X < Z false
5520 // max(X, Y) == Z X > Z false
5521 // min(X, Y) != Z X < Z true
5522 // max(X, Y) != Z X > Z true
5523 return replaceInstUsesWith(
5524 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5525 } else {
5526 // Expr Fact Result
5527 // min(X, Y) == Z X > Z Y == Z
5528 // max(X, Y) == Z X < Z Y == Z
5529 // min(X, Y) != Z X > Z Y != Z
5530 // max(X, Y) != Z X < Z Y != Z
5531 return FoldIntoCmpYZ();
5532 }
5533 break;
5534 }
5535 case ICmpInst::ICMP_SLT:
5536 case ICmpInst::ICMP_ULT:
5537 case ICmpInst::ICMP_SLE:
5538 case ICmpInst::ICMP_ULE:
5539 case ICmpInst::ICMP_SGT:
5540 case ICmpInst::ICMP_UGT:
5541 case ICmpInst::ICMP_SGE:
5542 case ICmpInst::ICMP_UGE: {
5543 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5544 if (*CmpXZ) {
5545 if (IsSame) {
5546 // Expr Fact Result
5547 // min(X, Y) < Z X < Z true
5548 // min(X, Y) <= Z X <= Z true
5549 // max(X, Y) > Z X > Z true
5550 // max(X, Y) >= Z X >= Z true
5551 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5552 } else {
5553 // Expr Fact Result
5554 // max(X, Y) < Z X < Z Y < Z
5555 // max(X, Y) <= Z X <= Z Y <= Z
5556 // min(X, Y) > Z X > Z Y > Z
5557 // min(X, Y) >= Z X >= Z Y >= Z
5558 return FoldIntoCmpYZ();
5559 }
5560 } else {
5561 if (IsSame) {
5562 // Expr Fact Result
5563 // min(X, Y) < Z X >= Z Y < Z
5564 // min(X, Y) <= Z X > Z Y <= Z
5565 // max(X, Y) > Z X <= Z Y > Z
5566 // max(X, Y) >= Z X < Z Y >= Z
5567 return FoldIntoCmpYZ();
5568 } else {
5569 // Expr Fact Result
5570 // max(X, Y) < Z X >= Z false
5571 // max(X, Y) <= Z X > Z false
5572 // min(X, Y) > Z X <= Z false
5573 // min(X, Y) >= Z X < Z false
5574 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5575 }
5576 }
5577 break;
5578 }
5579 default:
5580 break;
5581 }
5582
5583 return nullptr;
5584}
5585
5586// Canonicalize checking for a power-of-2-or-zero value:
5588 InstCombiner::BuilderTy &Builder) {
5589 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5590 const CmpInst::Predicate Pred = I.getPredicate();
5591 Value *A = nullptr;
5592 bool CheckIs;
5593 if (I.isEquality()) {
5594 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5595 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5596 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5597 m_Deferred(A)))) ||
5598 !match(Op1, m_ZeroInt()))
5599 A = nullptr;
5600
5601 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5602 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5603 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5604 A = Op1;
5605 else if (match(Op1,
5607 A = Op0;
5608
5609 CheckIs = Pred == ICmpInst::ICMP_EQ;
5610 } else if (ICmpInst::isUnsigned(Pred)) {
5611 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5612 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5613
5614 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5616 m_Specific(Op1))))) {
5617 A = Op1;
5618 CheckIs = Pred == ICmpInst::ICMP_UGE;
5619 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5621 m_Specific(Op0))))) {
5622 A = Op0;
5623 CheckIs = Pred == ICmpInst::ICMP_ULE;
5624 }
5625 }
5626
5627 if (A) {
5628 Type *Ty = A->getType();
5629 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5630 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5631 ConstantInt::get(Ty, 2))
5632 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5633 ConstantInt::get(Ty, 1));
5634 }
5635
5636 return nullptr;
5637}
5638
5640 if (!I.isEquality())
5641 return nullptr;
5642
5643 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5644 const CmpInst::Predicate Pred = I.getPredicate();
5645 Value *A, *B, *C, *D;
5646 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5647 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5648 Value *OtherVal = A == Op1 ? B : A;
5649 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5650 }
5651
5652 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
5653 // A^c1 == C^c2 --> A == C^(c1^c2)
5654 ConstantInt *C1, *C2;
5655 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
5656 Op1->hasOneUse()) {
5657 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
5659 return new ICmpInst(Pred, A, Xor);
5660 }
5661
5662 // A^B == A^D -> B == D
5663 if (A == C)
5664 return new ICmpInst(Pred, B, D);
5665 if (A == D)
5666 return new ICmpInst(Pred, B, C);
5667 if (B == C)
5668 return new ICmpInst(Pred, A, D);
5669 if (B == D)
5670 return new ICmpInst(Pred, A, C);
5671 }
5672 }
5673
5674 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
5675 // A == (A^B) -> B == 0
5676 Value *OtherVal = A == Op0 ? B : A;
5677 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5678 }
5679
5680 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
5681 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
5682 match(Op1, m_And(m_Value(C), m_Value(D)))) {
5683 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
5684
5685 if (A == C) {
5686 X = B;
5687 Y = D;
5688 Z = A;
5689 } else if (A == D) {
5690 X = B;
5691 Y = C;
5692 Z = A;
5693 } else if (B == C) {
5694 X = A;
5695 Y = D;
5696 Z = B;
5697 } else if (B == D) {
5698 X = A;
5699 Y = C;
5700 Z = B;
5701 }
5702
5703 if (X) {
5704 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
5705 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
5706 // instructions.
5707 const APInt *C0, *C1;
5708 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
5709 (*C0 ^ *C1).isNegatedPowerOf2();
5710
5711 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
5712 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
5713 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
5714 int UseCnt =
5715 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
5716 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
5717 if (XorIsNegP2 || UseCnt >= 2) {
5718 // Build (X^Y) & Z
5719 Op1 = Builder.CreateXor(X, Y);
5720 Op1 = Builder.CreateAnd(Op1, Z);
5721 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
5722 }
5723 }
5724 }
5725
5726 {
5727 // Similar to above, but specialized for constant because invert is needed:
5728 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
5729 Value *X, *Y;
5730 Constant *C;
5731 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
5732 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
5735 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
5736 }
5737 }
5738
5739 if (match(Op1, m_ZExt(m_Value(A))) &&
5740 (Op0->hasOneUse() || Op1->hasOneUse())) {
5741 // (B & (Pow2C-1)) == zext A --> A == trunc B
5742 // (B & (Pow2C-1)) != zext A --> A != trunc B
5743 const APInt *MaskC;
5744 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
5745 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
5746 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
5747 }
5748
5749 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
5750 // For lshr and ashr pairs.
5751 const APInt *AP1, *AP2;
5752 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5753 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
5754 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5755 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
5756 if (AP1 != AP2)
5757 return nullptr;
5758 unsigned TypeBits = AP1->getBitWidth();
5759 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
5760 if (ShAmt < TypeBits && ShAmt != 0) {
5761 ICmpInst::Predicate NewPred =
5763 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5764 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
5765 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
5766 }
5767 }
5768
5769 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
5770 ConstantInt *Cst1;
5771 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
5772 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
5773 unsigned TypeBits = Cst1->getBitWidth();
5774 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
5775 if (ShAmt < TypeBits && ShAmt != 0) {
5776 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5777 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
5779 I.getName() + ".mask");
5780 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
5781 }
5782 }
5783
5784 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
5785 // "icmp (and X, mask), cst"
5786 uint64_t ShAmt = 0;
5787 if (Op0->hasOneUse() &&
5788 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
5789 match(Op1, m_ConstantInt(Cst1)) &&
5790 // Only do this when A has multiple uses. This is most important to do
5791 // when it exposes other optimizations.
5792 !A->hasOneUse()) {
5793 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
5794
5795 if (ShAmt < ASize) {
5796 APInt MaskV =
5798 MaskV <<= ShAmt;
5799
5800 APInt CmpV = Cst1->getValue().zext(ASize);
5801 CmpV <<= ShAmt;
5802
5803 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
5804 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
5805 }
5806 }
5807
5809 return ICmp;
5810
5811 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks the
5812 // top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s INT_MAX",
5813 // which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a few steps
5814 // of instcombine.
5815 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
5816 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
5818 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
5819 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
5821 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
5822 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
5824 Add, ConstantInt::get(A->getType(), C.shl(1)));
5825 }
5826
5827 // Canonicalize:
5828 // Assume B_Pow2 != 0
5829 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
5830 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
5831 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
5832 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, 0, &I))
5833 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
5835
5836 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
5837 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, 0, &I))
5838 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
5839 ConstantInt::getNullValue(Op1->getType()));
5840
5841 // Canonicalize:
5842 // icmp eq/ne X, OneUse(rotate-right(X))
5843 // -> icmp eq/ne X, rotate-left(X)
5844 // We generally try to convert rotate-right -> rotate-left, this just
5845 // canonicalizes another case.
5846 if (match(&I, m_c_ICmp(m_Value(A),
5847 m_OneUse(m_Intrinsic<Intrinsic::fshr>(
5848 m_Deferred(A), m_Deferred(A), m_Value(B))))))
5849 return new ICmpInst(
5850 Pred, A,
5851 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
5852
5853 // Canonicalize:
5854 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
5855 Constant *Cst;
5858 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
5859
5860 {
5861 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5862 auto m_Matcher =
5865 m_Sub(m_Value(B), m_Deferred(A)));
5866 std::optional<bool> IsZero = std::nullopt;
5867 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),
5868 m_Deferred(A))))
5869 IsZero = false;
5870 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5871 else if (match(&I,
5872 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))
5873 IsZero = true;
5874
5875 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, /*Depth*/ 0, &I))
5876 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5877 // -> (icmp eq/ne (and X, P2), 0)
5878 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5879 // -> (icmp eq/ne (and X, P2), P2)
5880 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
5881 *IsZero ? A
5882 : ConstantInt::getNullValue(A->getType()));
5883 }
5884
5885 return nullptr;
5886}
5887
5889 ICmpInst::Predicate Pred = ICmp.getPredicate();
5890 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
5891
5892 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
5893 // The trunc masks high bits while the compare may effectively mask low bits.
5894 Value *X;
5895 const APInt *C;
5896 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
5897 return nullptr;
5898
5899 // This matches patterns corresponding to tests of the signbit as well as:
5900 // (trunc X) u< C --> (X & -C) == 0 (are all masked-high-bits clear?)
5901 // (trunc X) u> C --> (X & ~C) != 0 (are any masked-high-bits set?)
5902 APInt Mask;
5903 if (decomposeBitTestICmp(Op0, Op1, Pred, X, Mask, true /* WithTrunc */)) {
5904 Value *And = Builder.CreateAnd(X, Mask);
5905 Constant *Zero = ConstantInt::getNullValue(X->getType());
5906 return new ICmpInst(Pred, And, Zero);
5907 }
5908
5909 unsigned SrcBits = X->getType()->getScalarSizeInBits();
5910 if (Pred == ICmpInst::ICMP_ULT && C->isNegatedPowerOf2()) {
5911 // If C is a negative power-of-2 (high-bit mask):
5912 // (trunc X) u< C --> (X & C) != C (are any masked-high-bits clear?)
5913 Constant *MaskC = ConstantInt::get(X->getType(), C->zext(SrcBits));
5914 Value *And = Builder.CreateAnd(X, MaskC);
5915 return new ICmpInst(ICmpInst::ICMP_NE, And, MaskC);
5916 }
5917
5918 if (Pred == ICmpInst::ICMP_UGT && (~*C).isPowerOf2()) {
5919 // If C is not-of-power-of-2 (one clear bit):
5920 // (trunc X) u> C --> (X & (C+1)) == C+1 (are all masked-high-bits set?)
5921 Constant *MaskC = ConstantInt::get(X->getType(), (*C + 1).zext(SrcBits));
5922 Value *And = Builder.CreateAnd(X, MaskC);
5923 return new ICmpInst(ICmpInst::ICMP_EQ, And, MaskC);
5924 }
5925
5926 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
5927 if (II->getIntrinsicID() == Intrinsic::cttz ||
5928 II->getIntrinsicID() == Intrinsic::ctlz) {
5929 unsigned MaxRet = SrcBits;
5930 // If the "is_zero_poison" argument is set, then we know at least
5931 // one bit is set in the input, so the result is always at least one
5932 // less than the full bitwidth of that input.
5933 if (match(II->getArgOperand(1), m_One()))
5934 MaxRet--;
5935
5936 // Make sure the destination is wide enough to hold the largest output of
5937 // the intrinsic.
5938 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
5939 if (Instruction *I =
5940 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
5941 return I;
5942 }
5943 }
5944
5945 return nullptr;
5946}
5947
5949 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
5950 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
5951 Value *X;
5952 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
5953 return nullptr;
5954
5955 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
5956 bool IsSignedCmp = ICmp.isSigned();
5957
5958 // icmp Pred (ext X), (ext Y)
5959 Value *Y;
5960 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
5961 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
5962 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
5963
5964 if (IsZext0 != IsZext1) {
5965 // If X and Y and both i1
5966 // (icmp eq/ne (zext X) (sext Y))
5967 // eq -> (icmp eq (or X, Y), 0)
5968 // ne -> (icmp ne (or X, Y), 0)
5969 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
5970 Y->getType()->isIntOrIntVectorTy(1))
5971 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
5972 Constant::getNullValue(X->getType()));
5973
5974 // If we have mismatched casts and zext has the nneg flag, we can
5975 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
5976
5977 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
5978 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
5979
5980 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
5981 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
5982
5983 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
5984 IsSignedExt = true;
5985 else
5986 return nullptr;
5987 }
5988
5989 // Not an extension from the same type?
5990 Type *XTy = X->getType(), *YTy = Y->getType();
5991 if (XTy != YTy) {
5992 // One of the casts must have one use because we are creating a new cast.
5993 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
5994 return nullptr;
5995 // Extend the narrower operand to the type of the wider operand.
5996 CastInst::CastOps CastOpcode =
5997 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
5998 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
5999 X = Builder.CreateCast(CastOpcode, X, YTy);
6000 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6001 Y = Builder.CreateCast(CastOpcode, Y, XTy);
6002 else
6003 return nullptr;
6004 }
6005
6006 // (zext X) == (zext Y) --> X == Y
6007 // (sext X) == (sext Y) --> X == Y
6008 if (ICmp.isEquality())
6009 return new ICmpInst(ICmp.getPredicate(), X, Y);
6010
6011 // A signed comparison of sign extended values simplifies into a
6012 // signed comparison.
6013 if (IsSignedCmp && IsSignedExt)
6014 return new ICmpInst(ICmp.getPredicate(), X, Y);
6015
6016 // The other three cases all fold into an unsigned comparison.
6017 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6018 }
6019
6020 // Below here, we are only folding a compare with constant.
6021 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
6022 if (!C)
6023 return nullptr;
6024
6025 // If a lossless truncate is possible...
6026 Type *SrcTy = CastOp0->getSrcTy();
6027 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
6028 if (Res) {
6029 if (ICmp.isEquality())
6030 return new ICmpInst(ICmp.getPredicate(), X, Res);
6031
6032 // A signed comparison of sign extended values simplifies into a
6033 // signed comparison.
6034 if (IsSignedExt && IsSignedCmp)
6035 return new ICmpInst(ICmp.getPredicate(), X, Res);
6036
6037 // The other three cases all fold into an unsigned comparison.
6038 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6039 }
6040
6041 // The re-extended constant changed, partly changed (in the case of a vector),
6042 // or could not be determined to be equal (in the case of a constant
6043 // expression), so the constant cannot be represented in the shorter type.
6044 // All the cases that fold to true or false will have already been handled
6045 // by simplifyICmpInst, so only deal with the tricky case.
6046 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
6047 return nullptr;
6048
6049 // Is source op positive?
6050 // icmp ult (sext X), C --> icmp sgt X, -1
6051 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6053
6054 // Is source op negative?
6055 // icmp ugt (sext X), C --> icmp slt X, 0
6056 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6058}
6059
6060/// Handle icmp (cast x), (cast or constant).
6062 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6063 // icmp compares only pointer's value.
6064 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6065 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
6066 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
6067 if (SimplifiedOp0 || SimplifiedOp1)
6068 return new ICmpInst(ICmp.getPredicate(),
6069 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
6070 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
6071
6072 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
6073 if (!CastOp0)
6074 return nullptr;
6075 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
6076 return nullptr;
6077
6078 Value *Op0Src = CastOp0->getOperand(0);
6079 Type *SrcTy = CastOp0->getSrcTy();
6080 Type *DestTy = CastOp0->getDestTy();
6081
6082 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6083 // integer type is the same size as the pointer type.
6084 auto CompatibleSizes = [&](Type *SrcTy, Type *DestTy) {
6085 if (isa<VectorType>(SrcTy)) {
6086 SrcTy = cast<VectorType>(SrcTy)->getElementType();
6087 DestTy = cast<VectorType>(DestTy)->getElementType();
6088 }
6089 return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
6090 };
6091 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6092 CompatibleSizes(SrcTy, DestTy)) {
6093 Value *NewOp1 = nullptr;
6094 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
6095 Value *PtrSrc = PtrToIntOp1->getOperand(0);
6096 if (PtrSrc->getType() == Op0Src->getType())
6097 NewOp1 = PtrToIntOp1->getOperand(0);
6098 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6099 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
6100 }
6101
6102 if (NewOp1)
6103 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6104 }
6105
6106 if (Instruction *R = foldICmpWithTrunc(ICmp))
6107 return R;
6108
6109 return foldICmpWithZextOrSext(ICmp);
6110}
6111
6112static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned) {
6113 switch (BinaryOp) {
6114 default:
6115 llvm_unreachable("Unsupported binary op");
6116 case Instruction::Add:
6117 case Instruction::Sub:
6118 return match(RHS, m_Zero());
6119 case Instruction::Mul:
6120 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
6121 match(RHS, m_One());
6122 }
6123}
6124
6127 bool IsSigned, Value *LHS, Value *RHS,
6128 Instruction *CxtI) const {
6129 switch (BinaryOp) {
6130 default:
6131 llvm_unreachable("Unsupported binary op");
6132 case Instruction::Add:
6133 if (IsSigned)
6134 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6135 else
6136 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6137 case Instruction::Sub:
6138 if (IsSigned)
6139 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6140 else
6141 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6142 case Instruction::Mul:
6143 if (IsSigned)
6144 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6145 else
6146 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6147 }
6148}
6149
6150bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6151 bool IsSigned, Value *LHS,
6152 Value *RHS, Instruction &OrigI,
6153 Value *&Result,
6154 Constant *&Overflow) {
6155 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
6156 std::swap(LHS, RHS);
6157
6158 // If the overflow check was an add followed by a compare, the insertion point
6159 // may be pointing to the compare. We want to insert the new instructions
6160 // before the add in case there are uses of the add between the add and the
6161 // compare.
6162 Builder.SetInsertPoint(&OrigI);
6163
6164 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
6165 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
6166 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
6167
6168 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6169 Result = LHS;
6170 Overflow = ConstantInt::getFalse(OverflowTy);
6171 return true;
6172 }
6173
6174 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
6176 return false;
6179 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6180 Result->takeName(&OrigI);
6181 Overflow = ConstantInt::getTrue(OverflowTy);
6182 return true;
6184 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6185 Result->takeName(&OrigI);
6186 Overflow = ConstantInt::getFalse(OverflowTy);
6187 if (auto *Inst = dyn_cast<Instruction>(Result)) {
6188 if (IsSigned)
6189 Inst->setHasNoSignedWrap();
6190 else
6191 Inst->setHasNoUnsignedWrap();
6192 }
6193 return true;
6194 }
6195
6196 llvm_unreachable("Unexpected overflow result");
6197}
6198
6199/// Recognize and process idiom involving test for multiplication
6200/// overflow.
6201///
6202/// The caller has matched a pattern of the form:
6203/// I = cmp u (mul(zext A, zext B), V
6204/// The function checks if this is a test for overflow and if so replaces
6205/// multiplication with call to 'mul.with.overflow' intrinsic.
6206///
6207/// \param I Compare instruction.
6208/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6209/// the compare instruction. Must be of integer type.
6210/// \param OtherVal The other argument of compare instruction.
6211/// \returns Instruction which must replace the compare instruction, NULL if no
6212/// replacement required.
6214 const APInt *OtherVal,
6215 InstCombinerImpl &IC) {
6216 // Don't bother doing this transformation for pointers, don't do it for
6217 // vectors.
6218 if (!isa<IntegerType>(MulVal->getType()))
6219 return nullptr;
6220
6221 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6222 if (!MulInstr)
6223 return nullptr;
6224 assert(MulInstr->getOpcode() == Instruction::Mul);
6225
6226 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6227 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6228 assert(LHS->getOpcode() == Instruction::ZExt);
6229 assert(RHS->getOpcode() == Instruction::ZExt);
6230 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6231
6232 // Calculate type and width of the result produced by mul.with.overflow.
6233 Type *TyA = A->getType(), *TyB = B->getType();
6234 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6235 WidthB = TyB->getPrimitiveSizeInBits();
6236 unsigned MulWidth;
6237 Type *MulType;
6238 if (WidthB > WidthA) {
6239 MulWidth = WidthB;
6240 MulType = TyB;
6241 } else {
6242 MulWidth = WidthA;
6243 MulType = TyA;
6244 }
6245
6246 // In order to replace the original mul with a narrower mul.with.overflow,
6247 // all uses must ignore upper bits of the product. The number of used low
6248 // bits must be not greater than the width of mul.with.overflow.
6249 if (MulVal->hasNUsesOrMore(2))
6250 for (User *U : MulVal->users()) {
6251 if (U == &I)
6252 continue;
6253 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6254 // Check if truncation ignores bits above MulWidth.
6255 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6256 if (TruncWidth > MulWidth)
6257 return nullptr;
6258 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6259 // Check if AND ignores bits above MulWidth.
6260 if (BO->getOpcode() != Instruction::And)
6261 return nullptr;
6262 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6263 const APInt &CVal = CI->getValue();
6264 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6265 return nullptr;
6266 } else {
6267 // In this case we could have the operand of the binary operation
6268 // being defined in another block, and performing the replacement
6269 // could break the dominance relation.
6270 return nullptr;
6271 }
6272 } else {
6273 // Other uses prohibit this transformation.
6274 return nullptr;
6275 }
6276 }
6277
6278 // Recognize patterns
6279 switch (I.getPredicate()) {
6280 case ICmpInst::ICMP_UGT: {
6281 // Recognize pattern:
6282 // mulval = mul(zext A, zext B)
6283 // cmp ugt mulval, max
6284 APInt MaxVal = APInt::getMaxValue(MulWidth);
6285 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6286 if (MaxVal.eq(*OtherVal))
6287 break; // Recognized
6288 return nullptr;
6289 }
6290
6291 case ICmpInst::ICMP_ULT: {
6292 // Recognize pattern:
6293 // mulval = mul(zext A, zext B)
6294 // cmp ule mulval, max + 1
6295 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6296 if (MaxVal.eq(*OtherVal))
6297 break; // Recognized
6298 return nullptr;
6299 }
6300
6301 default:
6302 return nullptr;
6303 }
6304
6305 InstCombiner::BuilderTy &Builder = IC.Builder;
6306 Builder.SetInsertPoint(MulInstr);
6307
6308 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6309 Value *MulA = A, *MulB = B;
6310 if (WidthA < MulWidth)
6311 MulA = Builder.CreateZExt(A, MulType);
6312 if (WidthB < MulWidth)
6313 MulB = Builder.CreateZExt(B, MulType);
6315 I.getModule(), Intrinsic::umul_with_overflow, MulType);
6316 CallInst *Call = Builder.CreateCall(F, {MulA, MulB}, "umul");
6317 IC.addToWorklist(MulInstr);
6318
6319 // If there are uses of mul result other than the comparison, we know that
6320 // they are truncation or binary AND. Change them to use result of
6321 // mul.with.overflow and adjust properly mask/size.
6322 if (MulVal->hasNUsesOrMore(2)) {
6323 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6324 for (User *U : make_early_inc_range(MulVal->users())) {
6325 if (U == &I)
6326 continue;
6327 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6328 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6329 IC.replaceInstUsesWith(*TI, Mul);
6330 else
6331 TI->setOperand(0, Mul);
6332 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6333 assert(BO->getOpcode() == Instruction::And);
6334 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6335 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6336 APInt ShortMask = CI->getValue().trunc(MulWidth);
6337 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6338 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6339 IC.replaceInstUsesWith(*BO, Zext);
6340 } else {
6341 llvm_unreachable("Unexpected Binary operation");
6342 }
6343 IC.addToWorklist(cast<Instruction>(U));
6344 }
6345 }
6346
6347 // The original icmp gets replaced with the overflow value, maybe inverted
6348 // depending on predicate.
6349 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6350 Value *Res = Builder.CreateExtractValue(Call, 1);
6351 return BinaryOperator::CreateNot(Res);
6352 }
6353
6354 return ExtractValueInst::Create(Call, 1);
6355}
6356
6357/// When performing a comparison against a constant, it is possible that not all
6358/// the bits in the LHS are demanded. This helper method computes the mask that
6359/// IS demanded.
6361 const APInt *RHS;
6362 if (!match(I.getOperand(1), m_APInt(RHS)))
6364
6365 // If this is a normal comparison, it demands all bits. If it is a sign bit
6366 // comparison, it only demands the sign bit.
6367 bool UnusedBit;
6368 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6370
6371 switch (I.getPredicate()) {
6372 // For a UGT comparison, we don't care about any bits that
6373 // correspond to the trailing ones of the comparand. The value of these
6374 // bits doesn't impact the outcome of the comparison, because any value
6375 // greater than the RHS must differ in a bit higher than these due to carry.
6376 case ICmpInst::ICMP_UGT:
6377 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6378
6379 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6380 // Any value less than the RHS must differ in a higher bit because of carries.
6381 case ICmpInst::ICMP_ULT:
6382 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6383
6384 default:
6386 }
6387}
6388
6389/// Check that one use is in the same block as the definition and all
6390/// other uses are in blocks dominated by a given block.
6391///
6392/// \param DI Definition
6393/// \param UI Use
6394/// \param DB Block that must dominate all uses of \p DI outside
6395/// the parent block
6396/// \return true when \p UI is the only use of \p DI in the parent block
6397/// and all other uses of \p DI are in blocks dominated by \p DB.
6398///
6400 const Instruction *UI,
6401 const BasicBlock *DB) const {
6402 assert(DI && UI && "Instruction not defined\n");
6403 // Ignore incomplete definitions.
6404 if (!DI->getParent())
6405 return false;
6406 // DI and UI must be in the same block.
6407 if (DI->getParent() != UI->getParent())
6408 return false;
6409 // Protect from self-referencing blocks.
6410 if (DI->getParent() == DB)
6411 return false;
6412 for (const User *U : DI->users()) {
6413 auto *Usr = cast<Instruction>(U);
6414 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6415 return false;
6416 }
6417 return true;
6418}
6419
6420/// Return true when the instruction sequence within a block is select-cmp-br.
6421static bool isChainSelectCmpBranch(const SelectInst *SI) {
6422 const BasicBlock *BB = SI->getParent();
6423 if (!BB)
6424 return false;
6425 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
6426 if (!BI || BI->getNumSuccessors() != 2)
6427 return false;
6428 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6429 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6430 return false;
6431 return true;
6432}
6433
6434/// True when a select result is replaced by one of its operands
6435/// in select-icmp sequence. This will eventually result in the elimination
6436/// of the select.
6437///
6438/// \param SI Select instruction
6439/// \param Icmp Compare instruction
6440/// \param SIOpd Operand that replaces the select
6441///
6442/// Notes:
6443/// - The replacement is global and requires dominator information
6444/// - The caller is responsible for the actual replacement
6445///
6446/// Example:
6447///
6448/// entry:
6449/// %4 = select i1 %3, %C* %0, %C* null
6450/// %5 = icmp eq %C* %4, null
6451/// br i1 %5, label %9, label %7
6452/// ...
6453/// ; <label>:7 ; preds = %entry
6454/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6455/// ...
6456///
6457/// can be transformed to
6458///
6459/// %5 = icmp eq %C* %0, null
6460/// %6 = select i1 %3, i1 %5, i1 true
6461/// br i1 %6, label %9, label %7
6462/// ...
6463/// ; <label>:7 ; preds = %entry
6464/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6465///
6466/// Similar when the first operand of the select is a constant or/and
6467/// the compare is for not equal rather than equal.
6468///
6469/// NOTE: The function is only called when the select and compare constants
6470/// are equal, the optimization can work only for EQ predicates. This is not a
6471/// major restriction since a NE compare should be 'normalized' to an equal
6472/// compare, which usually happens in the combiner and test case
6473/// select-cmp-br.ll checks for it.
6475 const ICmpInst *Icmp,
6476 const unsigned SIOpd) {
6477 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6479 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6480 // The check for the single predecessor is not the best that can be
6481 // done. But it protects efficiently against cases like when SI's
6482 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6483 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6484 // replaced can be reached on either path. So the uniqueness check
6485 // guarantees that the path all uses of SI (outside SI's parent) are on
6486 // is disjoint from all other paths out of SI. But that information
6487 // is more expensive to compute, and the trade-off here is in favor
6488 // of compile-time. It should also be noticed that we check for a single
6489 // predecessor and not only uniqueness. This to handle the situation when
6490 // Succ and Succ1 points to the same basic block.
6491 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6492 NumSel++;
6493 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6494 return true;
6495 }
6496 }
6497 return false;
6498}
6499
6500/// Try to fold the comparison based on range information we can get by checking
6501/// whether bits are known to be zero or one in the inputs.
6503 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6504 Type *Ty = Op0->getType();
6505 ICmpInst::Predicate Pred = I.getPredicate();
6506
6507 // Get scalar or pointer size.
6508 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6509 ? Ty->getScalarSizeInBits()
6511
6512 if (!BitWidth)
6513 return nullptr;
6514
6515 KnownBits Op0Known(BitWidth);
6516 KnownBits Op1Known(BitWidth);
6517
6518 {
6519 // Don't use dominating conditions when folding icmp using known bits. This
6520 // may convert signed into unsigned predicates in ways that other passes
6521 // (especially IndVarSimplify) may not be able to reliably undo.
6524 Op0Known, /*Depth=*/0, Q))
6525 return &I;
6526
6528 /*Depth=*/0, Q))
6529 return &I;
6530 }
6531
6532 // Given the known and unknown bits, compute a range that the LHS could be
6533 // in. Compute the Min, Max and RHS values based on the known bits. For the
6534 // EQ and NE we use unsigned values.
6535 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6536 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6537 if (I.isSigned()) {
6538 Op0Min = Op0Known.getSignedMinValue();
6539 Op0Max = Op0Known.getSignedMaxValue();
6540 Op1Min = Op1Known.getSignedMinValue();
6541 Op1Max = Op1Known.getSignedMaxValue();
6542 } else {
6543 Op0Min = Op0Known.getMinValue();
6544 Op0Max = Op0Known.getMaxValue();
6545 Op1Min = Op1Known.getMinValue();
6546 Op1Max = Op1Known.getMaxValue();
6547 }
6548
6549 // If Min and Max are known to be the same, then SimplifyDemandedBits figured
6550 // out that the LHS or RHS is a constant. Constant fold this now, so that
6551 // code below can assume that Min != Max.
6552 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
6553 return new ICmpInst(Pred, ConstantExpr::getIntegerValue(Ty, Op0Min), Op1);
6554 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
6555 return new ICmpInst(Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Min));
6556
6557 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6558 // min/max canonical compare with some other compare. That could lead to
6559 // conflict with select canonicalization and infinite looping.
6560 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6561 auto isMinMaxCmp = [&](Instruction &Cmp) {
6562 if (!Cmp.hasOneUse())
6563 return false;
6564 Value *A, *B;
6565 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6567 return false;
6568 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6569 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6570 };
6571 if (!isMinMaxCmp(I)) {
6572 switch (Pred) {
6573 default:
6574 break;
6575 case ICmpInst::ICMP_ULT: {
6576 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6577 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6578 const APInt *CmpC;
6579 if (match(Op1, m_APInt(CmpC))) {
6580 // A <u C -> A == C-1 if min(A)+1 == C
6581 if (*CmpC == Op0Min + 1)
6582 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6583 ConstantInt::get(Op1->getType(), *CmpC - 1));
6584 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6585 // exceeds the log2 of C.
6586 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6587 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6588 Constant::getNullValue(Op1->getType()));
6589 }
6590 break;
6591 }
6592 case ICmpInst::ICMP_UGT: {
6593 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6594 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6595 const APInt *CmpC;
6596 if (match(Op1, m_APInt(CmpC))) {
6597 // A >u C -> A == C+1 if max(a)-1 == C
6598 if (*CmpC == Op0Max - 1)
6599 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6600 ConstantInt::get(Op1->getType(), *CmpC + 1));
6601 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6602 // exceeds the log2 of C.
6603 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6604 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6605 Constant::getNullValue(Op1->getType()));
6606 }
6607 break;
6608 }
6609 case ICmpInst::ICMP_SLT: {
6610 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6611 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6612 const APInt *CmpC;
6613 if (match(Op1, m_APInt(CmpC))) {
6614 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6615 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6616 ConstantInt::get(Op1->getType(), *CmpC - 1));
6617 }
6618 break;
6619 }
6620 case ICmpInst::ICMP_SGT: {
6621 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6622 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6623 const APInt *CmpC;
6624 if (match(Op1, m_APInt(CmpC))) {
6625 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6626 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6627 ConstantInt::get(Op1->getType(), *CmpC + 1));
6628 }
6629 break;
6630 }
6631 }
6632 }
6633
6634 // Based on the range information we know about the LHS, see if we can
6635 // simplify this comparison. For example, (x&4) < 8 is always true.
6636 switch (Pred) {
6637 default:
6638 llvm_unreachable("Unknown icmp opcode!");
6639 case ICmpInst::ICMP_EQ:
6640 case ICmpInst::ICMP_NE: {
6641 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
6642 return replaceInstUsesWith(
6643 I, ConstantInt::getBool(I.getType(), Pred == CmpInst::ICMP_NE));
6644
6645 // If all bits are known zero except for one, then we know at most one bit
6646 // is set. If the comparison is against zero, then this is a check to see if
6647 // *that* bit is set.
6648 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
6649 if (Op1Known.isZero()) {
6650 // If the LHS is an AND with the same constant, look through it.
6651 Value *LHS = nullptr;
6652 const APInt *LHSC;
6653 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
6654 *LHSC != Op0KnownZeroInverted)
6655 LHS = Op0;
6656
6657 Value *X;
6658 const APInt *C1;
6659 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
6660 Type *XTy = X->getType();
6661 unsigned Log2C1 = C1->countr_zero();
6662 APInt C2 = Op0KnownZeroInverted;
6663 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
6664 if (C2Pow2.isPowerOf2()) {
6665 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
6666 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
6667 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
6668 unsigned Log2C2 = C2Pow2.countr_zero();
6669 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
6670 auto NewPred =
6672 return new ICmpInst(NewPred, X, CmpC);
6673 }
6674 }
6675 }
6676
6677 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
6678 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
6679 (Op0Known & Op1Known) == Op0Known)
6680 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6681 ConstantInt::getNullValue(Op1->getType()));
6682 break;
6683 }
6684 case ICmpInst::ICMP_ULT: {
6685 if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
6686 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6687 if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
6688 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6689 break;
6690 }
6691 case ICmpInst::ICMP_UGT: {
6692 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
6693 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6694 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
6695 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6696 break;
6697 }
6698 case ICmpInst::ICMP_SLT: {
6699 if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
6700 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6701 if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
6702 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6703 break;
6704 }
6705 case ICmpInst::ICMP_SGT: {
6706 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
6707 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6708 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
6709 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6710 break;
6711 }
6712 case ICmpInst::ICMP_SGE:
6713 assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
6714 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
6715 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6716 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
6717 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6718 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
6719 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6720 break;
6721 case ICmpInst::ICMP_SLE:
6722 assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
6723 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
6724 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6725 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
6726 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6727 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
6728 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6729 break;
6730 case ICmpInst::ICMP_UGE:
6731 assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
6732 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
6733 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6734 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
6735 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6736 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
6737 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6738 break;
6739 case ICmpInst::ICMP_ULE:
6740 assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
6741 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
6742 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6743 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
6744 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6745 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
6746 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6747 break;
6748 }
6749
6750 // Turn a signed comparison into an unsigned one if both operands are known to
6751 // have the same sign.
6752 if (I.isSigned() &&
6753 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
6754 (Op0Known.One.isNegative() && Op1Known.One.isNegative())))
6755 return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
6756
6757 return nullptr;
6758}
6759
6760/// If one operand of an icmp is effectively a bool (value range of {0,1}),
6761/// then try to reduce patterns based on that limit.
6763 Value *X, *Y;
6765
6766 // X must be 0 and bool must be true for "ULT":
6767 // X <u (zext i1 Y) --> (X == 0) & Y
6768 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
6769 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
6770 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
6771
6772 // X must be 0 or bool must be true for "ULE":
6773 // X <=u (sext i1 Y) --> (X == 0) | Y
6774 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
6775 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
6776 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
6777
6778 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
6779 ICmpInst::Predicate Pred1, Pred2;
6780 const APInt *C;
6781 Instruction *ExtI;
6782 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
6785 m_APInt(C)))))) &&
6786 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
6787 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
6788 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
6789 auto CreateRangeCheck = [&] {
6790 Value *CmpV1 =
6791 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
6792 Value *CmpV2 = Builder.CreateICmp(
6793 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
6795 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
6796 CmpV1, CmpV2);
6797 };
6798 if (C->isZero()) {
6799 if (Pred2 == ICmpInst::ICMP_EQ) {
6800 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
6801 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
6802 return replaceInstUsesWith(
6803 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6804 } else if (!IsSExt || HasOneUse) {
6805 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
6806 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
6807 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
6808 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X == -1
6809 return CreateRangeCheck();
6810 }
6811 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
6812 if (Pred2 == ICmpInst::ICMP_NE) {
6813 // icmp eq X, (zext (icmp ne X, 1)) --> false
6814 // icmp ne X, (zext (icmp ne X, 1)) --> true
6815 // icmp eq X, (sext (icmp ne X, -1)) --> false
6816 // icmp ne X, (sext (icmp ne X, -1)) --> true
6817 return replaceInstUsesWith(
6818 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6819 } else if (!IsSExt || HasOneUse) {
6820 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
6821 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
6822 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
6823 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
6824 return CreateRangeCheck();
6825 }
6826 } else {
6827 // when C != 0 && C != 1:
6828 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
6829 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
6830 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
6831 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
6832 // when C != 0 && C != -1:
6833 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
6834 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
6835 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
6836 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
6837 return ICmpInst::Create(
6838 Instruction::ICmp, Pred1, X,
6839 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
6840 ? (IsSExt ? -1 : 1)
6841 : 0));
6842 }
6843 }
6844
6845 return nullptr;
6846}
6847
6848std::optional<std::pair<CmpInst::Predicate, Constant *>>
6850 Constant *C) {
6852 "Only for relational integer predicates.");
6853
6854 Type *Type = C->getType();
6855 bool IsSigned = ICmpInst::isSigned(Pred);
6856
6858 bool WillIncrement =
6859 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
6860
6861 // Check if the constant operand can be safely incremented/decremented
6862 // without overflowing/underflowing.
6863 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
6864 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
6865 };
6866
6867 Constant *SafeReplacementConstant = nullptr;
6868 if (auto *CI = dyn_cast<ConstantInt>(C)) {
6869 // Bail out if the constant can't be safely incremented/decremented.
6870 if (!ConstantIsOk(CI))
6871 return std::nullopt;
6872 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
6873 unsigned NumElts = FVTy->getNumElements();
6874 for (unsigned i = 0; i != NumElts; ++i) {
6875 Constant *Elt = C->getAggregateElement(i);
6876 if (!Elt)
6877 return std::nullopt;
6878
6879 if (isa<UndefValue>(Elt))
6880 continue;
6881
6882 // Bail out if we can't determine if this constant is min/max or if we
6883 // know that this constant is min/max.
6884 auto *CI = dyn_cast<ConstantInt>(Elt);
6885 if (!CI || !ConstantIsOk(CI))
6886 return std::nullopt;
6887
6888 if (!SafeReplacementConstant)
6889 SafeReplacementConstant = CI;
6890 }
6891 } else if (isa<VectorType>(C->getType())) {
6892 // Handle scalable splat
6893 Value *SplatC = C->getSplatValue();
6894 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
6895 // Bail out if the constant can't be safely incremented/decremented.
6896 if (!CI || !ConstantIsOk(CI))
6897 return std::nullopt;
6898 } else {
6899 // ConstantExpr?
6900 return std::nullopt;
6901 }
6902
6903 // It may not be safe to change a compare predicate in the presence of
6904 // undefined elements, so replace those elements with the first safe constant
6905 // that we found.
6906 // TODO: in case of poison, it is safe; let's replace undefs only.
6907 if (C->containsUndefOrPoisonElement()) {
6908 assert(SafeReplacementConstant && "Replacement constant not set");
6909 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
6910 }
6911
6913
6914 // Increment or decrement the constant.
6915 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
6916 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
6917
6918 return std::make_pair(NewPred, NewC);
6919}
6920
6921/// If we have an icmp le or icmp ge instruction with a constant operand, turn
6922/// it into the appropriate icmp lt or icmp gt instruction. This transform
6923/// allows them to be folded in visitICmpInst.
6925 ICmpInst::Predicate Pred = I.getPredicate();
6926 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
6928 return nullptr;
6929
6930 Value *Op0 = I.getOperand(0);
6931 Value *Op1 = I.getOperand(1);
6932 auto *Op1C = dyn_cast<Constant>(Op1);
6933 if (!Op1C)
6934 return nullptr;
6935
6936 auto FlippedStrictness =
6938 if (!FlippedStrictness)
6939 return nullptr;
6940
6941 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
6942}
6943
6944/// If we have a comparison with a non-canonical predicate, if we can update
6945/// all the users, invert the predicate and adjust all the users.
6947 // Is the predicate already canonical?
6948 CmpInst::Predicate Pred = I.getPredicate();
6950 return nullptr;
6951
6952 // Can all users be adjusted to predicate inversion?
6953 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
6954 return nullptr;
6955
6956 // Ok, we can canonicalize comparison!
6957 // Let's first invert the comparison's predicate.
6958 I.setPredicate(CmpInst::getInversePredicate(Pred));
6959 I.setName(I.getName() + ".not");
6960
6961 // And, adapt users.
6963
6964 return &I;
6965}
6966
6967/// Integer compare with boolean values can always be turned into bitwise ops.
6969 InstCombiner::BuilderTy &Builder) {
6970 Value *A = I.getOperand(0), *B = I.getOperand(1);
6971 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
6972
6973 // A boolean compared to true/false can be simplified to Op0/true/false in
6974 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
6975 // Cases not handled by InstSimplify are always 'not' of Op0.
6976 if (match(B, m_Zero())) {
6977 switch (I.getPredicate()) {
6978 case CmpInst::ICMP_EQ: // A == 0 -> !A
6979 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
6980 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
6982 default:
6983 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6984 }
6985 } else if (match(B, m_One())) {
6986 switch (I.getPredicate()) {
6987 case CmpInst::ICMP_NE: // A != 1 -> !A
6988 case CmpInst::ICMP_ULT: // A <u 1 -> !A
6989 case CmpInst::ICMP_SGT: // A >s -1 -> !A
6991 default:
6992 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6993 }
6994 }
6995
6996 switch (I.getPredicate()) {
6997 default:
6998 llvm_unreachable("Invalid icmp instruction!");
6999 case ICmpInst::ICMP_EQ:
7000 // icmp eq i1 A, B -> ~(A ^ B)
7001 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
7002
7003 case ICmpInst::ICMP_NE:
7004 // icmp ne i1 A, B -> A ^ B
7005 return BinaryOperator::CreateXor(A, B);
7006
7007 case ICmpInst::ICMP_UGT:
7008 // icmp ugt -> icmp ult
7009 std::swap(A, B);
7010 [[fallthrough]];
7011 case ICmpInst::ICMP_ULT:
7012 // icmp ult i1 A, B -> ~A & B
7013 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
7014
7015 case ICmpInst::ICMP_SGT:
7016 // icmp sgt -> icmp slt
7017 std::swap(A, B);
7018 [[fallthrough]];
7019 case ICmpInst::ICMP_SLT:
7020 // icmp slt i1 A, B -> A & ~B
7021 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
7022
7023 case ICmpInst::ICMP_UGE:
7024 // icmp uge -> icmp ule
7025 std::swap(A, B);
7026 [[fallthrough]];
7027 case ICmpInst::ICMP_ULE:
7028 // icmp ule i1 A, B -> ~A | B
7029 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
7030
7031 case ICmpInst::ICMP_SGE:
7032 // icmp sge -> icmp sle
7033 std::swap(A, B);
7034 [[fallthrough]];
7035 case ICmpInst::ICMP_SLE:
7036 // icmp sle i1 A, B -> A | ~B
7037 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
7038 }
7039}
7040
7041// Transform pattern like:
7042// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7043// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7044// Into:
7045// (X l>> Y) != 0
7046// (X l>> Y) == 0
7048 InstCombiner::BuilderTy &Builder) {
7049 ICmpInst::Predicate Pred, NewPred;
7050 Value *X, *Y;
7051 if (match(&Cmp,
7052 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
7053 switch (Pred) {
7054 case ICmpInst::ICMP_ULE:
7055 NewPred = ICmpInst::ICMP_NE;
7056 break;
7057 case ICmpInst::ICMP_UGT:
7058 NewPred = ICmpInst::ICMP_EQ;
7059 break;
7060 default:
7061 return nullptr;
7062 }
7063 } else if (match(&Cmp, m_c_ICmp(Pred,
7066 m_Add(m_Shl(m_One(), m_Value(Y)),
7067 m_AllOnes()))),
7068 m_Value(X)))) {
7069 // The variant with 'add' is not canonical, (the variant with 'not' is)
7070 // we only get it because it has extra uses, and can't be canonicalized,
7071
7072 switch (Pred) {
7073 case ICmpInst::ICMP_ULT:
7074 NewPred = ICmpInst::ICMP_NE;
7075 break;
7076 case ICmpInst::ICMP_UGE:
7077 NewPred = ICmpInst::ICMP_EQ;
7078 break;
7079 default:
7080 return nullptr;
7081 }
7082 } else
7083 return nullptr;
7084
7085 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
7086 Constant *Zero = Constant::getNullValue(NewX->getType());
7087 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
7088}
7089
7091 InstCombiner::BuilderTy &Builder) {
7092 const CmpInst::Predicate Pred = Cmp.getPredicate();
7093 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
7094 Value *V1, *V2;
7095
7096 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7097 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
7098 if (auto *I = dyn_cast<Instruction>(V))
7099 I->copyIRFlags(&Cmp);
7100 Module *M = Cmp.getModule();
7101 Function *F =
7102 Intrinsic::getDeclaration(M, Intrinsic::vector_reverse, V->getType());
7103 return CallInst::Create(F, V);
7104 };
7105
7106 if (match(LHS, m_VecReverse(m_Value(V1)))) {
7107 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7108 if (match(RHS, m_VecReverse(m_Value(V2))) &&
7109 (LHS->hasOneUse() || RHS->hasOneUse()))
7110 return createCmpReverse(Pred, V1, V2);
7111
7112 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7113 if (LHS->hasOneUse() && isSplatValue(RHS))
7114 return createCmpReverse(Pred, V1, RHS);
7115 }
7116 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7117 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
7118 return createCmpReverse(Pred, LHS, V2);
7119
7120 ArrayRef<int> M;
7121 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
7122 return nullptr;
7123
7124 // If both arguments of the cmp are shuffles that use the same mask and
7125 // shuffle within a single vector, move the shuffle after the cmp:
7126 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7127 Type *V1Ty = V1->getType();
7128 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
7129 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7130 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
7131 return new ShuffleVectorInst(NewCmp, M);
7132 }
7133
7134 // Try to canonicalize compare with splatted operand and splat constant.
7135 // TODO: We could generalize this for more than splats. See/use the code in
7136 // InstCombiner::foldVectorBinop().
7137 Constant *C;
7138 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
7139 return nullptr;
7140
7141 // Length-changing splats are ok, so adjust the constants as needed:
7142 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7143 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7144 int MaskSplatIndex;
7145 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
7146 // We allow poison in matching, but this transform removes it for safety.
7147 // Demanded elements analysis should be able to recover some/all of that.
7148 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
7149 ScalarC);
7150 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7151 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
7152 return new ShuffleVectorInst(NewCmp, NewM);
7153 }
7154
7155 return nullptr;
7156}
7157
7158// extract(uadd.with.overflow(A, B), 0) ult A
7159// -> extract(uadd.with.overflow(A, B), 1)
7161 CmpInst::Predicate Pred = I.getPredicate();
7162 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7163
7164 Value *UAddOv;
7165 Value *A, *B;
7166 auto UAddOvResultPat = m_ExtractValue<0>(
7167 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));
7168 if (match(Op0, UAddOvResultPat) &&
7169 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7170 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
7171 (match(A, m_One()) || match(B, m_One()))) ||
7172 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
7173 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
7174 // extract(uadd.with.overflow(A, B), 0) < A
7175 // extract(uadd.with.overflow(A, 1), 0) == 0
7176 // extract(uadd.with.overflow(A, -1), 0) != -1
7177 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
7178 else if (match(Op1, UAddOvResultPat) &&
7179 Pred == ICmpInst::ICMP_UGT && (Op0 == A || Op0 == B))
7180 // A > extract(uadd.with.overflow(A, B), 0)
7181 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
7182 else
7183 return nullptr;
7184
7185 return ExtractValueInst::Create(UAddOv, 1);
7186}
7187
7189 if (!I.getOperand(0)->getType()->isPointerTy() ||
7191 I.getParent()->getParent(),
7192 I.getOperand(0)->getType()->getPointerAddressSpace())) {
7193 return nullptr;
7194 }
7195 Instruction *Op;
7196 if (match(I.getOperand(0), m_Instruction(Op)) &&
7197 match(I.getOperand(1), m_Zero()) &&
7198 Op->isLaunderOrStripInvariantGroup()) {
7199 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7200 Op->getOperand(0), I.getOperand(1));
7201 }
7202 return nullptr;
7203}
7204
7205/// This function folds patterns produced by lowering of reduce idioms, such as
7206/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7207/// attempts to generate fewer number of scalar comparisons instead of vector
7208/// comparisons when possible.
7210 InstCombiner::BuilderTy &Builder,
7211 const DataLayout &DL) {
7212 if (I.getType()->isVectorTy())
7213 return nullptr;
7214 ICmpInst::Predicate OuterPred, InnerPred;
7215 Value *LHS, *RHS;
7216
7217 // Match lowering of @llvm.vector.reduce.and. Turn
7218 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7219 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7220 /// %res = icmp <pred> i8 %scalar_ne, 0
7221 ///
7222 /// into
7223 ///
7224 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7225 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7226 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7227 ///
7228 /// for <pred> in {ne, eq}.
7229 if (!match(&I, m_ICmp(OuterPred,
7231 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7232 m_Zero())))
7233 return nullptr;
7234 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7235 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7236 return nullptr;
7237 unsigned NumBits =
7238 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7239 // TODO: Relax this to "not wider than max legal integer type"?
7240 if (!DL.isLegalInteger(NumBits))
7241 return nullptr;
7242
7243 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7244 auto *ScalarTy = Builder.getIntNTy(NumBits);
7245 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7246 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7247 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7248 I.getName());
7249 }
7250
7251 return nullptr;
7252}
7253
7254// This helper will be called with icmp operands in both orders.
7256 Value *Op0, Value *Op1,
7257 ICmpInst &CxtI) {
7258 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7259 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7260 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7261 return NI;
7262
7263 if (auto *SI = dyn_cast<SelectInst>(Op0))
7264 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7265 return NI;
7266
7267 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0))
7268 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7269 return Res;
7270
7271 {
7272 Value *X;
7273 const APInt *C;
7274 // icmp X+Cst, X
7275 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7276 return foldICmpAddOpConst(X, *C, Pred);
7277 }
7278
7279 // abs(X) >= X --> true
7280 // abs(X) u<= X --> true
7281 // abs(X) < X --> false
7282 // abs(X) u> X --> false
7283 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7284 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7285 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7286 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7287 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7288 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7289 {
7290 Value *X;
7291 Constant *C;
7292 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
7293 match(Op1, m_Specific(X))) {
7294 Value *NullValue = Constant::getNullValue(X->getType());
7295 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7296 const APInt SMin =
7297 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7298 bool IsIntMinPosion = C->isAllOnesValue();
7299 switch (Pred) {
7300 case CmpInst::ICMP_ULE:
7301 case CmpInst::ICMP_SGE:
7302 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7303 case CmpInst::ICMP_UGT:
7304 case CmpInst::ICMP_SLT:
7306 case CmpInst::ICMP_UGE:
7307 case CmpInst::ICMP_SLE:
7308 case CmpInst::ICMP_EQ: {
7309 return replaceInstUsesWith(
7310 CxtI, IsIntMinPosion
7311 ? Builder.CreateICmpSGT(X, AllOnesValue)
7313 X, ConstantInt::get(X->getType(), SMin + 1)));
7314 }
7315 case CmpInst::ICMP_ULT:
7316 case CmpInst::ICMP_SGT:
7317 case CmpInst::ICMP_NE: {
7318 return replaceInstUsesWith(
7319 CxtI, IsIntMinPosion
7320 ? Builder.CreateICmpSLT(X, NullValue)
7322 X, ConstantInt::get(X->getType(), SMin)));
7323 }
7324 default:
7325 llvm_unreachable("Invalid predicate!");
7326 }
7327 }
7328 }
7329
7330 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7331 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7332 return replaceInstUsesWith(CxtI, V);
7333
7334 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7335 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7336 {
7337 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7338 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7340 }
7341
7342 if (!ICmpInst::isUnsigned(Pred) &&
7343 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7344 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7346 }
7347 }
7348
7349 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7350 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7351 {
7352 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7353 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7355 }
7356
7357 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7358 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7359 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7361 }
7362 }
7363
7364 return nullptr;
7365}
7366
7368 bool Changed = false;
7370 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7371 unsigned Op0Cplxity = getComplexity(Op0);
7372 unsigned Op1Cplxity = getComplexity(Op1);
7373
7374 /// Orders the operands of the compare so that they are listed from most
7375 /// complex to least complex. This puts constants before unary operators,
7376 /// before binary operators.
7377 if (Op0Cplxity < Op1Cplxity) {
7378 I.swapOperands();
7379 std::swap(Op0, Op1);
7380 Changed = true;
7381 }
7382
7383 if (Value *V = simplifyICmpInst(I.getPredicate(), Op0, Op1, Q))
7384 return replaceInstUsesWith(I, V);
7385
7386 // Comparing -val or val with non-zero is the same as just comparing val
7387 // ie, abs(val) != 0 -> val != 0
7388 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7389 Value *Cond, *SelectTrue, *SelectFalse;
7390 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7391 m_Value(SelectFalse)))) {
7392 if (Value *V = dyn_castNegVal(SelectTrue)) {
7393 if (V == SelectFalse)
7394 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7395 }
7396 else if (Value *V = dyn_castNegVal(SelectFalse)) {
7397 if (V == SelectTrue)
7398 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7399 }
7400 }
7401 }
7402
7403 if (Op0->getType()->isIntOrIntVectorTy(1))
7405 return Res;
7406
7408 return Res;
7409
7411 return Res;
7412
7414 return Res;
7415
7417 return Res;
7418
7420 return Res;
7421
7423 return Res;
7424
7426 return Res;
7427
7428 // Test if the ICmpInst instruction is used exclusively by a select as
7429 // part of a minimum or maximum operation. If so, refrain from doing
7430 // any other folding. This helps out other analyses which understand
7431 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7432 // and CodeGen. And in this case, at least one of the comparison
7433 // operands has at least one user besides the compare (the select),
7434 // which would often largely negate the benefit of folding anyway.
7435 //
7436 // Do the same for the other patterns recognized by matchSelectPattern.
7437 if (I.hasOneUse())
7438 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7439 Value *A, *B;
7441 if (SPR.Flavor != SPF_UNKNOWN)
7442 return nullptr;
7443 }
7444
7445 // Do this after checking for min/max to prevent infinite looping.
7446 if (Instruction *Res = foldICmpWithZero(I))
7447 return Res;
7448
7449 // FIXME: We only do this after checking for min/max to prevent infinite
7450 // looping caused by a reverse canonicalization of these patterns for min/max.
7451 // FIXME: The organization of folds is a mess. These would naturally go into
7452 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7453 // down here after the min/max restriction.
7454 ICmpInst::Predicate Pred = I.getPredicate();
7455 const APInt *C;
7456 if (match(Op1, m_APInt(C))) {
7457 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7458 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7459 Constant *Zero = Constant::getNullValue(Op0->getType());
7460 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7461 }
7462
7463 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7464 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7466 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7467 }
7468 }
7469
7470 // The folds in here may rely on wrapping flags and special constants, so
7471 // they can break up min/max idioms in some cases but not seemingly similar
7472 // patterns.
7473 // FIXME: It may be possible to enhance select folding to make this
7474 // unnecessary. It may also be moot if we canonicalize to min/max
7475 // intrinsics.
7476 if (Instruction *Res = foldICmpBinOp(I, Q))
7477 return Res;
7478
7480 return Res;
7481
7482 // Try to match comparison as a sign bit test. Intentionally do this after
7483 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7484 if (Instruction *New = foldSignBitTest(I))
7485 return New;
7486
7488 return Res;
7489
7490 if (Instruction *Res = foldICmpCommutative(I.getPredicate(), Op0, Op1, I))
7491 return Res;
7492 if (Instruction *Res =
7493 foldICmpCommutative(I.getSwappedPredicate(), Op1, Op0, I))
7494 return Res;
7495
7496 if (I.isCommutative()) {
7497 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7498 replaceOperand(I, 0, Pair->first);
7499 replaceOperand(I, 1, Pair->second);
7500 return &I;
7501 }
7502 }
7503
7504 // In case of a comparison with two select instructions having the same
7505 // condition, check whether one of the resulting branches can be simplified.
7506 // If so, just compare the other branch and select the appropriate result.
7507 // For example:
7508 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7509 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7510 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7511 // The icmp will result false for the false value of selects and the result
7512 // will depend upon the comparison of true values of selects if %cmp is
7513 // true. Thus, transform this into:
7514 // %cmp = icmp slt i32 %y, %z
7515 // %sel = select i1 %cond, i1 %cmp, i1 false
7516 // This handles similar cases to transform.
7517 {
7518 Value *Cond, *A, *B, *C, *D;
7519 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7521 (Op0->hasOneUse() || Op1->hasOneUse())) {
7522 // Check whether comparison of TrueValues can be simplified
7523 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7524 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7525 return SelectInst::Create(Cond, Res, NewICMP);
7526 }
7527 // Check whether comparison of FalseValues can be simplified
7528 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7529 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7530 return SelectInst::Create(Cond, NewICMP, Res);
7531 }
7532 }
7533 }
7534
7535 // Try to optimize equality comparisons against alloca-based pointers.
7536 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7537 assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?");
7538 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7539 if (foldAllocaCmp(Alloca))
7540 return nullptr;
7541 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7542 if (foldAllocaCmp(Alloca))
7543 return nullptr;
7544 }
7545
7546 if (Instruction *Res = foldICmpBitCast(I))
7547 return Res;
7548
7549 // TODO: Hoist this above the min/max bailout.
7551 return R;
7552
7553 {
7554 Value *X, *Y;
7555 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7556 // and (X & ~Y) != 0 --> (X & Y) == 0
7557 // if A is a power of 2.
7558 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7559 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, 0, &I) &&
7560 I.isEquality())
7561 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7562 Op1);
7563
7564 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7565 if (Op0->getType()->isIntOrIntVectorTy()) {
7566 bool ConsumesOp0, ConsumesOp1;
7567 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7568 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7569 (ConsumesOp0 || ConsumesOp1)) {
7570 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7571 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7572 assert(InvOp0 && InvOp1 &&
7573 "Mismatch between isFreeToInvert and getFreelyInverted");
7574 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7575 }
7576 }
7577
7578 Instruction *AddI = nullptr;
7580 m_Instruction(AddI))) &&
7581 isa<IntegerType>(X->getType())) {
7582 Value *Result;
7583 Constant *Overflow;
7584 // m_UAddWithOverflow can match patterns that do not include an explicit
7585 // "add" instruction, so check the opcode of the matched op.
7586 if (AddI->getOpcode() == Instruction::Add &&
7587 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7588 Result, Overflow)) {
7589 replaceInstUsesWith(*AddI, Result);
7590 eraseInstFromFunction(*AddI);
7591 return replaceInstUsesWith(I, Overflow);
7592 }
7593 }
7594
7595 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7596 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7597 match(Op1, m_APInt(C))) {
7598 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7599 return R;
7600 }
7601
7602 // Signbit test folds
7603 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7604 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7605 Instruction *ExtI;
7606 if ((I.isUnsigned() || I.isEquality()) &&
7607 match(Op1,
7609 Y->getType()->getScalarSizeInBits() == 1 &&
7610 (Op0->hasOneUse() || Op1->hasOneUse())) {
7611 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7612 Instruction *ShiftI;
7613 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7615 OpWidth - 1))))) {
7616 unsigned ExtOpc = ExtI->getOpcode();
7617 unsigned ShiftOpc = ShiftI->getOpcode();
7618 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7619 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7620 Value *SLTZero =
7622 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7623 return replaceInstUsesWith(I, Cmp);
7624 }
7625 }
7626 }
7627 }
7628
7629 if (Instruction *Res = foldICmpEquality(I))
7630 return Res;
7631
7633 return Res;
7634
7635 if (Instruction *Res = foldICmpOfUAddOv(I))
7636 return Res;
7637
7638 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7639 // an i1 which indicates whether or not we successfully did the swap.
7640 //
7641 // Replace comparisons between the old value and the expected value with the
7642 // indicator that 'cmpxchg' returns.
7643 //
7644 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7645 // spuriously fail. In those cases, the old value may equal the expected
7646 // value but it is possible for the swap to not occur.
7647 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7648 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7649 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7650 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7651 !ACXI->isWeak())
7652 return ExtractValueInst::Create(ACXI, 1);
7653
7655 return Res;
7656
7657 if (I.getType()->isVectorTy())
7658 if (Instruction *Res = foldVectorCmp(I, Builder))
7659 return Res;
7660
7662 return Res;
7663
7665 return Res;
7666
7667 return Changed ? &I : nullptr;
7668}
7669
7670/// Fold fcmp ([us]itofp x, cst) if possible.
7672 Instruction *LHSI,
7673 Constant *RHSC) {
7674 const APFloat *RHS;
7675 if (!match(RHSC, m_APFloat(RHS)))
7676 return nullptr;
7677
7678 // Get the width of the mantissa. We don't want to hack on conversions that
7679 // might lose information from the integer, e.g. "i64 -> float"
7680 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7681 if (MantissaWidth == -1) return nullptr; // Unknown.
7682
7683 Type *IntTy = LHSI->getOperand(0)->getType();
7684 unsigned IntWidth = IntTy->getScalarSizeInBits();
7685 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7686
7687 if (I.isEquality()) {
7688 FCmpInst::Predicate P = I.getPredicate();
7689 bool IsExact = false;
7690 APSInt RHSCvt(IntWidth, LHSUnsigned);
7691 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7692
7693 // If the floating point constant isn't an integer value, we know if we will
7694 // ever compare equal / not equal to it.
7695 if (!IsExact) {
7696 // TODO: Can never be -0.0 and other non-representable values
7697 APFloat RHSRoundInt(*RHS);
7699 if (*RHS != RHSRoundInt) {
7701 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7702
7704 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7705 }
7706 }
7707
7708 // TODO: If the constant is exactly representable, is it always OK to do
7709 // equality compares as integer?
7710 }
7711
7712 // Check to see that the input is converted from an integer type that is small
7713 // enough that preserves all bits. TODO: check here for "known" sign bits.
7714 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
7715
7716 // Following test does NOT adjust IntWidth downwards for signed inputs,
7717 // because the most negative value still requires all the mantissa bits
7718 // to distinguish it from one less than that value.
7719 if ((int)IntWidth > MantissaWidth) {
7720 // Conversion would lose accuracy. Check if loss can impact comparison.
7721 int Exp = ilogb(*RHS);
7722 if (Exp == APFloat::IEK_Inf) {
7723 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
7724 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
7725 // Conversion could create infinity.
7726 return nullptr;
7727 } else {
7728 // Note that if RHS is zero or NaN, then Exp is negative
7729 // and first condition is trivially false.
7730 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
7731 // Conversion could affect comparison.
7732 return nullptr;
7733 }
7734 }
7735
7736 // Otherwise, we can potentially simplify the comparison. We know that it
7737 // will always come through as an integer value and we know the constant is
7738 // not a NAN (it would have been previously simplified).
7739 assert(!RHS->isNaN() && "NaN comparison not already folded!");
7740
7742 switch (I.getPredicate()) {
7743 default: llvm_unreachable("Unexpected predicate!");
7744 case FCmpInst::FCMP_UEQ:
7745 case FCmpInst::FCMP_OEQ:
7746 Pred = ICmpInst::ICMP_EQ;
7747 break;
7748 case FCmpInst::FCMP_UGT:
7749 case FCmpInst::FCMP_OGT:
7750 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
7751 break;
7752 case FCmpInst::FCMP_UGE:
7753 case FCmpInst::FCMP_OGE:
7754 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
7755 break;
7756 case FCmpInst::FCMP_ULT:
7757 case FCmpInst::FCMP_OLT:
7758 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
7759 break;
7760 case FCmpInst::FCMP_ULE:
7761 case FCmpInst::FCMP_OLE:
7762 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
7763 break;
7764 case FCmpInst::FCMP_UNE:
7765 case FCmpInst::FCMP_ONE:
7766 Pred = ICmpInst::ICMP_NE;
7767 break;
7768 case FCmpInst::FCMP_ORD:
7769 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7770 case FCmpInst::FCMP_UNO:
7771 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7772 }
7773
7774 // Now we know that the APFloat is a normal number, zero or inf.
7775
7776 // See if the FP constant is too large for the integer. For example,
7777 // comparing an i8 to 300.0.
7778 if (!LHSUnsigned) {
7779 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
7780 // and large values.
7781 APFloat SMax(RHS->getSemantics());
7782 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
7784 if (SMax < *RHS) { // smax < 13123.0
7785 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
7786 Pred == ICmpInst::ICMP_SLE)
7787 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7788 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7789 }
7790 } else {
7791 // If the RHS value is > UnsignedMax, fold the comparison. This handles
7792 // +INF and large values.
7793 APFloat UMax(RHS->getSemantics());
7794 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
7796 if (UMax < *RHS) { // umax < 13123.0
7797 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
7798 Pred == ICmpInst::ICMP_ULE)
7799 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7800 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7801 }
7802 }
7803
7804 if (!LHSUnsigned) {
7805 // See if the RHS value is < SignedMin.
7806 APFloat SMin(RHS->getSemantics());
7807 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
7809 if (SMin > *RHS) { // smin > 12312.0
7810 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
7811 Pred == ICmpInst::ICMP_SGE)
7812 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7813 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7814 }
7815 } else {
7816 // See if the RHS value is < UnsignedMin.
7817 APFloat UMin(RHS->getSemantics());
7818 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
7820 if (UMin > *RHS) { // umin > 12312.0
7821 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
7822 Pred == ICmpInst::ICMP_UGE)
7823 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7824 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7825 }
7826 }
7827
7828 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
7829 // [0, UMAX], but it may still be fractional. Check whether this is the case
7830 // using the IsExact flag.
7831 // Don't do this for zero, because -0.0 is not fractional.
7832 APSInt RHSInt(IntWidth, LHSUnsigned);
7833 bool IsExact;
7834 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
7835 if (!RHS->isZero()) {
7836 if (!IsExact) {
7837 // If we had a comparison against a fractional value, we have to adjust
7838 // the compare predicate and sometimes the value. RHSC is rounded towards
7839 // zero at this point.
7840 switch (Pred) {
7841 default: llvm_unreachable("Unexpected integer comparison!");
7842 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
7843 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7844 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
7845 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7846 case ICmpInst::ICMP_ULE:
7847 // (float)int <= 4.4 --> int <= 4
7848 // (float)int <= -4.4 --> false
7849 if (RHS->isNegative())
7850 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7851 break;
7852 case ICmpInst::ICMP_SLE:
7853 // (float)int <= 4.4 --> int <= 4
7854 // (float)int <= -4.4 --> int < -4
7855 if (RHS->isNegative())
7856 Pred = ICmpInst::ICMP_SLT;
7857 break;
7858 case ICmpInst::ICMP_ULT:
7859 // (float)int < -4.4 --> false
7860 // (float)int < 4.4 --> int <= 4
7861 if (RHS->isNegative())
7862 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7863 Pred = ICmpInst::ICMP_ULE;
7864 break;
7865 case ICmpInst::ICMP_SLT:
7866 // (float)int < -4.4 --> int < -4
7867 // (float)int < 4.4 --> int <= 4
7868 if (!RHS->isNegative())
7869 Pred = ICmpInst::ICMP_SLE;
7870 break;
7871 case ICmpInst::ICMP_UGT:
7872 // (float)int > 4.4 --> int > 4
7873 // (float)int > -4.4 --> true
7874 if (RHS->isNegative())
7875 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7876 break;
7877 case ICmpInst::ICMP_SGT:
7878 // (float)int > 4.4 --> int > 4
7879 // (float)int > -4.4 --> int >= -4
7880 if (RHS->isNegative())
7881 Pred = ICmpInst::ICMP_SGE;
7882 break;
7883 case ICmpInst::ICMP_UGE:
7884 // (float)int >= -4.4 --> true
7885 // (float)int >= 4.4 --> int > 4
7886 if (RHS->isNegative())
7887 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7888 Pred = ICmpInst::ICMP_UGT;
7889 break;
7890 case ICmpInst::ICMP_SGE:
7891 // (float)int >= -4.4 --> int >= -4
7892 // (float)int >= 4.4 --> int > 4
7893 if (!RHS->isNegative())
7894 Pred = ICmpInst::ICMP_SGT;
7895 break;
7896 }
7897 }
7898 }
7899
7900 // Lower this FP comparison into an appropriate integer version of the
7901 // comparison.
7902 return new ICmpInst(Pred, LHSI->getOperand(0),
7903 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
7904}
7905
7906/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
7908 Constant *RHSC) {
7909 // When C is not 0.0 and infinities are not allowed:
7910 // (C / X) < 0.0 is a sign-bit test of X
7911 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
7912 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
7913 //
7914 // Proof:
7915 // Multiply (C / X) < 0.0 by X * X / C.
7916 // - X is non zero, if it is the flag 'ninf' is violated.
7917 // - C defines the sign of X * X * C. Thus it also defines whether to swap
7918 // the predicate. C is also non zero by definition.
7919 //
7920 // Thus X * X / C is non zero and the transformation is valid. [qed]
7921
7922 FCmpInst::Predicate Pred = I.getPredicate();
7923
7924 // Check that predicates are valid.
7925 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
7926 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
7927 return nullptr;
7928
7929 // Check that RHS operand is zero.
7930 if (!match(RHSC, m_AnyZeroFP()))
7931 return nullptr;
7932
7933 // Check fastmath flags ('ninf').
7934 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
7935 return nullptr;
7936
7937 // Check the properties of the dividend. It must not be zero to avoid a
7938 // division by zero (see Proof).
7939 const APFloat *C;
7940 if (!match(LHSI->getOperand(0), m_APFloat(C)))
7941 return nullptr;
7942
7943 if (C->isZero())
7944 return nullptr;
7945
7946 // Get swapped predicate if necessary.
7947 if (C->isNegative())
7948 Pred = I.getSwappedPredicate();
7949
7950 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
7951}
7952
7953/// Optimize fabs(X) compared with zero.
7955 Value *X;
7956 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
7957 return nullptr;
7958
7959 const APFloat *C;
7960 if (!match(I.getOperand(1), m_APFloat(C)))
7961 return nullptr;
7962
7963 if (!C->isPosZero()) {
7964 if (!C->isSmallestNormalized())
7965 return nullptr;
7966
7967 const Function *F = I.getFunction();
7968 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
7969 if (Mode.Input == DenormalMode::PreserveSign ||
7970 Mode.Input == DenormalMode::PositiveZero) {
7971
7972 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7973 Constant *Zero = ConstantFP::getZero(X->getType());
7974 return new FCmpInst(P, X, Zero, "", I);
7975 };
7976
7977 switch (I.getPredicate()) {
7978 case FCmpInst::FCMP_OLT:
7979 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
7980 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
7981 case FCmpInst::FCMP_UGE:
7982 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
7983 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
7984 case FCmpInst::FCMP_OGE:
7985 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
7986 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
7987 case FCmpInst::FCMP_ULT:
7988 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
7989 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
7990 default:
7991 break;
7992 }
7993 }
7994
7995 return nullptr;
7996 }
7997
7998 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7999 I->setPredicate(P);
8000 return IC.replaceOperand(*I, 0, X);
8001 };
8002
8003 switch (I.getPredicate()) {
8004 case FCmpInst::FCMP_UGE:
8005 case FCmpInst::FCMP_OLT:
8006 // fabs(X) >= 0.0 --> true
8007 // fabs(X) < 0.0 --> false
8008 llvm_unreachable("fcmp should have simplified");
8009
8010 case FCmpInst::FCMP_OGT:
8011 // fabs(X) > 0.0 --> X != 0.0
8012 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8013
8014 case FCmpInst::FCMP_UGT:
8015 // fabs(X) u> 0.0 --> X u!= 0.0
8016 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8017
8018 case FCmpInst::FCMP_OLE:
8019 // fabs(X) <= 0.0 --> X == 0.0
8020 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8021
8022 case FCmpInst::FCMP_ULE:
8023 // fabs(X) u<= 0.0 --> X u== 0.0
8024 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8025
8026 case FCmpInst::FCMP_OGE:
8027 // fabs(X) >= 0.0 --> !isnan(X)
8028 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8029 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8030
8031 case FCmpInst::FCMP_ULT:
8032 // fabs(X) u< 0.0 --> isnan(X)
8033 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8034 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8035
8036 case FCmpInst::FCMP_OEQ:
8037 case FCmpInst::FCMP_UEQ:
8038 case FCmpInst::FCMP_ONE:
8039 case FCmpInst::FCMP_UNE:
8040 case FCmpInst::FCMP_ORD:
8041 case FCmpInst::FCMP_UNO:
8042 // Look through the fabs() because it doesn't change anything but the sign.
8043 // fabs(X) == 0.0 --> X == 0.0,
8044 // fabs(X) != 0.0 --> X != 0.0
8045 // isnan(fabs(X)) --> isnan(X)
8046 // !isnan(fabs(X) --> !isnan(X)
8047 return replacePredAndOp0(&I, I.getPredicate(), X);
8048
8049 default:
8050 return nullptr;
8051 }
8052}
8053
8054/// Optimize sqrt(X) compared with zero.
8056 Value *X;
8057 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))
8058 return nullptr;
8059
8060 if (!match(I.getOperand(1), m_PosZeroFP()))
8061 return nullptr;
8062
8063 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8064 I.setPredicate(P);
8065 return IC.replaceOperand(I, 0, X);
8066 };
8067
8068 // Clear ninf flag if sqrt doesn't have it.
8069 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())
8070 I.setHasNoInfs(false);
8071
8072 switch (I.getPredicate()) {
8073 case FCmpInst::FCMP_OLT:
8074 case FCmpInst::FCMP_UGE:
8075 // sqrt(X) < 0.0 --> false
8076 // sqrt(X) u>= 0.0 --> true
8077 llvm_unreachable("fcmp should have simplified");
8078 case FCmpInst::FCMP_ULT:
8079 case FCmpInst::FCMP_ULE:
8080 case FCmpInst::FCMP_OGT:
8081 case FCmpInst::FCMP_OGE:
8082 case FCmpInst::FCMP_OEQ:
8083 case FCmpInst::FCMP_UNE:
8084 // sqrt(X) u< 0.0 --> X u< 0.0
8085 // sqrt(X) u<= 0.0 --> X u<= 0.0
8086 // sqrt(X) > 0.0 --> X > 0.0
8087 // sqrt(X) >= 0.0 --> X >= 0.0
8088 // sqrt(X) == 0.0 --> X == 0.0
8089 // sqrt(X) u!= 0.0 --> X u!= 0.0
8090 return IC.replaceOperand(I, 0, X);
8091
8092 case FCmpInst::FCMP_OLE:
8093 // sqrt(X) <= 0.0 --> X == 0.0
8094 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8095 case FCmpInst::FCMP_UGT:
8096 // sqrt(X) u> 0.0 --> X u!= 0.0
8097 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8098 case FCmpInst::FCMP_UEQ:
8099 // sqrt(X) u== 0.0 --> X u<= 0.0
8100 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8101 case FCmpInst::FCMP_ONE:
8102 // sqrt(X) != 0.0 --> X > 0.0
8103 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8104 case FCmpInst::FCMP_ORD:
8105 // !isnan(sqrt(X)) --> X >= 0.0
8106 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8107 case FCmpInst::FCMP_UNO:
8108 // isnan(sqrt(X)) --> X u< 0.0
8109 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8110 default:
8111 llvm_unreachable("Unexpected predicate!");
8112 }
8113}
8114
8116 CmpInst::Predicate Pred = I.getPredicate();
8117 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8118
8119 // Canonicalize fneg as Op1.
8120 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
8121 std::swap(Op0, Op1);
8122 Pred = I.getSwappedPredicate();
8123 }
8124
8125 if (!match(Op1, m_FNeg(m_Specific(Op0))))
8126 return nullptr;
8127
8128 // Replace the negated operand with 0.0:
8129 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8130 Constant *Zero = ConstantFP::getZero(Op0->getType());
8131 return new FCmpInst(Pred, Op0, Zero, "", &I);
8132}
8133
8135 Constant *RHSC, InstCombinerImpl &CI) {
8136 const CmpInst::Predicate Pred = I.getPredicate();
8137 Value *X = LHSI->getOperand(0);
8138 Value *Y = LHSI->getOperand(1);
8139 switch (Pred) {
8140 default:
8141 break;
8142 case FCmpInst::FCMP_UGT:
8143 case FCmpInst::FCMP_ULT:
8144 case FCmpInst::FCMP_UNE:
8145 case FCmpInst::FCMP_OEQ:
8146 case FCmpInst::FCMP_OGE:
8147 case FCmpInst::FCMP_OLE:
8148 // The optimization is not valid if X and Y are infinities of the same
8149 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8150 // flag then we can assume we do not have that case. Otherwise we might be
8151 // able to prove that either X or Y is not infinity.
8152 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8153 !isKnownNeverInfinity(Y, /*Depth=*/0,
8155 !isKnownNeverInfinity(X, /*Depth=*/0,
8157 break;
8158
8159 [[fallthrough]];
8160 case FCmpInst::FCMP_OGT:
8161 case FCmpInst::FCMP_OLT:
8162 case FCmpInst::FCMP_ONE:
8163 case FCmpInst::FCMP_UEQ:
8164 case FCmpInst::FCMP_UGE:
8165 case FCmpInst::FCMP_ULE:
8166 // fcmp pred (x - y), 0 --> fcmp pred x, y
8167 if (match(RHSC, m_AnyZeroFP()) &&
8168 I.getFunction()->getDenormalMode(
8169 LHSI->getType()->getScalarType()->getFltSemantics()) ==
8171 CI.replaceOperand(I, 0, X);
8172 CI.replaceOperand(I, 1, Y);
8173 return &I;
8174 }
8175 break;
8176 }
8177
8178 return nullptr;
8179}
8180
8182 bool Changed = false;
8183
8184 /// Orders the operands of the compare so that they are listed from most
8185 /// complex to least complex. This puts constants before unary operators,
8186 /// before binary operators.
8187 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
8188 I.swapOperands();
8189 Changed = true;
8190 }
8191
8192 const CmpInst::Predicate Pred = I.getPredicate();
8193 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8194 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
8196 return replaceInstUsesWith(I, V);
8197
8198 // Simplify 'fcmp pred X, X'
8199 Type *OpType = Op0->getType();
8200 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
8201 if (Op0 == Op1) {
8202 switch (Pred) {
8203 default: break;
8204 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
8205 case FCmpInst::FCMP_ULT: // True if unordered or less than
8206 case FCmpInst::FCMP_UGT: // True if unordered or greater than
8207 case FCmpInst::FCMP_UNE: // True if unordered or not equal
8208 // Canonicalize these to be 'fcmp uno %X, 0.0'.
8209 I.setPredicate(FCmpInst::FCMP_UNO);
8210 I.setOperand(1, Constant::getNullValue(OpType));
8211 return &I;
8212
8213 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
8214 case FCmpInst::FCMP_OEQ: // True if ordered and equal
8215 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
8216 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
8217 // Canonicalize these to be 'fcmp ord %X, 0.0'.
8218 I.setPredicate(FCmpInst::FCMP_ORD);
8219 I.setOperand(1, Constant::getNullValue(OpType));
8220 return &I;
8221 }
8222 }
8223
8224 if (I.isCommutative()) {
8225 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
8226 replaceOperand(I, 0, Pair->first);
8227 replaceOperand(I, 1, Pair->second);
8228 return &I;
8229 }
8230 }
8231
8232 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
8233 // then canonicalize the operand to 0.0.
8234 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
8235 if (!match(Op0, m_PosZeroFP()) &&
8236 isKnownNeverNaN(Op0, 0, getSimplifyQuery().getWithInstruction(&I)))
8237 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
8238
8239 if (!match(Op1, m_PosZeroFP()) &&
8240 isKnownNeverNaN(Op1, 0, getSimplifyQuery().getWithInstruction(&I)))
8241 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8242 }
8243
8244 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
8245 Value *X, *Y;
8246 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
8247 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
8248
8250 return R;
8251
8252 // Test if the FCmpInst instruction is used exclusively by a select as
8253 // part of a minimum or maximum operation. If so, refrain from doing
8254 // any other folding. This helps out other analyses which understand
8255 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
8256 // and CodeGen. And in this case, at least one of the comparison
8257 // operands has at least one user besides the compare (the select),
8258 // which would often largely negate the benefit of folding anyway.
8259 if (I.hasOneUse())
8260 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
8261 Value *A, *B;
8263 if (SPR.Flavor != SPF_UNKNOWN)
8264 return nullptr;
8265 }
8266
8267 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
8268 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
8269 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
8270 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8271
8272 // Canonicalize:
8273 // fcmp olt X, +inf -> fcmp one X, +inf
8274 // fcmp ole X, +inf -> fcmp ord X, 0
8275 // fcmp ogt X, +inf -> false
8276 // fcmp oge X, +inf -> fcmp oeq X, +inf
8277 // fcmp ult X, +inf -> fcmp une X, +inf
8278 // fcmp ule X, +inf -> true
8279 // fcmp ugt X, +inf -> fcmp uno X, 0
8280 // fcmp uge X, +inf -> fcmp ueq X, +inf
8281 // fcmp olt X, -inf -> false
8282 // fcmp ole X, -inf -> fcmp oeq X, -inf
8283 // fcmp ogt X, -inf -> fcmp one X, -inf
8284 // fcmp oge X, -inf -> fcmp ord X, 0
8285 // fcmp ult X, -inf -> fcmp uno X, 0
8286 // fcmp ule X, -inf -> fcmp ueq X, -inf
8287 // fcmp ugt X, -inf -> fcmp une X, -inf
8288 // fcmp uge X, -inf -> true
8289 const APFloat *C;
8290 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
8291 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
8292 default:
8293 break;
8294 case FCmpInst::FCMP_ORD:
8295 case FCmpInst::FCMP_UNO:
8298 case FCmpInst::FCMP_OGT:
8299 case FCmpInst::FCMP_ULE:
8300 llvm_unreachable("Should be simplified by InstSimplify");
8301 case FCmpInst::FCMP_OLT:
8302 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
8303 case FCmpInst::FCMP_OLE:
8304 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
8305 "", &I);
8306 case FCmpInst::FCMP_OGE:
8307 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
8308 case FCmpInst::FCMP_ULT:
8309 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
8310 case FCmpInst::FCMP_UGT:
8311 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8312 "", &I);
8313 case FCmpInst::FCMP_UGE:
8314 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8315 }
8316 }
8317
8318 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8319 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8320 if (match(Op1, m_PosZeroFP()) &&
8323 if (Pred == FCmpInst::FCMP_OEQ)
8324 IntPred = ICmpInst::ICMP_EQ;
8325 else if (Pred == FCmpInst::FCMP_UNE)
8326 IntPred = ICmpInst::ICMP_NE;
8327
8328 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8329 Type *IntTy = X->getType();
8330 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8331 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8332 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8333 }
8334 }
8335
8336 // Handle fcmp with instruction LHS and constant RHS.
8337 Instruction *LHSI;
8338 Constant *RHSC;
8339 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8340 switch (LHSI->getOpcode()) {
8341 case Instruction::Select:
8342 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8343 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8344 (match(LHSI,
8346 match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
8347 return replaceOperand(I, 0, X);
8348 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
8349 return NV;
8350 break;
8351 case Instruction::FSub:
8352 if (LHSI->hasOneUse())
8353 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
8354 return NV;
8355 break;
8356 case Instruction::PHI:
8357 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8358 return NV;
8359 break;
8360 case Instruction::SIToFP:
8361 case Instruction::UIToFP:
8362 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8363 return NV;
8364 break;
8365 case Instruction::FDiv:
8366 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8367 return NV;
8368 break;
8369 case Instruction::Load:
8370 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8371 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
8373 cast<LoadInst>(LHSI), GEP, GV, I))
8374 return Res;
8375 break;
8376 }
8377 }
8378
8379 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8380 return R;
8381
8382 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))
8383 return R;
8384
8385 if (match(Op0, m_FNeg(m_Value(X)))) {
8386 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8387 Constant *C;
8388 if (match(Op1, m_Constant(C)))
8389 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8390 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8391 }
8392
8393 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8394 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8395 return new FCmpInst(Pred, X, Op1, "", &I);
8396
8397 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8398 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8399 return new FCmpInst(Pred, Op0, Y, "", &I);
8400
8401 if (match(Op0, m_FPExt(m_Value(X)))) {
8402 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8403 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8404 return new FCmpInst(Pred, X, Y, "", &I);
8405
8406 const APFloat *C;
8407 if (match(Op1, m_APFloat(C))) {
8408 const fltSemantics &FPSem =
8409 X->getType()->getScalarType()->getFltSemantics();
8410 bool Lossy;
8411 APFloat TruncC = *C;
8412 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8413
8414 if (Lossy) {
8415 // X can't possibly equal the higher-precision constant, so reduce any
8416 // equality comparison.
8417 // TODO: Other predicates can be handled via getFCmpCode().
8418 switch (Pred) {
8419 case FCmpInst::FCMP_OEQ:
8420 // X is ordered and equal to an impossible constant --> false
8421 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8422 case FCmpInst::FCMP_ONE:
8423 // X is ordered and not equal to an impossible constant --> ordered
8424 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8425 ConstantFP::getZero(X->getType()));
8426 case FCmpInst::FCMP_UEQ:
8427 // X is unordered or equal to an impossible constant --> unordered
8428 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8429 ConstantFP::getZero(X->getType()));
8430 case FCmpInst::FCMP_UNE:
8431 // X is unordered or not equal to an impossible constant --> true
8432 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8433 default:
8434 break;
8435 }
8436 }
8437
8438 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8439 // Avoid lossy conversions and denormals.
8440 // Zero is a special case that's OK to convert.
8441 APFloat Fabs = TruncC;
8442 Fabs.clearSign();
8443 if (!Lossy &&
8444 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8445 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8446 return new FCmpInst(Pred, X, NewC, "", &I);
8447 }
8448 }
8449 }
8450
8451 // Convert a sign-bit test of an FP value into a cast and integer compare.
8452 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8453 // TODO: Handle non-zero compare constants.
8454 // TODO: Handle other predicates.
8455 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),
8456 m_Value(X)))) &&
8457 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8458 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8459 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8460 IntType = VectorType::get(IntType, VecTy->getElementCount());
8461
8462 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8463 if (Pred == FCmpInst::FCMP_OLT) {
8464 Value *IntX = Builder.CreateBitCast(X, IntType);
8465 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8466 ConstantInt::getNullValue(IntType));
8467 }
8468 }
8469
8470 {
8471 Value *CanonLHS = nullptr, *CanonRHS = nullptr;
8472 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));
8473 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));
8474
8475 // (canonicalize(x) == x) => (x == x)
8476 if (CanonLHS == Op1)
8477 return new FCmpInst(Pred, Op1, Op1, "", &I);
8478
8479 // (x == canonicalize(x)) => (x == x)
8480 if (CanonRHS == Op0)
8481 return new FCmpInst(Pred, Op0, Op0, "", &I);
8482
8483 // (canonicalize(x) == canonicalize(y)) => (x == y)
8484 if (CanonLHS && CanonRHS)
8485 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8486 }
8487
8488 if (I.getType()->isVectorTy())
8489 if (Instruction *Res = foldVectorCmp(I, Builder))
8490 return Res;
8491
8492 return Changed ? &I : nullptr;
8493}
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
const MachineOperand & RHS
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:166
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
Value * LHS
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5328
void clearSign()
Definition: APFloat.h:1211
bool isZero() const
Definition: APFloat.h:1347
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1066
APInt bitcastToAPInt() const
Definition: APFloat.h:1262
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1046
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1006
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5315
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1161
Class for arbitrary precision integers.
Definition: APInt.h:77
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:211
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition: APInt.h:426
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:206
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:400
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1497
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1469
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:183
APInt abs() const
Get the absolute value.
Definition: APInt.h:1743
unsigned ceilLogBase2() const
Definition: APInt.h:1712
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1178
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:348
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:1159
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:357
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition: APInt.h:443
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1445
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1088
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:186
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:193
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:306
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:1226
bool eq(const APInt &RHS) const
Equality comparison.
Definition: APInt.h:1056
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:1143
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1905
void negate()
Negate this APInt in place.
Definition: APInt.h:1427
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1595
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1554
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:196
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:333
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1571
unsigned logBase2() const
Definition: APInt.h:1709
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:452
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:804
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:382
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1127
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:850
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:417
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:283
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1107
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:273
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:177
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1214
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:366
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:263
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:216
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:828
unsigned countr_one() const
Count the number of trailing one bits.
Definition: APInt.h:1612
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1198
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:2280
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2241
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2617
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2604
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2631
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2610
static Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2598
static Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1037
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:849
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:856
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:863
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:1449
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:767
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:1744
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:846
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:738
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
Definition: DataLayout.cpp:873
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:461
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:861
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:496
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:154
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:229
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI, bool IsNSW=false) const
Definition: InstCombiner.h:459
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
Definition: InstCombiner.h:137
TargetLibraryInfo & TLI
Definition: InstCombiner.h:73
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
Definition: InstCombiner.h:438
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:383
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:482
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
Definition: InstCombiner.h:177
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:489
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:245
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:333
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:407
DominatorTree & DT
Definition: InstCombiner.h:74
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:467
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:428
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:474
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:210
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:339
unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:454
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
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
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:94
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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)
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:44
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:1928
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:2070
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:253
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:257
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