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 // These transform work when A is negative.
5069 // X s< X^A, X s<= X^A, X u> X^A, X u>= X^A --> X s< 0
5070 // X s> X^A, X s>= X^A, X u< X^A, X u<= X^A --> X s>= 0
5071 if (match(A, m_Negative())) {
5072 CmpInst::Predicate NewPred;
5073 switch (ICmpInst::getStrictPredicate(Pred)) {
5074 default:
5075 return nullptr;
5076 case ICmpInst::ICMP_SLT:
5077 case ICmpInst::ICMP_UGT:
5078 NewPred = ICmpInst::ICMP_SLT;
5079 break;
5080 case ICmpInst::ICMP_SGT:
5081 case ICmpInst::ICMP_ULT:
5082 NewPred = ICmpInst::ICMP_SGE;
5083 break;
5084 }
5085 Constant *Const = Constant::getNullValue(Op0->getType());
5086 return new ICmpInst(NewPred, Op0, Const);
5087 }
5088
5089 return nullptr;
5090}
5091
5092/// Try to fold icmp (binop), X or icmp X, (binop).
5093/// TODO: A large part of this logic is duplicated in InstSimplify's
5094/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
5095/// duplication.
5097 const SimplifyQuery &SQ) {
5099 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5100
5101 // Special logic for binary operators.
5102 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
5103 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
5104 if (!BO0 && !BO1)
5105 return nullptr;
5106
5107 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
5108 return NewICmp;
5109
5110 const CmpInst::Predicate Pred = I.getPredicate();
5111 Value *X;
5112
5113 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
5114 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
5115 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
5116 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5117 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
5118 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
5119 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
5120 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5121 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
5122
5123 {
5124 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
5125 Constant *C;
5126 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
5127 m_ImmConstant(C)))) &&
5128 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
5130 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
5131 }
5132 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
5133 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
5134 m_ImmConstant(C)))) &&
5135 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
5137 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
5138 }
5139 }
5140
5141 // (icmp eq/ne (X, -P2), INT_MIN)
5142 // -> (icmp slt/sge X, INT_MIN + P2)
5143 if (ICmpInst::isEquality(Pred) && BO0 &&
5144 match(I.getOperand(1), m_SignMask()) &&
5146 // Will Constant fold.
5147 Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
5148 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5150 BO0->getOperand(0), NewC);
5151 }
5152
5153 {
5154 // Similar to above: an unsigned overflow comparison may use offset + mask:
5155 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
5156 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
5157 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
5158 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
5159 BinaryOperator *BO;
5160 const APInt *C;
5161 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
5162 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5164 CmpInst::Predicate NewPred =
5166 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5167 return new ICmpInst(NewPred, Op1, Zero);
5168 }
5169
5170 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5171 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
5173 CmpInst::Predicate NewPred =
5175 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
5176 return new ICmpInst(NewPred, Op0, Zero);
5177 }
5178 }
5179
5180 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
5181 bool Op0HasNUW = false, Op1HasNUW = false;
5182 bool Op0HasNSW = false, Op1HasNSW = false;
5183 // Analyze the case when either Op0 or Op1 is an add instruction.
5184 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
5185 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
5186 bool &HasNSW, bool &HasNUW) -> bool {
5187 if (isa<OverflowingBinaryOperator>(BO)) {
5188 HasNUW = BO.hasNoUnsignedWrap();
5189 HasNSW = BO.hasNoSignedWrap();
5190 return ICmpInst::isEquality(Pred) ||
5191 (CmpInst::isUnsigned(Pred) && HasNUW) ||
5192 (CmpInst::isSigned(Pred) && HasNSW);
5193 } else if (BO.getOpcode() == Instruction::Or) {
5194 HasNUW = true;
5195 HasNSW = true;
5196 return true;
5197 } else {
5198 return false;
5199 }
5200 };
5201 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
5202
5203 if (BO0) {
5204 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
5205 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
5206 }
5207 if (BO1) {
5208 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
5209 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
5210 }
5211
5212 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
5213 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
5214 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
5215 return new ICmpInst(Pred, A == Op1 ? B : A,
5216 Constant::getNullValue(Op1->getType()));
5217
5218 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
5219 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
5220 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
5221 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
5222 C == Op0 ? D : C);
5223
5224 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
5225 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
5226 NoOp1WrapProblem) {
5227 // Determine Y and Z in the form icmp (X+Y), (X+Z).
5228 Value *Y, *Z;
5229 if (A == C) {
5230 // C + B == C + D -> B == D
5231 Y = B;
5232 Z = D;
5233 } else if (A == D) {
5234 // D + B == C + D -> B == C
5235 Y = B;
5236 Z = C;
5237 } else if (B == C) {
5238 // A + C == C + D -> A == D
5239 Y = A;
5240 Z = D;
5241 } else {
5242 assert(B == D);
5243 // A + D == C + D -> A == C
5244 Y = A;
5245 Z = C;
5246 }
5247 return new ICmpInst(Pred, Y, Z);
5248 }
5249
5250 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
5251 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
5252 match(B, m_AllOnes()))
5253 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
5254
5255 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
5256 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
5257 match(B, m_AllOnes()))
5258 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
5259
5260 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
5261 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
5262 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
5263
5264 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
5265 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
5266 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
5267
5268 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
5269 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
5270 match(D, m_AllOnes()))
5271 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
5272
5273 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
5274 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
5275 match(D, m_AllOnes()))
5276 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
5277
5278 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
5279 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
5280 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
5281
5282 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
5283 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
5284 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
5285
5286 // TODO: The subtraction-related identities shown below also hold, but
5287 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
5288 // wouldn't happen even if they were implemented.
5289 //
5290 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
5291 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
5292 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
5293 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
5294
5295 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
5296 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One()))
5297 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1);
5298
5299 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
5300 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One()))
5301 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1);
5302
5303 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
5304 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One()))
5305 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C);
5306
5307 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
5308 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One()))
5309 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C);
5310
5311 // if C1 has greater magnitude than C2:
5312 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
5313 // s.t. C3 = C1 - C2
5314 //
5315 // if C2 has greater magnitude than C1:
5316 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
5317 // s.t. C3 = C2 - C1
5318 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
5319 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
5320 const APInt *AP1, *AP2;
5321 // TODO: Support non-uniform vectors.
5322 // TODO: Allow poison passthrough if B or D's element is poison.
5323 if (match(B, m_APIntAllowPoison(AP1)) &&
5324 match(D, m_APIntAllowPoison(AP2)) &&
5325 AP1->isNegative() == AP2->isNegative()) {
5326 APInt AP1Abs = AP1->abs();
5327 APInt AP2Abs = AP2->abs();
5328 if (AP1Abs.uge(AP2Abs)) {
5329 APInt Diff = *AP1 - *AP2;
5330 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5331 Value *NewAdd = Builder.CreateAdd(
5332 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
5333 return new ICmpInst(Pred, NewAdd, C);
5334 } else {
5335 APInt Diff = *AP2 - *AP1;
5336 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
5337 Value *NewAdd = Builder.CreateAdd(
5338 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
5339 return new ICmpInst(Pred, A, NewAdd);
5340 }
5341 }
5342 Constant *Cst1, *Cst2;
5343 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5344 ICmpInst::isEquality(Pred)) {
5345 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5346 Value *NewAdd = Builder.CreateAdd(C, Diff);
5347 return new ICmpInst(Pred, A, NewAdd);
5348 }
5349 }
5350
5351 // Analyze the case when either Op0 or Op1 is a sub instruction.
5352 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5353 A = nullptr;
5354 B = nullptr;
5355 C = nullptr;
5356 D = nullptr;
5357 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5358 A = BO0->getOperand(0);
5359 B = BO0->getOperand(1);
5360 }
5361 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5362 C = BO1->getOperand(0);
5363 D = BO1->getOperand(1);
5364 }
5365
5366 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5367 if (A == Op1 && NoOp0WrapProblem)
5368 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5369 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5370 if (C == Op0 && NoOp1WrapProblem)
5371 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5372
5373 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5374 // (A - B) u>/u<= A --> B u>/u<= A
5375 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5376 return new ICmpInst(Pred, B, A);
5377 // C u</u>= (C - D) --> C u</u>= D
5378 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5379 return new ICmpInst(Pred, C, D);
5380 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5381 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5382 isKnownNonZero(B, Q))
5384 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5385 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5386 isKnownNonZero(D, Q))
5388
5389 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5390 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5391 return new ICmpInst(Pred, A, C);
5392
5393 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5394 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5395 return new ICmpInst(Pred, D, B);
5396
5397 // icmp (0-X) < cst --> x > -cst
5398 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5399 Value *X;
5400 if (match(BO0, m_Neg(m_Value(X))))
5401 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5402 if (RHSC->isNotMinSignedValue())
5403 return new ICmpInst(I.getSwappedPredicate(), X,
5404 ConstantExpr::getNeg(RHSC));
5405 }
5406
5407 if (Instruction *R = foldICmpXorXX(I, Q, *this))
5408 return R;
5409 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5410 return R;
5411
5412 {
5413 // Try to remove shared multiplier from comparison:
5414 // X * Z pred Y * Z
5415 Value *X, *Y, *Z;
5416 if ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5417 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5418 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5419 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y))))) {
5420 if (ICmpInst::isSigned(Pred)) {
5421 if (Op0HasNSW && Op1HasNSW) {
5422 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5423 if (ZKnown.isStrictlyPositive())
5424 return new ICmpInst(Pred, X, Y);
5425 if (ZKnown.isNegative())
5426 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y);
5429 if (LessThan && match(LessThan, m_One()))
5430 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z,
5431 Constant::getNullValue(Z->getType()));
5432 Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y,
5434 if (GreaterThan && match(GreaterThan, m_One()))
5435 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5436 }
5437 } else {
5438 bool NonZero;
5439 if (ICmpInst::isEquality(Pred)) {
5440 // If X != Y, fold (X *nw Z) eq/ne (Y *nw Z) -> Z eq/ne 0
5441 if (((Op0HasNSW && Op1HasNSW) || (Op0HasNUW && Op1HasNUW)) &&
5442 isKnownNonEqual(X, Y, SQ))
5443 return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
5444
5445 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5446 // if Z % 2 != 0
5447 // X * Z eq/ne Y * Z -> X eq/ne Y
5448 if (ZKnown.countMaxTrailingZeros() == 0)
5449 return new ICmpInst(Pred, X, Y);
5450 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5451 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5452 // X * Z eq/ne Y * Z -> X eq/ne Y
5453 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5454 return new ICmpInst(Pred, X, Y);
5455 } else
5456 NonZero = isKnownNonZero(Z, Q);
5457
5458 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5459 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5460 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5461 return new ICmpInst(Pred, X, Y);
5462 }
5463 }
5464 }
5465
5466 BinaryOperator *SRem = nullptr;
5467 // icmp (srem X, Y), Y
5468 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5469 SRem = BO0;
5470 // icmp Y, (srem X, Y)
5471 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5472 Op0 == BO1->getOperand(1))
5473 SRem = BO1;
5474 if (SRem) {
5475 // We don't check hasOneUse to avoid increasing register pressure because
5476 // the value we use is the same value this instruction was already using.
5477 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5478 default:
5479 break;
5480 case ICmpInst::ICMP_EQ:
5481 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5482 case ICmpInst::ICMP_NE:
5483 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5484 case ICmpInst::ICMP_SGT:
5485 case ICmpInst::ICMP_SGE:
5486 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5488 case ICmpInst::ICMP_SLT:
5489 case ICmpInst::ICMP_SLE:
5490 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5492 }
5493 }
5494
5495 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5496 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5497 BO0->getOperand(1) == BO1->getOperand(1)) {
5498 switch (BO0->getOpcode()) {
5499 default:
5500 break;
5501 case Instruction::Add:
5502 case Instruction::Sub:
5503 case Instruction::Xor: {
5504 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5505 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5506
5507 const APInt *C;
5508 if (match(BO0->getOperand(1), m_APInt(C))) {
5509 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5510 if (C->isSignMask()) {
5511 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5512 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5513 }
5514
5515 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5516 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5517 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5518 NewPred = I.getSwappedPredicate(NewPred);
5519 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5520 }
5521 }
5522 break;
5523 }
5524 case Instruction::Mul: {
5525 if (!I.isEquality())
5526 break;
5527
5528 const APInt *C;
5529 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5530 !C->isOne()) {
5531 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5532 // Mask = -1 >> count-trailing-zeros(C).
5533 if (unsigned TZs = C->countr_zero()) {
5534 Constant *Mask = ConstantInt::get(
5535 BO0->getType(),
5536 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5537 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5538 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5539 return new ICmpInst(Pred, And1, And2);
5540 }
5541 }
5542 break;
5543 }
5544 case Instruction::UDiv:
5545 case Instruction::LShr:
5546 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5547 break;
5548 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5549
5550 case Instruction::SDiv:
5551 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5552 !BO0->isExact() || !BO1->isExact())
5553 break;
5554 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5555
5556 case Instruction::AShr:
5557 if (!BO0->isExact() || !BO1->isExact())
5558 break;
5559 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5560
5561 case Instruction::Shl: {
5562 bool NUW = Op0HasNUW && Op1HasNUW;
5563 bool NSW = Op0HasNSW && Op1HasNSW;
5564 if (!NUW && !NSW)
5565 break;
5566 if (!NSW && I.isSigned())
5567 break;
5568 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5569 }
5570 }
5571 }
5572
5573 if (BO0) {
5574 // Transform A & (L - 1) `ult` L --> L != 0
5575 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5576 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5577
5578 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5579 auto *Zero = Constant::getNullValue(BO0->getType());
5580 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5581 }
5582 }
5583
5584 // For unsigned predicates / eq / ne:
5585 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5586 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5587 if (!ICmpInst::isSigned(Pred)) {
5588 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5589 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5590 Constant::getNullValue(Op1->getType()));
5591 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5592 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5593 Constant::getNullValue(Op0->getType()), Op0);
5594 }
5595
5597 return replaceInstUsesWith(I, V);
5598
5599 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5600 return R;
5601
5603 return replaceInstUsesWith(I, V);
5604
5606 return replaceInstUsesWith(I, V);
5607
5608 return nullptr;
5609}
5610
5611/// Fold icmp Pred min|max(X, Y), Z.
5614 Value *Z, CmpPredicate Pred) {
5615 Value *X = MinMax->getLHS();
5616 Value *Y = MinMax->getRHS();
5617 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5618 return nullptr;
5619 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5620 // Revert the transform signed pred -> unsigned pred
5621 // TODO: We can flip the signedness of predicate if both operands of icmp
5622 // are negative.
5626 } else
5627 return nullptr;
5628 }
5630 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5631 if (!Val)
5632 return std::nullopt;
5633 if (match(Val, m_One()))
5634 return true;
5635 if (match(Val, m_Zero()))
5636 return false;
5637 return std::nullopt;
5638 };
5639 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5640 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5641 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5642 return nullptr;
5643 if (!CmpXZ.has_value()) {
5644 std::swap(X, Y);
5645 std::swap(CmpXZ, CmpYZ);
5646 }
5647
5648 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5649 if (CmpYZ.has_value())
5650 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5651 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5652 };
5653
5654 switch (Pred) {
5655 case ICmpInst::ICMP_EQ:
5656 case ICmpInst::ICMP_NE: {
5657 // If X == Z:
5658 // Expr Result
5659 // min(X, Y) == Z X <= Y
5660 // max(X, Y) == Z X >= Y
5661 // min(X, Y) != Z X > Y
5662 // max(X, Y) != Z X < Y
5663 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5664 ICmpInst::Predicate NewPred =
5665 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5666 if (Pred == ICmpInst::ICMP_NE)
5667 NewPred = ICmpInst::getInversePredicate(NewPred);
5668 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5669 }
5670 // Otherwise (X != Z):
5671 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5672 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5673 if (!MinMaxCmpXZ.has_value()) {
5674 std::swap(X, Y);
5675 std::swap(CmpXZ, CmpYZ);
5676 // Re-check pre-condition X != Z
5677 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5678 break;
5679 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5680 }
5681 if (!MinMaxCmpXZ.has_value())
5682 break;
5683 if (*MinMaxCmpXZ) {
5684 // Expr Fact Result
5685 // min(X, Y) == Z X < Z false
5686 // max(X, Y) == Z X > Z false
5687 // min(X, Y) != Z X < Z true
5688 // max(X, Y) != Z X > Z true
5689 return replaceInstUsesWith(
5690 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5691 } else {
5692 // Expr Fact Result
5693 // min(X, Y) == Z X > Z Y == Z
5694 // max(X, Y) == Z X < Z Y == Z
5695 // min(X, Y) != Z X > Z Y != Z
5696 // max(X, Y) != Z X < Z Y != Z
5697 return FoldIntoCmpYZ();
5698 }
5699 break;
5700 }
5701 case ICmpInst::ICMP_SLT:
5702 case ICmpInst::ICMP_ULT:
5703 case ICmpInst::ICMP_SLE:
5704 case ICmpInst::ICMP_ULE:
5705 case ICmpInst::ICMP_SGT:
5706 case ICmpInst::ICMP_UGT:
5707 case ICmpInst::ICMP_SGE:
5708 case ICmpInst::ICMP_UGE: {
5709 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5710 if (*CmpXZ) {
5711 if (IsSame) {
5712 // Expr Fact Result
5713 // min(X, Y) < Z X < Z true
5714 // min(X, Y) <= Z X <= Z true
5715 // max(X, Y) > Z X > Z true
5716 // max(X, Y) >= Z X >= Z true
5717 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5718 } else {
5719 // Expr Fact Result
5720 // max(X, Y) < Z X < Z Y < Z
5721 // max(X, Y) <= Z X <= Z Y <= Z
5722 // min(X, Y) > Z X > Z Y > Z
5723 // min(X, Y) >= Z X >= Z Y >= Z
5724 return FoldIntoCmpYZ();
5725 }
5726 } else {
5727 if (IsSame) {
5728 // Expr Fact Result
5729 // min(X, Y) < Z X >= Z Y < Z
5730 // min(X, Y) <= Z X > Z Y <= Z
5731 // max(X, Y) > Z X <= Z Y > Z
5732 // max(X, Y) >= Z X < Z Y >= Z
5733 return FoldIntoCmpYZ();
5734 } else {
5735 // Expr Fact Result
5736 // max(X, Y) < Z X >= Z false
5737 // max(X, Y) <= Z X > Z false
5738 // min(X, Y) > Z X <= Z false
5739 // min(X, Y) >= Z X < Z false
5740 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5741 }
5742 }
5743 break;
5744 }
5745 default:
5746 break;
5747 }
5748
5749 return nullptr;
5750}
5751
5752// Canonicalize checking for a power-of-2-or-zero value:
5754 InstCombiner::BuilderTy &Builder) {
5755 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5756 const CmpInst::Predicate Pred = I.getPredicate();
5757 Value *A = nullptr;
5758 bool CheckIs;
5759 if (I.isEquality()) {
5760 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5761 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5762 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5763 m_Deferred(A)))) ||
5764 !match(Op1, m_ZeroInt()))
5765 A = nullptr;
5766
5767 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5768 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5769 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5770 A = Op1;
5771 else if (match(Op1,
5773 A = Op0;
5774
5775 CheckIs = Pred == ICmpInst::ICMP_EQ;
5776 } else if (ICmpInst::isUnsigned(Pred)) {
5777 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5778 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5779
5780 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5782 m_Specific(Op1))))) {
5783 A = Op1;
5784 CheckIs = Pred == ICmpInst::ICMP_UGE;
5785 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5787 m_Specific(Op0))))) {
5788 A = Op0;
5789 CheckIs = Pred == ICmpInst::ICMP_ULE;
5790 }
5791 }
5792
5793 if (A) {
5794 Type *Ty = A->getType();
5795 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5796 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5797 ConstantInt::get(Ty, 2))
5798 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5799 ConstantInt::get(Ty, 1));
5800 }
5801
5802 return nullptr;
5803}
5804
5806 if (!I.isEquality())
5807 return nullptr;
5808
5809 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5810 const CmpInst::Predicate Pred = I.getPredicate();
5811 Value *A, *B, *C, *D;
5812 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5813 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5814 Value *OtherVal = A == Op1 ? B : A;
5815 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5816 }
5817
5818 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
5819 // A^c1 == C^c2 --> A == C^(c1^c2)
5820 ConstantInt *C1, *C2;
5821 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
5822 Op1->hasOneUse()) {
5823 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
5825 return new ICmpInst(Pred, A, Xor);
5826 }
5827
5828 // A^B == A^D -> B == D
5829 if (A == C)
5830 return new ICmpInst(Pred, B, D);
5831 if (A == D)
5832 return new ICmpInst(Pred, B, C);
5833 if (B == C)
5834 return new ICmpInst(Pred, A, D);
5835 if (B == D)
5836 return new ICmpInst(Pred, A, C);
5837 }
5838 }
5839
5840 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
5841 // A == (A^B) -> B == 0
5842 Value *OtherVal = A == Op0 ? B : A;
5843 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5844 }
5845
5846 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
5847 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
5848 match(Op1, m_And(m_Value(C), m_Value(D)))) {
5849 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
5850
5851 if (A == C) {
5852 X = B;
5853 Y = D;
5854 Z = A;
5855 } else if (A == D) {
5856 X = B;
5857 Y = C;
5858 Z = A;
5859 } else if (B == C) {
5860 X = A;
5861 Y = D;
5862 Z = B;
5863 } else if (B == D) {
5864 X = A;
5865 Y = C;
5866 Z = B;
5867 }
5868
5869 if (X) {
5870 // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
5871 // will fold to `icmp ult/uge Z, -NegP2` incurringb no additional
5872 // instructions.
5873 const APInt *C0, *C1;
5874 bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
5875 (*C0 ^ *C1).isNegatedPowerOf2();
5876
5877 // If either Op0/Op1 are both one use or X^Y will constant fold and one of
5878 // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
5879 // but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.
5880 int UseCnt =
5881 int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
5882 (int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
5883 if (XorIsNegP2 || UseCnt >= 2) {
5884 // Build (X^Y) & Z
5885 Op1 = Builder.CreateXor(X, Y);
5886 Op1 = Builder.CreateAnd(Op1, Z);
5887 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
5888 }
5889 }
5890 }
5891
5892 {
5893 // Similar to above, but specialized for constant because invert is needed:
5894 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
5895 Value *X, *Y;
5896 Constant *C;
5897 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
5898 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
5901 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
5902 }
5903 }
5904
5905 if (match(Op1, m_ZExt(m_Value(A))) &&
5906 (Op0->hasOneUse() || Op1->hasOneUse())) {
5907 // (B & (Pow2C-1)) == zext A --> A == trunc B
5908 // (B & (Pow2C-1)) != zext A --> A != trunc B
5909 const APInt *MaskC;
5910 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
5911 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
5912 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
5913 }
5914
5915 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
5916 // For lshr and ashr pairs.
5917 const APInt *AP1, *AP2;
5918 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5919 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
5920 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5921 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
5922 if (AP1 != AP2)
5923 return nullptr;
5924 unsigned TypeBits = AP1->getBitWidth();
5925 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
5926 if (ShAmt < TypeBits && ShAmt != 0) {
5927 ICmpInst::Predicate NewPred =
5929 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5930 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
5931 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
5932 }
5933 }
5934
5935 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
5936 ConstantInt *Cst1;
5937 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
5938 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
5939 unsigned TypeBits = Cst1->getBitWidth();
5940 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
5941 if (ShAmt < TypeBits && ShAmt != 0) {
5942 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5943 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
5944 Value *And =
5945 Builder.CreateAnd(Xor, Builder.getInt(AndVal), I.getName() + ".mask");
5946 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
5947 }
5948 }
5949
5950 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
5951 // "icmp (and X, mask), cst"
5952 uint64_t ShAmt = 0;
5953 if (Op0->hasOneUse() &&
5954 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
5955 match(Op1, m_ConstantInt(Cst1)) &&
5956 // Only do this when A has multiple uses. This is most important to do
5957 // when it exposes other optimizations.
5958 !A->hasOneUse()) {
5959 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
5960
5961 if (ShAmt < ASize) {
5962 APInt MaskV =
5964 MaskV <<= ShAmt;
5965
5966 APInt CmpV = Cst1->getValue().zext(ASize);
5967 CmpV <<= ShAmt;
5968
5969 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
5970 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
5971 }
5972 }
5973
5975 return ICmp;
5976
5977 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks
5978 // the top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s
5979 // INT_MAX", which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a
5980 // few steps of instcombine.
5981 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
5982 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
5984 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
5985 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
5987 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
5988 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
5990 Add, ConstantInt::get(A->getType(), C.shl(1)));
5991 }
5992
5993 // Canonicalize:
5994 // Assume B_Pow2 != 0
5995 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
5996 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
5997 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
5998 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, 0, &I))
5999 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6001
6002 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
6003 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, 0, &I))
6004 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
6005 ConstantInt::getNullValue(Op1->getType()));
6006
6007 // Canonicalize:
6008 // icmp eq/ne X, OneUse(rotate-right(X))
6009 // -> icmp eq/ne X, rotate-left(X)
6010 // We generally try to convert rotate-right -> rotate-left, this just
6011 // canonicalizes another case.
6012 if (match(&I, m_c_ICmp(m_Value(A),
6013 m_OneUse(m_Intrinsic<Intrinsic::fshr>(
6014 m_Deferred(A), m_Deferred(A), m_Value(B))))))
6015 return new ICmpInst(
6016 Pred, A,
6017 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
6018
6019 // Canonicalize:
6020 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
6021 Constant *Cst;
6024 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
6025
6026 {
6027 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6028 auto m_Matcher =
6031 m_Sub(m_Value(B), m_Deferred(A)));
6032 std::optional<bool> IsZero = std::nullopt;
6033 if (match(&I, m_c_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)),
6034 m_Deferred(A))))
6035 IsZero = false;
6036 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6037 else if (match(&I,
6038 m_ICmp(m_OneUse(m_c_And(m_Value(A), m_Matcher)), m_Zero())))
6039 IsZero = true;
6040
6041 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, /*Depth*/ 0, &I))
6042 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
6043 // -> (icmp eq/ne (and X, P2), 0)
6044 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
6045 // -> (icmp eq/ne (and X, P2), P2)
6046 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
6047 *IsZero ? A
6048 : ConstantInt::getNullValue(A->getType()));
6049 }
6050
6051 return nullptr;
6052}
6053
6055 ICmpInst::Predicate Pred = ICmp.getPredicate();
6056 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
6057
6058 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
6059 // The trunc masks high bits while the compare may effectively mask low bits.
6060 Value *X;
6061 const APInt *C;
6062 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
6063 return nullptr;
6064
6065 // This matches patterns corresponding to tests of the signbit as well as:
6066 // (trunc X) pred C2 --> (X & Mask) == C
6067 if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*WithTrunc=*/true,
6068 /*AllowNonZeroC=*/true)) {
6069 Value *And = Builder.CreateAnd(Res->X, Res->Mask);
6070 Constant *C = ConstantInt::get(Res->X->getType(), Res->C);
6071 return new ICmpInst(Res->Pred, And, C);
6072 }
6073
6074 unsigned SrcBits = X->getType()->getScalarSizeInBits();
6075 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
6076 if (II->getIntrinsicID() == Intrinsic::cttz ||
6077 II->getIntrinsicID() == Intrinsic::ctlz) {
6078 unsigned MaxRet = SrcBits;
6079 // If the "is_zero_poison" argument is set, then we know at least
6080 // one bit is set in the input, so the result is always at least one
6081 // less than the full bitwidth of that input.
6082 if (match(II->getArgOperand(1), m_One()))
6083 MaxRet--;
6084
6085 // Make sure the destination is wide enough to hold the largest output of
6086 // the intrinsic.
6087 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
6088 if (Instruction *I =
6089 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
6090 return I;
6091 }
6092 }
6093
6094 return nullptr;
6095}
6096
6098 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
6099 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
6100 Value *X;
6101 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
6102 return nullptr;
6103
6104 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
6105 bool IsSignedCmp = ICmp.isSigned();
6106
6107 // icmp Pred (ext X), (ext Y)
6108 Value *Y;
6109 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
6110 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
6111 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
6112
6113 if (IsZext0 != IsZext1) {
6114 // If X and Y and both i1
6115 // (icmp eq/ne (zext X) (sext Y))
6116 // eq -> (icmp eq (or X, Y), 0)
6117 // ne -> (icmp ne (or X, Y), 0)
6118 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
6119 Y->getType()->isIntOrIntVectorTy(1))
6120 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
6121 Constant::getNullValue(X->getType()));
6122
6123 // If we have mismatched casts and zext has the nneg flag, we can
6124 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
6125
6126 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
6127 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
6128
6129 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
6130 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
6131
6132 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
6133 IsSignedExt = true;
6134 else
6135 return nullptr;
6136 }
6137
6138 // Not an extension from the same type?
6139 Type *XTy = X->getType(), *YTy = Y->getType();
6140 if (XTy != YTy) {
6141 // One of the casts must have one use because we are creating a new cast.
6142 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
6143 return nullptr;
6144 // Extend the narrower operand to the type of the wider operand.
6145 CastInst::CastOps CastOpcode =
6146 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
6147 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
6148 X = Builder.CreateCast(CastOpcode, X, YTy);
6149 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
6150 Y = Builder.CreateCast(CastOpcode, Y, XTy);
6151 else
6152 return nullptr;
6153 }
6154
6155 // (zext X) == (zext Y) --> X == Y
6156 // (sext X) == (sext Y) --> X == Y
6157 if (ICmp.isEquality())
6158 return new ICmpInst(ICmp.getPredicate(), X, Y);
6159
6160 // A signed comparison of sign extended values simplifies into a
6161 // signed comparison.
6162 if (IsSignedCmp && IsSignedExt)
6163 return new ICmpInst(ICmp.getPredicate(), X, Y);
6164
6165 // The other three cases all fold into an unsigned comparison.
6166 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
6167 }
6168
6169 // Below here, we are only folding a compare with constant.
6170 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
6171 if (!C)
6172 return nullptr;
6173
6174 // If a lossless truncate is possible...
6175 Type *SrcTy = CastOp0->getSrcTy();
6176 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
6177 if (Res) {
6178 if (ICmp.isEquality())
6179 return new ICmpInst(ICmp.getPredicate(), X, Res);
6180
6181 // A signed comparison of sign extended values simplifies into a
6182 // signed comparison.
6183 if (IsSignedExt && IsSignedCmp)
6184 return new ICmpInst(ICmp.getPredicate(), X, Res);
6185
6186 // The other three cases all fold into an unsigned comparison.
6187 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
6188 }
6189
6190 // The re-extended constant changed, partly changed (in the case of a vector),
6191 // or could not be determined to be equal (in the case of a constant
6192 // expression), so the constant cannot be represented in the shorter type.
6193 // All the cases that fold to true or false will have already been handled
6194 // by simplifyICmpInst, so only deal with the tricky case.
6195 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
6196 return nullptr;
6197
6198 // Is source op positive?
6199 // icmp ult (sext X), C --> icmp sgt X, -1
6200 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
6202
6203 // Is source op negative?
6204 // icmp ugt (sext X), C --> icmp slt X, 0
6205 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
6207}
6208
6209/// Handle icmp (cast x), (cast or constant).
6211 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
6212 // icmp compares only pointer's value.
6213 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
6214 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
6215 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
6216 if (SimplifiedOp0 || SimplifiedOp1)
6217 return new ICmpInst(ICmp.getPredicate(),
6218 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
6219 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
6220
6221 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
6222 if (!CastOp0)
6223 return nullptr;
6224 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
6225 return nullptr;
6226
6227 Value *Op0Src = CastOp0->getOperand(0);
6228 Type *SrcTy = CastOp0->getSrcTy();
6229 Type *DestTy = CastOp0->getDestTy();
6230
6231 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6232 // integer type is the same size as the pointer type.
6233 auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6234 if (isa<VectorType>(PtrTy)) {
6235 PtrTy = cast<VectorType>(PtrTy)->getElementType();
6236 IntTy = cast<VectorType>(IntTy)->getElementType();
6237 }
6238 return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
6239 };
6240 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
6241 CompatibleSizes(SrcTy, DestTy)) {
6242 Value *NewOp1 = nullptr;
6243 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
6244 Value *PtrSrc = PtrToIntOp1->getOperand(0);
6245 if (PtrSrc->getType() == Op0Src->getType())
6246 NewOp1 = PtrToIntOp1->getOperand(0);
6247 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6248 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
6249 }
6250
6251 if (NewOp1)
6252 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6253 }
6254
6255 // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6256 if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6257 CompatibleSizes(DestTy, SrcTy)) {
6258 Value *NewOp1 = nullptr;
6259 if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
6260 Value *IntSrc = IntToPtrOp1->getOperand(0);
6261 if (IntSrc->getType() == Op0Src->getType())
6262 NewOp1 = IntToPtrOp1->getOperand(0);
6263 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6264 NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
6265 }
6266
6267 if (NewOp1)
6268 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6269 }
6270
6271 if (Instruction *R = foldICmpWithTrunc(ICmp))
6272 return R;
6273
6274 return foldICmpWithZextOrSext(ICmp);
6275}
6276
6278 bool IsSigned) {
6279 switch (BinaryOp) {
6280 default:
6281 llvm_unreachable("Unsupported binary op");
6282 case Instruction::Add:
6283 case Instruction::Sub:
6284 return match(RHS, m_Zero());
6285 case Instruction::Mul:
6286 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
6287 match(RHS, m_One());
6288 }
6289}
6290
6293 bool IsSigned, Value *LHS, Value *RHS,
6294 Instruction *CxtI) const {
6295 switch (BinaryOp) {
6296 default:
6297 llvm_unreachable("Unsupported binary op");
6298 case Instruction::Add:
6299 if (IsSigned)
6300 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
6301 else
6302 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
6303 case Instruction::Sub:
6304 if (IsSigned)
6305 return computeOverflowForSignedSub(LHS, RHS, CxtI);
6306 else
6307 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
6308 case Instruction::Mul:
6309 if (IsSigned)
6310 return computeOverflowForSignedMul(LHS, RHS, CxtI);
6311 else
6312 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
6313 }
6314}
6315
6316bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
6317 bool IsSigned, Value *LHS,
6318 Value *RHS, Instruction &OrigI,
6319 Value *&Result,
6320 Constant *&Overflow) {
6321 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
6322 std::swap(LHS, RHS);
6323
6324 // If the overflow check was an add followed by a compare, the insertion point
6325 // may be pointing to the compare. We want to insert the new instructions
6326 // before the add in case there are uses of the add between the add and the
6327 // compare.
6328 Builder.SetInsertPoint(&OrigI);
6329
6330 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
6331 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
6332 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
6333
6334 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
6335 Result = LHS;
6336 Overflow = ConstantInt::getFalse(OverflowTy);
6337 return true;
6338 }
6339
6340 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
6342 return false;
6345 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6346 Result->takeName(&OrigI);
6347 Overflow = ConstantInt::getTrue(OverflowTy);
6348 return true;
6350 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
6351 Result->takeName(&OrigI);
6352 Overflow = ConstantInt::getFalse(OverflowTy);
6353 if (auto *Inst = dyn_cast<Instruction>(Result)) {
6354 if (IsSigned)
6355 Inst->setHasNoSignedWrap();
6356 else
6357 Inst->setHasNoUnsignedWrap();
6358 }
6359 return true;
6360 }
6361
6362 llvm_unreachable("Unexpected overflow result");
6363}
6364
6365/// Recognize and process idiom involving test for multiplication
6366/// overflow.
6367///
6368/// The caller has matched a pattern of the form:
6369/// I = cmp u (mul(zext A, zext B), V
6370/// The function checks if this is a test for overflow and if so replaces
6371/// multiplication with call to 'mul.with.overflow' intrinsic.
6372///
6373/// \param I Compare instruction.
6374/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6375/// the compare instruction. Must be of integer type.
6376/// \param OtherVal The other argument of compare instruction.
6377/// \returns Instruction which must replace the compare instruction, NULL if no
6378/// replacement required.
6380 const APInt *OtherVal,
6381 InstCombinerImpl &IC) {
6382 // Don't bother doing this transformation for pointers, don't do it for
6383 // vectors.
6384 if (!isa<IntegerType>(MulVal->getType()))
6385 return nullptr;
6386
6387 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6388 if (!MulInstr)
6389 return nullptr;
6390 assert(MulInstr->getOpcode() == Instruction::Mul);
6391
6392 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6393 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6394 assert(LHS->getOpcode() == Instruction::ZExt);
6395 assert(RHS->getOpcode() == Instruction::ZExt);
6396 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6397
6398 // Calculate type and width of the result produced by mul.with.overflow.
6399 Type *TyA = A->getType(), *TyB = B->getType();
6400 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6401 WidthB = TyB->getPrimitiveSizeInBits();
6402 unsigned MulWidth;
6403 Type *MulType;
6404 if (WidthB > WidthA) {
6405 MulWidth = WidthB;
6406 MulType = TyB;
6407 } else {
6408 MulWidth = WidthA;
6409 MulType = TyA;
6410 }
6411
6412 // In order to replace the original mul with a narrower mul.with.overflow,
6413 // all uses must ignore upper bits of the product. The number of used low
6414 // bits must be not greater than the width of mul.with.overflow.
6415 if (MulVal->hasNUsesOrMore(2))
6416 for (User *U : MulVal->users()) {
6417 if (U == &I)
6418 continue;
6419 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6420 // Check if truncation ignores bits above MulWidth.
6421 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6422 if (TruncWidth > MulWidth)
6423 return nullptr;
6424 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6425 // Check if AND ignores bits above MulWidth.
6426 if (BO->getOpcode() != Instruction::And)
6427 return nullptr;
6428 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6429 const APInt &CVal = CI->getValue();
6430 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6431 return nullptr;
6432 } else {
6433 // In this case we could have the operand of the binary operation
6434 // being defined in another block, and performing the replacement
6435 // could break the dominance relation.
6436 return nullptr;
6437 }
6438 } else {
6439 // Other uses prohibit this transformation.
6440 return nullptr;
6441 }
6442 }
6443
6444 // Recognize patterns
6445 switch (I.getPredicate()) {
6446 case ICmpInst::ICMP_UGT: {
6447 // Recognize pattern:
6448 // mulval = mul(zext A, zext B)
6449 // cmp ugt mulval, max
6450 APInt MaxVal = APInt::getMaxValue(MulWidth);
6451 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6452 if (MaxVal.eq(*OtherVal))
6453 break; // Recognized
6454 return nullptr;
6455 }
6456
6457 case ICmpInst::ICMP_ULT: {
6458 // Recognize pattern:
6459 // mulval = mul(zext A, zext B)
6460 // cmp ule mulval, max + 1
6461 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6462 if (MaxVal.eq(*OtherVal))
6463 break; // Recognized
6464 return nullptr;
6465 }
6466
6467 default:
6468 return nullptr;
6469 }
6470
6471 InstCombiner::BuilderTy &Builder = IC.Builder;
6472 Builder.SetInsertPoint(MulInstr);
6473
6474 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6475 Value *MulA = A, *MulB = B;
6476 if (WidthA < MulWidth)
6477 MulA = Builder.CreateZExt(A, MulType);
6478 if (WidthB < MulWidth)
6479 MulB = Builder.CreateZExt(B, MulType);
6480 CallInst *Call =
6481 Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, MulType,
6482 {MulA, MulB}, /*FMFSource=*/nullptr, "umul");
6483 IC.addToWorklist(MulInstr);
6484
6485 // If there are uses of mul result other than the comparison, we know that
6486 // they are truncation or binary AND. Change them to use result of
6487 // mul.with.overflow and adjust properly mask/size.
6488 if (MulVal->hasNUsesOrMore(2)) {
6489 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6490 for (User *U : make_early_inc_range(MulVal->users())) {
6491 if (U == &I)
6492 continue;
6493 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6494 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6495 IC.replaceInstUsesWith(*TI, Mul);
6496 else
6497 TI->setOperand(0, Mul);
6498 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6499 assert(BO->getOpcode() == Instruction::And);
6500 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6501 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6502 APInt ShortMask = CI->getValue().trunc(MulWidth);
6503 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6504 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6505 IC.replaceInstUsesWith(*BO, Zext);
6506 } else {
6507 llvm_unreachable("Unexpected Binary operation");
6508 }
6509 IC.addToWorklist(cast<Instruction>(U));
6510 }
6511 }
6512
6513 // The original icmp gets replaced with the overflow value, maybe inverted
6514 // depending on predicate.
6515 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6516 Value *Res = Builder.CreateExtractValue(Call, 1);
6517 return BinaryOperator::CreateNot(Res);
6518 }
6519
6520 return ExtractValueInst::Create(Call, 1);
6521}
6522
6523/// When performing a comparison against a constant, it is possible that not all
6524/// the bits in the LHS are demanded. This helper method computes the mask that
6525/// IS demanded.
6527 const APInt *RHS;
6528 if (!match(I.getOperand(1), m_APInt(RHS)))
6530
6531 // If this is a normal comparison, it demands all bits. If it is a sign bit
6532 // comparison, it only demands the sign bit.
6533 bool UnusedBit;
6534 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6536
6537 switch (I.getPredicate()) {
6538 // For a UGT comparison, we don't care about any bits that
6539 // correspond to the trailing ones of the comparand. The value of these
6540 // bits doesn't impact the outcome of the comparison, because any value
6541 // greater than the RHS must differ in a bit higher than these due to carry.
6542 case ICmpInst::ICMP_UGT:
6543 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6544
6545 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6546 // Any value less than the RHS must differ in a higher bit because of carries.
6547 case ICmpInst::ICMP_ULT:
6548 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6549
6550 default:
6552 }
6553}
6554
6555/// Check that one use is in the same block as the definition and all
6556/// other uses are in blocks dominated by a given block.
6557///
6558/// \param DI Definition
6559/// \param UI Use
6560/// \param DB Block that must dominate all uses of \p DI outside
6561/// the parent block
6562/// \return true when \p UI is the only use of \p DI in the parent block
6563/// and all other uses of \p DI are in blocks dominated by \p DB.
6564///
6566 const Instruction *UI,
6567 const BasicBlock *DB) const {
6568 assert(DI && UI && "Instruction not defined\n");
6569 // Ignore incomplete definitions.
6570 if (!DI->getParent())
6571 return false;
6572 // DI and UI must be in the same block.
6573 if (DI->getParent() != UI->getParent())
6574 return false;
6575 // Protect from self-referencing blocks.
6576 if (DI->getParent() == DB)
6577 return false;
6578 for (const User *U : DI->users()) {
6579 auto *Usr = cast<Instruction>(U);
6580 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6581 return false;
6582 }
6583 return true;
6584}
6585
6586/// Return true when the instruction sequence within a block is select-cmp-br.
6587static bool isChainSelectCmpBranch(const SelectInst *SI) {
6588 const BasicBlock *BB = SI->getParent();
6589 if (!BB)
6590 return false;
6591 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
6592 if (!BI || BI->getNumSuccessors() != 2)
6593 return false;
6594 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6595 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6596 return false;
6597 return true;
6598}
6599
6600/// True when a select result is replaced by one of its operands
6601/// in select-icmp sequence. This will eventually result in the elimination
6602/// of the select.
6603///
6604/// \param SI Select instruction
6605/// \param Icmp Compare instruction
6606/// \param SIOpd Operand that replaces the select
6607///
6608/// Notes:
6609/// - The replacement is global and requires dominator information
6610/// - The caller is responsible for the actual replacement
6611///
6612/// Example:
6613///
6614/// entry:
6615/// %4 = select i1 %3, %C* %0, %C* null
6616/// %5 = icmp eq %C* %4, null
6617/// br i1 %5, label %9, label %7
6618/// ...
6619/// ; <label>:7 ; preds = %entry
6620/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6621/// ...
6622///
6623/// can be transformed to
6624///
6625/// %5 = icmp eq %C* %0, null
6626/// %6 = select i1 %3, i1 %5, i1 true
6627/// br i1 %6, label %9, label %7
6628/// ...
6629/// ; <label>:7 ; preds = %entry
6630/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6631///
6632/// Similar when the first operand of the select is a constant or/and
6633/// the compare is for not equal rather than equal.
6634///
6635/// NOTE: The function is only called when the select and compare constants
6636/// are equal, the optimization can work only for EQ predicates. This is not a
6637/// major restriction since a NE compare should be 'normalized' to an equal
6638/// compare, which usually happens in the combiner and test case
6639/// select-cmp-br.ll checks for it.
6641 const ICmpInst *Icmp,
6642 const unsigned SIOpd) {
6643 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6645 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6646 // The check for the single predecessor is not the best that can be
6647 // done. But it protects efficiently against cases like when SI's
6648 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6649 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6650 // replaced can be reached on either path. So the uniqueness check
6651 // guarantees that the path all uses of SI (outside SI's parent) are on
6652 // is disjoint from all other paths out of SI. But that information
6653 // is more expensive to compute, and the trade-off here is in favor
6654 // of compile-time. It should also be noticed that we check for a single
6655 // predecessor and not only uniqueness. This to handle the situation when
6656 // Succ and Succ1 points to the same basic block.
6657 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6658 NumSel++;
6659 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6660 return true;
6661 }
6662 }
6663 return false;
6664}
6665
6666/// Try to fold the comparison based on range information we can get by checking
6667/// whether bits are known to be zero or one in the inputs.
6669 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6670 Type *Ty = Op0->getType();
6671 ICmpInst::Predicate Pred = I.getPredicate();
6672
6673 // Get scalar or pointer size.
6674 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6675 ? Ty->getScalarSizeInBits()
6677
6678 if (!BitWidth)
6679 return nullptr;
6680
6681 KnownBits Op0Known(BitWidth);
6682 KnownBits Op1Known(BitWidth);
6683
6684 {
6685 // Don't use dominating conditions when folding icmp using known bits. This
6686 // may convert signed into unsigned predicates in ways that other passes
6687 // (especially IndVarSimplify) may not be able to reliably undo.
6690 Op0Known, /*Depth=*/0, Q))
6691 return &I;
6692
6694 /*Depth=*/0, Q))
6695 return &I;
6696 }
6697
6698 if (!isa<Constant>(Op0) && Op0Known.isConstant())
6699 return new ICmpInst(
6700 Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1);
6701 if (!isa<Constant>(Op1) && Op1Known.isConstant())
6702 return new ICmpInst(
6703 Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Known.getConstant()));
6704
6705 if (std::optional<bool> Res = ICmpInst::compare(Op0Known, Op1Known, Pred))
6706 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *Res));
6707
6708 // Given the known and unknown bits, compute a range that the LHS could be
6709 // in. Compute the Min, Max and RHS values based on the known bits. For the
6710 // EQ and NE we use unsigned values.
6711 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6712 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6713 if (I.isSigned()) {
6714 Op0Min = Op0Known.getSignedMinValue();
6715 Op0Max = Op0Known.getSignedMaxValue();
6716 Op1Min = Op1Known.getSignedMinValue();
6717 Op1Max = Op1Known.getSignedMaxValue();
6718 } else {
6719 Op0Min = Op0Known.getMinValue();
6720 Op0Max = Op0Known.getMaxValue();
6721 Op1Min = Op1Known.getMinValue();
6722 Op1Max = Op1Known.getMaxValue();
6723 }
6724
6725 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6726 // min/max canonical compare with some other compare. That could lead to
6727 // conflict with select canonicalization and infinite looping.
6728 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6729 auto isMinMaxCmp = [&](Instruction &Cmp) {
6730 if (!Cmp.hasOneUse())
6731 return false;
6732 Value *A, *B;
6733 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6735 return false;
6736 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6737 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6738 };
6739 if (!isMinMaxCmp(I)) {
6740 switch (Pred) {
6741 default:
6742 break;
6743 case ICmpInst::ICMP_ULT: {
6744 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6745 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6746 const APInt *CmpC;
6747 if (match(Op1, m_APInt(CmpC))) {
6748 // A <u C -> A == C-1 if min(A)+1 == C
6749 if (*CmpC == Op0Min + 1)
6750 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6751 ConstantInt::get(Op1->getType(), *CmpC - 1));
6752 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6753 // exceeds the log2 of C.
6754 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6755 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6756 Constant::getNullValue(Op1->getType()));
6757 }
6758 break;
6759 }
6760 case ICmpInst::ICMP_UGT: {
6761 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6762 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6763 const APInt *CmpC;
6764 if (match(Op1, m_APInt(CmpC))) {
6765 // A >u C -> A == C+1 if max(a)-1 == C
6766 if (*CmpC == Op0Max - 1)
6767 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6768 ConstantInt::get(Op1->getType(), *CmpC + 1));
6769 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6770 // exceeds the log2 of C.
6771 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6772 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6773 Constant::getNullValue(Op1->getType()));
6774 }
6775 break;
6776 }
6777 case ICmpInst::ICMP_SLT: {
6778 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6779 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6780 const APInt *CmpC;
6781 if (match(Op1, m_APInt(CmpC))) {
6782 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6783 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6784 ConstantInt::get(Op1->getType(), *CmpC - 1));
6785 }
6786 break;
6787 }
6788 case ICmpInst::ICMP_SGT: {
6789 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6790 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6791 const APInt *CmpC;
6792 if (match(Op1, m_APInt(CmpC))) {
6793 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6794 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6795 ConstantInt::get(Op1->getType(), *CmpC + 1));
6796 }
6797 break;
6798 }
6799 }
6800 }
6801
6802 // Based on the range information we know about the LHS, see if we can
6803 // simplify this comparison. For example, (x&4) < 8 is always true.
6804 switch (Pred) {
6805 default:
6806 break;
6807 case ICmpInst::ICMP_EQ:
6808 case ICmpInst::ICMP_NE: {
6809 // If all bits are known zero except for one, then we know at most one bit
6810 // is set. If the comparison is against zero, then this is a check to see if
6811 // *that* bit is set.
6812 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
6813 if (Op1Known.isZero()) {
6814 // If the LHS is an AND with the same constant, look through it.
6815 Value *LHS = nullptr;
6816 const APInt *LHSC;
6817 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
6818 *LHSC != Op0KnownZeroInverted)
6819 LHS = Op0;
6820
6821 Value *X;
6822 const APInt *C1;
6823 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
6824 Type *XTy = X->getType();
6825 unsigned Log2C1 = C1->countr_zero();
6826 APInt C2 = Op0KnownZeroInverted;
6827 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
6828 if (C2Pow2.isPowerOf2()) {
6829 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
6830 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
6831 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
6832 unsigned Log2C2 = C2Pow2.countr_zero();
6833 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
6834 auto NewPred =
6836 return new ICmpInst(NewPred, X, CmpC);
6837 }
6838 }
6839 }
6840
6841 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
6842 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
6843 (Op0Known & Op1Known) == Op0Known)
6844 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6845 ConstantInt::getNullValue(Op1->getType()));
6846 break;
6847 }
6848 case ICmpInst::ICMP_SGE:
6849 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
6850 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6851 break;
6852 case ICmpInst::ICMP_SLE:
6853 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
6854 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6855 break;
6856 case ICmpInst::ICMP_UGE:
6857 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
6858 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6859 break;
6860 case ICmpInst::ICMP_ULE:
6861 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
6862 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6863 break;
6864 }
6865
6866 // Turn a signed comparison into an unsigned one if both operands are known to
6867 // have the same sign. Set samesign if possible (except for equality
6868 // predicates).
6869 if ((I.isSigned() || (I.isUnsigned() && !I.hasSameSign())) &&
6870 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
6871 (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) {
6872 I.setPredicate(I.getUnsignedPredicate());
6873 I.setSameSign();
6874 return &I;
6875 }
6876
6877 return nullptr;
6878}
6879
6880/// If one operand of an icmp is effectively a bool (value range of {0,1}),
6881/// then try to reduce patterns based on that limit.
6883 Value *X, *Y;
6884 CmpPredicate Pred;
6885
6886 // X must be 0 and bool must be true for "ULT":
6887 // X <u (zext i1 Y) --> (X == 0) & Y
6888 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
6889 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
6890 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
6891
6892 // X must be 0 or bool must be true for "ULE":
6893 // X <=u (sext i1 Y) --> (X == 0) | Y
6894 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
6895 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
6896 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
6897
6898 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
6899 CmpPredicate Pred1, Pred2;
6900 const APInt *C;
6901 Instruction *ExtI;
6902 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
6905 m_APInt(C)))))) &&
6906 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
6907 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
6908 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
6909 auto CreateRangeCheck = [&] {
6910 Value *CmpV1 =
6911 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
6912 Value *CmpV2 = Builder.CreateICmp(
6913 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
6915 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
6916 CmpV1, CmpV2);
6917 };
6918 if (C->isZero()) {
6919 if (Pred2 == ICmpInst::ICMP_EQ) {
6920 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
6921 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
6922 return replaceInstUsesWith(
6923 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6924 } else if (!IsSExt || HasOneUse) {
6925 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
6926 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
6927 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
6928 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X == -1
6929 return CreateRangeCheck();
6930 }
6931 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
6932 if (Pred2 == ICmpInst::ICMP_NE) {
6933 // icmp eq X, (zext (icmp ne X, 1)) --> false
6934 // icmp ne X, (zext (icmp ne X, 1)) --> true
6935 // icmp eq X, (sext (icmp ne X, -1)) --> false
6936 // icmp ne X, (sext (icmp ne X, -1)) --> true
6937 return replaceInstUsesWith(
6938 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6939 } else if (!IsSExt || HasOneUse) {
6940 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
6941 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
6942 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
6943 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
6944 return CreateRangeCheck();
6945 }
6946 } else {
6947 // when C != 0 && C != 1:
6948 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
6949 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
6950 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
6951 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
6952 // when C != 0 && C != -1:
6953 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
6954 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
6955 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
6956 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
6957 return ICmpInst::Create(
6958 Instruction::ICmp, Pred1, X,
6959 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
6960 ? (IsSExt ? -1 : 1)
6961 : 0));
6962 }
6963 }
6964
6965 return nullptr;
6966}
6967
6968/// If we have an icmp le or icmp ge instruction with a constant operand, turn
6969/// it into the appropriate icmp lt or icmp gt instruction. This transform
6970/// allows them to be folded in visitICmpInst.
6972 ICmpInst::Predicate Pred = I.getPredicate();
6973 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
6975 return nullptr;
6976
6977 Value *Op0 = I.getOperand(0);
6978 Value *Op1 = I.getOperand(1);
6979 auto *Op1C = dyn_cast<Constant>(Op1);
6980 if (!Op1C)
6981 return nullptr;
6982
6983 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
6984 if (!FlippedStrictness)
6985 return nullptr;
6986
6987 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
6988}
6989
6990/// If we have a comparison with a non-canonical predicate, if we can update
6991/// all the users, invert the predicate and adjust all the users.
6993 // Is the predicate already canonical?
6994 CmpInst::Predicate Pred = I.getPredicate();
6996 return nullptr;
6997
6998 // Can all users be adjusted to predicate inversion?
6999 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
7000 return nullptr;
7001
7002 // Ok, we can canonicalize comparison!
7003 // Let's first invert the comparison's predicate.
7004 I.setPredicate(CmpInst::getInversePredicate(Pred));
7005 I.setName(I.getName() + ".not");
7006
7007 // And, adapt users.
7009
7010 return &I;
7011}
7012
7013/// Integer compare with boolean values can always be turned into bitwise ops.
7015 InstCombiner::BuilderTy &Builder) {
7016 Value *A = I.getOperand(0), *B = I.getOperand(1);
7017 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
7018
7019 // A boolean compared to true/false can be simplified to Op0/true/false in
7020 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
7021 // Cases not handled by InstSimplify are always 'not' of Op0.
7022 if (match(B, m_Zero())) {
7023 switch (I.getPredicate()) {
7024 case CmpInst::ICMP_EQ: // A == 0 -> !A
7025 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
7026 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
7028 default:
7029 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7030 }
7031 } else if (match(B, m_One())) {
7032 switch (I.getPredicate()) {
7033 case CmpInst::ICMP_NE: // A != 1 -> !A
7034 case CmpInst::ICMP_ULT: // A <u 1 -> !A
7035 case CmpInst::ICMP_SGT: // A >s -1 -> !A
7037 default:
7038 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
7039 }
7040 }
7041
7042 switch (I.getPredicate()) {
7043 default:
7044 llvm_unreachable("Invalid icmp instruction!");
7045 case ICmpInst::ICMP_EQ:
7046 // icmp eq i1 A, B -> ~(A ^ B)
7047 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
7048
7049 case ICmpInst::ICMP_NE:
7050 // icmp ne i1 A, B -> A ^ B
7051 return BinaryOperator::CreateXor(A, B);
7052
7053 case ICmpInst::ICMP_UGT:
7054 // icmp ugt -> icmp ult
7055 std::swap(A, B);
7056 [[fallthrough]];
7057 case ICmpInst::ICMP_ULT:
7058 // icmp ult i1 A, B -> ~A & B
7059 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
7060
7061 case ICmpInst::ICMP_SGT:
7062 // icmp sgt -> icmp slt
7063 std::swap(A, B);
7064 [[fallthrough]];
7065 case ICmpInst::ICMP_SLT:
7066 // icmp slt i1 A, B -> A & ~B
7067 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
7068
7069 case ICmpInst::ICMP_UGE:
7070 // icmp uge -> icmp ule
7071 std::swap(A, B);
7072 [[fallthrough]];
7073 case ICmpInst::ICMP_ULE:
7074 // icmp ule i1 A, B -> ~A | B
7075 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
7076
7077 case ICmpInst::ICMP_SGE:
7078 // icmp sge -> icmp sle
7079 std::swap(A, B);
7080 [[fallthrough]];
7081 case ICmpInst::ICMP_SLE:
7082 // icmp sle i1 A, B -> A | ~B
7083 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
7084 }
7085}
7086
7087// Transform pattern like:
7088// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
7089// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
7090// Into:
7091// (X l>> Y) != 0
7092// (X l>> Y) == 0
7094 InstCombiner::BuilderTy &Builder) {
7095 CmpPredicate Pred, NewPred;
7096 Value *X, *Y;
7097 if (match(&Cmp,
7098 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
7099 switch (Pred) {
7100 case ICmpInst::ICMP_ULE:
7101 NewPred = ICmpInst::ICMP_NE;
7102 break;
7103 case ICmpInst::ICMP_UGT:
7104 NewPred = ICmpInst::ICMP_EQ;
7105 break;
7106 default:
7107 return nullptr;
7108 }
7109 } else if (match(&Cmp, m_c_ICmp(Pred,
7112 m_Add(m_Shl(m_One(), m_Value(Y)),
7113 m_AllOnes()))),
7114 m_Value(X)))) {
7115 // The variant with 'add' is not canonical, (the variant with 'not' is)
7116 // we only get it because it has extra uses, and can't be canonicalized,
7117
7118 switch (Pred) {
7119 case ICmpInst::ICMP_ULT:
7120 NewPred = ICmpInst::ICMP_NE;
7121 break;
7122 case ICmpInst::ICMP_UGE:
7123 NewPred = ICmpInst::ICMP_EQ;
7124 break;
7125 default:
7126 return nullptr;
7127 }
7128 } else
7129 return nullptr;
7130
7131 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
7132 Constant *Zero = Constant::getNullValue(NewX->getType());
7133 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
7134}
7135
7137 InstCombiner::BuilderTy &Builder) {
7138 const CmpInst::Predicate Pred = Cmp.getPredicate();
7139 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
7140 Value *V1, *V2;
7141
7142 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
7143 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
7144 if (auto *I = dyn_cast<Instruction>(V))
7145 I->copyIRFlags(&Cmp);
7146 Module *M = Cmp.getModule();
7148 M, Intrinsic::vector_reverse, V->getType());
7149 return CallInst::Create(F, V);
7150 };
7151
7152 if (match(LHS, m_VecReverse(m_Value(V1)))) {
7153 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
7154 if (match(RHS, m_VecReverse(m_Value(V2))) &&
7155 (LHS->hasOneUse() || RHS->hasOneUse()))
7156 return createCmpReverse(Pred, V1, V2);
7157
7158 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
7159 if (LHS->hasOneUse() && isSplatValue(RHS))
7160 return createCmpReverse(Pred, V1, RHS);
7161 }
7162 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
7163 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
7164 return createCmpReverse(Pred, LHS, V2);
7165
7166 ArrayRef<int> M;
7167 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
7168 return nullptr;
7169
7170 // If both arguments of the cmp are shuffles that use the same mask and
7171 // shuffle within a single vector, move the shuffle after the cmp:
7172 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
7173 Type *V1Ty = V1->getType();
7174 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
7175 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
7176 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
7177 return new ShuffleVectorInst(NewCmp, M);
7178 }
7179
7180 // Try to canonicalize compare with splatted operand and splat constant.
7181 // TODO: We could generalize this for more than splats. See/use the code in
7182 // InstCombiner::foldVectorBinop().
7183 Constant *C;
7184 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
7185 return nullptr;
7186
7187 // Length-changing splats are ok, so adjust the constants as needed:
7188 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
7189 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
7190 int MaskSplatIndex;
7191 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
7192 // We allow poison in matching, but this transform removes it for safety.
7193 // Demanded elements analysis should be able to recover some/all of that.
7194 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
7195 ScalarC);
7196 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
7197 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
7198 return new ShuffleVectorInst(NewCmp, NewM);
7199 }
7200
7201 return nullptr;
7202}
7203
7204// extract(uadd.with.overflow(A, B), 0) ult A
7205// -> extract(uadd.with.overflow(A, B), 1)
7207 CmpInst::Predicate Pred = I.getPredicate();
7208 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7209
7210 Value *UAddOv;
7211 Value *A, *B;
7212 auto UAddOvResultPat = m_ExtractValue<0>(
7213 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));
7214 if (match(Op0, UAddOvResultPat) &&
7215 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
7216 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
7217 (match(A, m_One()) || match(B, m_One()))) ||
7218 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
7219 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
7220 // extract(uadd.with.overflow(A, B), 0) < A
7221 // extract(uadd.with.overflow(A, 1), 0) == 0
7222 // extract(uadd.with.overflow(A, -1), 0) != -1
7223 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
7224 else if (match(Op1, UAddOvResultPat) && Pred == ICmpInst::ICMP_UGT &&
7225 (Op0 == A || Op0 == B))
7226 // A > extract(uadd.with.overflow(A, B), 0)
7227 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
7228 else
7229 return nullptr;
7230
7231 return ExtractValueInst::Create(UAddOv, 1);
7232}
7233
7235 if (!I.getOperand(0)->getType()->isPointerTy() ||
7237 I.getParent()->getParent(),
7238 I.getOperand(0)->getType()->getPointerAddressSpace())) {
7239 return nullptr;
7240 }
7241 Instruction *Op;
7242 if (match(I.getOperand(0), m_Instruction(Op)) &&
7243 match(I.getOperand(1), m_Zero()) &&
7244 Op->isLaunderOrStripInvariantGroup()) {
7245 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7246 Op->getOperand(0), I.getOperand(1));
7247 }
7248 return nullptr;
7249}
7250
7251/// This function folds patterns produced by lowering of reduce idioms, such as
7252/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7253/// attempts to generate fewer number of scalar comparisons instead of vector
7254/// comparisons when possible.
7256 InstCombiner::BuilderTy &Builder,
7257 const DataLayout &DL) {
7258 if (I.getType()->isVectorTy())
7259 return nullptr;
7260 CmpPredicate OuterPred, InnerPred;
7261 Value *LHS, *RHS;
7262
7263 // Match lowering of @llvm.vector.reduce.and. Turn
7264 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7265 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7266 /// %res = icmp <pred> i8 %scalar_ne, 0
7267 ///
7268 /// into
7269 ///
7270 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7271 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7272 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7273 ///
7274 /// for <pred> in {ne, eq}.
7275 if (!match(&I, m_ICmp(OuterPred,
7277 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7278 m_Zero())))
7279 return nullptr;
7280 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7281 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7282 return nullptr;
7283 unsigned NumBits =
7284 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7285 // TODO: Relax this to "not wider than max legal integer type"?
7286 if (!DL.isLegalInteger(NumBits))
7287 return nullptr;
7288
7289 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7290 auto *ScalarTy = Builder.getIntNTy(NumBits);
7291 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7292 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7293 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7294 I.getName());
7295 }
7296
7297 return nullptr;
7298}
7299
7300// This helper will be called with icmp operands in both orders.
7302 Value *Op0, Value *Op1,
7303 ICmpInst &CxtI) {
7304 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7305 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7306 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7307 return NI;
7308
7309 if (auto *SI = dyn_cast<SelectInst>(Op0))
7310 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7311 return NI;
7312
7313 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0))
7314 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7315 return Res;
7316
7317 {
7318 Value *X;
7319 const APInt *C;
7320 // icmp X+Cst, X
7321 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7322 return foldICmpAddOpConst(X, *C, Pred);
7323 }
7324
7325 // abs(X) >= X --> true
7326 // abs(X) u<= X --> true
7327 // abs(X) < X --> false
7328 // abs(X) u> X --> false
7329 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7330 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7331 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7332 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7333 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7334 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7335 {
7336 Value *X;
7337 Constant *C;
7338 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
7339 match(Op1, m_Specific(X))) {
7340 Value *NullValue = Constant::getNullValue(X->getType());
7341 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7342 const APInt SMin =
7343 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7344 bool IsIntMinPosion = C->isAllOnesValue();
7345 switch (Pred) {
7346 case CmpInst::ICMP_ULE:
7347 case CmpInst::ICMP_SGE:
7348 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7349 case CmpInst::ICMP_UGT:
7350 case CmpInst::ICMP_SLT:
7352 case CmpInst::ICMP_UGE:
7353 case CmpInst::ICMP_SLE:
7354 case CmpInst::ICMP_EQ: {
7355 return replaceInstUsesWith(
7356 CxtI, IsIntMinPosion
7357 ? Builder.CreateICmpSGT(X, AllOnesValue)
7359 X, ConstantInt::get(X->getType(), SMin + 1)));
7360 }
7361 case CmpInst::ICMP_ULT:
7362 case CmpInst::ICMP_SGT:
7363 case CmpInst::ICMP_NE: {
7364 return replaceInstUsesWith(
7365 CxtI, IsIntMinPosion
7366 ? Builder.CreateICmpSLT(X, NullValue)
7368 X, ConstantInt::get(X->getType(), SMin)));
7369 }
7370 default:
7371 llvm_unreachable("Invalid predicate!");
7372 }
7373 }
7374 }
7375
7376 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7377 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7378 return replaceInstUsesWith(CxtI, V);
7379
7380 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7381 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7382 {
7383 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7384 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7386 }
7387
7388 if (!ICmpInst::isUnsigned(Pred) &&
7389 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7390 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7392 }
7393 }
7394
7395 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7396 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7397 {
7398 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7399 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7401 }
7402
7403 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7404 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7405 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7407 }
7408 }
7409
7410 return nullptr;
7411}
7412
7414 bool Changed = false;
7416 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7417 unsigned Op0Cplxity = getComplexity(Op0);
7418 unsigned Op1Cplxity = getComplexity(Op1);
7419
7420 /// Orders the operands of the compare so that they are listed from most
7421 /// complex to least complex. This puts constants before unary operators,
7422 /// before binary operators.
7423 if (Op0Cplxity < Op1Cplxity) {
7424 I.swapOperands();
7425 std::swap(Op0, Op1);
7426 Changed = true;
7427 }
7428
7429 if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
7430 return replaceInstUsesWith(I, V);
7431
7432 // Comparing -val or val with non-zero is the same as just comparing val
7433 // ie, abs(val) != 0 -> val != 0
7434 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7435 Value *Cond, *SelectTrue, *SelectFalse;
7436 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7437 m_Value(SelectFalse)))) {
7438 if (Value *V = dyn_castNegVal(SelectTrue)) {
7439 if (V == SelectFalse)
7440 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7441 } else if (Value *V = dyn_castNegVal(SelectFalse)) {
7442 if (V == SelectTrue)
7443 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7444 }
7445 }
7446 }
7447
7448 if (Op0->getType()->isIntOrIntVectorTy(1))
7450 return Res;
7451
7453 return Res;
7454
7456 return Res;
7457
7459 return Res;
7460
7462 return Res;
7463
7465 return Res;
7466
7468 return Res;
7469
7471 return Res;
7472
7473 // Test if the ICmpInst instruction is used exclusively by a select as
7474 // part of a minimum or maximum operation. If so, refrain from doing
7475 // any other folding. This helps out other analyses which understand
7476 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7477 // and CodeGen. And in this case, at least one of the comparison
7478 // operands has at least one user besides the compare (the select),
7479 // which would often largely negate the benefit of folding anyway.
7480 //
7481 // Do the same for the other patterns recognized by matchSelectPattern.
7482 if (I.hasOneUse())
7483 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7484 Value *A, *B;
7486 if (SPR.Flavor != SPF_UNKNOWN)
7487 return nullptr;
7488 }
7489
7490 // Do this after checking for min/max to prevent infinite looping.
7491 if (Instruction *Res = foldICmpWithZero(I))
7492 return Res;
7493
7494 // FIXME: We only do this after checking for min/max to prevent infinite
7495 // looping caused by a reverse canonicalization of these patterns for min/max.
7496 // FIXME: The organization of folds is a mess. These would naturally go into
7497 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7498 // down here after the min/max restriction.
7499 ICmpInst::Predicate Pred = I.getPredicate();
7500 const APInt *C;
7501 if (match(Op1, m_APInt(C))) {
7502 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7503 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7504 Constant *Zero = Constant::getNullValue(Op0->getType());
7505 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7506 }
7507
7508 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7509 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7511 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7512 }
7513 }
7514
7515 // The folds in here may rely on wrapping flags and special constants, so
7516 // they can break up min/max idioms in some cases but not seemingly similar
7517 // patterns.
7518 // FIXME: It may be possible to enhance select folding to make this
7519 // unnecessary. It may also be moot if we canonicalize to min/max
7520 // intrinsics.
7521 if (Instruction *Res = foldICmpBinOp(I, Q))
7522 return Res;
7523
7525 return Res;
7526
7527 // Try to match comparison as a sign bit test. Intentionally do this after
7528 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7529 if (Instruction *New = foldSignBitTest(I))
7530 return New;
7531
7533 return Res;
7534
7535 if (Instruction *Res = foldICmpCommutative(I.getCmpPredicate(), Op0, Op1, I))
7536 return Res;
7537 if (Instruction *Res =
7538 foldICmpCommutative(I.getSwappedCmpPredicate(), Op1, Op0, I))
7539 return Res;
7540
7541 if (I.isCommutative()) {
7542 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7543 replaceOperand(I, 0, Pair->first);
7544 replaceOperand(I, 1, Pair->second);
7545 return &I;
7546 }
7547 }
7548
7549 // In case of a comparison with two select instructions having the same
7550 // condition, check whether one of the resulting branches can be simplified.
7551 // If so, just compare the other branch and select the appropriate result.
7552 // For example:
7553 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7554 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7555 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7556 // The icmp will result false for the false value of selects and the result
7557 // will depend upon the comparison of true values of selects if %cmp is
7558 // true. Thus, transform this into:
7559 // %cmp = icmp slt i32 %y, %z
7560 // %sel = select i1 %cond, i1 %cmp, i1 false
7561 // This handles similar cases to transform.
7562 {
7563 Value *Cond, *A, *B, *C, *D;
7564 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7566 (Op0->hasOneUse() || Op1->hasOneUse())) {
7567 // Check whether comparison of TrueValues can be simplified
7568 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7569 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7570 return SelectInst::Create(Cond, Res, NewICMP);
7571 }
7572 // Check whether comparison of FalseValues can be simplified
7573 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7574 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7575 return SelectInst::Create(Cond, NewICMP, Res);
7576 }
7577 }
7578 }
7579
7580 // Try to optimize equality comparisons against alloca-based pointers.
7581 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7582 assert(Op1->getType()->isPointerTy() &&
7583 "Comparing pointer with non-pointer?");
7584 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7585 if (foldAllocaCmp(Alloca))
7586 return nullptr;
7587 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7588 if (foldAllocaCmp(Alloca))
7589 return nullptr;
7590 }
7591
7592 if (Instruction *Res = foldICmpBitCast(I))
7593 return Res;
7594
7595 // TODO: Hoist this above the min/max bailout.
7597 return R;
7598
7599 {
7600 Value *X, *Y;
7601 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7602 // and (X & ~Y) != 0 --> (X & Y) == 0
7603 // if A is a power of 2.
7604 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7605 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, 0, &I) &&
7606 I.isEquality())
7607 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7608 Op1);
7609
7610 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7611 if (Op0->getType()->isIntOrIntVectorTy()) {
7612 bool ConsumesOp0, ConsumesOp1;
7613 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7614 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7615 (ConsumesOp0 || ConsumesOp1)) {
7616 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7617 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7618 assert(InvOp0 && InvOp1 &&
7619 "Mismatch between isFreeToInvert and getFreelyInverted");
7620 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7621 }
7622 }
7623
7624 Instruction *AddI = nullptr;
7626 m_Instruction(AddI))) &&
7627 isa<IntegerType>(X->getType())) {
7628 Value *Result;
7629 Constant *Overflow;
7630 // m_UAddWithOverflow can match patterns that do not include an explicit
7631 // "add" instruction, so check the opcode of the matched op.
7632 if (AddI->getOpcode() == Instruction::Add &&
7633 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7634 Result, Overflow)) {
7635 replaceInstUsesWith(*AddI, Result);
7636 eraseInstFromFunction(*AddI);
7637 return replaceInstUsesWith(I, Overflow);
7638 }
7639 }
7640
7641 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7642 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7643 match(Op1, m_APInt(C))) {
7644 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7645 return R;
7646 }
7647
7648 // Signbit test folds
7649 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7650 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7651 Instruction *ExtI;
7652 if ((I.isUnsigned() || I.isEquality()) &&
7653 match(Op1,
7655 Y->getType()->getScalarSizeInBits() == 1 &&
7656 (Op0->hasOneUse() || Op1->hasOneUse())) {
7657 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7658 Instruction *ShiftI;
7659 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7661 OpWidth - 1))))) {
7662 unsigned ExtOpc = ExtI->getOpcode();
7663 unsigned ShiftOpc = ShiftI->getOpcode();
7664 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7665 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7666 Value *SLTZero =
7668 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7669 return replaceInstUsesWith(I, Cmp);
7670 }
7671 }
7672 }
7673 }
7674
7675 if (Instruction *Res = foldICmpEquality(I))
7676 return Res;
7677
7679 return Res;
7680
7681 if (Instruction *Res = foldICmpOfUAddOv(I))
7682 return Res;
7683
7684 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7685 // an i1 which indicates whether or not we successfully did the swap.
7686 //
7687 // Replace comparisons between the old value and the expected value with the
7688 // indicator that 'cmpxchg' returns.
7689 //
7690 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7691 // spuriously fail. In those cases, the old value may equal the expected
7692 // value but it is possible for the swap to not occur.
7693 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7694 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7695 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7696 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7697 !ACXI->isWeak())
7698 return ExtractValueInst::Create(ACXI, 1);
7699
7701 return Res;
7702
7703 if (I.getType()->isVectorTy())
7704 if (Instruction *Res = foldVectorCmp(I, Builder))
7705 return Res;
7706
7708 return Res;
7709
7711 return Res;
7712
7713 {
7714 Value *A;
7715 const APInt *C1, *C2;
7716 ICmpInst::Predicate Pred = I.getPredicate();
7717 if (ICmpInst::isEquality(Pred)) {
7718 // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
7719 // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
7720 if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) &&
7721 match(Op1, m_APInt(C2))) {
7722 Type *InputTy = A->getType();
7723 unsigned InputBitWidth = InputTy->getScalarSizeInBits();
7724 // c2 must be non-negative at the bitwidth of a.
7725 if (C2->getActiveBits() < InputBitWidth) {
7726 APInt TruncC1 = C1->trunc(InputBitWidth);
7727 // Check if there are 1s in C1 high bits of size InputBitWidth.
7728 if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth)))
7729 TruncC1.setBit(InputBitWidth - 1);
7730 Value *AndInst = Builder.CreateAnd(A, TruncC1);
7731 return new ICmpInst(
7732 Pred, AndInst,
7733 ConstantInt::get(InputTy, C2->trunc(InputBitWidth)));
7734 }
7735 }
7736 }
7737 }
7738
7739 return Changed ? &I : nullptr;
7740}
7741
7742/// Fold fcmp ([us]itofp x, cst) if possible.
7744 Instruction *LHSI,
7745 Constant *RHSC) {
7746 const APFloat *RHS;
7747 if (!match(RHSC, m_APFloat(RHS)))
7748 return nullptr;
7749
7750 // Get the width of the mantissa. We don't want to hack on conversions that
7751 // might lose information from the integer, e.g. "i64 -> float"
7752 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7753 if (MantissaWidth == -1)
7754 return nullptr; // Unknown.
7755
7756 Type *IntTy = LHSI->getOperand(0)->getType();
7757 unsigned IntWidth = IntTy->getScalarSizeInBits();
7758 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7759
7760 if (I.isEquality()) {
7761 FCmpInst::Predicate P = I.getPredicate();
7762 bool IsExact = false;
7763 APSInt RHSCvt(IntWidth, LHSUnsigned);
7764 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7765
7766 // If the floating point constant isn't an integer value, we know if we will
7767 // ever compare equal / not equal to it.
7768 if (!IsExact) {
7769 // TODO: Can never be -0.0 and other non-representable values
7770 APFloat RHSRoundInt(*RHS);
7772 if (*RHS != RHSRoundInt) {
7774 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7775
7777 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7778 }
7779 }
7780
7781 // TODO: If the constant is exactly representable, is it always OK to do
7782 // equality compares as integer?
7783 }
7784
7785 // Check to see that the input is converted from an integer type that is small
7786 // enough that preserves all bits. TODO: check here for "known" sign bits.
7787 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
7788
7789 // Following test does NOT adjust IntWidth downwards for signed inputs,
7790 // because the most negative value still requires all the mantissa bits
7791 // to distinguish it from one less than that value.
7792 if ((int)IntWidth > MantissaWidth) {
7793 // Conversion would lose accuracy. Check if loss can impact comparison.
7794 int Exp = ilogb(*RHS);
7795 if (Exp == APFloat::IEK_Inf) {
7796 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
7797 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
7798 // Conversion could create infinity.
7799 return nullptr;
7800 } else {
7801 // Note that if RHS is zero or NaN, then Exp is negative
7802 // and first condition is trivially false.
7803 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
7804 // Conversion could affect comparison.
7805 return nullptr;
7806 }
7807 }
7808
7809 // Otherwise, we can potentially simplify the comparison. We know that it
7810 // will always come through as an integer value and we know the constant is
7811 // not a NAN (it would have been previously simplified).
7812 assert(!RHS->isNaN() && "NaN comparison not already folded!");
7813
7815 switch (I.getPredicate()) {
7816 default:
7817 llvm_unreachable("Unexpected predicate!");
7818 case FCmpInst::FCMP_UEQ:
7819 case FCmpInst::FCMP_OEQ:
7820 Pred = ICmpInst::ICMP_EQ;
7821 break;
7822 case FCmpInst::FCMP_UGT:
7823 case FCmpInst::FCMP_OGT:
7824 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
7825 break;
7826 case FCmpInst::FCMP_UGE:
7827 case FCmpInst::FCMP_OGE:
7828 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
7829 break;
7830 case FCmpInst::FCMP_ULT:
7831 case FCmpInst::FCMP_OLT:
7832 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
7833 break;
7834 case FCmpInst::FCMP_ULE:
7835 case FCmpInst::FCMP_OLE:
7836 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
7837 break;
7838 case FCmpInst::FCMP_UNE:
7839 case FCmpInst::FCMP_ONE:
7840 Pred = ICmpInst::ICMP_NE;
7841 break;
7842 case FCmpInst::FCMP_ORD:
7843 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7844 case FCmpInst::FCMP_UNO:
7845 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7846 }
7847
7848 // Now we know that the APFloat is a normal number, zero or inf.
7849
7850 // See if the FP constant is too large for the integer. For example,
7851 // comparing an i8 to 300.0.
7852 if (!LHSUnsigned) {
7853 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
7854 // and large values.
7855 APFloat SMax(RHS->getSemantics());
7856 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
7858 if (SMax < *RHS) { // smax < 13123.0
7859 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
7860 Pred == ICmpInst::ICMP_SLE)
7861 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7862 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7863 }
7864 } else {
7865 // If the RHS value is > UnsignedMax, fold the comparison. This handles
7866 // +INF and large values.
7867 APFloat UMax(RHS->getSemantics());
7868 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
7870 if (UMax < *RHS) { // umax < 13123.0
7871 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
7872 Pred == ICmpInst::ICMP_ULE)
7873 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7874 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7875 }
7876 }
7877
7878 if (!LHSUnsigned) {
7879 // See if the RHS value is < SignedMin.
7880 APFloat SMin(RHS->getSemantics());
7881 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
7883 if (SMin > *RHS) { // smin > 12312.0
7884 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
7885 Pred == ICmpInst::ICMP_SGE)
7886 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7887 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7888 }
7889 } else {
7890 // See if the RHS value is < UnsignedMin.
7891 APFloat UMin(RHS->getSemantics());
7892 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
7894 if (UMin > *RHS) { // umin > 12312.0
7895 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
7896 Pred == ICmpInst::ICMP_UGE)
7897 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7898 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7899 }
7900 }
7901
7902 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
7903 // [0, UMAX], but it may still be fractional. Check whether this is the case
7904 // using the IsExact flag.
7905 // Don't do this for zero, because -0.0 is not fractional.
7906 APSInt RHSInt(IntWidth, LHSUnsigned);
7907 bool IsExact;
7908 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
7909 if (!RHS->isZero()) {
7910 if (!IsExact) {
7911 // If we had a comparison against a fractional value, we have to adjust
7912 // the compare predicate and sometimes the value. RHSC is rounded towards
7913 // zero at this point.
7914 switch (Pred) {
7915 default:
7916 llvm_unreachable("Unexpected integer comparison!");
7917 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
7918 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7919 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
7920 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7921 case ICmpInst::ICMP_ULE:
7922 // (float)int <= 4.4 --> int <= 4
7923 // (float)int <= -4.4 --> false
7924 if (RHS->isNegative())
7925 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7926 break;
7927 case ICmpInst::ICMP_SLE:
7928 // (float)int <= 4.4 --> int <= 4
7929 // (float)int <= -4.4 --> int < -4
7930 if (RHS->isNegative())
7931 Pred = ICmpInst::ICMP_SLT;
7932 break;
7933 case ICmpInst::ICMP_ULT:
7934 // (float)int < -4.4 --> false
7935 // (float)int < 4.4 --> int <= 4
7936 if (RHS->isNegative())
7937 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7938 Pred = ICmpInst::ICMP_ULE;
7939 break;
7940 case ICmpInst::ICMP_SLT:
7941 // (float)int < -4.4 --> int < -4
7942 // (float)int < 4.4 --> int <= 4
7943 if (!RHS->isNegative())
7944 Pred = ICmpInst::ICMP_SLE;
7945 break;
7946 case ICmpInst::ICMP_UGT:
7947 // (float)int > 4.4 --> int > 4
7948 // (float)int > -4.4 --> true
7949 if (RHS->isNegative())
7950 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7951 break;
7952 case ICmpInst::ICMP_SGT:
7953 // (float)int > 4.4 --> int > 4
7954 // (float)int > -4.4 --> int >= -4
7955 if (RHS->isNegative())
7956 Pred = ICmpInst::ICMP_SGE;
7957 break;
7958 case ICmpInst::ICMP_UGE:
7959 // (float)int >= -4.4 --> true
7960 // (float)int >= 4.4 --> int > 4
7961 if (RHS->isNegative())
7962 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7963 Pred = ICmpInst::ICMP_UGT;
7964 break;
7965 case ICmpInst::ICMP_SGE:
7966 // (float)int >= -4.4 --> int >= -4
7967 // (float)int >= 4.4 --> int > 4
7968 if (!RHS->isNegative())
7969 Pred = ICmpInst::ICMP_SGT;
7970 break;
7971 }
7972 }
7973 }
7974
7975 // Lower this FP comparison into an appropriate integer version of the
7976 // comparison.
7977 return new ICmpInst(Pred, LHSI->getOperand(0),
7978 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
7979}
7980
7981/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
7983 Constant *RHSC) {
7984 // When C is not 0.0 and infinities are not allowed:
7985 // (C / X) < 0.0 is a sign-bit test of X
7986 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
7987 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
7988 //
7989 // Proof:
7990 // Multiply (C / X) < 0.0 by X * X / C.
7991 // - X is non zero, if it is the flag 'ninf' is violated.
7992 // - C defines the sign of X * X * C. Thus it also defines whether to swap
7993 // the predicate. C is also non zero by definition.
7994 //
7995 // Thus X * X / C is non zero and the transformation is valid. [qed]
7996
7997 FCmpInst::Predicate Pred = I.getPredicate();
7998
7999 // Check that predicates are valid.
8000 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
8001 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
8002 return nullptr;
8003
8004 // Check that RHS operand is zero.
8005 if (!match(RHSC, m_AnyZeroFP()))
8006 return nullptr;
8007
8008 // Check fastmath flags ('ninf').
8009 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
8010 return nullptr;
8011
8012 // Check the properties of the dividend. It must not be zero to avoid a
8013 // division by zero (see Proof).
8014 const APFloat *C;
8015 if (!match(LHSI->getOperand(0), m_APFloat(C)))
8016 return nullptr;
8017
8018 if (C->isZero())
8019 return nullptr;
8020
8021 // Get swapped predicate if necessary.
8022 if (C->isNegative())
8023 Pred = I.getSwappedPredicate();
8024
8025 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
8026}
8027
8028/// Optimize fabs(X) compared with zero.
8030 Value *X;
8031 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
8032 return nullptr;
8033
8034 const APFloat *C;
8035 if (!match(I.getOperand(1), m_APFloat(C)))
8036 return nullptr;
8037
8038 if (!C->isPosZero()) {
8039 if (!C->isSmallestNormalized())
8040 return nullptr;
8041
8042 const Function *F = I.getFunction();
8043 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
8044 if (Mode.Input == DenormalMode::PreserveSign ||
8045 Mode.Input == DenormalMode::PositiveZero) {
8046
8047 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8048 Constant *Zero = ConstantFP::getZero(X->getType());
8049 return new FCmpInst(P, X, Zero, "", I);
8050 };
8051
8052 switch (I.getPredicate()) {
8053 case FCmpInst::FCMP_OLT:
8054 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
8055 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
8056 case FCmpInst::FCMP_UGE:
8057 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
8058 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
8059 case FCmpInst::FCMP_OGE:
8060 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
8061 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
8062 case FCmpInst::FCMP_ULT:
8063 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
8064 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
8065 default:
8066 break;
8067 }
8068 }
8069
8070 return nullptr;
8071 }
8072
8073 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
8074 I->setPredicate(P);
8075 return IC.replaceOperand(*I, 0, X);
8076 };
8077
8078 switch (I.getPredicate()) {
8079 case FCmpInst::FCMP_UGE:
8080 case FCmpInst::FCMP_OLT:
8081 // fabs(X) >= 0.0 --> true
8082 // fabs(X) < 0.0 --> false
8083 llvm_unreachable("fcmp should have simplified");
8084
8085 case FCmpInst::FCMP_OGT:
8086 // fabs(X) > 0.0 --> X != 0.0
8087 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
8088
8089 case FCmpInst::FCMP_UGT:
8090 // fabs(X) u> 0.0 --> X u!= 0.0
8091 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
8092
8093 case FCmpInst::FCMP_OLE:
8094 // fabs(X) <= 0.0 --> X == 0.0
8095 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
8096
8097 case FCmpInst::FCMP_ULE:
8098 // fabs(X) u<= 0.0 --> X u== 0.0
8099 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
8100
8101 case FCmpInst::FCMP_OGE:
8102 // fabs(X) >= 0.0 --> !isnan(X)
8103 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8104 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
8105
8106 case FCmpInst::FCMP_ULT:
8107 // fabs(X) u< 0.0 --> isnan(X)
8108 assert(!I.hasNoNaNs() && "fcmp should have simplified");
8109 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
8110
8111 case FCmpInst::FCMP_OEQ:
8112 case FCmpInst::FCMP_UEQ:
8113 case FCmpInst::FCMP_ONE:
8114 case FCmpInst::FCMP_UNE:
8115 case FCmpInst::FCMP_ORD:
8116 case FCmpInst::FCMP_UNO:
8117 // Look through the fabs() because it doesn't change anything but the sign.
8118 // fabs(X) == 0.0 --> X == 0.0,
8119 // fabs(X) != 0.0 --> X != 0.0
8120 // isnan(fabs(X)) --> isnan(X)
8121 // !isnan(fabs(X) --> !isnan(X)
8122 return replacePredAndOp0(&I, I.getPredicate(), X);
8123
8124 default:
8125 return nullptr;
8126 }
8127}
8128
8129/// Optimize sqrt(X) compared with zero.
8131 Value *X;
8132 if (!match(I.getOperand(0), m_Sqrt(m_Value(X))))
8133 return nullptr;
8134
8135 if (!match(I.getOperand(1), m_PosZeroFP()))
8136 return nullptr;
8137
8138 auto ReplacePredAndOp0 = [&](FCmpInst::Predicate P) {
8139 I.setPredicate(P);
8140 return IC.replaceOperand(I, 0, X);
8141 };
8142
8143 // Clear ninf flag if sqrt doesn't have it.
8144 if (!cast<Instruction>(I.getOperand(0))->hasNoInfs())
8145 I.setHasNoInfs(false);
8146
8147 switch (I.getPredicate()) {
8148 case FCmpInst::FCMP_OLT:
8149 case FCmpInst::FCMP_UGE:
8150 // sqrt(X) < 0.0 --> false
8151 // sqrt(X) u>= 0.0 --> true
8152 llvm_unreachable("fcmp should have simplified");
8153 case FCmpInst::FCMP_ULT:
8154 case FCmpInst::FCMP_ULE:
8155 case FCmpInst::FCMP_OGT:
8156 case FCmpInst::FCMP_OGE:
8157 case FCmpInst::FCMP_OEQ:
8158 case FCmpInst::FCMP_UNE:
8159 // sqrt(X) u< 0.0 --> X u< 0.0
8160 // sqrt(X) u<= 0.0 --> X u<= 0.0
8161 // sqrt(X) > 0.0 --> X > 0.0
8162 // sqrt(X) >= 0.0 --> X >= 0.0
8163 // sqrt(X) == 0.0 --> X == 0.0
8164 // sqrt(X) u!= 0.0 --> X u!= 0.0
8165 return IC.replaceOperand(I, 0, X);
8166
8167 case FCmpInst::FCMP_OLE:
8168 // sqrt(X) <= 0.0 --> X == 0.0
8169 return ReplacePredAndOp0(FCmpInst::FCMP_OEQ);
8170 case FCmpInst::FCMP_UGT:
8171 // sqrt(X) u> 0.0 --> X u!= 0.0
8172 return ReplacePredAndOp0(FCmpInst::FCMP_UNE);
8173 case FCmpInst::FCMP_UEQ:
8174 // sqrt(X) u== 0.0 --> X u<= 0.0
8175 return ReplacePredAndOp0(FCmpInst::FCMP_ULE);
8176 case FCmpInst::FCMP_ONE:
8177 // sqrt(X) != 0.0 --> X > 0.0
8178 return ReplacePredAndOp0(FCmpInst::FCMP_OGT);
8179 case FCmpInst::FCMP_ORD:
8180 // !isnan(sqrt(X)) --> X >= 0.0
8181 return ReplacePredAndOp0(FCmpInst::FCMP_OGE);
8182 case FCmpInst::FCMP_UNO:
8183 // isnan(sqrt(X)) --> X u< 0.0
8184 return ReplacePredAndOp0(FCmpInst::FCMP_ULT);
8185 default:
8186 llvm_unreachable("Unexpected predicate!");
8187 }
8188}
8189
8191 CmpInst::Predicate Pred = I.getPredicate();
8192 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8193
8194 // Canonicalize fneg as Op1.
8195 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
8196 std::swap(Op0, Op1);
8197 Pred = I.getSwappedPredicate();
8198 }
8199
8200 if (!match(Op1, m_FNeg(m_Specific(Op0))))
8201 return nullptr;
8202
8203 // Replace the negated operand with 0.0:
8204 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
8205 Constant *Zero = ConstantFP::getZero(Op0->getType());
8206 return new FCmpInst(Pred, Op0, Zero, "", &I);
8207}
8208
8210 Constant *RHSC, InstCombinerImpl &CI) {
8211 const CmpInst::Predicate Pred = I.getPredicate();
8212 Value *X = LHSI->getOperand(0);
8213 Value *Y = LHSI->getOperand(1);
8214 switch (Pred) {
8215 default:
8216 break;
8217 case FCmpInst::FCMP_UGT:
8218 case FCmpInst::FCMP_ULT:
8219 case FCmpInst::FCMP_UNE:
8220 case FCmpInst::FCMP_OEQ:
8221 case FCmpInst::FCMP_OGE:
8222 case FCmpInst::FCMP_OLE:
8223 // The optimization is not valid if X and Y are infinities of the same
8224 // sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
8225 // flag then we can assume we do not have that case. Otherwise we might be
8226 // able to prove that either X or Y is not infinity.
8227 if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
8228 !isKnownNeverInfinity(Y, /*Depth=*/0,
8230 !isKnownNeverInfinity(X, /*Depth=*/0,
8232 break;
8233
8234 [[fallthrough]];
8235 case FCmpInst::FCMP_OGT:
8236 case FCmpInst::FCMP_OLT:
8237 case FCmpInst::FCMP_ONE:
8238 case FCmpInst::FCMP_UEQ:
8239 case FCmpInst::FCMP_UGE:
8240 case FCmpInst::FCMP_ULE:
8241 // fcmp pred (x - y), 0 --> fcmp pred x, y
8242 if (match(RHSC, m_AnyZeroFP()) &&
8243 I.getFunction()->getDenormalMode(
8244 LHSI->getType()->getScalarType()->getFltSemantics()) ==
8246 CI.replaceOperand(I, 0, X);
8247 CI.replaceOperand(I, 1, Y);
8248 return &I;
8249 }
8250 break;
8251 }
8252
8253 return nullptr;
8254}
8255
8257 InstCombinerImpl &IC) {
8258 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
8259 Type *OpType = LHS->getType();
8260 CmpInst::Predicate Pred = I.getPredicate();
8261
8262 bool FloorX = match(LHS, m_Intrinsic<Intrinsic::floor>(m_Specific(RHS)));
8263 bool CeilX = match(LHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(RHS)));
8264
8265 if (!FloorX && !CeilX) {
8266 if ((FloorX = match(RHS, m_Intrinsic<Intrinsic::floor>(m_Specific(LHS)))) ||
8267 (CeilX = match(RHS, m_Intrinsic<Intrinsic::ceil>(m_Specific(LHS))))) {
8268 std::swap(LHS, RHS);
8269 Pred = I.getSwappedPredicate();
8270 }
8271 }
8272
8273 switch (Pred) {
8274 case FCmpInst::FCMP_OLE:
8275 // fcmp ole floor(x), x => fcmp ord x, 0
8276 if (FloorX)
8278 "", &I);
8279 break;
8280 case FCmpInst::FCMP_OGT:
8281 // fcmp ogt floor(x), x => false
8282 if (FloorX)
8283 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8284 break;
8285 case FCmpInst::FCMP_OGE:
8286 // fcmp oge ceil(x), x => fcmp ord x, 0
8287 if (CeilX)
8289 "", &I);
8290 break;
8291 case FCmpInst::FCMP_OLT:
8292 // fcmp olt ceil(x), x => false
8293 if (CeilX)
8294 return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8295 break;
8296 case FCmpInst::FCMP_ULE:
8297 // fcmp ule floor(x), x => true
8298 if (FloorX)
8299 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8300 break;
8301 case FCmpInst::FCMP_UGT:
8302 // fcmp ugt floor(x), x => fcmp uno x, 0
8303 if (FloorX)
8305 "", &I);
8306 break;
8307 case FCmpInst::FCMP_UGE:
8308 // fcmp uge ceil(x), x => true
8309 if (CeilX)
8310 return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8311 break;
8312 case FCmpInst::FCMP_ULT:
8313 // fcmp ult ceil(x), x => fcmp uno x, 0
8314 if (CeilX)
8316 "", &I);
8317 break;
8318 default:
8319 break;
8320 }
8321
8322 return nullptr;
8323}
8324
8326 bool Changed = false;
8327
8328 /// Orders the operands of the compare so that they are listed from most
8329 /// complex to least complex. This puts constants before unary operators,
8330 /// before binary operators.
8331 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
8332 I.swapOperands();
8333 Changed = true;
8334 }
8335
8336 const CmpInst::Predicate Pred = I.getPredicate();
8337 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
8338 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
8340 return replaceInstUsesWith(I, V);
8341
8342 // Simplify 'fcmp pred X, X'
8343 Type *OpType = Op0->getType();
8344 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
8345 if (Op0 == Op1) {
8346 switch (Pred) {
8347 default:
8348 break;
8349 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
8350 case FCmpInst::FCMP_ULT: // True if unordered or less than
8351 case FCmpInst::FCMP_UGT: // True if unordered or greater than
8352 case FCmpInst::FCMP_UNE: // True if unordered or not equal
8353 // Canonicalize these to be 'fcmp uno %X, 0.0'.
8354 I.setPredicate(FCmpInst::FCMP_UNO);
8355 I.setOperand(1, Constant::getNullValue(OpType));
8356 return &I;
8357
8358 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
8359 case FCmpInst::FCMP_OEQ: // True if ordered and equal
8360 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
8361 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
8362 // Canonicalize these to be 'fcmp ord %X, 0.0'.
8363 I.setPredicate(FCmpInst::FCMP_ORD);
8364 I.setOperand(1, Constant::getNullValue(OpType));
8365 return &I;
8366 }
8367 }
8368
8369 if (I.isCommutative()) {
8370 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
8371 replaceOperand(I, 0, Pair->first);
8372 replaceOperand(I, 1, Pair->second);
8373 return &I;
8374 }
8375 }
8376
8377 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
8378 // then canonicalize the operand to 0.0.
8379 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
8380 if (!match(Op0, m_PosZeroFP()) &&
8381 isKnownNeverNaN(Op0, 0, getSimplifyQuery().getWithInstruction(&I)))
8382 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
8383
8384 if (!match(Op1, m_PosZeroFP()) &&
8385 isKnownNeverNaN(Op1, 0, getSimplifyQuery().getWithInstruction(&I)))
8386 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8387 }
8388
8389 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
8390 Value *X, *Y;
8391 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
8392 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
8393
8395 return R;
8396
8397 // Test if the FCmpInst instruction is used exclusively by a select as
8398 // part of a minimum or maximum operation. If so, refrain from doing
8399 // any other folding. This helps out other analyses which understand
8400 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
8401 // and CodeGen. And in this case, at least one of the comparison
8402 // operands has at least one user besides the compare (the select),
8403 // which would often largely negate the benefit of folding anyway.
8404 if (I.hasOneUse())
8405 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
8406 Value *A, *B;
8408 if (SPR.Flavor != SPF_UNKNOWN)
8409 return nullptr;
8410 }
8411
8412 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
8413 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
8414 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
8415 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
8416
8417 // Canonicalize:
8418 // fcmp olt X, +inf -> fcmp one X, +inf
8419 // fcmp ole X, +inf -> fcmp ord X, 0
8420 // fcmp ogt X, +inf -> false
8421 // fcmp oge X, +inf -> fcmp oeq X, +inf
8422 // fcmp ult X, +inf -> fcmp une X, +inf
8423 // fcmp ule X, +inf -> true
8424 // fcmp ugt X, +inf -> fcmp uno X, 0
8425 // fcmp uge X, +inf -> fcmp ueq X, +inf
8426 // fcmp olt X, -inf -> false
8427 // fcmp ole X, -inf -> fcmp oeq X, -inf
8428 // fcmp ogt X, -inf -> fcmp one X, -inf
8429 // fcmp oge X, -inf -> fcmp ord X, 0
8430 // fcmp ult X, -inf -> fcmp uno X, 0
8431 // fcmp ule X, -inf -> fcmp ueq X, -inf
8432 // fcmp ugt X, -inf -> fcmp une X, -inf
8433 // fcmp uge X, -inf -> true
8434 const APFloat *C;
8435 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
8436 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
8437 default:
8438 break;
8439 case FCmpInst::FCMP_ORD:
8440 case FCmpInst::FCMP_UNO:
8443 case FCmpInst::FCMP_OGT:
8444 case FCmpInst::FCMP_ULE:
8445 llvm_unreachable("Should be simplified by InstSimplify");
8446 case FCmpInst::FCMP_OLT:
8447 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
8448 case FCmpInst::FCMP_OLE:
8449 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
8450 "", &I);
8451 case FCmpInst::FCMP_OGE:
8452 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
8453 case FCmpInst::FCMP_ULT:
8454 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
8455 case FCmpInst::FCMP_UGT:
8456 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8457 "", &I);
8458 case FCmpInst::FCMP_UGE:
8459 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8460 }
8461 }
8462
8463 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8464 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8465 if (match(Op1, m_PosZeroFP()) &&
8468 if (Pred == FCmpInst::FCMP_OEQ)
8469 IntPred = ICmpInst::ICMP_EQ;
8470 else if (Pred == FCmpInst::FCMP_UNE)
8471 IntPred = ICmpInst::ICMP_NE;
8472
8473 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8474 Type *IntTy = X->getType();
8475 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8476 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8477 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8478 }
8479 }
8480
8481 // Handle fcmp with instruction LHS and constant RHS.
8482 Instruction *LHSI;
8483 Constant *RHSC;
8484 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8485 switch (LHSI->getOpcode()) {
8486 case Instruction::Select:
8487 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8488 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8490 return replaceOperand(I, 0, X);
8491 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
8492 return NV;
8493 break;
8494 case Instruction::FSub:
8495 if (LHSI->hasOneUse())
8496 if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
8497 return NV;
8498 break;
8499 case Instruction::PHI:
8500 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8501 return NV;
8502 break;
8503 case Instruction::SIToFP:
8504 case Instruction::UIToFP:
8505 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8506 return NV;
8507 break;
8508 case Instruction::FDiv:
8509 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8510 return NV;
8511 break;
8512 case Instruction::Load:
8513 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8514 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
8516 cast<LoadInst>(LHSI), GEP, GV, I))
8517 return Res;
8518 break;
8519 }
8520 }
8521
8522 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8523 return R;
8524
8525 if (Instruction *R = foldSqrtWithFcmpZero(I, *this))
8526 return R;
8527
8528 if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this))
8529 return R;
8530
8531 if (match(Op0, m_FNeg(m_Value(X)))) {
8532 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8533 Constant *C;
8534 if (match(Op1, m_Constant(C)))
8535 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8536 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8537 }
8538
8539 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8540 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8541 return new FCmpInst(Pred, X, Op1, "", &I);
8542
8543 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8544 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8545 return new FCmpInst(Pred, Op0, Y, "", &I);
8546
8547 if (match(Op0, m_FPExt(m_Value(X)))) {
8548 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8549 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8550 return new FCmpInst(Pred, X, Y, "", &I);
8551
8552 const APFloat *C;
8553 if (match(Op1, m_APFloat(C))) {
8554 const fltSemantics &FPSem =
8555 X->getType()->getScalarType()->getFltSemantics();
8556 bool Lossy;
8557 APFloat TruncC = *C;
8558 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8559
8560 if (Lossy) {
8561 // X can't possibly equal the higher-precision constant, so reduce any
8562 // equality comparison.
8563 // TODO: Other predicates can be handled via getFCmpCode().
8564 switch (Pred) {
8565 case FCmpInst::FCMP_OEQ:
8566 // X is ordered and equal to an impossible constant --> false
8567 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8568 case FCmpInst::FCMP_ONE:
8569 // X is ordered and not equal to an impossible constant --> ordered
8570 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8571 ConstantFP::getZero(X->getType()));
8572 case FCmpInst::FCMP_UEQ:
8573 // X is unordered or equal to an impossible constant --> unordered
8574 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8575 ConstantFP::getZero(X->getType()));
8576 case FCmpInst::FCMP_UNE:
8577 // X is unordered or not equal to an impossible constant --> true
8578 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8579 default:
8580 break;
8581 }
8582 }
8583
8584 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8585 // Avoid lossy conversions and denormals.
8586 // Zero is a special case that's OK to convert.
8587 APFloat Fabs = TruncC;
8588 Fabs.clearSign();
8589 if (!Lossy &&
8590 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8591 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8592 return new FCmpInst(Pred, X, NewC, "", &I);
8593 }
8594 }
8595 }
8596
8597 // Convert a sign-bit test of an FP value into a cast and integer compare.
8598 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8599 // TODO: Handle non-zero compare constants.
8600 // TODO: Handle other predicates.
8601 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),
8602 m_Value(X)))) &&
8603 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8604 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8605 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8606 IntType = VectorType::get(IntType, VecTy->getElementCount());
8607
8608 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8609 if (Pred == FCmpInst::FCMP_OLT) {
8610 Value *IntX = Builder.CreateBitCast(X, IntType);
8611 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8612 ConstantInt::getNullValue(IntType));
8613 }
8614 }
8615
8616 {
8617 Value *CanonLHS = nullptr, *CanonRHS = nullptr;
8618 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));
8619 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));
8620
8621 // (canonicalize(x) == x) => (x == x)
8622 if (CanonLHS == Op1)
8623 return new FCmpInst(Pred, Op1, Op1, "", &I);
8624
8625 // (x == canonicalize(x)) => (x == x)
8626 if (CanonRHS == Op0)
8627 return new FCmpInst(Pred, Op0, Op0, "", &I);
8628
8629 // (canonicalize(x) == canonicalize(y)) => (x == y)
8630 if (CanonLHS && CanonRHS)
8631 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8632 }
8633
8634 if (I.getType()->isVectorTy())
8635 if (Instruction *Res = foldVectorCmp(I, Builder))
8636 return Res;
8637
8638 return Changed ? &I : nullptr;
8639}
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:756
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:550
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:664
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
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:1171
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