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 }
1514 // Try to match icmp (trunc nuw X), (zext Y)
1515 else if (!Cmp.isSigned() &&
1516 match(&Cmp, m_c_ICmp(Pred, m_NUWTrunc(m_Value(X)),
1517 m_OneUse(m_ZExt(m_Value(Y)))))) {
1518 // Can fold trunc nuw + zext for unsigned and equality predicates.
1519 }
1520 // Try to match icmp (trunc nsw X), (sext Y)
1521 else if (match(&Cmp, m_c_ICmp(Pred, m_NSWTrunc(m_Value(X)),
1523 // Can fold trunc nsw + zext/sext for all predicates.
1524 YIsSExt =
1525 isa<SExtInst>(Cmp.getOperand(0)) || isa<SExtInst>(Cmp.getOperand(1));
1526 } else
1527 return nullptr;
1528
1529 Type *TruncTy = Cmp.getOperand(0)->getType();
1530 unsigned TruncBits = TruncTy->getScalarSizeInBits();
1531
1532 // If this transform will end up changing from desirable types -> undesirable
1533 // types skip it.
1534 if (isDesirableIntType(TruncBits) &&
1535 !isDesirableIntType(X->getType()->getScalarSizeInBits()))
1536 return nullptr;
1537
1538 Value *NewY = Builder.CreateIntCast(Y, X->getType(), YIsSExt);
1539 return new ICmpInst(Pred, X, NewY);
1540}
1541
1542/// Fold icmp (xor X, Y), C.
1545 const APInt &C) {
1546 if (Instruction *I = foldICmpXorShiftConst(Cmp, Xor, C))
1547 return I;
1548
1549 Value *X = Xor->getOperand(0);
1550 Value *Y = Xor->getOperand(1);
1551 const APInt *XorC;
1552 if (!match(Y, m_APInt(XorC)))
1553 return nullptr;
1554
1555 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1556 // fold the xor.
1557 ICmpInst::Predicate Pred = Cmp.getPredicate();
1558 bool TrueIfSigned = false;
1559 if (isSignBitCheck(Cmp.getPredicate(), C, TrueIfSigned)) {
1560
1561 // If the sign bit of the XorCst is not set, there is no change to
1562 // the operation, just stop using the Xor.
1563 if (!XorC->isNegative())
1564 return replaceOperand(Cmp, 0, X);
1565
1566 // Emit the opposite comparison.
1567 if (TrueIfSigned)
1568 return new ICmpInst(ICmpInst::ICMP_SGT, X,
1569 ConstantInt::getAllOnesValue(X->getType()));
1570 else
1571 return new ICmpInst(ICmpInst::ICMP_SLT, X,
1572 ConstantInt::getNullValue(X->getType()));
1573 }
1574
1575 if (Xor->hasOneUse()) {
1576 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1577 if (!Cmp.isEquality() && XorC->isSignMask()) {
1578 Pred = Cmp.getFlippedSignednessPredicate();
1579 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1580 }
1581
1582 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
1583 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1584 Pred = Cmp.getFlippedSignednessPredicate();
1585 Pred = Cmp.getSwappedPredicate(Pred);
1586 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
1587 }
1588 }
1589
1590 // Mask constant magic can eliminate an 'xor' with unsigned compares.
1591 if (Pred == ICmpInst::ICMP_UGT) {
1592 // (xor X, ~C) >u C --> X <u ~C (when C+1 is a power of 2)
1593 if (*XorC == ~C && (C + 1).isPowerOf2())
1594 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1595 // (xor X, C) >u C --> X >u C (when C+1 is a power of 2)
1596 if (*XorC == C && (C + 1).isPowerOf2())
1597 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
1598 }
1599 if (Pred == ICmpInst::ICMP_ULT) {
1600 // (xor X, -C) <u C --> X >u ~C (when C is a power of 2)
1601 if (*XorC == -C && C.isPowerOf2())
1602 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1603 ConstantInt::get(X->getType(), ~C));
1604 // (xor X, C) <u C --> X >u ~C (when -C is a power of 2)
1605 if (*XorC == C && (-C).isPowerOf2())
1606 return new ICmpInst(ICmpInst::ICMP_UGT, X,
1607 ConstantInt::get(X->getType(), ~C));
1608 }
1609 return nullptr;
1610}
1611
1612/// For power-of-2 C:
1613/// ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1)
1614/// ((X s>> ShiftC) ^ X) u> (C - 1) --> (X + C) u> ((C << 1) - 1)
1617 const APInt &C) {
1618 CmpInst::Predicate Pred = Cmp.getPredicate();
1619 APInt PowerOf2;
1620 if (Pred == ICmpInst::ICMP_ULT)
1621 PowerOf2 = C;
1622 else if (Pred == ICmpInst::ICMP_UGT && !C.isMaxValue())
1623 PowerOf2 = C + 1;
1624 else
1625 return nullptr;
1626 if (!PowerOf2.isPowerOf2())
1627 return nullptr;
1628 Value *X;
1629 const APInt *ShiftC;
1631 m_AShr(m_Deferred(X), m_APInt(ShiftC))))))
1632 return nullptr;
1633 uint64_t Shift = ShiftC->getLimitedValue();
1634 Type *XType = X->getType();
1635 if (Shift == 0 || PowerOf2.isMinSignedValue())
1636 return nullptr;
1637 Value *Add = Builder.CreateAdd(X, ConstantInt::get(XType, PowerOf2));
1638 APInt Bound =
1639 Pred == ICmpInst::ICMP_ULT ? PowerOf2 << 1 : ((PowerOf2 << 1) - 1);
1640 return new ICmpInst(Pred, Add, ConstantInt::get(XType, Bound));
1641}
1642
1643/// Fold icmp (and (sh X, Y), C2), C1.
1646 const APInt &C1,
1647 const APInt &C2) {
1648 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1649 if (!Shift || !Shift->isShift())
1650 return nullptr;
1651
1652 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1653 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1654 // code produced by the clang front-end, for bitfield access.
1655 // This seemingly simple opportunity to fold away a shift turns out to be
1656 // rather complicated. See PR17827 for details.
1657 unsigned ShiftOpcode = Shift->getOpcode();
1658 bool IsShl = ShiftOpcode == Instruction::Shl;
1659 const APInt *C3;
1660 if (match(Shift->getOperand(1), m_APInt(C3))) {
1661 APInt NewAndCst, NewCmpCst;
1662 bool AnyCmpCstBitsShiftedOut;
1663 if (ShiftOpcode == Instruction::Shl) {
1664 // For a left shift, we can fold if the comparison is not signed. We can
1665 // also fold a signed comparison if the mask value and comparison value
1666 // are not negative. These constraints may not be obvious, but we can
1667 // prove that they are correct using an SMT solver.
1668 if (Cmp.isSigned() && (C2.isNegative() || C1.isNegative()))
1669 return nullptr;
1670
1671 NewCmpCst = C1.lshr(*C3);
1672 NewAndCst = C2.lshr(*C3);
1673 AnyCmpCstBitsShiftedOut = NewCmpCst.shl(*C3) != C1;
1674 } else if (ShiftOpcode == Instruction::LShr) {
1675 // For a logical right shift, we can fold if the comparison is not signed.
1676 // We can also fold a signed comparison if the shifted mask value and the
1677 // shifted comparison value are not negative. These constraints may not be
1678 // obvious, but we can prove that they are correct using an SMT solver.
1679 NewCmpCst = C1.shl(*C3);
1680 NewAndCst = C2.shl(*C3);
1681 AnyCmpCstBitsShiftedOut = NewCmpCst.lshr(*C3) != C1;
1682 if (Cmp.isSigned() && (NewAndCst.isNegative() || NewCmpCst.isNegative()))
1683 return nullptr;
1684 } else {
1685 // For an arithmetic shift, check that both constants don't use (in a
1686 // signed sense) the top bits being shifted out.
1687 assert(ShiftOpcode == Instruction::AShr && "Unknown shift opcode");
1688 NewCmpCst = C1.shl(*C3);
1689 NewAndCst = C2.shl(*C3);
1690 AnyCmpCstBitsShiftedOut = NewCmpCst.ashr(*C3) != C1;
1691 if (NewAndCst.ashr(*C3) != C2)
1692 return nullptr;
1693 }
1694
1695 if (AnyCmpCstBitsShiftedOut) {
1696 // If we shifted bits out, the fold is not going to work out. As a
1697 // special case, check to see if this means that the result is always
1698 // true or false now.
1699 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1700 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1701 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1702 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1703 } else {
1704 Value *NewAnd = Builder.CreateAnd(
1705 Shift->getOperand(0), ConstantInt::get(And->getType(), NewAndCst));
1706 return new ICmpInst(Cmp.getPredicate(),
1707 NewAnd, ConstantInt::get(And->getType(), NewCmpCst));
1708 }
1709 }
1710
1711 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1712 // preferable because it allows the C2 << Y expression to be hoisted out of a
1713 // loop if Y is invariant and X is not.
1714 if (Shift->hasOneUse() && C1.isZero() && Cmp.isEquality() &&
1715 !Shift->isArithmeticShift() && !isa<Constant>(Shift->getOperand(0))) {
1716 // Compute C2 << Y.
1717 Value *NewShift =
1718 IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1))
1719 : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1));
1720
1721 // Compute X & (C2 << Y).
1722 Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1723 return replaceOperand(Cmp, 0, NewAnd);
1724 }
1725
1726 return nullptr;
1727}
1728
1729/// Fold icmp (and X, C2), C1.
1732 const APInt &C1) {
1733 bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1734
1735 // For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
1736 // TODO: We canonicalize to the longer form for scalars because we have
1737 // better analysis/folds for icmp, and codegen may be better with icmp.
1738 if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&
1739 match(And->getOperand(1), m_One()))
1740 return new TruncInst(And->getOperand(0), Cmp.getType());
1741
1742 const APInt *C2;
1743 Value *X;
1744 if (!match(And, m_And(m_Value(X), m_APInt(C2))))
1745 return nullptr;
1746
1747 // Don't perform the following transforms if the AND has multiple uses
1748 if (!And->hasOneUse())
1749 return nullptr;
1750
1751 if (Cmp.isEquality() && C1.isZero()) {
1752 // Restrict this fold to single-use 'and' (PR10267).
1753 // Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1754 if (C2->isSignMask()) {
1755 Constant *Zero = Constant::getNullValue(X->getType());
1756 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1757 return new ICmpInst(NewPred, X, Zero);
1758 }
1759
1760 APInt NewC2 = *C2;
1761 KnownBits Know = computeKnownBits(And->getOperand(0), 0, And);
1762 // Set high zeros of C2 to allow matching negated power-of-2.
1763 NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
1764 Know.countMinLeadingZeros());
1765
1766 // Restrict this fold only for single-use 'and' (PR10267).
1767 // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.
1768 if (NewC2.isNegatedPowerOf2()) {
1769 Constant *NegBOC = ConstantInt::get(And->getType(), -NewC2);
1770 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1771 return new ICmpInst(NewPred, X, NegBOC);
1772 }
1773 }
1774
1775 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1776 // the input width without changing the value produced, eliminate the cast:
1777 //
1778 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1779 //
1780 // We can do this transformation if the constants do not have their sign bits
1781 // set or if it is an equality comparison. Extending a relational comparison
1782 // when we're checking the sign bit would not work.
1783 Value *W;
1784 if (match(And->getOperand(0), m_OneUse(m_Trunc(m_Value(W)))) &&
1785 (Cmp.isEquality() || (!C1.isNegative() && !C2->isNegative()))) {
1786 // TODO: Is this a good transform for vectors? Wider types may reduce
1787 // throughput. Should this transform be limited (even for scalars) by using
1788 // shouldChangeType()?
1789 if (!Cmp.getType()->isVectorTy()) {
1790 Type *WideType = W->getType();
1791 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1792 Constant *ZextC1 = ConstantInt::get(WideType, C1.zext(WideScalarBits));
1793 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1794 Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName());
1795 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1796 }
1797 }
1798
1799 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, *C2))
1800 return I;
1801
1802 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1803 // (icmp pred (and A, (or (shl 1, B), 1), 0))
1804 //
1805 // iff pred isn't signed
1806 if (!Cmp.isSigned() && C1.isZero() && And->getOperand(0)->hasOneUse() &&
1807 match(And->getOperand(1), m_One())) {
1808 Constant *One = cast<Constant>(And->getOperand(1));
1809 Value *Or = And->getOperand(0);
1810 Value *A, *B, *LShr;
1811 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1812 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1813 unsigned UsesRemoved = 0;
1814 if (And->hasOneUse())
1815 ++UsesRemoved;
1816 if (Or->hasOneUse())
1817 ++UsesRemoved;
1818 if (LShr->hasOneUse())
1819 ++UsesRemoved;
1820
1821 // Compute A & ((1 << B) | 1)
1822 unsigned RequireUsesRemoved = match(B, m_ImmConstant()) ? 1 : 3;
1823 if (UsesRemoved >= RequireUsesRemoved) {
1824 Value *NewOr =
1825 Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(),
1826 /*HasNUW=*/true),
1827 One, Or->getName());
1828 Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1829 return replaceOperand(Cmp, 0, NewAnd);
1830 }
1831 }
1832 }
1833
1834 // (icmp eq (and (bitcast X to int), ExponentMask), ExponentMask) -->
1835 // llvm.is.fpclass(X, fcInf|fcNan)
1836 // (icmp ne (and (bitcast X to int), ExponentMask), ExponentMask) -->
1837 // llvm.is.fpclass(X, ~(fcInf|fcNan))
1838 Value *V;
1839 if (!Cmp.getParent()->getParent()->hasFnAttribute(
1840 Attribute::NoImplicitFloat) &&
1841 Cmp.isEquality() &&
1843 Type *FPType = V->getType()->getScalarType();
1844 if (FPType->isIEEELikeFPTy() && C1 == *C2) {
1845 APInt ExponentMask =
1847 if (C1 == ExponentMask) {
1848 unsigned Mask = FPClassTest::fcNan | FPClassTest::fcInf;
1849 if (isICMP_NE)
1850 Mask = ~Mask & fcAllFlags;
1851 return replaceInstUsesWith(Cmp, Builder.createIsFPClass(V, Mask));
1852 }
1853 }
1854 }
1855
1856 return nullptr;
1857}
1858
1859/// Fold icmp (and X, Y), C.
1862 const APInt &C) {
1863 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1864 return I;
1865
1866 const ICmpInst::Predicate Pred = Cmp.getPredicate();
1867 bool TrueIfNeg;
1868 if (isSignBitCheck(Pred, C, TrueIfNeg)) {
1869 // ((X - 1) & ~X) < 0 --> X == 0
1870 // ((X - 1) & ~X) >= 0 --> X != 0
1871 Value *X;
1872 if (match(And->getOperand(0), m_Add(m_Value(X), m_AllOnes())) &&
1873 match(And->getOperand(1), m_Not(m_Specific(X)))) {
1874 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1875 return new ICmpInst(NewPred, X, ConstantInt::getNullValue(X->getType()));
1876 }
1877 // (X & -X) < 0 --> X == MinSignedC
1878 // (X & -X) > -1 --> X != MinSignedC
1879 if (match(And, m_c_And(m_Neg(m_Value(X)), m_Deferred(X)))) {
1880 Constant *MinSignedC = ConstantInt::get(
1881 X->getType(),
1882 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
1883 auto NewPred = TrueIfNeg ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1884 return new ICmpInst(NewPred, X, MinSignedC);
1885 }
1886 }
1887
1888 // TODO: These all require that Y is constant too, so refactor with the above.
1889
1890 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1891 Value *X = And->getOperand(0);
1892 Value *Y = And->getOperand(1);
1893 if (auto *C2 = dyn_cast<ConstantInt>(Y))
1894 if (auto *LI = dyn_cast<LoadInst>(X))
1895 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1896 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1897 if (Instruction *Res =
1898 foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2))
1899 return Res;
1900
1901 if (!Cmp.isEquality())
1902 return nullptr;
1903
1904 // X & -C == -C -> X > u ~C
1905 // X & -C != -C -> X <= u ~C
1906 // iff C is a power of 2
1907 if (Cmp.getOperand(1) == Y && C.isNegatedPowerOf2()) {
1908 auto NewPred =
1910 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1911 }
1912
1913 // If we are testing the intersection of 2 select-of-nonzero-constants with no
1914 // common bits set, it's the same as checking if exactly one select condition
1915 // is set:
1916 // ((A ? TC : FC) & (B ? TC : FC)) == 0 --> xor A, B
1917 // ((A ? TC : FC) & (B ? TC : FC)) != 0 --> not(xor A, B)
1918 // TODO: Generalize for non-constant values.
1919 // TODO: Handle signed/unsigned predicates.
1920 // TODO: Handle other bitwise logic connectors.
1921 // TODO: Extend to handle a non-zero compare constant.
1922 if (C.isZero() && (Pred == CmpInst::ICMP_EQ || And->hasOneUse())) {
1923 assert(Cmp.isEquality() && "Not expecting non-equality predicates");
1924 Value *A, *B;
1925 const APInt *TC, *FC;
1926 if (match(X, m_Select(m_Value(A), m_APInt(TC), m_APInt(FC))) &&
1927 match(Y,
1928 m_Select(m_Value(B), m_SpecificInt(*TC), m_SpecificInt(*FC))) &&
1929 !TC->isZero() && !FC->isZero() && !TC->intersects(*FC)) {
1930 Value *R = Builder.CreateXor(A, B);
1931 if (Pred == CmpInst::ICMP_NE)
1932 R = Builder.CreateNot(R);
1933 return replaceInstUsesWith(Cmp, R);
1934 }
1935 }
1936
1937 // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
1938 // ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1939 // ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1940 // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
1942 X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
1943 Value *TruncY = Builder.CreateTrunc(Y, X->getType());
1944 if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
1945 Value *And = Builder.CreateAnd(TruncY, X);
1947 }
1948 return BinaryOperator::CreateAnd(TruncY, X);
1949 }
1950
1951 // (icmp eq/ne (and (shl -1, X), Y), 0)
1952 // -> (icmp eq/ne (lshr Y, X), 0)
1953 // We could technically handle any C == 0 or (C < 0 && isOdd(C)) but it seems
1954 // highly unlikely the non-zero case will ever show up in code.
1955 if (C.isZero() &&
1957 m_Value(Y))))) {
1958 Value *LShr = Builder.CreateLShr(Y, X);
1959 return new ICmpInst(Pred, LShr, Constant::getNullValue(LShr->getType()));
1960 }
1961
1962 return nullptr;
1963}
1964
1965/// Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
1967 InstCombiner::BuilderTy &Builder) {
1968 // Are we using xors or subs to bitwise check for a pair or pairs of
1969 // (in)equalities? Convert to a shorter form that has more potential to be
1970 // folded even further.
1971 // ((X1 ^/- X2) || (X3 ^/- X4)) == 0 --> (X1 == X2) && (X3 == X4)
1972 // ((X1 ^/- X2) || (X3 ^/- X4)) != 0 --> (X1 != X2) || (X3 != X4)
1973 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) == 0 -->
1974 // (X1 == X2) && (X3 == X4) && (X5 == X6)
1975 // ((X1 ^/- X2) || (X3 ^/- X4) || (X5 ^/- X6)) != 0 -->
1976 // (X1 != X2) || (X3 != X4) || (X5 != X6)
1978 SmallVector<Value *, 16> WorkList(1, Or);
1979
1980 while (!WorkList.empty()) {
1981 auto MatchOrOperatorArgument = [&](Value *OrOperatorArgument) {
1982 Value *Lhs, *Rhs;
1983
1984 if (match(OrOperatorArgument,
1985 m_OneUse(m_Xor(m_Value(Lhs), m_Value(Rhs))))) {
1986 CmpValues.emplace_back(Lhs, Rhs);
1987 return;
1988 }
1989
1990 if (match(OrOperatorArgument,
1991 m_OneUse(m_Sub(m_Value(Lhs), m_Value(Rhs))))) {
1992 CmpValues.emplace_back(Lhs, Rhs);
1993 return;
1994 }
1995
1996 WorkList.push_back(OrOperatorArgument);
1997 };
1998
1999 Value *CurrentValue = WorkList.pop_back_val();
2000 Value *OrOperatorLhs, *OrOperatorRhs;
2001
2002 if (!match(CurrentValue,
2003 m_Or(m_Value(OrOperatorLhs), m_Value(OrOperatorRhs)))) {
2004 return nullptr;
2005 }
2006
2007 MatchOrOperatorArgument(OrOperatorRhs);
2008 MatchOrOperatorArgument(OrOperatorLhs);
2009 }
2010
2011 ICmpInst::Predicate Pred = Cmp.getPredicate();
2012 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2013 Value *LhsCmp = Builder.CreateICmp(Pred, CmpValues.rbegin()->first,
2014 CmpValues.rbegin()->second);
2015
2016 for (auto It = CmpValues.rbegin() + 1; It != CmpValues.rend(); ++It) {
2017 Value *RhsCmp = Builder.CreateICmp(Pred, It->first, It->second);
2018 LhsCmp = Builder.CreateBinOp(BOpc, LhsCmp, RhsCmp);
2019 }
2020
2021 return LhsCmp;
2022}
2023
2024/// Fold icmp (or X, Y), C.
2027 const APInt &C) {
2028 ICmpInst::Predicate Pred = Cmp.getPredicate();
2029 if (C.isOne()) {
2030 // icmp slt signum(V) 1 --> icmp slt V, 1
2031 Value *V = nullptr;
2032 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
2033 return new ICmpInst(ICmpInst::ICMP_SLT, V,
2034 ConstantInt::get(V->getType(), 1));
2035 }
2036
2037 Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
2038
2039 // (icmp eq/ne (or disjoint x, C0), C1)
2040 // -> (icmp eq/ne x, C0^C1)
2041 if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
2042 cast<PossiblyDisjointInst>(Or)->isDisjoint()) {
2043 Value *NewC =
2044 Builder.CreateXor(OrOp1, ConstantInt::get(OrOp1->getType(), C));
2045 return new ICmpInst(Pred, OrOp0, NewC);
2046 }
2047
2048 const APInt *MaskC;
2049 if (match(OrOp1, m_APInt(MaskC)) && Cmp.isEquality()) {
2050 if (*MaskC == C && (C + 1).isPowerOf2()) {
2051 // X | C == C --> X <=u C
2052 // X | C != C --> X >u C
2053 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
2055 return new ICmpInst(Pred, OrOp0, OrOp1);
2056 }
2057
2058 // More general: canonicalize 'equality with set bits mask' to
2059 // 'equality with clear bits mask'.
2060 // (X | MaskC) == C --> (X & ~MaskC) == C ^ MaskC
2061 // (X | MaskC) != C --> (X & ~MaskC) != C ^ MaskC
2062 if (Or->hasOneUse()) {
2063 Value *And = Builder.CreateAnd(OrOp0, ~(*MaskC));
2064 Constant *NewC = ConstantInt::get(Or->getType(), C ^ (*MaskC));
2065 return new ICmpInst(Pred, And, NewC);
2066 }
2067 }
2068
2069 // (X | (X-1)) s< 0 --> X s< 1
2070 // (X | (X-1)) s> -1 --> X s> 0
2071 Value *X;
2072 bool TrueIfSigned;
2073 if (isSignBitCheck(Pred, C, TrueIfSigned) &&
2075 auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
2076 Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
2077 return new ICmpInst(NewPred, X, NewC);
2078 }
2079
2080 const APInt *OrC;
2081 // icmp(X | OrC, C) --> icmp(X, 0)
2082 if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) {
2083 switch (Pred) {
2084 // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0
2085 case ICmpInst::ICMP_SLT:
2086 // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0
2087 case ICmpInst::ICMP_SGE:
2088 if (OrC->sge(C))
2089 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
2090 break;
2091 // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0
2092 case ICmpInst::ICMP_SLE:
2093 // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0
2094 case ICmpInst::ICMP_SGT:
2095 if (OrC->sgt(C))
2097 ConstantInt::getNullValue(X->getType()));
2098 break;
2099 default:
2100 break;
2101 }
2102 }
2103
2104 if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
2105 return nullptr;
2106
2107 Value *P, *Q;
2109 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
2110 // -> and (icmp eq P, null), (icmp eq Q, null).
2111 Value *CmpP =
2112 Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
2113 Value *CmpQ =
2115 auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2116 return BinaryOperator::Create(BOpc, CmpP, CmpQ);
2117 }
2118
2119 if (Value *V = foldICmpOrXorSubChain(Cmp, Or, Builder))
2120 return replaceInstUsesWith(Cmp, V);
2121
2122 return nullptr;
2123}
2124
2125/// Fold icmp (mul X, Y), C.
2128 const APInt &C) {
2129 ICmpInst::Predicate Pred = Cmp.getPredicate();
2130 Type *MulTy = Mul->getType();
2131 Value *X = Mul->getOperand(0);
2132
2133 // If there's no overflow:
2134 // X * X == 0 --> X == 0
2135 // X * X != 0 --> X != 0
2136 if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
2137 (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2138 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2139
2140 const APInt *MulC;
2141 if (!match(Mul->getOperand(1), m_APInt(MulC)))
2142 return nullptr;
2143
2144 // If this is a test of the sign bit and the multiply is sign-preserving with
2145 // a constant operand, use the multiply LHS operand instead:
2146 // (X * +MulC) < 0 --> X < 0
2147 // (X * -MulC) < 0 --> X > 0
2148 if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
2149 if (MulC->isNegative())
2150 Pred = ICmpInst::getSwappedPredicate(Pred);
2151 return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
2152 }
2153
2154 if (MulC->isZero())
2155 return nullptr;
2156
2157 // If the multiply does not wrap or the constant is odd, try to divide the
2158 // compare constant by the multiplication factor.
2159 if (Cmp.isEquality()) {
2160 // (mul nsw X, MulC) eq/ne C --> X eq/ne C /s MulC
2161 if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2162 Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2163 return new ICmpInst(Pred, X, NewC);
2164 }
2165
2166 // C % MulC == 0 is weaker than we could use if MulC is odd because it
2167 // correct to transform if MulC * N == C including overflow. I.e with i8
2168 // (icmp eq (mul X, 5), 101) -> (icmp eq X, 225) but since 101 % 5 != 0, we
2169 // miss that case.
2170 if (C.urem(*MulC).isZero()) {
2171 // (mul nuw X, MulC) eq/ne C --> X eq/ne C /u MulC
2172 // (mul X, OddC) eq/ne N * C --> X eq/ne N
2173 if ((*MulC & 1).isOne() || Mul->hasNoUnsignedWrap()) {
2174 Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2175 return new ICmpInst(Pred, X, NewC);
2176 }
2177 }
2178 }
2179
2180 // With a matching no-overflow guarantee, fold the constants:
2181 // (X * MulC) < C --> X < (C / MulC)
2182 // (X * MulC) > C --> X > (C / MulC)
2183 // TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
2184 Constant *NewC = nullptr;
2185 if (Mul->hasNoSignedWrap() && ICmpInst::isSigned(Pred)) {
2186 // MININT / -1 --> overflow.
2187 if (C.isMinSignedValue() && MulC->isAllOnes())
2188 return nullptr;
2189 if (MulC->isNegative())
2190 Pred = ICmpInst::getSwappedPredicate(Pred);
2191
2192 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2193 NewC = ConstantInt::get(
2195 } else {
2196 assert((Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT) &&
2197 "Unexpected predicate");
2198 NewC = ConstantInt::get(
2200 }
2201 } else if (Mul->hasNoUnsignedWrap() && ICmpInst::isUnsigned(Pred)) {
2202 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) {
2203 NewC = ConstantInt::get(
2205 } else {
2206 assert((Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
2207 "Unexpected predicate");
2208 NewC = ConstantInt::get(
2210 }
2211 }
2212
2213 return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
2214}
2215
2216/// Fold icmp (shl 1, Y), C.
2218 const APInt &C) {
2219 Value *Y;
2220 if (!match(Shl, m_Shl(m_One(), m_Value(Y))))
2221 return nullptr;
2222
2223 Type *ShiftType = Shl->getType();
2224 unsigned TypeBits = C.getBitWidth();
2225 bool CIsPowerOf2 = C.isPowerOf2();
2226 ICmpInst::Predicate Pred = Cmp.getPredicate();
2227 if (Cmp.isUnsigned()) {
2228 // (1 << Y) pred C -> Y pred Log2(C)
2229 if (!CIsPowerOf2) {
2230 // (1 << Y) < 30 -> Y <= 4
2231 // (1 << Y) <= 30 -> Y <= 4
2232 // (1 << Y) >= 30 -> Y > 4
2233 // (1 << Y) > 30 -> Y > 4
2234 if (Pred == ICmpInst::ICMP_ULT)
2235 Pred = ICmpInst::ICMP_ULE;
2236 else if (Pred == ICmpInst::ICMP_UGE)
2237 Pred = ICmpInst::ICMP_UGT;
2238 }
2239
2240 unsigned CLog2 = C.logBase2();
2241 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2242 } else if (Cmp.isSigned()) {
2243 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
2244 // (1 << Y) > 0 -> Y != 31
2245 // (1 << Y) > C -> Y != 31 if C is negative.
2246 if (Pred == ICmpInst::ICMP_SGT && C.sle(0))
2247 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
2248
2249 // (1 << Y) < 0 -> Y == 31
2250 // (1 << Y) < 1 -> Y == 31
2251 // (1 << Y) < C -> Y == 31 if C is negative and not signed min.
2252 // Exclude signed min by subtracting 1 and lower the upper bound to 0.
2253 if (Pred == ICmpInst::ICMP_SLT && (C-1).sle(0))
2254 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
2255 }
2256
2257 return nullptr;
2258}
2259
2260/// Fold icmp (shl X, Y), C.
2262 BinaryOperator *Shl,
2263 const APInt &C) {
2264 const APInt *ShiftVal;
2265 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
2266 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), C, *ShiftVal);
2267
2268 ICmpInst::Predicate Pred = Cmp.getPredicate();
2269 // (icmp pred (shl nuw&nsw X, Y), Csle0)
2270 // -> (icmp pred X, Csle0)
2271 //
2272 // The idea is the nuw/nsw essentially freeze the sign bit for the shift op
2273 // so X's must be what is used.
2274 if (C.sle(0) && Shl->hasNoUnsignedWrap() && Shl->hasNoSignedWrap())
2275 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2276
2277 // (icmp eq/ne (shl nuw|nsw X, Y), 0)
2278 // -> (icmp eq/ne X, 0)
2279 if (ICmpInst::isEquality(Pred) && C.isZero() &&
2280 (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap()))
2281 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2282
2283 // (icmp slt (shl nsw X, Y), 0/1)
2284 // -> (icmp slt X, 0/1)
2285 // (icmp sgt (shl nsw X, Y), 0/-1)
2286 // -> (icmp sgt X, 0/-1)
2287 //
2288 // NB: sge/sle with a constant will canonicalize to sgt/slt.
2289 if (Shl->hasNoSignedWrap() &&
2290 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT))
2291 if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
2292 return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
2293
2294 const APInt *ShiftAmt;
2295 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2296 return foldICmpShlOne(Cmp, Shl, C);
2297
2298 // Check that the shift amount is in range. If not, don't perform undefined
2299 // shifts. When the shift is visited, it will be simplified.
2300 unsigned TypeBits = C.getBitWidth();
2301 if (ShiftAmt->uge(TypeBits))
2302 return nullptr;
2303
2304 Value *X = Shl->getOperand(0);
2305 Type *ShType = Shl->getType();
2306
2307 // NSW guarantees that we are only shifting out sign bits from the high bits,
2308 // so we can ASHR the compare constant without needing a mask and eliminate
2309 // the shift.
2310 if (Shl->hasNoSignedWrap()) {
2311 if (Pred == ICmpInst::ICMP_SGT) {
2312 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
2313 APInt ShiftedC = C.ashr(*ShiftAmt);
2314 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2315 }
2316 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2317 C.ashr(*ShiftAmt).shl(*ShiftAmt) == C) {
2318 APInt ShiftedC = C.ashr(*ShiftAmt);
2319 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2320 }
2321 if (Pred == ICmpInst::ICMP_SLT) {
2322 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
2323 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
2324 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
2325 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
2326 assert(!C.isMinSignedValue() && "Unexpected icmp slt");
2327 APInt ShiftedC = (C - 1).ashr(*ShiftAmt) + 1;
2328 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2329 }
2330 }
2331
2332 // NUW guarantees that we are only shifting out zero bits from the high bits,
2333 // so we can LSHR the compare constant without needing a mask and eliminate
2334 // the shift.
2335 if (Shl->hasNoUnsignedWrap()) {
2336 if (Pred == ICmpInst::ICMP_UGT) {
2337 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
2338 APInt ShiftedC = C.lshr(*ShiftAmt);
2339 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2340 }
2341 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2342 C.lshr(*ShiftAmt).shl(*ShiftAmt) == C) {
2343 APInt ShiftedC = C.lshr(*ShiftAmt);
2344 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2345 }
2346 if (Pred == ICmpInst::ICMP_ULT) {
2347 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
2348 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
2349 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
2350 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
2351 assert(C.ugt(0) && "ult 0 should have been eliminated");
2352 APInt ShiftedC = (C - 1).lshr(*ShiftAmt) + 1;
2353 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
2354 }
2355 }
2356
2357 if (Cmp.isEquality() && Shl->hasOneUse()) {
2358 // Strength-reduce the shift into an 'and'.
2359 Constant *Mask = ConstantInt::get(
2360 ShType,
2361 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
2362 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2363 Constant *LShrC = ConstantInt::get(ShType, C.lshr(*ShiftAmt));
2364 return new ICmpInst(Pred, And, LShrC);
2365 }
2366
2367 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2368 bool TrueIfSigned = false;
2369 if (Shl->hasOneUse() && isSignBitCheck(Pred, C, TrueIfSigned)) {
2370 // (X << 31) <s 0 --> (X & 1) != 0
2371 Constant *Mask = ConstantInt::get(
2372 ShType,
2373 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
2374 Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask");
2375 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
2376 And, Constant::getNullValue(ShType));
2377 }
2378
2379 // Simplify 'shl' inequality test into 'and' equality test.
2380 if (Cmp.isUnsigned() && Shl->hasOneUse()) {
2381 // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0
2382 if ((C + 1).isPowerOf2() &&
2383 (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) {
2384 Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue()));
2385 return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ
2387 And, Constant::getNullValue(ShType));
2388 }
2389 // (X l<< C2) u</u>= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0
2390 if (C.isPowerOf2() &&
2391 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
2392 Value *And =
2393 Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue()));
2394 return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ
2396 And, Constant::getNullValue(ShType));
2397 }
2398 }
2399
2400 // Transform (icmp pred iM (shl iM %v, N), C)
2401 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2402 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
2403 // This enables us to get rid of the shift in favor of a trunc that may be
2404 // free on the target. It has the additional benefit of comparing to a
2405 // smaller constant that may be more target-friendly.
2406 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
2407 if (Shl->hasOneUse() && Amt != 0 && C.countr_zero() >= Amt &&
2408 DL.isLegalInteger(TypeBits - Amt)) {
2409 Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt);
2410 if (auto *ShVTy = dyn_cast<VectorType>(ShType))
2411 TruncTy = VectorType::get(TruncTy, ShVTy->getElementCount());
2412 Constant *NewC =
2413 ConstantInt::get(TruncTy, C.ashr(*ShiftAmt).trunc(TypeBits - Amt));
2414 return new ICmpInst(Pred, Builder.CreateTrunc(X, TruncTy), NewC);
2415 }
2416
2417 return nullptr;
2418}
2419
2420/// Fold icmp ({al}shr X, Y), C.
2422 BinaryOperator *Shr,
2423 const APInt &C) {
2424 // An exact shr only shifts out zero bits, so:
2425 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2426 Value *X = Shr->getOperand(0);
2427 CmpInst::Predicate Pred = Cmp.getPredicate();
2428 if (Cmp.isEquality() && Shr->isExact() && C.isZero())
2429 return new ICmpInst(Pred, X, Cmp.getOperand(1));
2430
2431 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2432 const APInt *ShiftValC;
2433 if (match(X, m_APInt(ShiftValC))) {
2434 if (Cmp.isEquality())
2435 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
2436
2437 // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
2438 // (ShiftValC >> Y) <s 0 --> Y == 0 with ShiftValC < 0
2439 bool TrueIfSigned;
2440 if (!IsAShr && ShiftValC->isNegative() &&
2441 isSignBitCheck(Pred, C, TrueIfSigned))
2442 return new ICmpInst(TrueIfSigned ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE,
2443 Shr->getOperand(1),
2444 ConstantInt::getNullValue(X->getType()));
2445
2446 // If the shifted constant is a power-of-2, test the shift amount directly:
2447 // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
2448 // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
2449 if (!IsAShr && ShiftValC->isPowerOf2() &&
2450 (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
2451 bool IsUGT = Pred == CmpInst::ICMP_UGT;
2452 assert(ShiftValC->uge(C) && "Expected simplify of compare");
2453 assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
2454
2455 unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
2456 unsigned ShiftLZ = ShiftValC->countl_zero();
2457 Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
2458 auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
2459 return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
2460 }
2461 }
2462
2463 const APInt *ShiftAmtC;
2464 if (!match(Shr->getOperand(1), m_APInt(ShiftAmtC)))
2465 return nullptr;
2466
2467 // Check that the shift amount is in range. If not, don't perform undefined
2468 // shifts. When the shift is visited it will be simplified.
2469 unsigned TypeBits = C.getBitWidth();
2470 unsigned ShAmtVal = ShiftAmtC->getLimitedValue(TypeBits);
2471 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2472 return nullptr;
2473
2474 bool IsExact = Shr->isExact();
2475 Type *ShrTy = Shr->getType();
2476 // TODO: If we could guarantee that InstSimplify would handle all of the
2477 // constant-value-based preconditions in the folds below, then we could assert
2478 // those conditions rather than checking them. This is difficult because of
2479 // undef/poison (PR34838).
2480 if (IsAShr && Shr->hasOneUse()) {
2481 if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
2482 // When ShAmtC can be shifted losslessly:
2483 // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
2484 // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC)
2485 APInt ShiftedC = C.shl(ShAmtVal);
2486 if (ShiftedC.ashr(ShAmtVal) == C)
2487 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2488 }
2489 if (Pred == CmpInst::ICMP_SGT) {
2490 // icmp sgt (ashr X, ShAmtC), C --> icmp sgt X, ((C + 1) << ShAmtC) - 1
2491 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2492 if (!C.isMaxSignedValue() && !(C + 1).shl(ShAmtVal).isMinSignedValue() &&
2493 (ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
2494 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2495 }
2496 if (Pred == CmpInst::ICMP_UGT) {
2497 // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2498 // 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
2499 // clause accounts for that pattern.
2500 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2501 if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
2502 (C + 1).shl(ShAmtVal).isMinSignedValue())
2503 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2504 }
2505
2506 // If the compare constant has significant bits above the lowest sign-bit,
2507 // then convert an unsigned cmp to a test of the sign-bit:
2508 // (ashr X, ShiftC) u> C --> X s< 0
2509 // (ashr X, ShiftC) u< C --> X s> -1
2510 if (C.getBitWidth() > 2 && C.getNumSignBits() <= ShAmtVal) {
2511 if (Pred == CmpInst::ICMP_UGT) {
2512 return new ICmpInst(CmpInst::ICMP_SLT, X,
2514 }
2515 if (Pred == CmpInst::ICMP_ULT) {
2516 return new ICmpInst(CmpInst::ICMP_SGT, X,
2518 }
2519 }
2520 } else if (!IsAShr) {
2521 if (Pred == CmpInst::ICMP_ULT || (Pred == CmpInst::ICMP_UGT && IsExact)) {
2522 // icmp ult (lshr X, ShAmtC), C --> icmp ult X, (C << ShAmtC)
2523 // icmp ugt (lshr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
2524 APInt ShiftedC = C.shl(ShAmtVal);
2525 if (ShiftedC.lshr(ShAmtVal) == C)
2526 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2527 }
2528 if (Pred == CmpInst::ICMP_UGT) {
2529 // icmp ugt (lshr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
2530 APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
2531 if ((ShiftedC + 1).lshr(ShAmtVal) == (C + 1))
2532 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
2533 }
2534 }
2535
2536 if (!Cmp.isEquality())
2537 return nullptr;
2538
2539 // Handle equality comparisons of shift-by-constant.
2540
2541 // If the comparison constant changes with the shift, the comparison cannot
2542 // succeed (bits of the comparison constant cannot match the shifted value).
2543 // This should be known by InstSimplify and already be folded to true/false.
2544 assert(((IsAShr && C.shl(ShAmtVal).ashr(ShAmtVal) == C) ||
2545 (!IsAShr && C.shl(ShAmtVal).lshr(ShAmtVal) == C)) &&
2546 "Expected icmp+shr simplify did not occur.");
2547
2548 // If the bits shifted out are known zero, compare the unshifted value:
2549 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
2550 if (Shr->isExact())
2551 return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, C << ShAmtVal));
2552
2553 if (C.isZero()) {
2554 // == 0 is u< 1.
2555 if (Pred == CmpInst::ICMP_EQ)
2556 return new ICmpInst(CmpInst::ICMP_ULT, X,
2557 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal)));
2558 else
2559 return new ICmpInst(CmpInst::ICMP_UGT, X,
2560 ConstantInt::get(ShrTy, (C + 1).shl(ShAmtVal) - 1));
2561 }
2562
2563 if (Shr->hasOneUse()) {
2564 // Canonicalize the shift into an 'and':
2565 // icmp eq/ne (shr X, ShAmt), C --> icmp eq/ne (and X, HiMask), (C << ShAmt)
2566 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2567 Constant *Mask = ConstantInt::get(ShrTy, Val);
2568 Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask");
2569 return new ICmpInst(Pred, And, ConstantInt::get(ShrTy, C << ShAmtVal));
2570 }
2571
2572 return nullptr;
2573}
2574
2576 BinaryOperator *SRem,
2577 const APInt &C) {
2578 // Match an 'is positive' or 'is negative' comparison of remainder by a
2579 // constant power-of-2 value:
2580 // (X % pow2C) sgt/slt 0
2581 const ICmpInst::Predicate Pred = Cmp.getPredicate();
2582 if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
2583 Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2584 return nullptr;
2585
2586 // TODO: The one-use check is standard because we do not typically want to
2587 // create longer instruction sequences, but this might be a special-case
2588 // because srem is not good for analysis or codegen.
2589 if (!SRem->hasOneUse())
2590 return nullptr;
2591
2592 const APInt *DivisorC;
2593 if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
2594 return nullptr;
2595
2596 // For cmp_sgt/cmp_slt only zero valued C is handled.
2597 // For cmp_eq/cmp_ne only positive valued C is handled.
2598 if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
2599 !C.isZero()) ||
2600 ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
2601 !C.isStrictlyPositive()))
2602 return nullptr;
2603
2604 // Mask off the sign bit and the modulo bits (low-bits).
2605 Type *Ty = SRem->getType();
2607 Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
2608 Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);
2609
2610 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
2611 return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));
2612
2613 // For 'is positive?' check that the sign-bit is clear and at least 1 masked
2614 // bit is set. Example:
2615 // (i8 X % 32) s> 0 --> (X & 159) s> 0
2616 if (Pred == ICmpInst::ICMP_SGT)
2618
2619 // For 'is negative?' check that the sign-bit is set and at least 1 masked
2620 // bit is set. Example:
2621 // (i16 X % 4) s< 0 --> (X & 32771) u> 32768
2622 return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
2623}
2624
2625/// Fold icmp (udiv X, Y), C.
2627 BinaryOperator *UDiv,
2628 const APInt &C) {
2629 ICmpInst::Predicate Pred = Cmp.getPredicate();
2630 Value *X = UDiv->getOperand(0);
2631 Value *Y = UDiv->getOperand(1);
2632 Type *Ty = UDiv->getType();
2633
2634 const APInt *C2;
2635 if (!match(X, m_APInt(C2)))
2636 return nullptr;
2637
2638 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
2639
2640 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2641 if (Pred == ICmpInst::ICMP_UGT) {
2642 assert(!C.isMaxValue() &&
2643 "icmp ugt X, UINT_MAX should have been simplified already.");
2644 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2645 ConstantInt::get(Ty, C2->udiv(C + 1)));
2646 }
2647
2648 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2649 if (Pred == ICmpInst::ICMP_ULT) {
2650 assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2651 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2652 ConstantInt::get(Ty, C2->udiv(C)));
2653 }
2654
2655 return nullptr;
2656}
2657
2658/// Fold icmp ({su}div X, Y), C.
2660 BinaryOperator *Div,
2661 const APInt &C) {
2662 ICmpInst::Predicate Pred = Cmp.getPredicate();
2663 Value *X = Div->getOperand(0);
2664 Value *Y = Div->getOperand(1);
2665 Type *Ty = Div->getType();
2666 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2667
2668 // If unsigned division and the compare constant is bigger than
2669 // UMAX/2 (negative), there's only one pair of values that satisfies an
2670 // equality check, so eliminate the division:
2671 // (X u/ Y) == C --> (X == C) && (Y == 1)
2672 // (X u/ Y) != C --> (X != C) || (Y != 1)
2673 // Similarly, if signed division and the compare constant is exactly SMIN:
2674 // (X s/ Y) == SMIN --> (X == SMIN) && (Y == 1)
2675 // (X s/ Y) != SMIN --> (X != SMIN) || (Y != 1)
2676 if (Cmp.isEquality() && Div->hasOneUse() && C.isSignBitSet() &&
2677 (!DivIsSigned || C.isMinSignedValue())) {
2678 Value *XBig = Builder.CreateICmp(Pred, X, ConstantInt::get(Ty, C));
2679 Value *YOne = Builder.CreateICmp(Pred, Y, ConstantInt::get(Ty, 1));
2680 auto Logic = Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
2681 return BinaryOperator::Create(Logic, XBig, YOne);
2682 }
2683
2684 // Fold: icmp pred ([us]div X, C2), C -> range test
2685 // Fold this div into the comparison, producing a range check.
2686 // Determine, based on the divide type, what the range is being
2687 // checked. If there is an overflow on the low or high side, remember
2688 // it, otherwise compute the range [low, hi) bounding the new value.
2689 // See: InsertRangeTest above for the kinds of replacements possible.
2690 const APInt *C2;
2691 if (!match(Y, m_APInt(C2)))
2692 return nullptr;
2693
2694 // FIXME: If the operand types don't match the type of the divide
2695 // then don't attempt this transform. The code below doesn't have the
2696 // logic to deal with a signed divide and an unsigned compare (and
2697 // vice versa). This is because (x /s C2) <s C produces different
2698 // results than (x /s C2) <u C or (x /u C2) <s C or even
2699 // (x /u C2) <u C. Simply casting the operands and result won't
2700 // work. :( The if statement below tests that condition and bails
2701 // if it finds it.
2702 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
2703 return nullptr;
2704
2705 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2706 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2707 // division-by-constant cases should be present, we can not assert that they
2708 // have happened before we reach this icmp instruction.
2709 if (C2->isZero() || C2->isOne() || (DivIsSigned && C2->isAllOnes()))
2710 return nullptr;
2711
2712 // Compute Prod = C * C2. We are essentially solving an equation of
2713 // form X / C2 = C. We solve for X by multiplying C2 and C.
2714 // By solving for X, we can turn this into a range check instead of computing
2715 // a divide.
2716 APInt Prod = C * *C2;
2717
2718 // Determine if the product overflows by seeing if the product is not equal to
2719 // the divide. Make sure we do the same kind of divide as in the LHS
2720 // instruction that we're folding.
2721 bool ProdOV = (DivIsSigned ? Prod.sdiv(*C2) : Prod.udiv(*C2)) != C;
2722
2723 // If the division is known to be exact, then there is no remainder from the
2724 // divide, so the covered range size is unit, otherwise it is the divisor.
2725 APInt RangeSize = Div->isExact() ? APInt(C2->getBitWidth(), 1) : *C2;
2726
2727 // Figure out the interval that is being checked. For example, a comparison
2728 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2729 // Compute this interval based on the constants involved and the signedness of
2730 // the compare/divide. This computes a half-open interval, keeping track of
2731 // whether either value in the interval overflows. After analysis each
2732 // overflow variable is set to 0 if it's corresponding bound variable is valid
2733 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2734 int LoOverflow = 0, HiOverflow = 0;
2735 APInt LoBound, HiBound;
2736
2737 if (!DivIsSigned) { // udiv
2738 // e.g. X/5 op 3 --> [15, 20)
2739 LoBound = Prod;
2740 HiOverflow = LoOverflow = ProdOV;
2741 if (!HiOverflow) {
2742 // If this is not an exact divide, then many values in the range collapse
2743 // to the same result value.
2744 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2745 }
2746 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2747 if (C.isZero()) { // (X / pos) op 0
2748 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2749 LoBound = -(RangeSize - 1);
2750 HiBound = RangeSize;
2751 } else if (C.isStrictlyPositive()) { // (X / pos) op pos
2752 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2753 HiOverflow = LoOverflow = ProdOV;
2754 if (!HiOverflow)
2755 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2756 } else { // (X / pos) op neg
2757 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2758 HiBound = Prod + 1;
2759 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2760 if (!LoOverflow) {
2761 APInt DivNeg = -RangeSize;
2762 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2763 }
2764 }
2765 } else if (C2->isNegative()) { // Divisor is < 0.
2766 if (Div->isExact())
2767 RangeSize.negate();
2768 if (C.isZero()) { // (X / neg) op 0
2769 // e.g. X/-5 op 0 --> [-4, 5)
2770 LoBound = RangeSize + 1;
2771 HiBound = -RangeSize;
2772 if (HiBound == *C2) { // -INTMIN = INTMIN
2773 HiOverflow = 1; // [INTMIN+1, overflow)
2774 HiBound = APInt(); // e.g. X/INTMIN = 0 --> X > INTMIN
2775 }
2776 } else if (C.isStrictlyPositive()) { // (X / neg) op pos
2777 // e.g. X/-5 op 3 --> [-19, -14)
2778 HiBound = Prod + 1;
2779 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2780 if (!LoOverflow)
2781 LoOverflow =
2782 addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1 : 0;
2783 } else { // (X / neg) op neg
2784 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2785 LoOverflow = HiOverflow = ProdOV;
2786 if (!HiOverflow)
2787 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2788 }
2789
2790 // Dividing by a negative swaps the condition. LT <-> GT
2791 Pred = ICmpInst::getSwappedPredicate(Pred);
2792 }
2793
2794 switch (Pred) {
2795 default:
2796 llvm_unreachable("Unhandled icmp predicate!");
2797 case ICmpInst::ICMP_EQ:
2798 if (LoOverflow && HiOverflow)
2799 return replaceInstUsesWith(Cmp, Builder.getFalse());
2800 if (HiOverflow)
2801 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2802 X, ConstantInt::get(Ty, LoBound));
2803 if (LoOverflow)
2804 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2805 X, ConstantInt::get(Ty, HiBound));
2806 return replaceInstUsesWith(
2807 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, true));
2808 case ICmpInst::ICMP_NE:
2809 if (LoOverflow && HiOverflow)
2810 return replaceInstUsesWith(Cmp, Builder.getTrue());
2811 if (HiOverflow)
2812 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
2813 X, ConstantInt::get(Ty, LoBound));
2814 if (LoOverflow)
2815 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
2816 X, ConstantInt::get(Ty, HiBound));
2817 return replaceInstUsesWith(
2818 Cmp, insertRangeTest(X, LoBound, HiBound, DivIsSigned, false));
2819 case ICmpInst::ICMP_ULT:
2820 case ICmpInst::ICMP_SLT:
2821 if (LoOverflow == +1) // Low bound is greater than input range.
2822 return replaceInstUsesWith(Cmp, Builder.getTrue());
2823 if (LoOverflow == -1) // Low bound is less than input range.
2824 return replaceInstUsesWith(Cmp, Builder.getFalse());
2825 return new ICmpInst(Pred, X, ConstantInt::get(Ty, LoBound));
2826 case ICmpInst::ICMP_UGT:
2827 case ICmpInst::ICMP_SGT:
2828 if (HiOverflow == +1) // High bound greater than input range.
2829 return replaceInstUsesWith(Cmp, Builder.getFalse());
2830 if (HiOverflow == -1) // High bound less than input range.
2831 return replaceInstUsesWith(Cmp, Builder.getTrue());
2832 if (Pred == ICmpInst::ICMP_UGT)
2833 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, HiBound));
2834 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, HiBound));
2835 }
2836
2837 return nullptr;
2838}
2839
2840/// Fold icmp (sub X, Y), C.
2842 BinaryOperator *Sub,
2843 const APInt &C) {
2844 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2845 ICmpInst::Predicate Pred = Cmp.getPredicate();
2846 Type *Ty = Sub->getType();
2847
2848 // (SubC - Y) == C) --> Y == (SubC - C)
2849 // (SubC - Y) != C) --> Y != (SubC - C)
2850 Constant *SubC;
2851 if (Cmp.isEquality() && match(X, m_ImmConstant(SubC))) {
2852 return new ICmpInst(Pred, Y,
2853 ConstantExpr::getSub(SubC, ConstantInt::get(Ty, C)));
2854 }
2855
2856 // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
2857 const APInt *C2;
2858 APInt SubResult;
2859 ICmpInst::Predicate SwappedPred = Cmp.getSwappedPredicate();
2860 bool HasNSW = Sub->hasNoSignedWrap();
2861 bool HasNUW = Sub->hasNoUnsignedWrap();
2862 if (match(X, m_APInt(C2)) &&
2863 ((Cmp.isUnsigned() && HasNUW) || (Cmp.isSigned() && HasNSW)) &&
2864 !subWithOverflow(SubResult, *C2, C, Cmp.isSigned()))
2865 return new ICmpInst(SwappedPred, Y, ConstantInt::get(Ty, SubResult));
2866
2867 // X - Y == 0 --> X == Y.
2868 // X - Y != 0 --> X != Y.
2869 // TODO: We allow this with multiple uses as long as the other uses are not
2870 // in phis. The phi use check is guarding against a codegen regression
2871 // for a loop test. If the backend could undo this (and possibly
2872 // subsequent transforms), we would not need this hack.
2873 if (Cmp.isEquality() && C.isZero() &&
2874 none_of((Sub->users()), [](const User *U) { return isa<PHINode>(U); }))
2875 return new ICmpInst(Pred, X, Y);
2876
2877 // The following transforms are only worth it if the only user of the subtract
2878 // is the icmp.
2879 // TODO: This is an artificial restriction for all of the transforms below
2880 // that only need a single replacement icmp. Can these use the phi test
2881 // like the transform above here?
2882 if (!Sub->hasOneUse())
2883 return nullptr;
2884
2885 if (Sub->hasNoSignedWrap()) {
2886 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
2887 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
2888 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
2889
2890 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
2891 if (Pred == ICmpInst::ICMP_SGT && C.isZero())
2892 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
2893
2894 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
2895 if (Pred == ICmpInst::ICMP_SLT && C.isZero())
2896 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
2897
2898 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
2899 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
2900 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
2901 }
2902
2903 if (!match(X, m_APInt(C2)))
2904 return nullptr;
2905
2906 // C2 - Y <u C -> (Y | (C - 1)) == C2
2907 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
2908 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() &&
2909 (*C2 & (C - 1)) == (C - 1))
2910 return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, C - 1), X);
2911
2912 // C2 - Y >u C -> (Y | C) != C2
2913 // iff C2 & C == C and C + 1 is a power of 2
2914 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == C)
2915 return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, C), X);
2916
2917 // We have handled special cases that reduce.
2918 // Canonicalize any remaining sub to add as:
2919 // (C2 - Y) > C --> (Y + ~C2) < ~C
2920 Value *Add = Builder.CreateAdd(Y, ConstantInt::get(Ty, ~(*C2)), "notsub",
2921 HasNUW, HasNSW);
2922 return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
2923}
2924
2925static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
2926 Value *Op1, IRBuilderBase &Builder,
2927 bool HasOneUse) {
2928 auto FoldConstant = [&](bool Val) {
2929 Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
2930 if (Op0->getType()->isVectorTy())
2932 cast<VectorType>(Op0->getType())->getElementCount(), Res);
2933 return Res;
2934 };
2935
2936 switch (Table.to_ulong()) {
2937 case 0: // 0 0 0 0
2938 return FoldConstant(false);
2939 case 1: // 0 0 0 1
2940 return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
2941 case 2: // 0 0 1 0
2942 return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
2943 case 3: // 0 0 1 1
2944 return Builder.CreateNot(Op0);
2945 case 4: // 0 1 0 0
2946 return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
2947 case 5: // 0 1 0 1
2948 return Builder.CreateNot(Op1);
2949 case 6: // 0 1 1 0
2950 return Builder.CreateXor(Op0, Op1);
2951 case 7: // 0 1 1 1
2952 return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
2953 case 8: // 1 0 0 0
2954 return Builder.CreateAnd(Op0, Op1);
2955 case 9: // 1 0 0 1
2956 return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
2957 case 10: // 1 0 1 0
2958 return Op1;
2959 case 11: // 1 0 1 1
2960 return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
2961 case 12: // 1 1 0 0
2962 return Op0;
2963 case 13: // 1 1 0 1
2964 return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
2965 case 14: // 1 1 1 0
2966 return Builder.CreateOr(Op0, Op1);
2967 case 15: // 1 1 1 1
2968 return FoldConstant(true);
2969 default:
2970 llvm_unreachable("Invalid Operation");
2971 }
2972 return nullptr;
2973}
2974
2975/// Fold icmp (add X, Y), C.
2978 const APInt &C) {
2979 Value *Y = Add->getOperand(1);
2980 Value *X = Add->getOperand(0);
2981
2982 Value *Op0, *Op1;
2983 Instruction *Ext0, *Ext1;
2984 const CmpInst::Predicate Pred = Cmp.getPredicate();
2985 if (match(Add,
2988 m_ZExtOrSExt(m_Value(Op1))))) &&
2989 Op0->getType()->isIntOrIntVectorTy(1) &&
2990 Op1->getType()->isIntOrIntVectorTy(1)) {
2991 unsigned BW = C.getBitWidth();
2992 std::bitset<4> Table;
2993 auto ComputeTable = [&](bool Op0Val, bool Op1Val) {
2994 int Res = 0;
2995 if (Op0Val)
2996 Res += isa<ZExtInst>(Ext0) ? 1 : -1;
2997 if (Op1Val)
2998 Res += isa<ZExtInst>(Ext1) ? 1 : -1;
2999 return ICmpInst::compare(APInt(BW, Res, true), C, Pred);
3000 };
3001
3002 Table[0] = ComputeTable(false, false);
3003 Table[1] = ComputeTable(false, true);
3004 Table[2] = ComputeTable(true, false);
3005 Table[3] = ComputeTable(true, true);
3006 if (auto *Cond =
3007 createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))
3008 return replaceInstUsesWith(Cmp, Cond);
3009 }
3010 const APInt *C2;
3011 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3012 return nullptr;
3013
3014 // Fold icmp pred (add X, C2), C.
3015 Type *Ty = Add->getType();
3016
3017 // If the add does not wrap, we can always adjust the compare by subtracting
3018 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3019 // are canonicalized to SGT/SLT/UGT/ULT.
3020 if ((Add->hasNoSignedWrap() &&
3021 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
3022 (Add->hasNoUnsignedWrap() &&
3023 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
3024 bool Overflow;
3025 APInt NewC =
3026 Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);
3027 // If there is overflow, the result must be true or false.
3028 // TODO: Can we assert there is no overflow because InstSimplify always
3029 // handles those cases?
3030 if (!Overflow)
3031 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3032 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
3033 }
3034
3035 auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
3036 const APInt &Upper = CR.getUpper();
3037 const APInt &Lower = CR.getLower();
3038 if (Cmp.isSigned()) {
3039 if (Lower.isSignMask())
3040 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
3041 if (Upper.isSignMask())
3042 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
3043 } else {
3044 if (Lower.isMinValue())
3045 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
3046 if (Upper.isMinValue())
3047 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
3048 }
3049
3050 // This set of folds is intentionally placed after folds that use no-wrapping
3051 // flags because those folds are likely better for later analysis/codegen.
3054
3055 // Fold compare with offset to opposite sign compare if it eliminates offset:
3056 // (X + C2) >u C --> X <s -C2 (if C == C2 + SMAX)
3057 if (Pred == CmpInst::ICMP_UGT && C == *C2 + SMax)
3058 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, -(*C2)));
3059
3060 // (X + C2) <u C --> X >s ~C2 (if C == C2 + SMIN)
3061 if (Pred == CmpInst::ICMP_ULT && C == *C2 + SMin)
3062 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantInt::get(Ty, ~(*C2)));
3063
3064 // (X + C2) >s C --> X <u (SMAX - C) (if C == C2 - 1)
3065 if (Pred == CmpInst::ICMP_SGT && C == *C2 - 1)
3066 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, SMax - C));
3067
3068 // (X + C2) <s C --> X >u (C ^ SMAX) (if C == C2)
3069 if (Pred == CmpInst::ICMP_SLT && C == *C2)
3070 return new ICmpInst(ICmpInst::ICMP_UGT, X, ConstantInt::get(Ty, C ^ SMax));
3071
3072 // (X + -1) <u C --> X <=u C (if X is never null)
3073 if (Pred == CmpInst::ICMP_ULT && C2->isAllOnes()) {
3074 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3075 if (llvm::isKnownNonZero(X, Q))
3076 return new ICmpInst(ICmpInst::ICMP_ULE, X, ConstantInt::get(Ty, C));
3077 }
3078
3079 if (!Add->hasOneUse())
3080 return nullptr;
3081
3082 // X+C <u C2 -> (X & -C2) == C
3083 // iff C & (C2-1) == 0
3084 // C2 is a power of 2
3085 if (Pred == ICmpInst::ICMP_ULT && C.isPowerOf2() && (*C2 & (C - 1)) == 0)
3087 ConstantExpr::getNeg(cast<Constant>(Y)));
3088
3089 // X+C >u C2 -> (X & ~C2) != C
3090 // iff C & C2 == 0
3091 // C2+1 is a power of 2
3092 if (Pred == ICmpInst::ICMP_UGT && (C + 1).isPowerOf2() && (*C2 & C) == 0)
3094 ConstantExpr::getNeg(cast<Constant>(Y)));
3095
3096 // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
3097 // to the ult form.
3098 // X+C2 >u C -> X+(C2-C-1) <u ~C
3099 if (Pred == ICmpInst::ICMP_UGT)
3100 return new ICmpInst(ICmpInst::ICMP_ULT,
3101 Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
3102 ConstantInt::get(Ty, ~C));
3103
3104 return nullptr;
3105}
3106
3108 Value *&RHS, ConstantInt *&Less,
3109 ConstantInt *&Equal,
3110 ConstantInt *&Greater) {
3111 // TODO: Generalize this to work with other comparison idioms or ensure
3112 // they get canonicalized into this form.
3113
3114 // select i1 (a == b),
3115 // i32 Equal,
3116 // i32 (select i1 (a < b), i32 Less, i32 Greater)
3117 // where Equal, Less and Greater are placeholders for any three constants.
3118 ICmpInst::Predicate PredA;
3119 if (!match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) ||
3120 !ICmpInst::isEquality(PredA))
3121 return false;
3122 Value *EqualVal = SI->getTrueValue();
3123 Value *UnequalVal = SI->getFalseValue();
3124 // We still can get non-canonical predicate here, so canonicalize.
3125 if (PredA == ICmpInst::ICMP_NE)
3126 std::swap(EqualVal, UnequalVal);
3127 if (!match(EqualVal, m_ConstantInt(Equal)))
3128 return false;
3129 ICmpInst::Predicate PredB;
3130 Value *LHS2, *RHS2;
3131 if (!match(UnequalVal, m_Select(m_ICmp(PredB, m_Value(LHS2), m_Value(RHS2)),
3132 m_ConstantInt(Less), m_ConstantInt(Greater))))
3133 return false;
3134 // We can get predicate mismatch here, so canonicalize if possible:
3135 // First, ensure that 'LHS' match.
3136 if (LHS2 != LHS) {
3137 // x sgt y <--> y slt x
3138 std::swap(LHS2, RHS2);
3139 PredB = ICmpInst::getSwappedPredicate(PredB);
3140 }
3141 if (LHS2 != LHS)
3142 return false;
3143 // We also need to canonicalize 'RHS'.
3144 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
3145 // x sgt C-1 <--> x sge C <--> not(x slt C)
3146 auto FlippedStrictness =
3148 PredB, cast<Constant>(RHS2));
3149 if (!FlippedStrictness)
3150 return false;
3151 assert(FlippedStrictness->first == ICmpInst::ICMP_SGE &&
3152 "basic correctness failure");
3153 RHS2 = FlippedStrictness->second;
3154 // And kind-of perform the result swap.
3155 std::swap(Less, Greater);
3156 PredB = ICmpInst::ICMP_SLT;
3157 }
3158 return PredB == ICmpInst::ICMP_SLT && RHS == RHS2;
3159}
3160
3163 ConstantInt *C) {
3164
3165 assert(C && "Cmp RHS should be a constant int!");
3166 // If we're testing a constant value against the result of a three way
3167 // comparison, the result can be expressed directly in terms of the
3168 // original values being compared. Note: We could possibly be more
3169 // aggressive here and remove the hasOneUse test. The original select is
3170 // really likely to simplify or sink when we remove a test of the result.
3171 Value *OrigLHS, *OrigRHS;
3172 ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan;
3173 if (Cmp.hasOneUse() &&
3174 matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal,
3175 C3GreaterThan)) {
3176 assert(C1LessThan && C2Equal && C3GreaterThan);
3177
3178 bool TrueWhenLessThan =
3179 ConstantExpr::getCompare(Cmp.getPredicate(), C1LessThan, C)
3180 ->isAllOnesValue();
3181 bool TrueWhenEqual =
3182 ConstantExpr::getCompare(Cmp.getPredicate(), C2Equal, C)
3183 ->isAllOnesValue();
3184 bool TrueWhenGreaterThan =
3185 ConstantExpr::getCompare(Cmp.getPredicate(), C3GreaterThan, C)
3186 ->isAllOnesValue();
3187
3188 // This generates the new instruction that will replace the original Cmp
3189 // Instruction. Instead of enumerating the various combinations when
3190 // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus
3191 // false, we rely on chaining of ORs and future passes of InstCombine to
3192 // simplify the OR further (i.e. a s< b || a == b becomes a s<= b).
3193
3194 // When none of the three constants satisfy the predicate for the RHS (C),
3195 // the entire original Cmp can be simplified to a false.
3197 if (TrueWhenLessThan)
3199 OrigLHS, OrigRHS));
3200 if (TrueWhenEqual)
3202 OrigLHS, OrigRHS));
3203 if (TrueWhenGreaterThan)
3205 OrigLHS, OrigRHS));
3206
3207 return replaceInstUsesWith(Cmp, Cond);
3208 }
3209 return nullptr;
3210}
3211
3213 auto *Bitcast = dyn_cast<BitCastInst>(Cmp.getOperand(0));
3214 if (!Bitcast)
3215 return nullptr;
3216
3217 ICmpInst::Predicate Pred = Cmp.getPredicate();
3218 Value *Op1 = Cmp.getOperand(1);
3219 Value *BCSrcOp = Bitcast->getOperand(0);
3220 Type *SrcType = Bitcast->getSrcTy();
3221 Type *DstType = Bitcast->getType();
3222
3223 // Make sure the bitcast doesn't change between scalar and vector and
3224 // doesn't change the number of vector elements.
3225 if (SrcType->isVectorTy() == DstType->isVectorTy() &&
3226 SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits()) {
3227 // Zero-equality and sign-bit checks are preserved through sitofp + bitcast.
3228 Value *X;
3229 if (match(BCSrcOp, m_SIToFP(m_Value(X)))) {
3230 // icmp eq (bitcast (sitofp X)), 0 --> icmp eq X, 0
3231 // icmp ne (bitcast (sitofp X)), 0 --> icmp ne X, 0
3232 // icmp slt (bitcast (sitofp X)), 0 --> icmp slt X, 0
3233 // icmp sgt (bitcast (sitofp X)), 0 --> icmp sgt X, 0
3234 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_SLT ||
3235 Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) &&
3236 match(Op1, m_Zero()))
3237 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3238
3239 // icmp slt (bitcast (sitofp X)), 1 --> icmp slt X, 1
3240 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_One()))
3241 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), 1));
3242
3243 // icmp sgt (bitcast (sitofp X)), -1 --> icmp sgt X, -1
3244 if (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))
3245 return new ICmpInst(Pred, X,
3246 ConstantInt::getAllOnesValue(X->getType()));
3247 }
3248
3249 // Zero-equality checks are preserved through unsigned floating-point casts:
3250 // icmp eq (bitcast (uitofp X)), 0 --> icmp eq X, 0
3251 // icmp ne (bitcast (uitofp X)), 0 --> icmp ne X, 0
3252 if (match(BCSrcOp, m_UIToFP(m_Value(X))))
3253 if (Cmp.isEquality() && match(Op1, m_Zero()))
3254 return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType()));
3255
3256 const APInt *C;
3257 bool TrueIfSigned;
3258 if (match(Op1, m_APInt(C)) && Bitcast->hasOneUse()) {
3259 // If this is a sign-bit test of a bitcast of a casted FP value, eliminate
3260 // the FP extend/truncate because that cast does not change the sign-bit.
3261 // This is true for all standard IEEE-754 types and the X86 80-bit type.
3262 // The sign-bit is always the most significant bit in those types.
3263 if (isSignBitCheck(Pred, *C, TrueIfSigned) &&
3264 (match(BCSrcOp, m_FPExt(m_Value(X))) ||
3265 match(BCSrcOp, m_FPTrunc(m_Value(X))))) {
3266 // (bitcast (fpext/fptrunc X)) to iX) < 0 --> (bitcast X to iY) < 0
3267 // (bitcast (fpext/fptrunc X)) to iX) > -1 --> (bitcast X to iY) > -1
3268 Type *XType = X->getType();
3269
3270 // We can't currently handle Power style floating point operations here.
3271 if (!(XType->isPPC_FP128Ty() || SrcType->isPPC_FP128Ty())) {
3272 Type *NewType = Builder.getIntNTy(XType->getScalarSizeInBits());
3273 if (auto *XVTy = dyn_cast<VectorType>(XType))
3274 NewType = VectorType::get(NewType, XVTy->getElementCount());
3275 Value *NewBitcast = Builder.CreateBitCast(X, NewType);
3276 if (TrueIfSigned)
3277 return new ICmpInst(ICmpInst::ICMP_SLT, NewBitcast,
3278 ConstantInt::getNullValue(NewType));
3279 else
3280 return new ICmpInst(ICmpInst::ICMP_SGT, NewBitcast,
3282 }
3283 }
3284
3285 // icmp eq/ne (bitcast X to int), special fp -> llvm.is.fpclass(X, class)
3286 Type *FPType = SrcType->getScalarType();
3287 if (!Cmp.getParent()->getParent()->hasFnAttribute(
3288 Attribute::NoImplicitFloat) &&
3289 Cmp.isEquality() && FPType->isIEEELikeFPTy()) {
3290 FPClassTest Mask = APFloat(FPType->getFltSemantics(), *C).classify();
3291 if (Mask & (fcInf | fcZero)) {
3292 if (Pred == ICmpInst::ICMP_NE)
3293 Mask = ~Mask;
3294 return replaceInstUsesWith(Cmp,
3295 Builder.createIsFPClass(BCSrcOp, Mask));
3296 }
3297 }
3298 }
3299 }
3300
3301 const APInt *C;
3302 if (!match(Cmp.getOperand(1), m_APInt(C)) || !DstType->isIntegerTy() ||
3303 !SrcType->isIntOrIntVectorTy())
3304 return nullptr;
3305
3306 // If this is checking if all elements of a vector compare are set or not,
3307 // invert the casted vector equality compare and test if all compare
3308 // elements are clear or not. Compare against zero is generally easier for
3309 // analysis and codegen.
3310 // icmp eq/ne (bitcast (not X) to iN), -1 --> icmp eq/ne (bitcast X to iN), 0
3311 // Example: are all elements equal? --> are zero elements not equal?
3312 // TODO: Try harder to reduce compare of 2 freely invertible operands?
3313 if (Cmp.isEquality() && C->isAllOnes() && Bitcast->hasOneUse()) {
3314 if (Value *NotBCSrcOp =
3315 getFreelyInverted(BCSrcOp, BCSrcOp->hasOneUse(), &Builder)) {
3316 Value *Cast = Builder.CreateBitCast(NotBCSrcOp, DstType);
3317 return new ICmpInst(Pred, Cast, ConstantInt::getNullValue(DstType));
3318 }
3319 }
3320
3321 // If this is checking if all elements of an extended vector are clear or not,
3322 // compare in a narrow type to eliminate the extend:
3323 // icmp eq/ne (bitcast (ext X) to iN), 0 --> icmp eq/ne (bitcast X to iM), 0
3324 Value *X;
3325 if (Cmp.isEquality() && C->isZero() && Bitcast->hasOneUse() &&
3326 match(BCSrcOp, m_ZExtOrSExt(m_Value(X)))) {
3327 if (auto *VecTy = dyn_cast<FixedVectorType>(X->getType())) {
3328 Type *NewType = Builder.getIntNTy(VecTy->getPrimitiveSizeInBits());
3329 Value *NewCast = Builder.CreateBitCast(X, NewType);
3330 return new ICmpInst(Pred, NewCast, ConstantInt::getNullValue(NewType));
3331 }
3332 }
3333
3334 // Folding: icmp <pred> iN X, C
3335 // where X = bitcast <M x iK> (shufflevector <M x iK> %vec, undef, SC)) to iN
3336 // and C is a splat of a K-bit pattern
3337 // and SC is a constant vector = <C', C', C', ..., C'>
3338 // Into:
3339 // %E = extractelement <M x iK> %vec, i32 C'
3340 // icmp <pred> iK %E, trunc(C)
3341 Value *Vec;
3342 ArrayRef<int> Mask;
3343 if (match(BCSrcOp, m_Shuffle(m_Value(Vec), m_Undef(), m_Mask(Mask)))) {
3344 // Check whether every element of Mask is the same constant
3345 if (all_equal(Mask)) {
3346 auto *VecTy = cast<VectorType>(SrcType);
3347 auto *EltTy = cast<IntegerType>(VecTy->getElementType());
3348 if (C->isSplat(EltTy->getBitWidth())) {
3349 // Fold the icmp based on the value of C
3350 // If C is M copies of an iK sized bit pattern,
3351 // then:
3352 // => %E = extractelement <N x iK> %vec, i32 Elem
3353 // icmp <pred> iK %SplatVal, <pattern>
3354 Value *Elem = Builder.getInt32(Mask[0]);
3355 Value *Extract = Builder.CreateExtractElement(Vec, Elem);
3356 Value *NewC = ConstantInt::get(EltTy, C->trunc(EltTy->getBitWidth()));
3357 return new ICmpInst(Pred, Extract, NewC);
3358 }
3359 }
3360 }
3361 return nullptr;
3362}
3363
3364/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3365/// where X is some kind of instruction.
3367 const APInt *C;
3368
3369 if (match(Cmp.getOperand(1), m_APInt(C))) {
3370 if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0)))
3371 if (Instruction *I = foldICmpBinOpWithConstant(Cmp, BO, *C))
3372 return I;
3373
3374 if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0)))
3375 // For now, we only support constant integers while folding the
3376 // ICMP(SELECT)) pattern. We can extend this to support vector of integers
3377 // similar to the cases handled by binary ops above.
3378 if (auto *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1)))
3379 if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS))
3380 return I;
3381
3382 if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0)))
3383 if (Instruction *I = foldICmpTruncConstant(Cmp, TI, *C))
3384 return I;
3385
3386 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)))
3387 if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, II, *C))
3388 return I;
3389
3390 // (extractval ([s/u]subo X, Y), 0) == 0 --> X == Y
3391 // (extractval ([s/u]subo X, Y), 0) != 0 --> X != Y
3392 // TODO: This checks one-use, but that is not strictly necessary.
3393 Value *Cmp0 = Cmp.getOperand(0);
3394 Value *X, *Y;
3395 if (C->isZero() && Cmp.isEquality() && Cmp0->hasOneUse() &&
3396 (match(Cmp0,
3397 m_ExtractValue<0>(m_Intrinsic<Intrinsic::ssub_with_overflow>(
3398 m_Value(X), m_Value(Y)))) ||
3399 match(Cmp0,
3400 m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>(
3401 m_Value(X), m_Value(Y))))))
3402 return new ICmpInst(Cmp.getPredicate(), X, Y);
3403 }
3404
3405 if (match(Cmp.getOperand(1), m_APIntAllowPoison(C)))
3407
3408 return nullptr;
3409}
3410
3411/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
3412/// icmp eq/ne BO, C.
3414 ICmpInst &Cmp, BinaryOperator *BO, const APInt &C) {
3415 // TODO: Some of these folds could work with arbitrary constants, but this
3416 // function is limited to scalar and vector splat constants.
3417 if (!Cmp.isEquality())
3418 return nullptr;
3419
3420 ICmpInst::Predicate Pred = Cmp.getPredicate();
3421 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
3422 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
3423 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
3424
3425 switch (BO->getOpcode()) {
3426 case Instruction::SRem:
3427 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
3428 if (C.isZero() && BO->hasOneUse()) {
3429 const APInt *BOC;
3430 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
3431 Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName());
3432 return new ICmpInst(Pred, NewRem,
3434 }
3435 }
3436 break;
3437 case Instruction::Add: {
3438 // (A + C2) == C --> A == (C - C2)
3439 // (A + C2) != C --> A != (C - C2)
3440 // TODO: Remove the one-use limitation? See discussion in D58633.
3441 if (Constant *C2 = dyn_cast<Constant>(BOp1)) {
3442 if (BO->hasOneUse())
3443 return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2));
3444 } else if (C.isZero()) {
3445 // Replace ((add A, B) != 0) with (A != -B) if A or B is
3446 // efficiently invertible, or if the add has just this one use.
3447 if (Value *NegVal = dyn_castNegVal(BOp1))
3448 return new ICmpInst(Pred, BOp0, NegVal);
3449 if (Value *NegVal = dyn_castNegVal(BOp0))
3450 return new ICmpInst(Pred, NegVal, BOp1);
3451 if (BO->hasOneUse()) {
3452 // (add nuw A, B) != 0 -> (or A, B) != 0
3453 if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
3454 Value *Or = Builder.CreateOr(BOp0, BOp1);
3455 return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
3456 }
3457 Value *Neg = Builder.CreateNeg(BOp1);
3458 Neg->takeName(BO);
3459 return new ICmpInst(Pred, BOp0, Neg);
3460 }
3461 }
3462 break;
3463 }
3464 case Instruction::Xor:
3465 if (BO->hasOneUse()) {
3466 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
3467 // For the xor case, we can xor two constants together, eliminating
3468 // the explicit xor.
3469 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
3470 } else if (C.isZero()) {
3471 // Replace ((xor A, B) != 0) with (A != B)
3472 return new ICmpInst(Pred, BOp0, BOp1);
3473 }
3474 }
3475 break;
3476 case Instruction::Or: {
3477 const APInt *BOC;
3478 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
3479 // Comparing if all bits outside of a constant mask are set?
3480 // Replace (X | C) == -1 with (X & ~C) == ~C.
3481 // This removes the -1 constant.
3482 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));
3483 Value *And = Builder.CreateAnd(BOp0, NotBOC);
3484 return new ICmpInst(Pred, And, NotBOC);
3485 }
3486 break;
3487 }
3488 case Instruction::UDiv:
3489 case Instruction::SDiv:
3490 if (BO->isExact()) {
3491 // div exact X, Y eq/ne 0 -> X eq/ne 0
3492 // div exact X, Y eq/ne 1 -> X eq/ne Y
3493 // div exact X, Y eq/ne C ->
3494 // if Y * C never-overflow && OneUse:
3495 // -> Y * C eq/ne X
3496 if (C.isZero())
3497 return new ICmpInst(Pred, BOp0, Constant::getNullValue(BO->getType()));
3498 else if (C.isOne())
3499 return new ICmpInst(Pred, BOp0, BOp1);
3500 else if (BO->hasOneUse()) {
3502 Instruction::Mul, BO->getOpcode() == Instruction::SDiv, BOp1,
3503 Cmp.getOperand(1), BO);
3505 Value *YC =
3506 Builder.CreateMul(BOp1, ConstantInt::get(BO->getType(), C));
3507 return new ICmpInst(Pred, YC, BOp0);
3508 }
3509 }
3510 }
3511 if (BO->getOpcode() == Instruction::UDiv && C.isZero()) {
3512 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
3513 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
3514 return new ICmpInst(NewPred, BOp1, BOp0);
3515 }
3516 break;
3517 default:
3518 break;
3519 }
3520 return nullptr;
3521}
3522
3524 const APInt &CRhs,
3525 InstCombiner::BuilderTy &Builder,
3526 const SimplifyQuery &Q) {
3527 assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
3528 "Non-ctpop intrin in ctpop fold");
3529 if (!CtpopLhs->hasOneUse())
3530 return nullptr;
3531
3532 // Power of 2 test:
3533 // isPow2OrZero : ctpop(X) u< 2
3534 // isPow2 : ctpop(X) == 1
3535 // NotPow2OrZero: ctpop(X) u> 1
3536 // NotPow2 : ctpop(X) != 1
3537 // If we know any bit of X can be folded to:
3538 // IsPow2 : X & (~Bit) == 0
3539 // NotPow2 : X & (~Bit) != 0
3540 const ICmpInst::Predicate Pred = I.getPredicate();
3541 if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
3542 (Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
3543 Value *Op = CtpopLhs->getArgOperand(0);
3544 KnownBits OpKnown = computeKnownBits(Op, Q.DL,
3545 /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT);
3546 // No need to check for count > 1, that should be already constant folded.
3547 if (OpKnown.countMinPopulation() == 1) {
3548 Value *And = Builder.CreateAnd(
3549 Op, Constant::getIntegerValue(Op->getType(), ~(OpKnown.One)));
3550 return new ICmpInst(
3551 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_ULT)
3554 And, Constant::getNullValue(Op->getType()));
3555 }
3556 }
3557
3558 return nullptr;
3559}
3560
3561/// Fold an equality icmp with LLVM intrinsic and constant operand.
3563 ICmpInst &Cmp, IntrinsicInst *II, const APInt &C) {
3564 Type *Ty = II->getType();
3565 unsigned BitWidth = C.getBitWidth();
3566 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3567
3568 switch (II->getIntrinsicID()) {
3569 case Intrinsic::abs:
3570 // abs(A) == 0 -> A == 0
3571 // abs(A) == INT_MIN -> A == INT_MIN
3572 if (C.isZero() || C.isMinSignedValue())
3573 return new ICmpInst(Pred, II->getArgOperand(0), ConstantInt::get(Ty, C));
3574 break;
3575
3576 case Intrinsic::bswap:
3577 // bswap(A) == C -> A == bswap(C)
3578 return new ICmpInst(Pred, II->getArgOperand(0),
3579 ConstantInt::get(Ty, C.byteSwap()));
3580
3581 case Intrinsic::bitreverse:
3582 // bitreverse(A) == C -> A == bitreverse(C)
3583 return new ICmpInst(Pred, II->getArgOperand(0),
3584 ConstantInt::get(Ty, C.reverseBits()));
3585
3586 case Intrinsic::ctlz:
3587 case Intrinsic::cttz: {
3588 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
3589 if (C == BitWidth)
3590 return new ICmpInst(Pred, II->getArgOperand(0),
3592
3593 // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set
3594 // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits.
3595 // Limit to one use to ensure we don't increase instruction count.
3596 unsigned Num = C.getLimitedValue(BitWidth);
3597 if (Num != BitWidth && II->hasOneUse()) {
3598 bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz;
3599 APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1)
3600 : APInt::getHighBitsSet(BitWidth, Num + 1);
3601 APInt Mask2 = IsTrailing
3604 return new ICmpInst(Pred, Builder.CreateAnd(II->getArgOperand(0), Mask1),
3605 ConstantInt::get(Ty, Mask2));
3606 }
3607 break;
3608 }
3609
3610 case Intrinsic::ctpop: {
3611 // popcount(A) == 0 -> A == 0 and likewise for !=
3612 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
3613 bool IsZero = C.isZero();
3614 if (IsZero || C == BitWidth)
3615 return new ICmpInst(Pred, II->getArgOperand(0),
3616 IsZero ? Constant::getNullValue(Ty)
3618
3619 break;
3620 }
3621
3622 case Intrinsic::fshl:
3623 case Intrinsic::fshr:
3624 if (II->getArgOperand(0) == II->getArgOperand(1)) {
3625 const APInt *RotAmtC;
3626 // ror(X, RotAmtC) == C --> X == rol(C, RotAmtC)
3627 // rol(X, RotAmtC) == C --> X == ror(C, RotAmtC)
3628 if (match(II->getArgOperand(2), m_APInt(RotAmtC)))
3629 return new ICmpInst(Pred, II->getArgOperand(0),
3630 II->getIntrinsicID() == Intrinsic::fshl
3631 ? ConstantInt::get(Ty, C.rotr(*RotAmtC))
3632 : ConstantInt::get(Ty, C.rotl(*RotAmtC)));
3633 }
3634 break;
3635
3636 case Intrinsic::umax:
3637 case Intrinsic::uadd_sat: {
3638 // uadd.sat(a, b) == 0 -> (a | b) == 0
3639 // umax(a, b) == 0 -> (a | b) == 0
3640 if (C.isZero() && II->hasOneUse()) {
3642 return new ICmpInst(Pred, Or, Constant::getNullValue(Ty));
3643 }
3644 break;
3645 }
3646
3647 case Intrinsic::ssub_sat:
3648 // ssub.sat(a, b) == 0 -> a == b
3649 if (C.isZero())
3650 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3651 break;
3652 case Intrinsic::usub_sat: {
3653 // usub.sat(a, b) == 0 -> a <= b
3654 if (C.isZero()) {
3655 ICmpInst::Predicate NewPred =
3657 return new ICmpInst(NewPred, II->getArgOperand(0), II->getArgOperand(1));
3658 }
3659 break;
3660 }
3661 default:
3662 break;
3663 }
3664
3665 return nullptr;
3666}
3667
3668/// Fold an icmp with LLVM intrinsics
3669static Instruction *
3671 InstCombiner::BuilderTy &Builder) {
3672 assert(Cmp.isEquality());
3673
3674 ICmpInst::Predicate Pred = Cmp.getPredicate();
3675 Value *Op0 = Cmp.getOperand(0);
3676 Value *Op1 = Cmp.getOperand(1);
3677 const auto *IIOp0 = dyn_cast<IntrinsicInst>(Op0);
3678 const auto *IIOp1 = dyn_cast<IntrinsicInst>(Op1);
3679 if (!IIOp0 || !IIOp1 || IIOp0->getIntrinsicID() != IIOp1->getIntrinsicID())
3680 return nullptr;
3681
3682 switch (IIOp0->getIntrinsicID()) {
3683 case Intrinsic::bswap:
3684 case Intrinsic::bitreverse:
3685 // If both operands are byte-swapped or bit-reversed, just compare the
3686 // original values.
3687 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3688 case Intrinsic::fshl:
3689 case Intrinsic::fshr: {
3690 // If both operands are rotated by same amount, just compare the
3691 // original values.
3692 if (IIOp0->getOperand(0) != IIOp0->getOperand(1))
3693 break;
3694 if (IIOp1->getOperand(0) != IIOp1->getOperand(1))
3695 break;
3696 if (IIOp0->getOperand(2) == IIOp1->getOperand(2))
3697 return new ICmpInst(Pred, IIOp0->getOperand(0), IIOp1->getOperand(0));
3698
3699 // rotate(X, AmtX) == rotate(Y, AmtY)
3700 // -> rotate(X, AmtX - AmtY) == Y
3701 // Do this if either both rotates have one use or if only one has one use
3702 // and AmtX/AmtY are constants.
3703 unsigned OneUses = IIOp0->hasOneUse() + IIOp1->hasOneUse();
3704 if (OneUses == 2 ||
3705 (OneUses == 1 && match(IIOp0->getOperand(2), m_ImmConstant()) &&
3706 match(IIOp1->getOperand(2), m_ImmConstant()))) {
3707 Value *SubAmt =
3708 Builder.CreateSub(IIOp0->getOperand(2), IIOp1->getOperand(2));
3709 Value *CombinedRotate = Builder.CreateIntrinsic(
3710 Op0->getType(), IIOp0->getIntrinsicID(),
3711 {IIOp0->getOperand(0), IIOp0->getOperand(0), SubAmt});
3712 return new ICmpInst(Pred, IIOp1->getOperand(0), CombinedRotate);
3713 }
3714 } break;
3715 default:
3716 break;
3717 }
3718
3719 return nullptr;
3720}
3721
3722/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
3723/// where X is some kind of instruction and C is AllowPoison.
3724/// TODO: Move more folds which allow poison to this function.
3727 const APInt &C) {
3728 const ICmpInst::Predicate Pred = Cmp.getPredicate();
3729 if (auto *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0))) {
3730 switch (II->getIntrinsicID()) {
3731 default:
3732 break;
3733 case Intrinsic::fshl:
3734 case Intrinsic::fshr:
3735 if (Cmp.isEquality() && II->getArgOperand(0) == II->getArgOperand(1)) {
3736 // (rot X, ?) == 0/-1 --> X == 0/-1
3737 if (C.isZero() || C.isAllOnes())
3738 return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
3739 }
3740 break;
3741 }
3742 }
3743
3744 return nullptr;
3745}
3746
3747/// Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
3749 BinaryOperator *BO,
3750 const APInt &C) {
3751 switch (BO->getOpcode()) {
3752 case Instruction::Xor:
3753 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
3754 return I;
3755 break;
3756 case Instruction::And:
3757 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
3758 return I;
3759 break;
3760 case Instruction::Or:
3761 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
3762 return I;
3763 break;
3764 case Instruction::Mul:
3765 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
3766 return I;
3767 break;
3768 case Instruction::Shl:
3769 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
3770 return I;
3771 break;
3772 case Instruction::LShr:
3773 case Instruction::AShr:
3774 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
3775 return I;
3776 break;
3777 case Instruction::SRem:
3778 if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
3779 return I;
3780 break;
3781 case Instruction::UDiv:
3782 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
3783 return I;
3784 [[fallthrough]];
3785 case Instruction::SDiv:
3786 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
3787 return I;
3788 break;
3789 case Instruction::Sub:
3790 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
3791 return I;
3792 break;
3793 case Instruction::Add:
3794 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
3795 return I;
3796 break;
3797 default:
3798 break;
3799 }
3800
3801 // TODO: These folds could be refactored to be part of the above calls.
3802 return foldICmpBinOpEqualityWithConstant(Cmp, BO, C);
3803}
3804
3805static Instruction *
3807 SaturatingInst *II, const APInt &C,
3808 InstCombiner::BuilderTy &Builder) {
3809 // This transform may end up producing more than one instruction for the
3810 // intrinsic, so limit it to one user of the intrinsic.
3811 if (!II->hasOneUse())
3812 return nullptr;
3813
3814 // Let Y = [add/sub]_sat(X, C) pred C2
3815 // SatVal = The saturating value for the operation
3816 // WillWrap = Whether or not the operation will underflow / overflow
3817 // => Y = (WillWrap ? SatVal : (X binop C)) pred C2
3818 // => Y = WillWrap ? (SatVal pred C2) : ((X binop C) pred C2)
3819 //
3820 // When (SatVal pred C2) is true, then
3821 // Y = WillWrap ? true : ((X binop C) pred C2)
3822 // => Y = WillWrap || ((X binop C) pred C2)
3823 // else
3824 // Y = WillWrap ? false : ((X binop C) pred C2)
3825 // => Y = !WillWrap ? ((X binop C) pred C2) : false
3826 // => Y = !WillWrap && ((X binop C) pred C2)
3827 Value *Op0 = II->getOperand(0);
3828 Value *Op1 = II->getOperand(1);
3829
3830 const APInt *COp1;
3831 // This transform only works when the intrinsic has an integral constant or
3832 // splat vector as the second operand.
3833 if (!match(Op1, m_APInt(COp1)))
3834 return nullptr;
3835
3836 APInt SatVal;
3837 switch (II->getIntrinsicID()) {
3838 default:
3840 "This function only works with usub_sat and uadd_sat for now!");
3841 case Intrinsic::uadd_sat:
3842 SatVal = APInt::getAllOnes(C.getBitWidth());
3843 break;
3844 case Intrinsic::usub_sat:
3845 SatVal = APInt::getZero(C.getBitWidth());
3846 break;
3847 }
3848
3849 // Check (SatVal pred C2)
3850 bool SatValCheck = ICmpInst::compare(SatVal, C, Pred);
3851
3852 // !WillWrap.
3854 II->getBinaryOp(), *COp1, II->getNoWrapKind());
3855
3856 // WillWrap.
3857 if (SatValCheck)
3858 C1 = C1.inverse();
3859
3861 if (II->getBinaryOp() == Instruction::Add)
3862 C2 = C2.sub(*COp1);
3863 else
3864 C2 = C2.add(*COp1);
3865
3866 Instruction::BinaryOps CombiningOp =
3867 SatValCheck ? Instruction::BinaryOps::Or : Instruction::BinaryOps::And;
3868
3869 std::optional<ConstantRange> Combination;
3870 if (CombiningOp == Instruction::BinaryOps::Or)
3871 Combination = C1.exactUnionWith(C2);
3872 else /* CombiningOp == Instruction::BinaryOps::And */
3873 Combination = C1.exactIntersectWith(C2);
3874
3875 if (!Combination)
3876 return nullptr;
3877
3878 CmpInst::Predicate EquivPred;
3879 APInt EquivInt;
3880 APInt EquivOffset;
3881
3882 Combination->getEquivalentICmp(EquivPred, EquivInt, EquivOffset);
3883
3884 return new ICmpInst(
3885 EquivPred,
3886 Builder.CreateAdd(Op0, ConstantInt::get(Op1->getType(), EquivOffset)),
3887 ConstantInt::get(Op1->getType(), EquivInt));
3888}
3889
3890/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
3892 IntrinsicInst *II,
3893 const APInt &C) {
3894 ICmpInst::Predicate Pred = Cmp.getPredicate();
3895
3896 // Handle folds that apply for any kind of icmp.
3897 switch (II->getIntrinsicID()) {
3898 default:
3899 break;
3900 case Intrinsic::uadd_sat:
3901 case Intrinsic::usub_sat:
3902 if (auto *Folded = foldICmpUSubSatOrUAddSatWithConstant(
3903 Pred, cast<SaturatingInst>(II), C, Builder))
3904 return Folded;
3905 break;
3906 case Intrinsic::ctpop: {
3907 const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3908 if (Instruction *R = foldCtpopPow2Test(Cmp, II, C, Builder, Q))
3909 return R;
3910 } break;
3911 }
3912
3913 if (Cmp.isEquality())
3914 return foldICmpEqIntrinsicWithConstant(Cmp, II, C);
3915
3916 Type *Ty = II->getType();
3917 unsigned BitWidth = C.getBitWidth();
3918 switch (II->getIntrinsicID()) {
3919 case Intrinsic::ctpop: {
3920 // (ctpop X > BitWidth - 1) --> X == -1
3921 Value *X = II->getArgOperand(0);
3922 if (C == BitWidth - 1 && Pred == ICmpInst::ICMP_UGT)
3923 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, X,
3925 // (ctpop X < BitWidth) --> X != -1
3926 if (C == BitWidth && Pred == ICmpInst::ICMP_ULT)
3927 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, X,
3929 break;
3930 }
3931 case Intrinsic::ctlz: {
3932 // ctlz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX < 0b00010000
3933 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
3934 unsigned Num = C.getLimitedValue();
3935 APInt Limit = APInt::getOneBitSet(BitWidth, BitWidth - Num - 1);
3936 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT,
3937 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
3938 }
3939
3940 // ctlz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX > 0b00011111
3941 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
3942 unsigned Num = C.getLimitedValue();
3944 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT,
3945 II->getArgOperand(0), ConstantInt::get(Ty, Limit));
3946 }
3947 break;
3948 }
3949 case Intrinsic::cttz: {
3950 // Limit to one use to ensure we don't increase instruction count.
3951 if (!II->hasOneUse())
3952 return nullptr;
3953
3954 // cttz(0bXXXXXXXX) > 3 -> 0bXXXXXXXX & 0b00001111 == 0
3955 if (Pred == ICmpInst::ICMP_UGT && C.ult(BitWidth)) {
3956 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue() + 1);
3957 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ,
3958 Builder.CreateAnd(II->getArgOperand(0), Mask),
3960 }
3961
3962 // cttz(0bXXXXXXXX) < 3 -> 0bXXXXXXXX & 0b00000111 != 0
3963 if (Pred == ICmpInst::ICMP_ULT && C.uge(1) && C.ule(BitWidth)) {
3964 APInt Mask = APInt::getLowBitsSet(BitWidth, C.getLimitedValue());
3965 return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE,
3966 Builder.CreateAnd(II->getArgOperand(0), Mask),
3968 }
3969 break;
3970 }
3971 case Intrinsic::ssub_sat:
3972 // ssub.sat(a, b) spred 0 -> a spred b
3973 if (ICmpInst::isSigned(Pred)) {
3974 if (C.isZero())
3975 return new ICmpInst(Pred, II->getArgOperand(0), II->getArgOperand(1));
3976 // X s<= 0 is cannonicalized to X s< 1
3977 if (Pred == ICmpInst::ICMP_SLT && C.isOne())
3978 return new ICmpInst(ICmpInst::ICMP_SLE, II->getArgOperand(0),
3979 II->getArgOperand(1));
3980 // X s>= 0 is cannonicalized to X s> -1
3981 if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
3982 return new ICmpInst(ICmpInst::ICMP_SGE, II->getArgOperand(0),
3983 II->getArgOperand(1));
3984 }
3985 break;
3986 default:
3987 break;
3988 }
3989
3990 return nullptr;
3991}
3992
3993/// Handle icmp with constant (but not simple integer constant) RHS.
3995 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3996 Constant *RHSC = dyn_cast<Constant>(Op1);
3997 Instruction *LHSI = dyn_cast<Instruction>(Op0);
3998 if (!RHSC || !LHSI)
3999 return nullptr;
4000
4001 switch (LHSI->getOpcode()) {
4002 case Instruction::PHI:
4003 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
4004 return NV;
4005 break;
4006 case Instruction::IntToPtr:
4007 // icmp pred inttoptr(X), null -> icmp pred X, 0
4008 if (RHSC->isNullValue() &&
4009 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
4010 return new ICmpInst(
4011 I.getPredicate(), LHSI->getOperand(0),
4013 break;
4014
4015 case Instruction::Load:
4016 // Try to optimize things like "A[i] > 4" to index computations.
4017 if (GetElementPtrInst *GEP =
4018 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
4019 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
4020 if (Instruction *Res =
4021 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, GV, I))
4022 return Res;
4023 break;
4024 }
4025
4026 return nullptr;
4027}
4028
4030 SelectInst *SI, Value *RHS,
4031 const ICmpInst &I) {
4032 // Try to fold the comparison into the select arms, which will cause the
4033 // select to be converted into a logical and/or.
4034 auto SimplifyOp = [&](Value *Op, bool SelectCondIsTrue) -> Value * {
4035 if (Value *Res = simplifyICmpInst(Pred, Op, RHS, SQ))
4036 return Res;
4037 if (std::optional<bool> Impl = isImpliedCondition(
4038 SI->getCondition(), Pred, Op, RHS, DL, SelectCondIsTrue))
4039 return ConstantInt::get(I.getType(), *Impl);
4040 return nullptr;
4041 };
4042
4043 ConstantInt *CI = nullptr;
4044 Value *Op1 = SimplifyOp(SI->getOperand(1), true);
4045 if (Op1)
4046 CI = dyn_cast<ConstantInt>(Op1);
4047
4048 Value *Op2 = SimplifyOp(SI->getOperand(2), false);
4049 if (Op2)
4050 CI = dyn_cast<ConstantInt>(Op2);
4051
4052 // We only want to perform this transformation if it will not lead to
4053 // additional code. This is true if either both sides of the select
4054 // fold to a constant (in which case the icmp is replaced with a select
4055 // which will usually simplify) or this is the only user of the
4056 // select (in which case we are trading a select+icmp for a simpler
4057 // select+icmp) or all uses of the select can be replaced based on
4058 // dominance information ("Global cases").
4059 bool Transform = false;
4060 if (Op1 && Op2)
4061 Transform = true;
4062 else if (Op1 || Op2) {
4063 // Local case
4064 if (SI->hasOneUse())
4065 Transform = true;
4066 // Global cases
4067 else if (CI && !CI->isZero())
4068 // When Op1 is constant try replacing select with second operand.
4069 // Otherwise Op2 is constant and try replacing select with first
4070 // operand.
4071 Transform = replacedSelectWithOperand(SI, &I, Op1 ? 2 : 1);
4072 }
4073 if (Transform) {
4074 if (!Op1)
4075 Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
4076 if (!Op2)
4077 Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
4078 return SelectInst::Create(SI->getOperand(0), Op1, Op2);
4079 }
4080
4081 return nullptr;
4082}
4083
4084// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero)
4085static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
4086 unsigned Depth = 0) {
4087 if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero()))
4088 return true;
4089 if (V->getType()->getScalarSizeInBits() == 1)
4090 return true;
4092 return false;
4093 Value *X;
4094 const Instruction *I = dyn_cast<Instruction>(V);
4095 if (!I)
4096 return false;
4097 switch (I->getOpcode()) {
4098 case Instruction::ZExt:
4099 // ZExt(Mask) is a Mask.
4100 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4101 case Instruction::SExt:
4102 // SExt(Mask) is a Mask.
4103 // SExt(~Mask) is a ~Mask.
4104 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4105 case Instruction::And:
4106 case Instruction::Or:
4107 // Mask0 | Mask1 is a Mask.
4108 // Mask0 & Mask1 is a Mask.
4109 // ~Mask0 | ~Mask1 is a ~Mask.
4110 // ~Mask0 & ~Mask1 is a ~Mask.
4111 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4112 isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4113 case Instruction::Xor:
4114 if (match(V, m_Not(m_Value(X))))
4115 return isMaskOrZero(X, !Not, Q, Depth);
4116
4117 // (X ^ -X) is a ~Mask
4118 if (Not)
4119 return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
4120 // (X ^ (X - 1)) is a Mask
4121 else
4122 return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4123 case Instruction::Select:
4124 // c ? Mask0 : Mask1 is a Mask.
4125 return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
4126 isMaskOrZero(I->getOperand(2), Not, Q, Depth);
4127 case Instruction::Shl:
4128 // (~Mask) << X is a ~Mask.
4129 return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4130 case Instruction::LShr:
4131 // Mask >> X is a Mask.
4132 return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4133 case Instruction::AShr:
4134 // Mask s>> X is a Mask.
4135 // ~Mask s>> X is a ~Mask.
4136 return isMaskOrZero(I->getOperand(0), Not, Q, Depth);
4137 case Instruction::Add:
4138 // Pow2 - 1 is a Mask.
4139 if (!Not && match(I->getOperand(1), m_AllOnes()))
4140 return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true,
4141 Depth, Q.AC, Q.CxtI, Q.DT);
4142 break;
4143 case Instruction::Sub:
4144 // -Pow2 is a ~Mask.
4145 if (Not && match(I->getOperand(0), m_Zero()))
4146 return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true,
4147 Depth, Q.AC, Q.CxtI, Q.DT);
4148 break;
4149 case Instruction::Call: {
4150 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
4151 switch (II->getIntrinsicID()) {
4152 // min/max(Mask0, Mask1) is a Mask.
4153 // min/max(~Mask0, ~Mask1) is a ~Mask.
4154 case Intrinsic::umax:
4155 case Intrinsic::smax:
4156 case Intrinsic::umin:
4157 case Intrinsic::smin:
4158 return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) &&
4159 isMaskOrZero(II->getArgOperand(0), Not, Q, Depth);
4160
4161 // In the context of masks, bitreverse(Mask) == ~Mask
4162 case Intrinsic::bitreverse:
4163 return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth);
4164 default:
4165 break;
4166 }
4167 }
4168 break;
4169 }
4170 default:
4171 break;
4172 }
4173 return false;
4174}
4175
4176/// Some comparisons can be simplified.
4177/// In this case, we are looking for comparisons that look like
4178/// a check for a lossy truncation.
4179/// Folds:
4180/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
4181/// icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask
4182/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask
4183/// icmp eq/ne (~x | Mask), -1 to icmp DstPred x, Mask
4184/// Where Mask is some pattern that produces all-ones in low bits:
4185/// (-1 >> y)
4186/// ((-1 << y) >> y) <- non-canonical, has extra uses
4187/// ~(-1 << y)
4188/// ((1 << y) + (-1)) <- non-canonical, has extra uses
4189/// The Mask can be a constant, too.
4190/// For some predicates, the operands are commutative.
4191/// For others, x can only be on a specific side.
4193 Value *Op1, const SimplifyQuery &Q,
4194 InstCombiner &IC) {
4195
4196 ICmpInst::Predicate DstPred;
4197 switch (Pred) {
4199 // x & Mask == x
4200 // x & ~Mask == 0
4201 // ~x | Mask == -1
4202 // -> x u<= Mask
4203 // x & ~Mask == ~Mask
4204 // -> ~Mask u<= x
4206 break;
4208 // x & Mask != x
4209 // x & ~Mask != 0
4210 // ~x | Mask != -1
4211 // -> x u> Mask
4212 // x & ~Mask != ~Mask
4213 // -> ~Mask u> x
4215 break;
4217 // x & Mask u< x
4218 // -> x u> Mask
4219 // x & ~Mask u< ~Mask
4220 // -> ~Mask u> x
4222 break;
4224 // x & Mask u>= x
4225 // -> x u<= Mask
4226 // x & ~Mask u>= ~Mask
4227 // -> ~Mask u<= x
4229 break;
4231 // x & Mask s< x [iff Mask s>= 0]
4232 // -> x s> Mask
4233 // x & ~Mask s< ~Mask [iff ~Mask != 0]
4234 // -> ~Mask s> x
4236 break;
4238 // x & Mask s>= x [iff Mask s>= 0]
4239 // -> x s<= Mask
4240 // x & ~Mask s>= ~Mask [iff ~Mask != 0]
4241 // -> ~Mask s<= x
4243 break;
4244 default:
4245 // We don't support sgt,sle
4246 // ult/ugt are simplified to true/false respectively.
4247 return nullptr;
4248 }
4249
4250 Value *X, *M;
4251 // Put search code in lambda for early positive returns.
4252 auto IsLowBitMask = [&]() {
4253 if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M)))) {
4254 X = Op1;
4255 // Look for: x & Mask pred x
4256 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4257 return !ICmpInst::isSigned(Pred) ||
4258 (match(M, m_NonNegative()) || isKnownNonNegative(M, Q));
4259 }
4260
4261 // Look for: x & ~Mask pred ~Mask
4262 if (isMaskOrZero(X, /*Not=*/true, Q)) {
4263 return !ICmpInst::isSigned(Pred) || isKnownNonZero(X, Q);
4264 }
4265 return false;
4266 }
4267 if (ICmpInst::isEquality(Pred) && match(Op1, m_AllOnes()) &&
4268 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(M))))) {
4269
4270 auto Check = [&]() {
4271 // Look for: ~x | Mask == -1
4272 if (isMaskOrZero(M, /*Not=*/false, Q)) {
4273 if (Value *NotX =
4274 IC.getFreelyInverted(X, X->hasOneUse(), &IC.Builder)) {
4275 X = NotX;
4276 return true;
4277 }
4278 }
4279 return false;
4280 };
4281 if (Check())
4282 return true;
4283 std::swap(X, M);
4284 return Check();
4285 }
4286 if (ICmpInst::isEquality(Pred) && match(Op1, m_Zero()) &&
4287 match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) {
4288 auto Check = [&]() {
4289 // Look for: x & ~Mask == 0
4290 if (isMaskOrZero(M, /*Not=*/true, Q)) {
4291 if (Value *NotM =
4292 IC.getFreelyInverted(M, M->hasOneUse(), &IC.Builder)) {
4293 M = NotM;
4294 return true;
4295 }
4296 }
4297 return false;
4298 };
4299 if (Check())
4300 return true;
4301 std::swap(X, M);
4302 return Check();
4303 }
4304 return false;
4305 };
4306
4307 if (!IsLowBitMask())
4308 return nullptr;
4309
4310 return IC.Builder.CreateICmp(DstPred, X, M);
4311}
4312
4313/// Some comparisons can be simplified.
4314/// In this case, we are looking for comparisons that look like
4315/// a check for a lossy signed truncation.
4316/// Folds: (MaskedBits is a constant.)
4317/// ((%x << MaskedBits) a>> MaskedBits) SrcPred %x
4318/// Into:
4319/// (add %x, (1 << (KeptBits-1))) DstPred (1 << KeptBits)
4320/// Where KeptBits = bitwidth(%x) - MaskedBits
4321static Value *
4323 InstCombiner::BuilderTy &Builder) {
4324 ICmpInst::Predicate SrcPred;
4325 Value *X;
4326 const APInt *C0, *C1; // FIXME: non-splats, potentially with undef.
4327 // We are ok with 'shl' having multiple uses, but 'ashr' must be one-use.
4328 if (!match(&I, m_c_ICmp(SrcPred,
4330 m_APInt(C1))),
4331 m_Deferred(X))))
4332 return nullptr;
4333
4334 // Potential handling of non-splats: for each element:
4335 // * if both are undef, replace with constant 0.
4336 // Because (1<<0) is OK and is 1, and ((1<<0)>>1) is also OK and is 0.
4337 // * if both are not undef, and are different, bailout.
4338 // * else, only one is undef, then pick the non-undef one.
4339
4340 // The shift amount must be equal.
4341 if (*C0 != *C1)
4342 return nullptr;
4343 const APInt &MaskedBits = *C0;
4344 assert(MaskedBits != 0 && "shift by zero should be folded away already.");
4345
4346 ICmpInst::Predicate DstPred;
4347 switch (SrcPred) {
4349 // ((%x << MaskedBits) a>> MaskedBits) == %x
4350 // =>
4351 // (add %x, (1 << (KeptBits-1))) u< (1 << KeptBits)
4353 break;
4355 // ((%x << MaskedBits) a>> MaskedBits) != %x
4356 // =>
4357 // (add %x, (1 << (KeptBits-1))) u>= (1 << KeptBits)
4359 break;
4360 // FIXME: are more folds possible?
4361 default:
4362 return nullptr;
4363 }
4364
4365 auto *XType = X->getType();
4366 const unsigned XBitWidth = XType->getScalarSizeInBits();
4367 const APInt BitWidth = APInt(XBitWidth, XBitWidth);
4368 assert(BitWidth.ugt(MaskedBits) && "shifts should leave some bits untouched");
4369
4370 // KeptBits = bitwidth(%x) - MaskedBits
4371 const APInt KeptBits = BitWidth - MaskedBits;
4372 assert(KeptBits.ugt(0) && KeptBits.ult(BitWidth) && "unreachable");
4373 // ICmpCst = (1 << KeptBits)
4374 const APInt ICmpCst = APInt(XBitWidth, 1).shl(KeptBits);
4375 assert(ICmpCst.isPowerOf2());
4376 // AddCst = (1 << (KeptBits-1))
4377 const APInt AddCst = ICmpCst.lshr(1);
4378 assert(AddCst.ult(ICmpCst) && AddCst.isPowerOf2());
4379
4380 // T0 = add %x, AddCst
4381 Value *T0 = Builder.CreateAdd(X, ConstantInt::get(XType, AddCst));
4382 // T1 = T0 DstPred ICmpCst
4383 Value *T1 = Builder.CreateICmp(DstPred, T0, ConstantInt::get(XType, ICmpCst));
4384
4385 return T1;
4386}
4387
4388// Given pattern:
4389// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4390// we should move shifts to the same hand of 'and', i.e. rewrite as
4391// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4392// We are only interested in opposite logical shifts here.
4393// One of the shifts can be truncated.
4394// If we can, we want to end up creating 'lshr' shift.
4395static Value *
4397 InstCombiner::BuilderTy &Builder) {
4398 if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
4399 !I.getOperand(0)->hasOneUse())
4400 return nullptr;
4401
4402 auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
4403
4404 // Look for an 'and' of two logical shifts, one of which may be truncated.
4405 // We use m_TruncOrSelf() on the RHS to correctly handle commutative case.
4406 Instruction *XShift, *MaybeTruncation, *YShift;
4407 if (!match(
4408 I.getOperand(0),
4409 m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
4411 m_AnyLogicalShift, m_Instruction(YShift))),
4412 m_Instruction(MaybeTruncation)))))
4413 return nullptr;
4414
4415 // We potentially looked past 'trunc', but only when matching YShift,
4416 // therefore YShift must have the widest type.
4417 Instruction *WidestShift = YShift;
4418 // Therefore XShift must have the shallowest type.
4419 // Or they both have identical types if there was no truncation.
4420 Instruction *NarrowestShift = XShift;
4421
4422 Type *WidestTy = WidestShift->getType();
4423 Type *NarrowestTy = NarrowestShift->getType();
4424 assert(NarrowestTy == I.getOperand(0)->getType() &&
4425 "We did not look past any shifts while matching XShift though.");
4426 bool HadTrunc = WidestTy != I.getOperand(0)->getType();
4427
4428 // If YShift is a 'lshr', swap the shifts around.
4429 if (match(YShift, m_LShr(m_Value(), m_Value())))
4430 std::swap(XShift, YShift);
4431
4432 // The shifts must be in opposite directions.
4433 auto XShiftOpcode = XShift->getOpcode();
4434 if (XShiftOpcode == YShift->getOpcode())
4435 return nullptr; // Do not care about same-direction shifts here.
4436
4437 Value *X, *XShAmt, *Y, *YShAmt;
4438 match(XShift, m_BinOp(m_Value(X), m_ZExtOrSelf(m_Value(XShAmt))));
4439 match(YShift, m_BinOp(m_Value(Y), m_ZExtOrSelf(m_Value(YShAmt))));
4440
4441 // If one of the values being shifted is a constant, then we will end with
4442 // and+icmp, and [zext+]shift instrs will be constant-folded. If they are not,
4443 // however, we will need to ensure that we won't increase instruction count.
4444 if (!isa<Constant>(X) && !isa<Constant>(Y)) {
4445 // At least one of the hands of the 'and' should be one-use shift.
4446 if (!match(I.getOperand(0),
4447 m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
4448 return nullptr;
4449 if (HadTrunc) {
4450 // Due to the 'trunc', we will need to widen X. For that either the old
4451 // 'trunc' or the shift amt in the non-truncated shift should be one-use.
4452 if (!MaybeTruncation->hasOneUse() &&
4453 !NarrowestShift->getOperand(1)->hasOneUse())
4454 return nullptr;
4455 }
4456 }
4457
4458 // We have two shift amounts from two different shifts. The types of those
4459 // shift amounts may not match. If that's the case let's bailout now.
4460 if (XShAmt->getType() != YShAmt->getType())
4461 return nullptr;
4462
4463 // As input, we have the following pattern:
4464 // icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
4465 // We want to rewrite that as:
4466 // icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
4467 // While we know that originally (Q+K) would not overflow
4468 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
4469 // shift amounts. so it may now overflow in smaller bitwidth.
4470 // To ensure that does not happen, we need to ensure that the total maximal
4471 // shift amount is still representable in that smaller bit width.
4472 unsigned MaximalPossibleTotalShiftAmount =
4473 (WidestTy->getScalarSizeInBits() - 1) +
4474 (NarrowestTy->getScalarSizeInBits() - 1);
4475 APInt MaximalRepresentableShiftAmount =
4477 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
4478 return nullptr;
4479
4480 // Can we fold (XShAmt+YShAmt) ?
4481 auto *NewShAmt = dyn_cast_or_null<Constant>(
4482 simplifyAddInst(XShAmt, YShAmt, /*isNSW=*/false,
4483 /*isNUW=*/false, SQ.getWithInstruction(&I)));
4484 if (!NewShAmt)
4485 return nullptr;
4486 if (NewShAmt->getType() != WidestTy) {
4487 NewShAmt =
4488 ConstantFoldCastOperand(Instruction::ZExt, NewShAmt, WidestTy, SQ.DL);
4489 if (!NewShAmt)
4490 return nullptr;
4491 }
4492 unsigned WidestBitWidth = WidestTy->getScalarSizeInBits();
4493
4494 // Is the new shift amount smaller than the bit width?
4495 // FIXME: could also rely on ConstantRange.
4496 if (!match(NewShAmt,
4498 APInt(WidestBitWidth, WidestBitWidth))))
4499 return nullptr;
4500
4501 // An extra legality check is needed if we had trunc-of-lshr.
4502 if (HadTrunc && match(WidestShift, m_LShr(m_Value(), m_Value()))) {
4503 auto CanFold = [NewShAmt, WidestBitWidth, NarrowestShift, SQ,
4504 WidestShift]() {
4505 // It isn't obvious whether it's worth it to analyze non-constants here.
4506 // Also, let's basically give up on non-splat cases, pessimizing vectors.
4507 // If *any* of these preconditions matches we can perform the fold.
4508 Constant *NewShAmtSplat = NewShAmt->getType()->isVectorTy()
4509 ? NewShAmt->getSplatValue()
4510 : NewShAmt;
4511 // If it's edge-case shift (by 0 or by WidestBitWidth-1) we can fold.
4512 if (NewShAmtSplat &&
4513 (NewShAmtSplat->isNullValue() ||
4514 NewShAmtSplat->getUniqueInteger() == WidestBitWidth - 1))
4515 return true;
4516 // We consider *min* leading zeros so a single outlier
4517 // blocks the transform as opposed to allowing it.
4518 if (auto *C = dyn_cast<Constant>(NarrowestShift->getOperand(0))) {
4519 KnownBits Known = computeKnownBits(C, SQ.DL);
4520 unsigned MinLeadZero = Known.countMinLeadingZeros();
4521 // If the value being shifted has at most lowest bit set we can fold.
4522 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4523 if (MaxActiveBits <= 1)
4524 return true;
4525 // Precondition: NewShAmt u<= countLeadingZeros(C)
4526 if (NewShAmtSplat && NewShAmtSplat->getUniqueInteger().ule(MinLeadZero))
4527 return true;
4528 }
4529 if (auto *C = dyn_cast<Constant>(WidestShift->getOperand(0))) {
4530 KnownBits Known = computeKnownBits(C, SQ.DL);
4531 unsigned MinLeadZero = Known.countMinLeadingZeros();
4532 // If the value being shifted has at most lowest bit set we can fold.
4533 unsigned MaxActiveBits = Known.getBitWidth() - MinLeadZero;
4534 if (MaxActiveBits <= 1)
4535 return true;
4536 // Precondition: ((WidestBitWidth-1)-NewShAmt) u<= countLeadingZeros(C)
4537 if (NewShAmtSplat) {
4538 APInt AdjNewShAmt =
4539 (WidestBitWidth - 1) - NewShAmtSplat->getUniqueInteger();
4540 if (AdjNewShAmt.ule(MinLeadZero))
4541 return true;
4542 }
4543 }
4544 return false; // Can't tell if it's ok.
4545 };
4546 if (!CanFold())
4547 return nullptr;
4548 }
4549
4550 // All good, we can do this fold.
4551 X = Builder.CreateZExt(X, WidestTy);
4552 Y = Builder.CreateZExt(Y, WidestTy);
4553 // The shift is the same that was for X.
4554 Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
4555 ? Builder.CreateLShr(X, NewShAmt)
4556 : Builder.CreateShl(X, NewShAmt);
4557 Value *T1 = Builder.CreateAnd(T0, Y);
4558 return Builder.CreateICmp(I.getPredicate(), T1,
4559 Constant::getNullValue(WidestTy));
4560}
4561
4562/// Fold
4563/// (-1 u/ x) u< y
4564/// ((x * y) ?/ x) != y
4565/// to
4566/// @llvm.?mul.with.overflow(x, y) plus extraction of overflow bit
4567/// Note that the comparison is commutative, while inverted (u>=, ==) predicate
4568/// will mean that we are looking for the opposite answer.
4571 Value *X, *Y;
4573 Instruction *Div;
4574 bool NeedNegation;
4575 // Look for: (-1 u/ x) u</u>= y
4576 if (!I.isEquality() &&
4577 match(&I, m_c_ICmp(Pred,
4579 m_Instruction(Div)),
4580 m_Value(Y)))) {
4581 Mul = nullptr;
4582
4583 // Are we checking that overflow does not happen, or does happen?
4584 switch (Pred) {
4586 NeedNegation = false;
4587 break; // OK
4589 NeedNegation = true;
4590 break; // OK
4591 default:
4592 return nullptr; // Wrong predicate.
4593 }
4594 } else // Look for: ((x * y) / x) !=/== y
4595 if (I.isEquality() &&
4596 match(&I,
4597 m_c_ICmp(Pred, m_Value(Y),
4600 m_Value(X)),
4602 m_Deferred(X))),
4603 m_Instruction(Div))))) {
4604 NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ;
4605 } else
4606 return nullptr;
4607
4609 // If the pattern included (x * y), we'll want to insert new instructions
4610 // right before that original multiplication so that we can replace it.
4611 bool MulHadOtherUses = Mul && !Mul->hasOneUse();
4612 if (MulHadOtherUses)
4614
4615 Function *F = Intrinsic::getDeclaration(I.getModule(),
4616 Div->getOpcode() == Instruction::UDiv
4617 ? Intrinsic::umul_with_overflow
4618 : Intrinsic::smul_with_overflow,
4619 X->getType());
4620 CallInst *Call = Builder.CreateCall(F, {X, Y}, "mul");
4621
4622 // If the multiplication was used elsewhere, to ensure that we don't leave
4623 // "duplicate" instructions, replace uses of that original multiplication
4624 // with the multiplication result from the with.overflow intrinsic.
4625 if (MulHadOtherUses)
4626 replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "mul.val"));
4627
4628 Value *Res = Builder.CreateExtractValue(Call, 1, "mul.ov");
4629 if (NeedNegation) // This technically increases instruction count.
4630 Res = Builder.CreateNot(Res, "mul.not.ov");
4631
4632 // If we replaced the mul, erase it. Do this after all uses of Builder,
4633 // as the mul is used as insertion point.
4634 if (MulHadOtherUses)
4636
4637 return Res;
4638}
4639
4641 InstCombiner::BuilderTy &Builder) {
4642 CmpInst::Predicate Pred;
4643 Value *X;
4644 if (match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X)))) {
4645
4646 if (ICmpInst::isSigned(Pred))
4647 Pred = ICmpInst::getSwappedPredicate(Pred);
4648 else if (ICmpInst::isUnsigned(Pred))
4649 Pred = ICmpInst::getSignedPredicate(Pred);
4650 // else for equality-comparisons just keep the predicate.
4651
4652 return ICmpInst::Create(Instruction::ICmp, Pred, X,
4653 Constant::getNullValue(X->getType()), I.getName());
4654 }
4655
4656 // A value is not equal to its negation unless that value is 0 or
4657 // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
4658 if (match(&I, m_c_ICmp(Pred, m_OneUse(m_Neg(m_Value(X))), m_Deferred(X))) &&
4659 ICmpInst::isEquality(Pred)) {
4660 Type *Ty = X->getType();
4662 Constant *MaxSignedVal =
4663 ConstantInt::get(Ty, APInt::getSignedMaxValue(BitWidth));
4664 Value *And = Builder.CreateAnd(X, MaxSignedVal);
4665 Constant *Zero = Constant::getNullValue(Ty);
4666 return CmpInst::Create(Instruction::ICmp, Pred, And, Zero);
4667 }
4668
4669 return nullptr;
4670}
4671
4673 InstCombinerImpl &IC) {
4674 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4675 // Normalize and operand as operand 0.
4676 CmpInst::Predicate Pred = I.getPredicate();
4677 if (match(Op1, m_c_And(m_Specific(Op0), m_Value()))) {
4678 std::swap(Op0, Op1);
4679 Pred = ICmpInst::getSwappedPredicate(Pred);
4680 }
4681
4682 if (!match(Op0, m_c_And(m_Specific(Op1), m_Value(A))))
4683 return nullptr;
4684
4685 // (icmp (X & Y) u< X --> (X & Y) != X
4686 if (Pred == ICmpInst::ICMP_ULT)
4687 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4688
4689 // (icmp (X & Y) u>= X --> (X & Y) == X
4690 if (Pred == ICmpInst::ICMP_UGE)
4691 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4692
4693 return nullptr;
4694}
4695
4697 InstCombinerImpl &IC) {
4698 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4699
4700 // Normalize or operand as operand 0.
4701 CmpInst::Predicate Pred = I.getPredicate();
4702 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value(A)))) {
4703 std::swap(Op0, Op1);
4704 Pred = ICmpInst::getSwappedPredicate(Pred);
4705 } else if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) {
4706 return nullptr;
4707 }
4708
4709 // icmp (X | Y) u<= X --> (X | Y) == X
4710 if (Pred == ICmpInst::ICMP_ULE)
4711 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
4712
4713 // icmp (X | Y) u> X --> (X | Y) != X
4714 if (Pred == ICmpInst::ICMP_UGT)
4715 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4716
4717 if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) {
4718 // icmp (X | Y) eq/ne Y --> (X & ~Y) eq/ne 0 if Y is freely invertible
4719 if (Value *NotOp1 =
4720 IC.getFreelyInverted(Op1, Op1->hasOneUse(), &IC.Builder))
4721 return new ICmpInst(Pred, IC.Builder.CreateAnd(A, NotOp1),
4722 Constant::getNullValue(Op1->getType()));
4723 // icmp (X | Y) eq/ne Y --> (~X | Y) eq/ne -1 if X is freely invertible.
4724 if (Value *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
4725 return new ICmpInst(Pred, IC.Builder.CreateOr(Op1, NotA),
4726 Constant::getAllOnesValue(Op1->getType()));
4727 }
4728 return nullptr;
4729}
4730
4732 InstCombinerImpl &IC) {
4733 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
4734 // Normalize xor operand as operand 0.
4735 CmpInst::Predicate Pred = I.getPredicate();
4736 if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
4737 std::swap(Op0, Op1);
4738 Pred = ICmpInst::getSwappedPredicate(Pred);
4739 }
4740 if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
4741 return nullptr;
4742
4743 // icmp (X ^ Y_NonZero) u>= X --> icmp (X ^ Y_NonZero) u> X
4744 // icmp (X ^ Y_NonZero) u<= X --> icmp (X ^ Y_NonZero) u< X
4745 // icmp (X ^ Y_NonZero) s>= X --> icmp (X ^ Y_NonZero) s> X
4746 // icmp (X ^ Y_NonZero) s<= X --> icmp (X ^ Y_NonZero) s< X
4748 if (PredOut != Pred && isKnownNonZero(A, Q))
4749 return new ICmpInst(PredOut, Op0, Op1);
4750
4751 return nullptr;
4752}
4753
4754/// Try to fold icmp (binop), X or icmp X, (binop).
4755/// TODO: A large part of this logic is duplicated in InstSimplify's
4756/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
4757/// duplication.
4759 const SimplifyQuery &SQ) {
4761 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4762
4763 // Special logic for binary operators.
4764 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
4765 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
4766 if (!BO0 && !BO1)
4767 return nullptr;
4768
4769 if (Instruction *NewICmp = foldICmpXNegX(I, Builder))
4770 return NewICmp;
4771
4772 const CmpInst::Predicate Pred = I.getPredicate();
4773 Value *X;
4774
4775 // Convert add-with-unsigned-overflow comparisons into a 'not' with compare.
4776 // (Op1 + X) u</u>= Op1 --> ~Op1 u</u>= X
4777 if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) &&
4778 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
4779 return new ICmpInst(Pred, Builder.CreateNot(Op1), X);
4780 // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0
4781 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) &&
4782 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
4783 return new ICmpInst(Pred, X, Builder.CreateNot(Op0));
4784
4785 {
4786 // (Op1 + X) + C u</u>= Op1 --> ~C - X u</u>= Op1
4787 Constant *C;
4788 if (match(Op0, m_OneUse(m_Add(m_c_Add(m_Specific(Op1), m_Value(X)),
4789 m_ImmConstant(C)))) &&
4790 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) {
4792 return new ICmpInst(Pred, Builder.CreateSub(C2, X), Op1);
4793 }
4794 // Op0 u>/u<= (Op0 + X) + C --> Op0 u>/u<= ~C - X
4795 if (match(Op1, m_OneUse(m_Add(m_c_Add(m_Specific(Op0), m_Value(X)),
4796 m_ImmConstant(C)))) &&
4797 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
4799 return new ICmpInst(Pred, Op0, Builder.CreateSub(C2, X));
4800 }
4801 }
4802
4803 {
4804 // Similar to above: an unsigned overflow comparison may use offset + mask:
4805 // ((Op1 + C) & C) u< Op1 --> Op1 != 0
4806 // ((Op1 + C) & C) u>= Op1 --> Op1 == 0
4807 // Op0 u> ((Op0 + C) & C) --> Op0 != 0
4808 // Op0 u<= ((Op0 + C) & C) --> Op0 == 0
4809 BinaryOperator *BO;
4810 const APInt *C;
4811 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE) &&
4812 match(Op0, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
4814 CmpInst::Predicate NewPred =
4816 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
4817 return new ICmpInst(NewPred, Op1, Zero);
4818 }
4819
4820 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
4821 match(Op1, m_And(m_BinOp(BO), m_LowBitMask(C))) &&
4823 CmpInst::Predicate NewPred =
4825 Constant *Zero = ConstantInt::getNullValue(Op1->getType());
4826 return new ICmpInst(NewPred, Op0, Zero);
4827 }
4828 }
4829
4830 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
4831 bool Op0HasNUW = false, Op1HasNUW = false;
4832 bool Op0HasNSW = false, Op1HasNSW = false;
4833 // Analyze the case when either Op0 or Op1 is an add instruction.
4834 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
4835 auto hasNoWrapProblem = [](const BinaryOperator &BO, CmpInst::Predicate Pred,
4836 bool &HasNSW, bool &HasNUW) -> bool {
4837 if (isa<OverflowingBinaryOperator>(BO)) {
4838 HasNUW = BO.hasNoUnsignedWrap();
4839 HasNSW = BO.hasNoSignedWrap();
4840 return ICmpInst::isEquality(Pred) ||
4841 (CmpInst::isUnsigned(Pred) && HasNUW) ||
4842 (CmpInst::isSigned(Pred) && HasNSW);
4843 } else if (BO.getOpcode() == Instruction::Or) {
4844 HasNUW = true;
4845 HasNSW = true;
4846 return true;
4847 } else {
4848 return false;
4849 }
4850 };
4851 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
4852
4853 if (BO0) {
4854 match(BO0, m_AddLike(m_Value(A), m_Value(B)));
4855 NoOp0WrapProblem = hasNoWrapProblem(*BO0, Pred, Op0HasNSW, Op0HasNUW);
4856 }
4857 if (BO1) {
4858 match(BO1, m_AddLike(m_Value(C), m_Value(D)));
4859 NoOp1WrapProblem = hasNoWrapProblem(*BO1, Pred, Op1HasNSW, Op1HasNUW);
4860 }
4861
4862 // icmp (A+B), A -> icmp B, 0 for equalities or if there is no overflow.
4863 // icmp (A+B), B -> icmp A, 0 for equalities or if there is no overflow.
4864 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
4865 return new ICmpInst(Pred, A == Op1 ? B : A,
4866 Constant::getNullValue(Op1->getType()));
4867
4868 // icmp C, (C+D) -> icmp 0, D for equalities or if there is no overflow.
4869 // icmp D, (C+D) -> icmp 0, C for equalities or if there is no overflow.
4870 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
4871 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
4872 C == Op0 ? D : C);
4873
4874 // icmp (A+B), (A+D) -> icmp B, D for equalities or if there is no overflow.
4875 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
4876 NoOp1WrapProblem) {
4877 // Determine Y and Z in the form icmp (X+Y), (X+Z).
4878 Value *Y, *Z;
4879 if (A == C) {
4880 // C + B == C + D -> B == D
4881 Y = B;
4882 Z = D;
4883 } else if (A == D) {
4884 // D + B == C + D -> B == C
4885 Y = B;
4886 Z = C;
4887 } else if (B == C) {
4888 // A + C == C + D -> A == D
4889 Y = A;
4890 Z = D;
4891 } else {
4892 assert(B == D);
4893 // A + D == C + D -> A == C
4894 Y = A;
4895 Z = C;
4896 }
4897 return new ICmpInst(Pred, Y, Z);
4898 }
4899
4900 // icmp slt (A + -1), Op1 -> icmp sle A, Op1
4901 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
4902 match(B, m_AllOnes()))
4903 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
4904
4905 // icmp sge (A + -1), Op1 -> icmp sgt A, Op1
4906 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
4907 match(B, m_AllOnes()))
4908 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
4909
4910 // icmp sle (A + 1), Op1 -> icmp slt A, Op1
4911 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
4912 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
4913
4914 // icmp sgt (A + 1), Op1 -> icmp sge A, Op1
4915 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
4916 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
4917
4918 // icmp sgt Op0, (C + -1) -> icmp sge Op0, C
4919 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
4920 match(D, m_AllOnes()))
4921 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
4922
4923 // icmp sle Op0, (C + -1) -> icmp slt Op0, C
4924 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
4925 match(D, m_AllOnes()))
4926 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
4927
4928 // icmp sge Op0, (C + 1) -> icmp sgt Op0, C
4929 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
4930 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
4931
4932 // icmp slt Op0, (C + 1) -> icmp sle Op0, C
4933 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
4934 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
4935
4936 // TODO: The subtraction-related identities shown below also hold, but
4937 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
4938 // wouldn't happen even if they were implemented.
4939 //
4940 // icmp ult (A - 1), Op1 -> icmp ule A, Op1
4941 // icmp uge (A - 1), Op1 -> icmp ugt A, Op1
4942 // icmp ugt Op0, (C - 1) -> icmp uge Op0, C
4943 // icmp ule Op0, (C - 1) -> icmp ult Op0, C
4944
4945 // icmp ule (A + 1), Op0 -> icmp ult A, Op1
4946 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One()))
4947 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1);
4948
4949 // icmp ugt (A + 1), Op0 -> icmp uge A, Op1
4950 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One()))
4951 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1);
4952
4953 // icmp uge Op0, (C + 1) -> icmp ugt Op0, C
4954 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One()))
4955 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C);
4956
4957 // icmp ult Op0, (C + 1) -> icmp ule Op0, C
4958 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One()))
4959 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C);
4960
4961 // if C1 has greater magnitude than C2:
4962 // icmp (A + C1), (C + C2) -> icmp (A + C3), C
4963 // s.t. C3 = C1 - C2
4964 //
4965 // if C2 has greater magnitude than C1:
4966 // icmp (A + C1), (C + C2) -> icmp A, (C + C3)
4967 // s.t. C3 = C2 - C1
4968 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
4969 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
4970 const APInt *AP1, *AP2;
4971 // TODO: Support non-uniform vectors.
4972 // TODO: Allow poison passthrough if B or D's element is poison.
4973 if (match(B, m_APIntAllowPoison(AP1)) &&
4974 match(D, m_APIntAllowPoison(AP2)) &&
4975 AP1->isNegative() == AP2->isNegative()) {
4976 APInt AP1Abs = AP1->abs();
4977 APInt AP2Abs = AP2->abs();
4978 if (AP1Abs.uge(AP2Abs)) {
4979 APInt Diff = *AP1 - *AP2;
4980 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
4981 Value *NewAdd = Builder.CreateAdd(
4982 A, C3, "", Op0HasNUW && Diff.ule(*AP1), Op0HasNSW);
4983 return new ICmpInst(Pred, NewAdd, C);
4984 } else {
4985 APInt Diff = *AP2 - *AP1;
4986 Constant *C3 = Constant::getIntegerValue(BO0->getType(), Diff);
4987 Value *NewAdd = Builder.CreateAdd(
4988 C, C3, "", Op1HasNUW && Diff.ule(*AP2), Op1HasNSW);
4989 return new ICmpInst(Pred, A, NewAdd);
4990 }
4991 }
4992 Constant *Cst1, *Cst2;
4993 if (match(B, m_ImmConstant(Cst1)) && match(D, m_ImmConstant(Cst2)) &&
4994 ICmpInst::isEquality(Pred)) {
4995 Constant *Diff = ConstantExpr::getSub(Cst2, Cst1);
4996 Value *NewAdd = Builder.CreateAdd(C, Diff);
4997 return new ICmpInst(Pred, A, NewAdd);
4998 }
4999 }
5000
5001 // Analyze the case when either Op0 or Op1 is a sub instruction.
5002 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
5003 A = nullptr;
5004 B = nullptr;
5005 C = nullptr;
5006 D = nullptr;
5007 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
5008 A = BO0->getOperand(0);
5009 B = BO0->getOperand(1);
5010 }
5011 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
5012 C = BO1->getOperand(0);
5013 D = BO1->getOperand(1);
5014 }
5015
5016 // icmp (A-B), A -> icmp 0, B for equalities or if there is no overflow.
5017 if (A == Op1 && NoOp0WrapProblem)
5018 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
5019 // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow.
5020 if (C == Op0 && NoOp1WrapProblem)
5021 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
5022
5023 // Convert sub-with-unsigned-overflow comparisons into a comparison of args.
5024 // (A - B) u>/u<= A --> B u>/u<= A
5025 if (A == Op1 && (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE))
5026 return new ICmpInst(Pred, B, A);
5027 // C u</u>= (C - D) --> C u</u>= D
5028 if (C == Op0 && (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE))
5029 return new ICmpInst(Pred, C, D);
5030 // (A - B) u>=/u< A --> B u>/u<= A iff B != 0
5031 if (A == Op1 && (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5032 isKnownNonZero(B, Q))
5034 // C u<=/u> (C - D) --> C u</u>= D iff B != 0
5035 if (C == Op0 && (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT) &&
5036 isKnownNonZero(D, Q))
5038
5039 // icmp (A-B), (C-B) -> icmp A, C for equalities or if there is no overflow.
5040 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem)
5041 return new ICmpInst(Pred, A, C);
5042
5043 // icmp (A-B), (A-D) -> icmp D, B for equalities or if there is no overflow.
5044 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem)
5045 return new ICmpInst(Pred, D, B);
5046
5047 // icmp (0-X) < cst --> x > -cst
5048 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
5049 Value *X;
5050 if (match(BO0, m_Neg(m_Value(X))))
5051 if (Constant *RHSC = dyn_cast<Constant>(Op1))
5052 if (RHSC->isNotMinSignedValue())
5053 return new ICmpInst(I.getSwappedPredicate(), X,
5054 ConstantExpr::getNeg(RHSC));
5055 }
5056
5057 if (Instruction * R = foldICmpXorXX(I, Q, *this))
5058 return R;
5059 if (Instruction *R = foldICmpOrXX(I, Q, *this))
5060 return R;
5061
5062 {
5063 // Try to remove shared multiplier from comparison:
5064 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z
5065 Value *X, *Y, *Z;
5066 if (Pred == ICmpInst::getUnsignedPredicate(Pred) &&
5067 ((match(Op0, m_Mul(m_Value(X), m_Value(Z))) &&
5068 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))) ||
5069 (match(Op0, m_Mul(m_Value(Z), m_Value(X))) &&
5070 match(Op1, m_c_Mul(m_Specific(Z), m_Value(Y)))))) {
5071 bool NonZero;
5072 if (ICmpInst::isEquality(Pred)) {
5073 KnownBits ZKnown = computeKnownBits(Z, 0, &I);
5074 // if Z % 2 != 0
5075 // X * Z eq/ne Y * Z -> X eq/ne Y
5076 if (ZKnown.countMaxTrailingZeros() == 0)
5077 return new ICmpInst(Pred, X, Y);
5078 NonZero = !ZKnown.One.isZero() || isKnownNonZero(Z, Q);
5079 // if Z != 0 and nsw(X * Z) and nsw(Y * Z)
5080 // X * Z eq/ne Y * Z -> X eq/ne Y
5081 if (NonZero && BO0 && BO1 && Op0HasNSW && Op1HasNSW)
5082 return new ICmpInst(Pred, X, Y);
5083 } else
5084 NonZero = isKnownNonZero(Z, Q);
5085
5086 // If Z != 0 and nuw(X * Z) and nuw(Y * Z)
5087 // X * Z u{lt/le/gt/ge}/eq/ne Y * Z -> X u{lt/le/gt/ge}/eq/ne Y
5088 if (NonZero && BO0 && BO1 && Op0HasNUW && Op1HasNUW)
5089 return new ICmpInst(Pred, X, Y);
5090 }
5091 }
5092
5093 BinaryOperator *SRem = nullptr;
5094 // icmp (srem X, Y), Y
5095 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
5096 SRem = BO0;
5097 // icmp Y, (srem X, Y)
5098 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
5099 Op0 == BO1->getOperand(1))
5100 SRem = BO1;
5101 if (SRem) {
5102 // We don't check hasOneUse to avoid increasing register pressure because
5103 // the value we use is the same value this instruction was already using.
5104 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
5105 default:
5106 break;
5107 case ICmpInst::ICMP_EQ:
5108 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5109 case ICmpInst::ICMP_NE:
5110 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5111 case ICmpInst::ICMP_SGT:
5112 case ICmpInst::ICMP_SGE:
5113 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
5115 case ICmpInst::ICMP_SLT:
5116 case ICmpInst::ICMP_SLE:
5117 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
5119 }
5120 }
5121
5122 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
5123 (BO0->hasOneUse() || BO1->hasOneUse()) &&
5124 BO0->getOperand(1) == BO1->getOperand(1)) {
5125 switch (BO0->getOpcode()) {
5126 default:
5127 break;
5128 case Instruction::Add:
5129 case Instruction::Sub:
5130 case Instruction::Xor: {
5131 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
5132 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5133
5134 const APInt *C;
5135 if (match(BO0->getOperand(1), m_APInt(C))) {
5136 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
5137 if (C->isSignMask()) {
5138 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5139 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5140 }
5141
5142 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
5143 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
5144 ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
5145 NewPred = I.getSwappedPredicate(NewPred);
5146 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
5147 }
5148 }
5149 break;
5150 }
5151 case Instruction::Mul: {
5152 if (!I.isEquality())
5153 break;
5154
5155 const APInt *C;
5156 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isZero() &&
5157 !C->isOne()) {
5158 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
5159 // Mask = -1 >> count-trailing-zeros(C).
5160 if (unsigned TZs = C->countr_zero()) {
5161 Constant *Mask = ConstantInt::get(
5162 BO0->getType(),
5163 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
5164 Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask);
5165 Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask);
5166 return new ICmpInst(Pred, And1, And2);
5167 }
5168 }
5169 break;
5170 }
5171 case Instruction::UDiv:
5172 case Instruction::LShr:
5173 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
5174 break;
5175 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5176
5177 case Instruction::SDiv:
5178 if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) ||
5179 !BO0->isExact() || !BO1->isExact())
5180 break;
5181 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5182
5183 case Instruction::AShr:
5184 if (!BO0->isExact() || !BO1->isExact())
5185 break;
5186 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5187
5188 case Instruction::Shl: {
5189 bool NUW = Op0HasNUW && Op1HasNUW;
5190 bool NSW = Op0HasNSW && Op1HasNSW;
5191 if (!NUW && !NSW)
5192 break;
5193 if (!NSW && I.isSigned())
5194 break;
5195 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
5196 }
5197 }
5198 }
5199
5200 if (BO0) {
5201 // Transform A & (L - 1) `ult` L --> L != 0
5202 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
5203 auto BitwiseAnd = m_c_And(m_Value(), LSubOne);
5204
5205 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
5206 auto *Zero = Constant::getNullValue(BO0->getType());
5207 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
5208 }
5209 }
5210
5211 // For unsigned predicates / eq / ne:
5212 // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0
5213 // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x
5214 if (!ICmpInst::isSigned(Pred)) {
5215 if (match(Op0, m_Shl(m_Specific(Op1), m_One())))
5216 return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1,
5217 Constant::getNullValue(Op1->getType()));
5218 else if (match(Op1, m_Shl(m_Specific(Op0), m_One())))
5219 return new ICmpInst(ICmpInst::getSignedPredicate(Pred),
5220 Constant::getNullValue(Op0->getType()), Op0);
5221 }
5222
5224 return replaceInstUsesWith(I, V);
5225
5226 if (Instruction *R = foldICmpAndXX(I, Q, *this))
5227 return R;
5228
5230 return replaceInstUsesWith(I, V);
5231
5233 return replaceInstUsesWith(I, V);
5234
5235 return nullptr;
5236}
5237
5238/// Fold icmp Pred min|max(X, Y), Z.
5241 Value *Z,
5242 ICmpInst::Predicate Pred) {
5243 Value *X = MinMax->getLHS();
5244 Value *Y = MinMax->getRHS();
5245 if (ICmpInst::isSigned(Pred) && !MinMax->isSigned())
5246 return nullptr;
5247 if (ICmpInst::isUnsigned(Pred) && MinMax->isSigned()) {
5248 // Revert the transform signed pred -> unsigned pred
5249 // TODO: We can flip the signedness of predicate if both operands of icmp
5250 // are negative.
5254 } else
5255 return nullptr;
5256 }
5258 auto IsCondKnownTrue = [](Value *Val) -> std::optional<bool> {
5259 if (!Val)
5260 return std::nullopt;
5261 if (match(Val, m_One()))
5262 return true;
5263 if (match(Val, m_Zero()))
5264 return false;
5265 return std::nullopt;
5266 };
5267 auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
5268 auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
5269 if (!CmpXZ.has_value() && !CmpYZ.has_value())
5270 return nullptr;
5271 if (!CmpXZ.has_value()) {
5272 std::swap(X, Y);
5273 std::swap(CmpXZ, CmpYZ);
5274 }
5275
5276 auto FoldIntoCmpYZ = [&]() -> Instruction * {
5277 if (CmpYZ.has_value())
5278 return replaceInstUsesWith(I, ConstantInt::getBool(I.getType(), *CmpYZ));
5279 return ICmpInst::Create(Instruction::ICmp, Pred, Y, Z);
5280 };
5281
5282 switch (Pred) {
5283 case ICmpInst::ICMP_EQ:
5284 case ICmpInst::ICMP_NE: {
5285 // If X == Z:
5286 // Expr Result
5287 // min(X, Y) == Z X <= Y
5288 // max(X, Y) == Z X >= Y
5289 // min(X, Y) != Z X > Y
5290 // max(X, Y) != Z X < Y
5291 if ((Pred == ICmpInst::ICMP_EQ) == *CmpXZ) {
5292 ICmpInst::Predicate NewPred =
5293 ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
5294 if (Pred == ICmpInst::ICMP_NE)
5295 NewPred = ICmpInst::getInversePredicate(NewPred);
5296 return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
5297 }
5298 // Otherwise (X != Z):
5299 ICmpInst::Predicate NewPred = MinMax->getPredicate();
5300 auto MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5301 if (!MinMaxCmpXZ.has_value()) {
5302 std::swap(X, Y);
5303 std::swap(CmpXZ, CmpYZ);
5304 // Re-check pre-condition X != Z
5305 if (!CmpXZ.has_value() || (Pred == ICmpInst::ICMP_EQ) == *CmpXZ)
5306 break;
5307 MinMaxCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
5308 }
5309 if (!MinMaxCmpXZ.has_value())
5310 break;
5311 if (*MinMaxCmpXZ) {
5312 // Expr Fact Result
5313 // min(X, Y) == Z X < Z false
5314 // max(X, Y) == Z X > Z false
5315 // min(X, Y) != Z X < Z true
5316 // max(X, Y) != Z X > Z true
5317 return replaceInstUsesWith(
5318 I, ConstantInt::getBool(I.getType(), Pred == ICmpInst::ICMP_NE));
5319 } else {
5320 // Expr Fact Result
5321 // min(X, Y) == Z X > Z Y == Z
5322 // max(X, Y) == Z X < Z Y == Z
5323 // min(X, Y) != Z X > Z Y != Z
5324 // max(X, Y) != Z X < Z Y != Z
5325 return FoldIntoCmpYZ();
5326 }
5327 break;
5328 }
5329 case ICmpInst::ICMP_SLT:
5330 case ICmpInst::ICMP_ULT:
5331 case ICmpInst::ICMP_SLE:
5332 case ICmpInst::ICMP_ULE:
5333 case ICmpInst::ICMP_SGT:
5334 case ICmpInst::ICMP_UGT:
5335 case ICmpInst::ICMP_SGE:
5336 case ICmpInst::ICMP_UGE: {
5337 bool IsSame = MinMax->getPredicate() == ICmpInst::getStrictPredicate(Pred);
5338 if (*CmpXZ) {
5339 if (IsSame) {
5340 // Expr Fact Result
5341 // min(X, Y) < Z X < Z true
5342 // min(X, Y) <= Z X <= Z true
5343 // max(X, Y) > Z X > Z true
5344 // max(X, Y) >= Z X >= Z true
5345 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
5346 } else {
5347 // Expr Fact Result
5348 // max(X, Y) < Z X < Z Y < Z
5349 // max(X, Y) <= Z X <= Z Y <= Z
5350 // min(X, Y) > Z X > Z Y > Z
5351 // min(X, Y) >= Z X >= Z Y >= Z
5352 return FoldIntoCmpYZ();
5353 }
5354 } else {
5355 if (IsSame) {
5356 // Expr Fact Result
5357 // min(X, Y) < Z X >= Z Y < Z
5358 // min(X, Y) <= Z X > Z Y <= Z
5359 // max(X, Y) > Z X <= Z Y > Z
5360 // max(X, Y) >= Z X < Z Y >= Z
5361 return FoldIntoCmpYZ();
5362 } else {
5363 // Expr Fact Result
5364 // max(X, Y) < Z X >= Z false
5365 // max(X, Y) <= Z X > Z false
5366 // min(X, Y) > Z X <= Z false
5367 // min(X, Y) >= Z X < Z false
5368 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
5369 }
5370 }
5371 break;
5372 }
5373 default:
5374 break;
5375 }
5376
5377 return nullptr;
5378}
5379
5380// Canonicalize checking for a power-of-2-or-zero value:
5382 InstCombiner::BuilderTy &Builder) {
5383 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5384 const CmpInst::Predicate Pred = I.getPredicate();
5385 Value *A = nullptr;
5386 bool CheckIs;
5387 if (I.isEquality()) {
5388 // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants)
5389 // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants)
5390 if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()),
5391 m_Deferred(A)))) ||
5392 !match(Op1, m_ZeroInt()))
5393 A = nullptr;
5394
5395 // (A & -A) == A --> ctpop(A) < 2 (four commuted variants)
5396 // (-A & A) != A --> ctpop(A) > 1 (four commuted variants)
5397 if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1)))))
5398 A = Op1;
5399 else if (match(Op1,
5401 A = Op0;
5402
5403 CheckIs = Pred == ICmpInst::ICMP_EQ;
5404 } else if (ICmpInst::isUnsigned(Pred)) {
5405 // (A ^ (A-1)) u>= A --> ctpop(A) < 2 (two commuted variants)
5406 // ((A-1) ^ A) u< A --> ctpop(A) > 1 (two commuted variants)
5407
5408 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_ULT) &&
5410 m_Specific(Op1))))) {
5411 A = Op1;
5412 CheckIs = Pred == ICmpInst::ICMP_UGE;
5413 } else if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE) &&
5415 m_Specific(Op0))))) {
5416 A = Op0;
5417 CheckIs = Pred == ICmpInst::ICMP_ULE;
5418 }
5419 }
5420
5421 if (A) {
5422 Type *Ty = A->getType();
5423 CallInst *CtPop = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A);
5424 return CheckIs ? new ICmpInst(ICmpInst::ICMP_ULT, CtPop,
5425 ConstantInt::get(Ty, 2))
5426 : new ICmpInst(ICmpInst::ICMP_UGT, CtPop,
5427 ConstantInt::get(Ty, 1));
5428 }
5429
5430 return nullptr;
5431}
5432
5434 if (!I.isEquality())
5435 return nullptr;
5436
5437 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
5438 const CmpInst::Predicate Pred = I.getPredicate();
5439 Value *A, *B, *C, *D;
5440 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
5441 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
5442 Value *OtherVal = A == Op1 ? B : A;
5443 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5444 }
5445
5446 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
5447 // A^c1 == C^c2 --> A == C^(c1^c2)
5448 ConstantInt *C1, *C2;
5449 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
5450 Op1->hasOneUse()) {
5451 Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue());
5453 return new ICmpInst(Pred, A, Xor);
5454 }
5455
5456 // A^B == A^D -> B == D
5457 if (A == C)
5458 return new ICmpInst(Pred, B, D);
5459 if (A == D)
5460 return new ICmpInst(Pred, B, C);
5461 if (B == C)
5462 return new ICmpInst(Pred, A, D);
5463 if (B == D)
5464 return new ICmpInst(Pred, A, C);
5465 }
5466 }
5467
5468 // canoncalize:
5469 // (icmp eq/ne (and X, C), X)
5470 // -> (icmp eq/ne (and X, ~C), 0)
5471 {
5472 Constant *CMask;
5473 A = nullptr;
5474 if (match(Op0, m_OneUse(m_And(m_Specific(Op1), m_ImmConstant(CMask)))))
5475 A = Op1;
5476 else if (match(Op1, m_OneUse(m_And(m_Specific(Op0), m_ImmConstant(CMask)))))
5477 A = Op0;
5478 if (A)
5479 return new ICmpInst(Pred, Builder.CreateAnd(A, Builder.CreateNot(CMask)),
5480 Constant::getNullValue(A->getType()));
5481 }
5482
5483 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
5484 // A == (A^B) -> B == 0
5485 Value *OtherVal = A == Op0 ? B : A;
5486 return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
5487 }
5488
5489 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
5490 if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
5491 match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
5492 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
5493
5494 if (A == C) {
5495 X = B;
5496 Y = D;
5497 Z = A;
5498 } else if (A == D) {
5499 X = B;
5500 Y = C;
5501 Z = A;
5502 } else if (B == C) {
5503 X = A;
5504 Y = D;
5505 Z = B;
5506 } else if (B == D) {
5507 X = A;
5508 Y = C;
5509 Z = B;
5510 }
5511
5512 if (X) { // Build (X^Y) & Z
5513 Op1 = Builder.CreateXor(X, Y);
5514 Op1 = Builder.CreateAnd(Op1, Z);
5515 return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
5516 }
5517 }
5518
5519 {
5520 // Similar to above, but specialized for constant because invert is needed:
5521 // (X | C) == (Y | C) --> (X ^ Y) & ~C == 0
5522 Value *X, *Y;
5523 Constant *C;
5524 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_Constant(C)))) &&
5525 match(Op1, m_OneUse(m_Or(m_Value(Y), m_Specific(C))))) {
5528 return new ICmpInst(Pred, And, Constant::getNullValue(And->getType()));
5529 }
5530 }
5531
5532 if (match(Op1, m_ZExt(m_Value(A))) &&
5533 (Op0->hasOneUse() || Op1->hasOneUse())) {
5534 // (B & (Pow2C-1)) == zext A --> A == trunc B
5535 // (B & (Pow2C-1)) != zext A --> A != trunc B
5536 const APInt *MaskC;
5537 if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
5538 MaskC->countr_one() == A->getType()->getScalarSizeInBits())
5539 return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
5540 }
5541
5542 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
5543 // For lshr and ashr pairs.
5544 const APInt *AP1, *AP2;
5545 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5546 match(Op1, m_OneUse(m_LShr(m_Value(B), m_APIntAllowPoison(AP2))))) ||
5547 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_APIntAllowPoison(AP1)))) &&
5548 match(Op1, m_OneUse(m_AShr(m_Value(B), m_APIntAllowPoison(AP2)))))) {
5549 if (AP1 != AP2)
5550 return nullptr;
5551 unsigned TypeBits = AP1->getBitWidth();
5552 unsigned ShAmt = AP1->getLimitedValue(TypeBits);
5553 if (ShAmt < TypeBits && ShAmt != 0) {
5554 ICmpInst::Predicate NewPred =
5556 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5557 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
5558 return new ICmpInst(NewPred, Xor, ConstantInt::get(A->getType(), CmpVal));
5559 }
5560 }
5561
5562 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
5563 ConstantInt *Cst1;
5564 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
5565 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
5566 unsigned TypeBits = Cst1->getBitWidth();
5567 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
5568 if (ShAmt < TypeBits && ShAmt != 0) {
5569 Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted");
5570 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
5572 I.getName() + ".mask");
5573 return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType()));
5574 }
5575 }
5576
5577 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
5578 // "icmp (and X, mask), cst"
5579 uint64_t ShAmt = 0;
5580 if (Op0->hasOneUse() &&
5581 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
5582 match(Op1, m_ConstantInt(Cst1)) &&
5583 // Only do this when A has multiple uses. This is most important to do
5584 // when it exposes other optimizations.
5585 !A->hasOneUse()) {
5586 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
5587
5588 if (ShAmt < ASize) {
5589 APInt MaskV =
5591 MaskV <<= ShAmt;
5592
5593 APInt CmpV = Cst1->getValue().zext(ASize);
5594 CmpV <<= ShAmt;
5595
5596 Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV));
5597 return new ICmpInst(Pred, Mask, Builder.getInt(CmpV));
5598 }
5599 }
5600
5602 return ICmp;
5603
5604 // Match icmp eq (trunc (lshr A, BW), (ashr (trunc A), BW-1)), which checks the
5605 // top BW/2 + 1 bits are all the same. Create "A >=s INT_MIN && A <=s INT_MAX",
5606 // which we generate as "icmp ult (add A, 2^(BW-1)), 2^BW" to skip a few steps
5607 // of instcombine.
5608 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
5609 if (match(Op0, m_AShr(m_Trunc(m_Value(A)), m_SpecificInt(BitWidth - 1))) &&
5611 A->getType()->getScalarSizeInBits() == BitWidth * 2 &&
5612 (I.getOperand(0)->hasOneUse() || I.getOperand(1)->hasOneUse())) {
5614 Value *Add = Builder.CreateAdd(A, ConstantInt::get(A->getType(), C));
5615 return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT
5617 Add, ConstantInt::get(A->getType(), C.shl(1)));
5618 }
5619
5620 // Canonicalize:
5621 // Assume B_Pow2 != 0
5622 // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
5623 // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
5624 if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
5625 isKnownToBeAPowerOfTwo(Op1, /* OrZero */ false, 0, &I))
5626 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
5628
5629 if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
5630 isKnownToBeAPowerOfTwo(Op0, /* OrZero */ false, 0, &I))
5631 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
5632 ConstantInt::getNullValue(Op1->getType()));
5633
5634 // Canonicalize:
5635 // icmp eq/ne X, OneUse(rotate-right(X))
5636 // -> icmp eq/ne X, rotate-left(X)
5637 // We generally try to convert rotate-right -> rotate-left, this just
5638 // canonicalizes another case.
5639 CmpInst::Predicate PredUnused = Pred;
5640 if (match(&I, m_c_ICmp(PredUnused, m_Value(A),
5641 m_OneUse(m_Intrinsic<Intrinsic::fshr>(
5642 m_Deferred(A), m_Deferred(A), m_Value(B))))))
5643 return new ICmpInst(
5644 Pred, A,
5645 Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
5646
5647 // Canonicalize:
5648 // icmp eq/ne OneUse(A ^ Cst), B --> icmp eq/ne (A ^ B), Cst
5649 Constant *Cst;
5650 if (match(&I, m_c_ICmp(PredUnused,
5653 return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
5654
5655 {
5656 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5657 auto m_Matcher =
5660 m_Sub(m_Value(B), m_Deferred(A)));
5661 std::optional<bool> IsZero = std::nullopt;
5662 if (match(&I, m_c_ICmp(PredUnused, m_OneUse(m_c_And(m_Value(A), m_Matcher)),
5663 m_Deferred(A))))
5664 IsZero = false;
5665 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5666 else if (match(&I,
5667 m_ICmp(PredUnused, m_OneUse(m_c_And(m_Value(A), m_Matcher)),
5668 m_Zero())))
5669 IsZero = true;
5670
5671 if (IsZero && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, /*Depth*/ 0, &I))
5672 // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
5673 // -> (icmp eq/ne (and X, P2), 0)
5674 // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
5675 // -> (icmp eq/ne (and X, P2), P2)
5676 return new ICmpInst(Pred, Builder.CreateAnd(B, A),
5677 *IsZero ? A
5678 : ConstantInt::getNullValue(A->getType()));
5679 }
5680
5681 return nullptr;
5682}
5683
5685 ICmpInst::Predicate Pred = ICmp.getPredicate();
5686 Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
5687
5688 // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
5689 // The trunc masks high bits while the compare may effectively mask low bits.
5690 Value *X;
5691 const APInt *C;
5692 if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
5693 return nullptr;
5694
5695 // This matches patterns corresponding to tests of the signbit as well as:
5696 // (trunc X) u< C --> (X & -C) == 0 (are all masked-high-bits clear?)
5697 // (trunc X) u> C --> (X & ~C) != 0 (are any masked-high-bits set?)
5698 APInt Mask;
5699 if (decomposeBitTestICmp(Op0, Op1, Pred, X, Mask, true /* WithTrunc */)) {
5700 Value *And = Builder.CreateAnd(X, Mask);
5701 Constant *Zero = ConstantInt::getNullValue(X->getType());
5702 return new ICmpInst(Pred, And, Zero);
5703 }
5704
5705 unsigned SrcBits = X->getType()->getScalarSizeInBits();
5706 if (Pred == ICmpInst::ICMP_ULT && C->isNegatedPowerOf2()) {
5707 // If C is a negative power-of-2 (high-bit mask):
5708 // (trunc X) u< C --> (X & C) != C (are any masked-high-bits clear?)
5709 Constant *MaskC = ConstantInt::get(X->getType(), C->zext(SrcBits));
5710 Value *And = Builder.CreateAnd(X, MaskC);
5711 return new ICmpInst(ICmpInst::ICMP_NE, And, MaskC);
5712 }
5713
5714 if (Pred == ICmpInst::ICMP_UGT && (~*C).isPowerOf2()) {
5715 // If C is not-of-power-of-2 (one clear bit):
5716 // (trunc X) u> C --> (X & (C+1)) == C+1 (are all masked-high-bits set?)
5717 Constant *MaskC = ConstantInt::get(X->getType(), (*C + 1).zext(SrcBits));
5718 Value *And = Builder.CreateAnd(X, MaskC);
5719 return new ICmpInst(ICmpInst::ICMP_EQ, And, MaskC);
5720 }
5721
5722 if (auto *II = dyn_cast<IntrinsicInst>(X)) {
5723 if (II->getIntrinsicID() == Intrinsic::cttz ||
5724 II->getIntrinsicID() == Intrinsic::ctlz) {
5725 unsigned MaxRet = SrcBits;
5726 // If the "is_zero_poison" argument is set, then we know at least
5727 // one bit is set in the input, so the result is always at least one
5728 // less than the full bitwidth of that input.
5729 if (match(II->getArgOperand(1), m_One()))
5730 MaxRet--;
5731
5732 // Make sure the destination is wide enough to hold the largest output of
5733 // the intrinsic.
5734 if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
5735 if (Instruction *I =
5736 foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
5737 return I;
5738 }
5739 }
5740
5741 return nullptr;
5742}
5743
5745 assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
5746 auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
5747 Value *X;
5748 if (!match(CastOp0, m_ZExtOrSExt(m_Value(X))))
5749 return nullptr;
5750
5751 bool IsSignedExt = CastOp0->getOpcode() == Instruction::SExt;
5752 bool IsSignedCmp = ICmp.isSigned();
5753
5754 // icmp Pred (ext X), (ext Y)
5755 Value *Y;
5756 if (match(ICmp.getOperand(1), m_ZExtOrSExt(m_Value(Y)))) {
5757 bool IsZext0 = isa<ZExtInst>(ICmp.getOperand(0));
5758 bool IsZext1 = isa<ZExtInst>(ICmp.getOperand(1));
5759
5760 if (IsZext0 != IsZext1) {
5761 // If X and Y and both i1
5762 // (icmp eq/ne (zext X) (sext Y))
5763 // eq -> (icmp eq (or X, Y), 0)
5764 // ne -> (icmp ne (or X, Y), 0)
5765 if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
5766 Y->getType()->isIntOrIntVectorTy(1))
5767 return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
5768 Constant::getNullValue(X->getType()));
5769
5770 // If we have mismatched casts and zext has the nneg flag, we can
5771 // treat the "zext nneg" as "sext". Otherwise, we cannot fold and quit.
5772
5773 auto *NonNegInst0 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(0));
5774 auto *NonNegInst1 = dyn_cast<PossiblyNonNegInst>(ICmp.getOperand(1));
5775
5776 bool IsNonNeg0 = NonNegInst0 && NonNegInst0->hasNonNeg();
5777 bool IsNonNeg1 = NonNegInst1 && NonNegInst1->hasNonNeg();
5778
5779 if ((IsZext0 && IsNonNeg0) || (IsZext1 && IsNonNeg1))
5780 IsSignedExt = true;
5781 else
5782 return nullptr;
5783 }
5784
5785 // Not an extension from the same type?
5786 Type *XTy = X->getType(), *YTy = Y->getType();
5787 if (XTy != YTy) {
5788 // One of the casts must have one use because we are creating a new cast.
5789 if (!ICmp.getOperand(0)->hasOneUse() && !ICmp.getOperand(1)->hasOneUse())
5790 return nullptr;
5791 // Extend the narrower operand to the type of the wider operand.
5792 CastInst::CastOps CastOpcode =
5793 IsSignedExt ? Instruction::SExt : Instruction::ZExt;
5794 if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
5795 X = Builder.CreateCast(CastOpcode, X, YTy);
5796 else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
5797 Y = Builder.CreateCast(CastOpcode, Y, XTy);
5798 else
5799 return nullptr;
5800 }
5801
5802 // (zext X) == (zext Y) --> X == Y
5803 // (sext X) == (sext Y) --> X == Y
5804 if (ICmp.isEquality())
5805 return new ICmpInst(ICmp.getPredicate(), X, Y);
5806
5807 // A signed comparison of sign extended values simplifies into a
5808 // signed comparison.
5809 if (IsSignedCmp && IsSignedExt)
5810 return new ICmpInst(ICmp.getPredicate(), X, Y);
5811
5812 // The other three cases all fold into an unsigned comparison.
5813 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Y);
5814 }
5815
5816 // Below here, we are only folding a compare with constant.
5817 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
5818 if (!C)
5819 return nullptr;
5820
5821 // If a lossless truncate is possible...
5822 Type *SrcTy = CastOp0->getSrcTy();
5823 Constant *Res = getLosslessTrunc(C, SrcTy, CastOp0->getOpcode());
5824 if (Res) {
5825 if (ICmp.isEquality())
5826 return new ICmpInst(ICmp.getPredicate(), X, Res);
5827
5828 // A signed comparison of sign extended values simplifies into a
5829 // signed comparison.
5830 if (IsSignedExt && IsSignedCmp)
5831 return new ICmpInst(ICmp.getPredicate(), X, Res);
5832
5833 // The other three cases all fold into an unsigned comparison.
5834 return new ICmpInst(ICmp.getUnsignedPredicate(), X, Res);
5835 }
5836
5837 // The re-extended constant changed, partly changed (in the case of a vector),
5838 // or could not be determined to be equal (in the case of a constant
5839 // expression), so the constant cannot be represented in the shorter type.
5840 // All the cases that fold to true or false will have already been handled
5841 // by simplifyICmpInst, so only deal with the tricky case.
5842 if (IsSignedCmp || !IsSignedExt || !isa<ConstantInt>(C))
5843 return nullptr;
5844
5845 // Is source op positive?
5846 // icmp ult (sext X), C --> icmp sgt X, -1
5847 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
5849
5850 // Is source op negative?
5851 // icmp ugt (sext X), C --> icmp slt X, 0
5852 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
5854}
5855
5856/// Handle icmp (cast x), (cast or constant).
5858 // If any operand of ICmp is a inttoptr roundtrip cast then remove it as
5859 // icmp compares only pointer's value.
5860 // icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
5861 Value *SimplifiedOp0 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(0));
5862 Value *SimplifiedOp1 = simplifyIntToPtrRoundTripCast(ICmp.getOperand(1));
5863 if (SimplifiedOp0 || SimplifiedOp1)
5864 return new ICmpInst(ICmp.getPredicate(),
5865 SimplifiedOp0 ? SimplifiedOp0 : ICmp.getOperand(0),
5866 SimplifiedOp1 ? SimplifiedOp1 : ICmp.getOperand(1));
5867
5868 auto *CastOp0 = dyn_cast<CastInst>(ICmp.getOperand(0));
5869 if (!CastOp0)
5870 return nullptr;
5871 if (!isa<Constant>(ICmp.getOperand(1)) && !isa<CastInst>(ICmp.getOperand(1)))
5872 return nullptr;
5873
5874 Value *Op0Src = CastOp0->getOperand(0);
5875 Type *SrcTy = CastOp0->getSrcTy();
5876 Type *DestTy = CastOp0->getDestTy();
5877
5878 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
5879 // integer type is the same size as the pointer type.
5880 auto CompatibleSizes = [&](Type *SrcTy, Type *DestTy) {
5881 if (isa<VectorType>(SrcTy)) {
5882 SrcTy = cast<VectorType>(SrcTy)->getElementType();
5883 DestTy = cast<VectorType>(DestTy)->getElementType();
5884 }
5885 return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
5886 };
5887 if (CastOp0->getOpcode() == Instruction::PtrToInt &&
5888 CompatibleSizes(SrcTy, DestTy)) {
5889 Value *NewOp1 = nullptr;
5890 if (auto *PtrToIntOp1 = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
5891 Value *PtrSrc = PtrToIntOp1->getOperand(0);
5892 if (PtrSrc->getType() == Op0Src->getType())
5893 NewOp1 = PtrToIntOp1->getOperand(0);
5894 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
5895 NewOp1 = ConstantExpr::getIntToPtr(RHSC, SrcTy);
5896 }
5897
5898 if (NewOp1)
5899 return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
5900 }
5901
5902 if (Instruction *R = foldICmpWithTrunc(ICmp))
5903 return R;
5904
5905 return foldICmpWithZextOrSext(ICmp);
5906}
5907
5908static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned) {
5909 switch (BinaryOp) {
5910 default:
5911 llvm_unreachable("Unsupported binary op");
5912 case Instruction::Add:
5913 case Instruction::Sub:
5914 return match(RHS, m_Zero());
5915 case Instruction::Mul:
5916 return !(RHS->getType()->isIntOrIntVectorTy(1) && IsSigned) &&
5917 match(RHS, m_One());
5918 }
5919}
5920
5923 bool IsSigned, Value *LHS, Value *RHS,
5924 Instruction *CxtI) const {
5925 switch (BinaryOp) {
5926 default:
5927 llvm_unreachable("Unsupported binary op");
5928 case Instruction::Add:
5929 if (IsSigned)
5930 return computeOverflowForSignedAdd(LHS, RHS, CxtI);
5931 else
5932 return computeOverflowForUnsignedAdd(LHS, RHS, CxtI);
5933 case Instruction::Sub:
5934 if (IsSigned)
5935 return computeOverflowForSignedSub(LHS, RHS, CxtI);
5936 else
5937 return computeOverflowForUnsignedSub(LHS, RHS, CxtI);
5938 case Instruction::Mul:
5939 if (IsSigned)
5940 return computeOverflowForSignedMul(LHS, RHS, CxtI);
5941 else
5942 return computeOverflowForUnsignedMul(LHS, RHS, CxtI);
5943 }
5944}
5945
5946bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
5947 bool IsSigned, Value *LHS,
5948 Value *RHS, Instruction &OrigI,
5949 Value *&Result,
5950 Constant *&Overflow) {
5951 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
5952 std::swap(LHS, RHS);
5953
5954 // If the overflow check was an add followed by a compare, the insertion point
5955 // may be pointing to the compare. We want to insert the new instructions
5956 // before the add in case there are uses of the add between the add and the
5957 // compare.
5958 Builder.SetInsertPoint(&OrigI);
5959
5960 Type *OverflowTy = Type::getInt1Ty(LHS->getContext());
5961 if (auto *LHSTy = dyn_cast<VectorType>(LHS->getType()))
5962 OverflowTy = VectorType::get(OverflowTy, LHSTy->getElementCount());
5963
5964 if (isNeutralValue(BinaryOp, RHS, IsSigned)) {
5965 Result = LHS;
5966 Overflow = ConstantInt::getFalse(OverflowTy);
5967 return true;
5968 }
5969
5970 switch (computeOverflow(BinaryOp, IsSigned, LHS, RHS, &OrigI)) {
5972 return false;
5975 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
5976 Result->takeName(&OrigI);
5977 Overflow = ConstantInt::getTrue(OverflowTy);
5978 return true;
5980 Result = Builder.CreateBinOp(BinaryOp, LHS, RHS);
5981 Result->takeName(&OrigI);
5982 Overflow = ConstantInt::getFalse(OverflowTy);
5983 if (auto *Inst = dyn_cast<Instruction>(Result)) {
5984 if (IsSigned)
5985 Inst->setHasNoSignedWrap();
5986 else
5987 Inst->setHasNoUnsignedWrap();
5988 }
5989 return true;
5990 }
5991
5992 llvm_unreachable("Unexpected overflow result");
5993}
5994
5995/// Recognize and process idiom involving test for multiplication
5996/// overflow.
5997///
5998/// The caller has matched a pattern of the form:
5999/// I = cmp u (mul(zext A, zext B), V
6000/// The function checks if this is a test for overflow and if so replaces
6001/// multiplication with call to 'mul.with.overflow' intrinsic.
6002///
6003/// \param I Compare instruction.
6004/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
6005/// the compare instruction. Must be of integer type.
6006/// \param OtherVal The other argument of compare instruction.
6007/// \returns Instruction which must replace the compare instruction, NULL if no
6008/// replacement required.
6010 const APInt *OtherVal,
6011 InstCombinerImpl &IC) {
6012 // Don't bother doing this transformation for pointers, don't do it for
6013 // vectors.
6014 if (!isa<IntegerType>(MulVal->getType()))
6015 return nullptr;
6016
6017 auto *MulInstr = dyn_cast<Instruction>(MulVal);
6018 if (!MulInstr)
6019 return nullptr;
6020 assert(MulInstr->getOpcode() == Instruction::Mul);
6021
6022 auto *LHS = cast<ZExtInst>(MulInstr->getOperand(0)),
6023 *RHS = cast<ZExtInst>(MulInstr->getOperand(1));
6024 assert(LHS->getOpcode() == Instruction::ZExt);
6025 assert(RHS->getOpcode() == Instruction::ZExt);
6026 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
6027
6028 // Calculate type and width of the result produced by mul.with.overflow.
6029 Type *TyA = A->getType(), *TyB = B->getType();
6030 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
6031 WidthB = TyB->getPrimitiveSizeInBits();
6032 unsigned MulWidth;
6033 Type *MulType;
6034 if (WidthB > WidthA) {
6035 MulWidth = WidthB;
6036 MulType = TyB;
6037 } else {
6038 MulWidth = WidthA;
6039 MulType = TyA;
6040 }
6041
6042 // In order to replace the original mul with a narrower mul.with.overflow,
6043 // all uses must ignore upper bits of the product. The number of used low
6044 // bits must be not greater than the width of mul.with.overflow.
6045 if (MulVal->hasNUsesOrMore(2))
6046 for (User *U : MulVal->users()) {
6047 if (U == &I)
6048 continue;
6049 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6050 // Check if truncation ignores bits above MulWidth.
6051 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
6052 if (TruncWidth > MulWidth)
6053 return nullptr;
6054 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6055 // Check if AND ignores bits above MulWidth.
6056 if (BO->getOpcode() != Instruction::And)
6057 return nullptr;
6058 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
6059 const APInt &CVal = CI->getValue();
6060 if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
6061 return nullptr;
6062 } else {
6063 // In this case we could have the operand of the binary operation
6064 // being defined in another block, and performing the replacement
6065 // could break the dominance relation.
6066 return nullptr;
6067 }
6068 } else {
6069 // Other uses prohibit this transformation.
6070 return nullptr;
6071 }
6072 }
6073
6074 // Recognize patterns
6075 switch (I.getPredicate()) {
6076 case ICmpInst::ICMP_UGT: {
6077 // Recognize pattern:
6078 // mulval = mul(zext A, zext B)
6079 // cmp ugt mulval, max
6080 APInt MaxVal = APInt::getMaxValue(MulWidth);
6081 MaxVal = MaxVal.zext(OtherVal->getBitWidth());
6082 if (MaxVal.eq(*OtherVal))
6083 break; // Recognized
6084 return nullptr;
6085 }
6086
6087 case ICmpInst::ICMP_ULT: {
6088 // Recognize pattern:
6089 // mulval = mul(zext A, zext B)
6090 // cmp ule mulval, max + 1
6091 APInt MaxVal = APInt::getOneBitSet(OtherVal->getBitWidth(), MulWidth);
6092 if (MaxVal.eq(*OtherVal))
6093 break; // Recognized
6094 return nullptr;
6095 }
6096
6097 default:
6098 return nullptr;
6099 }
6100
6101 InstCombiner::BuilderTy &Builder = IC.Builder;
6102 Builder.SetInsertPoint(MulInstr);
6103
6104 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
6105 Value *MulA = A, *MulB = B;
6106 if (WidthA < MulWidth)
6107 MulA = Builder.CreateZExt(A, MulType);
6108 if (WidthB < MulWidth)
6109 MulB = Builder.CreateZExt(B, MulType);
6111 I.getModule(), Intrinsic::umul_with_overflow, MulType);
6112 CallInst *Call = Builder.CreateCall(F, {MulA, MulB}, "umul");
6113 IC.addToWorklist(MulInstr);
6114
6115 // If there are uses of mul result other than the comparison, we know that
6116 // they are truncation or binary AND. Change them to use result of
6117 // mul.with.overflow and adjust properly mask/size.
6118 if (MulVal->hasNUsesOrMore(2)) {
6119 Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value");
6120 for (User *U : make_early_inc_range(MulVal->users())) {
6121 if (U == &I)
6122 continue;
6123 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
6124 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
6125 IC.replaceInstUsesWith(*TI, Mul);
6126 else
6127 TI->setOperand(0, Mul);
6128 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
6129 assert(BO->getOpcode() == Instruction::And);
6130 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
6131 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
6132 APInt ShortMask = CI->getValue().trunc(MulWidth);
6133 Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask);
6134 Value *Zext = Builder.CreateZExt(ShortAnd, BO->getType());
6135 IC.replaceInstUsesWith(*BO, Zext);
6136 } else {
6137 llvm_unreachable("Unexpected Binary operation");
6138 }
6139 IC.addToWorklist(cast<Instruction>(U));
6140 }
6141 }
6142
6143 // The original icmp gets replaced with the overflow value, maybe inverted
6144 // depending on predicate.
6145 if (I.getPredicate() == ICmpInst::ICMP_ULT) {
6146 Value *Res = Builder.CreateExtractValue(Call, 1);
6147 return BinaryOperator::CreateNot(Res);
6148 }
6149
6150 return ExtractValueInst::Create(Call, 1);
6151}
6152
6153/// When performing a comparison against a constant, it is possible that not all
6154/// the bits in the LHS are demanded. This helper method computes the mask that
6155/// IS demanded.
6157 const APInt *RHS;
6158 if (!match(I.getOperand(1), m_APInt(RHS)))
6160
6161 // If this is a normal comparison, it demands all bits. If it is a sign bit
6162 // comparison, it only demands the sign bit.
6163 bool UnusedBit;
6164 if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6166
6167 switch (I.getPredicate()) {
6168 // For a UGT comparison, we don't care about any bits that
6169 // correspond to the trailing ones of the comparand. The value of these
6170 // bits doesn't impact the outcome of the comparison, because any value
6171 // greater than the RHS must differ in a bit higher than these due to carry.
6172 case ICmpInst::ICMP_UGT:
6173 return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
6174
6175 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
6176 // Any value less than the RHS must differ in a higher bit because of carries.
6177 case ICmpInst::ICMP_ULT:
6178 return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
6179
6180 default:
6182 }
6183}
6184
6185/// Check that one use is in the same block as the definition and all
6186/// other uses are in blocks dominated by a given block.
6187///
6188/// \param DI Definition
6189/// \param UI Use
6190/// \param DB Block that must dominate all uses of \p DI outside
6191/// the parent block
6192/// \return true when \p UI is the only use of \p DI in the parent block
6193/// and all other uses of \p DI are in blocks dominated by \p DB.
6194///
6196 const Instruction *UI,
6197 const BasicBlock *DB) const {
6198 assert(DI && UI && "Instruction not defined\n");
6199 // Ignore incomplete definitions.
6200 if (!DI->getParent())
6201 return false;
6202 // DI and UI must be in the same block.
6203 if (DI->getParent() != UI->getParent())
6204 return false;
6205 // Protect from self-referencing blocks.
6206 if (DI->getParent() == DB)
6207 return false;
6208 for (const User *U : DI->users()) {
6209 auto *Usr = cast<Instruction>(U);
6210 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
6211 return false;
6212 }
6213 return true;
6214}
6215
6216/// Return true when the instruction sequence within a block is select-cmp-br.
6217static bool isChainSelectCmpBranch(const SelectInst *SI) {
6218 const BasicBlock *BB = SI->getParent();
6219 if (!BB)
6220 return false;
6221 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
6222 if (!BI || BI->getNumSuccessors() != 2)
6223 return false;
6224 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
6225 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
6226 return false;
6227 return true;
6228}
6229
6230/// True when a select result is replaced by one of its operands
6231/// in select-icmp sequence. This will eventually result in the elimination
6232/// of the select.
6233///
6234/// \param SI Select instruction
6235/// \param Icmp Compare instruction
6236/// \param SIOpd Operand that replaces the select
6237///
6238/// Notes:
6239/// - The replacement is global and requires dominator information
6240/// - The caller is responsible for the actual replacement
6241///
6242/// Example:
6243///
6244/// entry:
6245/// %4 = select i1 %3, %C* %0, %C* null
6246/// %5 = icmp eq %C* %4, null
6247/// br i1 %5, label %9, label %7
6248/// ...
6249/// ; <label>:7 ; preds = %entry
6250/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
6251/// ...
6252///
6253/// can be transformed to
6254///
6255/// %5 = icmp eq %C* %0, null
6256/// %6 = select i1 %3, i1 %5, i1 true
6257/// br i1 %6, label %9, label %7
6258/// ...
6259/// ; <label>:7 ; preds = %entry
6260/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
6261///
6262/// Similar when the first operand of the select is a constant or/and
6263/// the compare is for not equal rather than equal.
6264///
6265/// NOTE: The function is only called when the select and compare constants
6266/// are equal, the optimization can work only for EQ predicates. This is not a
6267/// major restriction since a NE compare should be 'normalized' to an equal
6268/// compare, which usually happens in the combiner and test case
6269/// select-cmp-br.ll checks for it.
6271 const ICmpInst *Icmp,
6272 const unsigned SIOpd) {
6273 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
6275 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
6276 // The check for the single predecessor is not the best that can be
6277 // done. But it protects efficiently against cases like when SI's
6278 // home block has two successors, Succ and Succ1, and Succ1 predecessor
6279 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
6280 // replaced can be reached on either path. So the uniqueness check
6281 // guarantees that the path all uses of SI (outside SI's parent) are on
6282 // is disjoint from all other paths out of SI. But that information
6283 // is more expensive to compute, and the trade-off here is in favor
6284 // of compile-time. It should also be noticed that we check for a single
6285 // predecessor and not only uniqueness. This to handle the situation when
6286 // Succ and Succ1 points to the same basic block.
6287 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
6288 NumSel++;
6289 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
6290 return true;
6291 }
6292 }
6293 return false;
6294}
6295
6296/// Try to fold the comparison based on range information we can get by checking
6297/// whether bits are known to be zero or one in the inputs.
6299 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6300 Type *Ty = Op0->getType();
6301 ICmpInst::Predicate Pred = I.getPredicate();
6302
6303 // Get scalar or pointer size.
6304 unsigned BitWidth = Ty->isIntOrIntVectorTy()
6305 ? Ty->getScalarSizeInBits()
6307
6308 if (!BitWidth)
6309 return nullptr;
6310
6311 KnownBits Op0Known(BitWidth);
6312 KnownBits Op1Known(BitWidth);
6313
6314 {
6315 // Don't use dominating conditions when folding icmp using known bits. This
6316 // may convert signed into unsigned predicates in ways that other passes
6317 // (especially IndVarSimplify) may not be able to reliably undo.
6318 SQ.DC = nullptr;
6319 auto _ = make_scope_exit([&]() { SQ.DC = &DC; });
6321 Op0Known, 0))
6322 return &I;
6323
6324 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnes(BitWidth), Op1Known, 0))
6325 return &I;
6326 }
6327
6328 // Given the known and unknown bits, compute a range that the LHS could be
6329 // in. Compute the Min, Max and RHS values based on the known bits. For the
6330 // EQ and NE we use unsigned values.
6331 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
6332 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
6333 if (I.isSigned()) {
6334 Op0Min = Op0Known.getSignedMinValue();
6335 Op0Max = Op0Known.getSignedMaxValue();
6336 Op1Min = Op1Known.getSignedMinValue();
6337 Op1Max = Op1Known.getSignedMaxValue();
6338 } else {
6339 Op0Min = Op0Known.getMinValue();
6340 Op0Max = Op0Known.getMaxValue();
6341 Op1Min = Op1Known.getMinValue();
6342 Op1Max = Op1Known.getMaxValue();
6343 }
6344
6345 // If Min and Max are known to be the same, then SimplifyDemandedBits figured
6346 // out that the LHS or RHS is a constant. Constant fold this now, so that
6347 // code below can assume that Min != Max.
6348 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
6349 return new ICmpInst(Pred, ConstantExpr::getIntegerValue(Ty, Op0Min), Op1);
6350 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
6351 return new ICmpInst(Pred, Op0, ConstantExpr::getIntegerValue(Ty, Op1Min));
6352
6353 // Don't break up a clamp pattern -- (min(max X, Y), Z) -- by replacing a
6354 // min/max canonical compare with some other compare. That could lead to
6355 // conflict with select canonicalization and infinite looping.
6356 // FIXME: This constraint may go away if min/max intrinsics are canonical.
6357 auto isMinMaxCmp = [&](Instruction &Cmp) {
6358 if (!Cmp.hasOneUse())
6359 return false;
6360 Value *A, *B;
6361 SelectPatternFlavor SPF = matchSelectPattern(Cmp.user_back(), A, B).Flavor;
6363 return false;
6364 return match(Op0, m_MaxOrMin(m_Value(), m_Value())) ||
6365 match(Op1, m_MaxOrMin(m_Value(), m_Value()));
6366 };
6367 if (!isMinMaxCmp(I)) {
6368 switch (Pred) {
6369 default:
6370 break;
6371 case ICmpInst::ICMP_ULT: {
6372 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
6373 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6374 const APInt *CmpC;
6375 if (match(Op1, m_APInt(CmpC))) {
6376 // A <u C -> A == C-1 if min(A)+1 == C
6377 if (*CmpC == Op0Min + 1)
6378 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6379 ConstantInt::get(Op1->getType(), *CmpC - 1));
6380 // X <u C --> X == 0, if the number of zero bits in the bottom of X
6381 // exceeds the log2 of C.
6382 if (Op0Known.countMinTrailingZeros() >= CmpC->ceilLogBase2())
6383 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6384 Constant::getNullValue(Op1->getType()));
6385 }
6386 break;
6387 }
6388 case ICmpInst::ICMP_UGT: {
6389 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
6390 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6391 const APInt *CmpC;
6392 if (match(Op1, m_APInt(CmpC))) {
6393 // A >u C -> A == C+1 if max(a)-1 == C
6394 if (*CmpC == Op0Max - 1)
6395 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6396 ConstantInt::get(Op1->getType(), *CmpC + 1));
6397 // X >u C --> X != 0, if the number of zero bits in the bottom of X
6398 // exceeds the log2 of C.
6399 if (Op0Known.countMinTrailingZeros() >= CmpC->getActiveBits())
6400 return new ICmpInst(ICmpInst::ICMP_NE, Op0,
6401 Constant::getNullValue(Op1->getType()));
6402 }
6403 break;
6404 }
6405 case ICmpInst::ICMP_SLT: {
6406 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
6407 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6408 const APInt *CmpC;
6409 if (match(Op1, m_APInt(CmpC))) {
6410 if (*CmpC == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
6411 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6412 ConstantInt::get(Op1->getType(), *CmpC - 1));
6413 }
6414 break;
6415 }
6416 case ICmpInst::ICMP_SGT: {
6417 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
6418 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
6419 const APInt *CmpC;
6420 if (match(Op1, m_APInt(CmpC))) {
6421 if (*CmpC == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
6422 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
6423 ConstantInt::get(Op1->getType(), *CmpC + 1));
6424 }
6425 break;
6426 }
6427 }
6428 }
6429
6430 // Based on the range information we know about the LHS, see if we can
6431 // simplify this comparison. For example, (x&4) < 8 is always true.
6432 switch (Pred) {
6433 default:
6434 llvm_unreachable("Unknown icmp opcode!");
6435 case ICmpInst::ICMP_EQ:
6436 case ICmpInst::ICMP_NE: {
6437 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
6438 return replaceInstUsesWith(
6439 I, ConstantInt::getBool(I.getType(), Pred == CmpInst::ICMP_NE));
6440
6441 // If all bits are known zero except for one, then we know at most one bit
6442 // is set. If the comparison is against zero, then this is a check to see if
6443 // *that* bit is set.
6444 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
6445 if (Op1Known.isZero()) {
6446 // If the LHS is an AND with the same constant, look through it.
6447 Value *LHS = nullptr;
6448 const APInt *LHSC;
6449 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
6450 *LHSC != Op0KnownZeroInverted)
6451 LHS = Op0;
6452
6453 Value *X;
6454 const APInt *C1;
6455 if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
6456 Type *XTy = X->getType();
6457 unsigned Log2C1 = C1->countr_zero();
6458 APInt C2 = Op0KnownZeroInverted;
6459 APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
6460 if (C2Pow2.isPowerOf2()) {
6461 // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
6462 // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
6463 // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
6464 unsigned Log2C2 = C2Pow2.countr_zero();
6465 auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
6466 auto NewPred =
6468 return new ICmpInst(NewPred, X, CmpC);
6469 }
6470 }
6471 }
6472
6473 // Op0 eq C_Pow2 -> Op0 ne 0 if Op0 is known to be C_Pow2 or zero.
6474 if (Op1Known.isConstant() && Op1Known.getConstant().isPowerOf2() &&
6475 (Op0Known & Op1Known) == Op0Known)
6476 return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
6477 ConstantInt::getNullValue(Op1->getType()));
6478 break;
6479 }
6480 case ICmpInst::ICMP_ULT: {
6481 if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
6482 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6483 if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
6484 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6485 break;
6486 }
6487 case ICmpInst::ICMP_UGT: {
6488 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
6489 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6490 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
6491 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6492 break;
6493 }
6494 case ICmpInst::ICMP_SLT: {
6495 if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
6496 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6497 if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
6498 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6499 break;
6500 }
6501 case ICmpInst::ICMP_SGT: {
6502 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
6503 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6504 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
6505 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6506 break;
6507 }
6508 case ICmpInst::ICMP_SGE:
6509 assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
6510 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
6511 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6512 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
6513 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6514 if (Op1Min == Op0Max) // A >=s B -> A == B if max(A) == min(B)
6515 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6516 break;
6517 case ICmpInst::ICMP_SLE:
6518 assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
6519 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
6520 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6521 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
6522 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6523 if (Op1Max == Op0Min) // A <=s B -> A == B if min(A) == max(B)
6524 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6525 break;
6526 case ICmpInst::ICMP_UGE:
6527 assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
6528 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
6529 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6530 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
6531 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6532 if (Op1Min == Op0Max) // A >=u B -> A == B if max(A) == min(B)
6533 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6534 break;
6535 case ICmpInst::ICMP_ULE:
6536 assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
6537 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
6538 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
6539 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
6540 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
6541 if (Op1Max == Op0Min) // A <=u B -> A == B if min(A) == max(B)
6542 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
6543 break;
6544 }
6545
6546 // Turn a signed comparison into an unsigned one if both operands are known to
6547 // have the same sign.
6548 if (I.isSigned() &&
6549 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
6550 (Op0Known.One.isNegative() && Op1Known.One.isNegative())))
6551 return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
6552
6553 return nullptr;
6554}
6555
6556/// If one operand of an icmp is effectively a bool (value range of {0,1}),
6557/// then try to reduce patterns based on that limit.
6559 Value *X, *Y;
6561
6562 // X must be 0 and bool must be true for "ULT":
6563 // X <u (zext i1 Y) --> (X == 0) & Y
6564 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
6565 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)
6566 return BinaryOperator::CreateAnd(Builder.CreateIsNull(X), Y);
6567
6568 // X must be 0 or bool must be true for "ULE":
6569 // X <=u (sext i1 Y) --> (X == 0) | Y
6570 if (match(&I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_SExt(m_Value(Y))))) &&
6571 Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
6572 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
6573
6574 // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
6575 ICmpInst::Predicate Pred1, Pred2;
6576 const APInt *C;
6577 Instruction *ExtI;
6578 if (match(&I, m_c_ICmp(Pred1, m_Value(X),
6581 m_APInt(C)))))) &&
6582 ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
6583 bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
6584 bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
6585 auto CreateRangeCheck = [&] {
6586 Value *CmpV1 =
6587 Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
6588 Value *CmpV2 = Builder.CreateICmp(
6589 Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
6591 Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
6592 CmpV1, CmpV2);
6593 };
6594 if (C->isZero()) {
6595 if (Pred2 == ICmpInst::ICMP_EQ) {
6596 // icmp eq X, (zext/sext (icmp eq X, 0)) --> false
6597 // icmp ne X, (zext/sext (icmp eq X, 0)) --> true
6598 return replaceInstUsesWith(
6599 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6600 } else if (!IsSExt || HasOneUse) {
6601 // icmp eq X, (zext (icmp ne X, 0)) --> X == 0 || X == 1
6602 // icmp ne X, (zext (icmp ne X, 0)) --> X != 0 && X != 1
6603 // icmp eq X, (sext (icmp ne X, 0)) --> X == 0 || X == -1
6604 // icmp ne X, (sext (icmp ne X, 0)) --> X != 0 && X == -1
6605 return CreateRangeCheck();
6606 }
6607 } else if (IsSExt ? C->isAllOnes() : C->isOne()) {
6608 if (Pred2 == ICmpInst::ICMP_NE) {
6609 // icmp eq X, (zext (icmp ne X, 1)) --> false
6610 // icmp ne X, (zext (icmp ne X, 1)) --> true
6611 // icmp eq X, (sext (icmp ne X, -1)) --> false
6612 // icmp ne X, (sext (icmp ne X, -1)) --> true
6613 return replaceInstUsesWith(
6614 I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
6615 } else if (!IsSExt || HasOneUse) {
6616 // icmp eq X, (zext (icmp eq X, 1)) --> X == 0 || X == 1
6617 // icmp ne X, (zext (icmp eq X, 1)) --> X != 0 && X != 1
6618 // icmp eq X, (sext (icmp eq X, -1)) --> X == 0 || X == -1
6619 // icmp ne X, (sext (icmp eq X, -1)) --> X != 0 && X == -1
6620 return CreateRangeCheck();
6621 }
6622 } else {
6623 // when C != 0 && C != 1:
6624 // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
6625 // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
6626 // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
6627 // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
6628 // when C != 0 && C != -1:
6629 // icmp eq X, (sext (icmp eq X, C)) --> icmp eq X, 0
6630 // icmp eq X, (sext (icmp ne X, C)) --> icmp eq X, -1
6631 // icmp ne X, (sext (icmp eq X, C)) --> icmp ne X, 0
6632 // icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
6633 return ICmpInst::Create(
6634 Instruction::ICmp, Pred1, X,
6635 ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
6636 ? (IsSExt ? -1 : 1)
6637 : 0));
6638 }
6639 }
6640
6641 return nullptr;
6642}
6643
6644std::optional<std::pair<CmpInst::Predicate, Constant *>>
6646 Constant *C) {
6648 "Only for relational integer predicates.");
6649
6650 Type *Type = C->getType();
6651 bool IsSigned = ICmpInst::isSigned(Pred);
6652
6654 bool WillIncrement =
6655 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
6656
6657 // Check if the constant operand can be safely incremented/decremented
6658 // without overflowing/underflowing.
6659 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
6660 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
6661 };
6662
6663 Constant *SafeReplacementConstant = nullptr;
6664 if (auto *CI = dyn_cast<ConstantInt>(C)) {
6665 // Bail out if the constant can't be safely incremented/decremented.
6666 if (!ConstantIsOk(CI))
6667 return std::nullopt;
6668 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
6669 unsigned NumElts = FVTy->getNumElements();
6670 for (unsigned i = 0; i != NumElts; ++i) {
6671 Constant *Elt = C->getAggregateElement(i);
6672 if (!Elt)
6673 return std::nullopt;
6674
6675 if (isa<UndefValue>(Elt))
6676 continue;
6677
6678 // Bail out if we can't determine if this constant is min/max or if we
6679 // know that this constant is min/max.
6680 auto *CI = dyn_cast<ConstantInt>(Elt);
6681 if (!CI || !ConstantIsOk(CI))
6682 return std::nullopt;
6683
6684 if (!SafeReplacementConstant)
6685 SafeReplacementConstant = CI;
6686 }
6687 } else if (isa<VectorType>(C->getType())) {
6688 // Handle scalable splat
6689 Value *SplatC = C->getSplatValue();
6690 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
6691 // Bail out if the constant can't be safely incremented/decremented.
6692 if (!CI || !ConstantIsOk(CI))
6693 return std::nullopt;
6694 } else {
6695 // ConstantExpr?
6696 return std::nullopt;
6697 }
6698
6699 // It may not be safe to change a compare predicate in the presence of
6700 // undefined elements, so replace those elements with the first safe constant
6701 // that we found.
6702 // TODO: in case of poison, it is safe; let's replace undefs only.
6703 if (C->containsUndefOrPoisonElement()) {
6704 assert(SafeReplacementConstant && "Replacement constant not set");
6705 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
6706 }
6707
6709
6710 // Increment or decrement the constant.
6711 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
6712 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
6713
6714 return std::make_pair(NewPred, NewC);
6715}
6716
6717/// If we have an icmp le or icmp ge instruction with a constant operand, turn
6718/// it into the appropriate icmp lt or icmp gt instruction. This transform
6719/// allows them to be folded in visitICmpInst.
6721 ICmpInst::Predicate Pred = I.getPredicate();
6722 if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
6724 return nullptr;
6725
6726 Value *Op0 = I.getOperand(0);
6727 Value *Op1 = I.getOperand(1);
6728 auto *Op1C = dyn_cast<Constant>(Op1);
6729 if (!Op1C)
6730 return nullptr;
6731
6732 auto FlippedStrictness =
6734 if (!FlippedStrictness)
6735 return nullptr;
6736
6737 return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
6738}
6739
6740/// If we have a comparison with a non-canonical predicate, if we can update
6741/// all the users, invert the predicate and adjust all the users.
6743 // Is the predicate already canonical?
6744 CmpInst::Predicate Pred = I.getPredicate();
6746 return nullptr;
6747
6748 // Can all users be adjusted to predicate inversion?
6749 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
6750 return nullptr;
6751
6752 // Ok, we can canonicalize comparison!
6753 // Let's first invert the comparison's predicate.
6754 I.setPredicate(CmpInst::getInversePredicate(Pred));
6755 I.setName(I.getName() + ".not");
6756
6757 // And, adapt users.
6759
6760 return &I;
6761}
6762
6763/// Integer compare with boolean values can always be turned into bitwise ops.
6765 InstCombiner::BuilderTy &Builder) {
6766 Value *A = I.getOperand(0), *B = I.getOperand(1);
6767 assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only");
6768
6769 // A boolean compared to true/false can be simplified to Op0/true/false in
6770 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
6771 // Cases not handled by InstSimplify are always 'not' of Op0.
6772 if (match(B, m_Zero())) {
6773 switch (I.getPredicate()) {
6774 case CmpInst::ICMP_EQ: // A == 0 -> !A
6775 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
6776 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
6778 default:
6779 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6780 }
6781 } else if (match(B, m_One())) {
6782 switch (I.getPredicate()) {
6783 case CmpInst::ICMP_NE: // A != 1 -> !A
6784 case CmpInst::ICMP_ULT: // A <u 1 -> !A
6785 case CmpInst::ICMP_SGT: // A >s -1 -> !A
6787 default:
6788 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
6789 }
6790 }
6791
6792 switch (I.getPredicate()) {
6793 default:
6794 llvm_unreachable("Invalid icmp instruction!");
6795 case ICmpInst::ICMP_EQ:
6796 // icmp eq i1 A, B -> ~(A ^ B)
6797 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
6798
6799 case ICmpInst::ICMP_NE:
6800 // icmp ne i1 A, B -> A ^ B
6801 return BinaryOperator::CreateXor(A, B);
6802
6803 case ICmpInst::ICMP_UGT:
6804 // icmp ugt -> icmp ult
6805 std::swap(A, B);
6806 [[fallthrough]];
6807 case ICmpInst::ICMP_ULT:
6808 // icmp ult i1 A, B -> ~A & B
6809 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
6810
6811 case ICmpInst::ICMP_SGT:
6812 // icmp sgt -> icmp slt
6813 std::swap(A, B);
6814 [[fallthrough]];
6815 case ICmpInst::ICMP_SLT:
6816 // icmp slt i1 A, B -> A & ~B
6817 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
6818
6819 case ICmpInst::ICMP_UGE:
6820 // icmp uge -> icmp ule
6821 std::swap(A, B);
6822 [[fallthrough]];
6823 case ICmpInst::ICMP_ULE:
6824 // icmp ule i1 A, B -> ~A | B
6825 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
6826
6827 case ICmpInst::ICMP_SGE:
6828 // icmp sge -> icmp sle
6829 std::swap(A, B);
6830 [[fallthrough]];
6831 case ICmpInst::ICMP_SLE:
6832 // icmp sle i1 A, B -> A | ~B
6833 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
6834 }
6835}
6836
6837// Transform pattern like:
6838// (1 << Y) u<= X or ~(-1 << Y) u< X or ((1 << Y)+(-1)) u< X
6839// (1 << Y) u> X or ~(-1 << Y) u>= X or ((1 << Y)+(-1)) u>= X
6840// Into:
6841// (X l>> Y) != 0
6842// (X l>> Y) == 0
6844 InstCombiner::BuilderTy &Builder) {
6845 ICmpInst::Predicate Pred, NewPred;
6846 Value *X, *Y;
6847 if (match(&Cmp,
6848 m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) {
6849 switch (Pred) {
6850 case ICmpInst::ICMP_ULE:
6851 NewPred = ICmpInst::ICMP_NE;
6852 break;
6853 case ICmpInst::ICMP_UGT:
6854 NewPred = ICmpInst::ICMP_EQ;
6855 break;
6856 default:
6857 return nullptr;
6858 }
6859 } else if (match(&Cmp, m_c_ICmp(Pred,
6862 m_Add(m_Shl(m_One(), m_Value(Y)),
6863 m_AllOnes()))),
6864 m_Value(X)))) {
6865 // The variant with 'add' is not canonical, (the variant with 'not' is)
6866 // we only get it because it has extra uses, and can't be canonicalized,
6867
6868 switch (Pred) {
6869 case ICmpInst::ICMP_ULT:
6870 NewPred = ICmpInst::ICMP_NE;
6871 break;
6872 case ICmpInst::ICMP_UGE:
6873 NewPred = ICmpInst::ICMP_EQ;
6874 break;
6875 default:
6876 return nullptr;
6877 }
6878 } else
6879 return nullptr;
6880
6881 Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits");
6882 Constant *Zero = Constant::getNullValue(NewX->getType());
6883 return CmpInst::Create(Instruction::ICmp, NewPred, NewX, Zero);
6884}
6885
6887 InstCombiner::BuilderTy &Builder) {
6888 const CmpInst::Predicate Pred = Cmp.getPredicate();
6889 Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1);
6890 Value *V1, *V2;
6891
6892 auto createCmpReverse = [&](CmpInst::Predicate Pred, Value *X, Value *Y) {
6893 Value *V = Builder.CreateCmp(Pred, X, Y, Cmp.getName());
6894 if (auto *I = dyn_cast<Instruction>(V))
6895 I->copyIRFlags(&Cmp);
6896 Module *M = Cmp.getModule();
6897 Function *F =
6898 Intrinsic::getDeclaration(M, Intrinsic::vector_reverse, V->getType());
6899 return CallInst::Create(F, V);
6900 };
6901
6902 if (match(LHS, m_VecReverse(m_Value(V1)))) {
6903 // cmp Pred, rev(V1), rev(V2) --> rev(cmp Pred, V1, V2)
6904 if (match(RHS, m_VecReverse(m_Value(V2))) &&
6905 (LHS->hasOneUse() || RHS->hasOneUse()))
6906 return createCmpReverse(Pred, V1, V2);
6907
6908 // cmp Pred, rev(V1), RHSSplat --> rev(cmp Pred, V1, RHSSplat)
6909 if (LHS->hasOneUse() && isSplatValue(RHS))
6910 return createCmpReverse(Pred, V1, RHS);
6911 }
6912 // cmp Pred, LHSSplat, rev(V2) --> rev(cmp Pred, LHSSplat, V2)
6913 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
6914 return createCmpReverse(Pred, LHS, V2);
6915
6916 ArrayRef<int> M;
6917 if (!match(LHS, m_Shuffle(m_Value(V1), m_Undef(), m_Mask(M))))
6918 return nullptr;
6919
6920 // If both arguments of the cmp are shuffles that use the same mask and
6921 // shuffle within a single vector, move the shuffle after the cmp:
6922 // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M
6923 Type *V1Ty = V1->getType();
6924 if (match(RHS, m_Shuffle(m_Value(V2), m_Undef(), m_SpecificMask(M))) &&
6925 V1Ty == V2->getType() && (LHS->hasOneUse() || RHS->hasOneUse())) {
6926 Value *NewCmp = Builder.CreateCmp(Pred, V1, V2);
6927 return new ShuffleVectorInst(NewCmp, M);
6928 }
6929
6930 // Try to canonicalize compare with splatted operand and splat constant.
6931 // TODO: We could generalize this for more than splats. See/use the code in
6932 // InstCombiner::foldVectorBinop().
6933 Constant *C;
6934 if (!LHS->hasOneUse() || !match(RHS, m_Constant(C)))
6935 return nullptr;
6936
6937 // Length-changing splats are ok, so adjust the constants as needed:
6938 // cmp (shuffle V1, M), C --> shuffle (cmp V1, C'), M
6939 Constant *ScalarC = C->getSplatValue(/* AllowPoison */ true);
6940 int MaskSplatIndex;
6941 if (ScalarC && match(M, m_SplatOrPoisonMask(MaskSplatIndex))) {
6942 // We allow poison in matching, but this transform removes it for safety.
6943 // Demanded elements analysis should be able to recover some/all of that.
6944 C = ConstantVector::getSplat(cast<VectorType>(V1Ty)->getElementCount(),
6945 ScalarC);
6946 SmallVector<int, 8> NewM(M.size(), MaskSplatIndex);
6947 Value *NewCmp = Builder.CreateCmp(Pred, V1, C);
6948 return new ShuffleVectorInst(NewCmp, NewM);
6949 }
6950
6951 return nullptr;
6952}
6953
6954// extract(uadd.with.overflow(A, B), 0) ult A
6955// -> extract(uadd.with.overflow(A, B), 1)
6957 CmpInst::Predicate Pred = I.getPredicate();
6958 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
6959
6960 Value *UAddOv;
6961 Value *A, *B;
6962 auto UAddOvResultPat = m_ExtractValue<0>(
6963 m_Intrinsic<Intrinsic::uadd_with_overflow>(m_Value(A), m_Value(B)));
6964 if (match(Op0, UAddOvResultPat) &&
6965 ((Pred == ICmpInst::ICMP_ULT && (Op1 == A || Op1 == B)) ||
6966 (Pred == ICmpInst::ICMP_EQ && match(Op1, m_ZeroInt()) &&
6967 (match(A, m_One()) || match(B, m_One()))) ||
6968 (Pred == ICmpInst::ICMP_NE && match(Op1, m_AllOnes()) &&
6969 (match(A, m_AllOnes()) || match(B, m_AllOnes())))))
6970 // extract(uadd.with.overflow(A, B), 0) < A
6971 // extract(uadd.with.overflow(A, 1), 0) == 0
6972 // extract(uadd.with.overflow(A, -1), 0) != -1
6973 UAddOv = cast<ExtractValueInst>(Op0)->getAggregateOperand();
6974 else if (match(Op1, UAddOvResultPat) &&
6975 Pred == ICmpInst::ICMP_UGT && (Op0 == A || Op0 == B))
6976 // A > extract(uadd.with.overflow(A, B), 0)
6977 UAddOv = cast<ExtractValueInst>(Op1)->getAggregateOperand();
6978 else
6979 return nullptr;
6980
6981 return ExtractValueInst::Create(UAddOv, 1);
6982}
6983
6985 if (!I.getOperand(0)->getType()->isPointerTy() ||
6987 I.getParent()->getParent(),
6988 I.getOperand(0)->getType()->getPointerAddressSpace())) {
6989 return nullptr;
6990 }
6991 Instruction *Op;
6992 if (match(I.getOperand(0), m_Instruction(Op)) &&
6993 match(I.getOperand(1), m_Zero()) &&
6994 Op->isLaunderOrStripInvariantGroup()) {
6995 return ICmpInst::Create(Instruction::ICmp, I.getPredicate(),
6996 Op->getOperand(0), I.getOperand(1));
6997 }
6998 return nullptr;
6999}
7000
7001/// This function folds patterns produced by lowering of reduce idioms, such as
7002/// llvm.vector.reduce.and which are lowered into instruction chains. This code
7003/// attempts to generate fewer number of scalar comparisons instead of vector
7004/// comparisons when possible.
7006 InstCombiner::BuilderTy &Builder,
7007 const DataLayout &DL) {
7008 if (I.getType()->isVectorTy())
7009 return nullptr;
7010 ICmpInst::Predicate OuterPred, InnerPred;
7011 Value *LHS, *RHS;
7012
7013 // Match lowering of @llvm.vector.reduce.and. Turn
7014 /// %vec_ne = icmp ne <8 x i8> %lhs, %rhs
7015 /// %scalar_ne = bitcast <8 x i1> %vec_ne to i8
7016 /// %res = icmp <pred> i8 %scalar_ne, 0
7017 ///
7018 /// into
7019 ///
7020 /// %lhs.scalar = bitcast <8 x i8> %lhs to i64
7021 /// %rhs.scalar = bitcast <8 x i8> %rhs to i64
7022 /// %res = icmp <pred> i64 %lhs.scalar, %rhs.scalar
7023 ///
7024 /// for <pred> in {ne, eq}.
7025 if (!match(&I, m_ICmp(OuterPred,
7027 m_ICmp(InnerPred, m_Value(LHS), m_Value(RHS))))),
7028 m_Zero())))
7029 return nullptr;
7030 auto *LHSTy = dyn_cast<FixedVectorType>(LHS->getType());
7031 if (!LHSTy || !LHSTy->getElementType()->isIntegerTy())
7032 return nullptr;
7033 unsigned NumBits =
7034 LHSTy->getNumElements() * LHSTy->getElementType()->getIntegerBitWidth();
7035 // TODO: Relax this to "not wider than max legal integer type"?
7036 if (!DL.isLegalInteger(NumBits))
7037 return nullptr;
7038
7039 if (ICmpInst::isEquality(OuterPred) && InnerPred == ICmpInst::ICMP_NE) {
7040 auto *ScalarTy = Builder.getIntNTy(NumBits);
7041 LHS = Builder.CreateBitCast(LHS, ScalarTy, LHS->getName() + ".scalar");
7042 RHS = Builder.CreateBitCast(RHS, ScalarTy, RHS->getName() + ".scalar");
7043 return ICmpInst::Create(Instruction::ICmp, OuterPred, LHS, RHS,
7044 I.getName());
7045 }
7046
7047 return nullptr;
7048}
7049
7050// This helper will be called with icmp operands in both orders.
7052 Value *Op0, Value *Op1,
7053 ICmpInst &CxtI) {
7054 // Try to optimize 'icmp GEP, P' or 'icmp P, GEP'.
7055 if (auto *GEP = dyn_cast<GEPOperator>(Op0))
7056 if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
7057 return NI;
7058
7059 if (auto *SI = dyn_cast<SelectInst>(Op0))
7060 if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
7061 return NI;
7062
7063 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op0))
7064 if (Instruction *Res = foldICmpWithMinMax(CxtI, MinMax, Op1, Pred))
7065 return Res;
7066
7067 {
7068 Value *X;
7069 const APInt *C;
7070 // icmp X+Cst, X
7071 if (match(Op0, m_Add(m_Value(X), m_APInt(C))) && Op1 == X)
7072 return foldICmpAddOpConst(X, *C, Pred);
7073 }
7074
7075 // abs(X) >= X --> true
7076 // abs(X) u<= X --> true
7077 // abs(X) < X --> false
7078 // abs(X) u> X --> false
7079 // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7080 // abs(X) <= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7081 // abs(X) == X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
7082 // abs(X) u< X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7083 // abs(X) > X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7084 // abs(X) != X --> IsIntMinPosion ? `X < 0` : `X > INTMIN`
7085 {
7086 Value *X;
7087 Constant *C;
7088 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
7089 match(Op1, m_Specific(X))) {
7090 Value *NullValue = Constant::getNullValue(X->getType());
7091 Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
7092 const APInt SMin =
7093 APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
7094 bool IsIntMinPosion = C->isAllOnesValue();
7095 switch (Pred) {
7096 case CmpInst::ICMP_ULE:
7097 case CmpInst::ICMP_SGE:
7098 return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
7099 case CmpInst::ICMP_UGT:
7100 case CmpInst::ICMP_SLT:
7102 case CmpInst::ICMP_UGE:
7103 case CmpInst::ICMP_SLE:
7104 case CmpInst::ICMP_EQ: {
7105 return replaceInstUsesWith(
7106 CxtI, IsIntMinPosion
7107 ? Builder.CreateICmpSGT(X, AllOnesValue)
7109 X, ConstantInt::get(X->getType(), SMin + 1)));
7110 }
7111 case CmpInst::ICMP_ULT:
7112 case CmpInst::ICMP_SGT:
7113 case CmpInst::ICMP_NE: {
7114 return replaceInstUsesWith(
7115 CxtI, IsIntMinPosion
7116 ? Builder.CreateICmpSLT(X, NullValue)
7118 X, ConstantInt::get(X->getType(), SMin)));
7119 }
7120 default:
7121 llvm_unreachable("Invalid predicate!");
7122 }
7123 }
7124 }
7125
7126 const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
7127 if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
7128 return replaceInstUsesWith(CxtI, V);
7129
7130 // Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7131 auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
7132 {
7133 if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7134 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7136 }
7137
7138 if (!ICmpInst::isUnsigned(Pred) &&
7139 match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
7140 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7142 }
7143 }
7144
7145 // Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7146 auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
7147 {
7148 if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7149 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7151 }
7152
7153 if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7154 match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
7155 return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
7157 }
7158 }
7159
7160 return nullptr;
7161}
7162
7164 bool Changed = false;
7166 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7167 unsigned Op0Cplxity = getComplexity(Op0);
7168 unsigned Op1Cplxity = getComplexity(Op1);
7169
7170 /// Orders the operands of the compare so that they are listed from most
7171 /// complex to least complex. This puts constants before unary operators,
7172 /// before binary operators.
7173 if (Op0Cplxity < Op1Cplxity) {
7174 I.swapOperands();
7175 std::swap(Op0, Op1);
7176 Changed = true;
7177 }
7178
7179 if (Value *V = simplifyICmpInst(I.getPredicate(), Op0, Op1, Q))
7180 return replaceInstUsesWith(I, V);
7181
7182 // Comparing -val or val with non-zero is the same as just comparing val
7183 // ie, abs(val) != 0 -> val != 0
7184 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
7185 Value *Cond, *SelectTrue, *SelectFalse;
7186 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
7187 m_Value(SelectFalse)))) {
7188 if (Value *V = dyn_castNegVal(SelectTrue)) {
7189 if (V == SelectFalse)
7190 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7191 }
7192 else if (Value *V = dyn_castNegVal(SelectFalse)) {
7193 if (V == SelectTrue)
7194 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
7195 }
7196 }
7197 }
7198
7199 if (Op0->getType()->isIntOrIntVectorTy(1))
7201 return Res;
7202
7204 return Res;
7205
7207 return Res;
7208
7210 return Res;
7211
7213 return Res;
7214
7216 return Res;
7217
7219 return Res;
7220
7222 return Res;
7223
7224 // Test if the ICmpInst instruction is used exclusively by a select as
7225 // part of a minimum or maximum operation. If so, refrain from doing
7226 // any other folding. This helps out other analyses which understand
7227 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7228 // and CodeGen. And in this case, at least one of the comparison
7229 // operands has at least one user besides the compare (the select),
7230 // which would often largely negate the benefit of folding anyway.
7231 //
7232 // Do the same for the other patterns recognized by matchSelectPattern.
7233 if (I.hasOneUse())
7234 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7235 Value *A, *B;
7237 if (SPR.Flavor != SPF_UNKNOWN)
7238 return nullptr;
7239 }
7240
7241 // Do this after checking for min/max to prevent infinite looping.
7242 if (Instruction *Res = foldICmpWithZero(I))
7243 return Res;
7244
7245 // FIXME: We only do this after checking for min/max to prevent infinite
7246 // looping caused by a reverse canonicalization of these patterns for min/max.
7247 // FIXME: The organization of folds is a mess. These would naturally go into
7248 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
7249 // down here after the min/max restriction.
7250 ICmpInst::Predicate Pred = I.getPredicate();
7251 const APInt *C;
7252 if (match(Op1, m_APInt(C))) {
7253 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
7254 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
7255 Constant *Zero = Constant::getNullValue(Op0->getType());
7256 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
7257 }
7258
7259 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
7260 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
7262 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
7263 }
7264 }
7265
7266 // The folds in here may rely on wrapping flags and special constants, so
7267 // they can break up min/max idioms in some cases but not seemingly similar
7268 // patterns.
7269 // FIXME: It may be possible to enhance select folding to make this
7270 // unnecessary. It may also be moot if we canonicalize to min/max
7271 // intrinsics.
7272 if (Instruction *Res = foldICmpBinOp(I, Q))
7273 return Res;
7274
7276 return Res;
7277
7278 // Try to match comparison as a sign bit test. Intentionally do this after
7279 // foldICmpInstWithConstant() to potentially let other folds to happen first.
7280 if (Instruction *New = foldSignBitTest(I))
7281 return New;
7282
7284 return Res;
7285
7286 if (Instruction *Res = foldICmpCommutative(I.getPredicate(), Op0, Op1, I))
7287 return Res;
7288 if (Instruction *Res =
7289 foldICmpCommutative(I.getSwappedPredicate(), Op1, Op0, I))
7290 return Res;
7291
7292 if (I.isCommutative()) {
7293 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7294 replaceOperand(I, 0, Pair->first);
7295 replaceOperand(I, 1, Pair->second);
7296 return &I;
7297 }
7298 }
7299
7300 // In case of a comparison with two select instructions having the same
7301 // condition, check whether one of the resulting branches can be simplified.
7302 // If so, just compare the other branch and select the appropriate result.
7303 // For example:
7304 // %tmp1 = select i1 %cmp, i32 %y, i32 %x
7305 // %tmp2 = select i1 %cmp, i32 %z, i32 %x
7306 // %cmp2 = icmp slt i32 %tmp2, %tmp1
7307 // The icmp will result false for the false value of selects and the result
7308 // will depend upon the comparison of true values of selects if %cmp is
7309 // true. Thus, transform this into:
7310 // %cmp = icmp slt i32 %y, %z
7311 // %sel = select i1 %cond, i1 %cmp, i1 false
7312 // This handles similar cases to transform.
7313 {
7314 Value *Cond, *A, *B, *C, *D;
7315 if (match(Op0, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
7317 (Op0->hasOneUse() || Op1->hasOneUse())) {
7318 // Check whether comparison of TrueValues can be simplified
7319 if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) {
7320 Value *NewICMP = Builder.CreateICmp(Pred, B, D);
7321 return SelectInst::Create(Cond, Res, NewICMP);
7322 }
7323 // Check whether comparison of FalseValues can be simplified
7324 if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) {
7325 Value *NewICMP = Builder.CreateICmp(Pred, A, C);
7326 return SelectInst::Create(Cond, NewICMP, Res);
7327 }
7328 }
7329 }
7330
7331 // Try to optimize equality comparisons against alloca-based pointers.
7332 if (Op0->getType()->isPointerTy() && I.isEquality()) {
7333 assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?");
7334 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op0)))
7335 if (foldAllocaCmp(Alloca))
7336 return nullptr;
7337 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(Op1)))
7338 if (foldAllocaCmp(Alloca))
7339 return nullptr;
7340 }
7341
7342 if (Instruction *Res = foldICmpBitCast(I))
7343 return Res;
7344
7345 // TODO: Hoist this above the min/max bailout.
7347 return R;
7348
7349 {
7350 Value *X, *Y;
7351 // Transform (X & ~Y) == 0 --> (X & Y) != 0
7352 // and (X & ~Y) != 0 --> (X & Y) == 0
7353 // if A is a power of 2.
7354 if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
7355 match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, 0, &I) &&
7356 I.isEquality())
7357 return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
7358 Op1);
7359
7360 // Op0 pred Op1 -> ~Op1 pred ~Op0, if this allows us to drop an instruction.
7361 if (Op0->getType()->isIntOrIntVectorTy()) {
7362 bool ConsumesOp0, ConsumesOp1;
7363 if (isFreeToInvert(Op0, Op0->hasOneUse(), ConsumesOp0) &&
7364 isFreeToInvert(Op1, Op1->hasOneUse(), ConsumesOp1) &&
7365 (ConsumesOp0 || ConsumesOp1)) {
7366 Value *InvOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
7367 Value *InvOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder);
7368 assert(InvOp0 && InvOp1 &&
7369 "Mismatch between isFreeToInvert and getFreelyInverted");
7370 return new ICmpInst(I.getSwappedPredicate(), InvOp0, InvOp1);
7371 }
7372 }
7373
7374 Instruction *AddI = nullptr;
7376 m_Instruction(AddI))) &&
7377 isa<IntegerType>(X->getType())) {
7378 Value *Result;
7379 Constant *Overflow;
7380 // m_UAddWithOverflow can match patterns that do not include an explicit
7381 // "add" instruction, so check the opcode of the matched op.
7382 if (AddI->getOpcode() == Instruction::Add &&
7383 OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
7384 Result, Overflow)) {
7385 replaceInstUsesWith(*AddI, Result);
7386 eraseInstFromFunction(*AddI);
7387 return replaceInstUsesWith(I, Overflow);
7388 }
7389 }
7390
7391 // (zext X) * (zext Y) --> llvm.umul.with.overflow.
7392 if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
7393 match(Op1, m_APInt(C))) {
7394 if (Instruction *R = processUMulZExtIdiom(I, Op0, C, *this))
7395 return R;
7396 }
7397
7398 // Signbit test folds
7399 // Fold (X u>> BitWidth - 1 Pred ZExt(i1)) --> X s< 0 Pred i1
7400 // Fold (X s>> BitWidth - 1 Pred SExt(i1)) --> X s< 0 Pred i1
7401 Instruction *ExtI;
7402 if ((I.isUnsigned() || I.isEquality()) &&
7403 match(Op1,
7405 Y->getType()->getScalarSizeInBits() == 1 &&
7406 (Op0->hasOneUse() || Op1->hasOneUse())) {
7407 unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
7408 Instruction *ShiftI;
7409 if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
7411 OpWidth - 1))))) {
7412 unsigned ExtOpc = ExtI->getOpcode();
7413 unsigned ShiftOpc = ShiftI->getOpcode();
7414 if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
7415 (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
7416 Value *SLTZero =
7418 Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
7419 return replaceInstUsesWith(I, Cmp);
7420 }
7421 }
7422 }
7423 }
7424
7425 if (Instruction *Res = foldICmpEquality(I))
7426 return Res;
7427
7429 return Res;
7430
7431 if (Instruction *Res = foldICmpOfUAddOv(I))
7432 return Res;
7433
7434 // The 'cmpxchg' instruction returns an aggregate containing the old value and
7435 // an i1 which indicates whether or not we successfully did the swap.
7436 //
7437 // Replace comparisons between the old value and the expected value with the
7438 // indicator that 'cmpxchg' returns.
7439 //
7440 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
7441 // spuriously fail. In those cases, the old value may equal the expected
7442 // value but it is possible for the swap to not occur.
7443 if (I.getPredicate() == ICmpInst::ICMP_EQ)
7444 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
7445 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
7446 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
7447 !ACXI->isWeak())
7448 return ExtractValueInst::Create(ACXI, 1);
7449
7451 return Res;
7452
7453 if (I.getType()->isVectorTy())
7454 if (Instruction *Res = foldVectorCmp(I, Builder))
7455 return Res;
7456
7458 return Res;
7459
7461 return Res;
7462
7463 return Changed ? &I : nullptr;
7464}
7465
7466/// Fold fcmp ([us]itofp x, cst) if possible.
7468 Instruction *LHSI,
7469 Constant *RHSC) {
7470 const APFloat *RHS;
7471 if (!match(RHSC, m_APFloat(RHS)))
7472 return nullptr;
7473
7474 // Get the width of the mantissa. We don't want to hack on conversions that
7475 // might lose information from the integer, e.g. "i64 -> float"
7476 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
7477 if (MantissaWidth == -1) return nullptr; // Unknown.
7478
7479 Type *IntTy = LHSI->getOperand(0)->getType();
7480 unsigned IntWidth = IntTy->getScalarSizeInBits();
7481 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
7482
7483 if (I.isEquality()) {
7484 FCmpInst::Predicate P = I.getPredicate();
7485 bool IsExact = false;
7486 APSInt RHSCvt(IntWidth, LHSUnsigned);
7487 RHS->convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7488
7489 // If the floating point constant isn't an integer value, we know if we will
7490 // ever compare equal / not equal to it.
7491 if (!IsExact) {
7492 // TODO: Can never be -0.0 and other non-representable values
7493 APFloat RHSRoundInt(*RHS);
7495 if (*RHS != RHSRoundInt) {
7497 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7498
7500 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7501 }
7502 }
7503
7504 // TODO: If the constant is exactly representable, is it always OK to do
7505 // equality compares as integer?
7506 }
7507
7508 // Check to see that the input is converted from an integer type that is small
7509 // enough that preserves all bits. TODO: check here for "known" sign bits.
7510 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
7511
7512 // Following test does NOT adjust IntWidth downwards for signed inputs,
7513 // because the most negative value still requires all the mantissa bits
7514 // to distinguish it from one less than that value.
7515 if ((int)IntWidth > MantissaWidth) {
7516 // Conversion would lose accuracy. Check if loss can impact comparison.
7517 int Exp = ilogb(*RHS);
7518 if (Exp == APFloat::IEK_Inf) {
7519 int MaxExponent = ilogb(APFloat::getLargest(RHS->getSemantics()));
7520 if (MaxExponent < (int)IntWidth - !LHSUnsigned)
7521 // Conversion could create infinity.
7522 return nullptr;
7523 } else {
7524 // Note that if RHS is zero or NaN, then Exp is negative
7525 // and first condition is trivially false.
7526 if (MantissaWidth <= Exp && Exp <= (int)IntWidth - !LHSUnsigned)
7527 // Conversion could affect comparison.
7528 return nullptr;
7529 }
7530 }
7531
7532 // Otherwise, we can potentially simplify the comparison. We know that it
7533 // will always come through as an integer value and we know the constant is
7534 // not a NAN (it would have been previously simplified).
7535 assert(!RHS->isNaN() && "NaN comparison not already folded!");
7536
7538 switch (I.getPredicate()) {
7539 default: llvm_unreachable("Unexpected predicate!");
7540 case FCmpInst::FCMP_UEQ:
7541 case FCmpInst::FCMP_OEQ:
7542 Pred = ICmpInst::ICMP_EQ;
7543 break;
7544 case FCmpInst::FCMP_UGT:
7545 case FCmpInst::FCMP_OGT:
7546 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
7547 break;
7548 case FCmpInst::FCMP_UGE:
7549 case FCmpInst::FCMP_OGE:
7550 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
7551 break;
7552 case FCmpInst::FCMP_ULT:
7553 case FCmpInst::FCMP_OLT:
7554 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
7555 break;
7556 case FCmpInst::FCMP_ULE:
7557 case FCmpInst::FCMP_OLE:
7558 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
7559 break;
7560 case FCmpInst::FCMP_UNE:
7561 case FCmpInst::FCMP_ONE:
7562 Pred = ICmpInst::ICMP_NE;
7563 break;
7564 case FCmpInst::FCMP_ORD:
7565 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7566 case FCmpInst::FCMP_UNO:
7567 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7568 }
7569
7570 // Now we know that the APFloat is a normal number, zero or inf.
7571
7572 // See if the FP constant is too large for the integer. For example,
7573 // comparing an i8 to 300.0.
7574 if (!LHSUnsigned) {
7575 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
7576 // and large values.
7577 APFloat SMax(RHS->getSemantics());
7578 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
7580 if (SMax < *RHS) { // smax < 13123.0
7581 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
7582 Pred == ICmpInst::ICMP_SLE)
7583 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7584 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7585 }
7586 } else {
7587 // If the RHS value is > UnsignedMax, fold the comparison. This handles
7588 // +INF and large values.
7589 APFloat UMax(RHS->getSemantics());
7590 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
7592 if (UMax < *RHS) { // umax < 13123.0
7593 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
7594 Pred == ICmpInst::ICMP_ULE)
7595 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7596 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7597 }
7598 }
7599
7600 if (!LHSUnsigned) {
7601 // See if the RHS value is < SignedMin.
7602 APFloat SMin(RHS->getSemantics());
7603 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
7605 if (SMin > *RHS) { // smin > 12312.0
7606 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
7607 Pred == ICmpInst::ICMP_SGE)
7608 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7609 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7610 }
7611 } else {
7612 // See if the RHS value is < UnsignedMin.
7613 APFloat UMin(RHS->getSemantics());
7614 UMin.convertFromAPInt(APInt::getMinValue(IntWidth), false,
7616 if (UMin > *RHS) { // umin > 12312.0
7617 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
7618 Pred == ICmpInst::ICMP_UGE)
7619 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7620 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7621 }
7622 }
7623
7624 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
7625 // [0, UMAX], but it may still be fractional. Check whether this is the case
7626 // using the IsExact flag.
7627 // Don't do this for zero, because -0.0 is not fractional.
7628 APSInt RHSInt(IntWidth, LHSUnsigned);
7629 bool IsExact;
7630 RHS->convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
7631 if (!RHS->isZero()) {
7632 if (!IsExact) {
7633 // If we had a comparison against a fractional value, we have to adjust
7634 // the compare predicate and sometimes the value. RHSC is rounded towards
7635 // zero at this point.
7636 switch (Pred) {
7637 default: llvm_unreachable("Unexpected integer comparison!");
7638 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
7639 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7640 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
7641 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7642 case ICmpInst::ICMP_ULE:
7643 // (float)int <= 4.4 --> int <= 4
7644 // (float)int <= -4.4 --> false
7645 if (RHS->isNegative())
7646 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7647 break;
7648 case ICmpInst::ICMP_SLE:
7649 // (float)int <= 4.4 --> int <= 4
7650 // (float)int <= -4.4 --> int < -4
7651 if (RHS->isNegative())
7652 Pred = ICmpInst::ICMP_SLT;
7653 break;
7654 case ICmpInst::ICMP_ULT:
7655 // (float)int < -4.4 --> false
7656 // (float)int < 4.4 --> int <= 4
7657 if (RHS->isNegative())
7658 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
7659 Pred = ICmpInst::ICMP_ULE;
7660 break;
7661 case ICmpInst::ICMP_SLT:
7662 // (float)int < -4.4 --> int < -4
7663 // (float)int < 4.4 --> int <= 4
7664 if (!RHS->isNegative())
7665 Pred = ICmpInst::ICMP_SLE;
7666 break;
7667 case ICmpInst::ICMP_UGT:
7668 // (float)int > 4.4 --> int > 4
7669 // (float)int > -4.4 --> true
7670 if (RHS->isNegative())
7671 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7672 break;
7673 case ICmpInst::ICMP_SGT:
7674 // (float)int > 4.4 --> int > 4
7675 // (float)int > -4.4 --> int >= -4
7676 if (RHS->isNegative())
7677 Pred = ICmpInst::ICMP_SGE;
7678 break;
7679 case ICmpInst::ICMP_UGE:
7680 // (float)int >= -4.4 --> true
7681 // (float)int >= 4.4 --> int > 4
7682 if (RHS->isNegative())
7683 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
7684 Pred = ICmpInst::ICMP_UGT;
7685 break;
7686 case ICmpInst::ICMP_SGE:
7687 // (float)int >= -4.4 --> int >= -4
7688 // (float)int >= 4.4 --> int > 4
7689 if (!RHS->isNegative())
7690 Pred = ICmpInst::ICMP_SGT;
7691 break;
7692 }
7693 }
7694 }
7695
7696 // Lower this FP comparison into an appropriate integer version of the
7697 // comparison.
7698 return new ICmpInst(Pred, LHSI->getOperand(0),
7699 ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
7700}
7701
7702/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
7704 Constant *RHSC) {
7705 // When C is not 0.0 and infinities are not allowed:
7706 // (C / X) < 0.0 is a sign-bit test of X
7707 // (C / X) < 0.0 --> X < 0.0 (if C is positive)
7708 // (C / X) < 0.0 --> X > 0.0 (if C is negative, swap the predicate)
7709 //
7710 // Proof:
7711 // Multiply (C / X) < 0.0 by X * X / C.
7712 // - X is non zero, if it is the flag 'ninf' is violated.
7713 // - C defines the sign of X * X * C. Thus it also defines whether to swap
7714 // the predicate. C is also non zero by definition.
7715 //
7716 // Thus X * X / C is non zero and the transformation is valid. [qed]
7717
7718 FCmpInst::Predicate Pred = I.getPredicate();
7719
7720 // Check that predicates are valid.
7721 if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
7722 (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
7723 return nullptr;
7724
7725 // Check that RHS operand is zero.
7726 if (!match(RHSC, m_AnyZeroFP()))
7727 return nullptr;
7728
7729 // Check fastmath flags ('ninf').
7730 if (!LHSI->hasNoInfs() || !I.hasNoInfs())
7731 return nullptr;
7732
7733 // Check the properties of the dividend. It must not be zero to avoid a
7734 // division by zero (see Proof).
7735 const APFloat *C;
7736 if (!match(LHSI->getOperand(0), m_APFloat(C)))
7737 return nullptr;
7738
7739 if (C->isZero())
7740 return nullptr;
7741
7742 // Get swapped predicate if necessary.
7743 if (C->isNegative())
7744 Pred = I.getSwappedPredicate();
7745
7746 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
7747}
7748
7749/// Optimize fabs(X) compared with zero.
7751 Value *X;
7752 if (!match(I.getOperand(0), m_FAbs(m_Value(X))))
7753 return nullptr;
7754
7755 const APFloat *C;
7756 if (!match(I.getOperand(1), m_APFloat(C)))
7757 return nullptr;
7758
7759 if (!C->isPosZero()) {
7760 if (!C->isSmallestNormalized())
7761 return nullptr;
7762
7763 const Function *F = I.getFunction();
7764 DenormalMode Mode = F->getDenormalMode(C->getSemantics());
7765 if (Mode.Input == DenormalMode::PreserveSign ||
7766 Mode.Input == DenormalMode::PositiveZero) {
7767
7768 auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7769 Constant *Zero = ConstantFP::getZero(X->getType());
7770 return new FCmpInst(P, X, Zero, "", I);
7771 };
7772
7773 switch (I.getPredicate()) {
7774 case FCmpInst::FCMP_OLT:
7775 // fcmp olt fabs(x), smallest_normalized_number -> fcmp oeq x, 0.0
7776 return replaceFCmp(&I, FCmpInst::FCMP_OEQ, X);
7777 case FCmpInst::FCMP_UGE:
7778 // fcmp uge fabs(x), smallest_normalized_number -> fcmp une x, 0.0
7779 return replaceFCmp(&I, FCmpInst::FCMP_UNE, X);
7780 case FCmpInst::FCMP_OGE:
7781 // fcmp oge fabs(x), smallest_normalized_number -> fcmp one x, 0.0
7782 return replaceFCmp(&I, FCmpInst::FCMP_ONE, X);
7783 case FCmpInst::FCMP_ULT:
7784 // fcmp ult fabs(x), smallest_normalized_number -> fcmp ueq x, 0.0
7785 return replaceFCmp(&I, FCmpInst::FCMP_UEQ, X);
7786 default:
7787 break;
7788 }
7789 }
7790
7791 return nullptr;
7792 }
7793
7794 auto replacePredAndOp0 = [&IC](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
7795 I->setPredicate(P);
7796 return IC.replaceOperand(*I, 0, X);
7797 };
7798
7799 switch (I.getPredicate()) {
7800 case FCmpInst::FCMP_UGE:
7801 case FCmpInst::FCMP_OLT:
7802 // fabs(X) >= 0.0 --> true
7803 // fabs(X) < 0.0 --> false
7804 llvm_unreachable("fcmp should have simplified");
7805
7806 case FCmpInst::FCMP_OGT:
7807 // fabs(X) > 0.0 --> X != 0.0
7808 return replacePredAndOp0(&I, FCmpInst::FCMP_ONE, X);
7809
7810 case FCmpInst::FCMP_UGT:
7811 // fabs(X) u> 0.0 --> X u!= 0.0
7812 return replacePredAndOp0(&I, FCmpInst::FCMP_UNE, X);
7813
7814 case FCmpInst::FCMP_OLE:
7815 // fabs(X) <= 0.0 --> X == 0.0
7816 return replacePredAndOp0(&I, FCmpInst::FCMP_OEQ, X);
7817
7818 case FCmpInst::FCMP_ULE:
7819 // fabs(X) u<= 0.0 --> X u== 0.0
7820 return replacePredAndOp0(&I, FCmpInst::FCMP_UEQ, X);
7821
7822 case FCmpInst::FCMP_OGE:
7823 // fabs(X) >= 0.0 --> !isnan(X)
7824 assert(!I.hasNoNaNs() && "fcmp should have simplified");
7825 return replacePredAndOp0(&I, FCmpInst::FCMP_ORD, X);
7826
7827 case FCmpInst::FCMP_ULT:
7828 // fabs(X) u< 0.0 --> isnan(X)
7829 assert(!I.hasNoNaNs() && "fcmp should have simplified");
7830 return replacePredAndOp0(&I, FCmpInst::FCMP_UNO, X);
7831
7832 case FCmpInst::FCMP_OEQ:
7833 case FCmpInst::FCMP_UEQ:
7834 case FCmpInst::FCMP_ONE:
7835 case FCmpInst::FCMP_UNE:
7836 case FCmpInst::FCMP_ORD:
7837 case FCmpInst::FCMP_UNO:
7838 // Look through the fabs() because it doesn't change anything but the sign.
7839 // fabs(X) == 0.0 --> X == 0.0,
7840 // fabs(X) != 0.0 --> X != 0.0
7841 // isnan(fabs(X)) --> isnan(X)
7842 // !isnan(fabs(X) --> !isnan(X)
7843 return replacePredAndOp0(&I, I.getPredicate(), X);
7844
7845 default:
7846 return nullptr;
7847 }
7848}
7849
7851 CmpInst::Predicate Pred = I.getPredicate();
7852 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7853
7854 // Canonicalize fneg as Op1.
7855 if (match(Op0, m_FNeg(m_Value())) && !match(Op1, m_FNeg(m_Value()))) {
7856 std::swap(Op0, Op1);
7857 Pred = I.getSwappedPredicate();
7858 }
7859
7860 if (!match(Op1, m_FNeg(m_Specific(Op0))))
7861 return nullptr;
7862
7863 // Replace the negated operand with 0.0:
7864 // fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
7865 Constant *Zero = ConstantFP::getZero(Op0->getType());
7866 return new FCmpInst(Pred, Op0, Zero, "", &I);
7867}
7868
7870 bool Changed = false;
7871
7872 /// Orders the operands of the compare so that they are listed from most
7873 /// complex to least complex. This puts constants before unary operators,
7874 /// before binary operators.
7875 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
7876 I.swapOperands();
7877 Changed = true;
7878 }
7879
7880 const CmpInst::Predicate Pred = I.getPredicate();
7881 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
7882 if (Value *V = simplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(),
7884 return replaceInstUsesWith(I, V);
7885
7886 // Simplify 'fcmp pred X, X'
7887 Type *OpType = Op0->getType();
7888 assert(OpType == Op1->getType() && "fcmp with different-typed operands?");
7889 if (Op0 == Op1) {
7890 switch (Pred) {
7891 default: break;
7892 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
7893 case FCmpInst::FCMP_ULT: // True if unordered or less than
7894 case FCmpInst::FCMP_UGT: // True if unordered or greater than
7895 case FCmpInst::FCMP_UNE: // True if unordered or not equal
7896 // Canonicalize these to be 'fcmp uno %X, 0.0'.
7897 I.setPredicate(FCmpInst::FCMP_UNO);
7898 I.setOperand(1, Constant::getNullValue(OpType));
7899 return &I;
7900
7901 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
7902 case FCmpInst::FCMP_OEQ: // True if ordered and equal
7903 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
7904 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
7905 // Canonicalize these to be 'fcmp ord %X, 0.0'.
7906 I.setPredicate(FCmpInst::FCMP_ORD);
7907 I.setOperand(1, Constant::getNullValue(OpType));
7908 return &I;
7909 }
7910 }
7911
7912 if (I.isCommutative()) {
7913 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7914 replaceOperand(I, 0, Pair->first);
7915 replaceOperand(I, 1, Pair->second);
7916 return &I;
7917 }
7918 }
7919
7920 // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
7921 // then canonicalize the operand to 0.0.
7922 if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
7923 if (!match(Op0, m_PosZeroFP()) &&
7924 isKnownNeverNaN(Op0, 0, getSimplifyQuery().getWithInstruction(&I)))
7925 return replaceOperand(I, 0, ConstantFP::getZero(OpType));
7926
7927 if (!match(Op1, m_PosZeroFP()) &&
7928 isKnownNeverNaN(Op1, 0, getSimplifyQuery().getWithInstruction(&I)))
7929 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
7930 }
7931
7932 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
7933 Value *X, *Y;
7934 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
7935 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
7936
7938 return R;
7939
7940 // Test if the FCmpInst instruction is used exclusively by a select as
7941 // part of a minimum or maximum operation. If so, refrain from doing
7942 // any other folding. This helps out other analyses which understand
7943 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
7944 // and CodeGen. And in this case, at least one of the comparison
7945 // operands has at least one user besides the compare (the select),
7946 // which would often largely negate the benefit of folding anyway.
7947 if (I.hasOneUse())
7948 if (SelectInst *SI = dyn_cast<SelectInst>(I.user_back())) {
7949 Value *A, *B;
7951 if (SPR.Flavor != SPF_UNKNOWN)
7952 return nullptr;
7953 }
7954
7955 // The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
7956 // fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
7957 if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
7958 return replaceOperand(I, 1, ConstantFP::getZero(OpType));
7959
7960 // Canonicalize:
7961 // fcmp olt X, +inf -> fcmp one X, +inf
7962 // fcmp ole X, +inf -> fcmp ord X, 0
7963 // fcmp ogt X, +inf -> false
7964 // fcmp oge X, +inf -> fcmp oeq X, +inf
7965 // fcmp ult X, +inf -> fcmp une X, +inf
7966 // fcmp ule X, +inf -> true
7967 // fcmp ugt X, +inf -> fcmp uno X, 0
7968 // fcmp uge X, +inf -> fcmp ueq X, +inf
7969 // fcmp olt X, -inf -> false
7970 // fcmp ole X, -inf -> fcmp oeq X, -inf
7971 // fcmp ogt X, -inf -> fcmp one X, -inf
7972 // fcmp oge X, -inf -> fcmp ord X, 0
7973 // fcmp ult X, -inf -> fcmp uno X, 0
7974 // fcmp ule X, -inf -> fcmp ueq X, -inf
7975 // fcmp ugt X, -inf -> fcmp une X, -inf
7976 // fcmp uge X, -inf -> true
7977 const APFloat *C;
7978 if (match(Op1, m_APFloat(C)) && C->isInfinity()) {
7979 switch (C->isNegative() ? FCmpInst::getSwappedPredicate(Pred) : Pred) {
7980 default:
7981 break;
7982 case FCmpInst::FCMP_ORD:
7983 case FCmpInst::FCMP_UNO:
7986 case FCmpInst::FCMP_OGT:
7987 case FCmpInst::FCMP_ULE:
7988 llvm_unreachable("Should be simplified by InstSimplify");
7989 case FCmpInst::FCMP_OLT:
7990 return new FCmpInst(FCmpInst::FCMP_ONE, Op0, Op1, "", &I);
7991 case FCmpInst::FCMP_OLE:
7992 return new FCmpInst(FCmpInst::FCMP_ORD, Op0, ConstantFP::getZero(OpType),
7993 "", &I);
7994 case FCmpInst::FCMP_OGE:
7995 return new FCmpInst(FCmpInst::FCMP_OEQ, Op0, Op1, "", &I);
7996 case FCmpInst::FCMP_ULT:
7997 return new FCmpInst(FCmpInst::FCMP_UNE, Op0, Op1, "", &I);
7998 case FCmpInst::FCMP_UGT:
7999 return new FCmpInst(FCmpInst::FCMP_UNO, Op0, ConstantFP::getZero(OpType),
8000 "", &I);
8001 case FCmpInst::FCMP_UGE:
8002 return new FCmpInst(FCmpInst::FCMP_UEQ, Op0, Op1, "", &I);
8003 }
8004 }
8005
8006 // Ignore signbit of bitcasted int when comparing equality to FP 0.0:
8007 // fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
8008 if (match(Op1, m_PosZeroFP()) &&
8011 if (Pred == FCmpInst::FCMP_OEQ)
8012 IntPred = ICmpInst::ICMP_EQ;
8013 else if (Pred == FCmpInst::FCMP_UNE)
8014 IntPred = ICmpInst::ICMP_NE;
8015
8016 if (IntPred != ICmpInst::BAD_ICMP_PREDICATE) {
8017 Type *IntTy = X->getType();
8018 const APInt &SignMask = ~APInt::getSignMask(IntTy->getScalarSizeInBits());
8019 Value *MaskX = Builder.CreateAnd(X, ConstantInt::get(IntTy, SignMask));
8020 return new ICmpInst(IntPred, MaskX, ConstantInt::getNullValue(IntTy));
8021 }
8022 }
8023
8024 // Handle fcmp with instruction LHS and constant RHS.
8025 Instruction *LHSI;
8026 Constant *RHSC;
8027 if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
8028 switch (LHSI->getOpcode()) {
8029 case Instruction::Select:
8030 // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
8031 if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8032 (match(LHSI,
8034 match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
8035 return replaceOperand(I, 0, X);
8036 if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
8037 return NV;
8038 break;
8039 case Instruction::PHI:
8040 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
8041 return NV;
8042 break;
8043 case Instruction::SIToFP:
8044 case Instruction::UIToFP:
8045 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
8046 return NV;
8047 break;
8048 case Instruction::FDiv:
8049 if (Instruction *NV = foldFCmpReciprocalAndZero(I, LHSI, RHSC))
8050 return NV;
8051 break;
8052 case Instruction::Load:
8053 if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
8054 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
8056 cast<LoadInst>(LHSI), GEP, GV, I))
8057 return Res;
8058 break;
8059 }
8060 }
8061
8062 if (Instruction *R = foldFabsWithFcmpZero(I, *this))
8063 return R;
8064
8065 if (match(Op0, m_FNeg(m_Value(X)))) {
8066 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
8067 Constant *C;
8068 if (match(Op1, m_Constant(C)))
8069 if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
8070 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
8071 }
8072
8073 // fcmp (fadd X, 0.0), Y --> fcmp X, Y
8074 if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP())))
8075 return new FCmpInst(Pred, X, Op1, "", &I);
8076
8077 // fcmp X, (fadd Y, 0.0) --> fcmp X, Y
8078 if (match(Op1, m_FAdd(m_Value(Y), m_AnyZeroFP())))
8079 return new FCmpInst(Pred, Op0, Y, "", &I);
8080
8081 if (match(Op0, m_FPExt(m_Value(X)))) {
8082 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
8083 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
8084 return new FCmpInst(Pred, X, Y, "", &I);
8085
8086 const APFloat *C;
8087 if (match(Op1, m_APFloat(C))) {
8088 const fltSemantics &FPSem =
8089 X->getType()->getScalarType()->getFltSemantics();
8090 bool Lossy;
8091 APFloat TruncC = *C;
8092 TruncC.convert(FPSem, APFloat::rmNearestTiesToEven, &Lossy);
8093
8094 if (Lossy) {
8095 // X can't possibly equal the higher-precision constant, so reduce any
8096 // equality comparison.
8097 // TODO: Other predicates can be handled via getFCmpCode().
8098 switch (Pred) {
8099 case FCmpInst::FCMP_OEQ:
8100 // X is ordered and equal to an impossible constant --> false
8101 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
8102 case FCmpInst::FCMP_ONE:
8103 // X is ordered and not equal to an impossible constant --> ordered
8104 return new FCmpInst(FCmpInst::FCMP_ORD, X,
8105 ConstantFP::getZero(X->getType()));
8106 case FCmpInst::FCMP_UEQ:
8107 // X is unordered or equal to an impossible constant --> unordered
8108 return new FCmpInst(FCmpInst::FCMP_UNO, X,
8109 ConstantFP::getZero(X->getType()));
8110 case FCmpInst::FCMP_UNE:
8111 // X is unordered or not equal to an impossible constant --> true
8112 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
8113 default:
8114 break;
8115 }
8116 }
8117
8118 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
8119 // Avoid lossy conversions and denormals.
8120 // Zero is a special case that's OK to convert.
8121 APFloat Fabs = TruncC;
8122 Fabs.clearSign();
8123 if (!Lossy &&
8124 (Fabs.isZero() || !(Fabs < APFloat::getSmallestNormalized(FPSem)))) {
8125 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
8126 return new FCmpInst(Pred, X, NewC, "", &I);
8127 }
8128 }
8129 }
8130
8131 // Convert a sign-bit test of an FP value into a cast and integer compare.
8132 // TODO: Simplify if the copysign constant is 0.0 or NaN.
8133 // TODO: Handle non-zero compare constants.
8134 // TODO: Handle other predicates.
8135 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::copysign>(m_APFloat(C),
8136 m_Value(X)))) &&
8137 match(Op1, m_AnyZeroFP()) && !C->isZero() && !C->isNaN()) {
8138 Type *IntType = Builder.getIntNTy(X->getType()->getScalarSizeInBits());
8139 if (auto *VecTy = dyn_cast<VectorType>(OpType))
8140 IntType = VectorType::get(IntType, VecTy->getElementCount());
8141
8142 // copysign(non-zero constant, X) < 0.0 --> (bitcast X) < 0
8143 if (Pred == FCmpInst::FCMP_OLT) {
8144 Value *IntX = Builder.CreateBitCast(X, IntType);
8145 return new ICmpInst(ICmpInst::ICMP_SLT, IntX,
8146 ConstantInt::getNullValue(IntType));
8147 }
8148 }
8149
8150 {
8151 Value *CanonLHS = nullptr, *CanonRHS = nullptr;
8152 match(Op0, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonLHS)));
8153 match(Op1, m_Intrinsic<Intrinsic::canonicalize>(m_Value(CanonRHS)));
8154
8155 // (canonicalize(x) == x) => (x == x)
8156 if (CanonLHS == Op1)
8157 return new FCmpInst(Pred, Op1, Op1, "", &I);
8158
8159 // (x == canonicalize(x)) => (x == x)
8160 if (CanonRHS == Op0)
8161 return new FCmpInst(Pred, Op0, Op0, "", &I);
8162
8163 // (canonicalize(x) == canonicalize(y)) => (x == y)
8164 if (CanonLHS && CanonRHS)
8165 return new FCmpInst(Pred, CanonLHS, CanonRHS, "", &I);
8166 }
8167
8168 if (I.getType()->isVectorTy())
8169 if (Instruction *Res = foldVectorCmp(I, Builder))
8170 return Res;
8171
8172 return Changed ? &I : nullptr;
8173}
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 * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:2328
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
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
Definition: Constants.cpp:107
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:1212
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:1307
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:518
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:667
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:613
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:966
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:810
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:758
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:869
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:974
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:554
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:586
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:887
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:593
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:848
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:633
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:471
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:677
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:767
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:606
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:692
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:2048
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