LLVM 22.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/APFloat.h"
15#include "llvm/ADT/APSInt.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/Loads.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/InstrTypes.h"
34#include <bitset>
35
36using namespace llvm;
37using namespace PatternMatch;
38
39#define DEBUG_TYPE "instcombine"
40
41// How many times is a select replaced by one of its operands?
42STATISTIC(NumSel, "Number of select opts");
43
44/// Compute Result = In1+In2, returning true if the result overflowed for this
45/// type.
46static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
47 bool IsSigned = false) {
48 bool Overflow;
49 if (IsSigned)
50 Result = In1.sadd_ov(In2, Overflow);
51 else
52 Result = In1.uadd_ov(In2, Overflow);
53
54 return Overflow;
55}
56
57/// Compute Result = In1-In2, returning true if the result overflowed for this
58/// type.
59static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
60 bool IsSigned = false) {
61 bool Overflow;
62 if (IsSigned)
63 Result = In1.ssub_ov(In2, Overflow);
64 else
65 Result = In1.usub_ov(In2, Overflow);
66
67 return Overflow;
68}
69
70/// Given an icmp instruction, return true if any use of this comparison is a
71/// branch on sign bit comparison.
72static bool hasBranchUse(ICmpInst &I) {
73 for (auto *U : I.users())
74 if (isa<BranchInst>(U))
75 return true;
76 return false;
77}
78
79/// Returns true if the exploded icmp can be expressed as a signed comparison
80/// to zero and updates the predicate accordingly.
81/// The signedness of the comparison is preserved.
82/// TODO: Refactor with decomposeBitTestICmp()?
83static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
84 if (!ICmpInst::isSigned(Pred))
85 return false;
86
87 if (C.isZero())
88 return ICmpInst::isRelational(Pred);
89
90 if (C.isOne()) {
91 if (Pred == ICmpInst::ICMP_SLT) {
92 Pred = ICmpInst::ICMP_SLE;
93 return true;
94 }
95 } else if (C.isAllOnes()) {
96 if (Pred == ICmpInst::ICMP_SGT) {
97 Pred = ICmpInst::ICMP_SGE;
98 return true;
99 }
100 }
101
102 return false;
103}
104
105/// This is called when we see this pattern:
106/// cmp pred (load (gep GV, ...)), cmpcst
107/// where GV is a global variable with a constant initializer. Try to simplify
108/// this into some simple computation that does not need the load. For example
109/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
110///
111/// If AndCst is non-null, then the loaded value is masked with that constant
112/// before doing the comparison. This handles cases like "A[i]&4 == 0".
114 LoadInst *LI, GetElementPtrInst *GEP, CmpInst &ICI, ConstantInt *AndCst) {
116 if (LI->isVolatile() || !GV || !GV->isConstant() ||
117 !GV->hasDefinitiveInitializer())
118 return nullptr;
119
120 Type *EltTy = LI->getType();
121 TypeSize EltSize = DL.getTypeStoreSize(EltTy);
122 if (EltSize.isScalable())
123 return nullptr;
124
126 if (!Expr.Index || Expr.BasePtr != GV || Expr.Offset.getBitWidth() > 64)
127 return nullptr;
128
129 Constant *Init = GV->getInitializer();
130 TypeSize GlobalSize = DL.getTypeAllocSize(Init->getType());
131
132 Value *Idx = Expr.Index;
133 const APInt &Stride = Expr.Scale;
134 const APInt &ConstOffset = Expr.Offset;
135
136 // Allow an additional context offset, but only within the stride.
137 if (!ConstOffset.ult(Stride))
138 return nullptr;
139
140 // Don't handle overlapping loads for now.
141 if (!Stride.uge(EltSize.getFixedValue()))
142 return nullptr;
143
144 // Don't blow up on huge arrays.
145 uint64_t ArrayElementCount =
146 divideCeil((GlobalSize.getFixedValue() - ConstOffset.getZExtValue()),
147 Stride.getZExtValue());
148 if (ArrayElementCount > MaxArraySizeForCombine)
149 return nullptr;
150
151 enum { Overdefined = -3, Undefined = -2 };
152
153 // Variables for our state machines.
154
155 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
156 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
157 // and 87 is the second (and last) index. FirstTrueElement is -2 when
158 // undefined, otherwise set to the first true element. SecondTrueElement is
159 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
160 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
161
162 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
163 // form "i != 47 & i != 87". Same state transitions as for true elements.
164 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
165
166 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
167 /// define a state machine that triggers for ranges of values that the index
168 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
169 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
170 /// index in the range (inclusive). We use -2 for undefined here because we
171 /// use relative comparisons and don't want 0-1 to match -1.
172 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
173
174 // MagicBitvector - This is a magic bitvector where we set a bit if the
175 // comparison is true for element 'i'. If there are 64 elements or less in
176 // the array, this will fully represent all the comparison results.
177 uint64_t MagicBitvector = 0;
178
179 // Scan the array and see if one of our patterns matches.
180 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
181 APInt Offset = ConstOffset;
182 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i, Offset += Stride) {
184 if (!Elt)
185 return nullptr;
186
187 // If the element is masked, handle it.
188 if (AndCst) {
189 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);
190 if (!Elt)
191 return nullptr;
192 }
193
194 // Find out if the comparison would be true or false for the i'th element.
196 CompareRHS, DL, &TLI);
197 if (!C)
198 return nullptr;
199
200 // If the result is undef for this element, ignore it.
201 if (isa<UndefValue>(C)) {
202 // Extend range state machines to cover this element in case there is an
203 // undef in the middle of the range.
204 if (TrueRangeEnd == (int)i - 1)
205 TrueRangeEnd = i;
206 if (FalseRangeEnd == (int)i - 1)
207 FalseRangeEnd = i;
208 continue;
209 }
210
211 // If we can't compute the result for any of the elements, we have to give
212 // up evaluating the entire conditional.
213 if (!isa<ConstantInt>(C))
214 return nullptr;
215
216 // Otherwise, we know if the comparison is true or false for this element,
217 // update our state machines.
218 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
219
220 // State machine for single/double/range index comparison.
221 if (IsTrueForElt) {
222 // Update the TrueElement state machine.
223 if (FirstTrueElement == Undefined)
224 FirstTrueElement = TrueRangeEnd = i; // First true element.
225 else {
226 // Update double-compare state machine.
227 if (SecondTrueElement == Undefined)
228 SecondTrueElement = i;
229 else
230 SecondTrueElement = Overdefined;
231
232 // Update range state machine.
233 if (TrueRangeEnd == (int)i - 1)
234 TrueRangeEnd = i;
235 else
236 TrueRangeEnd = Overdefined;
237 }
238 } else {
239 // Update the FalseElement state machine.
240 if (FirstFalseElement == Undefined)
241 FirstFalseElement = FalseRangeEnd = i; // First false element.
242 else {
243 // Update double-compare state machine.
244 if (SecondFalseElement == Undefined)
245 SecondFalseElement = i;
246 else
247 SecondFalseElement = Overdefined;
248
249 // Update range state machine.
250 if (FalseRangeEnd == (int)i - 1)
251 FalseRangeEnd = i;
252 else
253 FalseRangeEnd = Overdefined;
254 }
255 }
256
257 // If this element is in range, update our magic bitvector.
258 if (i < 64 && IsTrueForElt)
259 MagicBitvector |= 1ULL << i;
260
261 // If all of our states become overdefined, bail out early. Since the
262 // predicate is expensive, only check it every 8 elements. This is only
263 // really useful for really huge arrays.
264 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
265 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
266 FalseRangeEnd == Overdefined)
267 return nullptr;
268 }
269
270 // Now that we've scanned the entire array, emit our new comparison(s). We
271 // order the state machines in complexity of the generated code.
272
273 // If inbounds keyword is not present, Idx * Stride can overflow.
274 // Let's assume that Stride is 2 and the wanted value is at offset 0.
275 // Then, there are two possible values for Idx to match offset 0:
276 // 0x00..00, 0x80..00.
277 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
278 // comparison is false if Idx was 0x80..00.
279 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.
280 auto MaskIdx = [&](Value *Idx) {
281 if (!Expr.Flags.isInBounds() && Stride.countr_zero() != 0) {
283 Mask = Builder.CreateLShr(Mask, Stride.countr_zero());
284 Idx = Builder.CreateAnd(Idx, Mask);
285 }
286 return Idx;
287 };
288
289 // If the comparison is only true for one or two elements, emit direct
290 // comparisons.
291 if (SecondTrueElement != Overdefined) {
292 Idx = MaskIdx(Idx);
293 // None true -> false.
294 if (FirstTrueElement == Undefined)
295 return replaceInstUsesWith(ICI, Builder.getFalse());
296
297 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
298
299 // True for one element -> 'i == 47'.
300 if (SecondTrueElement == Undefined)
301 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
302
303 // True for two elements -> 'i == 47 | i == 72'.
304 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx);
305 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
306 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx);
307 return BinaryOperator::CreateOr(C1, C2);
308 }
309
310 // If the comparison is only false for one or two elements, emit direct
311 // comparisons.
312 if (SecondFalseElement != Overdefined) {
313 Idx = MaskIdx(Idx);
314 // None false -> true.
315 if (FirstFalseElement == Undefined)
316 return replaceInstUsesWith(ICI, Builder.getTrue());
317
318 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
319
320 // False for one element -> 'i != 47'.
321 if (SecondFalseElement == Undefined)
322 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
323
324 // False for two elements -> 'i != 47 & i != 72'.
325 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx);
326 Value *SecondFalseIdx =
327 ConstantInt::get(Idx->getType(), SecondFalseElement);
328 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx);
329 return BinaryOperator::CreateAnd(C1, C2);
330 }
331
332 // If the comparison can be replaced with a range comparison for the elements
333 // where it is true, emit the range check.
334 if (TrueRangeEnd != Overdefined) {
335 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
336 Idx = MaskIdx(Idx);
337
338 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
339 if (FirstTrueElement) {
340 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
341 Idx = Builder.CreateAdd(Idx, Offs);
342 }
343
344 Value *End =
345 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1);
346 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
347 }
348
349 // False range check.
350 if (FalseRangeEnd != Overdefined) {
351 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
352 Idx = MaskIdx(Idx);
353 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
354 if (FirstFalseElement) {
355 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
356 Idx = Builder.CreateAdd(Idx, Offs);
357 }
358
359 Value *End =
360 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement);
361 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
362 }
363
364 // If a magic bitvector captures the entire comparison state
365 // of this load, replace it with computation that does:
366 // ((magic_cst >> i) & 1) != 0
367 {
368 Type *Ty = nullptr;
369
370 // Look for an appropriate type:
371 // - The type of Idx if the magic fits
372 // - The smallest fitting legal type
373 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
374 Ty = Idx->getType();
375 else
376 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
377
378 if (Ty) {
379 Idx = MaskIdx(Idx);
380 Value *V = Builder.CreateIntCast(Idx, Ty, false);
381 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
382 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V);
383 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
384 }
385 }
386
387 return nullptr;
388}
389
390/// Returns true if we can rewrite Start as a GEP with pointer Base
391/// and some integer offset. The nodes that need to be re-written
392/// for this transformation will be added to Explored.
394 const DataLayout &DL,
395 SetVector<Value *> &Explored) {
396 SmallVector<Value *, 16> WorkList(1, Start);
397 Explored.insert(Base);
398
399 // The following traversal gives us an order which can be used
400 // when doing the final transformation. Since in the final
401 // transformation we create the PHI replacement instructions first,
402 // we don't have to get them in any particular order.
403 //
404 // However, for other instructions we will have to traverse the
405 // operands of an instruction first, which means that we have to
406 // do a post-order traversal.
407 while (!WorkList.empty()) {
409
410 while (!WorkList.empty()) {
411 if (Explored.size() >= 100)
412 return false;
413
414 Value *V = WorkList.back();
415
416 if (Explored.contains(V)) {
417 WorkList.pop_back();
418 continue;
419 }
420
422 // We've found some value that we can't explore which is different from
423 // the base. Therefore we can't do this transformation.
424 return false;
425
426 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
427 // Only allow inbounds GEPs with at most one variable offset.
428 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); };
429 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1)
430 return false;
431
432 NW = NW.intersectForOffsetAdd(GEP->getNoWrapFlags());
433 if (!Explored.contains(GEP->getOperand(0)))
434 WorkList.push_back(GEP->getOperand(0));
435 }
436
437 if (WorkList.back() == V) {
438 WorkList.pop_back();
439 // We've finished visiting this node, mark it as such.
440 Explored.insert(V);
441 }
442
443 if (auto *PN = dyn_cast<PHINode>(V)) {
444 // We cannot transform PHIs on unsplittable basic blocks.
445 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
446 return false;
447 Explored.insert(PN);
448 PHIs.insert(PN);
449 }
450 }
451
452 // Explore the PHI nodes further.
453 for (auto *PN : PHIs)
454 for (Value *Op : PN->incoming_values())
455 if (!Explored.contains(Op))
456 WorkList.push_back(Op);
457 }
458
459 // Make sure that we can do this. Since we can't insert GEPs in a basic
460 // block before a PHI node, we can't easily do this transformation if
461 // we have PHI node users of transformed instructions.
462 for (Value *Val : Explored) {
463 for (Value *Use : Val->uses()) {
464
465 auto *PHI = dyn_cast<PHINode>(Use);
466 auto *Inst = dyn_cast<Instruction>(Val);
467
468 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
469 !Explored.contains(PHI))
470 continue;
471
472 if (PHI->getParent() == Inst->getParent())
473 return false;
474 }
475 }
476 return true;
477}
478
479// Sets the appropriate insert point on Builder where we can add
480// a replacement Instruction for V (if that is possible).
481static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
482 bool Before = true) {
483 if (auto *PHI = dyn_cast<PHINode>(V)) {
484 BasicBlock *Parent = PHI->getParent();
485 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
486 return;
487 }
488 if (auto *I = dyn_cast<Instruction>(V)) {
489 if (!Before)
490 I = &*std::next(I->getIterator());
491 Builder.SetInsertPoint(I);
492 return;
493 }
494 if (auto *A = dyn_cast<Argument>(V)) {
495 // Set the insertion point in the entry block.
496 BasicBlock &Entry = A->getParent()->getEntryBlock();
497 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt());
498 return;
499 }
500 // Otherwise, this is a constant and we don't need to set a new
501 // insertion point.
502 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
503}
504
505/// Returns a re-written value of Start as an indexed GEP using Base as a
506/// pointer.
508 const DataLayout &DL,
509 SetVector<Value *> &Explored,
510 InstCombiner &IC) {
511 // Perform all the substitutions. This is a bit tricky because we can
512 // have cycles in our use-def chains.
513 // 1. Create the PHI nodes without any incoming values.
514 // 2. Create all the other values.
515 // 3. Add the edges for the PHI nodes.
516 // 4. Emit GEPs to get the original pointers.
517 // 5. Remove the original instructions.
518 Type *IndexType = IntegerType::get(
519 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType()));
520
522 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
523
524 // Create the new PHI nodes, without adding any incoming values.
525 for (Value *Val : Explored) {
526 if (Val == Base)
527 continue;
528 // Create empty phi nodes. This avoids cyclic dependencies when creating
529 // the remaining instructions.
530 if (auto *PHI = dyn_cast<PHINode>(Val))
531 NewInsts[PHI] =
532 PHINode::Create(IndexType, PHI->getNumIncomingValues(),
533 PHI->getName() + ".idx", PHI->getIterator());
534 }
535 IRBuilder<> Builder(Base->getContext());
536
537 // Create all the other instructions.
538 for (Value *Val : Explored) {
539 if (NewInsts.contains(Val))
540 continue;
541
542 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
543 setInsertionPoint(Builder, GEP);
544 Value *Op = NewInsts[GEP->getOperand(0)];
545 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);
547 NewInsts[GEP] = OffsetV;
548 else
549 NewInsts[GEP] = Builder.CreateAdd(
550 Op, OffsetV, GEP->getOperand(0)->getName() + ".add",
551 /*NUW=*/NW.hasNoUnsignedWrap(),
552 /*NSW=*/NW.hasNoUnsignedSignedWrap());
553 continue;
554 }
555 if (isa<PHINode>(Val))
556 continue;
557
558 llvm_unreachable("Unexpected instruction type");
559 }
560
561 // Add the incoming values to the PHI nodes.
562 for (Value *Val : Explored) {
563 if (Val == Base)
564 continue;
565 // All the instructions have been created, we can now add edges to the
566 // phi nodes.
567 if (auto *PHI = dyn_cast<PHINode>(Val)) {
568 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
569 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
570 Value *NewIncoming = PHI->getIncomingValue(I);
571
572 auto It = NewInsts.find(NewIncoming);
573 if (It != NewInsts.end())
574 NewIncoming = It->second;
575
576 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
577 }
578 }
579 }
580
581 for (Value *Val : Explored) {
582 if (Val == Base)
583 continue;
584
585 setInsertionPoint(Builder, Val, false);
586 // Create GEP for external users.
587 Value *NewVal = Builder.CreateGEP(Builder.getInt8Ty(), Base, NewInsts[Val],
588 Val->getName() + ".ptr", NW);
589 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal);
590 // Add old instruction to worklist for DCE. We don't directly remove it
591 // here because the original compare is one of the users.
593 }
594
595 return NewInsts[Start];
596}
597
598/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
599/// We can look through PHIs, GEPs and casts in order to determine a common base
600/// between GEPLHS and RHS.
603 const DataLayout &DL,
604 InstCombiner &IC) {
605 // FIXME: Support vector of pointers.
606 if (GEPLHS->getType()->isVectorTy())
607 return nullptr;
608
609 if (!GEPLHS->hasAllConstantIndices())
610 return nullptr;
611
612 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
613 Value *PtrBase =
615 /*AllowNonInbounds*/ false);
616
617 // Bail if we looked through addrspacecast.
618 if (PtrBase->getType() != GEPLHS->getType())
619 return nullptr;
620
621 // The set of nodes that will take part in this transformation.
622 SetVector<Value *> Nodes;
623 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
624 if (!canRewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes))
625 return nullptr;
626
627 // We know we can re-write this as
628 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
629 // Since we've only looked through inbouds GEPs we know that we
630 // can't have overflow on either side. We can therefore re-write
631 // this as:
632 // OFFSET1 cmp OFFSET2
633 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes, IC);
634
635 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
636 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
637 // offset. Since Index is the offset of LHS to the base pointer, we will now
638 // compare the offsets instead of comparing the pointers.
640 IC.Builder.getInt(Offset), NewRHS);
641}
642
643/// Fold comparisons between a GEP instruction and something else. At this point
644/// we know that the GEP is on the LHS of the comparison.
647 // Don't transform signed compares of GEPs into index compares. Even if the
648 // GEP is inbounds, the final add of the base pointer can have signed overflow
649 // and would change the result of the icmp.
650 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
651 // the maximum signed value for the pointer type.
653 return nullptr;
654
655 // Look through bitcasts and addrspacecasts. We do not however want to remove
656 // 0 GEPs.
657 if (!isa<GetElementPtrInst>(RHS))
658 RHS = RHS->stripPointerCasts();
659
660 auto CanFold = [Cond](GEPNoWrapFlags NW) {
662 return true;
663
664 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags.
666 return NW != GEPNoWrapFlags::none();
667 };
668
669 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) {
670 if (!NW.hasNoUnsignedWrap()) {
671 // Convert signed to unsigned comparison.
672 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Op1, Op2);
673 }
674
675 auto *I = new ICmpInst(Cond, Op1, Op2);
676 I->setSameSign(NW.hasNoUnsignedSignedWrap());
677 return I;
678 };
679
681 if (Base.Ptr == RHS && CanFold(Base.LHSNW) && !Base.isExpensive()) {
682 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
683 Type *IdxTy = DL.getIndexType(GEPLHS->getType());
684 Value *Offset =
685 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);
686 return NewICmp(Base.LHSNW, Offset,
687 Constant::getNullValue(Offset->getType()));
688 }
689
690 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
691 isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&
692 !NullPointerIsDefined(I.getFunction(),
693 RHS->getType()->getPointerAddressSpace())) {
694 // For most address spaces, an allocation can't be placed at null, but null
695 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
696 // the only valid inbounds address derived from null, is null itself.
697 // Thus, we have four cases to consider:
698 // 1) Base == nullptr, Offset == 0 -> inbounds, null
699 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
700 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
701 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
702 //
703 // (Note if we're indexing a type of size 0, that simply collapses into one
704 // of the buckets above.)
705 //
706 // In general, we're allowed to make values less poison (i.e. remove
707 // sources of full UB), so in this case, we just select between the two
708 // non-poison cases (1 and 4 above).
709 //
710 // For vectors, we apply the same reasoning on a per-lane basis.
711 auto *Base = GEPLHS->getPointerOperand();
712 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
713 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount();
714 Base = Builder.CreateVectorSplat(EC, Base);
715 }
716 return new ICmpInst(Cond, Base,
718 cast<Constant>(RHS), Base->getType()));
719 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
720 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
721
722 // If the base pointers are different, but the indices are the same, just
723 // compare the base pointer.
724 if (GEPLHS->getOperand(0) != GEPRHS->getOperand(0)) {
725 bool IndicesTheSame =
726 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
727 GEPLHS->getPointerOperand()->getType() ==
728 GEPRHS->getPointerOperand()->getType() &&
729 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
730 if (IndicesTheSame)
731 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
732 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
733 IndicesTheSame = false;
734 break;
735 }
736
737 // If all indices are the same, just compare the base pointers.
738 Type *BaseType = GEPLHS->getOperand(0)->getType();
739 if (IndicesTheSame &&
740 CmpInst::makeCmpResultType(BaseType) == I.getType() && CanFold(NW))
741 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
742
743 // If we're comparing GEPs with two base pointers that only differ in type
744 // and both GEPs have only constant indices or just one use, then fold
745 // the compare with the adjusted indices.
746 // FIXME: Support vector of pointers.
747 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
748 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
749 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
750 GEPLHS->getOperand(0)->stripPointerCasts() ==
751 GEPRHS->getOperand(0)->stripPointerCasts() &&
752 !GEPLHS->getType()->isVectorTy()) {
753 Value *LOffset = EmitGEPOffset(GEPLHS);
754 Value *ROffset = EmitGEPOffset(GEPRHS);
755
756 // If we looked through an addrspacecast between different sized address
757 // spaces, the LHS and RHS pointers are different sized
758 // integers. Truncate to the smaller one.
759 Type *LHSIndexTy = LOffset->getType();
760 Type *RHSIndexTy = ROffset->getType();
761 if (LHSIndexTy != RHSIndexTy) {
762 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <
763 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {
764 ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy);
765 } else
766 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy);
767 }
768
770 LOffset, ROffset);
771 return replaceInstUsesWith(I, Cmp);
772 }
773 }
774
775 if (GEPLHS->getOperand(0) == GEPRHS->getOperand(0) &&
776 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
777 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
778 // If the GEPs only differ by one index, compare it.
779 unsigned NumDifferences = 0; // Keep track of # differences.
780 unsigned DiffOperand = 0; // The operand that differs.
781 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
782 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
783 Type *LHSType = GEPLHS->getOperand(i)->getType();
784 Type *RHSType = GEPRHS->getOperand(i)->getType();
785 // FIXME: Better support for vector of pointers.
786 if (LHSType->getPrimitiveSizeInBits() !=
787 RHSType->getPrimitiveSizeInBits() ||
788 (GEPLHS->getType()->isVectorTy() &&
789 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
790 // Irreconcilable differences.
791 NumDifferences = 2;
792 break;
793 }
794
795 if (NumDifferences++)
796 break;
797 DiffOperand = i;
798 }
799
800 if (NumDifferences == 0) // SAME GEP?
801 return replaceInstUsesWith(
802 I, // No comparison is needed here.
803 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));
804 // If two GEPs only differ by an index, compare them.
805 // Note that nowrap flags are always needed when comparing two indices.
806 else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {
807 Value *LHSV = GEPLHS->getOperand(DiffOperand);
808 Value *RHSV = GEPRHS->getOperand(DiffOperand);
809 return NewICmp(NW, LHSV, RHSV);
810 }
811 }
812
813 if (Base.Ptr && CanFold(Base.LHSNW & Base.RHSNW) && !Base.isExpensive()) {
814 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
815 Type *IdxTy = DL.getIndexType(GEPLHS->getType());
816 Value *L =
817 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEP=*/true);
818 Value *R =
819 EmitGEPOffsets(Base.RHSGEPs, Base.RHSNW, IdxTy, /*RewriteGEP=*/true);
820 return NewICmp(Base.LHSNW & Base.RHSNW, L, R);
821 }
822 }
823
824 // Try convert this to an indexed compare by looking through PHIs/casts as a
825 // last resort.
826 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
827}
828
830 // It would be tempting to fold away comparisons between allocas and any
831 // pointer not based on that alloca (e.g. an argument). However, even
832 // though such pointers cannot alias, they can still compare equal.
833 //
834 // But LLVM doesn't specify where allocas get their memory, so if the alloca
835 // doesn't escape we can argue that it's impossible to guess its value, and we
836 // can therefore act as if any such guesses are wrong.
837 //
838 // However, we need to ensure that this folding is consistent: We can't fold
839 // one comparison to false, and then leave a different comparison against the
840 // same value alone (as it might evaluate to true at runtime, leading to a
841 // contradiction). As such, this code ensures that all comparisons are folded
842 // at the same time, and there are no other escapes.
843
844 struct CmpCaptureTracker : public CaptureTracker {
845 AllocaInst *Alloca;
846 bool Captured = false;
847 /// The value of the map is a bit mask of which icmp operands the alloca is
848 /// used in.
850
851 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
852
853 void tooManyUses() override { Captured = true; }
854
855 Action captured(const Use *U, UseCaptureInfo CI) override {
856 // TODO(captures): Use UseCaptureInfo.
857 auto *ICmp = dyn_cast<ICmpInst>(U->getUser());
858 // We need to check that U is based *only* on the alloca, and doesn't
859 // have other contributions from a select/phi operand.
860 // TODO: We could check whether getUnderlyingObjects() reduces to one
861 // object, which would allow looking through phi nodes.
862 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) {
863 // Collect equality icmps of the alloca, and don't treat them as
864 // captures.
865 ICmps[ICmp] |= 1u << U->getOperandNo();
866 return Continue;
867 }
868
869 Captured = true;
870 return Stop;
871 }
872 };
873
874 CmpCaptureTracker Tracker(Alloca);
875 PointerMayBeCaptured(Alloca, &Tracker);
876 if (Tracker.Captured)
877 return false;
878
879 bool Changed = false;
880 for (auto [ICmp, Operands] : Tracker.ICmps) {
881 switch (Operands) {
882 case 1:
883 case 2: {
884 // The alloca is only used in one icmp operand. Assume that the
885 // equality is false.
886 auto *Res = ConstantInt::get(ICmp->getType(),
887 ICmp->getPredicate() == ICmpInst::ICMP_NE);
888 replaceInstUsesWith(*ICmp, Res);
890 Changed = true;
891 break;
892 }
893 case 3:
894 // Both icmp operands are based on the alloca, so this is comparing
895 // pointer offsets, without leaking any information about the address
896 // of the alloca. Ignore such comparisons.
897 break;
898 default:
899 llvm_unreachable("Cannot happen");
900 }
901 }
902
903 return Changed;
904}
905
906/// Fold "icmp pred (X+C), X".
908 CmpPredicate Pred) {
909 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
910 // so the values can never be equal. Similarly for all other "or equals"
911 // operators.
912 assert(!!C && "C should not be zero!");
913
914 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
915 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
916 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
917 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
918 Constant *R =
919 ConstantInt::get(X->getType(), APInt::getMaxValue(C.getBitWidth()) - C);
920 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
921 }
922
923 // (X+1) >u X --> X <u (0-1) --> X != 255
924 // (X+2) >u X --> X <u (0-2) --> X <u 254
925 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
926 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
927 return new ICmpInst(ICmpInst::ICMP_ULT, X,
928 ConstantInt::get(X->getType(), -C));
929
930 APInt SMax = APInt::getSignedMaxValue(C.getBitWidth());
931
932 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
933 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
934 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
935 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
936 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
937 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
938 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
939 return new ICmpInst(ICmpInst::ICMP_SGT, X,
940 ConstantInt::get(X->getType(), SMax - C));
941
942 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
943 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
944 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
945 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
946 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
947 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
948
949 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
950 return new ICmpInst(ICmpInst::ICMP_SLT, X,
951 ConstantInt::get(X->getType(), SMax - (C - 1)));
952}
953
954/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
955/// (icmp eq/ne A, Log2(AP2/AP1)) ->
956/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
958 const APInt &AP1,
959 const APInt &AP2) {
960 assert(I.isEquality() && "Cannot fold icmp gt/lt");
961
962 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
963 if (I.getPredicate() == I.ICMP_NE)
964 Pred = CmpInst::getInversePredicate(Pred);
965 return new ICmpInst(Pred, LHS, RHS);
966 };
967
968 // Don't bother doing any work for cases which InstSimplify handles.
969 if (AP2.isZero())
970 return nullptr;
971
972 bool IsAShr = isa<AShrOperator>(I.getOperand(0));
973 if (IsAShr) {
974 if (AP2.isAllOnes())
975 return nullptr;
976 if (AP2.isNegative() != AP1.isNegative())
977 return nullptr;
978 if (AP2.sgt(AP1))
979 return nullptr;
980 }
981
982 if (!AP1)
983 // 'A' must be large enough to shift out the highest set bit.
984 return getICmp(I.ICMP_UGT, A,
985 ConstantInt::get(A->getType(), AP2.logBase2()));
986
987 if (AP1 == AP2)
988 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
989
990 int Shift;
991 if (IsAShr && AP1.isNegative())
992 Shift = AP1.countl_one() - AP2.countl_one();
993 else
994 Shift = AP1.countl_zero() - AP2.countl_zero();
995
996 if (Shift > 0) {
997 if (IsAShr && AP1 == AP2.ashr(Shift)) {
998 // There are multiple solutions if we are comparing against -1 and the LHS
999 // of the ashr is not a power of two.
1000 if (AP1.isAllOnes() && !AP2.isPowerOf2())
1001 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));
1002 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1003 } else if (AP1 == AP2.lshr(Shift)) {
1004 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1005 }
1006 }
1007
1008 // Shifting const2 will never be equal to const1.
1009 // FIXME: This should always be handled by InstSimplify?
1010 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1011 return replaceInstUsesWith(I, TorF);
1012}
1013
1014/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1015/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1017 const APInt &AP1,
1018 const APInt &AP2) {
1019 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1020
1021 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1022 if (I.getPredicate() == I.ICMP_NE)
1023 Pred = CmpInst::getInversePredicate(Pred);
1024 return new ICmpInst(Pred, LHS, RHS);
1025 };
1026
1027 // Don't bother doing any work for cases which InstSimplify handles.
1028 if (AP2.isZero())
1029 return nullptr;
1030
1031 unsigned AP2TrailingZeros = AP2.countr_zero();
1032
1033 if (!AP1 && AP2TrailingZeros != 0)
1034 return getICmp(
1035 I.ICMP_UGE, A,
1036 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
1037
1038 if (AP1 == AP2)
1039 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1040
1041 // Get the distance between the lowest bits that are set.
1042 int Shift = AP1.countr_zero() - AP2TrailingZeros;
1043
1044 if (Shift > 0 && AP2.shl(Shift) == AP1)
1045 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1046
1047 // Shifting const2 will never be equal to const1.
1048 // FIXME: This should always be handled by InstSimplify?
1049 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1050 return replaceInstUsesWith(I, TorF);
1051}
1052
1053/// The caller has matched a pattern of the form:
1054/// I = icmp ugt (add (add A, B), CI2), CI1
1055/// If this is of the form:
1056/// sum = a + b
1057/// if (sum+128 >u 255)
1058/// Then replace it with llvm.sadd.with.overflow.i8.
1059///
1061 ConstantInt *CI2, ConstantInt *CI1,
1062 InstCombinerImpl &IC) {
1063 // The transformation we're trying to do here is to transform this into an
1064 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1065 // with a narrower add, and discard the add-with-constant that is part of the
1066 // range check (if we can't eliminate it, this isn't profitable).
1067
1068 // In order to eliminate the add-with-constant, the compare can be its only
1069 // use.
1070 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1071 if (!AddWithCst->hasOneUse())
1072 return nullptr;
1073
1074 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1075 if (!CI2->getValue().isPowerOf2())
1076 return nullptr;
1077 unsigned NewWidth = CI2->getValue().countr_zero();
1078 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1079 return nullptr;
1080
1081 // The width of the new add formed is 1 more than the bias.
1082 ++NewWidth;
1083
1084 // Check to see that CI1 is an all-ones value with NewWidth bits.
1085 if (CI1->getBitWidth() == NewWidth ||
1086 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1087 return nullptr;
1088
1089 // This is only really a signed overflow check if the inputs have been
1090 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1091 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1092 if (IC.ComputeMaxSignificantBits(A, &I) > NewWidth ||
1093 IC.ComputeMaxSignificantBits(B, &I) > NewWidth)
1094 return nullptr;
1095
1096 // In order to replace the original add with a narrower
1097 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1098 // and truncates that discard the high bits of the add. Verify that this is
1099 // the case.
1100 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1101 for (User *U : OrigAdd->users()) {
1102 if (U == AddWithCst)
1103 continue;
1104
1105 // Only accept truncates for now. We would really like a nice recursive
1106 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1107 // chain to see which bits of a value are actually demanded. If the
1108 // original add had another add which was then immediately truncated, we
1109 // could still do the transformation.
1111 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1112 return nullptr;
1113 }
1114
1115 // If the pattern matches, truncate the inputs to the narrower type and
1116 // use the sadd_with_overflow intrinsic to efficiently compute both the
1117 // result and the overflow bit.
1118 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1120 I.getModule(), Intrinsic::sadd_with_overflow, NewType);
1121
1122 InstCombiner::BuilderTy &Builder = IC.Builder;
1123
1124 // Put the new code above the original add, in case there are any uses of the
1125 // add between the add and the compare.
1126 Builder.SetInsertPoint(OrigAdd);
1127
1128 Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc");
1129 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc");
1130 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd");
1131 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result");
1132 Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType());
1133
1134 // The inner add was the result of the narrow add, zero extended to the
1135 // wider type. Replace it with the result computed by the intrinsic.
1136 IC.replaceInstUsesWith(*OrigAdd, ZExt);
1137 IC.eraseInstFromFunction(*OrigAdd);
1138
1139 // The original icmp gets replaced with the overflow value.
1140 return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1141}
1142
1143/// If we have:
1144/// icmp eq/ne (urem/srem %x, %y), 0
1145/// iff %y is a power-of-two, we can replace this with a bit test:
1146/// icmp eq/ne (and %x, (add %y, -1)), 0
1148 // This fold is only valid for equality predicates.
1149 if (!I.isEquality())
1150 return nullptr;
1151 CmpPredicate Pred;
1152 Value *X, *Y, *Zero;
1153 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))),
1154 m_CombineAnd(m_Zero(), m_Value(Zero)))))
1155 return nullptr;
1156 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, &I))
1157 return nullptr;
1158 // This may increase instruction count, we don't enforce that Y is a constant.
1159 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType()));
1160 Value *Masked = Builder.CreateAnd(X, Mask);
1161 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, Zero);
1162}
1163
1164/// Fold equality-comparison between zero and any (maybe truncated) right-shift
1165/// by one-less-than-bitwidth into a sign test on the original value.
1167 Instruction *Val;
1168 CmpPredicate Pred;
1169 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero())))
1170 return nullptr;
1171
1172 Value *X;
1173 Type *XTy;
1174
1175 Constant *C;
1176 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) {
1177 XTy = X->getType();
1178 unsigned XBitWidth = XTy->getScalarSizeInBits();
1180 APInt(XBitWidth, XBitWidth - 1))))
1181 return nullptr;
1182 } else if (isa<BinaryOperator>(Val) &&
1184 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val),
1185 /*AnalyzeForSignBitExtraction=*/true))) {
1186 XTy = X->getType();
1187 } else
1188 return nullptr;
1189
1190 return ICmpInst::Create(Instruction::ICmp,
1194}
1195
1196// Handle icmp pred X, 0
1198 CmpInst::Predicate Pred = Cmp.getPredicate();
1199 if (!match(Cmp.getOperand(1), m_Zero()))
1200 return nullptr;
1201
1202 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1203 if (Pred == ICmpInst::ICMP_SGT) {
1204 Value *A, *B;
1205 if (match(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) {
1206 if (isKnownPositive(A, SQ.getWithInstruction(&Cmp)))
1207 return new ICmpInst(Pred, B, Cmp.getOperand(1));
1208 if (isKnownPositive(B, SQ.getWithInstruction(&Cmp)))
1209 return new ICmpInst(Pred, A, Cmp.getOperand(1));
1210 }
1211 }
1212
1214 return New;
1215
1216 // Given:
1217 // icmp eq/ne (urem %x, %y), 0
1218 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
1219 // icmp eq/ne %x, 0
1220 Value *X, *Y;
1221 if (match(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) &&
1222 ICmpInst::isEquality(Pred)) {
1223 KnownBits XKnown = computeKnownBits(X, &Cmp);
1224 KnownBits YKnown = computeKnownBits(Y, &Cmp);
1225 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1226 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1227 }
1228
1229 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are
1230 // odd/non-zero/there is no overflow.
1231 if (match(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) &&
1232 ICmpInst::isEquality(Pred)) {
1233
1234 KnownBits XKnown = computeKnownBits(X, &Cmp);
1235 // if X % 2 != 0
1236 // (icmp eq/ne Y)
1237 if (XKnown.countMaxTrailingZeros() == 0)
1238 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1239
1240 KnownBits YKnown = computeKnownBits(Y, &Cmp);
1241 // if Y % 2 != 0
1242 // (icmp eq/ne X)
1243 if (YKnown.countMaxTrailingZeros() == 0)
1244 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1245
1246 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0));
1247 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1248 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
1249 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`
1250 // but to avoid unnecessary work, first just if this is an obvious case.
1251
1252 // if X non-zero and NoOverflow(X * Y)
1253 // (icmp eq/ne Y)
1254 if (!XKnown.One.isZero() || isKnownNonZero(X, Q))
1255 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1256
1257 // if Y non-zero and NoOverflow(X * Y)
1258 // (icmp eq/ne X)
1259 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q))
1260 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1261 }
1262 // Note, we are skipping cases:
1263 // if Y % 2 != 0 AND X % 2 != 0
1264 // (false/true)
1265 // if X non-zero and Y non-zero and NoOverflow(X * Y)
1266 // (false/true)
1267 // Those can be simplified later as we would have already replaced the (icmp
1268 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
1269 // will fold to a constant elsewhere.
1270 }
1271
1272 // (icmp eq/ne f(X), 0) -> (icmp eq/ne X, 0)
1273 // where f(X) == 0 if and only if X == 0
1274 if (ICmpInst::isEquality(Pred))
1275 if (Value *Stripped = stripNullTest(Cmp.getOperand(0)))
1276 return new ICmpInst(Pred, Stripped,
1277 Constant::getNullValue(Stripped->getType()));
1278
1279 return nullptr;
1280}
1281
1282/// Fold icmp eq (num + mask) & ~mask, num
1283/// to
1284/// icmp eq (and num, mask), 0
1285/// Where mask is a low bit mask.
1287 Value *Num;
1288 CmpPredicate Pred;
1289 const APInt *Mask, *Neg;
1290
1291 if (!match(&Cmp,
1292 m_c_ICmp(Pred, m_Value(Num),
1294 m_LowBitMask(Mask))),
1295 m_APInt(Neg))))))
1296 return nullptr;
1297
1298 if (*Neg != ~*Mask)
1299 return nullptr;
1300
1301 if (!ICmpInst::isEquality(Pred))
1302 return nullptr;
1303
1304 // Create new icmp eq (num & mask), 0
1305 auto *NewAnd = Builder.CreateAnd(Num, *Mask);
1306 auto *Zero = Constant::getNullValue(Num->getType());
1307
1308 return new ICmpInst(Pred, NewAnd, Zero);
1309}
1310
1311/// Fold icmp Pred X, C.
1312/// TODO: This code structure does not make sense. The saturating add fold
1313/// should be moved to some other helper and extended as noted below (it is also
1314/// possible that code has been made unnecessary - do we canonicalize IR to
1315/// overflow/saturating intrinsics or not?).
1317 // Match the following pattern, which is a common idiom when writing
1318 // overflow-safe integer arithmetic functions. The source performs an addition
1319 // in wider type and explicitly checks for overflow using comparisons against
1320 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1321 //
1322 // TODO: This could probably be generalized to handle other overflow-safe
1323 // operations if we worked out the formulas to compute the appropriate magic
1324 // constants.
1325 //
1326 // sum = a + b
1327 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1328 CmpInst::Predicate Pred = Cmp.getPredicate();
1329 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
1330 Value *A, *B;
1331 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1332 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) &&
1333 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1334 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
1335 return Res;
1336
1337 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1339 if (!C)
1340 return nullptr;
1341
1342 if (auto *Phi = dyn_cast<PHINode>(Op0))
1343 if (all_of(Phi->operands(), IsaPred<Constant>)) {
1345 for (Value *V : Phi->incoming_values()) {
1346 Constant *Res =
1348 if (!Res)
1349 return nullptr;
1350 Ops.push_back(Res);
1351 }
1352 Builder.SetInsertPoint(Phi);
1353 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());
1354 for (auto [V, Pred] : zip(Ops, Phi->blocks()))
1355 NewPhi->addIncoming(V, Pred);
1356 return replaceInstUsesWith(Cmp, NewPhi);
1357 }
1358
1360 return R;
1361
1362 return nullptr;
1363}
1364
1365/// Canonicalize icmp instructions based on dominating conditions.
1367 // We already checked simple implication in InstSimplify, only handle complex
1368 // cases here.
1369 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1);
1370 const APInt *C;
1371 if (!match(Y, m_APInt(C)))
1372 return nullptr;
1373
1374 CmpInst::Predicate Pred = Cmp.getPredicate();
1376
1377 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1378 const APInt *DomC) -> Instruction * {
1379 // We have 2 compares of a variable with constants. Calculate the constant
1380 // ranges of those compares to see if we can transform the 2nd compare:
1381 // DomBB:
1382 // DomCond = icmp DomPred X, DomC
1383 // br DomCond, CmpBB, FalseBB
1384 // CmpBB:
1385 // Cmp = icmp Pred X, C
1386 ConstantRange DominatingCR =
1387 ConstantRange::makeExactICmpRegion(DomPred, *DomC);
1388 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1389 ConstantRange Difference = DominatingCR.difference(CR);
1390 if (Intersection.isEmptySet())
1391 return replaceInstUsesWith(Cmp, Builder.getFalse());
1392 if (Difference.isEmptySet())
1393 return replaceInstUsesWith(Cmp, Builder.getTrue());
1394
1395 // Canonicalizing a sign bit comparison that gets used in a branch,
1396 // pessimizes codegen by generating branch on zero instruction instead
1397 // of a test and branch. So we avoid canonicalizing in such situations
1398 // because test and branch instruction has better branch displacement
1399 // than compare and branch instruction.
1400 bool UnusedBit;
1401 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit);
1402 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp)))
1403 return nullptr;
1404
1405 // Avoid an infinite loop with min/max canonicalization.
1406 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1407 if (Cmp.hasOneUse() &&
1408 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value())))
1409 return nullptr;
1410
1411 if (const APInt *EqC = Intersection.getSingleElement())
1412 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC));
1413 if (const APInt *NeC = Difference.getSingleElement())
1414 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC));
1415 return nullptr;
1416 };
1417
1418 for (BranchInst *BI : DC.conditionsFor(X)) {
1419 CmpPredicate DomPred;
1420 const APInt *DomC;
1421 if (!match(BI->getCondition(),
1422 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC))))
1423 continue;
1424
1425 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1426 if (DT.dominates(Edge0, Cmp.getParent())) {
1427 if (auto *V = handleDomCond(DomPred, DomC))
1428 return V;
1429 } else {
1430 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1431 if (DT.dominates(Edge1, Cmp.getParent()))
1432 if (auto *V =
1433 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC))
1434 return V;
1435 }
1436 }
1437
1438 return nullptr;
1439}
1440
1441/// Fold icmp (trunc X), C.
1443 TruncInst *Trunc,
1444 const APInt &C) {
1445 ICmpInst::Predicate Pred = Cmp.getPredicate();
1446 Value *X = Trunc->getOperand(0);
1447 Type *SrcTy = X->getType();
1448 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1449 SrcBits = SrcTy->getScalarSizeInBits();
1450
1451 // Match (icmp pred (trunc nuw/nsw X), C)
1452 // Which we can convert to (icmp pred X, (sext/zext C))
1453 if (shouldChangeType(Trunc->getType(), SrcTy)) {
1454 if (Trunc->hasNoSignedWrap())
1455 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
1456 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1457 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
1458 }
1459
1460 if (C.isOne() && C.getBitWidth() > 1) {
1461 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1462 Value *V = nullptr;
1463 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
1464 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1465 ConstantInt::get(V->getType(), 1));
1466 }
1467
1468 // TODO: Handle any shifted constant by subtracting trailing zeros.
1469 // TODO: Handle non-equality predicates.
1470 Value *Y;
1471 if (Cmp.isEquality() && match(X, m_Shl(m_One(), m_Value(Y)))) {
1472 // (trunc (1 << Y) to iN) == 0 --> Y u>= N
1473 // (trunc (1 << Y) to iN) != 0 --> Y u< N
1474 if (C.isZero()) {
1475 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1476 return new ICmpInst(NewPred, Y, ConstantInt::get(SrcTy, DstBits));
1477 }
1478 // (trunc (1 << Y) to iN) == 2**C --> Y == C
1479 // (trunc (1 << Y) to iN) != 2**C --> Y != C
1480 if (C.isPowerOf2())
1481 return new ICmpInst(Pred, Y, ConstantInt::get(SrcTy, C.logBase2()));
1482 }
1483
1484 if (Cmp.isEquality() && (Trunc->hasOneUse() || Trunc->hasNoUnsignedWrap())) {
1485 // Canonicalize to a mask and wider compare if the wide type is suitable:
1486 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1487 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {
1488 Constant *Mask =
1489 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits));
1490 Value *And = Trunc->hasNoUnsignedWrap() ? X : Builder.CreateAnd(X, Mask);
1491 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits));
1492 return new ICmpInst(Pred, And, WideC);
1493 }
1494
1495 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1496 // of the high bits truncated out of x are known.
1497 KnownBits Known = computeKnownBits(X, &Cmp);
1498
1499 // If all the high bits are known, we can do this xform.
1500 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1501 // Pull in the high bits from known-ones set.
1502 APInt NewRHS = C.zext(SrcBits);
1503 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
1504 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS));
1505 }
1506 }
1507
1508 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1509 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1510 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1511 Value *ShOp;
1512 uint64_t ShAmt;
1513 bool TrueIfSigned;
1514 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
1515 match(X, m_Shr(m_Value(ShOp), m_ConstantInt(ShAmt))) &&
1516 DstBits == SrcBits - ShAmt) {
1517 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1519 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1521 }
1522
1523 return nullptr;
1524}
1525
1526/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1527/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1530 const SimplifyQuery &Q) {
1531 Value *X, *Y;
1532 CmpPredicate Pred;
1533 bool YIsSExt = false;
1534 // Try to match icmp (trunc X), (trunc Y)
1535 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {
1536 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() &
1537 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind();
1538 if (Cmp.isSigned()) {
1539 // For signed comparisons, both truncs must be nsw.
1540 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1541 return nullptr;
1542 } else {
1543 // For unsigned and equality comparisons, either both must be nuw or
1544 // both must be nsw, we don't care which.
1545 if (!NoWrapFlags)
1546 return nullptr;
1547 }
1548
1549 if (X->getType() != Y->getType() &&
1550 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))
1551 return nullptr;
1552 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) &&
1553 isDesirableIntType(Y->getType()->getScalarSizeInBits())) {
1554 std::swap(X, Y);
1555 Pred = Cmp.getSwappedPredicate(Pred);
1556 }
1557 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1558 }
1559 // Try to match icmp (trunc nuw X), (zext Y)
1560 else if (!Cmp.isSigned() &&
1561 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),
1562 m_OneUse(m_ZExt(m_Value(Y)))))) {
1563 // Can fold trunc nuw + zext for unsigned and equality predicates.
1564 }
1565 // Try to match icmp (trunc nsw X), (sext Y)
1566 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),
1568 // Can fold trunc nsw + zext/sext for all predicates.
1569 YIsSExt =
1570 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));
1571 } else
1572 return nullptr;
1573
1574 Type *TruncTy = Cmp.getOperand(0)->getType();
1575 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1576
1577 // If this transform will end up changing from desirable types -> undesirable
1578 // types skip it.
1579 if (isDesirableIntType(TruncBits) &&
1580 !isDesirableIntType(X->getType()->getScalarSizeInBits()))
1581 return nullptr;
1582
1583 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);
1584 return new ICmpInst(Pred, X, NewY);
1585}
1586
1587/// Fold icmp (xor X, Y), C.
1590 const APInt &C) {
1591 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1592 return I;
1593
1594 Value *X = Xor->getOperand(0);
1595 Value *Y = Xor->getOperand(1);
1596 const APInt *XorC;
1597 if (!match(Y, m_APInt(XorC)))
1598 return nullptr;
1599
1600 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1601 // fold the xor.
1602 ICmpInst::Predicate Pred = Cmp.getPredicate();
1603 bool TrueIfSigned = false;
1604 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {
1605
1606 // If the sign bit of the XorCst is not set, there is no change to
1607 // the operation, just stop using the Xor.
1608 if (!XorC->isNegative())
1609 return replaceOperand(Cmp, 0, X);
1610
1611 // Emit the opposite comparison.
1612 if (TrueIfSigned)
1613 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1614 ConstantInt::getAllOnesValue(X->getType()));
1615 else
1616 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1617 ConstantInt::getNullValue(X->getType()));
1618 }
1619
1620 if (Xor->hasOneUse()) {
1621 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1622 if (!Cmp.isEquality() && XorC->isSignMask()) {
1623 Pred = Cmp.getFlippedSignednessPredicate();
1624 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1625 }
1626
1627 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1628 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1629 Pred = Cmp.getFlippedSignednessPredicate();
1630 Pred = Cmp.getSwappedPredicate(Pred);
1631 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1632 }
1633 }
1634
1635 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1636 if (Pred == ICmpInst::ICMP_UGT) {
1637 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1638 if (*XorC == ~C && (C + 1).isPowerOf2())
1639 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1640 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1641 if (*XorC == C && (C + 1).isPowerOf2())
1642 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1643 }
1644 if (Pred == ICmpInst::ICMP_ULT) {
1645 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1646 if (*XorC == -C && C.isPowerOf2())
1647 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1648 ConstantInt::get(X->getType(), ~C));
1649 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1650 if (*XorC == C && (-C).isPowerOf2())
1651 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1652 ConstantInt::get(X->getType(), ~C));
1653 }
1654 return nullptr;
1655}
1656
1657/// For power-of-2 C:
1658/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1659/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1662 const APInt &C) {
1663 CmpInst::Predicate Pred = Cmp.getPredicate();
1664 APInt PowerOf2;
1665 if (Pred == ICmpInst::ICMP_ULT)
1666 PowerOf2 = C;
1667 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1668 PowerOf2 = C + 1;
1669 else
1670 return nullptr;
1671 if (!PowerOf2.isPowerOf2())
1672 return nullptr;
1673 Value *X;
1674 const APInt *ShiftC;
1676 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))
1677 return nullptr;
1678 uint64_t Shift = ShiftC->getLimitedValue();
1679 Type *XType = X->getType();
1680 if (Shift == 0 || PowerOf2.isMinSignedValue())
1681 return nullptr;
1682 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));
1683 APInt Bound =
1684 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1685 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));
1686}
1687
1688/// Fold icmp (and (sh X, Y), C2), C1.
1691 const APInt &C1,
1692 const APInt &C2) {
1693 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1694 if (!Shift || !Shift->isShift())
1695 return nullptr;
1696
1697 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1698 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1699 // code produced by the clang front-end, for bitfield access.
1700 // This seemingly simple opportunity to fold away a shift turns out to be
1701 // rather complicated. See PR17827 for details.
1702 unsigned ShiftOpcode = Shift->getOpcode();
1703 bool IsShl = ShiftOpcode == Instruction::Shl;
1704 const APInt *C3;
1705 if (match(Shift->getOperand(1), m_APInt(C3))) {
1706 APInt NewAndCst, NewCmpCst;
1707 bool AnyCmpCstBitsShiftedOut;
1708 if (ShiftOpcode == Instruction::Shl) {
1709 // For a left shift, we can fold if the comparison is not signed. We can
1710 // also fold a signed comparison if the mask value and comparison value
1711 // are not negative. These constraints may not be obvious, but we can
1712 // prove that they are correct using an SMT solver.
1713 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1714 return nullptr;
1715
1716 NewCmpCst = C1.lshr(*C3);
1717 NewAndCst = C2.lshr(*C3);
1718 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;
1719 } else if (ShiftOpcode == Instruction::LShr) {
1720 // For a logical right shift, we can fold if the comparison is not signed.
1721 // We can also fold a signed comparison if the shifted mask value and the
1722 // shifted comparison value are not negative. These constraints may not be
1723 // obvious, but we can prove that they are correct using an SMT solver.
1724 NewCmpCst = C1.shl(*C3);
1725 NewAndCst = C2.shl(*C3);
1726 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;
1727 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1728 return nullptr;
1729 } else {
1730 // For an arithmetic shift, check that both constants don't use (in a
1731 // signed sense) the top bits being shifted out.
1732 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1733 NewCmpCst = C1.shl(*C3);
1734 NewAndCst = C2.shl(*C3);
1735 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;
1736 if (NewAndCst.ashr(*C3) != C2)
1737 return nullptr;
1738 }
1739
1740 if (AnyCmpCstBitsShiftedOut) {
1741 // If we shifted bits out, the fold is not going to work out. As a
1742 // special case, check to see if this means that the result is always
1743 // true or false now.
1744 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1745 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1746 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1747 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1748 } else {
1749 Value *NewAnd = Builder.CreateAnd(
1750 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));
1751 return new ICmpInst(Cmp.getPredicate(), NewAnd,
1752 ConstantInt::get(And->getType(), NewCmpCst));
1753 }
1754 }
1755
1756 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1757 // preferable because it allows the C2 << Y expression to be hoisted out of a
1758 // loop if Y is invariant and X is not.
1759 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1760 !Shift->isArithmeticShift() &&
1761 ((!IsShl && C2.isOne()) || !isa<Constant>(Shift->getOperand(0)))) {
1762 // Compute C2 << Y.
1763 Value *NewShift =
1764 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))
1765 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));
1766
1767 // Compute X & (C2 << Y).
1768 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1769 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1770 }
1771
1772 return nullptr;
1773}
1774
1775/// Fold icmp (and X, C2), C1.
1778 const APInt &C1) {
1779 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1780
1781 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
1782 // TODO: We canonicalize to the longer form for scalars because we have
1783 // better analysis/folds for icmp, and codegen may be better with icmp.
1784 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&
1785 match(And->getOperand(1), m_One()))
1786 return new TruncInst(And->getOperand(0), Cmp.getType());
1787
1788 const APInt *C2;
1789 Value *X;
1790 if (!match(And, m_And(m_Value(X), m_APInt(C2))))
1791 return nullptr;
1792
1793 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask
1794 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(~*C2) &&
1795 C2->isNegatedPowerOf2())
1796 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1797 ConstantInt::get(X->getType(), ~*C2));
1798 // (and X, highmask) s< [1, -highmask] --> X s< -highmask
1799 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() &&
1800 (C1 - 1).ule(~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask())
1801 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1802 ConstantInt::get(X->getType(), -*C2));
1803
1804 // Don't perform the following transforms if the AND has multiple uses
1805 if (!And->hasOneUse())
1806 return nullptr;
1807
1808 if (Cmp.isEquality() && C1.isZero()) {
1809 // Restrict this fold to single-use 'and' (PR10267).
1810 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1811 if (C2->isSignMask()) {
1812 Constant *Zero = Constant::getNullValue(X->getType());
1813 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1814 return new ICmpInst(NewPred, X, Zero);
1815 }
1816
1817 APInt NewC2 = *C2;
1818 KnownBits Know = computeKnownBits(And->getOperand(0), And);
1819 // Set high zeros of C2 to allow matching negated power-of-2.
1820 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
1821 Know.countMinLeadingZeros());
1822
1823 // Restrict this fold only for single-use 'and' (PR10267).
1824 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1825 if (NewC2.isNegatedPowerOf2()) {
1826 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);
1827 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1828 return new ICmpInst(NewPred, X, NegBOC);
1829 }
1830 }
1831
1832 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1833 // the input width without changing the value produced, eliminate the cast:
1834 //
1835 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1836 //
1837 // We can do this transformation if the constants do not have their sign bits
1838 // set or if it is an equality comparison. Extending a relational comparison
1839 // when we're checking the sign bit would not work.
1840 Value *W;
1841 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&
1842 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1843 // TODO: Is this a good transform for vectors? Wider types may reduce
1844 // throughput. Should this transform be limited (even for scalars) by using
1845 // shouldChangeType()?
1846 if (!Cmp.getType()->isVectorTy()) {
1847 Type *WideType = W->getType();
1848 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1849 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));
1850 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1851 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());
1852 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1853 }
1854 }
1855
1856 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))
1857 return I;
1858
1859 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1860 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1861 //
1862 // iff pred isn't signed
1863 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&
1864 match(And->getOperand(1), m_One())) {
1865 Constant *One = cast<Constant>(And->getOperand(1));
1866 Value *Or = And->getOperand(0);
1867 Value *A, *B, *LShr;
1868 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1869 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1870 unsigned UsesRemoved = 0;
1871 if (And->hasOneUse())
1872 ++UsesRemoved;
1873 if (Or->hasOneUse())
1874 ++UsesRemoved;
1875 if (LShr->hasOneUse())
1876 ++UsesRemoved;
1877
1878 // Compute A & ((1 << B) | 1)
1879 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;
1880 if (UsesRemoved >= RequireUsesRemoved) {
1881 Value *NewOr =
1882 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),
1883 /*HasNUW=*/true),
1884 One, Or->getName());
1885 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1886 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1887 }
1888 }
1889 }
1890
1891 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1892 // llvm.is.fpclass(X, fcInf|fcNan)
1893 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1894 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1895 // (icmp eq (and (bitcast X to int), ExponentMask), 0) -->
1896 // llvm.is.fpclass(X, fcSubnormal|fcZero)
1897 // (icmp ne (and (bitcast X to int), ExponentMask), 0) -->
1898 // llvm.is.fpclass(X, ~(fcSubnormal|fcZero))
1899 Value *V;
1900 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1901 Attribute::NoImplicitFloat) &&
1902 Cmp.isEquality() &&
1904 Type *FPType = V->getType()->getScalarType();
1905 if (FPType->isIEEELikeFPTy() && (C1.isZero() || C1 == *C2)) {
1906 APInt ExponentMask =
1907 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt();
1908 if (*C2 == ExponentMask) {
1909 unsigned Mask = C1.isZero()
1912 if (isICMP_NE)
1913 Mask = ~Mask & fcAllFlags;
1914 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));
1915 }
1916 }
1917 }
1918
1919 return nullptr;
1920}
1921
1922/// Fold icmp (and X, Y), C.
1925 const APInt &C) {
1926 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1927 return I;
1928
1929 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1930 bool TrueIfNeg;
1931 if (isSignBitCheck(Pred, C, TrueIfNeg)) {
1932 // ((X - 1) & ~X) < 0 --> X == 0
1933 // ((X - 1) & ~X) >= 0 --> X != 0
1934 Value *X;
1935 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&
1936 match(And->getOperand(1), m_Not(m_Specific(X)))) {
1937 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1938 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));
1939 }
1940 // (X & -X) < 0 --> X == MinSignedC
1941 // (X & -X) > -1 --> X != MinSignedC
1942 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {
1943 Constant *MinSignedC = ConstantInt::get(
1944 X->getType(),
1945 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
1946 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1947 return new ICmpInst(NewPred, X, MinSignedC);
1948 }
1949 }
1950
1951 // TODO: These all require that Y is constant too, so refactor with the above.
1952
1953 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1954 Value *X = And->getOperand(0);
1955 Value *Y = And->getOperand(1);
1956 if (auto *C2 = dyn_cast<ConstantInt>(Y))
1957 if (auto *LI = dyn_cast<LoadInst>(X))
1958 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1959 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(LI, GEP, Cmp, C2))
1960 return Res;
1961
1962 if (!Cmp.isEquality())
1963 return nullptr;
1964
1965 // X & -C == -C -> X > u ~C
1966 // X & -C != -C -> X <= u ~C
1967 // iff C is a power of 2
1968 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {
1969 auto NewPred =
1971 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1972 }
1973
1974 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
1975 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1976 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1977 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
1979 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
1980 Value *TruncY = Builder.CreateTrunc(Y, X->getType());
1981 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
1982 Value *And = Builder.CreateAnd(TruncY, X);
1984 }
1985 return BinaryOperator::CreateAnd(TruncY, X);
1986 }
1987
1988 // (icmp eq/ne (and (shl -1, X), Y), 0)
1989 // -> (icmp eq/ne (lshr Y, X), 0)
1990 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
1991 // highly unlikely the non-zero case will ever show up in code.
1992 if (C.isZero() &&
1994 m_Value(Y))))) {
1995 Value *LShr = Builder.CreateLShr(Y, X);
1996 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));
1997 }
1998
1999 // (icmp eq/ne (and (add A, Addend), Msk), C)
2000 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk))
2001 {
2002 Value *A;
2003 const APInt *Addend, *Msk;
2004 if (match(And, m_And(m_OneUse(m_Add(m_Value(A), m_APInt(Addend))),
2005 m_LowBitMask(Msk))) &&
2006 C.ule(*Msk)) {
2007 APInt NewComperand = (C - *Addend) & *Msk;
2008 Value *MaskA = Builder.CreateAnd(A, ConstantInt::get(A->getType(), *Msk));
2009 return new ICmpInst(Pred, MaskA,
2010 ConstantInt::get(MaskA->getType(), NewComperand));
2011 }
2012 }
2013
2014 return nullptr;
2015}
2016
2017/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
2019 InstCombiner::BuilderTy &Builder) {
2020 // Are we using xors or subs to bitwise check for a pair or pairs of
2021 // (in)equalities? Convert to a shorter form that has more potential to be
2022 // folded even further.
2023 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
2024 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
2025 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
2026 // (X1 == X2) && (X3 == X4) && (X5 == X6)
2027 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
2028 // (X1 != X2) || (X3 != X4) || (X5 != X6)
2030 SmallVector<Value *, 16> WorkList(1, Or);
2031
2032 while (!WorkList.empty()) {
2033 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
2034 Value *Lhs, *Rhs;
2035
2036 if (match(OrOperatorArgument,
2037 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {
2038 CmpValues.emplace_back(Lhs, Rhs);
2039 return;
2040 }
2041
2042 if (match(OrOperatorArgument,
2043 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {
2044 CmpValues.emplace_back(Lhs, Rhs);
2045 return;
2046 }
2047
2048 WorkList.push_back(OrOperatorArgument);
2049 };
2050
2051 Value *CurrentValue = WorkList.pop_back_val();
2052 Value *OrOperatorLhs, *OrOperatorRhs;
2053
2054 if (!match(CurrentValue,
2055 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {
2056 return nullptr;
2057 }
2058
2059 MatchOrOperatorArgument(OrOperatorRhs);
2060 MatchOrOperatorArgument(OrOperatorLhs);
2061 }
2062
2063 ICmpInst::Predicate Pred = Cmp.getPredicate();
2064 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2065 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,
2066 CmpValues.rbegin()->second);
2067
2068 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2069 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);
2070 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);
2071 }
2072
2073 return LhsCmp;
2074}
2075
2076/// Fold icmp (or X, Y), C.
2079 const APInt &C) {
2080 ICmpInst::Predicate Pred = Cmp.getPredicate();
2081 if (C.isOne()) {
2082 // icmp slt signum(V) 1 --> icmp slt V, 1
2083 Value *V = nullptr;
2084 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
2085 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2086 ConstantInt::get(V->getType(), 1));
2087 }
2088
2089 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
2090
2091 // (icmp eq/ne (or disjoint x, C0), C1)
2092 // -> (icmp eq/ne x, C0^C1)
2093 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
2094 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {
2095 Value *NewC =
2096 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));
2097 return new ICmpInst(Pred, OrOp0, NewC);
2098 }
2099
2100 const APInt *MaskC;
2101 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {
2102 if (*MaskC == C && (C + 1).isPowerOf2()) {
2103 // X | C == C --> X <=u C
2104 // X | C != C --> X >u C
2105 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2107 return new ICmpInst(Pred, OrOp0, OrOp1);
2108 }
2109
2110 // More general: canonicalize 'equality with set bits mask' to
2111 // 'equality with clear bits mask'.
2112 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2113 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2114 if (Or->hasOneUse()) {
2115 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));
2116 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));
2117 return new ICmpInst(Pred, And, NewC);
2118 }
2119 }
2120
2121 // (X | (X-1)) s< 0 --> X s< 1
2122 // (X | (X-1)) s> -1 --> X s> 0
2123 Value *X;
2124 bool TrueIfSigned;
2125 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
2127 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2128 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
2129 return new ICmpInst(NewPred, X, NewC);
2130 }
2131
2132 const APInt *OrC;
2133 // icmp(X | OrC, C) --> icmp(X, 0)
2134 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {
2135 switch (Pred) {
2136 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2137 case ICmpInst::ICMP_SLT:
2138 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2139 case ICmpInst::ICMP_SGE:
2140 if (OrC->sge(C))
2141 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
2142 break;
2143 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2144 case ICmpInst::ICMP_SLE:
2145 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2146 case ICmpInst::ICMP_SGT:
2147 if (OrC->sgt(C))
2149 ConstantInt::getNullValue(X->getType()));
2150 break;
2151 default:
2152 break;
2153 }
2154 }
2155
2156 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2157 return nullptr;
2158
2159 Value *P, *Q;
2161 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2162 // -> and (icmp eq P, null), (icmp eq Q, null).
2163 Value *CmpP =
2164 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
2165 Value *CmpQ =
2166 Builder.CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType()));
2167 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2168 return BinaryOperator::Create(BOpc, CmpP, CmpQ);
2169 }
2170
2171 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2172 return replaceInstUsesWith(Cmp, V);
2173
2174 return nullptr;
2175}
2176
2177/// Fold icmp (mul X, Y), C.
2180 const APInt &C) {
2181 ICmpInst::Predicate Pred = Cmp.getPredicate();
2182 Type *MulTy = Mul->getType();
2183 Value *X = Mul->getOperand(0);
2184
2185 // If there's no overflow:
2186 // X * X == 0 --> X == 0
2187 // X * X != 0 --> X != 0
2188 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
2189 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2190 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2191
2192 const APInt *MulC;
2193 if (!match(Mul->getOperand(1), m_APInt(MulC)))
2194 return nullptr;
2195
2196 // If this is a test of the sign bit and the multiply is sign-preserving with
2197 // a constant operand, use the multiply LHS operand instead:
2198 // (X * +MulC) < 0 --> X < 0
2199 // (X * -MulC) < 0 --> X > 0
2200 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2201 if (MulC->isNegative())
2202 Pred = ICmpInst::getSwappedPredicate(Pred);
2203 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2204 }
2205
2206 if (MulC->isZero())
2207 return nullptr;
2208
2209 // If the multiply does not wrap or the constant is odd, try to divide the
2210 // compare constant by the multiplication factor.
2211 if (Cmp.isEquality()) {
2212 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2213 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2214 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2215 return new ICmpInst(Pred, X, NewC);
2216 }
2217
2218 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2219 // correct to transform if MulC * N == C including overflow. I.e with i8
2220 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2221 // miss that case.
2222 if (C.urem(*MulC).isZero()) {
2223 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2224 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2225 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2226 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2227 return new ICmpInst(Pred, X, NewC);
2228 }
2229 }
2230 }
2231
2232 // With a matching no-overflow guarantee, fold the constants:
2233 // (X * MulC) < C --> X < (C / MulC)
2234 // (X * MulC) > C --> X > (C / MulC)
2235 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2236 Constant *NewC = nullptr;
2237 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2238 // MININT / -1 --> overflow.
2239 if (C.isMinSignedValue() && MulC->isAllOnes())
2240 return nullptr;
2241 if (MulC->isNegative())
2242 Pred = ICmpInst::getSwappedPredicate(Pred);
2243
2244 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2245 NewC = ConstantInt::get(
2247 } else {
2248 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2249 "Unexpected predicate");
2250 NewC = ConstantInt::get(
2252 }
2253 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2254 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2255 NewC = ConstantInt::get(
2257 } else {
2258 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2259 "Unexpected predicate");
2260 NewC = ConstantInt::get(
2262 }
2263 }
2264
2265 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2266}
2267
2268/// Fold icmp (shl nuw C2, Y), C.
2270 const APInt &C) {
2271 Value *Y;
2272 const APInt *C2;
2273 if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y))))
2274 return nullptr;
2275
2276 Type *ShiftType = Shl->getType();
2277 unsigned TypeBits = C.getBitWidth();
2278 ICmpInst::Predicate Pred = Cmp.getPredicate();
2279 if (Cmp.isUnsigned()) {
2280 if (C2->isZero() || C2->ugt(C))
2281 return nullptr;
2282 APInt Div, Rem;
2283 APInt::udivrem(C, *C2, Div, Rem);
2284 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();
2285
2286 // (1 << Y) pred C -> Y pred Log2(C)
2287 if (!CIsPowerOf2) {
2288 // (1 << Y) < 30 -> Y <= 4
2289 // (1 << Y) <= 30 -> Y <= 4
2290 // (1 << Y) >= 30 -> Y > 4
2291 // (1 << Y) > 30 -> Y > 4
2292 if (Pred == ICmpInst::ICMP_ULT)
2293 Pred = ICmpInst::ICMP_ULE;
2294 else if (Pred == ICmpInst::ICMP_UGE)
2295 Pred = ICmpInst::ICMP_UGT;
2296 }
2297
2298 unsigned CLog2 = Div.logBase2();
2299 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2300 } else if (Cmp.isSigned() && C2->isOne()) {
2301 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
2302 // (1 << Y) > 0 -> Y != 31
2303 // (1 << Y) > C -> Y != 31 if C is negative.
2304 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))
2305 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2306
2307 // (1 << Y) < 0 -> Y == 31
2308 // (1 << Y) < 1 -> Y == 31
2309 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2310 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2311 if (Pred == ICmpInst::ICMP_SLT && (C - 1).sle(0))
2312 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2313 }
2314
2315 return nullptr;
2316}
2317
2318/// Fold icmp (shl X, Y), C.
2320 BinaryOperator *Shl,
2321 const APInt &C) {
2322 const APInt *ShiftVal;
2323 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
2324 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);
2325
2326 ICmpInst::Predicate Pred = Cmp.getPredicate();
2327 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2328 // -> (icmp pred X, Csle0)
2329 //
2330 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2331 // so X's must be what is used.
2332 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2333 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2334
2335 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2336 // -> (icmp eq/ne X, 0)
2337 if (ICmpInst::isEquality(Pred) && C.isZero() &&
2338 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2339 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2340
2341 // (icmp slt (shl nsw X, Y), 0/1)
2342 // -> (icmp slt X, 0/1)
2343 // (icmp sgt (shl nsw X, Y), 0/-1)
2344 // -> (icmp sgt X, 0/-1)
2345 //
2346 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2347 if (Shl->hasNoSignedWrap() &&
2348 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2349 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2350 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2351
2352 const APInt *ShiftAmt;
2353 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2354 return foldICmpShlLHSC(Cmp, Shl, C);
2355
2356 // Check that the shift amount is in range. If not, don't perform undefined
2357 // shifts. When the shift is visited, it will be simplified.
2358 unsigned TypeBits = C.getBitWidth();
2359 if (ShiftAmt->uge(TypeBits))
2360 return nullptr;
2361
2362 Value *X = Shl->getOperand(0);
2363 Type *ShType = Shl->getType();
2364
2365 // NSW guarantees that we are only shifting out sign bits from the high bits,
2366 // so we can ASHR the compare constant without needing a mask and eliminate
2367 // the shift.
2368 if (Shl->hasNoSignedWrap()) {
2369 if (Pred == ICmpInst::ICMP_SGT) {
2370 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2371 APInt ShiftedC = C.ashr(*ShiftAmt);
2372 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2373 }
2374 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2375 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {
2376 APInt ShiftedC = C.ashr(*ShiftAmt);
2377 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2378 }
2379 if (Pred == ICmpInst::ICMP_SLT) {
2380 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2381 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2382 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2383 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2384 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2385 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;
2386 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2387 }
2388 }
2389
2390 // NUW guarantees that we are only shifting out zero bits from the high bits,
2391 // so we can LSHR the compare constant without needing a mask and eliminate
2392 // the shift.
2393 if (Shl->hasNoUnsignedWrap()) {
2394 if (Pred == ICmpInst::ICMP_UGT) {
2395 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2396 APInt ShiftedC = C.lshr(*ShiftAmt);
2397 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2398 }
2399 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2400 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {
2401 APInt ShiftedC = C.lshr(*ShiftAmt);
2402 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2403 }
2404 if (Pred == ICmpInst::ICMP_ULT) {
2405 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2406 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2407 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2408 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2409 assert(C.ugt(0) && "ult 0 should have been eliminated");
2410 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;
2411 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2412 }
2413 }
2414
2415 if (Cmp.isEquality() && Shl->hasOneUse()) {
2416 // Strength-reduce the shift into an 'and'.
2417 Constant *Mask = ConstantInt::get(
2418 ShType,
2419 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
2420 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2421 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));
2422 return new ICmpInst(Pred, And, LShrC);
2423 }
2424
2425 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2426 bool TrueIfSigned = false;
2427 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {
2428 // (X << 31) <s 0 --> (X & 1) != 0
2429 Constant *Mask = ConstantInt::get(
2430 ShType,
2431 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
2432 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2433 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2434 And, Constant::getNullValue(ShType));
2435 }
2436
2437 // Simplify 'shl' inequality test into 'and' equality test.
2438 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2439 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2440 if ((C + 1).isPowerOf2() &&
2441 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2442 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));
2443 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2445 And, Constant::getNullValue(ShType));
2446 }
2447 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2448 if (C.isPowerOf2() &&
2449 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2450 Value *And =
2451 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));
2452 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2454 And, Constant::getNullValue(ShType));
2455 }
2456 }
2457
2458 // Transform (icmp pred iM (shl iM %v, N), C)
2459 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2460 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2461 // This enables us to get rid of the shift in favor of a trunc that may be
2462 // free on the target. It has the additional benefit of comparing to a
2463 // smaller constant that may be more target-friendly.
2464 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
2465 if (Shl->hasOneUse() && Amt != 0 &&
2466 shouldChangeType(ShType->getScalarSizeInBits(), TypeBits - Amt)) {
2467 ICmpInst::Predicate CmpPred = Pred;
2468 APInt RHSC = C;
2469
2470 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(CmpPred)) {
2471 // Try the flipped strictness predicate.
2472 // e.g.:
2473 // icmp ult i64 (shl X, 32), 8589934593 ->
2474 // icmp ule i64 (shl X, 32), 8589934592 ->
2475 // icmp ule i32 (trunc X, i32), 2 ->
2476 // icmp ult i32 (trunc X, i32), 3
2477 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(
2478 Pred, ConstantInt::get(ShType->getContext(), C))) {
2479 CmpPred = FlippedStrictness->first;
2480 RHSC = cast<ConstantInt>(FlippedStrictness->second)->getValue();
2481 }
2482 }
2483
2484 if (RHSC.countr_zero() >= Amt) {
2485 Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt);
2486 Constant *NewC =
2487 ConstantInt::get(TruncTy, RHSC.ashr(*ShiftAmt).trunc(TypeBits - Amt));
2488 return new ICmpInst(CmpPred,
2489 Builder.CreateTrunc(X, TruncTy, "", /*IsNUW=*/false,
2490 Shl->hasNoSignedWrap()),
2491 NewC);
2492 }
2493 }
2494
2495 return nullptr;
2496}
2497
2498/// Fold icmp ({al}shr X, Y), C.
2500 BinaryOperator *Shr,
2501 const APInt &C) {
2502 // An exact shr only shifts out zero bits, so:
2503 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2504 Value *X = Shr->getOperand(0);
2505 CmpInst::Predicate Pred = Cmp.getPredicate();
2506 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2507 return new ICmpInst(Pred, X, Cmp.getOperand(1));
2508
2509 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2510 const APInt *ShiftValC;
2511 if (match(X, m_APInt(ShiftValC))) {
2512 if (Cmp.isEquality())
2513 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
2514
2515 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2516 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2517 bool TrueIfSigned;
2518 if (!IsAShr && ShiftValC->isNegative() &&
2519 isSignBitCheck(Pred, C, TrueIfSigned))
2520 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2521 Shr->getOperand(1),
2522 ConstantInt::getNullValue(X->getType()));
2523
2524 // If the shifted constant is a power-of-2, test the shift amount directly:
2525 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2526 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2527 if (!IsAShr && ShiftValC->isPowerOf2() &&
2528 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2529 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2530 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2531 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2532
2533 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2534 unsigned ShiftLZ = ShiftValC->countl_zero();
2535 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
2536 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2537 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
2538 }
2539 }
2540
2541 const APInt *ShiftAmtC;
2542 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))
2543 return nullptr;
2544
2545 // Check that the shift amount is in range. If not, don't perform undefined
2546 // shifts. When the shift is visited it will be simplified.
2547 unsigned TypeBits = C.getBitWidth();
2548 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);
2549 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2550 return nullptr;
2551
2552 bool IsExact = Shr->isExact();
2553 Type *ShrTy = Shr->getType();
2554 // TODO: If we could guarantee that InstSimplify would handle all of the
2555 // constant-value-based preconditions in the folds below, then we could assert
2556 // those conditions rather than checking them. This is difficult because of
2557 // undef/poison (PR34838).
2558 if (IsAShr && Shr->hasOneUse()) {
2559 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2560 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2561 // When C - 1 is a power of two and the transform can be legally
2562 // performed, prefer this form so the produced constant is close to a
2563 // power of two.
2564 // icmp slt/ult (ashr exact X, ShAmtC), C
2565 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2566 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
2567 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2568 }
2569 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2570 // When ShAmtC can be shifted losslessly:
2571 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2572 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2573 APInt ShiftedC = C.shl(ShAmtVal);
2574 if (ShiftedC.ashr(ShAmtVal) == C)
2575 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2576 }
2577 if (Pred == CmpInst::ICMP_SGT) {
2578 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2579 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2580 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&
2581 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
2582 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2583 }
2584 if (Pred == CmpInst::ICMP_UGT) {
2585 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2586 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2587 // clause accounts for that pattern.
2588 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2589 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
2590 (C + 1).shl(ShAmtVal).isMinSignedValue())
2591 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2592 }
2593
2594 // If the compare constant has significant bits above the lowest sign-bit,
2595 // then convert an unsigned cmp to a test of the sign-bit:
2596 // (ashr X, ShiftC) u> C --> X s< 0
2597 // (ashr X, ShiftC) u< C --> X s> -1
2598 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2599 if (Pred == CmpInst::ICMP_UGT) {
2600 return new ICmpInst(CmpInst::ICMP_SLT, X,
2602 }
2603 if (Pred == CmpInst::ICMP_ULT) {
2604 return new ICmpInst(CmpInst::ICMP_SGT, X,
2606 }
2607 }
2608 } else if (!IsAShr) {
2609 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2610 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2611 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2612 APInt ShiftedC = C.shl(ShAmtVal);
2613 if (ShiftedC.lshr(ShAmtVal) == C)
2614 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2615 }
2616 if (Pred == CmpInst::ICMP_UGT) {
2617 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2618 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2619 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))
2620 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2621 }
2622 }
2623
2624 if (!Cmp.isEquality())
2625 return nullptr;
2626
2627 // Handle equality comparisons of shift-by-constant.
2628
2629 // If the comparison constant changes with the shift, the comparison cannot
2630 // succeed (bits of the comparison constant cannot match the shifted value).
2631 // This should be known by InstSimplify and already be folded to true/false.
2632 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2633 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2634 "Expected icmp+shr simplify did not occur.");
2635
2636 // If the bits shifted out are known zero, compare the unshifted value:
2637 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2638 if (Shr->isExact())
2639 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));
2640
2641 if (Shr->hasOneUse()) {
2642 // Canonicalize the shift into an 'and':
2643 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2644 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2645 Constant *Mask = ConstantInt::get(ShrTy, Val);
2646 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");
2647 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));
2648 }
2649
2650 return nullptr;
2651}
2652
2654 BinaryOperator *SRem,
2655 const APInt &C) {
2656 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2657 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
2658 // Canonicalize unsigned predicates to signed:
2659 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 0
2660 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1
2661 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1
2662 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1
2663
2664 const APInt *DivisorC;
2665 if (!match(SRem->getOperand(1), m_APInt(DivisorC)))
2666 return nullptr;
2667
2668 APInt NormalizedC = C;
2669 if (Pred == ICmpInst::ICMP_ULT) {
2670 assert(!NormalizedC.isZero() &&
2671 "ult X, 0 should have been simplified already.");
2672 --NormalizedC;
2673 }
2674 if (C.isNegative())
2675 NormalizedC.flipAllBits();
2676 assert(!DivisorC->isZero() &&
2677 "srem X, 0 should have been simplified already.");
2678 if (!NormalizedC.uge(DivisorC->abs() - 1))
2679 return nullptr;
2680
2681 Type *Ty = SRem->getType();
2682 if (Pred == ICmpInst::ICMP_UGT)
2683 return new ICmpInst(ICmpInst::ICMP_SLT, SRem,
2685 return new ICmpInst(ICmpInst::ICMP_SGT, SRem,
2687 }
2688 // Match an 'is positive' or 'is negative' comparison of remainder by a
2689 // constant power-of-2 value:
2690 // (X % pow2C) sgt/slt 0
2691 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2692 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2693 return nullptr;
2694
2695 // TODO: The one-use check is standard because we do not typically want to
2696 // create longer instruction sequences, but this might be a special-case
2697 // because srem is not good for analysis or codegen.
2698 if (!SRem->hasOneUse())
2699 return nullptr;
2700
2701 const APInt *DivisorC;
2702 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
2703 return nullptr;
2704
2705 // For cmp_sgt/cmp_slt only zero valued C is handled.
2706 // For cmp_eq/cmp_ne only positive valued C is handled.
2707 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2708 !C.isZero()) ||
2709 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2710 !C.isStrictlyPositive()))
2711 return nullptr;
2712
2713 // Mask off the sign bit and the modulo bits (low-bits).
2714 Type *Ty = SRem->getType();
2715 APInt SignMask = APInt::getSignMask(Ty->getScalarSizeInBits());
2716 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
2717 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);
2718
2719 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2720 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));
2721
2722 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2723 // bit is set. Example:
2724 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2725 if (Pred == ICmpInst::ICMP_SGT)
2727
2728 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2729 // bit is set. Example:
2730 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2731 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
2732}
2733
2734/// Fold icmp (udiv X, Y), C.
2736 BinaryOperator *UDiv,
2737 const APInt &C) {
2738 ICmpInst::Predicate Pred = Cmp.getPredicate();
2739 Value *X = UDiv->getOperand(0);
2740 Value *Y = UDiv->getOperand(1);
2741 Type *Ty = UDiv->getType();
2742
2743 const APInt *C2;
2744 if (!match(X, m_APInt(C2)))
2745 return nullptr;
2746
2747 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2748
2749 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2750 if (Pred == ICmpInst::ICMP_UGT) {
2751 assert(!C.isMaxValue() &&
2752 "icmp ugt X, UINT_MAX should have been simplified already.");
2753 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2754 ConstantInt::get(Ty, C2->udiv(C + 1)));
2755 }
2756
2757 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2758 if (Pred == ICmpInst::ICMP_ULT) {
2759 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2760 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2761 ConstantInt::get(Ty, C2->udiv(C)));
2762 }
2763
2764 return nullptr;
2765}
2766
2767/// Fold icmp ({su}div X, Y), C.
2769 BinaryOperator *Div,
2770 const APInt &C) {
2771 ICmpInst::Predicate Pred = Cmp.getPredicate();
2772 Value *X = Div->getOperand(0);
2773 Value *Y = Div->getOperand(1);
2774 Type *Ty = Div->getType();
2775 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2776
2777 // If unsigned division and the compare constant is bigger than
2778 // UMAX/2 (negative), there's only one pair of values that satisfies an
2779 // equality check, so eliminate the division:
2780 // (X u/ Y) == C --> (X == C) && (Y == 1)
2781 // (X u/ Y) != C --> (X != C) || (Y != 1)
2782 // Similarly, if signed division and the compare constant is exactly SMIN:
2783 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2784 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2785 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2786 (!DivIsSigned || C.isMinSignedValue())) {
2787 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));
2788 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));
2789 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2790 return BinaryOperator::Create(Logic, XBig, YOne);
2791 }
2792
2793 // Fold: icmp pred ([us]div X, C2), C -> range test
2794 // Fold this div into the comparison, producing a range check.
2795 // Determine, based on the divide type, what the range is being
2796 // checked. If there is an overflow on the low or high side, remember
2797 // it, otherwise compute the range [low, hi) bounding the new value.
2798 // See: InsertRangeTest above for the kinds of replacements possible.
2799 const APInt *C2;
2800 if (!match(Y, m_APInt(C2)))
2801 return nullptr;
2802
2803 // FIXME: If the operand types don't match the type of the divide
2804 // then don't attempt this transform. The code below doesn't have the
2805 // logic to deal with a signed divide and an unsigned compare (and
2806 // vice versa). This is because (x /s C2) <s C produces different
2807 // results than (x /s C2) <u C or (x /u C2) <s C or even
2808 // (x /u C2) <u C. Simply casting the operands and result won't
2809 // work. :( The if statement below tests that condition and bails
2810 // if it finds it.
2811 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
2812 return nullptr;
2813
2814 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2815 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2816 // division-by-constant cases should be present, we can not assert that they
2817 // have happened before we reach this icmp instruction.
2818 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2819 return nullptr;
2820
2821 // Compute Prod = C * C2. We are essentially solving an equation of
2822 // form X / C2 = C. We solve for X by multiplying C2 and C.
2823 // By solving for X, we can turn this into a range check instead of computing
2824 // a divide.
2825 APInt Prod = C * *C2;
2826
2827 // Determine if the product overflows by seeing if the product is not equal to
2828 // the divide. Make sure we do the same kind of divide as in the LHS
2829 // instruction that we're folding.
2830 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;
2831
2832 // If the division is known to be exact, then there is no remainder from the
2833 // divide, so the covered range size is unit, otherwise it is the divisor.
2834 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2835
2836 // Figure out the interval that is being checked. For example, a comparison
2837 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2838 // Compute this interval based on the constants involved and the signedness of
2839 // the compare/divide. This computes a half-open interval, keeping track of
2840 // whether either value in the interval overflows. After analysis each
2841 // overflow variable is set to 0 if it's corresponding bound variable is valid
2842 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2843 int LoOverflow = 0, HiOverflow = 0;
2844 APInt LoBound, HiBound;
2845
2846 if (!DivIsSigned) { // udiv
2847 // e.g. X/5 op 3 --> [15, 20)
2848 LoBound = Prod;
2849 HiOverflow = LoOverflow = ProdOV;
2850 if (!HiOverflow) {
2851 // If this is not an exact divide, then many values in the range collapse
2852 // to the same result value.
2853 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2854 }
2855 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2856 if (C.isZero()) { // (X / pos) op 0
2857 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2858 LoBound = -(RangeSize - 1);
2859 HiBound = RangeSize;
2860 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2861 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2862 HiOverflow = LoOverflow = ProdOV;
2863 if (!HiOverflow)
2864 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2865 } else { // (X / pos) op neg
2866 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2867 HiBound = Prod + 1;
2868 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2869 if (!LoOverflow) {
2870 APInt DivNeg = -RangeSize;
2871 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2872 }
2873 }
2874 } else if (C2->isNegative()) { // Divisor is < 0.
2875 if (Div->isExact())
2876 RangeSize.negate();
2877 if (C.isZero()) { // (X / neg) op 0
2878 // e.g. X/-5 op 0 --> [-4, 5)
2879 LoBound = RangeSize + 1;
2880 HiBound = -RangeSize;
2881 if (HiBound == *C2) { // -INTMIN = INTMIN
2882 HiOverflow = 1; // [INTMIN+1, overflow)
2883 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2884 }
2885 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2886 // e.g. X/-5 op 3 --> [-19, -14)
2887 HiBound = Prod + 1;
2888 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2889 if (!LoOverflow)
2890 LoOverflow =
2891 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;
2892 } else { // (X / neg) op neg
2893 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2894 LoOverflow = HiOverflow = ProdOV;
2895 if (!HiOverflow)
2896 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2897 }
2898
2899 // Dividing by a negative swaps the condition. LT <-> GT
2900 Pred = ICmpInst::getSwappedPredicate(Pred);
2901 }
2902
2903 switch (Pred) {
2904 default:
2905 llvm_unreachable("Unhandled icmp predicate!");
2906 case ICmpInst::ICMP_EQ:
2907 if (LoOverflow && HiOverflow)
2908 return replaceInstUsesWith(Cmp, Builder.getFalse());
2909 if (HiOverflow)
2910 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2911 X, ConstantInt::get(Ty, LoBound));
2912 if (LoOverflow)
2913 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2914 X, ConstantInt::get(Ty, HiBound));
2915 return replaceInstUsesWith(
2916 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));
2917 case ICmpInst::ICMP_NE:
2918 if (LoOverflow && HiOverflow)
2919 return replaceInstUsesWith(Cmp, Builder.getTrue());
2920 if (HiOverflow)
2921 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2922 X, ConstantInt::get(Ty, LoBound));
2923 if (LoOverflow)
2924 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2925 X, ConstantInt::get(Ty, HiBound));
2926 return replaceInstUsesWith(
2927 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));
2928 case ICmpInst::ICMP_ULT:
2929 case ICmpInst::ICMP_SLT:
2930 if (LoOverflow == +1) // Low bound is greater than input range.
2931 return replaceInstUsesWith(Cmp, Builder.getTrue());
2932 if (LoOverflow == -1) // Low bound is less than input range.
2933 return replaceInstUsesWith(Cmp, Builder.getFalse());
2934 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));
2935 case ICmpInst::ICMP_UGT:
2936 case ICmpInst::ICMP_SGT:
2937 if (HiOverflow == +1) // High bound greater than input range.
2938 return replaceInstUsesWith(Cmp, Builder.getFalse());
2939 if (HiOverflow == -1) // High bound less than input range.
2940 return replaceInstUsesWith(Cmp, Builder.getTrue());
2941 if (Pred == ICmpInst::ICMP_UGT)
2942 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));
2943 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));
2944 }
2945
2946 return nullptr;
2947}
2948
2949/// Fold icmp (sub X, Y), C.
2952 const APInt &C) {
2953 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2954 ICmpInst::Predicate Pred = Cmp.getPredicate();
2955 Type *Ty = Sub->getType();
2956
2957 // (SubC - Y) == C) --> Y == (SubC - C)
2958 // (SubC - Y) != C) --> Y != (SubC - C)
2959 Constant *SubC;
2960 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {
2961 return new ICmpInst(Pred, Y,
2962 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));
2963 }
2964
2965 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
2966 const APInt *C2;
2967 APInt SubResult;
2968 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
2969 bool HasNSW = Sub->hasNoSignedWrap();
2970 bool HasNUW = Sub->hasNoUnsignedWrap();
2971 if (match(X, m_APInt(C2)) &&
2972 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
2973 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))
2974 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));
2975
2976 // X - Y == 0 --> X == Y.
2977 // X - Y != 0 --> X != Y.
2978 // TODO: We allow this with multiple uses as long as the other uses are not
2979 // in phis. The phi use check is guarding against a codegen regression
2980 // for a loop test. If the backend could undo this (and possibly
2981 // subsequent transforms), we would not need this hack.
2982 if (Cmp.isEquality() && C.isZero() &&
2983 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))
2984 return new ICmpInst(Pred, X, Y);
2985
2986 // The following transforms are only worth it if the only user of the subtract
2987 // is the icmp.
2988 // TODO: This is an artificial restriction for all of the transforms below
2989 // that only need a single replacement icmp. Can these use the phi test
2990 // like the transform above here?
2991 if (!Sub->hasOneUse())
2992 return nullptr;
2993
2994 if (Sub->hasNoSignedWrap()) {
2995 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
2996 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
2997 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
2998
2999 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
3000 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
3001 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3002
3003 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
3004 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
3005 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
3006
3007 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
3008 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3009 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3010 }
3011
3012 if (!match(X, m_APInt(C2)))
3013 return nullptr;
3014
3015 // C2 - Y <u C -> (Y | (C - 1)) == C2
3016 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
3017 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
3018 (*C2 & (C - 1)) == (C - 1))
3019 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);
3020
3021 // C2 - Y >u C -> (Y | C) != C2
3022 // iff C2 & C == C and C + 1 is a power of 2
3023 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
3024 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);
3025
3026 // We have handled special cases that reduce.
3027 // Canonicalize any remaining sub to add as:
3028 // (C2 - Y) > C --> (Y + ~C2) < ~C
3029 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",
3030 HasNUW, HasNSW);
3031 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
3032}
3033
3034static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
3035 Value *Op1, IRBuilderBase &Builder,
3036 bool HasOneUse) {
3037 auto FoldConstant = [&](bool Val) {
3038 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
3039 if (Op0->getType()->isVectorTy())
3041 cast<VectorType>(Op0->getType())->getElementCount(), Res);
3042 return Res;
3043 };
3044
3045 switch (Table.to_ulong()) {
3046 case 0: // 0 0 0 0
3047 return FoldConstant(false);
3048 case 1: // 0 0 0 1
3049 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
3050 case 2: // 0 0 1 0
3051 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
3052 case 3: // 0 0 1 1
3053 return Builder.CreateNot(Op0);
3054 case 4: // 0 1 0 0
3055 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
3056 case 5: // 0 1 0 1
3057 return Builder.CreateNot(Op1);
3058 case 6: // 0 1 1 0
3059 return Builder.CreateXor(Op0, Op1);
3060 case 7: // 0 1 1 1
3061 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
3062 case 8: // 1 0 0 0
3063 return Builder.CreateAnd(Op0, Op1);
3064 case 9: // 1 0 0 1
3065 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
3066 case 10: // 1 0 1 0
3067 return Op1;
3068 case 11: // 1 0 1 1
3069 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
3070 case 12: // 1 1 0 0
3071 return Op0;
3072 case 13: // 1 1 0 1
3073 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
3074 case 14: // 1 1 1 0
3075 return Builder.CreateOr(Op0, Op1);
3076 case 15: // 1 1 1 1
3077 return FoldConstant(true);
3078 default:
3079 llvm_unreachable("Invalid Operation");
3080 }
3081 return nullptr;
3082}
3083
3085 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3086 Value *A, *B;
3087 Constant *C1, *C2, *C3, *C4;
3088 if (!(match(BO->getOperand(0),
3089 m_Select(m_Value(A), m_Constant(C1), m_Constant(C2)))) ||
3090 !match(BO->getOperand(1),
3091 m_Select(m_Value(B), m_Constant(C3), m_Constant(C4))) ||
3092 Cmp.getType() != A->getType())
3093 return nullptr;
3094
3095 std::bitset<4> Table;
3096 auto ComputeTable = [&](bool First, bool Second) -> std::optional<bool> {
3097 Constant *L = First ? C1 : C2;
3098 Constant *R = Second ? C3 : C4;
3099 if (auto *Res = ConstantFoldBinaryOpOperands(BO->getOpcode(), L, R, DL)) {
3100 auto *Val = Res->getType()->isVectorTy() ? Res->getSplatValue() : Res;
3101 if (auto *CI = dyn_cast_or_null<ConstantInt>(Val))
3102 return ICmpInst::compare(CI->getValue(), C, Cmp.getPredicate());
3103 }
3104 return std::nullopt;
3105 };
3106
3107 for (unsigned I = 0; I < 4; ++I) {
3108 bool First = (I >> 1) & 1;
3109 bool Second = I & 1;
3110 if (auto Res = ComputeTable(First, Second))
3111 Table[I] = *Res;
3112 else
3113 return nullptr;
3114 }
3115
3116 // Synthesize optimal logic.
3117 if (auto *Cond = createLogicFromTable(Table, A, B, Builder, BO->hasOneUse()))
3118 return replaceInstUsesWith(Cmp, Cond);
3119 return nullptr;
3120}
3121
3122/// Fold icmp (add X, Y), C.
3125 const APInt &C) {
3126 Value *Y = Add->getOperand(1);
3127 Value *X = Add->getOperand(0);
3128
3129 Value *Op0, *Op1;
3130 Instruction *Ext0, *Ext1;
3131 const CmpInst::Predicate Pred = Cmp.getPredicate();
3132 if (match(Add,
3135 m_ZExtOrSExt(m_Value(Op1))))) &&
3136 Op0->getType()->isIntOrIntVectorTy(1) &&
3137 Op1->getType()->isIntOrIntVectorTy(1)) {
3138 unsigned BW = C.getBitWidth();
3139 std::bitset<4> Table;
3140 auto ComputeTable = [&](bool Op0Val, bool Op1Val) {
3141 APInt Res(BW, 0);
3142 if (Op0Val)
3143 Res += APInt(BW, isa<ZExtInst>(Ext0) ? 1 : -1, /*isSigned=*/true);
3144 if (Op1Val)
3145 Res += APInt(BW, isa<ZExtInst>(Ext1) ? 1 : -1, /*isSigned=*/true);
3146 return ICmpInst::compare(Res, C, Pred);
3147 };
3148
3149 Table[0] = ComputeTable(false, false);
3150 Table[1] = ComputeTable(false, true);
3151 Table[2] = ComputeTable(true, false);
3152 Table[3] = ComputeTable(true, true);
3153 if (auto *Cond =
3154 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))
3155 return replaceInstUsesWith(Cmp, Cond);
3156 }
3157 const APInt *C2;
3158 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3159 return nullptr;
3160
3161 // Fold icmp pred (add X, C2), C.
3162 Type *Ty = Add->getType();
3163
3164 // If the add does not wrap, we can always adjust the compare by subtracting
3165 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3166 // are canonicalized to SGT/SLT/UGT/ULT.
3167 if ((Add->hasNoSignedWrap() &&
3168 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
3169 (Add->hasNoUnsignedWrap() &&
3170 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
3171 bool Overflow;
3172 APInt NewC =
3173 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);
3174 // If there is overflow, the result must be true or false.
3175 // TODO: Can we assert there is no overflow because InstSimplify always
3176 // handles those cases?
3177 if (!Overflow)
3178 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3179 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
3180 }
3181
3182 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3183 C.isNonNegative() && (C - *C2).isNonNegative() &&
3184 computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())
3185 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
3186 ConstantInt::get(Ty, C - *C2));
3187
3188 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
3189 const APInt &Upper = CR.getUpper();
3190 const APInt &Lower = CR.getLower();
3191 if (Cmp.isSigned()) {
3192 if (Lower.isSignMask())
3193 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
3194 if (Upper.isSignMask())
3195 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
3196 } else {
3197 if (Lower.isMinValue())
3198 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
3199 if (Upper.isMinValue())
3200 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
3201 }
3202
3203 // This set of folds is intentionally placed after folds that use no-wrapping
3204 // flags because those folds are likely better for later analysis/codegen.
3205 const APInt SMax = APInt::getSignedMaxValue(Ty->getScalarSizeInBits());
3206 const APInt SMin = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
3207
3208 // Fold compare with offset to opposite sign compare if it eliminates offset:
3209 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3210 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3211 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));
3212
3213 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3214 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3215 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));
3216
3217 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3218 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3219 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));
3220
3221 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3222 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3223 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));
3224
3225 // (X + -1) <u C --> X <=u C (if X is never null)
3226 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3227 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3228 if (llvm::isKnownNonZero(X, Q))
3229 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));
3230 }
3231
3232 if (!Add->hasOneUse())
3233 return nullptr;
3234
3235 // X+C <u C2 -> (X & -C2) == C
3236 // iff C & (C2-1) == 0
3237 // C2 is a power of 2
3238 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3239 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(X, -C),
3241
3242 // X+C2 <u C -> (X & C) == 2C
3243 // iff C == -(C2)
3244 // C2 is a power of 2
3245 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3246 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, C),
3247 ConstantInt::get(Ty, C * 2));
3248
3249 // X+C >u C2 -> (X & ~C2) != C
3250 // iff C & C2 == 0
3251 // C2+1 is a power of 2
3252 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3253 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, ~C),
3255
3256 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3257 // to the ult form.
3258 // X+C2 >u C -> X+(C2-C-1) <u ~C
3259 if (Pred == ICmpInst::ICMP_UGT)
3260 return new ICmpInst(ICmpInst::ICMP_ULT,
3261 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
3262 ConstantInt::get(Ty, ~C));
3263
3264 // zext(V) + C2 pred C -> V + C3 pred' C4
3265 Value *V;
3266 if (match(X, m_ZExt(m_Value(V)))) {
3267 Type *NewCmpTy = V->getType();
3268 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();
3269 if (shouldChangeType(Ty, NewCmpTy)) {
3270 ConstantRange SrcCR = CR.truncate(NewCmpBW, TruncInst::NoUnsignedWrap);
3271 CmpInst::Predicate EquivPred;
3272 APInt EquivInt;
3273 APInt EquivOffset;
3274
3275 SrcCR.getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
3276 return new ICmpInst(
3277 EquivPred,
3278 EquivOffset.isZero()
3279 ? V
3280 : Builder.CreateAdd(V, ConstantInt::get(NewCmpTy, EquivOffset)),
3281 ConstantInt::get(NewCmpTy, EquivInt));
3282 }
3283 }
3284
3285 return nullptr;
3286}
3287
3289 Value *&RHS, ConstantInt *&Less,
3290 ConstantInt *&Equal,
3291 ConstantInt *&Greater) {
3292 // TODO: Generalize this to work with other comparison idioms or ensure
3293 // they get canonicalized into this form.
3294
3295 // select i1 (a == b),
3296 // i32 Equal,
3297 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3298 // where Equal, Less and Greater are placeholders for any three constants.
3299 CmpPredicate PredA;
3300 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||
3301 !ICmpInst::isEquality(PredA))
3302 return false;
3303 Value *EqualVal = SI->getTrueValue();
3304 Value *UnequalVal = SI->getFalseValue();
3305 // We still can get non-canonical predicate here, so canonicalize.
3306 if (PredA == ICmpInst::ICMP_NE)
3307 std::swap(EqualVal, UnequalVal);
3308 if (!match(EqualVal, m_ConstantInt(Equal)))
3309 return false;
3310 CmpPredicate PredB;
3311 Value *LHS2, *RHS2;
3312 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),
3313 m_ConstantInt(Less), m_ConstantInt(Greater))))
3314 return false;
3315 // We can get predicate mismatch here, so canonicalize if possible:
3316 // First, ensure that 'LHS' match.
3317 if (LHS2 != LHS) {
3318 // x sgt y <--> y slt x
3319 std::swap(LHS2, RHS2);
3320 PredB = ICmpInst::getSwappedPredicate(PredB);
3321 }
3322 if (LHS2 != LHS)
3323 return false;
3324 // We also need to canonicalize 'RHS'.
3325 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
3326 // x sgt C-1 <--> x sge C <--> not(x slt C)
3327 auto FlippedStrictness =
3329 if (!FlippedStrictness)
3330 return false;
3331 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3332 "basic correctness failure");
3333 RHS2 = FlippedStrictness->second;
3334 // And kind-of perform the result swap.
3335 std::swap(Less, Greater);
3336 PredB = ICmpInst::ICMP_SLT;
3337 }
3338 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3339}
3340
3343 ConstantInt *C) {
3344
3345 assert(C && "Cmp RHS should be a constant int!");
3346 // If we're testing a constant value against the result of a three way
3347 // comparison, the result can be expressed directly in terms of the
3348 // original values being compared. Note: We could possibly be more
3349 // aggressive here and remove the hasOneUse test. The original select is
3350 // really likely to simplify or sink when we remove a test of the result.
3351 Value *OrigLHS, *OrigRHS;
3352 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3353 if (Cmp.hasOneUse() &&
3354 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,
3355 C3GreaterThan)) {
3356 assert(C1LessThan && C2Equal && C3GreaterThan);
3357
3358 bool TrueWhenLessThan = ICmpInst::compare(
3359 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate());
3360 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(),
3361 Cmp.getPredicate());
3362 bool TrueWhenGreaterThan = ICmpInst::compare(
3363 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate());
3364
3365 // This generates the new instruction that will replace the original Cmp
3366 // Instruction. Instead of enumerating the various combinations when
3367 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3368 // false, we rely on chaining of ORs and future passes of InstCombine to
3369 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3370
3371 // When none of the three constants satisfy the predicate for the RHS (C),
3372 // the entire original Cmp can be simplified to a false.
3373 Value *Cond = Builder.getFalse();
3374 if (TrueWhenLessThan)
3375 Cond = Builder.CreateOr(
3376 Cond, Builder.CreateICmp(ICmpInst::ICMP_SLT, OrigLHS, OrigRHS));
3377 if (TrueWhenEqual)
3378 Cond = Builder.CreateOr(
3379 Cond, Builder.CreateICmp(ICmpInst::ICMP_EQ, OrigLHS, OrigRHS));
3380 if (TrueWhenGreaterThan)
3381 Cond = Builder.CreateOr(
3382 Cond, Builder.CreateICmp(ICmpInst::ICMP_SGT, OrigLHS, OrigRHS));
3383
3384 return replaceInstUsesWith(Cmp, Cond);
3385 }
3386 return nullptr;
3387}
3388
3390 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));
3391 if (!Bitcast)
3392 return nullptr;
3393
3394 ICmpInst::Predicate Pred = Cmp.getPredicate();
3395 Value *Op1 = Cmp.getOperand(1);
3396 Value *BCSrcOp = Bitcast->getOperand(0);
3397 Type *SrcType = Bitcast->getSrcTy();
3398 Type *DstType = Bitcast->getType();
3399
3400 // Make sure the bitcast doesn't change between scalar and vector and
3401 // doesn't change the number of vector elements.
3402 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3403 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3404 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3405 Value *X;
3406 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {
3407 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3408 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3409 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3410 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3411 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3412 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3413 match(Op1, m_Zero()))
3414 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3415
3416 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3417 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))
3418 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));
3419
3420 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3421 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))
3422 return new ICmpInst(Pred, X,
3423 ConstantInt::getAllOnesValue(X->getType()));
3424 }
3425
3426 // Zero-equality checks are preserved through unsigned floating-point casts:
3427 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3428 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3429 if (match(BCSrcOp, m_UIToFP(m_Value(X))))
3430 if (Cmp.isEquality() && match(Op1, m_Zero()))
3431 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3432
3433 const APInt *C;
3434 bool TrueIfSigned;
3435 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {
3436 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3437 // the FP extend/truncate because that cast does not change the sign-bit.
3438 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3439 // The sign-bit is always the most significant bit in those types.
3440 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&
3441 (match(BCSrcOp, m_FPExt(m_Value(X))) ||
3442 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {
3443 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3444 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3445 Type *XType = X->getType();
3446
3447 // We can't currently handle Power style floating point operations here.
3448 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3449 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());
3450 if (auto *XVTy = dyn_cast<VectorType>(XType))
3451 NewType = VectorType::get(NewType, XVTy->getElementCount());
3452 Value *NewBitcast = Builder.CreateBitCast(X, NewType);
3453 if (TrueIfSigned)
3454 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3455 ConstantInt::getNullValue(NewType));
3456 else
3457 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3459 }
3460 }
3461
3462 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3463 Type *FPType = SrcType->getScalarType();
3464 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3465 Attribute::NoImplicitFloat) &&
3466 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3467 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3468 if (Mask & (fcInf | fcZero)) {
3469 if (Pred == ICmpInst::ICMP_NE)
3470 Mask = ~Mask;
3471 return replaceInstUsesWith(Cmp,
3472 Builder.createIsFPClass(BCSrcOp, Mask));
3473 }
3474 }
3475 }
3476 }
3477
3478 const APInt *C;
3479 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||
3480 !SrcType->isIntOrIntVectorTy())
3481 return nullptr;
3482
3483 // If this is checking if all elements of a vector compare are set or not,
3484 // invert the casted vector equality compare and test if all compare
3485 // elements are clear or not. Compare against zero is generally easier for
3486 // analysis and codegen.
3487 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3488 // Example: are all elements equal? --> are zero elements not equal?
3489 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3490 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3491 if (Value *NotBCSrcOp =
3492 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {
3493 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);
3494 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));
3495 }
3496 }
3497
3498 // If this is checking if all elements of an extended vector are clear or not,
3499 // compare in a narrow type to eliminate the extend:
3500 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3501 Value *X;
3502 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3503 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {
3504 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {
3505 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());
3506 Value *NewCast = Builder.CreateBitCast(X, NewType);
3507 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));
3508 }
3509 }
3510
3511 // Folding: icmp <pred> iN X, C
3512 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3513 // and C is a splat of a K-bit pattern
3514 // and SC is a constant vector = <C', C', C', ..., C'>
3515 // Into:
3516 // %E = extractelement <M x iK> %vec, i32 C'
3517 // icmp <pred> iK %E, trunc(C)
3518 Value *Vec;
3519 ArrayRef<int> Mask;
3520 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {
3521 // Check whether every element of Mask is the same constant
3522 if (all_equal(Mask)) {
3523 auto *VecTy = cast<VectorType>(SrcType);
3524 auto *EltTy = cast<IntegerType>(VecTy->getElementType());
3525 if (C->isSplat(EltTy->getBitWidth())) {
3526 // Fold the icmp based on the value of C
3527 // If C is M copies of an iK sized bit pattern,
3528 // then:
3529 // => %E = extractelement <N x iK> %vec, i32 Elem
3530 // icmp <pred> iK %SplatVal, <pattern>
3531 Value *Elem = Builder.getInt32(Mask[0]);
3532 Value *Extract = Builder.CreateExtractElement(Vec, Elem);
3533 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));
3534 return new ICmpInst(Pred, Extract, NewC);
3535 }
3536 }
3537 }
3538 return nullptr;
3539}
3540
3541/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3542/// where X is some kind of instruction.
3544 const APInt *C;
3545
3546 if (match(Cmp.getOperand(1), m_APInt(C))) {
3547 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))
3548 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))
3549 return I;
3550
3551 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))
3552 // For now, we only support constant integers while folding the
3553 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3554 // similar to the cases handled by binary ops above.
3555 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))
3556 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))
3557 return I;
3558
3559 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))
3560 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))
3561 return I;
3562
3563 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))
3565 return I;
3566
3567 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3568 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3569 // TODO: This checks one-use, but that is not strictly necessary.
3570 Value *Cmp0 = Cmp.getOperand(0);
3571 Value *X, *Y;
3572 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3573 (match(Cmp0,
3575 m_Value(X), m_Value(Y)))) ||
3576 match(Cmp0,
3578 m_Value(X), m_Value(Y))))))
3579 return new ICmpInst(Cmp.getPredicate(), X, Y);
3580 }
3581
3582 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))
3584
3585 return nullptr;
3586}
3587
3588/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3589/// icmp eq/ne BO, C.
3591 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3592 // TODO: Some of these folds could work with arbitrary constants, but this
3593 // function is limited to scalar and vector splat constants.
3594 if (!Cmp.isEquality())
3595 return nullptr;
3596
3597 ICmpInst::Predicate Pred = Cmp.getPredicate();
3598 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3599 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
3600 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
3601
3602 switch (BO->getOpcode()) {
3603 case Instruction::SRem:
3604 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3605 if (C.isZero() && BO->hasOneUse()) {
3606 const APInt *BOC;
3607 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
3608 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());
3609 return new ICmpInst(Pred, NewRem,
3611 }
3612 }
3613 break;
3614 case Instruction::Add: {
3615 // (A + C2) == C --> A == (C - C2)
3616 // (A + C2) != C --> A != (C - C2)
3617 // TODO: Remove the one-use limitation? See discussion in D58633.
3618 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {
3619 if (BO->hasOneUse())
3620 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));
3621 } else if (C.isZero()) {
3622 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3623 // efficiently invertible, or if the add has just this one use.
3624 if (Value *NegVal = dyn_castNegVal(BOp1))
3625 return new ICmpInst(Pred, BOp0, NegVal);
3626 if (Value *NegVal = dyn_castNegVal(BOp0))
3627 return new ICmpInst(Pred, NegVal, BOp1);
3628 if (BO->hasOneUse()) {
3629 // (add nuw A, B) != 0 -> (or A, B) != 0
3630 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
3631 Value *Or = Builder.CreateOr(BOp0, BOp1);
3632 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
3633 }
3634 Value *Neg = Builder.CreateNeg(BOp1);
3635 Neg->takeName(BO);
3636 return new ICmpInst(Pred, BOp0, Neg);
3637 }
3638 }
3639 break;
3640 }
3641 case Instruction::Xor:
3642 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
3643 // For the xor case, we can xor two constants together, eliminating
3644 // the explicit xor.
3645 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
3646 } else if (C.isZero()) {
3647 // Replace ((xor A, B) != 0) with (A != B)
3648 return new ICmpInst(Pred, BOp0, BOp1);
3649 }
3650 break;
3651 case Instruction::Or: {
3652 const APInt *BOC;
3653 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3654 // Comparing if all bits outside of a constant mask are set?
3655 // Replace (X | C) == -1 with (X & ~C) == ~C.
3656 // This removes the -1 constant.
3658 Value *And = Builder.CreateAnd(BOp0, NotBOC);
3659 return new ICmpInst(Pred, And, NotBOC);
3660 }
3661 // (icmp eq (or (select cond, 0, NonZero), Other), 0)
3662 // -> (and cond, (icmp eq Other, 0))
3663 // (icmp ne (or (select cond, NonZero, 0), Other), 0)
3664 // -> (or cond, (icmp ne Other, 0))
3665 Value *Cond, *TV, *FV, *Other, *Sel;
3666 if (C.isZero() &&
3667 match(BO,
3670 m_Value(FV))),
3671 m_Value(Other)))) &&
3672 Cond->getType() == Cmp.getType()) {
3673 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3674 // Easy case is if eq/ne matches whether 0 is trueval/falseval.
3675 if (Pred == ICmpInst::ICMP_EQ
3676 ? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))
3677 : (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {
3678 Value *Cmp = Builder.CreateICmp(
3679 Pred, Other, Constant::getNullValue(Other->getType()));
3681 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3682 Cond);
3683 }
3684 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3685 // case we need to invert the select condition so we need to be careful to
3686 // avoid creating extra instructions.
3687 // (icmp ne (or (select cond, 0, NonZero), Other), 0)
3688 // -> (or (not cond), (icmp ne Other, 0))
3689 // (icmp eq (or (select cond, NonZero, 0), Other), 0)
3690 // -> (and (not cond), (icmp eq Other, 0))
3691 //
3692 // Only do this if the inner select has one use, in which case we are
3693 // replacing `select` with `(not cond)`. Otherwise, we will create more
3694 // uses. NB: Trying to freely invert cond doesn't make sense here, as if
3695 // cond was freely invertable, the select arms would have been inverted.
3696 if (Sel->hasOneUse() &&
3697 (Pred == ICmpInst::ICMP_EQ
3698 ? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))
3699 : (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {
3700 Value *NotCond = Builder.CreateNot(Cond);
3701 Value *Cmp = Builder.CreateICmp(
3702 Pred, Other, Constant::getNullValue(Other->getType()));
3704 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3705 NotCond);
3706 }
3707 }
3708 break;
3709 }
3710 case Instruction::UDiv:
3711 case Instruction::SDiv:
3712 if (BO->isExact()) {
3713 // div exact X, Y eq/ne 0 -> X eq/ne 0
3714 // div exact X, Y eq/ne 1 -> X eq/ne Y
3715 // div exact X, Y eq/ne C ->
3716 // if Y * C never-overflow && OneUse:
3717 // -> Y * C eq/ne X
3718 if (C.isZero())
3719 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));
3720 else if (C.isOne())
3721 return new ICmpInst(Pred, BOp0, BOp1);
3722 else if (BO->hasOneUse()) {
3724 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,
3725 Cmp.getOperand(1), BO);
3727 Value *YC =
3728 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));
3729 return new ICmpInst(Pred, YC, BOp0);
3730 }
3731 }
3732 }
3733 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3734 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3735 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3736 return new ICmpInst(NewPred, BOp1, BOp0);
3737 }
3738 break;
3739 default:
3740 break;
3741 }
3742 return nullptr;
3743}
3744
3746 const APInt &CRhs,
3747 InstCombiner::BuilderTy &Builder,
3748 const SimplifyQuery &Q) {
3749 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3750 "Non-ctpop intrin in ctpop fold");
3751 if (!CtpopLhs->hasOneUse())
3752 return nullptr;
3753
3754 // Power of 2 test:
3755 // isPow2OrZero : ctpop(X) u< 2
3756 // isPow2 : ctpop(X) == 1
3757 // NotPow2OrZero: ctpop(X) u> 1
3758 // NotPow2 : ctpop(X) != 1
3759 // If we know any bit of X can be folded to:
3760 // IsPow2 : X & (~Bit) == 0
3761 // NotPow2 : X & (~Bit) != 0
3762 const ICmpInst::Predicate Pred = I.getPredicate();
3763 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3764 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3765 Value *Op = CtpopLhs->getArgOperand(0);
3766 KnownBits OpKnown = computeKnownBits(Op, Q.DL, Q.AC, Q.CxtI, Q.DT);
3767 // No need to check for count > 1, that should be already constant folded.
3768 if (OpKnown.countMinPopulation() == 1) {
3769 Value *And = Builder.CreateAnd(
3770 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));
3771 return new ICmpInst(
3772 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3775 And, Constant::getNullValue(Op->getType()));
3776 }
3777 }
3778
3779 return nullptr;
3780}
3781
3782/// Fold an equality icmp with LLVM intrinsic and constant operand.
3784 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3785 Type *Ty = II->getType();
3786 unsigned BitWidth = C.getBitWidth();
3787 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3788
3789 switch (II->getIntrinsicID()) {
3790 case Intrinsic::abs:
3791 // abs(A) == 0 -> A == 0
3792 // abs(A) == INT_MIN -> A == INT_MIN
3793 if (C.isZero() || C.isMinSignedValue())
3794 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));
3795 break;
3796
3797 case Intrinsic::bswap:
3798 // bswap(A) == C -> A == bswap(C)
3799 return new ICmpInst(Pred, II->getArgOperand(0),
3800 ConstantInt::get(Ty, C.byteSwap()));
3801
3802 case Intrinsic::bitreverse:
3803 // bitreverse(A) == C -> A == bitreverse(C)
3804 return new ICmpInst(Pred, II->getArgOperand(0),
3805 ConstantInt::get(Ty, C.reverseBits()));
3806
3807 case Intrinsic::ctlz:
3808 case Intrinsic::cttz: {
3809 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3810 if (C == BitWidth)
3811 return new ICmpInst(Pred, II->getArgOperand(0),
3813
3814 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3815 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3816 // Limit to one use to ensure we don't increase instruction count.
3817 unsigned Num = C.getLimitedValue(BitWidth);
3818 if (Num != BitWidth && II->hasOneUse()) {
3819 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3820 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)
3821 : APInt::getHighBitsSet(BitWidth, Num + 1);
3822 APInt Mask2 = IsTrailing
3825 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),
3826 ConstantInt::get(Ty, Mask2));
3827 }
3828 break;
3829 }
3830
3831 case Intrinsic::ctpop: {
3832 // popcount(A) == 0 -> A == 0 and likewise for !=
3833 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3834 bool IsZero = C.isZero();
3835 if (IsZero || C == BitWidth)
3836 return new ICmpInst(Pred, II->getArgOperand(0),
3837 IsZero ? Constant::getNullValue(Ty)
3839
3840 break;
3841 }
3842
3843 case Intrinsic::fshl:
3844 case Intrinsic::fshr:
3845 if (II->getArgOperand(0) == II->getArgOperand(1)) {
3846 const APInt *RotAmtC;
3847 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3848 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3849 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))
3850 return new ICmpInst(Pred, II->getArgOperand(0),
3851 II->getIntrinsicID() == Intrinsic::fshl
3852 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))
3853 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));
3854 }
3855 break;
3856
3857 case Intrinsic::umax:
3858 case Intrinsic::uadd_sat: {
3859 // uadd.sat(a, b) == 0 -> (a | b) == 0
3860 // umax(a, b) == 0 -> (a | b) == 0
3861 if (C.isZero() && II->hasOneUse()) {
3862 Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1));
3863 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3864 }
3865 break;
3866 }
3867
3868 case Intrinsic::ssub_sat:
3869 // ssub.sat(a, b) == 0 -> a == b
3870 if (C.isZero())
3871 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3872 break;
3873 case Intrinsic::usub_sat: {
3874 // usub.sat(a, b) == 0 -> a <= b
3875 if (C.isZero()) {
3876 ICmpInst::Predicate NewPred =
3878 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));
3879 }
3880 break;
3881 }
3882 default:
3883 break;
3884 }
3885
3886 return nullptr;
3887}
3888
3889/// Fold an icmp with LLVM intrinsics
3890static Instruction *
3892 InstCombiner::BuilderTy &Builder) {
3893 assert(Cmp.isEquality());
3894
3895 ICmpInst::Predicate Pred = Cmp.getPredicate();
3896 Value *Op0 = Cmp.getOperand(0);
3897 Value *Op1 = Cmp.getOperand(1);
3898 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);
3899 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);
3900 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3901 return nullptr;
3902
3903 switch (IIOp0->getIntrinsicID()) {
3904 case Intrinsic::bswap:
3905 case Intrinsic::bitreverse:
3906 // If both operands are byte-swapped or bit-reversed, just compare the
3907 // original values.
3908 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3909 case Intrinsic::fshl:
3910 case Intrinsic::fshr: {
3911 // If both operands are rotated by same amount, just compare the
3912 // original values.
3913 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))
3914 break;
3915 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))
3916 break;
3917 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))
3918 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3919
3920 // rotate(X, AmtX) == rotate(Y, AmtY)
3921 // -> rotate(X, AmtX - AmtY) == Y
3922 // Do this if either both rotates have one use or if only one has one use
3923 // and AmtX/AmtY are constants.
3924 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3925 if (OneUses == 2 ||
3926 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&
3927 match(IIOp1->getOperand(2), m_ImmConstant()))) {
3928 Value *SubAmt =
3929 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));
3930 Value *CombinedRotate = Builder.CreateIntrinsic(
3931 Op0->getType(), IIOp0->getIntrinsicID(),
3932 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});
3933 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);
3934 }
3935 } break;
3936 default:
3937 break;
3938 }
3939
3940 return nullptr;
3941}
3942
3943/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3944/// where X is some kind of instruction and C is AllowPoison.
3945/// TODO: Move more folds which allow poison to this function.
3948 const APInt &C) {
3949 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3950 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {
3951 switch (II->getIntrinsicID()) {
3952 default:
3953 break;
3954 case Intrinsic::fshl:
3955 case Intrinsic::fshr:
3956 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {
3957 // (rot X, ?) == 0/-1 --> X == 0/-1
3958 if (C.isZero() || C.isAllOnes())
3959 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
3960 }
3961 break;
3962 }
3963 }
3964
3965 return nullptr;
3966}
3967
3968/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
3970 BinaryOperator *BO,
3971 const APInt &C) {
3972 switch (BO->getOpcode()) {
3973 case Instruction::Xor:
3974 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
3975 return I;
3976 break;
3977 case Instruction::And:
3978 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
3979 return I;
3980 break;
3981 case Instruction::Or:
3982 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
3983 return I;
3984 break;
3985 case Instruction::Mul:
3986 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
3987 return I;
3988 break;
3989 case Instruction::Shl:
3990 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
3991 return I;
3992 break;
3993 case Instruction::LShr:
3994 case Instruction::AShr:
3995 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
3996 return I;
3997 break;
3998 case Instruction::SRem:
3999 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
4000 return I;
4001 break;
4002 case Instruction::UDiv:
4003 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
4004 return I;
4005 [[fallthrough]];
4006 case Instruction::SDiv:
4007 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
4008 return I;
4009 break;
4010 case Instruction::Sub:
4011 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
4012 return I;
4013 break;
4014 case Instruction::Add:
4015 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
4016 return I;
4017 break;
4018 default:
4019 break;
4020 }
4021
4022 // TODO: These folds could be refactored to be part of the above calls.
4024 return I;
4025
4026 // Fall back to handling `icmp pred (select A ? C1 : C2) binop (select B ? C3
4027 // : C4), C5` pattern, by computing a truth table of the four constant
4028 // variants.
4030}
4031
4032static Instruction *
4034 const APInt &C,
4035 InstCombiner::BuilderTy &Builder) {
4036 // This transform may end up producing more than one instruction for the
4037 // intrinsic, so limit it to one user of the intrinsic.
4038 if (!II->hasOneUse())
4039 return nullptr;
4040
4041 // Let Y = [add/sub]_sat(X, C) pred C2
4042 // SatVal = The saturating value for the operation
4043 // WillWrap = Whether or not the operation will underflow / overflow
4044 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
4045 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
4046 //
4047 // When (SatVal pred C2) is true, then
4048 // Y = WillWrap ? true : ((X binop C) pred C2)
4049 // => Y = WillWrap || ((X binop C) pred C2)
4050 // else
4051 // Y = WillWrap ? false : ((X binop C) pred C2)
4052 // => Y = !WillWrap ? ((X binop C) pred C2) : false
4053 // => Y = !WillWrap && ((X binop C) pred C2)
4054 Value *Op0 = II->getOperand(0);
4055 Value *Op1 = II->getOperand(1);
4056
4057 const APInt *COp1;
4058 // This transform only works when the intrinsic has an integral constant or
4059 // splat vector as the second operand.
4060 if (!match(Op1, m_APInt(COp1)))
4061 return nullptr;
4062
4063 APInt SatVal;
4064 switch (II->getIntrinsicID()) {
4065 default:
4067 "This function only works with usub_sat and uadd_sat for now!");
4068 case Intrinsic::uadd_sat:
4069 SatVal = APInt::getAllOnes(C.getBitWidth());
4070 break;
4071 case Intrinsic::usub_sat:
4072 SatVal = APInt::getZero(C.getBitWidth());
4073 break;
4074 }
4075
4076 // Check (SatVal pred C2)
4077 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);
4078
4079 // !WillWrap.
4081 II->getBinaryOp(), *COp1, II->getNoWrapKind());
4082
4083 // WillWrap.
4084 if (SatValCheck)
4085 C1 = C1.inverse();
4086
4088 if (II->getBinaryOp() == Instruction::Add)
4089 C2 = C2.sub(*COp1);
4090 else
4091 C2 = C2.add(*COp1);
4092
4093 Instruction::BinaryOps CombiningOp =
4094 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
4095
4096 std::optional<ConstantRange> Combination;
4097 if (CombiningOp == Instruction::BinaryOps::Or)
4098 Combination = C1.exactUnionWith(C2);
4099 else /* CombiningOp == Instruction::BinaryOps::And */
4100 Combination = C1.exactIntersectWith(C2);
4101
4102 if (!Combination)
4103 return nullptr;
4104
4105 CmpInst::Predicate EquivPred;
4106 APInt EquivInt;
4107 APInt EquivOffset;
4108
4109 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
4110
4111 return new ICmpInst(
4112 EquivPred,
4113 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),
4114 ConstantInt::get(Op1->getType(), EquivInt));
4115}
4116
4117static Instruction *
4119 const APInt &C,
4120 InstCombiner::BuilderTy &Builder) {
4121 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
4122 switch (Pred) {
4123 case ICmpInst::ICMP_EQ:
4124 case ICmpInst::ICMP_NE:
4125 if (C.isZero())
4126 NewPredicate = Pred;
4127 else if (C.isOne())
4128 NewPredicate =
4130 else if (C.isAllOnes())
4131 NewPredicate =
4133 break;
4134
4135 case ICmpInst::ICMP_SGT:
4136 if (C.isAllOnes())
4137 NewPredicate = ICmpInst::ICMP_UGE;
4138 else if (C.isZero())
4139 NewPredicate = ICmpInst::ICMP_UGT;
4140 break;
4141
4142 case ICmpInst::ICMP_SLT:
4143 if (C.isZero())
4144 NewPredicate = ICmpInst::ICMP_ULT;
4145 else if (C.isOne())
4146 NewPredicate = ICmpInst::ICMP_ULE;
4147 break;
4148
4149 case ICmpInst::ICMP_ULT:
4150 if (C.ugt(1))
4151 NewPredicate = ICmpInst::ICMP_UGE;
4152 break;
4153
4154 case ICmpInst::ICMP_UGT:
4155 if (!C.isZero() && !C.isAllOnes())
4156 NewPredicate = ICmpInst::ICMP_ULT;
4157 break;
4158
4159 default:
4160 break;
4161 }
4162
4163 if (!NewPredicate)
4164 return nullptr;
4165
4166 if (I->getIntrinsicID() == Intrinsic::scmp)
4167 NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate);
4168 Value *LHS = I->getOperand(0);
4169 Value *RHS = I->getOperand(1);
4170 return new ICmpInst(*NewPredicate, LHS, RHS);
4171}
4172
4173/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
4176 const APInt &C) {
4177 ICmpInst::Predicate Pred = Cmp.getPredicate();
4178
4179 // Handle folds that apply for any kind of icmp.
4180 switch (II->getIntrinsicID()) {
4181 default:
4182 break;
4183 case Intrinsic::uadd_sat:
4184 case Intrinsic::usub_sat:
4185 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
4186 Pred, cast<SaturatingInst>(II), C, Builder))
4187 return Folded;
4188 break;
4189 case Intrinsic::ctpop: {
4190 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
4191 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))
4192 return R;
4193 } break;
4194 case Intrinsic::scmp:
4195 case Intrinsic::ucmp:
4196 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, II, C, Builder))
4197 return Folded;
4198 break;
4199 }
4200
4201 if (Cmp.isEquality())
4202 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
4203
4204 Type *Ty = II->getType();
4205 unsigned BitWidth = C.getBitWidth();
4206 switch (II->getIntrinsicID()) {
4207 case Intrinsic::ctpop: {
4208 // (ctpop X > BitWidth - 1) --> X == -1
4209 Value *X = II->getArgOperand(0);
4210 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
4211 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,
4213 // (ctpop X < BitWidth) --> X != -1
4214 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
4215 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,
4217 break;
4218 }
4219 case Intrinsic::ctlz: {
4220 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
4221 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4222 unsigned Num = C.getLimitedValue();
4223 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);
4224 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,
4225 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4226 }
4227
4228 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
4229 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4230 unsigned Num = C.getLimitedValue();
4232 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,
4233 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4234 }
4235 break;
4236 }
4237 case Intrinsic::cttz: {
4238 // Limit to one use to ensure we don't increase instruction count.
4239 if (!II->hasOneUse())
4240 return nullptr;
4241
4242 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
4243 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4244 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);
4245 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,
4246 Builder.CreateAnd(II->getArgOperand(0), Mask),
4248 }
4249
4250 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
4251 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4252 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());
4253 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,
4254 Builder.CreateAnd(II->getArgOperand(0), Mask),
4256 }
4257 break;
4258 }
4259 case Intrinsic::ssub_sat:
4260 // ssub.sat(a, b) spred 0 -> a spred b
4261 if (ICmpInst::isSigned(Pred)) {
4262 if (C.isZero())
4263 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
4264 // X s<= 0 is cannonicalized to X s< 1
4265 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
4266 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),
4267 II->getArgOperand(1));
4268 // X s>= 0 is cannonicalized to X s> -1
4269 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
4270 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),
4271 II->getArgOperand(1));
4272 }
4273 break;
4274 default:
4275 break;
4276 }
4277
4278 return nullptr;
4279}
4280
4281/// Handle icmp with constant (but not simple integer constant) RHS.
4283 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4284 Constant *RHSC = dyn_cast<Constant>(Op1);
4286 if (!RHSC || !LHSI)
4287 return nullptr;
4288
4289 switch (LHSI->getOpcode()) {
4290 case Instruction::IntToPtr:
4291 // icmp pred inttoptr(X), null -> icmp pred X, 0
4292 if (RHSC->isNullValue() &&
4293 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
4294 return new ICmpInst(
4295 I.getPredicate(), LHSI->getOperand(0),
4297 break;
4298
4299 case Instruction::Load:
4300 // Try to optimize things like "A[i] > 4" to index computations.
4301 if (GetElementPtrInst *GEP =
4303 if (Instruction *Res =
4305 return Res;
4306 break;
4307 }
4308
4309 return nullptr;
4310}
4311
4313 Value *RHS, const ICmpInst &I) {
4314 // Try to fold the comparison into the select arms, which will cause the
4315 // select to be converted into a logical and/or.
4316 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4317 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))
4318 return Res;
4319 if (std::optional<bool> Impl = isImpliedCondition(
4320 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))
4321 return ConstantInt::get(I.getType(), *Impl);
4322 return nullptr;
4323 };
4324
4325 ConstantInt *CI = nullptr;
4326 Value *Op1 = SimplifyOp(SI->getOperand(1), true);
4327 if (Op1)
4328 CI = dyn_cast<ConstantInt>(Op1);
4329
4330 Value *Op2 = SimplifyOp(SI->getOperand(2), false);
4331 if (Op2)
4332 CI = dyn_cast<ConstantInt>(Op2);
4333
4334 auto Simplifies = [&](Value *Op, unsigned Idx) {
4335 // A comparison of ucmp/scmp with a constant will fold into an icmp.
4336 const APInt *Dummy;
4337 return Op ||
4338 (isa<CmpIntrinsic>(SI->getOperand(Idx)) &&
4339 SI->getOperand(Idx)->hasOneUse() && match(RHS, m_APInt(Dummy)));
4340 };
4341
4342 // We only want to perform this transformation if it will not lead to
4343 // additional code. This is true if either both sides of the select
4344 // fold to a constant (in which case the icmp is replaced with a select
4345 // which will usually simplify) or this is the only user of the
4346 // select (in which case we are trading a select+icmp for a simpler
4347 // select+icmp) or all uses of the select can be replaced based on
4348 // dominance information ("Global cases").
4349 bool Transform = false;
4350 if (Op1 && Op2)
4351 Transform = true;
4352 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {
4353 // Local case
4354 if (SI->hasOneUse())
4355 Transform = true;
4356 // Global cases
4357 else if (CI && !CI->isZero())
4358 // When Op1 is constant try replacing select with second operand.
4359 // Otherwise Op2 is constant and try replacing select with first
4360 // operand.
4361 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4362 }
4363 if (Transform) {
4364 if (!Op1)
4365 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4366 if (!Op2)
4367 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4368 return SelectInst::Create(SI->getOperand(0), Op1, Op2);
4369 }
4370
4371 return nullptr;
4372}
4373
4374// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4375static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4376 unsigned Depth = 0) {
4377 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4378 return true;
4379 if (V->getType()->getScalarSizeInBits() == 1)
4380 return true;
4382 return false;
4383 Value *X;
4385 if (!I)
4386 return false;
4387 switch (I->getOpcode()) {
4388 case Instruction::ZExt:
4389 // ZExt(Mask) is a Mask.
4390 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4391 case Instruction::SExt:
4392 // SExt(Mask) is a Mask.
4393 // SExt(~Mask) is a ~Mask.
4394 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4395 case Instruction::And:
4396 case Instruction::Or:
4397 // Mask0 | Mask1 is a Mask.
4398 // Mask0 & Mask1 is a Mask.
4399 // ~Mask0 | ~Mask1 is a ~Mask.
4400 // ~Mask0 & ~Mask1 is a ~Mask.
4401 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4402 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4403 case Instruction::Xor:
4404 if (match(V, m_Not(m_Value(X))))
4405 return isMaskOrZero(X, !Not, Q, Depth);
4406
4407 // (X ^ -X) is a ~Mask
4408 if (Not)
4409 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4410 // (X ^ (X - 1)) is a Mask
4411 else
4412 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4413 case Instruction::Select:
4414 // c ? Mask0 : Mask1 is a Mask.
4415 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4416 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4417 case Instruction::Shl:
4418 // (~Mask) << X is a ~Mask.
4419 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4420 case Instruction::LShr:
4421 // Mask >> X is a Mask.
4422 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4423 case Instruction::AShr:
4424 // Mask s>> X is a Mask.
4425 // ~Mask s>> X is a ~Mask.
4426 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4427 case Instruction::Add:
4428 // Pow2 - 1 is a Mask.
4429 if (!Not && match(I->getOperand(1), m_AllOnes()))
4430 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4431 Q.AC, Q.CxtI, Q.DT, Depth);
4432 break;
4433 case Instruction::Sub:
4434 // -Pow2 is a ~Mask.
4435 if (Not && match(I->getOperand(0), m_Zero()))
4436 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4437 Q.AC, Q.CxtI, Q.DT, Depth);
4438 break;
4439 case Instruction::Call: {
4440 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4441 switch (II->getIntrinsicID()) {
4442 // min/max(Mask0, Mask1) is a Mask.
4443 // min/max(~Mask0, ~Mask1) is a ~Mask.
4444 case Intrinsic::umax:
4445 case Intrinsic::smax:
4446 case Intrinsic::umin:
4447 case Intrinsic::smin:
4448 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4449 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4450
4451 // In the context of masks, bitreverse(Mask) == ~Mask
4452 case Intrinsic::bitreverse:
4453 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4454 default:
4455 break;
4456 }
4457 }
4458 break;
4459 }
4460 default:
4461 break;
4462 }
4463 return false;
4464}
4465
4466/// Some comparisons can be simplified.
4467/// In this case, we are looking for comparisons that look like
4468/// a check for a lossy truncation.
4469/// Folds:
4470/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4471/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4472/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4473/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4474/// Where Mask is some pattern that produces all-ones in low bits:
4475/// (-1 >> y)
4476/// ((-1 << y) >> y) <- non-canonical, has extra uses
4477/// ~(-1 << y)
4478/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4479/// The Mask can be a constant, too.
4480/// For some predicates, the operands are commutative.
4481/// For others, x can only be on a specific side.
4483 Value *Op1, const SimplifyQuery &Q,
4484 InstCombiner &IC) {
4485
4486 ICmpInst::Predicate DstPred;
4487 switch (Pred) {
4489 // x & Mask == x
4490 // x & ~Mask == 0
4491 // ~x | Mask == -1
4492 // -> x u<= Mask
4493 // x & ~Mask == ~Mask
4494 // -> ~Mask u<= x
4496 break;
4498 // x & Mask != x
4499 // x & ~Mask != 0
4500 // ~x | Mask != -1
4501 // -> x u> Mask
4502 // x & ~Mask != ~Mask
4503 // -> ~Mask u> x
4505 break;
4507 // x & Mask u< x
4508 // -> x u> Mask
4509 // x & ~Mask u< ~Mask
4510 // -> ~Mask u> x
4512 break;
4514 // x & Mask u>= x
4515 // -> x u<= Mask
4516 // x & ~Mask u>= ~Mask
4517 // -> ~Mask u<= x
4519 break;
4521 // x & Mask s< x [iff Mask s>= 0]
4522 // -> x s> Mask
4523 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4524 // -> ~Mask s> x
4526 break;
4528 // x & Mask s>= x [iff Mask s>= 0]
4529 // -> x s<= Mask
4530 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4531 // -> ~Mask s<= x
4533 break;
4534 default:
4535 // We don't support sgt,sle
4536 // ult/ugt are simplified to true/false respectively.
4537 return nullptr;
4538 }
4539
4540 Value *X, *M;
4541 // Put search code in lambda for early positive returns.
4542 auto IsLowBitMask = [&]() {
4543 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4544 X = Op1;
4545 // Look for: x & Mask pred x
4546 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4547 return !ICmpInst::isSigned(Pred) ||
4548 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4549 }
4550
4551 // Look for: x & ~Mask pred ~Mask
4552 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4553 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4554 }
4555 return false;
4556 }
4557 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4558 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4559
4560 auto Check = [&]() {
4561 // Look for: ~x | Mask == -1
4562 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4563 if (Value *NotX =
4564 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4565 X = NotX;
4566 return true;
4567 }
4568 }
4569 return false;
4570 };
4571 if (Check())
4572 return true;
4573 std::swap(X, M);
4574 return Check();
4575 }
4576 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4577 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4578 auto Check = [&]() {
4579 // Look for: x & ~Mask == 0
4580 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4581 if (Value *NotM =
4582 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4583 M = NotM;
4584 return true;
4585 }
4586 }
4587 return false;
4588 };
4589 if (Check())
4590 return true;
4591 std::swap(X, M);
4592 return Check();
4593 }
4594 return false;
4595 };
4596
4597 if (!IsLowBitMask())
4598 return nullptr;
4599
4600 return IC.Builder.CreateICmp(DstPred, X, M);
4601}
4602
4603/// Some comparisons can be simplified.
4604/// In this case, we are looking for comparisons that look like
4605/// a check for a lossy signed truncation.
4606/// Folds: (MaskedBits is a constant.)
4607/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4608/// Into:
4609/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4610/// Where KeptBits = bitwidth(%x) - MaskedBits
4611static Value *
4613 InstCombiner::BuilderTy &Builder) {
4614 CmpPredicate SrcPred;
4615 Value *X;
4616 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4617 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4618 if (!match(&I, m_c_ICmp(SrcPred,
4620 m_APInt(C1))),
4621 m_Deferred(X))))
4622 return nullptr;
4623
4624 // Potential handling of non-splats: for each element:
4625 // * if both are undef, replace with constant 0.
4626 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4627 // * if both are not undef, and are different, bailout.
4628 // * else, only one is undef, then pick the non-undef one.
4629
4630 // The shift amount must be equal.
4631 if (*C0 != *C1)
4632 return nullptr;
4633 const APInt &MaskedBits = *C0;
4634 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4635
4636 ICmpInst::Predicate DstPred;
4637 switch (SrcPred) {
4639 // ((%x << MaskedBits) a>> MaskedBits) == %x
4640 // =>
4641 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4643 break;
4645 // ((%x << MaskedBits) a>> MaskedBits) != %x
4646 // =>
4647 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4649 break;
4650 // FIXME: are more folds possible?
4651 default:
4652 return nullptr;
4653 }
4654
4655 auto *XType = X->getType();
4656 const unsigned XBitWidth = XType->getScalarSizeInBits();
4657 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4658 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4659
4660 // KeptBits = bitwidth(%x) - MaskedBits
4661 const APInt KeptBits = BitWidth - MaskedBits;
4662 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4663 // ICmpCst = (1 << KeptBits)
4664 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4665 assert(ICmpCst.isPowerOf2());
4666 // AddCst = (1 << (KeptBits-1))
4667 const APInt AddCst = ICmpCst.lshr(1);
4668 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4669
4670 // T0 = add %x, AddCst
4671 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4672 // T1 = T0 DstPred ICmpCst
4673 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4674
4675 return T1;
4676}
4677
4678// Given pattern:
4679// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4680// we should move shifts to the same hand of 'and', i.e. rewrite as
4681// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4682// We are only interested in opposite logical shifts here.
4683// One of the shifts can be truncated.
4684// If we can, we want to end up creating 'lshr' shift.
4685static Value *
4687 InstCombiner::BuilderTy &Builder) {
4688 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4689 !I.getOperand(0)->hasOneUse())
4690 return nullptr;
4691
4692 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4693
4694 // Look for an 'and' of two logical shifts, one of which may be truncated.
4695 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4696 Instruction *XShift, *MaybeTruncation, *YShift;
4697 if (!match(
4698 I.getOperand(0),
4699 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4701 m_AnyLogicalShift, m_Instruction(YShift))),
4702 m_Instruction(MaybeTruncation)))))
4703 return nullptr;
4704
4705 // We potentially looked past 'trunc', but only when matching YShift,
4706 // therefore YShift must have the widest type.
4707 Instruction *WidestShift = YShift;
4708 // Therefore XShift must have the shallowest type.
4709 // Or they both have identical types if there was no truncation.
4710 Instruction *NarrowestShift = XShift;
4711
4712 Type *WidestTy = WidestShift->getType();
4713 Type *NarrowestTy = NarrowestShift->getType();
4714 assert(NarrowestTy == I.getOperand(0)->getType() &&
4715 "We did not look past any shifts while matching XShift though.");
4716 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4717
4718 // If YShift is a 'lshr', swap the shifts around.
4719 if (match(YShift, m_LShr(m_Value(), m_Value())))
4720 std::swap(XShift, YShift);
4721
4722 // The shifts must be in opposite directions.
4723 auto XShiftOpcode = XShift->getOpcode();
4724 if (XShiftOpcode == YShift->getOpcode())
4725 return nullptr; // Do not care about same-direction shifts here.
4726
4727 Value *X, *XShAmt, *Y, *YShAmt;
4728 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4729 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4730
4731 // If one of the values being shifted is a constant, then we will end with
4732 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4733 // however, we will need to ensure that we won't increase instruction count.
4734 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4735 // At least one of the hands of the 'and' should be one-use shift.
4736 if (!match(I.getOperand(0),
4737 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4738 return nullptr;
4739 if (HadTrunc) {
4740 // Due to the 'trunc', we will need to widen X. For that either the old
4741 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4742 if (!MaybeTruncation->hasOneUse() &&
4743 !NarrowestShift->getOperand(1)->hasOneUse())
4744 return nullptr;
4745 }
4746 }
4747
4748 // We have two shift amounts from two different shifts. The types of those
4749 // shift amounts may not match. If that's the case let's bailout now.
4750 if (XShAmt->getType() != YShAmt->getType())
4751 return nullptr;
4752
4753 // As input, we have the following pattern:
4754 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4755 // We want to rewrite that as:
4756 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4757 // While we know that originally (Q+K) would not overflow
4758 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4759 // shift amounts. so it may now overflow in smaller bitwidth.
4760 // To ensure that does not happen, we need to ensure that the total maximal
4761 // shift amount is still representable in that smaller bit width.
4762 unsigned MaximalPossibleTotalShiftAmount =
4763 (WidestTy->getScalarSizeInBits() - 1) +
4764 (NarrowestTy->getScalarSizeInBits() - 1);
4765 APInt MaximalRepresentableShiftAmount =
4767 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4768 return nullptr;
4769
4770 // Can we fold (XShAmt+YShAmt) ?
4771 auto *NewShAmt = dyn_cast_or_null<Constant>(
4772 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4773 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4774 if (!NewShAmt)
4775 return nullptr;
4776 if (NewShAmt->getType() != WidestTy) {
4777 NewShAmt =
4778 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4779 if (!NewShAmt)
4780 return nullptr;
4781 }
4782 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4783
4784 // Is the new shift amount smaller than the bit width?
4785 // FIXME: could also rely on ConstantRange.
4786 if (!match(NewShAmt,
4788 APInt(WidestBitWidth, WidestBitWidth))))
4789 return nullptr;
4790
4791 // An extra legality check is needed if we had trunc-of-lshr.
4792 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4793 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4794 WidestShift]() {
4795 // It isn't obvious whether it's worth it to analyze non-constants here.
4796 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4797 // If *any* of these preconditions matches we can perform the fold.
4798 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4799 ? NewShAmt->getSplatValue()
4800 : NewShAmt;
4801 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4802 if (NewShAmtSplat &&
4803 (NewShAmtSplat->isNullValue() ||
4804 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4805 return true;
4806 // We consider *min* leading zeros so a single outlier
4807 // blocks the transform as opposed to allowing it.
4808 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4809 KnownBits Known = computeKnownBits(C, SQ.DL);
4810 unsigned MinLeadZero = Known.countMinLeadingZeros();
4811 // If the value being shifted has at most lowest bit set we can fold.
4812 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4813 if (MaxActiveBits <= 1)
4814 return true;
4815 // Precondition: NewShAmt u<= countLeadingZeros(C)
4816 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4817 return true;
4818 }
4819 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4820 KnownBits Known = computeKnownBits(C, SQ.DL);
4821 unsigned MinLeadZero = Known.countMinLeadingZeros();
4822 // If the value being shifted has at most lowest bit set we can fold.
4823 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4824 if (MaxActiveBits <= 1)
4825 return true;
4826 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4827 if (NewShAmtSplat) {
4828 APInt AdjNewShAmt =
4829 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4830 if (AdjNewShAmt.ule(MinLeadZero))
4831 return true;
4832 }
4833 }
4834 return false; // Can't tell if it's ok.
4835 };
4836 if (!CanFold())
4837 return nullptr;
4838 }
4839
4840 // All good, we can do this fold.
4841 X = Builder.CreateZExt(X, WidestTy);
4842 Y = Builder.CreateZExt(Y, WidestTy);
4843 // The shift is the same that was for X.
4844 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4845 ? Builder.CreateLShr(X, NewShAmt)
4846 : Builder.CreateShl(X, NewShAmt);
4847 Value *T1 = Builder.CreateAnd(T0, Y);
4848 return Builder.CreateICmp(I.getPredicate(), T1,
4849 Constant::getNullValue(WidestTy));
4850}
4851
4852/// Fold
4853/// (-1 u/ x) u< y
4854/// ((x * y) ?/ x) != y
4855/// to
4856/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4857/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4858/// will mean that we are looking for the opposite answer.
4860 CmpPredicate Pred;
4861 Value *X, *Y;
4863 Instruction *Div;
4864 bool NeedNegation;
4865 // Look for: (-1 u/ x) u</u>= y
4866 if (!I.isEquality() &&
4867 match(&I, m_c_ICmp(Pred,
4869 m_Instruction(Div)),
4870 m_Value(Y)))) {
4871 Mul = nullptr;
4872
4873 // Are we checking that overflow does not happen, or does happen?
4874 switch (Pred) {
4876 NeedNegation = false;
4877 break; // OK
4879 NeedNegation = true;
4880 break; // OK
4881 default:
4882 return nullptr; // Wrong predicate.
4883 }
4884 } else // Look for: ((x * y) / x) !=/== y
4885 if (I.isEquality() &&
4886 match(&I, m_c_ICmp(Pred, m_Value(Y),
4889 m_Value(X)),
4891 m_Deferred(X))),
4892 m_Instruction(Div))))) {
4893 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4894 } else
4895 return nullptr;
4896
4898 // If the pattern included (x * y), we'll want to insert new instructions
4899 // right before that original multiplication so that we can replace it.
4900 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4901 if (MulHadOtherUses)
4902 Builder.SetInsertPoint(Mul);
4903
4904 CallInst *Call = Builder.CreateIntrinsic(
4905 Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow
4906 : Intrinsic::smul_with_overflow,
4907 X->getType(), {X, Y}, /*FMFSource=*/nullptr, "mul");
4908
4909 // If the multiplication was used elsewhere, to ensure that we don't leave
4910 // "duplicate" instructions, replace uses of that original multiplication
4911 // with the multiplication result from the with.overflow intrinsic.
4912 if (MulHadOtherUses)
4913 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4914
4915 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4916 if (NeedNegation) // This technically increases instruction count.
4917 Res = Builder.CreateNot(Res, "mul.not.ov");
4918
4919 // If we replaced the mul, erase it. Do this after all uses of Builder,
4920 // as the mul is used as insertion point.
4921 if (MulHadOtherUses)
4923
4924 return Res;
4925}
4926
4928 InstCombiner::BuilderTy &Builder) {
4929 CmpPredicate Pred;
4930 Value *X;
4931 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4932
4933 if (ICmpInst::isSigned(Pred))
4934 Pred = ICmpInst::getSwappedPredicate(Pred);
4935 else if (ICmpInst::isUnsigned(Pred))
4936 Pred = ICmpInst::getSignedPredicate(Pred);
4937 // else for equality-comparisons just keep the predicate.
4938
4939 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4940 Constant::getNullValue(X->getType()), I.getName());
4941 }
4942
4943 // A value is not equal to its negation unless that value is 0 or
4944 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4945 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
4946 ICmpInst::isEquality(Pred)) {
4947 Type *Ty = X->getType();
4948 uint32_t BitWidth = Ty->getScalarSizeInBits();
4949 Constant *MaxSignedVal =
4950 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
4951 Value *And = Builder.CreateAnd(X, MaxSignedVal);
4952 Constant *Zero = Constant::getNullValue(Ty);
4953 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
4954 }
4955
4956 return nullptr;
4957}
4958
4960 InstCombinerImpl &IC) {
4961 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4962 // Normalize and operand as operand 0.
4963 CmpInst::Predicate Pred = I.getPredicate();
4964 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
4965 std::swap(Op0, Op1);
4966 Pred = ICmpInst::getSwappedPredicate(Pred);
4967 }
4968
4969 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
4970 return nullptr;
4971
4972 // (icmp (X & Y) u< X --> (X & Y) != X
4973 if (Pred == ICmpInst::ICMP_ULT)
4974 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4975
4976 // (icmp (X & Y) u>= X --> (X & Y) == X
4977 if (Pred == ICmpInst::ICMP_UGE)
4978 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4979
4980 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4981 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
4982 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
4983 // so don't do this fold.
4984 if (!match(Op1, m_ImmConstant()))
4985 if (auto *NotOp1 =
4986 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
4987 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),
4988 Constant::getAllOnesValue(Op1->getType()));
4989 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
4990 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4991 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),
4992 Constant::getNullValue(Op1->getType()));
4993 }
4994
4995 if (!ICmpInst::isSigned(Pred))
4996 return nullptr;
4997
4998 KnownBits KnownY = IC.computeKnownBits(A, &I);
4999 // (X & NegY) spred X --> (X & NegY) upred X
5000 if (KnownY.isNegative())
5001 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
5002
5003 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
5004 return nullptr;
5005
5006 if (KnownY.isNonNegative())
5007 // (X & PosY) s<= X --> X s>= 0
5008 // (X & PosY) s> X --> X s< 0
5009 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
5010 Constant::getNullValue(Op1->getType()));
5011
5013 // (NegX & Y) s<= NegX --> Y s< 0
5014 // (NegX & Y) s> NegX --> Y s>= 0
5016 Constant::getNullValue(A->getType()));
5017
5018 return nullptr;
5019}
5020
5022 InstCombinerImpl &IC) {
5023 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5024
5025 // Normalize or operand as operand 0.
5026 CmpInst::Predicate Pred = I.getPredicate();
5027 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
5028 std::swap(Op0, Op1);
5029 Pred = ICmpInst::getSwappedPredicate(Pred);
5030 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
5031 return nullptr;
5032 }
5033
5034 // icmp (X | Y) u<= X --> (X | Y) == X
5035 if (Pred == ICmpInst::ICMP_ULE)
5036 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5037
5038 // icmp (X | Y) u> X --> (X | Y) != X
5039 if (Pred == ICmpInst::ICMP_UGT)
5040 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5041
5042 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
5043 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
5044 if (Value *NotOp1 = IC.getFreelyInverted(
5045 Op1, !isa<Constant>(Op1) && !Op1->hasNUsesOrMore(3), &IC.Builder))
5046 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
5047 Constant::getNullValue(Op1->getType()));
5048 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
5049 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
5050 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
5051 Constant::getAllOnesValue(Op1->getType()));
5052 }
5053 return nullptr;
5054}
5055
5057 InstCombinerImpl &IC) {
5058 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5059 // Normalize xor operand as operand 0.
5060 CmpInst::Predicate Pred = I.getPredicate();
5061 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
5062 std::swap(Op0, Op1);
5063 Pred = ICmpInst::getSwappedPredicate(Pred);
5064 }
5065 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
5066 return nullptr;
5067
5068 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
5069 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
5070 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
5071 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
5073 if (PredOut != Pred && isKnownNonZero(A, Q))
5074 return new ICmpInst(PredOut, Op0, Op1);
5075
5076 // These transform work when A is negative.
5077 // X s< X^A, X s<= X^A, X u> X^A, X u>= X^A --> X s< 0
5078 // X s> X^A, X s>= X^A, X u< X^A, X u<= X^A --> X s>= 0
5079 if (match(A, m_Negative())) {
5080 CmpInst::Predicate NewPred;
5081 switch (ICmpInst::getStrictPredicate(Pred)) {
5082 default:
5083 return nullptr;
5084 case ICmpInst::ICMP_SLT:
5085 case ICmpInst::ICMP_UGT:
5086 NewPred = ICmpInst::ICMP_SLT;
5087 break;
5088 case ICmpInst::ICMP_SGT:
5089 case ICmpInst::ICMP_ULT:
5090 NewPred = ICmpInst::ICMP_SGE;
5091 break;
5092 }
5093 Constant *Const = Constant::getNullValue(Op0->getType());
5094 return new ICmpInst(NewPred, Op0, Const);
5095 }
5096
5097 return nullptr;
5098}
5099
5100/// Return true if X is a multiple of C.
5101/// TODO: Handle non-power-of-2 factors.
5102static bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q) {
5103 if (C.isOne())
5104 return true;
5105
5106 if (!C.isPowerOf2())
5107 return false;
5108
5109 return MaskedValueIsZero(X, C - 1, Q);
5110}
5111
5112/// Try to fold icmp (binop), X or icmp X, (binop).
5113/// TODO: A large part of this logic is duplicated in InstSimplify's
5114/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
5115/// duplication.
5117 const SimplifyQuery &SQ) {
5118 const SimplifyQuery Q = SQ.getWithInstruction(&I);
5119 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5120
5121 // Special logic for binary operators.
5124 if (!BO0 && !BO1)
5125 return nullptr;
5126
5127 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
5128 return NewICmp;
5129
5130 const CmpInst::Predicate Pred = I.getPredicate();
5131 Value *X;
5132
5133 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
5134 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
5135 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
5136 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5137 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
5138 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
5139 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
5140 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5141 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
5142
5143 {
5144 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
5145 Constant *C;
5146 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
5147 m_ImmConstant(C)))) &&
5148 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
5150 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
5151 }
5152 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5153 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
5154 m_ImmConstant(C)))) &&
5155 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5157 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
5158 }
5159 }
5160
5161 // (icmp eq/ne (X, -P2), INT_MIN)
5162 // -> (icmp slt/sge X, INT_MIN + P2)
5163 if (ICmpInst::isEquality(Pred) && BO0 &&
5164 match(I.getOperand(1), m_SignMask()) &&
5166 // Will Constant fold.
5167 Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
5168 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5170 BO0->getOperand(0), NewC);
5171 }
5172
5173 {
5174 // Similar to above: an unsigned overflow comparison may use offset + mask:
5175 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5176 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5177 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5178 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5179 BinaryOperator *BO;
5180 const APInt *C;
5181 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5182 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5184 CmpInst::Predicate NewPred =
5186 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5187 return new ICmpInst(NewPred, Op1, Zero);
5188 }
5189
5190 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5191 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5193 CmpInst::Predicate NewPred =
5195 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5196 return new ICmpInst(NewPred, Op0, Zero);
5197 }
5198 }
5199
5200 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5201 bool Op0HasNUW = false, Op1HasNUW = false;
5202 bool Op0HasNSW = false, Op1HasNSW = false;
5203 // Analyze the case when either Op0 or Op1 is an add instruction.
5204 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5205 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5206 bool &HasNSW, bool &HasNUW) -> bool {
5208 HasNUW = BO.hasNoUnsignedWrap();
5209 HasNSW = BO.hasNoSignedWrap();
5210 return ICmpInst::isEquality(Pred) ||
5211 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5212 (CmpInst::isSigned(Pred) && HasNSW);
5213 } else if (BO.getOpcode() == Instruction::Or) {
5214 HasNUW = true;
5215 HasNSW = true;
5216 return true;
5217 } else {
5218 return false;
5219 }
5220 };
5221 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5222
5223 if (BO0) {
5224 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
5225 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5226 }
5227 if (BO1) {
5228 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
5229 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5230 }
5231
5232 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5233 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5234 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5235 return new ICmpInst(Pred, A == Op1 ? B : A,
5236 Constant::getNullValue(Op1->getType()));
5237
5238 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5239 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5240 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5241 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
5242 C == Op0 ? D : C);
5243
5244 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5245 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5246 NoOp1WrapProblem) {
5247 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5248 Value *Y, *Z;
5249 if (A == C) {
5250 // C + B == C + D -> B == D
5251 Y = B;
5252 Z = D;
5253 } else if (A == D) {
5254 // D + B == C + D -> B == C
5255 Y = B;
5256 Z = C;
5257 } else if (B == C) {
5258 // A + C == C + D -> A == D
5259 Y = A;
5260 Z = D;
5261 } else {
5262 assert(B == D);
5263 // A + D == C + D -> A == C
5264 Y = A;
5265 Z = C;
5266 }
5267 return new ICmpInst(Pred, Y, Z);
5268 }
5269
5270 if (ICmpInst::isRelational(Pred)) {
5271 // Return if both X and Y is divisible by Z/-Z.
5272 // TODO: Generalize to check if (X - Y) is divisible by Z/-Z.
5273 auto ShareCommonDivisor = [&Q](Value *X, Value *Y, Value *Z,
5274 bool IsNegative) -> bool {
5275 const APInt *OffsetC;
5276 if (!match(Z, m_APInt(OffsetC)))
5277 return false;
5278
5279 // Fast path for Z == 1/-1.
5280 if (IsNegative ? OffsetC->isAllOnes() : OffsetC->isOne())
5281 return true;
5282
5283 APInt C = *OffsetC;
5284 if (IsNegative)
5285 C.negate();
5286 // Note: -INT_MIN is also negative.
5287 if (!C.isStrictlyPositive())
5288 return false;
5289
5290 return isMultipleOf(X, C, Q) && isMultipleOf(Y, C, Q);
5291 };
5292
5293 // TODO: The subtraction-related identities shown below also hold, but
5294 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5295 // wouldn't happen even if they were implemented.
5296 //
5297 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5298 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5299 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5300 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5301
5302 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5303 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5304 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5305 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5306 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5307 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5308 if (A && NoOp0WrapProblem &&
5309 ShareCommonDivisor(A, Op1, B,
5310 ICmpInst::isLT(Pred) || ICmpInst::isGE(Pred)))
5312 Op1);
5313
5314 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5315 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5316 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5317 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5318 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5319 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5320 if (C && NoOp1WrapProblem &&
5321 ShareCommonDivisor(Op0, C, D,
5322 ICmpInst::isGT(Pred) || ICmpInst::isLE(Pred)))
5324 C);
5325 }
5326
5327 // if C1 has greater magnitude than C2:
5328 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5329 // s.t. C3 = C1 - C2
5330 //
5331 // if C2 has greater magnitude than C1:
5332 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5333 // s.t. C3 = C2 - C1
5334 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5335 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5336 const APInt *AP1, *AP2;
5337 // TODO: Support non-uniform vectors.
5338 // TODO: Allow poison passthrough if B or D's element is poison.
5339 if (match(B, m_APIntAllowPoison(AP1)) &&
5340 match(D, m_APIntAllowPoison(AP2)) &&
5341 AP1->isNegative() == AP2->isNegative()) {
5342 APInt AP1Abs = AP1->abs();
5343 APInt AP2Abs = AP2->abs();
5344 if (AP1Abs.uge(AP2Abs)) {
5345 APInt Diff = *AP1 - *AP2;
5346 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5347 Value *NewAdd = Builder.CreateAdd(
5348 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
5349 return new ICmpInst(Pred, NewAdd, C);
5350 } else {
5351 APInt Diff = *AP2 - *AP1;
5352 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5353 Value *NewAdd = Builder.CreateAdd(
5354 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
5355 return new ICmpInst(Pred, A, NewAdd);
5356 }
5357 }
5358 Constant *Cst1, *Cst2;
5359 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5360 ICmpInst::isEquality(Pred)) {
5361 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5362 Value *NewAdd = Builder.CreateAdd(C, Diff);
5363 return new ICmpInst(Pred, A, NewAdd);
5364 }
5365 }
5366
5367 // Analyze the case when either Op0 or Op1 is a sub instruction.
5368 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5369 A = nullptr;
5370 B = nullptr;
5371 C = nullptr;
5372 D = nullptr;
5373 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5374 A = BO0->getOperand(0);
5375 B = BO0->getOperand(1);
5376 }
5377 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5378 C = BO1->getOperand(0);
5379 D = BO1->getOperand(1);
5380 }
5381
5382 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5383 if (A == Op1 && NoOp0WrapProblem)
5384 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5385 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5386 if (C == Op0 && NoOp1WrapProblem)
5387 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5388
5389 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5390 // (A - B) u>/u<= A --> B u>/u<= A
5391 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5392 return new ICmpInst(Pred, B, A);
5393 // C u</u>= (C - D) --> C u</u>= D
5394 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5395 return new ICmpInst(Pred, C, D);
5396 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5397 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5398 isKnownNonZero(B, Q))
5400 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5401 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5402 isKnownNonZero(D, Q))
5404
5405 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5406 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5407 return new ICmpInst(Pred, A, C);
5408
5409 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5410 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5411 return new ICmpInst(Pred, D, B);
5412
5413 // icmp (0-X) < cst --> x > -cst
5414 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5415 Value *X;
5416 if (match(BO0, m_Neg(m_Value(X))))
5417 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5418 if (RHSC->isNotMinSignedValue())
5419 return new ICmpInst(I.getSwappedPredicate(), X,
5420 ConstantExpr::getNeg(RHSC));
5421 }
5422
5423 if (Instruction *R = foldICmpXorXX(I, Q, *this))
5424 return R;
5425 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5426 return R;
5427
5428 {
5429 // Try to remove shared multiplier from comparison:
5430 // X * Z pred Y * Z
5431 Value *X, *Y, *Z;
5432 if ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5433 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5434 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5435 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y))))) {
5436 if (ICmpInst::isSigned(Pred)) {
5437 if (Op0HasNSW && Op1HasNSW) {
5438 KnownBits ZKnown = computeKnownBits(Z, &I);
5439 if (ZKnown.isStrictlyPositive())
5440 return new ICmpInst(Pred, X, Y);
5441 if (ZKnown.isNegative())
5442 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y);
5444 SQ.getWithInstruction(&I));
5445 if (LessThan && match(LessThan, m_One()))
5446 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z,
5447 Constant::getNullValue(Z->getType()));
5448 Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y,
5449 SQ.getWithInstruction(&I));
5450 if (GreaterThan && match(GreaterThan, m_One()))
5451 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5452 }
5453 } else {
5454 bool NonZero;
5455 if (ICmpInst::isEquality(Pred)) {
5456 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0
5457 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&
5458 isKnownNonEqual(X, Y, SQ))
5459 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5460
5461 KnownBits ZKnown = computeKnownBits(Z, &I);
5462 // if Z % 2 != 0
5463 // X * Z eq/ne Y * Z -> X eq/ne Y
5464 if (ZKnown.countMaxTrailingZeros() == 0)
5465 return new ICmpInst(Pred, X, Y);
5466 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5467 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5468 // X * Z eq/ne Y * Z -> X eq/ne Y
5469 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5470 return new ICmpInst(Pred, X, Y);
5471 } else
5472 NonZero = isKnownNonZero(Z, Q);
5473
5474 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5475 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5476 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5477 return new ICmpInst(Pred, X, Y);
5478 }
5479 }
5480 }
5481
5482 BinaryOperator *SRem = nullptr;
5483 // icmp (srem X, Y), Y
5484 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5485 SRem = BO0;
5486 // icmp Y, (srem X, Y)
5487 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5488 Op0 == BO1->getOperand(1))
5489 SRem = BO1;
5490 if (SRem) {
5491 // We don't check hasOneUse to avoid increasing register pressure because
5492 // the value we use is the same value this instruction was already using.
5493 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5494 default:
5495 break;
5496 case ICmpInst::ICMP_EQ:
5497 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5498 case ICmpInst::ICMP_NE:
5499 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5500 case ICmpInst::ICMP_SGT:
5501 case ICmpInst::ICMP_SGE:
5502 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5504 case ICmpInst::ICMP_SLT:
5505 case ICmpInst::ICMP_SLE:
5506 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5508 }
5509 }
5510
5511 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5512 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5513 BO0->getOperand(1) == BO1->getOperand(1)) {
5514 switch (BO0->getOpcode()) {
5515 default:
5516 break;
5517 case Instruction::Add:
5518 case Instruction::Sub:
5519 case Instruction::Xor: {
5520 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5521 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5522
5523 const APInt *C;
5524 if (match(BO0->getOperand(1), m_APInt(C))) {
5525 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5526 if (C->isSignMask()) {
5527 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5528 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5529 }
5530
5531 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5532 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5533 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5534 NewPred = I.getSwappedPredicate(NewPred);
5535 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5536 }
5537 }
5538 break;
5539 }
5540 case Instruction::Mul: {
5541 if (!I.isEquality())
5542 break;
5543
5544 const APInt *C;
5545 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5546 !C->isOne()) {
5547 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5548 // Mask = -1 >> count-trailing-zeros(C).
5549 if (unsigned TZs = C->countr_zero()) {
5550 Constant *Mask = ConstantInt::get(
5551 BO0->getType(),
5552 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5553 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5554 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5555 return new ICmpInst(Pred, And1, And2);
5556 }
5557 }
5558 break;
5559 }
5560 case Instruction::UDiv:
5561 case Instruction::LShr:
5562 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5563 break;
5564 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5565
5566 case Instruction::SDiv:
5567 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5568 !BO0->isExact() || !BO1->isExact())
5569 break;
5570 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5571
5572 case Instruction::AShr:
5573 if (!BO0->isExact() || !BO1->isExact())
5574 break;
5575 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5576
5577 case Instruction::Shl: {
5578 bool NUW = Op0HasNUW && Op1HasNUW;
5579 bool NSW = Op0HasNSW && Op1HasNSW;
5580 if (!NUW && !NSW)
5581 break;
5582 if (!NSW && I.isSigned())
5583 break;
5584 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5585 }
5586 }
5587 }
5588
5589 if (BO0) {
5590 // Transform A & (L - 1) `ult` L --> L != 0
5591 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5592 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5593
5594 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5595 auto *Zero = Constant::getNullValue(BO0->getType());
5596 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5597 }
5598 }
5599
5600 // For unsigned predicates / eq / ne:
5601 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5602 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5603 if (!ICmpInst::isSigned(Pred)) {
5604 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5605 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5606 Constant::getNullValue(Op1->getType()));
5607 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5608 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5609 Constant::getNullValue(Op0->getType()), Op0);
5610 }
5611
5613 return replaceInstUsesWith(I, V);
5614
5615 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5616 return R;
5617
5619 return replaceInstUsesWith(I, V);
5620
5622 return replaceInstUsesWith(I, V);
5623
5624 return nullptr;
5625}
5626
5627/// Fold icmp Pred min|max(X, Y), Z.
5630 Value *Z, CmpPredicate Pred) {
5631 Value *X = MinMax->getLHS();
5632 Value *Y = MinMax->getRHS();
5633 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5634 return nullptr;
5635 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5636 // Revert the transform signed pred -> unsigned pred
5637 // TODO: We can flip the signedness of predicate if both operands of icmp
5638 // are negative.
5639 if (isKnownNonNegative(Z, SQ.getWithInstruction(&I)) &&
5640 isKnownNonNegative(MinMax, SQ.getWithInstruction(&I))) {
5642 } else
5643 return nullptr;
5644 }
5645 SimplifyQuery Q = SQ.getWithInstruction(&I);
5646 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5647 if (!Val)
5648 return std::nullopt;
5649 if (match(Val, m_One()))
5650 return true;
5651 if (match(Val, m_Zero()))
5652 return false;
5653 return std::nullopt;
5654 };
5655 // Remove samesign here since it is illegal to keep it when we speculatively
5656 // execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
5657 // cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
5658 // -46, -32)`. `X` is allowed to be non-negative here.
5659 Pred = Pred.dropSameSign();
5660 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5661 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5662 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5663 return nullptr;
5664 if (!CmpXZ.has_value()) {
5665 std::swap(X, Y);
5666 std::swap(CmpXZ, CmpYZ);
5667 }
5668
5669 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5670 if (CmpYZ.has_value())
5671 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5672 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5673 };
5674
5675 switch (Pred) {
5676 case ICmpInst::ICMP_EQ:
5677 case ICmpInst::ICMP_NE: {
5678 // If X == Z:
5679 // Expr Result
5680 // min(X, Y) == Z X <= Y
5681 // max(X, Y) == Z X >= Y
5682 // min(X, Y) != Z X > Y
5683 // max(X, Y) != Z X < Y
5684 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5685 ICmpInst::Predicate NewPred =
5686 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5687 if (Pred == ICmpInst::ICMP_NE)
5688 NewPred = ICmpInst::getInversePredicate(NewPred);
5689 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5690 }
5691 // Otherwise (X != Z):
5692 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5693 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5694 if (!MinMaxCmpXZ.has_value()) {
5695 std::swap(X, Y);
5696 std::swap(CmpXZ, CmpYZ);
5697 // Re-check pre-condition X != Z
5698 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5699 break;
5700 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5701 }
5702 if (!MinMaxCmpXZ.has_value())
5703 break;
5704 if (*MinMaxCmpXZ) {
5705 // Expr Fact Result
5706 // min(X, Y) == Z X < Z false
5707 // max(X, Y) == Z X > Z false
5708 // min(X, Y) != Z X < Z true
5709 // max(X, Y) != Z X > Z true
5710 return replaceInstUsesWith(
5711 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5712 } else {
5713 // Expr Fact Result
5714 // min(X, Y) == Z X > Z Y == Z
5715 // max(X, Y) == Z X < Z Y == Z
5716 // min(X, Y) != Z X > Z Y != Z
5717 // max(X, Y) != Z X < Z Y != Z
5718 return FoldIntoCmpYZ();
5719 }
5720 break;
5721 }
5722 case ICmpInst::ICMP_SLT:
5723 case ICmpInst::ICMP_ULT:
5724 case ICmpInst::ICMP_SLE:
5725 case ICmpInst::ICMP_ULE:
5726 case ICmpInst::ICMP_SGT:
5727 case ICmpInst::ICMP_UGT:
5728 case ICmpInst::ICMP_SGE:
5729 case ICmpInst::ICMP_UGE: {
5730 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5731 if (*CmpXZ) {
5732 if (IsSame) {
5733 // Expr Fact Result
5734 // min(X, Y) < Z X < Z true
5735 // min(X, Y) <= Z X <= Z true
5736 // max(X, Y) > Z X > Z true
5737 // max(X, Y) >= Z X >= Z true
5738 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5739 } else {
5740 // Expr Fact Result
5741 // max(X, Y) < Z X < Z Y < Z
5742 // max(X, Y) <= Z X <= Z Y <= Z
5743 // min(X, Y) > Z X > Z Y > Z
5744 // min(X, Y) >= Z X >= Z Y >= Z
5745 return FoldIntoCmpYZ();
5746 }
5747 } else {
5748 if (IsSame) {
5749 // Expr Fact Result
5750 // min(X, Y) < Z X >= Z Y < Z
5751 // min(X, Y) <= Z X > Z Y <= Z
5752 // max(X, Y) > Z X <= Z Y > Z
5753 // max(X, Y) >= Z X < Z Y >= Z
5754 return FoldIntoCmpYZ();
5755 } else {
5756 // Expr Fact Result
5757 // max(X, Y) < Z X >= Z false
5758 // max(X, Y) <= Z X > Z false
5759 // min(X, Y) > Z X <= Z false
5760 // min(X, Y) >= Z X < Z false
5761 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5762 }
5763 }
5764 break;
5765 }
5766 default:
5767 break;
5768 }
5769
5770 return nullptr;
5771}
5772
5773/// Match and fold patterns like:
5774/// icmp eq/ne X, min(max(X, Lo), Hi)
5775/// which represents a range check and can be repsented as a ConstantRange.
5776///
5777/// For icmp eq, build ConstantRange [Lo, Hi + 1) and convert to:
5778/// (X - Lo) u< (Hi + 1 - Lo)
5779/// For icmp ne, build ConstantRange [Hi + 1, Lo) and convert to:
5780/// (X - (Hi + 1)) u< (Lo - (Hi + 1))
5782 MinMaxIntrinsic *Min) {
5783 if (!I.isEquality() || !Min->hasOneUse() || !Min->isMin())
5784 return nullptr;
5785
5786 const APInt *Lo = nullptr, *Hi = nullptr;
5787 if (Min->isSigned()) {
5788 if (!match(Min->getLHS(), m_OneUse(m_SMax(m_Specific(X), m_APInt(Lo)))) ||
5789 !match(Min->getRHS(), m_APInt(Hi)) || !Lo->slt(*Hi))
5790 return nullptr;
5791 } else {
5792 if (!match(Min->getLHS(), m_OneUse(m_UMax(m_Specific(X), m_APInt(Lo)))) ||
5793 !match(Min->getRHS(), m_APInt(Hi)) || !Lo->ult(*Hi))
5794 return nullptr;
5795 }
5796
5799 APInt C, Offset;
5800 if (I.getPredicate() == ICmpInst::ICMP_EQ)
5801 CR.getEquivalentICmp(Pred, C, Offset);
5802 else
5803 CR.inverse().getEquivalentICmp(Pred, C, Offset);
5804
5805 if (!Offset.isZero())
5806 X = Builder.CreateAdd(X, ConstantInt::get(X->getType(), Offset));
5807
5808 return replaceInstUsesWith(
5809 I, Builder.CreateICmp(Pred, X, ConstantInt::get(X->getType(), C)));
5810}
5811
5812// Canonicalize checking for a power-of-2-or-zero value:
5814 InstCombiner::BuilderTy &Builder) {
5815 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5816 const CmpInst::Predicate Pred = I.getPredicate();
5817 Value *A = nullptr;
5818 bool CheckIs;
5819 if (I.isEquality()) {
5820 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5821 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5822 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5823 m_Deferred(A)))) ||
5824 !match(Op1, m_ZeroInt()))
5825 A = nullptr;
5826
5827 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5828 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5829 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5830 A = Op1;
5831 else if (match(Op1,
5833 A = Op0;
5834
5835 CheckIs = Pred == ICmpInst::ICMP_EQ;
5836 } else if (ICmpInst::isUnsigned(Pred)) {
5837 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5838 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5839
5840 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5842 m_Specific(Op1))))) {
5843 A = Op1;
5844 CheckIs = Pred == ICmpInst::ICMP_UGE;
5845 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5847 m_Specific(Op0))))) {
5848 A = Op0;
5849 CheckIs = Pred == ICmpInst::ICMP_ULE;
5850 }
5851 }
5852
5853 if (A) {
5854 Type *Ty = A->getType();
5855 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5856 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5857 ConstantInt::get(Ty, 2))
5858 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5859 ConstantInt::get(Ty, 1));
5860 }
5861
5862 return nullptr;
5863}
5864
5865/// Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
5866using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;
5868 bool AllowRecursion) {
5870 if (!Inst || !Inst->hasOneUse())
5871 return;
5872
5873 switch (Inst->getOpcode()) {
5874 case Instruction::Add:
5875 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(1));
5876 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(0));
5877 break;
5878 case Instruction::Sub:
5879 Offsets.emplace_back(Instruction::Add, Inst->getOperand(1));
5880 break;
5881 case Instruction::Xor:
5882 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(1));
5883 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(0));
5884 break;
5885 case Instruction::Select:
5886 if (AllowRecursion) {
5887 collectOffsetOp(Inst->getOperand(1), Offsets, /*AllowRecursion=*/false);
5888 collectOffsetOp(Inst->getOperand(2), Offsets, /*AllowRecursion=*/false);
5889 }
5890 break;
5891 default:
5892 break;
5893 }
5894}
5895
5897
5901
5903 return {OffsetKind::Invalid, nullptr, nullptr, nullptr};
5904 }
5906 return {OffsetKind::Value, V, nullptr, nullptr};
5907 }
5908 static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV) {
5909 return {OffsetKind::Select, Cond, TrueV, FalseV};
5910 }
5911 bool isValid() const { return Kind != OffsetKind::Invalid; }
5913 switch (Kind) {
5915 llvm_unreachable("Invalid offset result");
5916 case OffsetKind::Value:
5917 return V0;
5918 case OffsetKind::Select:
5919 return Builder.CreateSelect(V0, V1, V2);
5920 }
5921 llvm_unreachable("Unknown OffsetKind enum");
5922 }
5923};
5924
5925/// Offset both sides of an equality icmp to see if we can save some
5926/// instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.
5927/// Note: This operation should not introduce poison.
5929 InstCombiner::BuilderTy &Builder,
5930 const SimplifyQuery &SQ) {
5931 assert(I.isEquality() && "Expected an equality icmp");
5932 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5933 if (!Op0->getType()->isIntOrIntVectorTy())
5934 return nullptr;
5935
5936 SmallVector<OffsetOp, 4> OffsetOps;
5937 collectOffsetOp(Op0, OffsetOps, /*AllowRecursion=*/true);
5938 collectOffsetOp(Op1, OffsetOps, /*AllowRecursion=*/true);
5939
5940 auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {
5941 Value *Simplified = simplifyBinOp(BinOpc, V, RHS, SQ);
5942 // Avoid infinite loops by checking if RHS is an identity for the BinOp.
5943 if (!Simplified || Simplified == V)
5944 return nullptr;
5945 // Reject constant expressions as they don't simplify things.
5946 if (isa<Constant>(Simplified) && !match(Simplified, m_ImmConstant()))
5947 return nullptr;
5948 // Check if the transformation introduces poison.
5949 return impliesPoison(RHS, V) ? Simplified : nullptr;
5950 };
5951
5952 auto ApplyOffset = [&](Value *V, unsigned BinOpc,
5953 Value *RHS) -> OffsetResult {
5954 if (auto *Sel = dyn_cast<SelectInst>(V)) {
5955 if (!Sel->hasOneUse())
5956 return OffsetResult::invalid();
5957 Value *TrueVal = ApplyOffsetImpl(Sel->getTrueValue(), BinOpc, RHS);
5958 if (!TrueVal)
5959 return OffsetResult::invalid();
5960 Value *FalseVal = ApplyOffsetImpl(Sel->getFalseValue(), BinOpc, RHS);
5961 if (!FalseVal)
5962 return OffsetResult::invalid();
5963 return OffsetResult::select(Sel->getCondition(), TrueVal, FalseVal);
5964 }
5965 if (Value *Simplified = ApplyOffsetImpl(V, BinOpc, RHS))
5966 return OffsetResult::value(Simplified);
5967 return OffsetResult::invalid();
5968 };
5969
5970 for (auto [BinOp, RHS] : OffsetOps) {
5971 auto BinOpc = static_cast<unsigned>(BinOp);
5972
5973 auto Op0Result = ApplyOffset(Op0, BinOpc, RHS);
5974 if (!Op0Result.isValid())
5975 continue;
5976 auto Op1Result = ApplyOffset(Op1, BinOpc, RHS);
5977 if (!Op1Result.isValid())
5978 continue;
5979
5980 Value *NewLHS = Op0Result.materialize(Builder);
5981 Value *NewRHS = Op1Result.materialize(Builder);
5982 return new ICmpInst(I.getPredicate(), NewLHS, NewRHS);
5983 }
5984
5985 return nullptr;
5986}
5987
5989 if (!I.isEquality())
5990 return nullptr;
5991
5992 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5993 const CmpInst::Predicate Pred = I.getPredicate();
5994 Value *A, *B, *C, *D;
5995 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5996 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5997 Value *OtherVal = A == Op1 ? B : A;
5998 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5999 }
6000
6001 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
6002 // A^c1 == C^c2 --> A == C^(c1^c2)
6003 ConstantInt *C1, *C2;
6004 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
6005 Op1->hasOneUse()) {
6006 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
6007 Value *Xor = Builder.CreateXor(C, NC);
6008 return new ICmpInst(Pred, A, Xor);
6009 }
6010
6011 // A^B == A^D -> B == D
6012 if (A == C)
6013 return new ICmpInst(Pred, B, D);
6014 if (A == D)
6015 return new ICmpInst(Pred, B, C);
6016 if (B == C)
6017 return new ICmpInst(Pred, A, D);
6018 if (B == D)
6019 return new ICmpInst(Pred, A, C);
6020 }
6021 }
6022
6023 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
6024 // A == (A^B) -> B == 0
6025 Value *OtherVal = A == Op0 ? B : A;
6026 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
6027 }
6028
6029 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
6030 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
6031 match(Op1, m_And(m_Value(C), m_Value(D)))) {
6032 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
6033
6034 if (A == C) {
6035 X = B;
6036 Y = D;
6037 Z = A;
6038 } else if (A == D) {
6039 X = B;
6040 Y = C;
6041 Z = A;
6042 } else if (B == C) {
6043 X = A;
6044 Y = D;
6045 Z = B;
6046 } else if (B == D) {
6047 X = A;
6048 Y = C;
6049 Z = B;
6050 }
6051
6052 if (X) {
6053 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
6054 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
6055 // instructions.
6056 const APInt *C0, *C1;
6057 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
6058 (*C0 ^ *C1).isNegatedPowerOf2();
6059
6060 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
6061 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
6062 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
6063 int UseCnt =
6064 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
6065 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
6066 if (XorIsNegP2 || UseCnt >= 2) {
6067 // Build (X^Y) & Z
6068 Op1 = Builder.CreateXor(X, Y);
6069 Op1 = Builder.CreateAnd(Op1, Z);
6070 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
6071 }
6072 }
6073 }
6074
6075 {
6076 // Similar to above, but specialized for constant because invert is needed:
6077 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
6078 Value *X, *Y;
6079 Constant *C;
6080 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
6081 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
6082 Value *Xor = Builder.CreateXor(X, Y);
6083 Value *And = Builder.CreateAnd(Xor, ConstantExpr::getNot(C));
6084 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
6085 }
6086 }
6087
6088 if (match(Op1, m_ZExt(m_Value(A))) &&
6089 (Op0->hasOneUse() || Op1->hasOneUse())) {
6090 // (B & (Pow2C-1)) == zext A --> A == trunc B
6091 // (B & (Pow2C-1)) != zext A --> A != trunc B
6092 const APInt *MaskC;
6093 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
6094 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
6095 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
6096 }
6097
6098 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
6099 // For lshr and ashr pairs.
6100 const APInt *AP1, *AP2;
6101 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
6102 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
6103 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
6104 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
6105 if (*AP1 != *AP2)
6106 return nullptr;
6107 unsigned TypeBits = AP1->getBitWidth();
6108 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
6109 if (ShAmt < TypeBits && ShAmt != 0) {
6110 ICmpInst::Predicate NewPred =
6112 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
6113 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
6114 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
6115 }
6116 }
6117
6118 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
6119 ConstantInt *Cst1;
6120 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
6121 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
6122 unsigned TypeBits = Cst1->getBitWidth();
6123 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
6124 if (ShAmt < TypeBits && ShAmt != 0) {
6125 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
6126 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
6127 Value *And =
6128 Builder.CreateAnd(Xor, Builder.getInt(AndVal), I.getName() + ".mask");
6129 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
6130 }
6131 }
6132
6133 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
6134 // "icmp (and X, mask), cst"
6135 uint64_t ShAmt = 0;
6136 if (Op0->hasOneUse() &&
6137 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
6138 match(Op1, m_ConstantInt(Cst1)) &&
6139 // Only do this when A has multiple uses. This is most important to do
6140 // when it exposes other optimizations.
6141 !A->hasOneUse()) {
6142 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
6143
6144 if (ShAmt < ASize) {
6145 APInt MaskV =
6147 MaskV <<= ShAmt;
6148
6149 APInt CmpV = Cst1->getValue().zext(ASize);
6150 CmpV <<= ShAmt;
6151
6152 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
6153 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
6154 }
6155 }
6156
6158 return ICmp;
6159
6160 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks
6161 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s
6162 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a
6163 // few steps of instcombine.
6164 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6165 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
6167 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
6168 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
6170 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
6171 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
6173 Add, ConstantInt::get(A->getType(), C.shl(1)));
6174 }
6175
6176 // Canonicalize:
6177 // Assume B_Pow2 != 0
6178 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
6179 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
6180 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
6181 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, &I))
6182 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6184
6185 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
6186 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, &I))
6187 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
6188 ConstantInt::getNullValue(Op1->getType()));
6189
6190 // Canonicalize:
6191 // icmp eq/ne X, OneUse(rotate-right(X))
6192 // -> icmp eq/ne X, rotate-left(X)
6193 // We generally try to convert rotate-right -> rotate-left, this just
6194 // canonicalizes another case.
6195 if (match(&I, m_c_ICmp(m_Value(A),
6197 m_Deferred(A), m_Deferred(A), m_Value(B))))))
6198 return new ICmpInst(
6199 Pred, A,
6200 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
6201
6202 // Canonicalize:
6203 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
6204 Constant *Cst;
6207 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
6208
6209 {
6210 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6211 auto m_Matcher =
6214 m_Sub(m_Value(B), m_Deferred(A)));
6215 std::optional<bool> IsZero = std::nullopt;
6216 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),
6217 m_Deferred(A))))
6218 IsZero = false;
6219 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6220 else if (match(&I,
6221 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))
6222 IsZero = true;
6223
6224 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, &I))
6225 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6226 // -> (icmp eq/ne (and X, P2), 0)
6227 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6228 // -> (icmp eq/ne (and X, P2), P2)
6229 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
6230 *IsZero ? A
6231 : ConstantInt::getNullValue(A->getType()));
6232 }
6233
6234 if (auto *Res = foldICmpEqualityWithOffset(
6235 I, Builder, getSimplifyQuery().getWithInstruction(&I)))
6236 return Res;
6237
6238 return nullptr;
6239}
6240
6242 ICmpInst::Predicate Pred = ICmp.getPredicate();
6243 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
6244
6245 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
6246 // The trunc masks high bits while the compare may effectively mask low bits.
6247 Value *X;
6248 const APInt *C;
6249 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
6250 return nullptr;
6251
6252 // This matches patterns corresponding to tests of the signbit as well as:
6253 // (trunc X) pred C2 --> (X & Mask) == C
6254 if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*WithTrunc=*/true,
6255 /*AllowNonZeroC=*/true)) {
6256 Value *And = Builder.CreateAnd(Res->X, Res->Mask);
6257 Constant *C = ConstantInt::get(Res->X->getType(), Res->C);
6258 return new ICmpInst(Res->Pred, And, C);
6259 }
6260
6261 unsigned SrcBits = X->getType()->getScalarSizeInBits();
6262 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
6263 if (II->getIntrinsicID() == Intrinsic::cttz ||
6264 II->getIntrinsicID() == Intrinsic::ctlz) {
6265 unsigned MaxRet = SrcBits;
6266 // If the "is_zero_poison" argument is set, then we know at least
6267 // one bit is set in the input, so the result is always at least one
6268 // less than the full bitwidth of that input.
6269 if (match(II->getArgOperand(1), m_One()))
6270 MaxRet--;
6271
6272 // Make sure the destination is wide enough to hold the largest output of
6273 // the intrinsic.
6274 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
6275 if (Instruction *I =
6276 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
6277 return I;
6278 }
6279 }
6280
6281 return nullptr;
6282}
6283
6285 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
6286 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
6287 Value *X;
6288 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
6289 return nullptr;
6290
6291 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
6292 bool IsSignedCmp = ICmp.isSigned();
6293
6294 // icmp Pred (ext X), (ext Y)
6295 Value *Y;
6296 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
6297 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
6298 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
6299
6300 if (IsZext0 != IsZext1) {
6301 // If X and Y and both i1
6302 // (icmp eq/ne (zext X) (sext Y))
6303 // eq -> (icmp eq (or X, Y), 0)
6304 // ne -> (icmp ne (or X, Y), 0)
6305 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
6306 Y->getType()->isIntOrIntVectorTy(1))
6307 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
6308 Constant::getNullValue(X->getType()));
6309
6310 // If we have mismatched casts and zext has the nneg flag, we can
6311 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
6312
6313 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
6314 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
6315
6316 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
6317 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
6318
6319 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
6320 IsSignedExt = true;
6321 else
6322 return nullptr;
6323 }
6324
6325 // Not an extension from the same type?
6326 Type *XTy = X->getType(), *YTy = Y->getType();
6327 if (XTy != YTy) {
6328 // One of the casts must have one use because we are creating a new cast.
6329 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
6330 return nullptr;
6331 // Extend the narrower operand to the type of the wider operand.
6332 CastInst::CastOps CastOpcode =
6333 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
6334 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
6335 X = Builder.CreateCast(CastOpcode, X, YTy);
6336 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6337 Y = Builder.CreateCast(CastOpcode, Y, XTy);
6338 else
6339 return nullptr;
6340 }
6341
6342 // (zext X) == (zext Y) --> X == Y
6343 // (sext X) == (sext Y) --> X == Y
6344 if (ICmp.isEquality())
6345 return new ICmpInst(ICmp.getPredicate(), X, Y);
6346
6347 // A signed comparison of sign extended values simplifies into a
6348 // signed comparison.
6349 if (IsSignedCmp && IsSignedExt)
6350 return new ICmpInst(ICmp.getPredicate(), X, Y);
6351
6352 // The other three cases all fold into an unsigned comparison.
6353 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6354 }
6355
6356 // Below here, we are only folding a compare with constant.
6357 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
6358 if (!C)
6359 return nullptr;
6360
6361 // If a lossless truncate is possible...
6362 Type *SrcTy = CastOp0->getSrcTy();
6363 Constant *Res = getLosslessInvCast(C, SrcTy, CastOp0->getOpcode(), DL);
6364 if (Res) {
6365 if (ICmp.isEquality())
6366 return new ICmpInst(ICmp.getPredicate(), X, Res);
6367
6368 // A signed comparison of sign extended values simplifies into a
6369 // signed comparison.
6370 if (IsSignedExt && IsSignedCmp)
6371 return new ICmpInst(ICmp.getPredicate(), X, Res);
6372
6373 // The other three cases all fold into an unsigned comparison.
6374 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6375 }
6376
6377 // The re-extended constant changed, partly changed (in the case of a vector),
6378 // or could not be determined to be equal (in the case of a constant
6379 // expression), so the constant cannot be represented in the shorter type.
6380 // All the cases that fold to true or false will have already been handled
6381 // by simplifyICmpInst, so only deal with the tricky case.
6382 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
6383 return nullptr;
6384
6385 // Is source op positive?
6386 // icmp ult (sext X), C --> icmp sgt X, -1
6387 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6389
6390 // Is source op negative?
6391 // icmp ugt (sext X), C --> icmp slt X, 0
6392 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6394}
6395
6396/// Handle icmp (cast x), (cast or constant).
6398 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6399 // icmp compares only pointer's value.
6400 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6401 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
6402 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
6403 if (SimplifiedOp0 || SimplifiedOp1)
6404 return new ICmpInst(ICmp.getPredicate(),
6405 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
6406 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
6407
6408 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
6409 if (!CastOp0)
6410 return nullptr;
6411 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
6412 return nullptr;
6413
6414 Value *Op0Src = CastOp0->getOperand(0);
6415 Type *SrcTy = CastOp0->getSrcTy();
6416 Type *DestTy = CastOp0->getDestTy();
6417
6418 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6419 // integer type is the same size as the pointer type.
6420 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6421 if (isa<VectorType>(PtrTy)) {
6422 PtrTy = cast<VectorType>(PtrTy)->getElementType();
6423 IntTy = cast<VectorType>(IntTy)->getElementType();
6424 }
6425 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
6426 };
6427 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6428 CompatibleSizes(SrcTy, DestTy)) {
6429 Value *NewOp1 = nullptr;
6430 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
6431 Value *PtrSrc = PtrToIntOp1->getOperand(0);
6432 if (PtrSrc->getType() == Op0Src->getType())
6433 NewOp1 = PtrToIntOp1->getOperand(0);
6434 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6435 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
6436 }
6437
6438 if (NewOp1)
6439 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6440 }
6441
6442 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6443 if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6444 CompatibleSizes(DestTy, SrcTy)) {
6445 Value *NewOp1 = nullptr;
6446 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
6447 Value *IntSrc = IntToPtrOp1->getOperand(0);
6448 if (IntSrc->getType() == Op0Src->getType())
6449 NewOp1 = IntToPtrOp1->getOperand(0);
6450 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6451 NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
6452 }
6453
6454 if (NewOp1)
6455 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6456 }
6457
6458 if (Instruction *R = foldICmpWithTrunc(ICmp))
6459 return R;
6460
6461 return foldICmpWithZextOrSext(ICmp);
6462}
6463
6465 bool IsSigned) {
6466 switch (BinaryOp) {
6467 default:
6468 llvm_unreachable("Unsupported binary op");
6469 case Instruction::Add:
6470 case Instruction::Sub:
6471 return match(RHS, m_Zero());
6472 case Instruction::Mul:
6473 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
6474 match(RHS, m_One());
6475 }
6476}
6477
6480 bool IsSigned, Value *LHS, Value *RHS,
6481 Instruction *CxtI) const {
6482 switch (BinaryOp) {
6483 default:
6484 llvm_unreachable("Unsupported binary op");
6485 case Instruction::Add:
6486 if (IsSigned)
6487 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6488 else
6489 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6490 case Instruction::Sub:
6491 if (IsSigned)
6492 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6493 else
6494 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6495 case Instruction::Mul:
6496 if (IsSigned)
6497 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6498 else
6499 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6500 }
6501}
6502
6503bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6504 bool IsSigned, Value *LHS,
6505 Value *RHS, Instruction &OrigI,
6506 Value *&Result,
6507 Constant *&Overflow) {
6508 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
6509 std::swap(LHS, RHS);
6510
6511 // If the overflow check was an add followed by a compare, the insertion point
6512 // may be pointing to the compare. We want to insert the new instructions
6513 // before the add in case there are uses of the add between the add and the
6514 // compare.
6515 Builder.SetInsertPoint(&OrigI);
6516
6517 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
6518 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
6519 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
6520
6521 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6522 Result = LHS;
6523 Overflow = ConstantInt::getFalse(OverflowTy);
6524 return true;
6525 }
6526
6527 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
6529 return false;
6532 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6533 Result->takeName(&OrigI);
6534 Overflow = ConstantInt::getTrue(OverflowTy);
6535 return true;
6537 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6538 Result->takeName(&OrigI);
6539 Overflow = ConstantInt::getFalse(OverflowTy);
6540 if (auto *Inst = dyn_cast<Instruction>(Result)) {
6541 if (IsSigned)
6542 Inst->setHasNoSignedWrap();
6543 else
6544 Inst->setHasNoUnsignedWrap();
6545 }
6546 return true;
6547 }
6548
6549 llvm_unreachable("Unexpected overflow result");
6550}
6551
6552/// Recognize and process idiom involving test for multiplication
6553/// overflow.
6554///
6555/// The caller has matched a pattern of the form:
6556/// I = cmp u (mul(zext A, zext B), V
6557/// The function checks if this is a test for overflow and if so replaces
6558/// multiplication with call to 'mul.with.overflow' intrinsic.
6559///
6560/// \param I Compare instruction.
6561/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6562/// the compare instruction. Must be of integer type.
6563/// \param OtherVal The other argument of compare instruction.
6564/// \returns Instruction which must replace the compare instruction, NULL if no
6565/// replacement required.
6567 const APInt *OtherVal,
6568 InstCombinerImpl &IC) {
6569 // Don't bother doing this transformation for pointers, don't do it for
6570 // vectors.
6571 if (!isa<IntegerType>(MulVal->getType()))
6572 return nullptr;
6573
6574 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6575 if (!MulInstr)
6576 return nullptr;
6577 assert(MulInstr->getOpcode() == Instruction::Mul);
6578
6579 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6580 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6581 assert(LHS->getOpcode() == Instruction::ZExt);
6582 assert(RHS->getOpcode() == Instruction::ZExt);
6583 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6584
6585 // Calculate type and width of the result produced by mul.with.overflow.
6586 Type *TyA = A->getType(), *TyB = B->getType();
6587 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6588 WidthB = TyB->getPrimitiveSizeInBits();
6589 unsigned MulWidth;
6590 Type *MulType;
6591 if (WidthB > WidthA) {
6592 MulWidth = WidthB;
6593 MulType = TyB;
6594 } else {
6595 MulWidth = WidthA;
6596 MulType = TyA;
6597 }
6598
6599 // In order to replace the original mul with a narrower mul.with.overflow,
6600 // all uses must ignore upper bits of the product. The number of used low
6601 // bits must be not greater than the width of mul.with.overflow.
6602 if (MulVal->hasNUsesOrMore(2))
6603 for (User *U : MulVal->users()) {
6604 if (U == &I)
6605 continue;
6606 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6607 // Check if truncation ignores bits above MulWidth.
6608 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6609 if (TruncWidth > MulWidth)
6610 return nullptr;
6611 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6612 // Check if AND ignores bits above MulWidth.
6613 if (BO->getOpcode() != Instruction::And)
6614 return nullptr;
6615 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6616 const APInt &CVal = CI->getValue();
6617 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6618 return nullptr;
6619 } else {
6620 // In this case we could have the operand of the binary operation
6621 // being defined in another block, and performing the replacement
6622 // could break the dominance relation.
6623 return nullptr;
6624 }
6625 } else {
6626 // Other uses prohibit this transformation.
6627 return nullptr;
6628 }
6629 }
6630
6631 // Recognize patterns
6632 switch (I.getPredicate()) {
6633 case ICmpInst::ICMP_UGT: {
6634 // Recognize pattern:
6635 // mulval = mul(zext A, zext B)
6636 // cmp ugt mulval, max
6637 APInt MaxVal = APInt::getMaxValue(MulWidth);
6638 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6639 if (MaxVal.eq(*OtherVal))
6640 break; // Recognized
6641 return nullptr;
6642 }
6643
6644 case ICmpInst::ICMP_ULT: {
6645 // Recognize pattern:
6646 // mulval = mul(zext A, zext B)
6647 // cmp ule mulval, max + 1
6648 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6649 if (MaxVal.eq(*OtherVal))
6650 break; // Recognized
6651 return nullptr;
6652 }
6653
6654 default:
6655 return nullptr;
6656 }
6657
6658 InstCombiner::BuilderTy &Builder = IC.Builder;
6659 Builder.SetInsertPoint(MulInstr);
6660
6661 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6662 Value *MulA = A, *MulB = B;
6663 if (WidthA < MulWidth)
6664 MulA = Builder.CreateZExt(A, MulType);
6665 if (WidthB < MulWidth)
6666 MulB = Builder.CreateZExt(B, MulType);
6667 CallInst *Call =
6668 Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, MulType,
6669 {MulA, MulB}, /*FMFSource=*/nullptr, "umul");
6670 IC.addToWorklist(MulInstr);
6671
6672 // If there are uses of mul result other than the comparison, we know that
6673 // they are truncation or binary AND. Change them to use result of
6674 // mul.with.overflow and adjust properly mask/size.
6675 if (MulVal->hasNUsesOrMore(2)) {
6676 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6677 for (User *U : make_early_inc_range(MulVal->users())) {
6678 if (U == &I)
6679 continue;
6680 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6681 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6682 IC.replaceInstUsesWith(*TI, Mul);
6683 else
6684 TI->setOperand(0, Mul);
6685 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6686 assert(BO->getOpcode() == Instruction::And);
6687 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6688 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6689 APInt ShortMask = CI->getValue().trunc(MulWidth);
6690 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6691 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6692 IC.replaceInstUsesWith(*BO, Zext);
6693 } else {
6694 llvm_unreachable("Unexpected Binary operation");
6695 }
6697 }
6698 }
6699
6700 // The original icmp gets replaced with the overflow value, maybe inverted
6701 // depending on predicate.
6702 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6703 Value *Res = Builder.CreateExtractValue(Call, 1);
6704 return BinaryOperator::CreateNot(Res);
6705 }
6706
6707 return ExtractValueInst::Create(Call, 1);
6708}
6709
6710/// When performing a comparison against a constant, it is possible that not all
6711/// the bits in the LHS are demanded. This helper method computes the mask that
6712/// IS demanded.
6714 const APInt *RHS;
6715 if (!match(I.getOperand(1), m_APInt(RHS)))
6717
6718 // If this is a normal comparison, it demands all bits. If it is a sign bit
6719 // comparison, it only demands the sign bit.
6720 bool UnusedBit;
6721 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6723
6724 switch (I.getPredicate()) {
6725 // For a UGT comparison, we don't care about any bits that
6726 // correspond to the trailing ones of the comparand. The value of these
6727 // bits doesn't impact the outcome of the comparison, because any value
6728 // greater than the RHS must differ in a bit higher than these due to carry.
6729 case ICmpInst::ICMP_UGT:
6730 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6731
6732 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6733 // Any value less than the RHS must differ in a higher bit because of carries.
6734 case ICmpInst::ICMP_ULT:
6735 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6736
6737 default:
6739 }
6740}
6741
6742/// Check that one use is in the same block as the definition and all
6743/// other uses are in blocks dominated by a given block.
6744///
6745/// \param DI Definition
6746/// \param UI Use
6747/// \param DB Block that must dominate all uses of \p DI outside
6748/// the parent block
6749/// \return true when \p UI is the only use of \p DI in the parent block
6750/// and all other uses of \p DI are in blocks dominated by \p DB.
6751///
6753 const Instruction *UI,
6754 const BasicBlock *DB) const {
6755 assert(DI && UI && "Instruction not defined\n");
6756 // Ignore incomplete definitions.
6757 if (!DI->getParent())
6758 return false;
6759 // DI and UI must be in the same block.
6760 if (DI->getParent() != UI->getParent())
6761 return false;
6762 // Protect from self-referencing blocks.
6763 if (DI->getParent() == DB)
6764 return false;
6765 for (const User *U : DI->users()) {
6766 auto *Usr = cast<Instruction>(U);
6767 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6768 return false;
6769 }
6770 return true;
6771}
6772
6773/// Return true when the instruction sequence within a block is select-cmp-br.
6775 const BasicBlock *BB = SI->getParent();
6776 if (!BB)
6777 return false;
6779 if (!BI || BI->getNumSuccessors() != 2)
6780 return false;
6781 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6782 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6783 return false;
6784 return true;
6785}
6786
6787/// True when a select result is replaced by one of its operands
6788/// in select-icmp sequence. This will eventually result in the elimination
6789/// of the select.
6790///
6791/// \param SI Select instruction
6792/// \param Icmp Compare instruction
6793/// \param SIOpd Operand that replaces the select
6794///
6795/// Notes:
6796/// - The replacement is global and requires dominator information
6797/// - The caller is responsible for the actual replacement
6798///
6799/// Example:
6800///
6801/// entry:
6802/// %4 = select i1 %3, %C* %0, %C* null
6803/// %5 = icmp eq %C* %4, null
6804/// br i1 %5, label %9, label %7
6805/// ...
6806/// ; <label>:7 ; preds = %entry
6807/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6808/// ...
6809///
6810/// can be transformed to
6811///
6812/// %5 = icmp eq %C* %0, null
6813/// %6 = select i1 %3, i1 %5, i1 true
6814/// br i1 %6, label %9, label %7
6815/// ...
6816/// ; <label>:7 ; preds = %entry
6817/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6818///
6819/// Similar when the first operand of the select is a constant or/and
6820/// the compare is for not equal rather than equal.
6821///
6822/// NOTE: The function is only called when the select and compare constants
6823/// are equal, the optimization can work only for EQ predicates. This is not a
6824/// major restriction since a NE compare should be 'normalized' to an equal
6825/// compare, which usually happens in the combiner and test case
6826/// select-cmp-br.ll checks for it.
6828 const ICmpInst *Icmp,
6829 const unsigned SIOpd) {
6830 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6832 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6833 // The check for the single predecessor is not the best that can be
6834 // done. But it protects efficiently against cases like when SI's
6835 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6836 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6837 // replaced can be reached on either path. So the uniqueness check
6838 // guarantees that the path all uses of SI (outside SI's parent) are on
6839 // is disjoint from all other paths out of SI. But that information
6840 // is more expensive to compute, and the trade-off here is in favor
6841 // of compile-time. It should also be noticed that we check for a single
6842 // predecessor and not only uniqueness. This to handle the situation when
6843 // Succ and Succ1 points to the same basic block.
6844 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6845 NumSel++;
6846 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6847 return true;
6848 }
6849 }
6850 return false;
6851}
6852
6853/// Try to fold the comparison based on range information we can get by checking
6854/// whether bits are known to be zero or one in the inputs.
6856 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6857 Type *Ty = Op0->getType();
6858 ICmpInst::Predicate Pred = I.getPredicate();
6859
6860 // Get scalar or pointer size.
6861 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6862 ? Ty->getScalarSizeInBits()
6863 : DL.getPointerTypeSizeInBits(Ty->getScalarType());
6864
6865 if (!BitWidth)
6866 return nullptr;
6867
6868 KnownBits Op0Known(BitWidth);
6869 KnownBits Op1Known(BitWidth);
6870
6871 {
6872 // Don't use dominating conditions when folding icmp using known bits. This
6873 // may convert signed into unsigned predicates in ways that other passes
6874 // (especially IndVarSimplify) may not be able to reliably undo.
6875 SimplifyQuery Q = SQ.getWithoutDomCondCache().getWithInstruction(&I);
6877 Op0Known, Q))
6878 return &I;
6879
6880 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnes(BitWidth), Op1Known, Q))
6881 return &I;
6882 }
6883
6884 if (!isa<Constant>(Op0) && Op0Known.isConstant())
6885 return new ICmpInst(
6886 Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1);
6887 if (!isa<Constant>(Op1) && Op1Known.isConstant())
6888 return new ICmpInst(
6889 Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Known.getConstant()));
6890
6891 if (std::optional<bool> Res = ICmpInst::compare(Op0Known, Op1Known, Pred))
6892 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *Res));
6893
6894 // Given the known and unknown bits, compute a range that the LHS could be
6895 // in. Compute the Min, Max and RHS values based on the known bits. For the
6896 // EQ and NE we use unsigned values.
6897 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6898 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6899 if (I.isSigned()) {
6900 Op0Min = Op0Known.getSignedMinValue();
6901 Op0Max = Op0Known.getSignedMaxValue();
6902 Op1Min = Op1Known.getSignedMinValue();
6903 Op1Max = Op1Known.getSignedMaxValue();
6904 } else {
6905 Op0Min = Op0Known.getMinValue();
6906 Op0Max = Op0Known.getMaxValue();
6907 Op1Min = Op1Known.getMinValue();
6908 Op1Max = Op1Known.getMaxValue();
6909 }
6910
6911 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6912 // min/max canonical compare with some other compare. That could lead to
6913 // conflict with select canonicalization and infinite looping.
6914 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6915 auto isMinMaxCmp = [&](Instruction &Cmp) {
6916 if (!Cmp.hasOneUse())
6917 return false;
6918 Value *A, *B;
6919 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6921 return false;
6922 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6923 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6924 };
6925 if (!isMinMaxCmp(I)) {
6926 switch (Pred) {
6927 default:
6928 break;
6929 case ICmpInst::ICMP_ULT: {
6930 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6931 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6932 const APInt *CmpC;
6933 if (match(Op1, m_APInt(CmpC))) {
6934 // A <u C -> A == C-1 if min(A)+1 == C
6935 if (*CmpC == Op0Min + 1)
6936 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6937 ConstantInt::get(Op1->getType(), *CmpC - 1));
6938 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6939 // exceeds the log2 of C.
6940 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6941 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6942 Constant::getNullValue(Op1->getType()));
6943 }
6944 break;
6945 }
6946 case ICmpInst::ICMP_UGT: {
6947 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6948 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6949 const APInt *CmpC;
6950 if (match(Op1, m_APInt(CmpC))) {
6951 // A >u C -> A == C+1 if max(a)-1 == C
6952 if (*CmpC == Op0Max - 1)
6953 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6954 ConstantInt::get(Op1->getType(), *CmpC + 1));
6955 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6956 // exceeds the log2 of C.
6957 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6958 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6959 Constant::getNullValue(Op1->getType()));
6960 }
6961 break;
6962 }
6963 case ICmpInst::ICMP_SLT: {
6964 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6965 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6966 const APInt *CmpC;
6967 if (match(Op1, m_APInt(CmpC))) {
6968 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6969 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6970 ConstantInt::get(Op1->getType(), *CmpC - 1));
6971 }
6972 break;
6973 }
6974 case ICmpInst::ICMP_SGT: {
6975 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6976 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6977 const APInt *CmpC;
6978 if (match(Op1, m_APInt(CmpC))) {
6979 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6980 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6981 ConstantInt::get(Op1->getType(), *CmpC + 1));
6982 }
6983 break;
6984 }
6985 }
6986 }
6987
6988 // Based on the range information we know about the LHS, see if we can
6989 // simplify this comparison. For example, (x&4) < 8 is always true.
6990 switch (Pred) {
6991 default:
6992 break;
6993 case ICmpInst::ICMP_EQ:
6994 case ICmpInst::ICMP_NE: {
6995 // If all bits are known zero except for one, then we know at most one bit
6996 // is set. If the comparison is against zero, then this is a check to see if
6997 // *that* bit is set.
6998 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
6999 if (Op1Known.isZero()) {
7000 // If the LHS is an AND with the same constant, look through it.
7001 Value *LHS = nullptr;
7002 const APInt *LHSC;
7003 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
7004 *LHSC != Op0KnownZeroInverted)
7005 LHS = Op0;
7006
7007 Value *X;
7008 const APInt *C1;
7009 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
7010 Type *XTy = X->getType();
7011 unsigned Log2C1 = C1->countr_zero();
7012 APInt C2 = Op0KnownZeroInverted;
7013 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
7014 if (C2Pow2.isPowerOf2()) {
7015 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
7016 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
7017 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
7018 unsigned Log2C2 = C2Pow2.countr_zero();
7019 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
7020 auto NewPred =
7022 return new ICmpInst(NewPred, X, CmpC);
7023 }
7024 }
7025 }
7026
7027 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
7028 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
7029 (Op0Known & Op1Known) == Op0Known)
7030 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
7031 ConstantInt::getNullValue(Op1->getType()));
7032 break;
7033 }
7034 case ICmpInst::ICMP_SGE:
7035 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
7036 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7037 break;
7038 case ICmpInst::ICMP_SLE:
7039 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
7040 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7041 break;
7042 case ICmpInst::ICMP_UGE:
7043 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
7044 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7045 break;
7046 case ICmpInst::ICMP_ULE:
7047 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
7048 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7049 break;
7050 }
7051
7052 // Turn a signed comparison into an unsigned one if both operands are known to
7053 // have the same sign. Set samesign if possible (except for equality
7054 // predicates).
7055 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&
7056 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
7057 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {
7058 I.setPredicate(I.getUnsignedPredicate());
7059 I.setSameSign();
7060 return &I;
7061 }
7062
7063 return nullptr;
7064}
7065
7066/// If one operand of an icmp is effectively a bool (value range of {0,1}),
7067/// then try to reduce patterns based on that limit.
7069 Value *X, *Y;
7070 CmpPredicate Pred;
7071
7072 // X must be 0 and bool must be true for "ULT":
7073 // X <u (zext i1 Y) --> (X == 0) & Y
7074 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
7075 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
7076 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
7077
7078 // X must be 0 or bool must be true for "ULE":
7079 // X <=u (sext i1 Y) --> (X == 0) | Y
7080 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
7081 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
7082 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
7083
7084 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
7085 CmpPredicate Pred1, Pred2;
7086 const APInt *C;
7087 Instruction *ExtI;
7088 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
7091 m_APInt(C)))))) &&
7092 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
7093 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
7094 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
7095 auto CreateRangeCheck = [&] {
7096 Value *CmpV1 =
7097 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
7098 Value *CmpV2 = Builder.CreateICmp(
7099 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
7101 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
7102 CmpV1, CmpV2);
7103 };
7104 if (C->isZero()) {
7105 if (Pred2 == ICmpInst::ICMP_EQ) {
7106 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
7107 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
7108 return replaceInstUsesWith(
7109 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
7110 } else if (!IsSExt || HasOneUse) {
7111 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
7112 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
7113 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
7114 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X != -1
7115 return CreateRangeCheck();
7116 }
7117 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
7118 if (Pred2 == ICmpInst::ICMP_NE) {
7119 // icmp eq X, (zext (icmp ne X, 1)) --> false
7120 // icmp ne X, (zext (icmp ne X, 1)) --> true
7121 // icmp eq X, (sext (icmp ne X, -1)) --> false
7122 // icmp ne X, (sext (icmp ne X, -1)) --> true
7123 return replaceInstUsesWith(
7124 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
7125 } else if (!IsSExt || HasOneUse) {
7126 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
7127 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
7128 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
7129 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
7130 return CreateRangeCheck();
7131 }
7132 } else {
7133 // when C != 0 && C != 1:
7134 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
7135 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
7136 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
7137 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
7138 // when C != 0 && C != -1:
7139 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
7140 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
7141 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
7142 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
7143 return ICmpInst::Create(
7144 Instruction::ICmp, Pred1, X,
7145 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
7146 ? (IsSExt ? -1 : 1)
7147 : 0));
7148 }
7149 }
7150
7151 return nullptr;
7152}
7153
7154/// If we have an icmp le or icmp ge instruction with a constant operand, turn
7155/// it into the appropriate icmp lt or icmp gt instruction. This transform
7156/// allows them to be folded in visitICmpInst.
7158 ICmpInst::Predicate Pred = I.getPredicate();
7159 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
7161 return nullptr;
7162
7163 Value *Op0 = I.getOperand(0);
7164 Value *Op1 = I.getOperand(1);
7165 auto *Op1C = dyn_cast<Constant>(Op1);
7166 if (!Op1C)
7167 return nullptr;
7168
7169 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
7170 if (!FlippedStrictness)
7171 return nullptr;
7172
7173 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
7174}
7175
7176/// If we have a comparison with a non-canonical predicate, if we can update
7177/// all the users, invert the predicate and adjust all the users.
7179 // Is the predicate already canonical?
7180 CmpInst::Predicate Pred = I.getPredicate();
7182 return nullptr;
7183
7184 // Can all users be adjusted to predicate inversion?
7185 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
7186 return nullptr;
7187
7188 // Ok, we can canonicalize comparison!
7189 // Let's first invert the comparison's predicate.
7190 I.setPredicate(CmpInst::getInversePredicate(Pred));
7191 I.setName(I.getName() + ".not");
7192
7193 // And, adapt users.
7195
7196 return &I;
7197}
7198
7199/// Integer compare with boolean values can always be turned into bitwise ops.
7201 InstCombiner::BuilderTy &Builder) {
7202 Value *A = I.getOperand(0), *B = I.getOperand(1);
7203 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
7204
7205 // A boolean compared to true/false can be simplified to Op0/true/false in
7206 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
7207 // Cases not handled by InstSimplify are always 'not' of Op0.
7208 if (match(B, m_Zero())) {
7209 switch (I.getPredicate()) {
7210 case CmpInst::ICMP_EQ: // A == 0 -> !A
7211 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
7212 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
7214 default:
7215 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7216 }
7217 } else if (match(B, m_One())) {
7218 switch (I.getPredicate()) {
7219 case CmpInst::ICMP_NE: // A != 1 -> !A
7220 case CmpInst::ICMP_ULT: // A <u 1 -> !A
7221 case CmpInst::ICMP_SGT: // A >s -1 -> !A
7223 default:
7224 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7225 }
7226 }
7227
7228 switch (I.getPredicate()) {
7229 default:
7230 llvm_unreachable("Invalid icmp instruction!");
7231 case ICmpInst::ICMP_EQ:
7232 // icmp eq i1 A, B -> ~(A ^ B)
7233 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
7234
7235 case ICmpInst::ICMP_NE:
7236 // icmp ne i1 A, B -> A ^ B
7237 return BinaryOperator::CreateXor(A, B);
7238
7239 case ICmpInst::ICMP_UGT:
7240 // icmp ugt -> icmp ult
7241 std::swap(A, B);
7242 [[fallthrough]];
7243 case ICmpInst::ICMP_ULT:
7244 // icmp ult i1 A, B -> ~A & B
7245 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
7246
7247 case ICmpInst::ICMP_SGT:
7248 // icmp sgt -> icmp slt
7249 std::swap(A, B);
7250 [[fallthrough]];
7251 case ICmpInst::ICMP_SLT:
7252 // icmp slt i1 A, B -> A & ~B
7253 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
7254
7255 case ICmpInst::ICMP_UGE:
7256 // icmp uge -> icmp ule
7257 std::swap(A, B);
7258 [[fallthrough]];
7259 case ICmpInst::ICMP_ULE:
7260 // icmp ule i1 A, B -> ~A | B
7261 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
7262
7263 case ICmpInst::ICMP_SGE:
7264 // icmp sge -> icmp sle
7265 std::swap(A, B);
7266 [[fallthrough]];
7267 case ICmpInst::ICMP_SLE:
7268 // icmp sle i1 A, B -> A | ~B
7269 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
7270 }
7271}
7272
7273// Transform pattern like:
7274// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7275// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7276// Into:
7277// (X l>> Y) != 0
7278// (X l>> Y) == 0
7280 InstCombiner::BuilderTy &Builder) {
7281 CmpPredicate Pred, NewPred;
7282 Value *X, *Y;
7283 if (match(&Cmp,
7284 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
7285 switch (Pred) {
7286 case ICmpInst::ICMP_ULE:
7287 NewPred = ICmpInst::ICMP_NE;
7288 break;
7289 case ICmpInst::ICMP_UGT:
7290 NewPred = ICmpInst::ICMP_EQ;
7291 break;
7292 default:
7293 return nullptr;
7294 }
7295 } else if (match(&Cmp, m_c_ICmp(Pred,
7298 m_Add(m_Shl(m_One(), m_Value(Y)),
7299 m_AllOnes()))),
7300 m_Value(X)))) {
7301 // The variant with 'add' is not canonical, (the variant with 'not' is)
7302 // we only get it because it has extra uses, and can't be canonicalized,
7303
7304 switch (Pred) {
7305 case ICmpInst::ICMP_ULT:
7306 NewPred = ICmpInst::ICMP_NE;
7307 break;
7308 case ICmpInst::ICMP_UGE:
7309 NewPred = ICmpInst::ICMP_EQ;
7310 break;
7311 default:
7312 return nullptr;
7313 }
7314 } else
7315 return nullptr;
7316
7317 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
7318 Constant *Zero = Constant::getNullValue(NewX->getType());
7319 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
7320}
7321
7323 InstCombiner::BuilderTy &Builder) {
7324 const CmpInst::Predicate Pred = Cmp.getPredicate();
7325 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
7326 Value *V1, *V2;
7327
7328 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7329 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
7330 if (auto *I = dyn_cast<Instruction>(V))
7331 I->copyIRFlags(&Cmp);
7332 Module *M = Cmp.getModule();
7334 M, Intrinsic::vector_reverse, V->getType());
7335 return CallInst::Create(F, V);
7336 };
7337
7338 if (match(LHS, m_VecReverse(m_Value(V1)))) {
7339 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7340 if (match(RHS, m_VecReverse(m_Value(V2))) &&
7341 (LHS->hasOneUse() || RHS->hasOneUse()))
7342 return createCmpReverse(Pred, V1, V2);
7343
7344 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7345 if (LHS->hasOneUse() && isSplatValue(RHS))
7346 return createCmpReverse(Pred, V1, RHS);
7347 }
7348 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7349 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
7350 return createCmpReverse(Pred, LHS, V2);
7351
7352 ArrayRef<int> M;
7353 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
7354 return nullptr;
7355
7356 // If both arguments of the cmp are shuffles that use the same mask and
7357 // shuffle within a single vector, move the shuffle after the cmp:
7358 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7359 Type *V1Ty = V1->getType();
7360 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
7361 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7362 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
7363 return new ShuffleVectorInst(NewCmp, M);
7364 }
7365
7366 // Try to canonicalize compare with splatted operand and splat constant.
7367 // TODO: We could generalize this for more than splats. See/use the code in
7368 // InstCombiner::foldVectorBinop().
7369 Constant *C;
7370 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
7371 return nullptr;
7372
7373 // Length-changing splats are ok, so adjust the constants as needed:
7374 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7375 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7376 int MaskSplatIndex;
7377 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
7378 // We allow poison in matching, but this transform removes it for safety.
7379 // Demanded elements analysis should be able to recover some/all of that.
7380 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
7381 ScalarC);
7382 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7383 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
7384 return new ShuffleVectorInst(NewCmp, NewM);
7385 }
7386
7387 return nullptr;
7388}
7389
7390// extract(uadd.with.overflow(A, B), 0) ult A
7391// -> extract(uadd.with.overflow(A, B), 1)
7393 CmpInst::Predicate Pred = I.getPredicate();
7394 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7395
7396 Value *UAddOv;
7397 Value *A, *B;
7398 auto UAddOvResultPat = m_ExtractValue<0>(
7400 if (match(Op0, UAddOvResultPat) &&
7401 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7402 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
7403 (match(A, m_One()) || match(B, m_One()))) ||
7404 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
7405 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
7406 // extract(uadd.with.overflow(A, B), 0) < A
7407 // extract(uadd.with.overflow(A, 1), 0) == 0
7408 // extract(uadd.with.overflow(A, -1), 0) != -1
7409 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
7410 else if (match(Op1, UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&
7411 (Op0 == A || Op0 == B))
7412 // A > extract(uadd.with.overflow(A, B), 0)
7413 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
7414 else
7415 return nullptr;
7416
7417 return ExtractValueInst::Create(UAddOv, 1);
7418}
7419
7421 if (!I.getOperand(0)->getType()->isPointerTy() ||
7423 I.getParent()->getParent(),
7424 I.getOperand(0)->getType()->getPointerAddressSpace())) {
7425 return nullptr;
7426 }
7427 Instruction *Op;
7428 if (match(I.getOperand(0), m_Instruction(Op)) &&
7429 match(I.getOperand(1), m_Zero()) &&
7430 Op->isLaunderOrStripInvariantGroup()) {
7431 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7432 Op->getOperand(0), I.getOperand(1));
7433 }
7434 return nullptr;
7435}
7436
7437/// This function folds patterns produced by lowering of reduce idioms, such as
7438/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7439/// attempts to generate fewer number of scalar comparisons instead of vector
7440/// comparisons when possible.
7442 InstCombiner::BuilderTy &Builder,
7443 const DataLayout &DL) {
7444 if (I.getType()->isVectorTy())
7445 return nullptr;
7446 CmpPredicate OuterPred, InnerPred;
7447 Value *LHS, *RHS;
7448
7449 // Match lowering of @llvm.vector.reduce.and. Turn
7450 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7451 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7452 /// %res = icmp <pred> i8 %scalar_ne, 0
7453 ///
7454 /// into
7455 ///
7456 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7457 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7458 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7459 ///
7460 /// for <pred> in {ne, eq}.
7461 if (!match(&I, m_ICmp(OuterPred,
7463 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7464 m_Zero())))
7465 return nullptr;
7466 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7467 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7468 return nullptr;
7469 unsigned NumBits =
7470 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7471 // TODO: Relax this to "not wider than max legal integer type"?
7472 if (!DL.isLegalInteger(NumBits))
7473 return nullptr;
7474
7475 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7476 auto *ScalarTy = Builder.getIntNTy(NumBits);
7477 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7478 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7479 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7480 I.getName());
7481 }
7482
7483 return nullptr;
7484}
7485
7486// This helper will be called with icmp operands in both orders.
7488 Value *Op0, Value *Op1,
7489 ICmpInst &CxtI) {
7490 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7491 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7492 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7493 return NI;
7494
7495 if (auto *SI = dyn_cast<SelectInst>(Op0))
7496 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7497 return NI;
7498
7499 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0)) {
7500 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7501 return Res;
7502
7503 if (Instruction *Res = foldICmpWithClamp(CxtI, Op1, MinMax))
7504 return Res;
7505 }
7506
7507 {
7508 Value *X;
7509 const APInt *C;
7510 // icmp X+Cst, X
7511 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7512 return foldICmpAddOpConst(X, *C, Pred);
7513 }
7514
7515 // abs(X) >= X --> true
7516 // abs(X) u<= X --> true
7517 // abs(X) < X --> false
7518 // abs(X) u> X --> false
7519 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7520 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7521 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7522 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7523 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7524 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7525 {
7526 Value *X;
7527 Constant *C;
7529 match(Op1, m_Specific(X))) {
7530 Value *NullValue = Constant::getNullValue(X->getType());
7531 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7532 const APInt SMin =
7533 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7534 bool IsIntMinPosion = C->isAllOnesValue();
7535 switch (Pred) {
7536 case CmpInst::ICMP_ULE:
7537 case CmpInst::ICMP_SGE:
7538 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7539 case CmpInst::ICMP_UGT:
7540 case CmpInst::ICMP_SLT:
7542 case CmpInst::ICMP_UGE:
7543 case CmpInst::ICMP_SLE:
7544 case CmpInst::ICMP_EQ: {
7545 return replaceInstUsesWith(
7546 CxtI, IsIntMinPosion
7547 ? Builder.CreateICmpSGT(X, AllOnesValue)
7548 : Builder.CreateICmpULT(
7549 X, ConstantInt::get(X->getType(), SMin + 1)));
7550 }
7551 case CmpInst::ICMP_ULT:
7552 case CmpInst::ICMP_SGT:
7553 case CmpInst::ICMP_NE: {
7554 return replaceInstUsesWith(
7555 CxtI, IsIntMinPosion
7556 ? Builder.CreateICmpSLT(X, NullValue)
7557 : Builder.CreateICmpUGT(
7558 X, ConstantInt::get(X->getType(), SMin)));
7559 }
7560 default:
7561 llvm_unreachable("Invalid predicate!");
7562 }
7563 }
7564 }
7565
7566 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7567 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7568 return replaceInstUsesWith(CxtI, V);
7569
7570 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7571 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7572 {
7573 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7574 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7576 }
7577
7578 if (!ICmpInst::isUnsigned(Pred) &&
7579 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7580 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7582 }
7583 }
7584
7585 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7586 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7587 {
7588 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7589 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7591 }
7592
7593 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7594 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7595 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7597 }
7598 }
7599
7600 return nullptr;
7601}
7602
7604 bool Changed = false;
7605 const SimplifyQuery Q = SQ.getWithInstruction(&I);
7606 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7607 unsigned Op0Cplxity = getComplexity(Op0);
7608 unsigned Op1Cplxity = getComplexity(Op1);
7609
7610 /// Orders the operands of the compare so that they are listed from most
7611 /// complex to least complex. This puts constants before unary operators,
7612 /// before binary operators.
7613 if (Op0Cplxity < Op1Cplxity) {
7614 I.swapOperands();
7615 std::swap(Op0, Op1);
7616 Changed = true;
7617 }
7618
7619 if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
7620 return replaceInstUsesWith(I, V);
7621
7622 // Comparing -val or val with non-zero is the same as just comparing val
7623 // ie, abs(val) != 0 -> val != 0
7624 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7625 Value *Cond, *SelectTrue, *SelectFalse;
7626 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7627 m_Value(SelectFalse)))) {
7628 if (Value *V = dyn_castNegVal(SelectTrue)) {
7629 if (V == SelectFalse)
7630 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7631 } else if (Value *V = dyn_castNegVal(SelectFalse)) {
7632 if (V == SelectTrue)
7633 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7634 }
7635 }
7636 }
7637
7639 return Res;
7640
7641 if (Op0->getType()->isIntOrIntVectorTy(1))
7643 return Res;
7644
7646 return Res;
7647
7649 return Res;
7650
7652 return Res;
7653
7655 return Res;
7656
7658 return Res;
7659
7661 return Res;
7662
7664 return Res;
7665
7666 // Test if the ICmpInst instruction is used exclusively by a select as
7667 // part of a minimum or maximum operation. If so, refrain from doing
7668 // any other folding. This helps out other analyses which understand
7669 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7670 // and CodeGen. And in this case, at least one of the comparison
7671 // operands has at least one user besides the compare (the select),
7672 // which would often largely negate the benefit of folding anyway.
7673 //
7674 // Do the same for the other patterns recognized by matchSelectPattern.
7675 if (I.hasOneUse())
7676 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7677 Value *A, *B;
7679 if (SPR.Flavor != SPF_UNKNOWN)
7680 return nullptr;
7681 }
7682
7683 // Do this after checking for min/max to prevent infinite looping.
7684 if (Instruction *Res = foldICmpWithZero(I))
7685 return Res;
7686
7687 // FIXME: We only do this after checking for min/max to prevent infinite
7688 // looping caused by a reverse canonicalization of these patterns for min/max.
7689 // FIXME: The organization of folds is a mess. These would naturally go into
7690 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7691 // down here after the min/max restriction.
7692 ICmpInst::Predicate Pred = I.getPredicate();
7693 const APInt *C;
7694 if (match(Op1, m_APInt(C))) {
7695 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7696 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7697 Constant *Zero = Constant::getNullValue(Op0->getType());
7698 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7699 }
7700
7701 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7702 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7704 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7705 }
7706 }
7707
7708 // The folds in here may rely on wrapping flags and special constants, so
7709 // they can break up min/max idioms in some cases but not seemingly similar
7710 // patterns.
7711 // FIXME: It may be possible to enhance select folding to make this
7712 // unnecessary. It may also be moot if we canonicalize to min/max
7713 // intrinsics.
7714 if (Instruction *Res = foldICmpBinOp(I, Q))
7715 return Res;
7716
7718 return Res;
7719
7720 // Try to match comparison as a sign bit test. Intentionally do this after
7721 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7722 if (Instruction *New = foldSignBitTest(I))
7723 return New;
7724
7725 if (auto *PN = dyn_cast<PHINode>(Op0))
7726 if (Instruction *NV = foldOpIntoPhi(I, PN))
7727 return NV;
7728 if (auto *PN = dyn_cast<PHINode>(Op1))
7729 if (Instruction *NV = foldOpIntoPhi(I, PN))
7730 return NV;
7731
7733 return Res;
7734
7735 if (Instruction *Res = foldICmpCommutative(I.getCmpPredicate(), Op0, Op1, I))
7736 return Res;
7737 if (Instruction *Res =
7738 foldICmpCommutative(I.getSwappedCmpPredicate(), Op1, Op0, I))
7739 return Res;
7740
7741 if (I.isCommutative()) {
7742 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7743 replaceOperand(I, 0, Pair->first);
7744 replaceOperand(I, 1, Pair->second);
7745 return &I;
7746 }
7747 }
7748
7749 // In case of a comparison with two select instructions having the same
7750 // condition, check whether one of the resulting branches can be simplified.
7751 // If so, just compare the other branch and select the appropriate result.
7752 // For example:
7753 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7754 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7755 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7756 // The icmp will result false for the false value of selects and the result
7757 // will depend upon the comparison of true values of selects if %cmp is
7758 // true. Thus, transform this into:
7759 // %cmp = icmp slt i32 %y, %z
7760 // %sel = select i1 %cond, i1 %cmp, i1 false
7761 // This handles similar cases to transform.
7762 {
7763 Value *Cond, *A, *B, *C, *D;
7764 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7766 (Op0->hasOneUse() || Op1->hasOneUse())) {
7767 // Check whether comparison of TrueValues can be simplified
7768 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7769 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7770 return SelectInst::Create(Cond, Res, NewICMP);
7771 }
7772 // Check whether comparison of FalseValues can be simplified
7773 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7774 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7775 return SelectInst::Create(Cond, NewICMP, Res);
7776 }
7777 }
7778 }
7779
7780 // icmp slt (sub nsw x, y), (add nsw x, y) --> icmp sgt y, 0
7781 // icmp ult (sub nuw x, y), (add nuw x, y) --> icmp ugt y, 0
7782 // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y) --> icmp eq y, 0
7783 {
7784 Value *A, *B;
7785 CmpPredicate CmpPred;
7786 if (match(&I, m_c_ICmp(CmpPred, m_Sub(m_Value(A), m_Value(B)),
7788 auto *I0 = cast<OverflowingBinaryOperator>(Op0);
7789 auto *I1 = cast<OverflowingBinaryOperator>(Op1);
7790 bool I0NUW = I0->hasNoUnsignedWrap();
7791 bool I1NUW = I1->hasNoUnsignedWrap();
7792 bool I0NSW = I0->hasNoSignedWrap();
7793 bool I1NSW = I1->hasNoSignedWrap();
7794 if ((ICmpInst::isUnsigned(Pred) && I0NUW && I1NUW) ||
7795 (ICmpInst::isSigned(Pred) && I0NSW && I1NSW) ||
7796 (ICmpInst::isEquality(Pred) &&
7797 ((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {
7798 return new ICmpInst(CmpPredicate::getSwapped(CmpPred), B,
7799 ConstantInt::get(Op0->getType(), 0));
7800 }
7801 }
7802 }
7803
7804 // Try to optimize equality comparisons against alloca-based pointers.
7805 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7806 assert(Op1->getType()->isPointerTy() &&
7807 "Comparing pointer with non-pointer?");
7808 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7809 if (foldAllocaCmp(Alloca))
7810 return nullptr;
7811 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7812 if (foldAllocaCmp(Alloca))
7813 return nullptr;
7814 }
7815
7816 if (Instruction *Res = foldICmpBitCast(I))
7817 return Res;
7818
7819 // TODO: Hoist this above the min/max bailout.
7821 return R;
7822
7823 {
7824 Value *X, *Y;
7825 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7826 // and (X & ~Y) != 0 --> (X & Y) == 0
7827 // if A is a power of 2.
7828 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7829 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, &I) &&
7830 I.isEquality())
7831 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7832 Op1);
7833
7834 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7835 if (Op0->getType()->isIntOrIntVectorTy()) {
7836 bool ConsumesOp0, ConsumesOp1;
7837 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7838 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7839 (ConsumesOp0 || ConsumesOp1)) {
7840 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7841 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7842 assert(InvOp0 && InvOp1 &&
7843 "Mismatch between isFreeToInvert and getFreelyInverted");
7844 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7845 }
7846 }
7847
7848 Instruction *AddI = nullptr;
7850 m_Instruction(AddI))) &&
7851 isa<IntegerType>(X->getType())) {
7852 Value *Result;
7853 Constant *Overflow;
7854 // m_UAddWithOverflow can match patterns that do not include an explicit
7855 // "add" instruction, so check the opcode of the matched op.
7856 if (AddI->getOpcode() == Instruction::Add &&
7857 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7858 Result, Overflow)) {
7859 replaceInstUsesWith(*AddI, Result);
7860 eraseInstFromFunction(*AddI);
7861 return replaceInstUsesWith(I, Overflow);
7862 }
7863 }
7864
7865 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7866 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7867 match(Op1, m_APInt(C))) {
7868 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7869 return R;
7870 }
7871
7872 // Signbit test folds
7873 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7874 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7875 Instruction *ExtI;
7876 if ((I.isUnsigned() || I.isEquality()) &&
7877 match(Op1,
7879 Y->getType()->getScalarSizeInBits() == 1 &&
7880 (Op0->hasOneUse() || Op1->hasOneUse())) {
7881 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7882 Instruction *ShiftI;
7883 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7885 OpWidth - 1))))) {
7886 unsigned ExtOpc = ExtI->getOpcode();
7887 unsigned ShiftOpc = ShiftI->getOpcode();
7888 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7889 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7890 Value *SLTZero =
7891 Builder.CreateICmpSLT(X, Constant::getNullValue(X->getType()));
7892 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7893 return replaceInstUsesWith(I, Cmp);
7894 }
7895 }
7896 }
7897 }
7898
7899 if (Instruction *Res = foldICmpEquality(I))
7900 return Res;
7901
7903 return Res;
7904
7905 if (Instruction *Res = foldICmpOfUAddOv(I))
7906 return Res;
7907
7908 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7909 // an i1 which indicates whether or not we successfully did the swap.
7910 //
7911 // Replace comparisons between the old value and the expected value with the
7912 // indicator that 'cmpxchg' returns.
7913 //
7914 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7915 // spuriously fail. In those cases, the old value may equal the expected
7916 // value but it is possible for the swap to not occur.
7917 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7918 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7919 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7920 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7921 !ACXI->isWeak())
7922 return ExtractValueInst::Create(ACXI, 1);
7923
7925 return Res;
7926
7927 if (I.getType()->isVectorTy())
7928 if (Instruction *Res = foldVectorCmp(I, Builder))
7929 return Res;
7930
7932 return Res;
7933
7935 return Res;
7936
7937 {
7938 Value *A;
7939 const APInt *C1, *C2;
7940 ICmpInst::Predicate Pred = I.getPredicate();
7941 if (ICmpInst::isEquality(Pred)) {
7942 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
7943 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
7944 if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) &&
7945 match(Op1, m_APInt(C2))) {
7946 Type *InputTy = A->getType();
7947 unsigned InputBitWidth = InputTy->getScalarSizeInBits();
7948 // c2 must be non-negative at the bitwidth of a.
7949 if (C2->getActiveBits() < InputBitWidth) {
7950 APInt TruncC1 = C1->trunc(InputBitWidth);
7951 // Check if there are 1s in C1 high bits of size InputBitWidth.
7952 if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth)))
7953 TruncC1.setBit(InputBitWidth - 1);
7954 Value *AndInst = Builder.CreateAnd(A, TruncC1);
7955 return new ICmpInst(
7956 Pred, AndInst,
7957 ConstantInt::get(InputTy, C2->trunc(InputBitWidth)));
7958 }
7959 }
7960 }
7961 }
7962
7963 return Changed ? &I : nullptr;
7964}
7965
7966/// Fold fcmp ([us]itofp x, cst) if possible.
7968 Instruction *LHSI,
7969 Constant *RHSC) {
7970 const APFloat *RHS;
7971 if (!match(RHSC, m_APFloat(RHS)))
7972 return nullptr;
7973
7974 // Get the width of the mantissa. We don't want to hack on conversions that
7975 // might lose information from the integer, e.g. "i64 -> float"
7976 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7977 if (MantissaWidth == -1)
7978 return nullptr; // Unknown.
7979
7980 Type *IntTy = LHSI->getOperand(0)->getType();
7981 unsigned IntWidth = IntTy->getScalarSizeInBits();
7982 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7983
7984 if (I.isEquality()) {
7985 FCmpInst::Predicate P = I.getPredicate();
7986 bool IsExact = false;
7987 APSInt RHSCvt(IntWidth, LHSUnsigned);
7988 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7989
7990 // If the floating point constant isn't an integer value, we know if we will
7991 // ever compare equal / not equal to it.
7992 if (!IsExact) {
7993 // TODO: Can never be -0.0 and other non-representable values
7994 APFloat RHSRoundInt(*RHS);
7996 if (*RHS != RHSRoundInt) {
7998 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7999
8001 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8002 }
8003 }
8004
8005 // TODO: If the constant is exactly representable, is it always OK to do
8006 // equality compares as integer?
8007 }
8008
8009 // Check to see that the input is converted from an integer type that is small
8010 // enough that preserves all bits. TODO: check here for "known" sign bits.
8011 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
8012
8013 // Following test does NOT adjust IntWidth downwards for signed inputs,
8014 // because the most negative value still requires all the mantissa bits
8015 // to distinguish it from one less than that value.
8016 if ((int)IntWidth > MantissaWidth) {
8017 // Conversion would lose accuracy. Check if loss can impact comparison.
8018 int Exp = ilogb(*RHS);
8019 if (Exp == APFloat::IEK_Inf) {
8020 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
8021 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
8022 // Conversion could create infinity.
8023 return nullptr;
8024 } else {
8025 // Note that if RHS is zero or NaN, then Exp is negative
8026 // and first condition is trivially false.
8027 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
8028 // Conversion could affect comparison.
8029 return nullptr;
8030 }
8031 }
8032
8033 // Otherwise, we can potentially simplify the comparison. We know that it
8034 // will always come through as an integer value and we know the constant is
8035 // not a NAN (it would have been previously simplified).
8036 assert(!RHS->isNaN() && "NaN comparison not already folded!");
8037
8039 switch (I.getPredicate()) {
8040 default:
8041 llvm_unreachable("Unexpected predicate!");
8042 case FCmpInst::FCMP_UEQ:
8043 case FCmpInst::FCMP_OEQ:
8044 Pred = ICmpInst::ICMP_EQ;
8045 break;
8046 case FCmpInst::FCMP_UGT:
8047 case FCmpInst::FCMP_OGT:
8048 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
8049 break;
8050 case FCmpInst::FCMP_UGE:
8051 case FCmpInst::FCMP_OGE:
8052 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
8053 break;
8054 case FCmpInst::FCMP_ULT:
8055 case FCmpInst::FCMP_OLT:
8056 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
8057 break;
8058 case FCmpInst::FCMP_ULE:
8059 case FCmpInst::FCMP_OLE:
8060 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
8061 break;
8062 case FCmpInst::FCMP_UNE:
8063 case FCmpInst::FCMP_ONE:
8064 Pred = ICmpInst::ICMP_NE;
8065 break;
8066 case FCmpInst::FCMP_ORD:
8067 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8068 case FCmpInst::FCMP_UNO:
8069 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8070 }
8071
8072 // Now we know that the APFloat is a normal number, zero or inf.
8073
8074 // See if the FP constant is too large for the integer. For example,
8075 // comparing an i8 to 300.0.
8076 if (!LHSUnsigned) {
8077 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
8078 // and large values.
8079 APFloat SMax(RHS->getSemantics());
8080 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
8082 if (SMax < *RHS) { // smax < 13123.0
8083 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
8084 Pred == ICmpInst::ICMP_SLE)
8085 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8086 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8087 }
8088 } else {
8089 // If the RHS value is > UnsignedMax, fold the comparison. This handles
8090 // +INF and large values.
8091 APFloat UMax(RHS->getSemantics());
8092 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
8094 if (UMax < *RHS) { // umax < 13123.0
8095 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
8096 Pred == ICmpInst::ICMP_ULE)
8097 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8098 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8099 }
8100 }
8101
8102 if (!LHSUnsigned) {
8103 // See if the RHS value is < SignedMin.
8104 APFloat SMin(RHS->getSemantics());
8105 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
8107 if (SMin > *RHS) { // smin > 12312.0
8108 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
8109 Pred == ICmpInst::ICMP_SGE)
8110 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8111 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8112 }
8113 } else {
8114 // See if the RHS value is < UnsignedMin.
8115 APFloat UMin(RHS->getSemantics());
8116 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
8118 if (UMin > *RHS) { // umin > 12312.0
8119 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
8120 Pred == ICmpInst::ICMP_UGE)
8121 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8122 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8123 }
8124 }
8125
8126 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
8127 // [0, UMAX], but it may still be fractional. Check whether this is the case
8128 // using the IsExact flag.
8129 // Don't do this for zero, because -0.0 is not fractional.
8130 APSInt RHSInt(IntWidth, LHSUnsigned);
8131 bool IsExact;
8132 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
8133 if (!RHS->isZero()) {
8134 if (!IsExact) {
8135 // If we had a comparison against a fractional value, we have to adjust
8136 // the compare predicate and sometimes the value. RHSC is rounded towards
8137 // zero at this point.
8138 switch (Pred) {
8139 default:
8140 llvm_unreachable("Unexpected integer comparison!");
8141 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
8142 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8143 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
8144 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8145 case ICmpInst::ICMP_ULE:
8146 // (float)int <= 4.4 --> int <= 4
8147 // (float)int <= -4.4 --> false
8148 if (RHS->isNegative())
8149 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8150 break;
8151 case ICmpInst::ICMP_SLE:
8152 // (float)int <= 4.4 --> int <= 4
8153 // (float)int <= -4.4 --> int < -4
8154 if (RHS->isNegative())
8155 Pred = ICmpInst::ICMP_SLT;
8156 break;
8157 case ICmpInst::ICMP_ULT:
8158 // (float)int < -4.4 --> false
8159 // (float)int < 4.4 --> int <= 4
8160 if (RHS->isNegative())
8161 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8162 Pred = ICmpInst::ICMP_ULE;
8163 break;
8164 case ICmpInst::ICMP_SLT:
8165 // (float)int < -4.4 --> int < -4
8166 // (float)int < 4.4 --> int <= 4
8167 if (!RHS->isNegative())
8168 Pred = ICmpInst::ICMP_SLE;
8169 break;
8170 case ICmpInst::ICMP_UGT:
8171 // (float)int > 4.4 --> int > 4
8172 // (float)int > -4.4 --> true
8173 if (RHS->isNegative())
8174 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8175 break;
8176 case ICmpInst::ICMP_SGT:
8177 // (float)int > 4.4 --> int > 4
8178 // (float)int > -4.4 --> int >= -4
8179 if (RHS->isNegative())
8180 Pred = ICmpInst::ICMP_SGE;
8181 break;
8182 case ICmpInst::ICMP_UGE:
8183 // (float)int >= -4.4 --> true
8184 // (float)int >= 4.4 --> int > 4
8185 if (RHS->isNegative())
8186 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8187 Pred = ICmpInst::ICMP_UGT;
8188 break;
8189 case ICmpInst::ICMP_SGE:
8190 // (float)int >= -4.4 --> int >= -4
8191 // (float)int >= 4.4 --> int > 4
8192 if (!RHS->isNegative())
8193 Pred = ICmpInst::ICMP_SGT;
8194 break;
8195 }
8196 }
8197 }
8198
8199 // Lower this FP comparison into an appropriate integer version of the
8200 // comparison.
8201 return new ICmpInst(Pred, LHSI->getOperand(0),
8202 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
8203}
8204
8205/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
8207 Constant *RHSC) {
8208 // When C is not 0.0 and infinities are not allowed:
8209 // (C / X) < 0.0 is a sign-bit test of X
8210 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
8211 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
8212 //
8213 // Proof:
8214 // Multiply (C / X) < 0.0 by X * X / C.
8215 // - X is non zero, if it is the flag 'ninf' is violated.
8216 // - C defines the sign of X * X * C. Thus it also defines whether to swap
8217 // the predicate. C is also non zero by definition.
8218 //
8219 // Thus X * X / C is non zero and the transformation is valid. [qed]
8220
8221 FCmpInst::Predicate Pred = I.getPredicate();
8222
8223 // Check that predicates are valid.
8224 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
8225 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
8226 return nullptr;
8227
8228 // Check that RHS operand is zero.
8229 if (!match(RHSC, m_AnyZeroFP()))
8230 return nullptr;
8231
8232 // Check fastmath flags ('ninf').
8233 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
8234 return nullptr;
8235
8236 // Check the properties of the dividend. It must not be zero to avoid a
8237 // division by zero (see Proof).
8238 const APFloat *C;
8239 if (!match(LHSI->getOperand(0), m_APFloat(C)))
8240 return nullptr;
8241
8242 if (C->isZero())
8243 return nullptr;
8244
8245 // Get swapped predicate if necessary.
8246 if (C->isNegative())
8247 Pred = I.getSwappedPredicate();
8248
8249 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
8250}
8251
8252// Transform 'fptrunc(x) cmp C' to 'x cmp ext(C)' if possible.
8253// Patterns include:
8254// fptrunc(x) < C --> x < ext(C)
8255// fptrunc(x) <= C --> x <= ext(C)
8256// fptrunc(x) > C --> x > ext(C)
8257// fptrunc(x) >= C --> x >= ext(C)
8258// where 'ext(C)' is the extension of 'C' to the type of 'x' with a small bias
8259// due to precision loss.
8261 const Constant &C) {
8262 FCmpInst::Predicate Pred = I.getPredicate();
8263 bool RoundDown = false;
8264
8265 if (Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE ||
8266 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT)
8267 RoundDown = true;
8268 else if (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT ||
8269 Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)
8270 RoundDown = false;
8271 else
8272 return nullptr;
8273
8274 const APFloat *CValue;
8275 if (!match(&C, m_APFloat(CValue)))
8276 return nullptr;
8277
8278 if (CValue->isNaN() || CValue->isInfinity())
8279 return nullptr;
8280
8281 auto ConvertFltSema = [](const APFloat &Src, const fltSemantics &Sema) {
8282 bool LosesInfo;
8283 APFloat Dest = Src;
8284 Dest.convert(Sema, APFloat::rmNearestTiesToEven, &LosesInfo);
8285 return Dest;
8286 };
8287
8288 auto NextValue = [](const APFloat &Value, bool RoundDown) {
8289 APFloat NextValue = Value;
8290 NextValue.next(RoundDown);
8291 return NextValue;
8292 };
8293
8294 APFloat NextCValue = NextValue(*CValue, RoundDown);
8295
8296 Type *DestType = FPTrunc.getOperand(0)->getType();
8297 const fltSemantics &DestFltSema =
8298 DestType->getScalarType()->getFltSemantics();
8299
8300 APFloat ExtCValue = ConvertFltSema(*CValue, DestFltSema);
8301 APFloat ExtNextCValue = ConvertFltSema(NextCValue, DestFltSema);
8302
8303 // When 'NextCValue' is infinity, use an imaged 'NextCValue' that equals
8304 // 'CValue + bias' to avoid the infinity after conversion. The bias is
8305 // estimated as 'CValue - PrevCValue', where 'PrevCValue' is the previous
8306 // value of 'CValue'.
8307 if (NextCValue.isInfinity()) {
8308 APFloat PrevCValue = NextValue(*CValue, !RoundDown);
8309 APFloat Bias = ConvertFltSema(*CValue - PrevCValue, DestFltSema);
8310
8311 ExtNextCValue = ExtCValue + Bias;
8312 }
8313
8314 APFloat ExtMidValue =
8315 scalbn(ExtCValue + ExtNextCValue, -1, APFloat::rmNearestTiesToEven);
8316
8317 const fltSemantics &SrcFltSema =
8318 C.getType()->getScalarType()->getFltSemantics();
8319
8320 // 'MidValue' might be rounded to 'NextCValue'. Correct it here.
8321 APFloat MidValue = ConvertFltSema(ExtMidValue, SrcFltSema);
8322 if (MidValue != *CValue)
8323 ExtMidValue.next(!RoundDown);
8324
8325 // Check whether 'ExtMidValue' is a valid result since the assumption on
8326 // imaged 'NextCValue' might not hold for new float types.
8327 // ppc_fp128 can't pass here when converting from max float because of
8328 // APFloat implementation.
8329 if (NextCValue.isInfinity()) {
8330 // ExtMidValue --- narrowed ---> Finite
8331 if (ConvertFltSema(ExtMidValue, SrcFltSema).isInfinity())
8332 return nullptr;
8333
8334 // NextExtMidValue --- narrowed ---> Infinity
8335 APFloat NextExtMidValue = NextValue(ExtMidValue, RoundDown);
8336 if (ConvertFltSema(NextExtMidValue, SrcFltSema).isFinite())
8337 return nullptr;
8338 }
8339
8340 return new FCmpInst(Pred, FPTrunc.getOperand(0),
8341 ConstantFP::get(DestType, ExtMidValue), "", &I);
8342}
8343
8344/// Optimize fabs(X) compared with zero.
8346 Value *X;
8347 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
8348 return nullptr;
8349
8350 const APFloat *C;
8351 if (!match(I.getOperand(1), m_APFloat(C)))
8352 return nullptr;
8353
8354 if (!C->isPosZero()) {
8355 if (!C->isSmallestNormalized())
8356 return nullptr;
8357
8358 const Function *F = I.getFunction();
8359 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
8360 if (Mode.Input == DenormalMode::PreserveSign ||
8362
8363 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8364 Constant *Zero = ConstantFP::getZero(X->getType());
8365 return new FCmpInst(P, X, Zero, "", I);
8366 };
8367
8368 switch (I.getPredicate()) {
8369 case FCmpInst::FCMP_OLT:
8370 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
8371 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
8372 case FCmpInst::FCMP_UGE:
8373 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
8374 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
8375 case FCmpInst::FCMP_OGE:
8376 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
8377 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
8378 case FCmpInst::FCMP_ULT:
8379 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
8380 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
8381 default:
8382 break;
8383 }
8384 }
8385
8386 return nullptr;
8387 }
8388
8389 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8390 I->setPredicate(P);
8391 return IC.replaceOperand(*I, 0, X);
8392 };
8393
8394 switch (I.getPredicate()) {
8395 case FCmpInst::FCMP_UGE:
8396 case FCmpInst::FCMP_OLT:
8397 // fabs(X) >= 0.0 --> true
8398 // fabs(X) < 0.0 --> false
8399 llvm_unreachable("fcmp should have simplified");
8400
8401 case FCmpInst::FCMP_OGT:
8402 // fabs(X) > 0.0 --> X != 0.0
8403 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8404
8405 case FCmpInst::FCMP_UGT:
8406 // fabs(X) u> 0.0 --> X u!= 0.0
8407 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8408
8409 case FCmpInst::FCMP_OLE:
8410 // fabs(X) <= 0.0 --> X == 0.0
8411 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8412
8413 case FCmpInst::FCMP_ULE:
8414 // fabs(X) u<= 0.0 --> X u== 0.0
8415 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8416
8417 case FCmpInst::FCMP_OGE:
8418 // fabs(X) >= 0.0 --> !isnan(X)
8419 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8420 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8421
8422 case FCmpInst::FCMP_ULT:
8423 // fabs(X) u< 0.0 --> isnan(X)
8424 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8425 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8426
8427 case FCmpInst::FCMP_OEQ:
8428 case FCmpInst::FCMP_UEQ:
8429 case FCmpInst::FCMP_ONE:
8430 case FCmpInst::FCMP_UNE:
8431 case FCmpInst::FCMP_ORD:
8432 case FCmpInst::FCMP_UNO:
8433 // Look through the fabs() because it doesn't change anything but the sign.
8434 // fabs(X) == 0.0 --> X == 0.0,
8435 // fabs(X) != 0.0 --> X != 0.0
8436 // isnan(fabs(X)) --> isnan(X)
8437 // !isnan(fabs(X) --> !isnan(X)
8438 return replacePredAndOp0(&I, I.getPredicate(), X);
8439
8440 default:
8441 return nullptr;
8442 }
8443}
8444
8445/// Optimize sqrt(X) compared with zero.
8447 Value *X;
8448 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))
8449 return nullptr;
8450
8451 if (!match(I.getOperand(1), m_PosZeroFP()))
8452 return nullptr;
8453
8454 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8455 I.setPredicate(P);
8456 return IC.replaceOperand(I, 0, X);
8457 };
8458
8459 // Clear ninf flag if sqrt doesn't have it.
8460 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())
8461 I.setHasNoInfs(false);
8462
8463 switch (I.getPredicate()) {
8464 case FCmpInst::FCMP_OLT:
8465 case FCmpInst::FCMP_UGE:
8466 // sqrt(X) < 0.0 --> false
8467 // sqrt(X) u>= 0.0 --> true
8468 llvm_unreachable("fcmp should have simplified");
8469 case FCmpInst::FCMP_ULT:
8470 case FCmpInst::FCMP_ULE:
8471 case FCmpInst::FCMP_OGT:
8472 case FCmpInst::FCMP_OGE:
8473 case FCmpInst::FCMP_OEQ:
8474 case FCmpInst::FCMP_UNE:
8475 // sqrt(X) u< 0.0 --> X u< 0.0
8476 // sqrt(X) u<= 0.0 --> X u<= 0.0
8477 // sqrt(X) > 0.0 --> X > 0.0
8478 // sqrt(X) >= 0.0 --> X >= 0.0
8479 // sqrt(X) == 0.0 --> X == 0.0
8480 // sqrt(X) u!= 0.0 --> X u!= 0.0
8481 return IC.replaceOperand(I, 0, X);
8482
8483 case FCmpInst::FCMP_OLE:
8484 // sqrt(X) <= 0.0 --> X == 0.0
8485 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8486 case FCmpInst::FCMP_UGT:
8487 // sqrt(X) u> 0.0 --> X u!= 0.0
8488 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8489 case FCmpInst::FCMP_UEQ:
8490 // sqrt(X) u== 0.0 --> X u<= 0.0
8491 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8492 case FCmpInst::FCMP_ONE:
8493 // sqrt(X) != 0.0 --> X > 0.0
8494 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8495 case FCmpInst::FCMP_ORD:
8496 // !isnan(sqrt(X)) --> X >= 0.0
8497 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8498 case FCmpInst::FCMP_UNO:
8499 // isnan(sqrt(X)) --> X u< 0.0
8500 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8501 default:
8502 llvm_unreachable("Unexpected predicate!");
8503 }
8504}
8505
8507 CmpInst::Predicate Pred = I.getPredicate();
8508 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8509
8510 // Canonicalize fneg as Op1.
8511 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
8512 std::swap(Op0, Op1);
8513 Pred = I.getSwappedPredicate();
8514 }
8515
8516 if (!match(Op1, m_FNeg(m_Specific(Op0))))
8517 return nullptr;
8518
8519 // Replace the negated operand with 0.0:
8520 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8521 Constant *Zero = ConstantFP::getZero(Op0->getType());
8522 return new FCmpInst(Pred, Op0, Zero, "", &I);
8523}
8524
8526 Constant *RHSC, InstCombinerImpl &CI) {
8527 const CmpInst::Predicate Pred = I.getPredicate();
8528 Value *X = LHSI->getOperand(0);
8529 Value *Y = LHSI->getOperand(1);
8530 switch (Pred) {
8531 default:
8532 break;
8533 case FCmpInst::FCMP_UGT:
8534 case FCmpInst::FCMP_ULT:
8535 case FCmpInst::FCMP_UNE:
8536 case FCmpInst::FCMP_OEQ:
8537 case FCmpInst::FCMP_OGE:
8538 case FCmpInst::FCMP_OLE:
8539 // The optimization is not valid if X and Y are infinities of the same
8540 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8541 // flag then we can assume we do not have that case. Otherwise we might be
8542 // able to prove that either X or Y is not infinity.
8543 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8547 break;
8548
8549 [[fallthrough]];
8550 case FCmpInst::FCMP_OGT:
8551 case FCmpInst::FCMP_OLT:
8552 case FCmpInst::FCMP_ONE:
8553 case FCmpInst::FCMP_UEQ:
8554 case FCmpInst::FCMP_UGE:
8555 case FCmpInst::FCMP_ULE:
8556 // fcmp pred (x - y), 0 --> fcmp pred x, y
8557 if (match(RHSC, m_AnyZeroFP()) &&
8558 I.getFunction()->getDenormalMode(
8559 LHSI->getType()->getScalarType()->getFltSemantics()) ==
8561 CI.replaceOperand(I, 0, X);
8562 CI.replaceOperand(I, 1, Y);
8563 I.setHasNoInfs(LHSI->hasNoInfs());
8564 if (LHSI->hasNoNaNs())
8565 I.setHasNoNaNs(true);
8566 return &I;
8567 }
8568 break;
8569 }
8570
8571 return nullptr;
8572}
8573
8575 InstCombinerImpl &IC) {
8576 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
8577 Type *OpType = LHS->getType();
8578 CmpInst::Predicate Pred = I.getPredicate();
8579
8582
8583 if (!FloorX && !CeilX) {
8586 std::swap(LHS, RHS);
8587 Pred = I.getSwappedPredicate();
8588 }
8589 }
8590
8591 switch (Pred) {
8592 case FCmpInst::FCMP_OLE:
8593 // fcmp ole floor(x), x => fcmp ord x, 0
8594 if (FloorX)
8596 "", &I);
8597 break;
8598 case FCmpInst::FCMP_OGT:
8599 // fcmp ogt floor(x), x => false
8600 if (FloorX)
8601 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8602 break;
8603 case FCmpInst::FCMP_OGE:
8604 // fcmp oge ceil(x), x => fcmp ord x, 0
8605 if (CeilX)
8607 "", &I);
8608 break;
8609 case FCmpInst::FCMP_OLT:
8610 // fcmp olt ceil(x), x => false
8611 if (CeilX)
8612 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8613 break;
8614 case FCmpInst::FCMP_ULE:
8615 // fcmp ule floor(x), x => true
8616 if (FloorX)
8617 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8618 break;
8619 case FCmpInst::FCMP_UGT:
8620 // fcmp ugt floor(x), x => fcmp uno x, 0
8621 if (FloorX)
8623 "", &I);
8624 break;
8625 case FCmpInst::FCMP_UGE:
8626 // fcmp uge ceil(x), x => true
8627 if (CeilX)
8628 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8629 break;
8630 case FCmpInst::FCMP_ULT:
8631 // fcmp ult ceil(x), x => fcmp uno x, 0
8632 if (CeilX)
8634 "", &I);
8635 break;
8636 default:
8637 break;
8638 }
8639
8640 return nullptr;
8641}
8642
8644 bool Changed = false;
8645
8646 /// Orders the operands of the compare so that they are listed from most
8647 /// complex to least complex. This puts constants before unary operators,
8648 /// before binary operators.
8649 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
8650 I.swapOperands();
8651 Changed = true;
8652 }
8653
8654 const CmpInst::Predicate Pred = I.getPredicate();
8655 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8656 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
8657 SQ.getWithInstruction(&I)))
8658 return replaceInstUsesWith(I, V);
8659
8660 // Simplify 'fcmp pred X, X'
8661 Type *OpType = Op0->getType();
8662 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
8663 if (Op0 == Op1) {
8664 switch (Pred) {
8665 default:
8666 break;
8667 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
8668 case FCmpInst::FCMP_ULT: // True if unordered or less than
8669 case FCmpInst::FCMP_UGT: // True if unordered or greater than
8670 case FCmpInst::FCMP_UNE: // True if unordered or not equal
8671 // Canonicalize these to be 'fcmp uno %X, 0.0'.
8672 I.setPredicate(FCmpInst::FCMP_UNO);
8673 I.setOperand(1, Constant::getNullValue(OpType));
8674 return &I;
8675
8676 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
8677 case FCmpInst::FCMP_OEQ: // True if ordered and equal
8678 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
8679 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
8680 // Canonicalize these to be 'fcmp ord %X, 0.0'.
8681 I.setPredicate(FCmpInst::FCMP_ORD);
8682 I.setOperand(1, Constant::getNullValue(OpType));
8683 return &I;
8684 }
8685 }
8686
8687 if (I.isCommutative()) {
8688 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
8689 replaceOperand(I, 0, Pair->first);
8690 replaceOperand(I, 1, Pair->second);
8691 return &I;
8692 }
8693 }
8694
8695 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
8696 // then canonicalize the operand to 0.0.
8697 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
8698 if (!match(Op0, m_PosZeroFP()) &&
8699 isKnownNeverNaN(Op0, getSimplifyQuery().getWithInstruction(&I)))
8700 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
8701
8702 if (!match(Op1, m_PosZeroFP()) &&
8703 isKnownNeverNaN(Op1, getSimplifyQuery().getWithInstruction(&I)))
8704 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8705 }
8706
8707 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
8708 Value *X, *Y;
8709 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
8710 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
8711
8713 return R;
8714
8715 // Test if the FCmpInst instruction is used exclusively by a select as
8716 // part of a minimum or maximum operation. If so, refrain from doing
8717 // any other folding. This helps out other analyses which understand
8718 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
8719 // and CodeGen. And in this case, at least one of the comparison
8720 // operands has at least one user besides the compare (the select),
8721 // which would often largely negate the benefit of folding anyway.
8722 if (I.hasOneUse())
8723 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
8724 Value *A, *B;
8726 if (SPR.Flavor != SPF_UNKNOWN)
8727 return nullptr;
8728 }
8729
8730 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
8731 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
8732 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
8733 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8734
8735 // Canonicalize:
8736 // fcmp olt X, +inf -> fcmp one X, +inf
8737 // fcmp ole X, +inf -> fcmp ord X, 0
8738 // fcmp ogt X, +inf -> false
8739 // fcmp oge X, +inf -> fcmp oeq X, +inf
8740 // fcmp ult X, +inf -> fcmp une X, +inf
8741 // fcmp ule X, +inf -> true
8742 // fcmp ugt X, +inf -> fcmp uno X, 0
8743 // fcmp uge X, +inf -> fcmp ueq X, +inf
8744 // fcmp olt X, -inf -> false
8745 // fcmp ole X, -inf -> fcmp oeq X, -inf
8746 // fcmp ogt X, -inf -> fcmp one X, -inf
8747 // fcmp oge X, -inf -> fcmp ord X, 0
8748 // fcmp ult X, -inf -> fcmp uno X, 0
8749 // fcmp ule X, -inf -> fcmp ueq X, -inf
8750 // fcmp ugt X, -inf -> fcmp une X, -inf
8751 // fcmp uge X, -inf -> true
8752 const APFloat *C;
8753 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
8754 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
8755 default:
8756 break;
8757 case FCmpInst::FCMP_ORD:
8758 case FCmpInst::FCMP_UNO:
8761 case FCmpInst::FCMP_OGT:
8762 case FCmpInst::FCMP_ULE:
8763 llvm_unreachable("Should be simplified by InstSimplify");
8764 case FCmpInst::FCMP_OLT:
8765 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
8766 case FCmpInst::FCMP_OLE:
8767 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
8768 "", &I);
8769 case FCmpInst::FCMP_OGE:
8770 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
8771 case FCmpInst::FCMP_ULT:
8772 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
8773 case FCmpInst::FCMP_UGT:
8774 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8775 "", &I);
8776 case FCmpInst::FCMP_UGE:
8777 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8778 }
8779 }
8780
8781 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8782 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8783 if (match(Op1, m_PosZeroFP()) &&
8786 if (Pred == FCmpInst::FCMP_OEQ)
8787 IntPred = ICmpInst::ICMP_EQ;
8788 else if (Pred == FCmpInst::FCMP_UNE)
8789 IntPred = ICmpInst::ICMP_NE;
8790
8791 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8792 Type *IntTy = X->getType();
8793 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8794 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8795 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8796 }
8797 }
8798
8799 // Handle fcmp with instruction LHS and constant RHS.
8800 Instruction *LHSI;
8801 Constant *RHSC;
8802 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8803 switch (LHSI->getOpcode()) {
8804 case Instruction::Select:
8805 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8806 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8808 return replaceOperand(I, 0, X);
8810 return NV;
8811 break;
8812 case Instruction::FSub:
8813 if (LHSI->hasOneUse())
8814 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
8815 return NV;
8816 break;
8817 case Instruction::PHI:
8818 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8819 return NV;
8820 break;
8821 case Instruction::SIToFP:
8822 case Instruction::UIToFP:
8823 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8824 return NV;
8825 break;
8826 case Instruction::FDiv:
8827 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8828 return NV;
8829 break;
8830 case Instruction::Load:
8831 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8832 if (Instruction *Res =
8834 return Res;
8835 break;
8836 case Instruction::FPTrunc:
8837 if (Instruction *NV = foldFCmpFpTrunc(I, *LHSI, *RHSC))
8838 return NV;
8839 break;
8840 }
8841 }
8842
8843 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8844 return R;
8845
8846 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))
8847 return R;
8848
8849 if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this))
8850 return R;
8851
8852 if (match(Op0, m_FNeg(m_Value(X)))) {
8853 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8854 Constant *C;
8855 if (match(Op1, m_Constant(C)))
8856 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8857 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8858 }
8859
8860 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8861 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8862 return new FCmpInst(Pred, X, Op1, "", &I);
8863
8864 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8865 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8866 return new FCmpInst(Pred, Op0, Y, "", &I);
8867
8868 if (match(Op0, m_FPExt(m_Value(X)))) {
8869 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8870 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8871 return new FCmpInst(Pred, X, Y, "", &I);
8872
8873 const APFloat *C;
8874 if (match(Op1, m_APFloat(C))) {
8875 const fltSemantics &FPSem =
8876 X->getType()->getScalarType()->getFltSemantics();
8877 bool Lossy;
8878 APFloat TruncC = *C;
8879 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8880
8881 if (Lossy) {
8882 // X can't possibly equal the higher-precision constant, so reduce any
8883 // equality comparison.
8884 // TODO: Other predicates can be handled via getFCmpCode().
8885 switch (Pred) {
8886 case FCmpInst::FCMP_OEQ:
8887 // X is ordered and equal to an impossible constant --> false
8888 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8889 case FCmpInst::FCMP_ONE:
8890 // X is ordered and not equal to an impossible constant --> ordered
8891 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8892 ConstantFP::getZero(X->getType()));
8893 case FCmpInst::FCMP_UEQ:
8894 // X is unordered or equal to an impossible constant --> unordered
8895 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8896 ConstantFP::getZero(X->getType()));
8897 case FCmpInst::FCMP_UNE:
8898 // X is unordered or not equal to an impossible constant --> true
8899 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8900 default:
8901 break;
8902 }
8903 }
8904
8905 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8906 // Avoid lossy conversions and denormals.
8907 // Zero is a special case that's OK to convert.
8908 APFloat Fabs = TruncC;
8909 Fabs.clearSign();
8910 if (!Lossy &&
8911 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8912 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8913 return new FCmpInst(Pred, X, NewC, "", &I);
8914 }
8915 }
8916 }
8917
8918 // Convert a sign-bit test of an FP value into a cast and integer compare.
8919 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8920 // TODO: Handle non-zero compare constants.
8921 // TODO: Handle other predicates.
8923 m_Value(X)))) &&
8924 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8925 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8926 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8927 IntType = VectorType::get(IntType, VecTy->getElementCount());
8928
8929 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8930 if (Pred == FCmpInst::FCMP_OLT) {
8931 Value *IntX = Builder.CreateBitCast(X, IntType);
8932 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8933 ConstantInt::getNullValue(IntType));
8934 }
8935 }
8936
8937 {
8938 Value *CanonLHS = nullptr;
8940 // (canonicalize(x) == x) => (x == x)
8941 if (CanonLHS == Op1)
8942 return new FCmpInst(Pred, Op1, Op1, "", &I);
8943
8944 Value *CanonRHS = nullptr;
8946 // (x == canonicalize(x)) => (x == x)
8947 if (CanonRHS == Op0)
8948 return new FCmpInst(Pred, Op0, Op0, "", &I);
8949
8950 // (canonicalize(x) == canonicalize(y)) => (x == y)
8951 if (CanonLHS && CanonRHS)
8952 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8953 }
8954
8955 if (I.getType()->isVectorTy())
8956 if (Instruction *Res = foldVectorCmp(I, Builder))
8957 return Res;
8958
8959 return Changed ? &I : nullptr;
8960}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
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< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#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 void collectOffsetOp(Value *V, SmallVectorImpl< OffsetOp > &Offsets, bool AllowRecursion)
static Value * rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW, 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 * foldICmpEqualityWithOffset(ICmpInst &I, InstCombiner::BuilderTy &Builder, const SimplifyQuery &SQ)
Offset both sides of an equality icmp to see if we can save some instructions: icmp eq/ne X,...
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 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 bool isChainSelectCmpBranch(const SelectInst *SI)
Return true when the instruction sequence within a block is select-cmp-br.
static Instruction * foldICmpInvariantGroup(ICmpInst &I)
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
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 Value * foldICmpWithLowBitMaskedVal(CmpPredicate Pred, Value *Op0, Value *Op1, const SimplifyQuery &Q, InstCombiner &IC)
Some comparisons can be simplified.
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 * foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl, const APInt &C)
Fold icmp (shl nuw C2, Y), C.
static Instruction * foldFCmpWithFloorAndCeil(FCmpInst &I, InstCombinerImpl &IC)
static Instruction * foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * foldICmpOfCmpIntrinsicWithConstant(CmpPredicate Pred, IntrinsicInst *I, const APInt &C, InstCombiner::BuilderTy &Builder)
static Instruction * processUMulZExtIdiom(ICmpInst &I, Value *MulVal, const APInt *OtherVal, InstCombinerImpl &IC)
Recognize and process idiom involving test for multiplication overflow.
static Instruction * foldSqrtWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC)
Optimize sqrt(X) compared with zero.
static Instruction * foldFCmpFNegCommonOp(FCmpInst &I)
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 * foldICmpUSubSatOrUAddSatWithConstant(CmpPredicate Pred, SaturatingInst *II, const APInt &C, InstCombiner::BuilderTy &Builder)
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 bool canRewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags &NW, 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 * foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc, const Constant &C)
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 * transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, CmpPredicate Cond, const DataLayout &DL, InstCombiner &IC)
Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
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 bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q)
Return true if X is a multiple of C.
static Value * foldICmpWithTruncSignExtendedVal(ICmpInst &I, InstCombiner::BuilderTy &Builder)
Some comparisons can be simplified.
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.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T1
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
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:171
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
Value * RHS
Value * LHS
BinaryOperator * Mul
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6053
void clearSign()
Definition APFloat.h:1280
bool isNaN() const
Definition APFloat.h:1429
bool isZero() const
Definition APFloat.h:1427
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.h:1140
APInt bitcastToAPInt() const
Definition APFloat.h:1335
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
opStatus next(bool nextDown)
Definition APFloat.h:1236
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
LLVM_ABI FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition APFloat.cpp:5982
opStatus roundToIntegral(roundingMode RM)
Definition APFloat.h:1230
bool isInfinity() const
Definition APFloat.h:1428
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
Definition APInt.cpp:1758
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition APInt.h:450
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1012
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1541
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1513
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1331
APInt abs() const
Get the absolute value.
Definition APInt.h:1796
unsigned ceilLogBase2() const
Definition APInt.h:1765
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1202
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
LLVM_ABI APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1948
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition APInt.h:467
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1489
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1928
bool eq(const APInt &RHS) const
Equality comparison.
Definition APInt.h:1080
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1935
void negate()
Negate this APInt in place.
Definition APInt.h:1469
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1640
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1599
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
void flipAllBits()
Toggle every bit to its opposite value.
Definition APInt.h:1453
unsigned countl_one() const
Count the number of leading one bits.
Definition APInt.h:1616
unsigned logBase2() const
Definition APInt.h:1762
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:476
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1941
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
unsigned countr_one() const
Count the number of trailing one bits.
Definition APInt.h:1657
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
an instruction to allocate memory on the stack
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
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:233
BinaryOps getOpcode() const
Definition InstrTypes.h:374
static LLVM_ABI BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI 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
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:664
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition InstrTypes.h:982
Predicate getStrictPredicate() const
For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
Definition InstrTypes.h:858
static LLVM_ABI Predicate getFlippedStrictnessPredicate(Predicate pred)
This is a static version that you can use without an instruction available.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:679
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:692
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:686
bool isSigned() const
Definition InstrTypes.h:930
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:942
static LLVM_ABI 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:871
static LLVM_ABI bool isStrictPredicate(Predicate predicate)
This is a static version that you can use without an instruction available.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:765
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
bool isUnsigned() const
Definition InstrTypes.h:936
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getNot(Constant *C)
static LLVM_ABI Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getXor(Constant *C1, Constant *C2)
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
static LLVM_ABI Constant * getZero(Type *Ty, bool Negative=false)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
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:264
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:131
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:157
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
This class represents a range of values.
LLVM_ABI ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
LLVM_ABI 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...
LLVM_ABI bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
LLVM_ABI 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.
LLVM_ABI ConstantRange difference(const ConstantRange &CR) const
Subtract the specified range from this range (aka relative complement of the sets).
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
LLVM_ABI ConstantRange truncate(uint32_t BitWidth, unsigned NoWrapKind=0) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
static LLVM_ABI 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...
LLVM_ABI ConstantRange inverse() const
Return a new range that is the logical not of the current set.
LLVM_ABI 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...
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
LLVM_ABI 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 LLVM_ABI 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 LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI 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...
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
LLVM_ABI const APInt & getUniqueInteger() const
If C is a constant integer then return its value, otherwise C must be a vector of constant integers,...
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:169
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.
static bool isEquality(Predicate Pred)
Represents flags for the getelementptr instruction/expression.
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
bool isInBounds() const
GEPNoWrapFlags intersectForOffsetAdd(GEPNoWrapFlags Other) const
Given (gep (gep p, x), y), determine the nowrap flags for (gep p, x+y).
static GEPNoWrapFlags none()
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition Operator.h:430
LLVM_ABI Type * getSourceElementType() const
Definition Operator.cpp:71
Value * getPointerOperand()
Definition Operator.h:457
GEPNoWrapFlags getNoWrapFlags() const
Definition Operator.h:425
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
Definition Operator.h:504
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
This instruction compares its operands according to the predicate given to the constructor.
static bool isGE(Predicate P)
Return true if the predicate is SGE or UGE.
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static bool isLT(Predicate P)
Return true if the predicate is SLT or ULT.
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
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.
static bool isLE(Predicate P)
Return true if the predicate is SLE or ULE.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1551
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:207
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2442
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1573
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition IRBuilder.h:538
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
Instruction * foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr, const APInt &C)
Fold icmp ({al}shr X, Y), C.
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, bool AllowMultipleUses=false)
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 * foldCmpLoadFromIndexedGlobal(LoadInst *LI, GetElementPtrInst *GEP, 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 * foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub, const APInt &C)
Fold icmp (sub X, Y), C.
Instruction * foldICmpWithClamp(ICmpInst &Cmp, Value *X, MinMaxIntrinsic *Min)
Match and fold patterns like: icmp eq/ne X, min(max(X, Lo), Hi) which represents a range check and ca...
Instruction * foldICmpInstWithConstantNotInt(ICmpInst &Cmp)
Handle icmp with constant (but not simple integer constant) RHS.
bool SimplifyDemandedBits(Instruction *I, unsigned Op, const APInt &DemandedMask, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0) override
This form of SimplifyDemandedBits simplifies the specified instruction operand if possible,...
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.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false, bool SimplifyBothArms=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Value * foldMultiplicationOverflowCheck(ICmpInst &Cmp)
Fold (-1 u/ x) u< y ((x * y) ?
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 * foldICmpCommutative(CmpPredicate Pred, Value *Op0, Value *Op1, ICmpInst &CxtI)
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 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 * 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.
Instruction * foldICmpAddOpConst(Value *X, const APInt &C, CmpPredicate Pred)
Fold "icmp pred (X+C), X".
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 * foldSelectICmp(CmpPredicate Pred, SelectInst *SI, Value *RHS, const ICmpInst &I)
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 * foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp)
Fold icmp eq (num + mask) & ~mask, num to icmp eq (and num, mask), 0 Where mask is a low bit mask.
Instruction * foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, const APInt &C1, const APInt &C2)
Fold icmp (and (sh X, Y), C2), C1.
Instruction * foldICmpBinOpWithConstantViaTruthTable(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
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)
Instruction * foldICmpWithMinMax(Instruction &I, MinMaxIntrinsic *MinMax, Value *Z, CmpPredicate Pred)
Fold icmp Pred min|max(X, Y), Z.
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 * visitICmpInst(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)
Instruction * foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, CmpPredicate Cond, Instruction &I)
Fold comparisons between a GEP instruction and something else.
The core instruction combiner logic.
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
SimplifyQuery SQ
unsigned ComputeMaxSignificantBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
IRBuilder< TargetFolder, IRBuilderCallbackInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI, bool IsNSW=false) const
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
TargetLibraryInfo & TLI
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
static bool isCanonicalPredicate(CmpPredicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
const DataLayout & DL
DomConditionCache DC
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
bool canFreelyInvertAllUsersOf(Instruction *V, Value *IgnoredUser)
Given i1 V, can every user of V be freely adapted if V is changed to !V ?
void addToWorklist(Instruction *I)
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
DominatorTree & DT
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
BuilderTy & Builder
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
const SimplifyQuery & getSimplifyQuery() const
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, const Instruction *CxtI=nullptr, unsigned Depth=0)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI 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.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI 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.
bool isShift() const
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:318
A wrapper class for inspecting calls to intrinsic functions.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
An instruction for reading from memory.
bool isVolatile() const
Return true if this is a load from a volatile memory location.
This class represents min/max intrinsics.
Value * getLHS() const
Value * getRHS() const
static bool isMin(Intrinsic::ID ID)
Whether the intrinsic is a smin or umin.
static bool isSigned(Intrinsic::ID ID)
Whether the intrinsic is signed or unsigned.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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, const Instruction *MDFrom=nullptr)
A vector that has set insertion semantics.
Definition SetVector.h:58
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:101
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:149
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition SetVector.h:250
This instruction constructs a fixed permutation of two input vectors.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition Type.h:165
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI 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
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:293
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
Definition Type.cpp:235
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:106
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
void setOperand(unsigned i, Value *Val)
Definition User.h:237
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition Value.cpp:158
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:701
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
iterator_range< use_iterator > uses()
Definition Value.h:380
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
const ParentTy * getParent() const
Definition ilist_node.h:34
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI 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:2763
LLVM_ABI 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:2781
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
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.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
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.
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.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
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)
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
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.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
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)
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
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.
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()...
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
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::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
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)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
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.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
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)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
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)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, 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.
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.
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.
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.
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.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
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.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
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:829
@ 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:1725
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI 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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI 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:632
LLVM_ABI Value * simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1516
LLVM_ABI 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.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
LLVM_ABI Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI 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:331
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:236
LLVM_ABI 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
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_UNKNOWN
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI LinearExpression decomposeLinearExpression(const DataLayout &DL, Value *Ptr)
Decompose a pointer into a linear expression.
Definition Loads.cpp:887
LLVM_ABI bool isFinite(const Loop *L)
Return true if this loop can be assumed to run for a finite number of iterations.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Returns: X * 2^Exp for integral exponents.
Definition APFloat.h:1525
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI 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...
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
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:1739
LLVM_ABI Value * simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI 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.
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:394
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
constexpr unsigned BitWidth
LLVM_ABI Constant * getLosslessInvCast(Constant *C, Type *InvCastTo, unsigned CastOp, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or CastOp(InvC) is a refined valu...
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:1961
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
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:2108
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
@ Continue
Definition DWP.h:22
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
LLVM_ABI 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.
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false, bool DecomposeAnd=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#define NC
Definition regutils.h:42
Value * materialize(InstCombiner::BuilderTy &Builder) const
static OffsetResult value(Value *V)
static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV)
static OffsetResult invalid()
This callback is used in conjunction with PointerMayBeCaptured.
static CommonPointerBase compute(Value *LHS, Value *RHS)
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:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:274
APInt getSignedMaxValue() const
Return the maximal signed value possible given these KnownBits.
Definition KnownBits.h:151
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:129
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition KnownBits.h:286
APInt getSignedMinValue() const
Return the minimal signed value possible given these KnownBits.
Definition KnownBits.h:135
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition KnownBits.h:60
Linear expression BasePtr + Index * Scale + Offset.
Definition Loads.h:203
GEPNoWrapFlags Flags
Definition Loads.h:208
Matching combinators.
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
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:257
Capture information for a specific Use.