LLVM 21.0.0git
InstCombineCompares.cpp
Go to the documentation of this file.
1//===- InstCombineCompares.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the visitICmp and visitFCmp functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/APSInt.h"
15#include "llvm/ADT/SetVector.h"
16#include "llvm/ADT/Statistic.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/InstrTypes.h"
30#include <bitset>
31
32using namespace llvm;
33using namespace PatternMatch;
34
35#define DEBUG_TYPE "instcombine"
36
37// How many times is a select replaced by one of its operands?
38STATISTIC(NumSel, "Number of select opts");
39
40/// Compute Result = In1+In2, returning true if the result overflowed for this
41/// type.
42static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
43 bool IsSigned = false) {
44 bool Overflow;
45 if (IsSigned)
46 Result = In1.sadd_ov(In2, Overflow);
47 else
48 Result = In1.uadd_ov(In2, Overflow);
49
50 return Overflow;
51}
52
53/// Compute Result = In1-In2, returning true if the result overflowed for this
54/// type.
55static bool subWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
56 bool IsSigned = false) {
57 bool Overflow;
58 if (IsSigned)
59 Result = In1.ssub_ov(In2, Overflow);
60 else
61 Result = In1.usub_ov(In2, Overflow);
62
63 return Overflow;
64}
65
66/// Given an icmp instruction, return true if any use of this comparison is a
67/// branch on sign bit comparison.
68static bool hasBranchUse(ICmpInst &I) {
69 for (auto *U : I.users())
70 if (isa<BranchInst>(U))
71 return true;
72 return false;
73}
74
75/// Returns true if the exploded icmp can be expressed as a signed comparison
76/// to zero and updates the predicate accordingly.
77/// The signedness of the comparison is preserved.
78/// TODO: Refactor with decomposeBitTestICmp()?
79static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
80 if (!ICmpInst::isSigned(Pred))
81 return false;
82
83 if (C.isZero())
84 return ICmpInst::isRelational(Pred);
85
86 if (C.isOne()) {
87 if (Pred == ICmpInst::ICMP_SLT) {
88 Pred = ICmpInst::ICMP_SLE;
89 return true;
90 }
91 } else if (C.isAllOnes()) {
92 if (Pred == ICmpInst::ICMP_SGT) {
93 Pred = ICmpInst::ICMP_SGE;
94 return true;
95 }
96 }
97
98 return false;
99}
100
101/// This is called when we see this pattern:
102/// cmp pred (load (gep GV, ...)), cmpcst
103/// where GV is a global variable with a constant initializer. Try to simplify
104/// this into some simple computation that does not need the load. For example
105/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
106///
107/// If AndCst is non-null, then the loaded value is masked with that constant
108/// before doing the comparison. This handles cases like "A[i]&4 == 0".
111 ConstantInt *AndCst) {
112 if (LI->isVolatile() || LI->getType() != GEP->getResultElementType() ||
113 GV->getValueType() != GEP->getSourceElementType() || !GV->isConstant() ||
115 return nullptr;
116
118 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
119 return nullptr;
120
121 uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
122 // Don't blow up on huge arrays.
123 if (ArrayElementCount > MaxArraySizeForCombine)
124 return nullptr;
125
126 // There are many forms of this optimization we can handle, for now, just do
127 // the simple index into a single-dimensional array.
128 //
129 // Require: GEP GV, 0, i {{, constant indices}}
130 if (GEP->getNumOperands() < 3 || !isa<ConstantInt>(GEP->getOperand(1)) ||
131 !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
132 isa<Constant>(GEP->getOperand(2)))
133 return nullptr;
134
135 // Check that indices after the variable are constants and in-range for the
136 // type they index. Collect the indices. This is typically for arrays of
137 // structs.
138 SmallVector<unsigned, 4> LaterIndices;
139
140 Type *EltTy = Init->getType()->getArrayElementType();
141 for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
142 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
143 if (!Idx)
144 return nullptr; // Variable index.
145
146 uint64_t IdxVal = Idx->getZExtValue();
147 if ((unsigned)IdxVal != IdxVal)
148 return nullptr; // Too large array index.
149
150 if (StructType *STy = dyn_cast<StructType>(EltTy))
151 EltTy = STy->getElementType(IdxVal);
152 else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
153 if (IdxVal >= ATy->getNumElements())
154 return nullptr;
155 EltTy = ATy->getElementType();
156 } else {
157 return nullptr; // Unknown type.
158 }
159
160 LaterIndices.push_back(IdxVal);
161 }
162
163 enum { Overdefined = -3, Undefined = -2 };
164
165 // Variables for our state machines.
166
167 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
168 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
169 // and 87 is the second (and last) index. FirstTrueElement is -2 when
170 // undefined, otherwise set to the first true element. SecondTrueElement is
171 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
172 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
173
174 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
175 // form "i != 47 & i != 87". Same state transitions as for true elements.
176 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
177
178 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
179 /// define a state machine that triggers for ranges of values that the index
180 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
181 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
182 /// index in the range (inclusive). We use -2 for undefined here because we
183 /// use relative comparisons and don't want 0-1 to match -1.
184 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
185
186 // MagicBitvector - This is a magic bitvector where we set a bit if the
187 // comparison is true for element 'i'. If there are 64 elements or less in
188 // the array, this will fully represent all the comparison results.
189 uint64_t MagicBitvector = 0;
190
191 // Scan the array and see if one of our patterns matches.
192 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
193 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
194 Constant *Elt = Init->getAggregateElement(i);
195 if (!Elt)
196 return nullptr;
197
198 // If this is indexing an array of structures, get the structure element.
199 if (!LaterIndices.empty()) {
200 Elt = ConstantFoldExtractValueInstruction(Elt, LaterIndices);
201 if (!Elt)
202 return nullptr;
203 }
204
205 // If the element is masked, handle it.
206 if (AndCst) {
207 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);
208 if (!Elt)
209 return nullptr;
210 }
211
212 // Find out if the comparison would be true or false for the i'th element.
214 CompareRHS, DL, &TLI);
215 if (!C)
216 return nullptr;
217
218 // If the result is undef for this element, ignore it.
219 if (isa<UndefValue>(C)) {
220 // Extend range state machines to cover this element in case there is an
221 // undef in the middle of the range.
222 if (TrueRangeEnd == (int)i - 1)
223 TrueRangeEnd = i;
224 if (FalseRangeEnd == (int)i - 1)
225 FalseRangeEnd = i;
226 continue;
227 }
228
229 // If we can't compute the result for any of the elements, we have to give
230 // up evaluating the entire conditional.
231 if (!isa<ConstantInt>(C))
232 return nullptr;
233
234 // Otherwise, we know if the comparison is true or false for this element,
235 // update our state machines.
236 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
237
238 // State machine for single/double/range index comparison.
239 if (IsTrueForElt) {
240 // Update the TrueElement state machine.
241 if (FirstTrueElement == Undefined)
242 FirstTrueElement = TrueRangeEnd = i; // First true element.
243 else {
244 // Update double-compare state machine.
245 if (SecondTrueElement == Undefined)
246 SecondTrueElement = i;
247 else
248 SecondTrueElement = Overdefined;
249
250 // Update range state machine.
251 if (TrueRangeEnd == (int)i - 1)
252 TrueRangeEnd = i;
253 else
254 TrueRangeEnd = Overdefined;
255 }
256 } else {
257 // Update the FalseElement state machine.
258 if (FirstFalseElement == Undefined)
259 FirstFalseElement = FalseRangeEnd = i; // First false element.
260 else {
261 // Update double-compare state machine.
262 if (SecondFalseElement == Undefined)
263 SecondFalseElement = i;
264 else
265 SecondFalseElement = Overdefined;
266
267 // Update range state machine.
268 if (FalseRangeEnd == (int)i - 1)
269 FalseRangeEnd = i;
270 else
271 FalseRangeEnd = Overdefined;
272 }
273 }
274
275 // If this element is in range, update our magic bitvector.
276 if (i < 64 && IsTrueForElt)
277 MagicBitvector |= 1ULL << i;
278
279 // If all of our states become overdefined, bail out early. Since the
280 // predicate is expensive, only check it every 8 elements. This is only
281 // really useful for really huge arrays.
282 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
283 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
284 FalseRangeEnd == Overdefined)
285 return nullptr;
286 }
287
288 // Now that we've scanned the entire array, emit our new comparison(s). We
289 // order the state machines in complexity of the generated code.
290 Value *Idx = GEP->getOperand(2);
291
292 // If the index is larger than the pointer offset size of the target, truncate
293 // the index down like the GEP would do implicitly. We don't have to do this
294 // for an inbounds GEP because the index can't be out of range.
295 if (!GEP->isInBounds()) {
296 Type *PtrIdxTy = DL.getIndexType(GEP->getType());
297 unsigned OffsetSize = PtrIdxTy->getIntegerBitWidth();
298 if (Idx->getType()->getPrimitiveSizeInBits().getFixedValue() > OffsetSize)
299 Idx = Builder.CreateTrunc(Idx, PtrIdxTy);
300 }
301
302 // If inbounds keyword is not present, Idx * ElementSize can overflow.
303 // Let's assume that ElementSize is 2 and the wanted value is at offset 0.
304 // Then, there are two possible values for Idx to match offset 0:
305 // 0x00..00, 0x80..00.
306 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
307 // comparison is false if Idx was 0x80..00.
308 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.
309 unsigned ElementSize =
310 DL.getTypeAllocSize(Init->getType()->getArrayElementType());
311 auto MaskIdx = [&](Value *Idx) {
312 if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) {
313 Value *Mask = Constant::getAllOnesValue(Idx->getType());
314 Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize));
315 Idx = Builder.CreateAnd(Idx, Mask);
316 }
317 return Idx;
318 };
319
320 // If the comparison is only true for one or two elements, emit direct
321 // comparisons.
322 if (SecondTrueElement != Overdefined) {
323 Idx = MaskIdx(Idx);
324 // None true -> false.
325 if (FirstTrueElement == Undefined)
326 return replaceInstUsesWith(ICI, Builder.getFalse());
327
328 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
329
330 // True for one element -> 'i == 47'.
331 if (SecondTrueElement == Undefined)
332 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
333
334 // True for two elements -> 'i == 47 | i == 72'.
335 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx);
336 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
337 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx);
338 return BinaryOperator::CreateOr(C1, C2);
339 }
340
341 // If the comparison is only false for one or two elements, emit direct
342 // comparisons.
343 if (SecondFalseElement != Overdefined) {
344 Idx = MaskIdx(Idx);
345 // None false -> true.
346 if (FirstFalseElement == Undefined)
347 return replaceInstUsesWith(ICI, Builder.getTrue());
348
349 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
350
351 // False for one element -> 'i != 47'.
352 if (SecondFalseElement == Undefined)
353 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
354
355 // False for two elements -> 'i != 47 & i != 72'.
356 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx);
357 Value *SecondFalseIdx =
358 ConstantInt::get(Idx->getType(), SecondFalseElement);
359 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx);
360 return BinaryOperator::CreateAnd(C1, C2);
361 }
362
363 // If the comparison can be replaced with a range comparison for the elements
364 // where it is true, emit the range check.
365 if (TrueRangeEnd != Overdefined) {
366 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
367 Idx = MaskIdx(Idx);
368
369 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
370 if (FirstTrueElement) {
371 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
372 Idx = Builder.CreateAdd(Idx, Offs);
373 }
374
375 Value *End =
376 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1);
377 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
378 }
379
380 // False range check.
381 if (FalseRangeEnd != Overdefined) {
382 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
383 Idx = MaskIdx(Idx);
384 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
385 if (FirstFalseElement) {
386 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
387 Idx = Builder.CreateAdd(Idx, Offs);
388 }
389
390 Value *End =
391 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement);
392 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
393 }
394
395 // If a magic bitvector captures the entire comparison state
396 // of this load, replace it with computation that does:
397 // ((magic_cst >> i) & 1) != 0
398 {
399 Type *Ty = nullptr;
400
401 // Look for an appropriate type:
402 // - The type of Idx if the magic fits
403 // - The smallest fitting legal type
404 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
405 Ty = Idx->getType();
406 else
407 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
408
409 if (Ty) {
410 Idx = MaskIdx(Idx);
411 Value *V = Builder.CreateIntCast(Idx, Ty, false);
412 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
413 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V);
414 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
415 }
416 }
417
418 return nullptr;
419}
420
421/// Returns true if we can rewrite Start as a GEP with pointer Base
422/// and some integer offset. The nodes that need to be re-written
423/// for this transformation will be added to Explored.
425 const DataLayout &DL,
426 SetVector<Value *> &Explored) {
427 SmallVector<Value *, 16> WorkList(1, Start);
428 Explored.insert(Base);
429
430 // The following traversal gives us an order which can be used
431 // when doing the final transformation. Since in the final
432 // transformation we create the PHI replacement instructions first,
433 // we don't have to get them in any particular order.
434 //
435 // However, for other instructions we will have to traverse the
436 // operands of an instruction first, which means that we have to
437 // do a post-order traversal.
438 while (!WorkList.empty()) {
440
441 while (!WorkList.empty()) {
442 if (Explored.size() >= 100)
443 return false;
444
445 Value *V = WorkList.back();
446
447 if (Explored.contains(V)) {
448 WorkList.pop_back();
449 continue;
450 }
451
452 if (!isa<GetElementPtrInst>(V) && !isa<PHINode>(V))
453 // We've found some value that we can't explore which is different from
454 // the base. Therefore we can't do this transformation.
455 return false;
456
457 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
458 // Only allow inbounds GEPs with at most one variable offset.
459 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); };
460 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1)
461 return false;
462
463 NW = NW.intersectForOffsetAdd(GEP->getNoWrapFlags());
464 if (!Explored.contains(GEP->getOperand(0)))
465 WorkList.push_back(GEP->getOperand(0));
466 }
467
468 if (WorkList.back() == V) {
469 WorkList.pop_back();
470 // We've finished visiting this node, mark it as such.
471 Explored.insert(V);
472 }
473
474 if (auto *PN = dyn_cast<PHINode>(V)) {
475 // We cannot transform PHIs on unsplittable basic blocks.
476 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
477 return false;
478 Explored.insert(PN);
479 PHIs.insert(PN);
480 }
481 }
482
483 // Explore the PHI nodes further.
484 for (auto *PN : PHIs)
485 for (Value *Op : PN->incoming_values())
486 if (!Explored.contains(Op))
487 WorkList.push_back(Op);
488 }
489
490 // Make sure that we can do this. Since we can't insert GEPs in a basic
491 // block before a PHI node, we can't easily do this transformation if
492 // we have PHI node users of transformed instructions.
493 for (Value *Val : Explored) {
494 for (Value *Use : Val->uses()) {
495
496 auto *PHI = dyn_cast<PHINode>(Use);
497 auto *Inst = dyn_cast<Instruction>(Val);
498
499 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
500 !Explored.contains(PHI))
501 continue;
502
503 if (PHI->getParent() == Inst->getParent())
504 return false;
505 }
506 }
507 return true;
508}
509
510// Sets the appropriate insert point on Builder where we can add
511// a replacement Instruction for V (if that is possible).
512static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
513 bool Before = true) {
514 if (auto *PHI = dyn_cast<PHINode>(V)) {
515 BasicBlock *Parent = PHI->getParent();
516 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
517 return;
518 }
519 if (auto *I = dyn_cast<Instruction>(V)) {
520 if (!Before)
521 I = &*std::next(I->getIterator());
522 Builder.SetInsertPoint(I);
523 return;
524 }
525 if (auto *A = dyn_cast<Argument>(V)) {
526 // Set the insertion point in the entry block.
527 BasicBlock &Entry = A->getParent()->getEntryBlock();
528 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt());
529 return;
530 }
531 // Otherwise, this is a constant and we don't need to set a new
532 // insertion point.
533 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
534}
535
536/// Returns a re-written value of Start as an indexed GEP using Base as a
537/// pointer.
539 const DataLayout &DL,
540 SetVector<Value *> &Explored,
541 InstCombiner &IC) {
542 // Perform all the substitutions. This is a bit tricky because we can
543 // have cycles in our use-def chains.
544 // 1. Create the PHI nodes without any incoming values.
545 // 2. Create all the other values.
546 // 3. Add the edges for the PHI nodes.
547 // 4. Emit GEPs to get the original pointers.
548 // 5. Remove the original instructions.
549 Type *IndexType = IntegerType::get(
550 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType()));
551
553 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
554
555 // Create the new PHI nodes, without adding any incoming values.
556 for (Value *Val : Explored) {
557 if (Val == Base)
558 continue;
559 // Create empty phi nodes. This avoids cyclic dependencies when creating
560 // the remaining instructions.
561 if (auto *PHI = dyn_cast<PHINode>(Val))
562 NewInsts[PHI] =
563 PHINode::Create(IndexType, PHI->getNumIncomingValues(),
564 PHI->getName() + ".idx", PHI->getIterator());
565 }
566 IRBuilder<> Builder(Base->getContext());
567
568 // Create all the other instructions.
569 for (Value *Val : Explored) {
570 if (NewInsts.contains(Val))
571 continue;
572
573 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
574 setInsertionPoint(Builder, GEP);
575 Value *Op = NewInsts[GEP->getOperand(0)];
576 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);
577 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero())
578 NewInsts[GEP] = OffsetV;
579 else
580 NewInsts[GEP] = Builder.CreateAdd(
581 Op, OffsetV, GEP->getOperand(0)->getName() + ".add",
582 /*NUW=*/NW.hasNoUnsignedWrap(),
583 /*NSW=*/NW.hasNoUnsignedSignedWrap());
584 continue;
585 }
586 if (isa<PHINode>(Val))
587 continue;
588
589 llvm_unreachable("Unexpected instruction type");
590 }
591
592 // Add the incoming values to the PHI nodes.
593 for (Value *Val : Explored) {
594 if (Val == Base)
595 continue;
596 // All the instructions have been created, we can now add edges to the
597 // phi nodes.
598 if (auto *PHI = dyn_cast<PHINode>(Val)) {
599 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
600 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
601 Value *NewIncoming = PHI->getIncomingValue(I);
602
603 auto It = NewInsts.find(NewIncoming);
604 if (It != NewInsts.end())
605 NewIncoming = It->second;
606
607 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
608 }
609 }
610 }
611
612 for (Value *Val : Explored) {
613 if (Val == Base)
614 continue;
615
616 setInsertionPoint(Builder, Val, false);
617 // Create GEP for external users.
618 Value *NewVal = Builder.CreateGEP(Builder.getInt8Ty(), Base, NewInsts[Val],
619 Val->getName() + ".ptr", NW);
620 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal);
621 // Add old instruction to worklist for DCE. We don't directly remove it
622 // here because the original compare is one of the users.
623 IC.addToWorklist(cast<Instruction>(Val));
624 }
625
626 return NewInsts[Start];
627}
628
629/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
630/// We can look through PHIs, GEPs and casts in order to determine a common base
631/// between GEPLHS and RHS.
634 const DataLayout &DL,
635 InstCombiner &IC) {
636 // FIXME: Support vector of pointers.
637 if (GEPLHS->getType()->isVectorTy())
638 return nullptr;
639
640 if (!GEPLHS->hasAllConstantIndices())
641 return nullptr;
642
643 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
644 Value *PtrBase =
646 /*AllowNonInbounds*/ false);
647
648 // Bail if we looked through addrspacecast.
649 if (PtrBase->getType() != GEPLHS->getType())
650 return nullptr;
651
652 // The set of nodes that will take part in this transformation.
653 SetVector<Value *> Nodes;
654 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
655 if (!canRewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes))
656 return nullptr;
657
658 // We know we can re-write this as
659 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
660 // Since we've only looked through inbouds GEPs we know that we
661 // can't have overflow on either side. We can therefore re-write
662 // this as:
663 // OFFSET1 cmp OFFSET2
664 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes, IC);
665
666 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
667 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
668 // offset. Since Index is the offset of LHS to the base pointer, we will now
669 // compare the offsets instead of comparing the pointers.
671 IC.Builder.getInt(Offset), NewRHS);
672}
673
674/// Fold comparisons between a GEP instruction and something else. At this point
675/// we know that the GEP is on the LHS of the comparison.
678 // Don't transform signed compares of GEPs into index compares. Even if the
679 // GEP is inbounds, the final add of the base pointer can have signed overflow
680 // and would change the result of the icmp.
681 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
682 // the maximum signed value for the pointer type.
684 return nullptr;
685
686 // Look through bitcasts and addrspacecasts. We do not however want to remove
687 // 0 GEPs.
688 if (!isa<GetElementPtrInst>(RHS))
690
691 auto CanFold = [Cond](GEPNoWrapFlags NW) {
693 return true;
694
695 // Unsigned predicates can be folded if the GEPs have *any* nowrap flags.
697 return NW != GEPNoWrapFlags::none();
698 };
699
700 auto NewICmp = [Cond](GEPNoWrapFlags NW, Value *Op1, Value *Op2) {
701 if (!NW.hasNoUnsignedWrap()) {
702 // Convert signed to unsigned comparison.
703 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Op1, Op2);
704 }
705
706 auto *I = new ICmpInst(Cond, Op1, Op2);
707 I->setSameSign(NW.hasNoUnsignedSignedWrap());
708 return I;
709 };
710
711 Value *PtrBase = GEPLHS->getOperand(0);
712 if (PtrBase == RHS && CanFold(GEPLHS->getNoWrapFlags())) {
713 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
714 Value *Offset = EmitGEPOffset(GEPLHS);
715 return NewICmp(GEPLHS->getNoWrapFlags(), Offset,
716 Constant::getNullValue(Offset->getType()));
717 }
718
719 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
720 isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&
721 !NullPointerIsDefined(I.getFunction(),
723 // For most address spaces, an allocation can't be placed at null, but null
724 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
725 // the only valid inbounds address derived from null, is null itself.
726 // Thus, we have four cases to consider:
727 // 1) Base == nullptr, Offset == 0 -> inbounds, null
728 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
729 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
730 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
731 //
732 // (Note if we're indexing a type of size 0, that simply collapses into one
733 // of the buckets above.)
734 //
735 // In general, we're allowed to make values less poison (i.e. remove
736 // sources of full UB), so in this case, we just select between the two
737 // non-poison cases (1 and 4 above).
738 //
739 // For vectors, we apply the same reasoning on a per-lane basis.
740 auto *Base = GEPLHS->getPointerOperand();
741 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
742 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount();
744 }
745 return new ICmpInst(Cond, Base,
747 cast<Constant>(RHS), Base->getType()));
748 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
749 GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
750
751 // If the base pointers are different, but the indices are the same, just
752 // compare the base pointer.
753 if (PtrBase != GEPRHS->getOperand(0)) {
754 bool IndicesTheSame =
755 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
756 GEPLHS->getPointerOperand()->getType() ==
757 GEPRHS->getPointerOperand()->getType() &&
758 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
759 if (IndicesTheSame)
760 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
761 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
762 IndicesTheSame = false;
763 break;
764 }
765
766 // If all indices are the same, just compare the base pointers.
767 Type *BaseType = GEPLHS->getOperand(0)->getType();
768 if (IndicesTheSame &&
769 CmpInst::makeCmpResultType(BaseType) == I.getType() && CanFold(NW))
770 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
771
772 // If we're comparing GEPs with two base pointers that only differ in type
773 // and both GEPs have only constant indices or just one use, then fold
774 // the compare with the adjusted indices.
775 // FIXME: Support vector of pointers.
776 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
777 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
778 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
779 PtrBase->stripPointerCasts() ==
780 GEPRHS->getOperand(0)->stripPointerCasts() &&
781 !GEPLHS->getType()->isVectorTy()) {
782 Value *LOffset = EmitGEPOffset(GEPLHS);
783 Value *ROffset = EmitGEPOffset(GEPRHS);
784
785 // If we looked through an addrspacecast between different sized address
786 // spaces, the LHS and RHS pointers are different sized
787 // integers. Truncate to the smaller one.
788 Type *LHSIndexTy = LOffset->getType();
789 Type *RHSIndexTy = ROffset->getType();
790 if (LHSIndexTy != RHSIndexTy) {
791 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <
792 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {
793 ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy);
794 } else
795 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy);
796 }
797
799 LOffset, ROffset);
800 return replaceInstUsesWith(I, Cmp);
801 }
802
803 // Otherwise, the base pointers are different and the indices are
804 // different. Try convert this to an indexed compare by looking through
805 // PHIs/casts.
806 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
807 }
808
809 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
810 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
811 // If the GEPs only differ by one index, compare it.
812 unsigned NumDifferences = 0; // Keep track of # differences.
813 unsigned DiffOperand = 0; // The operand that differs.
814 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
815 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
816 Type *LHSType = GEPLHS->getOperand(i)->getType();
817 Type *RHSType = GEPRHS->getOperand(i)->getType();
818 // FIXME: Better support for vector of pointers.
819 if (LHSType->getPrimitiveSizeInBits() !=
820 RHSType->getPrimitiveSizeInBits() ||
821 (GEPLHS->getType()->isVectorTy() &&
822 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
823 // Irreconcilable differences.
824 NumDifferences = 2;
825 break;
826 }
827
828 if (NumDifferences++)
829 break;
830 DiffOperand = i;
831 }
832
833 if (NumDifferences == 0) // SAME GEP?
834 return replaceInstUsesWith(
835 I, // No comparison is needed here.
836 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));
837 // If two GEPs only differ by an index, compare them.
838 // Note that nowrap flags are always needed when comparing two indices.
839 else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {
840 Value *LHSV = GEPLHS->getOperand(DiffOperand);
841 Value *RHSV = GEPRHS->getOperand(DiffOperand);
842 return NewICmp(NW, LHSV, RHSV);
843 }
844 }
845
846 if (CanFold(NW)) {
847 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
848 Value *L = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true);
849 Value *R = EmitGEPOffset(GEPRHS, /*RewriteGEP=*/true);
850 return NewICmp(NW, L, R);
851 }
852 }
853
854 // Try convert this to an indexed compare by looking through PHIs/casts as a
855 // last resort.
856 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
857}
858
860 // It would be tempting to fold away comparisons between allocas and any
861 // pointer not based on that alloca (e.g. an argument). However, even
862 // though such pointers cannot alias, they can still compare equal.
863 //
864 // But LLVM doesn't specify where allocas get their memory, so if the alloca
865 // doesn't escape we can argue that it's impossible to guess its value, and we
866 // can therefore act as if any such guesses are wrong.
867 //
868 // However, we need to ensure that this folding is consistent: We can't fold
869 // one comparison to false, and then leave a different comparison against the
870 // same value alone (as it might evaluate to true at runtime, leading to a
871 // contradiction). As such, this code ensures that all comparisons are folded
872 // at the same time, and there are no other escapes.
873
874 struct CmpCaptureTracker : public CaptureTracker {
875 AllocaInst *Alloca;
876 bool Captured = false;
877 /// The value of the map is a bit mask of which icmp operands the alloca is
878 /// used in.
880
881 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
882
883 void tooManyUses() override { Captured = true; }
884
885 bool captured(const Use *U) override {
886 auto *ICmp = dyn_cast<ICmpInst>(U->getUser());
887 // We need to check that U is based *only* on the alloca, and doesn't
888 // have other contributions from a select/phi operand.
889 // TODO: We could check whether getUnderlyingObjects() reduces to one
890 // object, which would allow looking through phi nodes.
891 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) {
892 // Collect equality icmps of the alloca, and don't treat them as
893 // captures.
894 ICmps[ICmp] |= 1u << U->getOperandNo();
895 return false;
896 }
897
898 Captured = true;
899 return true;
900 }
901 };
902
903 CmpCaptureTracker Tracker(Alloca);
904 PointerMayBeCaptured(Alloca, &Tracker);
905 if (Tracker.Captured)
906 return false;
907
908 bool Changed = false;
909 for (auto [ICmp, Operands] : Tracker.ICmps) {
910 switch (Operands) {
911 case 1:
912 case 2: {
913 // The alloca is only used in one icmp operand. Assume that the
914 // equality is false.
915 auto *Res = ConstantInt::get(ICmp->getType(),
916 ICmp->getPredicate() == ICmpInst::ICMP_NE);
917 replaceInstUsesWith(*ICmp, Res);
919 Changed = true;
920 break;
921 }
922 case 3:
923 // Both icmp operands are based on the alloca, so this is comparing
924 // pointer offsets, without leaking any information about the address
925 // of the alloca. Ignore such comparisons.
926 break;
927 default:
928 llvm_unreachable("Cannot happen");
929 }
930 }
931
932 return Changed;
933}
934
935/// Fold "icmp pred (X+C), X".
937 CmpPredicate Pred) {
938 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
939 // so the values can never be equal. Similarly for all other "or equals"
940 // operators.
941 assert(!!C && "C should not be zero!");
942
943 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
944 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
945 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
946 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
947 Constant *R =
948 ConstantInt::get(X->getType(), APInt::getMaxValue(C.getBitWidth()) - C);
949 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
950 }
951
952 // (X+1) >u X --> X <u (0-1) --> X != 255
953 // (X+2) >u X --> X <u (0-2) --> X <u 254
954 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
955 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
956 return new ICmpInst(ICmpInst::ICMP_ULT, X,
957 ConstantInt::get(X->getType(), -C));
958
959 APInt SMax = APInt::getSignedMaxValue(C.getBitWidth());
960
961 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
962 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
963 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
964 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
965 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
966 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
967 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
968 return new ICmpInst(ICmpInst::ICMP_SGT, X,
969 ConstantInt::get(X->getType(), SMax - C));
970
971 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
972 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
973 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
974 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
975 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
976 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
977
978 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
979 return new ICmpInst(ICmpInst::ICMP_SLT, X,
980 ConstantInt::get(X->getType(), SMax - (C - 1)));
981}
982
983/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
984/// (icmp eq/ne A, Log2(AP2/AP1)) ->
985/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
987 const APInt &AP1,
988 const APInt &AP2) {
989 assert(I.isEquality() && "Cannot fold icmp gt/lt");
990
991 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
992 if (I.getPredicate() == I.ICMP_NE)
993 Pred = CmpInst::getInversePredicate(Pred);
994 return new ICmpInst(Pred, LHS, RHS);
995 };
996
997 // Don't bother doing any work for cases which InstSimplify handles.
998 if (AP2.isZero())
999 return nullptr;
1000
1001 bool IsAShr = isa<AShrOperator>(I.getOperand(0));
1002 if (IsAShr) {
1003 if (AP2.isAllOnes())
1004 return nullptr;
1005 if (AP2.isNegative() != AP1.isNegative())
1006 return nullptr;
1007 if (AP2.sgt(AP1))
1008 return nullptr;
1009 }
1010
1011 if (!AP1)
1012 // 'A' must be large enough to shift out the highest set bit.
1013 return getICmp(I.ICMP_UGT, A,
1014 ConstantInt::get(A->getType(), AP2.logBase2()));
1015
1016 if (AP1 == AP2)
1017 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1018
1019 int Shift;
1020 if (IsAShr && AP1.isNegative())
1021 Shift = AP1.countl_one() - AP2.countl_one();
1022 else
1023 Shift = AP1.countl_zero() - AP2.countl_zero();
1024
1025 if (Shift > 0) {
1026 if (IsAShr && AP1 == AP2.ashr(Shift)) {
1027 // There are multiple solutions if we are comparing against -1 and the LHS
1028 // of the ashr is not a power of two.
1029 if (AP1.isAllOnes() && !AP2.isPowerOf2())
1030 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));
1031 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1032 } else if (AP1 == AP2.lshr(Shift)) {
1033 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1034 }
1035 }
1036
1037 // Shifting const2 will never be equal to const1.
1038 // FIXME: This should always be handled by InstSimplify?
1039 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1040 return replaceInstUsesWith(I, TorF);
1041}
1042
1043/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1044/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1046 const APInt &AP1,
1047 const APInt &AP2) {
1048 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1049
1050 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1051 if (I.getPredicate() == I.ICMP_NE)
1052 Pred = CmpInst::getInversePredicate(Pred);
1053 return new ICmpInst(Pred, LHS, RHS);
1054 };
1055
1056 // Don't bother doing any work for cases which InstSimplify handles.
1057 if (AP2.isZero())
1058 return nullptr;
1059
1060 unsigned AP2TrailingZeros = AP2.countr_zero();
1061
1062 if (!AP1 && AP2TrailingZeros != 0)
1063 return getICmp(
1064 I.ICMP_UGE, A,
1065 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
1066
1067 if (AP1 == AP2)
1068 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1069
1070 // Get the distance between the lowest bits that are set.
1071 int Shift = AP1.countr_zero() - AP2TrailingZeros;
1072
1073 if (Shift > 0 && AP2.shl(Shift) == AP1)
1074 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1075
1076 // Shifting const2 will never be equal to const1.
1077 // FIXME: This should always be handled by InstSimplify?
1078 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1079 return replaceInstUsesWith(I, TorF);
1080}
1081
1082/// The caller has matched a pattern of the form:
1083/// I = icmp ugt (add (add A, B), CI2), CI1
1084/// If this is of the form:
1085/// sum = a + b
1086/// if (sum+128 >u 255)
1087/// Then replace it with llvm.sadd.with.overflow.i8.
1088///
1090 ConstantInt *CI2, ConstantInt *CI1,
1091 InstCombinerImpl &IC) {
1092 // The transformation we're trying to do here is to transform this into an
1093 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1094 // with a narrower add, and discard the add-with-constant that is part of the
1095 // range check (if we can't eliminate it, this isn't profitable).
1096
1097 // In order to eliminate the add-with-constant, the compare can be its only
1098 // use.
1099 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1100 if (!AddWithCst->hasOneUse())
1101 return nullptr;
1102
1103 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1104 if (!CI2->getValue().isPowerOf2())
1105 return nullptr;
1106 unsigned NewWidth = CI2->getValue().countr_zero();
1107 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1108 return nullptr;
1109
1110 // The width of the new add formed is 1 more than the bias.
1111 ++NewWidth;
1112
1113 // Check to see that CI1 is an all-ones value with NewWidth bits.
1114 if (CI1->getBitWidth() == NewWidth ||
1115 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1116 return nullptr;
1117
1118 // This is only really a signed overflow check if the inputs have been
1119 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1120 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1121 if (IC.ComputeMaxSignificantBits(A, 0, &I) > NewWidth ||
1122 IC.ComputeMaxSignificantBits(B, 0, &I) > NewWidth)
1123 return nullptr;
1124
1125 // In order to replace the original add with a narrower
1126 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1127 // and truncates that discard the high bits of the add. Verify that this is
1128 // the case.
1129 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1130 for (User *U : OrigAdd->users()) {
1131 if (U == AddWithCst)
1132 continue;
1133
1134 // Only accept truncates for now. We would really like a nice recursive
1135 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1136 // chain to see which bits of a value are actually demanded. If the
1137 // original add had another add which was then immediately truncated, we
1138 // could still do the transformation.
1139 TruncInst *TI = dyn_cast<TruncInst>(U);
1140 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1141 return nullptr;
1142 }
1143
1144 // If the pattern matches, truncate the inputs to the narrower type and
1145 // use the sadd_with_overflow intrinsic to efficiently compute both the
1146 // result and the overflow bit.
1147 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1149 I.getModule(), Intrinsic::sadd_with_overflow, NewType);
1150
1151 InstCombiner::BuilderTy &Builder = IC.Builder;
1152
1153 // Put the new code above the original add, in case there are any uses of the
1154 // add between the add and the compare.
1155 Builder.SetInsertPoint(OrigAdd);
1156
1157 Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc");
1158 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc");
1159 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd");
1160 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result");
1161 Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType());
1162
1163 // The inner add was the result of the narrow add, zero extended to the
1164 // wider type. Replace it with the result computed by the intrinsic.
1165 IC.replaceInstUsesWith(*OrigAdd, ZExt);
1166 IC.eraseInstFromFunction(*OrigAdd);
1167
1168 // The original icmp gets replaced with the overflow value.
1169 return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1170}
1171
1172/// If we have:
1173/// icmp eq/ne (urem/srem %x, %y), 0
1174/// iff %y is a power-of-two, we can replace this with a bit test:
1175/// icmp eq/ne (and %x, (add %y, -1)), 0
1177 // This fold is only valid for equality predicates.
1178 if (!I.isEquality())
1179 return nullptr;
1180 CmpPredicate Pred;
1181 Value *X, *Y, *Zero;
1182 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))),
1183 m_CombineAnd(m_Zero(), m_Value(Zero)))))
1184 return nullptr;
1185 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, 0, &I))
1186 return nullptr;
1187 // This may increase instruction count, we don't enforce that Y is a constant.
1188 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType()));
1189 Value *Masked = Builder.CreateAnd(X, Mask);
1190 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, Zero);
1191}
1192
1193/// Fold equality-comparison between zero and any (maybe truncated) right-shift
1194/// by one-less-than-bitwidth into a sign test on the original value.
1196 Instruction *Val;
1197 CmpPredicate Pred;
1198 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero())))
1199 return nullptr;
1200
1201 Value *X;
1202 Type *XTy;
1203
1204 Constant *C;
1205 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) {
1206 XTy = X->getType();
1207 unsigned XBitWidth = XTy->getScalarSizeInBits();
1209 APInt(XBitWidth, XBitWidth - 1))))
1210 return nullptr;
1211 } else if (isa<BinaryOperator>(Val) &&
1213 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val),
1214 /*AnalyzeForSignBitExtraction=*/true))) {
1215 XTy = X->getType();
1216 } else
1217 return nullptr;
1218
1219 return ICmpInst::Create(Instruction::ICmp,
1223}
1224
1225// Handle icmp pred X, 0
1227 CmpInst::Predicate Pred = Cmp.getPredicate();
1228 if (!match(Cmp.getOperand(1), m_Zero()))
1229 return nullptr;
1230
1231 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1232 if (Pred == ICmpInst::ICMP_SGT) {
1233 Value *A, *B;
1234 if (match(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) {
1236 return new ICmpInst(Pred, B, Cmp.getOperand(1));
1238 return new ICmpInst(Pred, A, Cmp.getOperand(1));
1239 }
1240 }
1241
1243 return New;
1244
1245 // Given:
1246 // icmp eq/ne (urem %x, %y), 0
1247 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
1248 // icmp eq/ne %x, 0
1249 Value *X, *Y;
1250 if (match(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) &&
1251 ICmpInst::isEquality(Pred)) {
1252 KnownBits XKnown = computeKnownBits(X, 0, &Cmp);
1253 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp);
1254 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1255 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1256 }
1257
1258 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are
1259 // odd/non-zero/there is no overflow.
1260 if (match(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) &&
1261 ICmpInst::isEquality(Pred)) {
1262
1263 KnownBits XKnown = computeKnownBits(X, 0, &Cmp);
1264 // if X % 2 != 0
1265 // (icmp eq/ne Y)
1266 if (XKnown.countMaxTrailingZeros() == 0)
1267 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1268
1269 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp);
1270 // if Y % 2 != 0
1271 // (icmp eq/ne X)
1272 if (YKnown.countMaxTrailingZeros() == 0)
1273 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1274
1275 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0));
1276 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1277 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
1278 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`
1279 // but to avoid unnecessary work, first just if this is an obvious case.
1280
1281 // if X non-zero and NoOverflow(X * Y)
1282 // (icmp eq/ne Y)
1283 if (!XKnown.One.isZero() || isKnownNonZero(X, Q))
1284 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1285
1286 // if Y non-zero and NoOverflow(X * Y)
1287 // (icmp eq/ne X)
1288 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q))
1289 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1290 }
1291 // Note, we are skipping cases:
1292 // if Y % 2 != 0 AND X % 2 != 0
1293 // (false/true)
1294 // if X non-zero and Y non-zero and NoOverflow(X * Y)
1295 // (false/true)
1296 // Those can be simplified later as we would have already replaced the (icmp
1297 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
1298 // will fold to a constant elsewhere.
1299 }
1300 return nullptr;
1301}
1302
1303/// Fold icmp Pred X, C.
1304/// TODO: This code structure does not make sense. The saturating add fold
1305/// should be moved to some other helper and extended as noted below (it is also
1306/// possible that code has been made unnecessary - do we canonicalize IR to
1307/// overflow/saturating intrinsics or not?).
1309 // Match the following pattern, which is a common idiom when writing
1310 // overflow-safe integer arithmetic functions. The source performs an addition
1311 // in wider type and explicitly checks for overflow using comparisons against
1312 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1313 //
1314 // TODO: This could probably be generalized to handle other overflow-safe
1315 // operations if we worked out the formulas to compute the appropriate magic
1316 // constants.
1317 //
1318 // sum = a + b
1319 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1320 CmpInst::Predicate Pred = Cmp.getPredicate();
1321 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
1322 Value *A, *B;
1323 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1324 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) &&
1325 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1326 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
1327 return Res;
1328
1329 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1330 Constant *C = dyn_cast<Constant>(Op1);
1331 if (!C)
1332 return nullptr;
1333
1334 if (auto *Phi = dyn_cast<PHINode>(Op0))
1335 if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
1337 for (Value *V : Phi->incoming_values()) {
1338 Constant *Res =
1339 ConstantFoldCompareInstOperands(Pred, cast<Constant>(V), C, DL);
1340 if (!Res)
1341 return nullptr;
1342 Ops.push_back(Res);
1343 }
1345 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());
1346 for (auto [V, Pred] : zip(Ops, Phi->blocks()))
1347 NewPhi->addIncoming(V, Pred);
1348 return replaceInstUsesWith(Cmp, NewPhi);
1349 }
1350
1352 return R;
1353
1354 return nullptr;
1355}
1356
1357/// Canonicalize icmp instructions based on dominating conditions.
1359 // We already checked simple implication in InstSimplify, only handle complex
1360 // cases here.
1361 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1);
1362 const APInt *C;
1363 if (!match(Y, m_APInt(C)))
1364 return nullptr;
1365
1366 CmpInst::Predicate Pred = Cmp.getPredicate();
1368
1369 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1370 const APInt *DomC) -> Instruction * {
1371 // We have 2 compares of a variable with constants. Calculate the constant
1372 // ranges of those compares to see if we can transform the 2nd compare:
1373 // DomBB:
1374 // DomCond = icmp DomPred X, DomC
1375 // br DomCond, CmpBB, FalseBB
1376 // CmpBB:
1377 // Cmp = icmp Pred X, C
1378 ConstantRange DominatingCR =
1379 ConstantRange::makeExactICmpRegion(DomPred, *DomC);
1380 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1381 ConstantRange Difference = DominatingCR.difference(CR);
1382 if (Intersection.isEmptySet())
1383 return replaceInstUsesWith(Cmp, Builder.getFalse());
1384 if (Difference.isEmptySet())
1385 return replaceInstUsesWith(Cmp, Builder.getTrue());
1386
1387 // Canonicalizing a sign bit comparison that gets used in a branch,
1388 // pessimizes codegen by generating branch on zero instruction instead
1389 // of a test and branch. So we avoid canonicalizing in such situations
1390 // because test and branch instruction has better branch displacement
1391 // than compare and branch instruction.
1392 bool UnusedBit;
1393 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit);
1394 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp)))
1395 return nullptr;
1396
1397 // Avoid an infinite loop with min/max canonicalization.
1398 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1399 if (Cmp.hasOneUse() &&
1400 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value())))
1401 return nullptr;
1402
1403 if (const APInt *EqC = Intersection.getSingleElement())
1404 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC));
1405 if (const APInt *NeC = Difference.getSingleElement())
1406 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC));
1407 return nullptr;
1408 };
1409
1410 for (BranchInst *BI : DC.conditionsFor(X)) {
1411 CmpPredicate DomPred;
1412 const APInt *DomC;
1413 if (!match(BI->getCondition(),
1414 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC))))
1415 continue;
1416
1417 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1418 if (DT.dominates(Edge0, Cmp.getParent())) {
1419 if (auto *V = handleDomCond(DomPred, DomC))
1420 return V;
1421 } else {
1422 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1423 if (DT.dominates(Edge1, Cmp.getParent()))
1424 if (auto *V =
1425 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC))
1426 return V;
1427 }
1428 }
1429
1430 return nullptr;
1431}
1432
1433/// Fold icmp (trunc X), C.
1435 TruncInst *Trunc,
1436 const APInt &C) {
1437 ICmpInst::Predicate Pred = Cmp.getPredicate();
1438 Value *X = Trunc->getOperand(0);
1439 Type *SrcTy = X->getType();
1440 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1441 SrcBits = SrcTy->getScalarSizeInBits();
1442
1443 // Match (icmp pred (trunc nuw/nsw X), C)
1444 // Which we can convert to (icmp pred X, (sext/zext C))
1445 if (shouldChangeType(Trunc->getType(), SrcTy)) {
1446 if (Trunc->hasNoSignedWrap())
1447 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
1448 if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1449 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
1450 }
1451
1452 if (C.isOne() && C.getBitWidth() > 1) {
1453 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1454 Value *V = nullptr;
1455 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
1456 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1457 ConstantInt::get(V->getType(), 1));
1458 }
1459
1460 // TODO: Handle any shifted constant by subtracting trailing zeros.
1461 // TODO: Handle non-equality predicates.
1462 Value *Y;
1463 if (Cmp.isEquality() && match(X, m_Shl(m_One(), m_Value(Y)))) {
1464 // (trunc (1 << Y) to iN) == 0 --> Y u>= N
1465 // (trunc (1 << Y) to iN) != 0 --> Y u< N
1466 if (C.isZero()) {
1467 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1468 return new ICmpInst(NewPred, Y, ConstantInt::get(SrcTy, DstBits));
1469 }
1470 // (trunc (1 << Y) to iN) == 2**C --> Y == C
1471 // (trunc (1 << Y) to iN) != 2**C --> Y != C
1472 if (C.isPowerOf2())
1473 return new ICmpInst(Pred, Y, ConstantInt::get(SrcTy, C.logBase2()));
1474 }
1475
1476 if (Cmp.isEquality() && Trunc->hasOneUse()) {
1477 // Canonicalize to a mask and wider compare if the wide type is suitable:
1478 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1479 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {
1480 Constant *Mask =
1481 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits));
1482 Value *And = Builder.CreateAnd(X, Mask);
1483 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits));
1484 return new ICmpInst(Pred, And, WideC);
1485 }
1486
1487 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1488 // of the high bits truncated out of x are known.
1489 KnownBits Known = computeKnownBits(X, 0, &Cmp);
1490
1491 // If all the high bits are known, we can do this xform.
1492 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1493 // Pull in the high bits from known-ones set.
1494 APInt NewRHS = C.zext(SrcBits);
1495 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
1496 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS));
1497 }
1498 }
1499
1500 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1501 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1502 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1503 Value *ShOp;
1504 const APInt *ShAmtC;
1505 bool TrueIfSigned;
1506 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
1507 match(X, m_Shr(m_Value(ShOp), m_APInt(ShAmtC))) &&
1508 DstBits == SrcBits - ShAmtC->getZExtValue()) {
1509 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1511 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1513 }
1514
1515 return nullptr;
1516}
1517
1518/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1519/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1522 const SimplifyQuery &Q) {
1523 Value *X, *Y;
1524 CmpPredicate Pred;
1525 bool YIsSExt = false;
1526 // Try to match icmp (trunc X), (trunc Y)
1527 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {
1528 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() &
1529 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind();
1530 if (Cmp.isSigned()) {
1531 // For signed comparisons, both truncs must be nsw.
1532 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1533 return nullptr;
1534 } else {
1535 // For unsigned and equality comparisons, either both must be nuw or
1536 // both must be nsw, we don't care which.
1537 if (!NoWrapFlags)
1538 return nullptr;
1539 }
1540
1541 if (X->getType() != Y->getType() &&
1542 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))
1543 return nullptr;
1544 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) &&
1545 isDesirableIntType(Y->getType()->getScalarSizeInBits())) {
1546 std::swap(X, Y);
1547 Pred = Cmp.getSwappedPredicate(Pred);
1548 }
1549 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1550 }
1551 // Try to match icmp (trunc nuw X), (zext Y)
1552 else if (!Cmp.isSigned() &&
1553 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),
1554 m_OneUse(m_ZExt(m_Value(Y)))))) {
1555 // Can fold trunc nuw + zext for unsigned and equality predicates.
1556 }
1557 // Try to match icmp (trunc nsw X), (sext Y)
1558 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),
1560 // Can fold trunc nsw + zext/sext for all predicates.
1561 YIsSExt =
1562 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));
1563 } else
1564 return nullptr;
1565
1566 Type *TruncTy = Cmp.getOperand(0)->getType();
1567 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1568
1569 // If this transform will end up changing from desirable types -> undesirable
1570 // types skip it.
1571 if (isDesirableIntType(TruncBits) &&
1572 !isDesirableIntType(X->getType()->getScalarSizeInBits()))
1573 return nullptr;
1574
1575 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);
1576 return new ICmpInst(Pred, X, NewY);
1577}
1578
1579/// Fold icmp (xor X, Y), C.
1582 const APInt &C) {
1583 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1584 return I;
1585
1586 Value *X = Xor->getOperand(0);
1587 Value *Y = Xor->getOperand(1);
1588 const APInt *XorC;
1589 if (!match(Y, m_APInt(XorC)))
1590 return nullptr;
1591
1592 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1593 // fold the xor.
1594 ICmpInst::Predicate Pred = Cmp.getPredicate();
1595 bool TrueIfSigned = false;
1596 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {
1597
1598 // If the sign bit of the XorCst is not set, there is no change to
1599 // the operation, just stop using the Xor.
1600 if (!XorC->isNegative())
1601 return replaceOperand(Cmp, 0, X);
1602
1603 // Emit the opposite comparison.
1604 if (TrueIfSigned)
1605 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1606 ConstantInt::getAllOnesValue(X->getType()));
1607 else
1608 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1609 ConstantInt::getNullValue(X->getType()));
1610 }
1611
1612 if (Xor->hasOneUse()) {
1613 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1614 if (!Cmp.isEquality() && XorC->isSignMask()) {
1615 Pred = Cmp.getFlippedSignednessPredicate();
1616 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1617 }
1618
1619 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1620 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1621 Pred = Cmp.getFlippedSignednessPredicate();
1622 Pred = Cmp.getSwappedPredicate(Pred);
1623 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1624 }
1625 }
1626
1627 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1628 if (Pred == ICmpInst::ICMP_UGT) {
1629 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1630 if (*XorC == ~C && (C + 1).isPowerOf2())
1631 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1632 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1633 if (*XorC == C && (C + 1).isPowerOf2())
1634 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1635 }
1636 if (Pred == ICmpInst::ICMP_ULT) {
1637 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1638 if (*XorC == -C && C.isPowerOf2())
1639 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1640 ConstantInt::get(X->getType(), ~C));
1641 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1642 if (*XorC == C && (-C).isPowerOf2())
1643 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1644 ConstantInt::get(X->getType(), ~C));
1645 }
1646 return nullptr;
1647}
1648
1649/// For power-of-2 C:
1650/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1651/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1654 const APInt &C) {
1655 CmpInst::Predicate Pred = Cmp.getPredicate();
1656 APInt PowerOf2;
1657 if (Pred == ICmpInst::ICMP_ULT)
1658 PowerOf2 = C;
1659 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1660 PowerOf2 = C + 1;
1661 else
1662 return nullptr;
1663 if (!PowerOf2.isPowerOf2())
1664 return nullptr;
1665 Value *X;
1666 const APInt *ShiftC;
1668 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))
1669 return nullptr;
1670 uint64_t Shift = ShiftC->getLimitedValue();
1671 Type *XType = X->getType();
1672 if (Shift == 0 || PowerOf2.isMinSignedValue())
1673 return nullptr;
1674 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));
1675 APInt Bound =
1676 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1677 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));
1678}
1679
1680/// Fold icmp (and (sh X, Y), C2), C1.
1683 const APInt &C1,
1684 const APInt &C2) {
1685 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1686 if (!Shift || !Shift->isShift())
1687 return nullptr;
1688
1689 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1690 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1691 // code produced by the clang front-end, for bitfield access.
1692 // This seemingly simple opportunity to fold away a shift turns out to be
1693 // rather complicated. See PR17827 for details.
1694 unsigned ShiftOpcode = Shift->getOpcode();
1695 bool IsShl = ShiftOpcode == Instruction::Shl;
1696 const APInt *C3;
1697 if (match(Shift->getOperand(1), m_APInt(C3))) {
1698 APInt NewAndCst, NewCmpCst;
1699 bool AnyCmpCstBitsShiftedOut;
1700 if (ShiftOpcode == Instruction::Shl) {
1701 // For a left shift, we can fold if the comparison is not signed. We can
1702 // also fold a signed comparison if the mask value and comparison value
1703 // are not negative. These constraints may not be obvious, but we can
1704 // prove that they are correct using an SMT solver.
1705 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1706 return nullptr;
1707
1708 NewCmpCst = C1.lshr(*C3);
1709 NewAndCst = C2.lshr(*C3);
1710 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;
1711 } else if (ShiftOpcode == Instruction::LShr) {
1712 // For a logical right shift, we can fold if the comparison is not signed.
1713 // We can also fold a signed comparison if the shifted mask value and the
1714 // shifted comparison value are not negative. These constraints may not be
1715 // obvious, but we can prove that they are correct using an SMT solver.
1716 NewCmpCst = C1.shl(*C3);
1717 NewAndCst = C2.shl(*C3);
1718 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;
1719 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1720 return nullptr;
1721 } else {
1722 // For an arithmetic shift, check that both constants don't use (in a
1723 // signed sense) the top bits being shifted out.
1724 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1725 NewCmpCst = C1.shl(*C3);
1726 NewAndCst = C2.shl(*C3);
1727 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;
1728 if (NewAndCst.ashr(*C3) != C2)
1729 return nullptr;
1730 }
1731
1732 if (AnyCmpCstBitsShiftedOut) {
1733 // If we shifted bits out, the fold is not going to work out. As a
1734 // special case, check to see if this means that the result is always
1735 // true or false now.
1736 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1737 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1738 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1739 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1740 } else {
1741 Value *NewAnd = Builder.CreateAnd(
1742 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));
1743 return new ICmpInst(Cmp.getPredicate(), NewAnd,
1744 ConstantInt::get(And->getType(), NewCmpCst));
1745 }
1746 }
1747
1748 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1749 // preferable because it allows the C2 << Y expression to be hoisted out of a
1750 // loop if Y is invariant and X is not.
1751 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1752 !Shift->isArithmeticShift() &&
1753 ((!IsShl && C2.isOne()) || !isa<Constant>(Shift->getOperand(0)))) {
1754 // Compute C2 << Y.
1755 Value *NewShift =
1756 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))
1757 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));
1758
1759 // Compute X & (C2 << Y).
1760 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1761 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1762 }
1763
1764 return nullptr;
1765}
1766
1767/// Fold icmp (and X, C2), C1.
1770 const APInt &C1) {
1771 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1772
1773 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
1774 // TODO: We canonicalize to the longer form for scalars because we have
1775 // better analysis/folds for icmp, and codegen may be better with icmp.
1776 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&
1777 match(And->getOperand(1), m_One()))
1778 return new TruncInst(And->getOperand(0), Cmp.getType());
1779
1780 const APInt *C2;
1781 Value *X;
1782 if (!match(And, m_And(m_Value(X), m_APInt(C2))))
1783 return nullptr;
1784
1785 // (and X, highmask) s> [0, ~highmask] --> X s> ~highmask
1786 if (Cmp.getPredicate() == ICmpInst::ICMP_SGT && C1.ule(~*C2) &&
1787 C2->isNegatedPowerOf2())
1788 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1789 ConstantInt::get(X->getType(), ~*C2));
1790 // (and X, highmask) s< [1, -highmask] --> X s< -highmask
1791 if (Cmp.getPredicate() == ICmpInst::ICMP_SLT && !C1.isSignMask() &&
1792 (C1 - 1).ule(~*C2) && C2->isNegatedPowerOf2() && !C2->isSignMask())
1793 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1794 ConstantInt::get(X->getType(), -*C2));
1795
1796 // Don't perform the following transforms if the AND has multiple uses
1797 if (!And->hasOneUse())
1798 return nullptr;
1799
1800 if (Cmp.isEquality() && C1.isZero()) {
1801 // Restrict this fold to single-use 'and' (PR10267).
1802 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1803 if (C2->isSignMask()) {
1804 Constant *Zero = Constant::getNullValue(X->getType());
1805 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1806 return new ICmpInst(NewPred, X, Zero);
1807 }
1808
1809 APInt NewC2 = *C2;
1810 KnownBits Know = computeKnownBits(And->getOperand(0), 0, And);
1811 // Set high zeros of C2 to allow matching negated power-of-2.
1812 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
1813 Know.countMinLeadingZeros());
1814
1815 // Restrict this fold only for single-use 'and' (PR10267).
1816 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1817 if (NewC2.isNegatedPowerOf2()) {
1818 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);
1819 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1820 return new ICmpInst(NewPred, X, NegBOC);
1821 }
1822 }
1823
1824 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1825 // the input width without changing the value produced, eliminate the cast:
1826 //
1827 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1828 //
1829 // We can do this transformation if the constants do not have their sign bits
1830 // set or if it is an equality comparison. Extending a relational comparison
1831 // when we're checking the sign bit would not work.
1832 Value *W;
1833 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&
1834 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1835 // TODO: Is this a good transform for vectors? Wider types may reduce
1836 // throughput. Should this transform be limited (even for scalars) by using
1837 // shouldChangeType()?
1838 if (!Cmp.getType()->isVectorTy()) {
1839 Type *WideType = W->getType();
1840 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1841 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));
1842 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1843 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());
1844 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1845 }
1846 }
1847
1848 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))
1849 return I;
1850
1851 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1852 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1853 //
1854 // iff pred isn't signed
1855 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&
1856 match(And->getOperand(1), m_One())) {
1857 Constant *One = cast<Constant>(And->getOperand(1));
1858 Value *Or = And->getOperand(0);
1859 Value *A, *B, *LShr;
1860 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1861 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1862 unsigned UsesRemoved = 0;
1863 if (And->hasOneUse())
1864 ++UsesRemoved;
1865 if (Or->hasOneUse())
1866 ++UsesRemoved;
1867 if (LShr->hasOneUse())
1868 ++UsesRemoved;
1869
1870 // Compute A & ((1 << B) | 1)
1871 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;
1872 if (UsesRemoved >= RequireUsesRemoved) {
1873 Value *NewOr =
1874 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),
1875 /*HasNUW=*/true),
1876 One, Or->getName());
1877 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1878 return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
1879 }
1880 }
1881 }
1882
1883 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1884 // llvm.is.fpclass(X, fcInf|fcNan)
1885 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1886 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1887 // (icmp eq (and (bitcast X to int), ExponentMask), 0) -->
1888 // llvm.is.fpclass(X, fcSubnormal|fcZero)
1889 // (icmp ne (and (bitcast X to int), ExponentMask), 0) -->
1890 // llvm.is.fpclass(X, ~(fcSubnormal|fcZero))
1891 Value *V;
1892 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1893 Attribute::NoImplicitFloat) &&
1894 Cmp.isEquality() &&
1896 Type *FPType = V->getType()->getScalarType();
1897 if (FPType->isIEEELikeFPTy() && (C1.isZero() || C1 == *C2)) {
1898 APInt ExponentMask =
1899 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt();
1900 if (*C2 == ExponentMask) {
1901 unsigned Mask = C1.isZero()
1904 if (isICMP_NE)
1905 Mask = ~Mask & fcAllFlags;
1906 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));
1907 }
1908 }
1909 }
1910
1911 return nullptr;
1912}
1913
1914/// Fold icmp (and X, Y), C.
1917 const APInt &C) {
1918 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1919 return I;
1920
1921 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1922 bool TrueIfNeg;
1923 if (isSignBitCheck(Pred, C, TrueIfNeg)) {
1924 // ((X - 1) & ~X) < 0 --> X == 0
1925 // ((X - 1) & ~X) >= 0 --> X != 0
1926 Value *X;
1927 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&
1928 match(And->getOperand(1), m_Not(m_Specific(X)))) {
1929 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1930 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));
1931 }
1932 // (X & -X) < 0 --> X == MinSignedC
1933 // (X & -X) > -1 --> X != MinSignedC
1934 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {
1935 Constant *MinSignedC = ConstantInt::get(
1936 X->getType(),
1937 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
1938 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1939 return new ICmpInst(NewPred, X, MinSignedC);
1940 }
1941 }
1942
1943 // TODO: These all require that Y is constant too, so refactor with the above.
1944
1945 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1946 Value *X = And->getOperand(0);
1947 Value *Y = And->getOperand(1);
1948 if (auto *C2 = dyn_cast<ConstantInt>(Y))
1949 if (auto *LI = dyn_cast<LoadInst>(X))
1950 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1951 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1952 if (Instruction *Res =
1953 foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2))
1954 return Res;
1955
1956 if (!Cmp.isEquality())
1957 return nullptr;
1958
1959 // X & -C == -C -> X > u ~C
1960 // X & -C != -C -> X <= u ~C
1961 // iff C is a power of 2
1962 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {
1963 auto NewPred =
1965 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1966 }
1967
1968 // If we are testing the intersection of 2 select-of-nonzero-constants with no
1969 // common bits set, it's the same as checking if exactly one select condition
1970 // is set:
1971 // ((A ? TC : FC) & (B ? TC : FC)) == 0 --> xor A, B
1972 // ((A ? TC : FC) & (B ? TC : FC)) != 0 --> not(xor A, B)
1973 // TODO: Generalize for non-constant values.
1974 // TODO: Handle signed/unsigned predicates.
1975 // TODO: Handle other bitwise logic connectors.
1976 // TODO: Extend to handle a non-zero compare constant.
1977 if (C.isZero() && (Pred == CmpInst::ICMP_EQ || And->hasOneUse())) {
1978 assert(Cmp.isEquality() && "Not expecting non-equality predicates");
1979 Value *A, *B;
1980 const APInt *TC, *FC;
1981 if (match(X, m_Select(m_Value(A), m_APInt(TC), m_APInt(FC))) &&
1982 match(Y,
1983 m_Select(m_Value(B), m_SpecificInt(*TC), m_SpecificInt(*FC))) &&
1984 !TC->isZero() && !FC->isZero() && !TC->intersects(*FC)) {
1985 Value *R = Builder.CreateXor(A, B);
1986 if (Pred == CmpInst::ICMP_NE)
1987 R = Builder.CreateNot(R);
1988 return replaceInstUsesWith(Cmp, R);
1989 }
1990 }
1991
1992 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
1993 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1994 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1995 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
1997 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
1998 Value *TruncY = Builder.CreateTrunc(Y, X->getType());
1999 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
2000 Value *And = Builder.CreateAnd(TruncY, X);
2002 }
2003 return BinaryOperator::CreateAnd(TruncY, X);
2004 }
2005
2006 // (icmp eq/ne (and (shl -1, X), Y), 0)
2007 // -> (icmp eq/ne (lshr Y, X), 0)
2008 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
2009 // highly unlikely the non-zero case will ever show up in code.
2010 if (C.isZero() &&
2012 m_Value(Y))))) {
2013 Value *LShr = Builder.CreateLShr(Y, X);
2014 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));
2015 }
2016
2017 // (icmp eq/ne (and (add A, Addend), Msk), C)
2018 // -> (icmp eq/ne (and A, Msk), (and (sub C, Addend), Msk))
2019 {
2020 Value *A;
2021 const APInt *Addend, *Msk;
2022 if (match(And, m_And(m_OneUse(m_Add(m_Value(A), m_APInt(Addend))),
2023 m_LowBitMask(Msk))) &&
2024 C.ule(*Msk)) {
2025 APInt NewComperand = (C - *Addend) & *Msk;
2026 Value *MaskA = Builder.CreateAnd(A, ConstantInt::get(A->getType(), *Msk));
2027 return new ICmpInst(Pred, MaskA,
2028 ConstantInt::get(MaskA->getType(), NewComperand));
2029 }
2030 }
2031
2032 return nullptr;
2033}
2034
2035/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
2037 InstCombiner::BuilderTy &Builder) {
2038 // Are we using xors or subs to bitwise check for a pair or pairs of
2039 // (in)equalities? Convert to a shorter form that has more potential to be
2040 // folded even further.
2041 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
2042 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
2043 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
2044 // (X1 == X2) && (X3 == X4) && (X5 == X6)
2045 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
2046 // (X1 != X2) || (X3 != X4) || (X5 != X6)
2048 SmallVector<Value *, 16> WorkList(1, Or);
2049
2050 while (!WorkList.empty()) {
2051 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
2052 Value *Lhs, *Rhs;
2053
2054 if (match(OrOperatorArgument,
2055 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {
2056 CmpValues.emplace_back(Lhs, Rhs);
2057 return;
2058 }
2059
2060 if (match(OrOperatorArgument,
2061 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {
2062 CmpValues.emplace_back(Lhs, Rhs);
2063 return;
2064 }
2065
2066 WorkList.push_back(OrOperatorArgument);
2067 };
2068
2069 Value *CurrentValue = WorkList.pop_back_val();
2070 Value *OrOperatorLhs, *OrOperatorRhs;
2071
2072 if (!match(CurrentValue,
2073 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {
2074 return nullptr;
2075 }
2076
2077 MatchOrOperatorArgument(OrOperatorRhs);
2078 MatchOrOperatorArgument(OrOperatorLhs);
2079 }
2080
2081 ICmpInst::Predicate Pred = Cmp.getPredicate();
2082 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2083 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,
2084 CmpValues.rbegin()->second);
2085
2086 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2087 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);
2088 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);
2089 }
2090
2091 return LhsCmp;
2092}
2093
2094/// Fold icmp (or X, Y), C.
2097 const APInt &C) {
2098 ICmpInst::Predicate Pred = Cmp.getPredicate();
2099 if (C.isOne()) {
2100 // icmp slt signum(V) 1 --> icmp slt V, 1
2101 Value *V = nullptr;
2102 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
2103 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2104 ConstantInt::get(V->getType(), 1));
2105 }
2106
2107 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
2108
2109 // (icmp eq/ne (or disjoint x, C0), C1)
2110 // -> (icmp eq/ne x, C0^C1)
2111 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
2112 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {
2113 Value *NewC =
2114 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));
2115 return new ICmpInst(Pred, OrOp0, NewC);
2116 }
2117
2118 const APInt *MaskC;
2119 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {
2120 if (*MaskC == C && (C + 1).isPowerOf2()) {
2121 // X | C == C --> X <=u C
2122 // X | C != C --> X >u C
2123 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2125 return new ICmpInst(Pred, OrOp0, OrOp1);
2126 }
2127
2128 // More general: canonicalize 'equality with set bits mask' to
2129 // 'equality with clear bits mask'.
2130 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2131 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2132 if (Or->hasOneUse()) {
2133 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));
2134 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));
2135 return new ICmpInst(Pred, And, NewC);
2136 }
2137 }
2138
2139 // (X | (X-1)) s< 0 --> X s< 1
2140 // (X | (X-1)) s> -1 --> X s> 0
2141 Value *X;
2142 bool TrueIfSigned;
2143 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
2145 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2146 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
2147 return new ICmpInst(NewPred, X, NewC);
2148 }
2149
2150 const APInt *OrC;
2151 // icmp(X | OrC, C) --> icmp(X, 0)
2152 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {
2153 switch (Pred) {
2154 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2155 case ICmpInst::ICMP_SLT:
2156 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2157 case ICmpInst::ICMP_SGE:
2158 if (OrC->sge(C))
2159 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
2160 break;
2161 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2162 case ICmpInst::ICMP_SLE:
2163 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2164 case ICmpInst::ICMP_SGT:
2165 if (OrC->sgt(C))
2167 ConstantInt::getNullValue(X->getType()));
2168 break;
2169 default:
2170 break;
2171 }
2172 }
2173
2174 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2175 return nullptr;
2176
2177 Value *P, *Q;
2179 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2180 // -> and (icmp eq P, null), (icmp eq Q, null).
2181 Value *CmpP =
2182 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
2183 Value *CmpQ =
2185 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2186 return BinaryOperator::Create(BOpc, CmpP, CmpQ);
2187 }
2188
2189 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2190 return replaceInstUsesWith(Cmp, V);
2191
2192 return nullptr;
2193}
2194
2195/// Fold icmp (mul X, Y), C.
2198 const APInt &C) {
2199 ICmpInst::Predicate Pred = Cmp.getPredicate();
2200 Type *MulTy = Mul->getType();
2201 Value *X = Mul->getOperand(0);
2202
2203 // If there's no overflow:
2204 // X * X == 0 --> X == 0
2205 // X * X != 0 --> X != 0
2206 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
2207 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2208 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2209
2210 const APInt *MulC;
2211 if (!match(Mul->getOperand(1), m_APInt(MulC)))
2212 return nullptr;
2213
2214 // If this is a test of the sign bit and the multiply is sign-preserving with
2215 // a constant operand, use the multiply LHS operand instead:
2216 // (X * +MulC) < 0 --> X < 0
2217 // (X * -MulC) < 0 --> X > 0
2218 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2219 if (MulC->isNegative())
2220 Pred = ICmpInst::getSwappedPredicate(Pred);
2221 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2222 }
2223
2224 if (MulC->isZero())
2225 return nullptr;
2226
2227 // If the multiply does not wrap or the constant is odd, try to divide the
2228 // compare constant by the multiplication factor.
2229 if (Cmp.isEquality()) {
2230 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2231 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2232 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2233 return new ICmpInst(Pred, X, NewC);
2234 }
2235
2236 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2237 // correct to transform if MulC * N == C including overflow. I.e with i8
2238 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2239 // miss that case.
2240 if (C.urem(*MulC).isZero()) {
2241 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2242 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2243 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2244 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2245 return new ICmpInst(Pred, X, NewC);
2246 }
2247 }
2248 }
2249
2250 // With a matching no-overflow guarantee, fold the constants:
2251 // (X * MulC) < C --> X < (C / MulC)
2252 // (X * MulC) > C --> X > (C / MulC)
2253 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2254 Constant *NewC = nullptr;
2255 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2256 // MININT / -1 --> overflow.
2257 if (C.isMinSignedValue() && MulC->isAllOnes())
2258 return nullptr;
2259 if (MulC->isNegative())
2260 Pred = ICmpInst::getSwappedPredicate(Pred);
2261
2262 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2263 NewC = ConstantInt::get(
2265 } else {
2266 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2267 "Unexpected predicate");
2268 NewC = ConstantInt::get(
2270 }
2271 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2272 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2273 NewC = ConstantInt::get(
2275 } else {
2276 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2277 "Unexpected predicate");
2278 NewC = ConstantInt::get(
2280 }
2281 }
2282
2283 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2284}
2285
2286/// Fold icmp (shl nuw C2, Y), C.
2288 const APInt &C) {
2289 Value *Y;
2290 const APInt *C2;
2291 if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y))))
2292 return nullptr;
2293
2294 Type *ShiftType = Shl->getType();
2295 unsigned TypeBits = C.getBitWidth();
2296 ICmpInst::Predicate Pred = Cmp.getPredicate();
2297 if (Cmp.isUnsigned()) {
2298 if (C2->isZero() || C2->ugt(C))
2299 return nullptr;
2300 APInt Div, Rem;
2301 APInt::udivrem(C, *C2, Div, Rem);
2302 bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();
2303
2304 // (1 << Y) pred C -> Y pred Log2(C)
2305 if (!CIsPowerOf2) {
2306 // (1 << Y) < 30 -> Y <= 4
2307 // (1 << Y) <= 30 -> Y <= 4
2308 // (1 << Y) >= 30 -> Y > 4
2309 // (1 << Y) > 30 -> Y > 4
2310 if (Pred == ICmpInst::ICMP_ULT)
2311 Pred = ICmpInst::ICMP_ULE;
2312 else if (Pred == ICmpInst::ICMP_UGE)
2313 Pred = ICmpInst::ICMP_UGT;
2314 }
2315
2316 unsigned CLog2 = Div.logBase2();
2317 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2318 } else if (Cmp.isSigned() && C2->isOne()) {
2319 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
2320 // (1 << Y) > 0 -> Y != 31
2321 // (1 << Y) > C -> Y != 31 if C is negative.
2322 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))
2323 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2324
2325 // (1 << Y) < 0 -> Y == 31
2326 // (1 << Y) < 1 -> Y == 31
2327 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2328 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2329 if (Pred == ICmpInst::ICMP_SLT && (C - 1).sle(0))
2330 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2331 }
2332
2333 return nullptr;
2334}
2335
2336/// Fold icmp (shl X, Y), C.
2338 BinaryOperator *Shl,
2339 const APInt &C) {
2340 const APInt *ShiftVal;
2341 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
2342 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);
2343
2344 ICmpInst::Predicate Pred = Cmp.getPredicate();
2345 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2346 // -> (icmp pred X, Csle0)
2347 //
2348 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2349 // so X's must be what is used.
2350 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2351 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2352
2353 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2354 // -> (icmp eq/ne X, 0)
2355 if (ICmpInst::isEquality(Pred) && C.isZero() &&
2356 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2357 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2358
2359 // (icmp slt (shl nsw X, Y), 0/1)
2360 // -> (icmp slt X, 0/1)
2361 // (icmp sgt (shl nsw X, Y), 0/-1)
2362 // -> (icmp sgt X, 0/-1)
2363 //
2364 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2365 if (Shl->hasNoSignedWrap() &&
2366 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2367 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2368 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2369
2370 const APInt *ShiftAmt;
2371 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2372 return foldICmpShlLHSC(Cmp, Shl, C);
2373
2374 // Check that the shift amount is in range. If not, don't perform undefined
2375 // shifts. When the shift is visited, it will be simplified.
2376 unsigned TypeBits = C.getBitWidth();
2377 if (ShiftAmt->uge(TypeBits))
2378 return nullptr;
2379
2380 Value *X = Shl->getOperand(0);
2381 Type *ShType = Shl->getType();
2382
2383 // NSW guarantees that we are only shifting out sign bits from the high bits,
2384 // so we can ASHR the compare constant without needing a mask and eliminate
2385 // the shift.
2386 if (Shl->hasNoSignedWrap()) {
2387 if (Pred == ICmpInst::ICMP_SGT) {
2388 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2389 APInt ShiftedC = C.ashr(*ShiftAmt);
2390 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2391 }
2392 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2393 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {
2394 APInt ShiftedC = C.ashr(*ShiftAmt);
2395 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2396 }
2397 if (Pred == ICmpInst::ICMP_SLT) {
2398 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2399 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2400 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2401 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2402 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2403 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;
2404 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2405 }
2406 }
2407
2408 // NUW guarantees that we are only shifting out zero bits from the high bits,
2409 // so we can LSHR the compare constant without needing a mask and eliminate
2410 // the shift.
2411 if (Shl->hasNoUnsignedWrap()) {
2412 if (Pred == ICmpInst::ICMP_UGT) {
2413 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2414 APInt ShiftedC = C.lshr(*ShiftAmt);
2415 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2416 }
2417 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2418 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {
2419 APInt ShiftedC = C.lshr(*ShiftAmt);
2420 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2421 }
2422 if (Pred == ICmpInst::ICMP_ULT) {
2423 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2424 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2425 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2426 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2427 assert(C.ugt(0) && "ult 0 should have been eliminated");
2428 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;
2429 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2430 }
2431 }
2432
2433 if (Cmp.isEquality() && Shl->hasOneUse()) {
2434 // Strength-reduce the shift into an 'and'.
2435 Constant *Mask = ConstantInt::get(
2436 ShType,
2437 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
2438 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2439 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));
2440 return new ICmpInst(Pred, And, LShrC);
2441 }
2442
2443 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2444 bool TrueIfSigned = false;
2445 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {
2446 // (X << 31) <s 0 --> (X & 1) != 0
2447 Constant *Mask = ConstantInt::get(
2448 ShType,
2449 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
2450 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2451 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2452 And, Constant::getNullValue(ShType));
2453 }
2454
2455 // Simplify 'shl' inequality test into 'and' equality test.
2456 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2457 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2458 if ((C + 1).isPowerOf2() &&
2459 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2460 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));
2461 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2463 And, Constant::getNullValue(ShType));
2464 }
2465 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2466 if (C.isPowerOf2() &&
2467 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2468 Value *And =
2469 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));
2470 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2472 And, Constant::getNullValue(ShType));
2473 }
2474 }
2475
2476 // Transform (icmp pred iM (shl iM %v, N), C)
2477 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2478 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2479 // This enables us to get rid of the shift in favor of a trunc that may be
2480 // free on the target. It has the additional benefit of comparing to a
2481 // smaller constant that may be more target-friendly.
2482 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
2483 if (Shl->hasOneUse() && Amt != 0 &&
2484 shouldChangeType(ShType->getScalarSizeInBits(), TypeBits - Amt)) {
2485 ICmpInst::Predicate CmpPred = Pred;
2486 APInt RHSC = C;
2487
2488 if (RHSC.countr_zero() < Amt && ICmpInst::isStrictPredicate(CmpPred)) {
2489 // Try the flipped strictness predicate.
2490 // e.g.:
2491 // icmp ult i64 (shl X, 32), 8589934593 ->
2492 // icmp ule i64 (shl X, 32), 8589934592 ->
2493 // icmp ule i32 (trunc X, i32), 2 ->
2494 // icmp ult i32 (trunc X, i32), 3
2495 if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(
2496 Pred, ConstantInt::get(ShType->getContext(), C))) {
2497 CmpPred = FlippedStrictness->first;
2498 RHSC = cast<ConstantInt>(FlippedStrictness->second)->getValue();
2499 }
2500 }
2501
2502 if (RHSC.countr_zero() >= Amt) {
2503 Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt);
2504 Constant *NewC =
2505 ConstantInt::get(TruncTy, RHSC.ashr(*ShiftAmt).trunc(TypeBits - Amt));
2506 return new ICmpInst(CmpPred,
2507 Builder.CreateTrunc(X, TruncTy, "", /*IsNUW=*/false,
2508 Shl->hasNoSignedWrap()),
2509 NewC);
2510 }
2511 }
2512
2513 return nullptr;
2514}
2515
2516/// Fold icmp ({al}shr X, Y), C.
2518 BinaryOperator *Shr,
2519 const APInt &C) {
2520 // An exact shr only shifts out zero bits, so:
2521 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2522 Value *X = Shr->getOperand(0);
2523 CmpInst::Predicate Pred = Cmp.getPredicate();
2524 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2525 return new ICmpInst(Pred, X, Cmp.getOperand(1));
2526
2527 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2528 const APInt *ShiftValC;
2529 if (match(X, m_APInt(ShiftValC))) {
2530 if (Cmp.isEquality())
2531 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
2532
2533 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2534 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2535 bool TrueIfSigned;
2536 if (!IsAShr && ShiftValC->isNegative() &&
2537 isSignBitCheck(Pred, C, TrueIfSigned))
2538 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2539 Shr->getOperand(1),
2540 ConstantInt::getNullValue(X->getType()));
2541
2542 // If the shifted constant is a power-of-2, test the shift amount directly:
2543 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2544 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2545 if (!IsAShr && ShiftValC->isPowerOf2() &&
2546 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2547 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2548 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2549 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2550
2551 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2552 unsigned ShiftLZ = ShiftValC->countl_zero();
2553 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
2554 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2555 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
2556 }
2557 }
2558
2559 const APInt *ShiftAmtC;
2560 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))
2561 return nullptr;
2562
2563 // Check that the shift amount is in range. If not, don't perform undefined
2564 // shifts. When the shift is visited it will be simplified.
2565 unsigned TypeBits = C.getBitWidth();
2566 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);
2567 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2568 return nullptr;
2569
2570 bool IsExact = Shr->isExact();
2571 Type *ShrTy = Shr->getType();
2572 // TODO: If we could guarantee that InstSimplify would handle all of the
2573 // constant-value-based preconditions in the folds below, then we could assert
2574 // those conditions rather than checking them. This is difficult because of
2575 // undef/poison (PR34838).
2576 if (IsAShr && Shr->hasOneUse()) {
2577 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2578 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2579 // When C - 1 is a power of two and the transform can be legally
2580 // performed, prefer this form so the produced constant is close to a
2581 // power of two.
2582 // icmp slt/ult (ashr exact X, ShAmtC), C
2583 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2584 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
2585 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2586 }
2587 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2588 // When ShAmtC can be shifted losslessly:
2589 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2590 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2591 APInt ShiftedC = C.shl(ShAmtVal);
2592 if (ShiftedC.ashr(ShAmtVal) == C)
2593 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2594 }
2595 if (Pred == CmpInst::ICMP_SGT) {
2596 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2597 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2598 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&
2599 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
2600 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2601 }
2602 if (Pred == CmpInst::ICMP_UGT) {
2603 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2604 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2605 // clause accounts for that pattern.
2606 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2607 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
2608 (C + 1).shl(ShAmtVal).isMinSignedValue())
2609 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2610 }
2611
2612 // If the compare constant has significant bits above the lowest sign-bit,
2613 // then convert an unsigned cmp to a test of the sign-bit:
2614 // (ashr X, ShiftC) u> C --> X s< 0
2615 // (ashr X, ShiftC) u< C --> X s> -1
2616 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2617 if (Pred == CmpInst::ICMP_UGT) {
2618 return new ICmpInst(CmpInst::ICMP_SLT, X,
2620 }
2621 if (Pred == CmpInst::ICMP_ULT) {
2622 return new ICmpInst(CmpInst::ICMP_SGT, X,
2624 }
2625 }
2626 } else if (!IsAShr) {
2627 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2628 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2629 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2630 APInt ShiftedC = C.shl(ShAmtVal);
2631 if (ShiftedC.lshr(ShAmtVal) == C)
2632 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2633 }
2634 if (Pred == CmpInst::ICMP_UGT) {
2635 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2636 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2637 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))
2638 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2639 }
2640 }
2641
2642 if (!Cmp.isEquality())
2643 return nullptr;
2644
2645 // Handle equality comparisons of shift-by-constant.
2646
2647 // If the comparison constant changes with the shift, the comparison cannot
2648 // succeed (bits of the comparison constant cannot match the shifted value).
2649 // This should be known by InstSimplify and already be folded to true/false.
2650 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2651 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2652 "Expected icmp+shr simplify did not occur.");
2653
2654 // If the bits shifted out are known zero, compare the unshifted value:
2655 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2656 if (Shr->isExact())
2657 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));
2658
2659 if (C.isZero()) {
2660 // == 0 is u< 1.
2661 if (Pred == CmpInst::ICMP_EQ)
2662 return new ICmpInst(CmpInst::ICMP_ULT, X,
2663 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal)));
2664 else
2665 return new ICmpInst(CmpInst::ICMP_UGT, X,
2666 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal) - 1));
2667 }
2668
2669 if (Shr->hasOneUse()) {
2670 // Canonicalize the shift into an 'and':
2671 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2672 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2673 Constant *Mask = ConstantInt::get(ShrTy, Val);
2674 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");
2675 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));
2676 }
2677
2678 return nullptr;
2679}
2680
2682 BinaryOperator *SRem,
2683 const APInt &C) {
2684 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2685 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
2686 // Canonicalize unsigned predicates to signed:
2687 // (X s% DivisorC) u> C -> (X s% DivisorC) s< 0
2688 // iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1
2689 // (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1
2690 // iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1
2691
2692 const APInt *DivisorC;
2693 if (!match(SRem->getOperand(1), m_APInt(DivisorC)))
2694 return nullptr;
2695
2696 APInt NormalizedC = C;
2697 if (Pred == ICmpInst::ICMP_ULT) {
2698 assert(!NormalizedC.isZero() &&
2699 "ult X, 0 should have been simplified already.");
2700 --NormalizedC;
2701 }
2702 if (C.isNegative())
2703 NormalizedC.flipAllBits();
2704 assert(!DivisorC->isZero() &&
2705 "srem X, 0 should have been simplified already.");
2706 if (!NormalizedC.uge(DivisorC->abs() - 1))
2707 return nullptr;
2708
2709 Type *Ty = SRem->getType();
2710 if (Pred == ICmpInst::ICMP_UGT)
2711 return new ICmpInst(ICmpInst::ICMP_SLT, SRem,
2713 return new ICmpInst(ICmpInst::ICMP_SGT, SRem,
2715 }
2716 // Match an 'is positive' or 'is negative' comparison of remainder by a
2717 // constant power-of-2 value:
2718 // (X % pow2C) sgt/slt 0
2719 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2720 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2721 return nullptr;
2722
2723 // TODO: The one-use check is standard because we do not typically want to
2724 // create longer instruction sequences, but this might be a special-case
2725 // because srem is not good for analysis or codegen.
2726 if (!SRem->hasOneUse())
2727 return nullptr;
2728
2729 const APInt *DivisorC;
2730 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
2731 return nullptr;
2732
2733 // For cmp_sgt/cmp_slt only zero valued C is handled.
2734 // For cmp_eq/cmp_ne only positive valued C is handled.
2735 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2736 !C.isZero()) ||
2737 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2738 !C.isStrictlyPositive()))
2739 return nullptr;
2740
2741 // Mask off the sign bit and the modulo bits (low-bits).
2742 Type *Ty = SRem->getType();
2744 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
2745 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);
2746
2747 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2748 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));
2749
2750 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2751 // bit is set. Example:
2752 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2753 if (Pred == ICmpInst::ICMP_SGT)
2755
2756 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2757 // bit is set. Example:
2758 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2759 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
2760}
2761
2762/// Fold icmp (udiv X, Y), C.
2764 BinaryOperator *UDiv,
2765 const APInt &C) {
2766 ICmpInst::Predicate Pred = Cmp.getPredicate();
2767 Value *X = UDiv->getOperand(0);
2768 Value *Y = UDiv->getOperand(1);
2769 Type *Ty = UDiv->getType();
2770
2771 const APInt *C2;
2772 if (!match(X, m_APInt(C2)))
2773 return nullptr;
2774
2775 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2776
2777 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2778 if (Pred == ICmpInst::ICMP_UGT) {
2779 assert(!C.isMaxValue() &&
2780 "icmp ugt X, UINT_MAX should have been simplified already.");
2781 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2782 ConstantInt::get(Ty, C2->udiv(C + 1)));
2783 }
2784
2785 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2786 if (Pred == ICmpInst::ICMP_ULT) {
2787 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2788 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2789 ConstantInt::get(Ty, C2->udiv(C)));
2790 }
2791
2792 return nullptr;
2793}
2794
2795/// Fold icmp ({su}div X, Y), C.
2797 BinaryOperator *Div,
2798 const APInt &C) {
2799 ICmpInst::Predicate Pred = Cmp.getPredicate();
2800 Value *X = Div->getOperand(0);
2801 Value *Y = Div->getOperand(1);
2802 Type *Ty = Div->getType();
2803 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2804
2805 // If unsigned division and the compare constant is bigger than
2806 // UMAX/2 (negative), there's only one pair of values that satisfies an
2807 // equality check, so eliminate the division:
2808 // (X u/ Y) == C --> (X == C) && (Y == 1)
2809 // (X u/ Y) != C --> (X != C) || (Y != 1)
2810 // Similarly, if signed division and the compare constant is exactly SMIN:
2811 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2812 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2813 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2814 (!DivIsSigned || C.isMinSignedValue())) {
2815 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));
2816 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));
2817 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2818 return BinaryOperator::Create(Logic, XBig, YOne);
2819 }
2820
2821 // Fold: icmp pred ([us]div X, C2), C -> range test
2822 // Fold this div into the comparison, producing a range check.
2823 // Determine, based on the divide type, what the range is being
2824 // checked. If there is an overflow on the low or high side, remember
2825 // it, otherwise compute the range [low, hi) bounding the new value.
2826 // See: InsertRangeTest above for the kinds of replacements possible.
2827 const APInt *C2;
2828 if (!match(Y, m_APInt(C2)))
2829 return nullptr;
2830
2831 // FIXME: If the operand types don't match the type of the divide
2832 // then don't attempt this transform. The code below doesn't have the
2833 // logic to deal with a signed divide and an unsigned compare (and
2834 // vice versa). This is because (x /s C2) <s C produces different
2835 // results than (x /s C2) <u C or (x /u C2) <s C or even
2836 // (x /u C2) <u C. Simply casting the operands and result won't
2837 // work. :( The if statement below tests that condition and bails
2838 // if it finds it.
2839 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
2840 return nullptr;
2841
2842 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2843 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2844 // division-by-constant cases should be present, we can not assert that they
2845 // have happened before we reach this icmp instruction.
2846 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2847 return nullptr;
2848
2849 // Compute Prod = C * C2. We are essentially solving an equation of
2850 // form X / C2 = C. We solve for X by multiplying C2 and C.
2851 // By solving for X, we can turn this into a range check instead of computing
2852 // a divide.
2853 APInt Prod = C * *C2;
2854
2855 // Determine if the product overflows by seeing if the product is not equal to
2856 // the divide. Make sure we do the same kind of divide as in the LHS
2857 // instruction that we're folding.
2858 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;
2859
2860 // If the division is known to be exact, then there is no remainder from the
2861 // divide, so the covered range size is unit, otherwise it is the divisor.
2862 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2863
2864 // Figure out the interval that is being checked. For example, a comparison
2865 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2866 // Compute this interval based on the constants involved and the signedness of
2867 // the compare/divide. This computes a half-open interval, keeping track of
2868 // whether either value in the interval overflows. After analysis each
2869 // overflow variable is set to 0 if it's corresponding bound variable is valid
2870 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2871 int LoOverflow = 0, HiOverflow = 0;
2872 APInt LoBound, HiBound;
2873
2874 if (!DivIsSigned) { // udiv
2875 // e.g. X/5 op 3 --> [15, 20)
2876 LoBound = Prod;
2877 HiOverflow = LoOverflow = ProdOV;
2878 if (!HiOverflow) {
2879 // If this is not an exact divide, then many values in the range collapse
2880 // to the same result value.
2881 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2882 }
2883 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2884 if (C.isZero()) { // (X / pos) op 0
2885 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2886 LoBound = -(RangeSize - 1);
2887 HiBound = RangeSize;
2888 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2889 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2890 HiOverflow = LoOverflow = ProdOV;
2891 if (!HiOverflow)
2892 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2893 } else { // (X / pos) op neg
2894 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2895 HiBound = Prod + 1;
2896 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2897 if (!LoOverflow) {
2898 APInt DivNeg = -RangeSize;
2899 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2900 }
2901 }
2902 } else if (C2->isNegative()) { // Divisor is < 0.
2903 if (Div->isExact())
2904 RangeSize.negate();
2905 if (C.isZero()) { // (X / neg) op 0
2906 // e.g. X/-5 op 0 --> [-4, 5)
2907 LoBound = RangeSize + 1;
2908 HiBound = -RangeSize;
2909 if (HiBound == *C2) { // -INTMIN = INTMIN
2910 HiOverflow = 1; // [INTMIN+1, overflow)
2911 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2912 }
2913 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2914 // e.g. X/-5 op 3 --> [-19, -14)
2915 HiBound = Prod + 1;
2916 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2917 if (!LoOverflow)
2918 LoOverflow =
2919 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;
2920 } else { // (X / neg) op neg
2921 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2922 LoOverflow = HiOverflow = ProdOV;
2923 if (!HiOverflow)
2924 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2925 }
2926
2927 // Dividing by a negative swaps the condition. LT <-> GT
2928 Pred = ICmpInst::getSwappedPredicate(Pred);
2929 }
2930
2931 switch (Pred) {
2932 default:
2933 llvm_unreachable("Unhandled icmp predicate!");
2934 case ICmpInst::ICMP_EQ:
2935 if (LoOverflow && HiOverflow)
2936 return replaceInstUsesWith(Cmp, Builder.getFalse());
2937 if (HiOverflow)
2938 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2939 X, ConstantInt::get(Ty, LoBound));
2940 if (LoOverflow)
2941 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2942 X, ConstantInt::get(Ty, HiBound));
2943 return replaceInstUsesWith(
2944 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));
2945 case ICmpInst::ICMP_NE:
2946 if (LoOverflow && HiOverflow)
2947 return replaceInstUsesWith(Cmp, Builder.getTrue());
2948 if (HiOverflow)
2949 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2950 X, ConstantInt::get(Ty, LoBound));
2951 if (LoOverflow)
2952 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2953 X, ConstantInt::get(Ty, HiBound));
2954 return replaceInstUsesWith(
2955 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));
2956 case ICmpInst::ICMP_ULT:
2957 case ICmpInst::ICMP_SLT:
2958 if (LoOverflow == +1) // Low bound is greater than input range.
2959 return replaceInstUsesWith(Cmp, Builder.getTrue());
2960 if (LoOverflow == -1) // Low bound is less than input range.
2961 return replaceInstUsesWith(Cmp, Builder.getFalse());
2962 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));
2963 case ICmpInst::ICMP_UGT:
2964 case ICmpInst::ICMP_SGT:
2965 if (HiOverflow == +1) // High bound greater than input range.
2966 return replaceInstUsesWith(Cmp, Builder.getFalse());
2967 if (HiOverflow == -1) // High bound less than input range.
2968 return replaceInstUsesWith(Cmp, Builder.getTrue());
2969 if (Pred == ICmpInst::ICMP_UGT)
2970 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));
2971 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));
2972 }
2973
2974 return nullptr;
2975}
2976
2977/// Fold icmp (sub X, Y), C.
2979 BinaryOperator *Sub,
2980 const APInt &C) {
2981 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2982 ICmpInst::Predicate Pred = Cmp.getPredicate();
2983 Type *Ty = Sub->getType();
2984
2985 // (SubC - Y) == C) --> Y == (SubC - C)
2986 // (SubC - Y) != C) --> Y != (SubC - C)
2987 Constant *SubC;
2988 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {
2989 return new ICmpInst(Pred, Y,
2990 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));
2991 }
2992
2993 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
2994 const APInt *C2;
2995 APInt SubResult;
2996 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
2997 bool HasNSW = Sub->hasNoSignedWrap();
2998 bool HasNUW = Sub->hasNoUnsignedWrap();
2999 if (match(X, m_APInt(C2)) &&
3000 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
3001 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))
3002 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));
3003
3004 // X - Y == 0 --> X == Y.
3005 // X - Y != 0 --> X != Y.
3006 // TODO: We allow this with multiple uses as long as the other uses are not
3007 // in phis. The phi use check is guarding against a codegen regression
3008 // for a loop test. If the backend could undo this (and possibly
3009 // subsequent transforms), we would not need this hack.
3010 if (Cmp.isEquality() && C.isZero() &&
3011 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))
3012 return new ICmpInst(Pred, X, Y);
3013
3014 // The following transforms are only worth it if the only user of the subtract
3015 // is the icmp.
3016 // TODO: This is an artificial restriction for all of the transforms below
3017 // that only need a single replacement icmp. Can these use the phi test
3018 // like the transform above here?
3019 if (!Sub->hasOneUse())
3020 return nullptr;
3021
3022 if (Sub->hasNoSignedWrap()) {
3023 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
3024 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
3025 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
3026
3027 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
3028 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
3029 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3030
3031 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
3032 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
3033 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
3034
3035 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
3036 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3037 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3038 }
3039
3040 if (!match(X, m_APInt(C2)))
3041 return nullptr;
3042
3043 // C2 - Y <u C -> (Y | (C - 1)) == C2
3044 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
3045 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
3046 (*C2 & (C - 1)) == (C - 1))
3047 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);
3048
3049 // C2 - Y >u C -> (Y | C) != C2
3050 // iff C2 & C == C and C + 1 is a power of 2
3051 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
3052 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);
3053
3054 // We have handled special cases that reduce.
3055 // Canonicalize any remaining sub to add as:
3056 // (C2 - Y) > C --> (Y + ~C2) < ~C
3057 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",
3058 HasNUW, HasNSW);
3059 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
3060}
3061
3062static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
3063 Value *Op1, IRBuilderBase &Builder,
3064 bool HasOneUse) {
3065 auto FoldConstant = [&](bool Val) {
3066 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
3067 if (Op0->getType()->isVectorTy())
3069 cast<VectorType>(Op0->getType())->getElementCount(), Res);
3070 return Res;
3071 };
3072
3073 switch (Table.to_ulong()) {
3074 case 0: // 0 0 0 0
3075 return FoldConstant(false);
3076 case 1: // 0 0 0 1
3077 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
3078 case 2: // 0 0 1 0
3079 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
3080 case 3: // 0 0 1 1
3081 return Builder.CreateNot(Op0);
3082 case 4: // 0 1 0 0
3083 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
3084 case 5: // 0 1 0 1
3085 return Builder.CreateNot(Op1);
3086 case 6: // 0 1 1 0
3087 return Builder.CreateXor(Op0, Op1);
3088 case 7: // 0 1 1 1
3089 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
3090 case 8: // 1 0 0 0
3091 return Builder.CreateAnd(Op0, Op1);
3092 case 9: // 1 0 0 1
3093 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
3094 case 10: // 1 0 1 0
3095 return Op1;
3096 case 11: // 1 0 1 1
3097 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
3098 case 12: // 1 1 0 0
3099 return Op0;
3100 case 13: // 1 1 0 1
3101 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
3102 case 14: // 1 1 1 0
3103 return Builder.CreateOr(Op0, Op1);
3104 case 15: // 1 1 1 1
3105 return FoldConstant(true);
3106 default:
3107 llvm_unreachable("Invalid Operation");
3108 }
3109 return nullptr;
3110}
3111
3112/// Fold icmp (add X, Y), C.
3115 const APInt &C) {
3116 Value *Y = Add->getOperand(1);
3117 Value *X = Add->getOperand(0);
3118
3119 Value *Op0, *Op1;
3120 Instruction *Ext0, *Ext1;
3121 const CmpInst::Predicate Pred = Cmp.getPredicate();
3122 if (match(Add,
3125 m_ZExtOrSExt(m_Value(Op1))))) &&
3126 Op0->getType()->isIntOrIntVectorTy(1) &&
3127 Op1->getType()->isIntOrIntVectorTy(1)) {
3128 unsigned BW = C.getBitWidth();
3129 std::bitset<4> Table;
3130 auto ComputeTable = [&](bool Op0Val, bool Op1Val) {
3131 APInt Res(BW, 0);
3132 if (Op0Val)
3133 Res += APInt(BW, isa<ZExtInst>(Ext0) ? 1 : -1, /*isSigned=*/true);
3134 if (Op1Val)
3135 Res += APInt(BW, isa<ZExtInst>(Ext1) ? 1 : -1, /*isSigned=*/true);
3136 return ICmpInst::compare(Res, C, Pred);
3137 };
3138
3139 Table[0] = ComputeTable(false, false);
3140 Table[1] = ComputeTable(false, true);
3141 Table[2] = ComputeTable(true, false);
3142 Table[3] = ComputeTable(true, true);
3143 if (auto *Cond =
3144 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))
3145 return replaceInstUsesWith(Cmp, Cond);
3146 }
3147 const APInt *C2;
3148 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3149 return nullptr;
3150
3151 // Fold icmp pred (add X, C2), C.
3152 Type *Ty = Add->getType();
3153
3154 // If the add does not wrap, we can always adjust the compare by subtracting
3155 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3156 // are canonicalized to SGT/SLT/UGT/ULT.
3157 if ((Add->hasNoSignedWrap() &&
3158 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
3159 (Add->hasNoUnsignedWrap() &&
3160 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
3161 bool Overflow;
3162 APInt NewC =
3163 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);
3164 // If there is overflow, the result must be true or false.
3165 // TODO: Can we assert there is no overflow because InstSimplify always
3166 // handles those cases?
3167 if (!Overflow)
3168 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3169 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
3170 }
3171
3172 if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3173 C.isNonNegative() && (C - *C2).isNonNegative() &&
3174 computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())
3175 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
3176 ConstantInt::get(Ty, C - *C2));
3177
3178 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
3179 const APInt &Upper = CR.getUpper();
3180 const APInt &Lower = CR.getLower();
3181 if (Cmp.isSigned()) {
3182 if (Lower.isSignMask())
3183 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
3184 if (Upper.isSignMask())
3185 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
3186 } else {
3187 if (Lower.isMinValue())
3188 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
3189 if (Upper.isMinValue())
3190 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
3191 }
3192
3193 // This set of folds is intentionally placed after folds that use no-wrapping
3194 // flags because those folds are likely better for later analysis/codegen.
3197
3198 // Fold compare with offset to opposite sign compare if it eliminates offset:
3199 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3200 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3201 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));
3202
3203 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3204 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3205 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));
3206
3207 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3208 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3209 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));
3210
3211 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3212 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3213 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));
3214
3215 // (X + -1) <u C --> X <=u C (if X is never null)
3216 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3217 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3218 if (llvm::isKnownNonZero(X, Q))
3219 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));
3220 }
3221
3222 if (!Add->hasOneUse())
3223 return nullptr;
3224
3225 // X+C <u C2 -> (X & -C2) == C
3226 // iff C & (C2-1) == 0
3227 // C2 is a power of 2
3228 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3230 ConstantExpr::getNeg(cast<Constant>(Y)));
3231
3232 // X+C2 <u C -> (X & C) == 2C
3233 // iff C == -(C2)
3234 // C2 is a power of 2
3235 if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3237 ConstantInt::get(Ty, C * 2));
3238
3239 // X+C >u C2 -> (X & ~C2) != C
3240 // iff C & C2 == 0
3241 // C2+1 is a power of 2
3242 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3244 ConstantExpr::getNeg(cast<Constant>(Y)));
3245
3246 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3247 // to the ult form.
3248 // X+C2 >u C -> X+(C2-C-1) <u ~C
3249 if (Pred == ICmpInst::ICMP_UGT)
3250 return new ICmpInst(ICmpInst::ICMP_ULT,
3251 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
3252 ConstantInt::get(Ty, ~C));
3253
3254 // zext(V) + C2 pred C -> V + C3 pred' C4
3255 Value *V;
3256 if (match(X, m_ZExt(m_Value(V)))) {
3257 Type *NewCmpTy = V->getType();
3258 unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();
3259 if (shouldChangeType(Ty, NewCmpTy)) {
3260 if (CR.getActiveBits() <= NewCmpBW) {
3261 ConstantRange SrcCR = CR.truncate(NewCmpBW);
3262 CmpInst::Predicate EquivPred;
3263 APInt EquivInt;
3264 APInt EquivOffset;
3265
3266 SrcCR.getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
3267 return new ICmpInst(
3268 EquivPred,
3269 EquivOffset.isZero()
3270 ? V
3271 : Builder.CreateAdd(V, ConstantInt::get(NewCmpTy, EquivOffset)),
3272 ConstantInt::get(NewCmpTy, EquivInt));
3273 }
3274 }
3275 }
3276
3277 return nullptr;
3278}
3279
3281 Value *&RHS, ConstantInt *&Less,
3282 ConstantInt *&Equal,
3283 ConstantInt *&Greater) {
3284 // TODO: Generalize this to work with other comparison idioms or ensure
3285 // they get canonicalized into this form.
3286
3287 // select i1 (a == b),
3288 // i32 Equal,
3289 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3290 // where Equal, Less and Greater are placeholders for any three constants.
3291 CmpPredicate PredA;
3292 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||
3293 !ICmpInst::isEquality(PredA))
3294 return false;
3295 Value *EqualVal = SI->getTrueValue();
3296 Value *UnequalVal = SI->getFalseValue();
3297 // We still can get non-canonical predicate here, so canonicalize.
3298 if (PredA == ICmpInst::ICMP_NE)
3299 std::swap(EqualVal, UnequalVal);
3300 if (!match(EqualVal, m_ConstantInt(Equal)))
3301 return false;
3302 CmpPredicate PredB;
3303 Value *LHS2, *RHS2;
3304 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),
3305 m_ConstantInt(Less), m_ConstantInt(Greater))))
3306 return false;
3307 // We can get predicate mismatch here, so canonicalize if possible:
3308 // First, ensure that 'LHS' match.
3309 if (LHS2 != LHS) {
3310 // x sgt y <--> y slt x
3311 std::swap(LHS2, RHS2);
3312 PredB = ICmpInst::getSwappedPredicate(PredB);
3313 }
3314 if (LHS2 != LHS)
3315 return false;
3316 // We also need to canonicalize 'RHS'.
3317 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
3318 // x sgt C-1 <--> x sge C <--> not(x slt C)
3319 auto FlippedStrictness =
3320 getFlippedStrictnessPredicateAndConstant(PredB, cast<Constant>(RHS2));
3321 if (!FlippedStrictness)
3322 return false;
3323 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3324 "basic correctness failure");
3325 RHS2 = FlippedStrictness->second;
3326 // And kind-of perform the result swap.
3327 std::swap(Less, Greater);
3328 PredB = ICmpInst::ICMP_SLT;
3329 }
3330 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3331}
3332
3335 ConstantInt *C) {
3336
3337 assert(C && "Cmp RHS should be a constant int!");
3338 // If we're testing a constant value against the result of a three way
3339 // comparison, the result can be expressed directly in terms of the
3340 // original values being compared. Note: We could possibly be more
3341 // aggressive here and remove the hasOneUse test. The original select is
3342 // really likely to simplify or sink when we remove a test of the result.
3343 Value *OrigLHS, *OrigRHS;
3344 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3345 if (Cmp.hasOneUse() &&
3346 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,
3347 C3GreaterThan)) {
3348 assert(C1LessThan && C2Equal && C3GreaterThan);
3349
3350 bool TrueWhenLessThan = ICmpInst::compare(
3351 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate());
3352 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(),
3353 Cmp.getPredicate());
3354 bool TrueWhenGreaterThan = ICmpInst::compare(
3355 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate());
3356
3357 // This generates the new instruction that will replace the original Cmp
3358 // Instruction. Instead of enumerating the various combinations when
3359 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3360 // false, we rely on chaining of ORs and future passes of InstCombine to
3361 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3362
3363 // When none of the three constants satisfy the predicate for the RHS (C),
3364 // the entire original Cmp can be simplified to a false.
3366 if (TrueWhenLessThan)
3368 Cond, Builder.CreateICmp(ICmpInst::ICMP_SLT, OrigLHS, OrigRHS));
3369 if (TrueWhenEqual)
3371 Cond, Builder.CreateICmp(ICmpInst::ICMP_EQ, OrigLHS, OrigRHS));
3372 if (TrueWhenGreaterThan)
3374 Cond, Builder.CreateICmp(ICmpInst::ICMP_SGT, OrigLHS, OrigRHS));
3375
3376 return replaceInstUsesWith(Cmp, Cond);
3377 }
3378 return nullptr;
3379}
3380
3382 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));
3383 if (!Bitcast)
3384 return nullptr;
3385
3386 ICmpInst::Predicate Pred = Cmp.getPredicate();
3387 Value *Op1 = Cmp.getOperand(1);
3388 Value *BCSrcOp = Bitcast->getOperand(0);
3389 Type *SrcType = Bitcast->getSrcTy();
3390 Type *DstType = Bitcast->getType();
3391
3392 // Make sure the bitcast doesn't change between scalar and vector and
3393 // doesn't change the number of vector elements.
3394 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3395 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3396 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3397 Value *X;
3398 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {
3399 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3400 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3401 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3402 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3403 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3404 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3405 match(Op1, m_Zero()))
3406 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3407
3408 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3409 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))
3410 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));
3411
3412 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3413 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))
3414 return new ICmpInst(Pred, X,
3415 ConstantInt::getAllOnesValue(X->getType()));
3416 }
3417
3418 // Zero-equality checks are preserved through unsigned floating-point casts:
3419 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3420 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3421 if (match(BCSrcOp, m_UIToFP(m_Value(X))))
3422 if (Cmp.isEquality() && match(Op1, m_Zero()))
3423 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3424
3425 const APInt *C;
3426 bool TrueIfSigned;
3427 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {
3428 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3429 // the FP extend/truncate because that cast does not change the sign-bit.
3430 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3431 // The sign-bit is always the most significant bit in those types.
3432 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&
3433 (match(BCSrcOp, m_FPExt(m_Value(X))) ||
3434 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {
3435 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3436 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3437 Type *XType = X->getType();
3438
3439 // We can't currently handle Power style floating point operations here.
3440 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3441 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());
3442 if (auto *XVTy = dyn_cast<VectorType>(XType))
3443 NewType = VectorType::get(NewType, XVTy->getElementCount());
3444 Value *NewBitcast = Builder.CreateBitCast(X, NewType);
3445 if (TrueIfSigned)
3446 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3447 ConstantInt::getNullValue(NewType));
3448 else
3449 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3451 }
3452 }
3453
3454 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3455 Type *FPType = SrcType->getScalarType();
3456 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3457 Attribute::NoImplicitFloat) &&
3458 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3459 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3460 if (Mask & (fcInf | fcZero)) {
3461 if (Pred == ICmpInst::ICMP_NE)
3462 Mask = ~Mask;
3463 return replaceInstUsesWith(Cmp,
3464 Builder.createIsFPClass(BCSrcOp, Mask));
3465 }
3466 }
3467 }
3468 }
3469
3470 const APInt *C;
3471 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||
3472 !SrcType->isIntOrIntVectorTy())
3473 return nullptr;
3474
3475 // If this is checking if all elements of a vector compare are set or not,
3476 // invert the casted vector equality compare and test if all compare
3477 // elements are clear or not. Compare against zero is generally easier for
3478 // analysis and codegen.
3479 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3480 // Example: are all elements equal? --> are zero elements not equal?
3481 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3482 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3483 if (Value *NotBCSrcOp =
3484 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {
3485 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);
3486 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));
3487 }
3488 }
3489
3490 // If this is checking if all elements of an extended vector are clear or not,
3491 // compare in a narrow type to eliminate the extend:
3492 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3493 Value *X;
3494 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3495 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {
3496 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {
3497 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());
3498 Value *NewCast = Builder.CreateBitCast(X, NewType);
3499 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));
3500 }
3501 }
3502
3503 // Folding: icmp <pred> iN X, C
3504 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3505 // and C is a splat of a K-bit pattern
3506 // and SC is a constant vector = <C', C', C', ..., C'>
3507 // Into:
3508 // %E = extractelement <M x iK> %vec, i32 C'
3509 // icmp <pred> iK %E, trunc(C)
3510 Value *Vec;
3511 ArrayRef<int> Mask;
3512 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {
3513 // Check whether every element of Mask is the same constant
3514 if (all_equal(Mask)) {
3515 auto *VecTy = cast<VectorType>(SrcType);
3516 auto *EltTy = cast<IntegerType>(VecTy->getElementType());
3517 if (C->isSplat(EltTy->getBitWidth())) {
3518 // Fold the icmp based on the value of C
3519 // If C is M copies of an iK sized bit pattern,
3520 // then:
3521 // => %E = extractelement <N x iK> %vec, i32 Elem
3522 // icmp <pred> iK %SplatVal, <pattern>
3523 Value *Elem = Builder.getInt32(Mask[0]);
3524 Value *Extract = Builder.CreateExtractElement(Vec, Elem);
3525 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));
3526 return new ICmpInst(Pred, Extract, NewC);
3527 }
3528 }
3529 }
3530 return nullptr;
3531}
3532
3533/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3534/// where X is some kind of instruction.
3536 const APInt *C;
3537
3538 if (match(Cmp.getOperand(1), m_APInt(C))) {
3539 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))
3540 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))
3541 return I;
3542
3543 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))
3544 // For now, we only support constant integers while folding the
3545 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3546 // similar to the cases handled by binary ops above.
3547 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))
3548 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))
3549 return I;
3550
3551 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))
3552 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))
3553 return I;
3554
3555 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))
3557 return I;
3558
3559 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3560 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3561 // TODO: This checks one-use, but that is not strictly necessary.
3562 Value *Cmp0 = Cmp.getOperand(0);
3563 Value *X, *Y;
3564 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3565 (match(Cmp0,
3566 m_ExtractValue<0>(m_Intrinsic<Intrinsic::ssub_with_overflow>(
3567 m_Value(X), m_Value(Y)))) ||
3568 match(Cmp0,
3569 m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>(
3570 m_Value(X), m_Value(Y))))))
3571 return new ICmpInst(Cmp.getPredicate(), X, Y);
3572 }
3573
3574 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))
3576
3577 return nullptr;
3578}
3579
3580/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3581/// icmp eq/ne BO, C.
3583 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3584 // TODO: Some of these folds could work with arbitrary constants, but this
3585 // function is limited to scalar and vector splat constants.
3586 if (!Cmp.isEquality())
3587 return nullptr;
3588
3589 ICmpInst::Predicate Pred = Cmp.getPredicate();
3590 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3591 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
3592 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
3593
3594 switch (BO->getOpcode()) {
3595 case Instruction::SRem:
3596 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3597 if (C.isZero() && BO->hasOneUse()) {
3598 const APInt *BOC;
3599 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
3600 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());
3601 return new ICmpInst(Pred, NewRem,
3603 }
3604 }
3605 break;
3606 case Instruction::Add: {
3607 // (A + C2) == C --> A == (C - C2)
3608 // (A + C2) != C --> A != (C - C2)
3609 // TODO: Remove the one-use limitation? See discussion in D58633.
3610 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {
3611 if (BO->hasOneUse())
3612 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));
3613 } else if (C.isZero()) {
3614 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3615 // efficiently invertible, or if the add has just this one use.
3616 if (Value *NegVal = dyn_castNegVal(BOp1))
3617 return new ICmpInst(Pred, BOp0, NegVal);
3618 if (Value *NegVal = dyn_castNegVal(BOp0))
3619 return new ICmpInst(Pred, NegVal, BOp1);
3620 if (BO->hasOneUse()) {
3621 // (add nuw A, B) != 0 -> (or A, B) != 0
3622 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
3623 Value *Or = Builder.CreateOr(BOp0, BOp1);
3624 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
3625 }
3626 Value *Neg = Builder.CreateNeg(BOp1);
3627 Neg->takeName(BO);
3628 return new ICmpInst(Pred, BOp0, Neg);
3629 }
3630 }
3631 break;
3632 }
3633 case Instruction::Xor:
3634 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
3635 // For the xor case, we can xor two constants together, eliminating
3636 // the explicit xor.
3637 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
3638 } else if (C.isZero()) {
3639 // Replace ((xor A, B) != 0) with (A != B)
3640 return new ICmpInst(Pred, BOp0, BOp1);
3641 }
3642 break;
3643 case Instruction::Or: {
3644 const APInt *BOC;
3645 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3646 // Comparing if all bits outside of a constant mask are set?
3647 // Replace (X | C) == -1 with (X & ~C) == ~C.
3648 // This removes the -1 constant.
3649 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));
3650 Value *And = Builder.CreateAnd(BOp0, NotBOC);
3651 return new ICmpInst(Pred, And, NotBOC);
3652 }
3653 // (icmp eq (or (select cond, 0, NonZero), Other), 0)
3654 // -> (and cond, (icmp eq Other, 0))
3655 // (icmp ne (or (select cond, NonZero, 0), Other), 0)
3656 // -> (or cond, (icmp ne Other, 0))
3657 Value *Cond, *TV, *FV, *Other, *Sel;
3658 if (C.isZero() &&
3659 match(BO,
3662 m_Value(FV))),
3663 m_Value(Other)))) &&
3664 Cond->getType() == Cmp.getType()) {
3665 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3666 // Easy case is if eq/ne matches whether 0 is trueval/falseval.
3667 if (Pred == ICmpInst::ICMP_EQ
3668 ? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))
3669 : (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {
3670 Value *Cmp = Builder.CreateICmp(
3671 Pred, Other, Constant::getNullValue(Other->getType()));
3673 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3674 Cond);
3675 }
3676 // Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3677 // case we need to invert the select condition so we need to be careful to
3678 // avoid creating extra instructions.
3679 // (icmp ne (or (select cond, 0, NonZero), Other), 0)
3680 // -> (or (not cond), (icmp ne Other, 0))
3681 // (icmp eq (or (select cond, NonZero, 0), Other), 0)
3682 // -> (and (not cond), (icmp eq Other, 0))
3683 //
3684 // Only do this if the inner select has one use, in which case we are
3685 // replacing `select` with `(not cond)`. Otherwise, we will create more
3686 // uses. NB: Trying to freely invert cond doesn't make sense here, as if
3687 // cond was freely invertable, the select arms would have been inverted.
3688 if (Sel->hasOneUse() &&
3689 (Pred == ICmpInst::ICMP_EQ
3690 ? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))
3691 : (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {
3692 Value *NotCond = Builder.CreateNot(Cond);
3693 Value *Cmp = Builder.CreateICmp(
3694 Pred, Other, Constant::getNullValue(Other->getType()));
3696 Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3697 NotCond);
3698 }
3699 }
3700 break;
3701 }
3702 case Instruction::UDiv:
3703 case Instruction::SDiv:
3704 if (BO->isExact()) {
3705 // div exact X, Y eq/ne 0 -> X eq/ne 0
3706 // div exact X, Y eq/ne 1 -> X eq/ne Y
3707 // div exact X, Y eq/ne C ->
3708 // if Y * C never-overflow && OneUse:
3709 // -> Y * C eq/ne X
3710 if (C.isZero())
3711 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));
3712 else if (C.isOne())
3713 return new ICmpInst(Pred, BOp0, BOp1);
3714 else if (BO->hasOneUse()) {
3716 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,
3717 Cmp.getOperand(1), BO);
3719 Value *YC =
3720 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));
3721 return new ICmpInst(Pred, YC, BOp0);
3722 }
3723 }
3724 }
3725 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3726 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3727 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3728 return new ICmpInst(NewPred, BOp1, BOp0);
3729 }
3730 break;
3731 default:
3732 break;
3733 }
3734 return nullptr;
3735}
3736
3738 const APInt &CRhs,
3739 InstCombiner::BuilderTy &Builder,
3740 const SimplifyQuery &Q) {
3741 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3742 "Non-ctpop intrin in ctpop fold");
3743 if (!CtpopLhs->hasOneUse())
3744 return nullptr;
3745
3746 // Power of 2 test:
3747 // isPow2OrZero : ctpop(X) u< 2
3748 // isPow2 : ctpop(X) == 1
3749 // NotPow2OrZero: ctpop(X) u> 1
3750 // NotPow2 : ctpop(X) != 1
3751 // If we know any bit of X can be folded to:
3752 // IsPow2 : X & (~Bit) == 0
3753 // NotPow2 : X & (~Bit) != 0
3754 const ICmpInst::Predicate Pred = I.getPredicate();
3755 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3756 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3757 Value *Op = CtpopLhs->getArgOperand(0);
3758 KnownBits OpKnown = computeKnownBits(Op, Q.DL,
3759 /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT);
3760 // No need to check for count > 1, that should be already constant folded.
3761 if (OpKnown.countMinPopulation() == 1) {
3762 Value *And = Builder.CreateAnd(
3763 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));
3764 return new ICmpInst(
3765 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3768 And, Constant::getNullValue(Op->getType()));
3769 }
3770 }
3771
3772 return nullptr;
3773}
3774
3775/// Fold an equality icmp with LLVM intrinsic and constant operand.
3777 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3778 Type *Ty = II->getType();
3779 unsigned BitWidth = C.getBitWidth();
3780 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3781
3782 switch (II->getIntrinsicID()) {
3783 case Intrinsic::abs:
3784 // abs(A) == 0 -> A == 0
3785 // abs(A) == INT_MIN -> A == INT_MIN
3786 if (C.isZero() || C.isMinSignedValue())
3787 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));
3788 break;
3789
3790 case Intrinsic::bswap:
3791 // bswap(A) == C -> A == bswap(C)
3792 return new ICmpInst(Pred, II->getArgOperand(0),
3793 ConstantInt::get(Ty, C.byteSwap()));
3794
3795 case Intrinsic::bitreverse:
3796 // bitreverse(A) == C -> A == bitreverse(C)
3797 return new ICmpInst(Pred, II->getArgOperand(0),
3798 ConstantInt::get(Ty, C.reverseBits()));
3799
3800 case Intrinsic::ctlz:
3801 case Intrinsic::cttz: {
3802 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3803 if (C == BitWidth)
3804 return new ICmpInst(Pred, II->getArgOperand(0),
3806
3807 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3808 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3809 // Limit to one use to ensure we don't increase instruction count.
3810 unsigned Num = C.getLimitedValue(BitWidth);
3811 if (Num != BitWidth && II->hasOneUse()) {
3812 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3813 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)
3814 : APInt::getHighBitsSet(BitWidth, Num + 1);
3815 APInt Mask2 = IsTrailing
3818 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),
3819 ConstantInt::get(Ty, Mask2));
3820 }
3821 break;
3822 }
3823
3824 case Intrinsic::ctpop: {
3825 // popcount(A) == 0 -> A == 0 and likewise for !=
3826 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3827 bool IsZero = C.isZero();
3828 if (IsZero || C == BitWidth)
3829 return new ICmpInst(Pred, II->getArgOperand(0),
3830 IsZero ? Constant::getNullValue(Ty)
3832
3833 break;
3834 }
3835
3836 case Intrinsic::fshl:
3837 case Intrinsic::fshr:
3838 if (II->getArgOperand(0) == II->getArgOperand(1)) {
3839 const APInt *RotAmtC;
3840 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3841 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3842 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))
3843 return new ICmpInst(Pred, II->getArgOperand(0),
3844 II->getIntrinsicID() == Intrinsic::fshl
3845 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))
3846 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));
3847 }
3848 break;
3849
3850 case Intrinsic::umax:
3851 case Intrinsic::uadd_sat: {
3852 // uadd.sat(a, b) == 0 -> (a | b) == 0
3853 // umax(a, b) == 0 -> (a | b) == 0
3854 if (C.isZero() && II->hasOneUse()) {
3855 Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1));
3856 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3857 }
3858 break;
3859 }
3860
3861 case Intrinsic::ssub_sat:
3862 // ssub.sat(a, b) == 0 -> a == b
3863 if (C.isZero())
3864 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3865 break;
3866 case Intrinsic::usub_sat: {
3867 // usub.sat(a, b) == 0 -> a <= b
3868 if (C.isZero()) {
3869 ICmpInst::Predicate NewPred =
3871 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));
3872 }
3873 break;
3874 }
3875 default:
3876 break;
3877 }
3878
3879 return nullptr;
3880}
3881
3882/// Fold an icmp with LLVM intrinsics
3883static Instruction *
3885 InstCombiner::BuilderTy &Builder) {
3886 assert(Cmp.isEquality());
3887
3888 ICmpInst::Predicate Pred = Cmp.getPredicate();
3889 Value *Op0 = Cmp.getOperand(0);
3890 Value *Op1 = Cmp.getOperand(1);
3891 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);
3892 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);
3893 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3894 return nullptr;
3895
3896 switch (IIOp0->getIntrinsicID()) {
3897 case Intrinsic::bswap:
3898 case Intrinsic::bitreverse:
3899 // If both operands are byte-swapped or bit-reversed, just compare the
3900 // original values.
3901 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3902 case Intrinsic::fshl:
3903 case Intrinsic::fshr: {
3904 // If both operands are rotated by same amount, just compare the
3905 // original values.
3906 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))
3907 break;
3908 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))
3909 break;
3910 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))
3911 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3912
3913 // rotate(X, AmtX) == rotate(Y, AmtY)
3914 // -> rotate(X, AmtX - AmtY) == Y
3915 // Do this if either both rotates have one use or if only one has one use
3916 // and AmtX/AmtY are constants.
3917 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3918 if (OneUses == 2 ||
3919 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&
3920 match(IIOp1->getOperand(2), m_ImmConstant()))) {
3921 Value *SubAmt =
3922 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));
3923 Value *CombinedRotate = Builder.CreateIntrinsic(
3924 Op0->getType(), IIOp0->getIntrinsicID(),
3925 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});
3926 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);
3927 }
3928 } break;
3929 default:
3930 break;
3931 }
3932
3933 return nullptr;
3934}
3935
3936/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3937/// where X is some kind of instruction and C is AllowPoison.
3938/// TODO: Move more folds which allow poison to this function.
3941 const APInt &C) {
3942 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3943 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {
3944 switch (II->getIntrinsicID()) {
3945 default:
3946 break;
3947 case Intrinsic::fshl:
3948 case Intrinsic::fshr:
3949 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {
3950 // (rot X, ?) == 0/-1 --> X == 0/-1
3951 if (C.isZero() || C.isAllOnes())
3952 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
3953 }
3954 break;
3955 }
3956 }
3957
3958 return nullptr;
3959}
3960
3961/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
3963 BinaryOperator *BO,
3964 const APInt &C) {
3965 switch (BO->getOpcode()) {
3966 case Instruction::Xor:
3967 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
3968 return I;
3969 break;
3970 case Instruction::And:
3971 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
3972 return I;
3973 break;
3974 case Instruction::Or:
3975 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
3976 return I;
3977 break;
3978 case Instruction::Mul:
3979 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
3980 return I;
3981 break;
3982 case Instruction::Shl:
3983 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
3984 return I;
3985 break;
3986 case Instruction::LShr:
3987 case Instruction::AShr:
3988 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
3989 return I;
3990 break;
3991 case Instruction::SRem:
3992 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
3993 return I;
3994 break;
3995 case Instruction::UDiv:
3996 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
3997 return I;
3998 [[fallthrough]];
3999 case Instruction::SDiv:
4000 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
4001 return I;
4002 break;
4003 case Instruction::Sub:
4004 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
4005 return I;
4006 break;
4007 case Instruction::Add:
4008 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
4009 return I;
4010 break;
4011 default:
4012 break;
4013 }
4014
4015 // TODO: These folds could be refactored to be part of the above calls.
4016 return foldICmpBinOpEqualityWithConstant(Cmp, BO, C);
4017}
4018
4019static Instruction *
4021 const APInt &C,
4022 InstCombiner::BuilderTy &Builder) {
4023 // This transform may end up producing more than one instruction for the
4024 // intrinsic, so limit it to one user of the intrinsic.
4025 if (!II->hasOneUse())
4026 return nullptr;
4027
4028 // Let Y = [add/sub]_sat(X, C) pred C2
4029 // SatVal = The saturating value for the operation
4030 // WillWrap = Whether or not the operation will underflow / overflow
4031 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
4032 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
4033 //
4034 // When (SatVal pred C2) is true, then
4035 // Y = WillWrap ? true : ((X binop C) pred C2)
4036 // => Y = WillWrap || ((X binop C) pred C2)
4037 // else
4038 // Y = WillWrap ? false : ((X binop C) pred C2)
4039 // => Y = !WillWrap ? ((X binop C) pred C2) : false
4040 // => Y = !WillWrap && ((X binop C) pred C2)
4041 Value *Op0 = II->getOperand(0);
4042 Value *Op1 = II->getOperand(1);
4043
4044 const APInt *COp1;
4045 // This transform only works when the intrinsic has an integral constant or
4046 // splat vector as the second operand.
4047 if (!match(Op1, m_APInt(COp1)))
4048 return nullptr;
4049
4050 APInt SatVal;
4051 switch (II->getIntrinsicID()) {
4052 default:
4054 "This function only works with usub_sat and uadd_sat for now!");
4055 case Intrinsic::uadd_sat:
4056 SatVal = APInt::getAllOnes(C.getBitWidth());
4057 break;
4058 case Intrinsic::usub_sat:
4059 SatVal = APInt::getZero(C.getBitWidth());
4060 break;
4061 }
4062
4063 // Check (SatVal pred C2)
4064 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);
4065
4066 // !WillWrap.
4068 II->getBinaryOp(), *COp1, II->getNoWrapKind());
4069
4070 // WillWrap.
4071 if (SatValCheck)
4072 C1 = C1.inverse();
4073
4075 if (II->getBinaryOp() == Instruction::Add)
4076 C2 = C2.sub(*COp1);
4077 else
4078 C2 = C2.add(*COp1);
4079
4080 Instruction::BinaryOps CombiningOp =
4081 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
4082
4083 std::optional<ConstantRange> Combination;
4084 if (CombiningOp == Instruction::BinaryOps::Or)
4085 Combination = C1.exactUnionWith(C2);
4086 else /* CombiningOp == Instruction::BinaryOps::And */
4087 Combination = C1.exactIntersectWith(C2);
4088
4089 if (!Combination)
4090 return nullptr;
4091
4092 CmpInst::Predicate EquivPred;
4093 APInt EquivInt;
4094 APInt EquivOffset;
4095
4096 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
4097
4098 return new ICmpInst(
4099 EquivPred,
4100 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),
4101 ConstantInt::get(Op1->getType(), EquivInt));
4102}
4103
4104static Instruction *
4106 const APInt &C,
4107 InstCombiner::BuilderTy &Builder) {
4108 std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
4109 switch (Pred) {
4110 case ICmpInst::ICMP_EQ:
4111 case ICmpInst::ICMP_NE:
4112 if (C.isZero())
4113 NewPredicate = Pred;
4114 else if (C.isOne())
4115 NewPredicate =
4117 else if (C.isAllOnes())
4118 NewPredicate =
4120 break;
4121
4122 case ICmpInst::ICMP_SGT:
4123 if (C.isAllOnes())
4124 NewPredicate = ICmpInst::ICMP_UGE;
4125 else if (C.isZero())
4126 NewPredicate = ICmpInst::ICMP_UGT;
4127 break;
4128
4129 case ICmpInst::ICMP_SLT:
4130 if (C.isZero())
4131 NewPredicate = ICmpInst::ICMP_ULT;
4132 else if (C.isOne())
4133 NewPredicate = ICmpInst::ICMP_ULE;
4134 break;
4135
4136 case ICmpInst::ICMP_ULT:
4137 if (C.ugt(1))
4138 NewPredicate = ICmpInst::ICMP_UGE;
4139 break;
4140
4141 case ICmpInst::ICMP_UGT:
4142 if (!C.isZero() && !C.isAllOnes())
4143 NewPredicate = ICmpInst::ICMP_ULT;
4144 break;
4145
4146 default:
4147 break;
4148 }
4149
4150 if (!NewPredicate)
4151 return nullptr;
4152
4153 if (I->getIntrinsicID() == Intrinsic::scmp)
4154 NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate);
4155 Value *LHS = I->getOperand(0);
4156 Value *RHS = I->getOperand(1);
4157 return new ICmpInst(*NewPredicate, LHS, RHS);
4158}
4159
4160/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
4163 const APInt &C) {
4164 ICmpInst::Predicate Pred = Cmp.getPredicate();
4165
4166 // Handle folds that apply for any kind of icmp.
4167 switch (II->getIntrinsicID()) {
4168 default:
4169 break;
4170 case Intrinsic::uadd_sat:
4171 case Intrinsic::usub_sat:
4172 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
4173 Pred, cast<SaturatingInst>(II), C, Builder))
4174 return Folded;
4175 break;
4176 case Intrinsic::ctpop: {
4177 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
4178 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))
4179 return R;
4180 } break;
4181 case Intrinsic::scmp:
4182 case Intrinsic::ucmp:
4183 if (auto *Folded = foldICmpOfCmpIntrinsicWithConstant(Pred, II, C, Builder))
4184 return Folded;
4185 break;
4186 }
4187
4188 if (Cmp.isEquality())
4189 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
4190
4191 Type *Ty = II->getType();
4192 unsigned BitWidth = C.getBitWidth();
4193 switch (II->getIntrinsicID()) {
4194 case Intrinsic::ctpop: {
4195 // (ctpop X > BitWidth - 1) --> X == -1
4196 Value *X = II->getArgOperand(0);
4197 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
4198 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,
4200 // (ctpop X < BitWidth) --> X != -1
4201 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
4202 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,
4204 break;
4205 }
4206 case Intrinsic::ctlz: {
4207 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
4208 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4209 unsigned Num = C.getLimitedValue();
4210 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);
4211 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,
4212 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4213 }
4214
4215 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
4216 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4217 unsigned Num = C.getLimitedValue();
4219 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,
4220 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
4221 }
4222 break;
4223 }
4224 case Intrinsic::cttz: {
4225 // Limit to one use to ensure we don't increase instruction count.
4226 if (!II->hasOneUse())
4227 return nullptr;
4228
4229 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
4230 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
4231 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);
4232 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,
4233 Builder.CreateAnd(II->getArgOperand(0), Mask),
4235 }
4236
4237 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
4238 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
4239 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());
4240 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,
4241 Builder.CreateAnd(II->getArgOperand(0), Mask),
4243 }
4244 break;
4245 }
4246 case Intrinsic::ssub_sat:
4247 // ssub.sat(a, b) spred 0 -> a spred b
4248 if (ICmpInst::isSigned(Pred)) {
4249 if (C.isZero())
4250 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
4251 // X s<= 0 is cannonicalized to X s< 1
4252 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
4253 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),
4254 II->getArgOperand(1));
4255 // X s>= 0 is cannonicalized to X s> -1
4256 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
4257 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),
4258 II->getArgOperand(1));
4259 }
4260 break;
4261 default:
4262 break;
4263 }
4264
4265 return nullptr;
4266}
4267
4268/// Handle icmp with constant (but not simple integer constant) RHS.
4270 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4271 Constant *RHSC = dyn_cast<Constant>(Op1);
4272 Instruction *LHSI = dyn_cast<Instruction>(Op0);
4273 if (!RHSC || !LHSI)
4274 return nullptr;
4275
4276 switch (LHSI->getOpcode()) {
4277 case Instruction::PHI:
4278 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
4279 return NV;
4280 break;
4281 case Instruction::IntToPtr:
4282 // icmp pred inttoptr(X), null -> icmp pred X, 0
4283 if (RHSC->isNullValue() &&
4284 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
4285 return new ICmpInst(
4286 I.getPredicate(), LHSI->getOperand(0),
4288 break;
4289
4290 case Instruction::Load:
4291 // Try to optimize things like "A[i] > 4" to index computations.
4292 if (GetElementPtrInst *GEP =
4293 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
4294 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
4295 if (Instruction *Res =
4296 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, GV, I))
4297 return Res;
4298 break;
4299 }
4300
4301 return nullptr;
4302}
4303
4305 Value *RHS, const ICmpInst &I) {
4306 // Try to fold the comparison into the select arms, which will cause the
4307 // select to be converted into a logical and/or.
4308 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4309 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))
4310 return Res;
4311 if (std::optional<bool> Impl = isImpliedCondition(
4312 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))
4313 return ConstantInt::get(I.getType(), *Impl);
4314 return nullptr;
4315 };
4316
4317 ConstantInt *CI = nullptr;
4318 Value *Op1 = SimplifyOp(SI->getOperand(1), true);
4319 if (Op1)
4320 CI = dyn_cast<ConstantInt>(Op1);
4321
4322 Value *Op2 = SimplifyOp(SI->getOperand(2), false);
4323 if (Op2)
4324 CI = dyn_cast<ConstantInt>(Op2);
4325
4326 auto Simplifies = [&](Value *Op, unsigned Idx) {
4327 // A comparison of ucmp/scmp with a constant will fold into an icmp.
4328 const APInt *Dummy;
4329 return Op ||
4330 (isa<CmpIntrinsic>(SI->getOperand(Idx)) &&
4331 SI->getOperand(Idx)->hasOneUse() && match(RHS, m_APInt(Dummy)));
4332 };
4333
4334 // We only want to perform this transformation if it will not lead to
4335 // additional code. This is true if either both sides of the select
4336 // fold to a constant (in which case the icmp is replaced with a select
4337 // which will usually simplify) or this is the only user of the
4338 // select (in which case we are trading a select+icmp for a simpler
4339 // select+icmp) or all uses of the select can be replaced based on
4340 // dominance information ("Global cases").
4341 bool Transform = false;
4342 if (Op1 && Op2)
4343 Transform = true;
4344 else if (Simplifies(Op1, 1) || Simplifies(Op2, 2)) {
4345 // Local case
4346 if (SI->hasOneUse())
4347 Transform = true;
4348 // Global cases
4349 else if (CI && !CI->isZero())
4350 // When Op1 is constant try replacing select with second operand.
4351 // Otherwise Op2 is constant and try replacing select with first
4352 // operand.
4353 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4354 }
4355 if (Transform) {
4356 if (!Op1)
4357 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4358 if (!Op2)
4359 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4360 return SelectInst::Create(SI->getOperand(0), Op1, Op2);
4361 }
4362
4363 return nullptr;
4364}
4365
4366// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4367static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4368 unsigned Depth = 0) {
4369 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4370 return true;
4371 if (V->getType()->getScalarSizeInBits() == 1)
4372 return true;
4374 return false;
4375 Value *X;
4376 const Instruction *I = dyn_cast<Instruction>(V);
4377 if (!I)
4378 return false;
4379 switch (I->getOpcode()) {
4380 case Instruction::ZExt:
4381 // ZExt(Mask) is a Mask.
4382 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4383 case Instruction::SExt:
4384 // SExt(Mask) is a Mask.
4385 // SExt(~Mask) is a ~Mask.
4386 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4387 case Instruction::And:
4388 case Instruction::Or:
4389 // Mask0 | Mask1 is a Mask.
4390 // Mask0 & Mask1 is a Mask.
4391 // ~Mask0 | ~Mask1 is a ~Mask.
4392 // ~Mask0 & ~Mask1 is a ~Mask.
4393 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4394 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4395 case Instruction::Xor:
4396 if (match(V, m_Not(m_Value(X))))
4397 return isMaskOrZero(X, !Not, Q, Depth);
4398
4399 // (X ^ -X) is a ~Mask
4400 if (Not)
4401 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4402 // (X ^ (X - 1)) is a Mask
4403 else
4404 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4405 case Instruction::Select:
4406 // c ? Mask0 : Mask1 is a Mask.
4407 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4408 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4409 case Instruction::Shl:
4410 // (~Mask) << X is a ~Mask.
4411 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4412 case Instruction::LShr:
4413 // Mask >> X is a Mask.
4414 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4415 case Instruction::AShr:
4416 // Mask s>> X is a Mask.
4417 // ~Mask s>> X is a ~Mask.
4418 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4419 case Instruction::Add:
4420 // Pow2 - 1 is a Mask.
4421 if (!Not && match(I->getOperand(1), m_AllOnes()))
4422 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4423 Depth, Q.AC, Q.CxtI, Q.DT);
4424 break;
4425 case Instruction::Sub:
4426 // -Pow2 is a ~Mask.
4427 if (Not && match(I->getOperand(0), m_Zero()))
4428 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4429 Depth, Q.AC, Q.CxtI, Q.DT);
4430 break;
4431 case Instruction::Call: {
4432 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4433 switch (II->getIntrinsicID()) {
4434 // min/max(Mask0, Mask1) is a Mask.
4435 // min/max(~Mask0, ~Mask1) is a ~Mask.
4436 case Intrinsic::umax:
4437 case Intrinsic::smax:
4438 case Intrinsic::umin:
4439 case Intrinsic::smin:
4440 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4441 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4442
4443 // In the context of masks, bitreverse(Mask) == ~Mask
4444 case Intrinsic::bitreverse:
4445 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4446 default:
4447 break;
4448 }
4449 }
4450 break;
4451 }
4452 default:
4453 break;
4454 }
4455 return false;
4456}
4457
4458/// Some comparisons can be simplified.
4459/// In this case, we are looking for comparisons that look like
4460/// a check for a lossy truncation.
4461/// Folds:
4462/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4463/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4464/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4465/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4466/// Where Mask is some pattern that produces all-ones in low bits:
4467/// (-1 >> y)
4468/// ((-1 << y) >> y) <- non-canonical, has extra uses
4469/// ~(-1 << y)
4470/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4471/// The Mask can be a constant, too.
4472/// For some predicates, the operands are commutative.
4473/// For others, x can only be on a specific side.
4475 Value *Op1, const SimplifyQuery &Q,
4476 InstCombiner &IC) {
4477
4478 ICmpInst::Predicate DstPred;
4479 switch (Pred) {
4481 // x & Mask == x
4482 // x & ~Mask == 0
4483 // ~x | Mask == -1
4484 // -> x u<= Mask
4485 // x & ~Mask == ~Mask
4486 // -> ~Mask u<= x
4488 break;
4490 // x & Mask != x
4491 // x & ~Mask != 0
4492 // ~x | Mask != -1
4493 // -> x u> Mask
4494 // x & ~Mask != ~Mask
4495 // -> ~Mask u> x
4497 break;
4499 // x & Mask u< x
4500 // -> x u> Mask
4501 // x & ~Mask u< ~Mask
4502 // -> ~Mask u> x
4504 break;
4506 // x & Mask u>= x
4507 // -> x u<= Mask
4508 // x & ~Mask u>= ~Mask
4509 // -> ~Mask u<= x
4511 break;
4513 // x & Mask s< x [iff Mask s>= 0]
4514 // -> x s> Mask
4515 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4516 // -> ~Mask s> x
4518 break;
4520 // x & Mask s>= x [iff Mask s>= 0]
4521 // -> x s<= Mask
4522 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4523 // -> ~Mask s<= x
4525 break;
4526 default:
4527 // We don't support sgt,sle
4528 // ult/ugt are simplified to true/false respectively.
4529 return nullptr;
4530 }
4531
4532 Value *X, *M;
4533 // Put search code in lambda for early positive returns.
4534 auto IsLowBitMask = [&]() {
4535 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4536 X = Op1;
4537 // Look for: x & Mask pred x
4538 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4539 return !ICmpInst::isSigned(Pred) ||
4540 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4541 }
4542
4543 // Look for: x & ~Mask pred ~Mask
4544 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4545 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4546 }
4547 return false;
4548 }
4549 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4550 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4551
4552 auto Check = [&]() {
4553 // Look for: ~x | Mask == -1
4554 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4555 if (Value *NotX =
4556 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4557 X = NotX;
4558 return true;
4559 }
4560 }
4561 return false;
4562 };
4563 if (Check())
4564 return true;
4565 std::swap(X, M);
4566 return Check();
4567 }
4568 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4569 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4570 auto Check = [&]() {
4571 // Look for: x & ~Mask == 0
4572 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4573 if (Value *NotM =
4574 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4575 M = NotM;
4576 return true;
4577 }
4578 }
4579 return false;
4580 };
4581 if (Check())
4582 return true;
4583 std::swap(X, M);
4584 return Check();
4585 }
4586 return false;
4587 };
4588
4589 if (!IsLowBitMask())
4590 return nullptr;
4591
4592 return IC.Builder.CreateICmp(DstPred, X, M);
4593}
4594
4595/// Some comparisons can be simplified.
4596/// In this case, we are looking for comparisons that look like
4597/// a check for a lossy signed truncation.
4598/// Folds: (MaskedBits is a constant.)
4599/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4600/// Into:
4601/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4602/// Where KeptBits = bitwidth(%x) - MaskedBits
4603static Value *
4605 InstCombiner::BuilderTy &Builder) {
4606 CmpPredicate SrcPred;
4607 Value *X;
4608 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4609 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4610 if (!match(&I, m_c_ICmp(SrcPred,
4612 m_APInt(C1))),
4613 m_Deferred(X))))
4614 return nullptr;
4615
4616 // Potential handling of non-splats: for each element:
4617 // * if both are undef, replace with constant 0.
4618 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4619 // * if both are not undef, and are different, bailout.
4620 // * else, only one is undef, then pick the non-undef one.
4621
4622 // The shift amount must be equal.
4623 if (*C0 != *C1)
4624 return nullptr;
4625 const APInt &MaskedBits = *C0;
4626 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4627
4628 ICmpInst::Predicate DstPred;
4629 switch (SrcPred) {
4631 // ((%x << MaskedBits) a>> MaskedBits) == %x
4632 // =>
4633 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4635 break;
4637 // ((%x << MaskedBits) a>> MaskedBits) != %x
4638 // =>
4639 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4641 break;
4642 // FIXME: are more folds possible?
4643 default:
4644 return nullptr;
4645 }
4646
4647 auto *XType = X->getType();
4648 const unsigned XBitWidth = XType->getScalarSizeInBits();
4649 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4650 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4651
4652 // KeptBits = bitwidth(%x) - MaskedBits
4653 const APInt KeptBits = BitWidth - MaskedBits;
4654 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4655 // ICmpCst = (1 << KeptBits)
4656 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4657 assert(ICmpCst.isPowerOf2());
4658 // AddCst = (1 << (KeptBits-1))
4659 const APInt AddCst = ICmpCst.lshr(1);
4660 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4661
4662 // T0 = add %x, AddCst
4663 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4664 // T1 = T0 DstPred ICmpCst
4665 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4666
4667 return T1;
4668}
4669
4670// Given pattern:
4671// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4672// we should move shifts to the same hand of 'and', i.e. rewrite as
4673// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4674// We are only interested in opposite logical shifts here.
4675// One of the shifts can be truncated.
4676// If we can, we want to end up creating 'lshr' shift.
4677static Value *
4679 InstCombiner::BuilderTy &Builder) {
4680 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4681 !I.getOperand(0)->hasOneUse())
4682 return nullptr;
4683
4684 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4685
4686 // Look for an 'and' of two logical shifts, one of which may be truncated.
4687 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4688 Instruction *XShift, *MaybeTruncation, *YShift;
4689 if (!match(
4690 I.getOperand(0),
4691 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4693 m_AnyLogicalShift, m_Instruction(YShift))),
4694 m_Instruction(MaybeTruncation)))))
4695 return nullptr;
4696
4697 // We potentially looked past 'trunc', but only when matching YShift,
4698 // therefore YShift must have the widest type.
4699 Instruction *WidestShift = YShift;
4700 // Therefore XShift must have the shallowest type.
4701 // Or they both have identical types if there was no truncation.
4702 Instruction *NarrowestShift = XShift;
4703
4704 Type *WidestTy = WidestShift->getType();
4705 Type *NarrowestTy = NarrowestShift->getType();
4706 assert(NarrowestTy == I.getOperand(0)->getType() &&
4707 "We did not look past any shifts while matching XShift though.");
4708 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4709
4710 // If YShift is a 'lshr', swap the shifts around.
4711 if (match(YShift, m_LShr(m_Value(), m_Value())))
4712 std::swap(XShift, YShift);
4713
4714 // The shifts must be in opposite directions.
4715 auto XShiftOpcode = XShift->getOpcode();
4716 if (XShiftOpcode == YShift->getOpcode())
4717 return nullptr; // Do not care about same-direction shifts here.
4718
4719 Value *X, *XShAmt, *Y, *YShAmt;
4720 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4721 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4722
4723 // If one of the values being shifted is a constant, then we will end with
4724 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4725 // however, we will need to ensure that we won't increase instruction count.
4726 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4727 // At least one of the hands of the 'and' should be one-use shift.
4728 if (!match(I.getOperand(0),
4729 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4730 return nullptr;
4731 if (HadTrunc) {
4732 // Due to the 'trunc', we will need to widen X. For that either the old
4733 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4734 if (!MaybeTruncation->hasOneUse() &&
4735 !NarrowestShift->getOperand(1)->hasOneUse())
4736 return nullptr;
4737 }
4738 }
4739
4740 // We have two shift amounts from two different shifts. The types of those
4741 // shift amounts may not match. If that's the case let's bailout now.
4742 if (XShAmt->getType() != YShAmt->getType())
4743 return nullptr;
4744
4745 // As input, we have the following pattern:
4746 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4747 // We want to rewrite that as:
4748 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4749 // While we know that originally (Q+K) would not overflow
4750 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4751 // shift amounts. so it may now overflow in smaller bitwidth.
4752 // To ensure that does not happen, we need to ensure that the total maximal
4753 // shift amount is still representable in that smaller bit width.
4754 unsigned MaximalPossibleTotalShiftAmount =
4755 (WidestTy->getScalarSizeInBits() - 1) +
4756 (NarrowestTy->getScalarSizeInBits() - 1);
4757 APInt MaximalRepresentableShiftAmount =
4759 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4760 return nullptr;
4761
4762 // Can we fold (XShAmt+YShAmt) ?
4763 auto *NewShAmt = dyn_cast_or_null<Constant>(
4764 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4765 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4766 if (!NewShAmt)
4767 return nullptr;
4768 if (NewShAmt->getType() != WidestTy) {
4769 NewShAmt =
4770 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4771 if (!NewShAmt)
4772 return nullptr;
4773 }
4774 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4775
4776 // Is the new shift amount smaller than the bit width?
4777 // FIXME: could also rely on ConstantRange.
4778 if (!match(NewShAmt,
4780 APInt(WidestBitWidth, WidestBitWidth))))
4781 return nullptr;
4782
4783 // An extra legality check is needed if we had trunc-of-lshr.
4784 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4785 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4786 WidestShift]() {
4787 // It isn't obvious whether it's worth it to analyze non-constants here.
4788 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4789 // If *any* of these preconditions matches we can perform the fold.
4790 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4791 ? NewShAmt->getSplatValue()
4792 : NewShAmt;
4793 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4794 if (NewShAmtSplat &&
4795 (NewShAmtSplat->isNullValue() ||
4796 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4797 return true;
4798 // We consider *min* leading zeros so a single outlier
4799 // blocks the transform as opposed to allowing it.
4800 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4801 KnownBits Known = computeKnownBits(C, SQ.DL);
4802 unsigned MinLeadZero = Known.countMinLeadingZeros();
4803 // If the value being shifted has at most lowest bit set we can fold.
4804 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4805 if (MaxActiveBits <= 1)
4806 return true;
4807 // Precondition: NewShAmt u<= countLeadingZeros(C)
4808 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4809 return true;
4810 }
4811 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4812 KnownBits Known = computeKnownBits(C, SQ.DL);
4813 unsigned MinLeadZero = Known.countMinLeadingZeros();
4814 // If the value being shifted has at most lowest bit set we can fold.
4815 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4816 if (MaxActiveBits <= 1)
4817 return true;
4818 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4819 if (NewShAmtSplat) {
4820 APInt AdjNewShAmt =
4821 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4822 if (AdjNewShAmt.ule(MinLeadZero))
4823 return true;
4824 }
4825 }
4826 return false; // Can't tell if it's ok.
4827 };
4828 if (!CanFold())
4829 return nullptr;
4830 }
4831
4832 // All good, we can do this fold.
4833 X = Builder.CreateZExt(X, WidestTy);
4834 Y = Builder.CreateZExt(Y, WidestTy);
4835 // The shift is the same that was for X.
4836 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4837 ? Builder.CreateLShr(X, NewShAmt)
4838 : Builder.CreateShl(X, NewShAmt);
4839 Value *T1 = Builder.CreateAnd(T0, Y);
4840 return Builder.CreateICmp(I.getPredicate(), T1,
4841 Constant::getNullValue(WidestTy));
4842}
4843
4844/// Fold
4845/// (-1 u/ x) u< y
4846/// ((x * y) ?/ x) != y
4847/// to
4848/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4849/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4850/// will mean that we are looking for the opposite answer.
4852 CmpPredicate Pred;
4853 Value *X, *Y;
4855 Instruction *Div;
4856 bool NeedNegation;
4857 // Look for: (-1 u/ x) u</u>= y
4858 if (!I.isEquality() &&
4859 match(&I, m_c_ICmp(Pred,
4861 m_Instruction(Div)),
4862 m_Value(Y)))) {
4863 Mul = nullptr;
4864
4865 // Are we checking that overflow does not happen, or does happen?
4866 switch (Pred) {
4868 NeedNegation = false;
4869 break; // OK
4871 NeedNegation = true;
4872 break; // OK
4873 default:
4874 return nullptr; // Wrong predicate.
4875 }
4876 } else // Look for: ((x * y) / x) !=/== y
4877 if (I.isEquality() &&
4878 match(&I, m_c_ICmp(Pred, m_Value(Y),
4881 m_Value(X)),
4883 m_Deferred(X))),
4884 m_Instruction(Div))))) {
4885 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4886 } else
4887 return nullptr;
4888
4890 // If the pattern included (x * y), we'll want to insert new instructions
4891 // right before that original multiplication so that we can replace it.
4892 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4893 if (MulHadOtherUses)
4895
4897 Div->getOpcode() == Instruction::UDiv ? Intrinsic::umul_with_overflow
4898 : Intrinsic::smul_with_overflow,
4899 X->getType(), {X, Y}, /*FMFSource=*/nullptr, "mul");
4900
4901 // If the multiplication was used elsewhere, to ensure that we don't leave
4902 // "duplicate" instructions, replace uses of that original multiplication
4903 // with the multiplication result from the with.overflow intrinsic.
4904 if (MulHadOtherUses)
4905 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4906
4907 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4908 if (NeedNegation) // This technically increases instruction count.
4909 Res = Builder.CreateNot(Res, "mul.not.ov");
4910
4911 // If we replaced the mul, erase it. Do this after all uses of Builder,
4912 // as the mul is used as insertion point.
4913 if (MulHadOtherUses)
4915
4916 return Res;
4917}
4918
4920 InstCombiner::BuilderTy &Builder) {
4921 CmpPredicate Pred;
4922 Value *X;
4923 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4924
4925 if (ICmpInst::isSigned(Pred))
4926 Pred = ICmpInst::getSwappedPredicate(Pred);
4927 else if (ICmpInst::isUnsigned(Pred))
4928 Pred = ICmpInst::getSignedPredicate(Pred);
4929 // else for equality-comparisons just keep the predicate.
4930
4931 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4932 Constant::getNullValue(X->getType()), I.getName());
4933 }
4934
4935 // A value is not equal to its negation unless that value is 0 or
4936 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4937 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
4938 ICmpInst::isEquality(Pred)) {
4939 Type *Ty = X->getType();
4941 Constant *MaxSignedVal =
4942 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
4943 Value *And = Builder.CreateAnd(X, MaxSignedVal);
4944 Constant *Zero = Constant::getNullValue(Ty);
4945 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
4946 }
4947
4948 return nullptr;
4949}
4950
4952 InstCombinerImpl &IC) {
4953 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4954 // Normalize and operand as operand 0.
4955 CmpInst::Predicate Pred = I.getPredicate();
4956 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
4957 std::swap(Op0, Op1);
4958 Pred = ICmpInst::getSwappedPredicate(Pred);
4959 }
4960
4961 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
4962 return nullptr;
4963
4964 // (icmp (X & Y) u< X --> (X & Y) != X
4965 if (Pred == ICmpInst::ICMP_ULT)
4966 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4967
4968 // (icmp (X & Y) u>= X --> (X & Y) == X
4969 if (Pred == ICmpInst::ICMP_UGE)
4970 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4971
4972 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4973 // icmp (X & Y) eq/ne Y --> (X | ~Y) eq/ne -1 if Y is freely invertible and
4974 // Y is non-constant. If Y is constant the `X & C == C` form is preferable
4975 // so don't do this fold.
4976 if (!match(Op1, m_ImmConstant()))
4977 if (auto *NotOp1 =
4978 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
4979 return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),
4980 Constant::getAllOnesValue(Op1->getType()));
4981 // icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
4982 if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4983 return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),
4984 Constant::getNullValue(Op1->getType()));
4985 }
4986
4987 if (!ICmpInst::isSigned(Pred))
4988 return nullptr;
4989
4990 KnownBits KnownY = IC.computeKnownBits(A, /*Depth=*/0, &I);
4991 // (X & NegY) spred X --> (X & NegY) upred X
4992 if (KnownY.isNegative())
4993 return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
4994
4995 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
4996 return nullptr;
4997
4998 if (KnownY.isNonNegative())
4999 // (X & PosY) s<= X --> X s>= 0
5000 // (X & PosY) s> X --> X s< 0
5001 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
5002 Constant::getNullValue(Op1->getType()));
5003
5005 // (NegX & Y) s<= NegX --> Y s< 0
5006 // (NegX & Y) s> NegX --> Y s>= 0
5008 Constant::getNullValue(A->getType()));
5009
5010 return nullptr;
5011}
5012
5014 InstCombinerImpl &IC) {
5015 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5016
5017 // Normalize or operand as operand 0.
5018 CmpInst::Predicate Pred = I.getPredicate();
5019 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
5020 std::swap(Op0, Op1);
5021 Pred = ICmpInst::getSwappedPredicate(Pred);
5022 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
5023 return nullptr;
5024 }
5025
5026 // icmp (X | Y) u<= X --> (X | Y) == X
5027 if (Pred == ICmpInst::ICMP_ULE)
5028 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
5029
5030 // icmp (X | Y) u> X --> (X | Y) != X
5031 if (Pred == ICmpInst::ICMP_UGT)
5032 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
5033
5034 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
5035 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
5036 if (Value *NotOp1 =
5037 IC.getFreelyInverted(Op1, !Op1->hasNUsesOrMore(3), &IC.Builder))
5038 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
5039 Constant::getNullValue(Op1->getType()));
5040 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
5041 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
5042 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
5043 Constant::getAllOnesValue(Op1->getType()));
5044 }
5045 return nullptr;
5046}
5047
5049 InstCombinerImpl &IC) {
5050 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
5051 // Normalize xor operand as operand 0.
5052 CmpInst::Predicate Pred = I.getPredicate();
5053 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
5054 std::swap(Op0, Op1);
5055 Pred = ICmpInst::getSwappedPredicate(Pred);
5056 }
5057 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
5058 return nullptr;
5059
5060 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
5061 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
5062 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
5063 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
5065 if (PredOut != Pred && isKnownNonZero(A, Q))
5066 return new ICmpInst(PredOut, Op0, Op1);
5067
5068 return nullptr;
5069}
5070
5071/// Try to fold icmp (binop), X or icmp X, (binop).
5072/// TODO: A large part of this logic is duplicated in InstSimplify's
5073/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
5074/// duplication.
5076 const SimplifyQuery &SQ) {
5078 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5079
5080 // Special logic for binary operators.
5081 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
5082 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
5083 if (!BO0 && !BO1)
5084 return nullptr;
5085
5086 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
5087 return NewICmp;
5088
5089 const CmpInst::Predicate Pred = I.getPredicate();
5090 Value *X;
5091
5092 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
5093 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
5094 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
5095 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5096 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
5097 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
5098 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
5099 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5100 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
5101
5102 {
5103 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
5104 Constant *C;
5105 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
5106 m_ImmConstant(C)))) &&
5107 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
5109 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
5110 }
5111 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5112 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
5113 m_ImmConstant(C)))) &&
5114 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5116 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
5117 }
5118 }
5119
5120 // (icmp eq/ne (X, -P2), INT_MIN)
5121 // -> (icmp slt/sge X, INT_MIN + P2)
5122 if (ICmpInst::isEquality(Pred) && BO0 &&
5123 match(I.getOperand(1), m_SignMask()) &&
5125 // Will Constant fold.
5126 Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
5127 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5129 BO0->getOperand(0), NewC);
5130 }
5131
5132 {
5133 // Similar to above: an unsigned overflow comparison may use offset + mask:
5134 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5135 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5136 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5137 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5138 BinaryOperator *BO;
5139 const APInt *C;
5140 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5141 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5143 CmpInst::Predicate NewPred =
5145 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5146 return new ICmpInst(NewPred, Op1, Zero);
5147 }
5148
5149 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5150 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5152 CmpInst::Predicate NewPred =
5154 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5155 return new ICmpInst(NewPred, Op0, Zero);
5156 }
5157 }
5158
5159 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5160 bool Op0HasNUW = false, Op1HasNUW = false;
5161 bool Op0HasNSW = false, Op1HasNSW = false;
5162 // Analyze the case when either Op0 or Op1 is an add instruction.
5163 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5164 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5165 bool &HasNSW, bool &HasNUW) -> bool {
5166 if (isa<OverflowingBinaryOperator>(BO)) {
5167 HasNUW = BO.hasNoUnsignedWrap();
5168 HasNSW = BO.hasNoSignedWrap();
5169 return ICmpInst::isEquality(Pred) ||
5170 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5171 (CmpInst::isSigned(Pred) && HasNSW);
5172 } else if (BO.getOpcode() == Instruction::Or) {
5173 HasNUW = true;
5174 HasNSW = true;
5175 return true;
5176 } else {
5177 return false;
5178 }
5179 };
5180 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5181
5182 if (BO0) {
5183 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
5184 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5185 }
5186 if (BO1) {
5187 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
5188 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5189 }
5190
5191 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5192 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5193 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5194 return new ICmpInst(Pred, A == Op1 ? B : A,
5195 Constant::getNullValue(Op1->getType()));
5196
5197 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5198 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5199 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5200 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
5201 C == Op0 ? D : C);
5202
5203 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5204 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5205 NoOp1WrapProblem) {
5206 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5207 Value *Y, *Z;
5208 if (A == C) {
5209 // C + B == C + D -> B == D
5210 Y = B;
5211 Z = D;
5212 } else if (A == D) {
5213 // D + B == C + D -> B == C
5214 Y = B;
5215 Z = C;
5216 } else if (B == C) {
5217 // A + C == C + D -> A == D
5218 Y = A;
5219 Z = D;
5220 } else {
5221 assert(B == D);
5222 // A + D == C + D -> A == C
5223 Y = A;
5224 Z = C;
5225 }
5226 return new ICmpInst(Pred, Y, Z);
5227 }
5228
5229 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5230 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
5231 match(B, m_AllOnes()))
5232 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
5233
5234 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5235 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
5236 match(B, m_AllOnes()))
5237 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
5238
5239 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5240 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
5241 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
5242
5243 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5244 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
5245 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
5246
5247 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5248 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
5249 match(D, m_AllOnes()))
5250 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
5251
5252 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5253 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
5254 match(D, m_AllOnes()))
5255 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
5256
5257 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5258 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
5259 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
5260
5261 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5262 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
5263 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
5264
5265 // TODO: The subtraction-related identities shown below also hold, but
5266 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5267 // wouldn't happen even if they were implemented.
5268 //
5269 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5270 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5271 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5272 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5273
5274 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5275 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One()))
5276 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1);
5277
5278 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5279 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One()))
5280 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1);
5281
5282 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5283 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One()))
5284 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C);
5285
5286 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5287 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One()))
5288 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C);
5289
5290 // if C1 has greater magnitude than C2:
5291 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5292 // s.t. C3 = C1 - C2
5293 //
5294 // if C2 has greater magnitude than C1:
5295 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5296 // s.t. C3 = C2 - C1
5297 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5298 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5299 const APInt *AP1, *AP2;
5300 // TODO: Support non-uniform vectors.
5301 // TODO: Allow poison passthrough if B or D's element is poison.
5302 if (match(B, m_APIntAllowPoison(AP1)) &&
5303 match(D, m_APIntAllowPoison(AP2)) &&
5304 AP1->isNegative() == AP2->isNegative()) {
5305 APInt AP1Abs = AP1->abs();
5306 APInt AP2Abs = AP2->abs();
5307 if (AP1Abs.uge(AP2Abs)) {
5308 APInt Diff = *AP1 - *AP2;
5309 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5310 Value *NewAdd = Builder.CreateAdd(
5311 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
5312 return new ICmpInst(Pred, NewAdd, C);
5313 } else {
5314 APInt Diff = *AP2 - *AP1;
5315 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5316 Value *NewAdd = Builder.CreateAdd(
5317 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
5318 return new ICmpInst(Pred, A, NewAdd);
5319 }
5320 }
5321 Constant *Cst1, *Cst2;
5322 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5323 ICmpInst::isEquality(Pred)) {
5324 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5325 Value *NewAdd = Builder.CreateAdd(C, Diff);
5326 return new ICmpInst(Pred, A, NewAdd);
5327 }
5328 }
5329
5330 // Analyze the case when either Op0 or Op1 is a sub instruction.
5331 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5332 A = nullptr;
5333 B = nullptr;
5334 C = nullptr;
5335 D = nullptr;
5336 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5337 A = BO0->getOperand(0);
5338 B = BO0->getOperand(1);
5339 }
5340 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5341 C = BO1->getOperand(0);
5342 D = BO1->getOperand(1);
5343 }
5344
5345 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5346 if (A == Op1 && NoOp0WrapProblem)
5347 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5348 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5349 if (C == Op0 && NoOp1WrapProblem)
5350 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5351
5352 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5353 // (A - B) u>/u<= A --> B u>/u<= A
5354 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5355 return new ICmpInst(Pred, B, A);
5356 // C u</u>= (C - D) --> C u</u>= D
5357 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5358 return new ICmpInst(Pred, C, D);
5359 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5360 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5361 isKnownNonZero(B, Q))
5363 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5364 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5365 isKnownNonZero(D, Q))
5367
5368 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5369 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5370 return new ICmpInst(Pred, A, C);
5371
5372 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5373 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5374 return new ICmpInst(Pred, D, B);
5375
5376 // icmp (0-X) < cst --> x > -cst
5377 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5378 Value *X;
5379 if (match(BO0, m_Neg(m_Value(X))))
5380 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5381 if (RHSC->isNotMinSignedValue())
5382 return new ICmpInst(I.getSwappedPredicate(), X,
5383 ConstantExpr::getNeg(RHSC));
5384 }
5385
5386 if (Instruction *R = foldICmpXorXX(I, Q, *this))
5387 return R;
5388 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5389 return R;
5390
5391 {
5392 // Try to remove shared multiplier from comparison:
5393 // X * Z pred Y * Z
5394 Value *X, *Y, *Z;
5395 if ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5396 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5397 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5398 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y))))) {
5399 if (ICmpInst::isSigned(Pred)) {
5400 if (Op0HasNSW && Op1HasNSW) {
5401 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5402 if (ZKnown.isStrictlyPositive())
5403 return new ICmpInst(Pred, X, Y);
5404 if (ZKnown.isNegative())
5405 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y);
5408 if (LessThan && match(LessThan, m_One()))
5409 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z,
5410 Constant::getNullValue(Z->getType()));
5411 Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y,
5413 if (GreaterThan && match(GreaterThan, m_One()))
5414 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5415 }
5416 } else {
5417 bool NonZero;
5418 if (ICmpInst::isEquality(Pred)) {
5419 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0
5420 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&
5421 isKnownNonEqual(X, Y, SQ))
5422 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5423
5424 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5425 // if Z % 2 != 0
5426 // X * Z eq/ne Y * Z -> X eq/ne Y
5427 if (ZKnown.countMaxTrailingZeros() == 0)
5428 return new ICmpInst(Pred, X, Y);
5429 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5430 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5431 // X * Z eq/ne Y * Z -> X eq/ne Y
5432 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5433 return new ICmpInst(Pred, X, Y);
5434 } else
5435 NonZero = isKnownNonZero(Z, Q);
5436
5437 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5438 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5439 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5440 return new ICmpInst(Pred, X, Y);
5441 }
5442 }
5443 }
5444
5445 BinaryOperator *SRem = nullptr;
5446 // icmp (srem X, Y), Y
5447 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5448 SRem = BO0;
5449 // icmp Y, (srem X, Y)
5450 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5451 Op0 == BO1->getOperand(1))
5452 SRem = BO1;
5453 if (SRem) {
5454 // We don't check hasOneUse to avoid increasing register pressure because
5455 // the value we use is the same value this instruction was already using.
5456 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5457 default:
5458 break;
5459 case ICmpInst::ICMP_EQ:
5460 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5461 case ICmpInst::ICMP_NE:
5462 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5463 case ICmpInst::ICMP_SGT:
5464 case ICmpInst::ICMP_SGE:
5465 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5467 case ICmpInst::ICMP_SLT:
5468 case ICmpInst::ICMP_SLE:
5469 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5471 }
5472 }
5473
5474 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5475 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5476 BO0->getOperand(1) == BO1->getOperand(1)) {
5477 switch (BO0->getOpcode()) {
5478 default:
5479 break;
5480 case Instruction::Add:
5481 case Instruction::Sub:
5482 case Instruction::Xor: {
5483 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5484 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5485
5486 const APInt *C;
5487 if (match(BO0->getOperand(1), m_APInt(C))) {
5488 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5489 if (C->isSignMask()) {
5490 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5491 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5492 }
5493
5494 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5495 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5496 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5497 NewPred = I.getSwappedPredicate(NewPred);
5498 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5499 }
5500 }
5501 break;
5502 }
5503 case Instruction::Mul: {
5504 if (!I.isEquality())
5505 break;
5506
5507 const APInt *C;
5508 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5509 !C->isOne()) {
5510 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5511 // Mask = -1 >> count-trailing-zeros(C).
5512 if (unsigned TZs = C->countr_zero()) {
5513 Constant *Mask = ConstantInt::get(
5514 BO0->getType(),
5515 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5516 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5517 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5518 return new ICmpInst(Pred, And1, And2);
5519 }
5520 }
5521 break;
5522 }
5523 case Instruction::UDiv:
5524 case Instruction::LShr:
5525 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5526 break;
5527 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5528
5529 case Instruction::SDiv:
5530 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5531 !BO0->isExact() || !BO1->isExact())
5532 break;
5533 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5534
5535 case Instruction::AShr:
5536 if (!BO0->isExact() || !BO1->isExact())
5537 break;
5538 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5539
5540 case Instruction::Shl: {
5541 bool NUW = Op0HasNUW && Op1HasNUW;
5542 bool NSW = Op0HasNSW && Op1HasNSW;
5543 if (!NUW && !NSW)
5544 break;
5545 if (!NSW && I.isSigned())
5546 break;
5547 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5548 }
5549 }
5550 }
5551
5552 if (BO0) {
5553 // Transform A & (L - 1) `ult` L --> L != 0
5554 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5555 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5556
5557 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5558 auto *Zero = Constant::getNullValue(BO0->getType());
5559 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5560 }
5561 }
5562
5563 // For unsigned predicates / eq / ne:
5564 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5565 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5566 if (!ICmpInst::isSigned(Pred)) {
5567 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5568 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5569 Constant::getNullValue(Op1->getType()));
5570 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5571 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5572 Constant::getNullValue(Op0->getType()), Op0);
5573 }
5574
5576 return replaceInstUsesWith(I, V);
5577
5578 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5579 return R;
5580
5582 return replaceInstUsesWith(I, V);
5583
5585 return replaceInstUsesWith(I, V);
5586
5587 return nullptr;
5588}
5589
5590/// Fold icmp Pred min|max(X, Y), Z.
5593 Value *Z, CmpPredicate Pred) {
5594 Value *X = MinMax->getLHS();
5595 Value *Y = MinMax->getRHS();
5596 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5597 return nullptr;
5598 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5599 // Revert the transform signed pred -> unsigned pred
5600 // TODO: We can flip the signedness of predicate if both operands of icmp
5601 // are negative.
5605 } else
5606 return nullptr;
5607 }
5609 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5610 if (!Val)
5611 return std::nullopt;
5612 if (match(Val, m_One()))
5613 return true;
5614 if (match(Val, m_Zero()))
5615 return false;
5616 return std::nullopt;
5617 };
5618 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5619 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5620 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5621 return nullptr;
5622 if (!CmpXZ.has_value()) {
5623 std::swap(X, Y);
5624 std::swap(CmpXZ, CmpYZ);
5625 }
5626
5627 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5628 if (CmpYZ.has_value())
5629 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5630 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5631 };
5632
5633 switch (Pred) {
5634 case ICmpInst::ICMP_EQ:
5635 case ICmpInst::ICMP_NE: {
5636 // If X == Z:
5637 // Expr Result
5638 // min(X, Y) == Z X <= Y
5639 // max(X, Y) == Z X >= Y
5640 // min(X, Y) != Z X > Y
5641 // max(X, Y) != Z X < Y
5642 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5643 ICmpInst::Predicate NewPred =
5644 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5645 if (Pred == ICmpInst::ICMP_NE)
5646 NewPred = ICmpInst::getInversePredicate(NewPred);
5647 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5648 }
5649 // Otherwise (X != Z):
5650 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5651 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5652 if (!MinMaxCmpXZ.has_value()) {
5653 std::swap(X, Y);
5654 std::swap(CmpXZ, CmpYZ);
5655 // Re-check pre-condition X != Z
5656 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5657 break;
5658 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5659 }
5660 if (!MinMaxCmpXZ.has_value())
5661 break;
5662 if (*MinMaxCmpXZ) {
5663 // Expr Fact Result
5664 // min(X, Y) == Z X < Z false
5665 // max(X, Y) == Z X > Z false
5666 // min(X, Y) != Z X < Z true
5667 // max(X, Y) != Z X > Z true
5668 return replaceInstUsesWith(
5669 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5670 } else {
5671 // Expr Fact Result
5672 // min(X, Y) == Z X > Z Y == Z
5673 // max(X, Y) == Z X < Z Y == Z
5674 // min(X, Y) != Z X > Z Y != Z
5675 // max(X, Y) != Z X < Z Y != Z
5676 return FoldIntoCmpYZ();
5677 }
5678 break;
5679 }
5680 case ICmpInst::ICMP_SLT:
5681 case ICmpInst::ICMP_ULT:
5682 case ICmpInst::ICMP_SLE:
5683 case ICmpInst::ICMP_ULE:
5684 case ICmpInst::ICMP_SGT:
5685 case ICmpInst::ICMP_UGT:
5686 case ICmpInst::ICMP_SGE:
5687 case ICmpInst::ICMP_UGE: {
5688 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5689 if (*CmpXZ) {
5690 if (IsSame) {
5691 // Expr Fact Result
5692 // min(X, Y) < Z X < Z true
5693 // min(X, Y) <= Z X <= Z true
5694 // max(X, Y) > Z X > Z true
5695 // max(X, Y) >= Z X >= Z true
5696 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5697 } else {
5698 // Expr Fact Result
5699 // max(X, Y) < Z X < Z Y < Z
5700 // max(X, Y) <= Z X <= Z Y <= Z
5701 // min(X, Y) > Z X > Z Y > Z
5702 // min(X, Y) >= Z X >= Z Y >= Z
5703 return FoldIntoCmpYZ();
5704 }
5705 } else {
5706 if (IsSame) {
5707 // Expr Fact Result
5708 // min(X, Y) < Z X >= Z Y < Z
5709 // min(X, Y) <= Z X > Z Y <= Z
5710 // max(X, Y) > Z X <= Z Y > Z
5711 // max(X, Y) >= Z X < Z Y >= Z
5712 return FoldIntoCmpYZ();
5713 } else {
5714 // Expr Fact Result
5715 // max(X, Y) < Z X >= Z false
5716 // max(X, Y) <= Z X > Z false
5717 // min(X, Y) > Z X <= Z false
5718 // min(X, Y) >= Z X < Z false
5719 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5720 }
5721 }
5722 break;
5723 }
5724 default:
5725 break;
5726 }
5727
5728 return nullptr;
5729}
5730
5731// Canonicalize checking for a power-of-2-or-zero value:
5733 InstCombiner::BuilderTy &Builder) {
5734 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5735 const CmpInst::Predicate Pred = I.getPredicate();
5736 Value *A = nullptr;
5737 bool CheckIs;
5738 if (I.isEquality()) {
5739 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5740 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5741 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5742 m_Deferred(A)))) ||
5743 !match(Op1, m_ZeroInt()))
5744 A = nullptr;
5745
5746 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5747 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5748 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5749 A = Op1;
5750 else if (match(Op1,
5752 A = Op0;
5753
5754 CheckIs = Pred == ICmpInst::ICMP_EQ;
5755 } else if (ICmpInst::isUnsigned(Pred)) {
5756 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5757 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5758
5759 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5761 m_Specific(Op1))))) {
5762 A = Op1;
5763 CheckIs = Pred == ICmpInst::ICMP_UGE;
5764 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5766 m_Specific(Op0))))) {
5767 A = Op0;
5768 CheckIs = Pred == ICmpInst::ICMP_ULE;
5769 }
5770 }
5771
5772 if (A) {
5773 Type *Ty = A->getType();
5774 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5775 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5776 ConstantInt::get(Ty, 2))
5777 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5778 ConstantInt::get(Ty, 1));
5779 }
5780
5781 return nullptr;
5782}
5783
5785 if (!I.isEquality())
5786 return nullptr;
5787
5788 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5789 const CmpInst::Predicate Pred = I.getPredicate();
5790 Value *A, *B, *C, *D;
5791 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5792 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5793 Value *OtherVal = A == Op1 ? B : A;
5794 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5795 }
5796
5797 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
5798 // A^c1 == C^c2 --> A == C^(c1^c2)
5799 ConstantInt *C1, *C2;
5800 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
5801 Op1->hasOneUse()) {
5802 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
5804 return new ICmpInst(Pred, A, Xor);
5805 }
5806
5807 // A^B == A^D -> B == D
5808 if (A == C)
5809 return new ICmpInst(Pred, B, D);
5810 if (A == D)
5811 return new ICmpInst(Pred, B, C);
5812 if (B == C)
5813 return new ICmpInst(Pred, A, D);
5814 if (B == D)
5815 return new ICmpInst(Pred, A, C);
5816 }
5817 }
5818
5819 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
5820 // A == (A^B) -> B == 0
5821 Value *OtherVal = A == Op0 ? B : A;
5822 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5823 }
5824
5825 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
5826 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
5827 match(Op1, m_And(m_Value(C), m_Value(D)))) {
5828 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
5829
5830 if (A == C) {
5831 X = B;
5832 Y = D;
5833 Z = A;
5834 } else if (A == D) {
5835 X = B;
5836 Y = C;
5837 Z = A;
5838 } else if (B == C) {
5839 X = A;
5840 Y = D;
5841 Z = B;
5842 } else if (B == D) {
5843 X = A;
5844 Y = C;
5845 Z = B;
5846 }
5847
5848 if (X) {
5849 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
5850 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
5851 // instructions.
5852 const APInt *C0, *C1;
5853 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
5854 (*C0 ^ *C1).isNegatedPowerOf2();
5855
5856 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
5857 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
5858 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
5859 int UseCnt =
5860 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
5861 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
5862 if (XorIsNegP2 || UseCnt >= 2) {
5863 // Build (X^Y) & Z
5864 Op1 = Builder.CreateXor(X, Y);
5865 Op1 = Builder.CreateAnd(Op1, Z);
5866 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
5867 }
5868 }
5869 }
5870
5871 {
5872 // Similar to above, but specialized for constant because invert is needed:
5873 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
5874 Value *X, *Y;
5875 Constant *C;
5876 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
5877 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
5880 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
5881 }
5882 }
5883
5884 if (match(Op1, m_ZExt(m_Value(A))) &&
5885 (Op0->hasOneUse() || Op1->hasOneUse())) {
5886 // (B & (Pow2C-1)) == zext A --> A == trunc B
5887 // (B & (Pow2C-1)) != zext A --> A != trunc B
5888 const APInt *MaskC;
5889 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
5890 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
5891 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
5892 }
5893
5894 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
5895 // For lshr and ashr pairs.
5896 const APInt *AP1, *AP2;
5897 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5898 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
5899 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5900 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
5901 if (AP1 != AP2)
5902 return nullptr;
5903 unsigned TypeBits = AP1->getBitWidth();
5904 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
5905 if (ShAmt < TypeBits && ShAmt != 0) {
5906 ICmpInst::Predicate NewPred =
5908 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5909 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
5910 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
5911 }
5912 }
5913
5914 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
5915 ConstantInt *Cst1;
5916 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
5917 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
5918 unsigned TypeBits = Cst1->getBitWidth();
5919 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
5920 if (ShAmt < TypeBits && ShAmt != 0) {
5921 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5922 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
5923 Value *And =
5924 Builder.CreateAnd(Xor, Builder.getInt(AndVal), I.getName() + ".mask");
5925 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
5926 }
5927 }
5928
5929 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
5930 // "icmp (and X, mask), cst"
5931 uint64_t ShAmt = 0;
5932 if (Op0->hasOneUse() &&
5933 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
5934 match(Op1, m_ConstantInt(Cst1)) &&
5935 // Only do this when A has multiple uses. This is most important to do
5936 // when it exposes other optimizations.
5937 !A->hasOneUse()) {
5938 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
5939
5940 if (ShAmt < ASize) {
5941 APInt MaskV =
5943 MaskV <<= ShAmt;
5944
5945 APInt CmpV = Cst1->getValue().zext(ASize);
5946 CmpV <<= ShAmt;
5947
5948 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
5949 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
5950 }
5951 }
5952
5954 return ICmp;
5955
5956 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks
5957 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s
5958 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a
5959 // few steps of instcombine.
5960 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
5961 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
5963 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
5964 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
5966 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
5967 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
5969 Add, ConstantInt::get(A->getType(), C.shl(1)));
5970 }
5971
5972 // Canonicalize:
5973 // Assume B_Pow2 != 0
5974 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
5975 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
5976 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
5977 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, 0, &I))
5978 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
5980
5981 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
5982 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, 0, &I))
5983 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
5984 ConstantInt::getNullValue(Op1->getType()));
5985
5986 // Canonicalize:
5987 // icmp eq/ne X, OneUse(rotate-right(X))
5988 // -> icmp eq/ne X, rotate-left(X)
5989 // We generally try to convert rotate-right -> rotate-left, this just
5990 // canonicalizes another case.
5991 if (match(&I, m_c_ICmp(m_Value(A),
5992 m_OneUse(m_Intrinsic<Intrinsic::fshr>(
5993 m_Deferred(A), m_Deferred(A), m_Value(B))))))
5994 return new ICmpInst(
5995 Pred, A,
5996 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
5997
5998 // Canonicalize:
5999 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
6000 Constant *Cst;
6003 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
6004
6005 {
6006 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6007 auto m_Matcher =
6010 m_Sub(m_Value(B), m_Deferred(A)));
6011 std::optional<bool> IsZero = std::nullopt;
6012 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),
6013 m_Deferred(A))))
6014 IsZero = false;
6015 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6016 else if (match(&I,
6017 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))
6018 IsZero = true;
6019
6020 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, /*Depth*/ 0, &I))
6021 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6022 // -> (icmp eq/ne (and X, P2), 0)
6023 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6024 // -> (icmp eq/ne (and X, P2), P2)
6025 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
6026 *IsZero ? A
6027 : ConstantInt::getNullValue(A->getType()));
6028 }
6029
6030 return nullptr;
6031}
6032
6034 ICmpInst::Predicate Pred = ICmp.getPredicate();
6035 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
6036
6037 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
6038 // The trunc masks high bits while the compare may effectively mask low bits.
6039 Value *X;
6040 const APInt *C;
6041 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
6042 return nullptr;
6043
6044 // This matches patterns corresponding to tests of the signbit as well as:
6045 // (trunc X) pred C2 --> (X & Mask) == C
6046 if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*WithTrunc=*/true,
6047 /*AllowNonZeroC=*/true)) {
6048 Value *And = Builder.CreateAnd(Res->X, Res->Mask);
6049 Constant *C = ConstantInt::get(Res->X->getType(), Res->C);
6050 return new ICmpInst(Res->Pred, And, C);
6051 }
6052
6053 unsigned SrcBits = X->getType()->getScalarSizeInBits();
6054 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
6055 if (II->getIntrinsicID() == Intrinsic::cttz ||
6056 II->getIntrinsicID() == Intrinsic::ctlz) {
6057 unsigned MaxRet = SrcBits;
6058 // If the "is_zero_poison" argument is set, then we know at least
6059 // one bit is set in the input, so the result is always at least one
6060 // less than the full bitwidth of that input.
6061 if (match(II->getArgOperand(1), m_One()))
6062 MaxRet--;
6063
6064 // Make sure the destination is wide enough to hold the largest output of
6065 // the intrinsic.
6066 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
6067 if (Instruction *I =
6068 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
6069 return I;
6070 }
6071 }
6072
6073 return nullptr;
6074}
6075
6077 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
6078 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
6079 Value *X;
6080 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
6081 return nullptr;
6082
6083 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
6084 bool IsSignedCmp = ICmp.isSigned();
6085
6086 // icmp Pred (ext X), (ext Y)
6087 Value *Y;
6088 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
6089 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
6090 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
6091
6092 if (IsZext0 != IsZext1) {
6093 // If X and Y and both i1
6094 // (icmp eq/ne (zext X) (sext Y))
6095 // eq -> (icmp eq (or X, Y), 0)
6096 // ne -> (icmp ne (or X, Y), 0)
6097 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
6098 Y->getType()->isIntOrIntVectorTy(1))
6099 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
6100 Constant::getNullValue(X->getType()));
6101
6102 // If we have mismatched casts and zext has the nneg flag, we can
6103 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
6104
6105 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
6106 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
6107
6108 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
6109 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
6110
6111 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
6112 IsSignedExt = true;
6113 else
6114 return nullptr;
6115 }
6116
6117 // Not an extension from the same type?
6118 Type *XTy = X->getType(), *YTy = Y->getType();
6119 if (XTy != YTy) {
6120 // One of the casts must have one use because we are creating a new cast.
6121 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
6122 return nullptr;
6123 // Extend the narrower operand to the type of the wider operand.
6124 CastInst::CastOps CastOpcode =
6125 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
6126 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
6127 X = Builder.CreateCast(CastOpcode, X, YTy);
6128 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6129 Y = Builder.CreateCast(CastOpcode, Y, XTy);
6130 else
6131 return nullptr;
6132 }
6133
6134 // (zext X) == (zext Y) --> X == Y
6135 // (sext X) == (sext Y) --> X == Y
6136 if (ICmp.isEquality())
6137 return new ICmpInst(ICmp.getPredicate(), X, Y);
6138
6139 // A signed comparison of sign extended values simplifies into a
6140 // signed comparison.
6141 if (IsSignedCmp && IsSignedExt)
6142 return new ICmpInst(ICmp.getPredicate(), X, Y);
6143
6144 // The other three cases all fold into an unsigned comparison.
6145 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6146 }
6147
6148 // Below here, we are only folding a compare with constant.
6149 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
6150 if (!C)
6151 return nullptr;
6152
6153 // If a lossless truncate is possible...
6154 Type *SrcTy = CastOp0->getSrcTy();
6155 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
6156 if (Res) {
6157 if (ICmp.isEquality())
6158 return new ICmpInst(ICmp.getPredicate(), X, Res);
6159
6160 // A signed comparison of sign extended values simplifies into a
6161 // signed comparison.
6162 if (IsSignedExt && IsSignedCmp)
6163 return new ICmpInst(ICmp.getPredicate(), X, Res);
6164
6165 // The other three cases all fold into an unsigned comparison.
6166 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6167 }
6168
6169 // The re-extended constant changed, partly changed (in the case of a vector),
6170 // or could not be determined to be equal (in the case of a constant
6171 // expression), so the constant cannot be represented in the shorter type.
6172 // All the cases that fold to true or false will have already been handled
6173 // by simplifyICmpInst, so only deal with the tricky case.
6174 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
6175 return nullptr;
6176
6177 // Is source op positive?
6178 // icmp ult (sext X), C --> icmp sgt X, -1
6179 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6181
6182 // Is source op negative?
6183 // icmp ugt (sext X), C --> icmp slt X, 0
6184 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6186}
6187
6188/// Handle icmp (cast x), (cast or constant).
6190 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6191 // icmp compares only pointer's value.
6192 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6193 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
6194 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
6195 if (SimplifiedOp0 || SimplifiedOp1)
6196 return new ICmpInst(ICmp.getPredicate(),
6197 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
6198 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
6199
6200 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
6201 if (!CastOp0)
6202 return nullptr;
6203 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
6204 return nullptr;
6205
6206 Value *Op0Src = CastOp0->getOperand(0);
6207 Type *SrcTy = CastOp0->getSrcTy();
6208 Type *DestTy = CastOp0->getDestTy();
6209
6210 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6211 // integer type is the same size as the pointer type.
6212 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6213 if (isa<VectorType>(PtrTy)) {
6214 PtrTy = cast<VectorType>(PtrTy)->getElementType();
6215 IntTy = cast<VectorType>(IntTy)->getElementType();
6216 }
6217 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
6218 };
6219 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6220 CompatibleSizes(SrcTy, DestTy)) {
6221 Value *NewOp1 = nullptr;
6222 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
6223 Value *PtrSrc = PtrToIntOp1->getOperand(0);
6224 if (PtrSrc->getType() == Op0Src->getType())
6225 NewOp1 = PtrToIntOp1->getOperand(0);
6226 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6227 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
6228 }
6229
6230 if (NewOp1)
6231 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6232 }
6233
6234 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6235 if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6236 CompatibleSizes(DestTy, SrcTy)) {
6237 Value *NewOp1 = nullptr;
6238 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
6239 Value *IntSrc = IntToPtrOp1->getOperand(0);
6240 if (IntSrc->getType() == Op0Src->getType())
6241 NewOp1 = IntToPtrOp1->getOperand(0);
6242 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6243 NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
6244 }
6245
6246 if (NewOp1)
6247 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6248 }
6249
6250 if (Instruction *R = foldICmpWithTrunc(ICmp))
6251 return R;
6252
6253 return foldICmpWithZextOrSext(ICmp);
6254}
6255
6257 bool IsSigned) {
6258 switch (BinaryOp) {
6259 default:
6260 llvm_unreachable("Unsupported binary op");
6261 case Instruction::Add:
6262 case Instruction::Sub:
6263 return match(RHS, m_Zero());
6264 case Instruction::Mul:
6265 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
6266 match(RHS, m_One());
6267 }
6268}
6269
6272 bool IsSigned, Value *LHS, Value *RHS,
6273 Instruction *CxtI) const {
6274 switch (BinaryOp) {
6275 default:
6276 llvm_unreachable("Unsupported binary op");
6277 case Instruction::Add:
6278 if (IsSigned)
6279 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6280 else
6281 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6282 case Instruction::Sub:
6283 if (IsSigned)
6284 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6285 else
6286 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6287 case Instruction::Mul:
6288 if (IsSigned)
6289 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6290 else
6291 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6292 }
6293}
6294
6295bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6296 bool IsSigned, Value *LHS,
6297 Value *RHS, Instruction &OrigI,
6298 Value *&Result,
6299 Constant *&Overflow) {
6300 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
6301 std::swap(LHS, RHS);
6302
6303 // If the overflow check was an add followed by a compare, the insertion point
6304 // may be pointing to the compare. We want to insert the new instructions
6305 // before the add in case there are uses of the add between the add and the
6306 // compare.
6307 Builder.SetInsertPoint(&OrigI);
6308
6309 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
6310 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
6311 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
6312
6313 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6314 Result = LHS;
6315 Overflow = ConstantInt::getFalse(OverflowTy);
6316 return true;
6317 }
6318
6319 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
6321 return false;
6324 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6325 Result->takeName(&OrigI);
6326 Overflow = ConstantInt::getTrue(OverflowTy);
6327 return true;
6329 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6330 Result->takeName(&OrigI);
6331 Overflow = ConstantInt::getFalse(OverflowTy);
6332 if (auto *Inst = dyn_cast<Instruction>(Result)) {
6333 if (IsSigned)
6334 Inst->setHasNoSignedWrap();
6335 else
6336 Inst->setHasNoUnsignedWrap();
6337 }
6338 return true;
6339 }
6340
6341 llvm_unreachable("Unexpected overflow result");
6342}
6343
6344/// Recognize and process idiom involving test for multiplication
6345/// overflow.
6346///
6347/// The caller has matched a pattern of the form:
6348/// I = cmp u (mul(zext A, zext B), V
6349/// The function checks if this is a test for overflow and if so replaces
6350/// multiplication with call to 'mul.with.overflow' intrinsic.
6351///
6352/// \param I Compare instruction.
6353/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6354/// the compare instruction. Must be of integer type.
6355/// \param OtherVal The other argument of compare instruction.
6356/// \returns Instruction which must replace the compare instruction, NULL if no
6357/// replacement required.
6359 const APInt *OtherVal,
6360 InstCombinerImpl &IC) {
6361 // Don't bother doing this transformation for pointers, don't do it for
6362 // vectors.
6363 if (!isa<IntegerType>(MulVal->getType()))
6364 return nullptr;
6365
6366 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6367 if (!MulInstr)
6368 return nullptr;
6369 assert(MulInstr->getOpcode() == Instruction::Mul);
6370
6371 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6372 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6373 assert(LHS->getOpcode() == Instruction::ZExt);
6374 assert(RHS->getOpcode() == Instruction::ZExt);
6375 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6376
6377 // Calculate type and width of the result produced by mul.with.overflow.
6378 Type *TyA = A->getType(), *TyB = B->getType();
6379 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6380 WidthB = TyB->getPrimitiveSizeInBits();
6381 unsigned MulWidth;
6382 Type *MulType;
6383 if (WidthB > WidthA) {
6384 MulWidth = WidthB;
6385 MulType = TyB;
6386 } else {
6387 MulWidth = WidthA;
6388 MulType = TyA;
6389 }
6390
6391 // In order to replace the original mul with a narrower mul.with.overflow,
6392 // all uses must ignore upper bits of the product. The number of used low
6393 // bits must be not greater than the width of mul.with.overflow.
6394 if (MulVal->hasNUsesOrMore(2))
6395 for (User *U : MulVal->users()) {
6396 if (U == &I)
6397 continue;
6398 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6399 // Check if truncation ignores bits above MulWidth.
6400 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6401 if (TruncWidth > MulWidth)
6402 return nullptr;
6403 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6404 // Check if AND ignores bits above MulWidth.
6405 if (BO->getOpcode() != Instruction::And)
6406 return nullptr;
6407 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6408 const APInt &CVal = CI->getValue();
6409 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6410 return nullptr;
6411 } else {
6412 // In this case we could have the operand of the binary operation
6413 // being defined in another block, and performing the replacement
6414 // could break the dominance relation.
6415 return nullptr;
6416 }
6417 } else {
6418 // Other uses prohibit this transformation.
6419 return nullptr;
6420 }
6421 }
6422
6423 // Recognize patterns
6424 switch (I.getPredicate()) {
6425 case ICmpInst::ICMP_UGT: {
6426 // Recognize pattern:
6427 // mulval = mul(zext A, zext B)
6428 // cmp ugt mulval, max
6429 APInt MaxVal = APInt::getMaxValue(MulWidth);
6430 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6431 if (MaxVal.eq(*OtherVal))
6432 break; // Recognized
6433 return nullptr;
6434 }
6435
6436 case ICmpInst::ICMP_ULT: {
6437 // Recognize pattern:
6438 // mulval = mul(zext A, zext B)
6439 // cmp ule mulval, max + 1
6440 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6441 if (MaxVal.eq(*OtherVal))
6442 break; // Recognized
6443 return nullptr;
6444 }
6445
6446 default:
6447 return nullptr;
6448 }
6449
6450 InstCombiner::BuilderTy &Builder = IC.Builder;
6451 Builder.SetInsertPoint(MulInstr);
6452
6453 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6454 Value *MulA = A, *MulB = B;
6455 if (WidthA < MulWidth)
6456 MulA = Builder.CreateZExt(A, MulType);
6457 if (WidthB < MulWidth)
6458 MulB = Builder.CreateZExt(B, MulType);
6459 CallInst *Call =
6460 Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, MulType,
6461 {MulA, MulB}, /*FMFSource=*/nullptr, "umul");
6462 IC.addToWorklist(MulInstr);
6463
6464 // If there are uses of mul result other than the comparison, we know that
6465 // they are truncation or binary AND. Change them to use result of
6466 // mul.with.overflow and adjust properly mask/size.
6467 if (MulVal->hasNUsesOrMore(2)) {
6468 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6469 for (User *U : make_early_inc_range(MulVal->users())) {
6470 if (U == &I)
6471 continue;
6472 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6473 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6474 IC.replaceInstUsesWith(*TI, Mul);
6475 else
6476 TI->setOperand(0, Mul);
6477 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6478 assert(BO->getOpcode() == Instruction::And);
6479 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6480 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6481 APInt ShortMask = CI->getValue().trunc(MulWidth);
6482 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6483 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6484 IC.replaceInstUsesWith(*BO, Zext);
6485 } else {
6486 llvm_unreachable("Unexpected Binary operation");
6487 }
6488 IC.addToWorklist(cast<Instruction>(U));
6489 }
6490 }
6491
6492 // The original icmp gets replaced with the overflow value, maybe inverted
6493 // depending on predicate.
6494 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6495 Value *Res = Builder.CreateExtractValue(Call, 1);
6496 return BinaryOperator::CreateNot(Res);
6497 }
6498
6499 return ExtractValueInst::Create(Call, 1);
6500}
6501
6502/// When performing a comparison against a constant, it is possible that not all
6503/// the bits in the LHS are demanded. This helper method computes the mask that
6504/// IS demanded.
6506 const APInt *RHS;
6507 if (!match(I.getOperand(1), m_APInt(RHS)))
6509
6510 // If this is a normal comparison, it demands all bits. If it is a sign bit
6511 // comparison, it only demands the sign bit.
6512 bool UnusedBit;
6513 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6515
6516 switch (I.getPredicate()) {
6517 // For a UGT comparison, we don't care about any bits that
6518 // correspond to the trailing ones of the comparand. The value of these
6519 // bits doesn't impact the outcome of the comparison, because any value
6520 // greater than the RHS must differ in a bit higher than these due to carry.
6521 case ICmpInst::ICMP_UGT:
6522 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6523
6524 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6525 // Any value less than the RHS must differ in a higher bit because of carries.
6526 case ICmpInst::ICMP_ULT:
6527 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6528
6529 default:
6531 }
6532}
6533
6534/// Check that one use is in the same block as the definition and all
6535/// other uses are in blocks dominated by a given block.
6536///
6537/// \param DI Definition
6538/// \param UI Use
6539/// \param DB Block that must dominate all uses of \p DI outside
6540/// the parent block
6541/// \return true when \p UI is the only use of \p DI in the parent block
6542/// and all other uses of \p DI are in blocks dominated by \p DB.
6543///
6545 const Instruction *UI,
6546 const BasicBlock *DB) const {
6547 assert(DI && UI && "Instruction not defined\n");
6548 // Ignore incomplete definitions.
6549 if (!DI->getParent())
6550 return false;
6551 // DI and UI must be in the same block.
6552 if (DI->getParent() != UI->getParent())
6553 return false;
6554 // Protect from self-referencing blocks.
6555 if (DI->getParent() == DB)
6556 return false;
6557 for (const User *U : DI->users()) {
6558 auto *Usr = cast<Instruction>(U);
6559 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6560 return false;
6561 }
6562 return true;
6563}
6564
6565/// Return true when the instruction sequence within a block is select-cmp-br.
6566static bool isChainSelectCmpBranch(const SelectInst *SI) {
6567 const BasicBlock *BB = SI->getParent();
6568 if (!BB)
6569 return false;
6570 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
6571 if (!BI || BI->getNumSuccessors() != 2)
6572 return false;
6573 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6574 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6575 return false;
6576 return true;
6577}
6578
6579/// True when a select result is replaced by one of its operands
6580/// in select-icmp sequence. This will eventually result in the elimination
6581/// of the select.
6582///
6583/// \param SI Select instruction
6584/// \param Icmp Compare instruction
6585/// \param SIOpd Operand that replaces the select
6586///
6587/// Notes:
6588/// - The replacement is global and requires dominator information
6589/// - The caller is responsible for the actual replacement
6590///
6591/// Example:
6592///
6593/// entry:
6594/// %4 = select i1 %3, %C* %0, %C* null
6595/// %5 = icmp eq %C* %4, null
6596/// br i1 %5, label %9, label %7
6597/// ...
6598/// ; <label>:7 ; preds = %entry
6599/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6600/// ...
6601///
6602/// can be transformed to
6603///
6604/// %5 = icmp eq %C* %0, null
6605/// %6 = select i1 %3, i1 %5, i1 true
6606/// br i1 %6, label %9, label %7
6607/// ...
6608/// ; <label>:7 ; preds = %entry
6609/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6610///
6611/// Similar when the first operand of the select is a constant or/and
6612/// the compare is for not equal rather than equal.
6613///
6614/// NOTE: The function is only called when the select and compare constants
6615/// are equal, the optimization can work only for EQ predicates. This is not a
6616/// major restriction since a NE compare should be 'normalized' to an equal
6617/// compare, which usually happens in the combiner and test case
6618/// select-cmp-br.ll checks for it.
6620 const ICmpInst *Icmp,
6621 const unsigned SIOpd) {
6622 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6624 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6625 // The check for the single predecessor is not the best that can be
6626 // done. But it protects efficiently against cases like when SI's
6627 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6628 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6629 // replaced can be reached on either path. So the uniqueness check
6630 // guarantees that the path all uses of SI (outside SI's parent) are on
6631 // is disjoint from all other paths out of SI. But that information
6632 // is more expensive to compute, and the trade-off here is in favor
6633 // of compile-time. It should also be noticed that we check for a single
6634 // predecessor and not only uniqueness. This to handle the situation when
6635 // Succ and Succ1 points to the same basic block.
6636 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6637 NumSel++;
6638 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6639 return true;
6640 }
6641 }
6642 return false;
6643}
6644
6645/// Try to fold the comparison based on range information we can get by checking
6646/// whether bits are known to be zero or one in the inputs.
6648 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6649 Type *Ty = Op0->getType();
6650 ICmpInst::Predicate Pred = I.getPredicate();
6651
6652 // Get scalar or pointer size.
6653 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6654 ? Ty->getScalarSizeInBits()
6656
6657 if (!BitWidth)
6658 return nullptr;
6659
6660 KnownBits Op0Known(BitWidth);
6661 KnownBits Op1Known(BitWidth);
6662
6663 {
6664 // Don't use dominating conditions when folding icmp using known bits. This
6665 // may convert signed into unsigned predicates in ways that other passes
6666 // (especially IndVarSimplify) may not be able to reliably undo.
6669 Op0Known, /*Depth=*/0, Q))
6670 return &I;
6671
6673 /*Depth=*/0, Q))
6674 return &I;
6675 }
6676
6677 if (!isa<Constant>(Op0) && Op0Known.isConstant())
6678 return new ICmpInst(
6679 Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1);
6680 if (!isa<Constant>(Op1) && Op1Known.isConstant())
6681 return new ICmpInst(
6682 Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Known.getConstant()));
6683
6684 if (std::optional<bool> Res = ICmpInst::compare(Op0Known, Op1Known, Pred))
6685 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *Res));
6686
6687 // Given the known and unknown bits, compute a range that the LHS could be
6688 // in. Compute the Min, Max and RHS values based on the known bits. For the
6689 // EQ and NE we use unsigned values.
6690 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6691 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6692 if (I.isSigned()) {
6693 Op0Min = Op0Known.getSignedMinValue();
6694 Op0Max = Op0Known.getSignedMaxValue();
6695 Op1Min = Op1Known.getSignedMinValue();
6696 Op1Max = Op1Known.getSignedMaxValue();
6697 } else {
6698 Op0Min = Op0Known.getMinValue();
6699 Op0Max = Op0Known.getMaxValue();
6700 Op1Min = Op1Known.getMinValue();
6701 Op1Max = Op1Known.getMaxValue();
6702 }
6703
6704 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6705 // min/max canonical compare with some other compare. That could lead to
6706 // conflict with select canonicalization and infinite looping.
6707 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6708 auto isMinMaxCmp = [&](Instruction &Cmp) {
6709 if (!Cmp.hasOneUse())
6710 return false;
6711 Value *A, *B;
6712 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6714 return false;
6715 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6716 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6717 };
6718 if (!isMinMaxCmp(I)) {
6719 switch (Pred) {
6720 default:
6721 break;
6722 case ICmpInst::ICMP_ULT: {
6723 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6724 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6725 const APInt *CmpC;
6726 if (match(Op1, m_APInt(CmpC))) {
6727 // A <u C -> A == C-1 if min(A)+1 == C
6728 if (*CmpC == Op0Min + 1)
6729 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6730 ConstantInt::get(Op1->getType(), *CmpC - 1));
6731 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6732 // exceeds the log2 of C.
6733 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6734 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6735 Constant::getNullValue(Op1->getType()));
6736 }
6737 break;
6738 }
6739 case ICmpInst::ICMP_UGT: {
6740 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6741 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6742 const APInt *CmpC;
6743 if (match(Op1, m_APInt(CmpC))) {
6744 // A >u C -> A == C+1 if max(a)-1 == C
6745 if (*CmpC == Op0Max - 1)
6746 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6747 ConstantInt::get(Op1->getType(), *CmpC + 1));
6748 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6749 // exceeds the log2 of C.
6750 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6751 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6752 Constant::getNullValue(Op1->getType()));
6753 }
6754 break;
6755 }
6756 case ICmpInst::ICMP_SLT: {
6757 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6758 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6759 const APInt *CmpC;
6760 if (match(Op1, m_APInt(CmpC))) {
6761 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6762 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6763 ConstantInt::get(Op1->getType(), *CmpC - 1));
6764 }
6765 break;
6766 }
6767 case ICmpInst::ICMP_SGT: {
6768 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6769 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6770 const APInt *CmpC;
6771 if (match(Op1, m_APInt(CmpC))) {
6772 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6773 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6774 ConstantInt::get(Op1->getType(), *CmpC + 1));
6775 }
6776 break;
6777 }
6778 }
6779 }
6780
6781 // Based on the range information we know about the LHS, see if we can
6782 // simplify this comparison. For example, (x&4) < 8 is always true.
6783 switch (Pred) {
6784 default:
6785 break;
6786 case ICmpInst::ICMP_EQ:
6787 case ICmpInst::ICMP_NE: {
6788 // If all bits are known zero except for one, then we know at most one bit
6789 // is set. If the comparison is against zero, then this is a check to see if
6790 // *that* bit is set.
6791 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
6792 if (Op1Known.isZero()) {
6793 // If the LHS is an AND with the same constant, look through it.
6794 Value *LHS = nullptr;
6795 const APInt *LHSC;
6796 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
6797 *LHSC != Op0KnownZeroInverted)
6798 LHS = Op0;
6799
6800 Value *X;
6801 const APInt *C1;
6802 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
6803 Type *XTy = X->getType();
6804 unsigned Log2C1 = C1->countr_zero();
6805 APInt C2 = Op0KnownZeroInverted;
6806 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
6807 if (C2Pow2.isPowerOf2()) {
6808 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
6809 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
6810 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
6811 unsigned Log2C2 = C2Pow2.countr_zero();
6812 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
6813 auto NewPred =
6815 return new ICmpInst(NewPred, X, CmpC);
6816 }
6817 }
6818 }
6819
6820 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
6821 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
6822 (Op0Known & Op1Known) == Op0Known)
6823 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6824 ConstantInt::getNullValue(Op1->getType()));
6825 break;
6826 }
6827 case ICmpInst::ICMP_SGE:
6828 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
6829 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6830 break;
6831 case ICmpInst::ICMP_SLE:
6832 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
6833 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6834 break;
6835 case ICmpInst::ICMP_UGE:
6836 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
6837 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6838 break;
6839 case ICmpInst::ICMP_ULE:
6840 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
6841 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6842 break;
6843 }
6844
6845 // Turn a signed comparison into an unsigned one if both operands are known to
6846 // have the same sign. Set samesign if possible (except for equality
6847 // predicates).
6848 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&
6849 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
6850 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {
6851 I.setPredicate(I.getUnsignedPredicate());
6852 I.setSameSign();
6853 return &I;
6854 }
6855
6856 return nullptr;
6857}
6858
6859/// If one operand of an icmp is effectively a bool (value range of {0,1}),
6860/// then try to reduce patterns based on that limit.
6862 Value *X, *Y;
6863 CmpPredicate Pred;
6864
6865 // X must be 0 and bool must be true for "ULT":
6866 // X <u (zext i1 Y) --> (X == 0) & Y
6867 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
6868 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
6869 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
6870
6871 // X must be 0 or bool must be true for "ULE":
6872 // X <=u (sext i1 Y) --> (X == 0) | Y
6873 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
6874 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
6875 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
6876
6877 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
6878 CmpPredicate Pred1, Pred2;
6879 const APInt *C;
6880 Instruction *ExtI;
6881 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
6884 m_APInt(C)))))) &&
6885 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
6886 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
6887 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
6888 auto CreateRangeCheck = [&] {
6889 Value *CmpV1 =
6890 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
6891 Value *CmpV2 = Builder.CreateICmp(
6892 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
6894 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
6895 CmpV1, CmpV2);
6896 };
6897 if (C->isZero()) {
6898 if (Pred2 == ICmpInst::ICMP_EQ) {
6899 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
6900 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
6901 return replaceInstUsesWith(
6902 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6903 } else if (!IsSExt || HasOneUse) {
6904 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
6905 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
6906 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
6907 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X == -1
6908 return CreateRangeCheck();
6909 }
6910 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
6911 if (Pred2 == ICmpInst::ICMP_NE) {
6912 // icmp eq X, (zext (icmp ne X, 1)) --> false
6913 // icmp ne X, (zext (icmp ne X, 1)) --> true
6914 // icmp eq X, (sext (icmp ne X, -1)) --> false
6915 // icmp ne X, (sext (icmp ne X, -1)) --> true
6916 return replaceInstUsesWith(
6917 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6918 } else if (!IsSExt || HasOneUse) {
6919 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
6920 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
6921 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
6922 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
6923 return CreateRangeCheck();
6924 }
6925 } else {
6926 // when C != 0 && C != 1:
6927 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
6928 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
6929 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
6930 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
6931 // when C != 0 && C != -1:
6932 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
6933 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
6934 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
6935 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
6936 return ICmpInst::Create(
6937 Instruction::ICmp, Pred1, X,
6938 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
6939 ? (IsSExt ? -1 : 1)
6940 : 0));
6941 }
6942 }
6943
6944 return nullptr;
6945}
6946
6947/// If we have an icmp le or icmp ge instruction with a constant operand, turn
6948/// it into the appropriate icmp lt or icmp gt instruction. This transform
6949/// allows them to be folded in visitICmpInst.
6951 ICmpInst::Predicate Pred = I.getPredicate();
6952 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
6954 return nullptr;
6955
6956 Value *Op0 = I.getOperand(0);
6957 Value *Op1 = I.getOperand(1);
6958 auto *Op1C = dyn_cast<Constant>(Op1);
6959 if (!Op1C)
6960 return nullptr;
6961
6962 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
6963 if (!FlippedStrictness)
6964 return nullptr;
6965
6966 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
6967}
6968
6969/// If we have a comparison with a non-canonical predicate, if we can update
6970/// all the users, invert the predicate and adjust all the users.
6972 // Is the predicate already canonical?
6973 CmpInst::Predicate Pred = I.getPredicate();
6975 return nullptr;
6976
6977 // Can all users be adjusted to predicate inversion?
6978 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
6979 return nullptr;
6980
6981 // Ok, we can canonicalize comparison!
6982 // Let's first invert the comparison's predicate.
6983 I.setPredicate(CmpInst::getInversePredicate(Pred));
6984 I.setName(I.getName() + ".not");
6985
6986 // And, adapt users.
6988
6989 return &I;
6990}
6991
6992/// Integer compare with boolean values can always be turned into bitwise ops.
6994 InstCombiner::BuilderTy &Builder) {
6995 Value *A = I.getOperand(0), *B = I.getOperand(1);
6996 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
6997
6998 // A boolean compared to true/false can be simplified to Op0/true/false in
6999 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
7000 // Cases not handled by InstSimplify are always 'not' of Op0.
7001 if (match(B, m_Zero())) {
7002 switch (I.getPredicate()) {
7003 case CmpInst::ICMP_EQ: // A == 0 -> !A
7004 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
7005 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
7007 default:
7008 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7009 }
7010 } else if (match(B, m_One())) {
7011 switch (I.getPredicate()) {
7012 case CmpInst::ICMP_NE: // A != 1 -> !A
7013 case CmpInst::ICMP_ULT: // A <u 1 -> !A
7014 case CmpInst::ICMP_SGT: // A >s -1 -> !A
7016 default:
7017 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7018 }
7019 }
7020
7021 switch (I.getPredicate()) {
7022 default:
7023 llvm_unreachable("Invalid icmp instruction!");
7024 case ICmpInst::ICMP_EQ:
7025 // icmp eq i1 A, B -> ~(A ^ B)
7026 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
7027
7028 case ICmpInst::ICMP_NE:
7029 // icmp ne i1 A, B -> A ^ B
7030 return BinaryOperator::CreateXor(A, B);
7031
7032 case ICmpInst::ICMP_UGT:
7033 // icmp ugt -> icmp ult
7034 std::swap(A, B);
7035 [[fallthrough]];
7036 case ICmpInst::ICMP_ULT:
7037 // icmp ult i1 A, B -> ~A & B
7038 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
7039
7040 case ICmpInst::ICMP_SGT:
7041 // icmp sgt -> icmp slt
7042 std::swap(A, B);
7043 [[fallthrough]];
7044 case ICmpInst::ICMP_SLT:
7045 // icmp slt i1 A, B -> A & ~B
7046 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
7047
7048 case ICmpInst::ICMP_UGE:
7049 // icmp uge -> icmp ule
7050 std::swap(A, B);
7051 [[fallthrough]];
7052 case ICmpInst::ICMP_ULE:
7053 // icmp ule i1 A, B -> ~A | B
7054 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
7055
7056 case ICmpInst::ICMP_SGE:
7057 // icmp sge -> icmp sle
7058 std::swap(A, B);
7059 [[fallthrough]];
7060 case ICmpInst::ICMP_SLE:
7061 // icmp sle i1 A, B -> A | ~B
7062 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
7063 }
7064}
7065
7066// Transform pattern like:
7067// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7068// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7069// Into:
7070// (X l>> Y) != 0
7071// (X l>> Y) == 0
7073 InstCombiner::BuilderTy &Builder) {
7074 CmpPredicate Pred, NewPred;
7075 Value *X, *Y;
7076 if (match(&Cmp,
7077 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
7078 switch (Pred) {
7079 case ICmpInst::ICMP_ULE:
7080 NewPred = ICmpInst::ICMP_NE;
7081 break;
7082 case ICmpInst::ICMP_UGT:
7083 NewPred = ICmpInst::ICMP_EQ;
7084 break;
7085 default:
7086 return nullptr;
7087 }
7088 } else if (match(&Cmp, m_c_ICmp(Pred,
7091 m_Add(m_Shl(m_One(), m_Value(Y)),
7092 m_AllOnes()))),
7093 m_Value(X)))) {
7094 // The variant with 'add' is not canonical, (the variant with 'not' is)
7095 // we only get it because it has extra uses, and can't be canonicalized,
7096
7097 switch (Pred) {
7098 case ICmpInst::ICMP_ULT:
7099 NewPred = ICmpInst::ICMP_NE;
7100 break;
7101 case ICmpInst::ICMP_UGE:
7102 NewPred = ICmpInst::ICMP_EQ;
7103 break;
7104 default:
7105 return nullptr;
7106 }
7107 } else
7108 return nullptr;
7109
7110 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
7111 Constant *Zero = Constant::getNullValue(NewX->getType());
7112 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
7113}
7114
7116 InstCombiner::BuilderTy &Builder) {
7117 const CmpInst::Predicate Pred = Cmp.getPredicate();
7118 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
7119 Value *V1, *V2;
7120
7121 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7122 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
7123 if (auto *I = dyn_cast<Instruction>(V))
7124 I->copyIRFlags(&Cmp);
7125 Module *M = Cmp.getModule();
7127 M, Intrinsic::vector_reverse, V->getType());
7128 return CallInst::Create(F, V);
7129 };
7130
7131 if (match(LHS, m_VecReverse(m_Value(V1)))) {
7132 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7133 if (match(RHS, m_VecReverse(m_Value(V2))) &&
7134 (LHS->hasOneUse() || RHS->hasOneUse()))
7135 return createCmpReverse(Pred, V1, V2);
7136
7137 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7138 if (LHS->hasOneUse() && isSplatValue(RHS))
7139 return createCmpReverse(Pred, V1, RHS);
7140 }
7141 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7142 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
7143 return createCmpReverse(Pred, LHS, V2);
7144
7145 ArrayRef<int> M;
7146 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
7147 return nullptr;
7148
7149 // If both arguments of the cmp are shuffles that use the same mask and
7150 // shuffle within a single vector, move the shuffle after the cmp:
7151 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7152 Type *V1Ty = V1->getType();
7153 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
7154 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7155 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
7156 return new ShuffleVectorInst(NewCmp, M);
7157 }
7158
7159 // Try to canonicalize compare with splatted operand and splat constant.
7160 // TODO: We could generalize this for more than splats. See/use the code in
7161 // InstCombiner::foldVectorBinop().
7162 Constant *C;
7163 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
7164 return nullptr;
7165
7166 // Length-changing splats are ok, so adjust the constants as needed:
7167 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7168 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7169 int MaskSplatIndex;
7170 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
7171 // We allow poison in matching, but this transform removes it for safety.
7172 // Demanded elements analysis should be able to recover some/all of that.
7173 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
7174 ScalarC);
7175 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7176 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
7177 return new ShuffleVectorInst(NewCmp, NewM);
7178 }
7179
7180 return nullptr;
7181}
7182
7183// extract(uadd.with.overflow(A, B), 0) ult A
7184// -> extract(uadd.with.overflow(A, B), 1)
7186 CmpInst::Predicate Pred = I.getPredicate();
7187 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7188
7189 Value *UAddOv;
7190 Value *A, *B;
7191 auto UAddOvResultPat = m_ExtractValue<0>(
7192 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));
7193 if (match(Op0, UAddOvResultPat) &&
7194 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7195 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
7196 (match(A, m_One()) || match(B, m_One()))) ||
7197 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
7198 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
7199 // extract(uadd.with.overflow(A, B), 0) < A
7200 // extract(uadd.with.overflow(A, 1), 0) == 0
7201 // extract(uadd.with.overflow(A, -1), 0) != -1
7202 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
7203 else if (match(Op1, UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&
7204 (Op0 == A || Op0 == B))
7205 // A > extract(uadd.with.overflow(A, B), 0)
7206 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
7207 else
7208 return nullptr;
7209
7210 return ExtractValueInst::Create(UAddOv, 1);
7211}
7212
7214 if (!I.getOperand(0)->getType()->isPointerTy() ||
7216 I.getParent()->getParent(),
7217 I.getOperand(0)->getType()->getPointerAddressSpace())) {
7218 return nullptr;
7219 }
7220 Instruction *Op;
7221 if (match(I.getOperand(0), m_Instruction(Op)) &&
7222 match(I.getOperand(1), m_Zero()) &&
7223 Op->isLaunderOrStripInvariantGroup()) {
7224 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7225 Op->getOperand(0), I.getOperand(1));
7226 }
7227 return nullptr;
7228}
7229
7230/// This function folds patterns produced by lowering of reduce idioms, such as
7231/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7232/// attempts to generate fewer number of scalar comparisons instead of vector
7233/// comparisons when possible.
7235 InstCombiner::BuilderTy &Builder,
7236 const DataLayout &DL) {
7237 if (I.getType()->isVectorTy())
7238 return nullptr;
7239 CmpPredicate OuterPred, InnerPred;
7240 Value *LHS, *RHS;
7241
7242 // Match lowering of @llvm.vector.reduce.and. Turn
7243 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7244 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7245 /// %res = icmp <pred> i8 %scalar_ne, 0
7246 ///
7247 /// into
7248 ///
7249 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7250 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7251 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7252 ///
7253 /// for <pred> in {ne, eq}.
7254 if (!match(&I, m_ICmp(OuterPred,
7256 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7257 m_Zero())))
7258 return nullptr;
7259 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7260 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7261 return nullptr;
7262 unsigned NumBits =
7263 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7264 // TODO: Relax this to "not wider than max legal integer type"?
7265 if (!DL.isLegalInteger(NumBits))
7266 return nullptr;
7267
7268 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7269 auto *ScalarTy = Builder.getIntNTy(NumBits);
7270 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7271 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7272 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7273 I.getName());
7274 }
7275
7276 return nullptr;
7277}
7278
7279// This helper will be called with icmp operands in both orders.
7281 Value *Op0, Value *Op1,
7282 ICmpInst &CxtI) {
7283 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7284 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7285 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7286 return NI;
7287
7288 if (auto *SI = dyn_cast<SelectInst>(Op0))
7289 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7290 return NI;
7291
7292 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0))
7293 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7294 return Res;
7295
7296 {
7297 Value *X;
7298 const APInt *C;
7299 // icmp X+Cst, X
7300 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7301 return foldICmpAddOpConst(X, *C, Pred);
7302 }
7303
7304 // abs(X) >= X --> true
7305 // abs(X) u<= X --> true
7306 // abs(X) < X --> false
7307 // abs(X) u> X --> false
7308 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7309 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7310 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7311 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7312 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7313 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7314 {
7315 Value *X;
7316 Constant *C;
7317 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
7318 match(Op1, m_Specific(X))) {
7319 Value *NullValue = Constant::getNullValue(X->getType());
7320 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7321 const APInt SMin =
7322 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7323 bool IsIntMinPosion = C->isAllOnesValue();
7324 switch (Pred) {
7325 case CmpInst::ICMP_ULE:
7326 case CmpInst::ICMP_SGE:
7327 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7328 case CmpInst::ICMP_UGT:
7329 case CmpInst::ICMP_SLT:
7331 case CmpInst::ICMP_UGE:
7332 case CmpInst::ICMP_SLE:
7333 case CmpInst::ICMP_EQ: {
7334 return replaceInstUsesWith(
7335 CxtI, IsIntMinPosion
7336 ? Builder.CreateICmpSGT(X, AllOnesValue)
7338 X, ConstantInt::get(X->getType(), SMin + 1)));
7339 }
7340 case CmpInst::ICMP_ULT:
7341 case CmpInst::ICMP_SGT:
7342 case CmpInst::ICMP_NE: {
7343 return replaceInstUsesWith(
7344 CxtI, IsIntMinPosion
7345 ? Builder.CreateICmpSLT(X, NullValue)
7347 X, ConstantInt::get(X->getType(), SMin)));
7348 }
7349 default:
7350 llvm_unreachable("Invalid predicate!");
7351 }
7352 }
7353 }
7354
7355 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7356 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7357 return replaceInstUsesWith(CxtI, V);
7358
7359 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7360 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7361 {
7362 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7363 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7365 }
7366
7367 if (!ICmpInst::isUnsigned(Pred) &&
7368 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7369 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7371 }
7372 }
7373
7374 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7375 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7376 {
7377 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7378 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7380 }
7381
7382 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7383 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7384 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7386 }
7387 }
7388
7389 return nullptr;
7390}
7391
7393 bool Changed = false;
7395 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7396 unsigned Op0Cplxity = getComplexity(Op0);
7397 unsigned Op1Cplxity = getComplexity(Op1);
7398
7399 /// Orders the operands of the compare so that they are listed from most
7400 /// complex to least complex. This puts constants before unary operators,
7401 /// before binary operators.
7402 if (Op0Cplxity < Op1Cplxity) {
7403 I.swapOperands();
7404 std::swap(Op0, Op1);
7405 Changed = true;
7406 }
7407
7408 if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
7409 return replaceInstUsesWith(I, V);
7410
7411 // Comparing -val or val with non-zero is the same as just comparing val
7412 // ie, abs(val) != 0 -> val != 0
7413 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7414 Value *Cond, *SelectTrue, *SelectFalse;
7415 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7416 m_Value(SelectFalse)))) {
7417 if (Value *V = dyn_castNegVal(SelectTrue)) {
7418 if (V == SelectFalse)
7419 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7420 } else if (Value *V = dyn_castNegVal(SelectFalse)) {
7421 if (V == SelectTrue)
7422 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7423 }
7424 }
7425 }
7426
7427 if (Op0->getType()->isIntOrIntVectorTy(1))
7429 return Res;
7430
7432 return Res;
7433
7435 return Res;
7436
7438 return Res;
7439
7441 return Res;
7442
7444 return Res;
7445
7447 return Res;
7448
7450 return Res;
7451
7452 // Test if the ICmpInst instruction is used exclusively by a select as
7453 // part of a minimum or maximum operation. If so, refrain from doing
7454 // any other folding. This helps out other analyses which understand
7455 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7456 // and CodeGen. And in this case, at least one of the comparison
7457 // operands has at least one user besides the compare (the select),
7458 // which would often largely negate the benefit of folding anyway.
7459 //
7460 // Do the same for the other patterns recognized by matchSelectPattern.
7461 if (I.hasOneUse())
7462 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7463 Value *A, *B;
7465 if (SPR.Flavor != SPF_UNKNOWN)
7466 return nullptr;
7467 }
7468
7469 // Do this after checking for min/max to prevent infinite looping.
7470 if (Instruction *Res = foldICmpWithZero(I))
7471 return Res;
7472
7473 // FIXME: We only do this after checking for min/max to prevent infinite
7474 // looping caused by a reverse canonicalization of these patterns for min/max.
7475 // FIXME: The organization of folds is a mess. These would naturally go into
7476 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7477 // down here after the min/max restriction.
7478 ICmpInst::Predicate Pred = I.getPredicate();
7479 const APInt *C;
7480 if (match(Op1, m_APInt(C))) {
7481 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7482 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7483 Constant *Zero = Constant::getNullValue(Op0->getType());
7484 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7485 }
7486
7487 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7488 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7490 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7491 }
7492 }
7493
7494 // The folds in here may rely on wrapping flags and special constants, so
7495 // they can break up min/max idioms in some cases but not seemingly similar
7496 // patterns.
7497 // FIXME: It may be possible to enhance select folding to make this
7498 // unnecessary. It may also be moot if we canonicalize to min/max
7499 // intrinsics.
7500 if (Instruction *Res = foldICmpBinOp(I, Q))
7501 return Res;
7502
7504 return Res;
7505
7506 // Try to match comparison as a sign bit test. Intentionally do this after
7507 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7508 if (Instruction *New = foldSignBitTest(I))
7509 return New;
7510
7512 return Res;
7513
7514 if (Instruction *Res = foldICmpCommutative(I.getCmpPredicate(), Op0, Op1, I))
7515 return Res;
7516 if (Instruction *Res =
7517 foldICmpCommutative(I.getSwappedCmpPredicate(), Op1, Op0, I))
7518 return Res;
7519
7520 if (I.isCommutative()) {
7521 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7522 replaceOperand(I, 0, Pair->first);
7523 replaceOperand(I, 1, Pair->second);
7524 return &I;
7525 }
7526 }
7527
7528 // In case of a comparison with two select instructions having the same
7529 // condition, check whether one of the resulting branches can be simplified.
7530 // If so, just compare the other branch and select the appropriate result.
7531 // For example:
7532 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7533 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7534 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7535 // The icmp will result false for the false value of selects and the result
7536 // will depend upon the comparison of true values of selects if %cmp is
7537 // true. Thus, transform this into:
7538 // %cmp = icmp slt i32 %y, %z
7539 // %sel = select i1 %cond, i1 %cmp, i1 false
7540 // This handles similar cases to transform.
7541 {
7542 Value *Cond, *A, *B, *C, *D;
7543 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7545 (Op0->hasOneUse() || Op1->hasOneUse())) {
7546 // Check whether comparison of TrueValues can be simplified
7547 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7548 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7549 return SelectInst::Create(Cond, Res, NewICMP);
7550 }
7551 // Check whether comparison of FalseValues can be simplified
7552 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7553 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7554 return SelectInst::Create(Cond, NewICMP, Res);
7555 }
7556 }
7557 }
7558
7559 // Try to optimize equality comparisons against alloca-based pointers.
7560 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7561 assert(Op1->getType()->isPointerTy() &&
7562 "Comparing pointer with non-pointer?");
7563 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7564 if (foldAllocaCmp(Alloca))
7565 return nullptr;
7566 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7567 if (foldAllocaCmp(Alloca))
7568 return nullptr;
7569 }
7570
7571 if (Instruction *Res = foldICmpBitCast(I))
7572 return Res;
7573
7574 // TODO: Hoist this above the min/max bailout.
7576 return R;
7577
7578 {
7579 Value *X, *Y;
7580 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7581 // and (X & ~Y) != 0 --> (X & Y) == 0
7582 // if A is a power of 2.
7583 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7584 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, 0, &I) &&
7585 I.isEquality())
7586 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7587 Op1);
7588
7589 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7590 if (Op0->getType()->isIntOrIntVectorTy()) {
7591 bool ConsumesOp0, ConsumesOp1;
7592 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7593 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7594 (ConsumesOp0 || ConsumesOp1)) {
7595 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7596 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7597 assert(InvOp0 && InvOp1 &&
7598 "Mismatch between isFreeToInvert and getFreelyInverted");
7599 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7600 }
7601 }
7602
7603 Instruction *AddI = nullptr;
7605 m_Instruction(AddI))) &&
7606 isa<IntegerType>(X->getType())) {
7607 Value *Result;
7608 Constant *Overflow;
7609 // m_UAddWithOverflow can match patterns that do not include an explicit
7610 // "add" instruction, so check the opcode of the matched op.
7611 if (AddI->getOpcode() == Instruction::Add &&
7612 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7613 Result, Overflow)) {
7614 replaceInstUsesWith(*AddI, Result);
7615 eraseInstFromFunction(*AddI);
7616 return replaceInstUsesWith(I, Overflow);
7617 }
7618 }
7619
7620 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7621 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7622 match(Op1, m_APInt(C))) {
7623 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7624 return R;
7625 }
7626
7627 // Signbit test folds
7628 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7629 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7630 Instruction *ExtI;
7631 if ((I.isUnsigned() || I.isEquality()) &&
7632 match(Op1,
7634 Y->getType()->getScalarSizeInBits() == 1 &&
7635 (Op0->hasOneUse() || Op1->hasOneUse())) {
7636 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7637 Instruction *ShiftI;
7638 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7640 OpWidth - 1))))) {
7641 unsigned ExtOpc = ExtI->getOpcode();
7642 unsigned ShiftOpc = ShiftI->getOpcode();
7643 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7644 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7645 Value *SLTZero =
7647 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7648 return replaceInstUsesWith(I, Cmp);
7649 }
7650 }
7651 }
7652 }
7653
7654 if (Instruction *Res = foldICmpEquality(I))
7655 return Res;
7656
7658 return Res;
7659
7660 if (Instruction *Res = foldICmpOfUAddOv(I))
7661 return Res;
7662
7663 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7664 // an i1 which indicates whether or not we successfully did the swap.
7665 //
7666 // Replace comparisons between the old value and the expected value with the
7667 // indicator that 'cmpxchg' returns.
7668 //
7669 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7670 // spuriously fail. In those cases, the old value may equal the expected
7671 // value but it is possible for the swap to not occur.
7672 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7673 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7674 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7675 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7676 !ACXI->isWeak())
7677 return ExtractValueInst::Create(ACXI, 1);
7678
7680 return Res;
7681
7682 if (I.getType()->isVectorTy())
7683 if (Instruction *Res = foldVectorCmp(I, Builder))
7684 return Res;
7685
7687 return Res;
7688
7690 return Res;
7691
7692 {
7693 Value *A;
7694 const APInt *C1, *C2;
7695 ICmpInst::Predicate Pred = I.getPredicate();
7696 if (ICmpInst::isEquality(Pred)) {
7697 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
7698 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
7699 if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) &&
7700 match(Op1, m_APInt(C2))) {
7701 Type *InputTy = A->getType();
7702 unsigned InputBitWidth = InputTy->getScalarSizeInBits();
7703 // c2 must be non-negative at the bitwidth of a.
7704 if (C2->getActiveBits() < InputBitWidth) {
7705 APInt TruncC1 = C1->trunc(InputBitWidth);
7706 // Check if there are 1s in C1 high bits of size InputBitWidth.
7707 if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth)))
7708 TruncC1.setBit(InputBitWidth - 1);
7709 Value *AndInst = Builder.CreateAnd(A, TruncC1);
7710 return new ICmpInst(
7711 Pred, AndInst,
7712 ConstantInt::get(InputTy, C2->trunc(InputBitWidth)));
7713 }
7714 }
7715 }
7716 }
7717
7718 return Changed ? &I : nullptr;
7719}
7720
7721/// Fold fcmp ([us]itofp x, cst) if possible.
7723 Instruction *LHSI,
7724 Constant *RHSC) {
7725 const APFloat *RHS;
7726 if (!match(RHSC, m_APFloat(RHS)))
7727 return nullptr;
7728
7729 // Get the width of the mantissa. We don't want to hack on conversions that
7730 // might lose information from the integer, e.g. "i64 -> float"
7731 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7732 if (MantissaWidth == -1)
7733 return nullptr; // Unknown.
7734
7735 Type *IntTy = LHSI->getOperand(0)->getType();
7736 unsigned IntWidth = IntTy->getScalarSizeInBits();
7737 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7738
7739 if (I.isEquality()) {
7740 FCmpInst::Predicate P = I.getPredicate();
7741 bool IsExact = false;
7742 APSInt RHSCvt(IntWidth, LHSUnsigned);
7743 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7744
7745 // If the floating point constant isn't an integer value, we know if we will
7746 // ever compare equal / not equal to it.
7747 if (!IsExact) {
7748 // TODO: Can never be -0.0 and other non-representable values
7749 APFloat RHSRoundInt(*RHS);
7751 if (*RHS != RHSRoundInt) {
7753 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7754
7756 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7757 }
7758 }
7759
7760 // TODO: If the constant is exactly representable, is it always OK to do
7761 // equality compares as integer?
7762 }
7763
7764 // Check to see that the input is converted from an integer type that is small
7765 // enough that preserves all bits. TODO: check here for "known" sign bits.
7766 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
7767
7768 // Following test does NOT adjust IntWidth downwards for signed inputs,
7769 // because the most negative value still requires all the mantissa bits
7770 // to distinguish it from one less than that value.
7771 if ((int)IntWidth > MantissaWidth) {
7772 // Conversion would lose accuracy. Check if loss can impact comparison.
7773 int Exp = ilogb(*RHS);
7774 if (Exp == APFloat::IEK_Inf) {
7775 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
7776 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
7777 // Conversion could create infinity.
7778 return nullptr;
7779 } else {
7780 // Note that if RHS is zero or NaN, then Exp is negative
7781 // and first condition is trivially false.
7782 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
7783 // Conversion could affect comparison.
7784 return nullptr;
7785 }
7786 }
7787
7788 // Otherwise, we can potentially simplify the comparison. We know that it
7789 // will always come through as an integer value and we know the constant is
7790 // not a NAN (it would have been previously simplified).
7791 assert(!RHS->isNaN() && "NaN comparison not already folded!");
7792
7794 switch (I.getPredicate()) {
7795 default:
7796 llvm_unreachable("Unexpected predicate!");
7797 case FCmpInst::FCMP_UEQ:
7798 case FCmpInst::FCMP_OEQ:
7799 Pred = ICmpInst::ICMP_EQ;
7800 break;
7801 case FCmpInst::FCMP_UGT:
7802 case FCmpInst::FCMP_OGT:
7803 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
7804 break;
7805 case FCmpInst::FCMP_UGE:
7806 case FCmpInst::FCMP_OGE:
7807 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
7808 break;
7809 case FCmpInst::FCMP_ULT:
7810 case FCmpInst::FCMP_OLT:
7811 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
7812 break;
7813 case FCmpInst::FCMP_ULE:
7814 case FCmpInst::FCMP_OLE:
7815 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
7816 break;
7817 case FCmpInst::FCMP_UNE:
7818 case FCmpInst::FCMP_ONE:
7819 Pred = ICmpInst::ICMP_NE;
7820 break;
7821 case FCmpInst::FCMP_ORD:
7822 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7823 case FCmpInst::FCMP_UNO:
7824 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7825 }
7826
7827 // Now we know that the APFloat is a normal number, zero or inf.
7828
7829 // See if the FP constant is too large for the integer. For example,
7830 // comparing an i8 to 300.0.
7831 if (!LHSUnsigned) {
7832 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
7833 // and large values.
7834 APFloat SMax(RHS->getSemantics());
7835 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
7837 if (SMax < *RHS) { // smax < 13123.0
7838 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
7839 Pred == ICmpInst::ICMP_SLE)
7840 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7841 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7842 }
7843 } else {
7844 // If the RHS value is > UnsignedMax, fold the comparison. This handles
7845 // +INF and large values.
7846 APFloat UMax(RHS->getSemantics());
7847 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
7849 if (UMax < *RHS) { // umax < 13123.0
7850 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
7851 Pred == ICmpInst::ICMP_ULE)
7852 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7853 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7854 }
7855 }
7856
7857 if (!LHSUnsigned) {
7858 // See if the RHS value is < SignedMin.
7859 APFloat SMin(RHS->getSemantics());
7860 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
7862 if (SMin > *RHS) { // smin > 12312.0
7863 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
7864 Pred == ICmpInst::ICMP_SGE)
7865 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7866 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7867 }
7868 } else {
7869 // See if the RHS value is < UnsignedMin.
7870 APFloat UMin(RHS->getSemantics());
7871 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
7873 if (UMin > *RHS) { // umin > 12312.0
7874 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
7875 Pred == ICmpInst::ICMP_UGE)
7876 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7877 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7878 }
7879 }
7880
7881 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
7882 // [0, UMAX], but it may still be fractional. Check whether this is the case
7883 // using the IsExact flag.
7884 // Don't do this for zero, because -0.0 is not fractional.
7885 APSInt RHSInt(IntWidth, LHSUnsigned);
7886 bool IsExact;
7887 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
7888 if (!RHS->isZero()) {
7889 if (!IsExact) {
7890 // If we had a comparison against a fractional value, we have to adjust
7891 // the compare predicate and sometimes the value. RHSC is rounded towards
7892 // zero at this point.
7893 switch (Pred) {
7894 default:
7895 llvm_unreachable("Unexpected integer comparison!");
7896 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
7897 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7898 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
7899 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7900 case ICmpInst::ICMP_ULE:
7901 // (float)int <= 4.4 --> int <= 4
7902 // (float)int <= -4.4 --> false
7903 if (RHS->isNegative())
7904 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7905 break;
7906 case ICmpInst::ICMP_SLE:
7907 // (float)int <= 4.4 --> int <= 4
7908 // (float)int <= -4.4 --> int < -4
7909 if (RHS->isNegative())
7910 Pred = ICmpInst::ICMP_SLT;
7911 break;
7912 case ICmpInst::ICMP_ULT:
7913 // (float)int < -4.4 --> false
7914 // (float)int < 4.4 --> int <= 4
7915 if (RHS->isNegative())
7916 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7917 Pred = ICmpInst::ICMP_ULE;
7918 break;
7919 case ICmpInst::ICMP_SLT:
7920 // (float)int < -4.4 --> int < -4
7921 // (float)int < 4.4 --> int <= 4
7922 if (!RHS->isNegative())
7923 Pred = ICmpInst::ICMP_SLE;
7924 break;
7925 case ICmpInst::ICMP_UGT:
7926 // (float)int > 4.4 --> int > 4
7927 // (float)int > -4.4 --> true
7928 if (RHS->isNegative())
7929 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7930 break;
7931 case ICmpInst::ICMP_SGT:
7932 // (float)int > 4.4 --> int > 4
7933 // (float)int > -4.4 --> int >= -4
7934 if (RHS->isNegative())
7935 Pred = ICmpInst::ICMP_SGE;
7936 break;
7937 case ICmpInst::ICMP_UGE:
7938 // (float)int >= -4.4 --> true
7939 // (float)int >= 4.4 --> int > 4
7940 if (RHS->isNegative())
7941 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7942 Pred = ICmpInst::ICMP_UGT;
7943 break;
7944 case ICmpInst::ICMP_SGE:
7945 // (float)int >= -4.4 --> int >= -4
7946 // (float)int >= 4.4 --> int > 4
7947 if (!RHS->isNegative())
7948 Pred = ICmpInst::ICMP_SGT;
7949 break;
7950 }
7951 }
7952 }
7953
7954 // Lower this FP comparison into an appropriate integer version of the
7955 // comparison.
7956 return new ICmpInst(Pred, LHSI->getOperand(0),
7957 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
7958}
7959
7960/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
7962 Constant *RHSC) {
7963 // When C is not 0.0 and infinities are not allowed:
7964 // (C / X) < 0.0 is a sign-bit test of X
7965 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
7966 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
7967 //
7968 // Proof:
7969 // Multiply (C / X) < 0.0 by X * X / C.
7970 // - X is non zero, if it is the flag 'ninf' is violated.
7971 // - C defines the sign of X * X * C. Thus it also defines whether to swap
7972 // the predicate. C is also non zero by definition.
7973 //
7974 // Thus X * X / C is non zero and the transformation is valid. [qed]
7975
7976 FCmpInst::Predicate Pred = I.getPredicate();
7977
7978 // Check that predicates are valid.
7979 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
7980 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
7981 return nullptr;
7982
7983 // Check that RHS operand is zero.
7984 if (!match(RHSC, m_AnyZeroFP()))
7985 return nullptr;
7986
7987 // Check fastmath flags ('ninf').
7988 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
7989 return nullptr;
7990
7991 // Check the properties of the dividend. It must not be zero to avoid a
7992 // division by zero (see Proof).
7993 const APFloat *C;
7994 if (!match(LHSI->getOperand(0), m_APFloat(C)))
7995 return nullptr;
7996
7997 if (C->isZero())
7998 return nullptr;
7999
8000 // Get swapped predicate if necessary.
8001 if (C->isNegative())
8002 Pred = I.getSwappedPredicate();
8003
8004 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
8005}
8006
8007/// Optimize fabs(X) compared with zero.
8009 Value *X;
8010 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
8011 return nullptr;
8012
8013 const APFloat *C;
8014 if (!match(I.getOperand(1), m_APFloat(C)))
8015 return nullptr;
8016
8017 if (!C->isPosZero()) {
8018 if (!C->isSmallestNormalized())
8019 return nullptr;
8020
8021 const Function *F = I.getFunction();
8022 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
8023 if (Mode.Input == DenormalMode::PreserveSign ||
8024 Mode.Input == DenormalMode::PositiveZero) {
8025
8026 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8027 Constant *Zero = ConstantFP::getZero(X->getType());
8028 return new FCmpInst(P, X, Zero, "", I);
8029 };
8030
8031 switch (I.getPredicate()) {
8032 case FCmpInst::FCMP_OLT:
8033 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
8034 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
8035 case FCmpInst::FCMP_UGE:
8036 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
8037 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
8038 case FCmpInst::FCMP_OGE:
8039 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
8040 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
8041 case FCmpInst::FCMP_ULT:
8042 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
8043 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
8044 default:
8045 break;
8046 }
8047 }
8048
8049 return nullptr;
8050 }
8051
8052 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8053 I->setPredicate(P);
8054 return IC.replaceOperand(*I, 0, X);
8055 };
8056
8057 switch (I.getPredicate()) {
8058 case FCmpInst::FCMP_UGE:
8059 case FCmpInst::FCMP_OLT:
8060 // fabs(X) >= 0.0 --> true
8061 // fabs(X) < 0.0 --> false
8062 llvm_unreachable("fcmp should have simplified");
8063
8064 case FCmpInst::FCMP_OGT:
8065 // fabs(X) > 0.0 --> X != 0.0
8066 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8067
8068 case FCmpInst::FCMP_UGT:
8069 // fabs(X) u> 0.0 --> X u!= 0.0
8070 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8071
8072 case FCmpInst::FCMP_OLE:
8073 // fabs(X) <= 0.0 --> X == 0.0
8074 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8075
8076 case FCmpInst::FCMP_ULE:
8077 // fabs(X) u<= 0.0 --> X u== 0.0
8078 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8079
8080 case FCmpInst::FCMP_OGE:
8081 // fabs(X) >= 0.0 --> !isnan(X)
8082 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8083 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8084
8085 case FCmpInst::FCMP_ULT:
8086 // fabs(X) u< 0.0 --> isnan(X)
8087 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8088 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8089
8090 case FCmpInst::FCMP_OEQ:
8091 case FCmpInst::FCMP_UEQ:
8092 case FCmpInst::FCMP_ONE:
8093 case FCmpInst::FCMP_UNE:
8094 case FCmpInst::FCMP_ORD:
8095 case FCmpInst::FCMP_UNO:
8096 // Look through the fabs() because it doesn't change anything but the sign.
8097 // fabs(X) == 0.0 --> X == 0.0,
8098 // fabs(X) != 0.0 --> X != 0.0
8099 // isnan(fabs(X)) --> isnan(X)
8100 // !isnan(fabs(X) --> !isnan(X)
8101 return replacePredAndOp0(&I, I.getPredicate(), X);
8102
8103 default:
8104 return nullptr;
8105 }
8106}
8107
8108/// Optimize sqrt(X) compared with zero.
8110 Value *X;
8111 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))
8112 return nullptr;
8113
8114 if (!match(I.getOperand(1), m_PosZeroFP()))
8115 return nullptr;
8116
8117 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8118 I.setPredicate(P);
8119 return IC.replaceOperand(I, 0, X);
8120 };
8121
8122 // Clear ninf flag if sqrt doesn't have it.
8123 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())
8124 I.setHasNoInfs(false);
8125
8126 switch (I.getPredicate()) {
8127 case FCmpInst::FCMP_OLT:
8128 case FCmpInst::FCMP_UGE:
8129 // sqrt(X) < 0.0 --> false
8130 // sqrt(X) u>= 0.0 --> true
8131 llvm_unreachable("fcmp should have simplified");
8132 case FCmpInst::FCMP_ULT:
8133 case FCmpInst::FCMP_ULE:
8134 case FCmpInst::FCMP_OGT:
8135 case FCmpInst::FCMP_OGE:
8136 case FCmpInst::FCMP_OEQ:
8137 case FCmpInst::FCMP_UNE:
8138 // sqrt(X) u< 0.0 --> X u< 0.0
8139 // sqrt(X) u<= 0.0 --> X u<= 0.0
8140 // sqrt(X) > 0.0 --> X > 0.0
8141 // sqrt(X) >= 0.0 --> X >= 0.0
8142 // sqrt(X) == 0.0 --> X == 0.0
8143 // sqrt(X) u!= 0.0 --> X u!= 0.0
8144 return IC.replaceOperand(I, 0, X);
8145
8146 case FCmpInst::FCMP_OLE:
8147 // sqrt(X) <= 0.0 --> X == 0.0
8148 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8149 case FCmpInst::FCMP_UGT:
8150 // sqrt(X) u> 0.0 --> X u!= 0.0
8151 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8152 case FCmpInst::FCMP_UEQ:
8153 // sqrt(X) u== 0.0 --> X u<= 0.0
8154 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8155 case FCmpInst::FCMP_ONE:
8156 // sqrt(X) != 0.0 --> X > 0.0
8157 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8158 case FCmpInst::FCMP_ORD:
8159 // !isnan(sqrt(X)) --> X >= 0.0
8160 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8161 case FCmpInst::FCMP_UNO:
8162 // isnan(sqrt(X)) --> X u< 0.0
8163 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8164 default:
8165 llvm_unreachable("Unexpected predicate!");
8166 }
8167}
8168
8170 CmpInst::Predicate Pred = I.getPredicate();
8171 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8172
8173 // Canonicalize fneg as Op1.
8174 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
8175 std::swap(Op0, Op1);
8176 Pred = I.getSwappedPredicate();
8177 }
8178
8179 if (!match(Op1, m_FNeg(m_Specific(Op0))))
8180 return nullptr;
8181
8182 // Replace the negated operand with 0.0:
8183 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8184 Constant *Zero = ConstantFP::getZero(Op0->getType());
8185 return new FCmpInst(Pred, Op0, Zero, "", &I);
8186}
8187
8189 Constant *RHSC, InstCombinerImpl &CI) {
8190 const CmpInst::Predicate Pred = I.getPredicate();
8191 Value *X = LHSI->getOperand(0);
8192 Value *Y = LHSI->getOperand(1);
8193 switch (Pred) {
8194 default:
8195 break;
8196 case FCmpInst::FCMP_UGT:
8197 case FCmpInst::FCMP_ULT:
8198 case FCmpInst::FCMP_UNE:
8199 case FCmpInst::FCMP_OEQ:
8200 case FCmpInst::FCMP_OGE:
8201 case FCmpInst::FCMP_OLE:
8202 // The optimization is not valid if X and Y are infinities of the same
8203 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8204 // flag then we can assume we do not have that case. Otherwise we might be
8205 // able to prove that either X or Y is not infinity.
8206 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8207 !isKnownNeverInfinity(Y, /*Depth=*/0,
8209 !isKnownNeverInfinity(X, /*Depth=*/0,
8211 break;
8212
8213 [[fallthrough]];
8214 case FCmpInst::FCMP_OGT:
8215 case FCmpInst::FCMP_OLT:
8216 case FCmpInst::FCMP_ONE:
8217 case FCmpInst::FCMP_UEQ:
8218 case FCmpInst::FCMP_UGE:
8219 case FCmpInst::FCMP_ULE:
8220 // fcmp pred (x - y), 0 --> fcmp pred x, y
8221 if (match(RHSC, m_AnyZeroFP()) &&
8222 I.getFunction()->getDenormalMode(
8223 LHSI->getType()->getScalarType()->getFltSemantics()) ==
8225 CI.replaceOperand(I, 0, X);
8226 CI.replaceOperand(I, 1, Y);
8227 return &I;
8228 }
8229 break;
8230 }
8231
8232 return nullptr;
8233}
8234
8236 InstCombinerImpl &IC) {
8237 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
8238 Type *OpType = LHS->getType();
8239 CmpInst::Predicate Pred = I.getPredicate();
8240
8241 bool FloorX = match(LHS, m_Intrinsic<Intrinsic::floor>(m_Specific(RHS)));
8242 bool CeilX = match(LHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(RHS)));
8243
8244 if (!FloorX && !CeilX) {
8245 if ((FloorX = match(RHS, m_Intrinsic<Intrinsic::floor>(m_Specific(LHS)))) ||
8246 (CeilX = match(RHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(LHS))))) {
8247 std::swap(LHS, RHS);
8248 Pred = I.getSwappedPredicate();
8249 }
8250 }
8251
8252 switch (Pred) {
8253 case FCmpInst::FCMP_OLE:
8254 // fcmp ole floor(x), x => fcmp ord x, 0
8255 if (FloorX)
8257 "", &I);
8258 break;
8259 case FCmpInst::FCMP_OGT:
8260 // fcmp ogt floor(x), x => false
8261 if (FloorX)
8262 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8263 break;
8264 case FCmpInst::FCMP_OGE:
8265 // fcmp oge ceil(x), x => fcmp ord x, 0
8266 if (CeilX)
8268 "", &I);
8269 break;
8270 case FCmpInst::FCMP_OLT:
8271 // fcmp olt ceil(x), x => false
8272 if (CeilX)
8273 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8274 break;
8275 case FCmpInst::FCMP_ULE:
8276 // fcmp ule floor(x), x => true
8277 if (FloorX)
8278 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8279 break;
8280 case FCmpInst::FCMP_UGT:
8281 // fcmp ugt floor(x), x => fcmp uno x, 0
8282 if (FloorX)
8284 "", &I);
8285 break;
8286 case FCmpInst::FCMP_UGE:
8287 // fcmp uge ceil(x), x => true
8288 if (CeilX)
8289 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8290 break;
8291 case FCmpInst::FCMP_ULT:
8292 // fcmp ult ceil(x), x => fcmp uno x, 0
8293 if (CeilX)
8295 "", &I);
8296 break;
8297 default:
8298 break;
8299 }
8300
8301 return nullptr;
8302}
8303
8305 bool Changed = false;
8306
8307 /// Orders the operands of the compare so that they are listed from most
8308 /// complex to least complex. This puts constants before unary operators,
8309 /// before binary operators.
8310 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
8311 I.swapOperands();
8312 Changed = true;
8313 }
8314
8315 const CmpInst::Predicate Pred = I.getPredicate();
8316 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8317 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
8319 return replaceInstUsesWith(I, V);
8320
8321 // Simplify 'fcmp pred X, X'
8322 Type *OpType = Op0->getType();
8323 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
8324 if (Op0 == Op1) {
8325 switch (Pred) {
8326 default:
8327 break;
8328 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
8329 case FCmpInst::FCMP_ULT: // True if unordered or less than
8330 case FCmpInst::FCMP_UGT: // True if unordered or greater than
8331 case FCmpInst::FCMP_UNE: // True if unordered or not equal
8332 // Canonicalize these to be 'fcmp uno %X, 0.0'.
8333 I.setPredicate(FCmpInst::FCMP_UNO);
8334 I.setOperand(1, Constant::getNullValue(OpType));
8335 return &I;
8336
8337 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
8338 case FCmpInst::FCMP_OEQ: // True if ordered and equal
8339 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
8340 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
8341 // Canonicalize these to be 'fcmp ord %X, 0.0'.
8342 I.setPredicate(FCmpInst::FCMP_ORD);
8343 I.setOperand(1, Constant::getNullValue(OpType));
8344 return &I;
8345 }
8346 }
8347
8348 if (I.isCommutative()) {
8349 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
8350 replaceOperand(I, 0, Pair->first);
8351 replaceOperand(I, 1, Pair->second);
8352 return &I;
8353 }
8354 }
8355
8356 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
8357 // then canonicalize the operand to 0.0.
8358 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
8359 if (!match(Op0, m_PosZeroFP()) &&
8360 isKnownNeverNaN(Op0, 0, getSimplifyQuery().getWithInstruction(&I)))
8361 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
8362
8363 if (!match(Op1, m_PosZeroFP()) &&
8364 isKnownNeverNaN(Op1, 0, getSimplifyQuery().getWithInstruction(&I)))
8365 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8366 }
8367
8368 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
8369 Value *X, *Y;
8370 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
8371 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
8372
8374 return R;
8375
8376 // Test if the FCmpInst instruction is used exclusively by a select as
8377 // part of a minimum or maximum operation. If so, refrain from doing
8378 // any other folding. This helps out other analyses which understand
8379 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
8380 // and CodeGen. And in this case, at least one of the comparison
8381 // operands has at least one user besides the compare (the select),
8382 // which would often largely negate the benefit of folding anyway.
8383 if (I.hasOneUse())
8384 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
8385 Value *A, *B;
8387 if (SPR.Flavor != SPF_UNKNOWN)
8388 return nullptr;
8389 }
8390
8391 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
8392 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
8393 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
8394 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8395
8396 // Canonicalize:
8397 // fcmp olt X, +inf -> fcmp one X, +inf
8398 // fcmp ole X, +inf -> fcmp ord X, 0
8399 // fcmp ogt X, +inf -> false
8400 // fcmp oge X, +inf -> fcmp oeq X, +inf
8401 // fcmp ult X, +inf -> fcmp une X, +inf
8402 // fcmp ule X, +inf -> true
8403 // fcmp ugt X, +inf -> fcmp uno X, 0
8404 // fcmp uge X, +inf -> fcmp ueq X, +inf
8405 // fcmp olt X, -inf -> false
8406 // fcmp ole X, -inf -> fcmp oeq X, -inf
8407 // fcmp ogt X, -inf -> fcmp one X, -inf
8408 // fcmp oge X, -inf -> fcmp ord X, 0
8409 // fcmp ult X, -inf -> fcmp uno X, 0
8410 // fcmp ule X, -inf -> fcmp ueq X, -inf
8411 // fcmp ugt X, -inf -> fcmp une X, -inf
8412 // fcmp uge X, -inf -> true
8413 const APFloat *C;
8414 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
8415 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
8416 default:
8417 break;
8418 case FCmpInst::FCMP_ORD:
8419 case FCmpInst::FCMP_UNO:
8422 case FCmpInst::FCMP_OGT:
8423 case FCmpInst::FCMP_ULE:
8424 llvm_unreachable("Should be simplified by InstSimplify");
8425 case FCmpInst::FCMP_OLT:
8426 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
8427 case FCmpInst::FCMP_OLE:
8428 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
8429 "", &I);
8430 case FCmpInst::FCMP_OGE:
8431 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
8432 case FCmpInst::FCMP_ULT:
8433 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
8434 case FCmpInst::FCMP_UGT:
8435 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8436 "", &I);
8437 case FCmpInst::FCMP_UGE:
8438 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8439 }
8440 }
8441
8442 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8443 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8444 if (match(Op1, m_PosZeroFP()) &&
8447 if (Pred == FCmpInst::FCMP_OEQ)
8448 IntPred = ICmpInst::ICMP_EQ;
8449 else if (Pred == FCmpInst::FCMP_UNE)
8450 IntPred = ICmpInst::ICMP_NE;
8451
8452 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8453 Type *IntTy = X->getType();
8454 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8455 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8456 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8457 }
8458 }
8459
8460 // Handle fcmp with instruction LHS and constant RHS.
8461 Instruction *LHSI;
8462 Constant *RHSC;
8463 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8464 switch (LHSI->getOpcode()) {
8465 case Instruction::Select:
8466 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8467 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8469 return replaceOperand(I, 0, X);
8470 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
8471 return NV;
8472 break;
8473 case Instruction::FSub:
8474 if (LHSI->hasOneUse())
8475 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
8476 return NV;
8477 break;
8478 case Instruction::PHI:
8479 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8480 return NV;
8481 break;
8482 case Instruction::SIToFP:
8483 case Instruction::UIToFP:
8484 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8485 return NV;
8486 break;
8487 case Instruction::FDiv:
8488 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8489 return NV;
8490 break;
8491 case Instruction::Load:
8492 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8493 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
8495 cast<LoadInst>(LHSI), GEP, GV, I))
8496 return Res;
8497 break;
8498 }
8499 }
8500
8501 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8502 return R;
8503
8504 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))
8505 return R;
8506
8507 if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this))
8508 return R;
8509
8510 if (match(Op0, m_FNeg(m_Value(X)))) {
8511 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8512 Constant *C;
8513 if (match(Op1, m_Constant(C)))
8514 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8515 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8516 }
8517
8518 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8519 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8520 return new FCmpInst(Pred, X, Op1, "", &I);
8521
8522 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8523 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8524 return new FCmpInst(Pred, Op0, Y, "", &I);
8525
8526 if (match(Op0, m_FPExt(m_Value(X)))) {
8527 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8528 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8529 return new FCmpInst(Pred, X, Y, "", &I);
8530
8531 const APFloat *C;
8532 if (match(Op1, m_APFloat(C))) {
8533 const fltSemantics &FPSem =
8534 X->getType()->getScalarType()->getFltSemantics();
8535 bool Lossy;
8536 APFloat TruncC = *C;
8537 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8538
8539 if (Lossy) {
8540 // X can't possibly equal the higher-precision constant, so reduce any
8541 // equality comparison.
8542 // TODO: Other predicates can be handled via getFCmpCode().
8543 switch (Pred) {
8544 case FCmpInst::FCMP_OEQ:
8545 // X is ordered and equal to an impossible constant --> false
8546 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8547 case FCmpInst::FCMP_ONE:
8548 // X is ordered and not equal to an impossible constant --> ordered
8549 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8550 ConstantFP::getZero(X->getType()));
8551 case FCmpInst::FCMP_UEQ:
8552 // X is unordered or equal to an impossible constant --> unordered
8553 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8554 ConstantFP::getZero(X->getType()));
8555 case FCmpInst::FCMP_UNE:
8556 // X is unordered or not equal to an impossible constant --> true
8557 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8558 default:
8559 break;
8560 }
8561 }
8562
8563 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8564 // Avoid lossy conversions and denormals.
8565 // Zero is a special case that's OK to convert.
8566 APFloat Fabs = TruncC;
8567 Fabs.clearSign();
8568 if (!Lossy &&
8569 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8570 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8571 return new FCmpInst(Pred, X, NewC, "", &I);
8572 }
8573 }
8574 }
8575
8576 // Convert a sign-bit test of an FP value into a cast and integer compare.
8577 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8578 // TODO: Handle non-zero compare constants.
8579 // TODO: Handle other predicates.
8580 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),
8581 m_Value(X)))) &&
8582 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8583 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8584 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8585 IntType = VectorType::get(IntType, VecTy->getElementCount());
8586
8587 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8588 if (Pred == FCmpInst::FCMP_OLT) {
8589 Value *IntX = Builder.CreateBitCast(X, IntType);
8590 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8591 ConstantInt::getNullValue(IntType));
8592 }
8593 }
8594
8595 {
8596 Value *CanonLHS = nullptr, *CanonRHS = nullptr;
8597 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));
8598 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));
8599
8600 // (canonicalize(x) == x) => (x == x)
8601 if (CanonLHS == Op1)
8602 return new FCmpInst(Pred, Op1, Op1, "", &I);
8603
8604 // (x == canonicalize(x)) => (x == x)
8605 if (CanonRHS == Op0)
8606 return new FCmpInst(Pred, Op0, Op0, "", &I);
8607
8608 // (canonicalize(x) == canonicalize(y)) => (x == y)
8609 if (CanonLHS && CanonRHS)
8610 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8611 }
8612
8613 if (I.getType()->isVectorTy())
8614 if (Instruction *Res = foldVectorCmp(I, Builder))
8615 return Res;
8616
8617 return Changed ? &I : nullptr;
8618}
AMDGPU Register Bank Select
Rewrite undef for PHI
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define Check(C,...)
Hexagon Common GEP
static Instruction * foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
static Instruction * foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC)
Optimize fabs(X) compared with zero.
static Value * rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW, const DataLayout &DL, SetVector< Value * > &Explored, InstCombiner &IC)
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
static bool 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)
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 * 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 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:557
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
#define T1
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
Value * RHS
Value * LHS
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5463
void clearSign()
Definition: APFloat.h:1300
bool isZero() const
Definition: APFloat.h:1441
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1160
APInt bitcastToAPInt() const
Definition: APFloat.h:1351
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1140
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1100
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5450
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1250
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1547
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
Definition: APInt.cpp:1732
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition: APInt.h:449
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:986
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:1520
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1492
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:910
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:1773
unsigned ceilLogBase2() const
Definition: APInt.h:1742
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
APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1922
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:1468
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
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1902
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition: APInt.h:1249
bool eq(const APInt &RHS) const
Equality comparison.
Definition: APInt.h:1079
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1618
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1909
void negate()
Negate this APInt in place.
Definition: APInt.h:1450
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1618
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1577
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:1434
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1594
unsigned logBase2() const
Definition: APInt.h:1739
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
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1915
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:1635
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:23
an instruction to allocate memory on the stack
Definition: Instructions.h:63
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:395
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:437
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:481
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:240
BinaryOps getOpcode() const
Definition: InstrTypes.h:370
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Conditional or Unconditional Branch instruction.
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1286
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:661
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:980
Predicate getStrictPredicate() const
For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
Definition: InstrTypes.h:856
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:690
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:702
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:703
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:679
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:688
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:677
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:678
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:697
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:696
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:700
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:687
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:681
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:684
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:698
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:685
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:680
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:682
@ ICMP_EQ
equal
Definition: InstrTypes.h:694
@ ICMP_NE
not equal
Definition: InstrTypes.h:695
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:701
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:689
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:699
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:686
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:675
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:683
bool isSigned() const
Definition: InstrTypes.h:928
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:825
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:940
static CmpInst * Create(OtherOps Op, Predicate Pred, Value *S1, Value *S2, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition: InstrTypes.h:869
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:787
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:763
bool isStrictPredicate() const
Definition: InstrTypes.h:841
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:891
bool isIntPredicate() const
Definition: InstrTypes.h:781
bool isUnsigned() const
Definition: InstrTypes.h:934
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:22
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2307
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2268
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2645
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2632
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2293
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2659
static Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2626
static Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1057
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
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:258
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:866
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:208
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:126
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:873
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:151
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:148
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:880
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
std::optional< ConstantRange > exactUnionWith(const ConstantRange &CR) const
Union the two ranges and return the result if it can be represented exactly, otherwise return std::nu...
bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
ConstantRange truncate(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
ConstantRange difference(const ConstantRange &CR) const
Subtract the specified range from this range (aka relative complement of the sets).
bool isEmptySet() const
Return true if this set contains no members.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
std::optional< ConstantRange > exactIntersectWith(const ConstantRange &CR) const
Intersect the two ranges and return the result if it can be represented exactly, otherwise return std...
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1472
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
Definition: Constants.cpp:403
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
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:1771
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
Definition: DataLayout.cpp:851
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:743
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
Definition: DataLayout.cpp:878
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:457
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:866
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:147
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This instruction compares its operands according to the predicate given to the constructor.
bool isEquality() const
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:435
Type * getSourceElementType() const
Definition: Operator.cpp:70
Value * getPointerOperand()
Definition: Operator.h:462
GEPNoWrapFlags getNoWrapFlags() const
Definition: Operator.h:430
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
Definition: Operator.h:509
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:933
Type * getValueType() const
Definition: GlobalValue.h:297
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Predicate 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.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:113
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2286
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2499
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:558
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2294
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1163
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2555
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:485
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1480
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition: IRBuilder.h:2186
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2274
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:1874
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1733
Value * createIsFPClass(Value *FPNum, unsigned Test)
Definition: IRBuilder.cpp:1248
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:900
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:505
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2404
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2435
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1757
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2270
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1387
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2152
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2278
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:881
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1459
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2033
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1518
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1370
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:490
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2449
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2019
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1540
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1671
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2302
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2225
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition: IRBuilder.h:2583
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:199
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1562
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2380
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:535
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:521
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1447
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1404
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2705
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.
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 SimplifyDemandedBits(Instruction *I, unsigned Op, const APInt &DemandedMask, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q) override
This form of SimplifyDemandedBits simplifies the specified instruction operand if possible,...
bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS, ConstantInt *&Less, ConstantInt *&Equal, ConstantInt *&Greater)
Match a select chain which produces one of three values based on whether the LHS is less than,...
Instruction * foldCmpLoadFromIndexedGlobal(LoadInst *LI, GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
This is called when we see this pattern: cmp pred (load (gep GV, ...)), cmpcst where GV is a global v...
Instruction * visitFCmpInst(FCmpInst &I)
Instruction * foldICmpUsingKnownBits(ICmpInst &Cmp)
Try to fold the comparison based on range information we can get by checking whether bits are known t...
Instruction * foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, const APInt &C)
Fold icmp ({su}div X, Y), C.
Instruction * foldIRemByPowerOfTwoToBitTest(ICmpInst &I)
If we have: icmp eq/ne (urem/srem x, y), 0 iff y is a power-of-two, we can replace this with a bit te...
Instruction * foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold fcmp ([us]itofp x, cst) if possible.
Instruction * foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Fold icmp (udiv X, Y), C.
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 * foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, const APInt &C1, const APInt &C2)
Fold icmp (and (sh X, Y), C2), C1.
Instruction * foldICmpInstWithConstant(ICmpInst &Cmp)
Try to fold integer comparisons with a constant operand: icmp Pred X, C where X is some kind of instr...
Instruction * foldICmpXorShiftConst(ICmpInst &Cmp, BinaryOperator *Xor, const APInt &C)
For power-of-2 C: ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1) ((X s>> ShiftC) ^ X) u> (C - 1) -...
Instruction * foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl, const APInt &C)
Fold icmp (shl X, Y), C.
Instruction * foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And, const APInt &C)
Fold icmp (and X, Y), C.
Instruction * foldICmpEquality(ICmpInst &Cmp)
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:502
SimplifyQuery SQ
Definition: InstCombiner.h:77
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:465
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
Definition: InstCombiner.h:143
TargetLibraryInfo & TLI
Definition: InstCombiner.h:74
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
Definition: InstCombiner.h:443
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:488
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:495
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
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:473
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:433
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:480
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:209
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:338
unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:460
bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoInfs() const LLVM_READONLY
Determine whether the no-infs flag is set.
bool isArithmeticShift() const
Return true if this is an arithmetic shift right.
Definition: Instruction.h:359
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:310
bool isShift() const
Definition: Instruction.h:318
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:311
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:55
An instruction for reading from memory.
Definition: Instructions.h:176
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:205
This class represents min/max intrinsics.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Represents a saturating add/sub intrinsic.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
A vector that has set insertion semantics.
Definition: SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition: SetVector.h:254
This instruction constructs a fixed permutation of two input vectors.
bool empty() const
Definition: SmallVector.h:81
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
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
unsigned getIntegerBitWidth() const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:270
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:243
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:264
static IntegerType * getInt1Ty(LLVMContext &C)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition: Type.h:165
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
int getFPMantissaWidth() const
Return the width of the mantissa of this type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:237
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
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:233
Value * getOperand(unsigned i) const
Definition: User.h:228
unsigned getNumOperands() const
Definition: User.h:250
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
iterator_range< user_iterator > users()
Definition: Value.h:421
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition: Value.cpp:153
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:694
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1094
iterator_range< use_iterator > uses()
Definition: Value.h:376
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
const ParentTy * getParent() const
Definition: ilist_node.h:32
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A unsign-divided by B, rounded by the given rounding mode.
Definition: APInt.cpp:2736
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:2754
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
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:732
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
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.
Definition: PatternMatch.h:982
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:826
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:885
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
Definition: PatternMatch.h:990
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:903
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:864
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)
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:773
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
Definition: PatternMatch.h:203
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
Definition: PatternMatch.h:698
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition: STLExtras.h:854
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:1739
bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
bool isKnownNeverInfinity(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
Value * simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:215
Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
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:341
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: bit.h:281
Value * emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
Definition: Local.cpp:22
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:44
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
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
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1187
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:1753
Value * simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
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.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ And
Bitwise or logical AND of integers.
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ Add
Sum of integers.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
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
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
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:1945
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:2087
bool isKnownNeverNaN(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define NC
Definition: regutils.h:42
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:302
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:306
This callback is used in conjunction with PointerMayBeCaptured.
Represent subnormal handling kind for floating point instruction inputs and outputs.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
static constexpr DenormalMode getIEEE()
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:100
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:79
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:234
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:266
APInt getSignedMaxValue() const
Return the maximal signed value possible given these KnownBits.
Definition: KnownBits.h:143
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:281
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:43
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:53
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:240
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:137
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:121
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:106
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:97
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:278
APInt getSignedMinValue() const
Return the minimal signed value possible given these KnownBits.
Definition: KnownBits.h:127
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:59
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
Definition: SimplifyQuery.h:71
const Instruction * CxtI
Definition: SimplifyQuery.h:75
const DominatorTree * DT
Definition: SimplifyQuery.h:73
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:74
SimplifyQuery getWithoutDomCondCache() const
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:254