LLVM 23.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"
29#include "llvm/IR/Instruction.h"
35#include <bitset>
36
37using namespace llvm;
38using namespace PatternMatch;
39
40#define DEBUG_TYPE "instcombine"
41
42// How many times is a select replaced by one of its operands?
43STATISTIC(NumSel, "Number of select opts");
44
45namespace llvm {
47}
48
49/// Compute Result = In1+In2, returning true if the result overflowed for this
50/// type.
51static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
52 bool IsSigned = false) {
53 bool Overflow;
54 if (IsSigned)
55 Result = In1.sadd_ov(In2, Overflow);
56 else
57 Result = In1.uadd_ov(In2, Overflow);
58
59 return Overflow;
60}
61
62/// Compute Result = In1-In2, returning true if the result overflowed for this
63/// type.
64static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
65 bool IsSigned = false) {
66 bool Overflow;
67 if (IsSigned)
68 Result = In1.ssub_ov(In2, Overflow);
69 else
70 Result = In1.usub_ov(In2, Overflow);
71
72 return Overflow;
73}
74
75/// Given an icmp instruction, return true if any use of this comparison is a
76/// branch on sign bit comparison.
77static bool hasBranchUse(ICmpInst &I) {
78 for (auto *U : I.users())
79 if (isa<CondBrInst>(U))
80 return true;
81 return false;
82}
83
84/// Returns true if the exploded icmp can be expressed as a signed comparison
85/// to zero and updates the predicate accordingly.
86/// The signedness of the comparison is preserved.
87/// TODO: Refactor with decomposeBitTestICmp()?
88static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
89 if (!ICmpInst::isSigned(Pred))
90 return false;
91
92 if (C.isZero())
93 return ICmpInst::isRelational(Pred);
94
95 if (C.isOne()) {
96 if (Pred == ICmpInst::ICMP_SLT) {
97 Pred = ICmpInst::ICMP_SLE;
98 return true;
99 }
100 } else if (C.isAllOnes()) {
101 if (Pred == ICmpInst::ICMP_SGT) {
102 Pred = ICmpInst::ICMP_SGE;
103 return true;
104 }
105 }
106
107 return false;
108}
109
110/// This is called when we see this pattern:
111/// cmp pred (load (gep GV, ...)), cmpcst
112/// where GV is a global variable with a constant initializer. Try to simplify
113/// this into some simple computation that does not need the load. For example
114/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
115///
116/// If AndCst is non-null, then the loaded value is masked with that constant
117/// before doing the comparison. This handles cases like "A[i]&4 == 0".
119 LoadInst *LI, GetElementPtrInst *GEP, CmpInst &ICI, ConstantInt *AndCst) {
121 if (LI->isVolatile() || !GV || !GV->isConstant() ||
122 !GV->hasDefinitiveInitializer())
123 return nullptr;
124
125 Type *EltTy = LI->getType();
126 TypeSize EltSize = DL.getTypeStoreSize(EltTy);
127 if (EltSize.isScalable())
128 return nullptr;
129
131 if (!Expr.Index || Expr.BasePtr != GV || Expr.Offset.getBitWidth() > 64)
132 return nullptr;
133
134 Constant *Init = GV->getInitializer();
135 TypeSize GlobalSize = DL.getTypeAllocSize(Init->getType());
136
137 Value *Idx = Expr.Index;
138 const APInt &Stride = Expr.Scale;
139 const APInt &ConstOffset = Expr.Offset;
140
141 // Allow an additional context offset, but only within the stride.
142 if (!ConstOffset.ult(Stride))
143 return nullptr;
144
145 // Don't handle overlapping loads for now.
146 if (!Stride.uge(EltSize.getFixedValue()))
147 return nullptr;
148
149 // Don't blow up on huge arrays.
150 uint64_t ArrayElementCount =
151 divideCeil((GlobalSize.getFixedValue() - ConstOffset.getZExtValue()),
152 Stride.getZExtValue());
153 if (ArrayElementCount > MaxArraySizeForCombine)
154 return nullptr;
155
156 enum { Overdefined = -3, Undefined = -2 };
157
158 // Variables for our state machines.
159
160 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
161 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
162 // and 87 is the second (and last) index. FirstTrueElement is -2 when
163 // undefined, otherwise set to the first true element. SecondTrueElement is
164 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
165 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
166
167 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
168 // form "i != 47 & i != 87". Same state transitions as for true elements.
169 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
170
171 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
172 /// define a state machine that triggers for ranges of values that the index
173 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
174 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
175 /// index in the range (inclusive). We use -2 for undefined here because we
176 /// use relative comparisons and don't want 0-1 to match -1.
177 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
178
179 // MagicBitvector - This is a magic bitvector where we set a bit if the
180 // comparison is true for element 'i'. If there are 64 elements or less in
181 // the array, this will fully represent all the comparison results.
182 uint64_t MagicBitvector = 0;
183
184 // Scan the array and see if one of our patterns matches.
185 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
186 APInt Offset = ConstOffset;
187 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i, Offset += Stride) {
189 if (!Elt)
190 return nullptr;
191
192 // If the element is masked, handle it.
193 if (AndCst) {
194 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);
195 if (!Elt)
196 return nullptr;
197 }
198
199 // Find out if the comparison would be true or false for the i'th element.
201 CompareRHS, DL, &TLI);
202 if (!C)
203 return nullptr;
204
205 // If the result is undef for this element, ignore it.
206 if (isa<UndefValue>(C)) {
207 // Extend range state machines to cover this element in case there is an
208 // undef in the middle of the range.
209 if (TrueRangeEnd == (int)i - 1)
210 TrueRangeEnd = i;
211 if (FalseRangeEnd == (int)i - 1)
212 FalseRangeEnd = i;
213 continue;
214 }
215
216 // If we can't compute the result for any of the elements, we have to give
217 // up evaluating the entire conditional.
218 if (!isa<ConstantInt>(C))
219 return nullptr;
220
221 // Otherwise, we know if the comparison is true or false for this element,
222 // update our state machines.
223 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
224
225 // State machine for single/double/range index comparison.
226 if (IsTrueForElt) {
227 // Update the TrueElement state machine.
228 if (FirstTrueElement == Undefined)
229 FirstTrueElement = TrueRangeEnd = i; // First true element.
230 else {
231 // Update double-compare state machine.
232 if (SecondTrueElement == Undefined)
233 SecondTrueElement = i;
234 else
235 SecondTrueElement = Overdefined;
236
237 // Update range state machine.
238 if (TrueRangeEnd == (int)i - 1)
239 TrueRangeEnd = i;
240 else
241 TrueRangeEnd = Overdefined;
242 }
243 } else {
244 // Update the FalseElement state machine.
245 if (FirstFalseElement == Undefined)
246 FirstFalseElement = FalseRangeEnd = i; // First false element.
247 else {
248 // Update double-compare state machine.
249 if (SecondFalseElement == Undefined)
250 SecondFalseElement = i;
251 else
252 SecondFalseElement = Overdefined;
253
254 // Update range state machine.
255 if (FalseRangeEnd == (int)i - 1)
256 FalseRangeEnd = i;
257 else
258 FalseRangeEnd = Overdefined;
259 }
260 }
261
262 // If this element is in range, update our magic bitvector.
263 if (i < 64 && IsTrueForElt)
264 MagicBitvector |= 1ULL << i;
265
266 // If all of our states become overdefined, bail out early. Since the
267 // predicate is expensive, only check it every 8 elements. This is only
268 // really useful for really huge arrays.
269 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
270 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
271 FalseRangeEnd == Overdefined)
272 return nullptr;
273 }
274
275 // Now that we've scanned the entire array, emit our new comparison(s). We
276 // order the state machines in complexity of the generated code.
277
278 // If inbounds keyword is not present, Idx * Stride can overflow.
279 // Let's assume that Stride is 2 and the wanted value is at offset 0.
280 // Then, there are two possible values for Idx to match offset 0:
281 // 0x00..00, 0x80..00.
282 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
283 // comparison is false if Idx was 0x80..00.
284 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.
285 auto MaskIdx = [&](Value *Idx) {
286 if (!Expr.Flags.isInBounds() && Stride.countr_zero() != 0) {
288 Mask = Builder.CreateLShr(Mask, Stride.countr_zero());
289 Idx = Builder.CreateAnd(Idx, Mask);
290 }
291 return Idx;
292 };
293
294 // If the comparison is only true for one or two elements, emit direct
295 // comparisons.
296 if (SecondTrueElement != Overdefined) {
297 Idx = MaskIdx(Idx);
298 // None true -> false.
299 if (FirstTrueElement == Undefined)
300 return replaceInstUsesWith(ICI, Builder.getFalse());
301
302 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
303
304 // True for one element -> 'i == 47'.
305 if (SecondTrueElement == Undefined)
306 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
307
308 // True for two elements -> 'i == 47 | i == 72'.
309 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx);
310 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
311 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx);
312 return BinaryOperator::CreateOr(C1, C2);
313 }
314
315 // If the comparison is only false for one or two elements, emit direct
316 // comparisons.
317 if (SecondFalseElement != Overdefined) {
318 Idx = MaskIdx(Idx);
319 // None false -> true.
320 if (FirstFalseElement == Undefined)
321 return replaceInstUsesWith(ICI, Builder.getTrue());
322
323 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
324
325 // False for one element -> 'i != 47'.
326 if (SecondFalseElement == Undefined)
327 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
328
329 // False for two elements -> 'i != 47 & i != 72'.
330 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx);
331 Value *SecondFalseIdx =
332 ConstantInt::get(Idx->getType(), SecondFalseElement);
333 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx);
334 return BinaryOperator::CreateAnd(C1, C2);
335 }
336
337 // If the comparison can be replaced with a range comparison for the elements
338 // where it is true, emit the range check.
339 if (TrueRangeEnd != Overdefined) {
340 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
341 Idx = MaskIdx(Idx);
342
343 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
344 if (FirstTrueElement) {
345 Value *Offs = ConstantInt::getSigned(Idx->getType(), -FirstTrueElement);
346 Idx = Builder.CreateAdd(Idx, Offs);
347 }
348
349 Value *End =
350 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1);
351 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
352 }
353
354 // False range check.
355 if (FalseRangeEnd != Overdefined) {
356 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
357 Idx = MaskIdx(Idx);
358 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
359 if (FirstFalseElement) {
360 Value *Offs = ConstantInt::getSigned(Idx->getType(), -FirstFalseElement);
361 Idx = Builder.CreateAdd(Idx, Offs);
362 }
363
364 Value *End =
365 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement);
366 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
367 }
368
369 // If a magic bitvector captures the entire comparison state
370 // of this load, replace it with computation that does:
371 // ((magic_cst >> i) & 1) != 0
372 {
373 Type *Ty = nullptr;
374
375 // Look for an appropriate type:
376 // - The type of Idx if the magic fits
377 // - The smallest fitting legal type
378 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
379 Ty = Idx->getType();
380 else
381 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
382
383 if (Ty) {
384 Idx = MaskIdx(Idx);
385 Value *V = Builder.CreateIntCast(Idx, Ty, false);
386 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
387 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V);
388 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
389 }
390 }
391
392 return nullptr;
393}
394
395/// Returns true if we can rewrite Start as a GEP with pointer Base
396/// and some integer offset. The nodes that need to be re-written
397/// for this transformation will be added to Explored.
399 const DataLayout &DL,
400 SetVector<Value *> &Explored) {
401 SmallVector<Value *, 16> WorkList(1, Start);
402 Explored.insert(Base);
403
404 // The following traversal gives us an order which can be used
405 // when doing the final transformation. Since in the final
406 // transformation we create the PHI replacement instructions first,
407 // we don't have to get them in any particular order.
408 //
409 // However, for other instructions we will have to traverse the
410 // operands of an instruction first, which means that we have to
411 // do a post-order traversal.
412 while (!WorkList.empty()) {
414
415 while (!WorkList.empty()) {
416 if (Explored.size() >= 100)
417 return false;
418
419 Value *V = WorkList.back();
420
421 if (Explored.contains(V)) {
422 WorkList.pop_back();
423 continue;
424 }
425
427 // We've found some value that we can't explore which is different from
428 // the base. Therefore we can't do this transformation.
429 return false;
430
431 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
432 // Only allow inbounds GEPs with at most one variable offset.
433 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); };
434 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1)
435 return false;
436
437 NW = NW.intersectForOffsetAdd(GEP->getNoWrapFlags());
438 if (!Explored.contains(GEP->getOperand(0)))
439 WorkList.push_back(GEP->getOperand(0));
440 }
441
442 if (WorkList.back() == V) {
443 WorkList.pop_back();
444 // We've finished visiting this node, mark it as such.
445 Explored.insert(V);
446 }
447
448 if (auto *PN = dyn_cast<PHINode>(V)) {
449 // We cannot transform PHIs on unsplittable basic blocks.
450 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
451 return false;
452 Explored.insert(PN);
453 PHIs.insert(PN);
454 }
455 }
456
457 // Explore the PHI nodes further.
458 for (auto *PN : PHIs)
459 for (Value *Op : PN->incoming_values())
460 if (!Explored.contains(Op))
461 WorkList.push_back(Op);
462 }
463
464 // Make sure that we can do this. Since we can't insert GEPs in a basic
465 // block before a PHI node, we can't easily do this transformation if
466 // we have PHI node users of transformed instructions.
467 for (Value *Val : Explored) {
468 for (Value *Use : Val->uses()) {
469
470 auto *PHI = dyn_cast<PHINode>(Use);
471 auto *Inst = dyn_cast<Instruction>(Val);
472
473 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
474 !Explored.contains(PHI))
475 continue;
476
477 if (PHI->getParent() == Inst->getParent())
478 return false;
479 }
480 }
481 return true;
482}
483
484// Sets the appropriate insert point on Builder where we can add
485// a replacement Instruction for V (if that is possible).
486static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
487 bool Before = true) {
488 if (auto *PHI = dyn_cast<PHINode>(V)) {
489 BasicBlock *Parent = PHI->getParent();
490 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
491 return;
492 }
493 if (auto *I = dyn_cast<Instruction>(V)) {
494 if (!Before)
495 I = &*std::next(I->getIterator());
496 Builder.SetInsertPoint(I);
497 return;
498 }
499 if (auto *A = dyn_cast<Argument>(V)) {
500 // Set the insertion point in the entry block.
501 BasicBlock &Entry = A->getParent()->getEntryBlock();
502 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt());
503 return;
504 }
505 // Otherwise, this is a constant and we don't need to set a new
506 // insertion point.
507 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
508}
509
510/// Returns a re-written value of Start as an indexed GEP using Base as a
511/// pointer.
513 const DataLayout &DL,
514 SetVector<Value *> &Explored,
515 InstCombiner &IC) {
516 // Perform all the substitutions. This is a bit tricky because we can
517 // have cycles in our use-def chains.
518 // 1. Create the PHI nodes without any incoming values.
519 // 2. Create all the other values.
520 // 3. Add the edges for the PHI nodes.
521 // 4. Emit GEPs to get the original pointers.
522 // 5. Remove the original instructions.
523 Type *IndexType = IntegerType::get(
524 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType()));
525
527 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
528
529 // Create the new PHI nodes, without adding any incoming values.
530 for (Value *Val : Explored) {
531 if (Val == Base)
532 continue;
533 // Create empty phi nodes. This avoids cyclic dependencies when creating
534 // the remaining instructions.
535 if (auto *PHI = dyn_cast<PHINode>(Val))
536 NewInsts[PHI] =
537 PHINode::Create(IndexType, PHI->getNumIncomingValues(),
538 PHI->getName() + ".idx", PHI->getIterator());
539 }
540 IRBuilder<> Builder(Base->getContext());
541
542 // Create all the other instructions.
543 for (Value *Val : Explored) {
544 if (NewInsts.contains(Val))
545 continue;
546
547 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
548 setInsertionPoint(Builder, GEP);
549 Value *Op = NewInsts[GEP->getOperand(0)];
550 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);
552 NewInsts[GEP] = OffsetV;
553 else
554 NewInsts[GEP] = Builder.CreateAdd(
555 Op, OffsetV, GEP->getOperand(0)->getName() + ".add",
556 /*NUW=*/NW.hasNoUnsignedWrap(),
557 /*NSW=*/NW.hasNoUnsignedSignedWrap());
558 continue;
559 }
560 if (isa<PHINode>(Val))
561 continue;
562
563 llvm_unreachable("Unexpected instruction type");
564 }
565
566 // Add the incoming values to the PHI nodes.
567 for (Value *Val : Explored) {
568 if (Val == Base)
569 continue;
570 // All the instructions have been created, we can now add edges to the
571 // phi nodes.
572 if (auto *PHI = dyn_cast<PHINode>(Val)) {
573 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
574 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
575 Value *NewIncoming = PHI->getIncomingValue(I);
576
577 auto It = NewInsts.find(NewIncoming);
578 if (It != NewInsts.end())
579 NewIncoming = It->second;
580
581 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
582 }
583 }
584 }
585
586 for (Value *Val : Explored) {
587 if (Val == Base)
588 continue;
589
590 setInsertionPoint(Builder, Val, false);
591 // Create GEP for external users.
592 Value *NewVal = Builder.CreateGEP(Builder.getInt8Ty(), Base, NewInsts[Val],
593 Val->getName() + ".ptr", NW);
594 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal);
595 // Add old instruction to worklist for DCE. We don't directly remove it
596 // here because the original compare is one of the users.
598 }
599
600 return NewInsts[Start];
601}
602
603/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
604/// We can look through PHIs, GEPs and casts in order to determine a common base
605/// between GEPLHS and RHS.
608 const DataLayout &DL,
609 InstCombiner &IC) {
610 // FIXME: Support vector of pointers.
611 if (GEPLHS->getType()->isVectorTy())
612 return nullptr;
613
614 if (!GEPLHS->hasAllConstantIndices())
615 return nullptr;
616
617 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
618 Value *PtrBase =
620 /*AllowNonInbounds*/ false);
621
622 // Bail if we looked through addrspacecast.
623 if (PtrBase->getType() != GEPLHS->getType())
624 return nullptr;
625
626 // The set of nodes that will take part in this transformation.
627 SetVector<Value *> Nodes;
628 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
629 if (!canRewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes))
630 return nullptr;
631
632 // We know we can re-write this as
633 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
634 // Since we've only looked through inbouds GEPs we know that we
635 // can't have overflow on either side. We can therefore re-write
636 // this as:
637 // OFFSET1 cmp OFFSET2
638 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes, IC);
639
640 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
641 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
642 // offset. Since Index is the offset of LHS to the base pointer, we will now
643 // compare the offsets instead of comparing the pointers.
645 IC.Builder.getInt(Offset), NewRHS);
646}
647
648/// Fold comparisons between a GEP instruction and something else. At this point
649/// we know that the GEP is on the LHS of the comparison.
652 // Don't transform signed compares of GEPs into index compares. Even if the
653 // GEP is inbounds, the final add of the base pointer can have signed overflow
654 // and would change the result of the icmp.
655 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
656 // the maximum signed value for the pointer type.
658 return nullptr;
659
660 // Look through bitcasts and addrspacecasts. We do not however want to remove
661 // 0 GEPs.
662 if (!isa<GetElementPtrInst>(RHS))
663 RHS = RHS->stripPointerCasts();
664
665 auto CanFold = [Cond](GEPNoWrapFlags NW) {
667 return true;
668
669 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags.
671 return NW != GEPNoWrapFlags::none();
672 };
673
674 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) {
675 if (!NW.hasNoUnsignedWrap()) {
676 // Convert signed to unsigned comparison.
677 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Op1, Op2);
678 }
679
680 auto *I = new ICmpInst(Cond, Op1, Op2);
681 I->setSameSign(NW.hasNoUnsignedSignedWrap());
682 return I;
683 };
684
686 if (Base.Ptr == RHS && CanFold(Base.LHSNW) && !Base.isExpensive()) {
687 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
688 Type *IdxTy = DL.getIndexType(GEPLHS->getType());
689 Value *Offset =
690 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);
691 return NewICmp(Base.LHSNW, Offset,
692 Constant::getNullValue(Offset->getType()));
693 }
694
695 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
697 !NullPointerIsDefined(I.getFunction(),
698 RHS->getType()->getPointerAddressSpace())) {
699 // For most address spaces, an allocation can't be placed at null, but null
700 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
701 // the only valid inbounds address derived from null, is null itself.
702 // Thus, we have four cases to consider:
703 // 1) Base == nullptr, Offset == 0 -> inbounds, null
704 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
705 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
706 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
707 //
708 // (Note if we're indexing a type of size 0, that simply collapses into one
709 // of the buckets above.)
710 //
711 // In general, we're allowed to make values less poison (i.e. remove
712 // sources of full UB), so in this case, we just select between the two
713 // non-poison cases (1 and 4 above).
714 //
715 // For vectors, we apply the same reasoning on a per-lane basis.
716 auto *Base = GEPLHS->getPointerOperand();
717 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
718 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount();
719 Base = Builder.CreateVectorSplat(EC, Base);
720 }
721 return new ICmpInst(Cond, Base,
723 cast<Constant>(RHS), Base->getType()));
724 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
725 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
726
727 // If the base pointers are different, but the indices are the same, just
728 // compare the base pointer.
729 if (GEPLHS->getOperand(0) != GEPRHS->getOperand(0)) {
730 bool IndicesTheSame =
731 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
732 GEPLHS->getPointerOperand()->getType() ==
733 GEPRHS->getPointerOperand()->getType() &&
734 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
735 if (IndicesTheSame)
736 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
737 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
738 IndicesTheSame = false;
739 break;
740 }
741
742 // If all indices are the same, just compare the base pointers.
743 Type *BaseType = GEPLHS->getOperand(0)->getType();
744 if (IndicesTheSame &&
745 CmpInst::makeCmpResultType(BaseType) == I.getType() && CanFold(NW))
746 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
747
748 // If we're comparing GEPs with two base pointers that only differ in type
749 // and both GEPs have only constant indices or just one use, then fold
750 // the compare with the adjusted indices.
751 // FIXME: Support vector of pointers.
752 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
753 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
754 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
755 GEPLHS->getOperand(0)->stripPointerCasts() ==
756 GEPRHS->getOperand(0)->stripPointerCasts() &&
757 !GEPLHS->getType()->isVectorTy()) {
758 Value *LOffset = EmitGEPOffset(GEPLHS);
759 Value *ROffset = EmitGEPOffset(GEPRHS);
760
761 // If we looked through an addrspacecast between different sized address
762 // spaces, the LHS and RHS pointers are different sized
763 // integers. Truncate to the smaller one.
764 Type *LHSIndexTy = LOffset->getType();
765 Type *RHSIndexTy = ROffset->getType();
766 if (LHSIndexTy != RHSIndexTy) {
767 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <
768 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {
769 ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy);
770 } else
771 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy);
772 }
773
775 LOffset, ROffset);
776 return replaceInstUsesWith(I, Cmp);
777 }
778 }
779
780 if (GEPLHS->getOperand(0) == GEPRHS->getOperand(0) &&
781 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
782 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
783 // If the GEPs only differ by one index, compare it.
784 unsigned NumDifferences = 0; // Keep track of # differences.
785 unsigned DiffOperand = 0; // The operand that differs.
786 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
787 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
788 Type *LHSType = GEPLHS->getOperand(i)->getType();
789 Type *RHSType = GEPRHS->getOperand(i)->getType();
790 // FIXME: Better support for vector of pointers.
791 if (LHSType->getPrimitiveSizeInBits() !=
792 RHSType->getPrimitiveSizeInBits() ||
793 (GEPLHS->getType()->isVectorTy() &&
794 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
795 // Irreconcilable differences.
796 NumDifferences = 2;
797 break;
798 }
799
800 if (NumDifferences++)
801 break;
802 DiffOperand = i;
803 }
804
805 if (NumDifferences == 0) // SAME GEP?
806 return replaceInstUsesWith(
807 I, // No comparison is needed here.
808 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));
809 // If two GEPs only differ by an index, compare them.
810 // Note that nowrap flags are always needed when comparing two indices.
811 else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {
812 Value *LHSV = GEPLHS->getOperand(DiffOperand);
813 Value *RHSV = GEPRHS->getOperand(DiffOperand);
814 return NewICmp(NW, LHSV, RHSV);
815 }
816 }
817
818 if (Base.Ptr && CanFold(Base.LHSNW & Base.RHSNW) && !Base.isExpensive()) {
819 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
820 Type *IdxTy = DL.getIndexType(GEPLHS->getType());
821 Value *L =
822 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEP=*/true);
823 Value *R =
824 EmitGEPOffsets(Base.RHSGEPs, Base.RHSNW, IdxTy, /*RewriteGEP=*/true);
825 return NewICmp(Base.LHSNW & Base.RHSNW, L, R);
826 }
827 }
828
829 // Try convert this to an indexed compare by looking through PHIs/casts as a
830 // last resort.
831 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
832}
833
835 // It would be tempting to fold away comparisons between allocas and any
836 // pointer not based on that alloca (e.g. an argument). However, even
837 // though such pointers cannot alias, they can still compare equal.
838 //
839 // But LLVM doesn't specify where allocas get their memory, so if the alloca
840 // doesn't escape we can argue that it's impossible to guess its value, and we
841 // can therefore act as if any such guesses are wrong.
842 //
843 // However, we need to ensure that this folding is consistent: We can't fold
844 // one comparison to false, and then leave a different comparison against the
845 // same value alone (as it might evaluate to true at runtime, leading to a
846 // contradiction). As such, this code ensures that all comparisons are folded
847 // at the same time, and there are no other escapes.
848
849 struct CmpCaptureTracker : public CaptureTracker {
850 AllocaInst *Alloca;
851 bool Captured = false;
852 /// The value of the map is a bit mask of which icmp operands the alloca is
853 /// used in.
855
856 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
857
858 void tooManyUses() override { Captured = true; }
859
860 Action captured(const Use *U, UseCaptureInfo CI) override {
861 // TODO(captures): Use UseCaptureInfo.
862 auto *ICmp = dyn_cast<ICmpInst>(U->getUser());
863 // We need to check that U is based *only* on the alloca, and doesn't
864 // have other contributions from a select/phi operand.
865 // TODO: We could check whether getUnderlyingObjects() reduces to one
866 // object, which would allow looking through phi nodes.
867 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) {
868 // Collect equality icmps of the alloca, and don't treat them as
869 // captures.
870 ICmps[ICmp] |= 1u << U->getOperandNo();
871 return Continue;
872 }
873
874 Captured = true;
875 return Stop;
876 }
877 };
878
879 CmpCaptureTracker Tracker(Alloca);
880 PointerMayBeCaptured(Alloca, &Tracker);
881 if (Tracker.Captured)
882 return false;
883
884 bool Changed = false;
885 for (auto [ICmp, Operands] : Tracker.ICmps) {
886 switch (Operands) {
887 case 1:
888 case 2: {
889 // The alloca is only used in one icmp operand. Assume that the
890 // equality is false.
891 auto *Res = ConstantInt::get(ICmp->getType(),
892 ICmp->getPredicate() == ICmpInst::ICMP_NE);
893 replaceInstUsesWith(*ICmp, Res);
895 Changed = true;
896 break;
897 }
898 case 3:
899 // Both icmp operands are based on the alloca, so this is comparing
900 // pointer offsets, without leaking any information about the address
901 // of the alloca. Ignore such comparisons.
902 break;
903 default:
904 llvm_unreachable("Cannot happen");
905 }
906 }
907
908 return Changed;
909}
910
911/// Fold "icmp pred (X+C), X".
913 CmpPredicate Pred) {
914 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
915 // so the values can never be equal. Similarly for all other "or equals"
916 // operators.
917 assert(!!C && "C should not be zero!");
918
919 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
920 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
921 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
922 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
923 Constant *R =
924 ConstantInt::get(X->getType(), APInt::getMaxValue(C.getBitWidth()) - C);
925 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
926 }
927
928 // (X+1) >u X --> X <u (0-1) --> X != 255
929 // (X+2) >u X --> X <u (0-2) --> X <u 254
930 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
931 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
932 return new ICmpInst(ICmpInst::ICMP_ULT, X,
933 ConstantInt::get(X->getType(), -C));
934
935 APInt SMax = APInt::getSignedMaxValue(C.getBitWidth());
936
937 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
938 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
939 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
940 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
941 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
942 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
943 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
944 return new ICmpInst(ICmpInst::ICMP_SGT, X,
945 ConstantInt::get(X->getType(), SMax - C));
946
947 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
948 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
949 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
950 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
951 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
952 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
953
954 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
955 return new ICmpInst(ICmpInst::ICMP_SLT, X,
956 ConstantInt::get(X->getType(), SMax - (C - 1)));
957}
958
959/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
960/// (icmp eq/ne A, Log2(AP2/AP1)) ->
961/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
963 const APInt &AP1,
964 const APInt &AP2) {
965 assert(I.isEquality() && "Cannot fold icmp gt/lt");
966
967 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
968 if (I.getPredicate() == I.ICMP_NE)
969 Pred = CmpInst::getInversePredicate(Pred);
970 return new ICmpInst(Pred, LHS, RHS);
971 };
972
973 // Don't bother doing any work for cases which InstSimplify handles.
974 if (AP2.isZero())
975 return nullptr;
976
977 bool IsAShr = isa<AShrOperator>(I.getOperand(0));
978 if (IsAShr) {
979 if (AP2.isAllOnes())
980 return nullptr;
981 if (AP2.isNegative() != AP1.isNegative())
982 return nullptr;
983 if (AP2.sgt(AP1))
984 return nullptr;
985 }
986
987 if (!AP1)
988 // 'A' must be large enough to shift out the highest set bit.
989 return getICmp(I.ICMP_UGT, A,
990 ConstantInt::get(A->getType(), AP2.logBase2()));
991
992 if (AP1 == AP2)
993 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
994
995 int Shift;
996 if (IsAShr && AP1.isNegative())
997 Shift = AP1.countl_one() - AP2.countl_one();
998 else
999 Shift = AP1.countl_zero() - AP2.countl_zero();
1000
1001 if (Shift > 0) {
1002 if (IsAShr && AP1 == AP2.ashr(Shift)) {
1003 // There are multiple solutions if we are comparing against -1 and the LHS
1004 // of the ashr is not a power of two.
1005 if (AP1.isAllOnes() && !AP2.isPowerOf2())
1006 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));
1007 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1008 } else if (AP1 == AP2.lshr(Shift)) {
1009 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1010 }
1011 }
1012
1013 // Shifting const2 will never be equal to const1.
1014 // FIXME: This should always be handled by InstSimplify?
1015 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1016 return replaceInstUsesWith(I, TorF);
1017}
1018
1019/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1020/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1022 const APInt &AP1,
1023 const APInt &AP2) {
1024 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1025
1026 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1027 if (I.getPredicate() == I.ICMP_NE)
1028 Pred = CmpInst::getInversePredicate(Pred);
1029 return new ICmpInst(Pred, LHS, RHS);
1030 };
1031
1032 // Don't bother doing any work for cases which InstSimplify handles.
1033 if (AP2.isZero())
1034 return nullptr;
1035
1036 unsigned AP2TrailingZeros = AP2.countr_zero();
1037
1038 if (!AP1 && AP2TrailingZeros != 0)
1039 return getICmp(
1040 I.ICMP_UGE, A,
1041 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
1042
1043 if (AP1 == AP2)
1044 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1045
1046 // Get the distance between the lowest bits that are set.
1047 int Shift = AP1.countr_zero() - AP2TrailingZeros;
1048
1049 if (Shift > 0 && AP2.shl(Shift) == AP1)
1050 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1051
1052 // Shifting const2 will never be equal to const1.
1053 // FIXME: This should always be handled by InstSimplify?
1054 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1055 return replaceInstUsesWith(I, TorF);
1056}
1057
1058/// The caller has matched a pattern of the form:
1059/// I = icmp ugt (add (add A, B), CI2), CI1
1060/// If this is of the form:
1061/// sum = a + b
1062/// if (sum+128 >u 255)
1063/// Then replace it with llvm.sadd.with.overflow.i8.
1064///
1066 ConstantInt *CI2, ConstantInt *CI1,
1067 InstCombinerImpl &IC) {
1068 // The transformation we're trying to do here is to transform this into an
1069 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1070 // with a narrower add, and discard the add-with-constant that is part of the
1071 // range check (if we can't eliminate it, this isn't profitable).
1072
1073 // In order to eliminate the add-with-constant, the compare can be its only
1074 // use.
1075 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1076 if (!AddWithCst->hasOneUse())
1077 return nullptr;
1078
1079 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1080 if (!CI2->getValue().isPowerOf2())
1081 return nullptr;
1082 unsigned NewWidth = CI2->getValue().countr_zero();
1083 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1084 return nullptr;
1085
1086 // The width of the new add formed is 1 more than the bias.
1087 ++NewWidth;
1088
1089 // Check to see that CI1 is an all-ones value with NewWidth bits.
1090 if (CI1->getBitWidth() == NewWidth ||
1091 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1092 return nullptr;
1093
1094 // This is only really a signed overflow check if the inputs have been
1095 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1096 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1097 if (IC.ComputeMaxSignificantBits(A, &I) > NewWidth ||
1098 IC.ComputeMaxSignificantBits(B, &I) > NewWidth)
1099 return nullptr;
1100
1101 // In order to replace the original add with a narrower
1102 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1103 // and truncates that discard the high bits of the add. Verify that this is
1104 // the case.
1105 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1106 for (User *U : OrigAdd->users()) {
1107 if (U == AddWithCst)
1108 continue;
1109
1110 // Only accept truncates for now. We would really like a nice recursive
1111 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1112 // chain to see which bits of a value are actually demanded. If the
1113 // original add had another add which was then immediately truncated, we
1114 // could still do the transformation.
1116 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1117 return nullptr;
1118 }
1119
1120 // If the pattern matches, truncate the inputs to the narrower type and
1121 // use the sadd_with_overflow intrinsic to efficiently compute both the
1122 // result and the overflow bit.
1123 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1125 I.getModule(), Intrinsic::sadd_with_overflow, NewType);
1126
1127 InstCombiner::BuilderTy &Builder = IC.Builder;
1128
1129 // Put the new code above the original add, in case there are any uses of the
1130 // add between the add and the compare.
1131 Builder.SetInsertPoint(OrigAdd);
1132
1133 Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc");
1134 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc");
1135 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd");
1136 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result");
1137 Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType());
1138
1139 // The inner add was the result of the narrow add, zero extended to the
1140 // wider type. Replace it with the result computed by the intrinsic.
1141 IC.replaceInstUsesWith(*OrigAdd, ZExt);
1142 IC.eraseInstFromFunction(*OrigAdd);
1143
1144 // The original icmp gets replaced with the overflow value.
1145 return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1146}
1147
1148/// If we have:
1149/// icmp eq/ne (urem/srem %x, %y), 0
1150/// iff %y is a power-of-two, we can replace this with a bit test:
1151/// icmp eq/ne (and %x, (add %y, -1)), 0
1153 // This fold is only valid for equality predicates.
1154 if (!I.isEquality())
1155 return nullptr;
1156 CmpPredicate Pred;
1157 Value *X, *Y, *Zero;
1158 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))),
1159 m_CombineAnd(m_Zero(), m_Value(Zero)))))
1160 return nullptr;
1161 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, &I))
1162 return nullptr;
1163 // This may increase instruction count, we don't enforce that Y is a constant.
1164 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType()));
1165 Value *Masked = Builder.CreateAnd(X, Mask);
1166 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, Zero);
1167}
1168
1169/// Fold equality-comparison between zero and any (maybe truncated) right-shift
1170/// by one-less-than-bitwidth into a sign test on the original value.
1172 Instruction *Val;
1173 CmpPredicate Pred;
1174 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero())))
1175 return nullptr;
1176
1177 Value *X;
1178 Type *XTy;
1179
1180 Constant *C;
1181 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) {
1182 XTy = X->getType();
1183 unsigned XBitWidth = XTy->getScalarSizeInBits();
1185 APInt(XBitWidth, XBitWidth - 1))))
1186 return nullptr;
1187 } else if (isa<BinaryOperator>(Val) &&
1189 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val),
1190 /*AnalyzeForSignBitExtraction=*/true))) {
1191 XTy = X->getType();
1192 } else
1193 return nullptr;
1194
1195 return ICmpInst::Create(Instruction::ICmp,
1199}
1200
1201// Handle icmp pred X, 0
1203 CmpInst::Predicate Pred = Cmp.getPredicate();
1204 if (!match(Cmp.getOperand(1), m_Zero()))
1205 return nullptr;
1206
1207 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1208 if (Pred == ICmpInst::ICMP_SGT) {
1209 Value *A, *B;
1210 if (match(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) {
1211 if (isKnownPositive(A, SQ.getWithInstruction(&Cmp)))
1212 return new ICmpInst(Pred, B, Cmp.getOperand(1));
1213 if (isKnownPositive(B, SQ.getWithInstruction(&Cmp)))
1214 return new ICmpInst(Pred, A, Cmp.getOperand(1));
1215 }
1216 }
1217
1219 return New;
1220
1221 // Given:
1222 // icmp eq/ne (urem %x, %y), 0
1223 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
1224 // icmp eq/ne %x, 0
1225 Value *X, *Y;
1226 if (match(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) &&
1227 ICmpInst::isEquality(Pred)) {
1228 KnownBits XKnown = computeKnownBits(X, &Cmp);
1229 KnownBits YKnown = computeKnownBits(Y, &Cmp);
1230 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1231 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1232 }
1233
1234 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are
1235 // odd/non-zero/there is no overflow.
1236 if (match(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) &&
1237 ICmpInst::isEquality(Pred)) {
1238
1239 KnownBits XKnown = computeKnownBits(X, &Cmp);
1240 // if X % 2 != 0
1241 // (icmp eq/ne Y)
1242 if (XKnown.countMaxTrailingZeros() == 0)
1243 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1244
1245 KnownBits YKnown = computeKnownBits(Y, &Cmp);
1246 // if Y % 2 != 0
1247 // (icmp eq/ne X)
1248 if (YKnown.countMaxTrailingZeros() == 0)
1249 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1250
1251 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0));
1252 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1253 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
1254 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`
1255 // but to avoid unnecessary work, first just if this is an obvious case.
1256
1257 // if X non-zero and NoOverflow(X * Y)
1258 // (icmp eq/ne Y)
1259 if (!XKnown.One.isZero() || isKnownNonZero(X, Q))
1260 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1261
1262 // if Y non-zero and NoOverflow(X * Y)
1263 // (icmp eq/ne X)
1264 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q))
1265 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1266 }
1267 // Note, we are skipping cases:
1268 // if Y % 2 != 0 AND X % 2 != 0
1269 // (false/true)
1270 // if X non-zero and Y non-zero and NoOverflow(X * Y)
1271 // (false/true)
1272 // Those can be simplified later as we would have already replaced the (icmp
1273 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
1274 // will fold to a constant elsewhere.
1275 }
1276
1277 // (icmp eq/ne f(X), 0) -> (icmp eq/ne X, 0)
1278 // where f(X) == 0 if and only if X == 0
1279 if (ICmpInst::isEquality(Pred))
1280 if (Value *Stripped = stripNullTest(Cmp.getOperand(0)))
1281 return new ICmpInst(Pred, Stripped,
1282 Constant::getNullValue(Stripped->getType()));
1283
1284 return nullptr;
1285}
1286
1287/// Fold icmp eq (num + mask) & ~mask, num
1288/// to
1289/// icmp eq (and num, mask), 0
1290/// Where mask is a low bit mask.
1292 Value *Num;
1293 CmpPredicate Pred;
1294 const APInt *Mask, *Neg;
1295
1296 if (!match(&Cmp,
1297 m_c_ICmp(Pred, m_Value(Num),
1299 m_LowBitMask(Mask))),
1300 m_APInt(Neg))))))
1301 return nullptr;
1302
1303 if (*Neg != ~*Mask)
1304 return nullptr;
1305
1306 if (!ICmpInst::isEquality(Pred))
1307 return nullptr;
1308
1309 // Create new icmp eq (num & mask), 0
1310 auto *NewAnd = Builder.CreateAnd(Num, *Mask);
1311 auto *Zero = Constant::getNullValue(Num->getType());
1312
1313 return new ICmpInst(Pred, NewAnd, Zero);
1314}
1315
1316/// Fold icmp Pred X, C.
1317/// TODO: This code structure does not make sense. The saturating add fold
1318/// should be moved to some other helper and extended as noted below (it is also
1319/// possible that code has been made unnecessary - do we canonicalize IR to
1320/// overflow/saturating intrinsics or not?).
1322 // Match the following pattern, which is a common idiom when writing
1323 // overflow-safe integer arithmetic functions. The source performs an addition
1324 // in wider type and explicitly checks for overflow using comparisons against
1325 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1326 //
1327 // TODO: This could probably be generalized to handle other overflow-safe
1328 // operations if we worked out the formulas to compute the appropriate magic
1329 // constants.
1330 //
1331 // sum = a + b
1332 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1333 CmpInst::Predicate Pred = Cmp.getPredicate();
1334 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
1335 Value *A, *B;
1336 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1337 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) &&
1338 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1339 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
1340 return Res;
1341
1342 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1344 if (!C)
1345 return nullptr;
1346
1347 if (auto *Phi = dyn_cast<PHINode>(Op0))
1348 if (all_of(Phi->operands(), IsaPred<Constant>)) {
1350 for (Value *V : Phi->incoming_values()) {
1351 Constant *Res =
1353 if (!Res)
1354 return nullptr;
1355 Ops.push_back(Res);
1356 }
1357 Builder.SetInsertPoint(Phi);
1358 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());
1359 for (auto [V, Pred] : zip(Ops, Phi->blocks()))
1360 NewPhi->addIncoming(V, Pred);
1361 return replaceInstUsesWith(Cmp, NewPhi);
1362 }
1363
1365 return R;
1366
1367 return nullptr;
1368}
1369
1370/// Canonicalize icmp instructions based on dominating conditions.
1372 // We already checked simple implication in InstSimplify, only handle complex
1373 // cases here.
1374 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1);
1375 const APInt *C;
1376 if (!match(Y, m_APInt(C)))
1377 return nullptr;
1378
1379 CmpInst::Predicate Pred = Cmp.getPredicate();
1381
1382 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1383 const APInt *DomC) -> Instruction * {
1384 // We have 2 compares of a variable with constants. Calculate the constant
1385 // ranges of those compares to see if we can transform the 2nd compare:
1386 // DomBB:
1387 // DomCond = icmp DomPred X, DomC
1388 // br DomCond, CmpBB, FalseBB
1389 // CmpBB:
1390 // Cmp = icmp Pred X, C
1391 ConstantRange DominatingCR =
1392 ConstantRange::makeExactICmpRegion(DomPred, *DomC);
1393 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1394 ConstantRange Difference = DominatingCR.difference(CR);
1395 if (Intersection.isEmptySet())
1396 return replaceInstUsesWith(Cmp, Builder.getFalse());
1397 if (Difference.isEmptySet())
1398 return replaceInstUsesWith(Cmp, Builder.getTrue());
1399
1400 // Canonicalizing a sign bit comparison that gets used in a branch,
1401 // pessimizes codegen by generating branch on zero instruction instead
1402 // of a test and branch. So we avoid canonicalizing in such situations
1403 // because test and branch instruction has better branch displacement
1404 // than compare and branch instruction.
1405 bool UnusedBit;
1406 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit);
1407 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp)))
1408 return nullptr;
1409
1410 // Avoid an infinite loop with min/max canonicalization.
1411 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1412 if (Cmp.hasOneUse() &&
1413 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value())))
1414 return nullptr;
1415
1416 if (const APInt *EqC = Intersection.getSingleElement())
1417 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC));
1418 if (const APInt *NeC = Difference.getSingleElement())
1419 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC));
1420 return nullptr;
1421 };
1422
1423 for (CondBrInst *BI : DC.conditionsFor(X)) {
1424 CmpPredicate DomPred;
1425 const APInt *DomC;
1426 if (!match(BI->getCondition(),
1427 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC))))
1428 continue;
1429
1430 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1431 if (DT.dominates(Edge0, Cmp.getParent())) {
1432 if (auto *V = handleDomCond(DomPred, DomC))
1433 return V;
1434 } else {
1435 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1436 if (DT.dominates(Edge1, Cmp.getParent()))
1437 if (auto *V =
1438 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC))
1439 return V;
1440 }
1441 }
1442
1443 return nullptr;
1444}
1445
1446/// Fold icmp (trunc X), C.
1448 TruncInst *Trunc,
1449 const APInt &C) {
1450 ICmpInst::Predicate Pred = Cmp.getPredicate();
1451 Value *X = Trunc->getOperand(0);
1452 Type *SrcTy = X->getType();
1453 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1454 SrcBits = SrcTy->getScalarSizeInBits();
1455
1456 // Match (icmp pred (trunc nuw/nsw X), C)
1457 // Which we can convert to (icmp pred X, (sext/zext C))
1458 if (shouldChangeType(Trunc->getType(), SrcTy)) {
1459 if (Trunc->hasNoSignedWrap())
1460 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
1461 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1462 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
1463 }
1464
1465 if (C.isOne() && C.getBitWidth() > 1) {
1466 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1467 Value *V = nullptr;
1468 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
1469 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1470 ConstantInt::get(V->getType(), 1));
1471 }
1472
1473 // TODO: Handle non-equality predicates.
1474 Value *Y;
1475 const APInt *Pow2;
1476 if (Cmp.isEquality() && match(X, m_Shl(m_Power2(Pow2), m_Value(Y))) &&
1477 DstBits > Pow2->logBase2()) {
1478 // (trunc (Pow2 << Y) to iN) == 0 --> Y u>= N - log2(Pow2)
1479 // (trunc (Pow2 << Y) to iN) != 0 --> Y u< N - log2(Pow2)
1480 // iff N > log2(Pow2)
1481 if (C.isZero()) {
1482 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1483 return new ICmpInst(NewPred, Y,
1484 ConstantInt::get(SrcTy, DstBits - Pow2->logBase2()));
1485 }
1486 // (trunc (Pow2 << Y) to iN) == 2**C --> Y == C - log2(Pow2)
1487 // (trunc (Pow2 << Y) to iN) != 2**C --> Y != C - log2(Pow2)
1488 if (C.isPowerOf2())
1489 return new ICmpInst(
1490 Pred, Y, ConstantInt::get(SrcTy, C.logBase2() - Pow2->logBase2()));
1491 }
1492
1493 if (Cmp.isEquality() && (Trunc->hasOneUse() || Trunc->hasNoUnsignedWrap())) {
1494 // Canonicalize to a mask and wider compare if the wide type is suitable:
1495 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1496 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {
1497 Constant *Mask =
1498 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits));
1499 Value *And = Trunc->hasNoUnsignedWrap() ? X : Builder.CreateAnd(X, Mask);
1500 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits));
1501 return new ICmpInst(Pred, And, WideC);
1502 }
1503
1504 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1505 // of the high bits truncated out of x are known.
1506 KnownBits Known = computeKnownBits(X, &Cmp);
1507
1508 // If all the high bits are known, we can do this xform.
1509 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1510 // Pull in the high bits from known-ones set.
1511 APInt NewRHS = C.zext(SrcBits);
1512 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
1513 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS));
1514 }
1515 }
1516
1517 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1518 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1519 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1520 Value *ShOp;
1521 uint64_t ShAmt;
1522 bool TrueIfSigned;
1523 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
1524 match(X, m_Shr(m_Value(ShOp), m_ConstantInt(ShAmt))) &&
1525 DstBits == SrcBits - ShAmt) {
1526 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1528 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1530 }
1531
1532 return nullptr;
1533}
1534
1535/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1536/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1539 const SimplifyQuery &Q) {
1540 Value *X, *Y;
1541 CmpPredicate Pred;
1542 bool YIsSExt = false;
1543 // Try to match icmp (trunc X), (trunc Y)
1544 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {
1545 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() &
1546 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind();
1547 if (Cmp.isSigned()) {
1548 // For signed comparisons, both truncs must be nsw.
1549 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1550 return nullptr;
1551 } else {
1552 // For unsigned and equality comparisons, either both must be nuw or
1553 // both must be nsw, we don't care which.
1554 if (!NoWrapFlags)
1555 return nullptr;
1556 }
1557
1558 if (X->getType() != Y->getType() &&
1559 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))
1560 return nullptr;
1561 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) &&
1562 isDesirableIntType(Y->getType()->getScalarSizeInBits())) {
1563 std::swap(X, Y);
1564 Pred = Cmp.getSwappedPredicate(Pred);
1565 }
1566 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1567 }
1568 // Try to match icmp (trunc nuw X), (zext Y)
1569 else if (!Cmp.isSigned() &&
1570 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),
1571 m_OneUse(m_ZExt(m_Value(Y)))))) {
1572 // Can fold trunc nuw + zext for unsigned and equality predicates.
1573 }
1574 // Try to match icmp (trunc nsw X), (sext Y)
1575 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),
1577 // Can fold trunc nsw + zext/sext for all predicates.
1578 YIsSExt =
1579 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));
1580 } else
1581 return nullptr;
1582
1583 Type *TruncTy = Cmp.getOperand(0)->getType();
1584 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1585
1586 // If this transform will end up changing from desirable types -> undesirable
1587 // types skip it.
1588 if (isDesirableIntType(TruncBits) &&
1589 !isDesirableIntType(X->getType()->getScalarSizeInBits()))
1590 return nullptr;
1591
1592 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);
1593 return new ICmpInst(Pred, X, NewY);
1594}
1595
1596/// Fold icmp (xor X, Y), C.
1599 const APInt &C) {
1600 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1601 return I;
1602
1603 Value *X = Xor->getOperand(0);
1604 Value *Y = Xor->getOperand(1);
1605 const APInt *XorC;
1606 if (!match(Y, m_APInt(XorC)))
1607 return nullptr;
1608
1609 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1610 // fold the xor.
1611 ICmpInst::Predicate Pred = Cmp.getPredicate();
1612 bool TrueIfSigned = false;
1613 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {
1614
1615 // If the sign bit of the XorCst is not set, there is no change to
1616 // the operation, just stop using the Xor.
1617 if (!XorC->isNegative())
1618 return replaceOperand(Cmp, 0, X);
1619
1620 // Emit the opposite comparison.
1621 if (TrueIfSigned)
1622 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1623 ConstantInt::getAllOnesValue(X->getType()));
1624 else
1625 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1626 ConstantInt::getNullValue(X->getType()));
1627 }
1628
1629 if (Xor->hasOneUse()) {
1630 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1631 if (!Cmp.isEquality() && XorC->isSignMask()) {
1632 Pred = Cmp.getFlippedSignednessPredicate();
1633 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1634 }
1635
1636 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1637 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1638 Pred = Cmp.getFlippedSignednessPredicate();
1639 Pred = Cmp.getSwappedPredicate(Pred);
1640 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1641 }
1642 }
1643
1644 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1645 if (Pred == ICmpInst::ICMP_UGT) {
1646 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1647 if (*XorC == ~C && (C + 1).isPowerOf2())
1648 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1649 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1650 if (*XorC == C && (C + 1).isPowerOf2())
1651 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1652 }
1653 if (Pred == ICmpInst::ICMP_ULT) {
1654 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1655 if (*XorC == -C && C.isPowerOf2())
1656 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1657 ConstantInt::get(X->getType(), ~C));
1658 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1659 if (*XorC == C && (-C).isPowerOf2())
1660 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1661 ConstantInt::get(X->getType(), ~C));
1662 }
1663 return nullptr;
1664}
1665
1666/// For power-of-2 C:
1667/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1668/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1671 const APInt &C) {
1672 CmpInst::Predicate Pred = Cmp.getPredicate();
1673 APInt PowerOf2;
1674 if (Pred == ICmpInst::ICMP_ULT)
1675 PowerOf2 = C;
1676 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1677 PowerOf2 = C + 1;
1678 else
1679 return nullptr;
1680 if (!PowerOf2.isPowerOf2())
1681 return nullptr;
1682 Value *X;
1683 const APInt *ShiftC;
1685 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))
1686 return nullptr;
1687 uint64_t Shift = ShiftC->getLimitedValue();
1688 Type *XType = X->getType();
1689 if (Shift == 0 || PowerOf2.isMinSignedValue())
1690 return nullptr;
1691 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));
1692 APInt Bound =
1693 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1694 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));
1695}
1696
1697/// Fold icmp (and (sh X, Y), C2), C1.
1700 const APInt &C1,
1701 const APInt &C2) {
1702 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1703 if (!Shift || !Shift->isShift())
1704 return nullptr;
1705
1706 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1707 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1708 // code produced by the clang front-end, for bitfield access.
1709 // This seemingly simple opportunity to fold away a shift turns out to be
1710 // rather complicated. See PR17827 for details.
1711 unsigned ShiftOpcode = Shift->getOpcode();
1712 bool IsShl = ShiftOpcode == Instruction::Shl;
1713 const APInt *C3;
1714 if (match(Shift->getOperand(1), m_APInt(C3))) {
1715 APInt NewAndCst, NewCmpCst;
1716 bool AnyCmpCstBitsShiftedOut;
1717 if (ShiftOpcode == Instruction::Shl) {
1718 // For a left shift, we can fold if the comparison is not signed. We can
1719 // also fold a signed comparison if the mask value and comparison value
1720 // are not negative. These constraints may not be obvious, but we can
1721 // prove that they are correct using an SMT solver.
1722 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1723 return nullptr;
1724
1725 NewCmpCst = C1.lshr(*C3);
1726 NewAndCst = C2.lshr(*C3);
1727 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;
1728 } else if (ShiftOpcode == Instruction::LShr) {
1729 // For a logical right shift, we can fold if the comparison is not signed.
1730 // We can also fold a signed comparison if the shifted mask value and the
1731 // shifted comparison value are not negative. These constraints may not be
1732 // obvious, but we can prove that they are correct using an SMT solver.
1733 NewCmpCst = C1.shl(*C3);
1734 NewAndCst = C2.shl(*C3);
1735 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;
1736 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1737 return nullptr;
1738 } else {
1739 // For an arithmetic shift, check that both constants don't use (in a
1740 // signed sense) the top bits being shifted out.
1741 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1742 NewCmpCst = C1.shl(*C3);
1743 NewAndCst = C2.shl(*C3);
1744 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;
1745 if (NewAndCst.ashr(*C3) != C2)
1746 return nullptr;
1747 }
1748
1749 if (AnyCmpCstBitsShiftedOut) {
1750 // If we shifted bits out, the fold is not going to work out. As a
1751 // special case, check to see if this means that the result is always
1752 // true or false now.
1753 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1754 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1755 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1756 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1757 } else {
1758 Value *NewAnd = Builder.CreateAnd(
1759 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));
1760 return new ICmpInst(Cmp.getPredicate(), NewAnd,
1761 ConstantInt::get(And->getType(), NewCmpCst));
1762 }
1763 }
1764
1765 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1766 // preferable because it allows the C2 << Y expression to be hoisted out of a
1767 // loop if Y is invariant and X is not.
1768 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1769 !Shift->isArithmeticShift() &&
1770 ((!IsShl && C2.isOne()) || !isa<Constant>(Shift->getOperand(0)))) {
1771 // Compute C2 << Y.
1772 Value *NewShift =
1773 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))
1774 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));
1775
1776 // Compute X & (C2 << Y).
1777 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1778 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1779 }
1780
1781 return nullptr;
1782}
1783
1784/// Fold icmp (and X, C2), C1.
1787 const APInt &C1) {
1788 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1789
1790 // icmp ne (and X, 1), 0 --> trunc X to i1
1791 if (isICMP_NE && C1.isZero() && match(And->getOperand(1), m_One()))
1792 return new TruncInst(And->getOperand(0), Cmp.getType());
1793
1794 const APInt *C2;
1795 Value *X;
1796 if (!match(And, m_And(m_Value(X), m_APInt(C2))))
1797 return nullptr;
1798
1799 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask
1800 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(~*C2) &&
1801 C2->isNegatedPowerOf2())
1802 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1803 ConstantInt::get(X->getType(), ~*C2));
1804 // (and X, highmask) s< [1, -highmask] --> X s< -highmask
1805 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() &&
1806 (C1 - 1).ule(~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask())
1807 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1808 ConstantInt::get(X->getType(), -*C2));
1809
1810 // Don't perform the following transforms if the AND has multiple uses
1811 if (!And->hasOneUse())
1812 return nullptr;
1813
1814 if (Cmp.isEquality() && C1.isZero()) {
1815 // Restrict this fold to single-use 'and' (PR10267).
1816 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1817 if (C2->isSignMask()) {
1818 Constant *Zero = Constant::getNullValue(X->getType());
1819 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1820 return new ICmpInst(NewPred, X, Zero);
1821 }
1822
1823 APInt NewC2 = *C2;
1824 KnownBits Know = computeKnownBits(And->getOperand(0), And);
1825 // Set high zeros of C2 to allow matching negated power-of-2.
1826 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
1827 Know.countMinLeadingZeros());
1828
1829 // Restrict this fold only for single-use 'and' (PR10267).
1830 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1831 if (NewC2.isNegatedPowerOf2()) {
1832 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);
1833 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1834 return new ICmpInst(NewPred, X, NegBOC);
1835 }
1836 }
1837
1838 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1839 // the input width without changing the value produced, eliminate the cast:
1840 //
1841 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1842 //
1843 // We can do this transformation if the constants do not have their sign bits
1844 // set or if it is an equality comparison. Extending a relational comparison
1845 // when we're checking the sign bit would not work.
1846 Value *W;
1847 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&
1848 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1849 // TODO: Is this a good transform for vectors? Wider types may reduce
1850 // throughput. Should this transform be limited (even for scalars) by using
1851 // shouldChangeType()?
1852 if (!Cmp.getType()->isVectorTy()) {
1853 Type *WideType = W->getType();
1854 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1855 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));
1856 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1857 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());
1858 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1859 }
1860 }
1861
1862 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))
1863 return I;
1864
1865 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1866 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1867 //
1868 // iff pred isn't signed
1869 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&
1870 match(And->getOperand(1), m_One())) {
1871 Constant *One = cast<Constant>(And->getOperand(1));
1872 Value *Or = And->getOperand(0);
1873 Value *A, *B, *LShr;
1874 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1875 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1876 unsigned UsesRemoved = 0;
1877 if (And->hasOneUse())
1878 ++UsesRemoved;
1879 if (Or->hasOneUse())
1880 ++UsesRemoved;
1881 if (LShr->hasOneUse())
1882 ++UsesRemoved;
1883
1884 // Compute A & ((1 << B) | 1)
1885 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;
1886 if (UsesRemoved >= RequireUsesRemoved) {
1887 Value *NewOr =
1888 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),
1889 /*HasNUW=*/true),
1890 One, Or->getName());
1891 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1892 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1893 }
1894 }
1895 }
1896
1897 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1898 // llvm.is.fpclass(X, fcInf|fcNan)
1899 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1900 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1901 // (icmp eq (and (bitcast X to int), ExponentMask), 0) -->
1902 // llvm.is.fpclass(X, fcSubnormal|fcZero)
1903 // (icmp ne (and (bitcast X to int), ExponentMask), 0) -->
1904 // llvm.is.fpclass(X, ~(fcSubnormal|fcZero))
1905 Value *V;
1906 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1907 Attribute::NoImplicitFloat) &&
1908 Cmp.isEquality() &&
1910 Type *FPType = V->getType()->getScalarType();
1911 if (FPType->isIEEELikeFPTy() && (C1.isZero() || C1 == *C2)) {
1912 APInt ExponentMask =
1913 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt();
1914 if (*C2 == ExponentMask) {
1915 unsigned Mask = C1.isZero()
1918 if (isICMP_NE)
1919 Mask = ~Mask & fcAllFlags;
1920 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));
1921 }
1922 }
1923 }
1924
1925 return nullptr;
1926}
1927
1928/// Fold icmp (and X, Y), C.
1931 const APInt &C) {
1932 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1933 return I;
1934
1935 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1936 bool TrueIfNeg;
1937 if (isSignBitCheck(Pred, C, TrueIfNeg)) {
1938 // ((X - 1) & ~X) < 0 --> X == 0
1939 // ((X - 1) & ~X) >= 0 --> X != 0
1940 Value *X;
1941 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&
1942 match(And->getOperand(1), m_Not(m_Specific(X)))) {
1943 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1944 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));
1945 }
1946 // (X & -X) < 0 --> X == MinSignedC
1947 // (X & -X) > -1 --> X != MinSignedC
1948 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {
1949 Constant *MinSignedC = ConstantInt::get(
1950 X->getType(),
1951 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
1952 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1953 return new ICmpInst(NewPred, X, MinSignedC);
1954 }
1955 }
1956
1957 // TODO: These all require that Y is constant too, so refactor with the above.
1958
1959 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1960 Value *X = And->getOperand(0);
1961 Value *Y = And->getOperand(1);
1962 if (auto *C2 = dyn_cast<ConstantInt>(Y))
1963 if (auto *LI = dyn_cast<LoadInst>(X))
1964 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1965 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(LI, GEP, Cmp, C2))
1966 return Res;
1967
1968 if (!Cmp.isEquality())
1969 return nullptr;
1970
1971 // X & -C == -C -> X > u ~C
1972 // X & -C != -C -> X <= u ~C
1973 // iff C is a power of 2
1974 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {
1975 auto NewPred =
1977 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1978 }
1979
1980 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
1981 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1982 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1983 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
1985 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
1986 Value *TruncY = Builder.CreateTrunc(Y, X->getType());
1987 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
1988 Value *And = Builder.CreateAnd(TruncY, X);
1990 }
1991 return BinaryOperator::CreateAnd(TruncY, X);
1992 }
1993
1994 // (icmp eq/ne (and (shl -1, X), Y), 0)
1995 // -> (icmp eq/ne (lshr Y, X), 0)
1996 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
1997 // highly unlikely the non-zero case will ever show up in code.
1998 if (C.isZero() &&
2000 m_Value(Y))))) {
2001 Value *LShr = Builder.CreateLShr(Y, X);
2002 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));
2003 }
2004
2005 // (icmp eq/ne (and (add A, Addend), Msk), C)
2006 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk))
2007 {
2008 Value *A;
2009 const APInt *Addend, *Msk;
2011 m_LowBitMask(Msk)))) &&
2012 C.ule(*Msk)) {
2013 APInt NewComperand = (C - *Addend) & *Msk;
2014 Value *MaskA = Builder.CreateAnd(A, ConstantInt::get(A->getType(), *Msk));
2015 return new ICmpInst(Pred, MaskA,
2016 ConstantInt::get(MaskA->getType(), NewComperand));
2017 }
2018 }
2019
2020 return nullptr;
2021}
2022
2023/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
2025 InstCombiner::BuilderTy &Builder) {
2026 // Are we using xors or subs to bitwise check for a pair or pairs of
2027 // (in)equalities? Convert to a shorter form that has more potential to be
2028 // folded even further.
2029 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
2030 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
2031 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
2032 // (X1 == X2) && (X3 == X4) && (X5 == X6)
2033 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
2034 // (X1 != X2) || (X3 != X4) || (X5 != X6)
2036 SmallVector<Value *, 16> WorkList(1, Or);
2037
2038 while (!WorkList.empty()) {
2039 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
2040 Value *Lhs, *Rhs;
2041
2042 if (match(OrOperatorArgument,
2043 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {
2044 CmpValues.emplace_back(Lhs, Rhs);
2045 return;
2046 }
2047
2048 if (match(OrOperatorArgument,
2049 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {
2050 CmpValues.emplace_back(Lhs, Rhs);
2051 return;
2052 }
2053
2054 WorkList.push_back(OrOperatorArgument);
2055 };
2056
2057 Value *CurrentValue = WorkList.pop_back_val();
2058 Value *OrOperatorLhs, *OrOperatorRhs;
2059
2060 if (!match(CurrentValue,
2061 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {
2062 return nullptr;
2063 }
2064
2065 MatchOrOperatorArgument(OrOperatorRhs);
2066 MatchOrOperatorArgument(OrOperatorLhs);
2067 }
2068
2069 ICmpInst::Predicate Pred = Cmp.getPredicate();
2070 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2071 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,
2072 CmpValues.rbegin()->second);
2073
2074 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2075 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);
2076 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);
2077 }
2078
2079 return LhsCmp;
2080}
2081
2082/// Fold icmp (or X, Y), C.
2085 const APInt &C) {
2086 ICmpInst::Predicate Pred = Cmp.getPredicate();
2087 if (C.isOne()) {
2088 // icmp slt signum(V) 1 --> icmp slt V, 1
2089 Value *V = nullptr;
2090 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
2091 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2092 ConstantInt::get(V->getType(), 1));
2093 }
2094
2095 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
2096
2097 // (icmp eq/ne (or disjoint x, C0), C1)
2098 // -> (icmp eq/ne x, C0^C1)
2099 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
2100 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {
2101 Value *NewC =
2102 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));
2103 return new ICmpInst(Pred, OrOp0, NewC);
2104 }
2105
2106 const APInt *MaskC;
2107 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {
2108 if (*MaskC == C && (C + 1).isPowerOf2()) {
2109 // X | C == C --> X <=u C
2110 // X | C != C --> X >u C
2111 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2113 return new ICmpInst(Pred, OrOp0, OrOp1);
2114 }
2115
2116 // More general: canonicalize 'equality with set bits mask' to
2117 // 'equality with clear bits mask'.
2118 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2119 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2120 if (Or->hasOneUse()) {
2121 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));
2122 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));
2123 return new ICmpInst(Pred, And, NewC);
2124 }
2125 }
2126
2127 // (X | (X-1)) s< 0 --> X s< 1
2128 // (X | (X-1)) s> -1 --> X s> 0
2129 Value *X;
2130 bool TrueIfSigned;
2131 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
2133 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2134 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
2135 return new ICmpInst(NewPred, X, NewC);
2136 }
2137
2138 const APInt *OrC;
2139 // icmp(X | OrC, C) --> icmp(X, 0)
2140 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {
2141 switch (Pred) {
2142 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2143 case ICmpInst::ICMP_SLT:
2144 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2145 case ICmpInst::ICMP_SGE:
2146 if (OrC->sge(C))
2147 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
2148 break;
2149 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2150 case ICmpInst::ICMP_SLE:
2151 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2152 case ICmpInst::ICMP_SGT:
2153 if (OrC->sgt(C))
2155 ConstantInt::getNullValue(X->getType()));
2156 break;
2157 default:
2158 break;
2159 }
2160 }
2161
2162 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2163 return nullptr;
2164
2165 Value *P, *Q;
2167 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2168 // -> and (icmp eq P, null), (icmp eq Q, null).
2169 Value *CmpP =
2170 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
2171 Value *CmpQ =
2172 Builder.CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType()));
2173 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2174 return BinaryOperator::Create(BOpc, CmpP, CmpQ);
2175 }
2176
2177 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2178 return replaceInstUsesWith(Cmp, V);
2179
2180 return nullptr;
2181}
2182
2183/// Fold icmp (mul X, Y), C.
2186 const APInt &C) {
2187 ICmpInst::Predicate Pred = Cmp.getPredicate();
2188 Type *MulTy = Mul->getType();
2189 Value *X = Mul->getOperand(0);
2190
2191 // If there's no overflow:
2192 // X * X == 0 --> X == 0
2193 // X * X != 0 --> X != 0
2194 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
2195 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2196 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2197
2198 const APInt *MulC;
2199 if (!match(Mul->getOperand(1), m_APInt(MulC)))
2200 return nullptr;
2201
2202 // If this is a test of the sign bit and the multiply is sign-preserving with
2203 // a constant operand, use the multiply LHS operand instead:
2204 // (X * +MulC) < 0 --> X < 0
2205 // (X * -MulC) < 0 --> X > 0
2206 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2207 if (MulC->isNegative())
2208 Pred = ICmpInst::getSwappedPredicate(Pred);
2209 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2210 }
2211
2212 if (MulC->isZero())
2213 return nullptr;
2214
2215 // If the multiply does not wrap or the constant is odd, try to divide the
2216 // compare constant by the multiplication factor.
2217 if (Cmp.isEquality()) {
2218 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2219 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2220 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2221 return new ICmpInst(Pred, X, NewC);
2222 }
2223
2224 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2225 // correct to transform if MulC * N == C including overflow. I.e with i8
2226 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2227 // miss that case.
2228 if (C.urem(*MulC).isZero()) {
2229 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2230 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2231 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2232 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2233 return new ICmpInst(Pred, X, NewC);
2234 }
2235 }
2236 }
2237
2238 // With a matching no-overflow guarantee, fold the constants:
2239 // (X * MulC) < C --> X < (C / MulC)
2240 // (X * MulC) > C --> X > (C / MulC)
2241 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2242 Constant *NewC = nullptr;
2243 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2244 // MININT / -1 --> overflow.
2245 if (C.isMinSignedValue() && MulC->isAllOnes())
2246 return nullptr;
2247 if (MulC->isNegative())
2248 Pred = ICmpInst::getSwappedPredicate(Pred);
2249
2250 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2251 NewC = ConstantInt::get(
2253 } else {
2254 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2255 "Unexpected predicate");
2256 NewC = ConstantInt::get(
2258 }
2259 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2260 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2261 NewC = ConstantInt::get(
2263 } else {
2264 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2265 "Unexpected predicate");
2266 NewC = ConstantInt::get(
2268 }
2269 }
2270
2271 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2272}
2273
2274/// Fold icmp (shl nuw C2, Y), C.
2276 const APInt &C) {
2277 Value *Y;
2278 const APInt *C2;
2279 if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y))))
2280 return nullptr;
2281
2282 Type *ShiftType = Shl->getType();
2283 unsigned TypeBits = C.getBitWidth();
2284 ICmpInst::Predicate Pred = Cmp.getPredicate();
2285 if (Cmp.isUnsigned()) {
2286 if (C2->isZero() || C2->ugt(C))
2287 return nullptr;
2288 APInt Div, Rem;
2289 APInt::udivrem(C, *C2, Div, Rem);
2290 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();
2291
2292 // (1 << Y) pred C -> Y pred Log2(C)
2293 if (!CIsPowerOf2) {
2294 // (1 << Y) < 30 -> Y <= 4
2295 // (1 << Y) <= 30 -> Y <= 4
2296 // (1 << Y) >= 30 -> Y > 4
2297 // (1 << Y) > 30 -> Y > 4
2298 if (Pred == ICmpInst::ICMP_ULT)
2299 Pred = ICmpInst::ICMP_ULE;
2300 else if (Pred == ICmpInst::ICMP_UGE)
2301 Pred = ICmpInst::ICMP_UGT;
2302 }
2303
2304 unsigned CLog2 = Div.logBase2();
2305 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2306 } else if (Cmp.isSigned() && C2->isOne()) {
2307 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
2308 // (1 << Y) > 0 -> Y != 31
2309 // (1 << Y) > C -> Y != 31 if C is negative.
2310 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))
2311 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2312
2313 // (1 << Y) < 0 -> Y == 31
2314 // (1 << Y) < 1 -> Y == 31
2315 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2316 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2317 if (Pred == ICmpInst::ICMP_SLT && (C - 1).sle(0))
2318 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2319 }
2320
2321 return nullptr;
2322}
2323
2324/// Fold icmp (shl X, Y), C.
2326 BinaryOperator *Shl,
2327 const APInt &C) {
2328 const APInt *ShiftVal;
2329 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
2330 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);
2331
2332 ICmpInst::Predicate Pred = Cmp.getPredicate();
2333 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2334 // -> (icmp pred X, Csle0)
2335 //
2336 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2337 // so X's must be what is used.
2338 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2339 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2340
2341 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2342 // -> (icmp eq/ne X, 0)
2343 if (ICmpInst::isEquality(Pred) && C.isZero() &&
2344 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2345 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2346
2347 // (icmp slt (shl nsw X, Y), 0/1)
2348 // -> (icmp slt X, 0/1)
2349 // (icmp sgt (shl nsw X, Y), 0/-1)
2350 // -> (icmp sgt X, 0/-1)
2351 //
2352 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2353 if (Shl->hasNoSignedWrap() &&
2354 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2355 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2356 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2357
2358 const APInt *ShiftAmt;
2359 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2360 return foldICmpShlLHSC(Cmp, Shl, C);
2361
2362 // Check that the shift amount is in range. If not, don't perform undefined
2363 // shifts. When the shift is visited, it will be simplified.
2364 unsigned TypeBits = C.getBitWidth();
2365 if (ShiftAmt->uge(TypeBits))
2366 return nullptr;
2367
2368 Value *X = Shl->getOperand(0);
2369 Type *ShType = Shl->getType();
2370
2371 // NSW guarantees that we are only shifting out sign bits from the high bits,
2372 // so we can ASHR the compare constant without needing a mask and eliminate
2373 // the shift.
2374 if (Shl->hasNoSignedWrap()) {
2375 if (Pred == ICmpInst::ICMP_SGT) {
2376 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2377 APInt ShiftedC = C.ashr(*ShiftAmt);
2378 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2379 }
2380 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2381 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {
2382 APInt ShiftedC = C.ashr(*ShiftAmt);
2383 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2384 }
2385 if (Pred == ICmpInst::ICMP_SLT) {
2386 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2387 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2388 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2389 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2390 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2391 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;
2392 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2393 }
2394 }
2395
2396 // NUW guarantees that we are only shifting out zero bits from the high bits,
2397 // so we can LSHR the compare constant without needing a mask and eliminate
2398 // the shift.
2399 if (Shl->hasNoUnsignedWrap()) {
2400 if (Pred == ICmpInst::ICMP_UGT) {
2401 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2402 APInt ShiftedC = C.lshr(*ShiftAmt);
2403 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2404 }
2405 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2406 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {
2407 APInt ShiftedC = C.lshr(*ShiftAmt);
2408 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2409 }
2410 if (Pred == ICmpInst::ICMP_ULT) {
2411 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2412 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2413 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2414 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2415 assert(C.ugt(0) && "ult 0 should have been eliminated");
2416 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;
2417 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2418 }
2419 }
2420
2421 if (Cmp.isEquality() && Shl->hasOneUse()) {
2422 // Strength-reduce the shift into an 'and'.
2423 Constant *Mask = ConstantInt::get(
2424 ShType,
2425 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
2426 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2427 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));
2428 return new ICmpInst(Pred, And, LShrC);
2429 }
2430
2431 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2432 bool TrueIfSigned = false;
2433 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {
2434 // (X << 31) <s 0 --> (X & 1) != 0
2435 Constant *Mask = ConstantInt::get(
2436 ShType,
2437 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
2438 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2439 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2440 And, Constant::getNullValue(ShType));
2441 }
2442
2443 // Simplify 'shl' inequality test into 'and' equality test.
2444 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2445 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2446 if ((C + 1).isPowerOf2() &&
2447 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2448 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));
2449 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2451 And, Constant::getNullValue(ShType));
2452 }
2453 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2454 if (C.isPowerOf2() &&
2455 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2456 Value *And =
2457 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));
2458 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2460 And, Constant::getNullValue(ShType));
2461 }
2462 }
2463
2464 // Transform (icmp pred iM (shl iM %v, N), C)
2465 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2466 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2467 // This enables us to get rid of the shift in favor of a trunc that may be
2468 // free on the target. It has the additional benefit of comparing to a
2469 // smaller constant that may be more target-friendly.
2470 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
2471 if (Shl->hasOneUse() && Amt != 0 &&
2472 shouldChangeType(ShType->getScalarSizeInBits(), TypeBits - Amt)) {
2473 ICmpInst::Predicate CmpPred = Pred;
2474 APInt RHSC = C;
2475
2476 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(CmpPred)) {
2477 // Try the flipped strictness predicate.
2478 // e.g.:
2479 // icmp ult i64 (shl X, 32), 8589934593 ->
2480 // icmp ule i64 (shl X, 32), 8589934592 ->
2481 // icmp ule i32 (trunc X, i32), 2 ->
2482 // icmp ult i32 (trunc X, i32), 3
2483 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(
2484 Pred, ConstantInt::get(ShType->getContext(), C))) {
2485 CmpPred = FlippedStrictness->first;
2486 RHSC = cast<ConstantInt>(FlippedStrictness->second)->getValue();
2487 }
2488 }
2489
2490 if (RHSC.countr_zero() >= Amt) {
2491 Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt);
2492 Constant *NewC =
2493 ConstantInt::get(TruncTy, RHSC.ashr(*ShiftAmt).trunc(TypeBits - Amt));
2494 return new ICmpInst(CmpPred,
2495 Builder.CreateTrunc(X, TruncTy, "", /*IsNUW=*/false,
2496 Shl->hasNoSignedWrap()),
2497 NewC);
2498 }
2499 }
2500
2501 return nullptr;
2502}
2503
2504/// Fold icmp ({al}shr X, Y), C.
2506 BinaryOperator *Shr,
2507 const APInt &C) {
2508 // An exact shr only shifts out zero bits, so:
2509 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2510 Value *X = Shr->getOperand(0);
2511 CmpInst::Predicate Pred = Cmp.getPredicate();
2512 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2513 return new ICmpInst(Pred, X, Cmp.getOperand(1));
2514
2515 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2516 const APInt *ShiftValC;
2517 if (match(X, m_APInt(ShiftValC))) {
2518 if (Cmp.isEquality())
2519 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
2520
2521 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2522 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2523 bool TrueIfSigned;
2524 if (!IsAShr && ShiftValC->isNegative() &&
2525 isSignBitCheck(Pred, C, TrueIfSigned))
2526 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2527 Shr->getOperand(1),
2528 ConstantInt::getNullValue(X->getType()));
2529
2530 // If the shifted constant is a power-of-2, test the shift amount directly:
2531 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2532 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2533 if (!IsAShr && ShiftValC->isPowerOf2() &&
2534 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2535 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2536 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2537 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2538
2539 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2540 unsigned ShiftLZ = ShiftValC->countl_zero();
2541 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
2542 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2543 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
2544 }
2545 }
2546
2547 const APInt *ShiftAmtC;
2548 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))
2549 return nullptr;
2550
2551 // Check that the shift amount is in range. If not, don't perform undefined
2552 // shifts. When the shift is visited it will be simplified.
2553 unsigned TypeBits = C.getBitWidth();
2554 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);
2555 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2556 return nullptr;
2557
2558 bool IsExact = Shr->isExact();
2559 Type *ShrTy = Shr->getType();
2560 // TODO: If we could guarantee that InstSimplify would handle all of the
2561 // constant-value-based preconditions in the folds below, then we could assert
2562 // those conditions rather than checking them. This is difficult because of
2563 // undef/poison (PR34838).
2564 if (IsAShr && Shr->hasOneUse()) {
2565 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2566 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2567 // When C - 1 is a power of two and the transform can be legally
2568 // performed, prefer this form so the produced constant is close to a
2569 // power of two.
2570 // icmp slt/ult (ashr exact X, ShAmtC), C
2571 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2572 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
2573 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2574 }
2575 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2576 // When ShAmtC can be shifted losslessly:
2577 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2578 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2579 APInt ShiftedC = C.shl(ShAmtVal);
2580 if (ShiftedC.ashr(ShAmtVal) == C)
2581 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2582 }
2583 if (Pred == CmpInst::ICMP_SGT) {
2584 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2585 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2586 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&
2587 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
2588 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2589 }
2590 if (Pred == CmpInst::ICMP_UGT) {
2591 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2592 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2593 // clause accounts for that pattern.
2594 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2595 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
2596 (C + 1).shl(ShAmtVal).isMinSignedValue())
2597 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2598 }
2599
2600 // If the compare constant has significant bits above the lowest sign-bit,
2601 // then convert an unsigned cmp to a test of the sign-bit:
2602 // (ashr X, ShiftC) u> C --> X s< 0
2603 // (ashr X, ShiftC) u< C --> X s> -1
2604 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2605 if (Pred == CmpInst::ICMP_UGT) {
2606 return new ICmpInst(CmpInst::ICMP_SLT, X,
2608 }
2609 if (Pred == CmpInst::ICMP_ULT) {
2610 return new ICmpInst(CmpInst::ICMP_SGT, X,
2612 }
2613 }
2614 } else if (!IsAShr) {
2615 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2616 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2617 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2618 APInt ShiftedC = C.shl(ShAmtVal);
2619 if (ShiftedC.lshr(ShAmtVal) == C)
2620 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2621 }
2622 if (Pred == CmpInst::ICMP_UGT) {
2623 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2624 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2625 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))
2626 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2627 }
2628 }
2629
2630 if (!Cmp.isEquality())
2631 return nullptr;
2632
2633 // Handle equality comparisons of shift-by-constant.
2634
2635 // If the comparison constant changes with the shift, the comparison cannot
2636 // succeed (bits of the comparison constant cannot match the shifted value).
2637 // This should be known by InstSimplify and already be folded to true/false.
2638 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2639 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2640 "Expected icmp+shr simplify did not occur.");
2641
2642 // If the bits shifted out are known zero, compare the unshifted value:
2643 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2644 if (Shr->isExact())
2645 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));
2646
2647 if (Shr->hasOneUse()) {
2648 // Canonicalize the shift into an 'and':
2649 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2650 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2651 Constant *Mask = ConstantInt::get(ShrTy, Val);
2652 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");
2653 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));
2654 }
2655
2656 return nullptr;
2657}
2658
2660 BinaryOperator *SRem,
2661 const APInt &C) {
2662 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2663 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
2664 // Canonicalize unsigned predicates to signed:
2665 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 0
2666 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1
2667 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1
2668 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1
2669
2670 const APInt *DivisorC;
2671 if (!match(SRem->getOperand(1), m_APInt(DivisorC)))
2672 return nullptr;
2673 if (DivisorC->isZero())
2674 return nullptr;
2675
2676 APInt NormalizedC = C;
2677 if (Pred == ICmpInst::ICMP_ULT) {
2678 assert(!NormalizedC.isZero() &&
2679 "ult X, 0 should have been simplified already.");
2680 --NormalizedC;
2681 }
2682 if (C.isNegative())
2683 NormalizedC.flipAllBits();
2684 if (!NormalizedC.uge(DivisorC->abs() - 1))
2685 return nullptr;
2686
2687 Type *Ty = SRem->getType();
2688 if (Pred == ICmpInst::ICMP_UGT)
2689 return new ICmpInst(ICmpInst::ICMP_SLT, SRem,
2691 return new ICmpInst(ICmpInst::ICMP_SGT, SRem,
2693 }
2694 // Match an 'is positive' or 'is negative' comparison of remainder by a
2695 // constant power-of-2 value:
2696 // (X % pow2C) sgt/slt 0
2697 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2698 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2699 return nullptr;
2700
2701 // TODO: The one-use check is standard because we do not typically want to
2702 // create longer instruction sequences, but this might be a special-case
2703 // because srem is not good for analysis or codegen.
2704 if (!SRem->hasOneUse())
2705 return nullptr;
2706
2707 const APInt *DivisorC;
2708 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
2709 return nullptr;
2710
2711 // For cmp_sgt/cmp_slt only zero valued C is handled.
2712 // For cmp_eq/cmp_ne only positive valued C is handled.
2713 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2714 !C.isZero()) ||
2715 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2716 !C.isStrictlyPositive()))
2717 return nullptr;
2718
2719 // Mask off the sign bit and the modulo bits (low-bits).
2720 Type *Ty = SRem->getType();
2721 APInt SignMask = APInt::getSignMask(Ty->getScalarSizeInBits());
2722 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
2723 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);
2724
2725 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2726 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));
2727
2728 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2729 // bit is set. Example:
2730 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2731 if (Pred == ICmpInst::ICMP_SGT)
2733
2734 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2735 // bit is set. Example:
2736 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2737 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
2738}
2739
2740/// Fold icmp (udiv X, Y), C.
2742 BinaryOperator *UDiv,
2743 const APInt &C) {
2744 ICmpInst::Predicate Pred = Cmp.getPredicate();
2745 Value *X = UDiv->getOperand(0);
2746 Value *Y = UDiv->getOperand(1);
2747 Type *Ty = UDiv->getType();
2748
2749 const APInt *C2;
2750 if (!match(X, m_APInt(C2)))
2751 return nullptr;
2752
2753 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2754
2755 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2756 if (Pred == ICmpInst::ICMP_UGT) {
2757 assert(!C.isMaxValue() &&
2758 "icmp ugt X, UINT_MAX should have been simplified already.");
2759 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2760 ConstantInt::get(Ty, C2->udiv(C + 1)));
2761 }
2762
2763 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2764 if (Pred == ICmpInst::ICMP_ULT) {
2765 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2766 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2767 ConstantInt::get(Ty, C2->udiv(C)));
2768 }
2769
2770 return nullptr;
2771}
2772
2773/// Fold icmp ({su}div X, Y), C.
2775 BinaryOperator *Div,
2776 const APInt &C) {
2777 ICmpInst::Predicate Pred = Cmp.getPredicate();
2778 Value *X = Div->getOperand(0);
2779 Value *Y = Div->getOperand(1);
2780 Type *Ty = Div->getType();
2781 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2782
2783 // If unsigned division and the compare constant is bigger than
2784 // UMAX/2 (negative), there's only one pair of values that satisfies an
2785 // equality check, so eliminate the division:
2786 // (X u/ Y) == C --> (X == C) && (Y == 1)
2787 // (X u/ Y) != C --> (X != C) || (Y != 1)
2788 // Similarly, if signed division and the compare constant is exactly SMIN:
2789 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2790 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2791 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2792 (!DivIsSigned || C.isMinSignedValue())) {
2793 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));
2794 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));
2795 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2796 return BinaryOperator::Create(Logic, XBig, YOne);
2797 }
2798
2799 // Fold: icmp pred ([us]div X, C2), C -> range test
2800 // Fold this div into the comparison, producing a range check.
2801 // Determine, based on the divide type, what the range is being
2802 // checked. If there is an overflow on the low or high side, remember
2803 // it, otherwise compute the range [low, hi) bounding the new value.
2804 // See: InsertRangeTest above for the kinds of replacements possible.
2805 const APInt *C2;
2806 if (!match(Y, m_APInt(C2)))
2807 return nullptr;
2808
2809 // FIXME: If the operand types don't match the type of the divide
2810 // then don't attempt this transform. The code below doesn't have the
2811 // logic to deal with a signed divide and an unsigned compare (and
2812 // vice versa). This is because (x /s C2) <s C produces different
2813 // results than (x /s C2) <u C or (x /u C2) <s C or even
2814 // (x /u C2) <u C. Simply casting the operands and result won't
2815 // work. :( The if statement below tests that condition and bails
2816 // if it finds it.
2817 // However, when the divisor is a positive constant and the dividend is
2818 // known non-negative, sdiv is equivalent to udiv, so we can lower
2819 // DivIsSigned and proceed through the unsigned path.
2820 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned()) {
2821 if (!DivIsSigned || !C2->isStrictlyPositive() ||
2822 !isKnownNonNegative(X, SQ.getWithInstruction(&Cmp)))
2823 return nullptr;
2824 DivIsSigned = false;
2825 }
2826
2827 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2828 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2829 // division-by-constant cases should be present, we can not assert that they
2830 // have happened before we reach this icmp instruction.
2831 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2832 return nullptr;
2833
2834 // Compute Prod = C * C2. We are essentially solving an equation of
2835 // form X / C2 = C. We solve for X by multiplying C2 and C.
2836 // By solving for X, we can turn this into a range check instead of computing
2837 // a divide.
2838 APInt Prod = C * *C2;
2839
2840 // Determine if the product overflows by seeing if the product is not equal to
2841 // the divide. Make sure we do the same kind of divide as in the LHS
2842 // instruction that we're folding.
2843 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;
2844
2845 // If the division is known to be exact, then there is no remainder from the
2846 // divide, so the covered range size is unit, otherwise it is the divisor.
2847 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2848
2849 // Figure out the interval that is being checked. For example, a comparison
2850 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2851 // Compute this interval based on the constants involved and the signedness of
2852 // the compare/divide. This computes a half-open interval, keeping track of
2853 // whether either value in the interval overflows. After analysis each
2854 // overflow variable is set to 0 if it's corresponding bound variable is valid
2855 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2856 int LoOverflow = 0, HiOverflow = 0;
2857 APInt LoBound, HiBound;
2858
2859 if (!DivIsSigned) { // udiv
2860 // e.g. X/5 op 3 --> [15, 20)
2861 LoBound = Prod;
2862 HiOverflow = LoOverflow = ProdOV;
2863 if (!HiOverflow) {
2864 // If this is not an exact divide, then many values in the range collapse
2865 // to the same result value.
2866 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2867 }
2868 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2869 if (C.isZero()) { // (X / pos) op 0
2870 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2871 LoBound = -(RangeSize - 1);
2872 HiBound = RangeSize;
2873 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2874 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2875 HiOverflow = LoOverflow = ProdOV;
2876 if (!HiOverflow)
2877 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2878 } else { // (X / pos) op neg
2879 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2880 HiBound = Prod + 1;
2881 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2882 if (!LoOverflow) {
2883 APInt DivNeg = -RangeSize;
2884 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2885 }
2886 }
2887 } else if (C2->isNegative()) { // Divisor is < 0.
2888 if (Div->isExact())
2889 RangeSize.negate();
2890 if (C.isZero()) { // (X / neg) op 0
2891 // e.g. X/-5 op 0 --> [-4, 5)
2892 LoBound = RangeSize + 1;
2893 HiBound = -RangeSize;
2894 if (HiBound == *C2) { // -INTMIN = INTMIN
2895 HiOverflow = 1; // [INTMIN+1, overflow)
2896 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2897 }
2898 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2899 // e.g. X/-5 op 3 --> [-19, -14)
2900 HiBound = Prod + 1;
2901 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2902 if (!LoOverflow)
2903 LoOverflow =
2904 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;
2905 } else { // (X / neg) op neg
2906 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2907 LoOverflow = HiOverflow = ProdOV;
2908 if (!HiOverflow)
2909 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2910 }
2911
2912 // Dividing by a negative swaps the condition. LT <-> GT
2913 Pred = ICmpInst::getSwappedPredicate(Pred);
2914 }
2915
2916 switch (Pred) {
2917 default:
2918 llvm_unreachable("Unhandled icmp predicate!");
2919 case ICmpInst::ICMP_EQ:
2920 if (LoOverflow && HiOverflow)
2921 return replaceInstUsesWith(Cmp, Builder.getFalse());
2922 if (HiOverflow)
2923 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2924 X, ConstantInt::get(Ty, LoBound));
2925 if (LoOverflow)
2926 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2927 X, ConstantInt::get(Ty, HiBound));
2928 return replaceInstUsesWith(
2929 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));
2930 case ICmpInst::ICMP_NE:
2931 if (LoOverflow && HiOverflow)
2932 return replaceInstUsesWith(Cmp, Builder.getTrue());
2933 if (HiOverflow)
2934 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2935 X, ConstantInt::get(Ty, LoBound));
2936 if (LoOverflow)
2937 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2938 X, ConstantInt::get(Ty, HiBound));
2939 return replaceInstUsesWith(
2940 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));
2941 case ICmpInst::ICMP_ULT:
2942 case ICmpInst::ICMP_SLT:
2943 if (LoOverflow == +1) // Low bound is greater than input range.
2944 return replaceInstUsesWith(Cmp, Builder.getTrue());
2945 if (LoOverflow == -1) // Low bound is less than input range.
2946 return replaceInstUsesWith(Cmp, Builder.getFalse());
2947 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));
2948 case ICmpInst::ICMP_UGT:
2949 case ICmpInst::ICMP_SGT:
2950 if (HiOverflow == +1) // High bound greater than input range.
2951 return replaceInstUsesWith(Cmp, Builder.getFalse());
2952 if (HiOverflow == -1) // High bound less than input range.
2953 return replaceInstUsesWith(Cmp, Builder.getTrue());
2954 if (Pred == ICmpInst::ICMP_UGT)
2955 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));
2956 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));
2957 }
2958
2959 return nullptr;
2960}
2961
2962/// Fold icmp (sub X, Y), C.
2965 const APInt &C) {
2966 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2967 ICmpInst::Predicate Pred = Cmp.getPredicate();
2968 Type *Ty = Sub->getType();
2969
2970 // (SubC - Y) == C) --> Y == (SubC - C)
2971 // (SubC - Y) != C) --> Y != (SubC - C)
2972 Constant *SubC;
2973 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {
2974 return new ICmpInst(Pred, Y,
2975 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));
2976 }
2977
2978 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
2979 const APInt *C2;
2980 APInt SubResult;
2981 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
2982 bool HasNSW = Sub->hasNoSignedWrap();
2983 bool HasNUW = Sub->hasNoUnsignedWrap();
2984 if (match(X, m_APInt(C2)) &&
2985 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
2986 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))
2987 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));
2988
2989 // X - Y == 0 --> X == Y.
2990 // X - Y != 0 --> X != Y.
2991 // TODO: We allow this with multiple uses as long as the other uses are not
2992 // in phis. The phi use check is guarding against a codegen regression
2993 // for a loop test. If the backend could undo this (and possibly
2994 // subsequent transforms), we would not need this hack.
2995 if (Cmp.isEquality() && C.isZero() &&
2996 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))
2997 return new ICmpInst(Pred, X, Y);
2998
2999 // The following transforms are only worth it if the only user of the subtract
3000 // is the icmp.
3001 // TODO: This is an artificial restriction for all of the transforms below
3002 // that only need a single replacement icmp. Can these use the phi test
3003 // like the transform above here?
3004 if (!Sub->hasOneUse())
3005 return nullptr;
3006
3007 if (Sub->hasNoSignedWrap()) {
3008 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
3009 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
3010 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
3011
3012 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
3013 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
3014 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3015
3016 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
3017 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
3018 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
3019
3020 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
3021 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3022 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3023 }
3024
3025 if (!match(X, m_APInt(C2)))
3026 return nullptr;
3027
3028 // C2 - Y <u C -> (Y | (C - 1)) == C2
3029 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
3030 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
3031 (*C2 & (C - 1)) == (C - 1))
3032 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);
3033
3034 // C2 - Y >u C -> (Y | C) != C2
3035 // iff C2 & C == C and C + 1 is a power of 2
3036 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
3037 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);
3038
3039 // We have handled special cases that reduce.
3040 // Canonicalize any remaining sub to add as:
3041 // (C2 - Y) > C --> (Y + ~C2) < ~C
3042 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",
3043 HasNUW, HasNSW);
3044 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
3045}
3046
3047static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
3048 Value *Op1, IRBuilderBase &Builder,
3049 bool HasOneUse) {
3050 auto FoldConstant = [&](bool Val) {
3051 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
3052 if (Op0->getType()->isVectorTy())
3054 cast<VectorType>(Op0->getType())->getElementCount(), Res);
3055 return Res;
3056 };
3057
3058 switch (Table.to_ulong()) {
3059 case 0: // 0 0 0 0
3060 return FoldConstant(false);
3061 case 1: // 0 0 0 1
3062 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
3063 case 2: // 0 0 1 0
3064 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
3065 case 3: // 0 0 1 1
3066 return Builder.CreateNot(Op0);
3067 case 4: // 0 1 0 0
3068 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
3069 case 5: // 0 1 0 1
3070 return Builder.CreateNot(Op1);
3071 case 6: // 0 1 1 0
3072 return Builder.CreateXor(Op0, Op1);
3073 case 7: // 0 1 1 1
3074 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
3075 case 8: // 1 0 0 0
3076 return Builder.CreateAnd(Op0, Op1);
3077 case 9: // 1 0 0 1
3078 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
3079 case 10: // 1 0 1 0
3080 return Op1;
3081 case 11: // 1 0 1 1
3082 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
3083 case 12: // 1 1 0 0
3084 return Op0;
3085 case 13: // 1 1 0 1
3086 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
3087 case 14: // 1 1 1 0
3088 return Builder.CreateOr(Op0, Op1);
3089 case 15: // 1 1 1 1
3090 return FoldConstant(true);
3091 default:
3092 llvm_unreachable("Invalid Operation");
3093 }
3094 return nullptr;
3095}
3096
3098 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3099 Value *A, *B;
3100 Constant *C1, *C2, *C3, *C4;
3101 if (!match(BO->getOperand(0),
3103 !match(BO->getOperand(1),
3105 Cmp.getType() != A->getType() || Cmp.getType() != B->getType())
3106 return nullptr;
3107
3108 std::bitset<4> Table;
3109 auto ComputeTable = [&](bool First, bool Second) -> std::optional<bool> {
3110 Constant *L = First ? C1 : C2;
3111 Constant *R = Second ? C3 : C4;
3112 if (auto *Res = ConstantFoldBinaryOpOperands(BO->getOpcode(), L, R, DL)) {
3113 auto *Val = Res->getType()->isVectorTy() ? Res->getSplatValue() : Res;
3114 if (auto *CI = dyn_cast_or_null<ConstantInt>(Val))
3115 return ICmpInst::compare(CI->getValue(), C, Cmp.getPredicate());
3116 }
3117 return std::nullopt;
3118 };
3119
3120 for (unsigned I = 0; I < 4; ++I) {
3121 bool First = (I >> 1) & 1;
3122 bool Second = I & 1;
3123 if (auto Res = ComputeTable(First, Second))
3124 Table[I] = *Res;
3125 else
3126 return nullptr;
3127 }
3128
3129 // Synthesize optimal logic.
3130 if (auto *Cond = createLogicFromTable(Table, A, B, Builder, BO->hasOneUse()))
3131 return replaceInstUsesWith(Cmp, Cond);
3132 return nullptr;
3133}
3134
3135/// Fold icmp (add X, Y), C.
3138 const APInt &C) {
3139 Value *Y = Add->getOperand(1);
3140 Value *X = Add->getOperand(0);
3141 const CmpPredicate Pred = Cmp.getCmpPredicate();
3142
3143 // icmp ult (add nuw A, (lshr A, ShAmtC)), C --> icmp ult A, C
3144 // when C <= (1 << ShAmtC).
3145 const APInt *ShAmtC;
3146 Value *A;
3147 unsigned BitWidth = C.getBitWidth();
3148 if (Pred == ICmpInst::ICMP_ULT &&
3149 match(Add,
3150 m_c_NUWAdd(m_Value(A), m_LShr(m_Deferred(A), m_APInt(ShAmtC)))) &&
3151 ShAmtC->ult(BitWidth) &&
3152 C.ule(APInt::getOneBitSet(BitWidth, ShAmtC->getZExtValue())))
3153 return new ICmpInst(Pred, A, ConstantInt::get(A->getType(), C));
3154
3155 const APInt *C2;
3156 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3157 return nullptr;
3158
3159 // Fold icmp pred (add X, C2), C.
3160 Type *Ty = Add->getType();
3161
3162 // If the add does not wrap, we can always adjust the compare by subtracting
3163 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3164 // have been canonicalized to SGT/SLT/UGT/ULT.
3165 if (Add->hasNoUnsignedWrap() &&
3166 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT)) {
3167 bool Overflow;
3168 APInt NewC = C.usub_ov(*C2, Overflow);
3169 // If there is overflow, the result must be true or false.
3170 if (!Overflow)
3171 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3172 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
3173 }
3174
3175 CmpInst::Predicate ChosenPred = Pred.getPreferredSignedPredicate();
3176
3177 if (Add->hasNoSignedWrap() &&
3178 (ChosenPred == ICmpInst::ICMP_SGT || ChosenPred == ICmpInst::ICMP_SLT)) {
3179 bool Overflow;
3180 APInt NewC = C.ssub_ov(*C2, Overflow);
3181 if (!Overflow)
3182 // icmp samesign ugt/ult (add nsw X, C2), C
3183 // -> icmp sgt/slt X, (C - C2)
3184 return new ICmpInst(ChosenPred, X, ConstantInt::get(Ty, NewC));
3185 }
3186
3187 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3188 C.isNonNegative() && (C - *C2).isNonNegative() &&
3189 computeConstantRange(X, /*ForSigned=*/true, SQ.getWithInstruction(&Cmp))
3190 .add(*C2)
3191 .isAllNonNegative())
3192 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
3193 ConstantInt::get(Ty, C - *C2));
3194
3195 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
3196 const APInt &Upper = CR.getUpper();
3197 const APInt &Lower = CR.getLower();
3198 if (Cmp.isSigned()) {
3199 if (Lower.isSignMask())
3200 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
3201 if (Upper.isSignMask())
3202 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
3203 } else {
3204 if (Lower.isMinValue())
3205 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
3206 if (Upper.isMinValue())
3207 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
3208 }
3209
3210 // This set of folds is intentionally placed after folds that use no-wrapping
3211 // flags because those folds are likely better for later analysis/codegen.
3212 const APInt SMax = APInt::getSignedMaxValue(Ty->getScalarSizeInBits());
3213 const APInt SMin = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
3214
3215 // Fold compare with offset to opposite sign compare if it eliminates offset:
3216 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3217 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3218 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));
3219
3220 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3221 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3222 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));
3223
3224 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3225 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3226 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));
3227
3228 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3229 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3230 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));
3231
3232 // (X + -1) <u C --> X <=u C (if X is never null)
3233 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3234 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3235 if (llvm::isKnownNonZero(X, Q))
3236 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));
3237 }
3238
3239 if (!Add->hasOneUse())
3240 return nullptr;
3241
3242 // X+C <u C2 -> (X & -C2) == C
3243 // iff C & (C2-1) == 0
3244 // C2 is a power of 2
3245 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3246 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(X, -C),
3248
3249 // X+C2 <u C -> (X & C) == 2C
3250 // iff C == -(C2)
3251 // C2 is a power of 2
3252 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3253 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, C),
3254 ConstantInt::get(Ty, C * 2));
3255
3256 // X+C >u C2 -> (X & ~C2) != C
3257 // iff C & C2 == 0
3258 // C2+1 is a power of 2
3259 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3260 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, ~C),
3262
3263 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3264 // to the ult form.
3265 // X+C2 >u C -> X+(C2-C-1) <u ~C
3266 if (Pred == ICmpInst::ICMP_UGT)
3267 return new ICmpInst(ICmpInst::ICMP_ULT,
3268 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
3269 ConstantInt::get(Ty, ~C));
3270
3271 // zext(V) + C2 pred C -> V + C3 pred' C4
3272 Value *V;
3273 if (match(X, m_ZExt(m_Value(V)))) {
3274 Type *NewCmpTy = V->getType();
3275 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();
3276 if (shouldChangeType(Ty, NewCmpTy)) {
3277 ConstantRange SrcCR = CR.truncate(NewCmpBW, TruncInst::NoUnsignedWrap);
3278 CmpInst::Predicate EquivPred;
3279 APInt EquivInt;
3280 APInt EquivOffset;
3281
3282 SrcCR.getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
3283 return new ICmpInst(
3284 EquivPred,
3285 EquivOffset.isZero()
3286 ? V
3287 : Builder.CreateAdd(V, ConstantInt::get(NewCmpTy, EquivOffset)),
3288 ConstantInt::get(NewCmpTy, EquivInt));
3289 }
3290 }
3291
3292 return nullptr;
3293}
3294
3296 Value *&RHS, ConstantInt *&Less,
3297 ConstantInt *&Equal,
3298 ConstantInt *&Greater) {
3299 // TODO: Generalize this to work with other comparison idioms or ensure
3300 // they get canonicalized into this form.
3301
3302 // select i1 (a == b),
3303 // i32 Equal,
3304 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3305 // where Equal, Less and Greater are placeholders for any three constants.
3306 CmpPredicate PredA;
3307 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||
3308 !ICmpInst::isEquality(PredA))
3309 return false;
3310 Value *EqualVal = SI->getTrueValue();
3311 Value *UnequalVal = SI->getFalseValue();
3312 // We still can get non-canonical predicate here, so canonicalize.
3313 if (PredA == ICmpInst::ICMP_NE)
3314 std::swap(EqualVal, UnequalVal);
3315 if (!match(EqualVal, m_ConstantInt(Equal)))
3316 return false;
3317 CmpPredicate PredB;
3318 Value *LHS2, *RHS2;
3319 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),
3320 m_ConstantInt(Less), m_ConstantInt(Greater))))
3321 return false;
3322 // We can get predicate mismatch here, so canonicalize if possible:
3323 // First, ensure that 'LHS' match.
3324 if (LHS2 != LHS) {
3325 // x sgt y <--> y slt x
3326 std::swap(LHS2, RHS2);
3327 PredB = ICmpInst::getSwappedPredicate(PredB);
3328 }
3329 if (LHS2 != LHS)
3330 return false;
3331 // We also need to canonicalize 'RHS'.
3332 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
3333 // x sgt C-1 <--> x sge C <--> not(x slt C)
3334 auto FlippedStrictness =
3336 if (!FlippedStrictness)
3337 return false;
3338 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3339 "basic correctness failure");
3340 RHS2 = FlippedStrictness->second;
3341 // And kind-of perform the result swap.
3342 std::swap(Less, Greater);
3343 PredB = ICmpInst::ICMP_SLT;
3344 }
3345 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3346}
3347
3350 ConstantInt *C) {
3351
3352 assert(C && "Cmp RHS should be a constant int!");
3353 // If we're testing a constant value against the result of a three way
3354 // comparison, the result can be expressed directly in terms of the
3355 // original values being compared. Note: We could possibly be more
3356 // aggressive here and remove the hasOneUse test. The original select is
3357 // really likely to simplify or sink when we remove a test of the result.
3358 Value *OrigLHS, *OrigRHS;
3359 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3360 if (Cmp.hasOneUse() &&
3361 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,
3362 C3GreaterThan)) {
3363 assert(C1LessThan && C2Equal && C3GreaterThan);
3364
3365 bool TrueWhenLessThan = ICmpInst::compare(
3366 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate());
3367 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(),
3368 Cmp.getPredicate());
3369 bool TrueWhenGreaterThan = ICmpInst::compare(
3370 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate());
3371
3372 // This generates the new instruction that will replace the original Cmp
3373 // Instruction. Instead of enumerating the various combinations when
3374 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3375 // false, we rely on chaining of ORs and future passes of InstCombine to
3376 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3377
3378 // When none of the three constants satisfy the predicate for the RHS (C),
3379 // the entire original Cmp can be simplified to a false.
3380 Value *Cond = Builder.getFalse();
3381 if (TrueWhenLessThan)
3382 Cond = Builder.CreateOr(
3383 Cond, Builder.CreateICmp(ICmpInst::ICMP_SLT, OrigLHS, OrigRHS));
3384 if (TrueWhenEqual)
3385 Cond = Builder.CreateOr(
3386 Cond, Builder.CreateICmp(ICmpInst::ICMP_EQ, OrigLHS, OrigRHS));
3387 if (TrueWhenGreaterThan)
3388 Cond = Builder.CreateOr(
3389 Cond, Builder.CreateICmp(ICmpInst::ICMP_SGT, OrigLHS, OrigRHS));
3390
3391 return replaceInstUsesWith(Cmp, Cond);
3392 }
3393 return nullptr;
3394}
3395
3397 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));
3398 if (!Bitcast)
3399 return nullptr;
3400
3401 ICmpInst::Predicate Pred = Cmp.getPredicate();
3402 Value *Op1 = Cmp.getOperand(1);
3403 Value *BCSrcOp = Bitcast->getOperand(0);
3404 Type *SrcType = Bitcast->getSrcTy();
3405 Type *DstType = Bitcast->getType();
3406
3407 // Make sure the bitcast doesn't change between scalar and vector and
3408 // doesn't change the number of vector elements.
3409 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3410 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3411 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3412 Value *X;
3413 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {
3414 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3415 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3416 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3417 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3418 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3419 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3420 match(Op1, m_Zero()))
3421 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3422
3423 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3424 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))
3425 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));
3426
3427 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3428 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))
3429 return new ICmpInst(Pred, X,
3430 ConstantInt::getAllOnesValue(X->getType()));
3431 }
3432
3433 // Zero-equality checks are preserved through unsigned floating-point casts:
3434 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3435 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3436 if (match(BCSrcOp, m_UIToFP(m_Value(X))))
3437 if (Cmp.isEquality() && match(Op1, m_Zero()))
3438 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3439
3440 const APInt *C;
3441 bool TrueIfSigned;
3442 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {
3443 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3444 // the FP extend/truncate because that cast does not change the sign-bit.
3445 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3446 // The sign-bit is always the most significant bit in those types.
3447 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&
3448 (match(BCSrcOp, m_FPExt(m_Value(X))) ||
3449 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {
3450 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3451 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3452 Type *XType = X->getType();
3453
3454 // We can't currently handle Power style floating point operations here.
3455 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3456 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());
3457 if (auto *XVTy = dyn_cast<VectorType>(XType))
3458 NewType = VectorType::get(NewType, XVTy->getElementCount());
3459 Value *NewBitcast = Builder.CreateBitCast(X, NewType);
3460 if (TrueIfSigned)
3461 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3462 ConstantInt::getNullValue(NewType));
3463 else
3464 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3466 }
3467 }
3468
3469 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3470 Type *FPType = SrcType->getScalarType();
3471 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3472 Attribute::NoImplicitFloat) &&
3473 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3474 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3475 if (Mask & (fcInf | fcZero)) {
3476 if (Pred == ICmpInst::ICMP_NE)
3477 Mask = ~Mask;
3478 return replaceInstUsesWith(Cmp,
3479 Builder.createIsFPClass(BCSrcOp, Mask));
3480 }
3481 }
3482 }
3483 }
3484
3485 const APInt *C;
3486 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||
3487 !SrcType->isIntOrIntVectorTy())
3488 return nullptr;
3489
3490 // If this is checking if all elements of a vector compare are set or not,
3491 // invert the casted vector equality compare and test if all compare
3492 // elements are clear or not. Compare against zero is generally easier for
3493 // analysis and codegen.
3494 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3495 // Example: are all elements equal? --> are zero elements not equal?
3496 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3497 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3498 if (Value *NotBCSrcOp =
3499 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {
3500 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);
3501 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));
3502 }
3503 }
3504
3505 // If this is checking if all elements of an extended vector are clear or not,
3506 // compare in a narrow type to eliminate the extend:
3507 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3508 Value *X;
3509 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3510 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {
3511 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {
3512 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());
3513 Value *NewCast = Builder.CreateBitCast(X, NewType);
3514 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));
3515 }
3516 }
3517
3518 // Folding: icmp <pred> iN X, C
3519 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3520 // and C is a splat of a K-bit pattern
3521 // and SC is a constant vector = <C', C', C', ..., C'>
3522 // Into:
3523 // %E = extractelement <M x iK> %vec, i32 C'
3524 // icmp <pred> iK %E, trunc(C)
3525 Value *Vec;
3526 ArrayRef<int> Mask;
3527 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {
3528 // Check whether every element of Mask is the same constant
3529 if (all_equal(Mask)) {
3530 auto *VecTy = cast<VectorType>(SrcType);
3531 auto *EltTy = cast<IntegerType>(VecTy->getElementType());
3532 if (C->isSplat(EltTy->getBitWidth())) {
3533 // Fold the icmp based on the value of C
3534 // If C is M copies of an iK sized bit pattern,
3535 // then:
3536 // => %E = extractelement <N x iK> %vec, i32 Elem
3537 // icmp <pred> iK %SplatVal, <pattern>
3538 Value *Elem = Builder.getInt32(Mask[0]);
3539 Value *Extract = Builder.CreateExtractElement(Vec, Elem);
3540 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));
3541 return new ICmpInst(Pred, Extract, NewC);
3542 }
3543 }
3544 }
3545 return nullptr;
3546}
3547
3548/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3549/// where X is some kind of instruction.
3551 const APInt *C;
3552
3553 if (match(Cmp.getOperand(1), m_APInt(C))) {
3554 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))
3555 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))
3556 return I;
3557
3558 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))
3559 // For now, we only support constant integers while folding the
3560 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3561 // similar to the cases handled by binary ops above.
3562 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))
3563 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))
3564 return I;
3565
3566 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))
3567 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))
3568 return I;
3569
3570 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))
3572 return I;
3573
3574 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3575 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3576 // TODO: This checks one-use, but that is not strictly necessary.
3577 Value *Cmp0 = Cmp.getOperand(0);
3578 Value *X, *Y;
3579 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3580 (match(Cmp0,
3582 m_Value(X), m_Value(Y)))) ||
3583 match(Cmp0,
3585 m_Value(X), m_Value(Y))))))
3586 return new ICmpInst(Cmp.getPredicate(), X, Y);
3587 }
3588
3589 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))
3591
3592 return nullptr;
3593}
3594
3595/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3596/// icmp eq/ne BO, C.
3598 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3599 // TODO: Some of these folds could work with arbitrary constants, but this
3600 // function is limited to scalar and vector splat constants.
3601 if (!Cmp.isEquality())
3602 return nullptr;
3603
3604 ICmpInst::Predicate Pred = Cmp.getPredicate();
3605 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3606 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
3607 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
3608
3609 switch (BO->getOpcode()) {
3610 case Instruction::SRem:
3611 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3612 if (C.isZero() && BO->hasOneUse()) {
3613 const APInt *BOC;
3614 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
3615 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());
3616 return new ICmpInst(Pred, NewRem,
3618 }
3619 }
3620 break;
3621 case Instruction::Add: {
3622 // (A + C2) == C --> A == (C - C2)
3623 // (A + C2) != C --> A != (C - C2)
3624 // TODO: Remove the one-use limitation? See discussion in D58633.
3625 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {
3626 if (BO->hasOneUse())
3627 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));
3628 } else if (C.isZero()) {
3629 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3630 // efficiently invertible, or if the add has just this one use.
3631 if (Value *NegVal = dyn_castNegVal(BOp1))
3632 return new ICmpInst(Pred, BOp0, NegVal);
3633 if (Value *NegVal = dyn_castNegVal(BOp0))
3634 return new ICmpInst(Pred, NegVal, BOp1);
3635 if (BO->hasOneUse()) {
3636 // (add nuw A, B) != 0 -> (or A, B) != 0
3637 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
3638 Value *Or = Builder.CreateOr(BOp0, BOp1);
3639 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
3640 }
3641 Value *Neg = Builder.CreateNeg(BOp1);
3642 Neg->takeName(BO);
3643 return new ICmpInst(Pred, BOp0, Neg);
3644 }
3645 }
3646 break;
3647 }
3648 case Instruction::Xor:
3649 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
3650 // For the xor case, we can xor two constants together, eliminating
3651 // the explicit xor.
3652 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
3653 } else if (C.isZero()) {
3654 // Replace ((xor A, B) != 0) with (A != B)
3655 return new ICmpInst(Pred, BOp0, BOp1);
3656 }
3657 break;
3658 case Instruction::Or: {
3659 const APInt *BOC;
3660 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3661 // Comparing if all bits outside of a constant mask are set?
3662 // Replace (X | C) == -1 with (X & ~C) == ~C.
3663 // This removes the -1 constant.
3665 Value *And = Builder.CreateAnd(BOp0, NotBOC);
3666 return new ICmpInst(Pred, And, NotBOC);
3667 }
3668 // (icmp eq (or (select cond, 0, NonZero), Other), 0)
3669 // -> (and cond, (icmp eq Other, 0))
3670 // (icmp ne (or (select cond, NonZero, 0), Other), 0)
3671 // -> (or cond, (icmp ne Other, 0))
3672 Value *Cond, *TV, *FV, *Other, *Sel;
3673 if (C.isZero() &&
3674 match(BO,
3677 m_Value(FV))),
3678 m_Value(Other)))) &&
3679 Cond->getType() == Cmp.getType()) {
3680 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3681 // Easy case is if eq/ne matches whether 0 is trueval/falseval.
3682 if (Pred == ICmpInst::ICMP_EQ
3683 ? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))
3684 : (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {
3685 Value *Cmp = Builder.CreateICmp(
3686 Pred, Other, Constant::getNullValue(Other->getType()));
3688 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3689 Cond);
3690 }
3691 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3692 // case we need to invert the select condition so we need to be careful to
3693 // avoid creating extra instructions.
3694 // (icmp ne (or (select cond, 0, NonZero), Other), 0)
3695 // -> (or (not cond), (icmp ne Other, 0))
3696 // (icmp eq (or (select cond, NonZero, 0), Other), 0)
3697 // -> (and (not cond), (icmp eq Other, 0))
3698 //
3699 // Only do this if the inner select has one use, in which case we are
3700 // replacing `select` with `(not cond)`. Otherwise, we will create more
3701 // uses. NB: Trying to freely invert cond doesn't make sense here, as if
3702 // cond was freely invertable, the select arms would have been inverted.
3703 if (Sel->hasOneUse() &&
3704 (Pred == ICmpInst::ICMP_EQ
3705 ? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))
3706 : (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {
3707 Value *NotCond = Builder.CreateNot(Cond);
3708 Value *Cmp = Builder.CreateICmp(
3709 Pred, Other, Constant::getNullValue(Other->getType()));
3711 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3712 NotCond);
3713 }
3714 }
3715 break;
3716 }
3717 case Instruction::UDiv:
3718 case Instruction::SDiv:
3719 if (BO->isExact()) {
3720 // div exact X, Y eq/ne 0 -> X eq/ne 0
3721 // div exact X, Y eq/ne 1 -> X eq/ne Y
3722 // div exact X, Y eq/ne C ->
3723 // if Y * C never-overflow && OneUse:
3724 // -> Y * C eq/ne X
3725 if (C.isZero())
3726 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));
3727 else if (C.isOne())
3728 return new ICmpInst(Pred, BOp0, BOp1);
3729 else if (BO->hasOneUse()) {
3731 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,
3732 Cmp.getOperand(1), BO);
3734 Value *YC =
3735 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));
3736 return new ICmpInst(Pred, YC, BOp0);
3737 }
3738 }
3739 }
3740 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3741 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3742 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3743 return new ICmpInst(NewPred, BOp1, BOp0);
3744 }
3745 break;
3746 default:
3747 break;
3748 }
3749 return nullptr;
3750}
3751
3753 const APInt &CRhs,
3754 InstCombiner::BuilderTy &Builder,
3755 const SimplifyQuery &Q) {
3756 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3757 "Non-ctpop intrin in ctpop fold");
3758 if (!CtpopLhs->hasOneUse())
3759 return nullptr;
3760
3761 // Power of 2 test:
3762 // isPow2OrZero : ctpop(X) u< 2
3763 // isPow2 : ctpop(X) == 1
3764 // NotPow2OrZero: ctpop(X) u> 1
3765 // NotPow2 : ctpop(X) != 1
3766 // If we know any bit of X can be folded to:
3767 // IsPow2 : X & (~Bit) == 0
3768 // NotPow2 : X & (~Bit) != 0
3769 const ICmpInst::Predicate Pred = I.getPredicate();
3770 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3771 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3772 Value *Op = CtpopLhs->getArgOperand(0);
3773 KnownBits OpKnown = computeKnownBits(Op, Q.DL, Q.AC, Q.CxtI, Q.DT);
3774 // No need to check for count > 1, that should be already constant folded.
3775 if (OpKnown.countMinPopulation() == 1) {
3776 Value *And = Builder.CreateAnd(
3777 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));
3778 return new ICmpInst(
3779 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3782 And, Constant::getNullValue(Op->getType()));
3783 }
3784 }
3785
3786 return nullptr;
3787}
3788
3789/// Fold an equality icmp with LLVM intrinsic and constant operand.
3791 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3792 Type *Ty = II->getType();
3793 unsigned BitWidth = C.getBitWidth();
3794 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3795
3796 switch (II->getIntrinsicID()) {
3797 case Intrinsic::abs:
3798 // abs(A) == 0 -> A == 0
3799 // abs(A) == INT_MIN -> A == INT_MIN
3800 if (C.isZero() || C.isMinSignedValue())
3801 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));
3802 break;
3803
3804 case Intrinsic::bswap:
3805 // bswap(A) == C -> A == bswap(C)
3806 return new ICmpInst(Pred, II->getArgOperand(0),
3807 ConstantInt::get(Ty, C.byteSwap()));
3808
3809 case Intrinsic::bitreverse:
3810 // bitreverse(A) == C -> A == bitreverse(C)
3811 return new ICmpInst(Pred, II->getArgOperand(0),
3812 ConstantInt::get(Ty, C.reverseBits()));
3813
3814 case Intrinsic::ctlz:
3815 case Intrinsic::cttz: {
3816 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3817 if (C == BitWidth)
3818 return new ICmpInst(Pred, II->getArgOperand(0),
3820
3821 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3822 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3823 // Limit to one use to ensure we don't increase instruction count.
3824 unsigned Num = C.getLimitedValue(BitWidth);
3825 if (Num != BitWidth && II->hasOneUse()) {
3826 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3827 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)
3828 : APInt::getHighBitsSet(BitWidth, Num + 1);
3829 APInt Mask2 = IsTrailing
3832 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),
3833 ConstantInt::get(Ty, Mask2));
3834 }
3835 break;
3836 }
3837
3838 case Intrinsic::ctpop: {
3839 // popcount(A) == 0 -> A == 0 and likewise for !=
3840 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3841 bool IsZero = C.isZero();
3842 if (IsZero || C == BitWidth)
3843 return new ICmpInst(Pred, II->getArgOperand(0),
3844 IsZero ? Constant::getNullValue(Ty)
3846
3847 break;
3848 }
3849
3850 case Intrinsic::fshl:
3851 case Intrinsic::fshr:
3852 if (II->getArgOperand(0) == II->getArgOperand(1)) {
3853 const APInt *RotAmtC;
3854 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3855 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3856 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))
3857 return new ICmpInst(Pred, II->getArgOperand(0),
3858 II->getIntrinsicID() == Intrinsic::fshl
3859 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))
3860 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));
3861 }
3862 break;
3863
3864 case Intrinsic::umax:
3865 case Intrinsic::uadd_sat: {
3866 // uadd.sat(a, b) == 0 -> (a | b) == 0
3867 // umax(a, b) == 0 -> (a | b) == 0
3868 if (C.isZero() && II->hasOneUse()) {
3869 Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1));
3870 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3871 }
3872 break;
3873 }
3874
3875 case Intrinsic::ssub_sat:
3876 // ssub.sat(a, b) == 0 -> a == b
3877 //
3878 // Note this doesn't work for ssub.sat.i1 because ssub.sat.i1 0, -1 = 0
3879 // (because 1 saturates to 0). Just skip the optimization for i1.
3880 if (C.isZero() && II->getType()->getScalarSizeInBits() > 1)
3881 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3882 break;
3883 case Intrinsic::usub_sat: {
3884 // usub.sat(a, b) == 0 -> a <= b
3885 if (C.isZero()) {
3886 ICmpInst::Predicate NewPred =
3888 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));
3889 }
3890 break;
3891 }
3892 default:
3893 break;
3894 }
3895
3896 return nullptr;
3897}
3898
3899/// Fold an icmp with LLVM intrinsics
3900static Instruction *
3902 InstCombiner::BuilderTy &Builder) {
3903 assert(Cmp.isEquality());
3904
3905 ICmpInst::Predicate Pred = Cmp.getPredicate();
3906 Value *Op0 = Cmp.getOperand(0);
3907 Value *Op1 = Cmp.getOperand(1);
3908 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);
3909 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);
3910 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3911 return nullptr;
3912
3913 switch (IIOp0->getIntrinsicID()) {
3914 case Intrinsic::bswap:
3915 case Intrinsic::bitreverse:
3916 // If both operands are byte-swapped or bit-reversed, just compare the
3917 // original values.
3918 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3919 case Intrinsic::fshl:
3920 case Intrinsic::fshr: {
3921 // If both operands are rotated by same amount, just compare the
3922 // original values.
3923 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))
3924 break;
3925 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))
3926 break;
3927 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))
3928 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3929
3930 // rotate(X, AmtX) == rotate(Y, AmtY)
3931 // -> rotate(X, AmtX - AmtY) == Y
3932 // Do this if either both rotates have one use or if only one has one use
3933 // and AmtX/AmtY are constants.
3934 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3935 if (OneUses == 2 ||
3936 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&
3937 match(IIOp1->getOperand(2), m_ImmConstant()))) {
3938 Value *SubAmt =
3939 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));
3940 Value *CombinedRotate = Builder.CreateIntrinsic(
3941 Op0->getType(), IIOp0->getIntrinsicID(),
3942 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});
3943 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);
3944 }
3945 } break;
3946 default:
3947 break;
3948 }
3949
3950 return nullptr;
3951}
3952
3953/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3954/// where X is some kind of instruction and C is AllowPoison.
3955/// TODO: Move more folds which allow poison to this function.
3958 const APInt &C) {
3959 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3960 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {
3961 switch (II->getIntrinsicID()) {
3962 default:
3963 break;
3964 case Intrinsic::fshl:
3965 case Intrinsic::fshr:
3966 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {
3967 // (rot X, ?) == 0/-1 --> X == 0/-1
3968 if (C.isZero() || C.isAllOnes())
3969 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
3970 }
3971 break;
3972 }
3973 }
3974
3975 return nullptr;
3976}
3977
3978/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
3980 BinaryOperator *BO,
3981 const APInt &C) {
3982 switch (BO->getOpcode()) {
3983 case Instruction::Xor:
3984 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
3985 return I;
3986 break;
3987 case Instruction::And:
3988 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
3989 return I;
3990 break;
3991 case Instruction::Or:
3992 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
3993 return I;
3994 break;
3995 case Instruction::Mul:
3996 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
3997 return I;
3998 break;
3999 case Instruction::Shl:
4000 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
4001 return I;
4002 break;
4003 case Instruction::LShr:
4004 case Instruction::AShr:
4005 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
4006 return I;
4007 break;
4008 case Instruction::SRem:
4009 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
4010 return I;
4011 break;
4012 case Instruction::UDiv:
4013 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
4014 return I;
4015 [[fallthrough]];
4016 case Instruction::SDiv:
4017 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
4018 return I;
4019 break;
4020 case Instruction::Sub:
4021 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
4022 return I;
4023 break;
4024 case Instruction::Add:
4025 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
4026 return I;
4027 break;
4028 default:
4029 break;
4030 }
4031
4032 // TODO: These folds could be refactored to be part of the above calls.
4034 return I;
4035
4036 // Fall back to handling `icmp pred (select A ? C1 : C2) binop (select B ? C3
4037 // : C4), C5` pattern, by computing a truth table of the four constant
4038 // variants.
4040}
4041
4042static Instruction *
4044 const APInt &C,
4045 InstCombiner::BuilderTy &Builder) {
4046 // This transform may end up producing more than one instruction for the
4047 // intrinsic, so limit it to one user of the intrinsic.
4048 if (!II->hasOneUse())
4049 return nullptr;
4050
4051 // Let Y = [add/sub]_sat(X, C) pred C2
4052 // SatVal = The saturating value for the operation
4053 // WillWrap = Whether or not the operation will underflow / overflow
4054 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
4055 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
4056 //
4057 // When (SatVal pred C2) is true, then
4058 // Y = WillWrap ? true : ((X binop C) pred C2)
4059 // => Y = WillWrap || ((X binop C) pred C2)
4060 // else
4061 // Y = WillWrap ? false : ((X binop C) pred C2)
4062 // => Y = !WillWrap ? ((X binop C) pred C2) : false
4063 // => Y = !WillWrap && ((X binop C) pred C2)
4064 Value *Op0 = II->getOperand(0);
4065 Value *Op1 = II->getOperand(1);
4066
4067 const APInt *COp1;
4068 // This transform only works when the intrinsic has an integral constant or
4069 // splat vector as the second operand.
4070 if (!match(Op1, m_APInt(COp1)))
4071 return nullptr;
4072
4073 APInt SatVal;
4074 switch (II->getIntrinsicID()) {
4075 default:
4077 "This function only works with usub_sat and uadd_sat for now!");
4078 case Intrinsic::uadd_sat:
4079 SatVal = APInt::getAllOnes(C.getBitWidth());
4080 break;
4081 case Intrinsic::usub_sat:
4082 SatVal = APInt::getZero(C.getBitWidth());
4083 break;
4084 }
4085
4086 // Check (SatVal pred C2)
4087 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);
4088
4089 // !WillWrap.
4091 II->getBinaryOp(), *COp1, II->getNoWrapKind());
4092
4093 // WillWrap.
4094 if (SatValCheck)
4095 C1 = C1.inverse();
4096
4098 if (II->getBinaryOp() == Instruction::Add)
4099 C2 = C2.sub(*COp1);
4100 else
4101 C2 = C2.add(*COp1);
4102
4103 Instruction::BinaryOps CombiningOp =
4104 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
4105
4106 std::optional<ConstantRange> Combination;
4107 if (CombiningOp == Instruction::BinaryOps::Or)
4108 Combination = C1.exactUnionWith(C2);
4109 else /* CombiningOp == Instruction::BinaryOps::And */
4110 Combination = C1.exactIntersectWith(C2);
4111
4112 if (!Combination)
4113 return nullptr;
4114
4115 CmpInst::Predicate EquivPred;
4116 APInt EquivInt;
4117 APInt EquivOffset;
4118
4119 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
4120
4121 return new ICmpInst(
4122 EquivPred,
4123 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),
4124 ConstantInt::get(Op1->getType(), EquivInt));
4125}
4126
4127static Instruction *
4129 const APInt &C,
4130 InstCombiner::BuilderTy &Builder) {
4131 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
4132 switch (Pred) {
4133 case ICmpInst::ICMP_EQ:
4134 case ICmpInst::ICMP_NE:
4135 if (C.isZero())
4136 NewPredicate = Pred;
4137 else if (C.isOne())
4138 NewPredicate =
4140 else if (C.isAllOnes())
4141 NewPredicate =
4143 break;
4144
4145 case ICmpInst::ICMP_SGT:
4146 if (C.isAllOnes())
4147 NewPredicate = ICmpInst::ICMP_UGE;
4148 else if (C.isZero())
4149 NewPredicate = ICmpInst::ICMP_UGT;
4150 break;
4151
4152 case ICmpInst::ICMP_SLT:
4153 if (C.isZero())
4154 NewPredicate = ICmpInst::ICMP_ULT;
4155 else if (C.isOne())
4156 NewPredicate = ICmpInst::ICMP_ULE;
4157 break;
4158
4159 case ICmpInst::ICMP_ULT:
4160 if (C.ugt(1))
4161 NewPredicate = ICmpInst::ICMP_UGE;
4162 break;
4163
4164 case ICmpInst::ICMP_UGT:
4165 if (!C.isZero() && !C.isAllOnes())
4166 NewPredicate = ICmpInst::ICMP_ULT;
4167 break;
4168
4169 default:
4170 break;
4171 }
4172
4173 if (!NewPredicate)
4174 return nullptr;
4175
4176 if (I->getIntrinsicID() == Intrinsic::scmp)
4177 NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate);
4178 Value *LHS = I->getOperand(0);
4179 Value *RHS = I->getOperand(1);
4180 return new ICmpInst(*NewPredicate, LHS, RHS);
4181}
4182
4183/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
4186 const APInt &C) {
4187 ICmpInst::Predicate Pred = Cmp.getPredicate();
4188
4189 // Handle folds that apply for any kind of icmp.
4190 switch (II->getIntrinsicID()) {
4191 default:
4192 break;
4193 case Intrinsic::uadd_sat:
4194 case Intrinsic::usub_sat:
4195 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
4196 Pred, cast<SaturatingInst>(II), C, Builder))
4197 return Folded;
4198 break;
4199 case Intrinsic::ctpop: {
4200 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
4201 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))
4202 return R;
4203 } break;
4204 case Intrinsic::scmp:
4205 case Intrinsic::ucmp:
4206 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, II, C, Builder))
4207 return Folded;
4208 break;
4209 }
4210
4211 if (Cmp.isEquality())
4212 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
4213
4214 Type *Ty = II->getType();
4215 unsigned BitWidth = C.getBitWidth();
4216 switch (II->getIntrinsicID()) {
4217 case Intrinsic::ctpop: {
4218 // (ctpop X > BitWidth - 1) --> X == -1
4219 Value *X = II->getArgOperand(0);
4220 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
4221 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,
4223 // (ctpop X < BitWidth) --> X != -1
4224 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
4225 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,
4227 break;
4228 }
4229 case Intrinsic::ctlz: {
4230 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
4231 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4232 unsigned Num = C.getLimitedValue();
4233 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);
4234 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,
4235 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4236 }
4237
4238 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
4239 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4240 unsigned Num = C.getLimitedValue();
4242 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,
4243 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4244 }
4245 break;
4246 }
4247 case Intrinsic::cttz: {
4248 // Limit to one use to ensure we don't increase instruction count.
4249 if (!II->hasOneUse())
4250 return nullptr;
4251
4252 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
4253 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4254 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);
4255 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,
4256 Builder.CreateAnd(II->getArgOperand(0), Mask),
4258 }
4259
4260 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
4261 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4262 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());
4263 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,
4264 Builder.CreateAnd(II->getArgOperand(0), Mask),
4266 }
4267 break;
4268 }
4269 case Intrinsic::ssub_sat:
4270 // ssub.sat(a, b) spred 0 -> a spred b
4271 //
4272 // Note this doesn't work for ssub.sat.i1 because ssub.sat.i1 0, -1 = 0
4273 // (because 1 saturates to 0). Just skip the optimization for i1.
4274 if (ICmpInst::isSigned(Pred) && C.getBitWidth() > 1) {
4275 if (C.isZero())
4276 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
4277 // X s<= 0 is cannonicalized to X s< 1
4278 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
4279 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),
4280 II->getArgOperand(1));
4281 // X s>= 0 is cannonicalized to X s> -1
4282 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
4283 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),
4284 II->getArgOperand(1));
4285 }
4286 break;
4287 case Intrinsic::abs: {
4288 if (!II->hasOneUse())
4289 return nullptr;
4290
4291 Value *X = II->getArgOperand(0);
4292 bool IsIntMinPoison =
4293 cast<ConstantInt>(II->getArgOperand(1))->getValue().isOne();
4294
4295 // If C >= 0:
4296 // abs(X) u> C --> X + C u> 2 * C
4297 if (Pred == CmpInst::ICMP_UGT && C.isNonNegative()) {
4298 return new ICmpInst(ICmpInst::ICMP_UGT,
4299 Builder.CreateAdd(X, ConstantInt::get(Ty, C)),
4300 ConstantInt::get(Ty, 2 * C));
4301 }
4302
4303 // If abs(INT_MIN) is poison and C >= 1:
4304 // abs(X) u< C --> X + (C - 1) u<= 2 * (C - 1)
4305 if (IsIntMinPoison && Pred == CmpInst::ICMP_ULT && C.sge(1)) {
4306 return new ICmpInst(ICmpInst::ICMP_ULE,
4307 Builder.CreateAdd(X, ConstantInt::get(Ty, C - 1)),
4308 ConstantInt::get(Ty, 2 * (C - 1)));
4309 }
4310
4311 break;
4312 }
4313 default:
4314 break;
4315 }
4316
4317 return nullptr;
4318}
4319
4320/// Handle icmp with constant (but not simple integer constant) RHS.
4322 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4323 Constant *RHSC = dyn_cast<Constant>(Op1);
4325 if (!RHSC || !LHSI)
4326 return nullptr;
4327
4328 switch (LHSI->getOpcode()) {
4329 case Instruction::IntToPtr:
4330 // icmp pred inttoptr(X), null -> icmp pred X, null pointer value
4331 if (isa<ConstantPointerNull>(RHSC)) {
4332 Type *IntPtrTy = DL.getIntPtrType(RHSC->getType());
4333 if (IntPtrTy == LHSI->getOperand(0)->getType()) {
4334 APInt NullPtrValue =
4335 DL.getNullPtrValue(RHSC->getType()->getPointerAddressSpace());
4336 return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
4337 Constant::getIntegerValue(IntPtrTy, NullPtrValue));
4338 }
4339 }
4340 break;
4341
4342 case Instruction::Load:
4343 // Try to optimize things like "A[i] > 4" to index computations.
4344 if (GetElementPtrInst *GEP =
4346 if (Instruction *Res =
4348 return Res;
4349 break;
4350 }
4351
4352 return nullptr;
4353}
4354
4356 Value *RHS, const ICmpInst &I) {
4357 // Try to fold the comparison into the select arms, which will cause the
4358 // select to be converted into a logical and/or.
4359 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4360 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))
4361 return Res;
4362 if (std::optional<bool> Impl = isImpliedCondition(
4363 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))
4364 return ConstantInt::get(I.getType(), *Impl);
4365 return nullptr;
4366 };
4367
4368 ConstantInt *CI = nullptr;
4369 Value *Op1 = SimplifyOp(SI->getOperand(1), true);
4370 if (Op1)
4371 CI = dyn_cast<ConstantInt>(Op1);
4372
4373 Value *Op2 = SimplifyOp(SI->getOperand(2), false);
4374 if (Op2)
4375 CI = dyn_cast<ConstantInt>(Op2);
4376
4377 auto Simplifies = [&](Value *Op, unsigned Idx) {
4378 // A comparison of ucmp/scmp with a constant will fold into an icmp.
4379 const APInt *Dummy;
4380 return Op ||
4381 (isa<CmpIntrinsic>(SI->getOperand(Idx)) &&
4382 SI->getOperand(Idx)->hasOneUse() && match(RHS, m_APInt(Dummy)));
4383 };
4384
4385 // We only want to perform this transformation if it will not lead to
4386 // additional code. This is true if either both sides of the select
4387 // fold to a constant (in which case the icmp is replaced with a select
4388 // which will usually simplify) or this is the only user of the
4389 // select (in which case we are trading a select+icmp for a simpler
4390 // select+icmp) or all uses of the select can be replaced based on
4391 // dominance information ("Global cases").
4392 bool Transform = false;
4393 if (Op1 && Op2)
4394 Transform = true;
4395 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {
4396 // Local case
4397 if (SI->hasOneUse())
4398 Transform = true;
4399 // Global cases
4400 else if (CI && !CI->isZero())
4401 // When Op1 is constant try replacing select with second operand.
4402 // Otherwise Op2 is constant and try replacing select with first
4403 // operand.
4404 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4405 }
4406 if (Transform) {
4407 if (!Op1)
4408 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4409 if (!Op2)
4410 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4411 return SelectInst::Create(SI->getOperand(0), Op1, Op2, "", nullptr,
4412 ProfcheckDisableMetadataFixes ? nullptr : SI);
4413 }
4414
4415 return nullptr;
4416}
4417
4418// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4419static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4420 unsigned Depth = 0) {
4421 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4422 return true;
4423 if (V->getType()->getScalarSizeInBits() == 1)
4424 return true;
4426 return false;
4427 Value *X;
4429 if (!I)
4430 return false;
4431 switch (I->getOpcode()) {
4432 case Instruction::ZExt:
4433 // ZExt(Mask) is a Mask.
4434 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4435 case Instruction::SExt:
4436 // SExt(Mask) is a Mask.
4437 // SExt(~Mask) is a ~Mask.
4438 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4439 case Instruction::And:
4440 case Instruction::Or:
4441 // Mask0 | Mask1 is a Mask.
4442 // Mask0 & Mask1 is a Mask.
4443 // ~Mask0 | ~Mask1 is a ~Mask.
4444 // ~Mask0 & ~Mask1 is a ~Mask.
4445 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4446 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4447 case Instruction::Xor:
4448 if (match(V, m_Not(m_Value(X))))
4449 return isMaskOrZero(X, !Not, Q, Depth);
4450
4451 // (X ^ -X) is a ~Mask
4452 if (Not)
4453 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4454 // (X ^ (X - 1)) is a Mask
4455 else
4456 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4457 case Instruction::Select:
4458 // c ? Mask0 : Mask1 is a Mask.
4459 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4460 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4461 case Instruction::Shl:
4462 // (~Mask) << X is a ~Mask.
4463 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4464 case Instruction::LShr:
4465 // Mask >> X is a Mask.
4466 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4467 case Instruction::AShr:
4468 // Mask s>> X is a Mask.
4469 // ~Mask s>> X is a ~Mask.
4470 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4471 case Instruction::Add:
4472 // Pow2 - 1 is a Mask.
4473 if (!Not && match(I->getOperand(1), m_AllOnes()))
4474 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4475 Q.AC, Q.CxtI, Q.DT, Depth);
4476 break;
4477 case Instruction::Sub:
4478 // -Pow2 is a ~Mask.
4479 if (Not && match(I->getOperand(0), m_Zero()))
4480 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4481 Q.AC, Q.CxtI, Q.DT, Depth);
4482 break;
4483 case Instruction::Call: {
4484 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4485 switch (II->getIntrinsicID()) {
4486 // min/max(Mask0, Mask1) is a Mask.
4487 // min/max(~Mask0, ~Mask1) is a ~Mask.
4488 case Intrinsic::umax:
4489 case Intrinsic::smax:
4490 case Intrinsic::umin:
4491 case Intrinsic::smin:
4492 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4493 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4494
4495 // In the context of masks, bitreverse(Mask) == ~Mask
4496 case Intrinsic::bitreverse:
4497 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4498 default:
4499 break;
4500 }
4501 }
4502 break;
4503 }
4504 default:
4505 break;
4506 }
4507 return false;
4508}
4509
4510/// Some comparisons can be simplified.
4511/// In this case, we are looking for comparisons that look like
4512/// a check for a lossy truncation.
4513/// Folds:
4514/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4515/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4516/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4517/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4518/// Where Mask is some pattern that produces all-ones in low bits:
4519/// (-1 >> y)
4520/// ((-1 << y) >> y) <- non-canonical, has extra uses
4521/// ~(-1 << y)
4522/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4523/// The Mask can be a constant, too.
4524/// For some predicates, the operands are commutative.
4525/// For others, x can only be on a specific side.
4527 Value *Op1, const SimplifyQuery &Q,
4528 InstCombiner &IC) {
4529
4530 ICmpInst::Predicate DstPred;
4531 switch (Pred) {
4533 // x & Mask == x
4534 // x & ~Mask == 0
4535 // ~x | Mask == -1
4536 // -> x u<= Mask
4537 // x & ~Mask == ~Mask
4538 // -> ~Mask u<= x
4540 break;
4542 // x & Mask != x
4543 // x & ~Mask != 0
4544 // ~x | Mask != -1
4545 // -> x u> Mask
4546 // x & ~Mask != ~Mask
4547 // -> ~Mask u> x
4549 break;
4551 // x & Mask u< x
4552 // -> x u> Mask
4553 // x & ~Mask u< ~Mask
4554 // -> ~Mask u> x
4556 break;
4558 // x & Mask u>= x
4559 // -> x u<= Mask
4560 // x & ~Mask u>= ~Mask
4561 // -> ~Mask u<= x
4563 break;
4565 // x & Mask s< x [iff Mask s>= 0]
4566 // -> x s> Mask
4567 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4568 // -> ~Mask s> x
4570 break;
4572 // x & Mask s>= x [iff Mask s>= 0]
4573 // -> x s<= Mask
4574 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4575 // -> ~Mask s<= x
4577 break;
4578 default:
4579 // We don't support sgt,sle
4580 // ult/ugt are simplified to true/false respectively.
4581 return nullptr;
4582 }
4583
4584 Value *X, *M;
4585 // Put search code in lambda for early positive returns.
4586 auto IsLowBitMask = [&]() {
4587 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4588 X = Op1;
4589 // Look for: x & Mask pred x
4590 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4591 return !ICmpInst::isSigned(Pred) ||
4592 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4593 }
4594
4595 // Look for: x & ~Mask pred ~Mask
4596 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4597 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4598 }
4599 return false;
4600 }
4601 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4602 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4603
4604 auto Check = [&]() {
4605 // Look for: ~x | Mask == -1
4606 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4607 if (Value *NotX =
4608 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4609 X = NotX;
4610 return true;
4611 }
4612 }
4613 return false;
4614 };
4615 if (Check())
4616 return true;
4617 std::swap(X, M);
4618 return Check();
4619 }
4620 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4621 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4622 auto Check = [&]() {
4623 // Look for: x & ~Mask == 0
4624 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4625 if (Value *NotM =
4626 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4627 M = NotM;
4628 return true;
4629 }
4630 }
4631 return false;
4632 };
4633 if (Check())
4634 return true;
4635 std::swap(X, M);
4636 return Check();
4637 }
4638 return false;
4639 };
4640
4641 if (!IsLowBitMask())
4642 return nullptr;
4643
4644 return IC.Builder.CreateICmp(DstPred, X, M);
4645}
4646
4647/// Some comparisons can be simplified.
4648/// In this case, we are looking for comparisons that look like
4649/// a check for a lossy signed truncation.
4650/// Folds: (MaskedBits is a constant.)
4651/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4652/// Into:
4653/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4654/// Where KeptBits = bitwidth(%x) - MaskedBits
4655static Value *
4657 InstCombiner::BuilderTy &Builder) {
4658 CmpPredicate SrcPred;
4659 Value *X;
4660 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4661 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4662 if (!match(&I, m_c_ICmp(SrcPred,
4664 m_APInt(C1))),
4665 m_Deferred(X))))
4666 return nullptr;
4667
4668 // Potential handling of non-splats: for each element:
4669 // * if both are undef, replace with constant 0.
4670 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4671 // * if both are not undef, and are different, bailout.
4672 // * else, only one is undef, then pick the non-undef one.
4673
4674 // The shift amount must be equal.
4675 if (*C0 != *C1)
4676 return nullptr;
4677 const APInt &MaskedBits = *C0;
4678 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4679
4680 ICmpInst::Predicate DstPred;
4681 switch (SrcPred) {
4683 // ((%x << MaskedBits) a>> MaskedBits) == %x
4684 // =>
4685 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4687 break;
4689 // ((%x << MaskedBits) a>> MaskedBits) != %x
4690 // =>
4691 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4693 break;
4694 // FIXME: are more folds possible?
4695 default:
4696 return nullptr;
4697 }
4698
4699 auto *XType = X->getType();
4700 const unsigned XBitWidth = XType->getScalarSizeInBits();
4701 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4702 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4703
4704 // KeptBits = bitwidth(%x) - MaskedBits
4705 const APInt KeptBits = BitWidth - MaskedBits;
4706 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4707 // ICmpCst = (1 << KeptBits)
4708 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4709 assert(ICmpCst.isPowerOf2());
4710 // AddCst = (1 << (KeptBits-1))
4711 const APInt AddCst = ICmpCst.lshr(1);
4712 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4713
4714 // T0 = add %x, AddCst
4715 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4716 // T1 = T0 DstPred ICmpCst
4717 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4718
4719 return T1;
4720}
4721
4722// Given pattern:
4723// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4724// we should move shifts to the same hand of 'and', i.e. rewrite as
4725// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4726// We are only interested in opposite logical shifts here.
4727// One of the shifts can be truncated.
4728// If we can, we want to end up creating 'lshr' shift.
4729static Value *
4731 InstCombiner::BuilderTy &Builder) {
4732 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4733 !I.getOperand(0)->hasOneUse())
4734 return nullptr;
4735
4736 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4737
4738 // Look for an 'and' of two logical shifts, one of which may be truncated.
4739 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4740 Instruction *XShift, *MaybeTruncation, *YShift;
4741 if (!match(
4742 I.getOperand(0),
4743 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4745 m_AnyLogicalShift, m_Instruction(YShift))),
4746 m_Instruction(MaybeTruncation)))))
4747 return nullptr;
4748
4749 // We potentially looked past 'trunc', but only when matching YShift,
4750 // therefore YShift must have the widest type.
4751 Instruction *WidestShift = YShift;
4752 // Therefore XShift must have the shallowest type.
4753 // Or they both have identical types if there was no truncation.
4754 Instruction *NarrowestShift = XShift;
4755
4756 Type *WidestTy = WidestShift->getType();
4757 Type *NarrowestTy = NarrowestShift->getType();
4758 assert(NarrowestTy == I.getOperand(0)->getType() &&
4759 "We did not look past any shifts while matching XShift though.");
4760 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4761
4762 // If YShift is a 'lshr', swap the shifts around.
4763 if (match(YShift, m_LShr(m_Value(), m_Value())))
4764 std::swap(XShift, YShift);
4765
4766 // The shifts must be in opposite directions.
4767 auto XShiftOpcode = XShift->getOpcode();
4768 if (XShiftOpcode == YShift->getOpcode())
4769 return nullptr; // Do not care about same-direction shifts here.
4770
4771 Value *X, *XShAmt, *Y, *YShAmt;
4772 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4773 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4774
4775 // If one of the values being shifted is a constant, then we will end with
4776 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4777 // however, we will need to ensure that we won't increase instruction count.
4778 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4779 // At least one of the hands of the 'and' should be one-use shift.
4780 if (!match(I.getOperand(0),
4781 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4782 return nullptr;
4783 if (HadTrunc) {
4784 // Due to the 'trunc', we will need to widen X. For that either the old
4785 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4786 if (!MaybeTruncation->hasOneUse() &&
4787 !NarrowestShift->getOperand(1)->hasOneUse())
4788 return nullptr;
4789 }
4790 }
4791
4792 // We have two shift amounts from two different shifts. The types of those
4793 // shift amounts may not match. If that's the case let's bailout now.
4794 if (XShAmt->getType() != YShAmt->getType())
4795 return nullptr;
4796
4797 // As input, we have the following pattern:
4798 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4799 // We want to rewrite that as:
4800 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4801 // While we know that originally (Q+K) would not overflow
4802 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4803 // shift amounts. so it may now overflow in smaller bitwidth.
4804 // To ensure that does not happen, we need to ensure that the total maximal
4805 // shift amount is still representable in that smaller bit width.
4806 unsigned MaximalPossibleTotalShiftAmount =
4807 (WidestTy->getScalarSizeInBits() - 1) +
4808 (NarrowestTy->getScalarSizeInBits() - 1);
4809 APInt MaximalRepresentableShiftAmount =
4811 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4812 return nullptr;
4813
4814 // Can we fold (XShAmt+YShAmt) ?
4815 auto *NewShAmt = dyn_cast_or_null<Constant>(
4816 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4817 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4818 if (!NewShAmt)
4819 return nullptr;
4820 if (NewShAmt->getType() != WidestTy) {
4821 NewShAmt =
4822 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4823 if (!NewShAmt)
4824 return nullptr;
4825 }
4826 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4827
4828 // Is the new shift amount smaller than the bit width?
4829 // FIXME: could also rely on ConstantRange.
4830 if (!match(NewShAmt,
4832 APInt(WidestBitWidth, WidestBitWidth))))
4833 return nullptr;
4834
4835 // An extra legality check is needed if we had trunc-of-lshr.
4836 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4837 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4838 WidestShift]() {
4839 // It isn't obvious whether it's worth it to analyze non-constants here.
4840 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4841 // If *any* of these preconditions matches we can perform the fold.
4842 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4843 ? NewShAmt->getSplatValue()
4844 : NewShAmt;
4845 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4846 if (NewShAmtSplat &&
4847 (NewShAmtSplat->isNullValue() ||
4848 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4849 return true;
4850 // We consider *min* leading zeros so a single outlier
4851 // blocks the transform as opposed to allowing it.
4852 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4853 KnownBits Known = computeKnownBits(C, SQ.DL);
4854 unsigned MinLeadZero = Known.countMinLeadingZeros();
4855 // If the value being shifted has at most lowest bit set we can fold.
4856 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4857 if (MaxActiveBits <= 1)
4858 return true;
4859 // Precondition: NewShAmt u<= countLeadingZeros(C)
4860 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4861 return true;
4862 }
4863 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4864 KnownBits Known = computeKnownBits(C, SQ.DL);
4865 unsigned MinLeadZero = Known.countMinLeadingZeros();
4866 // If the value being shifted has at most lowest bit set we can fold.
4867 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4868 if (MaxActiveBits <= 1)
4869 return true;
4870 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4871 if (NewShAmtSplat) {
4872 APInt AdjNewShAmt =
4873 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4874 if (AdjNewShAmt.ule(MinLeadZero))
4875 return true;
4876 }
4877 }
4878 return false; // Can't tell if it's ok.
4879 };
4880 if (!CanFold())
4881 return nullptr;
4882 }
4883
4884 // All good, we can do this fold.
4885 X = Builder.CreateZExt(X, WidestTy);
4886 Y = Builder.CreateZExt(Y, WidestTy);
4887 // The shift is the same that was for X.
4888 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4889 ? Builder.CreateLShr(X, NewShAmt)
4890 : Builder.CreateShl(X, NewShAmt);
4891 Value *T1 = Builder.CreateAnd(T0, Y);
4892 return Builder.CreateICmp(I.getPredicate(), T1,
4893 Constant::getNullValue(WidestTy));
4894}
4895
4896/// Fold
4897/// (-1 u/ x) u< y
4898/// ((x * y) ?/ x) != y
4899/// to
4900/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4901/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4902/// will mean that we are looking for the opposite answer.
4904 CmpPredicate Pred;
4905 Value *X, *Y;
4907 Instruction *Div;
4908 bool NeedNegation;
4909 // Look for: (-1 u/ x) u</u>= y
4910 if (!I.isEquality() &&
4911 match(&I, m_c_ICmp(Pred,
4913 m_Instruction(Div)),
4914 m_Value(Y)))) {
4915 Mul = nullptr;
4916
4917 // Are we checking that overflow does not happen, or does happen?
4918 switch (Pred) {
4920 NeedNegation = false;
4921 break; // OK
4923 NeedNegation = true;
4924 break; // OK
4925 default:
4926 return nullptr; // Wrong predicate.
4927 }
4928 } else // Look for: ((x * y) / x) !=/== y
4929 if (I.isEquality() &&
4930 match(&I, m_c_ICmp(Pred, m_Value(Y),
4933 m_Value(X)),
4935 m_Deferred(X))),
4936 m_Instruction(Div))))) {
4937 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4938 } else
4939 return nullptr;
4940
4942 // If the pattern included (x * y), we'll want to insert new instructions
4943 // right before that original multiplication so that we can replace it.
4944 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4945 if (MulHadOtherUses)
4946 Builder.SetInsertPoint(Mul);
4947
4948 CallInst *Call = Builder.CreateIntrinsic(
4949 Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow
4950 : Intrinsic::smul_with_overflow,
4951 X->getType(), {X, Y}, /*FMFSource=*/nullptr, "mul");
4952
4953 // If the multiplication was used elsewhere, to ensure that we don't leave
4954 // "duplicate" instructions, replace uses of that original multiplication
4955 // with the multiplication result from the with.overflow intrinsic.
4956 if (MulHadOtherUses)
4957 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4958
4959 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4960 if (NeedNegation) // This technically increases instruction count.
4961 Res = Builder.CreateNot(Res, "mul.not.ov");
4962
4963 // If we replaced the mul, erase it. Do this after all uses of Builder,
4964 // as the mul is used as insertion point.
4965 if (MulHadOtherUses)
4967
4968 return Res;
4969}
4970
4972 InstCombiner::BuilderTy &Builder) {
4973 CmpPredicate Pred;
4974 Value *X;
4975 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4976
4977 if (ICmpInst::isSigned(Pred))
4978 Pred = ICmpInst::getSwappedPredicate(Pred);
4979 else if (ICmpInst::isUnsigned(Pred))
4980 Pred = ICmpInst::getSignedPredicate(Pred);
4981 // else for equality-comparisons just keep the predicate.
4982
4983 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4984 Constant::getNullValue(X->getType()), I.getName());
4985 }
4986
4987 // A value is not equal to its negation unless that value is 0 or
4988 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4989 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
4990 ICmpInst::isEquality(Pred)) {
4991 Type *Ty = X->getType();
4992 uint32_t BitWidth = Ty->getScalarSizeInBits();
4993 Constant *MaxSignedVal =
4994 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
4995 Value *And = Builder.CreateAnd(X, MaxSignedVal);
4996 Constant *Zero = Constant::getNullValue(Ty);
4997 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
4998 }
4999
5000 return nullptr;
5001}
5002
5004 InstCombinerImpl &IC) {
5005 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5006 // Normalize and operand as operand 0.
5007 CmpInst::Predicate Pred = I.getPredicate();
5008 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
5009 std::swap(Op0, Op1);
5010 Pred = ICmpInst::getSwappedPredicate(Pred);
5011 }
5012
5013 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
5014 return nullptr;
5015
5016 // (icmp (X & Y) u< X --> (X & Y) != X
5017 if (Pred == ICmpInst::ICMP_ULT)
5018 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5019
5020 // (icmp (X & Y) u>= X --> (X & Y) == X
5021 if (Pred == ICmpInst::ICMP_UGE)
5022 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5023
5024 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
5025 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
5026 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
5027 // so don't do this fold.
5028 if (!match(Op1, m_ImmConstant()))
5029 if (auto *NotOp1 =
5030 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
5031 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),
5032 Constant::getAllOnesValue(Op1->getType()));
5033 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
5034 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
5035 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),
5036 Constant::getNullValue(Op1->getType()));
5037 }
5038
5039 if (!ICmpInst::isSigned(Pred))
5040 return nullptr;
5041
5042 KnownBits KnownY = IC.computeKnownBits(A, &I);
5043 // (X & NegY) spred X --> (X & NegY) upred X
5044 if (KnownY.isNegative())
5045 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
5046
5047 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
5048 return nullptr;
5049
5050 if (KnownY.isNonNegative())
5051 // (X & PosY) s<= X --> X s>= 0
5052 // (X & PosY) s> X --> X s< 0
5053 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
5054 Constant::getNullValue(Op1->getType()));
5055
5057 // (NegX & Y) s<= NegX --> Y s< 0
5058 // (NegX & Y) s> NegX --> Y s>= 0
5060 Constant::getNullValue(A->getType()));
5061
5062 return nullptr;
5063}
5064
5066 InstCombinerImpl &IC) {
5067 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5068
5069 // Normalize or operand as operand 0.
5070 CmpInst::Predicate Pred = I.getPredicate();
5071 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
5072 std::swap(Op0, Op1);
5073 Pred = ICmpInst::getSwappedPredicate(Pred);
5074 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
5075 return nullptr;
5076 }
5077
5078 // icmp (X | Y) u<= X --> (X | Y) == X
5079 if (Pred == ICmpInst::ICMP_ULE)
5080 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5081
5082 // icmp (X | Y) u> X --> (X | Y) != X
5083 if (Pred == ICmpInst::ICMP_UGT)
5084 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5085
5086 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
5087 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
5088 if (Value *NotOp1 = IC.getFreelyInverted(
5089 Op1, !isa<Constant>(Op1) && !Op1->hasNUsesOrMore(3), &IC.Builder))
5090 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
5091 Constant::getNullValue(Op1->getType()));
5092 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
5093 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
5094 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
5095 Constant::getAllOnesValue(Op1->getType()));
5096 }
5097 return nullptr;
5098}
5099
5101 InstCombinerImpl &IC) {
5102 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5103 // Normalize xor operand as operand 0.
5104 CmpInst::Predicate Pred = I.getPredicate();
5105 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
5106 std::swap(Op0, Op1);
5107 Pred = ICmpInst::getSwappedPredicate(Pred);
5108 }
5109 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
5110 return nullptr;
5111
5112 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
5113 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
5114 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
5115 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
5117 if (PredOut != Pred && isKnownNonZero(A, Q))
5118 return new ICmpInst(PredOut, Op0, Op1);
5119
5120 // These transform work when A is negative.
5121 // X s< X^A, X s<= X^A, X u> X^A, X u>= X^A --> X s< 0
5122 // X s> X^A, X s>= X^A, X u< X^A, X u<= X^A --> X s>= 0
5123 if (match(A, m_Negative())) {
5124 CmpInst::Predicate NewPred;
5125 switch (ICmpInst::getStrictPredicate(Pred)) {
5126 default:
5127 return nullptr;
5128 case ICmpInst::ICMP_SLT:
5129 case ICmpInst::ICMP_UGT:
5130 NewPred = ICmpInst::ICMP_SLT;
5131 break;
5132 case ICmpInst::ICMP_SGT:
5133 case ICmpInst::ICMP_ULT:
5134 NewPred = ICmpInst::ICMP_SGE;
5135 break;
5136 }
5137 Constant *Const = Constant::getNullValue(Op0->getType());
5138 return new ICmpInst(NewPred, Op0, Const);
5139 }
5140
5141 return nullptr;
5142}
5143
5144/// Return true if X is a multiple of C.
5145/// TODO: Handle non-power-of-2 factors.
5146static bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q) {
5147 if (C.isOne())
5148 return true;
5149
5150 if (!C.isPowerOf2())
5151 return false;
5152
5153 return MaskedValueIsZero(X, C - 1, Q);
5154}
5155
5156/// Try to fold icmp (binop), X or icmp X, (binop).
5157/// TODO: A large part of this logic is duplicated in InstSimplify's
5158/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
5159/// duplication.
5161 const SimplifyQuery &SQ) {
5162 const SimplifyQuery Q = SQ.getWithInstruction(&I);
5163 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5164
5165 // Special logic for binary operators.
5168 if (!BO0 && !BO1)
5169 return nullptr;
5170
5171 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
5172 return NewICmp;
5173
5174 const CmpInst::Predicate Pred = I.getPredicate();
5175 Value *X;
5176
5177 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
5178 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
5179 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
5180 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5181 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
5182 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
5183 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
5184 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5185 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
5186
5187 {
5188 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
5189 Constant *C;
5190 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
5191 m_ImmConstant(C)))) &&
5192 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
5194 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
5195 }
5196 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5197 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
5198 m_ImmConstant(C)))) &&
5199 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5201 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
5202 }
5203 }
5204
5205 // (icmp eq/ne (X, -P2), INT_MIN)
5206 // -> (icmp slt/sge X, INT_MIN + P2)
5207 if (ICmpInst::isEquality(Pred) && BO0 &&
5208 match(I.getOperand(1), m_SignMask()) &&
5210 // Will Constant fold.
5211 Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
5212 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5214 BO0->getOperand(0), NewC);
5215 }
5216
5217 {
5218 // Similar to above: an unsigned overflow comparison may use offset + mask:
5219 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5220 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5221 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5222 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5223 BinaryOperator *BO;
5224 const APInt *C;
5225 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5226 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5228 CmpInst::Predicate NewPred =
5230 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5231 return new ICmpInst(NewPred, Op1, Zero);
5232 }
5233
5234 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5235 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5237 CmpInst::Predicate NewPred =
5239 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5240 return new ICmpInst(NewPred, Op0, Zero);
5241 }
5242 }
5243
5244 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5245 bool Op0HasNUW = false, Op1HasNUW = false;
5246 bool Op0HasNSW = false, Op1HasNSW = false;
5247 // Analyze the case when either Op0 or Op1 is an add instruction.
5248 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5249 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5250 bool &HasNSW, bool &HasNUW) -> bool {
5252 HasNUW = BO.hasNoUnsignedWrap();
5253 HasNSW = BO.hasNoSignedWrap();
5254 return ICmpInst::isEquality(Pred) ||
5255 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5256 (CmpInst::isSigned(Pred) && HasNSW);
5257 } else if (BO.getOpcode() == Instruction::Or) {
5258 HasNUW = true;
5259 HasNSW = true;
5260 return true;
5261 } else {
5262 return false;
5263 }
5264 };
5265 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5266
5267 if (BO0) {
5268 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
5269 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5270 }
5271 if (BO1) {
5272 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
5273 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5274 }
5275
5276 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5277 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5278 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5279 return new ICmpInst(Pred, A == Op1 ? B : A,
5280 Constant::getNullValue(Op1->getType()));
5281
5282 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5283 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5284 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5285 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
5286 C == Op0 ? D : C);
5287
5288 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5289 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5290 NoOp1WrapProblem) {
5291 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5292 Value *Y, *Z;
5293 if (A == C) {
5294 // C + B == C + D -> B == D
5295 Y = B;
5296 Z = D;
5297 } else if (A == D) {
5298 // D + B == C + D -> B == C
5299 Y = B;
5300 Z = C;
5301 } else if (B == C) {
5302 // A + C == C + D -> A == D
5303 Y = A;
5304 Z = D;
5305 } else {
5306 assert(B == D);
5307 // A + D == C + D -> A == C
5308 Y = A;
5309 Z = C;
5310 }
5311 return new ICmpInst(Pred, Y, Z);
5312 }
5313
5314 if (ICmpInst::isRelational(Pred)) {
5315 // Return if both X and Y is divisible by Z/-Z.
5316 // TODO: Generalize to check if (X - Y) is divisible by Z/-Z.
5317 auto ShareCommonDivisor = [&Q](Value *X, Value *Y, Value *Z,
5318 bool IsNegative) -> bool {
5319 const APInt *OffsetC;
5320 if (!match(Z, m_APInt(OffsetC)))
5321 return false;
5322
5323 // Fast path for Z == 1/-1.
5324 if (IsNegative ? OffsetC->isAllOnes() : OffsetC->isOne())
5325 return true;
5326
5327 APInt C = *OffsetC;
5328 if (IsNegative)
5329 C.negate();
5330 // Note: -INT_MIN is also negative.
5331 if (!C.isStrictlyPositive())
5332 return false;
5333
5334 return isMultipleOf(X, C, Q) && isMultipleOf(Y, C, Q);
5335 };
5336
5337 // TODO: The subtraction-related identities shown below also hold, but
5338 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5339 // wouldn't happen even if they were implemented.
5340 //
5341 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5342 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5343 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5344 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5345
5346 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5347 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5348 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5349 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5350 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5351 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5352 if (A && NoOp0WrapProblem &&
5353 ShareCommonDivisor(A, Op1, B,
5354 ICmpInst::isLT(Pred) || ICmpInst::isGE(Pred)))
5356 Op1);
5357
5358 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5359 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5360 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5361 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5362 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5363 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5364 if (C && NoOp1WrapProblem &&
5365 ShareCommonDivisor(Op0, C, D,
5366 ICmpInst::isGT(Pred) || ICmpInst::isLE(Pred)))
5368 C);
5369 }
5370
5371 // if C1 has greater magnitude than C2:
5372 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5373 // s.t. C3 = C1 - C2
5374 //
5375 // if C2 has greater magnitude than C1:
5376 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5377 // s.t. C3 = C2 - C1
5378 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5379 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5380 const APInt *AP1, *AP2;
5381 // TODO: Support non-uniform vectors.
5382 // TODO: Allow poison passthrough if B or D's element is poison.
5383 if (match(B, m_APIntAllowPoison(AP1)) &&
5384 match(D, m_APIntAllowPoison(AP2)) &&
5385 AP1->isNegative() == AP2->isNegative()) {
5386 APInt AP1Abs = AP1->abs();
5387 APInt AP2Abs = AP2->abs();
5388 if (AP1Abs.uge(AP2Abs)) {
5389 APInt Diff = *AP1 - *AP2;
5390 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5391 Value *NewAdd = Builder.CreateAdd(
5392 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
5393 return new ICmpInst(Pred, NewAdd, C);
5394 } else {
5395 APInt Diff = *AP2 - *AP1;
5396 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5397 Value *NewAdd = Builder.CreateAdd(
5398 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
5399 return new ICmpInst(Pred, A, NewAdd);
5400 }
5401 }
5402 Constant *Cst1, *Cst2;
5403 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5404 ICmpInst::isEquality(Pred)) {
5405 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5406 Value *NewAdd = Builder.CreateAdd(C, Diff);
5407 return new ICmpInst(Pred, A, NewAdd);
5408 }
5409 }
5410
5411 // Analyze the case when either Op0 or Op1 is a sub instruction.
5412 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5413 A = nullptr;
5414 B = nullptr;
5415 C = nullptr;
5416 D = nullptr;
5417 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5418 A = BO0->getOperand(0);
5419 B = BO0->getOperand(1);
5420 }
5421 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5422 C = BO1->getOperand(0);
5423 D = BO1->getOperand(1);
5424 }
5425
5426 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5427 if (A == Op1 && NoOp0WrapProblem)
5428 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5429 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5430 if (C == Op0 && NoOp1WrapProblem)
5431 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5432
5433 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5434 // (A - B) u>/u<= A --> B u>/u<= A
5435 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5436 return new ICmpInst(Pred, B, A);
5437 // C u</u>= (C - D) --> C u</u>= D
5438 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5439 return new ICmpInst(Pred, C, D);
5440 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5441 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5442 isKnownNonZero(B, Q))
5444 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5445 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5446 isKnownNonZero(D, Q))
5448
5449 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5450 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5451 return new ICmpInst(Pred, A, C);
5452
5453 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5454 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5455 return new ICmpInst(Pred, D, B);
5456
5457 // icmp (0-X) < cst --> x > -cst
5458 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5459 Value *X;
5460 if (match(BO0, m_Neg(m_Value(X))))
5461 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5462 if (RHSC->isNotMinSignedValue())
5463 return new ICmpInst(I.getSwappedPredicate(), X,
5464 ConstantExpr::getNeg(RHSC));
5465 }
5466
5467 if (Instruction *R = foldICmpXorXX(I, Q, *this))
5468 return R;
5469 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5470 return R;
5471
5472 {
5473 // Try to remove shared multiplier from comparison:
5474 // X * Z pred Y * Z
5475 Value *X, *Y, *Z;
5476 if ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5477 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5478 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5479 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y))))) {
5480 if (ICmpInst::isSigned(Pred)) {
5481 if (Op0HasNSW && Op1HasNSW) {
5482 KnownBits ZKnown = computeKnownBits(Z, &I);
5483 if (ZKnown.isStrictlyPositive())
5484 return new ICmpInst(Pred, X, Y);
5485 if (ZKnown.isNegative())
5486 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y);
5488 SQ.getWithInstruction(&I));
5489 if (LessThan && match(LessThan, m_One()))
5490 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z,
5491 Constant::getNullValue(Z->getType()));
5492 Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y,
5493 SQ.getWithInstruction(&I));
5494 if (GreaterThan && match(GreaterThan, m_One()))
5495 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5496 }
5497 } else {
5498 bool NonZero;
5499 if (ICmpInst::isEquality(Pred)) {
5500 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0
5501 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&
5502 isKnownNonEqual(X, Y, SQ))
5503 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5504
5505 KnownBits ZKnown = computeKnownBits(Z, &I);
5506 // if Z % 2 != 0
5507 // X * Z eq/ne Y * Z -> X eq/ne Y
5508 if (ZKnown.countMaxTrailingZeros() == 0)
5509 return new ICmpInst(Pred, X, Y);
5510 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5511 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5512 // X * Z eq/ne Y * Z -> X eq/ne Y
5513 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5514 return new ICmpInst(Pred, X, Y);
5515 } else
5516 NonZero = isKnownNonZero(Z, Q);
5517
5518 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5519 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5520 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5521 return new ICmpInst(Pred, X, Y);
5522 }
5523 }
5524 }
5525
5526 BinaryOperator *SRem = nullptr;
5527 // icmp (srem X, Y), Y
5528 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5529 SRem = BO0;
5530 // icmp Y, (srem X, Y)
5531 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5532 Op0 == BO1->getOperand(1))
5533 SRem = BO1;
5534 if (SRem) {
5535 // We don't check hasOneUse to avoid increasing register pressure because
5536 // the value we use is the same value this instruction was already using.
5537 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5538 default:
5539 break;
5540 case ICmpInst::ICMP_EQ:
5541 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5542 case ICmpInst::ICMP_NE:
5543 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5544 case ICmpInst::ICMP_SGT:
5545 case ICmpInst::ICMP_SGE:
5546 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5548 case ICmpInst::ICMP_SLT:
5549 case ICmpInst::ICMP_SLE:
5550 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5552 }
5553 }
5554
5555 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5556 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5557 BO0->getOperand(1) == BO1->getOperand(1)) {
5558 switch (BO0->getOpcode()) {
5559 default:
5560 break;
5561 case Instruction::Add:
5562 case Instruction::Sub:
5563 case Instruction::Xor: {
5564 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5565 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5566
5567 const APInt *C;
5568 if (match(BO0->getOperand(1), m_APInt(C))) {
5569 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5570 if (C->isSignMask()) {
5571 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5572 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5573 }
5574
5575 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5576 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5577 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5578 NewPred = I.getSwappedPredicate(NewPred);
5579 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5580 }
5581 }
5582 break;
5583 }
5584 case Instruction::Mul: {
5585 if (!I.isEquality())
5586 break;
5587
5588 const APInt *C;
5589 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5590 !C->isOne()) {
5591 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5592 // Mask = -1 >> count-trailing-zeros(C).
5593 if (unsigned TZs = C->countr_zero()) {
5594 Constant *Mask = ConstantInt::get(
5595 BO0->getType(),
5596 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5597 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5598 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5599 return new ICmpInst(Pred, And1, And2);
5600 }
5601 }
5602 break;
5603 }
5604 case Instruction::UDiv:
5605 case Instruction::LShr:
5606 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5607 break;
5608 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5609
5610 case Instruction::SDiv:
5611 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5612 !BO0->isExact() || !BO1->isExact())
5613 break;
5614 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5615
5616 case Instruction::AShr:
5617 if (!BO0->isExact() || !BO1->isExact())
5618 break;
5619 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5620
5621 case Instruction::Shl: {
5622 bool NUW = Op0HasNUW && Op1HasNUW;
5623 bool NSW = Op0HasNSW && Op1HasNSW;
5624 if (!NUW && !NSW)
5625 break;
5626 if (!NSW && I.isSigned())
5627 break;
5628 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5629 }
5630 }
5631 }
5632
5633 if (BO0) {
5634 // Transform A & (L - 1) `ult` L --> L != 0
5635 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5636 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5637
5638 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5639 auto *Zero = Constant::getNullValue(BO0->getType());
5640 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5641 }
5642 }
5643
5644 // For unsigned predicates / eq / ne:
5645 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5646 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5647 if (!ICmpInst::isSigned(Pred)) {
5648 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5649 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5650 Constant::getNullValue(Op1->getType()));
5651 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5652 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5653 Constant::getNullValue(Op0->getType()), Op0);
5654 }
5655
5657 return replaceInstUsesWith(I, V);
5658
5659 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5660 return R;
5661
5663 return replaceInstUsesWith(I, V);
5664
5666 return replaceInstUsesWith(I, V);
5667
5668 return nullptr;
5669}
5670
5671/// Fold icmp Pred min|max(X, Y), Z.
5674 Value *Z, CmpPredicate Pred) {
5675 Value *X = MinMax->getLHS();
5676 Value *Y = MinMax->getRHS();
5677 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5678 return nullptr;
5679 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5680 // Revert the transform signed pred -> unsigned pred
5681 // TODO: We can flip the signedness of predicate if both operands of icmp
5682 // are negative.
5683 if (isKnownNonNegative(Z, SQ.getWithInstruction(&I)) &&
5684 isKnownNonNegative(MinMax, SQ.getWithInstruction(&I))) {
5686 } else
5687 return nullptr;
5688 }
5689 SimplifyQuery Q = SQ.getWithInstruction(&I);
5690 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5691 if (!Val)
5692 return std::nullopt;
5693 if (match(Val, m_One()))
5694 return true;
5695 if (match(Val, m_Zero()))
5696 return false;
5697 return std::nullopt;
5698 };
5699 // Remove samesign here since it is illegal to keep it when we speculatively
5700 // execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
5701 // cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
5702 // -46, -32)`. `X` is allowed to be non-negative here.
5703 Pred = Pred.dropSameSign();
5704 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5705 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5706 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5707 return nullptr;
5708 if (!CmpXZ.has_value()) {
5709 std::swap(X, Y);
5710 std::swap(CmpXZ, CmpYZ);
5711 }
5712
5713 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5714 if (CmpYZ.has_value())
5715 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5716 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5717 };
5718
5719 switch (Pred) {
5720 case ICmpInst::ICMP_EQ:
5721 case ICmpInst::ICMP_NE: {
5722 // If X == Z:
5723 // Expr Result
5724 // min(X, Y) == Z X <= Y
5725 // max(X, Y) == Z X >= Y
5726 // min(X, Y) != Z X > Y
5727 // max(X, Y) != Z X < Y
5728 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5729 ICmpInst::Predicate NewPred =
5730 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5731 if (Pred == ICmpInst::ICMP_NE)
5732 NewPred = ICmpInst::getInversePredicate(NewPred);
5733 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5734 }
5735 // Otherwise (X != Z):
5736 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5737 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5738 if (!MinMaxCmpXZ.has_value()) {
5739 std::swap(X, Y);
5740 std::swap(CmpXZ, CmpYZ);
5741 // Re-check pre-condition X != Z
5742 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5743 break;
5744 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5745 }
5746 if (!MinMaxCmpXZ.has_value())
5747 break;
5748 if (*MinMaxCmpXZ) {
5749 // Expr Fact Result
5750 // min(X, Y) == Z X < Z false
5751 // max(X, Y) == Z X > Z false
5752 // min(X, Y) != Z X < Z true
5753 // max(X, Y) != Z X > Z true
5754 return replaceInstUsesWith(
5755 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5756 } else {
5757 // Expr Fact Result
5758 // min(X, Y) == Z X > Z Y == Z
5759 // max(X, Y) == Z X < Z Y == Z
5760 // min(X, Y) != Z X > Z Y != Z
5761 // max(X, Y) != Z X < Z Y != Z
5762 return FoldIntoCmpYZ();
5763 }
5764 break;
5765 }
5766 case ICmpInst::ICMP_SLT:
5767 case ICmpInst::ICMP_ULT:
5768 case ICmpInst::ICMP_SLE:
5769 case ICmpInst::ICMP_ULE:
5770 case ICmpInst::ICMP_SGT:
5771 case ICmpInst::ICMP_UGT:
5772 case ICmpInst::ICMP_SGE:
5773 case ICmpInst::ICMP_UGE: {
5774 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5775 if (*CmpXZ) {
5776 if (IsSame) {
5777 // Expr Fact Result
5778 // min(X, Y) < Z X < Z true
5779 // min(X, Y) <= Z X <= Z true
5780 // max(X, Y) > Z X > Z true
5781 // max(X, Y) >= Z X >= Z true
5782 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5783 } else {
5784 // Expr Fact Result
5785 // max(X, Y) < Z X < Z Y < Z
5786 // max(X, Y) <= Z X <= Z Y <= Z
5787 // min(X, Y) > Z X > Z Y > Z
5788 // min(X, Y) >= Z X >= Z Y >= Z
5789 return FoldIntoCmpYZ();
5790 }
5791 } else {
5792 if (IsSame) {
5793 // Expr Fact Result
5794 // min(X, Y) < Z X >= Z Y < Z
5795 // min(X, Y) <= Z X > Z Y <= Z
5796 // max(X, Y) > Z X <= Z Y > Z
5797 // max(X, Y) >= Z X < Z Y >= Z
5798 return FoldIntoCmpYZ();
5799 } else {
5800 // Expr Fact Result
5801 // max(X, Y) < Z X >= Z false
5802 // max(X, Y) <= Z X > Z false
5803 // min(X, Y) > Z X <= Z false
5804 // min(X, Y) >= Z X < Z false
5805 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5806 }
5807 }
5808 break;
5809 }
5810 default:
5811 break;
5812 }
5813
5814 return nullptr;
5815}
5816
5817/// Match and fold patterns like:
5818/// icmp eq/ne X, min(max(X, Lo), Hi)
5819/// which represents a range check and can be represented as a ConstantRange.
5820///
5821/// For icmp eq, build ConstantRange [Lo, Hi + 1) and convert to:
5822/// (X - Lo) u< (Hi + 1 - Lo)
5823/// For icmp ne, build ConstantRange [Hi + 1, Lo) and convert to:
5824/// (X - (Hi + 1)) u< (Lo - (Hi + 1))
5826 MinMaxIntrinsic *Min) {
5827 if (!I.isEquality() || !Min->hasOneUse() || !Min->isMin())
5828 return nullptr;
5829
5830 const APInt *Lo = nullptr, *Hi = nullptr;
5831 if (Min->isSigned()) {
5832 if (!match(Min->getLHS(), m_OneUse(m_SMax(m_Specific(X), m_APInt(Lo)))) ||
5833 !match(Min->getRHS(), m_APInt(Hi)) || !Lo->slt(*Hi))
5834 return nullptr;
5835 } else {
5836 if (!match(Min->getLHS(), m_OneUse(m_UMax(m_Specific(X), m_APInt(Lo)))) ||
5837 !match(Min->getRHS(), m_APInt(Hi)) || !Lo->ult(*Hi))
5838 return nullptr;
5839 }
5840
5843 APInt C, Offset;
5844 if (I.getPredicate() == ICmpInst::ICMP_EQ)
5845 CR.getEquivalentICmp(Pred, C, Offset);
5846 else
5847 CR.inverse().getEquivalentICmp(Pred, C, Offset);
5848
5849 if (!Offset.isZero())
5850 X = Builder.CreateAdd(X, ConstantInt::get(X->getType(), Offset));
5851
5852 return replaceInstUsesWith(
5853 I, Builder.CreateICmp(Pred, X, ConstantInt::get(X->getType(), C)));
5854}
5855
5856// Canonicalize checking for a power-of-2-or-zero value:
5858 InstCombiner::BuilderTy &Builder) {
5859 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5860 const CmpInst::Predicate Pred = I.getPredicate();
5861 Value *A = nullptr;
5862 bool CheckIs;
5863 if (I.isEquality()) {
5864 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5865 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5866 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5867 m_Deferred(A)))) ||
5868 !match(Op1, m_ZeroInt()))
5869 A = nullptr;
5870
5871 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5872 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5873 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5874 A = Op1;
5875 else if (match(Op1,
5877 A = Op0;
5878
5879 CheckIs = Pred == ICmpInst::ICMP_EQ;
5880 } else if (ICmpInst::isUnsigned(Pred)) {
5881 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5882 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5883
5884 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5886 m_Specific(Op1))))) {
5887 A = Op1;
5888 CheckIs = Pred == ICmpInst::ICMP_UGE;
5889 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5891 m_Specific(Op0))))) {
5892 A = Op0;
5893 CheckIs = Pred == ICmpInst::ICMP_ULE;
5894 }
5895 }
5896
5897 if (A) {
5898 Type *Ty = A->getType();
5899 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5900 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5901 ConstantInt::get(Ty, 2))
5902 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5903 ConstantInt::get(Ty, 1));
5904 }
5905
5906 return nullptr;
5907}
5908
5909/// Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
5910using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;
5912 bool AllowRecursion) {
5914 if (!Inst || !Inst->hasOneUse())
5915 return;
5916
5917 switch (Inst->getOpcode()) {
5918 case Instruction::Add:
5919 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(1));
5920 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(0));
5921 break;
5922 case Instruction::Sub:
5923 Offsets.emplace_back(Instruction::Add, Inst->getOperand(1));
5924 break;
5925 case Instruction::Xor:
5926 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(1));
5927 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(0));
5928 break;
5929 case Instruction::Shl:
5930 if (Inst->hasNoSignedWrap())
5931 Offsets.emplace_back(Instruction::AShr, Inst->getOperand(1));
5932 if (Inst->hasNoUnsignedWrap())
5933 Offsets.emplace_back(Instruction::LShr, Inst->getOperand(1));
5934 break;
5935 case Instruction::Select:
5936 if (AllowRecursion) {
5937 collectOffsetOp(Inst->getOperand(1), Offsets, /*AllowRecursion=*/false);
5938 collectOffsetOp(Inst->getOperand(2), Offsets, /*AllowRecursion=*/false);
5939 }
5940 break;
5941 default:
5942 break;
5943 }
5944}
5945
5947
5952
5954 return {OffsetKind::Invalid, nullptr, nullptr, nullptr, nullptr};
5955 }
5957 return {OffsetKind::Value, V, nullptr, nullptr, nullptr};
5958 }
5959 static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV,
5961 return {OffsetKind::Select, Cond, TrueV, FalseV, MDFrom};
5962 }
5963 bool isValid() const { return Kind != OffsetKind::Invalid; }
5965 switch (Kind) {
5967 llvm_unreachable("Invalid offset result");
5968 case OffsetKind::Value:
5969 return V0;
5970 case OffsetKind::Select:
5971 return Builder.CreateSelect(
5972 V0, V1, V2, "", ProfcheckDisableMetadataFixes ? nullptr : MDFrom);
5973 }
5974 llvm_unreachable("Unknown OffsetKind enum");
5975 }
5976};
5977
5978/// Offset both sides of an equality icmp to see if we can save some
5979/// instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.
5980/// Note: This operation should not introduce poison.
5982 InstCombiner::BuilderTy &Builder,
5983 const SimplifyQuery &SQ) {
5984 assert(I.isEquality() && "Expected an equality icmp");
5985 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5986 if (!Op0->getType()->isIntOrIntVectorTy())
5987 return nullptr;
5988
5989 SmallVector<OffsetOp, 4> OffsetOps;
5990 collectOffsetOp(Op0, OffsetOps, /*AllowRecursion=*/true);
5991 collectOffsetOp(Op1, OffsetOps, /*AllowRecursion=*/true);
5992
5993 auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {
5994 switch (BinOpc) {
5995 // V = shl nsw X, RHS => X = ashr V, RHS
5996 case Instruction::AShr: {
5997 const APInt *CV, *CRHS;
5998 if (!(match(V, m_APInt(CV)) && match(RHS, m_APInt(CRHS)) &&
5999 CV->ashr(*CRHS).shl(*CRHS) == *CV) &&
6001 return nullptr;
6002 break;
6003 }
6004 // V = shl nuw X, RHS => X = lshr V, RHS
6005 case Instruction::LShr: {
6006 const APInt *CV, *CRHS;
6007 if (!(match(V, m_APInt(CV)) && match(RHS, m_APInt(CRHS)) &&
6008 CV->lshr(*CRHS).shl(*CRHS) == *CV) &&
6010 return nullptr;
6011 break;
6012 }
6013 default:
6014 break;
6015 }
6016
6017 Value *Simplified = simplifyBinOp(BinOpc, V, RHS, SQ);
6018 if (!Simplified)
6019 return nullptr;
6020 // Reject constant expressions as they don't simplify things.
6021 if (isa<Constant>(Simplified) && !match(Simplified, m_ImmConstant()))
6022 return nullptr;
6023 // Check if the transformation introduces poison.
6024 return impliesPoison(RHS, V) ? Simplified : nullptr;
6025 };
6026
6027 auto ApplyOffset = [&](Value *V, unsigned BinOpc,
6028 Value *RHS) -> OffsetResult {
6029 if (auto *Sel = dyn_cast<SelectInst>(V)) {
6030 if (!Sel->hasOneUse())
6031 return OffsetResult::invalid();
6032 Value *TrueVal = ApplyOffsetImpl(Sel->getTrueValue(), BinOpc, RHS);
6033 if (!TrueVal)
6034 return OffsetResult::invalid();
6035 Value *FalseVal = ApplyOffsetImpl(Sel->getFalseValue(), BinOpc, RHS);
6036 if (!FalseVal)
6037 return OffsetResult::invalid();
6038 return OffsetResult::select(Sel->getCondition(), TrueVal, FalseVal, Sel);
6039 }
6040 if (Value *Simplified = ApplyOffsetImpl(V, BinOpc, RHS))
6041 return OffsetResult::value(Simplified);
6042 return OffsetResult::invalid();
6043 };
6044
6045 for (auto [BinOp, RHS] : OffsetOps) {
6046 auto BinOpc = static_cast<unsigned>(BinOp);
6047
6048 auto Op0Result = ApplyOffset(Op0, BinOpc, RHS);
6049 if (!Op0Result.isValid())
6050 continue;
6051 auto Op1Result = ApplyOffset(Op1, BinOpc, RHS);
6052 if (!Op1Result.isValid())
6053 continue;
6054
6055 Value *NewLHS = Op0Result.materialize(Builder);
6056 Value *NewRHS = Op1Result.materialize(Builder);
6057 return new ICmpInst(I.getPredicate(), NewLHS, NewRHS);
6058 }
6059
6060 return nullptr;
6061}
6062
6064 if (!I.isEquality())
6065 return nullptr;
6066
6067 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6068 const CmpInst::Predicate Pred = I.getPredicate();
6069 Value *A, *B, *C, *D;
6070 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
6071 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
6072 Value *OtherVal = A == Op1 ? B : A;
6073 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
6074 }
6075
6076 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
6077 // A^c1 == C^c2 --> A == C^(c1^c2)
6078 ConstantInt *C1, *C2;
6079 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
6080 Op1->hasOneUse()) {
6081 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
6082 Value *Xor = Builder.CreateXor(C, NC);
6083 return new ICmpInst(Pred, A, Xor);
6084 }
6085
6086 // A^B == A^D -> B == D
6087 if (A == C)
6088 return new ICmpInst(Pred, B, D);
6089 if (A == D)
6090 return new ICmpInst(Pred, B, C);
6091 if (B == C)
6092 return new ICmpInst(Pred, A, D);
6093 if (B == D)
6094 return new ICmpInst(Pred, A, C);
6095 }
6096 }
6097
6098 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
6099 // A == (A^B) -> B == 0
6100 Value *OtherVal = A == Op0 ? B : A;
6101 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
6102 }
6103
6104 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
6105 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
6106 match(Op1, m_And(m_Value(C), m_Value(D)))) {
6107 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
6108
6109 if (A == C) {
6110 X = B;
6111 Y = D;
6112 Z = A;
6113 } else if (A == D) {
6114 X = B;
6115 Y = C;
6116 Z = A;
6117 } else if (B == C) {
6118 X = A;
6119 Y = D;
6120 Z = B;
6121 } else if (B == D) {
6122 X = A;
6123 Y = C;
6124 Z = B;
6125 }
6126
6127 if (X) {
6128 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
6129 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
6130 // instructions.
6131 const APInt *C0, *C1;
6132 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
6133 (*C0 ^ *C1).isNegatedPowerOf2();
6134
6135 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
6136 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
6137 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
6138 int UseCnt =
6139 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
6140 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
6141 if (XorIsNegP2 || UseCnt >= 2) {
6142 // Build (X^Y) & Z
6143 Op1 = Builder.CreateXor(X, Y);
6144 Op1 = Builder.CreateAnd(Op1, Z);
6145 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
6146 }
6147 }
6148 }
6149
6150 {
6151 // Similar to above, but specialized for constant because invert is needed:
6152 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
6153 Value *X, *Y;
6154 Constant *C;
6155 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
6156 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
6157 Value *Xor = Builder.CreateXor(X, Y);
6158 Value *And = Builder.CreateAnd(Xor, ConstantExpr::getNot(C));
6159 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
6160 }
6161 }
6162
6163 if (match(Op1, m_ZExt(m_Value(A))) &&
6164 (Op0->hasOneUse() || Op1->hasOneUse())) {
6165 // (B & (Pow2C-1)) == zext A --> A == trunc B
6166 // (B & (Pow2C-1)) != zext A --> A != trunc B
6167 const APInt *MaskC;
6168 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
6169 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
6170 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
6171 }
6172
6173 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
6174 // For lshr and ashr pairs.
6175 const APInt *AP1, *AP2;
6176 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
6177 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
6178 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
6179 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
6180 if (*AP1 != *AP2)
6181 return nullptr;
6182 unsigned TypeBits = AP1->getBitWidth();
6183 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
6184 if (ShAmt < TypeBits && ShAmt != 0) {
6185 ICmpInst::Predicate NewPred =
6187 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
6188 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
6189 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
6190 }
6191 }
6192
6193 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
6194 ConstantInt *Cst1;
6195 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
6196 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
6197 unsigned TypeBits = Cst1->getBitWidth();
6198 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
6199 if (ShAmt < TypeBits && ShAmt != 0) {
6200 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
6201 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
6202 Value *And =
6203 Builder.CreateAnd(Xor, Builder.getInt(AndVal), I.getName() + ".mask");
6204 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
6205 }
6206 }
6207
6208 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
6209 // "icmp (and X, mask), cst"
6210 uint64_t ShAmt = 0;
6211 if (Op0->hasOneUse() &&
6212 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
6213 match(Op1, m_ConstantInt(Cst1)) &&
6214 // Only do this when A has multiple uses. This is most important to do
6215 // when it exposes other optimizations.
6216 !A->hasOneUse()) {
6217 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
6218
6219 if (ShAmt < ASize) {
6220 APInt MaskV =
6222 MaskV <<= ShAmt;
6223
6224 APInt CmpV = Cst1->getValue().zext(ASize);
6225 CmpV <<= ShAmt;
6226
6227 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
6228 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
6229 }
6230 }
6231
6233 return ICmp;
6234
6235 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks
6236 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s
6237 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a
6238 // few steps of instcombine.
6239 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6240 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
6242 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
6243 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
6245 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
6246 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
6248 Add, ConstantInt::get(A->getType(), C.shl(1)));
6249 }
6250
6251 // Canonicalize:
6252 // Assume B_Pow2 != 0
6253 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
6254 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
6255 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
6256 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, &I))
6257 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6259
6260 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
6261 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, &I))
6262 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
6263 ConstantInt::getNullValue(Op1->getType()));
6264
6265 // Canonicalize:
6266 // icmp eq/ne X, OneUse(rotate-right(X))
6267 // -> icmp eq/ne X, rotate-left(X)
6268 // We generally try to convert rotate-right -> rotate-left, this just
6269 // canonicalizes another case.
6270 if (match(&I, m_c_ICmp(m_Value(A),
6272 m_Deferred(A), m_Deferred(A), m_Value(B))))))
6273 return new ICmpInst(
6274 Pred, A,
6275 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
6276
6277 // Canonicalize:
6278 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
6279 Constant *Cst;
6282 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
6283
6284 {
6285 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6286 auto m_Matcher =
6289 m_Sub(m_Value(B), m_Deferred(A)));
6290 std::optional<bool> IsZero = std::nullopt;
6291 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),
6292 m_Deferred(A))))
6293 IsZero = false;
6294 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6295 else if (match(&I,
6296 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))
6297 IsZero = true;
6298
6299 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, &I))
6300 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6301 // -> (icmp eq/ne (and X, P2), 0)
6302 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6303 // -> (icmp eq/ne (and X, P2), P2)
6304 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
6305 *IsZero ? A
6306 : ConstantInt::getNullValue(A->getType()));
6307 }
6308
6309 if (auto *Res = foldICmpEqualityWithOffset(
6310 I, Builder, getSimplifyQuery().getWithInstruction(&I)))
6311 return Res;
6312
6313 return nullptr;
6314}
6315
6317 ICmpInst::Predicate Pred = ICmp.getPredicate();
6318 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
6319
6320 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
6321 // The trunc masks high bits while the compare may effectively mask low bits.
6322 Value *X;
6323 const APInt *C;
6324 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
6325 return nullptr;
6326
6327 // This matches patterns corresponding to tests of the signbit as well as:
6328 // (trunc X) pred C2 --> (X & Mask) == C
6329 if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*LookThroughTrunc=*/true,
6330 /*AllowNonZeroC=*/true)) {
6331 Value *And = Builder.CreateAnd(Res->X, Res->Mask);
6332 Constant *C = ConstantInt::get(Res->X->getType(), Res->C);
6333 return new ICmpInst(Res->Pred, And, C);
6334 }
6335
6336 unsigned SrcBits = X->getType()->getScalarSizeInBits();
6337 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
6338 if (II->getIntrinsicID() == Intrinsic::cttz ||
6339 II->getIntrinsicID() == Intrinsic::ctlz) {
6340 unsigned MaxRet = SrcBits;
6341 // If the "is_zero_poison" argument is set, then we know at least
6342 // one bit is set in the input, so the result is always at least one
6343 // less than the full bitwidth of that input.
6344 if (match(II->getArgOperand(1), m_One()))
6345 MaxRet--;
6346
6347 // Make sure the destination is wide enough to hold the largest output of
6348 // the intrinsic.
6349 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
6350 if (Instruction *I =
6351 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
6352 return I;
6353 }
6354 }
6355
6356 return nullptr;
6357}
6358
6360 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
6361 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
6362 Value *X;
6363 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
6364 return nullptr;
6365
6366 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
6367 bool IsSignedCmp = ICmp.isSigned();
6368
6369 // icmp Pred (ext X), (ext Y)
6370 Value *Y;
6371 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
6372 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
6373 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
6374
6375 if (IsZext0 != IsZext1) {
6376 // If X and Y and both i1
6377 // (icmp eq/ne (zext X) (sext Y))
6378 // eq -> (icmp eq (or X, Y), 0)
6379 // ne -> (icmp ne (or X, Y), 0)
6380 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
6381 Y->getType()->isIntOrIntVectorTy(1))
6382 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
6383 Constant::getNullValue(X->getType()));
6384
6385 // If we have mismatched casts and zext has the nneg flag, we can
6386 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
6387
6388 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
6389 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
6390
6391 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
6392 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
6393
6394 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
6395 IsSignedExt = true;
6396 else
6397 return nullptr;
6398 }
6399
6400 // Not an extension from the same type?
6401 Type *XTy = X->getType(), *YTy = Y->getType();
6402 if (XTy != YTy) {
6403 // One of the casts must have one use because we are creating a new cast.
6404 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
6405 return nullptr;
6406 // Extend the narrower operand to the type of the wider operand.
6407 CastInst::CastOps CastOpcode =
6408 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
6409 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
6410 X = Builder.CreateCast(CastOpcode, X, YTy);
6411 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6412 Y = Builder.CreateCast(CastOpcode, Y, XTy);
6413 else
6414 return nullptr;
6415 }
6416
6417 // (zext X) == (zext Y) --> X == Y
6418 // (sext X) == (sext Y) --> X == Y
6419 if (ICmp.isEquality())
6420 return new ICmpInst(ICmp.getPredicate(), X, Y);
6421
6422 // A signed comparison of sign extended values simplifies into a
6423 // signed comparison.
6424 if (IsSignedCmp && IsSignedExt)
6425 return new ICmpInst(ICmp.getPredicate(), X, Y);
6426
6427 // The other three cases all fold into an unsigned comparison.
6428 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6429 }
6430
6431 // Below here, we are only folding a compare with constant.
6432 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
6433 if (!C)
6434 return nullptr;
6435
6436 // If a lossless truncate is possible...
6437 Type *SrcTy = CastOp0->getSrcTy();
6438 Constant *Res = getLosslessInvCast(C, SrcTy, CastOp0->getOpcode(), DL);
6439 if (Res) {
6440 if (ICmp.isEquality())
6441 return new ICmpInst(ICmp.getPredicate(), X, Res);
6442
6443 // A signed comparison of sign extended values simplifies into a
6444 // signed comparison.
6445 if (IsSignedExt && IsSignedCmp)
6446 return new ICmpInst(ICmp.getPredicate(), X, Res);
6447
6448 // The other three cases all fold into an unsigned comparison.
6449 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6450 }
6451
6452 // The re-extended constant changed, partly changed (in the case of a vector),
6453 // or could not be determined to be equal (in the case of a constant
6454 // expression), so the constant cannot be represented in the shorter type.
6455 // All the cases that fold to true or false will have already been handled
6456 // by simplifyICmpInst, so only deal with the tricky case.
6457 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
6458 return nullptr;
6459
6460 // Is source op positive?
6461 // icmp ult (sext X), C --> icmp sgt X, -1
6462 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6464
6465 // Is source op negative?
6466 // icmp ugt (sext X), C --> icmp slt X, 0
6467 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6469}
6470
6471/// Handle icmp (cast x), (cast or constant).
6473 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6474 // icmp compares only pointer's value.
6475 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6476 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
6477 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
6478 if (SimplifiedOp0 || SimplifiedOp1)
6479 return new ICmpInst(ICmp.getPredicate(),
6480 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
6481 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
6482
6483 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
6484 if (!CastOp0)
6485 return nullptr;
6486 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
6487 return nullptr;
6488
6489 Value *Op0Src = CastOp0->getOperand(0);
6490 Type *SrcTy = CastOp0->getSrcTy();
6491 Type *DestTy = CastOp0->getDestTy();
6492
6493 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6494 // integer type is the same size as the pointer type.
6495 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6496 if (isa<VectorType>(PtrTy)) {
6497 PtrTy = cast<VectorType>(PtrTy)->getElementType();
6498 IntTy = cast<VectorType>(IntTy)->getElementType();
6499 }
6500 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
6501 };
6502 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6503 CompatibleSizes(SrcTy, DestTy)) {
6504 Value *NewOp1 = nullptr;
6505 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
6506 Value *PtrSrc = PtrToIntOp1->getOperand(0);
6507 if (PtrSrc->getType() == Op0Src->getType())
6508 NewOp1 = PtrToIntOp1->getOperand(0);
6509 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6510 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
6511 }
6512
6513 if (NewOp1)
6514 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6515 }
6516
6517 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6518 if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6519 CompatibleSizes(DestTy, SrcTy)) {
6520 Value *NewOp1 = nullptr;
6521 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
6522 Value *IntSrc = IntToPtrOp1->getOperand(0);
6523 if (IntSrc->getType() == Op0Src->getType())
6524 NewOp1 = IntToPtrOp1->getOperand(0);
6525 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6526 NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
6527 }
6528
6529 if (NewOp1)
6530 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6531 }
6532
6533 if (Instruction *R = foldICmpWithTrunc(ICmp))
6534 return R;
6535
6536 return foldICmpWithZextOrSext(ICmp);
6537}
6538
6540 bool IsSigned) {
6541 switch (BinaryOp) {
6542 default:
6543 llvm_unreachable("Unsupported binary op");
6544 case Instruction::Add:
6545 case Instruction::Sub:
6546 return match(RHS, m_Zero());
6547 case Instruction::Mul:
6548 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
6549 match(RHS, m_One());
6550 }
6551}
6552
6555 bool IsSigned, Value *LHS, Value *RHS,
6556 Instruction *CxtI) const {
6557 switch (BinaryOp) {
6558 default:
6559 llvm_unreachable("Unsupported binary op");
6560 case Instruction::Add:
6561 if (IsSigned)
6562 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6563 else
6564 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6565 case Instruction::Sub:
6566 if (IsSigned)
6567 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6568 else
6569 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6570 case Instruction::Mul:
6571 if (IsSigned)
6572 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6573 else
6574 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6575 }
6576}
6577
6578bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6579 bool IsSigned, Value *LHS,
6580 Value *RHS, Instruction &OrigI,
6581 Value *&Result,
6582 Constant *&Overflow) {
6583 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
6584 std::swap(LHS, RHS);
6585
6586 // If the overflow check was an add followed by a compare, the insertion point
6587 // may be pointing to the compare. We want to insert the new instructions
6588 // before the add in case there are uses of the add between the add and the
6589 // compare.
6590 Builder.SetInsertPoint(&OrigI);
6591
6592 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
6593 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
6594 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
6595
6596 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6597 Result = LHS;
6598 Overflow = ConstantInt::getFalse(OverflowTy);
6599 return true;
6600 }
6601
6602 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
6604 return false;
6607 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6608 Result->takeName(&OrigI);
6609 Overflow = ConstantInt::getTrue(OverflowTy);
6610 return true;
6612 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6613 Result->takeName(&OrigI);
6614 Overflow = ConstantInt::getFalse(OverflowTy);
6615 if (auto *Inst = dyn_cast<Instruction>(Result)) {
6616 if (IsSigned)
6617 Inst->setHasNoSignedWrap();
6618 else
6619 Inst->setHasNoUnsignedWrap();
6620 }
6621 return true;
6622 }
6623
6624 llvm_unreachable("Unexpected overflow result");
6625}
6626
6627/// Recognize and process idiom involving test for multiplication
6628/// overflow.
6629///
6630/// The caller has matched a pattern of the form:
6631/// I = cmp u (mul(zext A, zext B), V
6632/// The function checks if this is a test for overflow and if so replaces
6633/// multiplication with call to 'mul.with.overflow' intrinsic.
6634///
6635/// \param I Compare instruction.
6636/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6637/// the compare instruction. Must be of integer type.
6638/// \param OtherVal The other argument of compare instruction.
6639/// \returns Instruction which must replace the compare instruction, NULL if no
6640/// replacement required.
6642 const APInt *OtherVal,
6643 InstCombinerImpl &IC) {
6644 // Don't bother doing this transformation for pointers, don't do it for
6645 // vectors.
6646 if (!isa<IntegerType>(MulVal->getType()))
6647 return nullptr;
6648
6649 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6650 if (!MulInstr)
6651 return nullptr;
6652 assert(MulInstr->getOpcode() == Instruction::Mul);
6653
6654 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6655 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6656 assert(LHS->getOpcode() == Instruction::ZExt);
6657 assert(RHS->getOpcode() == Instruction::ZExt);
6658 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6659
6660 // Calculate type and width of the result produced by mul.with.overflow.
6661 Type *TyA = A->getType(), *TyB = B->getType();
6662 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6663 WidthB = TyB->getPrimitiveSizeInBits();
6664 unsigned MulWidth;
6665 Type *MulType;
6666 if (WidthB > WidthA) {
6667 MulWidth = WidthB;
6668 MulType = TyB;
6669 } else {
6670 MulWidth = WidthA;
6671 MulType = TyA;
6672 }
6673
6674 // In order to replace the original mul with a narrower mul.with.overflow,
6675 // all uses must ignore upper bits of the product. The number of used low
6676 // bits must be not greater than the width of mul.with.overflow.
6677 if (MulVal->hasNUsesOrMore(2))
6678 for (User *U : MulVal->users()) {
6679 if (U == &I)
6680 continue;
6681 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6682 // Check if truncation ignores bits above MulWidth.
6683 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6684 if (TruncWidth > MulWidth)
6685 return nullptr;
6686 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6687 // Check if AND ignores bits above MulWidth.
6688 if (BO->getOpcode() != Instruction::And)
6689 return nullptr;
6690 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6691 const APInt &CVal = CI->getValue();
6692 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6693 return nullptr;
6694 } else {
6695 // In this case we could have the operand of the binary operation
6696 // being defined in another block, and performing the replacement
6697 // could break the dominance relation.
6698 return nullptr;
6699 }
6700 } else {
6701 // Other uses prohibit this transformation.
6702 return nullptr;
6703 }
6704 }
6705
6706 // Recognize patterns
6707 switch (I.getPredicate()) {
6708 case ICmpInst::ICMP_UGT: {
6709 // Recognize pattern:
6710 // mulval = mul(zext A, zext B)
6711 // cmp ugt mulval, max
6712 APInt MaxVal = APInt::getMaxValue(MulWidth);
6713 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6714 if (MaxVal.eq(*OtherVal))
6715 break; // Recognized
6716 return nullptr;
6717 }
6718
6719 case ICmpInst::ICMP_ULT: {
6720 // Recognize pattern:
6721 // mulval = mul(zext A, zext B)
6722 // cmp ule mulval, max + 1
6723 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6724 if (MaxVal.eq(*OtherVal))
6725 break; // Recognized
6726 return nullptr;
6727 }
6728
6729 default:
6730 return nullptr;
6731 }
6732
6733 InstCombiner::BuilderTy &Builder = IC.Builder;
6734 Builder.SetInsertPoint(MulInstr);
6735
6736 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6737 Value *MulA = A, *MulB = B;
6738 if (WidthA < MulWidth)
6739 MulA = Builder.CreateZExt(A, MulType);
6740 if (WidthB < MulWidth)
6741 MulB = Builder.CreateZExt(B, MulType);
6742 CallInst *Call =
6743 Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, MulType,
6744 {MulA, MulB}, /*FMFSource=*/nullptr, "umul");
6745 IC.addToWorklist(MulInstr);
6746
6747 // If there are uses of mul result other than the comparison, we know that
6748 // they are truncation or binary AND. Change them to use result of
6749 // mul.with.overflow and adjust properly mask/size.
6750 if (MulVal->hasNUsesOrMore(2)) {
6751 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6752 for (User *U : make_early_inc_range(MulVal->users())) {
6753 if (U == &I)
6754 continue;
6755 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6756 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6757 IC.replaceInstUsesWith(*TI, Mul);
6758 else
6759 TI->setOperand(0, Mul);
6760 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6761 assert(BO->getOpcode() == Instruction::And);
6762 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6763 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6764 APInt ShortMask = CI->getValue().trunc(MulWidth);
6765 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6766 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6767 IC.replaceInstUsesWith(*BO, Zext);
6768 } else {
6769 llvm_unreachable("Unexpected Binary operation");
6770 }
6772 }
6773 }
6774
6775 // The original icmp gets replaced with the overflow value, maybe inverted
6776 // depending on predicate.
6777 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6778 Value *Res = Builder.CreateExtractValue(Call, 1);
6779 return BinaryOperator::CreateNot(Res);
6780 }
6781
6782 return ExtractValueInst::Create(Call, 1);
6783}
6784
6785/// When performing a comparison against a constant, it is possible that not all
6786/// the bits in the LHS are demanded. This helper method computes the mask that
6787/// IS demanded.
6789 const APInt *RHS;
6790 if (!match(I.getOperand(1), m_APInt(RHS)))
6792
6793 // If this is a normal comparison, it demands all bits. If it is a sign bit
6794 // comparison, it only demands the sign bit.
6795 bool UnusedBit;
6796 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6798
6799 switch (I.getPredicate()) {
6800 // For a UGT comparison, we don't care about any bits that
6801 // correspond to the trailing ones of the comparand. The value of these
6802 // bits doesn't impact the outcome of the comparison, because any value
6803 // greater than the RHS must differ in a bit higher than these due to carry.
6804 case ICmpInst::ICMP_UGT:
6805 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6806
6807 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6808 // Any value less than the RHS must differ in a higher bit because of carries.
6809 case ICmpInst::ICMP_ULT:
6810 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6811
6812 default:
6814 }
6815}
6816
6817/// Check that one use is in the same block as the definition and all
6818/// other uses are in blocks dominated by a given block.
6819///
6820/// \param DI Definition
6821/// \param UI Use
6822/// \param DB Block that must dominate all uses of \p DI outside
6823/// the parent block
6824/// \return true when \p UI is the only use of \p DI in the parent block
6825/// and all other uses of \p DI are in blocks dominated by \p DB.
6826///
6828 const Instruction *UI,
6829 const BasicBlock *DB) const {
6830 assert(DI && UI && "Instruction not defined\n");
6831 // Ignore incomplete definitions.
6832 if (!DI->getParent())
6833 return false;
6834 // DI and UI must be in the same block.
6835 if (DI->getParent() != UI->getParent())
6836 return false;
6837 // Protect from self-referencing blocks.
6838 if (DI->getParent() == DB)
6839 return false;
6840 for (const User *U : DI->users()) {
6841 auto *Usr = cast<Instruction>(U);
6842 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6843 return false;
6844 }
6845 return true;
6846}
6847
6848/// Return true when the instruction sequence within a block is select-cmp-br.
6850 const BasicBlock *BB = SI->getParent();
6851 if (!BB)
6852 return false;
6854 if (!BI)
6855 return false;
6856 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6857 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6858 return false;
6859 return true;
6860}
6861
6862/// True when a select result is replaced by one of its operands
6863/// in select-icmp sequence. This will eventually result in the elimination
6864/// of the select.
6865///
6866/// \param SI Select instruction
6867/// \param Icmp Compare instruction
6868/// \param SIOpd Operand that replaces the select
6869///
6870/// Notes:
6871/// - The replacement is global and requires dominator information
6872/// - The caller is responsible for the actual replacement
6873///
6874/// Example:
6875///
6876/// entry:
6877/// %4 = select i1 %3, %C* %0, %C* null
6878/// %5 = icmp eq %C* %4, null
6879/// br i1 %5, label %9, label %7
6880/// ...
6881/// ; <label>:7 ; preds = %entry
6882/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6883/// ...
6884///
6885/// can be transformed to
6886///
6887/// %5 = icmp eq %C* %0, null
6888/// %6 = select i1 %3, i1 %5, i1 true
6889/// br i1 %6, label %9, label %7
6890/// ...
6891/// ; <label>:7 ; preds = %entry
6892/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6893///
6894/// Similar when the first operand of the select is a constant or/and
6895/// the compare is for not equal rather than equal.
6896///
6897/// NOTE: The function is only called when the select and compare constants
6898/// are equal, the optimization can work only for EQ predicates. This is not a
6899/// major restriction since a NE compare should be 'normalized' to an equal
6900/// compare, which usually happens in the combiner and test case
6901/// select-cmp-br.ll checks for it.
6903 const ICmpInst *Icmp,
6904 const unsigned SIOpd) {
6905 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6907 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6908 // The check for the single predecessor is not the best that can be
6909 // done. But it protects efficiently against cases like when SI's
6910 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6911 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6912 // replaced can be reached on either path. So the uniqueness check
6913 // guarantees that the path all uses of SI (outside SI's parent) are on
6914 // is disjoint from all other paths out of SI. But that information
6915 // is more expensive to compute, and the trade-off here is in favor
6916 // of compile-time. It should also be noticed that we check for a single
6917 // predecessor and not only uniqueness. This to handle the situation when
6918 // Succ and Succ1 points to the same basic block.
6919 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6920 NumSel++;
6921 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6922 return true;
6923 }
6924 }
6925 return false;
6926}
6927
6928/// Try to fold the comparison based on range information we can get by checking
6929/// whether bits are known to be zero or one in the inputs.
6931 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6932 Type *Ty = Op0->getType();
6933 ICmpInst::Predicate Pred = I.getPredicate();
6934
6935 // Get scalar or pointer size.
6936 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6937 ? Ty->getScalarSizeInBits()
6938 : DL.getPointerTypeSizeInBits(Ty->getScalarType());
6939
6940 if (!BitWidth)
6941 return nullptr;
6942
6943 KnownBits Op0Known(BitWidth);
6944 KnownBits Op1Known(BitWidth);
6945
6946 {
6947 // Don't use dominating conditions when folding icmp using known bits. This
6948 // may convert signed into unsigned predicates in ways that other passes
6949 // (especially IndVarSimplify) may not be able to reliably undo.
6950 SimplifyQuery Q = SQ.getWithoutDomCondCache().getWithInstruction(&I);
6952 Op0Known, Q))
6953 return &I;
6954
6955 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnes(BitWidth), Op1Known, Q))
6956 return &I;
6957 }
6958
6959 if (!isa<Constant>(Op0) && Op0Known.isConstant())
6960 return new ICmpInst(
6961 Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1);
6962 if (!isa<Constant>(Op1) && Op1Known.isConstant())
6963 return new ICmpInst(
6964 Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Known.getConstant()));
6965
6966 if (std::optional<bool> Res = ICmpInst::compare(Op0Known, Op1Known, Pred))
6967 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *Res));
6968
6969 // Given the known and unknown bits, compute a range that the LHS could be
6970 // in. Compute the Min, Max and RHS values based on the known bits. For the
6971 // EQ and NE we use unsigned values.
6972 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6973 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6974 if (I.isSigned()) {
6975 Op0Min = Op0Known.getSignedMinValue();
6976 Op0Max = Op0Known.getSignedMaxValue();
6977 Op1Min = Op1Known.getSignedMinValue();
6978 Op1Max = Op1Known.getSignedMaxValue();
6979 } else {
6980 Op0Min = Op0Known.getMinValue();
6981 Op0Max = Op0Known.getMaxValue();
6982 Op1Min = Op1Known.getMinValue();
6983 Op1Max = Op1Known.getMaxValue();
6984 }
6985
6986 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6987 // min/max canonical compare with some other compare. That could lead to
6988 // conflict with select canonicalization and infinite looping.
6989 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6990 auto isMinMaxCmp = [&](Instruction &Cmp) {
6991 if (!Cmp.hasOneUse())
6992 return false;
6993 Value *A, *B;
6994 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6996 return false;
6997 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6998 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6999 };
7000 if (!isMinMaxCmp(I)) {
7001 switch (Pred) {
7002 default:
7003 break;
7004 case ICmpInst::ICMP_ULT: {
7005 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
7006 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7007 const APInt *CmpC;
7008 if (match(Op1, m_APInt(CmpC))) {
7009 // A <u C -> A == C-1 if min(A)+1 == C
7010 if (*CmpC == Op0Min + 1)
7011 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7012 ConstantInt::get(Op1->getType(), *CmpC - 1));
7013 // X <u C --> X == 0, if the number of zero bits in the bottom of X
7014 // exceeds the log2 of C.
7015 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
7016 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7017 Constant::getNullValue(Op1->getType()));
7018 }
7019 break;
7020 }
7021 case ICmpInst::ICMP_UGT: {
7022 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
7023 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7024 const APInt *CmpC;
7025 if (match(Op1, m_APInt(CmpC))) {
7026 // A >u C -> A == C+1 if max(a)-1 == C
7027 if (*CmpC == Op0Max - 1)
7028 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7029 ConstantInt::get(Op1->getType(), *CmpC + 1));
7030 // X >u C --> X != 0, if the number of zero bits in the bottom of X
7031 // exceeds the log2 of C.
7032 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
7033 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
7034 Constant::getNullValue(Op1->getType()));
7035 }
7036 break;
7037 }
7038 case ICmpInst::ICMP_SLT: {
7039 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
7040 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7041 const APInt *CmpC;
7042 if (match(Op1, m_APInt(CmpC))) {
7043 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
7044 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7045 ConstantInt::get(Op1->getType(), *CmpC - 1));
7046 }
7047 break;
7048 }
7049 case ICmpInst::ICMP_SGT: {
7050 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
7051 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
7052 const APInt *CmpC;
7053 if (match(Op1, m_APInt(CmpC))) {
7054 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
7055 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
7056 ConstantInt::get(Op1->getType(), *CmpC + 1));
7057 }
7058 break;
7059 }
7060 }
7061 }
7062
7063 // Based on the range information we know about the LHS, see if we can
7064 // simplify this comparison. For example, (x&4) < 8 is always true.
7065 switch (Pred) {
7066 default:
7067 break;
7068 case ICmpInst::ICMP_EQ:
7069 case ICmpInst::ICMP_NE: {
7070 // If all bits are known zero except for one, then we know at most one bit
7071 // is set. If the comparison is against zero, then this is a check to see if
7072 // *that* bit is set.
7073 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
7074 if (Op1Known.isZero()) {
7075 // If the LHS is an AND with the same constant, look through it.
7076 Value *LHS = nullptr;
7077 const APInt *LHSC;
7078 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
7079 *LHSC != Op0KnownZeroInverted)
7080 LHS = Op0;
7081
7082 Value *X;
7083 const APInt *C1;
7084 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
7085 Type *XTy = X->getType();
7086 unsigned Log2C1 = C1->countr_zero();
7087 APInt C2 = Op0KnownZeroInverted;
7088 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
7089 if (C2Pow2.isPowerOf2()) {
7090 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
7091 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
7092 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
7093 unsigned Log2C2 = C2Pow2.countr_zero();
7094 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
7095 auto NewPred =
7097 return new ICmpInst(NewPred, X, CmpC);
7098 }
7099 }
7100 }
7101
7102 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
7103 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
7104 (Op0Known & Op1Known) == Op0Known)
7105 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
7106 ConstantInt::getNullValue(Op1->getType()));
7107 break;
7108 }
7109 case ICmpInst::ICMP_SGE:
7110 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
7111 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7112 break;
7113 case ICmpInst::ICMP_SLE:
7114 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
7115 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7116 break;
7117 case ICmpInst::ICMP_UGE:
7118 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
7119 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7120 break;
7121 case ICmpInst::ICMP_ULE:
7122 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
7123 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7124 break;
7125 }
7126
7127 // Turn a signed comparison into an unsigned one if both operands are known to
7128 // have the same sign. Set samesign if possible (except for equality
7129 // predicates).
7130 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&
7131 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
7132 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {
7133 I.setPredicate(I.getUnsignedPredicate());
7134 I.setSameSign();
7135 return &I;
7136 }
7137
7138 return nullptr;
7139}
7140
7141/// If one operand of an icmp is effectively a bool (value range of {0,1}),
7142/// then try to reduce patterns based on that limit.
7144 Value *X, *Y;
7145 CmpPredicate Pred;
7146
7147 // X must be 0 and bool must be true for "ULT":
7148 // X <u (zext i1 Y) --> (X == 0) & Y
7149 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
7150 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
7151 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
7152
7153 // X must be 0 or bool must be true for "ULE":
7154 // X <=u (sext i1 Y) --> (X == 0) | Y
7155 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
7156 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
7157 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
7158
7159 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
7160 CmpPredicate Pred1, Pred2;
7161 const APInt *C;
7162 Instruction *ExtI;
7163 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
7166 m_APInt(C)))))) &&
7167 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
7168 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
7169 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
7170 auto CreateRangeCheck = [&] {
7171 Value *CmpV1 =
7172 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
7173 Value *CmpV2 = Builder.CreateICmp(
7174 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
7176 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
7177 CmpV1, CmpV2);
7178 };
7179 if (C->isZero()) {
7180 if (Pred2 == ICmpInst::ICMP_EQ) {
7181 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
7182 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
7183 return replaceInstUsesWith(
7184 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
7185 } else if (!IsSExt || HasOneUse) {
7186 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
7187 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
7188 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
7189 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X != -1
7190 return CreateRangeCheck();
7191 }
7192 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
7193 if (Pred2 == ICmpInst::ICMP_NE) {
7194 // icmp eq X, (zext (icmp ne X, 1)) --> false
7195 // icmp ne X, (zext (icmp ne X, 1)) --> true
7196 // icmp eq X, (sext (icmp ne X, -1)) --> false
7197 // icmp ne X, (sext (icmp ne X, -1)) --> true
7198 return replaceInstUsesWith(
7199 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
7200 } else if (!IsSExt || HasOneUse) {
7201 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
7202 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
7203 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
7204 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
7205 return CreateRangeCheck();
7206 }
7207 } else {
7208 // when C != 0 && C != 1:
7209 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
7210 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
7211 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
7212 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
7213 // when C != 0 && C != -1:
7214 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
7215 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
7216 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
7217 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
7218 return ICmpInst::Create(
7219 Instruction::ICmp, Pred1, X,
7220 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
7221 ? (IsSExt ? -1 : 1)
7222 : 0));
7223 }
7224 }
7225
7226 return nullptr;
7227}
7228
7229/// If we have an icmp le or icmp ge instruction with a constant operand, turn
7230/// it into the appropriate icmp lt or icmp gt instruction. This transform
7231/// allows them to be folded in visitICmpInst.
7233 ICmpInst::Predicate Pred = I.getPredicate();
7234 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
7236 return nullptr;
7237
7238 Value *Op0 = I.getOperand(0);
7239 Value *Op1 = I.getOperand(1);
7240 auto *Op1C = dyn_cast<Constant>(Op1);
7241 if (!Op1C)
7242 return nullptr;
7243
7244 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
7245 if (!FlippedStrictness)
7246 return nullptr;
7247
7248 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
7249}
7250
7251/// If we have a comparison with a non-canonical predicate, if we can update
7252/// all the users, invert the predicate and adjust all the users.
7254 // Is the predicate already canonical?
7255 CmpInst::Predicate Pred = I.getPredicate();
7257 return nullptr;
7258
7259 // Can all users be adjusted to predicate inversion?
7260 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
7261 return nullptr;
7262
7263 // Ok, we can canonicalize comparison!
7264 // Let's first invert the comparison's predicate.
7265 I.setPredicate(CmpInst::getInversePredicate(Pred));
7266 I.setName(I.getName() + ".not");
7267
7268 // And, adapt users.
7270
7271 return &I;
7272}
7273
7274/// Integer compare with boolean values can always be turned into bitwise ops.
7276 InstCombiner::BuilderTy &Builder) {
7277 Value *A = I.getOperand(0), *B = I.getOperand(1);
7278 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
7279
7280 // A boolean compared to true/false can be simplified to Op0/true/false in
7281 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
7282 // Cases not handled by InstSimplify are always 'not' of Op0.
7283 if (match(B, m_Zero())) {
7284 switch (I.getPredicate()) {
7285 case CmpInst::ICMP_EQ: // A == 0 -> !A
7286 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
7287 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
7289 default:
7290 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7291 }
7292 } else if (match(B, m_One())) {
7293 switch (I.getPredicate()) {
7294 case CmpInst::ICMP_NE: // A != 1 -> !A
7295 case CmpInst::ICMP_ULT: // A <u 1 -> !A
7296 case CmpInst::ICMP_SGT: // A >s -1 -> !A
7298 default:
7299 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7300 }
7301 }
7302
7303 switch (I.getPredicate()) {
7304 default:
7305 llvm_unreachable("Invalid icmp instruction!");
7306 case ICmpInst::ICMP_EQ:
7307 // icmp eq i1 A, B -> ~(A ^ B)
7308 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
7309
7310 case ICmpInst::ICMP_NE:
7311 // icmp ne i1 A, B -> A ^ B
7312 return BinaryOperator::CreateXor(A, B);
7313
7314 case ICmpInst::ICMP_UGT:
7315 // icmp ugt -> icmp ult
7316 std::swap(A, B);
7317 [[fallthrough]];
7318 case ICmpInst::ICMP_ULT:
7319 // icmp ult i1 A, B -> ~A & B
7320 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
7321
7322 case ICmpInst::ICMP_SGT:
7323 // icmp sgt -> icmp slt
7324 std::swap(A, B);
7325 [[fallthrough]];
7326 case ICmpInst::ICMP_SLT:
7327 // icmp slt i1 A, B -> A & ~B
7328 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
7329
7330 case ICmpInst::ICMP_UGE:
7331 // icmp uge -> icmp ule
7332 std::swap(A, B);
7333 [[fallthrough]];
7334 case ICmpInst::ICMP_ULE:
7335 // icmp ule i1 A, B -> ~A | B
7336 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
7337
7338 case ICmpInst::ICMP_SGE:
7339 // icmp sge -> icmp sle
7340 std::swap(A, B);
7341 [[fallthrough]];
7342 case ICmpInst::ICMP_SLE:
7343 // icmp sle i1 A, B -> A | ~B
7344 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
7345 }
7346}
7347
7348// Transform pattern like:
7349// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7350// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7351// Into:
7352// (X l>> Y) != 0
7353// (X l>> Y) == 0
7355 InstCombiner::BuilderTy &Builder) {
7356 CmpPredicate Pred, NewPred;
7357 Value *X, *Y;
7358 if (match(&Cmp,
7359 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
7360 switch (Pred) {
7361 case ICmpInst::ICMP_ULE:
7362 NewPred = ICmpInst::ICMP_NE;
7363 break;
7364 case ICmpInst::ICMP_UGT:
7365 NewPred = ICmpInst::ICMP_EQ;
7366 break;
7367 default:
7368 return nullptr;
7369 }
7370 } else if (match(&Cmp, m_c_ICmp(Pred,
7373 m_Add(m_Shl(m_One(), m_Value(Y)),
7374 m_AllOnes()))),
7375 m_Value(X)))) {
7376 // The variant with 'add' is not canonical, (the variant with 'not' is)
7377 // we only get it because it has extra uses, and can't be canonicalized,
7378
7379 switch (Pred) {
7380 case ICmpInst::ICMP_ULT:
7381 NewPred = ICmpInst::ICMP_NE;
7382 break;
7383 case ICmpInst::ICMP_UGE:
7384 NewPred = ICmpInst::ICMP_EQ;
7385 break;
7386 default:
7387 return nullptr;
7388 }
7389 } else
7390 return nullptr;
7391
7392 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
7393 Constant *Zero = Constant::getNullValue(NewX->getType());
7394 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
7395}
7396
7398 InstCombiner::BuilderTy &Builder) {
7399 const CmpInst::Predicate Pred = Cmp.getPredicate();
7400 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
7401 Value *V1, *V2;
7402
7403 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7404 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
7405 if (auto *I = dyn_cast<Instruction>(V))
7406 I->copyIRFlags(&Cmp);
7407 Module *M = Cmp.getModule();
7409 M, Intrinsic::vector_reverse, V->getType());
7410 return CallInst::Create(F, V);
7411 };
7412
7413 if (match(LHS, m_VecReverse(m_Value(V1)))) {
7414 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7415 if (match(RHS, m_VecReverse(m_Value(V2))) &&
7416 (LHS->hasOneUse() || RHS->hasOneUse()))
7417 return createCmpReverse(Pred, V1, V2);
7418
7419 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7420 if (LHS->hasOneUse() && isSplatValue(RHS))
7421 return createCmpReverse(Pred, V1, RHS);
7422 }
7423 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7424 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
7425 return createCmpReverse(Pred, LHS, V2);
7426
7427 ArrayRef<int> M;
7428 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
7429 return nullptr;
7430
7431 // If both arguments of the cmp are shuffles that use the same mask and
7432 // shuffle within a single vector, move the shuffle after the cmp:
7433 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7434 Type *V1Ty = V1->getType();
7435 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
7436 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7437 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
7438 return new ShuffleVectorInst(NewCmp, M);
7439 }
7440
7441 // Try to canonicalize compare with splatted operand and splat constant.
7442 // TODO: We could generalize this for more than splats. See/use the code in
7443 // InstCombiner::foldVectorBinop().
7444 Constant *C;
7445 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
7446 return nullptr;
7447
7448 // Length-changing splats are ok, so adjust the constants as needed:
7449 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7450 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7451 int MaskSplatIndex;
7452 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
7453 // We allow poison in matching, but this transform removes it for safety.
7454 // Demanded elements analysis should be able to recover some/all of that.
7455 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
7456 ScalarC);
7457 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7458 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
7459 return new ShuffleVectorInst(NewCmp, NewM);
7460 }
7461
7462 return nullptr;
7463}
7464
7465// extract(uadd.with.overflow(A, B), 0) ult A
7466// -> extract(uadd.with.overflow(A, B), 1)
7468 CmpInst::Predicate Pred = I.getPredicate();
7469 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7470
7471 Value *UAddOv;
7472 Value *A, *B;
7473 auto UAddOvResultPat = m_ExtractValue<0>(
7475 if (match(Op0, UAddOvResultPat) &&
7476 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7477 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
7478 (match(A, m_One()) || match(B, m_One()))) ||
7479 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
7480 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
7481 // extract(uadd.with.overflow(A, B), 0) < A
7482 // extract(uadd.with.overflow(A, 1), 0) == 0
7483 // extract(uadd.with.overflow(A, -1), 0) != -1
7484 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
7485 else if (match(Op1, UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&
7486 (Op0 == A || Op0 == B))
7487 // A > extract(uadd.with.overflow(A, B), 0)
7488 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
7489 else
7490 return nullptr;
7491
7492 return ExtractValueInst::Create(UAddOv, 1);
7493}
7494
7496 if (!I.getOperand(0)->getType()->isPointerTy() ||
7498 I.getParent()->getParent(),
7499 I.getOperand(0)->getType()->getPointerAddressSpace())) {
7500 return nullptr;
7501 }
7502 Instruction *Op;
7503 if (match(I.getOperand(0), m_Instruction(Op)) &&
7504 match(I.getOperand(1), m_Zero()) &&
7505 Op->isLaunderOrStripInvariantGroup()) {
7506 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7507 Op->getOperand(0), I.getOperand(1));
7508 }
7509 return nullptr;
7510}
7511
7513 IRBuilderBase &Builder) {
7514 if (!ICmpInst::isEquality(I.getPredicate()))
7515 return nullptr;
7516
7517 // The caller puts constants after non-constants.
7518 Value *Op = I.getOperand(0);
7519 Value *Const = I.getOperand(1);
7520
7521 // For Cond an equality condition, fold
7522 //
7523 // icmp (eq|ne) (vreduce_(or|and) Op), (Zero|AllOnes) ->
7524 // icmp (eq|ne) Op, (Zero|AllOnes)
7525 //
7526 // with a bitcast.
7527 Value *Vec;
7528 if ((match(Const, m_ZeroInt()) &&
7530 m_Value(Vec))))) ||
7531 (match(Const, m_AllOnes()) &&
7533 m_Value(Vec)))))) {
7534 auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType());
7535 if (!VecTy)
7536 return nullptr;
7537 Type *VecEltTy = VecTy->getElementType();
7538 unsigned ScalarBW =
7539 DL.getTypeSizeInBits(VecEltTy) * VecTy->getNumElements();
7540 if (!DL.fitsInLegalInteger(ScalarBW))
7541 return nullptr;
7542 Type *ScalarTy = IntegerType::get(I.getContext(), ScalarBW);
7543 Value *NewConst = match(Const, m_ZeroInt())
7544 ? ConstantInt::get(ScalarTy, 0)
7545 : ConstantInt::getAllOnesValue(ScalarTy);
7546 return CmpInst::Create(Instruction::ICmp, I.getPredicate(),
7547 Builder.CreateBitCast(Vec, ScalarTy), NewConst);
7548 }
7549 return nullptr;
7550}
7551
7552/// This function folds patterns produced by lowering of reduce idioms, such as
7553/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7554/// attempts to generate fewer number of scalar comparisons instead of vector
7555/// comparisons when possible.
7557 InstCombiner::BuilderTy &Builder,
7558 const DataLayout &DL) {
7559 if (I.getType()->isVectorTy())
7560 return nullptr;
7561 CmpPredicate OuterPred, InnerPred;
7562 Value *LHS, *RHS;
7563
7564 // Match lowering of @llvm.vector.reduce.and. Turn
7565 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7566 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7567 /// %res = icmp <pred> i8 %scalar_ne, 0
7568 ///
7569 /// into
7570 ///
7571 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7572 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7573 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7574 ///
7575 /// for <pred> in {ne, eq}.
7576 if (!match(&I, m_ICmp(OuterPred,
7578 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7579 m_Zero())))
7580 return nullptr;
7581 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7582 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7583 return nullptr;
7584 unsigned NumBits =
7585 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7586 // TODO: Relax this to "not wider than max legal integer type"?
7587 if (!DL.isLegalInteger(NumBits))
7588 return nullptr;
7589
7590 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7591 auto *ScalarTy = Builder.getIntNTy(NumBits);
7592 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7593 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7594 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7595 I.getName());
7596 }
7597
7598 return nullptr;
7599}
7600
7601// This helper will be called with icmp operands in both orders.
7603 Value *Op0, Value *Op1,
7604 ICmpInst &CxtI) {
7605 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7606 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7607 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7608 return NI;
7609
7610 if (auto *SI = dyn_cast<SelectInst>(Op0))
7611 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7612 return NI;
7613
7614 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0)) {
7615 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7616 return Res;
7617
7618 if (Instruction *Res = foldICmpWithClamp(CxtI, Op1, MinMax))
7619 return Res;
7620 }
7621
7622 {
7623 Value *X;
7624 const APInt *C;
7625 // icmp X+Cst, X
7626 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7627 return foldICmpAddOpConst(X, *C, Pred);
7628 }
7629
7630 // abs(X) >= X --> true
7631 // abs(X) u<= X --> true
7632 // abs(X) < X --> false
7633 // abs(X) u> X --> false
7634 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7635 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7636 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7637 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7638 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7639 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7640 {
7641 Value *X;
7642 Constant *C;
7644 match(Op1, m_Specific(X))) {
7645 Value *NullValue = Constant::getNullValue(X->getType());
7646 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7647 const APInt SMin =
7648 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7649 bool IsIntMinPosion = C->isAllOnesValue();
7650 switch (Pred) {
7651 case CmpInst::ICMP_ULE:
7652 case CmpInst::ICMP_SGE:
7653 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7654 case CmpInst::ICMP_UGT:
7655 case CmpInst::ICMP_SLT:
7657 case CmpInst::ICMP_UGE:
7658 case CmpInst::ICMP_SLE:
7659 case CmpInst::ICMP_EQ: {
7660 return replaceInstUsesWith(
7661 CxtI, IsIntMinPosion
7662 ? Builder.CreateICmpSGT(X, AllOnesValue)
7663 : Builder.CreateICmpULT(
7664 X, ConstantInt::get(X->getType(), SMin + 1)));
7665 }
7666 case CmpInst::ICMP_ULT:
7667 case CmpInst::ICMP_SGT:
7668 case CmpInst::ICMP_NE: {
7669 return replaceInstUsesWith(
7670 CxtI, IsIntMinPosion
7671 ? Builder.CreateICmpSLT(X, NullValue)
7672 : Builder.CreateICmpUGT(
7673 X, ConstantInt::get(X->getType(), SMin)));
7674 }
7675 default:
7676 llvm_unreachable("Invalid predicate!");
7677 }
7678 }
7679 }
7680
7681 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7682 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7683 return replaceInstUsesWith(CxtI, V);
7684
7685 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7686 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7687 {
7688 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7689 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7691 }
7692
7693 if (!ICmpInst::isUnsigned(Pred) &&
7694 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7695 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7697 }
7698 }
7699
7700 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7701 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7702 {
7703 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7704 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7706 }
7707
7708 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7709 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7710 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7712 }
7713 }
7714
7715 // icmp (shl nsw/nuw X, L), (add nsw/nuw (shl nsw/nuw Y, L), K)
7716 // -> icmp X, (add nsw/nuw Y, K >> L)
7717 // We use AShr for nsw and LShr for nuw to safely peel off the shift.
7718 Value *X;
7719 uint64_t ShAmt;
7720 if (match(Op0, m_NUWShl(m_Value(X), m_ConstantInt(ShAmt))) &&
7721 !CxtI.isSigned()) {
7722 if (ShAmt >= X->getType()->getScalarSizeInBits())
7723 return nullptr;
7724 if (canEvaluateShifted(Op1, ShAmt, /*IsLeftShift=*/false,
7725 ShiftSemantics::Unsigned, &CxtI)) {
7726 Value *NewOp1 = getShiftedValue(Op1, ShAmt, /*IsLeftShift=*/false,
7728 return new ICmpInst(Pred, X, NewOp1);
7729 }
7730 }
7731
7732 if (match(Op0, m_NSWShl(m_Value(X), m_ConstantInt(ShAmt))) &&
7733 !CxtI.isUnsigned()) {
7734 if (ShAmt >= X->getType()->getScalarSizeInBits())
7735 return nullptr;
7736 if (canEvaluateShifted(Op1, ShAmt, /*IsLeftShift=*/false,
7737 ShiftSemantics::Signed, &CxtI)) {
7738 Value *NewOp1 = getShiftedValue(Op1, ShAmt, /*IsLeftShift=*/false,
7740 return new ICmpInst(Pred, X, NewOp1);
7741 }
7742 }
7743 return nullptr;
7744}
7745
7747 bool Changed = false;
7748 const SimplifyQuery Q = SQ.getWithInstruction(&I);
7749 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7750 unsigned Op0Cplxity = getComplexity(Op0);
7751 unsigned Op1Cplxity = getComplexity(Op1);
7752
7753 /// Orders the operands of the compare so that they are listed from most
7754 /// complex to least complex. This puts constants before unary operators,
7755 /// before binary operators.
7756 if (Op0Cplxity < Op1Cplxity) {
7757 I.swapOperands();
7758 std::swap(Op0, Op1);
7759 Changed = true;
7760 }
7761
7762 if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
7763 return replaceInstUsesWith(I, V);
7764
7765 // Comparing -val or val with non-zero is the same as just comparing val
7766 // ie, abs(val) != 0 -> val != 0
7767 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7768 Value *Cond, *SelectTrue, *SelectFalse;
7769 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7770 m_Value(SelectFalse)))) {
7771 if (Value *V = dyn_castNegVal(SelectTrue)) {
7772 if (V == SelectFalse)
7773 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7774 } else if (Value *V = dyn_castNegVal(SelectFalse)) {
7775 if (V == SelectTrue)
7776 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7777 }
7778 }
7779 }
7780
7782 return Res;
7783
7784 if (Op0->getType()->isIntOrIntVectorTy(1))
7786 return Res;
7787
7789 return Res;
7790
7792 return Res;
7793
7795 return Res;
7796
7798 return Res;
7799
7801 return Res;
7802
7804 return Res;
7805
7807 return Res;
7808
7809 // Test if the ICmpInst instruction is used exclusively by a select as
7810 // part of a minimum or maximum operation. If so, refrain from doing
7811 // any other folding. This helps out other analyses which understand
7812 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7813 // and CodeGen. And in this case, at least one of the comparison
7814 // operands has at least one user besides the compare (the select),
7815 // which would often largely negate the benefit of folding anyway.
7816 //
7817 // Do the same for the other patterns recognized by matchSelectPattern.
7818 if (I.hasOneUse())
7819 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7820 Value *A, *B;
7822 if (SPR.Flavor != SPF_UNKNOWN)
7823 return nullptr;
7824 }
7825
7826 // Do this after checking for min/max to prevent infinite looping.
7827 if (Instruction *Res = foldICmpWithZero(I))
7828 return Res;
7829
7830 Value *X;
7831 const APInt *C;
7832 if (I.getPredicate() == ICmpInst::ICMP_UGT &&
7833 match(Op0, m_UMax(m_Value(X), m_APInt(C))) &&
7834 match(Op1, m_Not(m_Specific(X)))) {
7835 if (C->isNonNegative())
7836 return new ICmpInst(ICmpInst::ICMP_SLT, X,
7837 Constant::getNullValue(X->getType()));
7838 return new ICmpInst(ICmpInst::ICMP_UGT, X,
7839 ConstantInt::get(X->getType(), ~*C));
7840 }
7841
7842 if (I.getPredicate() == ICmpInst::ICMP_ULT &&
7843 match(Op0, m_UMax(m_Value(X), m_APInt(C))) &&
7844 match(Op1, m_Not(m_Specific(X)))) {
7845 if (C->isNonNegative())
7846 return new ICmpInst(ICmpInst::ICMP_SGT, X,
7847 Constant::getAllOnesValue(X->getType()));
7848 return new ICmpInst(ICmpInst::ICMP_ULT, X,
7849 ConstantInt::get(X->getType(), ~*C));
7850 }
7851
7852 // FIXME: We only do this after checking for min/max to prevent infinite
7853 // looping caused by a reverse canonicalization of these patterns for min/max.
7854 // FIXME: The organization of folds is a mess. These would naturally go into
7855 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7856 // down here after the min/max restriction.
7857 ICmpInst::Predicate Pred = I.getPredicate();
7858 if (match(Op1, m_APInt(C))) {
7859 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7860 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7861 Constant *Zero = Constant::getNullValue(Op0->getType());
7862 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7863 }
7864
7865 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7866 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7868 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7869 }
7870 }
7871
7872 // The folds in here may rely on wrapping flags and special constants, so
7873 // they can break up min/max idioms in some cases but not seemingly similar
7874 // patterns.
7875 // FIXME: It may be possible to enhance select folding to make this
7876 // unnecessary. It may also be moot if we canonicalize to min/max
7877 // intrinsics.
7878 if (Instruction *Res = foldICmpBinOp(I, Q))
7879 return Res;
7880
7882 return Res;
7883
7884 // Try to match comparison as a sign bit test. Intentionally do this after
7885 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7886 if (Instruction *New = foldSignBitTest(I))
7887 return New;
7888
7889 if (auto *PN = dyn_cast<PHINode>(Op0))
7890 if (Instruction *NV = foldOpIntoPhi(I, PN))
7891 return NV;
7892 if (auto *PN = dyn_cast<PHINode>(Op1))
7893 if (Instruction *NV = foldOpIntoPhi(I, PN))
7894 return NV;
7895
7897 return Res;
7898
7899 if (Instruction *Res = foldICmpCommutative(I.getCmpPredicate(), Op0, Op1, I))
7900 return Res;
7901 if (Instruction *Res =
7902 foldICmpCommutative(I.getSwappedCmpPredicate(), Op1, Op0, I))
7903 return Res;
7904
7905 if (I.isCommutative()) {
7906 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7907 replaceOperand(I, 0, Pair->first);
7908 replaceOperand(I, 1, Pair->second);
7909 return &I;
7910 }
7911 }
7912
7913 // Fold icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2)
7914 // when all select arms are constants, via truth table.
7916 return R;
7917
7918 // In case of a comparison with two select instructions having the same
7919 // condition, check whether one of the resulting branches can be simplified.
7920 // If so, just compare the other branch and select the appropriate result.
7921 // For example:
7922 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7923 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7924 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7925 // The icmp will result false for the false value of selects and the result
7926 // will depend upon the comparison of true values of selects if %cmp is
7927 // true. Thus, transform this into:
7928 // %cmp = icmp slt i32 %y, %z
7929 // %sel = select i1 %cond, i1 %cmp, i1 false
7930 // This handles similar cases to transform.
7931 {
7932 Value *Cond, *A, *B, *C, *D;
7933 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7935 (Op0->hasOneUse() || Op1->hasOneUse())) {
7936 // Check whether comparison of TrueValues can be simplified
7937 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7938 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7939 return SelectInst::Create(
7940 Cond, Res, NewICMP, /*NameStr=*/"", /*InsertBefore=*/nullptr,
7942 }
7943 // Check whether comparison of FalseValues can be simplified
7944 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7945 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7946 return SelectInst::Create(
7947 Cond, NewICMP, Res, /*NameStr=*/"", /*InsertBefore=*/nullptr,
7949 }
7950 }
7951 }
7952
7953 // icmp slt (sub nsw x, y), (add nsw x, y) --> icmp sgt y, 0
7954 // icmp ult (sub nuw x, y), (add nuw x, y) --> icmp ugt y, 0
7955 // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y) --> icmp eq y, 0
7956 {
7957 Value *A, *B;
7958 CmpPredicate CmpPred;
7959 if (match(&I, m_c_ICmp(CmpPred, m_Sub(m_Value(A), m_Value(B)),
7961 auto *I0 = cast<OverflowingBinaryOperator>(Op0);
7962 auto *I1 = cast<OverflowingBinaryOperator>(Op1);
7963 bool I0NUW = I0->hasNoUnsignedWrap();
7964 bool I1NUW = I1->hasNoUnsignedWrap();
7965 bool I0NSW = I0->hasNoSignedWrap();
7966 bool I1NSW = I1->hasNoSignedWrap();
7967 if ((ICmpInst::isUnsigned(Pred) && I0NUW && I1NUW) ||
7968 (ICmpInst::isSigned(Pred) && I0NSW && I1NSW) ||
7969 (ICmpInst::isEquality(Pred) &&
7970 ((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {
7971 return new ICmpInst(CmpPredicate::getSwapped(CmpPred), B,
7972 ConstantInt::get(Op0->getType(), 0));
7973 }
7974 }
7975 }
7976
7977 // Try to optimize equality comparisons against alloca-based pointers.
7978 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7979 assert(Op1->getType()->isPointerTy() &&
7980 "Comparing pointer with non-pointer?");
7981 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7982 if (foldAllocaCmp(Alloca))
7983 return nullptr;
7984 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7985 if (foldAllocaCmp(Alloca))
7986 return nullptr;
7987 }
7988
7989 if (Instruction *Res = foldICmpBitCast(I))
7990 return Res;
7991
7992 // TODO: Hoist this above the min/max bailout.
7994 return R;
7995
7996 {
7997 Value *X, *Y;
7998 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7999 // and (X & ~Y) != 0 --> (X & Y) == 0
8000 // if A is a power of 2.
8001 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
8002 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, &I) &&
8003 I.isEquality())
8004 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
8005 Op1);
8006
8007 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
8008 if (Op0->getType()->isIntOrIntVectorTy()) {
8009 bool ConsumesOp0, ConsumesOp1;
8010 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
8011 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
8012 (ConsumesOp0 || ConsumesOp1)) {
8013 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
8014 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
8015 assert(InvOp0 && InvOp1 &&
8016 "Mismatch between isFreeToInvert and getFreelyInverted");
8017 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
8018 }
8019 }
8020
8021 Instruction *AddI = nullptr;
8023 m_Instruction(AddI))) &&
8024 isa<IntegerType>(X->getType())) {
8025 Value *Result;
8026 Constant *Overflow;
8027 // m_UAddWithOverflow can match patterns that do not include an explicit
8028 // "add" instruction, so check the opcode of the matched op.
8029 if (AddI->getOpcode() == Instruction::Add &&
8030 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
8031 Result, Overflow)) {
8032 replaceInstUsesWith(*AddI, Result);
8033 eraseInstFromFunction(*AddI);
8034 return replaceInstUsesWith(I, Overflow);
8035 }
8036 }
8037
8038 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
8039 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
8040 match(Op1, m_APInt(C))) {
8041 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
8042 return R;
8043 }
8044
8045 // Signbit test folds
8046 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
8047 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
8048 Instruction *ExtI;
8049 if ((I.isUnsigned() || I.isEquality()) &&
8050 match(Op1,
8052 Y->getType()->getScalarSizeInBits() == 1 &&
8053 (Op0->hasOneUse() || Op1->hasOneUse())) {
8054 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
8055 Instruction *ShiftI;
8056 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
8058 OpWidth - 1))))) {
8059 unsigned ExtOpc = ExtI->getOpcode();
8060 unsigned ShiftOpc = ShiftI->getOpcode();
8061 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
8062 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
8063 Value *SLTZero =
8064 Builder.CreateICmpSLT(X, Constant::getNullValue(X->getType()));
8065 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
8066 return replaceInstUsesWith(I, Cmp);
8067 }
8068 }
8069 }
8070 }
8071
8072 if (Instruction *Res = foldICmpEquality(I))
8073 return Res;
8074
8076 return Res;
8077
8078 if (Instruction *Res = foldICmpOfUAddOv(I))
8079 return Res;
8080
8082 return Res;
8083
8084 // The 'cmpxchg' instruction returns an aggregate containing the old value and
8085 // an i1 which indicates whether or not we successfully did the swap.
8086 //
8087 // Replace comparisons between the old value and the expected value with the
8088 // indicator that 'cmpxchg' returns.
8089 //
8090 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
8091 // spuriously fail. In those cases, the old value may equal the expected
8092 // value but it is possible for the swap to not occur.
8093 if (I.getPredicate() == ICmpInst::ICMP_EQ)
8094 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
8095 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
8096 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
8097 !ACXI->isWeak())
8098 return ExtractValueInst::Create(ACXI, 1);
8099
8101 return Res;
8102
8103 if (I.getType()->isVectorTy())
8104 if (Instruction *Res = foldVectorCmp(I, Builder))
8105 return Res;
8106
8108 return Res;
8109
8111 return Res;
8112
8113 {
8114 Value *A;
8115 const APInt *C1, *C2;
8116 ICmpInst::Predicate Pred = I.getPredicate();
8117 if (ICmpInst::isEquality(Pred)) {
8118 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
8119 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
8120 if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) &&
8121 match(Op1, m_APInt(C2))) {
8122 Type *InputTy = A->getType();
8123 unsigned InputBitWidth = InputTy->getScalarSizeInBits();
8124 // c2 must be non-negative at the bitwidth of a.
8125 if (C2->getActiveBits() < InputBitWidth) {
8126 APInt TruncC1 = C1->trunc(InputBitWidth);
8127 // Check if there are 1s in C1 high bits of size InputBitWidth.
8128 if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth)))
8129 TruncC1.setBit(InputBitWidth - 1);
8130 Value *AndInst = Builder.CreateAnd(A, TruncC1);
8131 return new ICmpInst(
8132 Pred, AndInst,
8133 ConstantInt::get(InputTy, C2->trunc(InputBitWidth)));
8134 }
8135 }
8136 }
8137 }
8138
8139 return Changed ? &I : nullptr;
8140}
8141
8142/// Fold fcmp ([us]itofp x, cst) if possible.
8144 Instruction *LHSI,
8145 Constant *RHSC) {
8146 const APFloat *RHS;
8147 if (!match(RHSC, m_APFloat(RHS)))
8148 return nullptr;
8149
8150 // Get the width of the mantissa. We don't want to hack on conversions that
8151 // might lose information from the integer, e.g. "i64 -> float"
8152 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
8153 if (MantissaWidth == -1)
8154 return nullptr; // Unknown.
8155
8156 Type *IntTy = LHSI->getOperand(0)->getType();
8157 unsigned IntWidth = IntTy->getScalarSizeInBits();
8158 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
8159
8160 if (I.isEquality()) {
8161 FCmpInst::Predicate P = I.getPredicate();
8162 bool IsExact = false;
8163 APSInt RHSCvt(IntWidth, LHSUnsigned);
8164 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
8165
8166 // If the floating point constant isn't an integer value, we know if we will
8167 // ever compare equal / not equal to it.
8168 if (!IsExact) {
8169 // TODO: Can never be -0.0 and other non-representable values
8170 APFloat RHSRoundInt(*RHS);
8172 if (*RHS != RHSRoundInt) {
8174 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8175
8177 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8178 }
8179 }
8180
8181 // TODO: If the constant is exactly representable, is it always OK to do
8182 // equality compares as integer?
8183 }
8184
8185 // Check to see that the input is converted from an integer type that is small
8186 // enough that preserves all bits. TODO: check here for "known" sign bits.
8187 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
8188
8189 // Following test does NOT adjust IntWidth downwards for signed inputs,
8190 // because the most negative value still requires all the mantissa bits
8191 // to distinguish it from one less than that value.
8192 if ((int)IntWidth > MantissaWidth) {
8193 // Conversion would lose accuracy. Check if loss can impact comparison.
8194 int Exp = ilogb(*RHS);
8195 if (Exp == APFloat::IEK_Inf) {
8196 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
8197 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
8198 // Conversion could create infinity.
8199 return nullptr;
8200 } else {
8201 // Note that if RHS is zero or NaN, then Exp is negative
8202 // and first condition is trivially false.
8203 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
8204 // Conversion could affect comparison.
8205 return nullptr;
8206 }
8207 }
8208
8209 // Otherwise, we can potentially simplify the comparison. We know that it
8210 // will always come through as an integer value and we know the constant is
8211 // not a NAN (it would have been previously simplified).
8212 assert(!RHS->isNaN() && "NaN comparison not already folded!");
8213
8215 switch (I.getPredicate()) {
8216 default:
8217 llvm_unreachable("Unexpected predicate!");
8218 case FCmpInst::FCMP_UEQ:
8219 case FCmpInst::FCMP_OEQ:
8220 Pred = ICmpInst::ICMP_EQ;
8221 break;
8222 case FCmpInst::FCMP_UGT:
8223 case FCmpInst::FCMP_OGT:
8224 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
8225 break;
8226 case FCmpInst::FCMP_UGE:
8227 case FCmpInst::FCMP_OGE:
8228 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
8229 break;
8230 case FCmpInst::FCMP_ULT:
8231 case FCmpInst::FCMP_OLT:
8232 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
8233 break;
8234 case FCmpInst::FCMP_ULE:
8235 case FCmpInst::FCMP_OLE:
8236 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
8237 break;
8238 case FCmpInst::FCMP_UNE:
8239 case FCmpInst::FCMP_ONE:
8240 Pred = ICmpInst::ICMP_NE;
8241 break;
8242 case FCmpInst::FCMP_ORD:
8243 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8244 case FCmpInst::FCMP_UNO:
8245 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8246 }
8247
8248 // Now we know that the APFloat is a normal number, zero or inf.
8249
8250 // See if the FP constant is too large for the integer. For example,
8251 // comparing an i8 to 300.0.
8252 if (!LHSUnsigned) {
8253 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
8254 // and large values.
8255 APFloat SMax(RHS->getSemantics());
8256 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
8258 if (SMax < *RHS) { // smax < 13123.0
8259 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
8260 Pred == ICmpInst::ICMP_SLE)
8261 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8262 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8263 }
8264 } else {
8265 // If the RHS value is > UnsignedMax, fold the comparison. This handles
8266 // +INF and large values.
8267 APFloat UMax(RHS->getSemantics());
8268 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
8270 if (UMax < *RHS) { // umax < 13123.0
8271 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
8272 Pred == ICmpInst::ICMP_ULE)
8273 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8274 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8275 }
8276 }
8277
8278 if (!LHSUnsigned) {
8279 // See if the RHS value is < SignedMin.
8280 APFloat SMin(RHS->getSemantics());
8281 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
8283 if (SMin > *RHS) { // smin > 12312.0
8284 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
8285 Pred == ICmpInst::ICMP_SGE)
8286 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8287 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8288 }
8289 } else {
8290 // See if the RHS value is < UnsignedMin.
8291 APFloat UMin(RHS->getSemantics());
8292 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
8294 if (UMin > *RHS) { // umin > 12312.0
8295 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
8296 Pred == ICmpInst::ICMP_UGE)
8297 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8298 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8299 }
8300 }
8301
8302 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
8303 // [0, UMAX], but it may still be fractional. Check whether this is the case
8304 // using the IsExact flag.
8305 // Don't do this for zero, because -0.0 is not fractional.
8306 APSInt RHSInt(IntWidth, LHSUnsigned);
8307 bool IsExact;
8308 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
8309 if (!RHS->isZero()) {
8310 if (!IsExact) {
8311 // If we had a comparison against a fractional value, we have to adjust
8312 // the compare predicate and sometimes the value. RHSC is rounded towards
8313 // zero at this point.
8314 switch (Pred) {
8315 default:
8316 llvm_unreachable("Unexpected integer comparison!");
8317 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
8318 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8319 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
8320 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8321 case ICmpInst::ICMP_ULE:
8322 // (float)int <= 4.4 --> int <= 4
8323 // (float)int <= -4.4 --> false
8324 if (RHS->isNegative())
8325 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8326 break;
8327 case ICmpInst::ICMP_SLE:
8328 // (float)int <= 4.4 --> int <= 4
8329 // (float)int <= -4.4 --> int < -4
8330 if (RHS->isNegative())
8331 Pred = ICmpInst::ICMP_SLT;
8332 break;
8333 case ICmpInst::ICMP_ULT:
8334 // (float)int < -4.4 --> false
8335 // (float)int < 4.4 --> int <= 4
8336 if (RHS->isNegative())
8337 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8338 Pred = ICmpInst::ICMP_ULE;
8339 break;
8340 case ICmpInst::ICMP_SLT:
8341 // (float)int < -4.4 --> int < -4
8342 // (float)int < 4.4 --> int <= 4
8343 if (!RHS->isNegative())
8344 Pred = ICmpInst::ICMP_SLE;
8345 break;
8346 case ICmpInst::ICMP_UGT:
8347 // (float)int > 4.4 --> int > 4
8348 // (float)int > -4.4 --> true
8349 if (RHS->isNegative())
8350 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8351 break;
8352 case ICmpInst::ICMP_SGT:
8353 // (float)int > 4.4 --> int > 4
8354 // (float)int > -4.4 --> int >= -4
8355 if (RHS->isNegative())
8356 Pred = ICmpInst::ICMP_SGE;
8357 break;
8358 case ICmpInst::ICMP_UGE:
8359 // (float)int >= -4.4 --> true
8360 // (float)int >= 4.4 --> int > 4
8361 if (RHS->isNegative())
8362 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8363 Pred = ICmpInst::ICMP_UGT;
8364 break;
8365 case ICmpInst::ICMP_SGE:
8366 // (float)int >= -4.4 --> int >= -4
8367 // (float)int >= 4.4 --> int > 4
8368 if (!RHS->isNegative())
8369 Pred = ICmpInst::ICMP_SGT;
8370 break;
8371 }
8372 }
8373 }
8374
8375 // Lower this FP comparison into an appropriate integer version of the
8376 // comparison.
8377 return new ICmpInst(Pred, LHSI->getOperand(0),
8378 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
8379}
8380
8381/// Fold fcmp/icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2)
8382/// where all true/false values are constants that allow the compare to be
8383/// constant-folded for every combination of C1 and C2.
8384/// We compute a 4-entry truth table and use createLogicFromTable to
8385/// synthesize a boolean expression of C1 and C2.
8387 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8388 Value *C1, *C2;
8389 Constant *TV1, *FV1, *TV2, *FV2;
8390
8391 if (!match(Op0, m_Select(m_Value(C1), m_Constant(TV1), m_Constant(FV1))) ||
8392 !match(Op1, m_Select(m_Value(C2), m_Constant(TV2), m_Constant(FV2))))
8393 return nullptr;
8394
8395 if (I.getType() != C1->getType() || I.getType() != C2->getType())
8396 return nullptr;
8397
8398 unsigned Pred = I.getPredicate();
8399 const DataLayout &DL = I.getDataLayout();
8400
8401 Constant *Res00 = ConstantFoldCompareInstOperands(Pred, FV1, FV2, DL);
8402 Constant *Res01 = ConstantFoldCompareInstOperands(Pred, FV1, TV2, DL);
8403 Constant *Res10 = ConstantFoldCompareInstOperands(Pred, TV1, FV2, DL);
8404 Constant *Res11 = ConstantFoldCompareInstOperands(Pred, TV1, TV2, DL);
8405
8406 if (!Res00 || !Res01 || !Res10 || !Res11)
8407 return nullptr;
8408
8409 if ((!Res00->isNullValue() && !Res00->isAllOnesValue()) ||
8410 (!Res01->isNullValue() && !Res01->isAllOnesValue()) ||
8411 (!Res10->isNullValue() && !Res10->isAllOnesValue()) ||
8412 (!Res11->isNullValue() && !Res11->isAllOnesValue()))
8413 return nullptr;
8414
8415 std::bitset<4> Table;
8416 if (!Res00->isNullValue())
8417 Table.set(0);
8418 if (!Res01->isNullValue())
8419 Table.set(1);
8420 if (!Res10->isNullValue())
8421 Table.set(2);
8422 if (!Res11->isNullValue())
8423 Table.set(3);
8424
8425 Value *Res = createLogicFromTable(Table, C1, C2, Builder,
8426 Op0->hasOneUse() && Op1->hasOneUse());
8427 if (!Res)
8428 return nullptr;
8429 return replaceInstUsesWith(I, Res);
8430}
8431
8432/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
8434 Constant *RHSC) {
8435 // When C is not 0.0 and infinities are not allowed:
8436 // (C / X) < 0.0 is a sign-bit test of X
8437 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
8438 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
8439 //
8440 // Proof:
8441 // Multiply (C / X) < 0.0 by X * X / C.
8442 // - X is non zero, if it is the flag 'ninf' is violated.
8443 // - C defines the sign of X * X * C. Thus it also defines whether to swap
8444 // the predicate. C is also non zero by definition.
8445 //
8446 // Thus X * X / C is non zero and the transformation is valid. [qed]
8447
8448 FCmpInst::Predicate Pred = I.getPredicate();
8449
8450 // Check that predicates are valid.
8451 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
8452 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
8453 return nullptr;
8454
8455 // Check that RHS operand is zero.
8456 if (!match(RHSC, m_AnyZeroFP()))
8457 return nullptr;
8458
8459 // Check fastmath flags ('ninf').
8460 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
8461 return nullptr;
8462
8463 // Check the properties of the dividend. It must not be zero to avoid a
8464 // division by zero (see Proof).
8465 const APFloat *C;
8466 if (!match(LHSI->getOperand(0), m_APFloat(C)))
8467 return nullptr;
8468
8469 if (C->isZero())
8470 return nullptr;
8471
8472 // Get swapped predicate if necessary.
8473 if (C->isNegative())
8474 Pred = I.getSwappedPredicate();
8475
8476 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
8477}
8478
8479// Transform 'fptrunc(x) cmp C' to 'x cmp ext(C)' if possible.
8480// Patterns include:
8481// fptrunc(x) < C --> x < ext(C)
8482// fptrunc(x) <= C --> x <= ext(C)
8483// fptrunc(x) > C --> x > ext(C)
8484// fptrunc(x) >= C --> x >= ext(C)
8485// fptrunc(x) ord/uno C --> x ord/uno 0
8486// where 'ext(C)' is the extension of 'C' to the type of 'x' with a small bias
8487// due to precision loss.
8489 const Constant &C) {
8490 FCmpInst::Predicate Pred = I.getPredicate();
8491 Type *DestType = FPTrunc.getOperand(0)->getType();
8492
8493 const APFloat *CValue;
8494 // TODO: support vec
8495 if (!match(&C, m_APFloat(CValue)))
8496 return nullptr;
8497
8498 // Handle ord/uno
8499 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
8500 assert(!CValue->isNaN() &&
8501 "X ord/uno NaN should be folded away by simplifyFCmpInst()");
8502 return new FCmpInst(Pred, FPTrunc.getOperand(0),
8503 ConstantFP::getZero(DestType), "", &I);
8504 }
8505
8506 // Handle <, >, <=, >=
8507 bool RoundDown = false;
8508
8509 if (Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE ||
8510 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT)
8511 RoundDown = true;
8512 else if (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT ||
8513 Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)
8514 RoundDown = false;
8515 else
8516 return nullptr;
8517
8518 if (CValue->isNaN() || CValue->isInfinity())
8519 return nullptr;
8520
8521 auto ConvertFltSema = [](const APFloat &Src, const fltSemantics &Sema) {
8522 bool LosesInfo;
8523 APFloat Dest = Src;
8524 Dest.convert(Sema, APFloat::rmNearestTiesToEven, &LosesInfo);
8525 return Dest;
8526 };
8527
8528 auto NextValue = [](const APFloat &Value, bool RoundDown) {
8529 APFloat NextValue = Value;
8530 NextValue.next(RoundDown);
8531 return NextValue;
8532 };
8533
8534 APFloat NextCValue = NextValue(*CValue, RoundDown);
8535
8536 const fltSemantics &DestFltSema =
8537 DestType->getScalarType()->getFltSemantics();
8538
8539 APFloat ExtCValue = ConvertFltSema(*CValue, DestFltSema);
8540 APFloat ExtNextCValue = ConvertFltSema(NextCValue, DestFltSema);
8541
8542 // When 'NextCValue' is infinity, use an imaged 'NextCValue' that equals
8543 // 'CValue + bias' to avoid the infinity after conversion. The bias is
8544 // estimated as 'CValue - PrevCValue', where 'PrevCValue' is the previous
8545 // value of 'CValue'.
8546 if (NextCValue.isInfinity()) {
8547 APFloat PrevCValue = NextValue(*CValue, !RoundDown);
8548 APFloat Bias = ConvertFltSema(*CValue - PrevCValue, DestFltSema);
8549
8550 ExtNextCValue = ExtCValue + Bias;
8551 }
8552
8553 APFloat ExtMidValue =
8554 scalbn(ExtCValue + ExtNextCValue, -1, APFloat::rmNearestTiesToEven);
8555
8556 const fltSemantics &SrcFltSema =
8557 C.getType()->getScalarType()->getFltSemantics();
8558
8559 // 'MidValue' might be rounded to 'NextCValue'. Correct it here.
8560 APFloat MidValue = ConvertFltSema(ExtMidValue, SrcFltSema);
8561 if (MidValue != *CValue)
8562 ExtMidValue.next(!RoundDown);
8563
8564 // Check whether 'ExtMidValue' is a valid result since the assumption on
8565 // imaged 'NextCValue' might not hold for new float types.
8566 // ppc_fp128 can't pass here when converting from max float because of
8567 // APFloat implementation.
8568 if (NextCValue.isInfinity()) {
8569 // ExtMidValue --- narrowed ---> Finite
8570 if (ConvertFltSema(ExtMidValue, SrcFltSema).isInfinity())
8571 return nullptr;
8572
8573 // NextExtMidValue --- narrowed ---> Infinity
8574 APFloat NextExtMidValue = NextValue(ExtMidValue, RoundDown);
8575 if (ConvertFltSema(NextExtMidValue, SrcFltSema).isFinite())
8576 return nullptr;
8577 }
8578
8579 return new FCmpInst(Pred, FPTrunc.getOperand(0),
8580 ConstantFP::get(DestType, ExtMidValue), "", &I);
8581}
8582
8583/// Optimize fabs(X) compared with zero.
8585 Value *X;
8586 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
8587 return nullptr;
8588
8589 const APFloat *C;
8590 if (!match(I.getOperand(1), m_APFloat(C)))
8591 return nullptr;
8592
8593 if (!C->isPosZero()) {
8594 if (!C->isSmallestNormalized())
8595 return nullptr;
8596
8597 const Function *F = I.getFunction();
8598 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
8599 if (Mode.Input == DenormalMode::PreserveSign ||
8601
8602 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8603 Constant *Zero = ConstantFP::getZero(X->getType());
8604 return new FCmpInst(P, X, Zero, "", I);
8605 };
8606
8607 switch (I.getPredicate()) {
8608 case FCmpInst::FCMP_OLT:
8609 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
8610 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
8611 case FCmpInst::FCMP_UGE:
8612 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
8613 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
8614 case FCmpInst::FCMP_OGE:
8615 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
8616 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
8617 case FCmpInst::FCMP_ULT:
8618 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
8619 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
8620 default:
8621 break;
8622 }
8623 }
8624
8625 return nullptr;
8626 }
8627
8628 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8629 I->setPredicate(P);
8630 return IC.replaceOperand(*I, 0, X);
8631 };
8632
8633 switch (I.getPredicate()) {
8634 case FCmpInst::FCMP_UGE:
8635 case FCmpInst::FCMP_OLT:
8636 // fabs(X) >= 0.0 --> true
8637 // fabs(X) < 0.0 --> false
8638 llvm_unreachable("fcmp should have simplified");
8639
8640 case FCmpInst::FCMP_OGT:
8641 // fabs(X) > 0.0 --> X != 0.0
8642 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8643
8644 case FCmpInst::FCMP_UGT:
8645 // fabs(X) u> 0.0 --> X u!= 0.0
8646 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8647
8648 case FCmpInst::FCMP_OLE:
8649 // fabs(X) <= 0.0 --> X == 0.0
8650 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8651
8652 case FCmpInst::FCMP_ULE:
8653 // fabs(X) u<= 0.0 --> X u== 0.0
8654 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8655
8656 case FCmpInst::FCMP_OGE:
8657 // fabs(X) >= 0.0 --> !isnan(X)
8658 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8659 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8660
8661 case FCmpInst::FCMP_ULT:
8662 // fabs(X) u< 0.0 --> isnan(X)
8663 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8664 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8665
8666 case FCmpInst::FCMP_OEQ:
8667 case FCmpInst::FCMP_UEQ:
8668 case FCmpInst::FCMP_ONE:
8669 case FCmpInst::FCMP_UNE:
8670 case FCmpInst::FCMP_ORD:
8671 case FCmpInst::FCMP_UNO:
8672 // Look through the fabs() because it doesn't change anything but the sign.
8673 // fabs(X) == 0.0 --> X == 0.0,
8674 // fabs(X) != 0.0 --> X != 0.0
8675 // isnan(fabs(X)) --> isnan(X)
8676 // !isnan(fabs(X) --> !isnan(X)
8677 return replacePredAndOp0(&I, I.getPredicate(), X);
8678
8679 default:
8680 return nullptr;
8681 }
8682}
8683
8684/// Optimize sqrt(X) compared with zero.
8686 Value *X;
8687 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))
8688 return nullptr;
8689
8690 if (!match(I.getOperand(1), m_PosZeroFP()))
8691 return nullptr;
8692
8693 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8694 I.setPredicate(P);
8695 return IC.replaceOperand(I, 0, X);
8696 };
8697
8698 // Clear ninf flag if sqrt doesn't have it.
8699 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())
8700 I.setHasNoInfs(false);
8701
8702 switch (I.getPredicate()) {
8703 case FCmpInst::FCMP_OLT:
8704 case FCmpInst::FCMP_UGE:
8705 // sqrt(X) < 0.0 --> false
8706 // sqrt(X) u>= 0.0 --> true
8707 llvm_unreachable("fcmp should have simplified");
8708 case FCmpInst::FCMP_ULT:
8709 case FCmpInst::FCMP_ULE:
8710 case FCmpInst::FCMP_OGT:
8711 case FCmpInst::FCMP_OGE:
8712 case FCmpInst::FCMP_OEQ:
8713 case FCmpInst::FCMP_UNE:
8714 // sqrt(X) u< 0.0 --> X u< 0.0
8715 // sqrt(X) u<= 0.0 --> X u<= 0.0
8716 // sqrt(X) > 0.0 --> X > 0.0
8717 // sqrt(X) >= 0.0 --> X >= 0.0
8718 // sqrt(X) == 0.0 --> X == 0.0
8719 // sqrt(X) u!= 0.0 --> X u!= 0.0
8720 return IC.replaceOperand(I, 0, X);
8721
8722 case FCmpInst::FCMP_OLE:
8723 // sqrt(X) <= 0.0 --> X == 0.0
8724 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8725 case FCmpInst::FCMP_UGT:
8726 // sqrt(X) u> 0.0 --> X u!= 0.0
8727 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8728 case FCmpInst::FCMP_UEQ:
8729 // sqrt(X) u== 0.0 --> X u<= 0.0
8730 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8731 case FCmpInst::FCMP_ONE:
8732 // sqrt(X) != 0.0 --> X > 0.0
8733 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8734 case FCmpInst::FCMP_ORD:
8735 // !isnan(sqrt(X)) --> X >= 0.0
8736 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8737 case FCmpInst::FCMP_UNO:
8738 // isnan(sqrt(X)) --> X u< 0.0
8739 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8740 default:
8741 llvm_unreachable("Unexpected predicate!");
8742 }
8743}
8744
8746 CmpInst::Predicate Pred = I.getPredicate();
8747 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8748
8749 // Canonicalize fneg as Op1.
8750 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
8751 std::swap(Op0, Op1);
8752 Pred = I.getSwappedPredicate();
8753 }
8754
8755 if (!match(Op1, m_FNeg(m_Specific(Op0))))
8756 return nullptr;
8757
8758 // Replace the negated operand with 0.0:
8759 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8760 Constant *Zero = ConstantFP::getZero(Op0->getType());
8761 return new FCmpInst(Pred, Op0, Zero, "", &I);
8762}
8763
8765 Constant *RHSC, InstCombinerImpl &CI) {
8766 const CmpInst::Predicate Pred = I.getPredicate();
8767 Value *X = LHSI->getOperand(0);
8768 Value *Y = LHSI->getOperand(1);
8769 switch (Pred) {
8770 default:
8771 break;
8772 case FCmpInst::FCMP_UGT:
8773 case FCmpInst::FCMP_ULT:
8774 case FCmpInst::FCMP_UNE:
8775 case FCmpInst::FCMP_OEQ:
8776 case FCmpInst::FCMP_OGE:
8777 case FCmpInst::FCMP_OLE:
8778 // The optimization is not valid if X and Y are infinities of the same
8779 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8780 // flag then we can assume we do not have that case. Otherwise we might be
8781 // able to prove that either X or Y is not infinity.
8782 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8786 break;
8787
8788 [[fallthrough]];
8789 case FCmpInst::FCMP_OGT:
8790 case FCmpInst::FCMP_OLT:
8791 case FCmpInst::FCMP_ONE:
8792 case FCmpInst::FCMP_UEQ:
8793 case FCmpInst::FCMP_UGE:
8794 case FCmpInst::FCMP_ULE:
8795 // fcmp pred (x - y), 0 --> fcmp pred x, y
8796 if (match(RHSC, m_AnyZeroFP()) &&
8797 I.getFunction()->getDenormalMode(
8798 LHSI->getType()->getScalarType()->getFltSemantics()) ==
8800 CI.replaceOperand(I, 0, X);
8801 CI.replaceOperand(I, 1, Y);
8802 I.setHasNoInfs(LHSI->hasNoInfs());
8803 if (LHSI->hasNoNaNs())
8804 I.setHasNoNaNs(true);
8805 return &I;
8806 }
8807 // fcmp `pred (C - Y), C` -> `fcmp swap(pred), Y, 0`
8808 // where C and Y can't be arbitrary floating-point values.
8809 // For example, with `C = 1.0f` and `Y = 0x1p-149`, `1.0f - Y` rounds back
8810 // to `1.0f`, so the source compare is false while the rewritten compare is
8811 // true.
8812 // We need to make sure (C - Y) never rounds back to C
8813 const APFloat *C;
8814 Value *IntSrc;
8815 if (match(RHSC, m_APFloat(C)) &&
8816 match(LHSI, m_FSub(m_Specific(RHSC), m_IToFP(m_Value(IntSrc)))) &&
8817 C->isNormal()) {
8818 // Requirements on C and Y:
8819 // 1. C is finite, nonzero, normal.
8820 // 2. C shouldn't be too large, that is, ULP(C) <= 1.
8821 // 3. Y must be the form of `[su]itofp`, so the finite nonzero result of Y
8822 // must be integer-valued with an absolute value of at least 1;
8823 // as long as the step size near C does not exceed 1,
8824 // C - Y cannot be rounded back to C when Y != 0.
8825 // 4. If Y = 0, `fcmp pred (C - 0), C` are equivalent to `fcmp swap(pred)
8826 // 0, 0` for ordered and unordered predicates as long as C is finite and
8827 // nonzero.
8828 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
8829 if (MantissaWidth != -1 && ilogb(*C) < MantissaWidth) {
8830 Constant *ZeroC = ConstantFP::getZero(LHSI->getType());
8831 I.setPredicate(I.getSwappedPredicate());
8832 CI.replaceOperand(I, 0, Y);
8833 CI.replaceOperand(I, 1, ZeroC);
8834 return &I;
8835 }
8836 }
8837 break;
8838 }
8839
8840 return nullptr;
8841}
8842
8843/// Fold: fabs(uitofp(a) - uitofp(b)) pred C --> a == b
8844/// where 'pred' is olt, ult, ogt, ugt, oge or uge and C is a positive, Non-NaN
8845/// float when the uitofp casts are exact and C is in the valid range.
8846///
8847/// Since exact uitofp means distinct integers map to distinct floats, the only
8848/// values fabs(uitofp(a) - uitofp(b)) can take are {0.0, 1.0, 2.0, ...}.
8849/// There are no values in the open interval (0, 1), so:
8850/// fabs(...) < C where 0 < C <= 1.0 --> a == b (strict lt: C=1.0 ok)
8851// fabs(..) >= C where C >= 1.0 -> a != b
8852///
8853/// The same logic applies to sitofp.
8855 Value *FAbsArg;
8856 if (!match(I.getOperand(0), m_FAbs(m_Value(FAbsArg))))
8857 return nullptr;
8858
8859 const APFloat *C;
8860 if (!match(I.getOperand(1), PatternMatch::m_FiniteNonZero(C)))
8861 return nullptr;
8862
8863 FCmpInst::Predicate Pred = I.getPredicate();
8864 bool IsStrictLt = Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT;
8865 bool IsLe = Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE;
8866 bool IsStrictGt = Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT;
8867 bool IsGe = Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE;
8868 if (!IsStrictLt && !IsStrictGt && !IsGe)
8869 return nullptr;
8870
8871 APFloat One = APFloat::getOne(C->getSemantics());
8872 APFloat::cmpResult Cmp = C->compare(One);
8873
8874 // For strict-lt (olt/ult): C must be in (0, 1.0] -- C == 1.0 is fine since
8875 // the next possible value after 0.0 is 1.0, and < 1.0 excludes it.
8876 if (IsStrictLt && Cmp == APFloat::cmpGreaterThan)
8877 return nullptr;
8878 if (IsGe && Cmp == APFloat::cmpGreaterThan)
8879 return nullptr;
8880 if (IsLe && Cmp != APFloat::cmpGreaterThan)
8881 return nullptr;
8882 if (IsStrictGt && Cmp != APFloat::cmpLessThan)
8883 return nullptr;
8884
8885 // Match: fsub(uitofp(A), uitofp(B)) where both casts are uitofp or sitofp
8886 Value *A, *B;
8887 bool IsSigned;
8888 if (match(FAbsArg, m_FSub(m_UIToFP(m_Value(A)), m_UIToFP(m_Value(B))))) {
8889 IsSigned = false;
8890 } else if (match(FAbsArg,
8892 IsSigned = true;
8893 } else {
8894 return nullptr;
8895 }
8896
8897 // A and B must have the same integer type
8898 if (A->getType() != B->getType())
8899 return nullptr;
8900
8901 Type *FPTy = FAbsArg->getType();
8902 if (!IC.canBeCastedExactlyIntToFP(A, FPTy, IsSigned, &I) ||
8903 !IC.canBeCastedExactlyIntToFP(B, FPTy, IsSigned, &I))
8904 return nullptr;
8905 ICmpInst::Predicate ResultPred =
8906 IsStrictLt || IsLe ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
8907 return new ICmpInst(ResultPred, A, B);
8908}
8909
8911 InstCombinerImpl &IC) {
8912 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
8913 Type *OpType = LHS->getType();
8914 CmpInst::Predicate Pred = I.getPredicate();
8915
8918
8919 if (!FloorX && !CeilX) {
8922 std::swap(LHS, RHS);
8923 Pred = I.getSwappedPredicate();
8924 }
8925 }
8926
8927 if ((FloorX || CeilX) && FCmpInst::isCommutative(Pred) && LHS->hasOneUse()) {
8928 // fcmp pred floor(x), x => fcmp pred trunc(x), x
8929 // fcmp pred ceil(x), x => fcmp pred trunc(x), x
8930 // where pred is oeq, one, ord, ueq, une, uno.
8931 Value *TruncX = IC.Builder.CreateUnaryIntrinsic(Intrinsic::trunc, RHS);
8932 return new FCmpInst(Pred, TruncX, RHS, "", &I);
8933 }
8934
8935 switch (Pred) {
8936 case FCmpInst::FCMP_OLE:
8937 // fcmp ole floor(x), x => fcmp ord x, 0
8938 if (FloorX)
8940 "", &I);
8941 break;
8942 case FCmpInst::FCMP_OGT:
8943 // fcmp ogt floor(x), x => false
8944 if (FloorX)
8945 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8946 break;
8947 case FCmpInst::FCMP_OGE:
8948 // fcmp oge ceil(x), x => fcmp ord x, 0
8949 if (CeilX)
8951 "", &I);
8952 break;
8953 case FCmpInst::FCMP_OLT:
8954 // fcmp olt ceil(x), x => false
8955 if (CeilX)
8956 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8957 break;
8958 case FCmpInst::FCMP_ULE:
8959 // fcmp ule floor(x), x => true
8960 if (FloorX)
8961 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8962 break;
8963 case FCmpInst::FCMP_UGT:
8964 // fcmp ugt floor(x), x => fcmp uno x, 0
8965 if (FloorX)
8967 "", &I);
8968 break;
8969 case FCmpInst::FCMP_UGE:
8970 // fcmp uge ceil(x), x => true
8971 if (CeilX)
8972 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8973 break;
8974 case FCmpInst::FCMP_ULT:
8975 // fcmp ult ceil(x), x => fcmp uno x, 0
8976 if (CeilX)
8978 "", &I);
8979 break;
8980 default:
8981 break;
8982 }
8983
8984 return nullptr;
8985}
8986
8987/// Returns true if a select that implements a min/max is redundant and
8988/// select result can be replaced with its non-constant operand, e.g.,
8989/// select ( (si/ui-to-fp A) <= C ), C, (si/ui-to-fp A)
8990/// where C is the FP constant equal to the minimum integer value
8991/// representable by A.
8993 Value *B) {
8994 const APFloat *APF;
8995 if (!match(B, m_APFloat(APF)))
8996 return false;
8997
8998 auto *I = dyn_cast<Instruction>(A);
8999 if (!I || !(I->getOpcode() == Instruction::SIToFP ||
9000 I->getOpcode() == Instruction::UIToFP))
9001 return false;
9002
9003 bool IsUnsigned = I->getOpcode() == Instruction::UIToFP;
9004 unsigned BitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
9005 APSInt IntBoundary = (Flavor == SPF_FMAXNUM)
9006 ? APSInt::getMinValue(BitWidth, IsUnsigned)
9007 : APSInt::getMaxValue(BitWidth, IsUnsigned);
9008 APSInt ConvertedInt(BitWidth, IsUnsigned);
9009 bool IsExact;
9011 APF->convertToInteger(ConvertedInt, APFloat::rmTowardZero, &IsExact);
9012 return Status == APFloat::opOK && IsExact && ConvertedInt == IntBoundary;
9013}
9014
9016 bool Changed = false;
9017
9018 /// Orders the operands of the compare so that they are listed from most
9019 /// complex to least complex. This puts constants before unary operators,
9020 /// before binary operators.
9021 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
9022 I.swapOperands();
9023 Changed = true;
9024 }
9025
9026 const CmpInst::Predicate Pred = I.getPredicate();
9027 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
9028 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
9029 SQ.getWithInstruction(&I)))
9030 return replaceInstUsesWith(I, V);
9031
9032 // Simplify 'fcmp pred X, X'
9033 Type *OpType = Op0->getType();
9034 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
9035 if (Op0 == Op1) {
9036 switch (Pred) {
9037 default:
9038 break;
9039 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
9040 case FCmpInst::FCMP_ULT: // True if unordered or less than
9041 case FCmpInst::FCMP_UGT: // True if unordered or greater than
9042 case FCmpInst::FCMP_UNE: // True if unordered or not equal
9043 // Canonicalize these to be 'fcmp uno %X, 0.0'.
9044 I.setPredicate(FCmpInst::FCMP_UNO);
9045 I.setOperand(1, Constant::getNullValue(OpType));
9046 return &I;
9047
9048 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
9049 case FCmpInst::FCMP_OEQ: // True if ordered and equal
9050 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
9051 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
9052 // Canonicalize these to be 'fcmp ord %X, 0.0'.
9053 I.setPredicate(FCmpInst::FCMP_ORD);
9054 I.setOperand(1, Constant::getNullValue(OpType));
9055 return &I;
9056 }
9057 }
9058
9059 if (I.isCommutative()) {
9060 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
9061 replaceOperand(I, 0, Pair->first);
9062 replaceOperand(I, 1, Pair->second);
9063 return &I;
9064 }
9065 }
9066
9067 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
9068 // then canonicalize the operand to 0.0.
9069 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
9070 if (!match(Op0, m_PosZeroFP()) &&
9071 isKnownNeverNaN(Op0, getSimplifyQuery().getWithInstruction(&I)))
9072 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
9073
9074 if (!match(Op1, m_PosZeroFP()) &&
9075 isKnownNeverNaN(Op1, getSimplifyQuery().getWithInstruction(&I)))
9076 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
9077 }
9078
9079 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
9080 Value *X, *Y;
9081 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
9082 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
9083
9085 return R;
9086
9087 // Test if the FCmpInst instruction is used exclusively by a select as
9088 // part of a minimum or maximum operation. If so, refrain from doing
9089 // any other folding. This helps out other analyses which understand
9090 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
9091 // and CodeGen. And in this case, at least one of the comparison
9092 // operands has at least one user besides the compare (the select),
9093 // which would often largely negate the benefit of folding anyway.
9094 if (I.hasOneUse())
9095 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
9096 Value *A, *B;
9098 bool IsRedundantMinMaxClamp =
9099 (SPR.Flavor == SPF_FMAXNUM || SPR.Flavor == SPF_FMINNUM) &&
9101 if (SPR.Flavor != SPF_UNKNOWN && !IsRedundantMinMaxClamp)
9102 return nullptr;
9103 }
9104
9105 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
9106 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
9107 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
9108 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
9109
9110 // Canonicalize:
9111 // fcmp olt X, +inf -> fcmp one X, +inf
9112 // fcmp ole X, +inf -> fcmp ord X, 0
9113 // fcmp ogt X, +inf -> false
9114 // fcmp oge X, +inf -> fcmp oeq X, +inf
9115 // fcmp ult X, +inf -> fcmp une X, +inf
9116 // fcmp ule X, +inf -> true
9117 // fcmp ugt X, +inf -> fcmp uno X, 0
9118 // fcmp uge X, +inf -> fcmp ueq X, +inf
9119 // fcmp olt X, -inf -> false
9120 // fcmp ole X, -inf -> fcmp oeq X, -inf
9121 // fcmp ogt X, -inf -> fcmp one X, -inf
9122 // fcmp oge X, -inf -> fcmp ord X, 0
9123 // fcmp ult X, -inf -> fcmp uno X, 0
9124 // fcmp ule X, -inf -> fcmp ueq X, -inf
9125 // fcmp ugt X, -inf -> fcmp une X, -inf
9126 // fcmp uge X, -inf -> true
9127 const APFloat *C;
9128 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
9129 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
9130 default:
9131 break;
9132 case FCmpInst::FCMP_ORD:
9133 case FCmpInst::FCMP_UNO:
9136 case FCmpInst::FCMP_OGT:
9137 case FCmpInst::FCMP_ULE:
9138 llvm_unreachable("Should be simplified by InstSimplify");
9139 case FCmpInst::FCMP_OLT:
9140 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
9141 case FCmpInst::FCMP_OLE:
9142 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
9143 "", &I);
9144 case FCmpInst::FCMP_OGE:
9145 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
9146 case FCmpInst::FCMP_ULT:
9147 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
9148 case FCmpInst::FCMP_UGT:
9149 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
9150 "", &I);
9151 case FCmpInst::FCMP_UGE:
9152 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
9153 }
9154 }
9155
9156 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
9157 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
9158 if (match(Op1, m_PosZeroFP()) &&
9160 !F.getDenormalMode(Op1->getType()->getScalarType()->getFltSemantics())
9161 .inputsMayBeZero()) {
9163 if (Pred == FCmpInst::FCMP_OEQ)
9164 IntPred = ICmpInst::ICMP_EQ;
9165 else if (Pred == FCmpInst::FCMP_UNE)
9166 IntPred = ICmpInst::ICMP_NE;
9167
9168 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
9169 Type *IntTy = X->getType();
9170 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
9171 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
9172 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
9173 }
9174 }
9175
9176 // Handle fcmp with instruction LHS and constant RHS.
9177 Instruction *LHSI;
9178 Constant *RHSC;
9179 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
9180 switch (LHSI->getOpcode()) {
9181 case Instruction::Select:
9182 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
9183 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
9185 return replaceOperand(I, 0, X);
9187 return NV;
9188 break;
9189 case Instruction::FSub:
9190 if (LHSI->hasOneUse())
9191 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
9192 return NV;
9193 break;
9194 case Instruction::PHI:
9195 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
9196 return NV;
9197 break;
9198 case Instruction::SIToFP:
9199 case Instruction::UIToFP:
9200 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
9201 return NV;
9202 break;
9203 case Instruction::FDiv:
9204 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
9205 return NV;
9206 break;
9207 case Instruction::Load:
9208 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
9209 if (Instruction *Res =
9211 return Res;
9212 break;
9213 case Instruction::FPTrunc:
9214 if (Instruction *NV = foldFCmpFpTrunc(I, *LHSI, *RHSC))
9215 return NV;
9216 break;
9217 }
9218 }
9219
9220 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
9221 return R;
9222
9223 if (Instruction *R = foldFCmpFAbsFSubIntToFP(I, *this))
9224 return R;
9225
9226 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))
9227 return R;
9228
9229 if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this))
9230 return R;
9231
9233 return R;
9234
9235 if (match(Op0, m_FNeg(m_Value(X)))) {
9236 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
9237 Constant *C;
9238 if (match(Op1, m_Constant(C)))
9239 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
9240 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
9241 }
9242
9243 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
9244 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
9245 return new FCmpInst(Pred, X, Op1, "", &I);
9246
9247 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
9248 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
9249 return new FCmpInst(Pred, Op0, Y, "", &I);
9250
9251 // fcmp ord/uno (fptrunc X), (fptrunc Y) -> fcmp ord/uno X, Y
9252 if ((Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) &&
9253 match(Op0, m_FPTrunc(m_Value(X))) && match(Op1, m_FPTrunc(m_Value(Y))) &&
9254 X->getType() == Y->getType())
9255 return new FCmpInst(Pred, X, Y, "", &I);
9256
9257 if (match(Op0, m_FPExt(m_Value(X)))) {
9258 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
9259 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
9260 return new FCmpInst(Pred, X, Y, "", &I);
9261
9262 const APFloat *C;
9263 if (match(Op1, m_APFloat(C))) {
9264 const fltSemantics &FPSem =
9265 X->getType()->getScalarType()->getFltSemantics();
9266 bool Lossy;
9267 APFloat TruncC = *C;
9269
9270 if (Lossy) {
9271 // X can't possibly equal the higher-precision constant, so reduce any
9272 // equality comparison.
9273 // TODO: Other predicates can be handled via getFCmpCode().
9274 switch (Pred) {
9275 case FCmpInst::FCMP_OEQ:
9276 // X is ordered and equal to an impossible constant --> false
9277 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
9278 case FCmpInst::FCMP_ONE:
9279 // X is ordered and not equal to an impossible constant --> ordered
9280 return new FCmpInst(FCmpInst::FCMP_ORD, X,
9281 ConstantFP::getZero(X->getType()));
9282 case FCmpInst::FCMP_UEQ:
9283 // X is unordered or equal to an impossible constant --> unordered
9284 return new FCmpInst(FCmpInst::FCMP_UNO, X,
9285 ConstantFP::getZero(X->getType()));
9286 case FCmpInst::FCMP_UNE:
9287 // X is unordered or not equal to an impossible constant --> true
9288 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
9289 default:
9290 break;
9291 }
9292 }
9293
9294 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
9295 // Avoid lossy conversions and denormals.
9296 // Zero is a special case that's OK to convert.
9297 APFloat Fabs = TruncC;
9298 Fabs.clearSign();
9299 if (!Lossy &&
9300 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
9301 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
9302 return new FCmpInst(Pred, X, NewC, "", &I);
9303 }
9304 }
9305 }
9306
9307 // Convert a sign-bit test of an FP value into a cast and integer compare.
9308 // TODO: Simplify if the copysign constant is 0.0 or NaN.
9309 // TODO: Handle non-zero compare constants.
9310 // TODO: Handle other predicates.
9312 m_Value(X)))) &&
9313 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
9314 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
9315 if (auto *VecTy = dyn_cast<VectorType>(OpType))
9316 IntType = VectorType::get(IntType, VecTy->getElementCount());
9317
9318 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
9319 if (Pred == FCmpInst::FCMP_OLT) {
9320 Value *IntX = Builder.CreateBitCast(X, IntType);
9321 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
9322 ConstantInt::getNullValue(IntType));
9323 }
9324 }
9325
9326 {
9327 Value *CanonLHS = nullptr;
9329 // (canonicalize(x) == x) => (x == x)
9330 if (CanonLHS == Op1)
9331 return new FCmpInst(Pred, Op1, Op1, "", &I);
9332
9333 Value *CanonRHS = nullptr;
9335 // (x == canonicalize(x)) => (x == x)
9336 if (CanonRHS == Op0)
9337 return new FCmpInst(Pred, Op0, Op0, "", &I);
9338
9339 // (canonicalize(x) == canonicalize(y)) => (x == y)
9340 if (CanonLHS && CanonRHS)
9341 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
9342 }
9343
9344 if (I.getType()->isVectorTy())
9345 if (Instruction *Res = foldVectorCmp(I, Builder))
9346 return Res;
9347
9348 return Changed ? &I : nullptr;
9349}
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
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
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 bool isMinMaxCmpSelectEliminable(SelectPatternFlavor Flavor, Value *A, Value *B)
Returns true if a select that implements a min/max is redundant and select result can be replaced wit...
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 * foldICmpOfVectorReduce(ICmpInst &I, const DataLayout &DL, IRBuilderBase &Builder)
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 Instruction * foldFCmpFAbsFSubIntToFP(FCmpInst &I, InstCombinerImpl &IC)
Fold: fabs(uitofp(a) - uitofp(b)) pred C --> a == b where 'pred' is olt, ult, ogt,...
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 SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
Value * RHS
Value * LHS
BinaryOperator * Mul
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5899
void clearSign()
Definition APFloat.h:1357
bool isNaN() const
Definition APFloat.h:1536
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
Definition APFloat.h:1147
bool isZero() const
Definition APFloat.h:1534
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.h:1217
APInt bitcastToAPInt() const
Definition APFloat.h:1430
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1197
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1391
opStatus next(bool nextDown)
Definition APFloat.h:1313
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1157
LLVM_ABI FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition APFloat.cpp:5828
opStatus roundToIntegral(roundingMode RM)
Definition APFloat.h:1307
bool isInfinity() const
Definition APFloat.h:1535
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1616
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:1810
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:1055
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:1563
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1535
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:968
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:1353
APInt abs() const
Get the absolute value.
Definition APInt.h:1818
unsigned ceilLogBase2() const
Definition APInt.h:1787
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1208
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:2000
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
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:1511
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
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:1980
bool eq(const APInt &RHS) const
Equality comparison.
Definition APInt.h:1086
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1687
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1987
void negate()
Negate this APInt in place.
Definition APInt.h:1491
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1662
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1621
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:1475
unsigned countl_one() const
Count the number of leading one bits.
Definition APInt.h:1638
unsigned logBase2() const
Definition APInt.h:1784
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:834
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:1157
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
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:1244
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1993
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:858
unsigned countr_one() const
Count the number of trailing one bits.
Definition APInt.h:1679
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
static APSInt getMinValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the minimum integer value with the given bit width and signedness.
Definition APSInt.h:310
static APSInt getMaxValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the maximum integer value with the given bit width and signedness.
Definition APSInt.h:302
an instruction to allocate memory on the stack
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; assumes that the block is well-formed.
Definition BasicBlock.h:237
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.
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:986
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.
Conditional Branch instruction.
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:269
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V, bool ImplicitTrunc=false)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:135
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
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 bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
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:84
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
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 isCommutative(Predicate Pred)
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:82
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
LLVM_ABI CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1592
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:2484
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1614
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition IRBuilder.h:544
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2858
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 * foldCmpSelectOfConstants(CmpInst &I)
Fold fcmp/icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2) where all true/false values are cons...
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.
bool canBeCastedExactlyIntToFP(Value *V, Type *FPTy, bool IsSigned, const Instruction *CxtI=nullptr) const
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:354
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:57
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
bool contains(const_arg_type key) const
Check if the SetVector contains the given key.
Definition SetVector.h:252
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
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:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:290
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:284
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition Type.h:167
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:201
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:130
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:236
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:310
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
Definition Type.cpp:241
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:110
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:212
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
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:154
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:709
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:318
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:399
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:2815
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:2833
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
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)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
match_combine_and< Ty... > m_CombineAnd(const Ty &...Ps)
Combine pattern matchers matching all of Ps patterns.
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)
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)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(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)
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.
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
match_deferred< 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()...
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< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
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.
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.
auto m_BinOp()
Match an arbitrary binary operation and ignore it.
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.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, UIToFPInst >, CastInst_match< OpTy, SIToFPInst > > m_IToFP(const OpTy &Op)
auto m_Value()
Match an arbitrary value and ignore it.
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)
auto m_Constant()
Match an arbitrary Constant and ignore it.
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)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
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.
SelectLike_match< CondTy, LTy, RTy > m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC)
Matches a value that behaves like a boolean-controlled select, i.e.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
auto m_MaxOrMin(const LHS &L, const RHS &R)
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.
cstfp_pred_ty< is_finitenonzero > m_FiniteNonZero()
Match a finite non-zero FP constant.
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)
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.
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.
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.
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:557
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:830
@ 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.
cl::opt< bool > ProfcheckDisableMetadataFixes
Definition LoopInfo.cpp:60
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:1738
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:633
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:1631
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:263
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_FMAXNUM
Floating point minnum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
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:910
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:1640
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:1752
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:2018
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:2165
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:27
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.
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, const SimplifyQuery &SQ, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:876
#define NC
Definition regutils.h:42
Value * materialize(InstCombiner::BuilderTy &Builder) const
static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV, Instruction *MDFrom)
static OffsetResult value(Value *V)
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:106
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:78
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:256
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:288
APInt getSignedMaxValue() const
Return the maximal signed value possible given these KnownBits.
Definition KnownBits.h:152
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
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:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:130
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:112
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition KnownBits.h:300
APInt getSignedMinValue() const
Return the minimal signed value possible given these KnownBits.
Definition KnownBits.h:136
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition KnownBits.h:58
Linear expression BasePtr + Index * Scale + Offset.
Definition Loads.h:211
GEPNoWrapFlags Flags
Definition Loads.h:216
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:334
Capture information for a specific Use.