LLVM 19.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/ScopeExit.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/Statistic.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/InstrTypes.h"
31#include <bitset>
32
33using namespace llvm;
34using namespace PatternMatch;
35
36#define DEBUG_TYPE "instcombine"
37
38// How many times is a select replaced by one of its operands?
39STATISTIC(NumSel, "Number of select opts");
40
41
42/// Compute Result = In1+In2, returning true if the result overflowed for this
43/// type.
44static bool addWithOverflow(APInt &Result, const APInt &In1,
45 const APInt &In2, bool IsSigned = false) {
46 bool Overflow;
47 if (IsSigned)
48 Result = In1.sadd_ov(In2, Overflow);
49 else
50 Result = In1.uadd_ov(In2, Overflow);
51
52 return Overflow;
53}
54
55/// Compute Result = In1-In2, returning true if the result overflowed for this
56/// type.
57static bool subWithOverflow(APInt &Result, const APInt &In1,
58 const APInt &In2, bool IsSigned = false) {
59 bool Overflow;
60 if (IsSigned)
61 Result = In1.ssub_ov(In2, Overflow);
62 else
63 Result = In1.usub_ov(In2, Overflow);
64
65 return Overflow;
66}
67
68/// Given an icmp instruction, return true if any use of this comparison is a
69/// branch on sign bit comparison.
70static bool hasBranchUse(ICmpInst &I) {
71 for (auto *U : I.users())
72 if (isa<BranchInst>(U))
73 return true;
74 return false;
75}
76
77/// Returns true if the exploded icmp can be expressed as a signed comparison
78/// to zero and updates the predicate accordingly.
79/// The signedness of the comparison is preserved.
80/// TODO: Refactor with decomposeBitTestICmp()?
81static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
82 if (!ICmpInst::isSigned(Pred))
83 return false;
84
85 if (C.isZero())
86 return ICmpInst::isRelational(Pred);
87
88 if (C.isOne()) {
89 if (Pred == ICmpInst::ICMP_SLT) {
90 Pred = ICmpInst::ICMP_SLE;
91 return true;
92 }
93 } else if (C.isAllOnes()) {
94 if (Pred == ICmpInst::ICMP_SGT) {
95 Pred = ICmpInst::ICMP_SGE;
96 return true;
97 }
98 }
99
100 return false;
101}
102
103/// This is called when we see this pattern:
104/// cmp pred (load (gep GV, ...)), cmpcst
105/// where GV is a global variable with a constant initializer. Try to simplify
106/// this into some simple computation that does not need the load. For example
107/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
108///
109/// If AndCst is non-null, then the loaded value is masked with that constant
110/// before doing the comparison. This handles cases like "A[i]&4 == 0".
113 ConstantInt *AndCst) {
114 if (LI->isVolatile() || LI->getType() != GEP->getResultElementType() ||
115 GV->getValueType() != GEP->getSourceElementType() || !GV->isConstant() ||
117 return nullptr;
118
120 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
121 return nullptr;
122
123 uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
124 // Don't blow up on huge arrays.
125 if (ArrayElementCount > MaxArraySizeForCombine)
126 return nullptr;
127
128 // There are many forms of this optimization we can handle, for now, just do
129 // the simple index into a single-dimensional array.
130 //
131 // Require: GEP GV, 0, i {{, constant indices}}
132 if (GEP->getNumOperands() < 3 || !isa<ConstantInt>(GEP->getOperand(1)) ||
133 !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
134 isa<Constant>(GEP->getOperand(2)))
135 return nullptr;
136
137 // Check that indices after the variable are constants and in-range for the
138 // type they index. Collect the indices. This is typically for arrays of
139 // structs.
140 SmallVector<unsigned, 4> LaterIndices;
141
142 Type *EltTy = Init->getType()->getArrayElementType();
143 for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
144 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
145 if (!Idx)
146 return nullptr; // Variable index.
147
148 uint64_t IdxVal = Idx->getZExtValue();
149 if ((unsigned)IdxVal != IdxVal)
150 return nullptr; // Too large array index.
151
152 if (StructType *STy = dyn_cast<StructType>(EltTy))
153 EltTy = STy->getElementType(IdxVal);
154 else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
155 if (IdxVal >= ATy->getNumElements())
156 return nullptr;
157 EltTy = ATy->getElementType();
158 } else {
159 return nullptr; // Unknown type.
160 }
161
162 LaterIndices.push_back(IdxVal);
163 }
164
165 enum { Overdefined = -3, Undefined = -2 };
166
167 // Variables for our state machines.
168
169 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
170 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
171 // and 87 is the second (and last) index. FirstTrueElement is -2 when
172 // undefined, otherwise set to the first true element. SecondTrueElement is
173 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
174 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
175
176 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
177 // form "i != 47 & i != 87". Same state transitions as for true elements.
178 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
179
180 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
181 /// define a state machine that triggers for ranges of values that the index
182 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
183 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
184 /// index in the range (inclusive). We use -2 for undefined here because we
185 /// use relative comparisons and don't want 0-1 to match -1.
186 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
187
188 // MagicBitvector - This is a magic bitvector where we set a bit if the
189 // comparison is true for element 'i'. If there are 64 elements or less in
190 // the array, this will fully represent all the comparison results.
191 uint64_t MagicBitvector = 0;
192
193 // Scan the array and see if one of our patterns matches.
194 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
195 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
196 Constant *Elt = Init->getAggregateElement(i);
197 if (!Elt)
198 return nullptr;
199
200 // If this is indexing an array of structures, get the structure element.
201 if (!LaterIndices.empty()) {
202 Elt = ConstantFoldExtractValueInstruction(Elt, LaterIndices);
203 if (!Elt)
204 return nullptr;
205 }
206
207 // If the element is masked, handle it.
208 if (AndCst) {
209 Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);
210 if (!Elt)
211 return nullptr;
212 }
213
214 // Find out if the comparison would be true or false for the i'th element.
216 CompareRHS, DL, &TLI);
217 // If the result is undef for this element, ignore it.
218 if (isa<UndefValue>(C)) {
219 // Extend range state machines to cover this element in case there is an
220 // undef in the middle of the range.
221 if (TrueRangeEnd == (int)i - 1)
222 TrueRangeEnd = i;
223 if (FalseRangeEnd == (int)i - 1)
224 FalseRangeEnd = i;
225 continue;
226 }
227
228 // If we can't compute the result for any of the elements, we have to give
229 // up evaluating the entire conditional.
230 if (!isa<ConstantInt>(C))
231 return nullptr;
232
233 // Otherwise, we know if the comparison is true or false for this element,
234 // update our state machines.
235 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
236
237 // State machine for single/double/range index comparison.
238 if (IsTrueForElt) {
239 // Update the TrueElement state machine.
240 if (FirstTrueElement == Undefined)
241 FirstTrueElement = TrueRangeEnd = i; // First true element.
242 else {
243 // Update double-compare state machine.
244 if (SecondTrueElement == Undefined)
245 SecondTrueElement = i;
246 else
247 SecondTrueElement = Overdefined;
248
249 // Update range state machine.
250 if (TrueRangeEnd == (int)i - 1)
251 TrueRangeEnd = i;
252 else
253 TrueRangeEnd = Overdefined;
254 }
255 } else {
256 // Update the FalseElement state machine.
257 if (FirstFalseElement == Undefined)
258 FirstFalseElement = FalseRangeEnd = i; // First false element.
259 else {
260 // Update double-compare state machine.
261 if (SecondFalseElement == Undefined)
262 SecondFalseElement = i;
263 else
264 SecondFalseElement = Overdefined;
265
266 // Update range state machine.
267 if (FalseRangeEnd == (int)i - 1)
268 FalseRangeEnd = i;
269 else
270 FalseRangeEnd = Overdefined;
271 }
272 }
273
274 // If this element is in range, update our magic bitvector.
275 if (i < 64 && IsTrueForElt)
276 MagicBitvector |= 1ULL << i;
277
278 // If all of our states become overdefined, bail out early. Since the
279 // predicate is expensive, only check it every 8 elements. This is only
280 // really useful for really huge arrays.
281 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
282 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
283 FalseRangeEnd == Overdefined)
284 return nullptr;
285 }
286
287 // Now that we've scanned the entire array, emit our new comparison(s). We
288 // order the state machines in complexity of the generated code.
289 Value *Idx = GEP->getOperand(2);
290
291 // If the index is larger than the pointer offset size of the target, truncate
292 // the index down like the GEP would do implicitly. We don't have to do this
293 // for an inbounds GEP because the index can't be out of range.
294 if (!GEP->isInBounds()) {
295 Type *PtrIdxTy = DL.getIndexType(GEP->getType());
296 unsigned OffsetSize = PtrIdxTy->getIntegerBitWidth();
297 if (Idx->getType()->getPrimitiveSizeInBits().getFixedValue() > OffsetSize)
298 Idx = Builder.CreateTrunc(Idx, PtrIdxTy);
299 }
300
301 // If inbounds keyword is not present, Idx * ElementSize can overflow.
302 // Let's assume that ElementSize is 2 and the wanted value is at offset 0.
303 // Then, there are two possible values for Idx to match offset 0:
304 // 0x00..00, 0x80..00.
305 // Emitting 'icmp eq Idx, 0' isn't correct in this case because the
306 // comparison is false if Idx was 0x80..00.
307 // We need to erase the highest countTrailingZeros(ElementSize) bits of Idx.
308 unsigned ElementSize =
309 DL.getTypeAllocSize(Init->getType()->getArrayElementType());
310 auto MaskIdx = [&](Value *Idx) {
311 if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) {
312 Value *Mask = ConstantInt::get(Idx->getType(), -1);
313 Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize));
314 Idx = Builder.CreateAnd(Idx, Mask);
315 }
316 return Idx;
317 };
318
319 // If the comparison is only true for one or two elements, emit direct
320 // comparisons.
321 if (SecondTrueElement != Overdefined) {
322 Idx = MaskIdx(Idx);
323 // None true -> false.
324 if (FirstTrueElement == Undefined)
325 return replaceInstUsesWith(ICI, Builder.getFalse());
326
327 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
328
329 // True for one element -> 'i == 47'.
330 if (SecondTrueElement == Undefined)
331 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
332
333 // True for two elements -> 'i == 47 | i == 72'.
334 Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx);
335 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
336 Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx);
337 return BinaryOperator::CreateOr(C1, C2);
338 }
339
340 // If the comparison is only false for one or two elements, emit direct
341 // comparisons.
342 if (SecondFalseElement != Overdefined) {
343 Idx = MaskIdx(Idx);
344 // None false -> true.
345 if (FirstFalseElement == Undefined)
346 return replaceInstUsesWith(ICI, Builder.getTrue());
347
348 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
349
350 // False for one element -> 'i != 47'.
351 if (SecondFalseElement == Undefined)
352 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
353
354 // False for two elements -> 'i != 47 & i != 72'.
355 Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx);
356 Value *SecondFalseIdx =
357 ConstantInt::get(Idx->getType(), SecondFalseElement);
358 Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx);
359 return BinaryOperator::CreateAnd(C1, C2);
360 }
361
362 // If the comparison can be replaced with a range comparison for the elements
363 // where it is true, emit the range check.
364 if (TrueRangeEnd != Overdefined) {
365 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
366 Idx = MaskIdx(Idx);
367
368 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
369 if (FirstTrueElement) {
370 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
371 Idx = Builder.CreateAdd(Idx, Offs);
372 }
373
374 Value *End =
375 ConstantInt::get(Idx->getType(), TrueRangeEnd - FirstTrueElement + 1);
376 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
377 }
378
379 // False range check.
380 if (FalseRangeEnd != Overdefined) {
381 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
382 Idx = MaskIdx(Idx);
383 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
384 if (FirstFalseElement) {
385 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
386 Idx = Builder.CreateAdd(Idx, Offs);
387 }
388
389 Value *End =
390 ConstantInt::get(Idx->getType(), FalseRangeEnd - FirstFalseElement);
391 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
392 }
393
394 // If a magic bitvector captures the entire comparison state
395 // of this load, replace it with computation that does:
396 // ((magic_cst >> i) & 1) != 0
397 {
398 Type *Ty = nullptr;
399
400 // Look for an appropriate type:
401 // - The type of Idx if the magic fits
402 // - The smallest fitting legal type
403 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
404 Ty = Idx->getType();
405 else
406 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
407
408 if (Ty) {
409 Idx = MaskIdx(Idx);
410 Value *V = Builder.CreateIntCast(Idx, Ty, false);
411 V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
412 V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V);
413 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
414 }
415 }
416
417 return nullptr;
418}
419
420/// Returns true if we can rewrite Start as a GEP with pointer Base
421/// and some integer offset. The nodes that need to be re-written
422/// for this transformation will be added to Explored.
424 const DataLayout &DL,
425 SetVector<Value *> &Explored) {
426 SmallVector<Value *, 16> WorkList(1, Start);
427 Explored.insert(Base);
428
429 // The following traversal gives us an order which can be used
430 // when doing the final transformation. Since in the final
431 // transformation we create the PHI replacement instructions first,
432 // we don't have to get them in any particular order.
433 //
434 // However, for other instructions we will have to traverse the
435 // operands of an instruction first, which means that we have to
436 // do a post-order traversal.
437 while (!WorkList.empty()) {
439
440 while (!WorkList.empty()) {
441 if (Explored.size() >= 100)
442 return false;
443
444 Value *V = WorkList.back();
445
446 if (Explored.contains(V)) {
447 WorkList.pop_back();
448 continue;
449 }
450
451 if (!isa<GetElementPtrInst>(V) && !isa<PHINode>(V))
452 // We've found some value that we can't explore which is different from
453 // the base. Therefore we can't do this transformation.
454 return false;
455
456 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
457 // Only allow inbounds GEPs with at most one variable offset.
458 auto IsNonConst = [](Value *V) { return !isa<ConstantInt>(V); };
459 if (!GEP->isInBounds() || count_if(GEP->indices(), IsNonConst) > 1)
460 return false;
461
462 if (!Explored.contains(GEP->getOperand(0)))
463 WorkList.push_back(GEP->getOperand(0));
464 }
465
466 if (WorkList.back() == V) {
467 WorkList.pop_back();
468 // We've finished visiting this node, mark it as such.
469 Explored.insert(V);
470 }
471
472 if (auto *PN = dyn_cast<PHINode>(V)) {
473 // We cannot transform PHIs on unsplittable basic blocks.
474 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
475 return false;
476 Explored.insert(PN);
477 PHIs.insert(PN);
478 }
479 }
480
481 // Explore the PHI nodes further.
482 for (auto *PN : PHIs)
483 for (Value *Op : PN->incoming_values())
484 if (!Explored.contains(Op))
485 WorkList.push_back(Op);
486 }
487
488 // Make sure that we can do this. Since we can't insert GEPs in a basic
489 // block before a PHI node, we can't easily do this transformation if
490 // we have PHI node users of transformed instructions.
491 for (Value *Val : Explored) {
492 for (Value *Use : Val->uses()) {
493
494 auto *PHI = dyn_cast<PHINode>(Use);
495 auto *Inst = dyn_cast<Instruction>(Val);
496
497 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
498 !Explored.contains(PHI))
499 continue;
500
501 if (PHI->getParent() == Inst->getParent())
502 return false;
503 }
504 }
505 return true;
506}
507
508// Sets the appropriate insert point on Builder where we can add
509// a replacement Instruction for V (if that is possible).
510static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
511 bool Before = true) {
512 if (auto *PHI = dyn_cast<PHINode>(V)) {
513 BasicBlock *Parent = PHI->getParent();
514 Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt());
515 return;
516 }
517 if (auto *I = dyn_cast<Instruction>(V)) {
518 if (!Before)
519 I = &*std::next(I->getIterator());
520 Builder.SetInsertPoint(I);
521 return;
522 }
523 if (auto *A = dyn_cast<Argument>(V)) {
524 // Set the insertion point in the entry block.
525 BasicBlock &Entry = A->getParent()->getEntryBlock();
526 Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt());
527 return;
528 }
529 // Otherwise, this is a constant and we don't need to set a new
530 // insertion point.
531 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
532}
533
534/// Returns a re-written value of Start as an indexed GEP using Base as a
535/// pointer.
537 const DataLayout &DL,
538 SetVector<Value *> &Explored,
539 InstCombiner &IC) {
540 // Perform all the substitutions. This is a bit tricky because we can
541 // have cycles in our use-def chains.
542 // 1. Create the PHI nodes without any incoming values.
543 // 2. Create all the other values.
544 // 3. Add the edges for the PHI nodes.
545 // 4. Emit GEPs to get the original pointers.
546 // 5. Remove the original instructions.
547 Type *IndexType = IntegerType::get(
548 Base->getContext(), DL.getIndexTypeSizeInBits(Start->getType()));
549
551 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
552
553 // Create the new PHI nodes, without adding any incoming values.
554 for (Value *Val : Explored) {
555 if (Val == Base)
556 continue;
557 // Create empty phi nodes. This avoids cyclic dependencies when creating
558 // the remaining instructions.
559 if (auto *PHI = dyn_cast<PHINode>(Val))
560 NewInsts[PHI] =
561 PHINode::Create(IndexType, PHI->getNumIncomingValues(),
562 PHI->getName() + ".idx", PHI->getIterator());
563 }
564 IRBuilder<> Builder(Base->getContext());
565
566 // Create all the other instructions.
567 for (Value *Val : Explored) {
568 if (NewInsts.contains(Val))
569 continue;
570
571 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
572 setInsertionPoint(Builder, GEP);
573 Value *Op = NewInsts[GEP->getOperand(0)];
574 Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);
575 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero())
576 NewInsts[GEP] = OffsetV;
577 else
578 NewInsts[GEP] = Builder.CreateNSWAdd(
579 Op, OffsetV, GEP->getOperand(0)->getName() + ".add");
580 continue;
581 }
582 if (isa<PHINode>(Val))
583 continue;
584
585 llvm_unreachable("Unexpected instruction type");
586 }
587
588 // Add the incoming values to the PHI nodes.
589 for (Value *Val : Explored) {
590 if (Val == Base)
591 continue;
592 // All the instructions have been created, we can now add edges to the
593 // phi nodes.
594 if (auto *PHI = dyn_cast<PHINode>(Val)) {
595 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
596 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
597 Value *NewIncoming = PHI->getIncomingValue(I);
598
599 if (NewInsts.contains(NewIncoming))
600 NewIncoming = NewInsts[NewIncoming];
601
602 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
603 }
604 }
605 }
606
607 for (Value *Val : Explored) {
608 if (Val == Base)
609 continue;
610
611 setInsertionPoint(Builder, Val, false);
612 // Create GEP for external users.
613 Value *NewVal = Builder.CreateInBoundsGEP(
614 Builder.getInt8Ty(), Base, NewInsts[Val], Val->getName() + ".ptr");
615 IC.replaceInstUsesWith(*cast<Instruction>(Val), NewVal);
616 // Add old instruction to worklist for DCE. We don't directly remove it
617 // here because the original compare is one of the users.
618 IC.addToWorklist(cast<Instruction>(Val));
619 }
620
621 return NewInsts[Start];
622}
623
624/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
625/// We can look through PHIs, GEPs and casts in order to determine a common base
626/// between GEPLHS and RHS.
629 const DataLayout &DL,
630 InstCombiner &IC) {
631 // FIXME: Support vector of pointers.
632 if (GEPLHS->getType()->isVectorTy())
633 return nullptr;
634
635 if (!GEPLHS->hasAllConstantIndices())
636 return nullptr;
637
638 APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
639 Value *PtrBase =
641 /*AllowNonInbounds*/ false);
642
643 // Bail if we looked through addrspacecast.
644 if (PtrBase->getType() != GEPLHS->getType())
645 return nullptr;
646
647 // The set of nodes that will take part in this transformation.
648 SetVector<Value *> Nodes;
649
650 if (!canRewriteGEPAsOffset(RHS, PtrBase, DL, Nodes))
651 return nullptr;
652
653 // We know we can re-write this as
654 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
655 // Since we've only looked through inbouds GEPs we know that we
656 // can't have overflow on either side. We can therefore re-write
657 // this as:
658 // OFFSET1 cmp OFFSET2
659 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, DL, Nodes, IC);
660
661 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
662 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
663 // offset. Since Index is the offset of LHS to the base pointer, we will now
664 // compare the offsets instead of comparing the pointers.
666 IC.Builder.getInt(Offset), NewRHS);
667}
668
669/// Fold comparisons between a GEP instruction and something else. At this point
670/// we know that the GEP is on the LHS of the comparison.
673 Instruction &I) {
674 // Don't transform signed compares of GEPs into index compares. Even if the
675 // GEP is inbounds, the final add of the base pointer can have signed overflow
676 // and would change the result of the icmp.
677 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
678 // the maximum signed value for the pointer type.
680 return nullptr;
681
682 // Look through bitcasts and addrspacecasts. We do not however want to remove
683 // 0 GEPs.
684 if (!isa<GetElementPtrInst>(RHS))
686
687 Value *PtrBase = GEPLHS->getOperand(0);
688 if (PtrBase == RHS && (GEPLHS->isInBounds() || ICmpInst::isEquality(Cond))) {
689 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
690 Value *Offset = EmitGEPOffset(GEPLHS);
692 Constant::getNullValue(Offset->getType()));
693 }
694
695 if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
696 isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&
697 !NullPointerIsDefined(I.getFunction(),
699 // For most address spaces, an allocation can't be placed at null, but null
700 // itself is treated as a 0 size allocation in the in bounds rules. Thus,
701 // the only valid inbounds address derived from null, is null itself.
702 // Thus, we have four cases to consider:
703 // 1) Base == nullptr, Offset == 0 -> inbounds, null
704 // 2) Base == nullptr, Offset != 0 -> poison as the result is out of bounds
705 // 3) Base != nullptr, Offset == (-base) -> poison (crossing allocations)
706 // 4) Base != nullptr, Offset != (-base) -> nonnull (and possibly poison)
707 //
708 // (Note if we're indexing a type of size 0, that simply collapses into one
709 // of the buckets above.)
710 //
711 // In general, we're allowed to make values less poison (i.e. remove
712 // sources of full UB), so in this case, we just select between the two
713 // non-poison cases (1 and 4 above).
714 //
715 // For vectors, we apply the same reasoning on a per-lane basis.
716 auto *Base = GEPLHS->getPointerOperand();
717 if (GEPLHS->getType()->isVectorTy() && Base->getType()->isPointerTy()) {
718 auto EC = cast<VectorType>(GEPLHS->getType())->getElementCount();
720 }
721 return new ICmpInst(Cond, Base,
723 cast<Constant>(RHS), Base->getType()));
724 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
725 // If the base pointers are different, but the indices are the same, just
726 // compare the base pointer.
727 if (PtrBase != GEPRHS->getOperand(0)) {
728 bool IndicesTheSame =
729 GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
730 GEPLHS->getPointerOperand()->getType() ==
731 GEPRHS->getPointerOperand()->getType() &&
732 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
733 if (IndicesTheSame)
734 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
735 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
736 IndicesTheSame = false;
737 break;
738 }
739
740 // If all indices are the same, just compare the base pointers.
741 Type *BaseType = GEPLHS->getOperand(0)->getType();
742 if (IndicesTheSame && CmpInst::makeCmpResultType(BaseType) == I.getType())
743 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
744
745 // If we're comparing GEPs with two base pointers that only differ in type
746 // and both GEPs have only constant indices or just one use, then fold
747 // the compare with the adjusted indices.
748 // FIXME: Support vector of pointers.
749 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
750 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
751 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
752 PtrBase->stripPointerCasts() ==
753 GEPRHS->getOperand(0)->stripPointerCasts() &&
754 !GEPLHS->getType()->isVectorTy()) {
755 Value *LOffset = EmitGEPOffset(GEPLHS);
756 Value *ROffset = EmitGEPOffset(GEPRHS);
757
758 // If we looked through an addrspacecast between different sized address
759 // spaces, the LHS and RHS pointers are different sized
760 // integers. Truncate to the smaller one.
761 Type *LHSIndexTy = LOffset->getType();
762 Type *RHSIndexTy = ROffset->getType();
763 if (LHSIndexTy != RHSIndexTy) {
764 if (LHSIndexTy->getPrimitiveSizeInBits().getFixedValue() <
765 RHSIndexTy->getPrimitiveSizeInBits().getFixedValue()) {
766 ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy);
767 } else
768 LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy);
769 }
770
772 LOffset, ROffset);
773 return replaceInstUsesWith(I, Cmp);
774 }
775
776 // Otherwise, the base pointers are different and the indices are
777 // different. Try convert this to an indexed compare by looking through
778 // PHIs/casts.
779 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
780 }
781
782 bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
783 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
784 GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
785 // If the GEPs only differ by one index, compare it.
786 unsigned NumDifferences = 0; // Keep track of # differences.
787 unsigned DiffOperand = 0; // The operand that differs.
788 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
789 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
790 Type *LHSType = GEPLHS->getOperand(i)->getType();
791 Type *RHSType = GEPRHS->getOperand(i)->getType();
792 // FIXME: Better support for vector of pointers.
793 if (LHSType->getPrimitiveSizeInBits() !=
794 RHSType->getPrimitiveSizeInBits() ||
795 (GEPLHS->getType()->isVectorTy() &&
796 (!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
797 // Irreconcilable differences.
798 NumDifferences = 2;
799 break;
800 }
801
802 if (NumDifferences++) break;
803 DiffOperand = i;
804 }
805
806 if (NumDifferences == 0) // SAME GEP?
807 return replaceInstUsesWith(I, // No comparison is needed here.
808 ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));
809
810 else if (NumDifferences == 1 && GEPsInBounds) {
811 Value *LHSV = GEPLHS->getOperand(DiffOperand);
812 Value *RHSV = GEPRHS->getOperand(DiffOperand);
813 // Make sure we do a signed comparison here.
814 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
815 }
816 }
817
818 if (GEPsInBounds || CmpInst::isEquality(Cond)) {
819 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
820 Value *L = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true);
821 Value *R = EmitGEPOffset(GEPRHS, /*RewriteGEP=*/true);
822 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
823 }
824 }
825
826 // Try convert this to an indexed compare by looking through PHIs/casts as a
827 // last resort.
828 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
829}
830
832 // It would be tempting to fold away comparisons between allocas and any
833 // pointer not based on that alloca (e.g. an argument). However, even
834 // though such pointers cannot alias, they can still compare equal.
835 //
836 // But LLVM doesn't specify where allocas get their memory, so if the alloca
837 // doesn't escape we can argue that it's impossible to guess its value, and we
838 // can therefore act as if any such guesses are wrong.
839 //
840 // However, we need to ensure that this folding is consistent: We can't fold
841 // one comparison to false, and then leave a different comparison against the
842 // same value alone (as it might evaluate to true at runtime, leading to a
843 // contradiction). As such, this code ensures that all comparisons are folded
844 // at the same time, and there are no other escapes.
845
846 struct CmpCaptureTracker : public CaptureTracker {
847 AllocaInst *Alloca;
848 bool Captured = false;
849 /// The value of the map is a bit mask of which icmp operands the alloca is
850 /// used in.
852
853 CmpCaptureTracker(AllocaInst *Alloca) : Alloca(Alloca) {}
854
855 void tooManyUses() override { Captured = true; }
856
857 bool captured(const Use *U) override {
858 auto *ICmp = dyn_cast<ICmpInst>(U->getUser());
859 // We need to check that U is based *only* on the alloca, and doesn't
860 // have other contributions from a select/phi operand.
861 // TODO: We could check whether getUnderlyingObjects() reduces to one
862 // object, which would allow looking through phi nodes.
863 if (ICmp && ICmp->isEquality() && getUnderlyingObject(*U) == Alloca) {
864 // Collect equality icmps of the alloca, and don't treat them as
865 // captures.
866 auto Res = ICmps.insert({ICmp, 0});
867 Res.first->second |= 1u << U->getOperandNo();
868 return false;
869 }
870
871 Captured = true;
872 return true;
873 }
874 };
875
876 CmpCaptureTracker Tracker(Alloca);
877 PointerMayBeCaptured(Alloca, &Tracker);
878 if (Tracker.Captured)
879 return false;
880
881 bool Changed = false;
882 for (auto [ICmp, Operands] : Tracker.ICmps) {
883 switch (Operands) {
884 case 1:
885 case 2: {
886 // The alloca is only used in one icmp operand. Assume that the
887 // equality is false.
888 auto *Res = ConstantInt::get(
889 ICmp->getType(), ICmp->getPredicate() == ICmpInst::ICMP_NE);
890 replaceInstUsesWith(*ICmp, Res);
892 Changed = true;
893 break;
894 }
895 case 3:
896 // Both icmp operands are based on the alloca, so this is comparing
897 // pointer offsets, without leaking any information about the address
898 // of the alloca. Ignore such comparisons.
899 break;
900 default:
901 llvm_unreachable("Cannot happen");
902 }
903 }
904
905 return Changed;
906}
907
908/// Fold "icmp pred (X+C), X".
910 ICmpInst::Predicate Pred) {
911 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
912 // so the values can never be equal. Similarly for all other "or equals"
913 // operators.
914 assert(!!C && "C should not be zero!");
915
916 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
917 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
918 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
919 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
920 Constant *R = ConstantInt::get(X->getType(),
921 APInt::getMaxValue(C.getBitWidth()) - C);
922 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
923 }
924
925 // (X+1) >u X --> X <u (0-1) --> X != 255
926 // (X+2) >u X --> X <u (0-2) --> X <u 254
927 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
928 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
929 return new ICmpInst(ICmpInst::ICMP_ULT, X,
930 ConstantInt::get(X->getType(), -C));
931
932 APInt SMax = APInt::getSignedMaxValue(C.getBitWidth());
933
934 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
935 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
936 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
937 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
938 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
939 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
940 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
941 return new ICmpInst(ICmpInst::ICMP_SGT, X,
942 ConstantInt::get(X->getType(), SMax - C));
943
944 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
945 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
946 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
947 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
948 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
949 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
950
951 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
952 return new ICmpInst(ICmpInst::ICMP_SLT, X,
953 ConstantInt::get(X->getType(), SMax - (C - 1)));
954}
955
956/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
957/// (icmp eq/ne A, Log2(AP2/AP1)) ->
958/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
960 const APInt &AP1,
961 const APInt &AP2) {
962 assert(I.isEquality() && "Cannot fold icmp gt/lt");
963
964 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
965 if (I.getPredicate() == I.ICMP_NE)
966 Pred = CmpInst::getInversePredicate(Pred);
967 return new ICmpInst(Pred, LHS, RHS);
968 };
969
970 // Don't bother doing any work for cases which InstSimplify handles.
971 if (AP2.isZero())
972 return nullptr;
973
974 bool IsAShr = isa<AShrOperator>(I.getOperand(0));
975 if (IsAShr) {
976 if (AP2.isAllOnes())
977 return nullptr;
978 if (AP2.isNegative() != AP1.isNegative())
979 return nullptr;
980 if (AP2.sgt(AP1))
981 return nullptr;
982 }
983
984 if (!AP1)
985 // 'A' must be large enough to shift out the highest set bit.
986 return getICmp(I.ICMP_UGT, A,
987 ConstantInt::get(A->getType(), AP2.logBase2()));
988
989 if (AP1 == AP2)
990 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
991
992 int Shift;
993 if (IsAShr && AP1.isNegative())
994 Shift = AP1.countl_one() - AP2.countl_one();
995 else
996 Shift = AP1.countl_zero() - AP2.countl_zero();
997
998 if (Shift > 0) {
999 if (IsAShr && AP1 == AP2.ashr(Shift)) {
1000 // There are multiple solutions if we are comparing against -1 and the LHS
1001 // of the ashr is not a power of two.
1002 if (AP1.isAllOnes() && !AP2.isPowerOf2())
1003 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));
1004 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1005 } else if (AP1 == AP2.lshr(Shift)) {
1006 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1007 }
1008 }
1009
1010 // Shifting const2 will never be equal to const1.
1011 // FIXME: This should always be handled by InstSimplify?
1012 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1013 return replaceInstUsesWith(I, TorF);
1014}
1015
1016/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1017/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1019 const APInt &AP1,
1020 const APInt &AP2) {
1021 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1022
1023 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1024 if (I.getPredicate() == I.ICMP_NE)
1025 Pred = CmpInst::getInversePredicate(Pred);
1026 return new ICmpInst(Pred, LHS, RHS);
1027 };
1028
1029 // Don't bother doing any work for cases which InstSimplify handles.
1030 if (AP2.isZero())
1031 return nullptr;
1032
1033 unsigned AP2TrailingZeros = AP2.countr_zero();
1034
1035 if (!AP1 && AP2TrailingZeros != 0)
1036 return getICmp(
1037 I.ICMP_UGE, A,
1038 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
1039
1040 if (AP1 == AP2)
1041 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1042
1043 // Get the distance between the lowest bits that are set.
1044 int Shift = AP1.countr_zero() - AP2TrailingZeros;
1045
1046 if (Shift > 0 && AP2.shl(Shift) == AP1)
1047 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1048
1049 // Shifting const2 will never be equal to const1.
1050 // FIXME: This should always be handled by InstSimplify?
1051 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1052 return replaceInstUsesWith(I, TorF);
1053}
1054
1055/// The caller has matched a pattern of the form:
1056/// I = icmp ugt (add (add A, B), CI2), CI1
1057/// If this is of the form:
1058/// sum = a + b
1059/// if (sum+128 >u 255)
1060/// Then replace it with llvm.sadd.with.overflow.i8.
1061///
1063 ConstantInt *CI2, ConstantInt *CI1,
1064 InstCombinerImpl &IC) {
1065 // The transformation we're trying to do here is to transform this into an
1066 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1067 // with a narrower add, and discard the add-with-constant that is part of the
1068 // range check (if we can't eliminate it, this isn't profitable).
1069
1070 // In order to eliminate the add-with-constant, the compare can be its only
1071 // use.
1072 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1073 if (!AddWithCst->hasOneUse())
1074 return nullptr;
1075
1076 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1077 if (!CI2->getValue().isPowerOf2())
1078 return nullptr;
1079 unsigned NewWidth = CI2->getValue().countr_zero();
1080 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1081 return nullptr;
1082
1083 // The width of the new add formed is 1 more than the bias.
1084 ++NewWidth;
1085
1086 // Check to see that CI1 is an all-ones value with NewWidth bits.
1087 if (CI1->getBitWidth() == NewWidth ||
1088 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1089 return nullptr;
1090
1091 // This is only really a signed overflow check if the inputs have been
1092 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1093 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1094 if (IC.ComputeMaxSignificantBits(A, 0, &I) > NewWidth ||
1095 IC.ComputeMaxSignificantBits(B, 0, &I) > NewWidth)
1096 return nullptr;
1097
1098 // In order to replace the original add with a narrower
1099 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1100 // and truncates that discard the high bits of the add. Verify that this is
1101 // the case.
1102 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1103 for (User *U : OrigAdd->users()) {
1104 if (U == AddWithCst)
1105 continue;
1106
1107 // Only accept truncates for now. We would really like a nice recursive
1108 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1109 // chain to see which bits of a value are actually demanded. If the
1110 // original add had another add which was then immediately truncated, we
1111 // could still do the transformation.
1112 TruncInst *TI = dyn_cast<TruncInst>(U);
1113 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1114 return nullptr;
1115 }
1116
1117 // If the pattern matches, truncate the inputs to the narrower type and
1118 // use the sadd_with_overflow intrinsic to efficiently compute both the
1119 // result and the overflow bit.
1120 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1122 I.getModule(), Intrinsic::sadd_with_overflow, NewType);
1123
1124 InstCombiner::BuilderTy &Builder = IC.Builder;
1125
1126 // Put the new code above the original add, in case there are any uses of the
1127 // add between the add and the compare.
1128 Builder.SetInsertPoint(OrigAdd);
1129
1130 Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc");
1131 Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc");
1132 CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd");
1133 Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result");
1134 Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType());
1135
1136 // The inner add was the result of the narrow add, zero extended to the
1137 // wider type. Replace it with the result computed by the intrinsic.
1138 IC.replaceInstUsesWith(*OrigAdd, ZExt);
1139 IC.eraseInstFromFunction(*OrigAdd);
1140
1141 // The original icmp gets replaced with the overflow value.
1142 return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1143}
1144
1145/// If we have:
1146/// icmp eq/ne (urem/srem %x, %y), 0
1147/// iff %y is a power-of-two, we can replace this with a bit test:
1148/// icmp eq/ne (and %x, (add %y, -1)), 0
1150 // This fold is only valid for equality predicates.
1151 if (!I.isEquality())
1152 return nullptr;
1154 Value *X, *Y, *Zero;
1155 if (!match(&I, m_ICmp(Pred, m_OneUse(m_IRem(m_Value(X), m_Value(Y))),
1156 m_CombineAnd(m_Zero(), m_Value(Zero)))))
1157 return nullptr;
1158 if (!isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, 0, &I))
1159 return nullptr;
1160 // This may increase instruction count, we don't enforce that Y is a constant.
1161 Value *Mask = Builder.CreateAdd(Y, Constant::getAllOnesValue(Y->getType()));
1162 Value *Masked = Builder.CreateAnd(X, Mask);
1163 return ICmpInst::Create(Instruction::ICmp, Pred, Masked, Zero);
1164}
1165
1166/// Fold equality-comparison between zero and any (maybe truncated) right-shift
1167/// by one-less-than-bitwidth into a sign test on the original value.
1169 Instruction *Val;
1171 if (!I.isEquality() || !match(&I, m_ICmp(Pred, m_Instruction(Val), m_Zero())))
1172 return nullptr;
1173
1174 Value *X;
1175 Type *XTy;
1176
1177 Constant *C;
1178 if (match(Val, m_TruncOrSelf(m_Shr(m_Value(X), m_Constant(C))))) {
1179 XTy = X->getType();
1180 unsigned XBitWidth = XTy->getScalarSizeInBits();
1182 APInt(XBitWidth, XBitWidth - 1))))
1183 return nullptr;
1184 } else if (isa<BinaryOperator>(Val) &&
1186 cast<BinaryOperator>(Val), SQ.getWithInstruction(Val),
1187 /*AnalyzeForSignBitExtraction=*/true))) {
1188 XTy = X->getType();
1189 } else
1190 return nullptr;
1191
1192 return ICmpInst::Create(Instruction::ICmp,
1196}
1197
1198// Handle icmp pred X, 0
1200 CmpInst::Predicate Pred = Cmp.getPredicate();
1201 if (!match(Cmp.getOperand(1), m_Zero()))
1202 return nullptr;
1203
1204 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1205 if (Pred == ICmpInst::ICMP_SGT) {
1206 Value *A, *B;
1207 if (match(Cmp.getOperand(0), m_SMin(m_Value(A), m_Value(B)))) {
1209 return new ICmpInst(Pred, B, Cmp.getOperand(1));
1211 return new ICmpInst(Pred, A, Cmp.getOperand(1));
1212 }
1213 }
1214
1216 return New;
1217
1218 // Given:
1219 // icmp eq/ne (urem %x, %y), 0
1220 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
1221 // icmp eq/ne %x, 0
1222 Value *X, *Y;
1223 if (match(Cmp.getOperand(0), m_URem(m_Value(X), m_Value(Y))) &&
1224 ICmpInst::isEquality(Pred)) {
1225 KnownBits XKnown = computeKnownBits(X, 0, &Cmp);
1226 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp);
1227 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
1228 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1229 }
1230
1231 // (icmp eq/ne (mul X Y)) -> (icmp eq/ne X/Y) if we know about whether X/Y are
1232 // odd/non-zero/there is no overflow.
1233 if (match(Cmp.getOperand(0), m_Mul(m_Value(X), m_Value(Y))) &&
1234 ICmpInst::isEquality(Pred)) {
1235
1236 KnownBits XKnown = computeKnownBits(X, 0, &Cmp);
1237 // if X % 2 != 0
1238 // (icmp eq/ne Y)
1239 if (XKnown.countMaxTrailingZeros() == 0)
1240 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1241
1242 KnownBits YKnown = computeKnownBits(Y, 0, &Cmp);
1243 // if Y % 2 != 0
1244 // (icmp eq/ne X)
1245 if (YKnown.countMaxTrailingZeros() == 0)
1246 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1247
1248 auto *BO0 = cast<OverflowingBinaryOperator>(Cmp.getOperand(0));
1249 if (BO0->hasNoUnsignedWrap() || BO0->hasNoSignedWrap()) {
1250 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
1251 // `isKnownNonZero` does more analysis than just `!KnownBits.One.isZero()`
1252 // but to avoid unnecessary work, first just if this is an obvious case.
1253
1254 // if X non-zero and NoOverflow(X * Y)
1255 // (icmp eq/ne Y)
1256 if (!XKnown.One.isZero() || isKnownNonZero(X, Q))
1257 return new ICmpInst(Pred, Y, Cmp.getOperand(1));
1258
1259 // if Y non-zero and NoOverflow(X * Y)
1260 // (icmp eq/ne X)
1261 if (!YKnown.One.isZero() || isKnownNonZero(Y, Q))
1262 return new ICmpInst(Pred, X, Cmp.getOperand(1));
1263 }
1264 // Note, we are skipping cases:
1265 // if Y % 2 != 0 AND X % 2 != 0
1266 // (false/true)
1267 // if X non-zero and Y non-zero and NoOverflow(X * Y)
1268 // (false/true)
1269 // Those can be simplified later as we would have already replaced the (icmp
1270 // eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
1271 // will fold to a constant elsewhere.
1272 }
1273 return nullptr;
1274}
1275
1276/// Fold icmp Pred X, C.
1277/// TODO: This code structure does not make sense. The saturating add fold
1278/// should be moved to some other helper and extended as noted below (it is also
1279/// possible that code has been made unnecessary - do we canonicalize IR to
1280/// overflow/saturating intrinsics or not?).
1282 // Match the following pattern, which is a common idiom when writing
1283 // overflow-safe integer arithmetic functions. The source performs an addition
1284 // in wider type and explicitly checks for overflow using comparisons against
1285 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1286 //
1287 // TODO: This could probably be generalized to handle other overflow-safe
1288 // operations if we worked out the formulas to compute the appropriate magic
1289 // constants.
1290 //
1291 // sum = a + b
1292 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1293 CmpInst::Predicate Pred = Cmp.getPredicate();
1294 Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
1295 Value *A, *B;
1296 ConstantInt *CI, *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1297 if (Pred == ICmpInst::ICMP_UGT && match(Op1, m_ConstantInt(CI)) &&
1298 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1299 if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
1300 return Res;
1301
1302 // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
1303 Constant *C = dyn_cast<Constant>(Op1);
1304 if (!C)
1305 return nullptr;
1306
1307 if (auto *Phi = dyn_cast<PHINode>(Op0))
1308 if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
1310 for (Value *V : Phi->incoming_values()) {
1311 Constant *Res =
1312 ConstantFoldCompareInstOperands(Pred, cast<Constant>(V), C, DL);
1313 if (!Res)
1314 return nullptr;
1315 Ops.push_back(Res);
1316 }
1318 PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());
1319 for (auto [V, Pred] : zip(Ops, Phi->blocks()))
1320 NewPhi->addIncoming(V, Pred);
1321 return replaceInstUsesWith(Cmp, NewPhi);
1322 }
1323
1325 return R;
1326
1327 return nullptr;
1328}
1329
1330/// Canonicalize icmp instructions based on dominating conditions.
1332 // We already checked simple implication in InstSimplify, only handle complex
1333 // cases here.
1334 Value *X = Cmp.getOperand(0), *Y = Cmp.getOperand(1);
1335 const APInt *C;
1336 if (!match(Y, m_APInt(C)))
1337 return nullptr;
1338
1339 CmpInst::Predicate Pred = Cmp.getPredicate();
1341
1342 auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1343 const APInt *DomC) -> Instruction * {
1344 // We have 2 compares of a variable with constants. Calculate the constant
1345 // ranges of those compares to see if we can transform the 2nd compare:
1346 // DomBB:
1347 // DomCond = icmp DomPred X, DomC
1348 // br DomCond, CmpBB, FalseBB
1349 // CmpBB:
1350 // Cmp = icmp Pred X, C
1351 ConstantRange DominatingCR =
1352 ConstantRange::makeExactICmpRegion(DomPred, *DomC);
1353 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1354 ConstantRange Difference = DominatingCR.difference(CR);
1355 if (Intersection.isEmptySet())
1356 return replaceInstUsesWith(Cmp, Builder.getFalse());
1357 if (Difference.isEmptySet())
1358 return replaceInstUsesWith(Cmp, Builder.getTrue());
1359
1360 // Canonicalizing a sign bit comparison that gets used in a branch,
1361 // pessimizes codegen by generating branch on zero instruction instead
1362 // of a test and branch. So we avoid canonicalizing in such situations
1363 // because test and branch instruction has better branch displacement
1364 // than compare and branch instruction.
1365 bool UnusedBit;
1366 bool IsSignBit = isSignBitCheck(Pred, *C, UnusedBit);
1367 if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp)))
1368 return nullptr;
1369
1370 // Avoid an infinite loop with min/max canonicalization.
1371 // TODO: This will be unnecessary if we canonicalize to min/max intrinsics.
1372 if (Cmp.hasOneUse() &&
1373 match(Cmp.user_back(), m_MaxOrMin(m_Value(), m_Value())))
1374 return nullptr;
1375
1376 if (const APInt *EqC = Intersection.getSingleElement())
1377 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*EqC));
1378 if (const APInt *NeC = Difference.getSingleElement())
1379 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*NeC));
1380 return nullptr;
1381 };
1382
1383 for (BranchInst *BI : DC.conditionsFor(X)) {
1384 ICmpInst::Predicate DomPred;
1385 const APInt *DomC;
1386 if (!match(BI->getCondition(),
1387 m_ICmp(DomPred, m_Specific(X), m_APInt(DomC))))
1388 continue;
1389
1390 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1391 if (DT.dominates(Edge0, Cmp.getParent())) {
1392 if (auto *V = handleDomCond(DomPred, DomC))
1393 return V;
1394 } else {
1395 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1396 if (DT.dominates(Edge1, Cmp.getParent()))
1397 if (auto *V =
1398 handleDomCond(CmpInst::getInversePredicate(DomPred), DomC))
1399 return V;
1400 }
1401 }
1402
1403 return nullptr;
1404}
1405
1406/// Fold icmp (trunc X), C.
1408 TruncInst *Trunc,
1409 const APInt &C) {
1410 ICmpInst::Predicate Pred = Cmp.getPredicate();
1411 Value *X = Trunc->getOperand(0);
1412 if (C.isOne() && C.getBitWidth() > 1) {
1413 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1414 Value *V = nullptr;
1415 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
1416 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1417 ConstantInt::get(V->getType(), 1));
1418 }
1419
1420 Type *SrcTy = X->getType();
1421 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1422 SrcBits = SrcTy->getScalarSizeInBits();
1423
1424 // TODO: Handle any shifted constant by subtracting trailing zeros.
1425 // TODO: Handle non-equality predicates.
1426 Value *Y;
1427 if (Cmp.isEquality() && match(X, m_Shl(m_One(), m_Value(Y)))) {
1428 // (trunc (1 << Y) to iN) == 0 --> Y u>= N
1429 // (trunc (1 << Y) to iN) != 0 --> Y u< N
1430 if (C.isZero()) {
1431 auto NewPred = (Pred == Cmp.ICMP_EQ) ? Cmp.ICMP_UGE : Cmp.ICMP_ULT;
1432 return new ICmpInst(NewPred, Y, ConstantInt::get(SrcTy, DstBits));
1433 }
1434 // (trunc (1 << Y) to iN) == 2**C --> Y == C
1435 // (trunc (1 << Y) to iN) != 2**C --> Y != C
1436 if (C.isPowerOf2())
1437 return new ICmpInst(Pred, Y, ConstantInt::get(SrcTy, C.logBase2()));
1438 }
1439
1440 if (Cmp.isEquality() && Trunc->hasOneUse()) {
1441 // Canonicalize to a mask and wider compare if the wide type is suitable:
1442 // (trunc X to i8) == C --> (X & 0xff) == (zext C)
1443 if (!SrcTy->isVectorTy() && shouldChangeType(DstBits, SrcBits)) {
1444 Constant *Mask =
1445 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcBits, DstBits));
1446 Value *And = Builder.CreateAnd(X, Mask);
1447 Constant *WideC = ConstantInt::get(SrcTy, C.zext(SrcBits));
1448 return new ICmpInst(Pred, And, WideC);
1449 }
1450
1451 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1452 // of the high bits truncated out of x are known.
1453 KnownBits Known = computeKnownBits(X, 0, &Cmp);
1454
1455 // If all the high bits are known, we can do this xform.
1456 if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
1457 // Pull in the high bits from known-ones set.
1458 APInt NewRHS = C.zext(SrcBits);
1459 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
1460 return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, NewRHS));
1461 }
1462 }
1463
1464 // Look through truncated right-shift of the sign-bit for a sign-bit check:
1465 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] < 0 --> ShOp < 0
1466 // trunc iN (ShOp >> ShAmtC) to i[N - ShAmtC] > -1 --> ShOp > -1
1467 Value *ShOp;
1468 const APInt *ShAmtC;
1469 bool TrueIfSigned;
1470 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
1471 match(X, m_Shr(m_Value(ShOp), m_APInt(ShAmtC))) &&
1472 DstBits == SrcBits - ShAmtC->getZExtValue()) {
1473 return TrueIfSigned ? new ICmpInst(ICmpInst::ICMP_SLT, ShOp,
1475 : new ICmpInst(ICmpInst::ICMP_SGT, ShOp,
1477 }
1478
1479 return nullptr;
1480}
1481
1482/// Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
1483/// Fold icmp (trunc nuw/nsw X), (zext/sext Y).
1486 const SimplifyQuery &Q) {
1487 Value *X, *Y;
1489 bool YIsSExt = false;
1490 // Try to match icmp (trunc X), (trunc Y)
1491 if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {
1492 unsigned NoWrapFlags = cast<TruncInst>(Cmp.getOperand(0))->getNoWrapKind() &
1493 cast<TruncInst>(Cmp.getOperand(1))->getNoWrapKind();
1494 if (Cmp.isSigned()) {
1495 // For signed comparisons, both truncs must be nsw.
1496 if (!(NoWrapFlags & TruncInst::NoSignedWrap))
1497 return nullptr;
1498 } else {
1499 // For unsigned and equality comparisons, either both must be nuw or
1500 // both must be nsw, we don't care which.
1501 if (!NoWrapFlags)
1502 return nullptr;
1503 }
1504
1505 if (X->getType() != Y->getType() &&
1506 (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))
1507 return nullptr;
1508 if (!isDesirableIntType(X->getType()->getScalarSizeInBits()) &&
1509 isDesirableIntType(Y->getType()->getScalarSizeInBits())) {
1510 std::swap(X, Y);
1511 Pred = Cmp.getSwappedPredicate(Pred);
1512 }
1513 YIsSExt = !(NoWrapFlags & TruncInst::NoUnsignedWrap);
1514 }
1515 // Try to match icmp (trunc nuw X), (zext Y)
1516 else if (!Cmp.isSigned() &&
1517 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),
1518 m_OneUse(m_ZExt(m_Value(Y)))))) {
1519 // Can fold trunc nuw + zext for unsigned and equality predicates.
1520 }
1521 // Try to match icmp (trunc nsw X), (sext Y)
1522 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),
1524 // Can fold trunc nsw + zext/sext for all predicates.
1525 YIsSExt =
1526 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));
1527 } else
1528 return nullptr;
1529
1530 Type *TruncTy = Cmp.getOperand(0)->getType();
1531 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1532
1533 // If this transform will end up changing from desirable types -> undesirable
1534 // types skip it.
1535 if (isDesirableIntType(TruncBits) &&
1536 !isDesirableIntType(X->getType()->getScalarSizeInBits()))
1537 return nullptr;
1538
1539 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);
1540 return new ICmpInst(Pred, X, NewY);
1541}
1542
1543/// Fold icmp (xor X, Y), C.
1546 const APInt &C) {
1547 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1548 return I;
1549
1550 Value *X = Xor->getOperand(0);
1551 Value *Y = Xor->getOperand(1);
1552 const APInt *XorC;
1553 if (!match(Y, m_APInt(XorC)))
1554 return nullptr;
1555
1556 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1557 // fold the xor.
1558 ICmpInst::Predicate Pred = Cmp.getPredicate();
1559 bool TrueIfSigned = false;
1560 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {
1561
1562 // If the sign bit of the XorCst is not set, there is no change to
1563 // the operation, just stop using the Xor.
1564 if (!XorC->isNegative())
1565 return replaceOperand(Cmp, 0, X);
1566
1567 // Emit the opposite comparison.
1568 if (TrueIfSigned)
1569 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1570 ConstantInt::getAllOnesValue(X->getType()));
1571 else
1572 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1573 ConstantInt::getNullValue(X->getType()));
1574 }
1575
1576 if (Xor->hasOneUse()) {
1577 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1578 if (!Cmp.isEquality() && XorC->isSignMask()) {
1579 Pred = Cmp.getFlippedSignednessPredicate();
1580 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1581 }
1582
1583 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1584 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1585 Pred = Cmp.getFlippedSignednessPredicate();
1586 Pred = Cmp.getSwappedPredicate(Pred);
1587 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1588 }
1589 }
1590
1591 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1592 if (Pred == ICmpInst::ICMP_UGT) {
1593 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1594 if (*XorC == ~C && (C + 1).isPowerOf2())
1595 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1596 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1597 if (*XorC == C && (C + 1).isPowerOf2())
1598 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1599 }
1600 if (Pred == ICmpInst::ICMP_ULT) {
1601 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1602 if (*XorC == -C && C.isPowerOf2())
1603 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1604 ConstantInt::get(X->getType(), ~C));
1605 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1606 if (*XorC == C && (-C).isPowerOf2())
1607 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1608 ConstantInt::get(X->getType(), ~C));
1609 }
1610 return nullptr;
1611}
1612
1613/// For power-of-2 C:
1614/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1615/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1618 const APInt &C) {
1619 CmpInst::Predicate Pred = Cmp.getPredicate();
1620 APInt PowerOf2;
1621 if (Pred == ICmpInst::ICMP_ULT)
1622 PowerOf2 = C;
1623 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1624 PowerOf2 = C + 1;
1625 else
1626 return nullptr;
1627 if (!PowerOf2.isPowerOf2())
1628 return nullptr;
1629 Value *X;
1630 const APInt *ShiftC;
1632 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))
1633 return nullptr;
1634 uint64_t Shift = ShiftC->getLimitedValue();
1635 Type *XType = X->getType();
1636 if (Shift == 0 || PowerOf2.isMinSignedValue())
1637 return nullptr;
1638 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));
1639 APInt Bound =
1640 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1641 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));
1642}
1643
1644/// Fold icmp (and (sh X, Y), C2), C1.
1647 const APInt &C1,
1648 const APInt &C2) {
1649 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1650 if (!Shift || !Shift->isShift())
1651 return nullptr;
1652
1653 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1654 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1655 // code produced by the clang front-end, for bitfield access.
1656 // This seemingly simple opportunity to fold away a shift turns out to be
1657 // rather complicated. See PR17827 for details.
1658 unsigned ShiftOpcode = Shift->getOpcode();
1659 bool IsShl = ShiftOpcode == Instruction::Shl;
1660 const APInt *C3;
1661 if (match(Shift->getOperand(1), m_APInt(C3))) {
1662 APInt NewAndCst, NewCmpCst;
1663 bool AnyCmpCstBitsShiftedOut;
1664 if (ShiftOpcode == Instruction::Shl) {
1665 // For a left shift, we can fold if the comparison is not signed. We can
1666 // also fold a signed comparison if the mask value and comparison value
1667 // are not negative. These constraints may not be obvious, but we can
1668 // prove that they are correct using an SMT solver.
1669 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1670 return nullptr;
1671
1672 NewCmpCst = C1.lshr(*C3);
1673 NewAndCst = C2.lshr(*C3);
1674 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;
1675 } else if (ShiftOpcode == Instruction::LShr) {
1676 // For a logical right shift, we can fold if the comparison is not signed.
1677 // We can also fold a signed comparison if the shifted mask value and the
1678 // shifted comparison value are not negative. These constraints may not be
1679 // obvious, but we can prove that they are correct using an SMT solver.
1680 NewCmpCst = C1.shl(*C3);
1681 NewAndCst = C2.shl(*C3);
1682 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;
1683 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1684 return nullptr;
1685 } else {
1686 // For an arithmetic shift, check that both constants don't use (in a
1687 // signed sense) the top bits being shifted out.
1688 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1689 NewCmpCst = C1.shl(*C3);
1690 NewAndCst = C2.shl(*C3);
1691 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;
1692 if (NewAndCst.ashr(*C3) != C2)
1693 return nullptr;
1694 }
1695
1696 if (AnyCmpCstBitsShiftedOut) {
1697 // If we shifted bits out, the fold is not going to work out. As a
1698 // special case, check to see if this means that the result is always
1699 // true or false now.
1700 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1701 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1702 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1703 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1704 } else {
1705 Value *NewAnd = Builder.CreateAnd(
1706 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));
1707 return new ICmpInst(Cmp.getPredicate(),
1708 NewAnd, ConstantInt::get(And->getType(), NewCmpCst));
1709 }
1710 }
1711
1712 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1713 // preferable because it allows the C2 << Y expression to be hoisted out of a
1714 // loop if Y is invariant and X is not.
1715 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1716 !Shift->isArithmeticShift() && !isa<Constant>(Shift->getOperand(0))) {
1717 // Compute C2 << Y.
1718 Value *NewShift =
1719 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))
1720 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));
1721
1722 // Compute X & (C2 << Y).
1723 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1724 return replaceOperand(Cmp, 0, NewAnd);
1725 }
1726
1727 return nullptr;
1728}
1729
1730/// Fold icmp (and X, C2), C1.
1733 const APInt &C1) {
1734 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1735
1736 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
1737 // TODO: We canonicalize to the longer form for scalars because we have
1738 // better analysis/folds for icmp, and codegen may be better with icmp.
1739 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&
1740 match(And->getOperand(1), m_One()))
1741 return new TruncInst(And->getOperand(0), Cmp.getType());
1742
1743 const APInt *C2;
1744 Value *X;
1745 if (!match(And, m_And(m_Value(X), m_APInt(C2))))
1746 return nullptr;
1747
1748 // Don't perform the following transforms if the AND has multiple uses
1749 if (!And->hasOneUse())
1750 return nullptr;
1751
1752 if (Cmp.isEquality() && C1.isZero()) {
1753 // Restrict this fold to single-use 'and' (PR10267).
1754 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1755 if (C2->isSignMask()) {
1756 Constant *Zero = Constant::getNullValue(X->getType());
1757 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1758 return new ICmpInst(NewPred, X, Zero);
1759 }
1760
1761 APInt NewC2 = *C2;
1762 KnownBits Know = computeKnownBits(And->getOperand(0), 0, And);
1763 // Set high zeros of C2 to allow matching negated power-of-2.
1764 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
1765 Know.countMinLeadingZeros());
1766
1767 // Restrict this fold only for single-use 'and' (PR10267).
1768 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1769 if (NewC2.isNegatedPowerOf2()) {
1770 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);
1771 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1772 return new ICmpInst(NewPred, X, NegBOC);
1773 }
1774 }
1775
1776 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1777 // the input width without changing the value produced, eliminate the cast:
1778 //
1779 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1780 //
1781 // We can do this transformation if the constants do not have their sign bits
1782 // set or if it is an equality comparison. Extending a relational comparison
1783 // when we're checking the sign bit would not work.
1784 Value *W;
1785 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&
1786 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1787 // TODO: Is this a good transform for vectors? Wider types may reduce
1788 // throughput. Should this transform be limited (even for scalars) by using
1789 // shouldChangeType()?
1790 if (!Cmp.getType()->isVectorTy()) {
1791 Type *WideType = W->getType();
1792 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1793 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));
1794 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1795 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());
1796 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1797 }
1798 }
1799
1800 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))
1801 return I;
1802
1803 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1804 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1805 //
1806 // iff pred isn't signed
1807 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&
1808 match(And->getOperand(1), m_One())) {
1809 Constant *One = cast<Constant>(And->getOperand(1));
1810 Value *Or = And->getOperand(0);
1811 Value *A, *B, *LShr;
1812 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1813 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1814 unsigned UsesRemoved = 0;
1815 if (And->hasOneUse())
1816 ++UsesRemoved;
1817 if (Or->hasOneUse())
1818 ++UsesRemoved;
1819 if (LShr->hasOneUse())
1820 ++UsesRemoved;
1821
1822 // Compute A & ((1 << B) | 1)
1823 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;
1824 if (UsesRemoved >= RequireUsesRemoved) {
1825 Value *NewOr =
1826 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),
1827 /*HasNUW=*/true),
1828 One, Or->getName());
1829 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1830 return replaceOperand(Cmp, 0, NewAnd);
1831 }
1832 }
1833 }
1834
1835 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1836 // llvm.is.fpclass(X, fcInf|fcNan)
1837 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1838 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1839 Value *V;
1840 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1841 Attribute::NoImplicitFloat) &&
1842 Cmp.isEquality() &&
1844 Type *FPType = V->getType()->getScalarType();
1845 if (FPType->isIEEELikeFPTy() && C1 == *C2) {
1846 APInt ExponentMask =
1848 if (C1 == ExponentMask) {
1849 unsigned Mask = FPClassTest::fcNan | FPClassTest::fcInf;
1850 if (isICMP_NE)
1851 Mask = ~Mask & fcAllFlags;
1852 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));
1853 }
1854 }
1855 }
1856
1857 return nullptr;
1858}
1859
1860/// Fold icmp (and X, Y), C.
1863 const APInt &C) {
1864 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1865 return I;
1866
1867 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1868 bool TrueIfNeg;
1869 if (isSignBitCheck(Pred, C, TrueIfNeg)) {
1870 // ((X - 1) & ~X) < 0 --> X == 0
1871 // ((X - 1) & ~X) >= 0 --> X != 0
1872 Value *X;
1873 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&
1874 match(And->getOperand(1), m_Not(m_Specific(X)))) {
1875 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1876 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));
1877 }
1878 // (X & -X) < 0 --> X == MinSignedC
1879 // (X & -X) > -1 --> X != MinSignedC
1880 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {
1881 Constant *MinSignedC = ConstantInt::get(
1882 X->getType(),
1883 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
1884 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1885 return new ICmpInst(NewPred, X, MinSignedC);
1886 }
1887 }
1888
1889 // TODO: These all require that Y is constant too, so refactor with the above.
1890
1891 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1892 Value *X = And->getOperand(0);
1893 Value *Y = And->getOperand(1);
1894 if (auto *C2 = dyn_cast<ConstantInt>(Y))
1895 if (auto *LI = dyn_cast<LoadInst>(X))
1896 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1897 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1898 if (Instruction *Res =
1899 foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2))
1900 return Res;
1901
1902 if (!Cmp.isEquality())
1903 return nullptr;
1904
1905 // X & -C == -C -> X > u ~C
1906 // X & -C != -C -> X <= u ~C
1907 // iff C is a power of 2
1908 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {
1909 auto NewPred =
1911 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1912 }
1913
1914 // If we are testing the intersection of 2 select-of-nonzero-constants with no
1915 // common bits set, it's the same as checking if exactly one select condition
1916 // is set:
1917 // ((A ? TC : FC) & (B ? TC : FC)) == 0 --> xor A, B
1918 // ((A ? TC : FC) & (B ? TC : FC)) != 0 --> not(xor A, B)
1919 // TODO: Generalize for non-constant values.
1920 // TODO: Handle signed/unsigned predicates.
1921 // TODO: Handle other bitwise logic connectors.
1922 // TODO: Extend to handle a non-zero compare constant.
1923 if (C.isZero() && (Pred == CmpInst::ICMP_EQ || And->hasOneUse())) {
1924 assert(Cmp.isEquality() && "Not expecting non-equality predicates");
1925 Value *A, *B;
1926 const APInt *TC, *FC;
1927 if (match(X, m_Select(m_Value(A), m_APInt(TC), m_APInt(FC))) &&
1928 match(Y,
1929 m_Select(m_Value(B), m_SpecificInt(*TC), m_SpecificInt(*FC))) &&
1930 !TC->isZero() && !FC->isZero() && !TC->intersects(*FC)) {
1931 Value *R = Builder.CreateXor(A, B);
1932 if (Pred == CmpInst::ICMP_NE)
1933 R = Builder.CreateNot(R);
1934 return replaceInstUsesWith(Cmp, R);
1935 }
1936 }
1937
1938 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
1939 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1940 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1941 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
1943 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
1944 Value *TruncY = Builder.CreateTrunc(Y, X->getType());
1945 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
1946 Value *And = Builder.CreateAnd(TruncY, X);
1948 }
1949 return BinaryOperator::CreateAnd(TruncY, X);
1950 }
1951
1952 // (icmp eq/ne (and (shl -1, X), Y), 0)
1953 // -> (icmp eq/ne (lshr Y, X), 0)
1954 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
1955 // highly unlikely the non-zero case will ever show up in code.
1956 if (C.isZero() &&
1958 m_Value(Y))))) {
1959 Value *LShr = Builder.CreateLShr(Y, X);
1960 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));
1961 }
1962
1963 return nullptr;
1964}
1965
1966/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
1968 InstCombiner::BuilderTy &Builder) {
1969 // Are we using xors or subs to bitwise check for a pair or pairs of
1970 // (in)equalities? Convert to a shorter form that has more potential to be
1971 // folded even further.
1972 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
1973 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
1974 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
1975 // (X1 == X2) && (X3 == X4) && (X5 == X6)
1976 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
1977 // (X1 != X2) || (X3 != X4) || (X5 != X6)
1979 SmallVector<Value *, 16> WorkList(1, Or);
1980
1981 while (!WorkList.empty()) {
1982 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
1983 Value *Lhs, *Rhs;
1984
1985 if (match(OrOperatorArgument,
1986 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {
1987 CmpValues.emplace_back(Lhs, Rhs);
1988 return;
1989 }
1990
1991 if (match(OrOperatorArgument,
1992 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {
1993 CmpValues.emplace_back(Lhs, Rhs);
1994 return;
1995 }
1996
1997 WorkList.push_back(OrOperatorArgument);
1998 };
1999
2000 Value *CurrentValue = WorkList.pop_back_val();
2001 Value *OrOperatorLhs, *OrOperatorRhs;
2002
2003 if (!match(CurrentValue,
2004 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {
2005 return nullptr;
2006 }
2007
2008 MatchOrOperatorArgument(OrOperatorRhs);
2009 MatchOrOperatorArgument(OrOperatorLhs);
2010 }
2011
2012 ICmpInst::Predicate Pred = Cmp.getPredicate();
2013 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2014 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,
2015 CmpValues.rbegin()->second);
2016
2017 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2018 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);
2019 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);
2020 }
2021
2022 return LhsCmp;
2023}
2024
2025/// Fold icmp (or X, Y), C.
2028 const APInt &C) {
2029 ICmpInst::Predicate Pred = Cmp.getPredicate();
2030 if (C.isOne()) {
2031 // icmp slt signum(V) 1 --> icmp slt V, 1
2032 Value *V = nullptr;
2033 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
2034 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2035 ConstantInt::get(V->getType(), 1));
2036 }
2037
2038 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
2039
2040 // (icmp eq/ne (or disjoint x, C0), C1)
2041 // -> (icmp eq/ne x, C0^C1)
2042 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
2043 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {
2044 Value *NewC =
2045 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));
2046 return new ICmpInst(Pred, OrOp0, NewC);
2047 }
2048
2049 const APInt *MaskC;
2050 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {
2051 if (*MaskC == C && (C + 1).isPowerOf2()) {
2052 // X | C == C --> X <=u C
2053 // X | C != C --> X >u C
2054 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2056 return new ICmpInst(Pred, OrOp0, OrOp1);
2057 }
2058
2059 // More general: canonicalize 'equality with set bits mask' to
2060 // 'equality with clear bits mask'.
2061 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2062 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2063 if (Or->hasOneUse()) {
2064 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));
2065 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));
2066 return new ICmpInst(Pred, And, NewC);
2067 }
2068 }
2069
2070 // (X | (X-1)) s< 0 --> X s< 1
2071 // (X | (X-1)) s> -1 --> X s> 0
2072 Value *X;
2073 bool TrueIfSigned;
2074 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
2076 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2077 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
2078 return new ICmpInst(NewPred, X, NewC);
2079 }
2080
2081 const APInt *OrC;
2082 // icmp(X | OrC, C) --> icmp(X, 0)
2083 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {
2084 switch (Pred) {
2085 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2086 case ICmpInst::ICMP_SLT:
2087 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2088 case ICmpInst::ICMP_SGE:
2089 if (OrC->sge(C))
2090 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
2091 break;
2092 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2093 case ICmpInst::ICMP_SLE:
2094 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2095 case ICmpInst::ICMP_SGT:
2096 if (OrC->sgt(C))
2098 ConstantInt::getNullValue(X->getType()));
2099 break;
2100 default:
2101 break;
2102 }
2103 }
2104
2105 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2106 return nullptr;
2107
2108 Value *P, *Q;
2110 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2111 // -> and (icmp eq P, null), (icmp eq Q, null).
2112 Value *CmpP =
2113 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
2114 Value *CmpQ =
2116 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2117 return BinaryOperator::Create(BOpc, CmpP, CmpQ);
2118 }
2119
2120 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2121 return replaceInstUsesWith(Cmp, V);
2122
2123 return nullptr;
2124}
2125
2126/// Fold icmp (mul X, Y), C.
2129 const APInt &C) {
2130 ICmpInst::Predicate Pred = Cmp.getPredicate();
2131 Type *MulTy = Mul->getType();
2132 Value *X = Mul->getOperand(0);
2133
2134 // If there's no overflow:
2135 // X * X == 0 --> X == 0
2136 // X * X != 0 --> X != 0
2137 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
2138 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2139 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2140
2141 const APInt *MulC;
2142 if (!match(Mul->getOperand(1), m_APInt(MulC)))
2143 return nullptr;
2144
2145 // If this is a test of the sign bit and the multiply is sign-preserving with
2146 // a constant operand, use the multiply LHS operand instead:
2147 // (X * +MulC) < 0 --> X < 0
2148 // (X * -MulC) < 0 --> X > 0
2149 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2150 if (MulC->isNegative())
2151 Pred = ICmpInst::getSwappedPredicate(Pred);
2152 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2153 }
2154
2155 if (MulC->isZero())
2156 return nullptr;
2157
2158 // If the multiply does not wrap or the constant is odd, try to divide the
2159 // compare constant by the multiplication factor.
2160 if (Cmp.isEquality()) {
2161 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2162 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2163 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2164 return new ICmpInst(Pred, X, NewC);
2165 }
2166
2167 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2168 // correct to transform if MulC * N == C including overflow. I.e with i8
2169 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2170 // miss that case.
2171 if (C.urem(*MulC).isZero()) {
2172 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2173 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2174 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2175 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2176 return new ICmpInst(Pred, X, NewC);
2177 }
2178 }
2179 }
2180
2181 // With a matching no-overflow guarantee, fold the constants:
2182 // (X * MulC) < C --> X < (C / MulC)
2183 // (X * MulC) > C --> X > (C / MulC)
2184 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2185 Constant *NewC = nullptr;
2186 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2187 // MININT / -1 --> overflow.
2188 if (C.isMinSignedValue() && MulC->isAllOnes())
2189 return nullptr;
2190 if (MulC->isNegative())
2191 Pred = ICmpInst::getSwappedPredicate(Pred);
2192
2193 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2194 NewC = ConstantInt::get(
2196 } else {
2197 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2198 "Unexpected predicate");
2199 NewC = ConstantInt::get(
2201 }
2202 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2203 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2204 NewC = ConstantInt::get(
2206 } else {
2207 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2208 "Unexpected predicate");
2209 NewC = ConstantInt::get(
2211 }
2212 }
2213
2214 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2215}
2216
2217/// Fold icmp (shl 1, Y), C.
2219 const APInt &C) {
2220 Value *Y;
2221 if (!match(Shl, m_Shl(m_One(), m_Value(Y))))
2222 return nullptr;
2223
2224 Type *ShiftType = Shl->getType();
2225 unsigned TypeBits = C.getBitWidth();
2226 bool CIsPowerOf2 = C.isPowerOf2();
2227 ICmpInst::Predicate Pred = Cmp.getPredicate();
2228 if (Cmp.isUnsigned()) {
2229 // (1 << Y) pred C -> Y pred Log2(C)
2230 if (!CIsPowerOf2) {
2231 // (1 << Y) < 30 -> Y <= 4
2232 // (1 << Y) <= 30 -> Y <= 4
2233 // (1 << Y) >= 30 -> Y > 4
2234 // (1 << Y) > 30 -> Y > 4
2235 if (Pred == ICmpInst::ICMP_ULT)
2236 Pred = ICmpInst::ICMP_ULE;
2237 else if (Pred == ICmpInst::ICMP_UGE)
2238 Pred = ICmpInst::ICMP_UGT;
2239 }
2240
2241 unsigned CLog2 = C.logBase2();
2242 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2243 } else if (Cmp.isSigned()) {
2244 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
2245 // (1 << Y) > 0 -> Y != 31
2246 // (1 << Y) > C -> Y != 31 if C is negative.
2247 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))
2248 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2249
2250 // (1 << Y) < 0 -> Y == 31
2251 // (1 << Y) < 1 -> Y == 31
2252 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2253 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2254 if (Pred == ICmpInst::ICMP_SLT && (C-1).sle(0))
2255 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2256 }
2257
2258 return nullptr;
2259}
2260
2261/// Fold icmp (shl X, Y), C.
2263 BinaryOperator *Shl,
2264 const APInt &C) {
2265 const APInt *ShiftVal;
2266 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
2267 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);
2268
2269 ICmpInst::Predicate Pred = Cmp.getPredicate();
2270 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2271 // -> (icmp pred X, Csle0)
2272 //
2273 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2274 // so X's must be what is used.
2275 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2276 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2277
2278 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2279 // -> (icmp eq/ne X, 0)
2280 if (ICmpInst::isEquality(Pred) && C.isZero() &&
2281 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2282 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2283
2284 // (icmp slt (shl nsw X, Y), 0/1)
2285 // -> (icmp slt X, 0/1)
2286 // (icmp sgt (shl nsw X, Y), 0/-1)
2287 // -> (icmp sgt X, 0/-1)
2288 //
2289 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2290 if (Shl->hasNoSignedWrap() &&
2291 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2292 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2293 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2294
2295 const APInt *ShiftAmt;
2296 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2297 return foldICmpShlOne(Cmp, Shl, C);
2298
2299 // Check that the shift amount is in range. If not, don't perform undefined
2300 // shifts. When the shift is visited, it will be simplified.
2301 unsigned TypeBits = C.getBitWidth();
2302 if (ShiftAmt->uge(TypeBits))
2303 return nullptr;
2304
2305 Value *X = Shl->getOperand(0);
2306 Type *ShType = Shl->getType();
2307
2308 // NSW guarantees that we are only shifting out sign bits from the high bits,
2309 // so we can ASHR the compare constant without needing a mask and eliminate
2310 // the shift.
2311 if (Shl->hasNoSignedWrap()) {
2312 if (Pred == ICmpInst::ICMP_SGT) {
2313 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2314 APInt ShiftedC = C.ashr(*ShiftAmt);
2315 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2316 }
2317 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2318 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {
2319 APInt ShiftedC = C.ashr(*ShiftAmt);
2320 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2321 }
2322 if (Pred == ICmpInst::ICMP_SLT) {
2323 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2324 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2325 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2326 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2327 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2328 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;
2329 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2330 }
2331 }
2332
2333 // NUW guarantees that we are only shifting out zero bits from the high bits,
2334 // so we can LSHR the compare constant without needing a mask and eliminate
2335 // the shift.
2336 if (Shl->hasNoUnsignedWrap()) {
2337 if (Pred == ICmpInst::ICMP_UGT) {
2338 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2339 APInt ShiftedC = C.lshr(*ShiftAmt);
2340 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2341 }
2342 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2343 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {
2344 APInt ShiftedC = C.lshr(*ShiftAmt);
2345 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2346 }
2347 if (Pred == ICmpInst::ICMP_ULT) {
2348 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2349 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2350 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2351 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2352 assert(C.ugt(0) && "ult 0 should have been eliminated");
2353 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;
2354 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2355 }
2356 }
2357
2358 if (Cmp.isEquality() && Shl->hasOneUse()) {
2359 // Strength-reduce the shift into an 'and'.
2360 Constant *Mask = ConstantInt::get(
2361 ShType,
2362 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
2363 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2364 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));
2365 return new ICmpInst(Pred, And, LShrC);
2366 }
2367
2368 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2369 bool TrueIfSigned = false;
2370 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {
2371 // (X << 31) <s 0 --> (X & 1) != 0
2372 Constant *Mask = ConstantInt::get(
2373 ShType,
2374 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
2375 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2376 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2377 And, Constant::getNullValue(ShType));
2378 }
2379
2380 // Simplify 'shl' inequality test into 'and' equality test.
2381 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2382 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2383 if ((C + 1).isPowerOf2() &&
2384 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2385 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));
2386 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2388 And, Constant::getNullValue(ShType));
2389 }
2390 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2391 if (C.isPowerOf2() &&
2392 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2393 Value *And =
2394 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));
2395 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2397 And, Constant::getNullValue(ShType));
2398 }
2399 }
2400
2401 // Transform (icmp pred iM (shl iM %v, N), C)
2402 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2403 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2404 // This enables us to get rid of the shift in favor of a trunc that may be
2405 // free on the target. It has the additional benefit of comparing to a
2406 // smaller constant that may be more target-friendly.
2407 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
2408 if (Shl->hasOneUse() && Amt != 0 && C.countr_zero() >= Amt &&
2409 DL.isLegalInteger(TypeBits - Amt)) {
2410 Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt);
2411 if (auto *ShVTy = dyn_cast<VectorType>(ShType))
2412 TruncTy = VectorType::get(TruncTy, ShVTy->getElementCount());
2413 Constant *NewC =
2414 ConstantInt::get(TruncTy, C.ashr(*ShiftAmt).trunc(TypeBits - Amt));
2415 return new ICmpInst(Pred, Builder.CreateTrunc(X, TruncTy), NewC);
2416 }
2417
2418 return nullptr;
2419}
2420
2421/// Fold icmp ({al}shr X, Y), C.
2423 BinaryOperator *Shr,
2424 const APInt &C) {
2425 // An exact shr only shifts out zero bits, so:
2426 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2427 Value *X = Shr->getOperand(0);
2428 CmpInst::Predicate Pred = Cmp.getPredicate();
2429 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2430 return new ICmpInst(Pred, X, Cmp.getOperand(1));
2431
2432 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2433 const APInt *ShiftValC;
2434 if (match(X, m_APInt(ShiftValC))) {
2435 if (Cmp.isEquality())
2436 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
2437
2438 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2439 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2440 bool TrueIfSigned;
2441 if (!IsAShr && ShiftValC->isNegative() &&
2442 isSignBitCheck(Pred, C, TrueIfSigned))
2443 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2444 Shr->getOperand(1),
2445 ConstantInt::getNullValue(X->getType()));
2446
2447 // If the shifted constant is a power-of-2, test the shift amount directly:
2448 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2449 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2450 if (!IsAShr && ShiftValC->isPowerOf2() &&
2451 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2452 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2453 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2454 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2455
2456 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2457 unsigned ShiftLZ = ShiftValC->countl_zero();
2458 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
2459 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2460 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
2461 }
2462 }
2463
2464 const APInt *ShiftAmtC;
2465 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))
2466 return nullptr;
2467
2468 // Check that the shift amount is in range. If not, don't perform undefined
2469 // shifts. When the shift is visited it will be simplified.
2470 unsigned TypeBits = C.getBitWidth();
2471 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);
2472 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2473 return nullptr;
2474
2475 bool IsExact = Shr->isExact();
2476 Type *ShrTy = Shr->getType();
2477 // TODO: If we could guarantee that InstSimplify would handle all of the
2478 // constant-value-based preconditions in the folds below, then we could assert
2479 // those conditions rather than checking them. This is difficult because of
2480 // undef/poison (PR34838).
2481 if (IsAShr && Shr->hasOneUse()) {
2482 if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
2483 (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
2484 // When C - 1 is a power of two and the transform can be legally
2485 // performed, prefer this form so the produced constant is close to a
2486 // power of two.
2487 // icmp slt/ult (ashr exact X, ShAmtC), C
2488 // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
2489 APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
2490 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2491 }
2492 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2493 // When ShAmtC can be shifted losslessly:
2494 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2495 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2496 APInt ShiftedC = C.shl(ShAmtVal);
2497 if (ShiftedC.ashr(ShAmtVal) == C)
2498 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2499 }
2500 if (Pred == CmpInst::ICMP_SGT) {
2501 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2502 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2503 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&
2504 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
2505 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2506 }
2507 if (Pred == CmpInst::ICMP_UGT) {
2508 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2509 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2510 // clause accounts for that pattern.
2511 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2512 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
2513 (C + 1).shl(ShAmtVal).isMinSignedValue())
2514 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2515 }
2516
2517 // If the compare constant has significant bits above the lowest sign-bit,
2518 // then convert an unsigned cmp to a test of the sign-bit:
2519 // (ashr X, ShiftC) u> C --> X s< 0
2520 // (ashr X, ShiftC) u< C --> X s> -1
2521 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2522 if (Pred == CmpInst::ICMP_UGT) {
2523 return new ICmpInst(CmpInst::ICMP_SLT, X,
2525 }
2526 if (Pred == CmpInst::ICMP_ULT) {
2527 return new ICmpInst(CmpInst::ICMP_SGT, X,
2529 }
2530 }
2531 } else if (!IsAShr) {
2532 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2533 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2534 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2535 APInt ShiftedC = C.shl(ShAmtVal);
2536 if (ShiftedC.lshr(ShAmtVal) == C)
2537 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2538 }
2539 if (Pred == CmpInst::ICMP_UGT) {
2540 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2541 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2542 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))
2543 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2544 }
2545 }
2546
2547 if (!Cmp.isEquality())
2548 return nullptr;
2549
2550 // Handle equality comparisons of shift-by-constant.
2551
2552 // If the comparison constant changes with the shift, the comparison cannot
2553 // succeed (bits of the comparison constant cannot match the shifted value).
2554 // This should be known by InstSimplify and already be folded to true/false.
2555 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2556 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2557 "Expected icmp+shr simplify did not occur.");
2558
2559 // If the bits shifted out are known zero, compare the unshifted value:
2560 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2561 if (Shr->isExact())
2562 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));
2563
2564 if (C.isZero()) {
2565 // == 0 is u< 1.
2566 if (Pred == CmpInst::ICMP_EQ)
2567 return new ICmpInst(CmpInst::ICMP_ULT, X,
2568 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal)));
2569 else
2570 return new ICmpInst(CmpInst::ICMP_UGT, X,
2571 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal) - 1));
2572 }
2573
2574 if (Shr->hasOneUse()) {
2575 // Canonicalize the shift into an 'and':
2576 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2577 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2578 Constant *Mask = ConstantInt::get(ShrTy, Val);
2579 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");
2580 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));
2581 }
2582
2583 return nullptr;
2584}
2585
2587 BinaryOperator *SRem,
2588 const APInt &C) {
2589 // Match an 'is positive' or 'is negative' comparison of remainder by a
2590 // constant power-of-2 value:
2591 // (X % pow2C) sgt/slt 0
2592 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2593 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2594 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2595 return nullptr;
2596
2597 // TODO: The one-use check is standard because we do not typically want to
2598 // create longer instruction sequences, but this might be a special-case
2599 // because srem is not good for analysis or codegen.
2600 if (!SRem->hasOneUse())
2601 return nullptr;
2602
2603 const APInt *DivisorC;
2604 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
2605 return nullptr;
2606
2607 // For cmp_sgt/cmp_slt only zero valued C is handled.
2608 // For cmp_eq/cmp_ne only positive valued C is handled.
2609 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2610 !C.isZero()) ||
2611 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2612 !C.isStrictlyPositive()))
2613 return nullptr;
2614
2615 // Mask off the sign bit and the modulo bits (low-bits).
2616 Type *Ty = SRem->getType();
2618 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
2619 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);
2620
2621 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2622 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));
2623
2624 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2625 // bit is set. Example:
2626 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2627 if (Pred == ICmpInst::ICMP_SGT)
2629
2630 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2631 // bit is set. Example:
2632 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2633 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
2634}
2635
2636/// Fold icmp (udiv X, Y), C.
2638 BinaryOperator *UDiv,
2639 const APInt &C) {
2640 ICmpInst::Predicate Pred = Cmp.getPredicate();
2641 Value *X = UDiv->getOperand(0);
2642 Value *Y = UDiv->getOperand(1);
2643 Type *Ty = UDiv->getType();
2644
2645 const APInt *C2;
2646 if (!match(X, m_APInt(C2)))
2647 return nullptr;
2648
2649 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2650
2651 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2652 if (Pred == ICmpInst::ICMP_UGT) {
2653 assert(!C.isMaxValue() &&
2654 "icmp ugt X, UINT_MAX should have been simplified already.");
2655 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2656 ConstantInt::get(Ty, C2->udiv(C + 1)));
2657 }
2658
2659 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2660 if (Pred == ICmpInst::ICMP_ULT) {
2661 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2662 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2663 ConstantInt::get(Ty, C2->udiv(C)));
2664 }
2665
2666 return nullptr;
2667}
2668
2669/// Fold icmp ({su}div X, Y), C.
2671 BinaryOperator *Div,
2672 const APInt &C) {
2673 ICmpInst::Predicate Pred = Cmp.getPredicate();
2674 Value *X = Div->getOperand(0);
2675 Value *Y = Div->getOperand(1);
2676 Type *Ty = Div->getType();
2677 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2678
2679 // If unsigned division and the compare constant is bigger than
2680 // UMAX/2 (negative), there's only one pair of values that satisfies an
2681 // equality check, so eliminate the division:
2682 // (X u/ Y) == C --> (X == C) && (Y == 1)
2683 // (X u/ Y) != C --> (X != C) || (Y != 1)
2684 // Similarly, if signed division and the compare constant is exactly SMIN:
2685 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2686 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2687 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2688 (!DivIsSigned || C.isMinSignedValue())) {
2689 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));
2690 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));
2691 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2692 return BinaryOperator::Create(Logic, XBig, YOne);
2693 }
2694
2695 // Fold: icmp pred ([us]div X, C2), C -> range test
2696 // Fold this div into the comparison, producing a range check.
2697 // Determine, based on the divide type, what the range is being
2698 // checked. If there is an overflow on the low or high side, remember
2699 // it, otherwise compute the range [low, hi) bounding the new value.
2700 // See: InsertRangeTest above for the kinds of replacements possible.
2701 const APInt *C2;
2702 if (!match(Y, m_APInt(C2)))
2703 return nullptr;
2704
2705 // FIXME: If the operand types don't match the type of the divide
2706 // then don't attempt this transform. The code below doesn't have the
2707 // logic to deal with a signed divide and an unsigned compare (and
2708 // vice versa). This is because (x /s C2) <s C produces different
2709 // results than (x /s C2) <u C or (x /u C2) <s C or even
2710 // (x /u C2) <u C. Simply casting the operands and result won't
2711 // work. :( The if statement below tests that condition and bails
2712 // if it finds it.
2713 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
2714 return nullptr;
2715
2716 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2717 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2718 // division-by-constant cases should be present, we can not assert that they
2719 // have happened before we reach this icmp instruction.
2720 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2721 return nullptr;
2722
2723 // Compute Prod = C * C2. We are essentially solving an equation of
2724 // form X / C2 = C. We solve for X by multiplying C2 and C.
2725 // By solving for X, we can turn this into a range check instead of computing
2726 // a divide.
2727 APInt Prod = C * *C2;
2728
2729 // Determine if the product overflows by seeing if the product is not equal to
2730 // the divide. Make sure we do the same kind of divide as in the LHS
2731 // instruction that we're folding.
2732 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;
2733
2734 // If the division is known to be exact, then there is no remainder from the
2735 // divide, so the covered range size is unit, otherwise it is the divisor.
2736 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2737
2738 // Figure out the interval that is being checked. For example, a comparison
2739 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2740 // Compute this interval based on the constants involved and the signedness of
2741 // the compare/divide. This computes a half-open interval, keeping track of
2742 // whether either value in the interval overflows. After analysis each
2743 // overflow variable is set to 0 if it's corresponding bound variable is valid
2744 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2745 int LoOverflow = 0, HiOverflow = 0;
2746 APInt LoBound, HiBound;
2747
2748 if (!DivIsSigned) { // udiv
2749 // e.g. X/5 op 3 --> [15, 20)
2750 LoBound = Prod;
2751 HiOverflow = LoOverflow = ProdOV;
2752 if (!HiOverflow) {
2753 // If this is not an exact divide, then many values in the range collapse
2754 // to the same result value.
2755 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2756 }
2757 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2758 if (C.isZero()) { // (X / pos) op 0
2759 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2760 LoBound = -(RangeSize - 1);
2761 HiBound = RangeSize;
2762 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2763 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2764 HiOverflow = LoOverflow = ProdOV;
2765 if (!HiOverflow)
2766 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2767 } else { // (X / pos) op neg
2768 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2769 HiBound = Prod + 1;
2770 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2771 if (!LoOverflow) {
2772 APInt DivNeg = -RangeSize;
2773 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2774 }
2775 }
2776 } else if (C2->isNegative()) { // Divisor is < 0.
2777 if (Div->isExact())
2778 RangeSize.negate();
2779 if (C.isZero()) { // (X / neg) op 0
2780 // e.g. X/-5 op 0 --> [-4, 5)
2781 LoBound = RangeSize + 1;
2782 HiBound = -RangeSize;
2783 if (HiBound == *C2) { // -INTMIN = INTMIN
2784 HiOverflow = 1; // [INTMIN+1, overflow)
2785 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2786 }
2787 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2788 // e.g. X/-5 op 3 --> [-19, -14)
2789 HiBound = Prod + 1;
2790 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2791 if (!LoOverflow)
2792 LoOverflow =
2793 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;
2794 } else { // (X / neg) op neg
2795 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2796 LoOverflow = HiOverflow = ProdOV;
2797 if (!HiOverflow)
2798 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2799 }
2800
2801 // Dividing by a negative swaps the condition. LT <-> GT
2802 Pred = ICmpInst::getSwappedPredicate(Pred);
2803 }
2804
2805 switch (Pred) {
2806 default:
2807 llvm_unreachable("Unhandled icmp predicate!");
2808 case ICmpInst::ICMP_EQ:
2809 if (LoOverflow && HiOverflow)
2810 return replaceInstUsesWith(Cmp, Builder.getFalse());
2811 if (HiOverflow)
2812 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2813 X, ConstantInt::get(Ty, LoBound));
2814 if (LoOverflow)
2815 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2816 X, ConstantInt::get(Ty, HiBound));
2817 return replaceInstUsesWith(
2818 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));
2819 case ICmpInst::ICMP_NE:
2820 if (LoOverflow && HiOverflow)
2821 return replaceInstUsesWith(Cmp, Builder.getTrue());
2822 if (HiOverflow)
2823 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2824 X, ConstantInt::get(Ty, LoBound));
2825 if (LoOverflow)
2826 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2827 X, ConstantInt::get(Ty, HiBound));
2828 return replaceInstUsesWith(
2829 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));
2830 case ICmpInst::ICMP_ULT:
2831 case ICmpInst::ICMP_SLT:
2832 if (LoOverflow == +1) // Low bound is greater than input range.
2833 return replaceInstUsesWith(Cmp, Builder.getTrue());
2834 if (LoOverflow == -1) // Low bound is less than input range.
2835 return replaceInstUsesWith(Cmp, Builder.getFalse());
2836 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));
2837 case ICmpInst::ICMP_UGT:
2838 case ICmpInst::ICMP_SGT:
2839 if (HiOverflow == +1) // High bound greater than input range.
2840 return replaceInstUsesWith(Cmp, Builder.getFalse());
2841 if (HiOverflow == -1) // High bound less than input range.
2842 return replaceInstUsesWith(Cmp, Builder.getTrue());
2843 if (Pred == ICmpInst::ICMP_UGT)
2844 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));
2845 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));
2846 }
2847
2848 return nullptr;
2849}
2850
2851/// Fold icmp (sub X, Y), C.
2853 BinaryOperator *Sub,
2854 const APInt &C) {
2855 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2856 ICmpInst::Predicate Pred = Cmp.getPredicate();
2857 Type *Ty = Sub->getType();
2858
2859 // (SubC - Y) == C) --> Y == (SubC - C)
2860 // (SubC - Y) != C) --> Y != (SubC - C)
2861 Constant *SubC;
2862 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {
2863 return new ICmpInst(Pred, Y,
2864 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));
2865 }
2866
2867 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
2868 const APInt *C2;
2869 APInt SubResult;
2870 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
2871 bool HasNSW = Sub->hasNoSignedWrap();
2872 bool HasNUW = Sub->hasNoUnsignedWrap();
2873 if (match(X, m_APInt(C2)) &&
2874 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
2875 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))
2876 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));
2877
2878 // X - Y == 0 --> X == Y.
2879 // X - Y != 0 --> X != Y.
2880 // TODO: We allow this with multiple uses as long as the other uses are not
2881 // in phis. The phi use check is guarding against a codegen regression
2882 // for a loop test. If the backend could undo this (and possibly
2883 // subsequent transforms), we would not need this hack.
2884 if (Cmp.isEquality() && C.isZero() &&
2885 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))
2886 return new ICmpInst(Pred, X, Y);
2887
2888 // The following transforms are only worth it if the only user of the subtract
2889 // is the icmp.
2890 // TODO: This is an artificial restriction for all of the transforms below
2891 // that only need a single replacement icmp. Can these use the phi test
2892 // like the transform above here?
2893 if (!Sub->hasOneUse())
2894 return nullptr;
2895
2896 if (Sub->hasNoSignedWrap()) {
2897 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
2898 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
2899 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
2900
2901 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
2902 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
2903 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
2904
2905 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
2906 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
2907 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
2908
2909 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
2910 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
2911 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
2912 }
2913
2914 if (!match(X, m_APInt(C2)))
2915 return nullptr;
2916
2917 // C2 - Y <u C -> (Y | (C - 1)) == C2
2918 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
2919 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
2920 (*C2 & (C - 1)) == (C - 1))
2921 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);
2922
2923 // C2 - Y >u C -> (Y | C) != C2
2924 // iff C2 & C == C and C + 1 is a power of 2
2925 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
2926 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);
2927
2928 // We have handled special cases that reduce.
2929 // Canonicalize any remaining sub to add as:
2930 // (C2 - Y) > C --> (Y + ~C2) < ~C
2931 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",
2932 HasNUW, HasNSW);
2933 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
2934}
2935
2936static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
2937 Value *Op1, IRBuilderBase &Builder,
2938 bool HasOneUse) {
2939 auto FoldConstant = [&](bool Val) {
2940 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
2941 if (Op0->getType()->isVectorTy())
2943 cast<VectorType>(Op0->getType())->getElementCount(), Res);
2944 return Res;
2945 };
2946
2947 switch (Table.to_ulong()) {
2948 case 0: // 0 0 0 0
2949 return FoldConstant(false);
2950 case 1: // 0 0 0 1
2951 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
2952 case 2: // 0 0 1 0
2953 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
2954 case 3: // 0 0 1 1
2955 return Builder.CreateNot(Op0);
2956 case 4: // 0 1 0 0
2957 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
2958 case 5: // 0 1 0 1
2959 return Builder.CreateNot(Op1);
2960 case 6: // 0 1 1 0
2961 return Builder.CreateXor(Op0, Op1);
2962 case 7: // 0 1 1 1
2963 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
2964 case 8: // 1 0 0 0
2965 return Builder.CreateAnd(Op0, Op1);
2966 case 9: // 1 0 0 1
2967 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
2968 case 10: // 1 0 1 0
2969 return Op1;
2970 case 11: // 1 0 1 1
2971 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
2972 case 12: // 1 1 0 0
2973 return Op0;
2974 case 13: // 1 1 0 1
2975 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
2976 case 14: // 1 1 1 0
2977 return Builder.CreateOr(Op0, Op1);
2978 case 15: // 1 1 1 1
2979 return FoldConstant(true);
2980 default:
2981 llvm_unreachable("Invalid Operation");
2982 }
2983 return nullptr;
2984}
2985
2986/// Fold icmp (add X, Y), C.
2989 const APInt &C) {
2990 Value *Y = Add->getOperand(1);
2991 Value *X = Add->getOperand(0);
2992
2993 Value *Op0, *Op1;
2994 Instruction *Ext0, *Ext1;
2995 const CmpInst::Predicate Pred = Cmp.getPredicate();
2996 if (match(Add,
2999 m_ZExtOrSExt(m_Value(Op1))))) &&
3000 Op0->getType()->isIntOrIntVectorTy(1) &&
3001 Op1->getType()->isIntOrIntVectorTy(1)) {
3002 unsigned BW = C.getBitWidth();
3003 std::bitset<4> Table;
3004 auto ComputeTable = [&](bool Op0Val, bool Op1Val) {
3005 int Res = 0;
3006 if (Op0Val)
3007 Res += isa<ZExtInst>(Ext0) ? 1 : -1;
3008 if (Op1Val)
3009 Res += isa<ZExtInst>(Ext1) ? 1 : -1;
3010 return ICmpInst::compare(APInt(BW, Res, true), C, Pred);
3011 };
3012
3013 Table[0] = ComputeTable(false, false);
3014 Table[1] = ComputeTable(false, true);
3015 Table[2] = ComputeTable(true, false);
3016 Table[3] = ComputeTable(true, true);
3017 if (auto *Cond =
3018 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))
3019 return replaceInstUsesWith(Cmp, Cond);
3020 }
3021 const APInt *C2;
3022 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3023 return nullptr;
3024
3025 // Fold icmp pred (add X, C2), C.
3026 Type *Ty = Add->getType();
3027
3028 // If the add does not wrap, we can always adjust the compare by subtracting
3029 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3030 // are canonicalized to SGT/SLT/UGT/ULT.
3031 if ((Add->hasNoSignedWrap() &&
3032 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
3033 (Add->hasNoUnsignedWrap() &&
3034 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
3035 bool Overflow;
3036 APInt NewC =
3037 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);
3038 // If there is overflow, the result must be true or false.
3039 // TODO: Can we assert there is no overflow because InstSimplify always
3040 // handles those cases?
3041 if (!Overflow)
3042 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3043 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
3044 }
3045
3046 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
3047 const APInt &Upper = CR.getUpper();
3048 const APInt &Lower = CR.getLower();
3049 if (Cmp.isSigned()) {
3050 if (Lower.isSignMask())
3051 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
3052 if (Upper.isSignMask())
3053 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
3054 } else {
3055 if (Lower.isMinValue())
3056 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
3057 if (Upper.isMinValue())
3058 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
3059 }
3060
3061 // This set of folds is intentionally placed after folds that use no-wrapping
3062 // flags because those folds are likely better for later analysis/codegen.
3065
3066 // Fold compare with offset to opposite sign compare if it eliminates offset:
3067 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3068 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3069 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));
3070
3071 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3072 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3073 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));
3074
3075 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3076 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3077 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));
3078
3079 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3080 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3081 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));
3082
3083 // (X + -1) <u C --> X <=u C (if X is never null)
3084 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3085 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3086 if (llvm::isKnownNonZero(X, Q))
3087 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));
3088 }
3089
3090 if (!Add->hasOneUse())
3091 return nullptr;
3092
3093 // X+C <u C2 -> (X & -C2) == C
3094 // iff C & (C2-1) == 0
3095 // C2 is a power of 2
3096 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3098 ConstantExpr::getNeg(cast<Constant>(Y)));
3099
3100 // X+C >u C2 -> (X & ~C2) != C
3101 // iff C & C2 == 0
3102 // C2+1 is a power of 2
3103 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3105 ConstantExpr::getNeg(cast<Constant>(Y)));
3106
3107 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3108 // to the ult form.
3109 // X+C2 >u C -> X+(C2-C-1) <u ~C
3110 if (Pred == ICmpInst::ICMP_UGT)
3111 return new ICmpInst(ICmpInst::ICMP_ULT,
3112 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
3113 ConstantInt::get(Ty, ~C));
3114
3115 return nullptr;
3116}
3117
3119 Value *&RHS, ConstantInt *&Less,
3120 ConstantInt *&Equal,
3121 ConstantInt *&Greater) {
3122 // TODO: Generalize this to work with other comparison idioms or ensure
3123 // they get canonicalized into this form.
3124
3125 // select i1 (a == b),
3126 // i32 Equal,
3127 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3128 // where Equal, Less and Greater are placeholders for any three constants.
3129 ICmpInst::Predicate PredA;
3130 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||
3131 !ICmpInst::isEquality(PredA))
3132 return false;
3133 Value *EqualVal = SI->getTrueValue();
3134 Value *UnequalVal = SI->getFalseValue();
3135 // We still can get non-canonical predicate here, so canonicalize.
3136 if (PredA == ICmpInst::ICMP_NE)
3137 std::swap(EqualVal, UnequalVal);
3138 if (!match(EqualVal, m_ConstantInt(Equal)))
3139 return false;
3140 ICmpInst::Predicate PredB;
3141 Value *LHS2, *RHS2;
3142 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),
3143 m_ConstantInt(Less), m_ConstantInt(Greater))))
3144 return false;
3145 // We can get predicate mismatch here, so canonicalize if possible:
3146 // First, ensure that 'LHS' match.
3147 if (LHS2 != LHS) {
3148 // x sgt y <--> y slt x
3149 std::swap(LHS2, RHS2);
3150 PredB = ICmpInst::getSwappedPredicate(PredB);
3151 }
3152 if (LHS2 != LHS)
3153 return false;
3154 // We also need to canonicalize 'RHS'.
3155 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
3156 // x sgt C-1 <--> x sge C <--> not(x slt C)
3157 auto FlippedStrictness =
3159 PredB, cast<Constant>(RHS2));
3160 if (!FlippedStrictness)
3161 return false;
3162 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3163 "basic correctness failure");
3164 RHS2 = FlippedStrictness->second;
3165 // And kind-of perform the result swap.
3166 std::swap(Less, Greater);
3167 PredB = ICmpInst::ICMP_SLT;
3168 }
3169 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3170}
3171
3174 ConstantInt *C) {
3175
3176 assert(C && "Cmp RHS should be a constant int!");
3177 // If we're testing a constant value against the result of a three way
3178 // comparison, the result can be expressed directly in terms of the
3179 // original values being compared. Note: We could possibly be more
3180 // aggressive here and remove the hasOneUse test. The original select is
3181 // really likely to simplify or sink when we remove a test of the result.
3182 Value *OrigLHS, *OrigRHS;
3183 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3184 if (Cmp.hasOneUse() &&
3185 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,
3186 C3GreaterThan)) {
3187 assert(C1LessThan && C2Equal && C3GreaterThan);
3188
3189 bool TrueWhenLessThan = ICmpInst::compare(
3190 C1LessThan->getValue(), C->getValue(), Cmp.getPredicate());
3191 bool TrueWhenEqual = ICmpInst::compare(C2Equal->getValue(), C->getValue(),
3192 Cmp.getPredicate());
3193 bool TrueWhenGreaterThan = ICmpInst::compare(
3194 C3GreaterThan->getValue(), C->getValue(), Cmp.getPredicate());
3195
3196 // This generates the new instruction that will replace the original Cmp
3197 // Instruction. Instead of enumerating the various combinations when
3198 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3199 // false, we rely on chaining of ORs and future passes of InstCombine to
3200 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3201
3202 // When none of the three constants satisfy the predicate for the RHS (C),
3203 // the entire original Cmp can be simplified to a false.
3205 if (TrueWhenLessThan)
3207 OrigLHS, OrigRHS));
3208 if (TrueWhenEqual)
3210 OrigLHS, OrigRHS));
3211 if (TrueWhenGreaterThan)
3213 OrigLHS, OrigRHS));
3214
3215 return replaceInstUsesWith(Cmp, Cond);
3216 }
3217 return nullptr;
3218}
3219
3221 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));
3222 if (!Bitcast)
3223 return nullptr;
3224
3225 ICmpInst::Predicate Pred = Cmp.getPredicate();
3226 Value *Op1 = Cmp.getOperand(1);
3227 Value *BCSrcOp = Bitcast->getOperand(0);
3228 Type *SrcType = Bitcast->getSrcTy();
3229 Type *DstType = Bitcast->getType();
3230
3231 // Make sure the bitcast doesn't change between scalar and vector and
3232 // doesn't change the number of vector elements.
3233 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3234 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3235 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3236 Value *X;
3237 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {
3238 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3239 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3240 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3241 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3242 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3243 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3244 match(Op1, m_Zero()))
3245 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3246
3247 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3248 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))
3249 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));
3250
3251 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3252 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))
3253 return new ICmpInst(Pred, X,
3254 ConstantInt::getAllOnesValue(X->getType()));
3255 }
3256
3257 // Zero-equality checks are preserved through unsigned floating-point casts:
3258 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3259 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3260 if (match(BCSrcOp, m_UIToFP(m_Value(X))))
3261 if (Cmp.isEquality() && match(Op1, m_Zero()))
3262 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3263
3264 const APInt *C;
3265 bool TrueIfSigned;
3266 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {
3267 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3268 // the FP extend/truncate because that cast does not change the sign-bit.
3269 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3270 // The sign-bit is always the most significant bit in those types.
3271 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&
3272 (match(BCSrcOp, m_FPExt(m_Value(X))) ||
3273 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {
3274 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3275 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3276 Type *XType = X->getType();
3277
3278 // We can't currently handle Power style floating point operations here.
3279 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3280 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());
3281 if (auto *XVTy = dyn_cast<VectorType>(XType))
3282 NewType = VectorType::get(NewType, XVTy->getElementCount());
3283 Value *NewBitcast = Builder.CreateBitCast(X, NewType);
3284 if (TrueIfSigned)
3285 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3286 ConstantInt::getNullValue(NewType));
3287 else
3288 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3290 }
3291 }
3292
3293 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3294 Type *FPType = SrcType->getScalarType();
3295 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3296 Attribute::NoImplicitFloat) &&
3297 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3298 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3299 if (Mask & (fcInf | fcZero)) {
3300 if (Pred == ICmpInst::ICMP_NE)
3301 Mask = ~Mask;
3302 return replaceInstUsesWith(Cmp,
3303 Builder.createIsFPClass(BCSrcOp, Mask));
3304 }
3305 }
3306 }
3307 }
3308
3309 const APInt *C;
3310 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||
3311 !SrcType->isIntOrIntVectorTy())
3312 return nullptr;
3313
3314 // If this is checking if all elements of a vector compare are set or not,
3315 // invert the casted vector equality compare and test if all compare
3316 // elements are clear or not. Compare against zero is generally easier for
3317 // analysis and codegen.
3318 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3319 // Example: are all elements equal? --> are zero elements not equal?
3320 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3321 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3322 if (Value *NotBCSrcOp =
3323 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {
3324 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);
3325 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));
3326 }
3327 }
3328
3329 // If this is checking if all elements of an extended vector are clear or not,
3330 // compare in a narrow type to eliminate the extend:
3331 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3332 Value *X;
3333 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3334 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {
3335 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {
3336 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());
3337 Value *NewCast = Builder.CreateBitCast(X, NewType);
3338 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));
3339 }
3340 }
3341
3342 // Folding: icmp <pred> iN X, C
3343 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3344 // and C is a splat of a K-bit pattern
3345 // and SC is a constant vector = <C', C', C', ..., C'>
3346 // Into:
3347 // %E = extractelement <M x iK> %vec, i32 C'
3348 // icmp <pred> iK %E, trunc(C)
3349 Value *Vec;
3350 ArrayRef<int> Mask;
3351 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {
3352 // Check whether every element of Mask is the same constant
3353 if (all_equal(Mask)) {
3354 auto *VecTy = cast<VectorType>(SrcType);
3355 auto *EltTy = cast<IntegerType>(VecTy->getElementType());
3356 if (C->isSplat(EltTy->getBitWidth())) {
3357 // Fold the icmp based on the value of C
3358 // If C is M copies of an iK sized bit pattern,
3359 // then:
3360 // => %E = extractelement <N x iK> %vec, i32 Elem
3361 // icmp <pred> iK %SplatVal, <pattern>
3362 Value *Elem = Builder.getInt32(Mask[0]);
3363 Value *Extract = Builder.CreateExtractElement(Vec, Elem);
3364 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));
3365 return new ICmpInst(Pred, Extract, NewC);
3366 }
3367 }
3368 }
3369 return nullptr;
3370}
3371
3372/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3373/// where X is some kind of instruction.
3375 const APInt *C;
3376
3377 if (match(Cmp.getOperand(1), m_APInt(C))) {
3378 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))
3379 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))
3380 return I;
3381
3382 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))
3383 // For now, we only support constant integers while folding the
3384 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3385 // similar to the cases handled by binary ops above.
3386 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))
3387 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))
3388 return I;
3389
3390 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))
3391 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))
3392 return I;
3393
3394 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))
3395 if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, II, *C))
3396 return I;
3397
3398 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3399 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3400 // TODO: This checks one-use, but that is not strictly necessary.
3401 Value *Cmp0 = Cmp.getOperand(0);
3402 Value *X, *Y;
3403 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3404 (match(Cmp0,
3405 m_ExtractValue<0>(m_Intrinsic<Intrinsic::ssub_with_overflow>(
3406 m_Value(X), m_Value(Y)))) ||
3407 match(Cmp0,
3408 m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>(
3409 m_Value(X), m_Value(Y))))))
3410 return new ICmpInst(Cmp.getPredicate(), X, Y);
3411 }
3412
3413 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))
3415
3416 return nullptr;
3417}
3418
3419/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3420/// icmp eq/ne BO, C.
3422 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3423 // TODO: Some of these folds could work with arbitrary constants, but this
3424 // function is limited to scalar and vector splat constants.
3425 if (!Cmp.isEquality())
3426 return nullptr;
3427
3428 ICmpInst::Predicate Pred = Cmp.getPredicate();
3429 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3430 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
3431 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
3432
3433 switch (BO->getOpcode()) {
3434 case Instruction::SRem:
3435 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3436 if (C.isZero() && BO->hasOneUse()) {
3437 const APInt *BOC;
3438 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
3439 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());
3440 return new ICmpInst(Pred, NewRem,
3442 }
3443 }
3444 break;
3445 case Instruction::Add: {
3446 // (A + C2) == C --> A == (C - C2)
3447 // (A + C2) != C --> A != (C - C2)
3448 // TODO: Remove the one-use limitation? See discussion in D58633.
3449 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {
3450 if (BO->hasOneUse())
3451 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));
3452 } else if (C.isZero()) {
3453 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3454 // efficiently invertible, or if the add has just this one use.
3455 if (Value *NegVal = dyn_castNegVal(BOp1))
3456 return new ICmpInst(Pred, BOp0, NegVal);
3457 if (Value *NegVal = dyn_castNegVal(BOp0))
3458 return new ICmpInst(Pred, NegVal, BOp1);
3459 if (BO->hasOneUse()) {
3460 // (add nuw A, B) != 0 -> (or A, B) != 0
3461 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
3462 Value *Or = Builder.CreateOr(BOp0, BOp1);
3463 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
3464 }
3465 Value *Neg = Builder.CreateNeg(BOp1);
3466 Neg->takeName(BO);
3467 return new ICmpInst(Pred, BOp0, Neg);
3468 }
3469 }
3470 break;
3471 }
3472 case Instruction::Xor:
3473 if (BO->hasOneUse()) {
3474 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
3475 // For the xor case, we can xor two constants together, eliminating
3476 // the explicit xor.
3477 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
3478 } else if (C.isZero()) {
3479 // Replace ((xor A, B) != 0) with (A != B)
3480 return new ICmpInst(Pred, BOp0, BOp1);
3481 }
3482 }
3483 break;
3484 case Instruction::Or: {
3485 const APInt *BOC;
3486 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3487 // Comparing if all bits outside of a constant mask are set?
3488 // Replace (X | C) == -1 with (X & ~C) == ~C.
3489 // This removes the -1 constant.
3490 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));
3491 Value *And = Builder.CreateAnd(BOp0, NotBOC);
3492 return new ICmpInst(Pred, And, NotBOC);
3493 }
3494 break;
3495 }
3496 case Instruction::UDiv:
3497 case Instruction::SDiv:
3498 if (BO->isExact()) {
3499 // div exact X, Y eq/ne 0 -> X eq/ne 0
3500 // div exact X, Y eq/ne 1 -> X eq/ne Y
3501 // div exact X, Y eq/ne C ->
3502 // if Y * C never-overflow && OneUse:
3503 // -> Y * C eq/ne X
3504 if (C.isZero())
3505 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));
3506 else if (C.isOne())
3507 return new ICmpInst(Pred, BOp0, BOp1);
3508 else if (BO->hasOneUse()) {
3510 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,
3511 Cmp.getOperand(1), BO);
3513 Value *YC =
3514 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));
3515 return new ICmpInst(Pred, YC, BOp0);
3516 }
3517 }
3518 }
3519 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3520 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3521 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3522 return new ICmpInst(NewPred, BOp1, BOp0);
3523 }
3524 break;
3525 default:
3526 break;
3527 }
3528 return nullptr;
3529}
3530
3532 const APInt &CRhs,
3533 InstCombiner::BuilderTy &Builder,
3534 const SimplifyQuery &Q) {
3535 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3536 "Non-ctpop intrin in ctpop fold");
3537 if (!CtpopLhs->hasOneUse())
3538 return nullptr;
3539
3540 // Power of 2 test:
3541 // isPow2OrZero : ctpop(X) u< 2
3542 // isPow2 : ctpop(X) == 1
3543 // NotPow2OrZero: ctpop(X) u> 1
3544 // NotPow2 : ctpop(X) != 1
3545 // If we know any bit of X can be folded to:
3546 // IsPow2 : X & (~Bit) == 0
3547 // NotPow2 : X & (~Bit) != 0
3548 const ICmpInst::Predicate Pred = I.getPredicate();
3549 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3550 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3551 Value *Op = CtpopLhs->getArgOperand(0);
3552 KnownBits OpKnown = computeKnownBits(Op, Q.DL,
3553 /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT);
3554 // No need to check for count > 1, that should be already constant folded.
3555 if (OpKnown.countMinPopulation() == 1) {
3556 Value *And = Builder.CreateAnd(
3557 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));
3558 return new ICmpInst(
3559 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3562 And, Constant::getNullValue(Op->getType()));
3563 }
3564 }
3565
3566 return nullptr;
3567}
3568
3569/// Fold an equality icmp with LLVM intrinsic and constant operand.
3571 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3572 Type *Ty = II->getType();
3573 unsigned BitWidth = C.getBitWidth();
3574 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3575
3576 switch (II->getIntrinsicID()) {
3577 case Intrinsic::abs:
3578 // abs(A) == 0 -> A == 0
3579 // abs(A) == INT_MIN -> A == INT_MIN
3580 if (C.isZero() || C.isMinSignedValue())
3581 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));
3582 break;
3583
3584 case Intrinsic::bswap:
3585 // bswap(A) == C -> A == bswap(C)
3586 return new ICmpInst(Pred, II->getArgOperand(0),
3587 ConstantInt::get(Ty, C.byteSwap()));
3588
3589 case Intrinsic::bitreverse:
3590 // bitreverse(A) == C -> A == bitreverse(C)
3591 return new ICmpInst(Pred, II->getArgOperand(0),
3592 ConstantInt::get(Ty, C.reverseBits()));
3593
3594 case Intrinsic::ctlz:
3595 case Intrinsic::cttz: {
3596 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3597 if (C == BitWidth)
3598 return new ICmpInst(Pred, II->getArgOperand(0),
3600
3601 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3602 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3603 // Limit to one use to ensure we don't increase instruction count.
3604 unsigned Num = C.getLimitedValue(BitWidth);
3605 if (Num != BitWidth && II->hasOneUse()) {
3606 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3607 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)
3608 : APInt::getHighBitsSet(BitWidth, Num + 1);
3609 APInt Mask2 = IsTrailing
3612 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),
3613 ConstantInt::get(Ty, Mask2));
3614 }
3615 break;
3616 }
3617
3618 case Intrinsic::ctpop: {
3619 // popcount(A) == 0 -> A == 0 and likewise for !=
3620 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3621 bool IsZero = C.isZero();
3622 if (IsZero || C == BitWidth)
3623 return new ICmpInst(Pred, II->getArgOperand(0),
3624 IsZero ? Constant::getNullValue(Ty)
3626
3627 break;
3628 }
3629
3630 case Intrinsic::fshl:
3631 case Intrinsic::fshr:
3632 if (II->getArgOperand(0) == II->getArgOperand(1)) {
3633 const APInt *RotAmtC;
3634 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3635 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3636 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))
3637 return new ICmpInst(Pred, II->getArgOperand(0),
3638 II->getIntrinsicID() == Intrinsic::fshl
3639 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))
3640 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));
3641 }
3642 break;
3643
3644 case Intrinsic::umax:
3645 case Intrinsic::uadd_sat: {
3646 // uadd.sat(a, b) == 0 -> (a | b) == 0
3647 // umax(a, b) == 0 -> (a | b) == 0
3648 if (C.isZero() && II->hasOneUse()) {
3650 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3651 }
3652 break;
3653 }
3654
3655 case Intrinsic::ssub_sat:
3656 // ssub.sat(a, b) == 0 -> a == b
3657 if (C.isZero())
3658 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3659 break;
3660 case Intrinsic::usub_sat: {
3661 // usub.sat(a, b) == 0 -> a <= b
3662 if (C.isZero()) {
3663 ICmpInst::Predicate NewPred =
3665 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));
3666 }
3667 break;
3668 }
3669 default:
3670 break;
3671 }
3672
3673 return nullptr;
3674}
3675
3676/// Fold an icmp with LLVM intrinsics
3677static Instruction *
3679 InstCombiner::BuilderTy &Builder) {
3680 assert(Cmp.isEquality());
3681
3682 ICmpInst::Predicate Pred = Cmp.getPredicate();
3683 Value *Op0 = Cmp.getOperand(0);
3684 Value *Op1 = Cmp.getOperand(1);
3685 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);
3686 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);
3687 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3688 return nullptr;
3689
3690 switch (IIOp0->getIntrinsicID()) {
3691 case Intrinsic::bswap:
3692 case Intrinsic::bitreverse:
3693 // If both operands are byte-swapped or bit-reversed, just compare the
3694 // original values.
3695 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3696 case Intrinsic::fshl:
3697 case Intrinsic::fshr: {
3698 // If both operands are rotated by same amount, just compare the
3699 // original values.
3700 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))
3701 break;
3702 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))
3703 break;
3704 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))
3705 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3706
3707 // rotate(X, AmtX) == rotate(Y, AmtY)
3708 // -> rotate(X, AmtX - AmtY) == Y
3709 // Do this if either both rotates have one use or if only one has one use
3710 // and AmtX/AmtY are constants.
3711 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3712 if (OneUses == 2 ||
3713 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&
3714 match(IIOp1->getOperand(2), m_ImmConstant()))) {
3715 Value *SubAmt =
3716 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));
3717 Value *CombinedRotate = Builder.CreateIntrinsic(
3718 Op0->getType(), IIOp0->getIntrinsicID(),
3719 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});
3720 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);
3721 }
3722 } break;
3723 default:
3724 break;
3725 }
3726
3727 return nullptr;
3728}
3729
3730/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3731/// where X is some kind of instruction and C is AllowPoison.
3732/// TODO: Move more folds which allow poison to this function.
3735 const APInt &C) {
3736 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3737 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {
3738 switch (II->getIntrinsicID()) {
3739 default:
3740 break;
3741 case Intrinsic::fshl:
3742 case Intrinsic::fshr:
3743 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {
3744 // (rot X, ?) == 0/-1 --> X == 0/-1
3745 if (C.isZero() || C.isAllOnes())
3746 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
3747 }
3748 break;
3749 }
3750 }
3751
3752 return nullptr;
3753}
3754
3755/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
3757 BinaryOperator *BO,
3758 const APInt &C) {
3759 switch (BO->getOpcode()) {
3760 case Instruction::Xor:
3761 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
3762 return I;
3763 break;
3764 case Instruction::And:
3765 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
3766 return I;
3767 break;
3768 case Instruction::Or:
3769 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
3770 return I;
3771 break;
3772 case Instruction::Mul:
3773 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
3774 return I;
3775 break;
3776 case Instruction::Shl:
3777 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
3778 return I;
3779 break;
3780 case Instruction::LShr:
3781 case Instruction::AShr:
3782 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
3783 return I;
3784 break;
3785 case Instruction::SRem:
3786 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
3787 return I;
3788 break;
3789 case Instruction::UDiv:
3790 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
3791 return I;
3792 [[fallthrough]];
3793 case Instruction::SDiv:
3794 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
3795 return I;
3796 break;
3797 case Instruction::Sub:
3798 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
3799 return I;
3800 break;
3801 case Instruction::Add:
3802 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
3803 return I;
3804 break;
3805 default:
3806 break;
3807 }
3808
3809 // TODO: These folds could be refactored to be part of the above calls.
3810 return foldICmpBinOpEqualityWithConstant(Cmp, BO, C);
3811}
3812
3813static Instruction *
3815 SaturatingInst *II, const APInt &C,
3816 InstCombiner::BuilderTy &Builder) {
3817 // This transform may end up producing more than one instruction for the
3818 // intrinsic, so limit it to one user of the intrinsic.
3819 if (!II->hasOneUse())
3820 return nullptr;
3821
3822 // Let Y = [add/sub]_sat(X, C) pred C2
3823 // SatVal = The saturating value for the operation
3824 // WillWrap = Whether or not the operation will underflow / overflow
3825 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
3826 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
3827 //
3828 // When (SatVal pred C2) is true, then
3829 // Y = WillWrap ? true : ((X binop C) pred C2)
3830 // => Y = WillWrap || ((X binop C) pred C2)
3831 // else
3832 // Y = WillWrap ? false : ((X binop C) pred C2)
3833 // => Y = !WillWrap ? ((X binop C) pred C2) : false
3834 // => Y = !WillWrap && ((X binop C) pred C2)
3835 Value *Op0 = II->getOperand(0);
3836 Value *Op1 = II->getOperand(1);
3837
3838 const APInt *COp1;
3839 // This transform only works when the intrinsic has an integral constant or
3840 // splat vector as the second operand.
3841 if (!match(Op1, m_APInt(COp1)))
3842 return nullptr;
3843
3844 APInt SatVal;
3845 switch (II->getIntrinsicID()) {
3846 default:
3848 "This function only works with usub_sat and uadd_sat for now!");
3849 case Intrinsic::uadd_sat:
3850 SatVal = APInt::getAllOnes(C.getBitWidth());
3851 break;
3852 case Intrinsic::usub_sat:
3853 SatVal = APInt::getZero(C.getBitWidth());
3854 break;
3855 }
3856
3857 // Check (SatVal pred C2)
3858 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);
3859
3860 // !WillWrap.
3862 II->getBinaryOp(), *COp1, II->getNoWrapKind());
3863
3864 // WillWrap.
3865 if (SatValCheck)
3866 C1 = C1.inverse();
3867
3869 if (II->getBinaryOp() == Instruction::Add)
3870 C2 = C2.sub(*COp1);
3871 else
3872 C2 = C2.add(*COp1);
3873
3874 Instruction::BinaryOps CombiningOp =
3875 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
3876
3877 std::optional<ConstantRange> Combination;
3878 if (CombiningOp == Instruction::BinaryOps::Or)
3879 Combination = C1.exactUnionWith(C2);
3880 else /* CombiningOp == Instruction::BinaryOps::And */
3881 Combination = C1.exactIntersectWith(C2);
3882
3883 if (!Combination)
3884 return nullptr;
3885
3886 CmpInst::Predicate EquivPred;
3887 APInt EquivInt;
3888 APInt EquivOffset;
3889
3890 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
3891
3892 return new ICmpInst(
3893 EquivPred,
3894 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),
3895 ConstantInt::get(Op1->getType(), EquivInt));
3896}
3897
3898/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
3900 IntrinsicInst *II,
3901 const APInt &C) {
3902 ICmpInst::Predicate Pred = Cmp.getPredicate();
3903
3904 // Handle folds that apply for any kind of icmp.
3905 switch (II->getIntrinsicID()) {
3906 default:
3907 break;
3908 case Intrinsic::uadd_sat:
3909 case Intrinsic::usub_sat:
3910 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
3911 Pred, cast<SaturatingInst>(II), C, Builder))
3912 return Folded;
3913 break;
3914 case Intrinsic::ctpop: {
3915 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3916 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))
3917 return R;
3918 } break;
3919 }
3920
3921 if (Cmp.isEquality())
3922 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
3923
3924 Type *Ty = II->getType();
3925 unsigned BitWidth = C.getBitWidth();
3926 switch (II->getIntrinsicID()) {
3927 case Intrinsic::ctpop: {
3928 // (ctpop X > BitWidth - 1) --> X == -1
3929 Value *X = II->getArgOperand(0);
3930 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
3931 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,
3933 // (ctpop X < BitWidth) --> X != -1
3934 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
3935 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,
3937 break;
3938 }
3939 case Intrinsic::ctlz: {
3940 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
3941 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
3942 unsigned Num = C.getLimitedValue();
3943 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);
3944 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,
3945 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
3946 }
3947
3948 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
3949 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
3950 unsigned Num = C.getLimitedValue();
3952 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,
3953 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
3954 }
3955 break;
3956 }
3957 case Intrinsic::cttz: {
3958 // Limit to one use to ensure we don't increase instruction count.
3959 if (!II->hasOneUse())
3960 return nullptr;
3961
3962 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
3963 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
3964 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);
3965 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,
3966 Builder.CreateAnd(II->getArgOperand(0), Mask),
3968 }
3969
3970 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
3971 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
3972 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());
3973 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,
3974 Builder.CreateAnd(II->getArgOperand(0), Mask),
3976 }
3977 break;
3978 }
3979 case Intrinsic::ssub_sat:
3980 // ssub.sat(a, b) spred 0 -> a spred b
3981 if (ICmpInst::isSigned(Pred)) {
3982 if (C.isZero())
3983 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3984 // X s<= 0 is cannonicalized to X s< 1
3985 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3986 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),
3987 II->getArgOperand(1));
3988 // X s>= 0 is cannonicalized to X s> -1
3989 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
3990 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),
3991 II->getArgOperand(1));
3992 }
3993 break;
3994 default:
3995 break;
3996 }
3997
3998 return nullptr;
3999}
4000
4001/// Handle icmp with constant (but not simple integer constant) RHS.
4003 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4004 Constant *RHSC = dyn_cast<Constant>(Op1);
4005 Instruction *LHSI = dyn_cast<Instruction>(Op0);
4006 if (!RHSC || !LHSI)
4007 return nullptr;
4008
4009 switch (LHSI->getOpcode()) {
4010 case Instruction::PHI:
4011 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
4012 return NV;
4013 break;
4014 case Instruction::IntToPtr:
4015 // icmp pred inttoptr(X), null -> icmp pred X, 0
4016 if (RHSC->isNullValue() &&
4017 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
4018 return new ICmpInst(
4019 I.getPredicate(), LHSI->getOperand(0),
4021 break;
4022
4023 case Instruction::Load:
4024 // Try to optimize things like "A[i] > 4" to index computations.
4025 if (GetElementPtrInst *GEP =
4026 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
4027 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
4028 if (Instruction *Res =
4029 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, GV, I))
4030 return Res;
4031 break;
4032 }
4033
4034 return nullptr;
4035}
4036
4038 SelectInst *SI, Value *RHS,
4039 const ICmpInst &I) {
4040 // Try to fold the comparison into the select arms, which will cause the
4041 // select to be converted into a logical and/or.
4042 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4043 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))
4044 return Res;
4045 if (std::optional<bool> Impl = isImpliedCondition(
4046 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))
4047 return ConstantInt::get(I.getType(), *Impl);
4048 return nullptr;
4049 };
4050
4051 ConstantInt *CI = nullptr;
4052 Value *Op1 = SimplifyOp(SI->getOperand(1), true);
4053 if (Op1)
4054 CI = dyn_cast<ConstantInt>(Op1);
4055
4056 Value *Op2 = SimplifyOp(SI->getOperand(2), false);
4057 if (Op2)
4058 CI = dyn_cast<ConstantInt>(Op2);
4059
4060 // We only want to perform this transformation if it will not lead to
4061 // additional code. This is true if either both sides of the select
4062 // fold to a constant (in which case the icmp is replaced with a select
4063 // which will usually simplify) or this is the only user of the
4064 // select (in which case we are trading a select+icmp for a simpler
4065 // select+icmp) or all uses of the select can be replaced based on
4066 // dominance information ("Global cases").
4067 bool Transform = false;
4068 if (Op1 && Op2)
4069 Transform = true;
4070 else if (Op1 || Op2) {
4071 // Local case
4072 if (SI->hasOneUse())
4073 Transform = true;
4074 // Global cases
4075 else if (CI && !CI->isZero())
4076 // When Op1 is constant try replacing select with second operand.
4077 // Otherwise Op2 is constant and try replacing select with first
4078 // operand.
4079 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4080 }
4081 if (Transform) {
4082 if (!Op1)
4083 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4084 if (!Op2)
4085 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4086 return SelectInst::Create(SI->getOperand(0), Op1, Op2);
4087 }
4088
4089 return nullptr;
4090}
4091
4092// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4093static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4094 unsigned Depth = 0) {
4095 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4096 return true;
4097 if (V->getType()->getScalarSizeInBits() == 1)
4098 return true;
4100 return false;
4101 Value *X;
4102 const Instruction *I = dyn_cast<Instruction>(V);
4103 if (!I)
4104 return false;
4105 switch (I->getOpcode()) {
4106 case Instruction::ZExt:
4107 // ZExt(Mask) is a Mask.
4108 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4109 case Instruction::SExt:
4110 // SExt(Mask) is a Mask.
4111 // SExt(~Mask) is a ~Mask.
4112 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4113 case Instruction::And:
4114 case Instruction::Or:
4115 // Mask0 | Mask1 is a Mask.
4116 // Mask0 & Mask1 is a Mask.
4117 // ~Mask0 | ~Mask1 is a ~Mask.
4118 // ~Mask0 & ~Mask1 is a ~Mask.
4119 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4120 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4121 case Instruction::Xor:
4122 if (match(V, m_Not(m_Value(X))))
4123 return isMaskOrZero(X, !Not, Q, Depth);
4124
4125 // (X ^ -X) is a ~Mask
4126 if (Not)
4127 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4128 // (X ^ (X - 1)) is a Mask
4129 else
4130 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4131 case Instruction::Select:
4132 // c ? Mask0 : Mask1 is a Mask.
4133 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4134 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4135 case Instruction::Shl:
4136 // (~Mask) << X is a ~Mask.
4137 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4138 case Instruction::LShr:
4139 // Mask >> X is a Mask.
4140 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4141 case Instruction::AShr:
4142 // Mask s>> X is a Mask.
4143 // ~Mask s>> X is a ~Mask.
4144 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4145 case Instruction::Add:
4146 // Pow2 - 1 is a Mask.
4147 if (!Not && match(I->getOperand(1), m_AllOnes()))
4148 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4149 Depth, Q.AC, Q.CxtI, Q.DT);
4150 break;
4151 case Instruction::Sub:
4152 // -Pow2 is a ~Mask.
4153 if (Not && match(I->getOperand(0), m_Zero()))
4154 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4155 Depth, Q.AC, Q.CxtI, Q.DT);
4156 break;
4157 case Instruction::Call: {
4158 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4159 switch (II->getIntrinsicID()) {
4160 // min/max(Mask0, Mask1) is a Mask.
4161 // min/max(~Mask0, ~Mask1) is a ~Mask.
4162 case Intrinsic::umax:
4163 case Intrinsic::smax:
4164 case Intrinsic::umin:
4165 case Intrinsic::smin:
4166 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4167 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4168
4169 // In the context of masks, bitreverse(Mask) == ~Mask
4170 case Intrinsic::bitreverse:
4171 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4172 default:
4173 break;
4174 }
4175 }
4176 break;
4177 }
4178 default:
4179 break;
4180 }
4181 return false;
4182}
4183
4184/// Some comparisons can be simplified.
4185/// In this case, we are looking for comparisons that look like
4186/// a check for a lossy truncation.
4187/// Folds:
4188/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4189/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4190/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4191/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4192/// Where Mask is some pattern that produces all-ones in low bits:
4193/// (-1 >> y)
4194/// ((-1 << y) >> y) <- non-canonical, has extra uses
4195/// ~(-1 << y)
4196/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4197/// The Mask can be a constant, too.
4198/// For some predicates, the operands are commutative.
4199/// For others, x can only be on a specific side.
4201 Value *Op1, const SimplifyQuery &Q,
4202 InstCombiner &IC) {
4203
4204 ICmpInst::Predicate DstPred;
4205 switch (Pred) {
4207 // x & Mask == x
4208 // x & ~Mask == 0
4209 // ~x | Mask == -1
4210 // -> x u<= Mask
4211 // x & ~Mask == ~Mask
4212 // -> ~Mask u<= x
4214 break;
4216 // x & Mask != x
4217 // x & ~Mask != 0
4218 // ~x | Mask != -1
4219 // -> x u> Mask
4220 // x & ~Mask != ~Mask
4221 // -> ~Mask u> x
4223 break;
4225 // x & Mask u< x
4226 // -> x u> Mask
4227 // x & ~Mask u< ~Mask
4228 // -> ~Mask u> x
4230 break;
4232 // x & Mask u>= x
4233 // -> x u<= Mask
4234 // x & ~Mask u>= ~Mask
4235 // -> ~Mask u<= x
4237 break;
4239 // x & Mask s< x [iff Mask s>= 0]
4240 // -> x s> Mask
4241 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4242 // -> ~Mask s> x
4244 break;
4246 // x & Mask s>= x [iff Mask s>= 0]
4247 // -> x s<= Mask
4248 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4249 // -> ~Mask s<= x
4251 break;
4252 default:
4253 // We don't support sgt,sle
4254 // ult/ugt are simplified to true/false respectively.
4255 return nullptr;
4256 }
4257
4258 Value *X, *M;
4259 // Put search code in lambda for early positive returns.
4260 auto IsLowBitMask = [&]() {
4261 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4262 X = Op1;
4263 // Look for: x & Mask pred x
4264 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4265 return !ICmpInst::isSigned(Pred) ||
4266 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4267 }
4268
4269 // Look for: x & ~Mask pred ~Mask
4270 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4271 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4272 }
4273 return false;
4274 }
4275 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4276 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4277
4278 auto Check = [&]() {
4279 // Look for: ~x | Mask == -1
4280 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4281 if (Value *NotX =
4282 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4283 X = NotX;
4284 return true;
4285 }
4286 }
4287 return false;
4288 };
4289 if (Check())
4290 return true;
4291 std::swap(X, M);
4292 return Check();
4293 }
4294 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4295 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4296 auto Check = [&]() {
4297 // Look for: x & ~Mask == 0
4298 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4299 if (Value *NotM =
4300 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4301 M = NotM;
4302 return true;
4303 }
4304 }
4305 return false;
4306 };
4307 if (Check())
4308 return true;
4309 std::swap(X, M);
4310 return Check();
4311 }
4312 return false;
4313 };
4314
4315 if (!IsLowBitMask())
4316 return nullptr;
4317
4318 return IC.Builder.CreateICmp(DstPred, X, M);
4319}
4320
4321/// Some comparisons can be simplified.
4322/// In this case, we are looking for comparisons that look like
4323/// a check for a lossy signed truncation.
4324/// Folds: (MaskedBits is a constant.)
4325/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4326/// Into:
4327/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4328/// Where KeptBits = bitwidth(%x) - MaskedBits
4329static Value *
4331 InstCombiner::BuilderTy &Builder) {
4332 ICmpInst::Predicate SrcPred;
4333 Value *X;
4334 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4335 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4336 if (!match(&I, m_c_ICmp(SrcPred,
4338 m_APInt(C1))),
4339 m_Deferred(X))))
4340 return nullptr;
4341
4342 // Potential handling of non-splats: for each element:
4343 // * if both are undef, replace with constant 0.
4344 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4345 // * if both are not undef, and are different, bailout.
4346 // * else, only one is undef, then pick the non-undef one.
4347
4348 // The shift amount must be equal.
4349 if (*C0 != *C1)
4350 return nullptr;
4351 const APInt &MaskedBits = *C0;
4352 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4353
4354 ICmpInst::Predicate DstPred;
4355 switch (SrcPred) {
4357 // ((%x << MaskedBits) a>> MaskedBits) == %x
4358 // =>
4359 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4361 break;
4363 // ((%x << MaskedBits) a>> MaskedBits) != %x
4364 // =>
4365 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4367 break;
4368 // FIXME: are more folds possible?
4369 default:
4370 return nullptr;
4371 }
4372
4373 auto *XType = X->getType();
4374 const unsigned XBitWidth = XType->getScalarSizeInBits();
4375 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4376 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4377
4378 // KeptBits = bitwidth(%x) - MaskedBits
4379 const APInt KeptBits = BitWidth - MaskedBits;
4380 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4381 // ICmpCst = (1 << KeptBits)
4382 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4383 assert(ICmpCst.isPowerOf2());
4384 // AddCst = (1 << (KeptBits-1))
4385 const APInt AddCst = ICmpCst.lshr(1);
4386 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4387
4388 // T0 = add %x, AddCst
4389 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4390 // T1 = T0 DstPred ICmpCst
4391 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4392
4393 return T1;
4394}
4395
4396// Given pattern:
4397// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4398// we should move shifts to the same hand of 'and', i.e. rewrite as
4399// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4400// We are only interested in opposite logical shifts here.
4401// One of the shifts can be truncated.
4402// If we can, we want to end up creating 'lshr' shift.
4403static Value *
4405 InstCombiner::BuilderTy &Builder) {
4406 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4407 !I.getOperand(0)->hasOneUse())
4408 return nullptr;
4409
4410 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4411
4412 // Look for an 'and' of two logical shifts, one of which may be truncated.
4413 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4414 Instruction *XShift, *MaybeTruncation, *YShift;
4415 if (!match(
4416 I.getOperand(0),
4417 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4419 m_AnyLogicalShift, m_Instruction(YShift))),
4420 m_Instruction(MaybeTruncation)))))
4421 return nullptr;
4422
4423 // We potentially looked past 'trunc', but only when matching YShift,
4424 // therefore YShift must have the widest type.
4425 Instruction *WidestShift = YShift;
4426 // Therefore XShift must have the shallowest type.
4427 // Or they both have identical types if there was no truncation.
4428 Instruction *NarrowestShift = XShift;
4429
4430 Type *WidestTy = WidestShift->getType();
4431 Type *NarrowestTy = NarrowestShift->getType();
4432 assert(NarrowestTy == I.getOperand(0)->getType() &&
4433 "We did not look past any shifts while matching XShift though.");
4434 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4435
4436 // If YShift is a 'lshr', swap the shifts around.
4437 if (match(YShift, m_LShr(m_Value(), m_Value())))
4438 std::swap(XShift, YShift);
4439
4440 // The shifts must be in opposite directions.
4441 auto XShiftOpcode = XShift->getOpcode();
4442 if (XShiftOpcode == YShift->getOpcode())
4443 return nullptr; // Do not care about same-direction shifts here.
4444
4445 Value *X, *XShAmt, *Y, *YShAmt;
4446 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4447 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4448
4449 // If one of the values being shifted is a constant, then we will end with
4450 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4451 // however, we will need to ensure that we won't increase instruction count.
4452 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4453 // At least one of the hands of the 'and' should be one-use shift.
4454 if (!match(I.getOperand(0),
4455 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4456 return nullptr;
4457 if (HadTrunc) {
4458 // Due to the 'trunc', we will need to widen X. For that either the old
4459 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4460 if (!MaybeTruncation->hasOneUse() &&
4461 !NarrowestShift->getOperand(1)->hasOneUse())
4462 return nullptr;
4463 }
4464 }
4465
4466 // We have two shift amounts from two different shifts. The types of those
4467 // shift amounts may not match. If that's the case let's bailout now.
4468 if (XShAmt->getType() != YShAmt->getType())
4469 return nullptr;
4470
4471 // As input, we have the following pattern:
4472 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4473 // We want to rewrite that as:
4474 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4475 // While we know that originally (Q+K) would not overflow
4476 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4477 // shift amounts. so it may now overflow in smaller bitwidth.
4478 // To ensure that does not happen, we need to ensure that the total maximal
4479 // shift amount is still representable in that smaller bit width.
4480 unsigned MaximalPossibleTotalShiftAmount =
4481 (WidestTy->getScalarSizeInBits() - 1) +
4482 (NarrowestTy->getScalarSizeInBits() - 1);
4483 APInt MaximalRepresentableShiftAmount =
4485 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4486 return nullptr;
4487
4488 // Can we fold (XShAmt+YShAmt) ?
4489 auto *NewShAmt = dyn_cast_or_null<Constant>(
4490 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4491 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4492 if (!NewShAmt)
4493 return nullptr;
4494 if (NewShAmt->getType() != WidestTy) {
4495 NewShAmt =
4496 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4497 if (!NewShAmt)
4498 return nullptr;
4499 }
4500 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4501
4502 // Is the new shift amount smaller than the bit width?
4503 // FIXME: could also rely on ConstantRange.
4504 if (!match(NewShAmt,
4506 APInt(WidestBitWidth, WidestBitWidth))))
4507 return nullptr;
4508
4509 // An extra legality check is needed if we had trunc-of-lshr.
4510 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4511 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4512 WidestShift]() {
4513 // It isn't obvious whether it's worth it to analyze non-constants here.
4514 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4515 // If *any* of these preconditions matches we can perform the fold.
4516 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4517 ? NewShAmt->getSplatValue()
4518 : NewShAmt;
4519 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4520 if (NewShAmtSplat &&
4521 (NewShAmtSplat->isNullValue() ||
4522 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4523 return true;
4524 // We consider *min* leading zeros so a single outlier
4525 // blocks the transform as opposed to allowing it.
4526 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4527 KnownBits Known = computeKnownBits(C, SQ.DL);
4528 unsigned MinLeadZero = Known.countMinLeadingZeros();
4529 // If the value being shifted has at most lowest bit set we can fold.
4530 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4531 if (MaxActiveBits <= 1)
4532 return true;
4533 // Precondition: NewShAmt u<= countLeadingZeros(C)
4534 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4535 return true;
4536 }
4537 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4538 KnownBits Known = computeKnownBits(C, SQ.DL);
4539 unsigned MinLeadZero = Known.countMinLeadingZeros();
4540 // If the value being shifted has at most lowest bit set we can fold.
4541 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4542 if (MaxActiveBits <= 1)
4543 return true;
4544 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4545 if (NewShAmtSplat) {
4546 APInt AdjNewShAmt =
4547 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4548 if (AdjNewShAmt.ule(MinLeadZero))
4549 return true;
4550 }
4551 }
4552 return false; // Can't tell if it's ok.
4553 };
4554 if (!CanFold())
4555 return nullptr;
4556 }
4557
4558 // All good, we can do this fold.
4559 X = Builder.CreateZExt(X, WidestTy);
4560 Y = Builder.CreateZExt(Y, WidestTy);
4561 // The shift is the same that was for X.
4562 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4563 ? Builder.CreateLShr(X, NewShAmt)
4564 : Builder.CreateShl(X, NewShAmt);
4565 Value *T1 = Builder.CreateAnd(T0, Y);
4566 return Builder.CreateICmp(I.getPredicate(), T1,
4567 Constant::getNullValue(WidestTy));
4568}
4569
4570/// Fold
4571/// (-1 u/ x) u< y
4572/// ((x * y) ?/ x) != y
4573/// to
4574/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4575/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4576/// will mean that we are looking for the opposite answer.
4579 Value *X, *Y;
4581 Instruction *Div;
4582 bool NeedNegation;
4583 // Look for: (-1 u/ x) u</u>= y
4584 if (!I.isEquality() &&
4585 match(&I, m_c_ICmp(Pred,
4587 m_Instruction(Div)),
4588 m_Value(Y)))) {
4589 Mul = nullptr;
4590
4591 // Are we checking that overflow does not happen, or does happen?
4592 switch (Pred) {
4594 NeedNegation = false;
4595 break; // OK
4597 NeedNegation = true;
4598 break; // OK
4599 default:
4600 return nullptr; // Wrong predicate.
4601 }
4602 } else // Look for: ((x * y) / x) !=/== y
4603 if (I.isEquality() &&
4604 match(&I,
4605 m_c_ICmp(Pred, m_Value(Y),
4608 m_Value(X)),
4610 m_Deferred(X))),
4611 m_Instruction(Div))))) {
4612 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4613 } else
4614 return nullptr;
4615
4617 // If the pattern included (x * y), we'll want to insert new instructions
4618 // right before that original multiplication so that we can replace it.
4619 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4620 if (MulHadOtherUses)
4622
4623 Function *F = Intrinsic::getDeclaration(I.getModule(),
4624 Div->getOpcode() == Instruction::UDiv
4625 ? Intrinsic::umul_with_overflow
4626 : Intrinsic::smul_with_overflow,
4627 X->getType());
4628 CallInst *Call = Builder.CreateCall(F, {X, Y}, "mul");
4629
4630 // If the multiplication was used elsewhere, to ensure that we don't leave
4631 // "duplicate" instructions, replace uses of that original multiplication
4632 // with the multiplication result from the with.overflow intrinsic.
4633 if (MulHadOtherUses)
4634 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4635
4636 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4637 if (NeedNegation) // This technically increases instruction count.
4638 Res = Builder.CreateNot(Res, "mul.not.ov");
4639
4640 // If we replaced the mul, erase it. Do this after all uses of Builder,
4641 // as the mul is used as insertion point.
4642 if (MulHadOtherUses)
4644
4645 return Res;
4646}
4647
4649 InstCombiner::BuilderTy &Builder) {
4650 CmpInst::Predicate Pred;
4651 Value *X;
4652 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4653
4654 if (ICmpInst::isSigned(Pred))
4655 Pred = ICmpInst::getSwappedPredicate(Pred);
4656 else if (ICmpInst::isUnsigned(Pred))
4657 Pred = ICmpInst::getSignedPredicate(Pred);
4658 // else for equality-comparisons just keep the predicate.
4659
4660 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4661 Constant::getNullValue(X->getType()), I.getName());
4662 }
4663
4664 // A value is not equal to its negation unless that value is 0 or
4665 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4666 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
4667 ICmpInst::isEquality(Pred)) {
4668 Type *Ty = X->getType();
4670 Constant *MaxSignedVal =
4671 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
4672 Value *And = Builder.CreateAnd(X, MaxSignedVal);
4673 Constant *Zero = Constant::getNullValue(Ty);
4674 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
4675 }
4676
4677 return nullptr;
4678}
4679
4681 InstCombinerImpl &IC) {
4682 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4683 // Normalize and operand as operand 0.
4684 CmpInst::Predicate Pred = I.getPredicate();
4685 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
4686 std::swap(Op0, Op1);
4687 Pred = ICmpInst::getSwappedPredicate(Pred);
4688 }
4689
4690 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
4691 return nullptr;
4692
4693 // (icmp (X & Y) u< X --> (X & Y) != X
4694 if (Pred == ICmpInst::ICMP_ULT)
4695 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4696
4697 // (icmp (X & Y) u>= X --> (X & Y) == X
4698 if (Pred == ICmpInst::ICMP_UGE)
4699 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4700
4701 return nullptr;
4702}
4703
4705 InstCombinerImpl &IC) {
4706 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4707
4708 // Normalize or operand as operand 0.
4709 CmpInst::Predicate Pred = I.getPredicate();
4710 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
4711 std::swap(Op0, Op1);
4712 Pred = ICmpInst::getSwappedPredicate(Pred);
4713 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
4714 return nullptr;
4715 }
4716
4717 // icmp (X | Y) u<= X --> (X | Y) == X
4718 if (Pred == ICmpInst::ICMP_ULE)
4719 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4720
4721 // icmp (X | Y) u> X --> (X | Y) != X
4722 if (Pred == ICmpInst::ICMP_UGT)
4723 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4724
4725 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4726 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
4727 if (Value *NotOp1 =
4728 IC.getFreelyInverted(Op1, Op1->hasOneUse(), &IC.Builder))
4729 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
4730 Constant::getNullValue(Op1->getType()));
4731 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
4732 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4733 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
4734 Constant::getAllOnesValue(Op1->getType()));
4735 }
4736 return nullptr;
4737}
4738
4740 InstCombinerImpl &IC) {
4741 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4742 // Normalize xor operand as operand 0.
4743 CmpInst::Predicate Pred = I.getPredicate();
4744 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
4745 std::swap(Op0, Op1);
4746 Pred = ICmpInst::getSwappedPredicate(Pred);
4747 }
4748 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
4749 return nullptr;
4750
4751 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
4752 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
4753 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
4754 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
4756 if (PredOut != Pred && isKnownNonZero(A, Q))
4757 return new ICmpInst(PredOut, Op0, Op1);
4758
4759 return nullptr;
4760}
4761
4762/// Try to fold icmp (binop), X or icmp X, (binop).
4763/// TODO: A large part of this logic is duplicated in InstSimplify's
4764/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
4765/// duplication.
4767 const SimplifyQuery &SQ) {
4769 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4770
4771 // Special logic for binary operators.
4772 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
4773 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
4774 if (!BO0 && !BO1)
4775 return nullptr;
4776
4777 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
4778 return NewICmp;
4779
4780 const CmpInst::Predicate Pred = I.getPredicate();
4781 Value *X;
4782
4783 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
4784 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
4785 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
4786 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
4787 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
4788 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
4789 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
4790 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
4791 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
4792
4793 {
4794 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
4795 Constant *C;
4796 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
4797 m_ImmConstant(C)))) &&
4798 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
4800 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
4801 }
4802 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
4803 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
4804 m_ImmConstant(C)))) &&
4805 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
4807 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
4808 }
4809 }
4810
4811 {
4812 // Similar to above: an unsigned overflow comparison may use offset + mask:
4813 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
4814 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
4815 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
4816 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
4817 BinaryOperator *BO;
4818 const APInt *C;
4819 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
4820 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
4822 CmpInst::Predicate NewPred =
4824 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
4825 return new ICmpInst(NewPred, Op1, Zero);
4826 }
4827
4828 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
4829 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
4831 CmpInst::Predicate NewPred =
4833 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
4834 return new ICmpInst(NewPred, Op0, Zero);
4835 }
4836 }
4837
4838 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
4839 bool Op0HasNUW = false, Op1HasNUW = false;
4840 bool Op0HasNSW = false, Op1HasNSW = false;
4841 // Analyze the case when either Op0 or Op1 is an add instruction.
4842 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
4843 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
4844 bool &HasNSW, bool &HasNUW) -> bool {
4845 if (isa<OverflowingBinaryOperator>(BO)) {
4846 HasNUW = BO.hasNoUnsignedWrap();
4847 HasNSW = BO.hasNoSignedWrap();
4848 return ICmpInst::isEquality(Pred) ||
4849 (CmpInst::isUnsigned(Pred) && HasNUW) ||
4850 (CmpInst::isSigned(Pred) && HasNSW);
4851 } else if (BO.getOpcode() == Instruction::Or) {
4852 HasNUW = true;
4853 HasNSW = true;
4854 return true;
4855 } else {
4856 return false;
4857 }
4858 };
4859 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
4860
4861 if (BO0) {
4862 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
4863 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
4864 }
4865 if (BO1) {
4866 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
4867 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
4868 }
4869
4870 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
4871 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
4872 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
4873 return new ICmpInst(Pred, A == Op1 ? B : A,
4874 Constant::getNullValue(Op1->getType()));
4875
4876 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
4877 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
4878 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
4879 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
4880 C == Op0 ? D : C);
4881
4882 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
4883 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
4884 NoOp1WrapProblem) {
4885 // Determine Y and Z in the form icmp (X+Y), (X+Z).
4886 Value *Y, *Z;
4887 if (A == C) {
4888 // C + B == C + D -> B == D
4889 Y = B;
4890 Z = D;
4891 } else if (A == D) {
4892 // D + B == C + D -> B == C
4893 Y = B;
4894 Z = C;
4895 } else if (B == C) {
4896 // A + C == C + D -> A == D
4897 Y = A;
4898 Z = D;
4899 } else {
4900 assert(B == D);
4901 // A + D == C + D -> A == C
4902 Y = A;
4903 Z = C;
4904 }
4905 return new ICmpInst(Pred, Y, Z);
4906 }
4907
4908 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
4909 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
4910 match(B, m_AllOnes()))
4911 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
4912
4913 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
4914 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
4915 match(B, m_AllOnes()))
4916 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
4917
4918 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
4919 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
4920 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
4921
4922 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
4923 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
4924 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
4925
4926 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
4927 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
4928 match(D, m_AllOnes()))
4929 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
4930
4931 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
4932 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
4933 match(D, m_AllOnes()))
4934 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
4935
4936 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
4937 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
4938 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
4939
4940 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
4941 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
4942 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
4943
4944 // TODO: The subtraction-related identities shown below also hold, but
4945 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
4946 // wouldn't happen even if they were implemented.
4947 //
4948 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
4949 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
4950 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
4951 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
4952
4953 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
4954 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One()))
4955 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1);
4956
4957 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
4958 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One()))
4959 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1);
4960
4961 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
4962 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One()))
4963 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C);
4964
4965 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
4966 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One()))
4967 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C);
4968
4969 // if C1 has greater magnitude than C2:
4970 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
4971 // s.t. C3 = C1 - C2
4972 //
4973 // if C2 has greater magnitude than C1:
4974 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
4975 // s.t. C3 = C2 - C1
4976 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
4977 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
4978 const APInt *AP1, *AP2;
4979 // TODO: Support non-uniform vectors.
4980 // TODO: Allow poison passthrough if B or D's element is poison.
4981 if (match(B, m_APIntAllowPoison(AP1)) &&
4982 match(D, m_APIntAllowPoison(AP2)) &&
4983 AP1->isNegative() == AP2->isNegative()) {
4984 APInt AP1Abs = AP1->abs();
4985 APInt AP2Abs = AP2->abs();
4986 if (AP1Abs.uge(AP2Abs)) {
4987 APInt Diff = *AP1 - *AP2;
4988 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
4989 Value *NewAdd = Builder.CreateAdd(
4990 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
4991 return new ICmpInst(Pred, NewAdd, C);
4992 } else {
4993 APInt Diff = *AP2 - *AP1;
4994 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
4995 Value *NewAdd = Builder.CreateAdd(
4996 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
4997 return new ICmpInst(Pred, A, NewAdd);
4998 }
4999 }
5000 Constant *Cst1, *Cst2;
5001 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
5002 ICmpInst::isEquality(Pred)) {
5003 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
5004 Value *NewAdd = Builder.CreateAdd(C, Diff);
5005 return new ICmpInst(Pred, A, NewAdd);
5006 }
5007 }
5008
5009 // Analyze the case when either Op0 or Op1 is a sub instruction.
5010 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5011 A = nullptr;
5012 B = nullptr;
5013 C = nullptr;
5014 D = nullptr;
5015 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5016 A = BO0->getOperand(0);
5017 B = BO0->getOperand(1);
5018 }
5019 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5020 C = BO1->getOperand(0);
5021 D = BO1->getOperand(1);
5022 }
5023
5024 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5025 if (A == Op1 && NoOp0WrapProblem)
5026 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5027 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5028 if (C == Op0 && NoOp1WrapProblem)
5029 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5030
5031 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5032 // (A - B) u>/u<= A --> B u>/u<= A
5033 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5034 return new ICmpInst(Pred, B, A);
5035 // C u</u>= (C - D) --> C u</u>= D
5036 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5037 return new ICmpInst(Pred, C, D);
5038 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5039 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5040 isKnownNonZero(B, Q))
5042 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5043 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5044 isKnownNonZero(D, Q))
5046
5047 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5048 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5049 return new ICmpInst(Pred, A, C);
5050
5051 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5052 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5053 return new ICmpInst(Pred, D, B);
5054
5055 // icmp (0-X) < cst --> x > -cst
5056 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5057 Value *X;
5058 if (match(BO0, m_Neg(m_Value(X))))
5059 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5060 if (RHSC->isNotMinSignedValue())
5061 return new ICmpInst(I.getSwappedPredicate(), X,
5062 ConstantExpr::getNeg(RHSC));
5063 }
5064
5065 if (Instruction * R = foldICmpXorXX(I, Q, *this))
5066 return R;
5067 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5068 return R;
5069
5070 {
5071 // Try to remove shared multiplier from comparison:
5072 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z
5073 Value *X, *Y, *Z;
5074 if (Pred == ICmpInst::getUnsignedPredicate(Pred) &&
5075 ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5076 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5077 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5078 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))))) {
5079 bool NonZero;
5080 if (ICmpInst::isEquality(Pred)) {
5081 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5082 // if Z % 2 != 0
5083 // X * Z eq/ne Y * Z -> X eq/ne Y
5084 if (ZKnown.countMaxTrailingZeros() == 0)
5085 return new ICmpInst(Pred, X, Y);
5086 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5087 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5088 // X * Z eq/ne Y * Z -> X eq/ne Y
5089 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5090 return new ICmpInst(Pred, X, Y);
5091 } else
5092 NonZero = isKnownNonZero(Z, Q);
5093
5094 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5095 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5096 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5097 return new ICmpInst(Pred, X, Y);
5098 }
5099 }
5100
5101 BinaryOperator *SRem = nullptr;
5102 // icmp (srem X, Y), Y
5103 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5104 SRem = BO0;
5105 // icmp Y, (srem X, Y)
5106 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5107 Op0 == BO1->getOperand(1))
5108 SRem = BO1;
5109 if (SRem) {
5110 // We don't check hasOneUse to avoid increasing register pressure because
5111 // the value we use is the same value this instruction was already using.
5112 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5113 default:
5114 break;
5115 case ICmpInst::ICMP_EQ:
5116 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5117 case ICmpInst::ICMP_NE:
5118 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5119 case ICmpInst::ICMP_SGT:
5120 case ICmpInst::ICMP_SGE:
5121 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5123 case ICmpInst::ICMP_SLT:
5124 case ICmpInst::ICMP_SLE:
5125 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5127 }
5128 }
5129
5130 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5131 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5132 BO0->getOperand(1) == BO1->getOperand(1)) {
5133 switch (BO0->getOpcode()) {
5134 default:
5135 break;
5136 case Instruction::Add:
5137 case Instruction::Sub:
5138 case Instruction::Xor: {
5139 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5140 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5141
5142 const APInt *C;
5143 if (match(BO0->getOperand(1), m_APInt(C))) {
5144 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5145 if (C->isSignMask()) {
5146 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5147 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5148 }
5149
5150 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5151 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5152 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5153 NewPred = I.getSwappedPredicate(NewPred);
5154 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5155 }
5156 }
5157 break;
5158 }
5159 case Instruction::Mul: {
5160 if (!I.isEquality())
5161 break;
5162
5163 const APInt *C;
5164 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5165 !C->isOne()) {
5166 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5167 // Mask = -1 >> count-trailing-zeros(C).
5168 if (unsigned TZs = C->countr_zero()) {
5169 Constant *Mask = ConstantInt::get(
5170 BO0->getType(),
5171 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5172 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5173 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5174 return new ICmpInst(Pred, And1, And2);
5175 }
5176 }
5177 break;
5178 }
5179 case Instruction::UDiv:
5180 case Instruction::LShr:
5181 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5182 break;
5183 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5184
5185 case Instruction::SDiv:
5186 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5187 !BO0->isExact() || !BO1->isExact())
5188 break;
5189 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5190
5191 case Instruction::AShr:
5192 if (!BO0->isExact() || !BO1->isExact())
5193 break;
5194 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5195
5196 case Instruction::Shl: {
5197 bool NUW = Op0HasNUW && Op1HasNUW;
5198 bool NSW = Op0HasNSW && Op1HasNSW;
5199 if (!NUW && !NSW)
5200 break;
5201 if (!NSW && I.isSigned())
5202 break;
5203 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5204 }
5205 }
5206 }
5207
5208 if (BO0) {
5209 // Transform A & (L - 1) `ult` L --> L != 0
5210 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5211 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5212
5213 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5214 auto *Zero = Constant::getNullValue(BO0->getType());
5215 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5216 }
5217 }
5218
5219 // For unsigned predicates / eq / ne:
5220 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5221 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5222 if (!ICmpInst::isSigned(Pred)) {
5223 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5224 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5225 Constant::getNullValue(Op1->getType()));
5226 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5227 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5228 Constant::getNullValue(Op0->getType()), Op0);
5229 }
5230
5232 return replaceInstUsesWith(I, V);
5233
5234 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5235 return R;
5236
5238 return replaceInstUsesWith(I, V);
5239
5241 return replaceInstUsesWith(I, V);
5242
5243 return nullptr;
5244}
5245
5246/// Fold icmp Pred min|max(X, Y), Z.
5249 Value *Z,
5250 ICmpInst::Predicate Pred) {
5251 Value *X = MinMax->getLHS();
5252 Value *Y = MinMax->getRHS();
5253 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5254 return nullptr;
5255 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5256 // Revert the transform signed pred -> unsigned pred
5257 // TODO: We can flip the signedness of predicate if both operands of icmp
5258 // are negative.
5262 } else
5263 return nullptr;
5264 }
5266 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5267 if (!Val)
5268 return std::nullopt;
5269 if (match(Val, m_One()))
5270 return true;
5271 if (match(Val, m_Zero()))
5272 return false;
5273 return std::nullopt;
5274 };
5275 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5276 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5277 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5278 return nullptr;
5279 if (!CmpXZ.has_value()) {
5280 std::swap(X, Y);
5281 std::swap(CmpXZ, CmpYZ);
5282 }
5283
5284 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5285 if (CmpYZ.has_value())
5286 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5287 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5288 };
5289
5290 switch (Pred) {
5291 case ICmpInst::ICMP_EQ:
5292 case ICmpInst::ICMP_NE: {
5293 // If X == Z:
5294 // Expr Result
5295 // min(X, Y) == Z X <= Y
5296 // max(X, Y) == Z X >= Y
5297 // min(X, Y) != Z X > Y
5298 // max(X, Y) != Z X < Y
5299 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5300 ICmpInst::Predicate NewPred =
5301 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5302 if (Pred == ICmpInst::ICMP_NE)
5303 NewPred = ICmpInst::getInversePredicate(NewPred);
5304 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5305 }
5306 // Otherwise (X != Z):
5307 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5308 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5309 if (!MinMaxCmpXZ.has_value()) {
5310 std::swap(X, Y);
5311 std::swap(CmpXZ, CmpYZ);
5312 // Re-check pre-condition X != Z
5313 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5314 break;
5315 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5316 }
5317 if (!MinMaxCmpXZ.has_value())
5318 break;
5319 if (*MinMaxCmpXZ) {
5320 // Expr Fact Result
5321 // min(X, Y) == Z X < Z false
5322 // max(X, Y) == Z X > Z false
5323 // min(X, Y) != Z X < Z true
5324 // max(X, Y) != Z X > Z true
5325 return replaceInstUsesWith(
5326 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5327 } else {
5328 // Expr Fact Result
5329 // min(X, Y) == Z X > Z Y == Z
5330 // max(X, Y) == Z X < Z Y == Z
5331 // min(X, Y) != Z X > Z Y != Z
5332 // max(X, Y) != Z X < Z Y != Z
5333 return FoldIntoCmpYZ();
5334 }
5335 break;
5336 }
5337 case ICmpInst::ICMP_SLT:
5338 case ICmpInst::ICMP_ULT:
5339 case ICmpInst::ICMP_SLE:
5340 case ICmpInst::ICMP_ULE:
5341 case ICmpInst::ICMP_SGT:
5342 case ICmpInst::ICMP_UGT:
5343 case ICmpInst::ICMP_SGE:
5344 case ICmpInst::ICMP_UGE: {
5345 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5346 if (*CmpXZ) {
5347 if (IsSame) {
5348 // Expr Fact Result
5349 // min(X, Y) < Z X < Z true
5350 // min(X, Y) <= Z X <= Z true
5351 // max(X, Y) > Z X > Z true
5352 // max(X, Y) >= Z X >= Z true
5353 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5354 } else {
5355 // Expr Fact Result
5356 // max(X, Y) < Z X < Z Y < Z
5357 // max(X, Y) <= Z X <= Z Y <= Z
5358 // min(X, Y) > Z X > Z Y > Z
5359 // min(X, Y) >= Z X >= Z Y >= Z
5360 return FoldIntoCmpYZ();
5361 }
5362 } else {
5363 if (IsSame) {
5364 // Expr Fact Result
5365 // min(X, Y) < Z X >= Z Y < Z
5366 // min(X, Y) <= Z X > Z Y <= Z
5367 // max(X, Y) > Z X <= Z Y > Z
5368 // max(X, Y) >= Z X < Z Y >= Z
5369 return FoldIntoCmpYZ();
5370 } else {
5371 // Expr Fact Result
5372 // max(X, Y) < Z X >= Z false
5373 // max(X, Y) <= Z X > Z false
5374 // min(X, Y) > Z X <= Z false
5375 // min(X, Y) >= Z X < Z false
5376 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5377 }
5378 }
5379 break;
5380 }
5381 default:
5382 break;
5383 }
5384
5385 return nullptr;
5386}
5387
5388// Canonicalize checking for a power-of-2-or-zero value:
5390 InstCombiner::BuilderTy &Builder) {
5391 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5392 const CmpInst::Predicate Pred = I.getPredicate();
5393 Value *A = nullptr;
5394 bool CheckIs;
5395 if (I.isEquality()) {
5396 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5397 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5398 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5399 m_Deferred(A)))) ||
5400 !match(Op1, m_ZeroInt()))
5401 A = nullptr;
5402
5403 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5404 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5405 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5406 A = Op1;
5407 else if (match(Op1,
5409 A = Op0;
5410
5411 CheckIs = Pred == ICmpInst::ICMP_EQ;
5412 } else if (ICmpInst::isUnsigned(Pred)) {
5413 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5414 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5415
5416 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5418 m_Specific(Op1))))) {
5419 A = Op1;
5420 CheckIs = Pred == ICmpInst::ICMP_UGE;
5421 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5423 m_Specific(Op0))))) {
5424 A = Op0;
5425 CheckIs = Pred == ICmpInst::ICMP_ULE;
5426 }
5427 }
5428
5429 if (A) {
5430 Type *Ty = A->getType();
5431 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5432 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5433 ConstantInt::get(Ty, 2))
5434 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5435 ConstantInt::get(Ty, 1));
5436 }
5437
5438 return nullptr;
5439}
5440
5442 if (!I.isEquality())
5443 return nullptr;
5444
5445 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5446 const CmpInst::Predicate Pred = I.getPredicate();
5447 Value *A, *B, *C, *D;
5448 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5449 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5450 Value *OtherVal = A == Op1 ? B : A;
5451 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5452 }
5453
5454 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
5455 // A^c1 == C^c2 --> A == C^(c1^c2)
5456 ConstantInt *C1, *C2;
5457 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
5458 Op1->hasOneUse()) {
5459 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
5461 return new ICmpInst(Pred, A, Xor);
5462 }
5463
5464 // A^B == A^D -> B == D
5465 if (A == C)
5466 return new ICmpInst(Pred, B, D);
5467 if (A == D)
5468 return new ICmpInst(Pred, B, C);
5469 if (B == C)
5470 return new ICmpInst(Pred, A, D);
5471 if (B == D)
5472 return new ICmpInst(Pred, A, C);
5473 }
5474 }
5475
5476 // canoncalize:
5477 // (icmp eq/ne (and X, C), X)
5478 // -> (icmp eq/ne (and X, ~C), 0)
5479 {
5480 Constant *CMask;
5481 A = nullptr;
5482 if (match(Op0, m_OneUse(m_And(m_Specific(Op1), m_ImmConstant(CMask)))))
5483 A = Op1;
5484 else if (match(Op1, m_OneUse(m_And(m_Specific(Op0), m_ImmConstant(CMask)))))
5485 A = Op0;
5486 if (A)
5487 return new ICmpInst(Pred, Builder.CreateAnd(A, Builder.CreateNot(CMask)),
5488 Constant::getNullValue(A->getType()));
5489 }
5490
5491 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
5492 // A == (A^B) -> B == 0
5493 Value *OtherVal = A == Op0 ? B : A;
5494 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5495 }
5496
5497 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
5498 if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
5499 match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
5500 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
5501
5502 if (A == C) {
5503 X = B;
5504 Y = D;
5505 Z = A;
5506 } else if (A == D) {
5507 X = B;
5508 Y = C;
5509 Z = A;
5510 } else if (B == C) {
5511 X = A;
5512 Y = D;
5513 Z = B;
5514 } else if (B == D) {
5515 X = A;
5516 Y = C;
5517 Z = B;
5518 }
5519
5520 if (X) { // Build (X^Y) & Z
5521 Op1 = Builder.CreateXor(X, Y);
5522 Op1 = Builder.CreateAnd(Op1, Z);
5523 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
5524 }
5525 }
5526
5527 {
5528 // Similar to above, but specialized for constant because invert is needed:
5529 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
5530 Value *X, *Y;
5531 Constant *C;
5532 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
5533 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
5536 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
5537 }
5538 }
5539
5540 if (match(Op1, m_ZExt(m_Value(A))) &&
5541 (Op0->hasOneUse() || Op1->hasOneUse())) {
5542 // (B & (Pow2C-1)) == zext A --> A == trunc B
5543 // (B & (Pow2C-1)) != zext A --> A != trunc B
5544 const APInt *MaskC;
5545 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
5546 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
5547 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
5548 }
5549
5550 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
5551 // For lshr and ashr pairs.
5552 const APInt *AP1, *AP2;
5553 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5554 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
5555 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5556 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
5557 if (AP1 != AP2)
5558 return nullptr;
5559 unsigned TypeBits = AP1->getBitWidth();
5560 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
5561 if (ShAmt < TypeBits && ShAmt != 0) {
5562 ICmpInst::Predicate NewPred =
5564 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5565 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
5566 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
5567 }
5568 }
5569
5570 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
5571 ConstantInt *Cst1;
5572 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
5573 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
5574 unsigned TypeBits = Cst1->getBitWidth();
5575 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
5576 if (ShAmt < TypeBits && ShAmt != 0) {
5577 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5578 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
5580 I.getName() + ".mask");
5581 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
5582 }
5583 }
5584
5585 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
5586 // "icmp (and X, mask), cst"
5587 uint64_t ShAmt = 0;
5588 if (Op0->hasOneUse() &&
5589 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
5590 match(Op1, m_ConstantInt(Cst1)) &&
5591 // Only do this when A has multiple uses. This is most important to do
5592 // when it exposes other optimizations.
5593 !A->hasOneUse()) {
5594 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
5595
5596 if (ShAmt < ASize) {
5597 APInt MaskV =
5599 MaskV <<= ShAmt;
5600
5601 APInt CmpV = Cst1->getValue().zext(ASize);
5602 CmpV <<= ShAmt;
5603
5604 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
5605 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
5606 }
5607 }
5608
5610 return ICmp;
5611
5612 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks the
5613 // top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s INT_MAX",
5614 // which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a few steps
5615 // of instcombine.
5616 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
5617 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
5619 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
5620 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
5622 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
5623 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
5625 Add, ConstantInt::get(A->getType(), C.shl(1)));
5626 }
5627
5628 // Canonicalize:
5629 // Assume B_Pow2 != 0
5630 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
5631 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
5632 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
5633 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, 0, &I))
5634 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
5636
5637 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
5638 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, 0, &I))
5639 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
5640 ConstantInt::getNullValue(Op1->getType()));
5641
5642 // Canonicalize:
5643 // icmp eq/ne X, OneUse(rotate-right(X))
5644 // -> icmp eq/ne X, rotate-left(X)
5645 // We generally try to convert rotate-right -> rotate-left, this just
5646 // canonicalizes another case.
5647 CmpInst::Predicate PredUnused = Pred;
5648 if (match(&I, m_c_ICmp(PredUnused, m_Value(A),
5649 m_OneUse(m_Intrinsic<Intrinsic::fshr>(
5650 m_Deferred(A), m_Deferred(A), m_Value(B))))))
5651 return new ICmpInst(
5652 Pred, A,
5653 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
5654
5655 // Canonicalize:
5656 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
5657 Constant *Cst;
5658 if (match(&I, m_c_ICmp(PredUnused,
5661 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
5662
5663 {
5664 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5665 auto m_Matcher =
5668 m_Sub(m_Value(B), m_Deferred(A)));
5669 std::optional<bool> IsZero = std::nullopt;
5670 if (match(&I, m_c_ICmp(PredUnused, m_OneUse(m_c_And(m_Value(A), m_Matcher)),
5671 m_Deferred(A))))
5672 IsZero = false;
5673 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5674 else if (match(&I,
5675 m_ICmp(PredUnused, m_OneUse(m_c_And(m_Value(A), m_Matcher)),
5676 m_Zero())))
5677 IsZero = true;
5678
5679 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, /*Depth*/ 0, &I))
5680 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5681 // -> (icmp eq/ne (and X, P2), 0)
5682 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5683 // -> (icmp eq/ne (and X, P2), P2)
5684 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
5685 *IsZero ? A
5686 : ConstantInt::getNullValue(A->getType()));
5687 }
5688
5689 return nullptr;
5690}
5691
5693 ICmpInst::Predicate Pred = ICmp.getPredicate();
5694 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
5695
5696 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
5697 // The trunc masks high bits while the compare may effectively mask low bits.
5698 Value *X;
5699 const APInt *C;
5700 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
5701 return nullptr;
5702
5703 // This matches patterns corresponding to tests of the signbit as well as:
5704 // (trunc X) u< C --> (X & -C) == 0 (are all masked-high-bits clear?)
5705 // (trunc X) u> C --> (X & ~C) != 0 (are any masked-high-bits set?)
5706 APInt Mask;
5707 if (decomposeBitTestICmp(Op0, Op1, Pred, X, Mask, true /* WithTrunc */)) {
5708 Value *And = Builder.CreateAnd(X, Mask);
5709 Constant *Zero = ConstantInt::getNullValue(X->getType());
5710 return new ICmpInst(Pred, And, Zero);
5711 }
5712
5713 unsigned SrcBits = X->getType()->getScalarSizeInBits();
5714 if (Pred == ICmpInst::ICMP_ULT && C->isNegatedPowerOf2()) {
5715 // If C is a negative power-of-2 (high-bit mask):
5716 // (trunc X) u< C --> (X & C) != C (are any masked-high-bits clear?)
5717 Constant *MaskC = ConstantInt::get(X->getType(), C->zext(SrcBits));
5718 Value *And = Builder.CreateAnd(X, MaskC);
5719 return new ICmpInst(ICmpInst::ICMP_NE, And, MaskC);
5720 }
5721
5722 if (Pred == ICmpInst::ICMP_UGT && (~*C).isPowerOf2()) {
5723 // If C is not-of-power-of-2 (one clear bit):
5724 // (trunc X) u> C --> (X & (C+1)) == C+1 (are all masked-high-bits set?)
5725 Constant *MaskC = ConstantInt::get(X->getType(), (*C + 1).zext(SrcBits));
5726 Value *And = Builder.CreateAnd(X, MaskC);
5727 return new ICmpInst(ICmpInst::ICMP_EQ, And, MaskC);
5728 }
5729
5730 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
5731 if (II->getIntrinsicID() == Intrinsic::cttz ||
5732 II->getIntrinsicID() == Intrinsic::ctlz) {
5733 unsigned MaxRet = SrcBits;
5734 // If the "is_zero_poison" argument is set, then we know at least
5735 // one bit is set in the input, so the result is always at least one
5736 // less than the full bitwidth of that input.
5737 if (match(II->getArgOperand(1), m_One()))
5738 MaxRet--;
5739
5740 // Make sure the destination is wide enough to hold the largest output of
5741 // the intrinsic.
5742 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
5743 if (Instruction *I =
5744 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
5745 return I;
5746 }
5747 }
5748
5749 return nullptr;
5750}
5751
5753 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
5754 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
5755 Value *X;
5756 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
5757 return nullptr;
5758
5759 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
5760 bool IsSignedCmp = ICmp.isSigned();
5761
5762 // icmp Pred (ext X), (ext Y)
5763 Value *Y;
5764 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
5765 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
5766 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
5767
5768 if (IsZext0 != IsZext1) {
5769 // If X and Y and both i1
5770 // (icmp eq/ne (zext X) (sext Y))
5771 // eq -> (icmp eq (or X, Y), 0)
5772 // ne -> (icmp ne (or X, Y), 0)
5773 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
5774 Y->getType()->isIntOrIntVectorTy(1))
5775 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
5776 Constant::getNullValue(X->getType()));
5777
5778 // If we have mismatched casts and zext has the nneg flag, we can
5779 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
5780
5781 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
5782 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
5783
5784 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
5785 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
5786
5787 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
5788 IsSignedExt = true;
5789 else
5790 return nullptr;
5791 }
5792
5793 // Not an extension from the same type?
5794 Type *XTy = X->getType(), *YTy = Y->getType();
5795 if (XTy != YTy) {
5796 // One of the casts must have one use because we are creating a new cast.
5797 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
5798 return nullptr;
5799 // Extend the narrower operand to the type of the wider operand.
5800 CastInst::CastOps CastOpcode =
5801 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
5802 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
5803 X = Builder.CreateCast(CastOpcode, X, YTy);
5804 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
5805 Y = Builder.CreateCast(CastOpcode, Y, XTy);
5806 else
5807 return nullptr;
5808 }
5809
5810 // (zext X) == (zext Y) --> X == Y
5811 // (sext X) == (sext Y) --> X == Y
5812 if (ICmp.isEquality())
5813 return new ICmpInst(ICmp.getPredicate(), X, Y);
5814
5815 // A signed comparison of sign extended values simplifies into a
5816 // signed comparison.
5817 if (IsSignedCmp && IsSignedExt)
5818 return new ICmpInst(ICmp.getPredicate(), X, Y);
5819
5820 // The other three cases all fold into an unsigned comparison.
5821 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
5822 }
5823
5824 // Below here, we are only folding a compare with constant.
5825 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
5826 if (!C)
5827 return nullptr;
5828
5829 // If a lossless truncate is possible...
5830 Type *SrcTy = CastOp0->getSrcTy();
5831 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
5832 if (Res) {
5833 if (ICmp.isEquality())
5834 return new ICmpInst(ICmp.getPredicate(), X, Res);
5835
5836 // A signed comparison of sign extended values simplifies into a
5837 // signed comparison.
5838 if (IsSignedExt && IsSignedCmp)
5839 return new ICmpInst(ICmp.getPredicate(), X, Res);
5840
5841 // The other three cases all fold into an unsigned comparison.
5842 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
5843 }
5844
5845 // The re-extended constant changed, partly changed (in the case of a vector),
5846 // or could not be determined to be equal (in the case of a constant
5847 // expression), so the constant cannot be represented in the shorter type.
5848 // All the cases that fold to true or false will have already been handled
5849 // by simplifyICmpInst, so only deal with the tricky case.
5850 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
5851 return nullptr;
5852
5853 // Is source op positive?
5854 // icmp ult (sext X), C --> icmp sgt X, -1
5855 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
5857
5858 // Is source op negative?
5859 // icmp ugt (sext X), C --> icmp slt X, 0
5860 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
5862}
5863
5864/// Handle icmp (cast x), (cast or constant).
5866 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
5867 // icmp compares only pointer's value.
5868 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
5869 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
5870 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
5871 if (SimplifiedOp0 || SimplifiedOp1)
5872 return new ICmpInst(ICmp.getPredicate(),
5873 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
5874 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
5875
5876 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
5877 if (!CastOp0)
5878 return nullptr;
5879 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
5880 return nullptr;
5881
5882 Value *Op0Src = CastOp0->getOperand(0);
5883 Type *SrcTy = CastOp0->getSrcTy();
5884 Type *DestTy = CastOp0->getDestTy();
5885
5886 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
5887 // integer type is the same size as the pointer type.
5888 auto CompatibleSizes = [&](Type *SrcTy, Type *DestTy) {
5889 if (isa<VectorType>(SrcTy)) {
5890 SrcTy = cast<VectorType>(SrcTy)->getElementType();
5891 DestTy = cast<VectorType>(DestTy)->getElementType();
5892 }
5893 return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
5894 };
5895 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
5896 CompatibleSizes(SrcTy, DestTy)) {
5897 Value *NewOp1 = nullptr;
5898 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
5899 Value *PtrSrc = PtrToIntOp1->getOperand(0);
5900 if (PtrSrc->getType() == Op0Src->getType())
5901 NewOp1 = PtrToIntOp1->getOperand(0);
5902 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
5903 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
5904 }
5905
5906 if (NewOp1)
5907 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
5908 }
5909
5910 if (Instruction *R = foldICmpWithTrunc(ICmp))
5911 return R;
5912
5913 return foldICmpWithZextOrSext(ICmp);
5914}
5915
5916static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned) {
5917 switch (BinaryOp) {
5918 default:
5919 llvm_unreachable("Unsupported binary op");
5920 case Instruction::Add:
5921 case Instruction::Sub:
5922 return match(RHS, m_Zero());
5923 case Instruction::Mul:
5924 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
5925 match(RHS, m_One());
5926 }
5927}
5928
5931 bool IsSigned, Value *LHS, Value *RHS,
5932 Instruction *CxtI) const {
5933 switch (BinaryOp) {
5934 default:
5935 llvm_unreachable("Unsupported binary op");
5936 case Instruction::Add:
5937 if (IsSigned)
5938 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
5939 else
5940 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
5941 case Instruction::Sub:
5942 if (IsSigned)
5943 return computeOverflowForSignedSub(LHS, RHS, CxtI);
5944 else
5945 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
5946 case Instruction::Mul:
5947 if (IsSigned)
5948 return computeOverflowForSignedMul(LHS, RHS, CxtI);
5949 else
5950 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
5951 }
5952}
5953
5954bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
5955 bool IsSigned, Value *LHS,
5956 Value *RHS, Instruction &OrigI,
5957 Value *&Result,
5958 Constant *&Overflow) {
5959 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
5960 std::swap(LHS, RHS);
5961
5962 // If the overflow check was an add followed by a compare, the insertion point
5963 // may be pointing to the compare. We want to insert the new instructions
5964 // before the add in case there are uses of the add between the add and the
5965 // compare.
5966 Builder.SetInsertPoint(&OrigI);
5967
5968 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
5969 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
5970 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
5971
5972 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
5973 Result = LHS;
5974 Overflow = ConstantInt::getFalse(OverflowTy);
5975 return true;
5976 }
5977
5978 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
5980 return false;
5983 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
5984 Result->takeName(&OrigI);
5985 Overflow = ConstantInt::getTrue(OverflowTy);
5986 return true;
5988 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
5989 Result->takeName(&OrigI);
5990 Overflow = ConstantInt::getFalse(OverflowTy);
5991 if (auto *Inst = dyn_cast<Instruction>(Result)) {
5992 if (IsSigned)
5993 Inst->setHasNoSignedWrap();
5994 else
5995 Inst->setHasNoUnsignedWrap();
5996 }
5997 return true;
5998 }
5999
6000 llvm_unreachable("Unexpected overflow result");
6001}
6002
6003/// Recognize and process idiom involving test for multiplication
6004/// overflow.
6005///
6006/// The caller has matched a pattern of the form:
6007/// I = cmp u (mul(zext A, zext B), V
6008/// The function checks if this is a test for overflow and if so replaces
6009/// multiplication with call to 'mul.with.overflow' intrinsic.
6010///
6011/// \param I Compare instruction.
6012/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6013/// the compare instruction. Must be of integer type.
6014/// \param OtherVal The other argument of compare instruction.
6015/// \returns Instruction which must replace the compare instruction, NULL if no
6016/// replacement required.
6018 const APInt *OtherVal,
6019 InstCombinerImpl &IC) {
6020 // Don't bother doing this transformation for pointers, don't do it for
6021 // vectors.
6022 if (!isa<IntegerType>(MulVal->getType()))
6023 return nullptr;
6024
6025 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6026 if (!MulInstr)
6027 return nullptr;
6028 assert(MulInstr->getOpcode() == Instruction::Mul);
6029
6030 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6031 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6032 assert(LHS->getOpcode() == Instruction::ZExt);
6033 assert(RHS->getOpcode() == Instruction::ZExt);
6034 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6035
6036 // Calculate type and width of the result produced by mul.with.overflow.
6037 Type *TyA = A->getType(), *TyB = B->getType();
6038 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6039 WidthB = TyB->getPrimitiveSizeInBits();
6040 unsigned MulWidth;
6041 Type *MulType;
6042 if (WidthB > WidthA) {
6043 MulWidth = WidthB;
6044 MulType = TyB;
6045 } else {
6046 MulWidth = WidthA;
6047 MulType = TyA;
6048 }
6049
6050 // In order to replace the original mul with a narrower mul.with.overflow,
6051 // all uses must ignore upper bits of the product. The number of used low
6052 // bits must be not greater than the width of mul.with.overflow.
6053 if (MulVal->hasNUsesOrMore(2))
6054 for (User *U : MulVal->users()) {
6055 if (U == &I)
6056 continue;
6057 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6058 // Check if truncation ignores bits above MulWidth.
6059 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6060 if (TruncWidth > MulWidth)
6061 return nullptr;
6062 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6063 // Check if AND ignores bits above MulWidth.
6064 if (BO->getOpcode() != Instruction::And)
6065 return nullptr;
6066 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6067 const APInt &CVal = CI->getValue();
6068 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6069 return nullptr;
6070 } else {
6071 // In this case we could have the operand of the binary operation
6072 // being defined in another block, and performing the replacement
6073 // could break the dominance relation.
6074 return nullptr;
6075 }
6076 } else {
6077 // Other uses prohibit this transformation.
6078 return nullptr;
6079 }
6080 }
6081
6082 // Recognize patterns
6083 switch (I.getPredicate()) {
6084 case ICmpInst::ICMP_UGT: {
6085 // Recognize pattern:
6086 // mulval = mul(zext A, zext B)
6087 // cmp ugt mulval, max
6088 APInt MaxVal = APInt::getMaxValue(MulWidth);
6089 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6090 if (MaxVal.eq(*OtherVal))
6091 break; // Recognized
6092 return nullptr;
6093 }
6094
6095 case ICmpInst::ICMP_ULT: {
6096 // Recognize pattern:
6097 // mulval = mul(zext A, zext B)
6098 // cmp ule mulval, max + 1
6099 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6100 if (MaxVal.eq(*OtherVal))
6101 break; // Recognized
6102 return nullptr;
6103 }
6104
6105 default:
6106 return nullptr;
6107 }
6108
6109 InstCombiner::BuilderTy &Builder = IC.Builder;
6110 Builder.SetInsertPoint(MulInstr);
6111
6112 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6113 Value *MulA = A, *MulB = B;
6114 if (WidthA < MulWidth)
6115 MulA = Builder.CreateZExt(A, MulType);
6116 if (WidthB < MulWidth)
6117 MulB = Builder.CreateZExt(B, MulType);
6119 I.getModule(), Intrinsic::umul_with_overflow, MulType);
6120 CallInst *Call = Builder.CreateCall(F, {MulA, MulB}, "umul");
6121 IC.addToWorklist(MulInstr);
6122
6123 // If there are uses of mul result other than the comparison, we know that
6124 // they are truncation or binary AND. Change them to use result of
6125 // mul.with.overflow and adjust properly mask/size.
6126 if (MulVal->hasNUsesOrMore(2)) {
6127 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6128 for (User *U : make_early_inc_range(MulVal->users())) {
6129 if (U == &I)
6130 continue;
6131 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6132 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6133 IC.replaceInstUsesWith(*TI, Mul);
6134 else
6135 TI->setOperand(0, Mul);
6136 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6137 assert(BO->getOpcode() == Instruction::And);
6138 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6139 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6140 APInt ShortMask = CI->getValue().trunc(MulWidth);
6141 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6142 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6143 IC.replaceInstUsesWith(*BO, Zext);
6144 } else {
6145 llvm_unreachable("Unexpected Binary operation");
6146 }
6147 IC.addToWorklist(cast<Instruction>(U));
6148 }
6149 }
6150
6151 // The original icmp gets replaced with the overflow value, maybe inverted
6152 // depending on predicate.
6153 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6154 Value *Res = Builder.CreateExtractValue(Call, 1);
6155 return BinaryOperator::CreateNot(Res);
6156 }
6157
6158 return ExtractValueInst::Create(Call, 1);
6159}
6160
6161/// When performing a comparison against a constant, it is possible that not all
6162/// the bits in the LHS are demanded. This helper method computes the mask that
6163/// IS demanded.
6165 const APInt *RHS;
6166 if (!match(I.getOperand(1), m_APInt(RHS)))
6168
6169 // If this is a normal comparison, it demands all bits. If it is a sign bit
6170 // comparison, it only demands the sign bit.
6171 bool UnusedBit;
6172 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6174
6175 switch (I.getPredicate()) {
6176 // For a UGT comparison, we don't care about any bits that
6177 // correspond to the trailing ones of the comparand. The value of these
6178 // bits doesn't impact the outcome of the comparison, because any value
6179 // greater than the RHS must differ in a bit higher than these due to carry.
6180 case ICmpInst::ICMP_UGT:
6181 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6182
6183 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6184 // Any value less than the RHS must differ in a higher bit because of carries.
6185 case ICmpInst::ICMP_ULT:
6186 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6187
6188 default:
6190 }
6191}
6192
6193/// Check that one use is in the same block as the definition and all
6194/// other uses are in blocks dominated by a given block.
6195///
6196/// \param DI Definition
6197/// \param UI Use
6198/// \param DB Block that must dominate all uses of \p DI outside
6199/// the parent block
6200/// \return true when \p UI is the only use of \p DI in the parent block
6201/// and all other uses of \p DI are in blocks dominated by \p DB.
6202///
6204 const Instruction *UI,
6205 const BasicBlock *DB) const {
6206 assert(DI && UI && "Instruction not defined\n");
6207 // Ignore incomplete definitions.
6208 if (!DI->getParent())
6209 return false;
6210 // DI and UI must be in the same block.
6211 if (DI->getParent() != UI->getParent())
6212 return false;
6213 // Protect from self-referencing blocks.
6214 if (DI->getParent() == DB)
6215 return false;
6216 for (const User *U : DI->users()) {
6217 auto *Usr = cast<Instruction>(U);
6218 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6219 return false;
6220 }
6221 return true;
6222}
6223
6224/// Return true when the instruction sequence within a block is select-cmp-br.
6225static bool isChainSelectCmpBranch(const SelectInst *SI) {
6226 const BasicBlock *BB = SI->getParent();
6227 if (!BB)
6228 return false;
6229 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
6230 if (!BI || BI->getNumSuccessors() != 2)
6231 return false;
6232 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6233 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6234 return false;
6235 return true;
6236}
6237
6238/// True when a select result is replaced by one of its operands
6239/// in select-icmp sequence. This will eventually result in the elimination
6240/// of the select.
6241///
6242/// \param SI Select instruction
6243/// \param Icmp Compare instruction
6244/// \param SIOpd Operand that replaces the select
6245///
6246/// Notes:
6247/// - The replacement is global and requires dominator information
6248/// - The caller is responsible for the actual replacement
6249///
6250/// Example:
6251///
6252/// entry:
6253/// %4 = select i1 %3, %C* %0, %C* null
6254/// %5 = icmp eq %C* %4, null
6255/// br i1 %5, label %9, label %7
6256/// ...
6257/// ; <label>:7 ; preds = %entry
6258/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6259/// ...
6260///
6261/// can be transformed to
6262///
6263/// %5 = icmp eq %C* %0, null
6264/// %6 = select i1 %3, i1 %5, i1 true
6265/// br i1 %6, label %9, label %7
6266/// ...
6267/// ; <label>:7 ; preds = %entry
6268/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6269///
6270/// Similar when the first operand of the select is a constant or/and
6271/// the compare is for not equal rather than equal.
6272///
6273/// NOTE: The function is only called when the select and compare constants
6274/// are equal, the optimization can work only for EQ predicates. This is not a
6275/// major restriction since a NE compare should be 'normalized' to an equal
6276/// compare, which usually happens in the combiner and test case
6277/// select-cmp-br.ll checks for it.
6279 const ICmpInst *Icmp,
6280 const unsigned SIOpd) {
6281 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6283 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6284 // The check for the single predecessor is not the best that can be
6285 // done. But it protects efficiently against cases like when SI's
6286 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6287 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6288 // replaced can be reached on either path. So the uniqueness check
6289 // guarantees that the path all uses of SI (outside SI's parent) are on
6290 // is disjoint from all other paths out of SI. But that information
6291 // is more expensive to compute, and the trade-off here is in favor
6292 // of compile-time. It should also be noticed that we check for a single
6293 // predecessor and not only uniqueness. This to handle the situation when
6294 // Succ and Succ1 points to the same basic block.
6295 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6296 NumSel++;
6297 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6298 return true;
6299 }
6300 }
6301 return false;
6302}
6303
6304/// Try to fold the comparison based on range information we can get by checking
6305/// whether bits are known to be zero or one in the inputs.
6307 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6308 Type *Ty = Op0->getType();
6309 ICmpInst::Predicate Pred = I.getPredicate();
6310
6311 // Get scalar or pointer size.
6312 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6313 ? Ty->getScalarSizeInBits()
6315
6316 if (!BitWidth)
6317 return nullptr;
6318
6319 KnownBits Op0Known(BitWidth);
6320 KnownBits Op1Known(BitWidth);
6321
6322 {
6323 // Don't use dominating conditions when folding icmp using known bits. This
6324 // may convert signed into unsigned predicates in ways that other passes
6325 // (especially IndVarSimplify) may not be able to reliably undo.
6326 SQ.DC = nullptr;
6327 auto _ = make_scope_exit([&]() { SQ.DC = &DC; });
6329 Op0Known, 0))
6330 return &I;
6331
6332 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnes(BitWidth), Op1Known, 0))
6333 return &I;
6334 }
6335
6336 // Given the known and unknown bits, compute a range that the LHS could be
6337 // in. Compute the Min, Max and RHS values based on the known bits. For the
6338 // EQ and NE we use unsigned values.
6339 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6340 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6341 if (I.isSigned()) {
6342 Op0Min = Op0Known.getSignedMinValue();
6343 Op0Max = Op0Known.getSignedMaxValue();
6344 Op1Min = Op1Known.getSignedMinValue();
6345 Op1Max = Op1Known.getSignedMaxValue();
6346 } else {
6347 Op0Min = Op0Known.getMinValue();
6348 Op0Max = Op0Known.getMaxValue();
6349 Op1Min = Op1Known.getMinValue();
6350 Op1Max = Op1Known.getMaxValue();
6351 }
6352
6353 // If Min and Max are known to be the same, then SimplifyDemandedBits figured
6354 // out that the LHS or RHS is a constant. Constant fold this now, so that
6355 // code below can assume that Min != Max.
6356 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
6357 return new ICmpInst(Pred, ConstantExpr::getIntegerValue(Ty, Op0Min), Op1);
6358 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
6359 return new ICmpInst(Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Min));
6360
6361 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6362 // min/max canonical compare with some other compare. That could lead to
6363 // conflict with select canonicalization and infinite looping.
6364 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6365 auto isMinMaxCmp = [&](Instruction &Cmp) {
6366 if (!Cmp.hasOneUse())
6367 return false;
6368 Value *A, *B;
6369 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6371 return false;
6372 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6373 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6374 };
6375 if (!isMinMaxCmp(I)) {
6376 switch (Pred) {
6377 default:
6378 break;
6379 case ICmpInst::ICMP_ULT: {
6380 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6381 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6382 const APInt *CmpC;
6383 if (match(Op1, m_APInt(CmpC))) {
6384 // A <u C -> A == C-1 if min(A)+1 == C
6385 if (*CmpC == Op0Min + 1)
6386 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6387 ConstantInt::get(Op1->getType(), *CmpC - 1));
6388 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6389 // exceeds the log2 of C.
6390 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6391 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6392 Constant::getNullValue(Op1->getType()));
6393 }
6394 break;
6395 }
6396 case ICmpInst::ICMP_UGT: {
6397 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6398 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6399 const APInt *CmpC;
6400 if (match(Op1, m_APInt(CmpC))) {
6401 // A >u C -> A == C+1 if max(a)-1 == C
6402 if (*CmpC == Op0Max - 1)
6403 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6404 ConstantInt::get(Op1->getType(), *CmpC + 1));
6405 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6406 // exceeds the log2 of C.
6407 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6408 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6409 Constant::getNullValue(Op1->getType()));
6410 }
6411 break;
6412 }
6413 case ICmpInst::ICMP_SLT: {
6414 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6415 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6416 const APInt *CmpC;
6417 if (match(Op1, m_APInt(CmpC))) {
6418 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6419 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6420 ConstantInt::get(Op1->getType(), *CmpC - 1));
6421 }
6422 break;
6423 }
6424 case ICmpInst::ICMP_SGT: {
6425 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6426 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6427 const APInt *CmpC;
6428 if (match(Op1, m_APInt(CmpC))) {
6429 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6430 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6431 ConstantInt::get(Op1->getType(), *CmpC + 1));
6432 }
6433 break;
6434 }
6435 }
6436 }
6437
6438 // Based on the range information we know about the LHS, see if we can
6439 // simplify this comparison. For example, (x&4) < 8 is always true.
6440 switch (Pred) {
6441 default:
6442 llvm_unreachable("Unknown icmp opcode!");
6443 case ICmpInst::ICMP_EQ:
6444 case ICmpInst::ICMP_NE: {
6445 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
6446 return replaceInstUsesWith(
6447 I, ConstantInt::getBool(I.getType(), Pred == CmpInst::ICMP_NE));
6448
6449 // If all bits are known zero except for one, then we know at most one bit
6450 // is set. If the comparison is against zero, then this is a check to see if
6451 // *that* bit is set.
6452 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
6453 if (Op1Known.isZero()) {
6454 // If the LHS is an AND with the same constant, look through it.
6455 Value *LHS = nullptr;
6456 const APInt *LHSC;
6457 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
6458 *LHSC != Op0KnownZeroInverted)
6459 LHS = Op0;
6460
6461 Value *X;
6462 const APInt *C1;
6463 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
6464 Type *XTy = X->getType();
6465 unsigned Log2C1 = C1->countr_zero();
6466 APInt C2 = Op0KnownZeroInverted;
6467 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
6468 if (C2Pow2.isPowerOf2()) {
6469 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
6470 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
6471 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
6472 unsigned Log2C2 = C2Pow2.countr_zero();
6473 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
6474 auto NewPred =
6476 return new ICmpInst(NewPred, X, CmpC);
6477 }
6478 }
6479 }
6480
6481 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
6482 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
6483 (Op0Known & Op1Known) == Op0Known)
6484 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6485 ConstantInt::getNullValue(Op1->getType()));
6486 break;
6487 }
6488 case ICmpInst::ICMP_ULT: {
6489 if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
6490 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6491 if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
6492 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6493 break;
6494 }
6495 case ICmpInst::ICMP_UGT: {
6496 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
6497 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6498 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
6499 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6500 break;
6501 }
6502 case ICmpInst::ICMP_SLT: {
6503 if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
6504 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6505 if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
6506 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6507 break;
6508 }
6509 case ICmpInst::ICMP_SGT: {
6510 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
6511 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6512 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
6513 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6514 break;
6515 }
6516 case ICmpInst::ICMP_SGE:
6517 assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
6518 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
6519 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6520 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
6521 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6522 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
6523 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6524 break;
6525 case ICmpInst::ICMP_SLE:
6526 assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
6527 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
6528 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6529 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
6530 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6531 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
6532 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6533 break;
6534 case ICmpInst::ICMP_UGE:
6535 assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
6536 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
6537 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6538 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
6539 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6540 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
6541 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6542 break;
6543 case ICmpInst::ICMP_ULE:
6544 assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
6545 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
6546 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6547 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
6548 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6549 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
6550 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6551 break;
6552 }
6553
6554 // Turn a signed comparison into an unsigned one if both operands are known to
6555 // have the same sign.
6556 if (I.isSigned() &&
6557 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
6558 (Op0Known.One.isNegative() && Op1Known.One.isNegative())))
6559 return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
6560
6561 return nullptr;
6562}
6563
6564/// If one operand of an icmp is effectively a bool (value range of {0,1}),
6565/// then try to reduce patterns based on that limit.
6567 Value *X, *Y;
6569
6570 // X must be 0 and bool must be true for "ULT":
6571 // X <u (zext i1 Y) --> (X == 0) & Y
6572 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
6573 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
6574 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
6575
6576 // X must be 0 or bool must be true for "ULE":
6577 // X <=u (sext i1 Y) --> (X == 0) | Y
6578 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
6579 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
6580 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
6581
6582 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
6583 ICmpInst::Predicate Pred1, Pred2;
6584 const APInt *C;
6585 Instruction *ExtI;
6586 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
6589 m_APInt(C)))))) &&
6590 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
6591 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
6592 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
6593 auto CreateRangeCheck = [&] {
6594 Value *CmpV1 =
6595 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
6596 Value *CmpV2 = Builder.CreateICmp(
6597 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
6599 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
6600 CmpV1, CmpV2);
6601 };
6602 if (C->isZero()) {
6603 if (Pred2 == ICmpInst::ICMP_EQ) {
6604 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
6605 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
6606 return replaceInstUsesWith(
6607 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6608 } else if (!IsSExt || HasOneUse) {
6609 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
6610 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
6611 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
6612 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X == -1
6613 return CreateRangeCheck();
6614 }
6615 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
6616 if (Pred2 == ICmpInst::ICMP_NE) {
6617 // icmp eq X, (zext (icmp ne X, 1)) --> false
6618 // icmp ne X, (zext (icmp ne X, 1)) --> true
6619 // icmp eq X, (sext (icmp ne X, -1)) --> false
6620 // icmp ne X, (sext (icmp ne X, -1)) --> true
6621 return replaceInstUsesWith(
6622 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6623 } else if (!IsSExt || HasOneUse) {
6624 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
6625 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
6626 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
6627 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
6628 return CreateRangeCheck();
6629 }
6630 } else {
6631 // when C != 0 && C != 1:
6632 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
6633 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
6634 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
6635 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
6636 // when C != 0 && C != -1:
6637 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
6638 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
6639 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
6640 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
6641 return ICmpInst::Create(
6642 Instruction::ICmp, Pred1, X,
6643 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
6644 ? (IsSExt ? -1 : 1)
6645 : 0));
6646 }
6647 }
6648
6649 return nullptr;
6650}
6651
6652std::optional<std::pair<CmpInst::Predicate, Constant *>>
6654 Constant *C) {
6656 "Only for relational integer predicates.");
6657
6658 Type *Type = C->getType();
6659 bool IsSigned = ICmpInst::isSigned(Pred);
6660
6662 bool WillIncrement =
6663 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
6664
6665 // Check if the constant operand can be safely incremented/decremented
6666 // without overflowing/underflowing.
6667 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
6668 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
6669 };
6670
6671 Constant *SafeReplacementConstant = nullptr;
6672 if (auto *CI = dyn_cast<ConstantInt>(C)) {
6673 // Bail out if the constant can't be safely incremented/decremented.
6674 if (!ConstantIsOk(CI))
6675 return std::nullopt;
6676 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
6677 unsigned NumElts = FVTy->getNumElements();
6678 for (unsigned i = 0; i != NumElts; ++i) {
6679 Constant *Elt = C->getAggregateElement(i);
6680 if (!Elt)
6681 return std::nullopt;
6682
6683 if (isa<UndefValue>(Elt))
6684 continue;
6685
6686 // Bail out if we can't determine if this constant is min/max or if we
6687 // know that this constant is min/max.
6688 auto *CI = dyn_cast<ConstantInt>(Elt);
6689 if (!CI || !ConstantIsOk(CI))
6690 return std::nullopt;
6691
6692 if (!SafeReplacementConstant)
6693 SafeReplacementConstant = CI;
6694 }
6695 } else if (isa<VectorType>(C->getType())) {
6696 // Handle scalable splat
6697 Value *SplatC = C->getSplatValue();
6698 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
6699 // Bail out if the constant can't be safely incremented/decremented.
6700 if (!CI || !ConstantIsOk(CI))
6701 return std::nullopt;
6702 } else {
6703 // ConstantExpr?
6704 return std::nullopt;
6705 }
6706
6707 // It may not be safe to change a compare predicate in the presence of
6708 // undefined elements, so replace those elements with the first safe constant
6709 // that we found.
6710 // TODO: in case of poison, it is safe; let's replace undefs only.
6711 if (C->containsUndefOrPoisonElement()) {
6712 assert(SafeReplacementConstant && "Replacement constant not set");
6713 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
6714 }
6715
6717
6718 // Increment or decrement the constant.
6719 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
6720 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
6721
6722 return std::make_pair(NewPred, NewC);
6723}
6724
6725/// If we have an icmp le or icmp ge instruction with a constant operand, turn
6726/// it into the appropriate icmp lt or icmp gt instruction. This transform
6727/// allows them to be folded in visitICmpInst.
6729 ICmpInst::Predicate Pred = I.getPredicate();
6730 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
6732 return nullptr;
6733
6734 Value *Op0 = I.getOperand(0);
6735 Value *Op1 = I.getOperand(1);
6736 auto *Op1C = dyn_cast<Constant>(Op1);
6737 if (!Op1C)
6738 return nullptr;
6739
6740 auto FlippedStrictness =
6742 if (!FlippedStrictness)
6743 return nullptr;
6744
6745 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
6746}
6747
6748/// If we have a comparison with a non-canonical predicate, if we can update
6749/// all the users, invert the predicate and adjust all the users.
6751 // Is the predicate already canonical?
6752 CmpInst::Predicate Pred = I.getPredicate();
6754 return nullptr;
6755
6756 // Can all users be adjusted to predicate inversion?
6757 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
6758 return nullptr;
6759
6760 // Ok, we can canonicalize comparison!
6761 // Let's first invert the comparison's predicate.
6762 I.setPredicate(CmpInst::getInversePredicate(Pred));
6763 I.setName(I.getName() + ".not");
6764
6765 // And, adapt users.
6767
6768 return &I;
6769}
6770
6771/// Integer compare with boolean values can always be turned into bitwise ops.
6773 InstCombiner::BuilderTy &Builder) {
6774 Value *A = I.getOperand(0), *B = I.getOperand(1);
6775 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
6776
6777 // A boolean compared to true/false can be simplified to Op0/true/false in
6778 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
6779 // Cases not handled by InstSimplify are always 'not' of Op0.
6780 if (match(B, m_Zero())) {
6781 switch (I.getPredicate()) {
6782 case CmpInst::ICMP_EQ: // A == 0 -> !A
6783 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
6784 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
6786 default:
6787 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6788 }
6789 } else if (match(B, m_One())) {
6790 switch (I.getPredicate()) {
6791 case CmpInst::ICMP_NE: // A != 1 -> !A
6792 case CmpInst::ICMP_ULT: // A <u 1 -> !A
6793 case CmpInst::ICMP_SGT: // A >s -1 -> !A
6795 default:
6796 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6797 }
6798 }
6799
6800 switch (I.getPredicate()) {
6801 default:
6802 llvm_unreachable("Invalid icmp instruction!");
6803 case ICmpInst::ICMP_EQ:
6804 // icmp eq i1 A, B -> ~(A ^ B)
6805 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
6806
6807 case ICmpInst::ICMP_NE:
6808 // icmp ne i1 A, B -> A ^ B
6809 return BinaryOperator::CreateXor(A, B);
6810
6811 case ICmpInst::ICMP_UGT:
6812 // icmp ugt -> icmp ult
6813 std::swap(A, B);
6814 [[fallthrough]];
6815 case ICmpInst::ICMP_ULT:
6816 // icmp ult i1 A, B -> ~A & B
6817 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
6818
6819 case ICmpInst::ICMP_SGT:
6820 // icmp sgt -> icmp slt
6821 std::swap(A, B);
6822 [[fallthrough]];
6823 case ICmpInst::ICMP_SLT:
6824 // icmp slt i1 A, B -> A & ~B
6825 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
6826
6827 case ICmpInst::ICMP_UGE:
6828 // icmp uge -> icmp ule
6829 std::swap(A, B);
6830 [[fallthrough]];
6831 case ICmpInst::ICMP_ULE:
6832 // icmp ule i1 A, B -> ~A | B
6833 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
6834
6835 case ICmpInst::ICMP_SGE:
6836 // icmp sge -> icmp sle
6837 std::swap(A, B);
6838 [[fallthrough]];
6839 case ICmpInst::ICMP_SLE:
6840 // icmp sle i1 A, B -> A | ~B
6841 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
6842 }
6843}
6844
6845// Transform pattern like:
6846// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
6847// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
6848// Into:
6849// (X l>> Y) != 0
6850// (X l>> Y) == 0
6852 InstCombiner::BuilderTy &Builder) {
6853 ICmpInst::Predicate Pred, NewPred;
6854 Value *X, *Y;
6855 if (match(&Cmp,
6856 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
6857 switch (Pred) {
6858 case ICmpInst::ICMP_ULE:
6859 NewPred = ICmpInst::ICMP_NE;
6860 break;
6861 case ICmpInst::ICMP_UGT:
6862 NewPred = ICmpInst::ICMP_EQ;
6863 break;
6864 default:
6865 return nullptr;
6866 }
6867 } else if (match(&Cmp, m_c_ICmp(Pred,
6870 m_Add(m_Shl(m_One(), m_Value(Y)),
6871 m_AllOnes()))),
6872 m_Value(X)))) {
6873 // The variant with 'add' is not canonical, (the variant with 'not' is)
6874 // we only get it because it has extra uses, and can't be canonicalized,
6875
6876 switch (Pred) {
6877 case ICmpInst::ICMP_ULT:
6878 NewPred = ICmpInst::ICMP_NE;
6879 break;
6880 case ICmpInst::ICMP_UGE:
6881 NewPred = ICmpInst::ICMP_EQ;
6882 break;
6883 default:
6884 return nullptr;
6885 }
6886 } else
6887 return nullptr;
6888
6889 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
6890 Constant *Zero = Constant::getNullValue(NewX->getType());
6891 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
6892}
6893
6895 InstCombiner::BuilderTy &Builder) {
6896 const CmpInst::Predicate Pred = Cmp.getPredicate();
6897 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
6898 Value *V1, *V2;
6899
6900 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
6901 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
6902 if (auto *I = dyn_cast<Instruction>(V))
6903 I->copyIRFlags(&Cmp);
6904 Module *M = Cmp.getModule();
6905 Function *F =
6906 Intrinsic::getDeclaration(M, Intrinsic::vector_reverse, V->getType());
6907 return CallInst::Create(F, V);
6908 };
6909
6910 if (match(LHS, m_VecReverse(m_Value(V1)))) {
6911 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
6912 if (match(RHS, m_VecReverse(m_Value(V2))) &&
6913 (LHS->hasOneUse() || RHS->hasOneUse()))
6914 return createCmpReverse(Pred, V1, V2);
6915
6916 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
6917 if (LHS->hasOneUse() && isSplatValue(RHS))
6918 return createCmpReverse(Pred, V1, RHS);
6919 }
6920 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
6921 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
6922 return createCmpReverse(Pred, LHS, V2);
6923
6924 ArrayRef<int> M;
6925 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
6926 return nullptr;
6927
6928 // If both arguments of the cmp are shuffles that use the same mask and
6929 // shuffle within a single vector, move the shuffle after the cmp:
6930 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
6931 Type *V1Ty = V1->getType();
6932 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
6933 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
6934 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
6935 return new ShuffleVectorInst(NewCmp, M);
6936 }
6937
6938 // Try to canonicalize compare with splatted operand and splat constant.
6939 // TODO: We could generalize this for more than splats. See/use the code in
6940 // InstCombiner::foldVectorBinop().
6941 Constant *C;
6942 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
6943 return nullptr;
6944
6945 // Length-changing splats are ok, so adjust the constants as needed:
6946 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
6947 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
6948 int MaskSplatIndex;
6949 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
6950 // We allow poison in matching, but this transform removes it for safety.
6951 // Demanded elements analysis should be able to recover some/all of that.
6952 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
6953 ScalarC);
6954 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
6955 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
6956 return new ShuffleVectorInst(NewCmp, NewM);
6957 }
6958
6959 return nullptr;
6960}
6961
6962// extract(uadd.with.overflow(A, B), 0) ult A
6963// -> extract(uadd.with.overflow(A, B), 1)
6965 CmpInst::Predicate Pred = I.getPredicate();
6966 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6967
6968 Value *UAddOv;
6969 Value *A, *B;
6970 auto UAddOvResultPat = m_ExtractValue<0>(
6971 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));
6972 if (match(Op0, UAddOvResultPat) &&
6973 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
6974 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
6975 (match(A, m_One()) || match(B, m_One()))) ||
6976 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
6977 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
6978 // extract(uadd.with.overflow(A, B), 0) < A
6979 // extract(uadd.with.overflow(A, 1), 0) == 0
6980 // extract(uadd.with.overflow(A, -1), 0) != -1
6981 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
6982 else if (match(Op1, UAddOvResultPat) &&
6983 Pred == ICmpInst::ICMP_UGT && (Op0 == A || Op0 == B))
6984 // A > extract(uadd.with.overflow(A, B), 0)
6985 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
6986 else
6987 return nullptr;
6988
6989 return ExtractValueInst::Create(UAddOv, 1);
6990}
6991
6993 if (!I.getOperand(0)->getType()->isPointerTy() ||
6995 I.getParent()->getParent(),
6996 I.getOperand(0)->getType()->getPointerAddressSpace())) {
6997 return nullptr;
6998 }
6999 Instruction *Op;
7000 if (match(I.getOperand(0), m_Instruction(Op)) &&
7001 match(I.getOperand(1), m_Zero()) &&
7002 Op->isLaunderOrStripInvariantGroup()) {
7003 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
7004 Op->getOperand(0), I.getOperand(1));
7005 }
7006 return nullptr;
7007}
7008
7009/// This function folds patterns produced by lowering of reduce idioms, such as
7010/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7011/// attempts to generate fewer number of scalar comparisons instead of vector
7012/// comparisons when possible.
7014 InstCombiner::BuilderTy &Builder,
7015 const DataLayout &DL) {
7016 if (I.getType()->isVectorTy())
7017 return nullptr;
7018 ICmpInst::Predicate OuterPred, InnerPred;
7019 Value *LHS, *RHS;
7020
7021 // Match lowering of @llvm.vector.reduce.and. Turn
7022 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7023 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7024 /// %res = icmp <pred> i8 %scalar_ne, 0
7025 ///
7026 /// into
7027 ///
7028 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7029 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7030 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7031 ///
7032 /// for <pred> in {ne, eq}.
7033 if (!match(&I, m_ICmp(OuterPred,
7035 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7036 m_Zero())))
7037 return nullptr;
7038 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7039 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7040 return nullptr;
7041 unsigned NumBits =
7042 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7043 // TODO: Relax this to "not wider than max legal integer type"?
7044 if (!DL.isLegalInteger(NumBits))
7045 return nullptr;
7046
7047 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7048 auto *ScalarTy = Builder.getIntNTy(NumBits);
7049 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7050 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7051 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7052 I.getName());
7053 }
7054
7055 return nullptr;
7056}
7057
7058// This helper will be called with icmp operands in both orders.
7060 Value *Op0, Value *Op1,
7061 ICmpInst &CxtI) {
7062 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7063 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7064 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7065 return NI;
7066
7067 if (auto *SI = dyn_cast<SelectInst>(Op0))
7068 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7069 return NI;
7070
7071 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0))
7072 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7073 return Res;
7074
7075 {
7076 Value *X;
7077 const APInt *C;
7078 // icmp X+Cst, X
7079 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7080 return foldICmpAddOpConst(X, *C, Pred);
7081 }
7082
7083 // abs(X) >= X --> true
7084 // abs(X) u<= X --> true
7085 // abs(X) < X --> false
7086 // abs(X) u> X --> false
7087 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7088 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7089 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7090 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7091 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7092 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7093 {
7094 Value *X;
7095 Constant *C;
7096 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
7097 match(Op1, m_Specific(X))) {
7098 Value *NullValue = Constant::getNullValue(X->getType());
7099 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7100 const APInt SMin =
7101 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7102 bool IsIntMinPosion = C->isAllOnesValue();
7103 switch (Pred) {
7104 case CmpInst::ICMP_ULE:
7105 case CmpInst::ICMP_SGE:
7106 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7107 case CmpInst::ICMP_UGT:
7108 case CmpInst::ICMP_SLT:
7110 case CmpInst::ICMP_UGE:
7111 case CmpInst::ICMP_SLE:
7112 case CmpInst::ICMP_EQ: {
7113 return replaceInstUsesWith(
7114 CxtI, IsIntMinPosion
7115 ? Builder.CreateICmpSGT(X, AllOnesValue)
7117 X, ConstantInt::get(X->getType(), SMin + 1)));
7118 }
7119 case CmpInst::ICMP_ULT:
7120 case CmpInst::ICMP_SGT:
7121 case CmpInst::ICMP_NE: {
7122 return replaceInstUsesWith(
7123 CxtI, IsIntMinPosion
7124 ? Builder.CreateICmpSLT(X, NullValue)
7126 X, ConstantInt::get(X->getType(), SMin)));
7127 }
7128 default:
7129 llvm_unreachable("Invalid predicate!");
7130 }
7131 }
7132 }
7133
7134 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7135 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7136 return replaceInstUsesWith(CxtI, V);
7137
7138 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7139 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7140 {
7141 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7142 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7144 }
7145
7146 if (!ICmpInst::isUnsigned(Pred) &&
7147 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7148 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7150 }
7151 }
7152
7153 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7154 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7155 {
7156 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7157 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7159 }
7160
7161 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7162 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7163 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7165 }
7166 }
7167
7168 return nullptr;
7169}
7170
7172 bool Changed = false;
7174 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7175 unsigned Op0Cplxity = getComplexity(Op0);
7176 unsigned Op1Cplxity = getComplexity(Op1);
7177
7178 /// Orders the operands of the compare so that they are listed from most
7179 /// complex to least complex. This puts constants before unary operators,
7180 /// before binary operators.
7181 if (Op0Cplxity < Op1Cplxity) {
7182 I.swapOperands();
7183 std::swap(Op0, Op1);
7184 Changed = true;
7185 }
7186
7187 if (Value *V = simplifyICmpInst(I.getPredicate(), Op0, Op1, Q))
7188 return replaceInstUsesWith(I, V);
7189
7190 // Comparing -val or val with non-zero is the same as just comparing val
7191 // ie, abs(val) != 0 -> val != 0
7192 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7193 Value *Cond, *SelectTrue, *SelectFalse;
7194 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7195 m_Value(SelectFalse)))) {
7196 if (Value *V = dyn_castNegVal(SelectTrue)) {
7197 if (V == SelectFalse)
7198 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7199 }
7200 else if (Value *V = dyn_castNegVal(SelectFalse)) {
7201 if (V == SelectTrue)
7202 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7203 }
7204 }
7205 }
7206
7207 if (Op0->getType()->isIntOrIntVectorTy(1))
7209 return Res;
7210
7212 return Res;
7213
7215 return Res;
7216
7218 return Res;
7219
7221 return Res;
7222
7224 return Res;
7225
7227 return Res;
7228
7230 return Res;
7231
7232 // Test if the ICmpInst instruction is used exclusively by a select as
7233 // part of a minimum or maximum operation. If so, refrain from doing
7234 // any other folding. This helps out other analyses which understand
7235 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7236 // and CodeGen. And in this case, at least one of the comparison
7237 // operands has at least one user besides the compare (the select),
7238 // which would often largely negate the benefit of folding anyway.
7239 //
7240 // Do the same for the other patterns recognized by matchSelectPattern.
7241 if (I.hasOneUse())
7242 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7243 Value *A, *B;
7245 if (SPR.Flavor != SPF_UNKNOWN)
7246 return nullptr;
7247 }
7248
7249 // Do this after checking for min/max to prevent infinite looping.
7250 if (Instruction *Res = foldICmpWithZero(I))
7251 return Res;
7252
7253 // FIXME: We only do this after checking for min/max to prevent infinite
7254 // looping caused by a reverse canonicalization of these patterns for min/max.
7255 // FIXME: The organization of folds is a mess. These would naturally go into
7256 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7257 // down here after the min/max restriction.
7258 ICmpInst::Predicate Pred = I.getPredicate();
7259 const APInt *C;
7260 if (match(Op1, m_APInt(C))) {
7261 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7262 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7263 Constant *Zero = Constant::getNullValue(Op0->getType());
7264 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7265 }
7266
7267 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7268 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7270 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7271 }
7272 }
7273
7274 // The folds in here may rely on wrapping flags and special constants, so
7275 // they can break up min/max idioms in some cases but not seemingly similar
7276 // patterns.
7277 // FIXME: It may be possible to enhance select folding to make this
7278 // unnecessary. It may also be moot if we canonicalize to min/max
7279 // intrinsics.
7280 if (Instruction *Res = foldICmpBinOp(I, Q))
7281 return Res;
7282
7284 return Res;
7285
7286 // Try to match comparison as a sign bit test. Intentionally do this after
7287 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7288 if (Instruction *New = foldSignBitTest(I))
7289 return New;
7290
7292 return Res;
7293
7294 if (Instruction *Res = foldICmpCommutative(I.getPredicate(), Op0, Op1, I))
7295 return Res;
7296 if (Instruction *Res =
7297 foldICmpCommutative(I.getSwappedPredicate(), Op1, Op0, I))
7298 return Res;
7299
7300 if (I.isCommutative()) {
7301 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7302 replaceOperand(I, 0, Pair->first);
7303 replaceOperand(I, 1, Pair->second);
7304 return &I;
7305 }
7306 }
7307
7308 // In case of a comparison with two select instructions having the same
7309 // condition, check whether one of the resulting branches can be simplified.
7310 // If so, just compare the other branch and select the appropriate result.
7311 // For example:
7312 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7313 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7314 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7315 // The icmp will result false for the false value of selects and the result
7316 // will depend upon the comparison of true values of selects if %cmp is
7317 // true. Thus, transform this into:
7318 // %cmp = icmp slt i32 %y, %z
7319 // %sel = select i1 %cond, i1 %cmp, i1 false
7320 // This handles similar cases to transform.
7321 {
7322 Value *Cond, *A, *B, *C, *D;
7323 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7325 (Op0->hasOneUse() || Op1->hasOneUse())) {
7326 // Check whether comparison of TrueValues can be simplified
7327 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7328 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7329 return SelectInst::Create(Cond, Res, NewICMP);
7330 }
7331 // Check whether comparison of FalseValues can be simplified
7332 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7333 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7334 return SelectInst::Create(Cond, NewICMP, Res);
7335 }
7336 }
7337 }
7338
7339 // Try to optimize equality comparisons against alloca-based pointers.
7340 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7341 assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?");
7342 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7343 if (foldAllocaCmp(Alloca))
7344 return nullptr;
7345 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7346 if (foldAllocaCmp(Alloca))
7347 return nullptr;
7348 }
7349
7350 if (Instruction *Res = foldICmpBitCast(I))
7351 return Res;
7352
7353 // TODO: Hoist this above the min/max bailout.
7355 return R;
7356
7357 {
7358 Value *X, *Y;
7359 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7360 // and (X & ~Y) != 0 --> (X & Y) == 0
7361 // if A is a power of 2.
7362 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7363 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, 0, &I) &&
7364 I.isEquality())
7365 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7366 Op1);
7367
7368 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7369 if (Op0->getType()->isIntOrIntVectorTy()) {
7370 bool ConsumesOp0, ConsumesOp1;
7371 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7372 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7373 (ConsumesOp0 || ConsumesOp1)) {
7374 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7375 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7376 assert(InvOp0 && InvOp1 &&
7377 "Mismatch between isFreeToInvert and getFreelyInverted");
7378 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7379 }
7380 }
7381
7382 Instruction *AddI = nullptr;
7384 m_Instruction(AddI))) &&
7385 isa<IntegerType>(X->getType())) {
7386 Value *Result;
7387 Constant *Overflow;
7388 // m_UAddWithOverflow can match patterns that do not include an explicit
7389 // "add" instruction, so check the opcode of the matched op.
7390 if (AddI->getOpcode() == Instruction::Add &&
7391 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7392 Result, Overflow)) {
7393 replaceInstUsesWith(*AddI, Result);
7394 eraseInstFromFunction(*AddI);
7395 return replaceInstUsesWith(I, Overflow);
7396 }
7397 }
7398
7399 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7400 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7401 match(Op1, m_APInt(C))) {
7402 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7403 return R;
7404 }
7405
7406 // Signbit test folds
7407 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7408 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7409 Instruction *ExtI;
7410 if ((I.isUnsigned() || I.isEquality()) &&
7411 match(Op1,
7413 Y->getType()->getScalarSizeInBits() == 1 &&
7414 (Op0->hasOneUse() || Op1->hasOneUse())) {
7415 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7416 Instruction *ShiftI;
7417 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7419 OpWidth - 1))))) {
7420 unsigned ExtOpc = ExtI->getOpcode();
7421 unsigned ShiftOpc = ShiftI->getOpcode();
7422 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7423 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7424 Value *SLTZero =
7426 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7427 return replaceInstUsesWith(I, Cmp);
7428 }
7429 }
7430 }
7431 }
7432
7433 if (Instruction *Res = foldICmpEquality(I))
7434 return Res;
7435
7437 return Res;
7438
7439 if (Instruction *Res = foldICmpOfUAddOv(I))
7440 return Res;
7441
7442 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7443 // an i1 which indicates whether or not we successfully did the swap.
7444 //
7445 // Replace comparisons between the old value and the expected value with the
7446 // indicator that 'cmpxchg' returns.
7447 //
7448 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7449 // spuriously fail. In those cases, the old value may equal the expected
7450 // value but it is possible for the swap to not occur.
7451 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7452 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7453 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7454 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7455 !ACXI->isWeak())
7456 return ExtractValueInst::Create(ACXI, 1);
7457
7459 return Res;
7460
7461 if (I.getType()->isVectorTy())
7462 if (Instruction *Res = foldVectorCmp(I, Builder))
7463 return Res;
7464
7466 return Res;
7467
7469 return Res;
7470
7471 return Changed ? &I : nullptr;
7472}
7473
7474/// Fold fcmp ([us]itofp x, cst) if possible.
7476 Instruction *LHSI,
7477 Constant *RHSC) {
7478 const APFloat *RHS;
7479 if (!match(RHSC, m_APFloat(RHS)))
7480 return nullptr;
7481
7482 // Get the width of the mantissa. We don't want to hack on conversions that
7483 // might lose information from the integer, e.g. "i64 -> float"
7484 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7485 if (MantissaWidth == -1) return nullptr; // Unknown.
7486
7487 Type *IntTy = LHSI->getOperand(0)->getType();
7488 unsigned IntWidth = IntTy->getScalarSizeInBits();
7489 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7490
7491 if (I.isEquality()) {
7492 FCmpInst::Predicate P = I.getPredicate();
7493 bool IsExact = false;
7494 APSInt RHSCvt(IntWidth, LHSUnsigned);
7495 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7496
7497 // If the floating point constant isn't an integer value, we know if we will
7498 // ever compare equal / not equal to it.
7499 if (!IsExact) {
7500 // TODO: Can never be -0.0 and other non-representable values
7501 APFloat RHSRoundInt(*RHS);
7503 if (*RHS != RHSRoundInt) {
7505 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7506
7508 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7509 }
7510 }
7511
7512 // TODO: If the constant is exactly representable, is it always OK to do
7513 // equality compares as integer?
7514 }
7515
7516 // Check to see that the input is converted from an integer type that is small
7517 // enough that preserves all bits. TODO: check here for "known" sign bits.
7518 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
7519
7520 // Following test does NOT adjust IntWidth downwards for signed inputs,
7521 // because the most negative value still requires all the mantissa bits
7522 // to distinguish it from one less than that value.
7523 if ((int)IntWidth > MantissaWidth) {
7524 // Conversion would lose accuracy. Check if loss can impact comparison.
7525 int Exp = ilogb(*RHS);
7526 if (Exp == APFloat::IEK_Inf) {
7527 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
7528 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
7529 // Conversion could create infinity.
7530 return nullptr;
7531 } else {
7532 // Note that if RHS is zero or NaN, then Exp is negative
7533 // and first condition is trivially false.
7534 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
7535 // Conversion could affect comparison.
7536 return nullptr;
7537 }
7538 }
7539
7540 // Otherwise, we can potentially simplify the comparison. We know that it
7541 // will always come through as an integer value and we know the constant is
7542 // not a NAN (it would have been previously simplified).
7543 assert(!RHS->isNaN() && "NaN comparison not already folded!");
7544
7546 switch (I.getPredicate()) {
7547 default: llvm_unreachable("Unexpected predicate!");
7548 case FCmpInst::FCMP_UEQ:
7549 case FCmpInst::FCMP_OEQ:
7550 Pred = ICmpInst::ICMP_EQ;
7551 break;
7552 case FCmpInst::FCMP_UGT:
7553 case FCmpInst::FCMP_OGT:
7554 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
7555 break;
7556 case FCmpInst::FCMP_UGE:
7557 case FCmpInst::FCMP_OGE:
7558 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
7559 break;
7560 case FCmpInst::FCMP_ULT:
7561 case FCmpInst::FCMP_OLT:
7562 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
7563 break;
7564 case FCmpInst::FCMP_ULE:
7565 case FCmpInst::FCMP_OLE:
7566 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
7567 break;
7568 case FCmpInst::FCMP_UNE:
7569 case FCmpInst::FCMP_ONE:
7570 Pred = ICmpInst::ICMP_NE;
7571 break;
7572 case FCmpInst::FCMP_ORD:
7573 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7574 case FCmpInst::FCMP_UNO:
7575 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7576 }
7577
7578 // Now we know that the APFloat is a normal number, zero or inf.
7579
7580 // See if the FP constant is too large for the integer. For example,
7581 // comparing an i8 to 300.0.
7582 if (!LHSUnsigned) {
7583 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
7584 // and large values.
7585 APFloat SMax(RHS->getSemantics());
7586 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
7588 if (SMax < *RHS) { // smax < 13123.0
7589 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
7590 Pred == ICmpInst::ICMP_SLE)
7591 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7592 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7593 }
7594 } else {
7595 // If the RHS value is > UnsignedMax, fold the comparison. This handles
7596 // +INF and large values.
7597 APFloat UMax(RHS->getSemantics());
7598 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
7600 if (UMax < *RHS) { // umax < 13123.0
7601 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
7602 Pred == ICmpInst::ICMP_ULE)
7603 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7604 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7605 }
7606 }
7607
7608 if (!LHSUnsigned) {
7609 // See if the RHS value is < SignedMin.
7610 APFloat SMin(RHS->getSemantics());
7611 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
7613 if (SMin > *RHS) { // smin > 12312.0
7614 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
7615 Pred == ICmpInst::ICMP_SGE)
7616 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7617 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7618 }
7619 } else {
7620 // See if the RHS value is < UnsignedMin.
7621 APFloat UMin(RHS->getSemantics());
7622 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
7624 if (UMin > *RHS) { // umin > 12312.0
7625 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
7626 Pred == ICmpInst::ICMP_UGE)
7627 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7628 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7629 }
7630 }
7631
7632 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
7633 // [0, UMAX], but it may still be fractional. Check whether this is the case
7634 // using the IsExact flag.
7635 // Don't do this for zero, because -0.0 is not fractional.
7636 APSInt RHSInt(IntWidth, LHSUnsigned);
7637 bool IsExact;
7638 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
7639 if (!RHS->isZero()) {
7640 if (!IsExact) {
7641 // If we had a comparison against a fractional value, we have to adjust
7642 // the compare predicate and sometimes the value. RHSC is rounded towards
7643 // zero at this point.
7644 switch (Pred) {
7645 default: llvm_unreachable("Unexpected integer comparison!");
7646 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
7647 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7648 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
7649 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7650 case ICmpInst::ICMP_ULE:
7651 // (float)int <= 4.4 --> int <= 4
7652 // (float)int <= -4.4 --> false
7653 if (RHS->isNegative())
7654 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7655 break;
7656 case ICmpInst::ICMP_SLE:
7657 // (float)int <= 4.4 --> int <= 4
7658 // (float)int <= -4.4 --> int < -4
7659 if (RHS->isNegative())
7660 Pred = ICmpInst::ICMP_SLT;
7661 break;
7662 case ICmpInst::ICMP_ULT:
7663 // (float)int < -4.4 --> false
7664 // (float)int < 4.4 --> int <= 4
7665 if (RHS->isNegative())
7666 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7667 Pred = ICmpInst::ICMP_ULE;
7668 break;
7669 case ICmpInst::ICMP_SLT:
7670 // (float)int < -4.4 --> int < -4
7671 // (float)int < 4.4 --> int <= 4
7672 if (!RHS->isNegative())
7673 Pred = ICmpInst::ICMP_SLE;
7674 break;
7675 case ICmpInst::ICMP_UGT:
7676 // (float)int > 4.4 --> int > 4
7677 // (float)int > -4.4 --> true
7678 if (RHS->isNegative())
7679 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7680 break;
7681 case ICmpInst::ICMP_SGT:
7682 // (float)int > 4.4 --> int > 4
7683 // (float)int > -4.4 --> int >= -4
7684 if (RHS->isNegative())
7685 Pred = ICmpInst::ICMP_SGE;
7686 break;
7687 case ICmpInst::ICMP_UGE:
7688 // (float)int >= -4.4 --> true
7689 // (float)int >= 4.4 --> int > 4
7690 if (RHS->isNegative())
7691 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7692 Pred = ICmpInst::ICMP_UGT;
7693 break;
7694 case ICmpInst::ICMP_SGE:
7695 // (float)int >= -4.4 --> int >= -4
7696 // (float)int >= 4.4 --> int > 4
7697 if (!RHS->isNegative())
7698 Pred = ICmpInst::ICMP_SGT;
7699 break;
7700 }
7701 }
7702 }
7703
7704 // Lower this FP comparison into an appropriate integer version of the
7705 // comparison.
7706 return new ICmpInst(Pred, LHSI->getOperand(0),
7707 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
7708}
7709
7710/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
7712 Constant *RHSC) {
7713 // When C is not 0.0 and infinities are not allowed:
7714 // (C / X) < 0.0 is a sign-bit test of X
7715 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
7716 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
7717 //
7718 // Proof:
7719 // Multiply (C / X) < 0.0 by X * X / C.
7720 // - X is non zero, if it is the flag 'ninf' is violated.
7721 // - C defines the sign of X * X * C. Thus it also defines whether to swap
7722 // the predicate. C is also non zero by definition.
7723 //
7724 // Thus X * X / C is non zero and the transformation is valid. [qed]
7725
7726 FCmpInst::Predicate Pred = I.getPredicate();
7727
7728 // Check that predicates are valid.
7729 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
7730 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
7731 return nullptr;
7732
7733 // Check that RHS operand is zero.
7734 if (!match(RHSC, m_AnyZeroFP()))
7735 return nullptr;
7736
7737 // Check fastmath flags ('ninf').
7738 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
7739 return nullptr;
7740
7741 // Check the properties of the dividend. It must not be zero to avoid a
7742 // division by zero (see Proof).
7743 const APFloat *C;
7744 if (!match(LHSI->getOperand(0), m_APFloat(C)))
7745 return nullptr;
7746
7747 if (C->isZero())
7748 return nullptr;
7749
7750 // Get swapped predicate if necessary.
7751 if (C->isNegative())
7752 Pred = I.getSwappedPredicate();
7753
7754 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
7755}
7756
7757/// Optimize fabs(X) compared with zero.
7759 Value *X;
7760 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
7761 return nullptr;
7762
7763 const APFloat *C;
7764 if (!match(I.getOperand(1), m_APFloat(C)))
7765 return nullptr;
7766
7767 if (!C->isPosZero()) {
7768 if (!C->isSmallestNormalized())
7769 return nullptr;
7770
7771 const Function *F = I.getFunction();
7772 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
7773 if (Mode.Input == DenormalMode::PreserveSign ||
7774 Mode.Input == DenormalMode::PositiveZero) {
7775
7776 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7777 Constant *Zero = ConstantFP::getZero(X->getType());
7778 return new FCmpInst(P, X, Zero, "", I);
7779 };
7780
7781 switch (I.getPredicate()) {
7782 case FCmpInst::FCMP_OLT:
7783 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
7784 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
7785 case FCmpInst::FCMP_UGE:
7786 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
7787 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
7788 case FCmpInst::FCMP_OGE:
7789 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
7790 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
7791 case FCmpInst::FCMP_ULT:
7792 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
7793 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
7794 default:
7795 break;
7796 }
7797 }
7798
7799 return nullptr;
7800 }
7801
7802 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7803 I->setPredicate(P);
7804 return IC.replaceOperand(*I, 0, X);
7805 };
7806
7807 switch (I.getPredicate()) {
7808 case FCmpInst::FCMP_UGE:
7809 case FCmpInst::FCMP_OLT:
7810 // fabs(X) >= 0.0 --> true
7811 // fabs(X) < 0.0 --> false
7812 llvm_unreachable("fcmp should have simplified");
7813
7814 case FCmpInst::FCMP_OGT:
7815 // fabs(X) > 0.0 --> X != 0.0
7816 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
7817
7818 case FCmpInst::FCMP_UGT:
7819 // fabs(X) u> 0.0 --> X u!= 0.0
7820 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
7821
7822 case FCmpInst::FCMP_OLE:
7823 // fabs(X) <= 0.0 --> X == 0.0
7824 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
7825
7826 case FCmpInst::FCMP_ULE:
7827 // fabs(X) u<= 0.0 --> X u== 0.0
7828 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
7829
7830 case FCmpInst::FCMP_OGE:
7831 // fabs(X) >= 0.0 --> !isnan(X)
7832 assert(!I.hasNoNaNs() && "fcmp should have simplified");
7833 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
7834
7835 case FCmpInst::FCMP_ULT:
7836 // fabs(X) u< 0.0 --> isnan(X)
7837 assert(!I.hasNoNaNs() && "fcmp should have simplified");
7838 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
7839
7840 case FCmpInst::FCMP_OEQ:
7841 case FCmpInst::FCMP_UEQ:
7842 case FCmpInst::FCMP_ONE:
7843 case FCmpInst::FCMP_UNE:
7844 case FCmpInst::FCMP_ORD:
7845 case FCmpInst::FCMP_UNO:
7846 // Look through the fabs() because it doesn't change anything but the sign.
7847 // fabs(X) == 0.0 --> X == 0.0,
7848 // fabs(X) != 0.0 --> X != 0.0
7849 // isnan(fabs(X)) --> isnan(X)
7850 // !isnan(fabs(X) --> !isnan(X)
7851 return replacePredAndOp0(&I, I.getPredicate(), X);
7852
7853 default:
7854 return nullptr;
7855 }
7856}
7857
7859 CmpInst::Predicate Pred = I.getPredicate();
7860 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7861
7862 // Canonicalize fneg as Op1.
7863 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
7864 std::swap(Op0, Op1);
7865 Pred = I.getSwappedPredicate();
7866 }
7867
7868 if (!match(Op1, m_FNeg(m_Specific(Op0))))
7869 return nullptr;
7870
7871 // Replace the negated operand with 0.0:
7872 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
7873 Constant *Zero = ConstantFP::getZero(Op0->getType());
7874 return new FCmpInst(Pred, Op0, Zero, "", &I);
7875}
7876
7878 bool Changed = false;
7879
7880 /// Orders the operands of the compare so that they are listed from most
7881 /// complex to least complex. This puts constants before unary operators,
7882 /// before binary operators.
7883 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
7884 I.swapOperands();
7885 Changed = true;
7886 }
7887
7888 const CmpInst::Predicate Pred = I.getPredicate();
7889 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7890 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
7892 return replaceInstUsesWith(I, V);
7893
7894 // Simplify 'fcmp pred X, X'
7895 Type *OpType = Op0->getType();
7896 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
7897 if (Op0 == Op1) {
7898 switch (Pred) {
7899 default: break;
7900 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
7901 case FCmpInst::FCMP_ULT: // True if unordered or less than
7902 case FCmpInst::FCMP_UGT: // True if unordered or greater than
7903 case FCmpInst::FCMP_UNE: // True if unordered or not equal
7904 // Canonicalize these to be 'fcmp uno %X, 0.0'.
7905 I.setPredicate(FCmpInst::FCMP_UNO);
7906 I.setOperand(1, Constant::getNullValue(OpType));
7907 return &I;
7908
7909 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
7910 case FCmpInst::FCMP_OEQ: // True if ordered and equal
7911 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
7912 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
7913 // Canonicalize these to be 'fcmp ord %X, 0.0'.
7914 I.setPredicate(FCmpInst::FCMP_ORD);
7915 I.setOperand(1, Constant::getNullValue(OpType));
7916 return &I;
7917 }
7918 }
7919
7920 if (I.isCommutative()) {
7921 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7922 replaceOperand(I, 0, Pair->first);
7923 replaceOperand(I, 1, Pair->second);
7924 return &I;
7925 }
7926 }
7927
7928 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
7929 // then canonicalize the operand to 0.0.
7930 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
7931 if (!match(Op0, m_PosZeroFP()) &&
7932 isKnownNeverNaN(Op0, 0, getSimplifyQuery().getWithInstruction(&I)))
7933 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
7934
7935 if (!match(Op1, m_PosZeroFP()) &&
7936 isKnownNeverNaN(Op1, 0, getSimplifyQuery().getWithInstruction(&I)))
7937 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
7938 }
7939
7940 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
7941 Value *X, *Y;
7942 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
7943 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
7944
7946 return R;
7947
7948 // Test if the FCmpInst instruction is used exclusively by a select as
7949 // part of a minimum or maximum operation. If so, refrain from doing
7950 // any other folding. This helps out other analyses which understand
7951 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7952 // and CodeGen. And in this case, at least one of the comparison
7953 // operands has at least one user besides the compare (the select),
7954 // which would often largely negate the benefit of folding anyway.
7955 if (I.hasOneUse())
7956 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7957 Value *A, *B;
7959 if (SPR.Flavor != SPF_UNKNOWN)
7960 return nullptr;
7961 }
7962
7963 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
7964 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
7965 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
7966 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
7967
7968 // Canonicalize:
7969 // fcmp olt X, +inf -> fcmp one X, +inf
7970 // fcmp ole X, +inf -> fcmp ord X, 0
7971 // fcmp ogt X, +inf -> false
7972 // fcmp oge X, +inf -> fcmp oeq X, +inf
7973 // fcmp ult X, +inf -> fcmp une X, +inf
7974 // fcmp ule X, +inf -> true
7975 // fcmp ugt X, +inf -> fcmp uno X, 0
7976 // fcmp uge X, +inf -> fcmp ueq X, +inf
7977 // fcmp olt X, -inf -> false
7978 // fcmp ole X, -inf -> fcmp oeq X, -inf
7979 // fcmp ogt X, -inf -> fcmp one X, -inf
7980 // fcmp oge X, -inf -> fcmp ord X, 0
7981 // fcmp ult X, -inf -> fcmp uno X, 0
7982 // fcmp ule X, -inf -> fcmp ueq X, -inf
7983 // fcmp ugt X, -inf -> fcmp une X, -inf
7984 // fcmp uge X, -inf -> true
7985 const APFloat *C;
7986 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
7987 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
7988 default:
7989 break;
7990 case FCmpInst::FCMP_ORD:
7991 case FCmpInst::FCMP_UNO:
7994 case FCmpInst::FCMP_OGT:
7995 case FCmpInst::FCMP_ULE:
7996 llvm_unreachable("Should be simplified by InstSimplify");
7997 case FCmpInst::FCMP_OLT:
7998 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
7999 case FCmpInst::FCMP_OLE:
8000 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
8001 "", &I);
8002 case FCmpInst::FCMP_OGE:
8003 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
8004 case FCmpInst::FCMP_ULT:
8005 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
8006 case FCmpInst::FCMP_UGT:
8007 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8008 "", &I);
8009 case FCmpInst::FCMP_UGE:
8010 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8011 }
8012 }
8013
8014 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8015 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8016 if (match(Op1, m_PosZeroFP()) &&
8019 if (Pred == FCmpInst::FCMP_OEQ)
8020 IntPred = ICmpInst::ICMP_EQ;
8021 else if (Pred == FCmpInst::FCMP_UNE)
8022 IntPred = ICmpInst::ICMP_NE;
8023
8024 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8025 Type *IntTy = X->getType();
8026 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8027 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8028 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8029 }
8030 }
8031
8032 // Handle fcmp with instruction LHS and constant RHS.
8033 Instruction *LHSI;
8034 Constant *RHSC;
8035 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8036 switch (LHSI->getOpcode()) {
8037 case Instruction::Select:
8038 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8039 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8040 (match(LHSI,
8042 match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
8043 return replaceOperand(I, 0, X);
8044 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
8045 return NV;
8046 break;
8047 case Instruction::PHI:
8048 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8049 return NV;
8050 break;
8051 case Instruction::SIToFP:
8052 case Instruction::UIToFP:
8053 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8054 return NV;
8055 break;
8056 case Instruction::FDiv:
8057 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8058 return NV;
8059 break;
8060 case Instruction::Load:
8061 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8062 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
8064 cast<LoadInst>(LHSI), GEP, GV, I))
8065 return Res;
8066 break;
8067 }
8068 }
8069
8070 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8071 return R;
8072
8073 if (match(Op0, m_FNeg(m_Value(X)))) {
8074 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8075 Constant *C;
8076 if (match(Op1, m_Constant(C)))
8077 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8078 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8079 }
8080
8081 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8082 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8083 return new FCmpInst(Pred, X, Op1, "", &I);
8084
8085 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8086 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8087 return new FCmpInst(Pred, Op0, Y, "", &I);
8088
8089 if (match(Op0, m_FPExt(m_Value(X)))) {
8090 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8091 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8092 return new FCmpInst(Pred, X, Y, "", &I);
8093
8094 const APFloat *C;
8095 if (match(Op1, m_APFloat(C))) {
8096 const fltSemantics &FPSem =
8097 X->getType()->getScalarType()->getFltSemantics();
8098 bool Lossy;
8099 APFloat TruncC = *C;
8100 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8101
8102 if (Lossy) {
8103 // X can't possibly equal the higher-precision constant, so reduce any
8104 // equality comparison.
8105 // TODO: Other predicates can be handled via getFCmpCode().
8106 switch (Pred) {
8107 case FCmpInst::FCMP_OEQ:
8108 // X is ordered and equal to an impossible constant --> false
8109 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8110 case FCmpInst::FCMP_ONE:
8111 // X is ordered and not equal to an impossible constant --> ordered
8112 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8113 ConstantFP::getZero(X->getType()));
8114 case FCmpInst::FCMP_UEQ:
8115 // X is unordered or equal to an impossible constant --> unordered
8116 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8117 ConstantFP::getZero(X->getType()));
8118 case FCmpInst::FCMP_UNE:
8119 // X is unordered or not equal to an impossible constant --> true
8120 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8121 default:
8122 break;
8123 }
8124 }
8125
8126 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8127 // Avoid lossy conversions and denormals.
8128 // Zero is a special case that's OK to convert.
8129 APFloat Fabs = TruncC;
8130 Fabs.clearSign();
8131 if (!Lossy &&
8132 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8133 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8134 return new FCmpInst(Pred, X, NewC, "", &I);
8135 }
8136 }
8137 }
8138
8139 // Convert a sign-bit test of an FP value into a cast and integer compare.
8140 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8141 // TODO: Handle non-zero compare constants.
8142 // TODO: Handle other predicates.
8143 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),
8144 m_Value(X)))) &&
8145 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8146 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8147 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8148 IntType = VectorType::get(IntType, VecTy->getElementCount());
8149
8150 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8151 if (Pred == FCmpInst::FCMP_OLT) {
8152 Value *IntX = Builder.CreateBitCast(X, IntType);
8153 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8154 ConstantInt::getNullValue(IntType));
8155 }
8156 }
8157
8158 {
8159 Value *CanonLHS = nullptr, *CanonRHS = nullptr;
8160 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));
8161 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));
8162
8163 // (canonicalize(x) == x) => (x == x)
8164 if (CanonLHS == Op1)
8165 return new FCmpInst(Pred, Op1, Op1, "", &I);
8166
8167 // (x == canonicalize(x)) => (x == x)
8168 if (CanonRHS == Op0)
8169 return new FCmpInst(Pred, Op0, Op0, "", &I);
8170
8171 // (canonicalize(x) == canonicalize(y)) => (x == y)
8172 if (CanonLHS && CanonRHS)
8173 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8174 }
8175
8176 if (I.getType()->isVectorTy())
8177 if (Instruction *Res = foldVectorCmp(I, Builder))
8178 return Res;
8179
8180 return Changed ? &I : nullptr;
8181}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu 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...
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
#define _
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 Instruction * foldICmpUSubSatOrUAddSatWithConstant(ICmpInst::Predicate Pred, SaturatingInst *II, const APInt &C, InstCombiner::BuilderTy &Builder)
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 Value * foldICmpWithLowBitMaskedVal(ICmpInst::Predicate Pred, Value *Op0, Value *Op1, const SimplifyQuery &Q, InstCombiner &IC)
Some comparisons can be simplified.
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 Instruction * foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl, const APInt &C)
Fold icmp (shl 1, Y), C.
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 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 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 * foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * processUMulZExtIdiom(ICmpInst &I, Value *MulVal, const APInt *OtherVal, InstCombinerImpl &IC)
Recognize and process idiom involving test for multiplication overflow.
static Instruction * transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, const DataLayout &DL, InstCombiner &IC)
Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
static Instruction * foldFCmpFNegCommonOp(FCmpInst &I)
static bool canRewriteGEPAsOffset(Value *Start, Value *Base, 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 * 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 * 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 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 * 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 Value * rewriteGEPAsOffset(Value *Start, Value *Base, const DataLayout &DL, SetVector< Value * > &Explored, InstCombiner &IC)
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
static Instruction * 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:528
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
#define T1
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 defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
Value * RHS
Value * LHS
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
void clearSign()
Definition: APFloat.h:1159
bool isZero() const
Definition: APFloat.h:1291
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1026
APInt bitcastToAPInt() const
Definition: APFloat.h:1210
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1006
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:966
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5183
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1109
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition: APInt.h:427
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:207
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:401
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1463
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:184
APInt abs() const
Get the absolute value.
Definition: APInt.h:1737
unsigned ceilLogBase2() const
Definition: APInt.h:1706
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1179
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1918
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition: APInt.h:444
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:194
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1898
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:1227
bool eq(const APInt &RHS) const
Equality comparison.
Definition: APInt.h:1057
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1144
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1905
void negate()
Negate this APInt in place.
Definition: APInt.h:1421
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1589
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1548
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:334
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1565
unsigned logBase2() const
Definition: APInt.h:1703
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:453
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:383
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1128
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:418
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1108
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:274
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1215
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1911
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:367
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
unsigned countr_one() const
Count the number of trailing one bits.
Definition: APInt.h:1606
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
an instruction to allocate memory on the stack
Definition: Instructions.h:59
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:371
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
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:409
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:452
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:221
unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore)
Construct a binary instruction, given the opcode and the two operands.
BinaryOps getOpcode() const
Definition: InstrTypes.h:513
static BinaryOperator * CreateNot(Value *Op, const Twine &Name, BasicBlock::iterator InsertBefore)
Conditional or Unconditional Branch instruction.
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1687
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr, BasicBlock::iterator InsertBefore)
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:983
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1362
Predicate getStrictPredicate() const
For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
Definition: InstrTypes.h:1198
bool isEquality() const
Determine if this is an equals/not equals predicate.
Definition: InstrTypes.h:1255
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:996
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:1010
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:999
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:1008
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:997
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:998
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:1007
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:1001
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:1004
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:1005
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:1000
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:1002
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_NE
not equal
Definition: InstrTypes.h:1015
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:1009
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:1006
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:995
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:1003
bool isSigned() const
Definition: InstrTypes.h:1265
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:1167
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:1314
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition: InstrTypes.h:1211
static CmpInst * Create(OtherOps Op, Predicate Pred, Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore)
Construct a compare instruction, given the opcode, the predicate and the two operands.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:1129
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:1105
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:1233
Predicate getFlippedSignednessPredicate()
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert.
Definition: InstrTypes.h:1308
bool isIntPredicate() const
Definition: InstrTypes.h:1123
bool isUnsigned() const
Definition: InstrTypes.h:1271
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2126
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2087
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2542
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2529
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2556
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2535
static Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2523
static Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1037
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
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:255
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:849
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:205
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:123
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:856
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:148
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:145
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:863
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...
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 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:1449
This is an important base class in LLVM.
Definition: Constant.h:41
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:400
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:767
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:417
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:1758
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
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:110
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
Definition: DataLayout.h:260
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:878
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:763
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
Definition: DataLayout.cpp:905
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:504
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:893
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:145
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, BasicBlock::iterator InsertBefore)
This instruction compares its operands according to the predicate given to the constructor.
bool isEquality() const
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:420
Type * getSourceElementType() const
Definition: Operator.cpp:67
Value * getPointerOperand()
Definition: Operator.h:441
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
Definition: Operator.h:488
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:973
Type * getValueType() const
Definition: GlobalValue.h:296
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 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:94
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:913
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2257
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2460
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:539
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2265
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1193
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2516
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:466
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:932
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1437
Value * CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1336
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1876
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2245
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1721
Value * createIsFPClass(Value *FPNum, unsigned Test)
Definition: IRBuilder.cpp:1288
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:486
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2366
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2397
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1749
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2241
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1344
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2127
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2249
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1416
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2021
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1475
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1327
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:471
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2007
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1497
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1666
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2273
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2161
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2196
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition: IRBuilder.h:2544
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2412
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1519
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2351
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:516
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:502
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1404
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1361
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
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...
bool SimplifyDemandedBits(Instruction *I, unsigned Op, const APInt &DemandedMask, KnownBits &Known, unsigned Depth=0) override
This form of SimplifyDemandedBits simplifies the specified instruction operand if possible,...
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN)
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 * foldICmpWithMinMax(Instruction &I, MinMaxIntrinsic *MinMax, Value *Z, ICmpInst::Predicate Pred)
Fold icmp Pred min|max(X, Y), Z.
Instruction * foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I)
Fold comparisons between a GEP instruction and something else.
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 * foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Fold an icmp equality instruction with binary operator LHS and constant RHS: icmp eq/ne BO,...
Instruction * foldICmpUsingBoolRange(ICmpInst &I)
If one operand of an icmp is effectively a bool (value range of {0,1}), then try to reduce patterns b...
Instruction * foldICmpWithTrunc(ICmpInst &Cmp)
Instruction * foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, const APInt &C)
Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS, ConstantInt *&Less, ConstantInt *&Equal, ConstantInt *&Greater)
Match a select chain which produces one of three values based on whether the LHS is less than,...
Instruction * foldCmpLoadFromIndexedGlobal(LoadInst *LI, GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
This is called when we see this pattern: cmp pred (load (gep GV, ...)), cmpcst where GV is a global v...
Instruction * visitFCmpInst(FCmpInst &I)
Instruction * foldICmpUsingKnownBits(ICmpInst &Cmp)
Try to fold the comparison based on range information we can get by checking whether bits are known t...
Instruction * foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, const APInt &C)
Fold icmp ({su}div X, Y), C.
Instruction * foldIRemByPowerOfTwoToBitTest(ICmpInst &I)
If we have: icmp eq/ne (urem/srem x, y), 0 iff y is a power-of-two, we can replace this with a bit te...
Instruction * foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold fcmp ([us]itofp x, cst) if possible.
Instruction * foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Fold icmp (udiv X, Y), C.
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 * foldICmpAddOpConst(Value *X, const APInt &C, ICmpInst::Predicate Pred)
Fold "icmp pred (X+C), X".
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)
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 * foldICmpCommutative(ICmpInst::Predicate Pred, Value *Op0, Value *Op1, ICmpInst &CxtI)
Instruction * visitICmpInst(ICmpInst &I)
Instruction * foldSelectICmp(ICmpInst::Predicate Pred, SelectInst *SI, Value *RHS, const 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)
The core instruction combiner logic.
Definition: InstCombiner.h:47
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:499
SimplifyQuery SQ
Definition: InstCombiner.h:76
static bool isCanonicalPredicate(CmpInst::Predicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
Definition: InstCombiner.h:157
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:232
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI, bool IsNSW=false) const
Definition: InstCombiner.h:462
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
Definition: InstCombiner.h:139
TargetLibraryInfo & TLI
Definition: InstCombiner.h:73
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
Definition: InstCombiner.h:441
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:386
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
Definition: InstCombiner.h:55
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:485
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
Definition: InstCombiner.h:180
static std::optional< std::pair< CmpInst::Predicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpInst::Predicate Pred, Constant *C)
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:492
const DataLayout & DL
Definition: InstCombiner.h:75
DomConditionCache DC
Definition: InstCombiner.h:81
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:248
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:336
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:410
DominatorTree & DT
Definition: InstCombiner.h:74
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:470
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:431
BuilderTy & Builder
Definition: InstCombiner.h:60
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:477
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:213
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:342
unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:457
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:296
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:
const BasicBlock * getParent() const
Definition: Instruction.h:152
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:252
bool isShift() const
Definition: Instruction.h:259
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:278
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:54
An instruction for reading from memory.
Definition: Instructions.h:184
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:141
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, BasicBlock::iterator InsertBefore)
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, BasicBlock::iterator InsertBefore, 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:94
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
Class to represent struct types.
Definition: DerivedTypes.h:216
This class represents a truncation of integer types.
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:265
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:234
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
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:166
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
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:228
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout as defined b...
Definition: Type.h:171
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
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) 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:693
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
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.
Definition: Type.cpp:676
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:199
#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:2732
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:2750
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1471
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
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)
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.
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:972
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:816
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:875
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:980
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)
CastOperator_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
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:893
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.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
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:854
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::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.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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.
match_combine_or< CastOperator_match< OpTy, Instruction::Trunc >, OpTy > m_TruncOrSelf(const OpTy &Op)
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)
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:456
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:853
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:1722
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
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.
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:656
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.
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.
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:324
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:48
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
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:2060
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:1736
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.
Value * simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
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.
@ 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()).
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...
DWARFExpression::Operation Op
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
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:1921
bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, Value *&X, APInt &Mask, bool LookThroughTrunc=true)
Decompose an icmp into the form ((X & Mask) pred 0) if possible.
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:2039
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.
Value * simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
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:230
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:234
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.
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:77
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:238
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:270
APInt getSignedMaxValue() const
Return the maximal signed value possible given these KnownBits.
Definition: KnownBits.h:147
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:285
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:50
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:244
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:141
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:125
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:282
APInt getSignedMinValue() const
Return the minimal signed value possible given these KnownBits.
Definition: KnownBits.h:131
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:57
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:61
const Instruction * CxtI
Definition: SimplifyQuery.h:65
const DominatorTree * DT
Definition: SimplifyQuery.h:63
SimplifyQuery getWithInstruction(const Instruction *I) const
Definition: SimplifyQuery.h:96
AssumptionCache * AC
Definition: SimplifyQuery.h:64
const DomConditionCache * DC
Definition: SimplifyQuery.h:66
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:254