LLVM 19.0.0git
InstructionSimplify.cpp
Go to the documentation of this file.
1//===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
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 routines for folding instructions into simpler forms
10// that do not require creating new instructions. This does constant folding
11// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
12// returning a constant ("and i32 %x, 0" -> "0") or an already existing value
13// ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been
14// simplified: This is usually true and assuming it simplifies the logic (if
15// they have not been simplified then results are correct but maybe suboptimal).
16//
17//===----------------------------------------------------------------------===//
18
20
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/Statistic.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/Dominators.h"
38#include "llvm/IR/InstrTypes.h"
40#include "llvm/IR/Operator.h"
42#include "llvm/IR/Statepoint.h"
44#include <algorithm>
45#include <optional>
46using namespace llvm;
47using namespace llvm::PatternMatch;
48
49#define DEBUG_TYPE "instsimplify"
50
51enum { RecursionLimit = 3 };
52
53STATISTIC(NumExpand, "Number of expansions");
54STATISTIC(NumReassoc, "Number of reassociations");
55
56static Value *simplifyAndInst(Value *, Value *, const SimplifyQuery &,
57 unsigned);
58static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned);
59static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &,
60 const SimplifyQuery &, unsigned);
61static Value *simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &,
62 unsigned);
63static Value *simplifyBinOp(unsigned, Value *, Value *, const FastMathFlags &,
64 const SimplifyQuery &, unsigned);
65static Value *simplifyCmpInst(unsigned, Value *, Value *, const SimplifyQuery &,
66 unsigned);
67static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
68 const SimplifyQuery &Q, unsigned MaxRecurse);
69static Value *simplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned);
70static Value *simplifyXorInst(Value *, Value *, const SimplifyQuery &,
71 unsigned);
72static Value *simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &,
73 unsigned);
75 const SimplifyQuery &, unsigned);
77 const SimplifyQuery &, unsigned);
79 ArrayRef<Value *> NewOps,
80 const SimplifyQuery &SQ,
81 unsigned MaxRecurse);
82
84 Value *FalseVal) {
86 if (auto *BO = dyn_cast<BinaryOperator>(Cond))
87 BinOpCode = BO->getOpcode();
88 else
89 return nullptr;
90
91 CmpInst::Predicate ExpectedPred, Pred1, Pred2;
92 if (BinOpCode == BinaryOperator::Or) {
93 ExpectedPred = ICmpInst::ICMP_NE;
94 } else if (BinOpCode == BinaryOperator::And) {
95 ExpectedPred = ICmpInst::ICMP_EQ;
96 } else
97 return nullptr;
98
99 // %A = icmp eq %TV, %FV
100 // %B = icmp eq %X, %Y (and one of these is a select operand)
101 // %C = and %A, %B
102 // %D = select %C, %TV, %FV
103 // -->
104 // %FV
105
106 // %A = icmp ne %TV, %FV
107 // %B = icmp ne %X, %Y (and one of these is a select operand)
108 // %C = or %A, %B
109 // %D = select %C, %TV, %FV
110 // -->
111 // %TV
112 Value *X, *Y;
113 if (!match(Cond, m_c_BinOp(m_c_ICmp(Pred1, m_Specific(TrueVal),
114 m_Specific(FalseVal)),
115 m_ICmp(Pred2, m_Value(X), m_Value(Y)))) ||
116 Pred1 != Pred2 || Pred1 != ExpectedPred)
117 return nullptr;
118
119 if (X == TrueVal || X == FalseVal || Y == TrueVal || Y == FalseVal)
120 return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal;
121
122 return nullptr;
123}
124
125/// For a boolean type or a vector of boolean type, return false or a vector
126/// with every element false.
127static Constant *getFalse(Type *Ty) { return ConstantInt::getFalse(Ty); }
128
129/// For a boolean type or a vector of boolean type, return true or a vector
130/// with every element true.
131static Constant *getTrue(Type *Ty) { return ConstantInt::getTrue(Ty); }
132
133/// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
134static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
135 Value *RHS) {
136 CmpInst *Cmp = dyn_cast<CmpInst>(V);
137 if (!Cmp)
138 return false;
139 CmpInst::Predicate CPred = Cmp->getPredicate();
140 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
141 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
142 return true;
143 return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
144 CRHS == LHS;
145}
146
147/// Simplify comparison with true or false branch of select:
148/// %sel = select i1 %cond, i32 %tv, i32 %fv
149/// %cmp = icmp sle i32 %sel, %rhs
150/// Compose new comparison by substituting %sel with either %tv or %fv
151/// and see if it simplifies.
153 Value *RHS, Value *Cond,
154 const SimplifyQuery &Q, unsigned MaxRecurse,
155 Constant *TrueOrFalse) {
156 Value *SimplifiedCmp = simplifyCmpInst(Pred, LHS, RHS, Q, MaxRecurse);
157 if (SimplifiedCmp == Cond) {
158 // %cmp simplified to the select condition (%cond).
159 return TrueOrFalse;
160 } else if (!SimplifiedCmp && isSameCompare(Cond, Pred, LHS, RHS)) {
161 // It didn't simplify. However, if composed comparison is equivalent
162 // to the select condition (%cond) then we can replace it.
163 return TrueOrFalse;
164 }
165 return SimplifiedCmp;
166}
167
168/// Simplify comparison with true branch of select
170 Value *RHS, Value *Cond,
171 const SimplifyQuery &Q,
172 unsigned MaxRecurse) {
173 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
174 getTrue(Cond->getType()));
175}
176
177/// Simplify comparison with false branch of select
179 Value *RHS, Value *Cond,
180 const SimplifyQuery &Q,
181 unsigned MaxRecurse) {
182 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
183 getFalse(Cond->getType()));
184}
185
186/// We know comparison with both branches of select can be simplified, but they
187/// are not equal. This routine handles some logical simplifications.
189 Value *Cond,
190 const SimplifyQuery &Q,
191 unsigned MaxRecurse) {
192 // If the false value simplified to false, then the result of the compare
193 // is equal to "Cond && TCmp". This also catches the case when the false
194 // value simplified to false and the true value to true, returning "Cond".
195 // Folding select to and/or isn't poison-safe in general; impliesPoison
196 // checks whether folding it does not convert a well-defined value into
197 // poison.
198 if (match(FCmp, m_Zero()) && impliesPoison(TCmp, Cond))
199 if (Value *V = simplifyAndInst(Cond, TCmp, Q, MaxRecurse))
200 return V;
201 // If the true value simplified to true, then the result of the compare
202 // is equal to "Cond || FCmp".
203 if (match(TCmp, m_One()) && impliesPoison(FCmp, Cond))
204 if (Value *V = simplifyOrInst(Cond, FCmp, Q, MaxRecurse))
205 return V;
206 // Finally, if the false value simplified to true and the true value to
207 // false, then the result of the compare is equal to "!Cond".
208 if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
209 if (Value *V = simplifyXorInst(
210 Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse))
211 return V;
212 return nullptr;
213}
214
215/// Does the given value dominate the specified phi node?
216static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
217 Instruction *I = dyn_cast<Instruction>(V);
218 if (!I)
219 // Arguments and constants dominate all instructions.
220 return true;
221
222 // If we have a DominatorTree then do a precise test.
223 if (DT)
224 return DT->dominates(I, P);
225
226 // Otherwise, if the instruction is in the entry block and is not an invoke,
227 // then it obviously dominates all phi nodes.
228 if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
229 !isa<CallBrInst>(I))
230 return true;
231
232 return false;
233}
234
235/// Try to simplify a binary operator of form "V op OtherOp" where V is
236/// "(B0 opex B1)" by distributing 'op' across 'opex' as
237/// "(B0 op OtherOp) opex (B1 op OtherOp)".
239 Value *OtherOp, Instruction::BinaryOps OpcodeToExpand,
240 const SimplifyQuery &Q, unsigned MaxRecurse) {
241 auto *B = dyn_cast<BinaryOperator>(V);
242 if (!B || B->getOpcode() != OpcodeToExpand)
243 return nullptr;
244 Value *B0 = B->getOperand(0), *B1 = B->getOperand(1);
245 Value *L =
246 simplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), MaxRecurse);
247 if (!L)
248 return nullptr;
249 Value *R =
250 simplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), MaxRecurse);
251 if (!R)
252 return nullptr;
253
254 // Does the expanded pair of binops simplify to the existing binop?
255 if ((L == B0 && R == B1) ||
256 (Instruction::isCommutative(OpcodeToExpand) && L == B1 && R == B0)) {
257 ++NumExpand;
258 return B;
259 }
260
261 // Otherwise, return "L op' R" if it simplifies.
262 Value *S = simplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse);
263 if (!S)
264 return nullptr;
265
266 ++NumExpand;
267 return S;
268}
269
270/// Try to simplify binops of form "A op (B op' C)" or the commuted variant by
271/// distributing op over op'.
273 Value *R,
274 Instruction::BinaryOps OpcodeToExpand,
275 const SimplifyQuery &Q,
276 unsigned MaxRecurse) {
277 // Recursion is always used, so bail out at once if we already hit the limit.
278 if (!MaxRecurse--)
279 return nullptr;
280
281 if (Value *V = expandBinOp(Opcode, L, R, OpcodeToExpand, Q, MaxRecurse))
282 return V;
283 if (Value *V = expandBinOp(Opcode, R, L, OpcodeToExpand, Q, MaxRecurse))
284 return V;
285 return nullptr;
286}
287
288/// Generic simplifications for associative binary operations.
289/// Returns the simpler value, or null if none was found.
291 Value *LHS, Value *RHS,
292 const SimplifyQuery &Q,
293 unsigned MaxRecurse) {
294 assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
295
296 // Recursion is always used, so bail out at once if we already hit the limit.
297 if (!MaxRecurse--)
298 return nullptr;
299
300 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
301 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
302
303 // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
304 if (Op0 && Op0->getOpcode() == Opcode) {
305 Value *A = Op0->getOperand(0);
306 Value *B = Op0->getOperand(1);
307 Value *C = RHS;
308
309 // Does "B op C" simplify?
310 if (Value *V = simplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
311 // It does! Return "A op V" if it simplifies or is already available.
312 // If V equals B then "A op V" is just the LHS.
313 if (V == B)
314 return LHS;
315 // Otherwise return "A op V" if it simplifies.
316 if (Value *W = simplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
317 ++NumReassoc;
318 return W;
319 }
320 }
321 }
322
323 // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
324 if (Op1 && Op1->getOpcode() == Opcode) {
325 Value *A = LHS;
326 Value *B = Op1->getOperand(0);
327 Value *C = Op1->getOperand(1);
328
329 // Does "A op B" simplify?
330 if (Value *V = simplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
331 // It does! Return "V op C" if it simplifies or is already available.
332 // If V equals B then "V op C" is just the RHS.
333 if (V == B)
334 return RHS;
335 // Otherwise return "V op C" if it simplifies.
336 if (Value *W = simplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
337 ++NumReassoc;
338 return W;
339 }
340 }
341 }
342
343 // The remaining transforms require commutativity as well as associativity.
344 if (!Instruction::isCommutative(Opcode))
345 return nullptr;
346
347 // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
348 if (Op0 && Op0->getOpcode() == Opcode) {
349 Value *A = Op0->getOperand(0);
350 Value *B = Op0->getOperand(1);
351 Value *C = RHS;
352
353 // Does "C op A" simplify?
354 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
355 // It does! Return "V op B" if it simplifies or is already available.
356 // If V equals A then "V op B" is just the LHS.
357 if (V == A)
358 return LHS;
359 // Otherwise return "V op B" if it simplifies.
360 if (Value *W = simplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
361 ++NumReassoc;
362 return W;
363 }
364 }
365 }
366
367 // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
368 if (Op1 && Op1->getOpcode() == Opcode) {
369 Value *A = LHS;
370 Value *B = Op1->getOperand(0);
371 Value *C = Op1->getOperand(1);
372
373 // Does "C op A" simplify?
374 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
375 // It does! Return "B op V" if it simplifies or is already available.
376 // If V equals C then "B op V" is just the RHS.
377 if (V == C)
378 return RHS;
379 // Otherwise return "B op V" if it simplifies.
380 if (Value *W = simplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
381 ++NumReassoc;
382 return W;
383 }
384 }
385 }
386
387 return nullptr;
388}
389
390/// In the case of a binary operation with a select instruction as an operand,
391/// try to simplify the binop by seeing whether evaluating it on both branches
392/// of the select results in the same value. Returns the common value if so,
393/// otherwise returns null.
395 Value *RHS, const SimplifyQuery &Q,
396 unsigned MaxRecurse) {
397 // Recursion is always used, so bail out at once if we already hit the limit.
398 if (!MaxRecurse--)
399 return nullptr;
400
401 SelectInst *SI;
402 if (isa<SelectInst>(LHS)) {
403 SI = cast<SelectInst>(LHS);
404 } else {
405 assert(isa<SelectInst>(RHS) && "No select instruction operand!");
406 SI = cast<SelectInst>(RHS);
407 }
408
409 // Evaluate the BinOp on the true and false branches of the select.
410 Value *TV;
411 Value *FV;
412 if (SI == LHS) {
413 TV = simplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
414 FV = simplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
415 } else {
416 TV = simplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
417 FV = simplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
418 }
419
420 // If they simplified to the same value, then return the common value.
421 // If they both failed to simplify then return null.
422 if (TV == FV)
423 return TV;
424
425 // If one branch simplified to undef, return the other one.
426 if (TV && Q.isUndefValue(TV))
427 return FV;
428 if (FV && Q.isUndefValue(FV))
429 return TV;
430
431 // If applying the operation did not change the true and false select values,
432 // then the result of the binop is the select itself.
433 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
434 return SI;
435
436 // If one branch simplified and the other did not, and the simplified
437 // value is equal to the unsimplified one, return the simplified value.
438 // For example, select (cond, X, X & Z) & Z -> X & Z.
439 if ((FV && !TV) || (TV && !FV)) {
440 // Check that the simplified value has the form "X op Y" where "op" is the
441 // same as the original operation.
442 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
443 if (Simplified && Simplified->getOpcode() == unsigned(Opcode) &&
444 !Simplified->hasPoisonGeneratingFlags()) {
445 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
446 // We already know that "op" is the same as for the simplified value. See
447 // if the operands match too. If so, return the simplified value.
448 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
449 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
450 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
451 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
452 Simplified->getOperand(1) == UnsimplifiedRHS)
453 return Simplified;
454 if (Simplified->isCommutative() &&
455 Simplified->getOperand(1) == UnsimplifiedLHS &&
456 Simplified->getOperand(0) == UnsimplifiedRHS)
457 return Simplified;
458 }
459 }
460
461 return nullptr;
462}
463
464/// In the case of a comparison with a select instruction, try to simplify the
465/// comparison by seeing whether both branches of the select result in the same
466/// value. Returns the common value if so, otherwise returns null.
467/// For example, if we have:
468/// %tmp = select i1 %cmp, i32 1, i32 2
469/// %cmp1 = icmp sle i32 %tmp, 3
470/// We can simplify %cmp1 to true, because both branches of select are
471/// less than 3. We compose new comparison by substituting %tmp with both
472/// branches of select and see if it can be simplified.
474 Value *RHS, const SimplifyQuery &Q,
475 unsigned MaxRecurse) {
476 // Recursion is always used, so bail out at once if we already hit the limit.
477 if (!MaxRecurse--)
478 return nullptr;
479
480 // Make sure the select is on the LHS.
481 if (!isa<SelectInst>(LHS)) {
482 std::swap(LHS, RHS);
483 Pred = CmpInst::getSwappedPredicate(Pred);
484 }
485 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
486 SelectInst *SI = cast<SelectInst>(LHS);
487 Value *Cond = SI->getCondition();
488 Value *TV = SI->getTrueValue();
489 Value *FV = SI->getFalseValue();
490
491 // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
492 // Does "cmp TV, RHS" simplify?
493 Value *TCmp = simplifyCmpSelTrueCase(Pred, TV, RHS, Cond, Q, MaxRecurse);
494 if (!TCmp)
495 return nullptr;
496
497 // Does "cmp FV, RHS" simplify?
498 Value *FCmp = simplifyCmpSelFalseCase(Pred, FV, RHS, Cond, Q, MaxRecurse);
499 if (!FCmp)
500 return nullptr;
501
502 // If both sides simplified to the same value, then use it as the result of
503 // the original comparison.
504 if (TCmp == FCmp)
505 return TCmp;
506
507 // The remaining cases only make sense if the select condition has the same
508 // type as the result of the comparison, so bail out if this is not so.
509 if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy())
510 return handleOtherCmpSelSimplifications(TCmp, FCmp, Cond, Q, MaxRecurse);
511
512 return nullptr;
513}
514
515/// In the case of a binary operation with an operand that is a PHI instruction,
516/// try to simplify the binop by seeing whether evaluating it on the incoming
517/// phi values yields the same result for every value. If so returns the common
518/// value, otherwise returns null.
520 Value *RHS, const SimplifyQuery &Q,
521 unsigned MaxRecurse) {
522 // Recursion is always used, so bail out at once if we already hit the limit.
523 if (!MaxRecurse--)
524 return nullptr;
525
526 PHINode *PI;
527 if (isa<PHINode>(LHS)) {
528 PI = cast<PHINode>(LHS);
529 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
530 if (!valueDominatesPHI(RHS, PI, Q.DT))
531 return nullptr;
532 } else {
533 assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
534 PI = cast<PHINode>(RHS);
535 // Bail out if LHS and the phi may be mutually interdependent due to a loop.
536 if (!valueDominatesPHI(LHS, PI, Q.DT))
537 return nullptr;
538 }
539
540 // Evaluate the BinOp on the incoming phi values.
541 Value *CommonValue = nullptr;
542 for (Use &Incoming : PI->incoming_values()) {
543 // If the incoming value is the phi node itself, it can safely be skipped.
544 if (Incoming == PI)
545 continue;
547 Value *V = PI == LHS
548 ? simplifyBinOp(Opcode, Incoming, RHS,
549 Q.getWithInstruction(InTI), MaxRecurse)
550 : simplifyBinOp(Opcode, LHS, Incoming,
551 Q.getWithInstruction(InTI), MaxRecurse);
552 // If the operation failed to simplify, or simplified to a different value
553 // to previously, then give up.
554 if (!V || (CommonValue && V != CommonValue))
555 return nullptr;
556 CommonValue = V;
557 }
558
559 return CommonValue;
560}
561
562/// In the case of a comparison with a PHI instruction, try to simplify the
563/// comparison by seeing whether comparing with all of the incoming phi values
564/// yields the same result every time. If so returns the common result,
565/// otherwise returns null.
567 const SimplifyQuery &Q, unsigned MaxRecurse) {
568 // Recursion is always used, so bail out at once if we already hit the limit.
569 if (!MaxRecurse--)
570 return nullptr;
571
572 // Make sure the phi is on the LHS.
573 if (!isa<PHINode>(LHS)) {
574 std::swap(LHS, RHS);
575 Pred = CmpInst::getSwappedPredicate(Pred);
576 }
577 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
578 PHINode *PI = cast<PHINode>(LHS);
579
580 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
581 if (!valueDominatesPHI(RHS, PI, Q.DT))
582 return nullptr;
583
584 // Evaluate the BinOp on the incoming phi values.
585 Value *CommonValue = nullptr;
586 for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
589 // If the incoming value is the phi node itself, it can safely be skipped.
590 if (Incoming == PI)
591 continue;
592 // Change the context instruction to the "edge" that flows into the phi.
593 // This is important because that is where incoming is actually "evaluated"
594 // even though it is used later somewhere else.
596 MaxRecurse);
597 // If the operation failed to simplify, or simplified to a different value
598 // to previously, then give up.
599 if (!V || (CommonValue && V != CommonValue))
600 return nullptr;
601 CommonValue = V;
602 }
603
604 return CommonValue;
605}
606
608 Value *&Op0, Value *&Op1,
609 const SimplifyQuery &Q) {
610 if (auto *CLHS = dyn_cast<Constant>(Op0)) {
611 if (auto *CRHS = dyn_cast<Constant>(Op1)) {
612 switch (Opcode) {
613 default:
614 break;
615 case Instruction::FAdd:
616 case Instruction::FSub:
617 case Instruction::FMul:
618 case Instruction::FDiv:
619 case Instruction::FRem:
620 if (Q.CxtI != nullptr)
621 return ConstantFoldFPInstOperands(Opcode, CLHS, CRHS, Q.DL, Q.CxtI);
622 }
623 return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
624 }
625
626 // Canonicalize the constant to the RHS if this is a commutative operation.
627 if (Instruction::isCommutative(Opcode))
628 std::swap(Op0, Op1);
629 }
630 return nullptr;
631}
632
633/// Given operands for an Add, see if we can fold the result.
634/// If not, this returns null.
635static Value *simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
636 const SimplifyQuery &Q, unsigned MaxRecurse) {
637 if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q))
638 return C;
639
640 // X + poison -> poison
641 if (isa<PoisonValue>(Op1))
642 return Op1;
643
644 // X + undef -> undef
645 if (Q.isUndefValue(Op1))
646 return Op1;
647
648 // X + 0 -> X
649 if (match(Op1, m_Zero()))
650 return Op0;
651
652 // If two operands are negative, return 0.
653 if (isKnownNegation(Op0, Op1))
654 return Constant::getNullValue(Op0->getType());
655
656 // X + (Y - X) -> Y
657 // (Y - X) + X -> Y
658 // Eg: X + -X -> 0
659 Value *Y = nullptr;
660 if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
661 match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
662 return Y;
663
664 // X + ~X -> -1 since ~X = -X-1
665 Type *Ty = Op0->getType();
666 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
667 return Constant::getAllOnesValue(Ty);
668
669 // add nsw/nuw (xor Y, signmask), signmask --> Y
670 // The no-wrapping add guarantees that the top bit will be set by the add.
671 // Therefore, the xor must be clearing the already set sign bit of Y.
672 if ((IsNSW || IsNUW) && match(Op1, m_SignMask()) &&
673 match(Op0, m_Xor(m_Value(Y), m_SignMask())))
674 return Y;
675
676 // add nuw %x, -1 -> -1, because %x can only be 0.
677 if (IsNUW && match(Op1, m_AllOnes()))
678 return Op1; // Which is -1.
679
680 /// i1 add -> xor.
681 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
682 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
683 return V;
684
685 // Try some generic simplifications for associative operations.
686 if (Value *V =
687 simplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, MaxRecurse))
688 return V;
689
690 // Threading Add over selects and phi nodes is pointless, so don't bother.
691 // Threading over the select in "A + select(cond, B, C)" means evaluating
692 // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
693 // only if B and C are equal. If B and C are equal then (since we assume
694 // that operands have already been simplified) "select(cond, B, C)" should
695 // have been simplified to the common value of B and C already. Analysing
696 // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
697 // for threading over phi nodes.
698
699 return nullptr;
700}
701
702Value *llvm::simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
703 const SimplifyQuery &Query) {
704 return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit);
705}
706
707/// Compute the base pointer and cumulative constant offsets for V.
708///
709/// This strips all constant offsets off of V, leaving it the base pointer, and
710/// accumulates the total constant offset applied in the returned constant.
711/// It returns zero if there are no constant offsets applied.
712///
713/// This is very similar to stripAndAccumulateConstantOffsets(), except it
714/// normalizes the offset bitwidth to the stripped pointer type, not the
715/// original pointer type.
717 bool AllowNonInbounds = false) {
718 assert(V->getType()->isPtrOrPtrVectorTy());
719
720 APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
721 V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
722 // As that strip may trace through `addrspacecast`, need to sext or trunc
723 // the offset calculated.
724 return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType()));
725}
726
727/// Compute the constant difference between two pointer values.
728/// If the difference is not a constant, returns zero.
730 Value *RHS) {
733
734 // If LHS and RHS are not related via constant offsets to the same base
735 // value, there is nothing we can do here.
736 if (LHS != RHS)
737 return nullptr;
738
739 // Otherwise, the difference of LHS - RHS can be computed as:
740 // LHS - RHS
741 // = (LHSOffset + Base) - (RHSOffset + Base)
742 // = LHSOffset - RHSOffset
743 Constant *Res = ConstantInt::get(LHS->getContext(), LHSOffset - RHSOffset);
744 if (auto *VecTy = dyn_cast<VectorType>(LHS->getType()))
745 Res = ConstantVector::getSplat(VecTy->getElementCount(), Res);
746 return Res;
747}
748
749/// Test if there is a dominating equivalence condition for the
750/// two operands. If there is, try to reduce the binary operation
751/// between the two operands.
752/// Example: Op0 - Op1 --> 0 when Op0 == Op1
753static Value *simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1,
754 const SimplifyQuery &Q, unsigned MaxRecurse) {
755 // Recursive run it can not get any benefit
756 if (MaxRecurse != RecursionLimit)
757 return nullptr;
758
759 std::optional<bool> Imp =
761 if (Imp && *Imp) {
762 Type *Ty = Op0->getType();
763 switch (Opcode) {
764 case Instruction::Sub:
765 case Instruction::Xor:
766 case Instruction::URem:
767 case Instruction::SRem:
768 return Constant::getNullValue(Ty);
769
770 case Instruction::SDiv:
771 case Instruction::UDiv:
772 return ConstantInt::get(Ty, 1);
773
774 case Instruction::And:
775 case Instruction::Or:
776 // Could be either one - choose Op1 since that's more likely a constant.
777 return Op1;
778 default:
779 break;
780 }
781 }
782 return nullptr;
783}
784
785/// Given operands for a Sub, see if we can fold the result.
786/// If not, this returns null.
787static Value *simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
788 const SimplifyQuery &Q, unsigned MaxRecurse) {
789 if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
790 return C;
791
792 // X - poison -> poison
793 // poison - X -> poison
794 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
795 return PoisonValue::get(Op0->getType());
796
797 // X - undef -> undef
798 // undef - X -> undef
799 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
800 return UndefValue::get(Op0->getType());
801
802 // X - 0 -> X
803 if (match(Op1, m_Zero()))
804 return Op0;
805
806 // X - X -> 0
807 if (Op0 == Op1)
808 return Constant::getNullValue(Op0->getType());
809
810 // Is this a negation?
811 if (match(Op0, m_Zero())) {
812 // 0 - X -> 0 if the sub is NUW.
813 if (IsNUW)
814 return Constant::getNullValue(Op0->getType());
815
816 KnownBits Known = computeKnownBits(Op1, /* Depth */ 0, Q);
817 if (Known.Zero.isMaxSignedValue()) {
818 // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
819 // Op1 must be 0 because negating the minimum signed value is undefined.
820 if (IsNSW)
821 return Constant::getNullValue(Op0->getType());
822
823 // 0 - X -> X if X is 0 or the minimum signed value.
824 return Op1;
825 }
826 }
827
828 // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
829 // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
830 Value *X = nullptr, *Y = nullptr, *Z = Op1;
831 if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
832 // See if "V === Y - Z" simplifies.
833 if (Value *V = simplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1))
834 // It does! Now see if "X + V" simplifies.
835 if (Value *W = simplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) {
836 // It does, we successfully reassociated!
837 ++NumReassoc;
838 return W;
839 }
840 // See if "V === X - Z" simplifies.
841 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
842 // It does! Now see if "Y + V" simplifies.
843 if (Value *W = simplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) {
844 // It does, we successfully reassociated!
845 ++NumReassoc;
846 return W;
847 }
848 }
849
850 // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
851 // For example, X - (X + 1) -> -1
852 X = Op0;
853 if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
854 // See if "V === X - Y" simplifies.
855 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
856 // It does! Now see if "V - Z" simplifies.
857 if (Value *W = simplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) {
858 // It does, we successfully reassociated!
859 ++NumReassoc;
860 return W;
861 }
862 // See if "V === X - Z" simplifies.
863 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
864 // It does! Now see if "V - Y" simplifies.
865 if (Value *W = simplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) {
866 // It does, we successfully reassociated!
867 ++NumReassoc;
868 return W;
869 }
870 }
871
872 // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
873 // For example, X - (X - Y) -> Y.
874 Z = Op0;
875 if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
876 // See if "V === Z - X" simplifies.
877 if (Value *V = simplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1))
878 // It does! Now see if "V + Y" simplifies.
879 if (Value *W = simplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) {
880 // It does, we successfully reassociated!
881 ++NumReassoc;
882 return W;
883 }
884
885 // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
886 if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
887 match(Op1, m_Trunc(m_Value(Y))))
888 if (X->getType() == Y->getType())
889 // See if "V === X - Y" simplifies.
890 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
891 // It does! Now see if "trunc V" simplifies.
892 if (Value *W = simplifyCastInst(Instruction::Trunc, V, Op0->getType(),
893 Q, MaxRecurse - 1))
894 // It does, return the simplified "trunc V".
895 return W;
896
897 // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
898 if (match(Op0, m_PtrToInt(m_Value(X))) && match(Op1, m_PtrToInt(m_Value(Y))))
899 if (Constant *Result = computePointerDifference(Q.DL, X, Y))
900 return ConstantFoldIntegerCast(Result, Op0->getType(), /*IsSigned*/ true,
901 Q.DL);
902
903 // i1 sub -> xor.
904 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
905 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
906 return V;
907
908 // Threading Sub over selects and phi nodes is pointless, so don't bother.
909 // Threading over the select in "A - select(cond, B, C)" means evaluating
910 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
911 // only if B and C are equal. If B and C are equal then (since we assume
912 // that operands have already been simplified) "select(cond, B, C)" should
913 // have been simplified to the common value of B and C already. Analysing
914 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
915 // for threading over phi nodes.
916
917 if (Value *V = simplifyByDomEq(Instruction::Sub, Op0, Op1, Q, MaxRecurse))
918 return V;
919
920 return nullptr;
921}
922
923Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
924 const SimplifyQuery &Q) {
925 return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
926}
927
928/// Given operands for a Mul, see if we can fold the result.
929/// If not, this returns null.
930static Value *simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
931 const SimplifyQuery &Q, unsigned MaxRecurse) {
932 if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q))
933 return C;
934
935 // X * poison -> poison
936 if (isa<PoisonValue>(Op1))
937 return Op1;
938
939 // X * undef -> 0
940 // X * 0 -> 0
941 if (Q.isUndefValue(Op1) || match(Op1, m_Zero()))
942 return Constant::getNullValue(Op0->getType());
943
944 // X * 1 -> X
945 if (match(Op1, m_One()))
946 return Op0;
947
948 // (X / Y) * Y -> X if the division is exact.
949 Value *X = nullptr;
950 if (Q.IIQ.UseInstrInfo &&
951 (match(Op0,
952 m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
953 match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))) // Y * (X / Y)
954 return X;
955
956 if (Op0->getType()->isIntOrIntVectorTy(1)) {
957 // mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not
958 // representable). All other cases reduce to 0, so just return 0.
959 if (IsNSW)
960 return ConstantInt::getNullValue(Op0->getType());
961
962 // Treat "mul i1" as "and i1".
963 if (MaxRecurse)
964 if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
965 return V;
966 }
967
968 // Try some generic simplifications for associative operations.
969 if (Value *V =
970 simplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
971 return V;
972
973 // Mul distributes over Add. Try some generic simplifications based on this.
974 if (Value *V = expandCommutativeBinOp(Instruction::Mul, Op0, Op1,
975 Instruction::Add, Q, MaxRecurse))
976 return V;
977
978 // If the operation is with the result of a select instruction, check whether
979 // operating on either branch of the select always yields the same value.
980 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
981 if (Value *V =
982 threadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
983 return V;
984
985 // If the operation is with the result of a phi instruction, check whether
986 // operating on all incoming values of the phi always yields the same value.
987 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
988 if (Value *V =
989 threadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
990 return V;
991
992 return nullptr;
993}
994
995Value *llvm::simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
996 const SimplifyQuery &Q) {
997 return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
998}
999
1000/// Given a predicate and two operands, return true if the comparison is true.
1001/// This is a helper for div/rem simplification where we return some other value
1002/// when we can prove a relationship between the operands.
1003static bool isICmpTrue(ICmpInst::Predicate Pred, Value *LHS, Value *RHS,
1004 const SimplifyQuery &Q, unsigned MaxRecurse) {
1005 Value *V = simplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse);
1006 Constant *C = dyn_cast_or_null<Constant>(V);
1007 return (C && C->isAllOnesValue());
1008}
1009
1010/// Return true if we can simplify X / Y to 0. Remainder can adapt that answer
1011/// to simplify X % Y to X.
1012static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q,
1013 unsigned MaxRecurse, bool IsSigned) {
1014 // Recursion is always used, so bail out at once if we already hit the limit.
1015 if (!MaxRecurse--)
1016 return false;
1017
1018 if (IsSigned) {
1019 // (X srem Y) sdiv Y --> 0
1020 if (match(X, m_SRem(m_Value(), m_Specific(Y))))
1021 return true;
1022
1023 // |X| / |Y| --> 0
1024 //
1025 // We require that 1 operand is a simple constant. That could be extended to
1026 // 2 variables if we computed the sign bit for each.
1027 //
1028 // Make sure that a constant is not the minimum signed value because taking
1029 // the abs() of that is undefined.
1030 Type *Ty = X->getType();
1031 const APInt *C;
1032 if (match(X, m_APInt(C)) && !C->isMinSignedValue()) {
1033 // Is the variable divisor magnitude always greater than the constant
1034 // dividend magnitude?
1035 // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
1036 Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
1037 Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
1038 if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) ||
1039 isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse))
1040 return true;
1041 }
1042 if (match(Y, m_APInt(C))) {
1043 // Special-case: we can't take the abs() of a minimum signed value. If
1044 // that's the divisor, then all we have to do is prove that the dividend
1045 // is also not the minimum signed value.
1046 if (C->isMinSignedValue())
1047 return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse);
1048
1049 // Is the variable dividend magnitude always less than the constant
1050 // divisor magnitude?
1051 // |X| < |C| --> X > -abs(C) and X < abs(C)
1052 Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
1053 Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
1054 if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) &&
1055 isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse))
1056 return true;
1057 }
1058 return false;
1059 }
1060
1061 // IsSigned == false.
1062
1063 // Is the unsigned dividend known to be less than a constant divisor?
1064 // TODO: Convert this (and above) to range analysis
1065 // ("computeConstantRangeIncludingKnownBits")?
1066 const APInt *C;
1067 if (match(Y, m_APInt(C)) &&
1068 computeKnownBits(X, /* Depth */ 0, Q).getMaxValue().ult(*C))
1069 return true;
1070
1071 // Try again for any divisor:
1072 // Is the dividend unsigned less than the divisor?
1073 return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
1074}
1075
1076/// Check for common or similar folds of integer division or integer remainder.
1077/// This applies to all 4 opcodes (sdiv/udiv/srem/urem).
1079 Value *Op1, const SimplifyQuery &Q,
1080 unsigned MaxRecurse) {
1081 bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
1082 bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
1083
1084 Type *Ty = Op0->getType();
1085
1086 // X / undef -> poison
1087 // X % undef -> poison
1088 if (Q.isUndefValue(Op1) || isa<PoisonValue>(Op1))
1089 return PoisonValue::get(Ty);
1090
1091 // X / 0 -> poison
1092 // X % 0 -> poison
1093 // We don't need to preserve faults!
1094 if (match(Op1, m_Zero()))
1095 return PoisonValue::get(Ty);
1096
1097 // If any element of a constant divisor fixed width vector is zero or undef
1098 // the behavior is undefined and we can fold the whole op to poison.
1099 auto *Op1C = dyn_cast<Constant>(Op1);
1100 auto *VTy = dyn_cast<FixedVectorType>(Ty);
1101 if (Op1C && VTy) {
1102 unsigned NumElts = VTy->getNumElements();
1103 for (unsigned i = 0; i != NumElts; ++i) {
1104 Constant *Elt = Op1C->getAggregateElement(i);
1105 if (Elt && (Elt->isNullValue() || Q.isUndefValue(Elt)))
1106 return PoisonValue::get(Ty);
1107 }
1108 }
1109
1110 // poison / X -> poison
1111 // poison % X -> poison
1112 if (isa<PoisonValue>(Op0))
1113 return Op0;
1114
1115 // undef / X -> 0
1116 // undef % X -> 0
1117 if (Q.isUndefValue(Op0))
1118 return Constant::getNullValue(Ty);
1119
1120 // 0 / X -> 0
1121 // 0 % X -> 0
1122 if (match(Op0, m_Zero()))
1123 return Constant::getNullValue(Op0->getType());
1124
1125 // X / X -> 1
1126 // X % X -> 0
1127 if (Op0 == Op1)
1128 return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty);
1129
1130 KnownBits Known = computeKnownBits(Op1, /* Depth */ 0, Q);
1131 // X / 0 -> poison
1132 // X % 0 -> poison
1133 // If the divisor is known to be zero, just return poison. This can happen in
1134 // some cases where its provable indirectly the denominator is zero but it's
1135 // not trivially simplifiable (i.e known zero through a phi node).
1136 if (Known.isZero())
1137 return PoisonValue::get(Ty);
1138
1139 // X / 1 -> X
1140 // X % 1 -> 0
1141 // If the divisor can only be zero or one, we can't have division-by-zero
1142 // or remainder-by-zero, so assume the divisor is 1.
1143 // e.g. 1, zext (i8 X), sdiv X (Y and 1)
1144 if (Known.countMinLeadingZeros() == Known.getBitWidth() - 1)
1145 return IsDiv ? Op0 : Constant::getNullValue(Ty);
1146
1147 // If X * Y does not overflow, then:
1148 // X * Y / Y -> X
1149 // X * Y % Y -> 0
1150 Value *X;
1151 if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
1152 auto *Mul = cast<OverflowingBinaryOperator>(Op0);
1153 // The multiplication can't overflow if it is defined not to, or if
1154 // X == A / Y for some A.
1155 if ((IsSigned && Q.IIQ.hasNoSignedWrap(Mul)) ||
1156 (!IsSigned && Q.IIQ.hasNoUnsignedWrap(Mul)) ||
1157 (IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) ||
1158 (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1))))) {
1159 return IsDiv ? X : Constant::getNullValue(Op0->getType());
1160 }
1161 }
1162
1163 if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1164 return IsDiv ? Constant::getNullValue(Op0->getType()) : Op0;
1165
1166 if (Value *V = simplifyByDomEq(Opcode, Op0, Op1, Q, MaxRecurse))
1167 return V;
1168
1169 // If the operation is with the result of a select instruction, check whether
1170 // operating on either branch of the select always yields the same value.
1171 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1172 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1173 return V;
1174
1175 // If the operation is with the result of a phi instruction, check whether
1176 // operating on all incoming values of the phi always yields the same value.
1177 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1178 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1179 return V;
1180
1181 return nullptr;
1182}
1183
1184/// These are simplifications common to SDiv and UDiv.
1186 bool IsExact, const SimplifyQuery &Q,
1187 unsigned MaxRecurse) {
1188 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1189 return C;
1190
1191 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1192 return V;
1193
1194 const APInt *DivC;
1195 if (IsExact && match(Op1, m_APInt(DivC))) {
1196 // If this is an exact divide by a constant, then the dividend (Op0) must
1197 // have at least as many trailing zeros as the divisor to divide evenly. If
1198 // it has less trailing zeros, then the result must be poison.
1199 if (DivC->countr_zero()) {
1200 KnownBits KnownOp0 = computeKnownBits(Op0, /* Depth */ 0, Q);
1201 if (KnownOp0.countMaxTrailingZeros() < DivC->countr_zero())
1202 return PoisonValue::get(Op0->getType());
1203 }
1204
1205 // udiv exact (mul nsw X, C), C --> X
1206 // sdiv exact (mul nuw X, C), C --> X
1207 // where C is not a power of 2.
1208 Value *X;
1209 if (!DivC->isPowerOf2() &&
1210 (Opcode == Instruction::UDiv
1211 ? match(Op0, m_NSWMul(m_Value(X), m_Specific(Op1)))
1212 : match(Op0, m_NUWMul(m_Value(X), m_Specific(Op1)))))
1213 return X;
1214 }
1215
1216 return nullptr;
1217}
1218
1219/// These are simplifications common to SRem and URem.
1221 const SimplifyQuery &Q, unsigned MaxRecurse) {
1222 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1223 return C;
1224
1225 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1226 return V;
1227
1228 // (X << Y) % X -> 0
1229 if (Q.IIQ.UseInstrInfo &&
1230 ((Opcode == Instruction::SRem &&
1231 match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))) ||
1232 (Opcode == Instruction::URem &&
1233 match(Op0, m_NUWShl(m_Specific(Op1), m_Value())))))
1234 return Constant::getNullValue(Op0->getType());
1235
1236 return nullptr;
1237}
1238
1239/// Given operands for an SDiv, see if we can fold the result.
1240/// If not, this returns null.
1241static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1242 const SimplifyQuery &Q, unsigned MaxRecurse) {
1243 // If two operands are negated and no signed overflow, return -1.
1244 if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
1245 return Constant::getAllOnesValue(Op0->getType());
1246
1247 return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1248}
1249
1250Value *llvm::simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1251 const SimplifyQuery &Q) {
1252 return ::simplifySDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1253}
1254
1255/// Given operands for a UDiv, see if we can fold the result.
1256/// If not, this returns null.
1257static Value *simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1258 const SimplifyQuery &Q, unsigned MaxRecurse) {
1259 return simplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1260}
1261
1262Value *llvm::simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1263 const SimplifyQuery &Q) {
1264 return ::simplifyUDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1265}
1266
1267/// Given operands for an SRem, see if we can fold the result.
1268/// If not, this returns null.
1269static Value *simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1270 unsigned MaxRecurse) {
1271 // If the divisor is 0, the result is undefined, so assume the divisor is -1.
1272 // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
1273 Value *X;
1274 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
1275 return ConstantInt::getNullValue(Op0->getType());
1276
1277 // If the two operands are negated, return 0.
1278 if (isKnownNegation(Op0, Op1))
1279 return ConstantInt::getNullValue(Op0->getType());
1280
1281 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1282}
1283
1285 return ::simplifySRemInst(Op0, Op1, Q, RecursionLimit);
1286}
1287
1288/// Given operands for a URem, see if we can fold the result.
1289/// If not, this returns null.
1290static Value *simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1291 unsigned MaxRecurse) {
1292 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1293}
1294
1296 return ::simplifyURemInst(Op0, Op1, Q, RecursionLimit);
1297}
1298
1299/// Returns true if a shift by \c Amount always yields poison.
1300static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) {
1301 Constant *C = dyn_cast<Constant>(Amount);
1302 if (!C)
1303 return false;
1304
1305 // X shift by undef -> poison because it may shift by the bitwidth.
1306 if (Q.isUndefValue(C))
1307 return true;
1308
1309 // Shifting by the bitwidth or more is poison. This covers scalars and
1310 // fixed/scalable vectors with splat constants.
1311 const APInt *AmountC;
1312 if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth()))
1313 return true;
1314
1315 // Try harder for fixed-length vectors:
1316 // If all lanes of a vector shift are poison, the whole shift is poison.
1317 if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) {
1318 for (unsigned I = 0,
1319 E = cast<FixedVectorType>(C->getType())->getNumElements();
1320 I != E; ++I)
1321 if (!isPoisonShift(C->getAggregateElement(I), Q))
1322 return false;
1323 return true;
1324 }
1325
1326 return false;
1327}
1328
1329/// Given operands for an Shl, LShr or AShr, see if we can fold the result.
1330/// If not, this returns null.
1332 Value *Op1, bool IsNSW, const SimplifyQuery &Q,
1333 unsigned MaxRecurse) {
1334 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1335 return C;
1336
1337 // poison shift by X -> poison
1338 if (isa<PoisonValue>(Op0))
1339 return Op0;
1340
1341 // 0 shift by X -> 0
1342 if (match(Op0, m_Zero()))
1343 return Constant::getNullValue(Op0->getType());
1344
1345 // X shift by 0 -> X
1346 // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
1347 // would be poison.
1348 Value *X;
1349 if (match(Op1, m_Zero()) ||
1350 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1351 return Op0;
1352
1353 // Fold undefined shifts.
1354 if (isPoisonShift(Op1, Q))
1355 return PoisonValue::get(Op0->getType());
1356
1357 // If the operation is with the result of a select instruction, check whether
1358 // operating on either branch of the select always yields the same value.
1359 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1360 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1361 return V;
1362
1363 // If the operation is with the result of a phi instruction, check whether
1364 // operating on all incoming values of the phi always yields the same value.
1365 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1366 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1367 return V;
1368
1369 // If any bits in the shift amount make that value greater than or equal to
1370 // the number of bits in the type, the shift is undefined.
1371 KnownBits KnownAmt = computeKnownBits(Op1, /* Depth */ 0, Q);
1372 if (KnownAmt.getMinValue().uge(KnownAmt.getBitWidth()))
1373 return PoisonValue::get(Op0->getType());
1374
1375 // If all valid bits in the shift amount are known zero, the first operand is
1376 // unchanged.
1377 unsigned NumValidShiftBits = Log2_32_Ceil(KnownAmt.getBitWidth());
1378 if (KnownAmt.countMinTrailingZeros() >= NumValidShiftBits)
1379 return Op0;
1380
1381 // Check for nsw shl leading to a poison value.
1382 if (IsNSW) {
1383 assert(Opcode == Instruction::Shl && "Expected shl for nsw instruction");
1384 KnownBits KnownVal = computeKnownBits(Op0, /* Depth */ 0, Q);
1385 KnownBits KnownShl = KnownBits::shl(KnownVal, KnownAmt);
1386
1387 if (KnownVal.Zero.isSignBitSet())
1388 KnownShl.Zero.setSignBit();
1389 if (KnownVal.One.isSignBitSet())
1390 KnownShl.One.setSignBit();
1391
1392 if (KnownShl.hasConflict())
1393 return PoisonValue::get(Op0->getType());
1394 }
1395
1396 return nullptr;
1397}
1398
1399/// Given operands for an LShr or AShr, see if we can fold the result. If not,
1400/// this returns null.
1402 Value *Op1, bool IsExact,
1403 const SimplifyQuery &Q, unsigned MaxRecurse) {
1404 if (Value *V =
1405 simplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse))
1406 return V;
1407
1408 // X >> X -> 0
1409 if (Op0 == Op1)
1410 return Constant::getNullValue(Op0->getType());
1411
1412 // undef >> X -> 0
1413 // undef >> X -> undef (if it's exact)
1414 if (Q.isUndefValue(Op0))
1415 return IsExact ? Op0 : Constant::getNullValue(Op0->getType());
1416
1417 // The low bit cannot be shifted out of an exact shift if it is set.
1418 // TODO: Generalize by counting trailing zeros (see fold for exact division).
1419 if (IsExact) {
1420 KnownBits Op0Known = computeKnownBits(Op0, /* Depth */ 0, Q);
1421 if (Op0Known.One[0])
1422 return Op0;
1423 }
1424
1425 return nullptr;
1426}
1427
1428/// Given operands for an Shl, see if we can fold the result.
1429/// If not, this returns null.
1430static Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1431 const SimplifyQuery &Q, unsigned MaxRecurse) {
1432 if (Value *V =
1433 simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
1434 return V;
1435
1436 Type *Ty = Op0->getType();
1437 // undef << X -> 0
1438 // undef << X -> undef if (if it's NSW/NUW)
1439 if (Q.isUndefValue(Op0))
1440 return IsNSW || IsNUW ? Op0 : Constant::getNullValue(Ty);
1441
1442 // (X >> A) << A -> X
1443 Value *X;
1444 if (Q.IIQ.UseInstrInfo &&
1445 match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
1446 return X;
1447
1448 // shl nuw i8 C, %x -> C iff C has sign bit set.
1449 if (IsNUW && match(Op0, m_Negative()))
1450 return Op0;
1451 // NOTE: could use computeKnownBits() / LazyValueInfo,
1452 // but the cost-benefit analysis suggests it isn't worth it.
1453
1454 // "nuw" guarantees that only zeros are shifted out, and "nsw" guarantees
1455 // that the sign-bit does not change, so the only input that does not
1456 // produce poison is 0, and "0 << (bitwidth-1) --> 0".
1457 if (IsNSW && IsNUW &&
1458 match(Op1, m_SpecificInt(Ty->getScalarSizeInBits() - 1)))
1459 return Constant::getNullValue(Ty);
1460
1461 return nullptr;
1462}
1463
1464Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1465 const SimplifyQuery &Q) {
1466 return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
1467}
1468
1469/// Given operands for an LShr, see if we can fold the result.
1470/// If not, this returns null.
1471static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1472 const SimplifyQuery &Q, unsigned MaxRecurse) {
1473 if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, IsExact, Q,
1474 MaxRecurse))
1475 return V;
1476
1477 // (X << A) >> A -> X
1478 Value *X;
1479 if (Q.IIQ.UseInstrInfo && match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
1480 return X;
1481
1482 // ((X << A) | Y) >> A -> X if effective width of Y is not larger than A.
1483 // We can return X as we do in the above case since OR alters no bits in X.
1484 // SimplifyDemandedBits in InstCombine can do more general optimization for
1485 // bit manipulation. This pattern aims to provide opportunities for other
1486 // optimizers by supporting a simple but common case in InstSimplify.
1487 Value *Y;
1488 const APInt *ShRAmt, *ShLAmt;
1489 if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(ShRAmt)) &&
1490 match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y))) &&
1491 *ShRAmt == *ShLAmt) {
1492 const KnownBits YKnown = computeKnownBits(Y, /* Depth */ 0, Q);
1493 const unsigned EffWidthY = YKnown.countMaxActiveBits();
1494 if (ShRAmt->uge(EffWidthY))
1495 return X;
1496 }
1497
1498 return nullptr;
1499}
1500
1501Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1502 const SimplifyQuery &Q) {
1503 return ::simplifyLShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1504}
1505
1506/// Given operands for an AShr, see if we can fold the result.
1507/// If not, this returns null.
1508static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1509 const SimplifyQuery &Q, unsigned MaxRecurse) {
1510 if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, IsExact, Q,
1511 MaxRecurse))
1512 return V;
1513
1514 // -1 >>a X --> -1
1515 // (-1 << X) a>> X --> -1
1516 // We could return the original -1 constant to preserve poison elements.
1517 if (match(Op0, m_AllOnes()) ||
1518 match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1))))
1519 return Constant::getAllOnesValue(Op0->getType());
1520
1521 // (X << A) >> A -> X
1522 Value *X;
1523 if (Q.IIQ.UseInstrInfo && match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))
1524 return X;
1525
1526 // Arithmetic shifting an all-sign-bit value is a no-op.
1527 unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1528 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1529 return Op0;
1530
1531 return nullptr;
1532}
1533
1534Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1535 const SimplifyQuery &Q) {
1536 return ::simplifyAShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1537}
1538
1539/// Commuted variants are assumed to be handled by calling this function again
1540/// with the parameters swapped.
1542 ICmpInst *UnsignedICmp, bool IsAnd,
1543 const SimplifyQuery &Q) {
1544 Value *X, *Y;
1545
1546 ICmpInst::Predicate EqPred;
1547 if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
1548 !ICmpInst::isEquality(EqPred))
1549 return nullptr;
1550
1551 ICmpInst::Predicate UnsignedPred;
1552
1553 Value *A, *B;
1554 // Y = (A - B);
1555 if (match(Y, m_Sub(m_Value(A), m_Value(B)))) {
1556 if (match(UnsignedICmp,
1557 m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) &&
1558 ICmpInst::isUnsigned(UnsignedPred)) {
1559 // A >=/<= B || (A - B) != 0 <--> true
1560 if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1561 UnsignedPred == ICmpInst::ICMP_ULE) &&
1562 EqPred == ICmpInst::ICMP_NE && !IsAnd)
1563 return ConstantInt::getTrue(UnsignedICmp->getType());
1564 // A </> B && (A - B) == 0 <--> false
1565 if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1566 UnsignedPred == ICmpInst::ICMP_UGT) &&
1567 EqPred == ICmpInst::ICMP_EQ && IsAnd)
1568 return ConstantInt::getFalse(UnsignedICmp->getType());
1569
1570 // A </> B && (A - B) != 0 <--> A </> B
1571 // A </> B || (A - B) != 0 <--> (A - B) != 0
1572 if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1573 UnsignedPred == ICmpInst::ICMP_UGT))
1574 return IsAnd ? UnsignedICmp : ZeroICmp;
1575
1576 // A <=/>= B && (A - B) == 0 <--> (A - B) == 0
1577 // A <=/>= B || (A - B) == 0 <--> A <=/>= B
1578 if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1579 UnsignedPred == ICmpInst::ICMP_UGE))
1580 return IsAnd ? ZeroICmp : UnsignedICmp;
1581 }
1582
1583 // Given Y = (A - B)
1584 // Y >= A && Y != 0 --> Y >= A iff B != 0
1585 // Y < A || Y == 0 --> Y < A iff B != 0
1586 if (match(UnsignedICmp,
1587 m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) {
1588 if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1589 EqPred == ICmpInst::ICMP_NE && isKnownNonZero(B, Q))
1590 return UnsignedICmp;
1591 if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1592 EqPred == ICmpInst::ICMP_EQ && isKnownNonZero(B, Q))
1593 return UnsignedICmp;
1594 }
1595 }
1596
1597 if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
1598 ICmpInst::isUnsigned(UnsignedPred))
1599 ;
1600 else if (match(UnsignedICmp,
1601 m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
1602 ICmpInst::isUnsigned(UnsignedPred))
1603 UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1604 else
1605 return nullptr;
1606
1607 // X > Y && Y == 0 --> Y == 0 iff X != 0
1608 // X > Y || Y == 0 --> X > Y iff X != 0
1609 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1610 isKnownNonZero(X, Q))
1611 return IsAnd ? ZeroICmp : UnsignedICmp;
1612
1613 // X <= Y && Y != 0 --> X <= Y iff X != 0
1614 // X <= Y || Y != 0 --> Y != 0 iff X != 0
1615 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1616 isKnownNonZero(X, Q))
1617 return IsAnd ? UnsignedICmp : ZeroICmp;
1618
1619 // The transforms below here are expected to be handled more generally with
1620 // simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's
1621 // foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap,
1622 // these are candidates for removal.
1623
1624 // X < Y && Y != 0 --> X < Y
1625 // X < Y || Y != 0 --> Y != 0
1626 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1627 return IsAnd ? UnsignedICmp : ZeroICmp;
1628
1629 // X >= Y && Y == 0 --> Y == 0
1630 // X >= Y || Y == 0 --> X >= Y
1631 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1632 return IsAnd ? ZeroICmp : UnsignedICmp;
1633
1634 // X < Y && Y == 0 --> false
1635 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1636 IsAnd)
1637 return getFalse(UnsignedICmp->getType());
1638
1639 // X >= Y || Y != 0 --> true
1640 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1641 !IsAnd)
1642 return getTrue(UnsignedICmp->getType());
1643
1644 return nullptr;
1645}
1646
1647/// Test if a pair of compares with a shared operand and 2 constants has an
1648/// empty set intersection, full set union, or if one compare is a superset of
1649/// the other.
1651 bool IsAnd) {
1652 // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)).
1653 if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
1654 return nullptr;
1655
1656 const APInt *C0, *C1;
1657 if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
1658 !match(Cmp1->getOperand(1), m_APInt(C1)))
1659 return nullptr;
1660
1661 auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
1662 auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
1663
1664 // For and-of-compares, check if the intersection is empty:
1665 // (icmp X, C0) && (icmp X, C1) --> empty set --> false
1666 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1667 return getFalse(Cmp0->getType());
1668
1669 // For or-of-compares, check if the union is full:
1670 // (icmp X, C0) || (icmp X, C1) --> full set --> true
1671 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1672 return getTrue(Cmp0->getType());
1673
1674 // Is one range a superset of the other?
1675 // If this is and-of-compares, take the smaller set:
1676 // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
1677 // If this is or-of-compares, take the larger set:
1678 // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
1679 if (Range0.contains(Range1))
1680 return IsAnd ? Cmp1 : Cmp0;
1681 if (Range1.contains(Range0))
1682 return IsAnd ? Cmp0 : Cmp1;
1683
1684 return nullptr;
1685}
1686
1688 const InstrInfoQuery &IIQ) {
1689 // (icmp (add V, C0), C1) & (icmp V, C0)
1690 ICmpInst::Predicate Pred0, Pred1;
1691 const APInt *C0, *C1;
1692 Value *V;
1693 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1694 return nullptr;
1695
1696 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1697 return nullptr;
1698
1699 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
1700 if (AddInst->getOperand(1) != Op1->getOperand(1))
1701 return nullptr;
1702
1703 Type *ITy = Op0->getType();
1704 bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1705 bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1706
1707 const APInt Delta = *C1 - *C0;
1708 if (C0->isStrictlyPositive()) {
1709 if (Delta == 2) {
1710 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1711 return getFalse(ITy);
1712 if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1713 return getFalse(ITy);
1714 }
1715 if (Delta == 1) {
1716 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1717 return getFalse(ITy);
1718 if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1719 return getFalse(ITy);
1720 }
1721 }
1722 if (C0->getBoolValue() && IsNUW) {
1723 if (Delta == 2)
1724 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1725 return getFalse(ITy);
1726 if (Delta == 1)
1727 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1728 return getFalse(ITy);
1729 }
1730
1731 return nullptr;
1732}
1733
1734/// Try to simplify and/or of icmp with ctpop intrinsic.
1736 bool IsAnd) {
1737 ICmpInst::Predicate Pred0, Pred1;
1738 Value *X;
1739 const APInt *C;
1740 if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
1741 m_APInt(C))) ||
1742 !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())) || C->isZero())
1743 return nullptr;
1744
1745 // (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0
1746 if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1747 return Cmp1;
1748 // (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0
1749 if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1750 return Cmp1;
1751
1752 return nullptr;
1753}
1754
1756 const SimplifyQuery &Q) {
1757 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true, Q))
1758 return X;
1759 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true, Q))
1760 return X;
1761
1762 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true))
1763 return X;
1764
1765 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, true))
1766 return X;
1767 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, true))
1768 return X;
1769
1770 if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1771 return X;
1772 if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1773 return X;
1774
1775 return nullptr;
1776}
1777
1779 const InstrInfoQuery &IIQ) {
1780 // (icmp (add V, C0), C1) | (icmp V, C0)
1781 ICmpInst::Predicate Pred0, Pred1;
1782 const APInt *C0, *C1;
1783 Value *V;
1784 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1785 return nullptr;
1786
1787 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1788 return nullptr;
1789
1790 auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1791 if (AddInst->getOperand(1) != Op1->getOperand(1))
1792 return nullptr;
1793
1794 Type *ITy = Op0->getType();
1795 bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1796 bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1797
1798 const APInt Delta = *C1 - *C0;
1799 if (C0->isStrictlyPositive()) {
1800 if (Delta == 2) {
1801 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1802 return getTrue(ITy);
1803 if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1804 return getTrue(ITy);
1805 }
1806 if (Delta == 1) {
1807 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1808 return getTrue(ITy);
1809 if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1810 return getTrue(ITy);
1811 }
1812 }
1813 if (C0->getBoolValue() && IsNUW) {
1814 if (Delta == 2)
1815 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1816 return getTrue(ITy);
1817 if (Delta == 1)
1818 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1819 return getTrue(ITy);
1820 }
1821
1822 return nullptr;
1823}
1824
1826 const SimplifyQuery &Q) {
1827 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false, Q))
1828 return X;
1829 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false, Q))
1830 return X;
1831
1832 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false))
1833 return X;
1834
1835 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, false))
1836 return X;
1837 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, false))
1838 return X;
1839
1840 if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1841 return X;
1842 if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1843 return X;
1844
1845 return nullptr;
1846}
1847
1849 FCmpInst *RHS, bool IsAnd) {
1850 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1851 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1852 if (LHS0->getType() != RHS0->getType())
1853 return nullptr;
1854
1855 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1856 if ((PredL == FCmpInst::FCMP_ORD || PredL == FCmpInst::FCMP_UNO) &&
1857 ((FCmpInst::isOrdered(PredR) && IsAnd) ||
1858 (FCmpInst::isUnordered(PredR) && !IsAnd))) {
1859 // (fcmp ord X, NNAN) & (fcmp o** X, Y) --> fcmp o** X, Y
1860 // (fcmp uno X, NNAN) & (fcmp o** X, Y) --> false
1861 // (fcmp uno X, NNAN) | (fcmp u** X, Y) --> fcmp u** X, Y
1862 // (fcmp ord X, NNAN) | (fcmp u** X, Y) --> true
1863 if (((LHS1 == RHS0 || LHS1 == RHS1) &&
1864 isKnownNeverNaN(LHS0, /*Depth=*/0, Q)) ||
1865 ((LHS0 == RHS0 || LHS0 == RHS1) &&
1866 isKnownNeverNaN(LHS1, /*Depth=*/0, Q)))
1867 return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1868 ? static_cast<Value *>(RHS)
1869 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1870 }
1871
1872 if ((PredR == FCmpInst::FCMP_ORD || PredR == FCmpInst::FCMP_UNO) &&
1873 ((FCmpInst::isOrdered(PredL) && IsAnd) ||
1874 (FCmpInst::isUnordered(PredL) && !IsAnd))) {
1875 // (fcmp o** X, Y) & (fcmp ord X, NNAN) --> fcmp o** X, Y
1876 // (fcmp o** X, Y) & (fcmp uno X, NNAN) --> false
1877 // (fcmp u** X, Y) | (fcmp uno X, NNAN) --> fcmp u** X, Y
1878 // (fcmp u** X, Y) | (fcmp ord X, NNAN) --> true
1879 if (((RHS1 == LHS0 || RHS1 == LHS1) &&
1880 isKnownNeverNaN(RHS0, /*Depth=*/0, Q)) ||
1881 ((RHS0 == LHS0 || RHS0 == LHS1) &&
1882 isKnownNeverNaN(RHS1, /*Depth=*/0, Q)))
1883 return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1884 ? static_cast<Value *>(LHS)
1885 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1886 }
1887
1888 return nullptr;
1889}
1890
1892 Value *Op1, bool IsAnd) {
1893 // Look through casts of the 'and' operands to find compares.
1894 auto *Cast0 = dyn_cast<CastInst>(Op0);
1895 auto *Cast1 = dyn_cast<CastInst>(Op1);
1896 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1897 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1898 Op0 = Cast0->getOperand(0);
1899 Op1 = Cast1->getOperand(0);
1900 }
1901
1902 Value *V = nullptr;
1903 auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
1904 auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
1905 if (ICmp0 && ICmp1)
1906 V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1, Q)
1907 : simplifyOrOfICmps(ICmp0, ICmp1, Q);
1908
1909 auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
1910 auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
1911 if (FCmp0 && FCmp1)
1912 V = simplifyAndOrOfFCmps(Q, FCmp0, FCmp1, IsAnd);
1913
1914 if (!V)
1915 return nullptr;
1916 if (!Cast0)
1917 return V;
1918
1919 // If we looked through casts, we can only handle a constant simplification
1920 // because we are not allowed to create a cast instruction here.
1921 if (auto *C = dyn_cast<Constant>(V))
1922 return ConstantFoldCastOperand(Cast0->getOpcode(), C, Cast0->getType(),
1923 Q.DL);
1924
1925 return nullptr;
1926}
1927
1928static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
1929 const SimplifyQuery &Q,
1930 bool AllowRefinement,
1932 unsigned MaxRecurse);
1933
1934static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,
1935 const SimplifyQuery &Q,
1936 unsigned MaxRecurse) {
1937 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1938 "Must be and/or");
1940 Value *A, *B;
1941 if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) ||
1942 !ICmpInst::isEquality(Pred))
1943 return nullptr;
1944
1945 auto Simplify = [&](Value *Res) -> Value * {
1946 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, Res->getType());
1947
1948 // and (icmp eq a, b), x implies (a==b) inside x.
1949 // or (icmp ne a, b), x implies (a==b) inside x.
1950 // If x simplifies to true/false, we can simplify the and/or.
1951 if (Pred ==
1952 (Opcode == Instruction::And ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
1953 if (Res == Absorber)
1954 return Absorber;
1955 if (Res == ConstantExpr::getBinOpIdentity(Opcode, Res->getType()))
1956 return Op0;
1957 return nullptr;
1958 }
1959
1960 // If we have and (icmp ne a, b), x and for a==b we can simplify x to false,
1961 // then we can drop the icmp, as x will already be false in the case where
1962 // the icmp is false. Similar for or and true.
1963 if (Res == Absorber)
1964 return Op1;
1965 return nullptr;
1966 };
1967
1968 if (Value *Res =
1969 simplifyWithOpReplaced(Op1, A, B, Q, /* AllowRefinement */ true,
1970 /* DropFlags */ nullptr, MaxRecurse))
1971 return Simplify(Res);
1972 if (Value *Res =
1973 simplifyWithOpReplaced(Op1, B, A, Q, /* AllowRefinement */ true,
1974 /* DropFlags */ nullptr, MaxRecurse))
1975 return Simplify(Res);
1976
1977 return nullptr;
1978}
1979
1980/// Given a bitwise logic op, check if the operands are add/sub with a common
1981/// source value and inverted constant (identity: C - X -> ~(X + ~C)).
1983 Instruction::BinaryOps Opcode) {
1984 assert(Op0->getType() == Op1->getType() && "Mismatched binop types");
1985 assert(BinaryOperator::isBitwiseLogicOp(Opcode) && "Expected logic op");
1986 Value *X;
1987 Constant *C1, *C2;
1988 if ((match(Op0, m_Add(m_Value(X), m_Constant(C1))) &&
1989 match(Op1, m_Sub(m_Constant(C2), m_Specific(X)))) ||
1990 (match(Op1, m_Add(m_Value(X), m_Constant(C1))) &&
1991 match(Op0, m_Sub(m_Constant(C2), m_Specific(X))))) {
1992 if (ConstantExpr::getNot(C1) == C2) {
1993 // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0
1994 // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1
1995 // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1
1996 Type *Ty = Op0->getType();
1997 return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
1998 : ConstantInt::getAllOnesValue(Ty);
1999 }
2000 }
2001 return nullptr;
2002}
2003
2004// Commutative patterns for and that will be tried with both operand orders.
2006 const SimplifyQuery &Q,
2007 unsigned MaxRecurse) {
2008 // ~A & A = 0
2009 if (match(Op0, m_Not(m_Specific(Op1))))
2010 return Constant::getNullValue(Op0->getType());
2011
2012 // (A | ?) & A = A
2013 if (match(Op0, m_c_Or(m_Specific(Op1), m_Value())))
2014 return Op1;
2015
2016 // (X | ~Y) & (X | Y) --> X
2017 Value *X, *Y;
2018 if (match(Op0, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
2019 match(Op1, m_c_Or(m_Deferred(X), m_Deferred(Y))))
2020 return X;
2021
2022 // If we have a multiplication overflow check that is being 'and'ed with a
2023 // check that one of the multipliers is not zero, we can omit the 'and', and
2024 // only keep the overflow check.
2025 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, true))
2026 return Op1;
2027
2028 // -A & A = A if A is a power of two or zero.
2029 if (match(Op0, m_Neg(m_Specific(Op1))) &&
2030 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
2031 return Op1;
2032
2033 // This is a similar pattern used for checking if a value is a power-of-2:
2034 // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
2035 if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
2036 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
2037 return Constant::getNullValue(Op1->getType());
2038
2039 // (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and
2040 // M <= N.
2041 const APInt *Shift1, *Shift2;
2042 if (match(Op0, m_Shl(m_Value(X), m_APInt(Shift1))) &&
2043 match(Op1, m_Add(m_Shl(m_Specific(X), m_APInt(Shift2)), m_AllOnes())) &&
2044 isKnownToBeAPowerOfTwo(X, Q.DL, /*OrZero*/ true, /*Depth*/ 0, Q.AC,
2045 Q.CxtI) &&
2046 Shift1->uge(*Shift2))
2047 return Constant::getNullValue(Op0->getType());
2048
2049 if (Value *V =
2050 simplifyAndOrWithICmpEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2051 return V;
2052
2053 return nullptr;
2054}
2055
2056/// Given operands for an And, see if we can fold the result.
2057/// If not, this returns null.
2058static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2059 unsigned MaxRecurse) {
2060 if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
2061 return C;
2062
2063 // X & poison -> poison
2064 if (isa<PoisonValue>(Op1))
2065 return Op1;
2066
2067 // X & undef -> 0
2068 if (Q.isUndefValue(Op1))
2069 return Constant::getNullValue(Op0->getType());
2070
2071 // X & X = X
2072 if (Op0 == Op1)
2073 return Op0;
2074
2075 // X & 0 = 0
2076 if (match(Op1, m_Zero()))
2077 return Constant::getNullValue(Op0->getType());
2078
2079 // X & -1 = X
2080 if (match(Op1, m_AllOnes()))
2081 return Op0;
2082
2083 if (Value *Res = simplifyAndCommutative(Op0, Op1, Q, MaxRecurse))
2084 return Res;
2085 if (Value *Res = simplifyAndCommutative(Op1, Op0, Q, MaxRecurse))
2086 return Res;
2087
2088 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::And))
2089 return V;
2090
2091 // A mask that only clears known zeros of a shifted value is a no-op.
2092 const APInt *Mask;
2093 const APInt *ShAmt;
2094 Value *X, *Y;
2095 if (match(Op1, m_APInt(Mask))) {
2096 // If all bits in the inverted and shifted mask are clear:
2097 // and (shl X, ShAmt), Mask --> shl X, ShAmt
2098 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) &&
2099 (~(*Mask)).lshr(*ShAmt).isZero())
2100 return Op0;
2101
2102 // If all bits in the inverted and shifted mask are clear:
2103 // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
2104 if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
2105 (~(*Mask)).shl(*ShAmt).isZero())
2106 return Op0;
2107 }
2108
2109 // and 2^x-1, 2^C --> 0 where x <= C.
2110 const APInt *PowerC;
2111 Value *Shift;
2112 if (match(Op1, m_Power2(PowerC)) &&
2113 match(Op0, m_Add(m_Value(Shift), m_AllOnes())) &&
2114 isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ false, 0, Q.AC, Q.CxtI,
2115 Q.DT)) {
2116 KnownBits Known = computeKnownBits(Shift, /* Depth */ 0, Q);
2117 // Use getActiveBits() to make use of the additional power of two knowledge
2118 if (PowerC->getActiveBits() >= Known.getMaxValue().getActiveBits())
2119 return ConstantInt::getNullValue(Op1->getType());
2120 }
2121
2122 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
2123 return V;
2124
2125 // Try some generic simplifications for associative operations.
2126 if (Value *V =
2127 simplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse))
2128 return V;
2129
2130 // And distributes over Or. Try some generic simplifications based on this.
2131 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2132 Instruction::Or, Q, MaxRecurse))
2133 return V;
2134
2135 // And distributes over Xor. Try some generic simplifications based on this.
2136 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2137 Instruction::Xor, Q, MaxRecurse))
2138 return V;
2139
2140 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2141 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2142 // A & (A && B) -> A && B
2143 if (match(Op1, m_Select(m_Specific(Op0), m_Value(), m_Zero())))
2144 return Op1;
2145 else if (match(Op0, m_Select(m_Specific(Op1), m_Value(), m_Zero())))
2146 return Op0;
2147 }
2148 // If the operation is with the result of a select instruction, check
2149 // whether operating on either branch of the select always yields the same
2150 // value.
2151 if (Value *V =
2152 threadBinOpOverSelect(Instruction::And, Op0, Op1, Q, MaxRecurse))
2153 return V;
2154 }
2155
2156 // If the operation is with the result of a phi instruction, check whether
2157 // operating on all incoming values of the phi always yields the same value.
2158 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2159 if (Value *V =
2160 threadBinOpOverPHI(Instruction::And, Op0, Op1, Q, MaxRecurse))
2161 return V;
2162
2163 // Assuming the effective width of Y is not larger than A, i.e. all bits
2164 // from X and Y are disjoint in (X << A) | Y,
2165 // if the mask of this AND op covers all bits of X or Y, while it covers
2166 // no bits from the other, we can bypass this AND op. E.g.,
2167 // ((X << A) | Y) & Mask -> Y,
2168 // if Mask = ((1 << effective_width_of(Y)) - 1)
2169 // ((X << A) | Y) & Mask -> X << A,
2170 // if Mask = ((1 << effective_width_of(X)) - 1) << A
2171 // SimplifyDemandedBits in InstCombine can optimize the general case.
2172 // This pattern aims to help other passes for a common case.
2173 Value *XShifted;
2174 if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(Mask)) &&
2176 m_Value(XShifted)),
2177 m_Value(Y)))) {
2178 const unsigned Width = Op0->getType()->getScalarSizeInBits();
2179 const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
2180 const KnownBits YKnown = computeKnownBits(Y, /* Depth */ 0, Q);
2181 const unsigned EffWidthY = YKnown.countMaxActiveBits();
2182 if (EffWidthY <= ShftCnt) {
2183 const KnownBits XKnown = computeKnownBits(X, /* Depth */ 0, Q);
2184 const unsigned EffWidthX = XKnown.countMaxActiveBits();
2185 const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY);
2186 const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt;
2187 // If the mask is extracting all bits from X or Y as is, we can skip
2188 // this AND op.
2189 if (EffBitsY.isSubsetOf(*Mask) && !EffBitsX.intersects(*Mask))
2190 return Y;
2191 if (EffBitsX.isSubsetOf(*Mask) && !EffBitsY.intersects(*Mask))
2192 return XShifted;
2193 }
2194 }
2195
2196 // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
2197 // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
2199 if (match(Op0, m_c_Xor(m_Value(X),
2201 m_c_Or(m_Deferred(X), m_Value(Y))))) &&
2203 return Constant::getNullValue(Op0->getType());
2204
2205 const APInt *C1;
2206 Value *A;
2207 // (A ^ C) & (A ^ ~C) -> 0
2208 if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2209 match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2210 return Constant::getNullValue(Op0->getType());
2211
2212 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2213 if (std::optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) {
2214 // If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
2215 if (*Implied == true)
2216 return Op0;
2217 // If Op0 is true implies Op1 is false, then they are not true together.
2218 if (*Implied == false)
2219 return ConstantInt::getFalse(Op0->getType());
2220 }
2221 if (std::optional<bool> Implied = isImpliedCondition(Op1, Op0, Q.DL)) {
2222 // If Op1 is true implies Op0 is true, then Op1 is a subset of Op0.
2223 if (*Implied)
2224 return Op1;
2225 // If Op1 is true implies Op0 is false, then they are not true together.
2226 if (!*Implied)
2227 return ConstantInt::getFalse(Op1->getType());
2228 }
2229 }
2230
2231 if (Value *V = simplifyByDomEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2232 return V;
2233
2234 return nullptr;
2235}
2236
2238 return ::simplifyAndInst(Op0, Op1, Q, RecursionLimit);
2239}
2240
2241// TODO: Many of these folds could use LogicalAnd/LogicalOr.
2243 assert(X->getType() == Y->getType() && "Expected same type for 'or' ops");
2244 Type *Ty = X->getType();
2245
2246 // X | ~X --> -1
2247 if (match(Y, m_Not(m_Specific(X))))
2248 return ConstantInt::getAllOnesValue(Ty);
2249
2250 // X | ~(X & ?) = -1
2251 if (match(Y, m_Not(m_c_And(m_Specific(X), m_Value()))))
2252 return ConstantInt::getAllOnesValue(Ty);
2253
2254 // X | (X & ?) --> X
2255 if (match(Y, m_c_And(m_Specific(X), m_Value())))
2256 return X;
2257
2258 Value *A, *B;
2259
2260 // (A ^ B) | (A | B) --> A | B
2261 // (A ^ B) | (B | A) --> B | A
2262 if (match(X, m_Xor(m_Value(A), m_Value(B))) &&
2264 return Y;
2265
2266 // ~(A ^ B) | (A | B) --> -1
2267 // ~(A ^ B) | (B | A) --> -1
2268 if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) &&
2270 return ConstantInt::getAllOnesValue(Ty);
2271
2272 // (A & ~B) | (A ^ B) --> A ^ B
2273 // (~B & A) | (A ^ B) --> A ^ B
2274 // (A & ~B) | (B ^ A) --> B ^ A
2275 // (~B & A) | (B ^ A) --> B ^ A
2276 if (match(X, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2278 return Y;
2279
2280 // (~A ^ B) | (A & B) --> ~A ^ B
2281 // (B ^ ~A) | (A & B) --> B ^ ~A
2282 // (~A ^ B) | (B & A) --> ~A ^ B
2283 // (B ^ ~A) | (B & A) --> B ^ ~A
2284 if (match(X, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2286 return X;
2287
2288 // (~A | B) | (A ^ B) --> -1
2289 // (~A | B) | (B ^ A) --> -1
2290 // (B | ~A) | (A ^ B) --> -1
2291 // (B | ~A) | (B ^ A) --> -1
2292 if (match(X, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2294 return ConstantInt::getAllOnesValue(Ty);
2295
2296 // (~A & B) | ~(A | B) --> ~A
2297 // (~A & B) | ~(B | A) --> ~A
2298 // (B & ~A) | ~(A | B) --> ~A
2299 // (B & ~A) | ~(B | A) --> ~A
2300 Value *NotA;
2302 m_Value(B))) &&
2304 return NotA;
2305 // The same is true of Logical And
2306 // TODO: This could share the logic of the version above if there was a
2307 // version of LogicalAnd that allowed more than just i1 types.
2309 m_Value(B))) &&
2311 return NotA;
2312
2313 // ~(A ^ B) | (A & B) --> ~(A ^ B)
2314 // ~(A ^ B) | (B & A) --> ~(A ^ B)
2315 Value *NotAB;
2317 m_Value(NotAB))) &&
2319 return NotAB;
2320
2321 // ~(A & B) | (A ^ B) --> ~(A & B)
2322 // ~(A & B) | (B ^ A) --> ~(A & B)
2324 m_Value(NotAB))) &&
2326 return NotAB;
2327
2328 return nullptr;
2329}
2330
2331/// Given operands for an Or, see if we can fold the result.
2332/// If not, this returns null.
2333static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2334 unsigned MaxRecurse) {
2335 if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q))
2336 return C;
2337
2338 // X | poison -> poison
2339 if (isa<PoisonValue>(Op1))
2340 return Op1;
2341
2342 // X | undef -> -1
2343 // X | -1 = -1
2344 // Do not return Op1 because it may contain undef elements if it's a vector.
2345 if (Q.isUndefValue(Op1) || match(Op1, m_AllOnes()))
2346 return Constant::getAllOnesValue(Op0->getType());
2347
2348 // X | X = X
2349 // X | 0 = X
2350 if (Op0 == Op1 || match(Op1, m_Zero()))
2351 return Op0;
2352
2353 if (Value *R = simplifyOrLogic(Op0, Op1))
2354 return R;
2355 if (Value *R = simplifyOrLogic(Op1, Op0))
2356 return R;
2357
2358 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Or))
2359 return V;
2360
2361 // Rotated -1 is still -1:
2362 // (-1 << X) | (-1 >> (C - X)) --> -1
2363 // (-1 >> X) | (-1 << (C - X)) --> -1
2364 // ...with C <= bitwidth (and commuted variants).
2365 Value *X, *Y;
2366 if ((match(Op0, m_Shl(m_AllOnes(), m_Value(X))) &&
2367 match(Op1, m_LShr(m_AllOnes(), m_Value(Y)))) ||
2368 (match(Op1, m_Shl(m_AllOnes(), m_Value(X))) &&
2369 match(Op0, m_LShr(m_AllOnes(), m_Value(Y))))) {
2370 const APInt *C;
2371 if ((match(X, m_Sub(m_APInt(C), m_Specific(Y))) ||
2372 match(Y, m_Sub(m_APInt(C), m_Specific(X)))) &&
2373 C->ule(X->getType()->getScalarSizeInBits())) {
2374 return ConstantInt::getAllOnesValue(X->getType());
2375 }
2376 }
2377
2378 // A funnel shift (rotate) can be decomposed into simpler shifts. See if we
2379 // are mixing in another shift that is redundant with the funnel shift.
2380
2381 // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y
2382 // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y
2383 if (match(Op0,
2384 m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2385 match(Op1, m_Shl(m_Specific(X), m_Specific(Y))))
2386 return Op0;
2387 if (match(Op1,
2388 m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2389 match(Op0, m_Shl(m_Specific(X), m_Specific(Y))))
2390 return Op1;
2391
2392 // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y
2393 // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y
2394 if (match(Op0,
2395 m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2396 match(Op1, m_LShr(m_Specific(X), m_Specific(Y))))
2397 return Op0;
2398 if (match(Op1,
2399 m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2400 match(Op0, m_LShr(m_Specific(X), m_Specific(Y))))
2401 return Op1;
2402
2403 if (Value *V =
2404 simplifyAndOrWithICmpEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2405 return V;
2406 if (Value *V =
2407 simplifyAndOrWithICmpEq(Instruction::Or, Op1, Op0, Q, MaxRecurse))
2408 return V;
2409
2410 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false))
2411 return V;
2412
2413 // If we have a multiplication overflow check that is being 'and'ed with a
2414 // check that one of the multipliers is not zero, we can omit the 'and', and
2415 // only keep the overflow check.
2416 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, false))
2417 return Op1;
2418 if (isCheckForZeroAndMulWithOverflow(Op1, Op0, false))
2419 return Op0;
2420
2421 // Try some generic simplifications for associative operations.
2422 if (Value *V =
2423 simplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2424 return V;
2425
2426 // Or distributes over And. Try some generic simplifications based on this.
2427 if (Value *V = expandCommutativeBinOp(Instruction::Or, Op0, Op1,
2428 Instruction::And, Q, MaxRecurse))
2429 return V;
2430
2431 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2432 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2433 // A | (A || B) -> A || B
2434 if (match(Op1, m_Select(m_Specific(Op0), m_One(), m_Value())))
2435 return Op1;
2436 else if (match(Op0, m_Select(m_Specific(Op1), m_One(), m_Value())))
2437 return Op0;
2438 }
2439 // If the operation is with the result of a select instruction, check
2440 // whether operating on either branch of the select always yields the same
2441 // value.
2442 if (Value *V =
2443 threadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2444 return V;
2445 }
2446
2447 // (A & C1)|(B & C2)
2448 Value *A, *B;
2449 const APInt *C1, *C2;
2450 if (match(Op0, m_And(m_Value(A), m_APInt(C1))) &&
2451 match(Op1, m_And(m_Value(B), m_APInt(C2)))) {
2452 if (*C1 == ~*C2) {
2453 // (A & C1)|(B & C2)
2454 // If we have: ((V + N) & C1) | (V & C2)
2455 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
2456 // replace with V+N.
2457 Value *N;
2458 if (C2->isMask() && // C2 == 0+1+
2460 // Add commutes, try both ways.
2461 if (MaskedValueIsZero(N, *C2, Q))
2462 return A;
2463 }
2464 // Or commutes, try both ways.
2465 if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))) {
2466 // Add commutes, try both ways.
2467 if (MaskedValueIsZero(N, *C1, Q))
2468 return B;
2469 }
2470 }
2471 }
2472
2473 // If the operation is with the result of a phi instruction, check whether
2474 // operating on all incoming values of the phi always yields the same value.
2475 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2476 if (Value *V = threadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2477 return V;
2478
2479 // (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one.
2480 if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2481 match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2482 return Constant::getAllOnesValue(Op0->getType());
2483
2484 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2485 if (std::optional<bool> Implied =
2486 isImpliedCondition(Op0, Op1, Q.DL, false)) {
2487 // If Op0 is false implies Op1 is false, then Op1 is a subset of Op0.
2488 if (*Implied == false)
2489 return Op0;
2490 // If Op0 is false implies Op1 is true, then at least one is always true.
2491 if (*Implied == true)
2492 return ConstantInt::getTrue(Op0->getType());
2493 }
2494 if (std::optional<bool> Implied =
2495 isImpliedCondition(Op1, Op0, Q.DL, false)) {
2496 // If Op1 is false implies Op0 is false, then Op0 is a subset of Op1.
2497 if (*Implied == false)
2498 return Op1;
2499 // If Op1 is false implies Op0 is true, then at least one is always true.
2500 if (*Implied == true)
2501 return ConstantInt::getTrue(Op1->getType());
2502 }
2503 }
2504
2505 if (Value *V = simplifyByDomEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2506 return V;
2507
2508 return nullptr;
2509}
2510
2512 return ::simplifyOrInst(Op0, Op1, Q, RecursionLimit);
2513}
2514
2515/// Given operands for a Xor, see if we can fold the result.
2516/// If not, this returns null.
2517static Value *simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2518 unsigned MaxRecurse) {
2519 if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q))
2520 return C;
2521
2522 // X ^ poison -> poison
2523 if (isa<PoisonValue>(Op1))
2524 return Op1;
2525
2526 // A ^ undef -> undef
2527 if (Q.isUndefValue(Op1))
2528 return Op1;
2529
2530 // A ^ 0 = A
2531 if (match(Op1, m_Zero()))
2532 return Op0;
2533
2534 // A ^ A = 0
2535 if (Op0 == Op1)
2536 return Constant::getNullValue(Op0->getType());
2537
2538 // A ^ ~A = ~A ^ A = -1
2539 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
2540 return Constant::getAllOnesValue(Op0->getType());
2541
2542 auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
2543 Value *A, *B;
2544 // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
2545 if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
2547 return A;
2548
2549 // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
2550 // The 'not' op must contain a complete -1 operand (no undef elements for
2551 // vector) for the transform to be safe.
2552 Value *NotA;
2554 m_Value(B))) &&
2556 return NotA;
2557
2558 return nullptr;
2559 };
2560 if (Value *R = foldAndOrNot(Op0, Op1))
2561 return R;
2562 if (Value *R = foldAndOrNot(Op1, Op0))
2563 return R;
2564
2565 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor))
2566 return V;
2567
2568 // Try some generic simplifications for associative operations.
2569 if (Value *V =
2570 simplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2571 return V;
2572
2573 // Threading Xor over selects and phi nodes is pointless, so don't bother.
2574 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2575 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2576 // only if B and C are equal. If B and C are equal then (since we assume
2577 // that operands have already been simplified) "select(cond, B, C)" should
2578 // have been simplified to the common value of B and C already. Analysing
2579 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
2580 // for threading over phi nodes.
2581
2582 if (Value *V = simplifyByDomEq(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2583 return V;
2584
2585 return nullptr;
2586}
2587
2589 return ::simplifyXorInst(Op0, Op1, Q, RecursionLimit);
2590}
2591
2593 return CmpInst::makeCmpResultType(Op->getType());
2594}
2595
2596/// Rummage around inside V looking for something equivalent to the comparison
2597/// "LHS Pred RHS". Return such a value if found, otherwise return null.
2598/// Helper function for analyzing max/min idioms.
2600 Value *LHS, Value *RHS) {
2601 SelectInst *SI = dyn_cast<SelectInst>(V);
2602 if (!SI)
2603 return nullptr;
2604 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2605 if (!Cmp)
2606 return nullptr;
2607 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2608 if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2609 return Cmp;
2610 if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2611 LHS == CmpRHS && RHS == CmpLHS)
2612 return Cmp;
2613 return nullptr;
2614}
2615
2616/// Return true if the underlying object (storage) must be disjoint from
2617/// storage returned by any noalias return call.
2618static bool isAllocDisjoint(const Value *V) {
2619 // For allocas, we consider only static ones (dynamic
2620 // allocas might be transformed into calls to malloc not simultaneously
2621 // live with the compared-to allocation). For globals, we exclude symbols
2622 // that might be resolve lazily to symbols in another dynamically-loaded
2623 // library (and, thus, could be malloc'ed by the implementation).
2624 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2625 return AI->isStaticAlloca();
2626 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2627 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2628 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2629 !GV->isThreadLocal();
2630 if (const Argument *A = dyn_cast<Argument>(V))
2631 return A->hasByValAttr();
2632 return false;
2633}
2634
2635/// Return true if V1 and V2 are each the base of some distict storage region
2636/// [V, object_size(V)] which do not overlap. Note that zero sized regions
2637/// *are* possible, and that zero sized regions do not overlap with any other.
2638static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
2639 // Global variables always exist, so they always exist during the lifetime
2640 // of each other and all allocas. Global variables themselves usually have
2641 // non-overlapping storage, but since their addresses are constants, the
2642 // case involving two globals does not reach here and is instead handled in
2643 // constant folding.
2644 //
2645 // Two different allocas usually have different addresses...
2646 //
2647 // However, if there's an @llvm.stackrestore dynamically in between two
2648 // allocas, they may have the same address. It's tempting to reduce the
2649 // scope of the problem by only looking at *static* allocas here. That would
2650 // cover the majority of allocas while significantly reducing the likelihood
2651 // of having an @llvm.stackrestore pop up in the middle. However, it's not
2652 // actually impossible for an @llvm.stackrestore to pop up in the middle of
2653 // an entry block. Also, if we have a block that's not attached to a
2654 // function, we can't tell if it's "static" under the current definition.
2655 // Theoretically, this problem could be fixed by creating a new kind of
2656 // instruction kind specifically for static allocas. Such a new instruction
2657 // could be required to be at the top of the entry block, thus preventing it
2658 // from being subject to a @llvm.stackrestore. Instcombine could even
2659 // convert regular allocas into these special allocas. It'd be nifty.
2660 // However, until then, this problem remains open.
2661 //
2662 // So, we'll assume that two non-empty allocas have different addresses
2663 // for now.
2664 auto isByValArg = [](const Value *V) {
2665 const Argument *A = dyn_cast<Argument>(V);
2666 return A && A->hasByValAttr();
2667 };
2668
2669 // Byval args are backed by store which does not overlap with each other,
2670 // allocas, or globals.
2671 if (isByValArg(V1))
2672 return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2673 if (isByValArg(V2))
2674 return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2675
2676 return isa<AllocaInst>(V1) &&
2677 (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
2678}
2679
2680// A significant optimization not implemented here is assuming that alloca
2681// addresses are not equal to incoming argument values. They don't *alias*,
2682// as we say, but that doesn't mean they aren't equal, so we take a
2683// conservative approach.
2684//
2685// This is inspired in part by C++11 5.10p1:
2686// "Two pointers of the same type compare equal if and only if they are both
2687// null, both point to the same function, or both represent the same
2688// address."
2689//
2690// This is pretty permissive.
2691//
2692// It's also partly due to C11 6.5.9p6:
2693// "Two pointers compare equal if and only if both are null pointers, both are
2694// pointers to the same object (including a pointer to an object and a
2695// subobject at its beginning) or function, both are pointers to one past the
2696// last element of the same array object, or one is a pointer to one past the
2697// end of one array object and the other is a pointer to the start of a
2698// different array object that happens to immediately follow the first array
2699// object in the address space.)
2700//
2701// C11's version is more restrictive, however there's no reason why an argument
2702// couldn't be a one-past-the-end value for a stack object in the caller and be
2703// equal to the beginning of a stack object in the callee.
2704//
2705// If the C and C++ standards are ever made sufficiently restrictive in this
2706// area, it may be possible to update LLVM's semantics accordingly and reinstate
2707// this optimization.
2709 Value *RHS, const SimplifyQuery &Q) {
2710 assert(LHS->getType() == RHS->getType() && "Must have same types");
2711 const DataLayout &DL = Q.DL;
2712 const TargetLibraryInfo *TLI = Q.TLI;
2713
2714 // We can only fold certain predicates on pointer comparisons.
2715 switch (Pred) {
2716 default:
2717 return nullptr;
2718
2719 // Equality comparisons are easy to fold.
2720 case CmpInst::ICMP_EQ:
2721 case CmpInst::ICMP_NE:
2722 break;
2723
2724 // We can only handle unsigned relational comparisons because 'inbounds' on
2725 // a GEP only protects against unsigned wrapping.
2726 case CmpInst::ICMP_UGT:
2727 case CmpInst::ICMP_UGE:
2728 case CmpInst::ICMP_ULT:
2729 case CmpInst::ICMP_ULE:
2730 // However, we have to switch them to their signed variants to handle
2731 // negative indices from the base pointer.
2732 Pred = ICmpInst::getSignedPredicate(Pred);
2733 break;
2734 }
2735
2736 // Strip off any constant offsets so that we can reason about them.
2737 // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2738 // here and compare base addresses like AliasAnalysis does, however there are
2739 // numerous hazards. AliasAnalysis and its utilities rely on special rules
2740 // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2741 // doesn't need to guarantee pointer inequality when it says NoAlias.
2742
2743 // Even if an non-inbounds GEP occurs along the path we can still optimize
2744 // equality comparisons concerning the result.
2745 bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2746 unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
2747 APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2748 LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
2749 RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);
2750
2751 // If LHS and RHS are related via constant offsets to the same base
2752 // value, we can replace it with an icmp which just compares the offsets.
2753 if (LHS == RHS)
2754 return ConstantInt::get(getCompareTy(LHS),
2755 ICmpInst::compare(LHSOffset, RHSOffset, Pred));
2756
2757 // Various optimizations for (in)equality comparisons.
2758 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
2759 // Different non-empty allocations that exist at the same time have
2760 // different addresses (if the program can tell). If the offsets are
2761 // within the bounds of their allocations (and not one-past-the-end!
2762 // so we can't use inbounds!), and their allocations aren't the same,
2763 // the pointers are not equal.
2765 uint64_t LHSSize, RHSSize;
2766 ObjectSizeOpts Opts;
2767 Opts.EvalMode = ObjectSizeOpts::Mode::Min;
2768 auto *F = [](Value *V) -> Function * {
2769 if (auto *I = dyn_cast<Instruction>(V))
2770 return I->getFunction();
2771 if (auto *A = dyn_cast<Argument>(V))
2772 return A->getParent();
2773 return nullptr;
2774 }(LHS);
2775 Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
2776 if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&
2777 getObjectSize(RHS, RHSSize, DL, TLI, Opts)) {
2778 APInt Dist = LHSOffset - RHSOffset;
2779 if (Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize))
2780 return ConstantInt::get(getCompareTy(LHS),
2782 }
2783 }
2784
2785 // If one side of the equality comparison must come from a noalias call
2786 // (meaning a system memory allocation function), and the other side must
2787 // come from a pointer that cannot overlap with dynamically-allocated
2788 // memory within the lifetime of the current function (allocas, byval
2789 // arguments, globals), then determine the comparison result here.
2790 SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2791 getUnderlyingObjects(LHS, LHSUObjs);
2792 getUnderlyingObjects(RHS, RHSUObjs);
2793
2794 // Is the set of underlying objects all noalias calls?
2795 auto IsNAC = [](ArrayRef<const Value *> Objects) {
2796 return all_of(Objects, isNoAliasCall);
2797 };
2798
2799 // Is the set of underlying objects all things which must be disjoint from
2800 // noalias calls. We assume that indexing from such disjoint storage
2801 // into the heap is undefined, and thus offsets can be safely ignored.
2802 auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2803 return all_of(Objects, ::isAllocDisjoint);
2804 };
2805
2806 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2807 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2808 return ConstantInt::get(getCompareTy(LHS),
2810
2811 // Fold comparisons for non-escaping pointer even if the allocation call
2812 // cannot be elided. We cannot fold malloc comparison to null. Also, the
2813 // dynamic allocation call could be either of the operands. Note that
2814 // the other operand can not be based on the alloc - if it were, then
2815 // the cmp itself would be a capture.
2816 Value *MI = nullptr;
2817 if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonZero(RHS, Q))
2818 MI = LHS;
2819 else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonZero(LHS, Q))
2820 MI = RHS;
2821 if (MI) {
2822 // FIXME: This is incorrect, see PR54002. While we can assume that the
2823 // allocation is at an address that makes the comparison false, this
2824 // requires that *all* comparisons to that address be false, which
2825 // InstSimplify cannot guarantee.
2826 struct CustomCaptureTracker : public CaptureTracker {
2827 bool Captured = false;
2828 void tooManyUses() override { Captured = true; }
2829 bool captured(const Use *U) override {
2830 if (auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2831 // Comparison against value stored in global variable. Given the
2832 // pointer does not escape, its value cannot be guessed and stored
2833 // separately in a global variable.
2834 unsigned OtherIdx = 1 - U->getOperandNo();
2835 auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2836 if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2837 return false;
2838 }
2839
2840 Captured = true;
2841 return true;
2842 }
2843 };
2844 CustomCaptureTracker Tracker;
2845 PointerMayBeCaptured(MI, &Tracker);
2846 if (!Tracker.Captured)
2847 return ConstantInt::get(getCompareTy(LHS),
2849 }
2850 }
2851
2852 // Otherwise, fail.
2853 return nullptr;
2854}
2855
2856/// Fold an icmp when its operands have i1 scalar type.
2858 Value *RHS, const SimplifyQuery &Q) {
2859 Type *ITy = getCompareTy(LHS); // The return type.
2860 Type *OpTy = LHS->getType(); // The operand type.
2861 if (!OpTy->isIntOrIntVectorTy(1))
2862 return nullptr;
2863
2864 // A boolean compared to true/false can be reduced in 14 out of the 20
2865 // (10 predicates * 2 constants) possible combinations. The other
2866 // 6 cases require a 'not' of the LHS.
2867
2868 auto ExtractNotLHS = [](Value *V) -> Value * {
2869 Value *X;
2870 if (match(V, m_Not(m_Value(X))))
2871 return X;
2872 return nullptr;
2873 };
2874
2875 if (match(RHS, m_Zero())) {
2876 switch (Pred) {
2877 case CmpInst::ICMP_NE: // X != 0 -> X
2878 case CmpInst::ICMP_UGT: // X >u 0 -> X
2879 case CmpInst::ICMP_SLT: // X <s 0 -> X
2880 return LHS;
2881
2882 case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
2883 case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2884 case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2885 if (Value *X = ExtractNotLHS(LHS))
2886 return X;
2887 break;
2888
2889 case CmpInst::ICMP_ULT: // X <u 0 -> false
2890 case CmpInst::ICMP_SGT: // X >s 0 -> false
2891 return getFalse(ITy);
2892
2893 case CmpInst::ICMP_UGE: // X >=u 0 -> true
2894 case CmpInst::ICMP_SLE: // X <=s 0 -> true
2895 return getTrue(ITy);
2896
2897 default:
2898 break;
2899 }
2900 } else if (match(RHS, m_One())) {
2901 switch (Pred) {
2902 case CmpInst::ICMP_EQ: // X == 1 -> X
2903 case CmpInst::ICMP_UGE: // X >=u 1 -> X
2904 case CmpInst::ICMP_SLE: // X <=s -1 -> X
2905 return LHS;
2906
2907 case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
2908 case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
2909 case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
2910 if (Value *X = ExtractNotLHS(LHS))
2911 return X;
2912 break;
2913
2914 case CmpInst::ICMP_UGT: // X >u 1 -> false
2915 case CmpInst::ICMP_SLT: // X <s -1 -> false
2916 return getFalse(ITy);
2917
2918 case CmpInst::ICMP_ULE: // X <=u 1 -> true
2919 case CmpInst::ICMP_SGE: // X >=s -1 -> true
2920 return getTrue(ITy);
2921
2922 default:
2923 break;
2924 }
2925 }
2926
2927 switch (Pred) {
2928 default:
2929 break;
2930 case ICmpInst::ICMP_UGE:
2931 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2932 return getTrue(ITy);
2933 break;
2934 case ICmpInst::ICMP_SGE:
2935 /// For signed comparison, the values for an i1 are 0 and -1
2936 /// respectively. This maps into a truth table of:
2937 /// LHS | RHS | LHS >=s RHS | LHS implies RHS
2938 /// 0 | 0 | 1 (0 >= 0) | 1
2939 /// 0 | 1 | 1 (0 >= -1) | 1
2940 /// 1 | 0 | 0 (-1 >= 0) | 0
2941 /// 1 | 1 | 1 (-1 >= -1) | 1
2942 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2943 return getTrue(ITy);
2944 break;
2945 case ICmpInst::ICMP_ULE:
2946 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2947 return getTrue(ITy);
2948 break;
2949 case ICmpInst::ICMP_SLE:
2950 /// SLE follows the same logic as SGE with the LHS and RHS swapped.
2951 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2952 return getTrue(ITy);
2953 break;
2954 }
2955
2956 return nullptr;
2957}
2958
2959/// Try hard to fold icmp with zero RHS because this is a common case.
2961 Value *RHS, const SimplifyQuery &Q) {
2962 if (!match(RHS, m_Zero()))
2963 return nullptr;
2964
2965 Type *ITy = getCompareTy(LHS); // The return type.
2966 switch (Pred) {
2967 default:
2968 llvm_unreachable("Unknown ICmp predicate!");
2969 case ICmpInst::ICMP_ULT:
2970 return getFalse(ITy);
2971 case ICmpInst::ICMP_UGE:
2972 return getTrue(ITy);
2973 case ICmpInst::ICMP_EQ:
2974 case ICmpInst::ICMP_ULE:
2975 if (isKnownNonZero(LHS, Q))
2976 return getFalse(ITy);
2977 break;
2978 case ICmpInst::ICMP_NE:
2979 case ICmpInst::ICMP_UGT:
2980 if (isKnownNonZero(LHS, Q))
2981 return getTrue(ITy);
2982 break;
2983 case ICmpInst::ICMP_SLT: {
2984 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2985 if (LHSKnown.isNegative())
2986 return getTrue(ITy);
2987 if (LHSKnown.isNonNegative())
2988 return getFalse(ITy);
2989 break;
2990 }
2991 case ICmpInst::ICMP_SLE: {
2992 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2993 if (LHSKnown.isNegative())
2994 return getTrue(ITy);
2995 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2996 return getFalse(ITy);
2997 break;
2998 }
2999 case ICmpInst::ICMP_SGE: {
3000 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
3001 if (LHSKnown.isNegative())
3002 return getFalse(ITy);
3003 if (LHSKnown.isNonNegative())
3004 return getTrue(ITy);
3005 break;
3006 }
3007 case ICmpInst::ICMP_SGT: {
3008 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
3009 if (LHSKnown.isNegative())
3010 return getFalse(ITy);
3011 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
3012 return getTrue(ITy);
3013 break;
3014 }
3015 }
3016
3017 return nullptr;
3018}
3019
3021 Value *RHS, const InstrInfoQuery &IIQ) {
3022 Type *ITy = getCompareTy(RHS); // The return type.
3023
3024 Value *X;
3025 const APInt *C;
3026 if (!match(RHS, m_APIntAllowPoison(C)))
3027 return nullptr;
3028
3029 // Sign-bit checks can be optimized to true/false after unsigned
3030 // floating-point casts:
3031 // icmp slt (bitcast (uitofp X)), 0 --> false
3032 // icmp sgt (bitcast (uitofp X)), -1 --> true
3034 bool TrueIfSigned;
3035 if (isSignBitCheck(Pred, *C, TrueIfSigned))
3036 return ConstantInt::getBool(ITy, !TrueIfSigned);
3037 }
3038
3039 // Rule out tautological comparisons (eg., ult 0 or uge 0).
3041 if (RHS_CR.isEmptySet())
3042 return ConstantInt::getFalse(ITy);
3043 if (RHS_CR.isFullSet())
3044 return ConstantInt::getTrue(ITy);
3045
3046 ConstantRange LHS_CR =
3048 if (!LHS_CR.isFullSet()) {
3049 if (RHS_CR.contains(LHS_CR))
3050 return ConstantInt::getTrue(ITy);
3051 if (RHS_CR.inverse().contains(LHS_CR))
3052 return ConstantInt::getFalse(ITy);
3053 }
3054
3055 // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
3056 // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
3057 const APInt *MulC;
3058 if (IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
3060 *MulC != 0 && C->urem(*MulC) != 0) ||
3062 *MulC != 0 && C->srem(*MulC) != 0)))
3063 return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
3064
3065 return nullptr;
3066}
3067
3069 BinaryOperator *LBO, Value *RHS,
3070 const SimplifyQuery &Q,
3071 unsigned MaxRecurse) {
3072 Type *ITy = getCompareTy(RHS); // The return type.
3073
3074 Value *Y = nullptr;
3075 // icmp pred (or X, Y), X
3076 if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
3077 if (Pred == ICmpInst::ICMP_ULT)
3078 return getFalse(ITy);
3079 if (Pred == ICmpInst::ICMP_UGE)
3080 return getTrue(ITy);
3081
3082 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3083 KnownBits RHSKnown = computeKnownBits(RHS, /* Depth */ 0, Q);
3084 KnownBits YKnown = computeKnownBits(Y, /* Depth */ 0, Q);
3085 if (RHSKnown.isNonNegative() && YKnown.isNegative())
3086 return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
3087 if (RHSKnown.isNegative() || YKnown.isNonNegative())
3088 return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
3089 }
3090 }
3091
3092 // icmp pred (and X, Y), X
3093 if (match(LBO, m_c_And(m_Value(), m_Specific(RHS)))) {
3094 if (Pred == ICmpInst::ICMP_UGT)
3095 return getFalse(ITy);
3096 if (Pred == ICmpInst::ICMP_ULE)
3097 return getTrue(ITy);
3098 }
3099
3100 // icmp pred (urem X, Y), Y
3101 if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
3102 switch (Pred) {
3103 default:
3104 break;
3105 case ICmpInst::ICMP_SGT:
3106 case ICmpInst::ICMP_SGE: {
3107 KnownBits Known = computeKnownBits(RHS, /* Depth */ 0, Q);
3108 if (!Known.isNonNegative())
3109 break;
3110 [[fallthrough]];
3111 }
3112 case ICmpInst::ICMP_EQ:
3113 case ICmpInst::ICMP_UGT:
3114 case ICmpInst::ICMP_UGE:
3115 return getFalse(ITy);
3116 case ICmpInst::ICMP_SLT:
3117 case ICmpInst::ICMP_SLE: {
3118 KnownBits Known = computeKnownBits(RHS, /* Depth */ 0, Q);
3119 if (!Known.isNonNegative())
3120 break;
3121 [[fallthrough]];
3122 }
3123 case ICmpInst::ICMP_NE:
3124 case ICmpInst::ICMP_ULT:
3125 case ICmpInst::ICMP_ULE:
3126 return getTrue(ITy);
3127 }
3128 }
3129
3130 // icmp pred (urem X, Y), X
3131 if (match(LBO, m_URem(m_Specific(RHS), m_Value()))) {
3132 if (Pred == ICmpInst::ICMP_ULE)
3133 return getTrue(ITy);
3134 if (Pred == ICmpInst::ICMP_UGT)
3135 return getFalse(ITy);
3136 }
3137
3138 // x >>u y <=u x --> true.
3139 // x >>u y >u x --> false.
3140 // x udiv y <=u x --> true.
3141 // x udiv y >u x --> false.
3142 if (match(LBO, m_LShr(m_Specific(RHS), m_Value())) ||
3143 match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) {
3144 // icmp pred (X op Y), X
3145 if (Pred == ICmpInst::ICMP_UGT)
3146 return getFalse(ITy);
3147 if (Pred == ICmpInst::ICMP_ULE)
3148 return getTrue(ITy);
3149 }
3150
3151 // If x is nonzero:
3152 // x >>u C <u x --> true for C != 0.
3153 // x >>u C != x --> true for C != 0.
3154 // x >>u C >=u x --> false for C != 0.
3155 // x >>u C == x --> false for C != 0.
3156 // x udiv C <u x --> true for C != 1.
3157 // x udiv C != x --> true for C != 1.
3158 // x udiv C >=u x --> false for C != 1.
3159 // x udiv C == x --> false for C != 1.
3160 // TODO: allow non-constant shift amount/divisor
3161 const APInt *C;
3162 if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 0) ||
3163 (match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C))) && *C != 1)) {
3164 if (isKnownNonZero(RHS, Q)) {
3165 switch (Pred) {
3166 default:
3167 break;
3168 case ICmpInst::ICMP_EQ:
3169 case ICmpInst::ICMP_UGE:
3170 return getFalse(ITy);
3171 case ICmpInst::ICMP_NE:
3172 case ICmpInst::ICMP_ULT:
3173 return getTrue(ITy);
3174 case ICmpInst::ICMP_UGT:
3175 case ICmpInst::ICMP_ULE:
3176 // UGT/ULE are handled by the more general case just above
3177 llvm_unreachable("Unexpected UGT/ULE, should have been handled");
3178 }
3179 }
3180 }
3181
3182 // (x*C1)/C2 <= x for C1 <= C2.
3183 // This holds even if the multiplication overflows: Assume that x != 0 and
3184 // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
3185 // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3186 //
3187 // Additionally, either the multiplication and division might be represented
3188 // as shifts:
3189 // (x*C1)>>C2 <= x for C1 < 2**C2.
3190 // (x<<C1)/C2 <= x for 2**C1 < C2.
3191 const APInt *C1, *C2;
3192 if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3193 C1->ule(*C2)) ||
3194 (match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3195 C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
3196 (match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3197 (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
3198 if (Pred == ICmpInst::ICMP_UGT)
3199 return getFalse(ITy);
3200 if (Pred == ICmpInst::ICMP_ULE)
3201 return getTrue(ITy);
3202 }
3203
3204 // (sub C, X) == X, C is odd --> false
3205 // (sub C, X) != X, C is odd --> true
3206 if (match(LBO, m_Sub(m_APIntAllowPoison(C), m_Specific(RHS))) &&
3207 (*C & 1) == 1 && ICmpInst::isEquality(Pred))
3208 return (Pred == ICmpInst::ICMP_EQ) ? getFalse(ITy) : getTrue(ITy);
3209
3210 return nullptr;
3211}
3212
3213// If only one of the icmp's operands has NSW flags, try to prove that:
3214//
3215// icmp slt (x + C1), (x +nsw C2)
3216//
3217// is equivalent to:
3218//
3219// icmp slt C1, C2
3220//
3221// which is true if x + C2 has the NSW flags set and:
3222// *) C1 < C2 && C1 >= 0, or
3223// *) C2 < C1 && C1 <= 0.
3224//
3226 Value *RHS, const InstrInfoQuery &IIQ) {
3227 // TODO: only support icmp slt for now.
3228 if (Pred != CmpInst::ICMP_SLT || !IIQ.UseInstrInfo)
3229 return false;
3230
3231 // Canonicalize nsw add as RHS.
3232 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3233 std::swap(LHS, RHS);
3234 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3235 return false;
3236
3237 Value *X;
3238 const APInt *C1, *C2;
3239 if (!match(LHS, m_Add(m_Value(X), m_APInt(C1))) ||
3240 !match(RHS, m_Add(m_Specific(X), m_APInt(C2))))
3241 return false;
3242
3243 return (C1->slt(*C2) && C1->isNonNegative()) ||
3244 (C2->slt(*C1) && C1->isNonPositive());
3245}
3246
3247/// TODO: A large part of this logic is duplicated in InstCombine's
3248/// foldICmpBinOp(). We should be able to share that and avoid the code
3249/// duplication.
3251 Value *RHS, const SimplifyQuery &Q,
3252 unsigned MaxRecurse) {
3253 BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
3254 BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
3255 if (MaxRecurse && (LBO || RBO)) {
3256 // Analyze the case when either LHS or RHS is an add instruction.
3257 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
3258 // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
3259 bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
3260 if (LBO && LBO->getOpcode() == Instruction::Add) {
3261 A = LBO->getOperand(0);
3262 B = LBO->getOperand(1);
3263 NoLHSWrapProblem =
3264 ICmpInst::isEquality(Pred) ||
3265 (CmpInst::isUnsigned(Pred) &&
3266 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))) ||
3267 (CmpInst::isSigned(Pred) &&
3268 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)));
3269 }
3270 if (RBO && RBO->getOpcode() == Instruction::Add) {
3271 C = RBO->getOperand(0);
3272 D = RBO->getOperand(1);
3273 NoRHSWrapProblem =
3274 ICmpInst::isEquality(Pred) ||
3275 (CmpInst::isUnsigned(Pred) &&
3276 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))) ||
3277 (CmpInst::isSigned(Pred) &&
3278 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO)));
3279 }
3280
3281 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3282 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
3283 if (Value *V = simplifyICmpInst(Pred, A == RHS ? B : A,
3285 MaxRecurse - 1))
3286 return V;
3287
3288 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3289 if ((C == LHS || D == LHS) && NoRHSWrapProblem)
3290 if (Value *V =
3292 C == LHS ? D : C, Q, MaxRecurse - 1))
3293 return V;
3294
3295 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3296 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3298 if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
3299 // Determine Y and Z in the form icmp (X+Y), (X+Z).
3300 Value *Y, *Z;
3301 if (A == C) {
3302 // C + B == C + D -> B == D
3303 Y = B;
3304 Z = D;
3305 } else if (A == D) {
3306 // D + B == C + D -> B == C
3307 Y = B;
3308 Z = C;
3309 } else if (B == C) {
3310 // A + C == C + D -> A == D
3311 Y = A;
3312 Z = D;
3313 } else {
3314 assert(B == D);
3315 // A + D == C + D -> A == C
3316 Y = A;
3317 Z = C;
3318 }
3319 if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
3320 return V;
3321 }
3322 }
3323
3324 if (LBO)
3325 if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
3326 return V;
3327
3328 if (RBO)
3330 ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse))
3331 return V;
3332
3333 // 0 - (zext X) pred C
3334 if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
3335 const APInt *C;
3336 if (match(RHS, m_APInt(C))) {
3337 if (C->isStrictlyPositive()) {
3338 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3340 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3342 }
3343 if (C->isNonNegative()) {
3344 if (Pred == ICmpInst::ICMP_SLE)
3346 if (Pred == ICmpInst::ICMP_SGT)
3348 }
3349 }
3350 }
3351
3352 // If C2 is a power-of-2 and C is not:
3353 // (C2 << X) == C --> false
3354 // (C2 << X) != C --> true
3355 const APInt *C;
3356 if (match(LHS, m_Shl(m_Power2(), m_Value())) &&
3357 match(RHS, m_APIntAllowPoison(C)) && !C->isPowerOf2()) {
3358 // C2 << X can equal zero in some circumstances.
3359 // This simplification might be unsafe if C is zero.
3360 //
3361 // We know it is safe if:
3362 // - The shift is nsw. We can't shift out the one bit.
3363 // - The shift is nuw. We can't shift out the one bit.
3364 // - C2 is one.
3365 // - C isn't zero.
3366 if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3367 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3368 match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
3369 if (Pred == ICmpInst::ICMP_EQ)
3371 if (Pred == ICmpInst::ICMP_NE)
3373 }
3374 }
3375
3376 // If C is a power-of-2:
3377 // (C << X) >u 0x8000 --> false
3378 // (C << X) <=u 0x8000 --> true
3379 if (match(LHS, m_Shl(m_Power2(), m_Value())) && match(RHS, m_SignMask())) {
3380 if (Pred == ICmpInst::ICMP_UGT)
3382 if (Pred == ICmpInst::ICMP_ULE)
3384 }
3385
3386 if (!MaxRecurse || !LBO || !RBO || LBO->getOpcode() != RBO->getOpcode())
3387 return nullptr;
3388
3389 if (LBO->getOperand(0) == RBO->getOperand(0)) {
3390 switch (LBO->getOpcode()) {
3391 default:
3392 break;
3393 case Instruction::Shl: {
3394 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3395 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3396 if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3397 !isKnownNonZero(LBO->getOperand(0), Q))
3398 break;
3399 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1),
3400 RBO->getOperand(1), Q, MaxRecurse - 1))
3401 return V;
3402 break;
3403 }
3404 // If C1 & C2 == C1, A = X and/or C1, B = X and/or C2:
3405 // icmp ule A, B -> true
3406 // icmp ugt A, B -> false
3407 // icmp sle A, B -> true (C1 and C2 are the same sign)
3408 // icmp sgt A, B -> false (C1 and C2 are the same sign)
3409 case Instruction::And:
3410 case Instruction::Or: {
3411 const APInt *C1, *C2;
3412 if (ICmpInst::isRelational(Pred) &&
3413 match(LBO->getOperand(1), m_APInt(C1)) &&
3414 match(RBO->getOperand(1), m_APInt(C2))) {
3415 if (!C1->isSubsetOf(*C2)) {
3416 std::swap(C1, C2);
3417 Pred = ICmpInst::getSwappedPredicate(Pred);
3418 }
3419 if (C1->isSubsetOf(*C2)) {
3420 if (Pred == ICmpInst::ICMP_ULE)
3422 if (Pred == ICmpInst::ICMP_UGT)
3424 if (C1->isNonNegative() == C2->isNonNegative()) {
3425 if (Pred == ICmpInst::ICMP_SLE)
3427 if (Pred == ICmpInst::ICMP_SGT)
3429 }
3430 }
3431 }
3432 break;
3433 }
3434 }
3435 }
3436
3437 if (LBO->getOperand(1) == RBO->getOperand(1)) {
3438 switch (LBO->getOpcode()) {
3439 default:
3440 break;
3441 case Instruction::UDiv:
3442 case Instruction::LShr:
3443 if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO) ||
3444 !Q.IIQ.isExact(RBO))
3445 break;
3446 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3447 RBO->getOperand(0), Q, MaxRecurse - 1))
3448 return V;
3449 break;
3450 case Instruction::SDiv:
3451 if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) ||
3452 !Q.IIQ.isExact(RBO))
3453 break;
3454 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3455 RBO->getOperand(0), Q, MaxRecurse - 1))
3456 return V;
3457 break;
3458 case Instruction::AShr:
3459 if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO))
3460 break;
3461 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3462 RBO->getOperand(0), Q, MaxRecurse - 1))
3463 return V;
3464 break;
3465 case Instruction::Shl: {
3466 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3467 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3468 if (!NUW && !NSW)
3469 break;
3470 if (!NSW && ICmpInst::isSigned(Pred))
3471 break;
3472 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3473 RBO->getOperand(0), Q, MaxRecurse - 1))
3474 return V;
3475 break;
3476 }
3477 }
3478 }
3479 return nullptr;
3480}
3481
3482/// simplify integer comparisons where at least one operand of the compare
3483/// matches an integer min/max idiom.
3485 Value *RHS, const SimplifyQuery &Q,
3486 unsigned MaxRecurse) {
3487 Type *ITy = getCompareTy(LHS); // The return type.
3488 Value *A, *B;
3490 CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
3491
3492 // Signed variants on "max(a,b)>=a -> true".
3493 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3494 if (A != RHS)
3495 std::swap(A, B); // smax(A, B) pred A.
3496 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3497 // We analyze this as smax(A, B) pred A.
3498 P = Pred;
3499 } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
3500 (A == LHS || B == LHS)) {
3501 if (A != LHS)
3502 std::swap(A, B); // A pred smax(A, B).
3503 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3504 // We analyze this as smax(A, B) swapped-pred A.
3506 } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
3507 (A == RHS || B == RHS)) {
3508 if (A != RHS)
3509 std::swap(A, B); // smin(A, B) pred A.
3510 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3511 // We analyze this as smax(-A, -B) swapped-pred -A.
3512 // Note that we do not need to actually form -A or -B thanks to EqP.
3514 } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
3515 (A == LHS || B == LHS)) {
3516 if (A != LHS)
3517 std::swap(A, B); // A pred smin(A, B).
3518 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3519 // We analyze this as smax(-A, -B) pred -A.
3520 // Note that we do not need to actually form -A or -B thanks to EqP.
3521 P = Pred;
3522 }
3524 // Cases correspond to "max(A, B) p A".
3525 switch (P) {
3526 default:
3527 break;
3528 case CmpInst::ICMP_EQ:
3529 case CmpInst::ICMP_SLE:
3530 // Equivalent to "A EqP B". This may be the same as the condition tested
3531 // in the max/min; if so, we can just return that.
3532 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3533 return V;
3534 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3535 return V;
3536 // Otherwise, see if "A EqP B" simplifies.
3537 if (MaxRecurse)
3538 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3539 return V;
3540 break;
3541 case CmpInst::ICMP_NE:
3542 case CmpInst::ICMP_SGT: {
3544 // Equivalent to "A InvEqP B". This may be the same as the condition
3545 // tested in the max/min; if so, we can just return that.
3546 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3547 return V;
3548 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3549 return V;
3550 // Otherwise, see if "A InvEqP B" simplifies.
3551 if (MaxRecurse)
3552 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3553 return V;
3554 break;
3555 }
3556 case CmpInst::ICMP_SGE:
3557 // Always true.
3558 return getTrue(ITy);
3559 case CmpInst::ICMP_SLT:
3560 // Always false.
3561 return getFalse(ITy);
3562 }
3563 }
3564
3565 // Unsigned variants on "max(a,b)>=a -> true".
3567 if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3568 if (A != RHS)
3569 std::swap(A, B); // umax(A, B) pred A.
3570 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3571 // We analyze this as umax(A, B) pred A.
3572 P = Pred;
3573 } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
3574 (A == LHS || B == LHS)) {
3575 if (A != LHS)
3576 std::swap(A, B); // A pred umax(A, B).
3577 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3578 // We analyze this as umax(A, B) swapped-pred A.
3580 } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3581 (A == RHS || B == RHS)) {
3582 if (A != RHS)
3583 std::swap(A, B); // umin(A, B) pred A.
3584 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3585 // We analyze this as umax(-A, -B) swapped-pred -A.
3586 // Note that we do not need to actually form -A or -B thanks to EqP.
3588 } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
3589 (A == LHS || B == LHS)) {
3590 if (A != LHS)
3591 std::swap(A, B); // A pred umin(A, B).
3592 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3593 // We analyze this as umax(-A, -B) pred -A.
3594 // Note that we do not need to actually form -A or -B thanks to EqP.
3595 P = Pred;
3596 }
3598 // Cases correspond to "max(A, B) p A".
3599 switch (P) {
3600 default:
3601 break;
3602 case CmpInst::ICMP_EQ:
3603 case CmpInst::ICMP_ULE:
3604 // Equivalent to "A EqP B". This may be the same as the condition tested
3605 // in the max/min; if so, we can just return that.
3606 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3607 return V;
3608 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3609 return V;
3610 // Otherwise, see if "A EqP B" simplifies.
3611 if (MaxRecurse)
3612 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3613 return V;
3614 break;
3615 case CmpInst::ICMP_NE:
3616 case CmpInst::ICMP_UGT: {
3618 // Equivalent to "A InvEqP B". This may be the same as the condition
3619 // tested in the max/min; if so, we can just return that.
3620 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3621 return V;
3622 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3623 return V;
3624 // Otherwise, see if "A InvEqP B" simplifies.
3625 if (MaxRecurse)
3626 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3627 return V;
3628 break;
3629 }
3630 case CmpInst::ICMP_UGE:
3631 return getTrue(ITy);
3632 case CmpInst::ICMP_ULT:
3633 return getFalse(ITy);
3634 }
3635 }
3636
3637 // Comparing 1 each of min/max with a common operand?
3638 // Canonicalize min operand to RHS.
3639 if (match(LHS, m_UMin(m_Value(), m_Value())) ||
3640 match(LHS, m_SMin(m_Value(), m_Value()))) {
3641 std::swap(LHS, RHS);
3642 Pred = ICmpInst::getSwappedPredicate(Pred);
3643 }
3644
3645 Value *C, *D;
3646 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
3647 match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
3648 (A == C || A == D || B == C || B == D)) {
3649 // smax(A, B) >=s smin(A, D) --> true
3650 if (Pred == CmpInst::ICMP_SGE)
3651 return getTrue(ITy);
3652 // smax(A, B) <s smin(A, D) --> false
3653 if (Pred == CmpInst::ICMP_SLT)
3654 return getFalse(ITy);
3655 } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3656 match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
3657 (A == C || A == D || B == C || B == D)) {
3658 // umax(A, B) >=u umin(A, D) --> true
3659 if (Pred == CmpInst::ICMP_UGE)
3660 return getTrue(ITy);
3661 // umax(A, B) <u umin(A, D) --> false
3662 if (Pred == CmpInst::ICMP_ULT)
3663 return getFalse(ITy);
3664 }
3665
3666 return nullptr;
3667}
3668
3670 Value *LHS, Value *RHS,
3671 const SimplifyQuery &Q) {
3672 // Gracefully handle instructions that have not been inserted yet.
3673 if (!Q.AC || !Q.CxtI)
3674 return nullptr;
3675
3676 for (Value *AssumeBaseOp : {LHS, RHS}) {
3677 for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
3678 if (!AssumeVH)
3679 continue;
3680
3681 CallInst *Assume = cast<CallInst>(AssumeVH);
3682 if (std::optional<bool> Imp = isImpliedCondition(
3683 Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL))
3684 if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT))
3685 return ConstantInt::get(getCompareTy(LHS), *Imp);
3686 }
3687 }
3688
3689 return nullptr;
3690}
3691
3693 Value *LHS, Value *RHS) {
3694 auto *II = dyn_cast<IntrinsicInst>(LHS);
3695 if (!II)
3696 return nullptr;
3697
3698 switch (II->getIntrinsicID()) {
3699 case Intrinsic::uadd_sat:
3700 // uadd.sat(X, Y) uge X, uadd.sat(X, Y) uge Y
3701 if (II->getArgOperand(0) == RHS || II->getArgOperand(1) == RHS) {
3702 if (Pred == ICmpInst::ICMP_UGE)
3704 if (Pred == ICmpInst::ICMP_ULT)
3706 }
3707 return nullptr;
3708 case Intrinsic::usub_sat:
3709 // usub.sat(X, Y) ule X
3710 if (II->getArgOperand(0) == RHS) {
3711 if (Pred == ICmpInst::ICMP_ULE)
3713 if (Pred == ICmpInst::ICMP_UGT)
3715 }
3716 return nullptr;
3717 default:
3718 return nullptr;
3719 }
3720}
3721
3722/// Helper method to get range from metadata or attribute.
3723static std::optional<ConstantRange> getRange(Value *V,
3724 const InstrInfoQuery &IIQ) {
3725 if (Instruction *I = dyn_cast<Instruction>(V))
3726 if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
3727 return getConstantRangeFromMetadata(*MD);
3728
3729 if (const Argument *A = dyn_cast<Argument>(V))
3730 return A->getRange();
3731 else if (const CallBase *CB = dyn_cast<CallBase>(V))
3732 return CB->getRange();
3733
3734 return std::nullopt;
3735}
3736
3737/// Given operands for an ICmpInst, see if we can fold the result.
3738/// If not, this returns null.
3739static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3740 const SimplifyQuery &Q, unsigned MaxRecurse) {
3741 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
3742 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3743
3744 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3745 if (Constant *CRHS = dyn_cast<Constant>(RHS))
3746 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3747
3748 // If we have a constant, make sure it is on the RHS.
3749 std::swap(LHS, RHS);
3750 Pred = CmpInst::getSwappedPredicate(Pred);
3751 }
3752 assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
3753
3754 Type *ITy = getCompareTy(LHS); // The return type.
3755
3756 // icmp poison, X -> poison
3757 if (isa<PoisonValue>(RHS))
3758 return PoisonValue::get(ITy);
3759
3760 // For EQ and NE, we can always pick a value for the undef to make the
3761 // predicate pass or fail, so we can return undef.
3762 // Matches behavior in llvm::ConstantFoldCompareInstruction.
3763 if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred))
3764 return UndefValue::get(ITy);
3765
3766 // icmp X, X -> true/false
3767 // icmp X, undef -> true/false because undef could be X.
3768 if (LHS == RHS || Q.isUndefValue(RHS))
3769 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3770
3771 if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3772 return V;
3773
3774 // TODO: Sink/common this with other potentially expensive calls that use
3775 // ValueTracking? See comment below for isKnownNonEqual().
3776 if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3777 return V;
3778
3779 if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
3780 return V;
3781
3782 // If both operands have range metadata, use the metadata
3783 // to simplify the comparison.
3784 if (std::optional<ConstantRange> RhsCr = getRange(RHS, Q.IIQ))
3785 if (std::optional<ConstantRange> LhsCr = getRange(LHS, Q.IIQ)) {
3786 if (LhsCr->icmp(Pred, *RhsCr))
3787 return ConstantInt::getTrue(ITy);
3788
3789 if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr))
3790 return ConstantInt::getFalse(ITy);
3791 }
3792
3793 // Compare of cast, for example (zext X) != 0 -> X != 0
3794 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3795 Instruction *LI = cast<CastInst>(LHS);
3796 Value *SrcOp = LI->getOperand(0);
3797 Type *SrcTy = SrcOp->getType();
3798 Type *DstTy = LI->getType();
3799
3800 // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3801 // if the integer type is the same size as the pointer type.
3802 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3803 Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3804 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3805 // Transfer the cast to the constant.
3806 if (Value *V = simplifyICmpInst(Pred, SrcOp,
3807 ConstantExpr::getIntToPtr(RHSC, SrcTy),
3808 Q, MaxRecurse - 1))
3809 return V;
3810 } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3811 if (RI->getOperand(0)->getType() == SrcTy)
3812 // Compare without the cast.
3813 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3814 MaxRecurse - 1))
3815 return V;
3816 }
3817 }
3818
3819 if (isa<ZExtInst>(LHS)) {
3820 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3821 // same type.
3822 if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3823 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3824 // Compare X and Y. Note that signed predicates become unsigned.
3825 if (Value *V =
3827 RI->getOperand(0), Q, MaxRecurse - 1))
3828 return V;
3829 }
3830 // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
3831 else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3832 if (SrcOp == RI->getOperand(0)) {
3833 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3834 return ConstantInt::getTrue(ITy);
3835 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3836 return ConstantInt::getFalse(ITy);
3837 }
3838 }
3839 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3840 // too. If not, then try to deduce the result of the comparison.
3841 else if (match(RHS, m_ImmConstant())) {
3842 Constant *C = dyn_cast<Constant>(RHS);
3843 assert(C != nullptr);
3844
3845 // Compute the constant that would happen if we truncated to SrcTy then
3846 // reextended to DstTy.
3847 Constant *Trunc =
3848 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3849 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3850 Constant *RExt =
3851 ConstantFoldCastOperand(CastInst::ZExt, Trunc, DstTy, Q.DL);
3852 assert(RExt && "Constant-fold of ImmConstant should not fail");
3853 Constant *AnyEq =
3854 ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3855 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3856
3857 // If the re-extended constant didn't change any of the elements then
3858 // this is effectively also a case of comparing two zero-extended
3859 // values.
3860 if (AnyEq->isAllOnesValue() && MaxRecurse)
3862 SrcOp, Trunc, Q, MaxRecurse - 1))
3863 return V;
3864
3865 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3866 // there. Use this to work out the result of the comparison.
3867 if (AnyEq->isNullValue()) {
3868 switch (Pred) {
3869 default:
3870 llvm_unreachable("Unknown ICmp predicate!");
3871 // LHS <u RHS.
3872 case ICmpInst::ICMP_EQ:
3873 case ICmpInst::ICMP_UGT:
3874 case ICmpInst::ICMP_UGE:
3875 return Constant::getNullValue(ITy);
3876
3877 case ICmpInst::ICMP_NE:
3878 case ICmpInst::ICMP_ULT:
3879 case ICmpInst::ICMP_ULE:
3880 return Constant::getAllOnesValue(ITy);
3881
3882 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
3883 // is non-negative then LHS <s RHS.
3884 case ICmpInst::ICMP_SGT:
3885 case ICmpInst::ICMP_SGE:
3887 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3888 Q.DL);
3889 case ICmpInst::ICMP_SLT:
3890 case ICmpInst::ICMP_SLE:
3892 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3893 Q.DL);
3894 }
3895 }
3896 }
3897 }
3898
3899 if (isa<SExtInst>(LHS)) {
3900 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3901 // same type.
3902 if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3903 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3904 // Compare X and Y. Note that the predicate does not change.
3905 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3906 MaxRecurse - 1))
3907 return V;
3908 }
3909 // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
3910 else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3911 if (SrcOp == RI->getOperand(0)) {
3912 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3913 return ConstantInt::getTrue(ITy);
3914 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3915 return ConstantInt::getFalse(ITy);
3916 }
3917 }
3918 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3919 // too. If not, then try to deduce the result of the comparison.
3920 else if (match(RHS, m_ImmConstant())) {
3921 Constant *C = cast<Constant>(RHS);
3922
3923 // Compute the constant that would happen if we truncated to SrcTy then
3924 // reextended to DstTy.
3925 Constant *Trunc =
3926 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3927 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3928 Constant *RExt =
3929 ConstantFoldCastOperand(CastInst::SExt, Trunc, DstTy, Q.DL);
3930 assert(RExt && "Constant-fold of ImmConstant should not fail");
3931 Constant *AnyEq =
3932 ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3933 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3934
3935 // If the re-extended constant didn't change then this is effectively
3936 // also a case of comparing two sign-extended values.
3937 if (AnyEq->isAllOnesValue() && MaxRecurse)
3938 if (Value *V =
3939 simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1))
3940 return V;
3941
3942 // Otherwise the upper bits of LHS are all equal, while RHS has varying
3943 // bits there. Use this to work out the result of the comparison.
3944 if (AnyEq->isNullValue()) {
3945 switch (Pred) {
3946 default:
3947 llvm_unreachable("Unknown ICmp predicate!");
3948 case ICmpInst::ICMP_EQ:
3949 return Constant::getNullValue(ITy);
3950 case ICmpInst::ICMP_NE:
3951 return Constant::getAllOnesValue(ITy);
3952
3953 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
3954 // LHS >s RHS.
3955 case ICmpInst::ICMP_SGT:
3956 case ICmpInst::ICMP_SGE:
3957 return ConstantExpr::getICmp(ICmpInst::ICMP_SLT, C,
3958 Constant::getNullValue(C->getType()));
3959 case ICmpInst::ICMP_SLT:
3960 case ICmpInst::ICMP_SLE:
3961 return ConstantExpr::getICmp(ICmpInst::ICMP_SGE, C,
3962 Constant::getNullValue(C->getType()));
3963
3964 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
3965 // LHS >u RHS.
3966 case ICmpInst::ICMP_UGT:
3967 case ICmpInst::ICMP_UGE:
3968 // Comparison is true iff the LHS <s 0.
3969 if (MaxRecurse)
3970 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
3971 Constant::getNullValue(SrcTy), Q,
3972 MaxRecurse - 1))
3973 return V;
3974 break;
3975 case ICmpInst::ICMP_ULT:
3976 case ICmpInst::ICMP_ULE:
3977 // Comparison is true iff the LHS >=s 0.
3978 if (MaxRecurse)
3979 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
3980 Constant::getNullValue(SrcTy), Q,
3981 MaxRecurse - 1))
3982 return V;
3983 break;
3984 }
3985 }
3986 }
3987 }
3988 }
3989
3990 // icmp eq|ne X, Y -> false|true if X != Y
3991 // This is potentially expensive, and we have already computedKnownBits for
3992 // compares with 0 above here, so only try this for a non-zero compare.
3993 if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero()) &&
3994 isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo)) {
3995 return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy);
3996 }
3997
3998 if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
3999 return V;
4000
4001 if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
4002 return V;
4003
4005 return V;
4007 ICmpInst::getSwappedPredicate(Pred), RHS, LHS))
4008 return V;
4009
4010 if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q))
4011 return V;
4012
4013 if (std::optional<bool> Res =
4014 isImpliedByDomCondition(Pred, LHS, RHS, Q.CxtI, Q.DL))
4015 return ConstantInt::getBool(ITy, *Res);
4016
4017 // Simplify comparisons of related pointers using a powerful, recursive
4018 // GEP-walk when we have target data available..
4019 if (LHS->getType()->isPointerTy())
4020 if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
4021 return C;
4022 if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
4023 if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
4024 if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4025 Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4026 Q.DL.getTypeSizeInBits(CLHS->getType()))
4027 if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
4028 CRHS->getPointerOperand(), Q))
4029 return C;
4030
4031 // If the comparison is with the result of a select instruction, check whether
4032 // comparing with either branch of the select always yields the same value.
4033 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4034 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4035 return V;
4036
4037 // If the comparison is with the result of a phi instruction, check whether
4038 // doing the compare with each incoming phi value yields a common result.
4039 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4040 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4041 return V;
4042
4043 return nullptr;
4044}
4045
4046Value *llvm::simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4047 const SimplifyQuery &Q) {
4048 return ::simplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
4049}
4050
4051/// Given operands for an FCmpInst, see if we can fold the result.
4052/// If not, this returns null.
4053static Value *simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4054 FastMathFlags FMF, const SimplifyQuery &Q,
4055 unsigned MaxRecurse) {
4056 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
4057 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
4058
4059 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
4060 if (Constant *CRHS = dyn_cast<Constant>(RHS))
4061 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI,
4062 Q.CxtI);
4063
4064 // If we have a constant, make sure it is on the RHS.
4065 std::swap(LHS, RHS);
4066 Pred = CmpInst::getSwappedPredicate(Pred);
4067 }
4068
4069 // Fold trivial predicates.
4071 if (Pred == FCmpInst::FCMP_FALSE)
4072 return getFalse(RetTy);
4073 if (Pred == FCmpInst::FCMP_TRUE)
4074 return getTrue(RetTy);
4075
4076 // fcmp pred x, poison and fcmp pred poison, x
4077 // fold to poison
4078 if (isa<PoisonValue>(LHS) || isa<PoisonValue>(RHS))
4079 return PoisonValue::get(RetTy);
4080
4081 // fcmp pred x, undef and fcmp pred undef, x
4082 // fold to true if unordered, false if ordered
4083 if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) {
4084 // Choosing NaN for the undef will always make unordered comparison succeed
4085 // and ordered comparison fail.
4086 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4087 }
4088
4089 // fcmp x,x -> true/false. Not all compares are foldable.
4090 if (LHS == RHS) {
4091 if (CmpInst::isTrueWhenEqual(Pred))
4092 return getTrue(RetTy);
4093 if (CmpInst::isFalseWhenEqual(Pred))
4094 return getFalse(RetTy);
4095 }
4096
4097 // Fold (un)ordered comparison if we can determine there are no NaNs.
4098 //
4099 // This catches the 2 variable input case, constants are handled below as a
4100 // class-like compare.
4101 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4102 if (FMF.noNaNs() || (isKnownNeverNaN(RHS, /*Depth=*/0, Q) &&
4103 isKnownNeverNaN(LHS, /*Depth=*/0, Q)))
4104 return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
4105 }
4106
4107 const APFloat *C = nullptr;
4109 std::optional<KnownFPClass> FullKnownClassLHS;
4110
4111 // Lazily compute the possible classes for LHS. Avoid computing it twice if
4112 // RHS is a 0.
4113 auto computeLHSClass = [=, &FullKnownClassLHS](FPClassTest InterestedFlags =
4114 fcAllFlags) {
4115 if (FullKnownClassLHS)
4116 return *FullKnownClassLHS;
4117 return computeKnownFPClass(LHS, FMF, InterestedFlags, 0, Q);
4118 };
4119
4120 if (C && Q.CxtI) {
4121 // Fold out compares that express a class test.
4122 //
4123 // FIXME: Should be able to perform folds without context
4124 // instruction. Always pass in the context function?
4125
4126 const Function *ParentF = Q.CxtI->getFunction();
4127 auto [ClassVal, ClassTest] = fcmpToClassTest(Pred, *ParentF, LHS, C);
4128 if (ClassVal) {
4129 FullKnownClassLHS = computeLHSClass();
4130 if ((FullKnownClassLHS->KnownFPClasses & ClassTest) == fcNone)
4131 return getFalse(RetTy);
4132 if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) == fcNone)
4133 return getTrue(RetTy);
4134 }
4135 }
4136
4137 // Handle fcmp with constant RHS.
4138 if (C) {
4139 // TODO: If we always required a context function, we wouldn't need to
4140 // special case nans.
4141 if (C->isNaN())
4142 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4143
4144 // TODO: Need version fcmpToClassTest which returns implied class when the
4145 // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but
4146 // isn't implementable as a class call.
4147 if (C->isNegative() && !C->isNegZero()) {
4149
4150 // TODO: We can catch more cases by using a range check rather than
4151 // relying on CannotBeOrderedLessThanZero.
4152 switch (Pred) {
4153 case FCmpInst::FCMP_UGE:
4154 case FCmpInst::FCMP_UGT:
4155 case FCmpInst::FCMP_UNE: {
4156 KnownFPClass KnownClass = computeLHSClass(Interested);
4157
4158 // (X >= 0) implies (X > C) when (C < 0)
4159 if (KnownClass.cannotBeOrderedLessThanZero())
4160 return getTrue(RetTy);
4161 break;
4162 }
4163 case FCmpInst::FCMP_OEQ:
4164 case FCmpInst::FCMP_OLE:
4165 case FCmpInst::FCMP_OLT: {
4166 KnownFPClass KnownClass = computeLHSClass(Interested);
4167
4168 // (X >= 0) implies !(X < C) when (C < 0)
4169 if (KnownClass.cannotBeOrderedLessThanZero())
4170 return getFalse(RetTy);
4171 break;
4172 }
4173 default:
4174 break;
4175 }
4176 }
4177 // Check comparison of [minnum/maxnum with constant] with other constant.
4178 const APFloat *C2;
4179 if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
4180 *C2 < *C) ||
4181 (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) &&
4182 *C2 > *C)) {
4183 bool IsMaxNum =
4184 cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
4185 // The ordered relationship and minnum/maxnum guarantee that we do not
4186 // have NaN constants, so ordered/unordered preds are handled the same.
4187 switch (Pred) {
4188 case FCmpInst::FCMP_OEQ:
4189 case FCmpInst::FCMP_UEQ:
4190 // minnum(X, LesserC) == C --> false
4191 // maxnum(X, GreaterC) == C --> false
4192 return getFalse(RetTy);
4193 case FCmpInst::FCMP_ONE:
4194 case FCmpInst::FCMP_UNE:
4195 // minnum(X, LesserC) != C --> true
4196 // maxnum(X, GreaterC) != C --> true
4197 return getTrue(RetTy);
4198 case FCmpInst::FCMP_OGE:
4199 case FCmpInst::FCMP_UGE:
4200 case FCmpInst::FCMP_OGT:
4201 case FCmpInst::FCMP_UGT:
4202 // minnum(X, LesserC) >= C --> false
4203 // minnum(X, LesserC) > C --> false
4204 // maxnum(X, GreaterC) >= C --> true
4205 // maxnum(X, GreaterC) > C --> true
4206 return ConstantInt::get(RetTy, IsMaxNum);
4207 case FCmpInst::FCMP_OLE:
4208 case FCmpInst::FCMP_ULE:
4209 case FCmpInst::FCMP_OLT:
4210 case FCmpInst::FCMP_ULT:
4211 // minnum(X, LesserC) <= C --> true
4212 // minnum(X, LesserC) < C --> true
4213 // maxnum(X, GreaterC) <= C --> false
4214 // maxnum(X, GreaterC) < C --> false
4215 return ConstantInt::get(RetTy, !IsMaxNum);
4216 default:
4217 // TRUE/FALSE/ORD/UNO should be handled before this.
4218 llvm_unreachable("Unexpected fcmp predicate");
4219 }
4220 }
4221 }
4222
4223 // TODO: Could fold this with above if there were a matcher which returned all
4224 // classes in a non-splat vector.
4225 if (match(RHS, m_AnyZeroFP())) {
4226 switch (Pred) {
4227 case FCmpInst::FCMP_OGE:
4228 case FCmpInst::FCMP_ULT: {
4230 if (!FMF.noNaNs())
4231 Interested |= fcNan;
4232
4233 KnownFPClass Known = computeLHSClass(Interested);
4234
4235 // Positive or zero X >= 0.0 --> true
4236 // Positive or zero X < 0.0 --> false
4237 if ((FMF.noNaNs() || Known.isKnownNeverNaN()) &&
4239 return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy) : getFalse(RetTy);
4240 break;
4241 }
4242 case FCmpInst::FCMP_UGE:
4243 case FCmpInst::FCMP_OLT: {
4245 KnownFPClass Known = computeLHSClass(Interested);
4246
4247 // Positive or zero or nan X >= 0.0 --> true
4248 // Positive or zero or nan X < 0.0 --> false
4249 if (Known.cannotBeOrderedLessThanZero())
4250 return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy) : getFalse(RetTy);
4251 break;
4252 }
4253 default:
4254 break;
4255 }
4256 }
4257
4258 // If the comparison is with the result of a select instruction, check whether
4259 // comparing with either branch of the select always yields the same value.
4260 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4261 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4262 return V;
4263
4264 // If the comparison is with the result of a phi instruction, check whether
4265 // doing the compare with each incoming phi value yields a common result.
4266 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4267 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4268 return V;
4269
4270 return nullptr;
4271}
4272
4273Value *llvm::simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4274 FastMathFlags FMF, const SimplifyQuery &Q) {
4275 return ::simplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);