LLVM 22.0.0git
InstCombineCompares.cpp
Go to the documentation of this file.
1//===- InstCombineCompares.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the visitICmp and visitFCmp functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/APFloat.h"
15#include "llvm/ADT/APSInt.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/Statistic.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/InstrTypes.h"
33#include <bitset>
34
35using namespace llvm;
36using namespace PatternMatch;
37
38#define DEBUG_TYPE "instcombine"
39
40// How many times is a select replaced by one of its operands?
41STATISTIC(NumSel, "Number of select opts");
42
43/// Compute Result = In1+In2, returning true if the result overflowed for this
44/// type.
45static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
46 bool IsSigned = false) {
47 bool Overflow;
48 if (IsSigned)
49 Result = In1.sadd_ov(In2, Overflow);
50 else
51 Result = In1.uadd_ov(In2, Overflow);
52
53 return Overflow;
54}
55
56/// Compute Result = In1-In2, returning true if the result overflowed for this
57/// type.
58static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
59 bool IsSigned = false) {
60 bool Overflow;
61 if (IsSigned)
62 Result = In1.ssub_ov(In2, Overflow);
63 else
64 Result = In1.usub_ov(In2, Overflow);
65
66 return Overflow;
67}
68
69/// Given an icmp instruction, return true if any use of this comparison is a
70/// branch on sign bit comparison.
71static bool hasBranchUse(ICmpInst &I) {
72 for (auto *U : I.users())
73 if (isa<BranchInst>(U))
74 return true;
75 return false;
76}
77
78/// Returns true if the exploded icmp can be expressed as a signed comparison
79/// to zero and updates the predicate accordingly.
80/// The signedness of the comparison is preserved.
81/// TODO: Refactor with decomposeBitTestICmp()?
82static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
83 if (!ICmpInst::isSigned(Pred))
84 return false;
85
86 if (C.isZero())
87 return ICmpInst::isRelational(Pred);
88
89 if (C.isOne()) {
90 if (Pred == ICmpInst::ICMP_SLT) {
91 Pred = ICmpInst::ICMP_SLE;
92 return true;
93 }
94 } else if (C.isAllOnes()) {
95 if (Pred == ICmpInst::ICMP_SGT) {
96 Pred = ICmpInst::ICMP_SGE;
97 return true;
98 }
99 }
100
101 return false;
102}
103
104/// This is called when we see this pattern:
105/// cmp pred (load (gep GV, ...)), cmpcst
106/// where GV is a global variable with a constant initializer. Try to simplify
107/// this into some simple computation that does not need the load. For example
108/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
109///
110/// If AndCst is non-null, then the loaded value is masked with that constant
111/// before doing the comparison. This handles cases like "A[i]&4 == 0".
114 ConstantInt *AndCst) {
115 if (LI->isVolatile() || LI->getType() != GEP->getResultElementType() ||
116 !GV->getValueType()->isArrayTy() || !GV->isConstant() ||
118 return nullptr;
119
120 Type *GEPSrcEltTy = GEP->getSourceElementType();
121 if (GEPSrcEltTy->isArrayTy())
122 GEPSrcEltTy = GEPSrcEltTy->getArrayElementType();
123 if (GV->getValueType()->getArrayElementType() != GEPSrcEltTy)
124 return nullptr;
125
127 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
128 return nullptr;
129
130 uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
131 // Don't blow up on huge arrays.
132 if (ArrayElementCount > MaxArraySizeForCombine)
133 return nullptr;
134
135 // There are many forms of this optimization we can handle, for now, just do
136 // the simple index into a single-dimensional array or elements of equal size.
137 //
138 // Require: GEP [n x i8] GV, 0, Idx {{, constant indices}}
139 // Or: GEP i8 GV, Idx
140
141 unsigned GEPIdxOp = 1;
142 if (GEP->getSourceElementType()->isArrayTy()) {
143 GEPIdxOp = 2;
144 if (!match(GEP->getOperand(1), m_ZeroInt()))
145 return nullptr;
146 }
147 if (GEP->getNumOperands() < GEPIdxOp + 1 ||
148 isa<Constant>(GEP->getOperand(GEPIdxOp)))
149 return nullptr;
150
151 // Check that indices after the variable are constants and in-range for the
152 // type they index. Collect the indices. This is typically for arrays of
153 // structs.
154 SmallVector<unsigned, 4> LaterIndices;
155
156 Type *EltTy = Init->getType()->getArrayElementType();
157 for (unsigned i = GEPIdxOp + 1, e = GEP->getNumOperands(); i != e; ++i) {
158 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
159 if (!Idx)
160 return nullptr; // Variable index.
161
162 uint64_t IdxVal = Idx->getZExtValue();
163 if ((unsigned)IdxVal != IdxVal)
164 return nullptr; // Too large array index.
165
166 if (StructType *STy = dyn_cast<StructType>(EltTy))
167 EltTy = STy->getElementType(IdxVal);
168 else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
169 if (IdxVal >= ATy->getNumElements())
170 return nullptr;
171 EltTy = ATy->getElementType();
172 } else {
173 return nullptr; // Unknown type.
174 }
175
176 LaterIndices.push_back(IdxVal);
177 }
178
179 Value *Idx = GEP->getOperand(GEPIdxOp);
180 // If the index type is non-canonical, wait for it to be canonicalized.
181 if (Idx->getType() != DL.getIndexType(GEP->getType()))
182 return nullptr;
183
184 enum { Overdefined = -3, Undefined = -2 };
185
186 // Variables for our state machines.
187
188 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
189 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
190 // and 87 is the second (and last) index. FirstTrueElement is -2 when
191 // undefined, otherwise set to the first true element. SecondTrueElement is
192 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
193 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
194
195 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
196 // form "i != 47 & i != 87". Same state transitions as for true elements.
197 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
198
199 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
200 /// define a state machine that triggers for ranges of values that the index
201 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
202 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
203 /// index in the range (inclusive). We use -2 for undefined here because we
204 /// use relative comparisons and don't want 0-1 to match -1.
205 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
206
207 // MagicBitvector - This is a magic bitvector where we set a bit if the
208 // comparison is true for element 'i'. If there are 64 elements or less in
209 // the array, this will fully represent all the comparison results.
210 uint64_t MagicBitvector = 0;
211
212 // Scan the array and see if one of our patterns matches.
213 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
214 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
215 Constant *Elt = Init->getAggregateElement(i);
216 if (!Elt)
217 return nullptr;
218
219 // If this is indexing an array of structures, get the structure element.
220 if (!LaterIndices.empty()) {
221 Elt = ConstantFoldExtractValueInstruction(Elt, LaterIndices);
222 if (!Elt)
223 return nullptr;
224 }
225
226 // If the element is masked, handle it.
227 if (AndCst) {
228 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);
229 if (!Elt)
230 return nullptr;
231 }
232
233 // Find out if the comparison would be true or false for the i'th element.
235 CompareRHS, DL, &TLI);
236 if (!C)
237 return nullptr;
238
239 // If the result is undef for this element, ignore it.
240 if (isa<UndefValue>(C)) {
241 // Extend range state machines to cover this element in case there is an
242 // undef in the middle of the range.
243 if (TrueRangeEnd == (int)i - 1)
244 TrueRangeEnd = i;
245 if (FalseRangeEnd == (int)i - 1)
246 FalseRangeEnd = i;
247 continue;
248 }
249
250 // If we can't compute the result for any of the elements, we have to give
251 // up evaluating the entire conditional.
252 if (!isa<ConstantInt>(C))
253 return nullptr;
254
255 // Otherwise, we know if the comparison is true or false for this element,
256 // update our state machines.
257 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
258
259 // State machine for single/double/range index comparison.
260 if (IsTrueForElt) {
261 // Update the TrueElement state machine.
262 if (FirstTrueElement == Undefined)
263 FirstTrueElement = TrueRangeEnd = i; // First true element.
264 else {
265 // Update double-compare state machine.
266 if (SecondTrueElement == Undefined)
267 SecondTrueElement = i;
268 else
269 SecondTrueElement = Overdefined;
270
271 // Update range state machine.
272 if (TrueRangeEnd == (int)i - 1)
273 TrueRangeEnd = i;
274 else
275 TrueRangeEnd = Overdefined;
276 }
277 } else {
278 // Update the FalseElement state machine.
279 if (FirstFalseElement == Undefined)
280 FirstFalseElement = FalseRangeEnd = i; // First false element.
281 else {
282 // Update double-compare state machine.
283 if (SecondFalseElement == Undefined)
284 SecondFalseElement = i;
285 else
286 SecondFalseElement = Overdefined;
287
288 // Update range state machine.
289 if (FalseRangeEnd == (int)i - 1)
290 FalseRangeEnd = i;
291 else
292 FalseRangeEnd = Overdefined;
293 }
294 }
295
296 // If this element is in range, update our magic bitvector.
297 if (i < 64 && IsTrueForElt)
298 MagicBitvector |= 1ULL << i;
299
300 // If all of our states become overdefined, bail out early. Since the
301 // predicate is expensive, only check it every 8 elements. This is only
302 // really useful for really huge arrays.
303 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
304 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
305 FalseRangeEnd == Overdefined)
306 return nullptr;
307 }
308
309 // Now that we've scanned the entire array, emit our new comparison(s). We
310 // order the state machines in complexity of the generated code.
311
312 // If inbounds keyword is not present, Idx * ElementSize can overflow.
313 // Let's assume that ElementSize is 2 and the wanted value is at offset 0.
314 // Then, there are two possible values for Idx to match offset 0:
315 // 0x00..00, 0x80..00.
316 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
317 // comparison is false if Idx was 0x80..00.
318 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.
319 unsigned ElementSize =
320 DL.getTypeAllocSize(Init->getType()->getArrayElementType());
321 auto MaskIdx = [&](Value *Idx) {
322 if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) {
323 Value *Mask = Constant::getAllOnesValue(Idx->getType());
324 Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize));
325 Idx = Builder.CreateAnd(Idx, Mask);
326 }
327 return Idx;
328 };
329
330 // If the comparison is only true for one or two elements, emit direct
331 // comparisons.
332 if (SecondTrueElement != Overdefined) {
333 Idx = MaskIdx(Idx);
334 // None true -> false.
335 if (FirstTrueElement == Undefined)
336 return replaceInstUsesWith(ICI, Builder.getFalse());
337
338 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
339
340 // True for one element -> 'i == 47'.
341 if (SecondTrueElement == Undefined)
342 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
343
344 // True for two elements -> 'i == 47 | i == 72'.
345 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx);
346 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
347 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx);
348 return BinaryOperator::CreateOr(C1, C2);
349 }
350
351 // If the comparison is only false for one or two elements, emit direct
352 // comparisons.
353 if (SecondFalseElement != Overdefined) {
354 Idx = MaskIdx(Idx);
355 // None false -> true.
356 if (FirstFalseElement == Undefined)
357 return replaceInstUsesWith(ICI, Builder.getTrue());
358
359 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
360
361 // False for one element -> 'i != 47'.
362 if (SecondFalseElement == Undefined)
363 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
364
365 // False for two elements -> 'i != 47 & i != 72'.
366 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx);
367 Value *SecondFalseIdx =
368 ConstantInt::get(Idx->getType(), SecondFalseElement);
369 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx);
370 return BinaryOperator::CreateAnd(C1, C2);
371 }
372
373 // If the comparison can be replaced with a range comparison for the elements
374 // where it is true, emit the range check.
375 if (TrueRangeEnd != Overdefined) {
376 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
377 Idx = MaskIdx(Idx);
378
379 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
380 if (FirstTrueElement) {
381 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
382 Idx = Builder.CreateAdd(Idx, Offs);
383 }
384
385 Value *End =
386 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1);
387 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
388 }
389
390 // False range check.
391 if (FalseRangeEnd != Overdefined) {
392 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
393 Idx = MaskIdx(Idx);
394 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
395 if (FirstFalseElement) {
396 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
397 Idx = Builder.CreateAdd(Idx, Offs);
398 }
399
400 Value *End =
401 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement);
402 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
403 }
404
405 // If a magic bitvector captures the entire comparison state
406 // of this load, replace it with computation that does:
407 // ((magic_cst >> i) & 1) != 0
408 {
409 Type *Ty = nullptr;
410
411 // Look for an appropriate type:
412 // - The type of Idx if the magic fits
413 // - The smallest fitting legal type
414 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
415 Ty = Idx->getType();
416 else
417 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
418
419 if (Ty) {
420 Idx = MaskIdx(Idx);
421 Value *V = Builder.CreateIntCast(Idx, Ty, false);
422 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
423 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V);
424 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
425 }
426 }
427
428 return nullptr;
429}
430
431/// Returns true if we can rewrite Start as a GEP with pointer Base
432/// and some integer offset. The nodes that need to be re-written
433/// for this transformation will be added to Explored.
435 const DataLayout &DL,
436 SetVector<Value *> &Explored) {
437 SmallVector<Value *, 16> WorkList(1, Start);
438 Explored.insert(Base);
439
440 // The following traversal gives us an order which can be used
441 // when doing the final transformation. Since in the final
442 // transformation we create the PHI replacement instructions first,
443 // we don't have to get them in any particular order.
444 //
445 // However, for other instructions we will have to traverse the
446 // operands of an instruction first, which means that we have to
447 // do a post-order traversal.
448 while (!WorkList.empty()) {
450
451 while (!WorkList.empty()) {
452 if (Explored.size() >= 100)
453 return false;
454
455 Value *V = WorkList.back();
456
457 if (Explored.contains(V)) {
458 WorkList.pop_back();
459 continue;
460 }
461
462 if (!isa<GetElementPtrInst>(V) && !isa<PHINode>(V))
463 // We've found some value that we can't explore which is different from
464 // the base. Therefore we can't do this transformation.
465 return false;
466
467 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
468 // Only allow inbounds GEPs with at most one variable offset.
469 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); };
470 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1)
471 return false;
472
473 NW = NW.intersectForOffsetAdd(GEP->getNoWrapFlags());
474 if (!Explored.contains(GEP->getOperand(0)))
475 WorkList.push_back(GEP->getOperand(0));
476 }
477
478 if (WorkList.back() == V) {
479 WorkList.pop_back();
480 // We've finished visiting this node, mark it as such.
481 Explored.insert(V);
482 }
483
484 if (auto *PN = dyn_cast<PHINode>(V)) {
485 // We cannot transform PHIs on unsplittable basic blocks.
486 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
487 return false;
488 Explored.insert(PN);
489 PHIs.insert(PN);
490 }
491 }
492
493 // Explore the PHI nodes further.
494 for (auto *PN : PHIs)
495 for (Value *Op : PN->incoming_values())
496 if (!Explored.contains(Op))
497 WorkList.push_back(Op);
498 }
499
500 // Make sure that we can do this. Since we can't insert GEPs in a basic
501 // block before a PHI node, we can't easily do this transformation if
502 // we have PHI node users of transformed instructions.
503 for (Value *Val : Explored) {
504 for (Value *Use : Val->uses()) {
505
506 auto *PHI = dyn_cast<PHINode>(Use);
507 auto *Inst = dyn_cast<Instruction>(Val);
508
509 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
510 !Explored.contains(PHI))
511 continue;
512
513 if (PHI->getParent() == Inst->getParent())
514 return false;
515 }
516 }
517 return true;
518}
519
520// Sets the appropriate insert point on Builder where we can add
521// a replacement Instruction for V (if that is possible).
522static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
523 bool Before = true) {
524 if (auto *PHI = dyn_cast<PHINode>(V)) {
525 BasicBlock *Parent = PHI->getParent();
526 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
527 return;
528 }
529 if (auto *I = dyn_cast<Instruction>(V)) {
530 if (!Before)
531 I = &*std::next(I->getIterator());
532 Builder.SetInsertPoint(I);
533 return;
534 }
535 if (auto *A = dyn_cast<Argument>(V)) {
536 // Set the insertion point in the entry block.
537 BasicBlock &Entry = A->getParent()->getEntryBlock();
538 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt());
539 return;
540 }
541 // Otherwise, this is a constant and we don't need to set a new
542 // insertion point.
543 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
544}
545
546/// Returns a re-written value of Start as an indexed GEP using Base as a
547/// pointer.
549 const DataLayout &DL,
550 SetVector<Value *> &Explored,
551 InstCombiner &IC) {
552 // Perform all the substitutions. This is a bit tricky because we can
553 // have cycles in our use-def chains.
554 // 1. Create the PHI nodes without any incoming values.
555 // 2. Create all the other values.
556 // 3. Add the edges for the PHI nodes.
557 // 4. Emit GEPs to get the original pointers.
558 // 5. Remove the original instructions.
559 Type *IndexType = IntegerType::get(
560 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType()));
561
563 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
564
565 // Create the new PHI nodes, without adding any incoming values.
566 for (Value *Val : Explored) {
567 if (Val == Base)
568 continue;
569 // Create empty phi nodes. This avoids cyclic dependencies when creating
570 // the remaining instructions.
571 if (auto *PHI = dyn_cast<PHINode>(Val))
572 NewInsts[PHI] =
573 PHINode::Create(IndexType, PHI->getNumIncomingValues(),
574 PHI->getName() + ".idx", PHI->getIterator());
575 }
576 IRBuilder<> Builder(Base->getContext());
577
578 // Create all the other instructions.
579 for (Value *Val : Explored) {
580 if (NewInsts.contains(Val))
581 continue;
582
583 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
584 setInsertionPoint(Builder, GEP);
585 Value *Op = NewInsts[GEP->getOperand(0)];
586 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);
587 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero())
588 NewInsts[GEP] = OffsetV;
589 else
590 NewInsts[GEP] = Builder.CreateAdd(
591 Op, OffsetV, GEP->getOperand(0)->getName() + ".add",
592 /*NUW=*/NW.hasNoUnsignedWrap(),
593 /*NSW=*/NW.hasNoUnsignedSignedWrap());
594 continue;
595 }
596 if (isa<PHINode>(Val))
597 continue;
598
599 llvm_unreachable("Unexpected instruction type");
600 }
601
602 // Add the incoming values to the PHI nodes.
603 for (Value *Val : Explored) {
604 if (Val == Base)
605 continue;
606 // All the instructions have been created, we can now add edges to the
607 // phi nodes.
608 if (auto *PHI = dyn_cast<PHINode>(Val)) {
609 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
610 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
611 Value *NewIncoming = PHI->getIncomingValue(I);
612
613 auto It = NewInsts.find(NewIncoming);
614 if (It != NewInsts.end())
615 NewIncoming = It->second;
616
617 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
618 }
619 }
620 }
621
622 for (Value *Val : Explored) {
623 if (Val == Base)
624 continue;
625
626 setInsertionPoint(Builder, Val, false);
627 // Create GEP for external users.
628 Value *NewVal = Builder.CreateGEP(Builder.getInt8Ty(), Base, NewInsts[Val],
629 Val->getName() + ".ptr", NW);
630 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal);
631 // Add old instruction to worklist for DCE. We don't directly remove it
632 // here because the original compare is one of the users.
633 IC.addToWorklist(cast<Instruction>(Val));
634 }
635
636 return NewInsts[Start];
637}
638
639/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
640/// We can look through PHIs, GEPs and casts in order to determine a common base
641/// between GEPLHS and RHS.
644 const DataLayout &DL,
645 InstCombiner &IC) {
646 // FIXME: Support vector of pointers.
647 if (GEPLHS->getType()->isVectorTy())
648 return nullptr;
649
650 if (!GEPLHS->hasAllConstantIndices())
651 return nullptr;
652
653 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
654 Value *PtrBase =
656 /*AllowNonInbounds*/ false);
657
658 // Bail if we looked through addrspacecast.
659 if (PtrBase->getType() != GEPLHS->getType())
660 return nullptr;
661
662 // The set of nodes that will take part in this transformation.
663 SetVector<Value *> Nodes;
664 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
665 if (!canRewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes))
666 return nullptr;
667
668 // We know we can re-write this as
669 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
670 // Since we've only looked through inbouds GEPs we know that we
671 // can't have overflow on either side. We can therefore re-write
672 // this as:
673 // OFFSET1 cmp OFFSET2
674 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes, IC);
675
676 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
677 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
678 // offset. Since Index is the offset of LHS to the base pointer, we will now
679 // compare the offsets instead of comparing the pointers.
681 IC.Builder.getInt(Offset), NewRHS);
682}
683
684/// Fold comparisons between a GEP instruction and something else. At this point
685/// we know that the GEP is on the LHS of the comparison.
688 // Don't transform signed compares of GEPs into index compares. Even if the
689 // GEP is inbounds, the final add of the base pointer can have signed overflow
690 // and would change the result of the icmp.
691 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
692 // the maximum signed value for the pointer type.
694 return nullptr;
695
696 // Look through bitcasts and addrspacecasts. We do not however want to remove
697 // 0 GEPs.
698 if (!isa<GetElementPtrInst>(RHS))
700
701 auto CanFold = [Cond](GEPNoWrapFlags NW) {
703 return true;
704
705 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags.
707 return NW != GEPNoWrapFlags::none();
708 };
709
710 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) {
711 if (!NW.hasNoUnsignedWrap()) {
712 // Convert signed to unsigned comparison.
713 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Op1, Op2);
714 }
715
716 auto *I = new ICmpInst(Cond, Op1, Op2);
717 I->setSameSign(NW.hasNoUnsignedSignedWrap());
718 return I;
719 };
720
722 if (Base.Ptr == RHS && CanFold(Base.LHSNW) && !Base.isExpensive()) {
723 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
724 Type *IdxTy = DL.getIndexType(GEPLHS->getType());
725 Value *Offset =
726 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);
727 return NewICmp(Base.LHSNW, Offset,
728 Constant::getNullValue(Offset->getType()));
729 }
730
731 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
732 isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&
733 !NullPointerIsDefined(I.getFunction(),
735 // For most address spaces, an allocation can't be placed at null, but null
736 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
737 // the only valid inbounds address derived from null, is null itself.
738 // Thus, we have four cases to consider:
739 // 1) Base == nullptr, Offset == 0 -> inbounds, null
740 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
741 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
742 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
743 //
744 // (Note if we're indexing a type of size 0, that simply collapses into one
745 // of the buckets above.)
746 //
747 // In general, we're allowed to make values less poison (i.e. remove
748 // sources of full UB), so in this case, we just select between the two
749 // non-poison cases (1 and 4 above).
750 //
751 // For vectors, we apply the same reasoning on a per-lane basis.
752 auto *Base = GEPLHS->getPointerOperand();
753 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
754 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount();
756 }
757 return new ICmpInst(Cond, Base,
759 cast<Constant>(RHS), Base->getType()));
760 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
761 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
762
763 // If the base pointers are different, but the indices are the same, just
764 // compare the base pointer.
765 if (GEPLHS->getOperand(0) != GEPRHS->getOperand(0)) {
766 bool IndicesTheSame =
767 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
768 GEPLHS->getPointerOperand()->getType() ==
769 GEPRHS->getPointerOperand()->getType() &&
770 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
771 if (IndicesTheSame)
772 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
773 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
774 IndicesTheSame = false;
775 break;
776 }
777
778 // If all indices are the same, just compare the base pointers.
779 Type *BaseType = GEPLHS->getOperand(0)->getType();
780 if (IndicesTheSame &&
781 CmpInst::makeCmpResultType(BaseType) == I.getType() && CanFold(NW))
782 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
783
784 // If we're comparing GEPs with two base pointers that only differ in type
785 // and both GEPs have only constant indices or just one use, then fold
786 // the compare with the adjusted indices.
787 // FIXME: Support vector of pointers.
788 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
789 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
790 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
791 GEPLHS->getOperand(0)->stripPointerCasts() ==
792 GEPRHS->getOperand(0)->stripPointerCasts() &&
793 !GEPLHS->getType()->isVectorTy()) {
794 Value *LOffset = EmitGEPOffset(GEPLHS);
795 Value *ROffset = EmitGEPOffset(GEPRHS);
796
797 // If we looked through an addrspacecast between different sized address
798 // spaces, the LHS and RHS pointers are different sized
799 // integers. Truncate to the smaller one.
800 Type *LHSIndexTy = LOffset->getType();
801 Type *RHSIndexTy = ROffset->getType();
802 if (LHSIndexTy != RHSIndexTy) {
803 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <
804 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {
805 ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy);
806 } else
807 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy);
808 }
809
811 LOffset, ROffset);
812 return replaceInstUsesWith(I, Cmp);
813 }
814 }
815
816 if (GEPLHS->getOperand(0) == GEPRHS->getOperand(0) &&
817 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
818 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
819 // If the GEPs only differ by one index, compare it.
820 unsigned NumDifferences = 0; // Keep track of # differences.
821 unsigned DiffOperand = 0; // The operand that differs.
822 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
823 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
824 Type *LHSType = GEPLHS->getOperand(i)->getType();
825 Type *RHSType = GEPRHS->getOperand(i)->getType();
826 // FIXME: Better support for vector of pointers.
827 if (LHSType->getPrimitiveSizeInBits() !=
828 RHSType->getPrimitiveSizeInBits() ||
829 (GEPLHS->getType()->isVectorTy() &&
830 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
831 // Irreconcilable differences.
832 NumDifferences = 2;
833 break;
834 }
835
836 if (NumDifferences++)
837 break;
838 DiffOperand = i;
839 }
840
841 if (NumDifferences == 0) // SAME GEP?
842 return replaceInstUsesWith(
843 I, // No comparison is needed here.
844 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));
845 // If two GEPs only differ by an index, compare them.
846 // Note that nowrap flags are always needed when comparing two indices.
847 else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {
848 Value *LHSV = GEPLHS->getOperand(DiffOperand);
849 Value *RHSV = GEPRHS->getOperand(DiffOperand);
850 return NewICmp(NW, LHSV, RHSV);
851 }
852 }
853
854 if (Base.Ptr && CanFold(Base.LHSNW & Base.RHSNW) && !Base.isExpensive()) {
855 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
856 Type *IdxTy = DL.getIndexType(GEPLHS->getType());
857 Value *L =
858 EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEP=*/true);
859 Value *R =
860 EmitGEPOffsets(Base.RHSGEPs, Base.RHSNW, IdxTy, /*RewriteGEP=*/true);
861 return NewICmp(Base.LHSNW & Base.RHSNW, L, R);
862 }
863 }
864
865 // Try convert this to an indexed compare by looking through PHIs/casts as a
866 // last resort.
867 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
868}
869
871 // It would be tempting to fold away comparisons between allocas and any
872 // pointer not based on that alloca (e.g. an argument). However, even
873 // though such pointers cannot alias, they can still compare equal.
874 //
875 // But LLVM doesn't specify where allocas get their memory, so if the alloca
876 // doesn't escape we can argue that it's impossible to guess its value, and we
877 // can therefore act as if any such guesses are wrong.
878 //
879 // However, we need to ensure that this folding is consistent: We can't fold
880 // one comparison to false, and then leave a different comparison against the
881 // same value alone (as it might evaluate to true at runtime, leading to a
882 // contradiction). As such, this code ensures that all comparisons are folded
883 // at the same time, and there are no other escapes.
884
885 struct CmpCaptureTracker : public CaptureTracker {
886 AllocaInst *Alloca;
887 bool Captured = false;
888 /// The value of the map is a bit mask of which icmp operands the alloca is
889 /// used in.
891
892 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
893
894 void tooManyUses() override { Captured = true; }
895
896 Action captured(const Use *U, UseCaptureInfo CI) override {
897 // TODO(captures): Use UseCaptureInfo.
898 auto *ICmp = dyn_cast<ICmpInst>(U->getUser());
899 // We need to check that U is based *only* on the alloca, and doesn't
900 // have other contributions from a select/phi operand.
901 // TODO: We could check whether getUnderlyingObjects() reduces to one
902 // object, which would allow looking through phi nodes.
903 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) {
904 // Collect equality icmps of the alloca, and don't treat them as
905 // captures.
906 ICmps[ICmp] |= 1u << U->getOperandNo();
907 return Continue;
908 }
909
910 Captured = true;
911 return Stop;
912 }
913 };
914
915 CmpCaptureTracker Tracker(Alloca);
916 PointerMayBeCaptured(Alloca, &Tracker);
917 if (Tracker.Captured)
918 return false;
919
920 bool Changed = false;
921 for (auto [ICmp, Operands] : Tracker.ICmps) {
922 switch (Operands) {
923 case 1:
924 case 2: {
925 // The alloca is only used in one icmp operand. Assume that the
926 // equality is false.
927 auto *Res = ConstantInt::get(ICmp->getType(),
928 ICmp->getPredicate() == ICmpInst::ICMP_NE);
929 replaceInstUsesWith(*ICmp, Res);
931 Changed = true;
932 break;
933 }
934 case 3:
935 // Both icmp operands are based on the alloca, so this is comparing
936 // pointer offsets, without leaking any information about the address
937 // of the alloca. Ignore such comparisons.
938 break;
939 default:
940 llvm_unreachable("Cannot happen");
941 }
942 }
943
944 return Changed;
945}
946
947/// Fold "icmp pred (X+C), X".
949 CmpPredicate Pred) {
950 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
951 // so the values can never be equal. Similarly for all other "or equals"
952 // operators.
953 assert(!!C && "C should not be zero!");
954
955 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
956 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
957 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
958 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
959 Constant *R =
960 ConstantInt::get(X->getType(), APInt::getMaxValue(C.getBitWidth()) - C);
961 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
962 }
963
964 // (X+1) >u X --> X <u (0-1) --> X != 255
965 // (X+2) >u X --> X <u (0-2) --> X <u 254
966 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
967 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
968 return new ICmpInst(ICmpInst::ICMP_ULT, X,
969 ConstantInt::get(X->getType(), -C));
970
971 APInt SMax = APInt::getSignedMaxValue(C.getBitWidth());
972
973 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
974 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
975 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
976 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
977 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
978 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
979 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
980 return new ICmpInst(ICmpInst::ICMP_SGT, X,
981 ConstantInt::get(X->getType(), SMax - C));
982
983 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
984 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
985 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
986 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
987 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
988 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
989
990 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
991 return new ICmpInst(ICmpInst::ICMP_SLT, X,
992 ConstantInt::get(X->getType(), SMax - (C - 1)));
993}
994
995/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
996/// (icmp eq/ne A, Log2(AP2/AP1)) ->
997/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
999 const APInt &AP1,
1000 const APInt &AP2) {
1001 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1002
1003 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1004 if (I.getPredicate() == I.ICMP_NE)
1005 Pred = CmpInst::getInversePredicate(Pred);
1006 return new ICmpInst(Pred, LHS, RHS);
1007 };
1008
1009 // Don't bother doing any work for cases which InstSimplify handles.
1010 if (AP2.isZero())
1011 return nullptr;
1012
1013 bool IsAShr = isa<AShrOperator>(I.getOperand(0));
1014 if (IsAShr) {
1015 if (AP2.isAllOnes())
1016 return nullptr;
1017 if (AP2.isNegative() != AP1.isNegative())
1018 return nullptr;
1019 if (AP2.sgt(AP1))
1020 return nullptr;
1021 }
1022
1023 if (!AP1)
1024 // 'A' must be large enough to shift out the highest set bit.
1025 return getICmp(I.ICMP_UGT, A,
1026 ConstantInt::get(A->getType(), AP2.logBase2()));
1027
1028 if (AP1 == AP2)
1029 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1030
1031 int Shift;
1032 if (IsAShr && AP1.isNegative())
1033 Shift = AP1.countl_one() - AP2.countl_one();
1034 else
1035 Shift = AP1.countl_zero() - AP2.countl_zero();
1036
1037 if (Shift > 0) {
1038 if (IsAShr && AP1 == AP2.ashr(Shift)) {
1039 // There are multiple solutions if we are comparing against -1 and the LHS
1040 // of the ashr is not a power of two.
1041 if (AP1.isAllOnes() && !AP2.isPowerOf2())
1042 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));
1043 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1044 } else if (AP1 == AP2.lshr(Shift)) {
1045 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1046 }
1047 }
1048
1049 // Shifting const2 will never be equal to const1.
1050 // FIXME: This should always be handled by InstSimplify?
1051 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1052 return replaceInstUsesWith(I, TorF);
1053}
1054
1055/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1056/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1058 const APInt &AP1,
1059 const APInt &AP2) {
1060 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1061
1062 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1063 if (I.getPredicate() == I.ICMP_NE)
1064 Pred = CmpInst::getInversePredicate(Pred);
1065 return new ICmpInst(Pred, LHS, RHS);
1066 };
1067
1068 // Don't bother doing any work for cases which InstSimplify handles.
1069 if (AP2.isZero())
1070 return nullptr;
1071
1072 unsigned AP2TrailingZeros = AP2.countr_zero();
1073
1074 if (!AP1 && AP2TrailingZeros != 0)
1075 return getICmp(
1076 I.ICMP_UGE, A,
1077 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
1078
1079 if (AP1 == AP2)
1080 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1081
1082 // Get the distance between the lowest bits that are set.
1083 int Shift = AP1.countr_zero() - AP2TrailingZeros;
1084
1085 if (Shift > 0 && AP2.shl(Shift) == AP1)
1086 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1087
1088 // Shifting const2 will never be equal to const1.
1089 // FIXME: This should always be handled by InstSimplify?
1090 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1091 return replaceInstUsesWith(I, TorF);
1092}
1093
1094/// The caller has matched a pattern of the form:
1095/// I = icmp ugt (add (add A, B), CI2), CI1
1096/// If this is of the form:
1097/// sum = a + b
1098/// if (sum+128 >u 255)
1099/// Then replace it with llvm.sadd.with.overflow.i8.
1100///
1102 ConstantInt *CI2, ConstantInt *CI1,
1103 InstCombinerImpl &IC) {
1104 // The transformation we're trying to do here is to transform this into an
1105 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1106 // with a narrower add, and discard the add-with-constant that is part of the
1107 // range check (if we can't eliminate it, this isn't profitable).
1108
1109 // In order to eliminate the add-with-constant, the compare can be its only
1110 // use.
1111 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1112 if (!AddWithCst->hasOneUse())
1113 return nullptr;
1114
1115 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1116 if (!CI2->getValue().isPowerOf2())
1117 return nullptr;
1118 unsigned NewWidth = CI2->getValue().countr_zero();
1119 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1120 return nullptr;
1121
1122 // The width of the new add formed is 1 more than the bias.
1123 ++NewWidth;
1124
1125 // Check to see that CI1 is an all-ones value with NewWidth bits.
1126 if (CI1->getBitWidth() == NewWidth ||
1127 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1128 return nullptr;
1129
1130 // This is only really a signed overflow check if the inputs have been
1131 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1132 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1133 if (IC.ComputeMaxSignificantBits(A, &I) > NewWidth ||
1134 IC.ComputeMaxSignificantBits(B, &I) > NewWidth)
1135 return nullptr;
1136
1137 // In order to replace the original add with a narrower
1138 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1139 // and truncates that discard the high bits of the add. Verify that this is
1140 // the case.
1141 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1142 for (User *U : OrigAdd->users()) {
1143 if (U == AddWithCst)
1144 continue;
1145
1146 // Only accept truncates for now. We would really like a nice recursive
1147 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1148 // chain to see which bits of a value are actually demanded. If the
1149 // original add had another add which was then immediately truncated, we
1150 // could still do the transformation.
1151 TruncInst *TI = dyn_cast<TruncInst>(U);
1152 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1153 return nullptr;
1154 }
1155
1156 // If the pattern matches, truncate the inputs to the narrower type and
1157 // use the sadd_with_overflow intrinsic to efficiently compute both the
1158 // result and the overflow bit.
1159 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1161 I.getModule(), Intrinsic::sadd_with_overflow, NewType);
1162
1163 InstCombiner::BuilderTy &Builder = IC.Builder;
1164
1165 // Put the new code above the original add, in case there are any uses of the
1166 // add between the add and the compare.
1167 Builder.SetInsertPoint(OrigAdd);
1168
1169 Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc");
1170 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc");
1171 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd");
1172 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result");
1173 Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType());
1174
1175 // The inner add was the result of the narrow add, zero extended to the
1176 // wider type. Replace it with the result computed by the intrinsic.
1177 IC.replaceInstUsesWith(*OrigAdd, ZExt);
1178 IC.eraseInstFromFunction(*OrigAdd);
1179
1180 // The original icmp gets replaced with the overflow value.
1181 return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1182}
1183
1184/// If we have:
1185/// icmp eq/ne (urem/srem %x, %y), 0
1186/// iff %y is a power-of-two, we can replace this with a bit test:
1187/// icmp eq/ne (and %x, (add %y, -1)), 0
1189 // This fold is only valid for equality predicates.
1190 if (!I.isEquality())
1191 return nullptr;
1192 CmpPredicate Pred;
1193 Value *X, *Y, *Zero;
1194 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))),
1195 m_CombineAnd(m_Zero(), m_Value(Zero)))))
1196 return nullptr;
1197 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, &I))
1198 return nullptr;
1199 // This may increase instruction count, we don't enforce that Y is a constant.
1200 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType()));
1201 Value *Masked = Builder.CreateAnd(X, Mask);
1202 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, Zero);
1203}
1204
1205/// Fold equality-comparison between zero and any (maybe truncated) right-shift
1206/// by one-less-than-bitwidth into a sign test on the original value.
1208 Instruction *Val;
1209 CmpPredicate Pred;
1210 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero())))
1211 return nullptr;
1212
1213 Value *X;
1214 Type *XTy;
1215
1216 Constant *C;
1217 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) {
1218 XTy = X->getType();
1219 unsigned XBitWidth = XTy->getScalarSizeInBits();
1221 APInt(XBitWidth, XBitWidth - 1))))
1222 return nullptr;
1223 } else if (isa<BinaryOperator>(Val) &&
1225 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val),
1226 /*AnalyzeForSignBitExtraction=*/true))) {
1227 XTy = X->getType();
1228 } else
1229 return nullptr;
1230
1231 return ICmpInst::Create(Instruction::ICmp,
1235}
1236
1237// Handle icmp pred X, 0
1239 CmpInst::Predicate Pred = Cmp.getPredicate();
1240 if (!match(Cmp.getOperand(1), m_Zero()))
1241 return nullptr;
1242
1243 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1244 if (Pred == ICmpInst::ICMP_SGT) {
1245 Value *A, *B;
1246 if (match(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) {
1248 return new ICmpInst(Pred, B, Cmp.getOperand(1));
1250 return new ICmpInst(Pred, A, Cmp.getOperand(1));
1251 }
1252 }
1253
1255 return New;
1256
1257 // Given:
1258 // icmp eq/ne (urem %x, %y), 0
1259 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
1260 // icmp eq/ne %x, 0
1261 Value *X, *Y;
1262 if (match(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) &&
1263 ICmpInst::isEquality(Pred)) {
1264 KnownBits XKnown = computeKnownBits(X, &Cmp);
1265 KnownBits YKnown = computeKnownBits(Y, &Cmp);
1266 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1267 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1268 }
1269
1270 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are
1271 // odd/non-zero/there is no overflow.
1272 if (match(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) &&
1273 ICmpInst::isEquality(Pred)) {
1274
1275 KnownBits XKnown = computeKnownBits(X, &Cmp);
1276 // if X % 2 != 0
1277 // (icmp eq/ne Y)
1278 if (XKnown.countMaxTrailingZeros() == 0)
1279 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1280
1281 KnownBits YKnown = computeKnownBits(Y, &Cmp);
1282 // if Y % 2 != 0
1283 // (icmp eq/ne X)
1284 if (YKnown.countMaxTrailingZeros() == 0)
1285 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1286
1287 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0));
1288 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1289 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
1290 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`
1291 // but to avoid unnecessary work, first just if this is an obvious case.
1292
1293 // if X non-zero and NoOverflow(X * Y)
1294 // (icmp eq/ne Y)
1295 if (!XKnown.One.isZero() || isKnownNonZero(X, Q))
1296 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1297
1298 // if Y non-zero and NoOverflow(X * Y)
1299 // (icmp eq/ne X)
1300 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q))
1301 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1302 }
1303 // Note, we are skipping cases:
1304 // if Y % 2 != 0 AND X % 2 != 0
1305 // (false/true)
1306 // if X non-zero and Y non-zero and NoOverflow(X * Y)
1307 // (false/true)
1308 // Those can be simplified later as we would have already replaced the (icmp
1309 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
1310 // will fold to a constant elsewhere.
1311 }
1312
1313 // (icmp eq/ne f(X), 0) -> (icmp eq/ne X, 0)
1314 // where f(X) == 0 if and only if X == 0
1315 if (ICmpInst::isEquality(Pred))
1316 if (Value *Stripped = stripNullTest(Cmp.getOperand(0)))
1317 return new ICmpInst(Pred, Stripped,
1318 Constant::getNullValue(Stripped->getType()));
1319
1320 return nullptr;
1321}
1322
1323/// Fold icmp eq (num + mask) & ~mask, num
1324/// to
1325/// icmp eq (and num, mask), 0
1326/// Where mask is a low bit mask.
1328 Value *Num;
1329 CmpPredicate Pred;
1330 const APInt *Mask, *Neg;
1331
1332 if (!match(&Cmp,
1333 m_c_ICmp(Pred, m_Value(Num),
1335 m_LowBitMask(Mask))),
1336 m_APInt(Neg))))))
1337 return nullptr;
1338
1339 if (*Neg != ~*Mask)
1340 return nullptr;
1341
1342 if (!ICmpInst::isEquality(Pred))
1343 return nullptr;
1344
1345 // Create new icmp eq (num & mask), 0
1346 auto *NewAnd = Builder.CreateAnd(Num, *Mask);
1347 auto *Zero = Constant::getNullValue(Num->getType());
1348
1349 return new ICmpInst(Pred, NewAnd, Zero);
1350}
1351
1352/// Fold icmp Pred X, C.
1353/// TODO: This code structure does not make sense. The saturating add fold
1354/// should be moved to some other helper and extended as noted below (it is also
1355/// possible that code has been made unnecessary - do we canonicalize IR to
1356/// overflow/saturating intrinsics or not?).
1358 // Match the following pattern, which is a common idiom when writing
1359 // overflow-safe integer arithmetic functions. The source performs an addition
1360 // in wider type and explicitly checks for overflow using comparisons against
1361 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1362 //
1363 // TODO: This could probably be generalized to handle other overflow-safe
1364 // operations if we worked out the formulas to compute the appropriate magic
1365 // constants.
1366 //
1367 // sum = a + b
1368 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1369 CmpInst::Predicate Pred = Cmp.getPredicate();
1370 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
1371 Value *A, *B;
1372 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1373 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) &&
1374 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1375 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
1376 return Res;
1377
1378 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1379 Constant *C = dyn_cast<Constant>(Op1);
1380 if (!C)
1381 return nullptr;
1382
1383 if (auto *Phi = dyn_cast<PHINode>(Op0))
1384 if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
1386 for (Value *V : Phi->incoming_values()) {
1387 Constant *Res =
1388 ConstantFoldCompareInstOperands(Pred, cast<Constant>(V), C, DL);
1389 if (!Res)
1390 return nullptr;
1391 Ops.push_back(Res);
1392 }
1394 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());
1395 for (auto [V, Pred] : zip(Ops, Phi->blocks()))
1396 NewPhi->addIncoming(V, Pred);
1397 return replaceInstUsesWith(Cmp, NewPhi);
1398 }
1399
1401 return R;
1402
1403 return nullptr;
1404}
1405
1406/// Canonicalize icmp instructions based on dominating conditions.
1408 // We already checked simple implication in InstSimplify, only handle complex
1409 // cases here.
1410 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1);
1411 const APInt *C;
1412 if (!match(Y, m_APInt(C)))
1413 return nullptr;
1414
1415 CmpInst::Predicate Pred = Cmp.getPredicate();
1417
1418 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1419 const APInt *DomC) -> Instruction * {
1420 // We have 2 compares of a variable with constants. Calculate the constant
1421 // ranges of those compares to see if we can transform the 2nd compare:
1422 // DomBB:
1423 // DomCond = icmp DomPred X, DomC
1424 // br DomCond, CmpBB, FalseBB
1425 // CmpBB:
1426 // Cmp = icmp Pred X, C
1427 ConstantRange DominatingCR =
1428 ConstantRange::makeExactICmpRegion(DomPred, *DomC);
1429 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1430 ConstantRange Difference = DominatingCR.difference(CR);
1431 if (Intersection.isEmptySet())
1432 return replaceInstUsesWith(Cmp, Builder.getFalse());
1433 if (Difference.isEmptySet())
1434 return replaceInstUsesWith(Cmp, Builder.getTrue());
1435
1436 // Canonicalizing a sign bit comparison that gets used in a branch,
1437 // pessimizes codegen by generating branch on zero instruction instead
1438 // of a test and branch. So we avoid canonicalizing in such situations
1439 // because test and branch instruction has better branch displacement
1440 // than compare and branch instruction.
1441 bool UnusedBit;
1442 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit);
1443 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp)))
1444 return nullptr;
1445
1446 // Avoid an infinite loop with min/max canonicalization.
1447 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1448 if (Cmp.hasOneUse() &&
1449 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value())))
1450 return nullptr;
1451
1452 if (const APInt *EqC = Intersection.getSingleElement())
1453 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC));
1454 if (const APInt *NeC = Difference.getSingleElement())
1455 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC));
1456 return nullptr;
1457 };
1458
1459 for (BranchInst *BI : DC.conditionsFor(X)) {
1460 CmpPredicate DomPred;
1461 const APInt *DomC;
1462 if (!match(BI->getCondition(),
1463 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC))))
1464 continue;
1465
1466 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1467 if (DT.dominates(Edge0, Cmp.getParent())) {
1468 if (auto *V = handleDomCond(DomPred, DomC))
1469 return V;
1470 } else {
1471 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1472 if (DT.dominates(Edge1, Cmp.getParent()))
1473 if (auto *V =
1474 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC))
1475 return V;
1476 }
1477 }
1478
1479 return nullptr;
1480}
1481
1482/// Fold icmp (trunc X), C.
1484 TruncInst *Trunc,
1485 const APInt &C) {
1486 ICmpInst::Predicate Pred = Cmp.getPredicate();
1487 Value *X = Trunc->getOperand(0);
1488 Type *SrcTy = X->getType();
1489 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1490 SrcBits = SrcTy->getScalarSizeInBits();
1491
1492 // Match (icmp pred (trunc nuw/nsw X), C)
1493 // Which we can convert to (icmp pred X, (sext/zext C))
1494 if (shouldChangeType(Trunc->getType(), SrcTy)) {
1495 if (Trunc->hasNoSignedWrap())
1496 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
1497 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1498 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
1499 }
1500
1501 if (C.isOne() && C.getBitWidth() > 1) {
1502 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1503 Value *V = nullptr;
1504 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
1505 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1506 ConstantInt::get(V->getType(), 1));
1507 }
1508
1509 // TODO: Handle any shifted constant by subtracting trailing zeros.
1510 // TODO: Handle non-equality predicates.
1511 Value *Y;
1512 if (Cmp.isEquality() && match(X, m_Shl(m_One(), m_Value(Y)))) {
1513 // (trunc (1 << Y) to iN) == 0 --> Y u>= N
1514 // (trunc (1 << Y) to iN) != 0 --> Y u< N
1515 if (C.isZero()) {
1516 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1517 return new ICmpInst(NewPred, Y, ConstantInt::get(SrcTy, DstBits));
1518 }
1519 // (trunc (1 << Y) to iN) == 2**C --> Y == C
1520 // (trunc (1 << Y) to iN) != 2**C --> Y != C
1521 if (C.isPowerOf2())
1522 return new ICmpInst(Pred, Y, ConstantInt::get(SrcTy, C.logBase2()));
1523 }
1524
1525 if (Cmp.isEquality() && Trunc->hasOneUse()) {
1526 // Canonicalize to a mask and wider compare if the wide type is suitable:
1527 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1528 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {
1529 Constant *Mask =
1530 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits));
1531 Value *And = Builder.CreateAnd(X, Mask);
1532 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits));
1533 return new ICmpInst(Pred, And, WideC);
1534 }
1535
1536 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1537 // of the high bits truncated out of x are known.
1538 KnownBits Known = computeKnownBits(X, &Cmp);
1539
1540 // If all the high bits are known, we can do this xform.
1541 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1542 // Pull in the high bits from known-ones set.
1543 APInt NewRHS = C.zext(SrcBits);
1544 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
1545 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS));
1546 }
1547 }
1548
1549 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1550 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1551 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1552 Value *ShOp;
1553 uint64_t ShAmt;
1554 bool TrueIfSigned;
1555 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
1556 match(X, m_Shr(m_Value(ShOp), m_ConstantInt(ShAmt))) &&
1557 DstBits == SrcBits - ShAmt) {
1558 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1560 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1562 }
1563
1564 return nullptr;
1565}
1566
1567/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1568/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1571 const SimplifyQuery &Q) {
1572 Value *X, *Y;
1573 CmpPredicate Pred;
1574 bool YIsSExt = false;
1575 // Try to match icmp (trunc X), (trunc Y)
1576 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {
1577 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() &
1578 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind();
1579 if (Cmp.isSigned()) {
1580 // For signed comparisons, both truncs must be nsw.
1581 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1582 return nullptr;
1583 } else {
1584 // For unsigned and equality comparisons, either both must be nuw or
1585 // both must be nsw, we don't care which.
1586 if (!NoWrapFlags)
1587 return nullptr;
1588 }
1589
1590 if (X->getType() != Y->getType() &&
1591 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))
1592 return nullptr;
1593 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) &&
1594 isDesirableIntType(Y->getType()->getScalarSizeInBits())) {
1595 std::swap(X, Y);
1596 Pred = Cmp.getSwappedPredicate(Pred);
1597 }
1598 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1599 }
1600 // Try to match icmp (trunc nuw X), (zext Y)
1601 else if (!Cmp.isSigned() &&
1602 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),
1603 m_OneUse(m_ZExt(m_Value(Y)))))) {
1604 // Can fold trunc nuw + zext for unsigned and equality predicates.
1605 }
1606 // Try to match icmp (trunc nsw X), (sext Y)
1607 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),
1609 // Can fold trunc nsw + zext/sext for all predicates.
1610 YIsSExt =
1611 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));
1612 } else
1613 return nullptr;
1614
1615 Type *TruncTy = Cmp.getOperand(0)->getType();
1616 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1617
1618 // If this transform will end up changing from desirable types -> undesirable
1619 // types skip it.
1620 if (isDesirableIntType(TruncBits) &&
1621 !isDesirableIntType(X->getType()->getScalarSizeInBits()))
1622 return nullptr;
1623
1624 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);
1625 return new ICmpInst(Pred, X, NewY);
1626}
1627
1628/// Fold icmp (xor X, Y), C.
1631 const APInt &C) {
1632 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1633 return I;
1634
1635 Value *X = Xor->getOperand(0);
1636 Value *Y = Xor->getOperand(1);
1637 const APInt *XorC;
1638 if (!match(Y, m_APInt(XorC)))
1639 return nullptr;
1640
1641 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1642 // fold the xor.
1643 ICmpInst::Predicate Pred = Cmp.getPredicate();
1644 bool TrueIfSigned = false;
1645 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {
1646
1647 // If the sign bit of the XorCst is not set, there is no change to
1648 // the operation, just stop using the Xor.
1649 if (!XorC->isNegative())
1650 return replaceOperand(Cmp, 0, X);
1651
1652 // Emit the opposite comparison.
1653 if (TrueIfSigned)
1654 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1655 ConstantInt::getAllOnesValue(X->getType()));
1656 else
1657 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1658 ConstantInt::getNullValue(X->getType()));
1659 }
1660
1661 if (Xor->hasOneUse()) {
1662 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1663 if (!Cmp.isEquality() && XorC->isSignMask()) {
1664 Pred = Cmp.getFlippedSignednessPredicate();
1665 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1666 }
1667
1668 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1669 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1670 Pred = Cmp.getFlippedSignednessPredicate();
1671 Pred = Cmp.getSwappedPredicate(Pred);
1672 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1673 }
1674 }
1675
1676 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1677 if (Pred == ICmpInst::ICMP_UGT) {
1678 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1679 if (*XorC == ~C && (C + 1).isPowerOf2())
1680 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1681 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1682 if (*XorC == C && (C + 1).isPowerOf2())
1683 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1684 }
1685 if (Pred == ICmpInst::ICMP_ULT) {
1686 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1687 if (*XorC == -C && C.isPowerOf2())
1688 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1689 ConstantInt::get(X->getType(), ~C));
1690 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1691 if (*XorC == C && (-C).isPowerOf2())
1692 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1693 ConstantInt::get(X->getType(), ~C));
1694 }
1695 return nullptr;
1696}
1697
1698/// For power-of-2 C:
1699/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1700/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1703 const APInt &C) {
1704 CmpInst::Predicate Pred = Cmp.getPredicate();
1705 APInt PowerOf2;
1706 if (Pred == ICmpInst::ICMP_ULT)
1707 PowerOf2 = C;
1708 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1709 PowerOf2 = C + 1;
1710 else
1711 return nullptr;
1712 if (!PowerOf2.isPowerOf2())
1713 return nullptr;
1714 Value *X;
1715 const APInt *ShiftC;
1717 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))
1718 return nullptr;
1719 uint64_t Shift = ShiftC->getLimitedValue();
1720 Type *XType = X->getType();
1721 if (Shift == 0 || PowerOf2.isMinSignedValue())
1722 return nullptr;
1723 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));
1724 APInt Bound =
1725 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1726 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));
1727}
1728
1729/// Fold icmp (and (sh X, Y), C2), C1.
1732 const APInt &C1,
1733 const APInt &C2) {
1734 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1735 if (!Shift || !Shift->isShift())
1736 return nullptr;
1737
1738 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1739 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1740 // code produced by the clang front-end, for bitfield access.
1741 // This seemingly simple opportunity to fold away a shift turns out to be
1742 // rather complicated. See PR17827 for details.
1743 unsigned ShiftOpcode = Shift->getOpcode();
1744 bool IsShl = ShiftOpcode == Instruction::Shl;
1745 const APInt *C3;
1746 if (match(Shift->getOperand(1), m_APInt(C3))) {
1747 APInt NewAndCst, NewCmpCst;
1748 bool AnyCmpCstBitsShiftedOut;
1749 if (ShiftOpcode == Instruction::Shl) {
1750 // For a left shift, we can fold if the comparison is not signed. We can
1751 // also fold a signed comparison if the mask value and comparison value
1752 // are not negative. These constraints may not be obvious, but we can
1753 // prove that they are correct using an SMT solver.
1754 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1755 return nullptr;
1756
1757 NewCmpCst = C1.lshr(*C3);
1758 NewAndCst = C2.lshr(*C3);
1759 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;
1760 } else if (ShiftOpcode == Instruction::LShr) {
1761 // For a logical right shift, we can fold if the comparison is not signed.
1762 // We can also fold a signed comparison if the shifted mask value and the
1763 // shifted comparison value are not negative. These constraints may not be
1764 // obvious, but we can prove that they are correct using an SMT solver.
1765 NewCmpCst = C1.shl(*C3);
1766 NewAndCst = C2.shl(*C3);
1767 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;
1768 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1769 return nullptr;
1770 } else {
1771 // For an arithmetic shift, check that both constants don't use (in a
1772 // signed sense) the top bits being shifted out.
1773 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1774 NewCmpCst = C1.shl(*C3);
1775 NewAndCst = C2.shl(*C3);
1776 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;
1777 if (NewAndCst.ashr(*C3) != C2)
1778 return nullptr;
1779 }
1780
1781 if (AnyCmpCstBitsShiftedOut) {
1782 // If we shifted bits out, the fold is not going to work out. As a
1783 // special case, check to see if this means that the result is always
1784 // true or false now.
1785 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1786 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1787 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1788 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1789 } else {
1790 Value *NewAnd = Builder.CreateAnd(
1791 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));
1792 return new ICmpInst(Cmp.getPredicate(), NewAnd,
1793 ConstantInt::get(And->getType(), NewCmpCst));
1794 }
1795 }
1796
1797 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1798 // preferable because it allows the C2 << Y expression to be hoisted out of a
1799 // loop if Y is invariant and X is not.
1800 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1801 !Shift->isArithmeticShift() &&
1802 ((!IsShl && C2.isOne()) || !isa<Constant>(Shift->getOperand(0)))) {
1803 // Compute C2 << Y.
1804 Value *NewShift =
1805 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))
1806 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));
1807
1808 // Compute X & (C2 << Y).
1809 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1810 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1811 }
1812
1813 return nullptr;
1814}
1815
1816/// Fold icmp (and X, C2), C1.
1819 const APInt &C1) {
1820 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1821
1822 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
1823 // TODO: We canonicalize to the longer form for scalars because we have
1824 // better analysis/folds for icmp, and codegen may be better with icmp.
1825 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&
1826 match(And->getOperand(1), m_One()))
1827 return new TruncInst(And->getOperand(0), Cmp.getType());
1828
1829 const APInt *C2;
1830 Value *X;
1831 if (!match(And, m_And(m_Value(X), m_APInt(C2))))
1832 return nullptr;
1833
1834 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask
1835 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(~*C2) &&
1836 C2->isNegatedPowerOf2())
1837 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1838 ConstantInt::get(X->getType(), ~*C2));
1839 // (and X, highmask) s< [1, -highmask] --> X s< -highmask
1840 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() &&
1841 (C1 - 1).ule(~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask())
1842 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1843 ConstantInt::get(X->getType(), -*C2));
1844
1845 // Don't perform the following transforms if the AND has multiple uses
1846 if (!And->hasOneUse())
1847 return nullptr;
1848
1849 if (Cmp.isEquality() && C1.isZero()) {
1850 // Restrict this fold to single-use 'and' (PR10267).
1851 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1852 if (C2->isSignMask()) {
1853 Constant *Zero = Constant::getNullValue(X->getType());
1854 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1855 return new ICmpInst(NewPred, X, Zero);
1856 }
1857
1858 APInt NewC2 = *C2;
1859 KnownBits Know = computeKnownBits(And->getOperand(0), And);
1860 // Set high zeros of C2 to allow matching negated power-of-2.
1861 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
1862 Know.countMinLeadingZeros());
1863
1864 // Restrict this fold only for single-use 'and' (PR10267).
1865 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1866 if (NewC2.isNegatedPowerOf2()) {
1867 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);
1868 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1869 return new ICmpInst(NewPred, X, NegBOC);
1870 }
1871 }
1872
1873 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1874 // the input width without changing the value produced, eliminate the cast:
1875 //
1876 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1877 //
1878 // We can do this transformation if the constants do not have their sign bits
1879 // set or if it is an equality comparison. Extending a relational comparison
1880 // when we're checking the sign bit would not work.
1881 Value *W;
1882 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&
1883 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1884 // TODO: Is this a good transform for vectors? Wider types may reduce
1885 // throughput. Should this transform be limited (even for scalars) by using
1886 // shouldChangeType()?
1887 if (!Cmp.getType()->isVectorTy()) {
1888 Type *WideType = W->getType();
1889 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1890 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));
1891 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1892 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());
1893 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1894 }
1895 }
1896
1897 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))
1898 return I;
1899
1900 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1901 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1902 //
1903 // iff pred isn't signed
1904 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&
1905 match(And->getOperand(1), m_One())) {
1906 Constant *One = cast<Constant>(And->getOperand(1));
1907 Value *Or = And->getOperand(0);
1908 Value *A, *B, *LShr;
1909 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1910 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1911 unsigned UsesRemoved = 0;
1912 if (And->hasOneUse())
1913 ++UsesRemoved;
1914 if (Or->hasOneUse())
1915 ++UsesRemoved;
1916 if (LShr->hasOneUse())
1917 ++UsesRemoved;
1918
1919 // Compute A & ((1 << B) | 1)
1920 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;
1921 if (UsesRemoved >= RequireUsesRemoved) {
1922 Value *NewOr =
1923 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),
1924 /*HasNUW=*/true),
1925 One, Or->getName());
1926 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1927 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1928 }
1929 }
1930 }
1931
1932 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1933 // llvm.is.fpclass(X, fcInf|fcNan)
1934 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1935 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1936 // (icmp eq (and (bitcast X to int), ExponentMask), 0) -->
1937 // llvm.is.fpclass(X, fcSubnormal|fcZero)
1938 // (icmp ne (and (bitcast X to int), ExponentMask), 0) -->
1939 // llvm.is.fpclass(X, ~(fcSubnormal|fcZero))
1940 Value *V;
1941 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1942 Attribute::NoImplicitFloat) &&
1943 Cmp.isEquality() &&
1945 Type *FPType = V->getType()->getScalarType();
1946 if (FPType->isIEEELikeFPTy() && (C1.isZero() || C1 == *C2)) {
1947 APInt ExponentMask =
1948 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt();
1949 if (*C2 == ExponentMask) {
1950 unsigned Mask = C1.isZero()
1953 if (isICMP_NE)
1954 Mask = ~Mask & fcAllFlags;
1955 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));
1956 }
1957 }
1958 }
1959
1960 return nullptr;
1961}
1962
1963/// Fold icmp (and X, Y), C.
1966 const APInt &C) {
1967 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1968 return I;
1969
1970 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1971 bool TrueIfNeg;
1972 if (isSignBitCheck(Pred, C, TrueIfNeg)) {
1973 // ((X - 1) & ~X) < 0 --> X == 0
1974 // ((X - 1) & ~X) >= 0 --> X != 0
1975 Value *X;
1976 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&
1977 match(And->getOperand(1), m_Not(m_Specific(X)))) {
1978 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1979 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));
1980 }
1981 // (X & -X) < 0 --> X == MinSignedC
1982 // (X & -X) > -1 --> X != MinSignedC
1983 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {
1984 Constant *MinSignedC = ConstantInt::get(
1985 X->getType(),
1986 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
1987 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1988 return new ICmpInst(NewPred, X, MinSignedC);
1989 }
1990 }
1991
1992 // TODO: These all require that Y is constant too, so refactor with the above.
1993
1994 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1995 Value *X = And->getOperand(0);
1996 Value *Y = And->getOperand(1);
1997 if (auto *C2 = dyn_cast<ConstantInt>(Y))
1998 if (auto *LI = dyn_cast<LoadInst>(X))
1999 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
2000 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2001 if (Instruction *Res =
2002 foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2))
2003 return Res;
2004
2005 if (!Cmp.isEquality())
2006 return nullptr;
2007
2008 // X & -C == -C -> X > u ~C
2009 // X & -C != -C -> X <= u ~C
2010 // iff C is a power of 2
2011 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {
2012 auto NewPred =
2014 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
2015 }
2016
2017 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
2018 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
2019 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
2020 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
2022 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
2023 Value *TruncY = Builder.CreateTrunc(Y, X->getType());
2024 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
2025 Value *And = Builder.CreateAnd(TruncY, X);
2027 }
2028 return BinaryOperator::CreateAnd(TruncY, X);
2029 }
2030
2031 // (icmp eq/ne (and (shl -1, X), Y), 0)
2032 // -> (icmp eq/ne (lshr Y, X), 0)
2033 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
2034 // highly unlikely the non-zero case will ever show up in code.
2035 if (C.isZero() &&
2037 m_Value(Y))))) {
2038 Value *LShr = Builder.CreateLShr(Y, X);
2039 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));
2040 }
2041
2042 // (icmp eq/ne (and (add A, Addend), Msk), C)
2043 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk))
2044 {
2045 Value *A;
2046 const APInt *Addend, *Msk;
2047 if (match(And, m_And(m_OneUse(m_Add(m_Value(A), m_APInt(Addend))),
2048 m_LowBitMask(Msk))) &&
2049 C.ule(*Msk)) {
2050 APInt NewComperand = (C - *Addend) & *Msk;
2051 Value *MaskA = Builder.CreateAnd(A, ConstantInt::get(A->getType(), *Msk));
2052 return new ICmpInst(Pred, MaskA,
2053 ConstantInt::get(MaskA->getType(), NewComperand));
2054 }
2055 }
2056
2057 return nullptr;
2058}
2059
2060/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
2062 InstCombiner::BuilderTy &Builder) {
2063 // Are we using xors or subs to bitwise check for a pair or pairs of
2064 // (in)equalities? Convert to a shorter form that has more potential to be
2065 // folded even further.
2066 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
2067 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
2068 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
2069 // (X1 == X2) && (X3 == X4) && (X5 == X6)
2070 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
2071 // (X1 != X2) || (X3 != X4) || (X5 != X6)
2073 SmallVector<Value *, 16> WorkList(1, Or);
2074
2075 while (!WorkList.empty()) {
2076 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
2077 Value *Lhs, *Rhs;
2078
2079 if (match(OrOperatorArgument,
2080 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {
2081 CmpValues.emplace_back(Lhs, Rhs);
2082 return;
2083 }
2084
2085 if (match(OrOperatorArgument,
2086 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {
2087 CmpValues.emplace_back(Lhs, Rhs);
2088 return;
2089 }
2090
2091 WorkList.push_back(OrOperatorArgument);
2092 };
2093
2094 Value *CurrentValue = WorkList.pop_back_val();
2095 Value *OrOperatorLhs, *OrOperatorRhs;
2096
2097 if (!match(CurrentValue,
2098 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {
2099 return nullptr;
2100 }
2101
2102 MatchOrOperatorArgument(OrOperatorRhs);
2103 MatchOrOperatorArgument(OrOperatorLhs);
2104 }
2105
2106 ICmpInst::Predicate Pred = Cmp.getPredicate();
2107 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2108 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,
2109 CmpValues.rbegin()->second);
2110
2111 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2112 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);
2113 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);
2114 }
2115
2116 return LhsCmp;
2117}
2118
2119/// Fold icmp (or X, Y), C.
2122 const APInt &C) {
2123 ICmpInst::Predicate Pred = Cmp.getPredicate();
2124 if (C.isOne()) {
2125 // icmp slt signum(V) 1 --> icmp slt V, 1
2126 Value *V = nullptr;
2127 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
2128 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2129 ConstantInt::get(V->getType(), 1));
2130 }
2131
2132 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
2133
2134 // (icmp eq/ne (or disjoint x, C0), C1)
2135 // -> (icmp eq/ne x, C0^C1)
2136 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
2137 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {
2138 Value *NewC =
2139 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));
2140 return new ICmpInst(Pred, OrOp0, NewC);
2141 }
2142
2143 const APInt *MaskC;
2144 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {
2145 if (*MaskC == C && (C + 1).isPowerOf2()) {
2146 // X | C == C --> X <=u C
2147 // X | C != C --> X >u C
2148 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2150 return new ICmpInst(Pred, OrOp0, OrOp1);
2151 }
2152
2153 // More general: canonicalize 'equality with set bits mask' to
2154 // 'equality with clear bits mask'.
2155 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2156 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2157 if (Or->hasOneUse()) {
2158 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));
2159 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));
2160 return new ICmpInst(Pred, And, NewC);
2161 }
2162 }
2163
2164 // (X | (X-1)) s< 0 --> X s< 1
2165 // (X | (X-1)) s> -1 --> X s> 0
2166 Value *X;
2167 bool TrueIfSigned;
2168 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
2170 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2171 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
2172 return new ICmpInst(NewPred, X, NewC);
2173 }
2174
2175 const APInt *OrC;
2176 // icmp(X | OrC, C) --> icmp(X, 0)
2177 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {
2178 switch (Pred) {
2179 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2180 case ICmpInst::ICMP_SLT:
2181 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2182 case ICmpInst::ICMP_SGE:
2183 if (OrC->sge(C))
2184 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
2185 break;
2186 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2187 case ICmpInst::ICMP_SLE:
2188 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2189 case ICmpInst::ICMP_SGT:
2190 if (OrC->sgt(C))
2192 ConstantInt::getNullValue(X->getType()));
2193 break;
2194 default:
2195 break;
2196 }
2197 }
2198
2199 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2200 return nullptr;
2201
2202 Value *P, *Q;
2204 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2205 // -> and (icmp eq P, null), (icmp eq Q, null).
2206 Value *CmpP =
2207 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
2208 Value *CmpQ =
2210 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2211 return BinaryOperator::Create(BOpc, CmpP, CmpQ);
2212 }
2213
2214 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2215 return replaceInstUsesWith(Cmp, V);
2216
2217 return nullptr;
2218}
2219
2220/// Fold icmp (mul X, Y), C.
2223 const APInt &C) {
2224 ICmpInst::Predicate Pred = Cmp.getPredicate();
2225 Type *MulTy = Mul->getType();
2226 Value *X = Mul->getOperand(0);
2227
2228 // If there's no overflow:
2229 // X * X == 0 --> X == 0
2230 // X * X != 0 --> X != 0
2231 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
2232 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2233 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2234
2235 const APInt *MulC;
2236 if (!match(Mul->getOperand(1), m_APInt(MulC)))
2237 return nullptr;
2238
2239 // If this is a test of the sign bit and the multiply is sign-preserving with
2240 // a constant operand, use the multiply LHS operand instead:
2241 // (X * +MulC) < 0 --> X < 0
2242 // (X * -MulC) < 0 --> X > 0
2243 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2244 if (MulC->isNegative())
2245 Pred = ICmpInst::getSwappedPredicate(Pred);
2246 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2247 }
2248
2249 if (MulC->isZero())
2250 return nullptr;
2251
2252 // If the multiply does not wrap or the constant is odd, try to divide the
2253 // compare constant by the multiplication factor.
2254 if (Cmp.isEquality()) {
2255 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2256 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2257 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2258 return new ICmpInst(Pred, X, NewC);
2259 }
2260
2261 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2262 // correct to transform if MulC * N == C including overflow. I.e with i8
2263 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2264 // miss that case.
2265 if (C.urem(*MulC).isZero()) {
2266 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2267 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2268 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2269 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2270 return new ICmpInst(Pred, X, NewC);
2271 }
2272 }
2273 }
2274
2275 // With a matching no-overflow guarantee, fold the constants:
2276 // (X * MulC) < C --> X < (C / MulC)
2277 // (X * MulC) > C --> X > (C / MulC)
2278 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2279 Constant *NewC = nullptr;
2280 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2281 // MININT / -1 --> overflow.
2282 if (C.isMinSignedValue() && MulC->isAllOnes())
2283 return nullptr;
2284 if (MulC->isNegative())
2285 Pred = ICmpInst::getSwappedPredicate(Pred);
2286
2287 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2288 NewC = ConstantInt::get(
2290 } else {
2291 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2292 "Unexpected predicate");
2293 NewC = ConstantInt::get(
2295 }
2296 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2297 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2298 NewC = ConstantInt::get(
2300 } else {
2301 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2302 "Unexpected predicate");
2303 NewC = ConstantInt::get(
2305 }
2306 }
2307
2308 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2309}
2310
2311/// Fold icmp (shl nuw C2, Y), C.
2313 const APInt &C) {
2314 Value *Y;
2315 const APInt *C2;
2316 if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y))))
2317 return nullptr;
2318
2319 Type *ShiftType = Shl->getType();
2320 unsigned TypeBits = C.getBitWidth();
2321 ICmpInst::Predicate Pred = Cmp.getPredicate();
2322 if (Cmp.isUnsigned()) {
2323 if (C2->isZero() || C2->ugt(C))
2324 return nullptr;
2325 APInt Div, Rem;
2326 APInt::udivrem(C, *C2, Div, Rem);
2327 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();
2328
2329 // (1 << Y) pred C -> Y pred Log2(C)
2330 if (!CIsPowerOf2) {
2331 // (1 << Y) < 30 -> Y <= 4
2332 // (1 << Y) <= 30 -> Y <= 4
2333 // (1 << Y) >= 30 -> Y > 4
2334 // (1 << Y) > 30 -> Y > 4
2335 if (Pred == ICmpInst::ICMP_ULT)
2336 Pred = ICmpInst::ICMP_ULE;
2337 else if (Pred == ICmpInst::ICMP_UGE)
2338 Pred = ICmpInst::ICMP_UGT;
2339 }
2340
2341 unsigned CLog2 = Div.logBase2();
2342 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2343 } else if (Cmp.isSigned() && C2->isOne()) {
2344 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
2345 // (1 << Y) > 0 -> Y != 31
2346 // (1 << Y) > C -> Y != 31 if C is negative.
2347 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))
2348 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2349
2350 // (1 << Y) < 0 -> Y == 31
2351 // (1 << Y) < 1 -> Y == 31
2352 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2353 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2354 if (Pred == ICmpInst::ICMP_SLT && (C - 1).sle(0))
2355 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2356 }
2357
2358 return nullptr;
2359}
2360
2361/// Fold icmp (shl X, Y), C.
2363 BinaryOperator *Shl,
2364 const APInt &C) {
2365 const APInt *ShiftVal;
2366 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
2367 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);
2368
2369 ICmpInst::Predicate Pred = Cmp.getPredicate();
2370 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2371 // -> (icmp pred X, Csle0)
2372 //
2373 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2374 // so X's must be what is used.
2375 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2376 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2377
2378 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2379 // -> (icmp eq/ne X, 0)
2380 if (ICmpInst::isEquality(Pred) && C.isZero() &&
2381 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2382 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2383
2384 // (icmp slt (shl nsw X, Y), 0/1)
2385 // -> (icmp slt X, 0/1)
2386 // (icmp sgt (shl nsw X, Y), 0/-1)
2387 // -> (icmp sgt X, 0/-1)
2388 //
2389 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2390 if (Shl->hasNoSignedWrap() &&
2391 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2392 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2393 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2394
2395 const APInt *ShiftAmt;
2396 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2397 return foldICmpShlLHSC(Cmp, Shl, C);
2398
2399 // Check that the shift amount is in range. If not, don't perform undefined
2400 // shifts. When the shift is visited, it will be simplified.
2401 unsigned TypeBits = C.getBitWidth();
2402 if (ShiftAmt->uge(TypeBits))
2403 return nullptr;
2404
2405 Value *X = Shl->getOperand(0);
2406 Type *ShType = Shl->getType();
2407
2408 // NSW guarantees that we are only shifting out sign bits from the high bits,
2409 // so we can ASHR the compare constant without needing a mask and eliminate
2410 // the shift.
2411 if (Shl->hasNoSignedWrap()) {
2412 if (Pred == ICmpInst::ICMP_SGT) {
2413 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2414 APInt ShiftedC = C.ashr(*ShiftAmt);
2415 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2416 }
2417 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2418 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {
2419 APInt ShiftedC = C.ashr(*ShiftAmt);
2420 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2421 }
2422 if (Pred == ICmpInst::ICMP_SLT) {
2423 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2424 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2425 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2426 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2427 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2428 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;
2429 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2430 }
2431 }
2432
2433 // NUW guarantees that we are only shifting out zero bits from the high bits,
2434 // so we can LSHR the compare constant without needing a mask and eliminate
2435 // the shift.
2436 if (Shl->hasNoUnsignedWrap()) {
2437 if (Pred == ICmpInst::ICMP_UGT) {
2438 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2439 APInt ShiftedC = C.lshr(*ShiftAmt);
2440 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2441 }
2442 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2443 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {
2444 APInt ShiftedC = C.lshr(*ShiftAmt);
2445 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2446 }
2447 if (Pred == ICmpInst::ICMP_ULT) {
2448 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2449 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2450 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2451 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2452 assert(C.ugt(0) && "ult 0 should have been eliminated");
2453 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;
2454 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2455 }
2456 }
2457
2458 if (Cmp.isEquality() && Shl->hasOneUse()) {
2459 // Strength-reduce the shift into an 'and'.
2460 Constant *Mask = ConstantInt::get(
2461 ShType,
2462 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
2463 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2464 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));
2465 return new ICmpInst(Pred, And, LShrC);
2466 }
2467
2468 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2469 bool TrueIfSigned = false;
2470 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {
2471 // (X << 31) <s 0 --> (X & 1) != 0
2472 Constant *Mask = ConstantInt::get(
2473 ShType,
2474 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
2475 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2476 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2477 And, Constant::getNullValue(ShType));
2478 }
2479
2480 // Simplify 'shl' inequality test into 'and' equality test.
2481 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2482 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2483 if ((C + 1).isPowerOf2() &&
2484 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2485 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));
2486 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2488 And, Constant::getNullValue(ShType));
2489 }
2490 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2491 if (C.isPowerOf2() &&
2492 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2493 Value *And =
2494 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));
2495 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2497 And, Constant::getNullValue(ShType));
2498 }
2499 }
2500
2501 // Transform (icmp pred iM (shl iM %v, N), C)
2502 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2503 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2504 // This enables us to get rid of the shift in favor of a trunc that may be
2505 // free on the target. It has the additional benefit of comparing to a
2506 // smaller constant that may be more target-friendly.
2507 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
2508 if (Shl->hasOneUse() && Amt != 0 &&
2509 shouldChangeType(ShType->getScalarSizeInBits(), TypeBits - Amt)) {
2510 ICmpInst::Predicate CmpPred = Pred;
2511 APInt RHSC = C;
2512
2513 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(CmpPred)) {
2514 // Try the flipped strictness predicate.
2515 // e.g.:
2516 // icmp ult i64 (shl X, 32), 8589934593 ->
2517 // icmp ule i64 (shl X, 32), 8589934592 ->
2518 // icmp ule i32 (trunc X, i32), 2 ->
2519 // icmp ult i32 (trunc X, i32), 3
2520 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(
2521 Pred, ConstantInt::get(ShType->getContext(), C))) {
2522 CmpPred = FlippedStrictness->first;
2523 RHSC = cast<ConstantInt>(FlippedStrictness->second)->getValue();
2524 }
2525 }
2526
2527 if (RHSC.countr_zero() >= Amt) {
2528 Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt);
2529 Constant *NewC =
2530 ConstantInt::get(TruncTy, RHSC.ashr(*ShiftAmt).trunc(TypeBits - Amt));
2531 return new ICmpInst(CmpPred,
2532 Builder.CreateTrunc(X, TruncTy, "", /*IsNUW=*/false,
2533 Shl->hasNoSignedWrap()),
2534 NewC);
2535 }
2536 }
2537
2538 return nullptr;
2539}
2540
2541/// Fold icmp ({al}shr X, Y), C.
2543 BinaryOperator *Shr,
2544 const APInt &C) {
2545 // An exact shr only shifts out zero bits, so:
2546 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2547 Value *X = Shr->getOperand(0);
2548 CmpInst::Predicate Pred = Cmp.getPredicate();
2549 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2550 return new ICmpInst(Pred, X, Cmp.getOperand(1));
2551
2552 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2553 const APInt *ShiftValC;
2554 if (match(X, m_APInt(ShiftValC))) {
2555 if (Cmp.isEquality())
2556 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
2557
2558 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2559 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2560 bool TrueIfSigned;
2561 if (!IsAShr && ShiftValC->isNegative() &&
2562 isSignBitCheck(Pred, C, TrueIfSigned))
2563 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2564 Shr->getOperand(1),
2565 ConstantInt::getNullValue(X->getType()));
2566
2567 // If the shifted constant is a power-of-2, test the shift amount directly:
2568 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2569 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2570 if (!IsAShr && ShiftValC->isPowerOf2() &&
2571 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2572 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2573 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2574 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2575
2576 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2577 unsigned ShiftLZ = ShiftValC->countl_zero();
2578 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
2579 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2580 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
2581 }
2582 }
2583
2584 const APInt *ShiftAmtC;
2585 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))
2586 return nullptr;
2587
2588 // Check that the shift amount is in range. If not, don't perform undefined
2589 // shifts. When the shift is visited it will be simplified.
2590 unsigned TypeBits = C.getBitWidth();
2591 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);
2592 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2593 return nullptr;
2594
2595 bool IsExact = Shr->isExact();
2596 Type *ShrTy = Shr->getType();
2597 // TODO: If we could guarantee that InstSimplify would handle all of the
2598 // constant-value-based preconditions in the folds below, then we could assert
2599 // those conditions rather than checking them. This is difficult because of
2600 // undef/poison (PR34838).
2601 if (IsAShr && Shr->hasOneUse()) {
2602 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2603 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2604 // When C - 1 is a power of two and the transform can be legally
2605 // performed, prefer this form so the produced constant is close to a
2606 // power of two.
2607 // icmp slt/ult (ashr exact X, ShAmtC), C
2608 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2609 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
2610 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2611 }
2612 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2613 // When ShAmtC can be shifted losslessly:
2614 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2615 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2616 APInt ShiftedC = C.shl(ShAmtVal);
2617 if (ShiftedC.ashr(ShAmtVal) == C)
2618 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2619 }
2620 if (Pred == CmpInst::ICMP_SGT) {
2621 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2622 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2623 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&
2624 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
2625 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2626 }
2627 if (Pred == CmpInst::ICMP_UGT) {
2628 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2629 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2630 // clause accounts for that pattern.
2631 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2632 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
2633 (C + 1).shl(ShAmtVal).isMinSignedValue())
2634 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2635 }
2636
2637 // If the compare constant has significant bits above the lowest sign-bit,
2638 // then convert an unsigned cmp to a test of the sign-bit:
2639 // (ashr X, ShiftC) u> C --> X s< 0
2640 // (ashr X, ShiftC) u< C --> X s> -1
2641 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2642 if (Pred == CmpInst::ICMP_UGT) {
2643 return new ICmpInst(CmpInst::ICMP_SLT, X,
2645 }
2646 if (Pred == CmpInst::ICMP_ULT) {
2647 return new ICmpInst(CmpInst::ICMP_SGT, X,
2649 }
2650 }
2651 } else if (!IsAShr) {
2652 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2653 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2654 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2655 APInt ShiftedC = C.shl(ShAmtVal);
2656 if (ShiftedC.lshr(ShAmtVal) == C)
2657 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2658 }
2659 if (Pred == CmpInst::ICMP_UGT) {
2660 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2661 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2662 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))
2663 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2664 }
2665 }
2666
2667 if (!Cmp.isEquality())
2668 return nullptr;
2669
2670 // Handle equality comparisons of shift-by-constant.
2671
2672 // If the comparison constant changes with the shift, the comparison cannot
2673 // succeed (bits of the comparison constant cannot match the shifted value).
2674 // This should be known by InstSimplify and already be folded to true/false.
2675 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2676 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2677 "Expected icmp+shr simplify did not occur.");
2678
2679 // If the bits shifted out are known zero, compare the unshifted value:
2680 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2681 if (Shr->isExact())
2682 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));
2683
2684 if (C.isZero()) {
2685 // == 0 is u< 1.
2686 if (Pred == CmpInst::ICMP_EQ)
2687 return new ICmpInst(CmpInst::ICMP_ULT, X,
2688 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal)));
2689 else
2690 return new ICmpInst(CmpInst::ICMP_UGT, X,
2691 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal) - 1));
2692 }
2693
2694 if (Shr->hasOneUse()) {
2695 // Canonicalize the shift into an 'and':
2696 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2697 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2698 Constant *Mask = ConstantInt::get(ShrTy, Val);
2699 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");
2700 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));
2701 }
2702
2703 return nullptr;
2704}
2705
2707 BinaryOperator *SRem,
2708 const APInt &C) {
2709 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2710 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
2711 // Canonicalize unsigned predicates to signed:
2712 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 0
2713 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1
2714 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1
2715 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1
2716
2717 const APInt *DivisorC;
2718 if (!match(SRem->getOperand(1), m_APInt(DivisorC)))
2719 return nullptr;
2720
2721 APInt NormalizedC = C;
2722 if (Pred == ICmpInst::ICMP_ULT) {
2723 assert(!NormalizedC.isZero() &&
2724 "ult X, 0 should have been simplified already.");
2725 --NormalizedC;
2726 }
2727 if (C.isNegative())
2728 NormalizedC.flipAllBits();
2729 assert(!DivisorC->isZero() &&
2730 "srem X, 0 should have been simplified already.");
2731 if (!NormalizedC.uge(DivisorC->abs() - 1))
2732 return nullptr;
2733
2734 Type *Ty = SRem->getType();
2735 if (Pred == ICmpInst::ICMP_UGT)
2736 return new ICmpInst(ICmpInst::ICMP_SLT, SRem,
2738 return new ICmpInst(ICmpInst::ICMP_SGT, SRem,
2740 }
2741 // Match an 'is positive' or 'is negative' comparison of remainder by a
2742 // constant power-of-2 value:
2743 // (X % pow2C) sgt/slt 0
2744 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2745 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2746 return nullptr;
2747
2748 // TODO: The one-use check is standard because we do not typically want to
2749 // create longer instruction sequences, but this might be a special-case
2750 // because srem is not good for analysis or codegen.
2751 if (!SRem->hasOneUse())
2752 return nullptr;
2753
2754 const APInt *DivisorC;
2755 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
2756 return nullptr;
2757
2758 // For cmp_sgt/cmp_slt only zero valued C is handled.
2759 // For cmp_eq/cmp_ne only positive valued C is handled.
2760 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2761 !C.isZero()) ||
2762 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2763 !C.isStrictlyPositive()))
2764 return nullptr;
2765
2766 // Mask off the sign bit and the modulo bits (low-bits).
2767 Type *Ty = SRem->getType();
2769 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
2770 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);
2771
2772 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2773 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));
2774
2775 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2776 // bit is set. Example:
2777 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2778 if (Pred == ICmpInst::ICMP_SGT)
2780
2781 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2782 // bit is set. Example:
2783 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2784 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
2785}
2786
2787/// Fold icmp (udiv X, Y), C.
2789 BinaryOperator *UDiv,
2790 const APInt &C) {
2791 ICmpInst::Predicate Pred = Cmp.getPredicate();
2792 Value *X = UDiv->getOperand(0);
2793 Value *Y = UDiv->getOperand(1);
2794 Type *Ty = UDiv->getType();
2795
2796 const APInt *C2;
2797 if (!match(X, m_APInt(C2)))
2798 return nullptr;
2799
2800 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2801
2802 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2803 if (Pred == ICmpInst::ICMP_UGT) {
2804 assert(!C.isMaxValue() &&
2805 "icmp ugt X, UINT_MAX should have been simplified already.");
2806 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2807 ConstantInt::get(Ty, C2->udiv(C + 1)));
2808 }
2809
2810 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2811 if (Pred == ICmpInst::ICMP_ULT) {
2812 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2813 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2814 ConstantInt::get(Ty, C2->udiv(C)));
2815 }
2816
2817 return nullptr;
2818}
2819
2820/// Fold icmp ({su}div X, Y), C.
2822 BinaryOperator *Div,
2823 const APInt &C) {
2824 ICmpInst::Predicate Pred = Cmp.getPredicate();
2825 Value *X = Div->getOperand(0);
2826 Value *Y = Div->getOperand(1);
2827 Type *Ty = Div->getType();
2828 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2829
2830 // If unsigned division and the compare constant is bigger than
2831 // UMAX/2 (negative), there's only one pair of values that satisfies an
2832 // equality check, so eliminate the division:
2833 // (X u/ Y) == C --> (X == C) && (Y == 1)
2834 // (X u/ Y) != C --> (X != C) || (Y != 1)
2835 // Similarly, if signed division and the compare constant is exactly SMIN:
2836 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2837 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2838 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2839 (!DivIsSigned || C.isMinSignedValue())) {
2840 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));
2841 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));
2842 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2843 return BinaryOperator::Create(Logic, XBig, YOne);
2844 }
2845
2846 // Fold: icmp pred ([us]div X, C2), C -> range test
2847 // Fold this div into the comparison, producing a range check.
2848 // Determine, based on the divide type, what the range is being
2849 // checked. If there is an overflow on the low or high side, remember
2850 // it, otherwise compute the range [low, hi) bounding the new value.
2851 // See: InsertRangeTest above for the kinds of replacements possible.
2852 const APInt *C2;
2853 if (!match(Y, m_APInt(C2)))
2854 return nullptr;
2855
2856 // FIXME: If the operand types don't match the type of the divide
2857 // then don't attempt this transform. The code below doesn't have the
2858 // logic to deal with a signed divide and an unsigned compare (and
2859 // vice versa). This is because (x /s C2) <s C produces different
2860 // results than (x /s C2) <u C or (x /u C2) <s C or even
2861 // (x /u C2) <u C. Simply casting the operands and result won't
2862 // work. :( The if statement below tests that condition and bails
2863 // if it finds it.
2864 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
2865 return nullptr;
2866
2867 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2868 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2869 // division-by-constant cases should be present, we can not assert that they
2870 // have happened before we reach this icmp instruction.
2871 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2872 return nullptr;
2873
2874 // Compute Prod = C * C2. We are essentially solving an equation of
2875 // form X / C2 = C. We solve for X by multiplying C2 and C.
2876 // By solving for X, we can turn this into a range check instead of computing
2877 // a divide.
2878 APInt Prod = C * *C2;
2879
2880 // Determine if the product overflows by seeing if the product is not equal to
2881 // the divide. Make sure we do the same kind of divide as in the LHS
2882 // instruction that we're folding.
2883 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;
2884
2885 // If the division is known to be exact, then there is no remainder from the
2886 // divide, so the covered range size is unit, otherwise it is the divisor.
2887 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2888
2889 // Figure out the interval that is being checked. For example, a comparison
2890 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2891 // Compute this interval based on the constants involved and the signedness of
2892 // the compare/divide. This computes a half-open interval, keeping track of
2893 // whether either value in the interval overflows. After analysis each
2894 // overflow variable is set to 0 if it's corresponding bound variable is valid
2895 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2896 int LoOverflow = 0, HiOverflow = 0;
2897 APInt LoBound, HiBound;
2898
2899 if (!DivIsSigned) { // udiv
2900 // e.g. X/5 op 3 --> [15, 20)
2901 LoBound = Prod;
2902 HiOverflow = LoOverflow = ProdOV;
2903 if (!HiOverflow) {
2904 // If this is not an exact divide, then many values in the range collapse
2905 // to the same result value.
2906 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2907 }
2908 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2909 if (C.isZero()) { // (X / pos) op 0
2910 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2911 LoBound = -(RangeSize - 1);
2912 HiBound = RangeSize;
2913 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2914 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2915 HiOverflow = LoOverflow = ProdOV;
2916 if (!HiOverflow)
2917 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2918 } else { // (X / pos) op neg
2919 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2920 HiBound = Prod + 1;
2921 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2922 if (!LoOverflow) {
2923 APInt DivNeg = -RangeSize;
2924 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2925 }
2926 }
2927 } else if (C2->isNegative()) { // Divisor is < 0.
2928 if (Div->isExact())
2929 RangeSize.negate();
2930 if (C.isZero()) { // (X / neg) op 0
2931 // e.g. X/-5 op 0 --> [-4, 5)
2932 LoBound = RangeSize + 1;
2933 HiBound = -RangeSize;
2934 if (HiBound == *C2) { // -INTMIN = INTMIN
2935 HiOverflow = 1; // [INTMIN+1, overflow)
2936 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2937 }
2938 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2939 // e.g. X/-5 op 3 --> [-19, -14)
2940 HiBound = Prod + 1;
2941 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2942 if (!LoOverflow)
2943 LoOverflow =
2944 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;
2945 } else { // (X / neg) op neg
2946 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2947 LoOverflow = HiOverflow = ProdOV;
2948 if (!HiOverflow)
2949 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2950 }
2951
2952 // Dividing by a negative swaps the condition. LT <-> GT
2953 Pred = ICmpInst::getSwappedPredicate(Pred);
2954 }
2955
2956 switch (Pred) {
2957 default:
2958 llvm_unreachable("Unhandled icmp predicate!");
2959 case ICmpInst::ICMP_EQ:
2960 if (LoOverflow && HiOverflow)
2961 return replaceInstUsesWith(Cmp, Builder.getFalse());
2962 if (HiOverflow)
2963 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2964 X, ConstantInt::get(Ty, LoBound));
2965 if (LoOverflow)
2966 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2967 X, ConstantInt::get(Ty, HiBound));
2968 return replaceInstUsesWith(
2969 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));
2970 case ICmpInst::ICMP_NE:
2971 if (LoOverflow && HiOverflow)
2972 return replaceInstUsesWith(Cmp, Builder.getTrue());
2973 if (HiOverflow)
2974 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2975 X, ConstantInt::get(Ty, LoBound));
2976 if (LoOverflow)
2977 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2978 X, ConstantInt::get(Ty, HiBound));
2979 return replaceInstUsesWith(
2980 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));
2981 case ICmpInst::ICMP_ULT:
2982 case ICmpInst::ICMP_SLT:
2983 if (LoOverflow == +1) // Low bound is greater than input range.
2984 return replaceInstUsesWith(Cmp, Builder.getTrue());
2985 if (LoOverflow == -1) // Low bound is less than input range.
2986 return replaceInstUsesWith(Cmp, Builder.getFalse());
2987 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));
2988 case ICmpInst::ICMP_UGT:
2989 case ICmpInst::ICMP_SGT:
2990 if (HiOverflow == +1) // High bound greater than input range.
2991 return replaceInstUsesWith(Cmp, Builder.getFalse());
2992 if (HiOverflow == -1) // High bound less than input range.
2993 return replaceInstUsesWith(Cmp, Builder.getTrue());
2994 if (Pred == ICmpInst::ICMP_UGT)
2995 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));
2996 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));
2997 }
2998
2999 return nullptr;
3000}
3001
3002/// Fold icmp (sub X, Y), C.
3005 const APInt &C) {
3006 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
3007 ICmpInst::Predicate Pred = Cmp.getPredicate();
3008 Type *Ty = Sub->getType();
3009
3010 // (SubC - Y) == C) --> Y == (SubC - C)
3011 // (SubC - Y) != C) --> Y != (SubC - C)
3012 Constant *SubC;
3013 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {
3014 return new ICmpInst(Pred, Y,
3015 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));
3016 }
3017
3018 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
3019 const APInt *C2;
3020 APInt SubResult;
3021 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
3022 bool HasNSW = Sub->hasNoSignedWrap();
3023 bool HasNUW = Sub->hasNoUnsignedWrap();
3024 if (match(X, m_APInt(C2)) &&
3025 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
3026 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))
3027 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));
3028
3029 // X - Y == 0 --> X == Y.
3030 // X - Y != 0 --> X != Y.
3031 // TODO: We allow this with multiple uses as long as the other uses are not
3032 // in phis. The phi use check is guarding against a codegen regression
3033 // for a loop test. If the backend could undo this (and possibly
3034 // subsequent transforms), we would not need this hack.
3035 if (Cmp.isEquality() && C.isZero() &&
3036 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))
3037 return new ICmpInst(Pred, X, Y);
3038
3039 // The following transforms are only worth it if the only user of the subtract
3040 // is the icmp.
3041 // TODO: This is an artificial restriction for all of the transforms below
3042 // that only need a single replacement icmp. Can these use the phi test
3043 // like the transform above here?
3044 if (!Sub->hasOneUse())
3045 return nullptr;
3046
3047 if (Sub->hasNoSignedWrap()) {
3048 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
3049 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
3050 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
3051
3052 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
3053 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
3054 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3055
3056 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
3057 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
3058 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
3059
3060 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
3061 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3062 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3063 }
3064
3065 if (!match(X, m_APInt(C2)))
3066 return nullptr;
3067
3068 // C2 - Y <u C -> (Y | (C - 1)) == C2
3069 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
3070 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
3071 (*C2 & (C - 1)) == (C - 1))
3072 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);
3073
3074 // C2 - Y >u C -> (Y | C) != C2
3075 // iff C2 & C == C and C + 1 is a power of 2
3076 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
3077 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);
3078
3079 // We have handled special cases that reduce.
3080 // Canonicalize any remaining sub to add as:
3081 // (C2 - Y) > C --> (Y + ~C2) < ~C
3082 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",
3083 HasNUW, HasNSW);
3084 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
3085}
3086
3087static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
3088 Value *Op1, IRBuilderBase &Builder,
3089 bool HasOneUse) {
3090 auto FoldConstant = [&](bool Val) {
3091 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
3092 if (Op0->getType()->isVectorTy())
3094 cast<VectorType>(Op0->getType())->getElementCount(), Res);
3095 return Res;
3096 };
3097
3098 switch (Table.to_ulong()) {
3099 case 0: // 0 0 0 0
3100 return FoldConstant(false);
3101 case 1: // 0 0 0 1
3102 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
3103 case 2: // 0 0 1 0
3104 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
3105 case 3: // 0 0 1 1
3106 return Builder.CreateNot(Op0);
3107 case 4: // 0 1 0 0
3108 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
3109 case 5: // 0 1 0 1
3110 return Builder.CreateNot(Op1);
3111 case 6: // 0 1 1 0
3112 return Builder.CreateXor(Op0, Op1);
3113 case 7: // 0 1 1 1
3114 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
3115 case 8: // 1 0 0 0
3116 return Builder.CreateAnd(Op0, Op1);
3117 case 9: // 1 0 0 1
3118 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
3119 case 10: // 1 0 1 0
3120 return Op1;
3121 case 11: // 1 0 1 1
3122 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
3123 case 12: // 1 1 0 0
3124 return Op0;
3125 case 13: // 1 1 0 1
3126 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
3127 case 14: // 1 1 1 0
3128 return Builder.CreateOr(Op0, Op1);
3129 case 15: // 1 1 1 1
3130 return FoldConstant(true);
3131 default:
3132 llvm_unreachable("Invalid Operation");
3133 }
3134 return nullptr;
3135}
3136
3138 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3139 Value *A, *B;
3140 Constant *C1, *C2, *C3, *C4;
3141 if (!(match(BO->getOperand(0),
3142 m_Select(m_Value(A), m_Constant(C1), m_Constant(C2)))) ||
3143 !match(BO->getOperand(1),
3144 m_Select(m_Value(B), m_Constant(C3), m_Constant(C4))) ||
3145 Cmp.getType() != A->getType())
3146 return nullptr;
3147
3148 std::bitset<4> Table;
3149 auto ComputeTable = [&](bool First, bool Second) -> std::optional<bool> {
3150 Constant *L = First ? C1 : C2;
3151 Constant *R = Second ? C3 : C4;
3152 if (auto *Res = ConstantFoldBinaryOpOperands(BO->getOpcode(), L, R, DL)) {
3153 auto *Val = Res->getType()->isVectorTy() ? Res->getSplatValue() : Res;
3154 if (auto *CI = dyn_cast_or_null<ConstantInt>(Val))
3155 return ICmpInst::compare(CI->getValue(), C, Cmp.getPredicate());
3156 }
3157 return std::nullopt;
3158 };
3159
3160 for (unsigned I = 0; I < 4; ++I) {
3161 bool First = (I >> 1) & 1;
3162 bool Second = I & 1;
3163 if (auto Res = ComputeTable(First, Second))
3164 Table[I] = *Res;
3165 else
3166 return nullptr;
3167 }
3168
3169 // Synthesize optimal logic.
3170 if (auto *Cond = createLogicFromTable(Table, A, B, Builder, BO->hasOneUse()))
3171 return replaceInstUsesWith(Cmp, Cond);
3172 return nullptr;
3173}
3174
3175/// Fold icmp (add X, Y), C.
3178 const APInt &C) {
3179 Value *Y = Add->getOperand(1);
3180 Value *X = Add->getOperand(0);
3181
3182 Value *Op0, *Op1;
3183 Instruction *Ext0, *Ext1;
3184 const CmpInst::Predicate Pred = Cmp.getPredicate();
3185 if (match(Add,
3188 m_ZExtOrSExt(m_Value(Op1))))) &&
3189 Op0->getType()->isIntOrIntVectorTy(1) &&
3190 Op1->getType()->isIntOrIntVectorTy(1)) {
3191 unsigned BW = C.getBitWidth();
3192 std::bitset<4> Table;
3193 auto ComputeTable = [&](bool Op0Val, bool Op1Val) {
3194 APInt Res(BW, 0);
3195 if (Op0Val)
3196 Res += APInt(BW, isa<ZExtInst>(Ext0) ? 1 : -1, /*isSigned=*/true);
3197 if (Op1Val)
3198 Res += APInt(BW, isa<ZExtInst>(Ext1) ? 1 : -1, /*isSigned=*/true);
3199 return ICmpInst::compare(Res, C, Pred);
3200 };
3201
3202 Table[0] = ComputeTable(false, false);
3203 Table[1] = ComputeTable(false, true);
3204 Table[2] = ComputeTable(true, false);
3205 Table[3] = ComputeTable(true, true);
3206 if (auto *Cond =
3207 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))
3208 return replaceInstUsesWith(Cmp, Cond);
3209 }
3210 const APInt *C2;
3211 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3212 return nullptr;
3213
3214 // Fold icmp pred (add X, C2), C.
3215 Type *Ty = Add->getType();
3216
3217 // If the add does not wrap, we can always adjust the compare by subtracting
3218 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3219 // are canonicalized to SGT/SLT/UGT/ULT.
3220 if ((Add->hasNoSignedWrap() &&
3221 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
3222 (Add->hasNoUnsignedWrap() &&
3223 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
3224 bool Overflow;
3225 APInt NewC =
3226 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);
3227 // If there is overflow, the result must be true or false.
3228 // TODO: Can we assert there is no overflow because InstSimplify always
3229 // handles those cases?
3230 if (!Overflow)
3231 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3232 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
3233 }
3234
3235 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3236 C.isNonNegative() && (C - *C2).isNonNegative() &&
3237 computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())
3238 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
3239 ConstantInt::get(Ty, C - *C2));
3240
3241 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
3242 const APInt &Upper = CR.getUpper();
3243 const APInt &Lower = CR.getLower();
3244 if (Cmp.isSigned()) {
3245 if (Lower.isSignMask())
3246 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
3247 if (Upper.isSignMask())
3248 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
3249 } else {
3250 if (Lower.isMinValue())
3251 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
3252 if (Upper.isMinValue())
3253 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
3254 }
3255
3256 // This set of folds is intentionally placed after folds that use no-wrapping
3257 // flags because those folds are likely better for later analysis/codegen.
3260
3261 // Fold compare with offset to opposite sign compare if it eliminates offset:
3262 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3263 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3264 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));
3265
3266 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3267 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3268 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));
3269
3270 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3271 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3272 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));
3273
3274 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3275 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3276 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));
3277
3278 // (X + -1) <u C --> X <=u C (if X is never null)
3279 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3280 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3281 if (llvm::isKnownNonZero(X, Q))
3282 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));
3283 }
3284
3285 if (!Add->hasOneUse())
3286 return nullptr;
3287
3288 // X+C <u C2 -> (X & -C2) == C
3289 // iff C & (C2-1) == 0
3290 // C2 is a power of 2
3291 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3293 ConstantExpr::getNeg(cast<Constant>(Y)));
3294
3295 // X+C2 <u C -> (X & C) == 2C
3296 // iff C == -(C2)
3297 // C2 is a power of 2
3298 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3300 ConstantInt::get(Ty, C * 2));
3301
3302 // X+C >u C2 -> (X & ~C2) != C
3303 // iff C & C2 == 0
3304 // C2+1 is a power of 2
3305 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3307 ConstantExpr::getNeg(cast<Constant>(Y)));
3308
3309 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3310 // to the ult form.
3311 // X+C2 >u C -> X+(C2-C-1) <u ~C
3312 if (Pred == ICmpInst::ICMP_UGT)
3313 return new ICmpInst(ICmpInst::ICMP_ULT,
3314 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
3315 ConstantInt::get(Ty, ~C));
3316
3317 // zext(V) + C2 pred C -> V + C3 pred' C4
3318 Value *V;
3319 if (match(X, m_ZExt(m_Value(V)))) {
3320 Type *NewCmpTy = V->getType();
3321 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();
3322 if (shouldChangeType(Ty, NewCmpTy)) {
3323 ConstantRange SrcCR = CR.truncate(NewCmpBW, TruncInst::NoUnsignedWrap);
3324 CmpInst::Predicate EquivPred;
3325 APInt EquivInt;
3326 APInt EquivOffset;
3327
3328 SrcCR.getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
3329 return new ICmpInst(
3330 EquivPred,
3331 EquivOffset.isZero()
3332 ? V
3333 : Builder.CreateAdd(V, ConstantInt::get(NewCmpTy, EquivOffset)),
3334 ConstantInt::get(NewCmpTy, EquivInt));
3335 }
3336 }
3337
3338 return nullptr;
3339}
3340
3342 Value *&RHS, ConstantInt *&Less,
3343 ConstantInt *&Equal,
3344 ConstantInt *&Greater) {
3345 // TODO: Generalize this to work with other comparison idioms or ensure
3346 // they get canonicalized into this form.
3347
3348 // select i1 (a == b),
3349 // i32 Equal,
3350 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3351 // where Equal, Less and Greater are placeholders for any three constants.
3352 CmpPredicate PredA;
3353 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||
3354 !ICmpInst::isEquality(PredA))
3355 return false;
3356 Value *EqualVal = SI->getTrueValue();
3357 Value *UnequalVal = SI->getFalseValue();
3358 // We still can get non-canonical predicate here, so canonicalize.
3359 if (PredA == ICmpInst::ICMP_NE)
3360 std::swap(EqualVal, UnequalVal);
3361 if (!match(EqualVal, m_ConstantInt(Equal)))
3362 return false;
3363 CmpPredicate PredB;
3364 Value *LHS2, *RHS2;
3365 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),
3366 m_ConstantInt(Less), m_ConstantInt(Greater))))
3367 return false;
3368 // We can get predicate mismatch here, so canonicalize if possible:
3369 // First, ensure that 'LHS' match.
3370 if (LHS2 != LHS) {
3371 // x sgt y <--> y slt x
3372 std::swap(LHS2, RHS2);
3373 PredB = ICmpInst::getSwappedPredicate(PredB);
3374 }
3375 if (LHS2 != LHS)
3376 return false;
3377 // We also need to canonicalize 'RHS'.
3378 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
3379 // x sgt C-1 <--> x sge C <--> not(x slt C)
3380 auto FlippedStrictness =
3381 getFlippedStrictnessPredicateAndConstant(PredB, cast<Constant>(RHS2));
3382 if (!FlippedStrictness)
3383 return false;
3384 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3385 "basic correctness failure");
3386 RHS2 = FlippedStrictness->second;
3387 // And kind-of perform the result swap.
3388 std::swap(Less, Greater);
3389 PredB = ICmpInst::ICMP_SLT;
3390 }
3391 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3392}
3393
3396 ConstantInt *C) {
3397
3398 assert(C && "Cmp RHS should be a constant int!");
3399 // If we're testing a constant value against the result of a three way
3400 // comparison, the result can be expressed directly in terms of the
3401 // original values being compared. Note: We could possibly be more
3402 // aggressive here and remove the hasOneUse test. The original select is
3403 // really likely to simplify or sink when we remove a test of the result.
3404 Value *OrigLHS, *OrigRHS;
3405 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3406 if (Cmp.hasOneUse() &&
3407 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,
3408 C3GreaterThan)) {
3409 assert(C1LessThan && C2Equal && C3GreaterThan);
3410
3411 bool TrueWhenLessThan = ICmpInst::compare(
3412 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate());
3413 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(),
3414 Cmp.getPredicate());
3415 bool TrueWhenGreaterThan = ICmpInst::compare(
3416 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate());
3417
3418 // This generates the new instruction that will replace the original Cmp
3419 // Instruction. Instead of enumerating the various combinations when
3420 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3421 // false, we rely on chaining of ORs and future passes of InstCombine to
3422 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3423
3424 // When none of the three constants satisfy the predicate for the RHS (C),
3425 // the entire original Cmp can be simplified to a false.
3427 if (TrueWhenLessThan)
3429 Cond, Builder.CreateICmp(ICmpInst::ICMP_SLT, OrigLHS, OrigRHS));
3430 if (TrueWhenEqual)
3432 Cond, Builder.CreateICmp(ICmpInst::ICMP_EQ, OrigLHS, OrigRHS));
3433 if (TrueWhenGreaterThan)
3435 Cond, Builder.CreateICmp(ICmpInst::ICMP_SGT, OrigLHS, OrigRHS));
3436
3437 return replaceInstUsesWith(Cmp, Cond);
3438 }
3439 return nullptr;
3440}
3441
3443 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));
3444 if (!Bitcast)
3445 return nullptr;
3446
3447 ICmpInst::Predicate Pred = Cmp.getPredicate();
3448 Value *Op1 = Cmp.getOperand(1);
3449 Value *BCSrcOp = Bitcast->getOperand(0);
3450 Type *SrcType = Bitcast->getSrcTy();
3451 Type *DstType = Bitcast->getType();
3452
3453 // Make sure the bitcast doesn't change between scalar and vector and
3454 // doesn't change the number of vector elements.
3455 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3456 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3457 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3458 Value *X;
3459 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {
3460 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3461 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3462 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3463 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3464 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3465 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3466 match(Op1, m_Zero()))
3467 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3468
3469 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3470 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))
3471 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));
3472
3473 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3474 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))
3475 return new ICmpInst(Pred, X,
3476 ConstantInt::getAllOnesValue(X->getType()));
3477 }
3478
3479 // Zero-equality checks are preserved through unsigned floating-point casts:
3480 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3481 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3482 if (match(BCSrcOp, m_UIToFP(m_Value(X))))
3483 if (Cmp.isEquality() && match(Op1, m_Zero()))
3484 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3485
3486 const APInt *C;
3487 bool TrueIfSigned;
3488 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {
3489 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3490 // the FP extend/truncate because that cast does not change the sign-bit.
3491 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3492 // The sign-bit is always the most significant bit in those types.
3493 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&
3494 (match(BCSrcOp, m_FPExt(m_Value(X))) ||
3495 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {
3496 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3497 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3498 Type *XType = X->getType();
3499
3500 // We can't currently handle Power style floating point operations here.
3501 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3502 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());
3503 if (auto *XVTy = dyn_cast<VectorType>(XType))
3504 NewType = VectorType::get(NewType, XVTy->getElementCount());
3505 Value *NewBitcast = Builder.CreateBitCast(X, NewType);
3506 if (TrueIfSigned)
3507 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3508 ConstantInt::getNullValue(NewType));
3509 else
3510 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3512 }
3513 }
3514
3515 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3516 Type *FPType = SrcType->getScalarType();
3517 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3518 Attribute::NoImplicitFloat) &&
3519 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3520 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3521 if (Mask & (fcInf | fcZero)) {
3522 if (Pred == ICmpInst::ICMP_NE)
3523 Mask = ~Mask;
3524 return replaceInstUsesWith(Cmp,
3525 Builder.createIsFPClass(BCSrcOp, Mask));
3526 }
3527 }
3528 }
3529 }
3530
3531 const APInt *C;
3532 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||
3533 !SrcType->isIntOrIntVectorTy())
3534 return nullptr;
3535
3536 // If this is checking if all elements of a vector compare are set or not,
3537 // invert the casted vector equality compare and test if all compare
3538 // elements are clear or not. Compare against zero is generally easier for
3539 // analysis and codegen.
3540 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3541 // Example: are all elements equal? --> are zero elements not equal?
3542 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3543 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3544 if (Value *NotBCSrcOp =
3545 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {
3546 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);
3547 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));
3548 }
3549 }
3550
3551 // If this is checking if all elements of an extended vector are clear or not,
3552 // compare in a narrow type to eliminate the extend:
3553 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3554 Value *X;
3555 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3556 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {
3557 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {
3558 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());
3559 Value *NewCast = Builder.CreateBitCast(X, NewType);
3560 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));
3561 }
3562 }
3563
3564 // Folding: icmp <pred> iN X, C
3565 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3566 // and C is a splat of a K-bit pattern
3567 // and SC is a constant vector = <C', C', C', ..., C'>
3568 // Into:
3569 // %E = extractelement <M x iK> %vec, i32 C'
3570 // icmp <pred> iK %E, trunc(C)
3571 Value *Vec;
3572 ArrayRef<int> Mask;
3573 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {
3574 // Check whether every element of Mask is the same constant
3575 if (all_equal(Mask)) {
3576 auto *VecTy = cast<VectorType>(SrcType);
3577 auto *EltTy = cast<IntegerType>(VecTy->getElementType());
3578 if (C->isSplat(EltTy->getBitWidth())) {
3579 // Fold the icmp based on the value of C
3580 // If C is M copies of an iK sized bit pattern,
3581 // then:
3582 // => %E = extractelement <N x iK> %vec, i32 Elem
3583 // icmp <pred> iK %SplatVal, <pattern>
3584 Value *Elem = Builder.getInt32(Mask[0]);
3585 Value *Extract = Builder.CreateExtractElement(Vec, Elem);
3586 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));
3587 return new ICmpInst(Pred, Extract, NewC);
3588 }
3589 }
3590 }
3591 return nullptr;
3592}
3593
3594/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3595/// where X is some kind of instruction.
3597 const APInt *C;
3598
3599 if (match(Cmp.getOperand(1), m_APInt(C))) {
3600 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))
3601 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))
3602 return I;
3603
3604 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))
3605 // For now, we only support constant integers while folding the
3606 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3607 // similar to the cases handled by binary ops above.
3608 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))
3609 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))
3610 return I;
3611
3612 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))
3613 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))
3614 return I;
3615
3616 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))
3618 return I;
3619
3620 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3621 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3622 // TODO: This checks one-use, but that is not strictly necessary.
3623 Value *Cmp0 = Cmp.getOperand(0);
3624 Value *X, *Y;
3625 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3626 (match(Cmp0,
3627 m_ExtractValue<0>(m_Intrinsic<Intrinsic::ssub_with_overflow>(
3628 m_Value(X), m_Value(Y)))) ||
3629 match(Cmp0,
3630 m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>(
3631 m_Value(X), m_Value(Y))))))
3632 return new ICmpInst(Cmp.getPredicate(), X, Y);
3633 }
3634
3635 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))
3637
3638 return nullptr;
3639}
3640
3641/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3642/// icmp eq/ne BO, C.
3644 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3645 // TODO: Some of these folds could work with arbitrary constants, but this
3646 // function is limited to scalar and vector splat constants.
3647 if (!Cmp.isEquality())
3648 return nullptr;
3649
3650 ICmpInst::Predicate Pred = Cmp.getPredicate();
3651 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3652 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
3653 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
3654
3655 switch (BO->getOpcode()) {
3656 case Instruction::SRem:
3657 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3658 if (C.isZero() && BO->hasOneUse()) {
3659 const APInt *BOC;
3660 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
3661 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());
3662 return new ICmpInst(Pred, NewRem,
3664 }
3665 }
3666 break;
3667 case Instruction::Add: {
3668 // (A + C2) == C --> A == (C - C2)
3669 // (A + C2) != C --> A != (C - C2)
3670 // TODO: Remove the one-use limitation? See discussion in D58633.
3671 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {
3672 if (BO->hasOneUse())
3673 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));
3674 } else if (C.isZero()) {
3675 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3676 // efficiently invertible, or if the add has just this one use.
3677 if (Value *NegVal = dyn_castNegVal(BOp1))
3678 return new ICmpInst(Pred, BOp0, NegVal);
3679 if (Value *NegVal = dyn_castNegVal(BOp0))
3680 return new ICmpInst(Pred, NegVal, BOp1);
3681 if (BO->hasOneUse()) {
3682 // (add nuw A, B) != 0 -> (or A, B) != 0
3683 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
3684 Value *Or = Builder.CreateOr(BOp0, BOp1);
3685 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
3686 }
3687 Value *Neg = Builder.CreateNeg(BOp1);
3688 Neg->takeName(BO);
3689 return new ICmpInst(Pred, BOp0, Neg);
3690 }
3691 }
3692 break;
3693 }
3694 case Instruction::Xor:
3695 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
3696 // For the xor case, we can xor two constants together, eliminating
3697 // the explicit xor.
3698 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
3699 } else if (C.isZero()) {
3700 // Replace ((xor A, B) != 0) with (A != B)
3701 return new ICmpInst(Pred, BOp0, BOp1);
3702 }
3703 break;
3704 case Instruction::Or: {
3705 const APInt *BOC;
3706 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3707 // Comparing if all bits outside of a constant mask are set?
3708 // Replace (X | C) == -1 with (X & ~C) == ~C.
3709 // This removes the -1 constant.
3710 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));
3711 Value *And = Builder.CreateAnd(BOp0, NotBOC);
3712 return new ICmpInst(Pred, And, NotBOC);
3713 }
3714 // (icmp eq (or (select cond, 0, NonZero), Other), 0)
3715 // -> (and cond, (icmp eq Other, 0))
3716 // (icmp ne (or (select cond, NonZero, 0), Other), 0)
3717 // -> (or cond, (icmp ne Other, 0))
3718 Value *Cond, *TV, *FV, *Other, *Sel;
3719 if (C.isZero() &&
3720 match(BO,
3723 m_Value(FV))),
3724 m_Value(Other)))) &&
3725 Cond->getType() == Cmp.getType()) {
3726 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3727 // Easy case is if eq/ne matches whether 0 is trueval/falseval.
3728 if (Pred == ICmpInst::ICMP_EQ
3729 ? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))
3730 : (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {
3731 Value *Cmp = Builder.CreateICmp(
3732 Pred, Other, Constant::getNullValue(Other->getType()));
3734 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3735 Cond);
3736 }
3737 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3738 // case we need to invert the select condition so we need to be careful to
3739 // avoid creating extra instructions.
3740 // (icmp ne (or (select cond, 0, NonZero), Other), 0)
3741 // -> (or (not cond), (icmp ne Other, 0))
3742 // (icmp eq (or (select cond, NonZero, 0), Other), 0)
3743 // -> (and (not cond), (icmp eq Other, 0))
3744 //
3745 // Only do this if the inner select has one use, in which case we are
3746 // replacing `select` with `(not cond)`. Otherwise, we will create more
3747 // uses. NB: Trying to freely invert cond doesn't make sense here, as if
3748 // cond was freely invertable, the select arms would have been inverted.
3749 if (Sel->hasOneUse() &&
3750 (Pred == ICmpInst::ICMP_EQ
3751 ? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))
3752 : (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {
3753 Value *NotCond = Builder.CreateNot(Cond);
3754 Value *Cmp = Builder.CreateICmp(
3755 Pred, Other, Constant::getNullValue(Other->getType()));
3757 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3758 NotCond);
3759 }
3760 }
3761 break;
3762 }
3763 case Instruction::UDiv:
3764 case Instruction::SDiv:
3765 if (BO->isExact()) {
3766 // div exact X, Y eq/ne 0 -> X eq/ne 0
3767 // div exact X, Y eq/ne 1 -> X eq/ne Y
3768 // div exact X, Y eq/ne C ->
3769 // if Y * C never-overflow && OneUse:
3770 // -> Y * C eq/ne X
3771 if (C.isZero())
3772 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));
3773 else if (C.isOne())
3774 return new ICmpInst(Pred, BOp0, BOp1);
3775 else if (BO->hasOneUse()) {
3777 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,
3778 Cmp.getOperand(1), BO);
3780 Value *YC =
3781 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));
3782 return new ICmpInst(Pred, YC, BOp0);
3783 }
3784 }
3785 }
3786 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3787 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3788 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3789 return new ICmpInst(NewPred, BOp1, BOp0);
3790 }
3791 break;
3792 default:
3793 break;
3794 }
3795 return nullptr;
3796}
3797
3799 const APInt &CRhs,
3800 InstCombiner::BuilderTy &Builder,
3801 const SimplifyQuery &Q) {
3802 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3803 "Non-ctpop intrin in ctpop fold");
3804 if (!CtpopLhs->hasOneUse())
3805 return nullptr;
3806
3807 // Power of 2 test:
3808 // isPow2OrZero : ctpop(X) u< 2
3809 // isPow2 : ctpop(X) == 1
3810 // NotPow2OrZero: ctpop(X) u> 1
3811 // NotPow2 : ctpop(X) != 1
3812 // If we know any bit of X can be folded to:
3813 // IsPow2 : X & (~Bit) == 0
3814 // NotPow2 : X & (~Bit) != 0
3815 const ICmpInst::Predicate Pred = I.getPredicate();
3816 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3817 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3818 Value *Op = CtpopLhs->getArgOperand(0);
3819 KnownBits OpKnown = computeKnownBits(Op, Q.DL, Q.AC, Q.CxtI, Q.DT);
3820 // No need to check for count > 1, that should be already constant folded.
3821 if (OpKnown.countMinPopulation() == 1) {
3822 Value *And = Builder.CreateAnd(
3823 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));
3824 return new ICmpInst(
3825 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3828 And, Constant::getNullValue(Op->getType()));
3829 }
3830 }
3831
3832 return nullptr;
3833}
3834
3835/// Fold an equality icmp with LLVM intrinsic and constant operand.
3837 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3838 Type *Ty = II->getType();
3839 unsigned BitWidth = C.getBitWidth();
3840 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3841
3842 switch (II->getIntrinsicID()) {
3843 case Intrinsic::abs:
3844 // abs(A) == 0 -> A == 0
3845 // abs(A) == INT_MIN -> A == INT_MIN
3846 if (C.isZero() || C.isMinSignedValue())
3847 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));
3848 break;
3849
3850 case Intrinsic::bswap:
3851 // bswap(A) == C -> A == bswap(C)
3852 return new ICmpInst(Pred, II->getArgOperand(0),
3853 ConstantInt::get(Ty, C.byteSwap()));
3854
3855 case Intrinsic::bitreverse:
3856 // bitreverse(A) == C -> A == bitreverse(C)
3857 return new ICmpInst(Pred, II->getArgOperand(0),
3858 ConstantInt::get(Ty, C.reverseBits()));
3859
3860 case Intrinsic::ctlz:
3861 case Intrinsic::cttz: {
3862 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3863 if (C == BitWidth)
3864 return new ICmpInst(Pred, II->getArgOperand(0),
3866
3867 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3868 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3869 // Limit to one use to ensure we don't increase instruction count.
3870 unsigned Num = C.getLimitedValue(BitWidth);
3871 if (Num != BitWidth && II->hasOneUse()) {
3872 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3873 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)
3874 : APInt::getHighBitsSet(BitWidth, Num + 1);
3875 APInt Mask2 = IsTrailing
3878 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),
3879 ConstantInt::get(Ty, Mask2));
3880 }
3881 break;
3882 }
3883
3884 case Intrinsic::ctpop: {
3885 // popcount(A) == 0 -> A == 0 and likewise for !=
3886 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3887 bool IsZero = C.isZero();
3888 if (IsZero || C == BitWidth)
3889 return new ICmpInst(Pred, II->getArgOperand(0),
3890 IsZero ? Constant::getNullValue(Ty)
3892
3893 break;
3894 }
3895
3896 case Intrinsic::fshl:
3897 case Intrinsic::fshr:
3898 if (II->getArgOperand(0) == II->getArgOperand(1)) {
3899 const APInt *RotAmtC;
3900 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3901 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3902 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))
3903 return new ICmpInst(Pred, II->getArgOperand(0),
3904 II->getIntrinsicID() == Intrinsic::fshl
3905 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))
3906 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));
3907 }
3908 break;
3909
3910 case Intrinsic::umax:
3911 case Intrinsic::uadd_sat: {
3912 // uadd.sat(a, b) == 0 -> (a | b) == 0
3913 // umax(a, b) == 0 -> (a | b) == 0
3914 if (C.isZero() && II->hasOneUse()) {
3915 Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1));
3916 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3917 }
3918 break;
3919 }
3920
3921 case Intrinsic::ssub_sat:
3922 // ssub.sat(a, b) == 0 -> a == b
3923 if (C.isZero())
3924 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3925 break;
3926 case Intrinsic::usub_sat: {
3927 // usub.sat(a, b) == 0 -> a <= b
3928 if (C.isZero()) {
3929 ICmpInst::Predicate NewPred =
3931 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));
3932 }
3933 break;
3934 }
3935 default:
3936 break;
3937 }
3938
3939 return nullptr;
3940}
3941
3942/// Fold an icmp with LLVM intrinsics
3943static Instruction *
3945 InstCombiner::BuilderTy &Builder) {
3946 assert(Cmp.isEquality());
3947
3948 ICmpInst::Predicate Pred = Cmp.getPredicate();
3949 Value *Op0 = Cmp.getOperand(0);
3950 Value *Op1 = Cmp.getOperand(1);
3951 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);
3952 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);
3953 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3954 return nullptr;
3955
3956 switch (IIOp0->getIntrinsicID()) {
3957 case Intrinsic::bswap:
3958 case Intrinsic::bitreverse:
3959 // If both operands are byte-swapped or bit-reversed, just compare the
3960 // original values.
3961 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3962 case Intrinsic::fshl:
3963 case Intrinsic::fshr: {
3964 // If both operands are rotated by same amount, just compare the
3965 // original values.
3966 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))
3967 break;
3968 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))
3969 break;
3970 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))
3971 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3972
3973 // rotate(X, AmtX) == rotate(Y, AmtY)
3974 // -> rotate(X, AmtX - AmtY) == Y
3975 // Do this if either both rotates have one use or if only one has one use
3976 // and AmtX/AmtY are constants.
3977 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3978 if (OneUses == 2 ||
3979 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&
3980 match(IIOp1->getOperand(2), m_ImmConstant()))) {
3981 Value *SubAmt =
3982 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));
3983 Value *CombinedRotate = Builder.CreateIntrinsic(
3984 Op0->getType(), IIOp0->getIntrinsicID(),
3985 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});
3986 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);
3987 }
3988 } break;
3989 default:
3990 break;
3991 }
3992
3993 return nullptr;
3994}
3995
3996/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3997/// where X is some kind of instruction and C is AllowPoison.
3998/// TODO: Move more folds which allow poison to this function.
4001 const APInt &C) {
4002 const ICmpInst::Predicate Pred = Cmp.getPredicate();
4003 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {
4004 switch (II->getIntrinsicID()) {
4005 default:
4006 break;
4007 case Intrinsic::fshl:
4008 case Intrinsic::fshr:
4009 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {
4010 // (rot X, ?) == 0/-1 --> X == 0/-1
4011 if (C.isZero() || C.isAllOnes())
4012 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
4013 }
4014 break;
4015 }
4016 }
4017
4018 return nullptr;
4019}
4020
4021/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
4023 BinaryOperator *BO,
4024 const APInt &C) {
4025 switch (BO->getOpcode()) {
4026 case Instruction::Xor:
4027 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
4028 return I;
4029 break;
4030 case Instruction::And:
4031 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
4032 return I;
4033 break;
4034 case Instruction::Or:
4035 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
4036 return I;
4037 break;
4038 case Instruction::Mul:
4039 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
4040 return I;
4041 break;
4042 case Instruction::Shl:
4043 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
4044 return I;
4045 break;
4046 case Instruction::LShr:
4047 case Instruction::AShr:
4048 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
4049 return I;
4050 break;
4051 case Instruction::SRem:
4052 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
4053 return I;
4054 break;
4055 case Instruction::UDiv:
4056 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
4057 return I;
4058 [[fallthrough]];
4059 case Instruction::SDiv:
4060 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
4061 return I;
4062 break;
4063 case Instruction::Sub:
4064 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
4065 return I;
4066 break;
4067 case Instruction::Add:
4068 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
4069 return I;
4070 break;
4071 default:
4072 break;
4073 }
4074
4075 // TODO: These folds could be refactored to be part of the above calls.
4077 return I;
4078
4079 // Fall back to handling `icmp pred (select A ? C1 : C2) binop (select B ? C3
4080 // : C4), C5` pattern, by computing a truth table of the four constant
4081 // variants.
4083}
4084
4085static Instruction *
4087 const APInt &C,
4088 InstCombiner::BuilderTy &Builder) {
4089 // This transform may end up producing more than one instruction for the
4090 // intrinsic, so limit it to one user of the intrinsic.
4091 if (!II->hasOneUse())
4092 return nullptr;
4093
4094 // Let Y = [add/sub]_sat(X, C) pred C2
4095 // SatVal = The saturating value for the operation
4096 // WillWrap = Whether or not the operation will underflow / overflow
4097 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
4098 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
4099 //
4100 // When (SatVal pred C2) is true, then
4101 // Y = WillWrap ? true : ((X binop C) pred C2)
4102 // => Y = WillWrap || ((X binop C) pred C2)
4103 // else
4104 // Y = WillWrap ? false : ((X binop C) pred C2)
4105 // => Y = !WillWrap ? ((X binop C) pred C2) : false
4106 // => Y = !WillWrap && ((X binop C) pred C2)
4107 Value *Op0 = II->getOperand(0);
4108 Value *Op1 = II->getOperand(1);
4109
4110 const APInt *COp1;
4111 // This transform only works when the intrinsic has an integral constant or
4112 // splat vector as the second operand.
4113 if (!match(Op1, m_APInt(COp1)))
4114 return nullptr;
4115
4116 APInt SatVal;
4117 switch (II->getIntrinsicID()) {
4118 default:
4120 "This function only works with usub_sat and uadd_sat for now!");
4121 case Intrinsic::uadd_sat:
4122 SatVal = APInt::getAllOnes(C.getBitWidth());
4123 break;
4124 case Intrinsic::usub_sat:
4125 SatVal = APInt::getZero(C.getBitWidth());
4126 break;
4127 }
4128
4129 // Check (SatVal pred C2)
4130 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);
4131
4132 // !WillWrap.
4134 II->getBinaryOp(), *COp1, II->getNoWrapKind());
4135
4136 // WillWrap.
4137 if (SatValCheck)
4138 C1 = C1.inverse();
4139
4141 if (II->getBinaryOp() == Instruction::Add)
4142 C2 = C2.sub(*COp1);
4143 else
4144 C2 = C2.add(*COp1);
4145
4146 Instruction::BinaryOps CombiningOp =
4147 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
4148
4149 std::optional<ConstantRange> Combination;
4150 if (CombiningOp == Instruction::BinaryOps::Or)
4151 Combination = C1.exactUnionWith(C2);
4152 else /* CombiningOp == Instruction::BinaryOps::And */
4153 Combination = C1.exactIntersectWith(C2);
4154
4155 if (!Combination)
4156 return nullptr;
4157
4158 CmpInst::Predicate EquivPred;
4159 APInt EquivInt;
4160 APInt EquivOffset;
4161
4162 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
4163
4164 return new ICmpInst(
4165 EquivPred,
4166 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),
4167 ConstantInt::get(Op1->getType(), EquivInt));
4168}
4169
4170static Instruction *
4172 const APInt &C,
4173 InstCombiner::BuilderTy &Builder) {
4174 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
4175 switch (Pred) {
4176 case ICmpInst::ICMP_EQ:
4177 case ICmpInst::ICMP_NE:
4178 if (C.isZero())
4179 NewPredicate = Pred;
4180 else if (C.isOne())
4181 NewPredicate =
4183 else if (C.isAllOnes())
4184 NewPredicate =
4186 break;
4187
4188 case ICmpInst::ICMP_SGT:
4189 if (C.isAllOnes())
4190 NewPredicate = ICmpInst::ICMP_UGE;
4191 else if (C.isZero())
4192 NewPredicate = ICmpInst::ICMP_UGT;
4193 break;
4194
4195 case ICmpInst::ICMP_SLT:
4196 if (C.isZero())
4197 NewPredicate = ICmpInst::ICMP_ULT;
4198 else if (C.isOne())
4199 NewPredicate = ICmpInst::ICMP_ULE;
4200 break;
4201
4202 case ICmpInst::ICMP_ULT:
4203 if (C.ugt(1))
4204 NewPredicate = ICmpInst::ICMP_UGE;
4205 break;
4206
4207 case ICmpInst::ICMP_UGT:
4208 if (!C.isZero() && !C.isAllOnes())
4209 NewPredicate = ICmpInst::ICMP_ULT;
4210 break;
4211
4212 default:
4213 break;
4214 }
4215
4216 if (!NewPredicate)
4217 return nullptr;
4218
4219 if (I->getIntrinsicID() == Intrinsic::scmp)
4220 NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate);
4221 Value *LHS = I->getOperand(0);
4222 Value *RHS = I->getOperand(1);
4223 return new ICmpInst(*NewPredicate, LHS, RHS);
4224}
4225
4226/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
4229 const APInt &C) {
4230 ICmpInst::Predicate Pred = Cmp.getPredicate();
4231
4232 // Handle folds that apply for any kind of icmp.
4233 switch (II->getIntrinsicID()) {
4234 default:
4235 break;
4236 case Intrinsic::uadd_sat:
4237 case Intrinsic::usub_sat:
4238 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
4239 Pred, cast<SaturatingInst>(II), C, Builder))
4240 return Folded;
4241 break;
4242 case Intrinsic::ctpop: {
4243 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
4244 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))
4245 return R;
4246 } break;
4247 case Intrinsic::scmp:
4248 case Intrinsic::ucmp:
4249 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, II, C, Builder))
4250 return Folded;
4251 break;
4252 }
4253
4254 if (Cmp.isEquality())
4255 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
4256
4257 Type *Ty = II->getType();
4258 unsigned BitWidth = C.getBitWidth();
4259 switch (II->getIntrinsicID()) {
4260 case Intrinsic::ctpop: {
4261 // (ctpop X > BitWidth - 1) --> X == -1
4262 Value *X = II->getArgOperand(0);
4263 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
4264 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,
4266 // (ctpop X < BitWidth) --> X != -1
4267 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
4268 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,
4270 break;
4271 }
4272 case Intrinsic::ctlz: {
4273 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
4274 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4275 unsigned Num = C.getLimitedValue();
4276 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);
4277 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,
4278 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4279 }
4280
4281 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
4282 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4283 unsigned Num = C.getLimitedValue();
4285 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,
4286 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4287 }
4288 break;
4289 }
4290 case Intrinsic::cttz: {
4291 // Limit to one use to ensure we don't increase instruction count.
4292 if (!II->hasOneUse())
4293 return nullptr;
4294
4295 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
4296 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4297 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);
4298 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,
4299 Builder.CreateAnd(II->getArgOperand(0), Mask),
4301 }
4302
4303 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
4304 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4305 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());
4306 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,
4307 Builder.CreateAnd(II->getArgOperand(0), Mask),
4309 }
4310 break;
4311 }
4312 case Intrinsic::ssub_sat:
4313 // ssub.sat(a, b) spred 0 -> a spred b
4314 if (ICmpInst::isSigned(Pred)) {
4315 if (C.isZero())
4316 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
4317 // X s<= 0 is cannonicalized to X s< 1
4318 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
4319 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),
4320 II->getArgOperand(1));
4321 // X s>= 0 is cannonicalized to X s> -1
4322 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
4323 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),
4324 II->getArgOperand(1));
4325 }
4326 break;
4327 default:
4328 break;
4329 }
4330
4331 return nullptr;
4332}
4333
4334/// Handle icmp with constant (but not simple integer constant) RHS.
4336 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4337 Constant *RHSC = dyn_cast<Constant>(Op1);
4338 Instruction *LHSI = dyn_cast<Instruction>(Op0);
4339 if (!RHSC || !LHSI)
4340 return nullptr;
4341
4342 switch (LHSI->getOpcode()) {
4343 case Instruction::IntToPtr:
4344 // icmp pred inttoptr(X), null -> icmp pred X, 0
4345 if (RHSC->isNullValue() &&
4346 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
4347 return new ICmpInst(
4348 I.getPredicate(), LHSI->getOperand(0),
4350 break;
4351
4352 case Instruction::Load:
4353 // Try to optimize things like "A[i] > 4" to index computations.
4354 if (GetElementPtrInst *GEP =
4355 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
4356 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
4357 if (Instruction *Res =
4358 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, GV, I))
4359 return Res;
4360 break;
4361 }
4362
4363 return nullptr;
4364}
4365
4367 Value *RHS, const ICmpInst &I) {
4368 // Try to fold the comparison into the select arms, which will cause the
4369 // select to be converted into a logical and/or.
4370 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4371 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))
4372 return Res;
4373 if (std::optional<bool> Impl = isImpliedCondition(
4374 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))
4375 return ConstantInt::get(I.getType(), *Impl);
4376 return nullptr;
4377 };
4378
4379 ConstantInt *CI = nullptr;
4380 Value *Op1 = SimplifyOp(SI->getOperand(1), true);
4381 if (Op1)
4382 CI = dyn_cast<ConstantInt>(Op1);
4383
4384 Value *Op2 = SimplifyOp(SI->getOperand(2), false);
4385 if (Op2)
4386 CI = dyn_cast<ConstantInt>(Op2);
4387
4388 auto Simplifies = [&](Value *Op, unsigned Idx) {
4389 // A comparison of ucmp/scmp with a constant will fold into an icmp.
4390 const APInt *Dummy;
4391 return Op ||
4392 (isa<CmpIntrinsic>(SI->getOperand(Idx)) &&
4393 SI->getOperand(Idx)->hasOneUse() && match(RHS, m_APInt(Dummy)));
4394 };
4395
4396 // We only want to perform this transformation if it will not lead to
4397 // additional code. This is true if either both sides of the select
4398 // fold to a constant (in which case the icmp is replaced with a select
4399 // which will usually simplify) or this is the only user of the
4400 // select (in which case we are trading a select+icmp for a simpler
4401 // select+icmp) or all uses of the select can be replaced based on
4402 // dominance information ("Global cases").
4403 bool Transform = false;
4404 if (Op1 && Op2)
4405 Transform = true;
4406 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {
4407 // Local case
4408 if (SI->hasOneUse())
4409 Transform = true;
4410 // Global cases
4411 else if (CI && !CI->isZero())
4412 // When Op1 is constant try replacing select with second operand.
4413 // Otherwise Op2 is constant and try replacing select with first
4414 // operand.
4415 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4416 }
4417 if (Transform) {
4418 if (!Op1)
4419 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4420 if (!Op2)
4421 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4422 return SelectInst::Create(SI->getOperand(0), Op1, Op2);
4423 }
4424
4425 return nullptr;
4426}
4427
4428// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4429static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4430 unsigned Depth = 0) {
4431 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4432 return true;
4433 if (V->getType()->getScalarSizeInBits() == 1)
4434 return true;
4436 return false;
4437 Value *X;
4438 const Instruction *I = dyn_cast<Instruction>(V);
4439 if (!I)
4440 return false;
4441 switch (I->getOpcode()) {
4442 case Instruction::ZExt:
4443 // ZExt(Mask) is a Mask.
4444 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4445 case Instruction::SExt:
4446 // SExt(Mask) is a Mask.
4447 // SExt(~Mask) is a ~Mask.
4448 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4449 case Instruction::And:
4450 case Instruction::Or:
4451 // Mask0 | Mask1 is a Mask.
4452 // Mask0 & Mask1 is a Mask.
4453 // ~Mask0 | ~Mask1 is a ~Mask.
4454 // ~Mask0 & ~Mask1 is a ~Mask.
4455 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4456 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4457 case Instruction::Xor:
4458 if (match(V, m_Not(m_Value(X))))
4459 return isMaskOrZero(X, !Not, Q, Depth);
4460
4461 // (X ^ -X) is a ~Mask
4462 if (Not)
4463 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4464 // (X ^ (X - 1)) is a Mask
4465 else
4466 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4467 case Instruction::Select:
4468 // c ? Mask0 : Mask1 is a Mask.
4469 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4470 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4471 case Instruction::Shl:
4472 // (~Mask) << X is a ~Mask.
4473 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4474 case Instruction::LShr:
4475 // Mask >> X is a Mask.
4476 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4477 case Instruction::AShr:
4478 // Mask s>> X is a Mask.
4479 // ~Mask s>> X is a ~Mask.
4480 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4481 case Instruction::Add:
4482 // Pow2 - 1 is a Mask.
4483 if (!Not && match(I->getOperand(1), m_AllOnes()))
4484 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4485 Q.AC, Q.CxtI, Q.DT, Depth);
4486 break;
4487 case Instruction::Sub:
4488 // -Pow2 is a ~Mask.
4489 if (Not && match(I->getOperand(0), m_Zero()))
4490 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4491 Q.AC, Q.CxtI, Q.DT, Depth);
4492 break;
4493 case Instruction::Call: {
4494 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4495 switch (II->getIntrinsicID()) {
4496 // min/max(Mask0, Mask1) is a Mask.
4497 // min/max(~Mask0, ~Mask1) is a ~Mask.
4498 case Intrinsic::umax:
4499 case Intrinsic::smax:
4500 case Intrinsic::umin:
4501 case Intrinsic::smin:
4502 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4503 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4504
4505 // In the context of masks, bitreverse(Mask) == ~Mask
4506 case Intrinsic::bitreverse:
4507 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4508 default:
4509 break;
4510 }
4511 }
4512 break;
4513 }
4514 default:
4515 break;
4516 }
4517 return false;
4518}
4519
4520/// Some comparisons can be simplified.
4521/// In this case, we are looking for comparisons that look like
4522/// a check for a lossy truncation.
4523/// Folds:
4524/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4525/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4526/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4527/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4528/// Where Mask is some pattern that produces all-ones in low bits:
4529/// (-1 >> y)
4530/// ((-1 << y) >> y) <- non-canonical, has extra uses
4531/// ~(-1 << y)
4532/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4533/// The Mask can be a constant, too.
4534/// For some predicates, the operands are commutative.
4535/// For others, x can only be on a specific side.
4537 Value *Op1, const SimplifyQuery &Q,
4538 InstCombiner &IC) {
4539
4540 ICmpInst::Predicate DstPred;
4541 switch (Pred) {
4543 // x & Mask == x
4544 // x & ~Mask == 0
4545 // ~x | Mask == -1
4546 // -> x u<= Mask
4547 // x & ~Mask == ~Mask
4548 // -> ~Mask u<= x
4550 break;
4552 // x & Mask != x
4553 // x & ~Mask != 0
4554 // ~x | Mask != -1
4555 // -> x u> Mask
4556 // x & ~Mask != ~Mask
4557 // -> ~Mask u> x
4559 break;
4561 // x & Mask u< x
4562 // -> x u> Mask
4563 // x & ~Mask u< ~Mask
4564 // -> ~Mask u> x
4566 break;
4568 // x & Mask u>= x
4569 // -> x u<= Mask
4570 // x & ~Mask u>= ~Mask
4571 // -> ~Mask u<= x
4573 break;
4575 // x & Mask s< x [iff Mask s>= 0]
4576 // -> x s> Mask
4577 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4578 // -> ~Mask s> x
4580 break;
4582 // x & Mask s>= x [iff Mask s>= 0]
4583 // -> x s<= Mask
4584 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4585 // -> ~Mask s<= x
4587 break;
4588 default:
4589 // We don't support sgt,sle
4590 // ult/ugt are simplified to true/false respectively.
4591 return nullptr;
4592 }
4593
4594 Value *X, *M;
4595 // Put search code in lambda for early positive returns.
4596 auto IsLowBitMask = [&]() {
4597 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4598 X = Op1;
4599 // Look for: x & Mask pred x
4600 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4601 return !ICmpInst::isSigned(Pred) ||
4602 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4603 }
4604
4605 // Look for: x & ~Mask pred ~Mask
4606 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4607 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4608 }
4609 return false;
4610 }
4611 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4612 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4613
4614 auto Check = [&]() {
4615 // Look for: ~x | Mask == -1
4616 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4617 if (Value *NotX =
4618 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4619 X = NotX;
4620 return true;
4621 }
4622 }
4623 return false;
4624 };
4625 if (Check())
4626 return true;
4627 std::swap(X, M);
4628 return Check();
4629 }
4630 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4631 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4632 auto Check = [&]() {
4633 // Look for: x & ~Mask == 0
4634 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4635 if (Value *NotM =
4636 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4637 M = NotM;
4638 return true;
4639 }
4640 }
4641 return false;
4642 };
4643 if (Check())
4644 return true;
4645 std::swap(X, M);
4646 return Check();
4647 }
4648 return false;
4649 };
4650
4651 if (!IsLowBitMask())
4652 return nullptr;
4653
4654 return IC.Builder.CreateICmp(DstPred, X, M);
4655}
4656
4657/// Some comparisons can be simplified.
4658/// In this case, we are looking for comparisons that look like
4659/// a check for a lossy signed truncation.
4660/// Folds: (MaskedBits is a constant.)
4661/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4662/// Into:
4663/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4664/// Where KeptBits = bitwidth(%x) - MaskedBits
4665static Value *
4667 InstCombiner::BuilderTy &Builder) {
4668 CmpPredicate SrcPred;
4669 Value *X;
4670 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4671 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4672 if (!match(&I, m_c_ICmp(SrcPred,
4674 m_APInt(C1))),
4675 m_Deferred(X))))
4676 return nullptr;
4677
4678 // Potential handling of non-splats: for each element:
4679 // * if both are undef, replace with constant 0.
4680 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4681 // * if both are not undef, and are different, bailout.
4682 // * else, only one is undef, then pick the non-undef one.
4683
4684 // The shift amount must be equal.
4685 if (*C0 != *C1)
4686 return nullptr;
4687 const APInt &MaskedBits = *C0;
4688 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4689
4690 ICmpInst::Predicate DstPred;
4691 switch (SrcPred) {
4693 // ((%x << MaskedBits) a>> MaskedBits) == %x
4694 // =>
4695 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4697 break;
4699 // ((%x << MaskedBits) a>> MaskedBits) != %x
4700 // =>
4701 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4703 break;
4704 // FIXME: are more folds possible?
4705 default:
4706 return nullptr;
4707 }
4708
4709 auto *XType = X->getType();
4710 const unsigned XBitWidth = XType->getScalarSizeInBits();
4711 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4712 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4713
4714 // KeptBits = bitwidth(%x) - MaskedBits
4715 const APInt KeptBits = BitWidth - MaskedBits;
4716 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4717 // ICmpCst = (1 << KeptBits)
4718 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4719 assert(ICmpCst.isPowerOf2());
4720 // AddCst = (1 << (KeptBits-1))
4721 const APInt AddCst = ICmpCst.lshr(1);
4722 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4723
4724 // T0 = add %x, AddCst
4725 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4726 // T1 = T0 DstPred ICmpCst
4727 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4728
4729 return T1;
4730}
4731
4732// Given pattern:
4733// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4734// we should move shifts to the same hand of 'and', i.e. rewrite as
4735// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4736// We are only interested in opposite logical shifts here.
4737// One of the shifts can be truncated.
4738// If we can, we want to end up creating 'lshr' shift.
4739static Value *
4741 InstCombiner::BuilderTy &Builder) {
4742 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4743 !I.getOperand(0)->hasOneUse())
4744 return nullptr;
4745
4746 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4747
4748 // Look for an 'and' of two logical shifts, one of which may be truncated.
4749 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4750 Instruction *XShift, *MaybeTruncation, *YShift;
4751 if (!match(
4752 I.getOperand(0),
4753 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4755 m_AnyLogicalShift, m_Instruction(YShift))),
4756 m_Instruction(MaybeTruncation)))))
4757 return nullptr;
4758
4759 // We potentially looked past 'trunc', but only when matching YShift,
4760 // therefore YShift must have the widest type.
4761 Instruction *WidestShift = YShift;
4762 // Therefore XShift must have the shallowest type.
4763 // Or they both have identical types if there was no truncation.
4764 Instruction *NarrowestShift = XShift;
4765
4766 Type *WidestTy = WidestShift->getType();
4767 Type *NarrowestTy = NarrowestShift->getType();
4768 assert(NarrowestTy == I.getOperand(0)->getType() &&
4769 "We did not look past any shifts while matching XShift though.");
4770 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4771
4772 // If YShift is a 'lshr', swap the shifts around.
4773 if (match(YShift, m_LShr(m_Value(), m_Value())))
4774 std::swap(XShift, YShift);
4775
4776 // The shifts must be in opposite directions.
4777 auto XShiftOpcode = XShift->getOpcode();
4778 if (XShiftOpcode == YShift->getOpcode())
4779 return nullptr; // Do not care about same-direction shifts here.
4780
4781 Value *X, *XShAmt, *Y, *YShAmt;
4782 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4783 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4784
4785 // If one of the values being shifted is a constant, then we will end with
4786 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4787 // however, we will need to ensure that we won't increase instruction count.
4788 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4789 // At least one of the hands of the 'and' should be one-use shift.
4790 if (!match(I.getOperand(0),
4791 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4792 return nullptr;
4793 if (HadTrunc) {
4794 // Due to the 'trunc', we will need to widen X. For that either the old
4795 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4796 if (!MaybeTruncation->hasOneUse() &&
4797 !NarrowestShift->getOperand(1)->hasOneUse())
4798 return nullptr;
4799 }
4800 }
4801
4802 // We have two shift amounts from two different shifts. The types of those
4803 // shift amounts may not match. If that's the case let's bailout now.
4804 if (XShAmt->getType() != YShAmt->getType())
4805 return nullptr;
4806
4807 // As input, we have the following pattern:
4808 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4809 // We want to rewrite that as:
4810 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4811 // While we know that originally (Q+K) would not overflow
4812 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4813 // shift amounts. so it may now overflow in smaller bitwidth.
4814 // To ensure that does not happen, we need to ensure that the total maximal
4815 // shift amount is still representable in that smaller bit width.
4816 unsigned MaximalPossibleTotalShiftAmount =
4817 (WidestTy->getScalarSizeInBits() - 1) +
4818 (NarrowestTy->getScalarSizeInBits() - 1);
4819 APInt MaximalRepresentableShiftAmount =
4821 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4822 return nullptr;
4823
4824 // Can we fold (XShAmt+YShAmt) ?
4825 auto *NewShAmt = dyn_cast_or_null<Constant>(
4826 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4827 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4828 if (!NewShAmt)
4829 return nullptr;
4830 if (NewShAmt->getType() != WidestTy) {
4831 NewShAmt =
4832 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4833 if (!NewShAmt)
4834 return nullptr;
4835 }
4836 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4837
4838 // Is the new shift amount smaller than the bit width?
4839 // FIXME: could also rely on ConstantRange.
4840 if (!match(NewShAmt,
4842 APInt(WidestBitWidth, WidestBitWidth))))
4843 return nullptr;
4844
4845 // An extra legality check is needed if we had trunc-of-lshr.
4846 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4847 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4848 WidestShift]() {
4849 // It isn't obvious whether it's worth it to analyze non-constants here.
4850 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4851 // If *any* of these preconditions matches we can perform the fold.
4852 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4853 ? NewShAmt->getSplatValue()
4854 : NewShAmt;
4855 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4856 if (NewShAmtSplat &&
4857 (NewShAmtSplat->isNullValue() ||
4858 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4859 return true;
4860 // We consider *min* leading zeros so a single outlier
4861 // blocks the transform as opposed to allowing it.
4862 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4863 KnownBits Known = computeKnownBits(C, SQ.DL);
4864 unsigned MinLeadZero = Known.countMinLeadingZeros();
4865 // If the value being shifted has at most lowest bit set we can fold.
4866 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4867 if (MaxActiveBits <= 1)
4868 return true;
4869 // Precondition: NewShAmt u<= countLeadingZeros(C)
4870 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4871 return true;
4872 }
4873 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4874 KnownBits Known = computeKnownBits(C, SQ.DL);
4875 unsigned MinLeadZero = Known.countMinLeadingZeros();
4876 // If the value being shifted has at most lowest bit set we can fold.
4877 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4878 if (MaxActiveBits <= 1)
4879 return true;
4880 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4881 if (NewShAmtSplat) {
4882 APInt AdjNewShAmt =
4883 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4884 if (AdjNewShAmt.ule(MinLeadZero))
4885 return true;
4886 }
4887 }
4888 return false; // Can't tell if it's ok.
4889 };
4890 if (!CanFold())
4891 return nullptr;
4892 }
4893
4894 // All good, we can do this fold.
4895 X = Builder.CreateZExt(X, WidestTy);
4896 Y = Builder.CreateZExt(Y, WidestTy);
4897 // The shift is the same that was for X.
4898 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4899 ? Builder.CreateLShr(X, NewShAmt)
4900 : Builder.CreateShl(X, NewShAmt);
4901 Value *T1 = Builder.CreateAnd(T0, Y);
4902 return Builder.CreateICmp(I.getPredicate(), T1,
4903 Constant::getNullValue(WidestTy));
4904}
4905
4906/// Fold
4907/// (-1 u/ x) u< y
4908/// ((x * y) ?/ x) != y
4909/// to
4910/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4911/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4912/// will mean that we are looking for the opposite answer.
4914 CmpPredicate Pred;
4915 Value *X, *Y;
4917 Instruction *Div;
4918 bool NeedNegation;
4919 // Look for: (-1 u/ x) u</u>= y
4920 if (!I.isEquality() &&
4921 match(&I, m_c_ICmp(Pred,
4923 m_Instruction(Div)),
4924 m_Value(Y)))) {
4925 Mul = nullptr;
4926
4927 // Are we checking that overflow does not happen, or does happen?
4928 switch (Pred) {
4930 NeedNegation = false;
4931 break; // OK
4933 NeedNegation = true;
4934 break; // OK
4935 default:
4936 return nullptr; // Wrong predicate.
4937 }
4938 } else // Look for: ((x * y) / x) !=/== y
4939 if (I.isEquality() &&
4940 match(&I, m_c_ICmp(Pred, m_Value(Y),
4943 m_Value(X)),
4945 m_Deferred(X))),
4946 m_Instruction(Div))))) {
4947 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4948 } else
4949 return nullptr;
4950
4952 // If the pattern included (x * y), we'll want to insert new instructions
4953 // right before that original multiplication so that we can replace it.
4954 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4955 if (MulHadOtherUses)
4957
4959 Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow
4960 : Intrinsic::smul_with_overflow,
4961 X->getType(), {X, Y}, /*FMFSource=*/nullptr, "mul");
4962
4963 // If the multiplication was used elsewhere, to ensure that we don't leave
4964 // "duplicate" instructions, replace uses of that original multiplication
4965 // with the multiplication result from the with.overflow intrinsic.
4966 if (MulHadOtherUses)
4967 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4968
4969 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4970 if (NeedNegation) // This technically increases instruction count.
4971 Res = Builder.CreateNot(Res, "mul.not.ov");
4972
4973 // If we replaced the mul, erase it. Do this after all uses of Builder,
4974 // as the mul is used as insertion point.
4975 if (MulHadOtherUses)
4977
4978 return Res;
4979}
4980
4982 InstCombiner::BuilderTy &Builder) {
4983 CmpPredicate Pred;
4984 Value *X;
4985 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4986
4987 if (ICmpInst::isSigned(Pred))
4988 Pred = ICmpInst::getSwappedPredicate(Pred);
4989 else if (ICmpInst::isUnsigned(Pred))
4990 Pred = ICmpInst::getSignedPredicate(Pred);
4991 // else for equality-comparisons just keep the predicate.
4992
4993 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4994 Constant::getNullValue(X->getType()), I.getName());
4995 }
4996
4997 // A value is not equal to its negation unless that value is 0 or
4998 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4999 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
5000 ICmpInst::isEquality(Pred)) {
5001 Type *Ty = X->getType();
5003 Constant *MaxSignedVal =
5004 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
5005 Value *And = Builder.CreateAnd(X, MaxSignedVal);
5006 Constant *Zero = Constant::getNullValue(Ty);
5007 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
5008 }
5009
5010 return nullptr;
5011}
5012
5014 InstCombinerImpl &IC) {
5015 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5016 // Normalize and operand as operand 0.
5017 CmpInst::Predicate Pred = I.getPredicate();
5018 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
5019 std::swap(Op0, Op1);
5020 Pred = ICmpInst::getSwappedPredicate(Pred);
5021 }
5022
5023 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
5024 return nullptr;
5025
5026 // (icmp (X & Y) u< X --> (X & Y) != X
5027 if (Pred == ICmpInst::ICMP_ULT)
5028 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5029
5030 // (icmp (X & Y) u>= X --> (X & Y) == X
5031 if (Pred == ICmpInst::ICMP_UGE)
5032 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5033
5034 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
5035 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
5036 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
5037 // so don't do this fold.
5038 if (!match(Op1, m_ImmConstant()))
5039 if (auto *NotOp1 =
5040 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
5041 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),
5042 Constant::getAllOnesValue(Op1->getType()));
5043 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
5044 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
5045 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),
5046 Constant::getNullValue(Op1->getType()));
5047 }
5048
5049 if (!ICmpInst::isSigned(Pred))
5050 return nullptr;
5051
5052 KnownBits KnownY = IC.computeKnownBits(A, &I);
5053 // (X & NegY) spred X --> (X & NegY) upred X
5054 if (KnownY.isNegative())
5055 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
5056
5057 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
5058 return nullptr;
5059
5060 if (KnownY.isNonNegative())
5061 // (X & PosY) s<= X --> X s>= 0
5062 // (X & PosY) s> X --> X s< 0
5063 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
5064 Constant::getNullValue(Op1->getType()));
5065
5067 // (NegX & Y) s<= NegX --> Y s< 0
5068 // (NegX & Y) s> NegX --> Y s>= 0
5070 Constant::getNullValue(A->getType()));
5071
5072 return nullptr;
5073}
5074
5076 InstCombinerImpl &IC) {
5077 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5078
5079 // Normalize or operand as operand 0.
5080 CmpInst::Predicate Pred = I.getPredicate();
5081 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
5082 std::swap(Op0, Op1);
5083 Pred = ICmpInst::getSwappedPredicate(Pred);
5084 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
5085 return nullptr;
5086 }
5087
5088 // icmp (X | Y) u<= X --> (X | Y) == X
5089 if (Pred == ICmpInst::ICMP_ULE)
5090 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5091
5092 // icmp (X | Y) u> X --> (X | Y) != X
5093 if (Pred == ICmpInst::ICMP_UGT)
5094 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5095
5096 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
5097 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
5098 if (Value *NotOp1 = IC.getFreelyInverted(
5099 Op1, !isa<Constant>(Op1) && !Op1->hasNUsesOrMore(3), &IC.Builder))
5100 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
5101 Constant::getNullValue(Op1->getType()));
5102 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
5103 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
5104 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
5105 Constant::getAllOnesValue(Op1->getType()));
5106 }
5107 return nullptr;
5108}
5109
5111 InstCombinerImpl &IC) {
5112 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5113 // Normalize xor operand as operand 0.
5114 CmpInst::Predicate Pred = I.getPredicate();
5115 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
5116 std::swap(Op0, Op1);
5117 Pred = ICmpInst::getSwappedPredicate(Pred);
5118 }
5119 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
5120 return nullptr;
5121
5122 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
5123 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
5124 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
5125 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
5127 if (PredOut != Pred && isKnownNonZero(A, Q))
5128 return new ICmpInst(PredOut, Op0, Op1);
5129
5130 // These transform work when A is negative.
5131 // X s< X^A, X s<= X^A, X u> X^A, X u>= X^A --> X s< 0
5132 // X s> X^A, X s>= X^A, X u< X^A, X u<= X^A --> X s>= 0
5133 if (match(A, m_Negative())) {
5134 CmpInst::Predicate NewPred;
5135 switch (ICmpInst::getStrictPredicate(Pred)) {
5136 default:
5137 return nullptr;
5138 case ICmpInst::ICMP_SLT:
5139 case ICmpInst::ICMP_UGT:
5140 NewPred = ICmpInst::ICMP_SLT;
5141 break;
5142 case ICmpInst::ICMP_SGT:
5143 case ICmpInst::ICMP_ULT:
5144 NewPred = ICmpInst::ICMP_SGE;
5145 break;
5146 }
5147 Constant *Const = Constant::getNullValue(Op0->getType());
5148 return new ICmpInst(NewPred, Op0, Const);
5149 }
5150
5151 return nullptr;
5152}
5153
5154/// Return true if X is a multiple of C.
5155/// TODO: Handle non-power-of-2 factors.
5156static bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q) {
5157 if (C.isOne())
5158 return true;
5159
5160 if (!C.isPowerOf2())
5161 return false;
5162
5163 return MaskedValueIsZero(X, C - 1, Q);
5164}
5165
5166/// Try to fold icmp (binop), X or icmp X, (binop).
5167/// TODO: A large part of this logic is duplicated in InstSimplify's
5168/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
5169/// duplication.
5171 const SimplifyQuery &SQ) {
5173 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5174
5175 // Special logic for binary operators.
5176 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
5177 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
5178 if (!BO0 && !BO1)
5179 return nullptr;
5180
5181 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
5182 return NewICmp;
5183
5184 const CmpInst::Predicate Pred = I.getPredicate();
5185 Value *X;
5186
5187 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
5188 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
5189 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
5190 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5191 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
5192 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
5193 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
5194 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5195 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
5196
5197 {
5198 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
5199 Constant *C;
5200 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
5201 m_ImmConstant(C)))) &&
5202 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
5204 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
5205 }
5206 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5207 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
5208 m_ImmConstant(C)))) &&
5209 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5211 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
5212 }
5213 }
5214
5215 // (icmp eq/ne (X, -P2), INT_MIN)
5216 // -> (icmp slt/sge X, INT_MIN + P2)
5217 if (ICmpInst::isEquality(Pred) && BO0 &&
5218 match(I.getOperand(1), m_SignMask()) &&
5220 // Will Constant fold.
5221 Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
5222 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5224 BO0->getOperand(0), NewC);
5225 }
5226
5227 {
5228 // Similar to above: an unsigned overflow comparison may use offset + mask:
5229 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5230 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5231 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5232 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5233 BinaryOperator *BO;
5234 const APInt *C;
5235 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5236 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5238 CmpInst::Predicate NewPred =
5240 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5241 return new ICmpInst(NewPred, Op1, Zero);
5242 }
5243
5244 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5245 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5247 CmpInst::Predicate NewPred =
5249 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5250 return new ICmpInst(NewPred, Op0, Zero);
5251 }
5252 }
5253
5254 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5255 bool Op0HasNUW = false, Op1HasNUW = false;
5256 bool Op0HasNSW = false, Op1HasNSW = false;
5257 // Analyze the case when either Op0 or Op1 is an add instruction.
5258 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5259 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5260 bool &HasNSW, bool &HasNUW) -> bool {
5261 if (isa<OverflowingBinaryOperator>(BO)) {
5262 HasNUW = BO.hasNoUnsignedWrap();
5263 HasNSW = BO.hasNoSignedWrap();
5264 return ICmpInst::isEquality(Pred) ||
5265 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5266 (CmpInst::isSigned(Pred) && HasNSW);
5267 } else if (BO.getOpcode() == Instruction::Or) {
5268 HasNUW = true;
5269 HasNSW = true;
5270 return true;
5271 } else {
5272 return false;
5273 }
5274 };
5275 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5276
5277 if (BO0) {
5278 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
5279 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5280 }
5281 if (BO1) {
5282 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
5283 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5284 }
5285
5286 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5287 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5288 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5289 return new ICmpInst(Pred, A == Op1 ? B : A,
5290 Constant::getNullValue(Op1->getType()));
5291
5292 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5293 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5294 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5295 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
5296 C == Op0 ? D : C);
5297
5298 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5299 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5300 NoOp1WrapProblem) {
5301 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5302 Value *Y, *Z;
5303 if (A == C) {
5304 // C + B == C + D -> B == D
5305 Y = B;
5306 Z = D;
5307 } else if (A == D) {
5308 // D + B == C + D -> B == C
5309 Y = B;
5310 Z = C;
5311 } else if (B == C) {
5312 // A + C == C + D -> A == D
5313 Y = A;
5314 Z = D;
5315 } else {
5316 assert(B == D);
5317 // A + D == C + D -> A == C
5318 Y = A;
5319 Z = C;
5320 }
5321 return new ICmpInst(Pred, Y, Z);
5322 }
5323
5324 if (ICmpInst::isRelational(Pred)) {
5325 // Return if both X and Y is divisible by Z/-Z.
5326 // TODO: Generalize to check if (X - Y) is divisible by Z/-Z.
5327 auto ShareCommonDivisor = [&Q](Value *X, Value *Y, Value *Z,
5328 bool IsNegative) -> bool {
5329 const APInt *OffsetC;
5330 if (!match(Z, m_APInt(OffsetC)))
5331 return false;
5332
5333 // Fast path for Z == 1/-1.
5334 if (IsNegative ? OffsetC->isAllOnes() : OffsetC->isOne())
5335 return true;
5336
5337 APInt C = *OffsetC;
5338 if (IsNegative)
5339 C.negate();
5340 // Note: -INT_MIN is also negative.
5341 if (!C.isStrictlyPositive())
5342 return false;
5343
5344 return isMultipleOf(X, C, Q) && isMultipleOf(Y, C, Q);
5345 };
5346
5347 // TODO: The subtraction-related identities shown below also hold, but
5348 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5349 // wouldn't happen even if they were implemented.
5350 //
5351 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5352 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5353 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5354 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5355
5356 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5357 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5358 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5359 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5360 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5361 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5362 if (A && NoOp0WrapProblem &&
5363 ShareCommonDivisor(A, Op1, B,
5364 ICmpInst::isLT(Pred) || ICmpInst::isGE(Pred)))
5366 Op1);
5367
5368 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5369 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5370 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5371 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5372 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5373 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5374 if (C && NoOp1WrapProblem &&
5375 ShareCommonDivisor(Op0, C, D,
5376 ICmpInst::isGT(Pred) || ICmpInst::isLE(Pred)))
5378 C);
5379 }
5380
5381 // if C1 has greater magnitude than C2:
5382 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5383 // s.t. C3 = C1 - C2
5384 //
5385 // if C2 has greater magnitude than C1:
5386 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5387 // s.t. C3 = C2 - C1
5388 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5389 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5390 const APInt *AP1, *AP2;
5391 // TODO: Support non-uniform vectors.
5392 // TODO: Allow poison passthrough if B or D's element is poison.
5393 if (match(B, m_APIntAllowPoison(AP1)) &&
5394 match(D, m_APIntAllowPoison(AP2)) &&
5395 AP1->isNegative() == AP2->isNegative()) {
5396 APInt AP1Abs = AP1->abs();
5397 APInt AP2Abs = AP2->abs();
5398 if (AP1Abs.uge(AP2Abs)) {
5399 APInt Diff = *AP1 - *AP2;
5400 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5401 Value *NewAdd = Builder.CreateAdd(
5402 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
5403 return new ICmpInst(Pred, NewAdd, C);
5404 } else {
5405 APInt Diff = *AP2 - *AP1;
5406 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5407 Value *NewAdd = Builder.CreateAdd(
5408 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
5409 return new ICmpInst(Pred, A, NewAdd);
5410 }
5411 }
5412 Constant *Cst1, *Cst2;
5413 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5414 ICmpInst::isEquality(Pred)) {
5415 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5416 Value *NewAdd = Builder.CreateAdd(C, Diff);
5417 return new ICmpInst(Pred, A, NewAdd);
5418 }
5419 }
5420
5421 // Analyze the case when either Op0 or Op1 is a sub instruction.
5422 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5423 A = nullptr;
5424 B = nullptr;
5425 C = nullptr;
5426 D = nullptr;
5427 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5428 A = BO0->getOperand(0);
5429 B = BO0->getOperand(1);
5430 }
5431 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5432 C = BO1->getOperand(0);
5433 D = BO1->getOperand(1);
5434 }
5435
5436 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5437 if (A == Op1 && NoOp0WrapProblem)
5438 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5439 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5440 if (C == Op0 && NoOp1WrapProblem)
5441 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5442
5443 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5444 // (A - B) u>/u<= A --> B u>/u<= A
5445 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5446 return new ICmpInst(Pred, B, A);
5447 // C u</u>= (C - D) --> C u</u>= D
5448 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5449 return new ICmpInst(Pred, C, D);
5450 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5451 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5452 isKnownNonZero(B, Q))
5454 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5455 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5456 isKnownNonZero(D, Q))
5458
5459 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5460 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5461 return new ICmpInst(Pred, A, C);
5462
5463 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5464 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5465 return new ICmpInst(Pred, D, B);
5466
5467 // icmp (0-X) < cst --> x > -cst
5468 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5469 Value *X;
5470 if (match(BO0, m_Neg(m_Value(X))))
5471 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5472 if (RHSC->isNotMinSignedValue())
5473 return new ICmpInst(I.getSwappedPredicate(), X,
5474 ConstantExpr::getNeg(RHSC));
5475 }
5476
5477 if (Instruction *R = foldICmpXorXX(I, Q, *this))
5478 return R;
5479 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5480 return R;
5481
5482 {
5483 // Try to remove shared multiplier from comparison:
5484 // X * Z pred Y * Z
5485 Value *X, *Y, *Z;
5486 if ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5487 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5488 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5489 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y))))) {
5490 if (ICmpInst::isSigned(Pred)) {
5491 if (Op0HasNSW && Op1HasNSW) {
5492 KnownBits ZKnown = computeKnownBits(Z, &I);
5493 if (ZKnown.isStrictlyPositive())
5494 return new ICmpInst(Pred, X, Y);
5495 if (ZKnown.isNegative())
5496 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y);
5499 if (LessThan && match(LessThan, m_One()))
5500 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z,
5501 Constant::getNullValue(Z->getType()));
5502 Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y,
5504 if (GreaterThan && match(GreaterThan, m_One()))
5505 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5506 }
5507 } else {
5508 bool NonZero;
5509 if (ICmpInst::isEquality(Pred)) {
5510 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0
5511 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&
5512 isKnownNonEqual(X, Y, SQ))
5513 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5514
5515 KnownBits ZKnown = computeKnownBits(Z, &I);
5516 // if Z % 2 != 0
5517 // X * Z eq/ne Y * Z -> X eq/ne Y
5518 if (ZKnown.countMaxTrailingZeros() == 0)
5519 return new ICmpInst(Pred, X, Y);
5520 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5521 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5522 // X * Z eq/ne Y * Z -> X eq/ne Y
5523 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5524 return new ICmpInst(Pred, X, Y);
5525 } else
5526 NonZero = isKnownNonZero(Z, Q);
5527
5528 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5529 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5530 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5531 return new ICmpInst(Pred, X, Y);
5532 }
5533 }
5534 }
5535
5536 BinaryOperator *SRem = nullptr;
5537 // icmp (srem X, Y), Y
5538 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5539 SRem = BO0;
5540 // icmp Y, (srem X, Y)
5541 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5542 Op0 == BO1->getOperand(1))
5543 SRem = BO1;
5544 if (SRem) {
5545 // We don't check hasOneUse to avoid increasing register pressure because
5546 // the value we use is the same value this instruction was already using.
5547 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5548 default:
5549 break;
5550 case ICmpInst::ICMP_EQ:
5551 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5552 case ICmpInst::ICMP_NE:
5553 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5554 case ICmpInst::ICMP_SGT:
5555 case ICmpInst::ICMP_SGE:
5556 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5558 case ICmpInst::ICMP_SLT:
5559 case ICmpInst::ICMP_SLE:
5560 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5562 }
5563 }
5564
5565 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5566 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5567 BO0->getOperand(1) == BO1->getOperand(1)) {
5568 switch (BO0->getOpcode()) {
5569 default:
5570 break;
5571 case Instruction::Add:
5572 case Instruction::Sub:
5573 case Instruction::Xor: {
5574 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5575 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5576
5577 const APInt *C;
5578 if (match(BO0->getOperand(1), m_APInt(C))) {
5579 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5580 if (C->isSignMask()) {
5581 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5582 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5583 }
5584
5585 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5586 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5587 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5588 NewPred = I.getSwappedPredicate(NewPred);
5589 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5590 }
5591 }
5592 break;
5593 }
5594 case Instruction::Mul: {
5595 if (!I.isEquality())
5596 break;
5597
5598 const APInt *C;
5599 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5600 !C->isOne()) {
5601 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5602 // Mask = -1 >> count-trailing-zeros(C).
5603 if (unsigned TZs = C->countr_zero()) {
5604 Constant *Mask = ConstantInt::get(
5605 BO0->getType(),
5606 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5607 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5608 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5609 return new ICmpInst(Pred, And1, And2);
5610 }
5611 }
5612 break;
5613 }
5614 case Instruction::UDiv:
5615 case Instruction::LShr:
5616 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5617 break;
5618 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5619
5620 case Instruction::SDiv:
5621 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5622 !BO0->isExact() || !BO1->isExact())
5623 break;
5624 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5625
5626 case Instruction::AShr:
5627 if (!BO0->isExact() || !BO1->isExact())
5628 break;
5629 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5630
5631 case Instruction::Shl: {
5632 bool NUW = Op0HasNUW && Op1HasNUW;
5633 bool NSW = Op0HasNSW && Op1HasNSW;
5634 if (!NUW && !NSW)
5635 break;
5636 if (!NSW && I.isSigned())
5637 break;
5638 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5639 }
5640 }
5641 }
5642
5643 if (BO0) {
5644 // Transform A & (L - 1) `ult` L --> L != 0
5645 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5646 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5647
5648 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5649 auto *Zero = Constant::getNullValue(BO0->getType());
5650 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5651 }
5652 }
5653
5654 // For unsigned predicates / eq / ne:
5655 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5656 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5657 if (!ICmpInst::isSigned(Pred)) {
5658 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5659 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5660 Constant::getNullValue(Op1->getType()));
5661 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5662 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5663 Constant::getNullValue(Op0->getType()), Op0);
5664 }
5665
5667 return replaceInstUsesWith(I, V);
5668
5669 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5670 return R;
5671
5673 return replaceInstUsesWith(I, V);
5674
5676 return replaceInstUsesWith(I, V);
5677
5678 return nullptr;
5679}
5680
5681/// Fold icmp Pred min|max(X, Y), Z.
5684 Value *Z, CmpPredicate Pred) {
5685 Value *X = MinMax->getLHS();
5686 Value *Y = MinMax->getRHS();
5687 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5688 return nullptr;
5689 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5690 // Revert the transform signed pred -> unsigned pred
5691 // TODO: We can flip the signedness of predicate if both operands of icmp
5692 // are negative.
5696 } else
5697 return nullptr;
5698 }
5700 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5701 if (!Val)
5702 return std::nullopt;
5703 if (match(Val, m_One()))
5704 return true;
5705 if (match(Val, m_Zero()))
5706 return false;
5707 return std::nullopt;
5708 };
5709 // Remove samesign here since it is illegal to keep it when we speculatively
5710 // execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
5711 // cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
5712 // -46, -32)`. `X` is allowed to be non-negative here.
5713 Pred = Pred.dropSameSign();
5714 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5715 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5716 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5717 return nullptr;
5718 if (!CmpXZ.has_value()) {
5719 std::swap(X, Y);
5720 std::swap(CmpXZ, CmpYZ);
5721 }
5722
5723 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5724 if (CmpYZ.has_value())
5725 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5726 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5727 };
5728
5729 switch (Pred) {
5730 case ICmpInst::ICMP_EQ:
5731 case ICmpInst::ICMP_NE: {
5732 // If X == Z:
5733 // Expr Result
5734 // min(X, Y) == Z X <= Y
5735 // max(X, Y) == Z X >= Y
5736 // min(X, Y) != Z X > Y
5737 // max(X, Y) != Z X < Y
5738 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5739 ICmpInst::Predicate NewPred =
5740 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5741 if (Pred == ICmpInst::ICMP_NE)
5742 NewPred = ICmpInst::getInversePredicate(NewPred);
5743 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5744 }
5745 // Otherwise (X != Z):
5746 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5747 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5748 if (!MinMaxCmpXZ.has_value()) {
5749 std::swap(X, Y);
5750 std::swap(CmpXZ, CmpYZ);
5751 // Re-check pre-condition X != Z
5752 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5753 break;
5754 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5755 }
5756 if (!MinMaxCmpXZ.has_value())
5757 break;
5758 if (*MinMaxCmpXZ) {
5759 // Expr Fact Result
5760 // min(X, Y) == Z X < Z false
5761 // max(X, Y) == Z X > Z false
5762 // min(X, Y) != Z X < Z true
5763 // max(X, Y) != Z X > Z true
5764 return replaceInstUsesWith(
5765 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5766 } else {
5767 // Expr Fact Result
5768 // min(X, Y) == Z X > Z Y == Z
5769 // max(X, Y) == Z X < Z Y == Z
5770 // min(X, Y) != Z X > Z Y != Z
5771 // max(X, Y) != Z X < Z Y != Z
5772 return FoldIntoCmpYZ();
5773 }
5774 break;
5775 }
5776 case ICmpInst::ICMP_SLT:
5777 case ICmpInst::ICMP_ULT:
5778 case ICmpInst::ICMP_SLE:
5779 case ICmpInst::ICMP_ULE:
5780 case ICmpInst::ICMP_SGT:
5781 case ICmpInst::ICMP_UGT:
5782 case ICmpInst::ICMP_SGE:
5783 case ICmpInst::ICMP_UGE: {
5784 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5785 if (*CmpXZ) {
5786 if (IsSame) {
5787 // Expr Fact Result
5788 // min(X, Y) < Z X < Z true
5789 // min(X, Y) <= Z X <= Z true
5790 // max(X, Y) > Z X > Z true
5791 // max(X, Y) >= Z X >= Z true
5792 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5793 } else {
5794 // Expr Fact Result
5795 // max(X, Y) < Z X < Z Y < Z
5796 // max(X, Y) <= Z X <= Z Y <= Z
5797 // min(X, Y) > Z X > Z Y > Z
5798 // min(X, Y) >= Z X >= Z Y >= Z
5799 return FoldIntoCmpYZ();
5800 }
5801 } else {
5802 if (IsSame) {
5803 // Expr Fact Result
5804 // min(X, Y) < Z X >= Z Y < Z
5805 // min(X, Y) <= Z X > Z Y <= Z
5806 // max(X, Y) > Z X <= Z Y > Z
5807 // max(X, Y) >= Z X < Z Y >= Z
5808 return FoldIntoCmpYZ();
5809 } else {
5810 // Expr Fact Result
5811 // max(X, Y) < Z X >= Z false
5812 // max(X, Y) <= Z X > Z false
5813 // min(X, Y) > Z X <= Z false
5814 // min(X, Y) >= Z X < Z false
5815 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5816 }
5817 }
5818 break;
5819 }
5820 default:
5821 break;
5822 }
5823
5824 return nullptr;
5825}
5826
5827// Canonicalize checking for a power-of-2-or-zero value:
5829 InstCombiner::BuilderTy &Builder) {
5830 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5831 const CmpInst::Predicate Pred = I.getPredicate();
5832 Value *A = nullptr;
5833 bool CheckIs;
5834 if (I.isEquality()) {
5835 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5836 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5837 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5838 m_Deferred(A)))) ||
5839 !match(Op1, m_ZeroInt()))
5840 A = nullptr;
5841
5842 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5843 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5844 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5845 A = Op1;
5846 else if (match(Op1,
5848 A = Op0;
5849
5850 CheckIs = Pred == ICmpInst::ICMP_EQ;
5851 } else if (ICmpInst::isUnsigned(Pred)) {
5852 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5853 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5854
5855 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5857 m_Specific(Op1))))) {
5858 A = Op1;
5859 CheckIs = Pred == ICmpInst::ICMP_UGE;
5860 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5862 m_Specific(Op0))))) {
5863 A = Op0;
5864 CheckIs = Pred == ICmpInst::ICMP_ULE;
5865 }
5866 }
5867
5868 if (A) {
5869 Type *Ty = A->getType();
5870 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5871 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5872 ConstantInt::get(Ty, 2))
5873 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5874 ConstantInt::get(Ty, 1));
5875 }
5876
5877 return nullptr;
5878}
5879
5880/// Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
5881using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;
5883 bool AllowRecursion) {
5884 Instruction *Inst = dyn_cast<Instruction>(V);
5885 if (!Inst || !Inst->hasOneUse())
5886 return;
5887
5888 switch (Inst->getOpcode()) {
5889 case Instruction::Add:
5890 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(1));
5891 Offsets.emplace_back(Instruction::Sub, Inst->getOperand(0));
5892 break;
5893 case Instruction::Sub:
5894 Offsets.emplace_back(Instruction::Add, Inst->getOperand(1));
5895 break;
5896 case Instruction::Xor:
5897 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(1));
5898 Offsets.emplace_back(Instruction::Xor, Inst->getOperand(0));
5899 break;
5900 case Instruction::Select:
5901 if (AllowRecursion) {
5902 collectOffsetOp(Inst->getOperand(1), Offsets, /*AllowRecursion=*/false);
5903 collectOffsetOp(Inst->getOperand(2), Offsets, /*AllowRecursion=*/false);
5904 }
5905 break;
5906 default:
5907 break;
5908 }
5909}
5910
5912
5915 Value *V0, *V1, *V2;
5916
5918 return {OffsetKind::Invalid, nullptr, nullptr, nullptr};
5919 }
5921 return {OffsetKind::Value, V, nullptr, nullptr};
5922 }
5923 static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV) {
5924 return {OffsetKind::Select, Cond, TrueV, FalseV};
5925 }
5926 bool isValid() const { return Kind != OffsetKind::Invalid; }
5928 switch (Kind) {
5929 case OffsetKind::Invalid:
5930 llvm_unreachable("Invalid offset result");
5931 case OffsetKind::Value:
5932 return V0;
5933 case OffsetKind::Select:
5934 return Builder.CreateSelect(V0, V1, V2);
5935 }
5936 llvm_unreachable("Unknown OffsetKind enum");
5937 }
5938};
5939
5940/// Offset both sides of an equality icmp to see if we can save some
5941/// instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.
5942/// Note: This operation should not introduce poison.
5944 InstCombiner::BuilderTy &Builder,
5945 const SimplifyQuery &SQ) {
5946 assert(I.isEquality() && "Expected an equality icmp");
5947 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5948 if (!Op0->getType()->isIntOrIntVectorTy())
5949 return nullptr;
5950
5951 SmallVector<OffsetOp, 4> OffsetOps;
5952 collectOffsetOp(Op0, OffsetOps, /*AllowRecursion=*/true);
5953 collectOffsetOp(Op1, OffsetOps, /*AllowRecursion=*/true);
5954
5955 auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {
5956 Value *Simplified = simplifyBinOp(BinOpc, V, RHS, SQ);
5957 // Avoid infinite loops by checking if RHS is an identity for the BinOp.
5958 if (!Simplified || Simplified == V)
5959 return nullptr;
5960 // Reject constant expressions as they don't simplify things.
5961 if (isa<Constant>(Simplified) && !match(Simplified, m_ImmConstant()))
5962 return nullptr;
5963 // Check if the transformation introduces poison.
5964 return impliesPoison(RHS, V) ? Simplified : nullptr;
5965 };
5966
5967 auto ApplyOffset = [&](Value *V, unsigned BinOpc,
5968 Value *RHS) -> OffsetResult {
5969 if (auto *Sel = dyn_cast<SelectInst>(V)) {
5970 if (!Sel->hasOneUse())
5971 return OffsetResult::invalid();
5972 Value *TrueVal = ApplyOffsetImpl(Sel->getTrueValue(), BinOpc, RHS);
5973 if (!TrueVal)
5974 return OffsetResult::invalid();
5975 Value *FalseVal = ApplyOffsetImpl(Sel->getFalseValue(), BinOpc, RHS);
5976 if (!FalseVal)
5977 return OffsetResult::invalid();
5978 return OffsetResult::select(Sel->getCondition(), TrueVal, FalseVal);
5979 }
5980 if (Value *Simplified = ApplyOffsetImpl(V, BinOpc, RHS))
5981 return OffsetResult::value(Simplified);
5982 return OffsetResult::invalid();
5983 };
5984
5985 for (auto [BinOp, RHS] : OffsetOps) {
5986 auto BinOpc = static_cast<unsigned>(BinOp);
5987
5988 auto Op0Result = ApplyOffset(Op0, BinOpc, RHS);
5989 if (!Op0Result.isValid())
5990 continue;
5991 auto Op1Result = ApplyOffset(Op1, BinOpc, RHS);
5992 if (!Op1Result.isValid())
5993 continue;
5994
5995 Value *NewLHS = Op0Result.materialize(Builder);
5996 Value *NewRHS = Op1Result.materialize(Builder);
5997 return new ICmpInst(I.getPredicate(), NewLHS, NewRHS);
5998 }
5999
6000 return nullptr;
6001}
6002
6004 if (!I.isEquality())
6005 return nullptr;
6006
6007 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6008 const CmpInst::Predicate Pred = I.getPredicate();
6009 Value *A, *B, *C, *D;
6010 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
6011 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
6012 Value *OtherVal = A == Op1 ? B : A;
6013 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
6014 }
6015
6016 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
6017 // A^c1 == C^c2 --> A == C^(c1^c2)
6018 ConstantInt *C1, *C2;
6019 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
6020 Op1->hasOneUse()) {
6021 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
6023 return new ICmpInst(Pred, A, Xor);
6024 }
6025
6026 // A^B == A^D -> B == D
6027 if (A == C)
6028 return new ICmpInst(Pred, B, D);
6029 if (A == D)
6030 return new ICmpInst(Pred, B, C);
6031 if (B == C)
6032 return new ICmpInst(Pred, A, D);
6033 if (B == D)
6034 return new ICmpInst(Pred, A, C);
6035 }
6036 }
6037
6038 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
6039 // A == (A^B) -> B == 0
6040 Value *OtherVal = A == Op0 ? B : A;
6041 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
6042 }
6043
6044 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
6045 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
6046 match(Op1, m_And(m_Value(C), m_Value(D)))) {
6047 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
6048
6049 if (A == C) {
6050 X = B;
6051 Y = D;
6052 Z = A;
6053 } else if (A == D) {
6054 X = B;
6055 Y = C;
6056 Z = A;
6057 } else if (B == C) {
6058 X = A;
6059 Y = D;
6060 Z = B;
6061 } else if (B == D) {
6062 X = A;
6063 Y = C;
6064 Z = B;
6065 }
6066
6067 if (X) {
6068 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
6069 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
6070 // instructions.
6071 const APInt *C0, *C1;
6072 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
6073 (*C0 ^ *C1).isNegatedPowerOf2();
6074
6075 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
6076 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
6077 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
6078 int UseCnt =
6079 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
6080 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
6081 if (XorIsNegP2 || UseCnt >= 2) {
6082 // Build (X^Y) & Z
6083 Op1 = Builder.CreateXor(X, Y);
6084 Op1 = Builder.CreateAnd(Op1, Z);
6085 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
6086 }
6087 }
6088 }
6089
6090 {
6091 // Similar to above, but specialized for constant because invert is needed:
6092 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
6093 Value *X, *Y;
6094 Constant *C;
6095 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
6096 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
6099 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
6100 }
6101 }
6102
6103 if (match(Op1, m_ZExt(m_Value(A))) &&
6104 (Op0->hasOneUse() || Op1->hasOneUse())) {
6105 // (B & (Pow2C-1)) == zext A --> A == trunc B
6106 // (B & (Pow2C-1)) != zext A --> A != trunc B
6107 const APInt *MaskC;
6108 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
6109 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
6110 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
6111 }
6112
6113 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
6114 // For lshr and ashr pairs.
6115 const APInt *AP1, *AP2;
6116 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
6117 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
6118 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
6119 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
6120 if (*AP1 != *AP2)
6121 return nullptr;
6122 unsigned TypeBits = AP1->getBitWidth();
6123 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
6124 if (ShAmt < TypeBits && ShAmt != 0) {
6125 ICmpInst::Predicate NewPred =
6127 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
6128 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
6129 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
6130 }
6131 }
6132
6133 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
6134 ConstantInt *Cst1;
6135 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
6136 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
6137 unsigned TypeBits = Cst1->getBitWidth();
6138 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
6139 if (ShAmt < TypeBits && ShAmt != 0) {
6140 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
6141 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
6142 Value *And =
6143 Builder.CreateAnd(Xor, Builder.getInt(AndVal), I.getName() + ".mask");
6144 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
6145 }
6146 }
6147
6148 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
6149 // "icmp (and X, mask), cst"
6150 uint64_t ShAmt = 0;
6151 if (Op0->hasOneUse() &&
6152 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
6153 match(Op1, m_ConstantInt(Cst1)) &&
6154 // Only do this when A has multiple uses. This is most important to do
6155 // when it exposes other optimizations.
6156 !A->hasOneUse()) {
6157 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
6158
6159 if (ShAmt < ASize) {
6160 APInt MaskV =
6162 MaskV <<= ShAmt;
6163
6164 APInt CmpV = Cst1->getValue().zext(ASize);
6165 CmpV <<= ShAmt;
6166
6167 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
6168 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
6169 }
6170 }
6171
6173 return ICmp;
6174
6175 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks
6176 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s
6177 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a
6178 // few steps of instcombine.
6179 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6180 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
6182 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
6183 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
6185 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
6186 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
6188 Add, ConstantInt::get(A->getType(), C.shl(1)));
6189 }
6190
6191 // Canonicalize:
6192 // Assume B_Pow2 != 0
6193 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
6194 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
6195 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
6196 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, &I))
6197 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6199
6200 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
6201 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, &I))
6202 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
6203 ConstantInt::getNullValue(Op1->getType()));
6204
6205 // Canonicalize:
6206 // icmp eq/ne X, OneUse(rotate-right(X))
6207 // -> icmp eq/ne X, rotate-left(X)
6208 // We generally try to convert rotate-right -> rotate-left, this just
6209 // canonicalizes another case.
6210 if (match(&I, m_c_ICmp(m_Value(A),
6211 m_OneUse(m_Intrinsic<Intrinsic::fshr>(
6212 m_Deferred(A), m_Deferred(A), m_Value(B))))))
6213 return new ICmpInst(
6214 Pred, A,
6215 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
6216
6217 // Canonicalize:
6218 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
6219 Constant *Cst;
6222 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
6223
6224 {
6225 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6226 auto m_Matcher =
6229 m_Sub(m_Value(B), m_Deferred(A)));
6230 std::optional<bool> IsZero = std::nullopt;
6231 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),
6232 m_Deferred(A))))
6233 IsZero = false;
6234 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6235 else if (match(&I,
6236 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))
6237 IsZero = true;
6238
6239 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, &I))
6240 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6241 // -> (icmp eq/ne (and X, P2), 0)
6242 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6243 // -> (icmp eq/ne (and X, P2), P2)
6244 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
6245 *IsZero ? A
6246 : ConstantInt::getNullValue(A->getType()));
6247 }
6248
6249 if (auto *Res = foldICmpEqualityWithOffset(
6250 I, Builder, getSimplifyQuery().getWithInstruction(&I)))
6251 return Res;
6252
6253 return nullptr;
6254}
6255
6257 ICmpInst::Predicate Pred = ICmp.getPredicate();
6258 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
6259
6260 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
6261 // The trunc masks high bits while the compare may effectively mask low bits.
6262 Value *X;
6263 const APInt *C;
6264 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
6265 return nullptr;
6266
6267 // This matches patterns corresponding to tests of the signbit as well as:
6268 // (trunc X) pred C2 --> (X & Mask) == C
6269 if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*WithTrunc=*/true,
6270 /*AllowNonZeroC=*/true)) {
6271 Value *And = Builder.CreateAnd(Res->X, Res->Mask);
6272 Constant *C = ConstantInt::get(Res->X->getType(), Res->C);
6273 return new ICmpInst(Res->Pred, And, C);
6274 }
6275
6276 unsigned SrcBits = X->getType()->getScalarSizeInBits();
6277 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
6278 if (II->getIntrinsicID() == Intrinsic::cttz ||
6279 II->getIntrinsicID() == Intrinsic::ctlz) {
6280 unsigned MaxRet = SrcBits;
6281 // If the "is_zero_poison" argument is set, then we know at least
6282 // one bit is set in the input, so the result is always at least one
6283 // less than the full bitwidth of that input.
6284 if (match(II->getArgOperand(1), m_One()))
6285 MaxRet--;
6286
6287 // Make sure the destination is wide enough to hold the largest output of
6288 // the intrinsic.
6289 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
6290 if (Instruction *I =
6291 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
6292 return I;
6293 }
6294 }
6295
6296 return nullptr;
6297}
6298
6300 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
6301 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
6302 Value *X;
6303 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
6304 return nullptr;
6305
6306 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
6307 bool IsSignedCmp = ICmp.isSigned();
6308
6309 // icmp Pred (ext X), (ext Y)
6310 Value *Y;
6311 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
6312 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
6313 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
6314
6315 if (IsZext0 != IsZext1) {
6316 // If X and Y and both i1
6317 // (icmp eq/ne (zext X) (sext Y))
6318 // eq -> (icmp eq (or X, Y), 0)
6319 // ne -> (icmp ne (or X, Y), 0)
6320 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
6321 Y->getType()->isIntOrIntVectorTy(1))
6322 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
6323 Constant::getNullValue(X->getType()));
6324
6325 // If we have mismatched casts and zext has the nneg flag, we can
6326 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
6327
6328 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
6329 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
6330
6331 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
6332 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
6333
6334 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
6335 IsSignedExt = true;
6336 else
6337 return nullptr;
6338 }
6339
6340 // Not an extension from the same type?
6341 Type *XTy = X->getType(), *YTy = Y->getType();
6342 if (XTy != YTy) {
6343 // One of the casts must have one use because we are creating a new cast.
6344 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
6345 return nullptr;
6346 // Extend the narrower operand to the type of the wider operand.
6347 CastInst::CastOps CastOpcode =
6348 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
6349 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
6350 X = Builder.CreateCast(CastOpcode, X, YTy);
6351 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6352 Y = Builder.CreateCast(CastOpcode, Y, XTy);
6353 else
6354 return nullptr;
6355 }
6356
6357 // (zext X) == (zext Y) --> X == Y
6358 // (sext X) == (sext Y) --> X == Y
6359 if (ICmp.isEquality())
6360 return new ICmpInst(ICmp.getPredicate(), X, Y);
6361
6362 // A signed comparison of sign extended values simplifies into a
6363 // signed comparison.
6364 if (IsSignedCmp && IsSignedExt)
6365 return new ICmpInst(ICmp.getPredicate(), X, Y);
6366
6367 // The other three cases all fold into an unsigned comparison.
6368 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6369 }
6370
6371 // Below here, we are only folding a compare with constant.
6372 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
6373 if (!C)
6374 return nullptr;
6375
6376 // If a lossless truncate is possible...
6377 Type *SrcTy = CastOp0->getSrcTy();
6378 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
6379 if (Res) {
6380 if (ICmp.isEquality())
6381 return new ICmpInst(ICmp.getPredicate(), X, Res);
6382
6383 // A signed comparison of sign extended values simplifies into a
6384 // signed comparison.
6385 if (IsSignedExt && IsSignedCmp)
6386 return new ICmpInst(ICmp.getPredicate(), X, Res);
6387
6388 // The other three cases all fold into an unsigned comparison.
6389 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6390 }
6391
6392 // The re-extended constant changed, partly changed (in the case of a vector),
6393 // or could not be determined to be equal (in the case of a constant
6394 // expression), so the constant cannot be represented in the shorter type.
6395 // All the cases that fold to true or false will have already been handled
6396 // by simplifyICmpInst, so only deal with the tricky case.
6397 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
6398 return nullptr;
6399
6400 // Is source op positive?
6401 // icmp ult (sext X), C --> icmp sgt X, -1
6402 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6404
6405 // Is source op negative?
6406 // icmp ugt (sext X), C --> icmp slt X, 0
6407 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6409}
6410
6411/// Handle icmp (cast x), (cast or constant).
6413 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6414 // icmp compares only pointer's value.
6415 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6416 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
6417 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
6418 if (SimplifiedOp0 || SimplifiedOp1)
6419 return new ICmpInst(ICmp.getPredicate(),
6420 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
6421 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
6422
6423 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
6424 if (!CastOp0)
6425 return nullptr;
6426 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
6427 return nullptr;
6428
6429 Value *Op0Src = CastOp0->getOperand(0);
6430 Type *SrcTy = CastOp0->getSrcTy();
6431 Type *DestTy = CastOp0->getDestTy();
6432
6433 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6434 // integer type is the same size as the pointer type.
6435 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6436 if (isa<VectorType>(PtrTy)) {
6437 PtrTy = cast<VectorType>(PtrTy)->getElementType();
6438 IntTy = cast<VectorType>(IntTy)->getElementType();
6439 }
6440 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
6441 };
6442 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6443 CompatibleSizes(SrcTy, DestTy)) {
6444 Value *NewOp1 = nullptr;
6445 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
6446 Value *PtrSrc = PtrToIntOp1->getOperand(0);
6447 if (PtrSrc->getType() == Op0Src->getType())
6448 NewOp1 = PtrToIntOp1->getOperand(0);
6449 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6450 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
6451 }
6452
6453 if (NewOp1)
6454 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6455 }
6456
6457 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6458 if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6459 CompatibleSizes(DestTy, SrcTy)) {
6460 Value *NewOp1 = nullptr;
6461 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
6462 Value *IntSrc = IntToPtrOp1->getOperand(0);
6463 if (IntSrc->getType() == Op0Src->getType())
6464 NewOp1 = IntToPtrOp1->getOperand(0);
6465 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6466 NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
6467 }
6468
6469 if (NewOp1)
6470 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6471 }
6472
6473 if (Instruction *R = foldICmpWithTrunc(ICmp))
6474 return R;
6475
6476 return foldICmpWithZextOrSext(ICmp);
6477}
6478
6480 bool IsSigned) {
6481 switch (BinaryOp) {
6482 default:
6483 llvm_unreachable("Unsupported binary op");
6484 case Instruction::Add:
6485 case Instruction::Sub:
6486 return match(RHS, m_Zero());
6487 case Instruction::Mul:
6488 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
6489 match(RHS, m_One());
6490 }
6491}
6492
6495 bool IsSigned, Value *LHS, Value *RHS,
6496 Instruction *CxtI) const {
6497 switch (BinaryOp) {
6498 default:
6499 llvm_unreachable("Unsupported binary op");
6500 case Instruction::Add:
6501 if (IsSigned)
6502 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6503 else
6504 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6505 case Instruction::Sub:
6506 if (IsSigned)
6507 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6508 else
6509 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6510 case Instruction::Mul:
6511 if (IsSigned)
6512 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6513 else
6514 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6515 }
6516}
6517
6518bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6519 bool IsSigned, Value *LHS,
6520 Value *RHS, Instruction &OrigI,
6521 Value *&Result,
6522 Constant *&Overflow) {
6523 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
6524 std::swap(LHS, RHS);
6525
6526 // If the overflow check was an add followed by a compare, the insertion point
6527 // may be pointing to the compare. We want to insert the new instructions
6528 // before the add in case there are uses of the add between the add and the
6529 // compare.
6530 Builder.SetInsertPoint(&OrigI);
6531
6532 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
6533 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
6534 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
6535
6536 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6537 Result = LHS;
6538 Overflow = ConstantInt::getFalse(OverflowTy);
6539 return true;
6540 }
6541
6542 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
6544 return false;
6547 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6548 Result->takeName(&OrigI);
6549 Overflow = ConstantInt::getTrue(OverflowTy);
6550 return true;
6552 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6553 Result->takeName(&OrigI);
6554 Overflow = ConstantInt::getFalse(OverflowTy);
6555 if (auto *Inst = dyn_cast<Instruction>(Result)) {
6556 if (IsSigned)
6557 Inst->setHasNoSignedWrap();
6558 else
6559 Inst->setHasNoUnsignedWrap();
6560 }
6561 return true;
6562 }
6563
6564 llvm_unreachable("Unexpected overflow result");
6565}
6566
6567/// Recognize and process idiom involving test for multiplication
6568/// overflow.
6569///
6570/// The caller has matched a pattern of the form:
6571/// I = cmp u (mul(zext A, zext B), V
6572/// The function checks if this is a test for overflow and if so replaces
6573/// multiplication with call to 'mul.with.overflow' intrinsic.
6574///
6575/// \param I Compare instruction.
6576/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6577/// the compare instruction. Must be of integer type.
6578/// \param OtherVal The other argument of compare instruction.
6579/// \returns Instruction which must replace the compare instruction, NULL if no
6580/// replacement required.
6582 const APInt *OtherVal,
6583 InstCombinerImpl &IC) {
6584 // Don't bother doing this transformation for pointers, don't do it for
6585 // vectors.
6586 if (!isa<IntegerType>(MulVal->getType()))
6587 return nullptr;
6588
6589 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6590 if (!MulInstr)
6591 return nullptr;
6592 assert(MulInstr->getOpcode() == Instruction::Mul);
6593
6594 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6595 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6596 assert(LHS->getOpcode() == Instruction::ZExt);
6597 assert(RHS->getOpcode() == Instruction::ZExt);
6598 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6599
6600 // Calculate type and width of the result produced by mul.with.overflow.
6601 Type *TyA = A->getType(), *TyB = B->getType();
6602 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6603 WidthB = TyB->getPrimitiveSizeInBits();
6604 unsigned MulWidth;
6605 Type *MulType;
6606 if (WidthB > WidthA) {
6607 MulWidth = WidthB;
6608 MulType = TyB;
6609 } else {
6610 MulWidth = WidthA;
6611 MulType = TyA;
6612 }
6613
6614 // In order to replace the original mul with a narrower mul.with.overflow,
6615 // all uses must ignore upper bits of the product. The number of used low
6616 // bits must be not greater than the width of mul.with.overflow.
6617 if (MulVal->hasNUsesOrMore(2))
6618 for (User *U : MulVal->users()) {
6619 if (U == &I)
6620 continue;
6621 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6622 // Check if truncation ignores bits above MulWidth.
6623 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6624 if (TruncWidth > MulWidth)
6625 return nullptr;
6626 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6627 // Check if AND ignores bits above MulWidth.
6628 if (BO->getOpcode() != Instruction::And)
6629 return nullptr;
6630 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6631 const APInt &CVal = CI->getValue();
6632 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6633 return nullptr;
6634 } else {
6635 // In this case we could have the operand of the binary operation
6636 // being defined in another block, and performing the replacement
6637 // could break the dominance relation.
6638 return nullptr;
6639 }
6640 } else {
6641 // Other uses prohibit this transformation.
6642 return nullptr;
6643 }
6644 }
6645
6646 // Recognize patterns
6647 switch (I.getPredicate()) {
6648 case ICmpInst::ICMP_UGT: {
6649 // Recognize pattern:
6650 // mulval = mul(zext A, zext B)
6651 // cmp ugt mulval, max
6652 APInt MaxVal = APInt::getMaxValue(MulWidth);
6653 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6654 if (MaxVal.eq(*OtherVal))
6655 break; // Recognized
6656 return nullptr;
6657 }
6658
6659 case ICmpInst::ICMP_ULT: {
6660 // Recognize pattern:
6661 // mulval = mul(zext A, zext B)
6662 // cmp ule mulval, max + 1
6663 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6664 if (MaxVal.eq(*OtherVal))
6665 break; // Recognized
6666 return nullptr;
6667 }
6668
6669 default:
6670 return nullptr;
6671 }
6672
6673 InstCombiner::BuilderTy &Builder = IC.Builder;
6674 Builder.SetInsertPoint(MulInstr);
6675
6676 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6677 Value *MulA = A, *MulB = B;
6678 if (WidthA < MulWidth)
6679 MulA = Builder.CreateZExt(A, MulType);
6680 if (WidthB < MulWidth)
6681 MulB = Builder.CreateZExt(B, MulType);
6682 CallInst *Call =
6683 Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, MulType,
6684 {MulA, MulB}, /*FMFSource=*/nullptr, "umul");
6685 IC.addToWorklist(MulInstr);
6686
6687 // If there are uses of mul result other than the comparison, we know that
6688 // they are truncation or binary AND. Change them to use result of
6689 // mul.with.overflow and adjust properly mask/size.
6690 if (MulVal->hasNUsesOrMore(2)) {
6691 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6692 for (User *U : make_early_inc_range(MulVal->users())) {
6693 if (U == &I)
6694 continue;
6695 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6696 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6697 IC.replaceInstUsesWith(*TI, Mul);
6698 else
6699 TI->setOperand(0, Mul);
6700 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6701 assert(BO->getOpcode() == Instruction::And);
6702 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6703 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6704 APInt ShortMask = CI->getValue().trunc(MulWidth);
6705 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6706 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6707 IC.replaceInstUsesWith(*BO, Zext);
6708 } else {
6709 llvm_unreachable("Unexpected Binary operation");
6710 }
6711 IC.addToWorklist(cast<Instruction>(U));
6712 }
6713 }
6714
6715 // The original icmp gets replaced with the overflow value, maybe inverted
6716 // depending on predicate.
6717 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6718 Value *Res = Builder.CreateExtractValue(Call, 1);
6719 return BinaryOperator::CreateNot(Res);
6720 }
6721
6722 return ExtractValueInst::Create(Call, 1);
6723}
6724
6725/// When performing a comparison against a constant, it is possible that not all
6726/// the bits in the LHS are demanded. This helper method computes the mask that
6727/// IS demanded.
6729 const APInt *RHS;
6730 if (!match(I.getOperand(1), m_APInt(RHS)))
6732
6733 // If this is a normal comparison, it demands all bits. If it is a sign bit
6734 // comparison, it only demands the sign bit.
6735 bool UnusedBit;
6736 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6738
6739 switch (I.getPredicate()) {
6740 // For a UGT comparison, we don't care about any bits that
6741 // correspond to the trailing ones of the comparand. The value of these
6742 // bits doesn't impact the outcome of the comparison, because any value
6743 // greater than the RHS must differ in a bit higher than these due to carry.
6744 case ICmpInst::ICMP_UGT:
6745 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6746
6747 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6748 // Any value less than the RHS must differ in a higher bit because of carries.
6749 case ICmpInst::ICMP_ULT:
6750 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6751
6752 default:
6754 }
6755}
6756
6757/// Check that one use is in the same block as the definition and all
6758/// other uses are in blocks dominated by a given block.
6759///
6760/// \param DI Definition
6761/// \param UI Use
6762/// \param DB Block that must dominate all uses of \p DI outside
6763/// the parent block
6764/// \return true when \p UI is the only use of \p DI in the parent block
6765/// and all other uses of \p DI are in blocks dominated by \p DB.
6766///
6768 const Instruction *UI,
6769 const BasicBlock *DB) const {
6770 assert(DI && UI && "Instruction not defined\n");
6771 // Ignore incomplete definitions.
6772 if (!DI->getParent())
6773 return false;
6774 // DI and UI must be in the same block.
6775 if (DI->getParent() != UI->getParent())
6776 return false;
6777 // Protect from self-referencing blocks.
6778 if (DI->getParent() == DB)
6779 return false;
6780 for (const User *U : DI->users()) {
6781 auto *Usr = cast<Instruction>(U);
6782 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6783 return false;
6784 }
6785 return true;
6786}
6787
6788/// Return true when the instruction sequence within a block is select-cmp-br.
6789static bool isChainSelectCmpBranch(const SelectInst *SI) {
6790 const BasicBlock *BB = SI->getParent();
6791 if (!BB)
6792 return false;
6793 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
6794 if (!BI || BI->getNumSuccessors() != 2)
6795 return false;
6796 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6797 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6798 return false;
6799 return true;
6800}
6801
6802/// True when a select result is replaced by one of its operands
6803/// in select-icmp sequence. This will eventually result in the elimination
6804/// of the select.
6805///
6806/// \param SI Select instruction
6807/// \param Icmp Compare instruction
6808/// \param SIOpd Operand that replaces the select
6809///
6810/// Notes:
6811/// - The replacement is global and requires dominator information
6812/// - The caller is responsible for the actual replacement
6813///
6814/// Example:
6815///
6816/// entry:
6817/// %4 = select i1 %3, %C* %0, %C* null
6818/// %5 = icmp eq %C* %4, null
6819/// br i1 %5, label %9, label %7
6820/// ...
6821/// ; <label>:7 ; preds = %entry
6822/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6823/// ...
6824///
6825/// can be transformed to
6826///
6827/// %5 = icmp eq %C* %0, null
6828/// %6 = select i1 %3, i1 %5, i1 true
6829/// br i1 %6, label %9, label %7
6830/// ...
6831/// ; <label>:7 ; preds = %entry
6832/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6833///
6834/// Similar when the first operand of the select is a constant or/and
6835/// the compare is for not equal rather than equal.
6836///
6837/// NOTE: The function is only called when the select and compare constants
6838/// are equal, the optimization can work only for EQ predicates. This is not a
6839/// major restriction since a NE compare should be 'normalized' to an equal
6840/// compare, which usually happens in the combiner and test case
6841/// select-cmp-br.ll checks for it.
6843 const ICmpInst *Icmp,
6844 const unsigned SIOpd) {
6845 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6847 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6848 // The check for the single predecessor is not the best that can be
6849 // done. But it protects efficiently against cases like when SI's
6850 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6851 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6852 // replaced can be reached on either path. So the uniqueness check
6853 // guarantees that the path all uses of SI (outside SI's parent) are on
6854 // is disjoint from all other paths out of SI. But that information
6855 // is more expensive to compute, and the trade-off here is in favor
6856 // of compile-time. It should also be noticed that we check for a single
6857 // predecessor and not only uniqueness. This to handle the situation when
6858 // Succ and Succ1 points to the same basic block.
6859 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6860 NumSel++;
6861 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6862 return true;
6863 }
6864 }
6865 return false;
6866}
6867
6868/// Try to fold the comparison based on range information we can get by checking
6869/// whether bits are known to be zero or one in the inputs.
6871 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6872 Type *Ty = Op0->getType();
6873 ICmpInst::Predicate Pred = I.getPredicate();
6874
6875 // Get scalar or pointer size.
6876 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6877 ? Ty->getScalarSizeInBits()
6879
6880 if (!BitWidth)
6881 return nullptr;
6882
6883 KnownBits Op0Known(BitWidth);
6884 KnownBits Op1Known(BitWidth);
6885
6886 {
6887 // Don't use dominating conditions when folding icmp using known bits. This
6888 // may convert signed into unsigned predicates in ways that other passes
6889 // (especially IndVarSimplify) may not be able to reliably undo.
6892 Op0Known, Q))
6893 return &I;
6894
6895 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnes(BitWidth), Op1Known, Q))
6896 return &I;
6897 }
6898
6899 if (!isa<Constant>(Op0) && Op0Known.isConstant())
6900 return new ICmpInst(
6901 Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1);
6902 if (!isa<Constant>(Op1) && Op1Known.isConstant())
6903 return new ICmpInst(
6904 Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Known.getConstant()));
6905
6906 if (std::optional<bool> Res = ICmpInst::compare(Op0Known, Op1Known, Pred))
6907 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *Res));
6908
6909 // Given the known and unknown bits, compute a range that the LHS could be
6910 // in. Compute the Min, Max and RHS values based on the known bits. For the
6911 // EQ and NE we use unsigned values.
6912 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6913 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6914 if (I.isSigned()) {
6915 Op0Min = Op0Known.getSignedMinValue();
6916 Op0Max = Op0Known.getSignedMaxValue();
6917 Op1Min = Op1Known.getSignedMinValue();
6918 Op1Max = Op1Known.getSignedMaxValue();
6919 } else {
6920 Op0Min = Op0Known.getMinValue();
6921 Op0Max = Op0Known.getMaxValue();
6922 Op1Min = Op1Known.getMinValue();
6923 Op1Max = Op1Known.getMaxValue();
6924 }
6925
6926 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6927 // min/max canonical compare with some other compare. That could lead to
6928 // conflict with select canonicalization and infinite looping.
6929 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6930 auto isMinMaxCmp = [&](Instruction &Cmp) {
6931 if (!Cmp.hasOneUse())
6932 return false;
6933 Value *A, *B;
6934 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6936 return false;
6937 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6938 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6939 };
6940 if (!isMinMaxCmp(I)) {
6941 switch (Pred) {
6942 default:
6943 break;
6944 case ICmpInst::ICMP_ULT: {
6945 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6946 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6947 const APInt *CmpC;
6948 if (match(Op1, m_APInt(CmpC))) {
6949 // A <u C -> A == C-1 if min(A)+1 == C
6950 if (*CmpC == Op0Min + 1)
6951 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6952 ConstantInt::get(Op1->getType(), *CmpC - 1));
6953 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6954 // exceeds the log2 of C.
6955 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6956 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6957 Constant::getNullValue(Op1->getType()));
6958 }
6959 break;
6960 }
6961 case ICmpInst::ICMP_UGT: {
6962 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6963 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6964 const APInt *CmpC;
6965 if (match(Op1, m_APInt(CmpC))) {
6966 // A >u C -> A == C+1 if max(a)-1 == C
6967 if (*CmpC == Op0Max - 1)
6968 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6969 ConstantInt::get(Op1->getType(), *CmpC + 1));
6970 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6971 // exceeds the log2 of C.
6972 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6973 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6974 Constant::getNullValue(Op1->getType()));
6975 }
6976 break;
6977 }
6978 case ICmpInst::ICMP_SLT: {
6979 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6980 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6981 const APInt *CmpC;
6982 if (match(Op1, m_APInt(CmpC))) {
6983 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6984 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6985 ConstantInt::get(Op1->getType(), *CmpC - 1));
6986 }
6987 break;
6988 }
6989 case ICmpInst::ICMP_SGT: {
6990 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6991 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6992 const APInt *CmpC;
6993 if (match(Op1, m_APInt(CmpC))) {
6994 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6995 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6996 ConstantInt::get(Op1->getType(), *CmpC + 1));
6997 }
6998 break;
6999 }
7000 }
7001 }
7002
7003 // Based on the range information we know about the LHS, see if we can
7004 // simplify this comparison. For example, (x&4) < 8 is always true.
7005 switch (Pred) {
7006 default:
7007 break;
7008 case ICmpInst::ICMP_EQ:
7009 case ICmpInst::ICMP_NE: {
7010 // If all bits are known zero except for one, then we know at most one bit
7011 // is set. If the comparison is against zero, then this is a check to see if
7012 // *that* bit is set.
7013 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
7014 if (Op1Known.isZero()) {
7015 // If the LHS is an AND with the same constant, look through it.
7016 Value *LHS = nullptr;
7017 const APInt *LHSC;
7018 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
7019 *LHSC != Op0KnownZeroInverted)
7020 LHS = Op0;
7021
7022 Value *X;
7023 const APInt *C1;
7024 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
7025 Type *XTy = X->getType();
7026 unsigned Log2C1 = C1->countr_zero();
7027 APInt C2 = Op0KnownZeroInverted;
7028 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
7029 if (C2Pow2.isPowerOf2()) {
7030 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
7031 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
7032 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
7033 unsigned Log2C2 = C2Pow2.countr_zero();
7034 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
7035 auto NewPred =
7037 return new ICmpInst(NewPred, X, CmpC);
7038 }
7039 }
7040 }
7041
7042 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
7043 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
7044 (Op0Known & Op1Known) == Op0Known)
7045 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
7046 ConstantInt::getNullValue(Op1->getType()));
7047 break;
7048 }
7049 case ICmpInst::ICMP_SGE:
7050 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
7051 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7052 break;
7053 case ICmpInst::ICMP_SLE:
7054 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
7055 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7056 break;
7057 case ICmpInst::ICMP_UGE:
7058 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
7059 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7060 break;
7061 case ICmpInst::ICMP_ULE:
7062 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
7063 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
7064 break;
7065 }
7066
7067 // Turn a signed comparison into an unsigned one if both operands are known to
7068 // have the same sign. Set samesign if possible (except for equality
7069 // predicates).
7070 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&
7071 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
7072 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {
7073 I.setPredicate(I.getUnsignedPredicate());
7074 I.setSameSign();
7075 return &I;
7076 }
7077
7078 return nullptr;
7079}
7080
7081/// If one operand of an icmp is effectively a bool (value range of {0,1}),
7082/// then try to reduce patterns based on that limit.
7084 Value *X, *Y;
7085 CmpPredicate Pred;
7086
7087 // X must be 0 and bool must be true for "ULT":
7088 // X <u (zext i1 Y) --> (X == 0) & Y
7089 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
7090 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
7091 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
7092
7093 // X must be 0 or bool must be true for "ULE":
7094 // X <=u (sext i1 Y) --> (X == 0) | Y
7095 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
7096 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
7097 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
7098
7099 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
7100 CmpPredicate Pred1, Pred2;
7101 const APInt *C;
7102 Instruction *ExtI;
7103 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
7106 m_APInt(C)))))) &&
7107 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
7108 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
7109 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
7110 auto CreateRangeCheck = [&] {
7111 Value *CmpV1 =
7112 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
7113 Value *CmpV2 = Builder.CreateICmp(
7114 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
7116 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
7117 CmpV1, CmpV2);
7118 };
7119 if (C->isZero()) {
7120 if (Pred2 == ICmpInst::ICMP_EQ) {
7121 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
7122 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
7123 return replaceInstUsesWith(
7124 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
7125 } else if (!IsSExt || HasOneUse) {
7126 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
7127 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
7128 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
7129 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X != -1
7130 return CreateRangeCheck();
7131 }
7132 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
7133 if (Pred2 == ICmpInst::ICMP_NE) {
7134 // icmp eq X, (zext (icmp ne X, 1)) --> false
7135 // icmp ne X, (zext (icmp ne X, 1)) --> true
7136 // icmp eq X, (sext (icmp ne X, -1)) --> false
7137 // icmp ne X, (sext (icmp ne X, -1)) --> true
7138 return replaceInstUsesWith(
7139 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
7140 } else if (!IsSExt || HasOneUse) {
7141 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
7142 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
7143 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
7144 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
7145 return CreateRangeCheck();
7146 }
7147 } else {
7148 // when C != 0 && C != 1:
7149 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
7150 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
7151 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
7152 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
7153 // when C != 0 && C != -1:
7154 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
7155 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
7156 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
7157 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
7158 return ICmpInst::Create(
7159 Instruction::ICmp, Pred1, X,
7160 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
7161 ? (IsSExt ? -1 : 1)
7162 : 0));
7163 }
7164 }
7165
7166 return nullptr;
7167}
7168
7169/// If we have an icmp le or icmp ge instruction with a constant operand, turn
7170/// it into the appropriate icmp lt or icmp gt instruction. This transform
7171/// allows them to be folded in visitICmpInst.
7173 ICmpInst::Predicate Pred = I.getPredicate();
7174 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
7176 return nullptr;
7177
7178 Value *Op0 = I.getOperand(0);
7179 Value *Op1 = I.getOperand(1);
7180 auto *Op1C = dyn_cast<Constant>(Op1);
7181 if (!Op1C)
7182 return nullptr;
7183
7184 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
7185 if (!FlippedStrictness)
7186 return nullptr;
7187
7188 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
7189}
7190
7191/// If we have a comparison with a non-canonical predicate, if we can update
7192/// all the users, invert the predicate and adjust all the users.
7194 // Is the predicate already canonical?
7195 CmpInst::Predicate Pred = I.getPredicate();
7197 return nullptr;
7198
7199 // Can all users be adjusted to predicate inversion?
7200 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
7201 return nullptr;
7202
7203 // Ok, we can canonicalize comparison!
7204 // Let's first invert the comparison's predicate.
7205 I.setPredicate(CmpInst::getInversePredicate(Pred));
7206 I.setName(I.getName() + ".not");
7207
7208 // And, adapt users.
7210
7211 return &I;
7212}
7213
7214/// Integer compare with boolean values can always be turned into bitwise ops.
7216 InstCombiner::BuilderTy &Builder) {
7217 Value *A = I.getOperand(0), *B = I.getOperand(1);
7218 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
7219
7220 // A boolean compared to true/false can be simplified to Op0/true/false in
7221 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
7222 // Cases not handled by InstSimplify are always 'not' of Op0.
7223 if (match(B, m_Zero())) {
7224 switch (I.getPredicate()) {
7225 case CmpInst::ICMP_EQ: // A == 0 -> !A
7226 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
7227 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
7229 default:
7230 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7231 }
7232 } else if (match(B, m_One())) {
7233 switch (I.getPredicate()) {
7234 case CmpInst::ICMP_NE: // A != 1 -> !A
7235 case CmpInst::ICMP_ULT: // A <u 1 -> !A
7236 case CmpInst::ICMP_SGT: // A >s -1 -> !A
7238 default:
7239 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7240 }
7241 }
7242
7243 switch (I.getPredicate()) {
7244 default:
7245 llvm_unreachable("Invalid icmp instruction!");
7246 case ICmpInst::ICMP_EQ:
7247 // icmp eq i1 A, B -> ~(A ^ B)
7248 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
7249
7250 case ICmpInst::ICMP_NE:
7251 // icmp ne i1 A, B -> A ^ B
7252 return BinaryOperator::CreateXor(A, B);
7253
7254 case ICmpInst::ICMP_UGT:
7255 // icmp ugt -> icmp ult
7256 std::swap(A, B);
7257 [[fallthrough]];
7258 case ICmpInst::ICMP_ULT:
7259 // icmp ult i1 A, B -> ~A & B
7260 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
7261
7262 case ICmpInst::ICMP_SGT:
7263 // icmp sgt -> icmp slt
7264 std::swap(A, B);
7265 [[fallthrough]];
7266 case ICmpInst::ICMP_SLT:
7267 // icmp slt i1 A, B -> A & ~B
7268 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
7269
7270 case ICmpInst::ICMP_UGE:
7271 // icmp uge -> icmp ule
7272 std::swap(A, B);
7273 [[fallthrough]];
7274 case ICmpInst::ICMP_ULE:
7275 // icmp ule i1 A, B -> ~A | B
7276 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
7277
7278 case ICmpInst::ICMP_SGE:
7279 // icmp sge -> icmp sle
7280 std::swap(A, B);
7281 [[fallthrough]];
7282 case ICmpInst::ICMP_SLE:
7283 // icmp sle i1 A, B -> A | ~B
7284 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
7285 }
7286}
7287
7288// Transform pattern like:
7289// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7290// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7291// Into:
7292// (X l>> Y) != 0
7293// (X l>> Y) == 0
7295 InstCombiner::BuilderTy &Builder) {
7296 CmpPredicate Pred, NewPred;
7297 Value *X, *Y;
7298 if (match(&Cmp,
7299 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
7300 switch (Pred) {
7301 case ICmpInst::ICMP_ULE:
7302 NewPred = ICmpInst::ICMP_NE;
7303 break;
7304 case ICmpInst::ICMP_UGT:
7305 NewPred = ICmpInst::ICMP_EQ;
7306 break;
7307 default:
7308 return nullptr;
7309 }
7310 } else if (match(&Cmp, m_c_ICmp(Pred,
7313 m_Add(m_Shl(m_One(), m_Value(Y)),
7314 m_AllOnes()))),
7315 m_Value(X)))) {
7316 // The variant with 'add' is not canonical, (the variant with 'not' is)
7317 // we only get it because it has extra uses, and can't be canonicalized,
7318
7319 switch (Pred) {
7320 case ICmpInst::ICMP_ULT:
7321 NewPred = ICmpInst::ICMP_NE;
7322 break;
7323 case ICmpInst::ICMP_UGE:
7324 NewPred = ICmpInst::ICMP_EQ;
7325 break;
7326 default:
7327 return nullptr;
7328 }
7329 } else
7330 return nullptr;
7331
7332 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
7333 Constant *Zero = Constant::getNullValue(NewX->getType());
7334 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
7335}
7336
7338 InstCombiner::BuilderTy &Builder) {
7339 const CmpInst::Predicate Pred = Cmp.getPredicate();
7340 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
7341 Value *V1, *V2;
7342
7343 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7344 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
7345 if (auto *I = dyn_cast<Instruction>(V))
7346 I->copyIRFlags(&Cmp);
7347 Module *M = Cmp.getModule();
7349 M, Intrinsic::vector_reverse, V->getType());
7350 return CallInst::Create(F, V);
7351 };
7352
7353 if (match(LHS, m_VecReverse(m_Value(V1)))) {
7354 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7355 if (match(RHS, m_VecReverse(m_Value(V2))) &&
7356 (LHS->hasOneUse() || RHS->hasOneUse()))
7357 return createCmpReverse(Pred, V1, V2);
7358
7359 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7360 if (LHS->hasOneUse() && isSplatValue(RHS))
7361 return createCmpReverse(Pred, V1, RHS);
7362 }
7363 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7364 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
7365 return createCmpReverse(Pred, LHS, V2);
7366
7367 ArrayRef<int> M;
7368 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
7369 return nullptr;
7370
7371 // If both arguments of the cmp are shuffles that use the same mask and
7372 // shuffle within a single vector, move the shuffle after the cmp:
7373 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7374 Type *V1Ty = V1->getType();
7375 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
7376 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7377 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
7378 return new ShuffleVectorInst(NewCmp, M);
7379 }
7380
7381 // Try to canonicalize compare with splatted operand and splat constant.
7382 // TODO: We could generalize this for more than splats. See/use the code in
7383 // InstCombiner::foldVectorBinop().
7384 Constant *C;
7385 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
7386 return nullptr;
7387
7388 // Length-changing splats are ok, so adjust the constants as needed:
7389 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7390 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7391 int MaskSplatIndex;
7392 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
7393 // We allow poison in matching, but this transform removes it for safety.
7394 // Demanded elements analysis should be able to recover some/all of that.
7395 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
7396 ScalarC);
7397 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7398 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
7399 return new ShuffleVectorInst(NewCmp, NewM);
7400 }
7401
7402 return nullptr;
7403}
7404
7405// extract(uadd.with.overflow(A, B), 0) ult A
7406// -> extract(uadd.with.overflow(A, B), 1)
7408 CmpInst::Predicate Pred = I.getPredicate();
7409 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7410
7411 Value *UAddOv;
7412 Value *A, *B;
7413 auto UAddOvResultPat = m_ExtractValue<0>(
7414 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));
7415 if (match(Op0, UAddOvResultPat) &&
7416 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7417 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
7418 (match(A, m_One()) || match(B, m_One()))) ||
7419 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
7420 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
7421 // extract(uadd.with.overflow(A, B), 0) < A
7422 // extract(uadd.with.overflow(A, 1), 0) == 0
7423 // extract(uadd.with.overflow(A, -1), 0) != -1
7424 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
7425 else if (match(Op1, UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&
7426 (Op0 == A || Op0 == B))
7427 // A > extract(uadd.with.overflow(A, B), 0)
7428 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
7429 else
7430 return nullptr;
7431
7432 return ExtractValueInst::Create(UAddOv, 1);
7433}
7434
7436 if (!I.getOperand(0)->getType()->isPointerTy() ||
7438 I.getParent()->getParent(),
7439 I.getOperand(0)->getType()->getPointerAddressSpace())) {
7440 return nullptr;
7441 }
7442 Instruction *Op;
7443 if (match(I.getOperand(0), m_Instruction(Op)) &&
7444 match(I.getOperand(1), m_Zero()) &&
7445 Op->isLaunderOrStripInvariantGroup()) {
7446 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7447 Op->getOperand(0), I.getOperand(1));
7448 }
7449 return nullptr;
7450}
7451
7452/// This function folds patterns produced by lowering of reduce idioms, such as
7453/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7454/// attempts to generate fewer number of scalar comparisons instead of vector
7455/// comparisons when possible.
7457 InstCombiner::BuilderTy &Builder,
7458 const DataLayout &DL) {
7459 if (I.getType()->isVectorTy())
7460 return nullptr;
7461 CmpPredicate OuterPred, InnerPred;
7462 Value *LHS, *RHS;
7463
7464 // Match lowering of @llvm.vector.reduce.and. Turn
7465 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7466 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7467 /// %res = icmp <pred> i8 %scalar_ne, 0
7468 ///
7469 /// into
7470 ///
7471 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7472 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7473 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7474 ///
7475 /// for <pred> in {ne, eq}.
7476 if (!match(&I, m_ICmp(OuterPred,
7478 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7479 m_Zero())))
7480 return nullptr;
7481 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7482 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7483 return nullptr;
7484 unsigned NumBits =
7485 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7486 // TODO: Relax this to "not wider than max legal integer type"?
7487 if (!DL.isLegalInteger(NumBits))
7488 return nullptr;
7489
7490 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7491 auto *ScalarTy = Builder.getIntNTy(NumBits);
7492 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7493 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7494 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7495 I.getName());
7496 }
7497
7498 return nullptr;
7499}
7500
7501// This helper will be called with icmp operands in both orders.
7503 Value *Op0, Value *Op1,
7504 ICmpInst &CxtI) {
7505 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7506 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7507 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7508 return NI;
7509
7510 if (auto *SI = dyn_cast<SelectInst>(Op0))
7511 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7512 return NI;
7513
7514 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0))
7515 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7516 return Res;
7517
7518 {
7519 Value *X;
7520 const APInt *C;
7521 // icmp X+Cst, X
7522 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7523 return foldICmpAddOpConst(X, *C, Pred);
7524 }
7525
7526 // abs(X) >= X --> true
7527 // abs(X) u<= X --> true
7528 // abs(X) < X --> false
7529 // abs(X) u> X --> false
7530 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7531 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7532 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7533 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7534 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7535 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7536 {
7537 Value *X;
7538 Constant *C;
7539 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
7540 match(Op1, m_Specific(X))) {
7541 Value *NullValue = Constant::getNullValue(X->getType());
7542 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7543 const APInt SMin =
7544 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7545 bool IsIntMinPosion = C->isAllOnesValue();
7546 switch (Pred) {
7547 case CmpInst::ICMP_ULE:
7548 case CmpInst::ICMP_SGE:
7549 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7550 case CmpInst::ICMP_UGT:
7551 case CmpInst::ICMP_SLT:
7553 case CmpInst::ICMP_UGE:
7554 case CmpInst::ICMP_SLE:
7555 case CmpInst::ICMP_EQ: {
7556 return replaceInstUsesWith(
7557 CxtI, IsIntMinPosion
7558 ? Builder.CreateICmpSGT(X, AllOnesValue)
7560 X, ConstantInt::get(X->getType(), SMin + 1)));
7561 }
7562 case CmpInst::ICMP_ULT:
7563 case CmpInst::ICMP_SGT:
7564 case CmpInst::ICMP_NE: {
7565 return replaceInstUsesWith(
7566 CxtI, IsIntMinPosion
7567 ? Builder.CreateICmpSLT(X, NullValue)
7569 X, ConstantInt::get(X->getType(), SMin)));
7570 }
7571 default:
7572 llvm_unreachable("Invalid predicate!");
7573 }
7574 }
7575 }
7576
7577 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7578 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7579 return replaceInstUsesWith(CxtI, V);
7580
7581 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7582 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7583 {
7584 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7585 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7587 }
7588
7589 if (!ICmpInst::isUnsigned(Pred) &&
7590 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7591 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7593 }
7594 }
7595
7596 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7597 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7598 {
7599 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7600 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7602 }
7603
7604 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7605 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7606 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7608 }
7609 }
7610
7611 return nullptr;
7612}
7613
7615 bool Changed = false;
7617 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7618 unsigned Op0Cplxity = getComplexity(Op0);
7619 unsigned Op1Cplxity = getComplexity(Op1);
7620
7621 /// Orders the operands of the compare so that they are listed from most
7622 /// complex to least complex. This puts constants before unary operators,
7623 /// before binary operators.
7624 if (Op0Cplxity < Op1Cplxity) {
7625 I.swapOperands();
7626 std::swap(Op0, Op1);
7627 Changed = true;
7628 }
7629
7630 if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
7631 return replaceInstUsesWith(I, V);
7632
7633 // Comparing -val or val with non-zero is the same as just comparing val
7634 // ie, abs(val) != 0 -> val != 0
7635 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7636 Value *Cond, *SelectTrue, *SelectFalse;
7637 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7638 m_Value(SelectFalse)))) {
7639 if (Value *V = dyn_castNegVal(SelectTrue)) {
7640 if (V == SelectFalse)
7641 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7642 } else if (Value *V = dyn_castNegVal(SelectFalse)) {
7643 if (V == SelectTrue)
7644 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7645 }
7646 }
7647 }
7648
7650 return Res;
7651
7652 if (Op0->getType()->isIntOrIntVectorTy(1))
7654 return Res;
7655
7657 return Res;
7658
7660 return Res;
7661
7663 return Res;
7664
7666 return Res;
7667
7669 return Res;
7670
7672 return Res;
7673
7675 return Res;
7676
7677 // Test if the ICmpInst instruction is used exclusively by a select as
7678 // part of a minimum or maximum operation. If so, refrain from doing
7679 // any other folding. This helps out other analyses which understand
7680 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7681 // and CodeGen. And in this case, at least one of the comparison
7682 // operands has at least one user besides the compare (the select),
7683 // which would often largely negate the benefit of folding anyway.
7684 //
7685 // Do the same for the other patterns recognized by matchSelectPattern.
7686 if (I.hasOneUse())
7687 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7688 Value *A, *B;
7690 if (SPR.Flavor != SPF_UNKNOWN)
7691 return nullptr;
7692 }
7693
7694 // Do this after checking for min/max to prevent infinite looping.
7695 if (Instruction *Res = foldICmpWithZero(I))
7696 return Res;
7697
7698 // FIXME: We only do this after checking for min/max to prevent infinite
7699 // looping caused by a reverse canonicalization of these patterns for min/max.
7700 // FIXME: The organization of folds is a mess. These would naturally go into
7701 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7702 // down here after the min/max restriction.
7703 ICmpInst::Predicate Pred = I.getPredicate();
7704 const APInt *C;
7705 if (match(Op1, m_APInt(C))) {
7706 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7707 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7708 Constant *Zero = Constant::getNullValue(Op0->getType());
7709 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7710 }
7711
7712 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7713 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7715 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7716 }
7717 }
7718
7719 // The folds in here may rely on wrapping flags and special constants, so
7720 // they can break up min/max idioms in some cases but not seemingly similar
7721 // patterns.
7722 // FIXME: It may be possible to enhance select folding to make this
7723 // unnecessary. It may also be moot if we canonicalize to min/max
7724 // intrinsics.
7725 if (Instruction *Res = foldICmpBinOp(I, Q))
7726 return Res;
7727
7729 return Res;
7730
7731 // Try to match comparison as a sign bit test. Intentionally do this after
7732 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7733 if (Instruction *New = foldSignBitTest(I))
7734 return New;
7735
7736 if (auto *PN = dyn_cast<PHINode>(Op0))
7737 if (Instruction *NV = foldOpIntoPhi(I, PN))
7738 return NV;
7739 if (auto *PN = dyn_cast<PHINode>(Op1))
7740 if (Instruction *NV = foldOpIntoPhi(I, PN))
7741 return NV;
7742
7744 return Res;
7745
7746 if (Instruction *Res = foldICmpCommutative(I.getCmpPredicate(), Op0, Op1, I))
7747 return Res;
7748 if (Instruction *Res =
7749 foldICmpCommutative(I.getSwappedCmpPredicate(), Op1, Op0, I))
7750 return Res;
7751
7752 if (I.isCommutative()) {
7753 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7754 replaceOperand(I, 0, Pair->first);
7755 replaceOperand(I, 1, Pair->second);
7756 return &I;
7757 }
7758 }
7759
7760 // In case of a comparison with two select instructions having the same
7761 // condition, check whether one of the resulting branches can be simplified.
7762 // If so, just compare the other branch and select the appropriate result.
7763 // For example:
7764 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7765 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7766 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7767 // The icmp will result false for the false value of selects and the result
7768 // will depend upon the comparison of true values of selects if %cmp is
7769 // true. Thus, transform this into:
7770 // %cmp = icmp slt i32 %y, %z
7771 // %sel = select i1 %cond, i1 %cmp, i1 false
7772 // This handles similar cases to transform.
7773 {
7774 Value *Cond, *A, *B, *C, *D;
7775 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7777 (Op0->hasOneUse() || Op1->hasOneUse())) {
7778 // Check whether comparison of TrueValues can be simplified
7779 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7780 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7781 return SelectInst::Create(Cond, Res, NewICMP);
7782 }
7783 // Check whether comparison of FalseValues can be simplified
7784 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7785 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7786 return SelectInst::Create(Cond, NewICMP, Res);
7787 }
7788 }
7789 }
7790
7791 // icmp slt (sub nsw x, y), (add nsw x, y) --> icmp sgt y, 0
7792 // icmp ult (sub nuw x, y), (add nuw x, y) --> icmp ugt y, 0
7793 // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y) --> icmp eq y, 0
7794 {
7795 Value *A, *B;
7796 CmpPredicate CmpPred;
7797 if (match(&I, m_c_ICmp(CmpPred, m_Sub(m_Value(A), m_Value(B)),
7799 auto *I0 = cast<OverflowingBinaryOperator>(Op0);
7800 auto *I1 = cast<OverflowingBinaryOperator>(Op1);
7801 bool I0NUW = I0->hasNoUnsignedWrap();
7802 bool I1NUW = I1->hasNoUnsignedWrap();
7803 bool I0NSW = I0->hasNoSignedWrap();
7804 bool I1NSW = I1->hasNoSignedWrap();
7805 if ((ICmpInst::isUnsigned(Pred) && I0NUW && I1NUW) ||
7806 (ICmpInst::isSigned(Pred) && I0NSW && I1NSW) ||
7807 (ICmpInst::isEquality(Pred) &&
7808 ((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {
7809 return new ICmpInst(CmpPredicate::getSwapped(CmpPred), B,
7810 ConstantInt::get(Op0->getType(), 0));
7811 }
7812 }
7813 }
7814
7815 // Try to optimize equality comparisons against alloca-based pointers.
7816 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7817 assert(Op1->getType()->isPointerTy() &&
7818 "Comparing pointer with non-pointer?");
7819 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7820 if (foldAllocaCmp(Alloca))
7821 return nullptr;
7822 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7823 if (foldAllocaCmp(Alloca))
7824 return nullptr;
7825 }
7826
7827 if (Instruction *Res = foldICmpBitCast(I))
7828 return Res;
7829
7830 // TODO: Hoist this above the min/max bailout.
7832 return R;
7833
7834 {
7835 Value *X, *Y;
7836 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7837 // and (X & ~Y) != 0 --> (X & Y) == 0
7838 // if A is a power of 2.
7839 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7840 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, &I) &&
7841 I.isEquality())
7842 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7843 Op1);
7844
7845 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7846 if (Op0->getType()->isIntOrIntVectorTy()) {
7847 bool ConsumesOp0, ConsumesOp1;
7848 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7849 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7850 (ConsumesOp0 || ConsumesOp1)) {
7851 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7852 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7853 assert(InvOp0 && InvOp1 &&
7854 "Mismatch between isFreeToInvert and getFreelyInverted");
7855 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7856 }
7857 }
7858
7859 Instruction *AddI = nullptr;
7861 m_Instruction(AddI))) &&
7862 isa<IntegerType>(X->getType())) {
7863 Value *Result;
7864 Constant *Overflow;
7865 // m_UAddWithOverflow can match patterns that do not include an explicit
7866 // "add" instruction, so check the opcode of the matched op.
7867 if (AddI->getOpcode() == Instruction::Add &&
7868 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7869 Result, Overflow)) {
7870 replaceInstUsesWith(*AddI, Result);
7871 eraseInstFromFunction(*AddI);
7872 return replaceInstUsesWith(I, Overflow);
7873 }
7874 }
7875
7876 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7877 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7878 match(Op1, m_APInt(C))) {
7879 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7880 return R;
7881 }
7882
7883 // Signbit test folds
7884 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7885 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7886 Instruction *ExtI;
7887 if ((I.isUnsigned() || I.isEquality()) &&
7888 match(Op1,
7890 Y->getType()->getScalarSizeInBits() == 1 &&
7891 (Op0->hasOneUse() || Op1->hasOneUse())) {
7892 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7893 Instruction *ShiftI;
7894 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7896 OpWidth - 1))))) {
7897 unsigned ExtOpc = ExtI->getOpcode();
7898 unsigned ShiftOpc = ShiftI->getOpcode();
7899 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7900 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7901 Value *SLTZero =
7903 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7904 return replaceInstUsesWith(I, Cmp);
7905 }
7906 }
7907 }
7908 }
7909
7910 if (Instruction *Res = foldICmpEquality(I))
7911 return Res;
7912
7914 return Res;
7915
7916 if (Instruction *Res = foldICmpOfUAddOv(I))
7917 return Res;
7918
7919 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7920 // an i1 which indicates whether or not we successfully did the swap.
7921 //
7922 // Replace comparisons between the old value and the expected value with the
7923 // indicator that 'cmpxchg' returns.
7924 //
7925 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7926 // spuriously fail. In those cases, the old value may equal the expected
7927 // value but it is possible for the swap to not occur.
7928 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7929 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7930 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7931 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7932 !ACXI->isWeak())
7933 return ExtractValueInst::Create(ACXI, 1);
7934
7936 return Res;
7937
7938 if (I.getType()->isVectorTy())
7939 if (Instruction *Res = foldVectorCmp(I, Builder))
7940 return Res;
7941
7943 return Res;
7944
7946 return Res;
7947
7948 {
7949 Value *A;
7950 const APInt *C1, *C2;
7951 ICmpInst::Predicate Pred = I.getPredicate();
7952 if (ICmpInst::isEquality(Pred)) {
7953 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
7954 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
7955 if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) &&
7956 match(Op1, m_APInt(C2))) {
7957 Type *InputTy = A->getType();
7958 unsigned InputBitWidth = InputTy->getScalarSizeInBits();
7959 // c2 must be non-negative at the bitwidth of a.
7960 if (C2->getActiveBits() < InputBitWidth) {
7961 APInt TruncC1 = C1->trunc(InputBitWidth);
7962 // Check if there are 1s in C1 high bits of size InputBitWidth.
7963 if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth)))
7964 TruncC1.setBit(InputBitWidth - 1);
7965 Value *AndInst = Builder.CreateAnd(A, TruncC1);
7966 return new ICmpInst(
7967 Pred, AndInst,
7968 ConstantInt::get(InputTy, C2->trunc(InputBitWidth)));
7969 }
7970 }
7971 }
7972 }
7973
7974 return Changed ? &I : nullptr;
7975}
7976
7977/// Fold fcmp ([us]itofp x, cst) if possible.
7979 Instruction *LHSI,
7980 Constant *RHSC) {
7981 const APFloat *RHS;
7982 if (!match(RHSC, m_APFloat(RHS)))
7983 return nullptr;
7984
7985 // Get the width of the mantissa. We don't want to hack on conversions that
7986 // might lose information from the integer, e.g. "i64 -> float"
7987 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7988 if (MantissaWidth == -1)
7989 return nullptr; // Unknown.
7990
7991 Type *IntTy = LHSI->getOperand(0)->getType();
7992 unsigned IntWidth = IntTy->getScalarSizeInBits();
7993 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7994
7995 if (I.isEquality()) {
7996 FCmpInst::Predicate P = I.getPredicate();
7997 bool IsExact = false;
7998 APSInt RHSCvt(IntWidth, LHSUnsigned);
7999 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
8000
8001 // If the floating point constant isn't an integer value, we know if we will
8002 // ever compare equal / not equal to it.
8003 if (!IsExact) {
8004 // TODO: Can never be -0.0 and other non-representable values
8005 APFloat RHSRoundInt(*RHS);
8007 if (*RHS != RHSRoundInt) {
8009 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8010
8012 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8013 }
8014 }
8015
8016 // TODO: If the constant is exactly representable, is it always OK to do
8017 // equality compares as integer?
8018 }
8019
8020 // Check to see that the input is converted from an integer type that is small
8021 // enough that preserves all bits. TODO: check here for "known" sign bits.
8022 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
8023
8024 // Following test does NOT adjust IntWidth downwards for signed inputs,
8025 // because the most negative value still requires all the mantissa bits
8026 // to distinguish it from one less than that value.
8027 if ((int)IntWidth > MantissaWidth) {
8028 // Conversion would lose accuracy. Check if loss can impact comparison.
8029 int Exp = ilogb(*RHS);
8030 if (Exp == APFloat::IEK_Inf) {
8031 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
8032 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
8033 // Conversion could create infinity.
8034 return nullptr;
8035 } else {
8036 // Note that if RHS is zero or NaN, then Exp is negative
8037 // and first condition is trivially false.
8038 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
8039 // Conversion could affect comparison.
8040 return nullptr;
8041 }
8042 }
8043
8044 // Otherwise, we can potentially simplify the comparison. We know that it
8045 // will always come through as an integer value and we know the constant is
8046 // not a NAN (it would have been previously simplified).
8047 assert(!RHS->isNaN() && "NaN comparison not already folded!");
8048
8050 switch (I.getPredicate()) {
8051 default:
8052 llvm_unreachable("Unexpected predicate!");
8053 case FCmpInst::FCMP_UEQ:
8054 case FCmpInst::FCMP_OEQ:
8055 Pred = ICmpInst::ICMP_EQ;
8056 break;
8057 case FCmpInst::FCMP_UGT:
8058 case FCmpInst::FCMP_OGT:
8059 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
8060 break;
8061 case FCmpInst::FCMP_UGE:
8062 case FCmpInst::FCMP_OGE:
8063 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
8064 break;
8065 case FCmpInst::FCMP_ULT:
8066 case FCmpInst::FCMP_OLT:
8067 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
8068 break;
8069 case FCmpInst::FCMP_ULE:
8070 case FCmpInst::FCMP_OLE:
8071 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
8072 break;
8073 case FCmpInst::FCMP_UNE:
8074 case FCmpInst::FCMP_ONE:
8075 Pred = ICmpInst::ICMP_NE;
8076 break;
8077 case FCmpInst::FCMP_ORD:
8078 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8079 case FCmpInst::FCMP_UNO:
8080 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8081 }
8082
8083 // Now we know that the APFloat is a normal number, zero or inf.
8084
8085 // See if the FP constant is too large for the integer. For example,
8086 // comparing an i8 to 300.0.
8087 if (!LHSUnsigned) {
8088 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
8089 // and large values.
8090 APFloat SMax(RHS->getSemantics());
8091 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
8093 if (SMax < *RHS) { // smax < 13123.0
8094 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
8095 Pred == ICmpInst::ICMP_SLE)
8096 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8097 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8098 }
8099 } else {
8100 // If the RHS value is > UnsignedMax, fold the comparison. This handles
8101 // +INF and large values.
8102 APFloat UMax(RHS->getSemantics());
8103 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
8105 if (UMax < *RHS) { // umax < 13123.0
8106 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
8107 Pred == ICmpInst::ICMP_ULE)
8108 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8109 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8110 }
8111 }
8112
8113 if (!LHSUnsigned) {
8114 // See if the RHS value is < SignedMin.
8115 APFloat SMin(RHS->getSemantics());
8116 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
8118 if (SMin > *RHS) { // smin > 12312.0
8119 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
8120 Pred == ICmpInst::ICMP_SGE)
8121 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8122 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8123 }
8124 } else {
8125 // See if the RHS value is < UnsignedMin.
8126 APFloat UMin(RHS->getSemantics());
8127 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
8129 if (UMin > *RHS) { // umin > 12312.0
8130 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
8131 Pred == ICmpInst::ICMP_UGE)
8132 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8133 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8134 }
8135 }
8136
8137 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
8138 // [0, UMAX], but it may still be fractional. Check whether this is the case
8139 // using the IsExact flag.
8140 // Don't do this for zero, because -0.0 is not fractional.
8141 APSInt RHSInt(IntWidth, LHSUnsigned);
8142 bool IsExact;
8143 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
8144 if (!RHS->isZero()) {
8145 if (!IsExact) {
8146 // If we had a comparison against a fractional value, we have to adjust
8147 // the compare predicate and sometimes the value. RHSC is rounded towards
8148 // zero at this point.
8149 switch (Pred) {
8150 default:
8151 llvm_unreachable("Unexpected integer comparison!");
8152 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
8153 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8154 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
8155 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8156 case ICmpInst::ICMP_ULE:
8157 // (float)int <= 4.4 --> int <= 4
8158 // (float)int <= -4.4 --> false
8159 if (RHS->isNegative())
8160 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8161 break;
8162 case ICmpInst::ICMP_SLE:
8163 // (float)int <= 4.4 --> int <= 4
8164 // (float)int <= -4.4 --> int < -4
8165 if (RHS->isNegative())
8166 Pred = ICmpInst::ICMP_SLT;
8167 break;
8168 case ICmpInst::ICMP_ULT:
8169 // (float)int < -4.4 --> false
8170 // (float)int < 4.4 --> int <= 4
8171 if (RHS->isNegative())
8172 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8173 Pred = ICmpInst::ICMP_ULE;
8174 break;
8175 case ICmpInst::ICMP_SLT:
8176 // (float)int < -4.4 --> int < -4
8177 // (float)int < 4.4 --> int <= 4
8178 if (!RHS->isNegative())
8179 Pred = ICmpInst::ICMP_SLE;
8180 break;
8181 case ICmpInst::ICMP_UGT:
8182 // (float)int > 4.4 --> int > 4
8183 // (float)int > -4.4 --> true
8184 if (RHS->isNegative())
8185 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8186 break;
8187 case ICmpInst::ICMP_SGT:
8188 // (float)int > 4.4 --> int > 4
8189 // (float)int > -4.4 --> int >= -4
8190 if (RHS->isNegative())
8191 Pred = ICmpInst::ICMP_SGE;
8192 break;
8193 case ICmpInst::ICMP_UGE:
8194 // (float)int >= -4.4 --> true
8195 // (float)int >= 4.4 --> int > 4
8196 if (RHS->isNegative())
8197 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8198 Pred = ICmpInst::ICMP_UGT;
8199 break;
8200 case ICmpInst::ICMP_SGE:
8201 // (float)int >= -4.4 --> int >= -4
8202 // (float)int >= 4.4 --> int > 4
8203 if (!RHS->isNegative())
8204 Pred = ICmpInst::ICMP_SGT;
8205 break;
8206 }
8207 }
8208 }
8209
8210 // Lower this FP comparison into an appropriate integer version of the
8211 // comparison.
8212 return new ICmpInst(Pred, LHSI->getOperand(0),
8213 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
8214}
8215
8216/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
8218 Constant *RHSC) {
8219 // When C is not 0.0 and infinities are not allowed:
8220 // (C / X) < 0.0 is a sign-bit test of X
8221 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
8222 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
8223 //
8224 // Proof:
8225 // Multiply (C / X) < 0.0 by X * X / C.
8226 // - X is non zero, if it is the flag 'ninf' is violated.
8227 // - C defines the sign of X * X * C. Thus it also defines whether to swap
8228 // the predicate. C is also non zero by definition.
8229 //
8230 // Thus X * X / C is non zero and the transformation is valid. [qed]
8231
8232 FCmpInst::Predicate Pred = I.getPredicate();
8233
8234 // Check that predicates are valid.
8235 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
8236 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
8237 return nullptr;
8238
8239 // Check that RHS operand is zero.
8240 if (!match(RHSC, m_AnyZeroFP()))
8241 return nullptr;
8242
8243 // Check fastmath flags ('ninf').
8244 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
8245 return nullptr;
8246
8247 // Check the properties of the dividend. It must not be zero to avoid a
8248 // division by zero (see Proof).
8249 const APFloat *C;
8250 if (!match(LHSI->getOperand(0), m_APFloat(C)))
8251 return nullptr;
8252
8253 if (C->isZero())
8254 return nullptr;
8255
8256 // Get swapped predicate if necessary.
8257 if (C->isNegative())
8258 Pred = I.getSwappedPredicate();
8259
8260 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
8261}
8262
8263// Transform 'fptrunc(x) cmp C' to 'x cmp ext(C)' if possible.
8264// Patterns include:
8265// fptrunc(x) < C --> x < ext(C)
8266// fptrunc(x) <= C --> x <= ext(C)
8267// fptrunc(x) > C --> x > ext(C)
8268// fptrunc(x) >= C --> x >= ext(C)
8269// where 'ext(C)' is the extension of 'C' to the type of 'x' with a small bias
8270// due to precision loss.
8272 const Constant &C) {
8273 FCmpInst::Predicate Pred = I.getPredicate();
8274 bool RoundDown = false;
8275
8276 if (Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE ||
8277 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT)
8278 RoundDown = true;
8279 else if (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT ||
8280 Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)
8281 RoundDown = false;
8282 else
8283 return nullptr;
8284
8285 const APFloat *CValue;
8286 if (!match(&C, m_APFloat(CValue)))
8287 return nullptr;
8288
8289 if (CValue->isNaN() || CValue->isInfinity())
8290 return nullptr;
8291
8292 auto ConvertFltSema = [](const APFloat &Src, const fltSemantics &Sema) {
8293 bool LosesInfo;
8294 APFloat Dest = Src;
8295 Dest.convert(Sema, APFloat::rmNearestTiesToEven, &LosesInfo);
8296 return Dest;
8297 };
8298
8299 auto NextValue = [](const APFloat &Value, bool RoundDown) {
8300 APFloat NextValue = Value;
8301 NextValue.next(RoundDown);
8302 return NextValue;
8303 };
8304
8305 APFloat NextCValue = NextValue(*CValue, RoundDown);
8306
8307 Type *DestType = FPTrunc.getOperand(0)->getType();
8308 const fltSemantics &DestFltSema =
8309 DestType->getScalarType()->getFltSemantics();
8310
8311 APFloat ExtCValue = ConvertFltSema(*CValue, DestFltSema);
8312 APFloat ExtNextCValue = ConvertFltSema(NextCValue, DestFltSema);
8313
8314 // When 'NextCValue' is infinity, use an imaged 'NextCValue' that equals
8315 // 'CValue + bias' to avoid the infinity after conversion. The bias is
8316 // estimated as 'CValue - PrevCValue', where 'PrevCValue' is the previous
8317 // value of 'CValue'.
8318 if (NextCValue.isInfinity()) {
8319 APFloat PrevCValue = NextValue(*CValue, !RoundDown);
8320 APFloat Bias = ConvertFltSema(*CValue - PrevCValue, DestFltSema);
8321
8322 ExtNextCValue = ExtCValue + Bias;
8323 }
8324
8325 APFloat ExtMidValue =
8326 scalbn(ExtCValue + ExtNextCValue, -1, APFloat::rmNearestTiesToEven);
8327
8328 const fltSemantics &SrcFltSema =
8329 C.getType()->getScalarType()->getFltSemantics();
8330
8331 // 'MidValue' might be rounded to 'NextCValue'. Correct it here.
8332 APFloat MidValue = ConvertFltSema(ExtMidValue, SrcFltSema);
8333 if (MidValue != *CValue)
8334 ExtMidValue.next(!RoundDown);
8335
8336 // Check whether 'ExtMidValue' is a valid result since the assumption on
8337 // imaged 'NextCValue' might not hold for new float types.
8338 // ppc_fp128 can't pass here when converting from max float because of
8339 // APFloat implementation.
8340 if (NextCValue.isInfinity()) {
8341 // ExtMidValue --- narrowed ---> Finite
8342 if (ConvertFltSema(ExtMidValue, SrcFltSema).isInfinity())
8343 return nullptr;
8344
8345 // NextExtMidValue --- narrowed ---> Infinity
8346 APFloat NextExtMidValue = NextValue(ExtMidValue, RoundDown);
8347 if (ConvertFltSema(NextExtMidValue, SrcFltSema).isFinite())
8348 return nullptr;
8349 }
8350
8351 return new FCmpInst(Pred, FPTrunc.getOperand(0),
8352 ConstantFP::get(DestType, ExtMidValue), "", &I);
8353}
8354
8355/// Optimize fabs(X) compared with zero.
8357 Value *X;
8358 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
8359 return nullptr;
8360
8361 const APFloat *C;
8362 if (!match(I.getOperand(1), m_APFloat(C)))
8363 return nullptr;
8364
8365 if (!C->isPosZero()) {
8366 if (!C->isSmallestNormalized())
8367 return nullptr;
8368
8369 const Function *F = I.getFunction();
8370 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
8371 if (Mode.Input == DenormalMode::PreserveSign ||
8372 Mode.Input == DenormalMode::PositiveZero) {
8373
8374 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8375 Constant *Zero = ConstantFP::getZero(X->getType());
8376 return new FCmpInst(P, X, Zero, "", I);
8377 };
8378
8379 switch (I.getPredicate()) {
8380 case FCmpInst::FCMP_OLT:
8381 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
8382 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
8383 case FCmpInst::FCMP_UGE:
8384 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
8385 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
8386 case FCmpInst::FCMP_OGE:
8387 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
8388 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
8389 case FCmpInst::FCMP_ULT:
8390 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
8391 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
8392 default:
8393 break;
8394 }
8395 }
8396
8397 return nullptr;
8398 }
8399
8400 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8401 I->setPredicate(P);
8402 return IC.replaceOperand(*I, 0, X);
8403 };
8404
8405 switch (I.getPredicate()) {
8406 case FCmpInst::FCMP_UGE:
8407 case FCmpInst::FCMP_OLT:
8408 // fabs(X) >= 0.0 --> true
8409 // fabs(X) < 0.0 --> false
8410 llvm_unreachable("fcmp should have simplified");
8411
8412 case FCmpInst::FCMP_OGT:
8413 // fabs(X) > 0.0 --> X != 0.0
8414 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8415
8416 case FCmpInst::FCMP_UGT:
8417 // fabs(X) u> 0.0 --> X u!= 0.0
8418 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8419
8420 case FCmpInst::FCMP_OLE:
8421 // fabs(X) <= 0.0 --> X == 0.0
8422 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8423
8424 case FCmpInst::FCMP_ULE:
8425 // fabs(X) u<= 0.0 --> X u== 0.0
8426 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8427
8428 case FCmpInst::FCMP_OGE:
8429 // fabs(X) >= 0.0 --> !isnan(X)
8430 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8431 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8432
8433 case FCmpInst::FCMP_ULT:
8434 // fabs(X) u< 0.0 --> isnan(X)
8435 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8436 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8437
8438 case FCmpInst::FCMP_OEQ:
8439 case FCmpInst::FCMP_UEQ:
8440 case FCmpInst::FCMP_ONE:
8441 case FCmpInst::FCMP_UNE:
8442 case FCmpInst::FCMP_ORD:
8443 case FCmpInst::FCMP_UNO:
8444 // Look through the fabs() because it doesn't change anything but the sign.
8445 // fabs(X) == 0.0 --> X == 0.0,
8446 // fabs(X) != 0.0 --> X != 0.0
8447 // isnan(fabs(X)) --> isnan(X)
8448 // !isnan(fabs(X) --> !isnan(X)
8449 return replacePredAndOp0(&I, I.getPredicate(), X);
8450
8451 default:
8452 return nullptr;
8453 }
8454}
8455
8456/// Optimize sqrt(X) compared with zero.
8458 Value *X;
8459 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))
8460 return nullptr;
8461
8462 if (!match(I.getOperand(1), m_PosZeroFP()))
8463 return nullptr;
8464
8465 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8466 I.setPredicate(P);
8467 return IC.replaceOperand(I, 0, X);
8468 };
8469
8470 // Clear ninf flag if sqrt doesn't have it.
8471 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())
8472 I.setHasNoInfs(false);
8473
8474 switch (I.getPredicate()) {
8475 case FCmpInst::FCMP_OLT:
8476 case FCmpInst::FCMP_UGE:
8477 // sqrt(X) < 0.0 --> false
8478 // sqrt(X) u>= 0.0 --> true
8479 llvm_unreachable("fcmp should have simplified");
8480 case FCmpInst::FCMP_ULT:
8481 case FCmpInst::FCMP_ULE:
8482 case FCmpInst::FCMP_OGT:
8483 case FCmpInst::FCMP_OGE:
8484 case FCmpInst::FCMP_OEQ:
8485 case FCmpInst::FCMP_UNE:
8486 // sqrt(X) u< 0.0 --> X u< 0.0
8487 // sqrt(X) u<= 0.0 --> X u<= 0.0
8488 // sqrt(X) > 0.0 --> X > 0.0
8489 // sqrt(X) >= 0.0 --> X >= 0.0
8490 // sqrt(X) == 0.0 --> X == 0.0
8491 // sqrt(X) u!= 0.0 --> X u!= 0.0
8492 return IC.replaceOperand(I, 0, X);
8493
8494 case FCmpInst::FCMP_OLE:
8495 // sqrt(X) <= 0.0 --> X == 0.0
8496 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8497 case FCmpInst::FCMP_UGT:
8498 // sqrt(X) u> 0.0 --> X u!= 0.0
8499 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8500 case FCmpInst::FCMP_UEQ:
8501 // sqrt(X) u== 0.0 --> X u<= 0.0
8502 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8503 case FCmpInst::FCMP_ONE:
8504 // sqrt(X) != 0.0 --> X > 0.0
8505 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8506 case FCmpInst::FCMP_ORD:
8507 // !isnan(sqrt(X)) --> X >= 0.0
8508 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8509 case FCmpInst::FCMP_UNO:
8510 // isnan(sqrt(X)) --> X u< 0.0
8511 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8512 default:
8513 llvm_unreachable("Unexpected predicate!");
8514 }
8515}
8516
8518 CmpInst::Predicate Pred = I.getPredicate();
8519 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8520
8521 // Canonicalize fneg as Op1.
8522 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
8523 std::swap(Op0, Op1);
8524 Pred = I.getSwappedPredicate();
8525 }
8526
8527 if (!match(Op1, m_FNeg(m_Specific(Op0))))
8528 return nullptr;
8529
8530 // Replace the negated operand with 0.0:
8531 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8532 Constant *Zero = ConstantFP::getZero(Op0->getType());
8533 return new FCmpInst(Pred, Op0, Zero, "", &I);
8534}
8535
8537 Constant *RHSC, InstCombinerImpl &CI) {
8538 const CmpInst::Predicate Pred = I.getPredicate();
8539 Value *X = LHSI->getOperand(0);
8540 Value *Y = LHSI->getOperand(1);
8541 switch (Pred) {
8542 default:
8543 break;
8544 case FCmpInst::FCMP_UGT:
8545 case FCmpInst::FCMP_ULT:
8546 case FCmpInst::FCMP_UNE:
8547 case FCmpInst::FCMP_OEQ:
8548 case FCmpInst::FCMP_OGE:
8549 case FCmpInst::FCMP_OLE:
8550 // The optimization is not valid if X and Y are infinities of the same
8551 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8552 // flag then we can assume we do not have that case. Otherwise we might be
8553 // able to prove that either X or Y is not infinity.
8554 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8558 break;
8559
8560 [[fallthrough]];
8561 case FCmpInst::FCMP_OGT:
8562 case FCmpInst::FCMP_OLT:
8563 case FCmpInst::FCMP_ONE:
8564 case FCmpInst::FCMP_UEQ:
8565 case FCmpInst::FCMP_UGE:
8566 case FCmpInst::FCMP_ULE:
8567 // fcmp pred (x - y), 0 --> fcmp pred x, y
8568 if (match(RHSC, m_AnyZeroFP()) &&
8569 I.getFunction()->getDenormalMode(
8570 LHSI->getType()->getScalarType()->getFltSemantics()) ==
8572 CI.replaceOperand(I, 0, X);
8573 CI.replaceOperand(I, 1, Y);
8574 return &I;
8575 }
8576 break;
8577 }
8578
8579 return nullptr;
8580}
8581
8583 InstCombinerImpl &IC) {
8584 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
8585 Type *OpType = LHS->getType();
8586 CmpInst::Predicate Pred = I.getPredicate();
8587
8588 bool FloorX = match(LHS, m_Intrinsic<Intrinsic::floor>(m_Specific(RHS)));
8589 bool CeilX = match(LHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(RHS)));
8590
8591 if (!FloorX && !CeilX) {
8592 if ((FloorX = match(RHS, m_Intrinsic<Intrinsic::floor>(m_Specific(LHS)))) ||
8593 (CeilX = match(RHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(LHS))))) {
8594 std::swap(LHS, RHS);
8595 Pred = I.getSwappedPredicate();
8596 }
8597 }
8598
8599 switch (Pred) {
8600 case FCmpInst::FCMP_OLE:
8601 // fcmp ole floor(x), x => fcmp ord x, 0
8602 if (FloorX)
8604 "", &I);
8605 break;
8606 case FCmpInst::FCMP_OGT:
8607 // fcmp ogt floor(x), x => false
8608 if (FloorX)
8609 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8610 break;
8611 case FCmpInst::FCMP_OGE:
8612 // fcmp oge ceil(x), x => fcmp ord x, 0
8613 if (CeilX)
8615 "", &I);
8616 break;
8617 case FCmpInst::FCMP_OLT:
8618 // fcmp olt ceil(x), x => false
8619 if (CeilX)
8620 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8621 break;
8622 case FCmpInst::FCMP_ULE:
8623 // fcmp ule floor(x), x => true
8624 if (FloorX)
8625 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8626 break;
8627 case FCmpInst::FCMP_UGT:
8628 // fcmp ugt floor(x), x => fcmp uno x, 0
8629 if (FloorX)
8631 "", &I);
8632 break;
8633 case FCmpInst::FCMP_UGE:
8634 // fcmp uge ceil(x), x => true
8635 if (CeilX)
8636 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8637 break;
8638 case FCmpInst::FCMP_ULT:
8639 // fcmp ult ceil(x), x => fcmp uno x, 0
8640 if (CeilX)
8642 "", &I);
8643 break;
8644 default:
8645 break;
8646 }
8647
8648 return nullptr;
8649}
8650
8652 bool Changed = false;
8653
8654 /// Orders the operands of the compare so that they are listed from most
8655 /// complex to least complex. This puts constants before unary operators,
8656 /// before binary operators.
8657 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
8658 I.swapOperands();
8659 Changed = true;
8660 }
8661
8662 const CmpInst::Predicate Pred = I.getPredicate();
8663 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8664 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
8666 return replaceInstUsesWith(I, V);
8667
8668 // Simplify 'fcmp pred X, X'
8669 Type *OpType = Op0->getType();
8670 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
8671 if (Op0 == Op1) {
8672 switch (Pred) {
8673 default:
8674 break;
8675 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
8676 case FCmpInst::FCMP_ULT: // True if unordered or less than
8677 case FCmpInst::FCMP_UGT: // True if unordered or greater than
8678 case FCmpInst::FCMP_UNE: // True if unordered or not equal
8679 // Canonicalize these to be 'fcmp uno %X, 0.0'.
8680 I.setPredicate(FCmpInst::FCMP_UNO);
8681 I.setOperand(1, Constant::getNullValue(OpType));
8682 return &I;
8683
8684 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
8685 case FCmpInst::FCMP_OEQ: // True if ordered and equal
8686 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
8687 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
8688 // Canonicalize these to be 'fcmp ord %X, 0.0'.
8689 I.setPredicate(FCmpInst::FCMP_ORD);
8690 I.setOperand(1, Constant::getNullValue(OpType));
8691 return &I;
8692 }
8693 }
8694
8695 if (I.isCommutative()) {
8696 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
8697 replaceOperand(I, 0, Pair->first);
8698 replaceOperand(I, 1, Pair->second);
8699 return &I;
8700 }
8701 }
8702
8703 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
8704 // then canonicalize the operand to 0.0.
8705 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
8706 if (!match(Op0, m_PosZeroFP()) &&
8707 isKnownNeverNaN(Op0, getSimplifyQuery().getWithInstruction(&I)))
8708 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
8709
8710 if (!match(Op1, m_PosZeroFP()) &&
8711 isKnownNeverNaN(Op1, getSimplifyQuery().getWithInstruction(&I)))
8712 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8713 }
8714
8715 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
8716 Value *X, *Y;
8717 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
8718 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
8719
8721 return R;
8722
8723 // Test if the FCmpInst instruction is used exclusively by a select as
8724 // part of a minimum or maximum operation. If so, refrain from doing
8725 // any other folding. This helps out other analyses which understand
8726 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
8727 // and CodeGen. And in this case, at least one of the comparison
8728 // operands has at least one user besides the compare (the select),
8729 // which would often largely negate the benefit of folding anyway.
8730 if (I.hasOneUse())
8731 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
8732 Value *A, *B;
8734 if (SPR.Flavor != SPF_UNKNOWN)
8735 return nullptr;
8736 }
8737
8738 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
8739 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
8740 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
8741 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8742
8743 // Canonicalize:
8744 // fcmp olt X, +inf -> fcmp one X, +inf
8745 // fcmp ole X, +inf -> fcmp ord X, 0
8746 // fcmp ogt X, +inf -> false
8747 // fcmp oge X, +inf -> fcmp oeq X, +inf
8748 // fcmp ult X, +inf -> fcmp une X, +inf
8749 // fcmp ule X, +inf -> true
8750 // fcmp ugt X, +inf -> fcmp uno X, 0
8751 // fcmp uge X, +inf -> fcmp ueq X, +inf
8752 // fcmp olt X, -inf -> false
8753 // fcmp ole X, -inf -> fcmp oeq X, -inf
8754 // fcmp ogt X, -inf -> fcmp one X, -inf
8755 // fcmp oge X, -inf -> fcmp ord X, 0
8756 // fcmp ult X, -inf -> fcmp uno X, 0
8757 // fcmp ule X, -inf -> fcmp ueq X, -inf
8758 // fcmp ugt X, -inf -> fcmp une X, -inf
8759 // fcmp uge X, -inf -> true
8760 const APFloat *C;
8761 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
8762 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
8763 default:
8764 break;
8765 case FCmpInst::FCMP_ORD:
8766 case FCmpInst::FCMP_UNO:
8769 case FCmpInst::FCMP_OGT:
8770 case FCmpInst::FCMP_ULE:
8771 llvm_unreachable("Should be simplified by InstSimplify");
8772 case FCmpInst::FCMP_OLT:
8773 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
8774 case FCmpInst::FCMP_OLE:
8775 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
8776 "", &I);
8777 case FCmpInst::FCMP_OGE:
8778 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
8779 case FCmpInst::FCMP_ULT:
8780 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
8781 case FCmpInst::FCMP_UGT:
8782 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8783 "", &I);
8784 case FCmpInst::FCMP_UGE:
8785 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8786 }
8787 }
8788
8789 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8790 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8791 if (match(Op1, m_PosZeroFP()) &&
8794 if (Pred == FCmpInst::FCMP_OEQ)
8795 IntPred = ICmpInst::ICMP_EQ;
8796 else if (Pred == FCmpInst::FCMP_UNE)
8797 IntPred = ICmpInst::ICMP_NE;
8798
8799 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8800 Type *IntTy = X->getType();
8801 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8802 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8803 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8804 }
8805 }
8806
8807 // Handle fcmp with instruction LHS and constant RHS.
8808 Instruction *LHSI;
8809 Constant *RHSC;
8810 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8811 switch (LHSI->getOpcode()) {
8812 case Instruction::Select:
8813 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8814 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8816 return replaceOperand(I, 0, X);
8817 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
8818 return NV;
8819 break;
8820 case Instruction::FSub:
8821 if (LHSI->hasOneUse())
8822 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
8823 return NV;
8824 break;
8825 case Instruction::PHI:
8826 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8827 return NV;
8828 break;
8829 case Instruction::SIToFP:
8830 case Instruction::UIToFP:
8831 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8832 return NV;
8833 break;
8834 case Instruction::FDiv:
8835 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8836 return NV;
8837 break;
8838 case Instruction::Load:
8839 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8840 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
8842 cast<LoadInst>(LHSI), GEP, GV, I))
8843 return Res;
8844 break;
8845 case Instruction::FPTrunc:
8846 if (Instruction *NV = foldFCmpFpTrunc(I, *LHSI, *RHSC))
8847 return NV;
8848 break;
8849 }
8850 }
8851
8852 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8853 return R;
8854
8855 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))
8856 return R;
8857
8858 if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this))
8859 return R;
8860
8861 if (match(Op0, m_FNeg(m_Value(X)))) {
8862 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8863 Constant *C;
8864 if (match(Op1, m_Constant(C)))
8865 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8866 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8867 }
8868
8869 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8870 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8871 return new FCmpInst(Pred, X, Op1, "", &I);
8872
8873 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8874 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8875 return new FCmpInst(Pred, Op0, Y, "", &I);
8876
8877 if (match(Op0, m_FPExt(m_Value(X)))) {
8878 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8879 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8880 return new FCmpInst(Pred, X, Y, "", &I);
8881
8882 const APFloat *C;
8883 if (match(Op1, m_APFloat(C))) {
8884 const fltSemantics &FPSem =
8885 X->getType()->getScalarType()->getFltSemantics();
8886 bool Lossy;
8887 APFloat TruncC = *C;
8888 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8889
8890 if (Lossy) {
8891 // X can't possibly equal the higher-precision constant, so reduce any
8892 // equality comparison.
8893 // TODO: Other predicates can be handled via getFCmpCode().
8894 switch (Pred) {
8895 case FCmpInst::FCMP_OEQ:
8896 // X is ordered and equal to an impossible constant --> false
8897 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8898 case FCmpInst::FCMP_ONE:
8899 // X is ordered and not equal to an impossible constant --> ordered
8900 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8901 ConstantFP::getZero(X->getType()));
8902 case FCmpInst::FCMP_UEQ:
8903 // X is unordered or equal to an impossible constant --> unordered
8904 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8905 ConstantFP::getZero(X->getType()));
8906 case FCmpInst::FCMP_UNE:
8907 // X is unordered or not equal to an impossible constant --> true
8908 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8909 default:
8910 break;
8911 }
8912 }
8913
8914 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8915 // Avoid lossy conversions and denormals.
8916 // Zero is a special case that's OK to convert.
8917 APFloat Fabs = TruncC;
8918 Fabs.clearSign();
8919 if (!Lossy &&
8920 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8921 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8922 return new FCmpInst(Pred, X, NewC, "", &I);
8923 }
8924 }
8925 }
8926
8927 // Convert a sign-bit test of an FP value into a cast and integer compare.
8928 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8929 // TODO: Handle non-zero compare constants.
8930 // TODO: Handle other predicates.
8931 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),
8932 m_Value(X)))) &&
8933 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8934 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8935 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8936 IntType = VectorType::get(IntType, VecTy->getElementCount());
8937
8938 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8939 if (Pred == FCmpInst::FCMP_OLT) {
8940 Value *IntX = Builder.CreateBitCast(X, IntType);
8941 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8942 ConstantInt::getNullValue(IntType));
8943 }
8944 }
8945
8946 {
8947 Value *CanonLHS = nullptr, *CanonRHS = nullptr;
8948 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));
8949 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));
8950
8951 // (canonicalize(x) == x) => (x == x)
8952 if (CanonLHS == Op1)
8953 return new FCmpInst(Pred, Op1, Op1, "", &I);
8954
8955 // (x == canonicalize(x)) => (x == x)
8956 if (CanonRHS == Op0)
8957 return new FCmpInst(Pred, Op0, Op0, "", &I);
8958
8959 // (canonicalize(x) == canonicalize(y)) => (x == y)
8960 if (CanonLHS && CanonRHS)
8961 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8962 }
8963
8964 if (I.getType()->isVectorTy())
8965 if (Instruction *Res = foldVectorCmp(I, Builder))
8966 return Res;
8967
8968 return Changed ? &I : nullptr;
8969}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define Check(C,...)
Hexagon Common GEP
static Instruction * foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
static Instruction * foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC)
Optimize fabs(X) compared with zero.
static void collectOffsetOp(Value *V, SmallVectorImpl< OffsetOp > &Offsets, bool AllowRecursion)
static Value * rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW, const DataLayout &DL, SetVector< Value * > &Explored, InstCombiner &IC)
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
static Instruction * foldICmpEqualityWithOffset(ICmpInst &I, InstCombiner::BuilderTy &Builder, const SimplifyQuery &SQ)
Offset both sides of an equality icmp to see if we can save some instructions: icmp eq/ne X,...
static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2, bool IsSigned=false)
Compute Result = In1+In2, returning true if the result overflowed for this type.
static Instruction * foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * foldVectorCmp(CmpInst &Cmp, InstCombiner::BuilderTy &Builder)
static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q, unsigned Depth=0)
static Value * createLogicFromTable(const std::bitset< 4 > &Table, Value *Op0, Value *Op1, IRBuilderBase &Builder, bool HasOneUse)
static Instruction * foldICmpOfUAddOv(ICmpInst &I)
static bool isChainSelectCmpBranch(const SelectInst *SI)
Return true when the instruction sequence within a block is select-cmp-br.
static Instruction * foldICmpInvariantGroup(ICmpInst &I)
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
static Instruction * foldReductionIdiom(ICmpInst &I, InstCombiner::BuilderTy &Builder, const DataLayout &DL)
This function folds patterns produced by lowering of reduce idioms, such as llvm.vector....
static Instruction * canonicalizeICmpBool(ICmpInst &I, InstCombiner::BuilderTy &Builder)
Integer compare with boolean values can always be turned into bitwise ops.
static Instruction * foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI, Constant *RHSC, InstCombinerImpl &CI)
static Value * foldICmpOrXorSubChain(ICmpInst &Cmp, BinaryOperator *Or, InstCombiner::BuilderTy &Builder)
Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
static bool hasBranchUse(ICmpInst &I)
Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparis...
static Value * foldICmpWithLowBitMaskedVal(CmpPredicate Pred, Value *Op0, Value *Op1, const SimplifyQuery &Q, InstCombiner &IC)
Some comparisons can be simplified.
static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth)
When performing a comparison against a constant, it is possible that not all the bits in the LHS are ...
static Instruction * foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl, const APInt &C)
Fold icmp (shl nuw C2, Y), C.
static Instruction * foldFCmpWithFloorAndCeil(FCmpInst &I, InstCombinerImpl &IC)
static Instruction * foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * foldICmpOfCmpIntrinsicWithConstant(CmpPredicate Pred, IntrinsicInst *I, const APInt &C, InstCombiner::BuilderTy &Builder)
static Instruction * processUMulZExtIdiom(ICmpInst &I, Value *MulVal, const APInt *OtherVal, InstCombinerImpl &IC)
Recognize and process idiom involving test for multiplication overflow.
static Instruction * foldSqrtWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC)
Optimize sqrt(X) compared with zero.
static Instruction * foldFCmpFNegCommonOp(FCmpInst &I)
static Instruction * foldICmpWithHighBitMask(ICmpInst &Cmp, InstCombiner::BuilderTy &Builder)
static ICmpInst * canonicalizeCmpWithConstant(ICmpInst &I)
If we have an icmp le or icmp ge instruction with a constant operand, turn it into the appropriate ic...
static Instruction * foldICmpIntrinsicWithIntrinsic(ICmpInst &Cmp, InstCombiner::BuilderTy &Builder)
Fold an icmp with LLVM intrinsics.
static Instruction * foldICmpUSubSatOrUAddSatWithConstant(CmpPredicate Pred, SaturatingInst *II, const APInt &C, InstCombiner::BuilderTy &Builder)
static Instruction * foldICmpPow2Test(ICmpInst &I, InstCombiner::BuilderTy &Builder)
static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2, bool IsSigned=false)
Compute Result = In1-In2, returning true if the result overflowed for this type.
static bool canRewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags &NW, const DataLayout &DL, SetVector< Value * > &Explored)
Returns true if we can rewrite Start as a GEP with pointer Base and some integer offset.
static Instruction * foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc, const Constant &C)
static Instruction * foldICmpXNegX(ICmpInst &I, InstCombiner::BuilderTy &Builder)
static Instruction * processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, ConstantInt *CI2, ConstantInt *CI1, InstCombinerImpl &IC)
The caller has matched a pattern of the form: I = icmp ugt (add (add A, B), CI2), CI1 If this is of t...
static Value * foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ, InstCombiner::BuilderTy &Builder)
static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C)
Returns true if the exploded icmp can be expressed as a signed comparison to zero and updates the pre...
static Instruction * transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, CmpPredicate Cond, const DataLayout &DL, InstCombiner &IC)
Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
static Instruction * foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs, const APInt &CRhs, InstCombiner::BuilderTy &Builder, const SimplifyQuery &Q)
static void setInsertionPoint(IRBuilder<> &Builder, Value *V, bool Before=true)
static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned)
static bool isMultipleOf(Value *X, const APInt &C, const SimplifyQuery &Q)
Return true if X is a multiple of C.
static Value * foldICmpWithTruncSignExtendedVal(ICmpInst &I, InstCombiner::BuilderTy &Builder)
Some comparisons can be simplified.
static Instruction * foldICmpOrXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
This file provides internal interfaces used to implement the InstCombine.
This file provides the interface for the instcombine pass implementation.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:546
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
#define T1
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
Value * RHS
Value * LHS
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:6057
void clearSign()
Definition: APFloat.h:1298
bool isNaN() const
Definition: APFloat.h:1447
bool isZero() const
Definition: APFloat.h:1445
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1158
APInt bitcastToAPInt() const
Definition: APFloat.h:1353
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1138
opStatus next(bool nextDown)
Definition: APFloat.h:1254
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1098
LLVM_ABI FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5986
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1248
bool isInfinity() const
Definition: APFloat.h:1446
Class for arbitrary precision integers.
Definition: APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
Definition: APInt.cpp:1758
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition: APInt.h:449
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:1012
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:229
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:423
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1540
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1512
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:936
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:206
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1330
APInt abs() const
Get the absolute value.
Definition: APInt.h:1795
unsigned ceilLogBase2() const
Definition: APInt.h:1764
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1201
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
LLVM_ABI APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1948
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition: APInt.h:466
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:216
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1928
bool eq(const APInt &RHS) const
Equality comparison.
Definition: APInt.h:1079
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1644
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1935
void negate()
Negate this APInt in place.
Definition: APInt.h:1468
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1639
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1598
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:356
void flipAllBits()
Toggle every bit to its opposite value.
Definition: APInt.h:1452
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1615
unsigned logBase2() const
Definition: APInt.h:1761
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:475
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:827
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:405
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1237
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:389
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
unsigned countr_one() const
Count the number of trailing one bits.
Definition: APInt.h:1656
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:24
an instruction to allocate memory on the stack
Definition: Instructions.h:64
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Class to represent array types.
Definition: DerivedTypes.h:398
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...
Definition: BasicBlock.cpp:393
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:437
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:233
BinaryOps getOpcode() const
Definition: InstrTypes.h:374
static LLVM_ABI BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Conditional or Unconditional Branch instruction.
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1292
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:666
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:984
Predicate getStrictPredicate() const
For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
Definition: InstrTypes.h:860
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:681
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:695
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:692
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:686
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:689
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:685
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:687
@ ICMP_EQ
equal
Definition: InstrTypes.h:699
@ ICMP_NE
not equal
Definition: InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:706
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:694
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:691
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:680
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:688
bool isSigned() const
Definition: InstrTypes.h:932
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:829
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:944
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:873
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:791
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:767
bool isStrictPredicate() const
Definition: InstrTypes.h:845
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:895
bool isIntPredicate() const
Definition: InstrTypes.h:785
bool isUnsigned() const
Definition: InstrTypes.h:938
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:23
CmpInst::Predicate dropSameSign() const
Drops samesign information.
Definition: CmpPredicate.h:47
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2314
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2261
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2654
static LLVM_ABI Constant * getNot(Constant *C)
Definition: Constants.cpp:2641
static LLVM_ABI Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2300
static LLVM_ABI Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2661
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2635
static LLVM_ABI Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1059
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition: Constants.h:264
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:868
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:214
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:131
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:875
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:157
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:154
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:882
This class represents a range of values.
Definition: ConstantRange.h:47
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.
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.
Definition: Constants.cpp:1474
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...
Definition: Constants.cpp:403
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
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,...
Definition: Constants.cpp:1778
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
Definition: DataLayout.cpp:850
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
Definition: DataLayout.cpp:742
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
Definition: DataLayout.cpp:877
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:504
LLVM_ABI Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
Definition: DataLayout.cpp:865
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:177
iterator end()
Definition: DenseMap.h:87
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:168
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:135
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This instruction compares its operands according to the predicate given to the constructor.
bool isEquality() const
Represents flags for the getelementptr instruction/expression.
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() 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:70
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
Definition: Instructions.h:949
Type * getValueType() const
Definition: GlobalValue.h:298
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
static bool isGE(Predicate P)
Return true if the predicate is SGE or UGE.
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static bool isLT(Predicate P)
Return true if the predicate is SLT or ULT.
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
static bool isLE(Predicate P)
Return true if the predicate is SLE or ULE.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:114
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2345
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2559
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:575
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2353
LLVM_ABI Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1115
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2618
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:502
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1005
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1513
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition: IRBuilder.h:2238
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2333
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:1923
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1781
LLVM_ABI Value * createIsFPClass(Value *FPNum, unsigned Test)
Definition: IRBuilder.cpp:1223
LLVM_ABI CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:834
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:522
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2463
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2494
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1805
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2329
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1420
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2204
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2337
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.
Definition: IRBuilder.cpp:815
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1492
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2082
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1551
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1403
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:507
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2508
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2068
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1708
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2361
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2277
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition: IRBuilder.h:2646
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:207
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1599
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2439
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition: IRBuilder.h:1573
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:552
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:538
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1480
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1437
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2780
Instruction * foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr, const APInt &C)
Fold icmp ({al}shr X, Y), C.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * foldICmpWithZextOrSext(ICmpInst &ICmp)
Instruction * foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select, ConstantInt *C)
Instruction * foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Instruction * foldICmpBinOpWithConstant(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
Instruction * foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, const APInt &C)
Fold icmp (or X, Y), C.
Instruction * foldICmpTruncWithTruncOrExt(ICmpInst &Cmp, const SimplifyQuery &Q)
Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
Instruction * foldSignBitTest(ICmpInst &I)
Fold equality-comparison between zero and any (maybe truncated) right-shift by one-less-than-bitwidth...
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN, 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 * foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub, const APInt &C)
Fold icmp (sub X, Y), C.
Instruction * foldICmpInstWithConstantNotInt(ICmpInst &Cmp)
Handle icmp with constant (but not simple integer constant) RHS.
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.
Value * foldMultiplicationOverflowCheck(ICmpInst &Cmp)
Fold (-1 u/ x) u< y ((x * y) ?/ x) != y to @llvm.
Instruction * foldICmpWithConstant(ICmpInst &Cmp)
Fold icmp Pred X, C.
CmpInst * canonicalizeICmpPredicate(CmpInst &I)
If we have a comparison with a non-canonical predicate, if we can update all the users,...
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * foldICmpWithZero(ICmpInst &Cmp)
Instruction * foldICmpCommutative(CmpPredicate Pred, Value *Op0, Value *Op1, ICmpInst &CxtI)
Instruction * foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Fold an icmp equality instruction with binary operator LHS and constant RHS: icmp eq/ne BO,...
Instruction * foldICmpUsingBoolRange(ICmpInst &I)
If one operand of an icmp is effectively a bool (value range of {0,1}), then try to reduce patterns b...
Instruction * foldICmpWithTrunc(ICmpInst &Cmp)
Instruction * foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, const APInt &C)
Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS, ConstantInt *&Less, ConstantInt *&Equal, ConstantInt *&Greater)
Match a select chain which produces one of three values based on whether the LHS is less than,...
Instruction * foldCmpLoadFromIndexedGlobal(LoadInst *LI, GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
This is called when we see this pattern: cmp pred (load (gep GV, ...)), cmpcst where GV is a global v...
Instruction * visitFCmpInst(FCmpInst &I)
Instruction * foldICmpUsingKnownBits(ICmpInst &Cmp)
Try to fold the comparison based on range information we can get by checking whether bits are known t...
Instruction * foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, const APInt &C)
Fold icmp ({su}div X, Y), C.
Instruction * foldIRemByPowerOfTwoToBitTest(ICmpInst &I)
If we have: icmp eq/ne (urem/srem x, y), 0 iff y is a power-of-two, we can replace this with a bit te...
Instruction * foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold fcmp ([us]itofp x, cst) if possible.
Instruction * foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Fold icmp (udiv X, Y), C.
Instruction * foldICmpAddOpConst(Value *X, const APInt &C, CmpPredicate Pred)
Fold "icmp pred (X+C), X".
Constant * getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp)
Instruction * foldICmpWithCastOp(ICmpInst &ICmp)
Handle icmp (cast x), (cast or constant).
Instruction * foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc, const APInt &C)
Fold icmp (trunc X), C.
Instruction * foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add, const APInt &C)
Fold icmp (add X, Y), C.
Instruction * foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul, const APInt &C)
Fold icmp (mul X, Y), C.
Instruction * tryFoldInstWithCtpopWithNot(Instruction *I)
Instruction * foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor, const APInt &C)
Fold icmp (xor X, Y), C.
Instruction * 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.
Definition: InstCombiner.h:48
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:505
SimplifyQuery SQ
Definition: InstCombiner.h:77
unsigned ComputeMaxSignificantBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
Definition: InstCombiner.h:462
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:228
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI, bool IsNSW=false) const
Definition: InstCombiner.h:468
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
Definition: InstCombiner.h:143
TargetLibraryInfo & TLI
Definition: InstCombiner.h:74
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:388
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
Definition: InstCombiner.h:56
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:491
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
Definition: InstCombiner.h:183
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:498
static bool isCanonicalPredicate(CmpPredicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
Definition: InstCombiner.h:160
const DataLayout & DL
Definition: InstCombiner.h:76
DomConditionCache DC
Definition: InstCombiner.h:82
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
Definition: InstCombiner.h:433
bool canFreelyInvertAllUsersOf(Instruction *V, Value *IgnoredUser)
Given i1 V, can every user of V be freely adapted if V is changed to !V ? InstCombine's freelyInvertA...
Definition: InstCombiner.h:244
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:332
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:412
DominatorTree & DT
Definition: InstCombiner.h:75
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:476
BuilderTy & Builder
Definition: InstCombiner.h:61
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:483
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:209
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:338
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, const Instruction *CxtI=nullptr, unsigned Depth=0)
Definition: InstCombiner.h:443
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.
Definition: Instruction.h:361
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.
Definition: Instruction.h:312
bool isShift() const
Definition: Instruction.h:320
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:319
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:56
An instruction for reading from memory.
Definition: Instructions.h:180
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:209
This class represents min/max intrinsics.
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, Instruction *MDFrom=nullptr)
A vector that has set insertion semantics.
Definition: SetVector.h:59
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:104
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:168
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition: SetVector.h:269
This instruction constructs a fixed permutation of two input vectors.
bool empty() const
Definition: SmallVector.h:82
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Class to represent struct types.
Definition: DerivedTypes.h:218
This class represents a truncation of integer types.
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:264
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:246
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:267
Type * getArrayElementType() const
Definition: Type.h:408
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition: Type.h:165
LLVM_ABI Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
LLVM_ABI const fltSemantics & getFltSemantics() const
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:240
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
void setOperand(unsigned i, Value *Val)
Definition: User.h:237
Value * getOperand(unsigned i) const
Definition: User.h:232
unsigned getNumOperands() const
Definition: User.h:254
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:439
iterator_range< user_iterator > users()
Definition: Value.h:426
LLVM_ABI bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition: Value.cpp:158
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:701
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
iterator_range< use_iterator > uses()
Definition: Value.h:380
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:396
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:203
const ParentTy * getParent() const
Definition: ilist_node.h:34
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A unsign-divided by B, rounded by the given rounding mode.
Definition: APInt.cpp:2763
LLVM_ABI APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A sign-divided by B, rounded by the given rounding mode.
Definition: APInt.cpp:2781
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:751
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:550
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:664
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:862
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:766
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:962
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)
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:980
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:931
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:639
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
Definition: PatternMatch.h:481
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:683
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
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.
Definition: PatternMatch.h:775
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
Definition: PatternMatch.h:203
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
Definition: PatternMatch.h:700
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
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:860
OverflowResult
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1744
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.
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:663
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:1534
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
LLVM_ABI Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
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.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:157
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.
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:336
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:203
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
Definition: ValueTracking.h:47
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_UNKNOWN
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI bool isFinite(const Loop *L)
Return true if this loop can be assumed to run for a finite number of iterations.
Definition: LoopInfo.cpp:1164
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:1543
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 ...
Definition: Function.cpp:1172
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:1758
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 * 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.
@ Other
Any other memory.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
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
Definition: BitmaskEnum.h:223
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:1980
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:2127
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
@ Continue
Definition: DWP.h:22
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false, bool DecomposeAnd=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858
#define NC
Definition: regutils.h:42
Value * materialize(InstCombiner::BuilderTy &Builder) const
static OffsetResult value(Value *V)
static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV)
static OffsetResult invalid()
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:304
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:308
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:101
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:80
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:235
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:267
APInt getSignedMaxValue() const
Return the maximal signed value possible given these KnownBits.
Definition: KnownBits.h:144
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:282
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:241
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:138
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:122
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:107
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:98
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:279
APInt getSignedMinValue() const
Return the minimal signed value possible given these KnownBits.
Definition: KnownBits.h:128
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:60
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
Definition: SimplifyQuery.h:72
const Instruction * CxtI
Definition: SimplifyQuery.h:76
const DominatorTree * DT
Definition: SimplifyQuery.h:74
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:75
SimplifyQuery getWithoutDomCondCache() const
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:249
Capture information for a specific Use.