LLVM 22.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"
31#include "llvm/Analysis/Loads.h"
39#include "llvm/IR/DataLayout.h"
40#include "llvm/IR/Dominators.h"
41#include "llvm/IR/InstrTypes.h"
43#include "llvm/IR/Operator.h"
45#include "llvm/IR/Statepoint.h"
48#include <algorithm>
49#include <optional>
50using namespace llvm;
51using namespace llvm::PatternMatch;
52
53#define DEBUG_TYPE "instsimplify"
54
55enum { RecursionLimit = 3 };
56
57STATISTIC(NumExpand, "Number of expansions");
58STATISTIC(NumReassoc, "Number of reassociations");
59
60static Value *simplifyAndInst(Value *, Value *, const SimplifyQuery &,
61 unsigned);
62static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned);
63static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &,
64 const SimplifyQuery &, unsigned);
65static Value *simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &,
66 unsigned);
67static Value *simplifyBinOp(unsigned, Value *, Value *, const FastMathFlags &,
68 const SimplifyQuery &, unsigned);
70 const SimplifyQuery &, unsigned);
72 const SimplifyQuery &Q, unsigned MaxRecurse);
73static Value *simplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned);
74static Value *simplifyXorInst(Value *, Value *, const SimplifyQuery &,
75 unsigned);
76static Value *simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &,
77 unsigned);
79 GEPNoWrapFlags, const SimplifyQuery &, unsigned);
81 const SimplifyQuery &, unsigned);
83 ArrayRef<Value *> NewOps,
84 const SimplifyQuery &SQ,
85 unsigned MaxRecurse);
86
87/// For a boolean type or a vector of boolean type, return false or a vector
88/// with every element false.
89static Constant *getFalse(Type *Ty) { return ConstantInt::getFalse(Ty); }
90
91/// For a boolean type or a vector of boolean type, return true or a vector
92/// with every element true.
93static Constant *getTrue(Type *Ty) { return ConstantInt::getTrue(Ty); }
94
95/// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
96static bool isSameCompare(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS) {
97 CmpInst *Cmp = dyn_cast<CmpInst>(V);
98 if (!Cmp)
99 return false;
100 CmpInst::Predicate CPred = Cmp->getPredicate();
101 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
102 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
103 return true;
104 return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
105 CRHS == LHS;
106}
107
108/// Simplify comparison with true or false branch of select:
109/// %sel = select i1 %cond, i32 %tv, i32 %fv
110/// %cmp = icmp sle i32 %sel, %rhs
111/// Compose new comparison by substituting %sel with either %tv or %fv
112/// and see if it simplifies.
114 Value *Cond, const SimplifyQuery &Q,
115 unsigned MaxRecurse, Constant *TrueOrFalse) {
116 Value *SimplifiedCmp = simplifyCmpInst(Pred, LHS, RHS, Q, MaxRecurse);
117 if (SimplifiedCmp == Cond) {
118 // %cmp simplified to the select condition (%cond).
119 return TrueOrFalse;
120 } else if (!SimplifiedCmp && isSameCompare(Cond, Pred, LHS, RHS)) {
121 // It didn't simplify. However, if composed comparison is equivalent
122 // to the select condition (%cond) then we can replace it.
123 return TrueOrFalse;
124 }
125 return SimplifiedCmp;
126}
127
128/// Simplify comparison with true branch of select
130 Value *Cond, const SimplifyQuery &Q,
131 unsigned MaxRecurse) {
132 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
133 getTrue(Cond->getType()));
134}
135
136/// Simplify comparison with false branch of select
138 Value *Cond, const SimplifyQuery &Q,
139 unsigned MaxRecurse) {
140 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
141 getFalse(Cond->getType()));
142}
143
144/// We know comparison with both branches of select can be simplified, but they
145/// are not equal. This routine handles some logical simplifications.
147 Value *Cond,
148 const SimplifyQuery &Q,
149 unsigned MaxRecurse) {
150 // If the false value simplified to false, then the result of the compare
151 // is equal to "Cond && TCmp". This also catches the case when the false
152 // value simplified to false and the true value to true, returning "Cond".
153 // Folding select to and/or isn't poison-safe in general; impliesPoison
154 // checks whether folding it does not convert a well-defined value into
155 // poison.
156 if (match(FCmp, m_Zero()) && impliesPoison(TCmp, Cond))
157 if (Value *V = simplifyAndInst(Cond, TCmp, Q, MaxRecurse))
158 return V;
159 // If the true value simplified to true, then the result of the compare
160 // is equal to "Cond || FCmp".
161 if (match(TCmp, m_One()) && impliesPoison(FCmp, Cond))
162 if (Value *V = simplifyOrInst(Cond, FCmp, Q, MaxRecurse))
163 return V;
164 // Finally, if the false value simplified to true and the true value to
165 // false, then the result of the compare is equal to "!Cond".
166 if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
167 if (Value *V = simplifyXorInst(
168 Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse))
169 return V;
170 return nullptr;
171}
172
173/// Does the given value dominate the specified phi node?
174static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
176 if (!I)
177 // Arguments and constants dominate all instructions.
178 return true;
179
180 // If we have a DominatorTree then do a precise test.
181 if (DT)
182 return DT->dominates(I, P);
183
184 // Otherwise, if the instruction is in the entry block and is not an invoke,
185 // then it obviously dominates all phi nodes.
186 if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
188 return true;
189
190 return false;
191}
192
193/// Try to simplify a binary operator of form "V op OtherOp" where V is
194/// "(B0 opex B1)" by distributing 'op' across 'opex' as
195/// "(B0 op OtherOp) opex (B1 op OtherOp)".
197 Value *OtherOp, Instruction::BinaryOps OpcodeToExpand,
198 const SimplifyQuery &Q, unsigned MaxRecurse) {
199 auto *B = dyn_cast<BinaryOperator>(V);
200 if (!B || B->getOpcode() != OpcodeToExpand)
201 return nullptr;
202 Value *B0 = B->getOperand(0), *B1 = B->getOperand(1);
203 Value *L =
204 simplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), MaxRecurse);
205 if (!L)
206 return nullptr;
207 Value *R =
208 simplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), MaxRecurse);
209 if (!R)
210 return nullptr;
211
212 // Does the expanded pair of binops simplify to the existing binop?
213 if ((L == B0 && R == B1) ||
214 (Instruction::isCommutative(OpcodeToExpand) && L == B1 && R == B0)) {
215 ++NumExpand;
216 return B;
217 }
218
219 // Otherwise, return "L op' R" if it simplifies.
220 Value *S = simplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse);
221 if (!S)
222 return nullptr;
223
224 ++NumExpand;
225 return S;
226}
227
228/// Try to simplify binops of form "A op (B op' C)" or the commuted variant by
229/// distributing op over op'.
231 Value *R,
232 Instruction::BinaryOps OpcodeToExpand,
233 const SimplifyQuery &Q,
234 unsigned MaxRecurse) {
235 // Recursion is always used, so bail out at once if we already hit the limit.
236 if (!MaxRecurse--)
237 return nullptr;
238
239 if (Value *V = expandBinOp(Opcode, L, R, OpcodeToExpand, Q, MaxRecurse))
240 return V;
241 if (Value *V = expandBinOp(Opcode, R, L, OpcodeToExpand, Q, MaxRecurse))
242 return V;
243 return nullptr;
244}
245
246/// Generic simplifications for associative binary operations.
247/// Returns the simpler value, or null if none was found.
249 Value *LHS, Value *RHS,
250 const SimplifyQuery &Q,
251 unsigned MaxRecurse) {
252 assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
253
254 // Recursion is always used, so bail out at once if we already hit the limit.
255 if (!MaxRecurse--)
256 return nullptr;
257
260
261 // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
262 if (Op0 && Op0->getOpcode() == Opcode) {
263 Value *A = Op0->getOperand(0);
264 Value *B = Op0->getOperand(1);
265 Value *C = RHS;
266
267 // Does "B op C" simplify?
268 if (Value *V = simplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
269 // It does! Return "A op V" if it simplifies or is already available.
270 // If V equals B then "A op V" is just the LHS.
271 if (V == B)
272 return LHS;
273 // Otherwise return "A op V" if it simplifies.
274 if (Value *W = simplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
275 ++NumReassoc;
276 return W;
277 }
278 }
279 }
280
281 // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
282 if (Op1 && Op1->getOpcode() == Opcode) {
283 Value *A = LHS;
284 Value *B = Op1->getOperand(0);
285 Value *C = Op1->getOperand(1);
286
287 // Does "A op B" simplify?
288 if (Value *V = simplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
289 // It does! Return "V op C" if it simplifies or is already available.
290 // If V equals B then "V op C" is just the RHS.
291 if (V == B)
292 return RHS;
293 // Otherwise return "V op C" if it simplifies.
294 if (Value *W = simplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
295 ++NumReassoc;
296 return W;
297 }
298 }
299 }
300
301 // The remaining transforms require commutativity as well as associativity.
302 if (!Instruction::isCommutative(Opcode))
303 return nullptr;
304
305 // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
306 if (Op0 && Op0->getOpcode() == Opcode) {
307 Value *A = Op0->getOperand(0);
308 Value *B = Op0->getOperand(1);
309 Value *C = RHS;
310
311 // Does "C op A" simplify?
312 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
313 // It does! Return "V op B" if it simplifies or is already available.
314 // If V equals A then "V op B" is just the LHS.
315 if (V == A)
316 return LHS;
317 // Otherwise return "V op B" if it simplifies.
318 if (Value *W = simplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
319 ++NumReassoc;
320 return W;
321 }
322 }
323 }
324
325 // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
326 if (Op1 && Op1->getOpcode() == Opcode) {
327 Value *A = LHS;
328 Value *B = Op1->getOperand(0);
329 Value *C = Op1->getOperand(1);
330
331 // Does "C op A" simplify?
332 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
333 // It does! Return "B op V" if it simplifies or is already available.
334 // If V equals C then "B op V" is just the RHS.
335 if (V == C)
336 return RHS;
337 // Otherwise return "B op V" if it simplifies.
338 if (Value *W = simplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
339 ++NumReassoc;
340 return W;
341 }
342 }
343 }
344
345 return nullptr;
346}
347
348/// In the case of a binary operation with a select instruction as an operand,
349/// try to simplify the binop by seeing whether evaluating it on both branches
350/// of the select results in the same value. Returns the common value if so,
351/// otherwise returns null.
353 Value *RHS, const SimplifyQuery &Q,
354 unsigned MaxRecurse) {
355 // Recursion is always used, so bail out at once if we already hit the limit.
356 if (!MaxRecurse--)
357 return nullptr;
358
359 SelectInst *SI;
360 if (isa<SelectInst>(LHS)) {
362 } else {
363 assert(isa<SelectInst>(RHS) && "No select instruction operand!");
365 }
366
367 // Evaluate the BinOp on the true and false branches of the select.
368 Value *TV;
369 Value *FV;
370 if (SI == LHS) {
371 TV = simplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
372 FV = simplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
373 } else {
374 TV = simplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
375 FV = simplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
376 }
377
378 // If they simplified to the same value, then return the common value.
379 // If they both failed to simplify then return null.
380 if (TV == FV)
381 return TV;
382
383 // If one branch simplified to undef, return the other one.
384 if (TV && Q.isUndefValue(TV))
385 return FV;
386 if (FV && Q.isUndefValue(FV))
387 return TV;
388
389 // If applying the operation did not change the true and false select values,
390 // then the result of the binop is the select itself.
391 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
392 return SI;
393
394 // If one branch simplified and the other did not, and the simplified
395 // value is equal to the unsimplified one, return the simplified value.
396 // For example, select (cond, X, X & Z) & Z -> X & Z.
397 if ((FV && !TV) || (TV && !FV)) {
398 // Check that the simplified value has the form "X op Y" where "op" is the
399 // same as the original operation.
400 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
401 if (Simplified && Simplified->getOpcode() == unsigned(Opcode) &&
402 !Simplified->hasPoisonGeneratingFlags()) {
403 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
404 // We already know that "op" is the same as for the simplified value. See
405 // if the operands match too. If so, return the simplified value.
406 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
407 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
408 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
409 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
410 Simplified->getOperand(1) == UnsimplifiedRHS)
411 return Simplified;
412 if (Simplified->isCommutative() &&
413 Simplified->getOperand(1) == UnsimplifiedLHS &&
414 Simplified->getOperand(0) == UnsimplifiedRHS)
415 return Simplified;
416 }
417 }
418
419 return nullptr;
420}
421
422/// In the case of a comparison with a select instruction, try to simplify the
423/// comparison by seeing whether both branches of the select result in the same
424/// value. Returns the common value if so, otherwise returns null.
425/// For example, if we have:
426/// %tmp = select i1 %cmp, i32 1, i32 2
427/// %cmp1 = icmp sle i32 %tmp, 3
428/// We can simplify %cmp1 to true, because both branches of select are
429/// less than 3. We compose new comparison by substituting %tmp with both
430/// branches of select and see if it can be simplified.
432 const SimplifyQuery &Q, unsigned MaxRecurse) {
433 // Recursion is always used, so bail out at once if we already hit the limit.
434 if (!MaxRecurse--)
435 return nullptr;
436
437 // Make sure the select is on the LHS.
438 if (!isa<SelectInst>(LHS)) {
439 std::swap(LHS, RHS);
440 Pred = CmpInst::getSwappedPredicate(Pred);
441 }
442 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
444 Value *Cond = SI->getCondition();
445 Value *TV = SI->getTrueValue();
446 Value *FV = SI->getFalseValue();
447
448 // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
449 // Does "cmp TV, RHS" simplify?
450 Value *TCmp = simplifyCmpSelTrueCase(Pred, TV, RHS, Cond, Q, MaxRecurse);
451 if (!TCmp)
452 return nullptr;
453
454 // Does "cmp FV, RHS" simplify?
455 Value *FCmp = simplifyCmpSelFalseCase(Pred, FV, RHS, Cond, Q, MaxRecurse);
456 if (!FCmp)
457 return nullptr;
458
459 // If both sides simplified to the same value, then use it as the result of
460 // the original comparison.
461 if (TCmp == FCmp)
462 return TCmp;
463
464 // The remaining cases only make sense if the select condition has the same
465 // type as the result of the comparison, so bail out if this is not so.
466 if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy())
467 return handleOtherCmpSelSimplifications(TCmp, FCmp, Cond, Q, MaxRecurse);
468
469 return nullptr;
470}
471
472/// In the case of a binary operation with an operand that is a PHI instruction,
473/// try to simplify the binop by seeing whether evaluating it on the incoming
474/// phi values yields the same result for every value. If so returns the common
475/// value, otherwise returns null.
477 Value *RHS, const SimplifyQuery &Q,
478 unsigned MaxRecurse) {
479 // Recursion is always used, so bail out at once if we already hit the limit.
480 if (!MaxRecurse--)
481 return nullptr;
482
483 PHINode *PI;
484 if (isa<PHINode>(LHS)) {
485 PI = cast<PHINode>(LHS);
486 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
487 if (!valueDominatesPHI(RHS, PI, Q.DT))
488 return nullptr;
489 } else {
490 assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
491 PI = cast<PHINode>(RHS);
492 // Bail out if LHS and the phi may be mutually interdependent due to a loop.
493 if (!valueDominatesPHI(LHS, PI, Q.DT))
494 return nullptr;
495 }
496
497 // Evaluate the BinOp on the incoming phi values.
498 Value *CommonValue = nullptr;
499 for (Use &Incoming : PI->incoming_values()) {
500 // If the incoming value is the phi node itself, it can safely be skipped.
501 if (Incoming == PI)
502 continue;
504 Value *V = PI == LHS
505 ? simplifyBinOp(Opcode, Incoming, RHS,
506 Q.getWithInstruction(InTI), MaxRecurse)
507 : simplifyBinOp(Opcode, LHS, Incoming,
508 Q.getWithInstruction(InTI), MaxRecurse);
509 // If the operation failed to simplify, or simplified to a different value
510 // to previously, then give up.
511 if (!V || (CommonValue && V != CommonValue))
512 return nullptr;
513 CommonValue = V;
514 }
515
516 return CommonValue;
517}
518
519/// In the case of a comparison with a PHI instruction, try to simplify the
520/// comparison by seeing whether comparing with all of the incoming phi values
521/// yields the same result every time. If so returns the common result,
522/// otherwise returns null.
524 const SimplifyQuery &Q, unsigned MaxRecurse) {
525 // Recursion is always used, so bail out at once if we already hit the limit.
526 if (!MaxRecurse--)
527 return nullptr;
528
529 // Make sure the phi is on the LHS.
530 if (!isa<PHINode>(LHS)) {
531 std::swap(LHS, RHS);
532 Pred = CmpInst::getSwappedPredicate(Pred);
533 }
534 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
536
537 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
538 if (!valueDominatesPHI(RHS, PI, Q.DT))
539 return nullptr;
540
541 // Evaluate the BinOp on the incoming phi values.
542 Value *CommonValue = nullptr;
543 for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
546 // If the incoming value is the phi node itself, it can safely be skipped.
547 if (Incoming == PI)
548 continue;
549 // Change the context instruction to the "edge" that flows into the phi.
550 // This is important because that is where incoming is actually "evaluated"
551 // even though it is used later somewhere else.
553 MaxRecurse);
554 // If the operation failed to simplify, or simplified to a different value
555 // to previously, then give up.
556 if (!V || (CommonValue && V != CommonValue))
557 return nullptr;
558 CommonValue = V;
559 }
560
561 return CommonValue;
562}
563
565 Value *&Op0, Value *&Op1,
566 const SimplifyQuery &Q) {
567 if (auto *CLHS = dyn_cast<Constant>(Op0)) {
568 if (auto *CRHS = dyn_cast<Constant>(Op1)) {
569 switch (Opcode) {
570 default:
571 break;
572 case Instruction::FAdd:
573 case Instruction::FSub:
574 case Instruction::FMul:
575 case Instruction::FDiv:
576 case Instruction::FRem:
577 if (Q.CxtI != nullptr)
578 return ConstantFoldFPInstOperands(Opcode, CLHS, CRHS, Q.DL, Q.CxtI);
579 }
580 return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
581 }
582
583 // Canonicalize the constant to the RHS if this is a commutative operation.
584 if (Instruction::isCommutative(Opcode))
585 std::swap(Op0, Op1);
586 }
587 return nullptr;
588}
589
590/// Given operands for an Add, see if we can fold the result.
591/// If not, this returns null.
592static Value *simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
593 const SimplifyQuery &Q, unsigned MaxRecurse) {
594 if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q))
595 return C;
596
597 // X + poison -> poison
598 if (isa<PoisonValue>(Op1))
599 return Op1;
600
601 // X + undef -> undef
602 if (Q.isUndefValue(Op1))
603 return Op1;
604
605 // X + 0 -> X
606 if (match(Op1, m_Zero()))
607 return Op0;
608
609 // If two operands are negative, return 0.
610 if (isKnownNegation(Op0, Op1))
611 return Constant::getNullValue(Op0->getType());
612
613 // X + (Y - X) -> Y
614 // (Y - X) + X -> Y
615 // Eg: X + -X -> 0
616 Value *Y = nullptr;
617 if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
618 match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
619 return Y;
620
621 // X + ~X -> -1 since ~X = -X-1
622 Type *Ty = Op0->getType();
623 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
624 return Constant::getAllOnesValue(Ty);
625
626 // add nsw/nuw (xor Y, signmask), signmask --> Y
627 // The no-wrapping add guarantees that the top bit will be set by the add.
628 // Therefore, the xor must be clearing the already set sign bit of Y.
629 if ((IsNSW || IsNUW) && match(Op1, m_SignMask()) &&
630 match(Op0, m_Xor(m_Value(Y), m_SignMask())))
631 return Y;
632
633 // add nuw %x, -1 -> -1, because %x can only be 0.
634 if (IsNUW && match(Op1, m_AllOnes()))
635 return Op1; // Which is -1.
636
637 /// i1 add -> xor.
638 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
639 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
640 return V;
641
642 // Try some generic simplifications for associative operations.
643 if (Value *V =
644 simplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, MaxRecurse))
645 return V;
646
647 // Threading Add over selects and phi nodes is pointless, so don't bother.
648 // Threading over the select in "A + select(cond, B, C)" means evaluating
649 // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
650 // only if B and C are equal. If B and C are equal then (since we assume
651 // that operands have already been simplified) "select(cond, B, C)" should
652 // have been simplified to the common value of B and C already. Analysing
653 // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
654 // for threading over phi nodes.
655
656 return nullptr;
657}
658
659Value *llvm::simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
660 const SimplifyQuery &Query) {
661 return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit);
662}
663
664/// Compute the base pointer and cumulative constant offsets for V.
665///
666/// This strips all constant offsets off of V, leaving it the base pointer, and
667/// accumulates the total constant offset applied in the returned constant.
668/// It returns zero if there are no constant offsets applied.
669///
670/// This is very similar to stripAndAccumulateConstantOffsets(), except it
671/// normalizes the offset bitwidth to the stripped pointer type, not the
672/// original pointer type.
674 bool AllowNonInbounds = false) {
675 assert(V->getType()->isPtrOrPtrVectorTy());
676
677 APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
678 V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
679 // As that strip may trace through `addrspacecast`, need to sext or trunc
680 // the offset calculated.
681 return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType()));
682}
683
684/// Compute the constant difference between two pointer values.
685/// If the difference is not a constant, returns zero.
687 Value *RHS) {
690
691 // If LHS and RHS are not related via constant offsets to the same base
692 // value, there is nothing we can do here.
693 if (LHS != RHS)
694 return nullptr;
695
696 // Otherwise, the difference of LHS - RHS can be computed as:
697 // LHS - RHS
698 // = (LHSOffset + Base) - (RHSOffset + Base)
699 // = LHSOffset - RHSOffset
700 Constant *Res = ConstantInt::get(LHS->getContext(), LHSOffset - RHSOffset);
701 if (auto *VecTy = dyn_cast<VectorType>(LHS->getType()))
702 Res = ConstantVector::getSplat(VecTy->getElementCount(), Res);
703 return Res;
704}
705
706/// Test if there is a dominating equivalence condition for the
707/// two operands. If there is, try to reduce the binary operation
708/// between the two operands.
709/// Example: Op0 - Op1 --> 0 when Op0 == Op1
710static Value *simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1,
711 const SimplifyQuery &Q, unsigned MaxRecurse) {
712 // Recursive run it can not get any benefit
713 if (MaxRecurse != RecursionLimit)
714 return nullptr;
715
716 std::optional<bool> Imp =
718 if (Imp && *Imp) {
719 Type *Ty = Op0->getType();
720 switch (Opcode) {
721 case Instruction::Sub:
722 case Instruction::Xor:
723 case Instruction::URem:
724 case Instruction::SRem:
725 return Constant::getNullValue(Ty);
726
727 case Instruction::SDiv:
728 case Instruction::UDiv:
729 return ConstantInt::get(Ty, 1);
730
731 case Instruction::And:
732 case Instruction::Or:
733 // Could be either one - choose Op1 since that's more likely a constant.
734 return Op1;
735 default:
736 break;
737 }
738 }
739 return nullptr;
740}
741
742/// Given operands for a Sub, see if we can fold the result.
743/// If not, this returns null.
744static Value *simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
745 const SimplifyQuery &Q, unsigned MaxRecurse) {
746 if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
747 return C;
748
749 // X - poison -> poison
750 // poison - X -> poison
751 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
752 return PoisonValue::get(Op0->getType());
753
754 // X - undef -> undef
755 // undef - X -> undef
756 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
757 return UndefValue::get(Op0->getType());
758
759 // X - 0 -> X
760 if (match(Op1, m_Zero()))
761 return Op0;
762
763 // X - X -> 0
764 if (Op0 == Op1)
765 return Constant::getNullValue(Op0->getType());
766
767 // Is this a negation?
768 if (match(Op0, m_Zero())) {
769 // 0 - X -> 0 if the sub is NUW.
770 if (IsNUW)
771 return Constant::getNullValue(Op0->getType());
772
773 KnownBits Known = computeKnownBits(Op1, Q);
774 if (Known.Zero.isMaxSignedValue()) {
775 // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
776 // Op1 must be 0 because negating the minimum signed value is undefined.
777 if (IsNSW)
778 return Constant::getNullValue(Op0->getType());
779
780 // 0 - X -> X if X is 0 or the minimum signed value.
781 return Op1;
782 }
783 }
784
785 // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
786 // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
787 Value *X = nullptr, *Y = nullptr, *Z = Op1;
788 if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
789 // See if "V === Y - Z" simplifies.
790 if (Value *V = simplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1))
791 // It does! Now see if "X + V" simplifies.
792 if (Value *W = simplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) {
793 // It does, we successfully reassociated!
794 ++NumReassoc;
795 return W;
796 }
797 // See if "V === X - Z" simplifies.
798 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
799 // It does! Now see if "Y + V" simplifies.
800 if (Value *W = simplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) {
801 // It does, we successfully reassociated!
802 ++NumReassoc;
803 return W;
804 }
805 }
806
807 // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
808 // For example, X - (X + 1) -> -1
809 X = Op0;
810 if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
811 // See if "V === X - Y" simplifies.
812 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
813 // It does! Now see if "V - Z" simplifies.
814 if (Value *W = simplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) {
815 // It does, we successfully reassociated!
816 ++NumReassoc;
817 return W;
818 }
819 // See if "V === X - Z" simplifies.
820 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
821 // It does! Now see if "V - Y" simplifies.
822 if (Value *W = simplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) {
823 // It does, we successfully reassociated!
824 ++NumReassoc;
825 return W;
826 }
827 }
828
829 // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
830 // For example, X - (X - Y) -> Y.
831 Z = Op0;
832 if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
833 // See if "V === Z - X" simplifies.
834 if (Value *V = simplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1))
835 // It does! Now see if "V + Y" simplifies.
836 if (Value *W = simplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) {
837 // It does, we successfully reassociated!
838 ++NumReassoc;
839 return W;
840 }
841
842 // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
843 if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
844 match(Op1, m_Trunc(m_Value(Y))))
845 if (X->getType() == Y->getType())
846 // See if "V === X - Y" simplifies.
847 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
848 // It does! Now see if "trunc V" simplifies.
849 if (Value *W = simplifyCastInst(Instruction::Trunc, V, Op0->getType(),
850 Q, MaxRecurse - 1))
851 // It does, return the simplified "trunc V".
852 return W;
853
854 // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
855 if (match(Op0, m_PtrToInt(m_Value(X))) && match(Op1, m_PtrToInt(m_Value(Y))))
856 if (Constant *Result = computePointerDifference(Q.DL, X, Y))
857 return ConstantFoldIntegerCast(Result, Op0->getType(), /*IsSigned*/ true,
858 Q.DL);
859
860 // i1 sub -> xor.
861 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
862 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
863 return V;
864
865 // Threading Sub over selects and phi nodes is pointless, so don't bother.
866 // Threading over the select in "A - select(cond, B, C)" means evaluating
867 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
868 // only if B and C are equal. If B and C are equal then (since we assume
869 // that operands have already been simplified) "select(cond, B, C)" should
870 // have been simplified to the common value of B and C already. Analysing
871 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
872 // for threading over phi nodes.
873
874 if (Value *V = simplifyByDomEq(Instruction::Sub, Op0, Op1, Q, MaxRecurse))
875 return V;
876
877 // (sub nuw C_Mask, (xor X, C_Mask)) -> X
878 if (IsNUW) {
879 Value *X;
880 if (match(Op1, m_Xor(m_Value(X), m_Specific(Op0))) &&
881 match(Op0, m_LowBitMask()))
882 return X;
883 }
884
885 return nullptr;
886}
887
888Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
889 const SimplifyQuery &Q) {
890 return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
891}
892
893/// Given operands for a Mul, see if we can fold the result.
894/// If not, this returns null.
895static Value *simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
896 const SimplifyQuery &Q, unsigned MaxRecurse) {
897 if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q))
898 return C;
899
900 // X * poison -> poison
901 if (isa<PoisonValue>(Op1))
902 return Op1;
903
904 // X * undef -> 0
905 // X * 0 -> 0
906 if (Q.isUndefValue(Op1) || match(Op1, m_Zero()))
907 return Constant::getNullValue(Op0->getType());
908
909 // X * 1 -> X
910 if (match(Op1, m_One()))
911 return Op0;
912
913 // (X / Y) * Y -> X if the division is exact.
914 Value *X = nullptr;
915 if (Q.IIQ.UseInstrInfo &&
916 (match(Op0,
917 m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
918 match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))) // Y * (X / Y)
919 return X;
920
921 if (Op0->getType()->isIntOrIntVectorTy(1)) {
922 // mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not
923 // representable). All other cases reduce to 0, so just return 0.
924 if (IsNSW)
925 return ConstantInt::getNullValue(Op0->getType());
926
927 // Treat "mul i1" as "and i1".
928 if (MaxRecurse)
929 if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
930 return V;
931 }
932
933 // Try some generic simplifications for associative operations.
934 if (Value *V =
935 simplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
936 return V;
937
938 // Mul distributes over Add. Try some generic simplifications based on this.
939 if (Value *V = expandCommutativeBinOp(Instruction::Mul, Op0, Op1,
940 Instruction::Add, Q, MaxRecurse))
941 return V;
942
943 // If the operation is with the result of a select instruction, check whether
944 // operating on either branch of the select always yields the same value.
945 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
946 if (Value *V =
947 threadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
948 return V;
949
950 // If the operation is with the result of a phi instruction, check whether
951 // operating on all incoming values of the phi always yields the same value.
952 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
953 if (Value *V =
954 threadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
955 return V;
956
957 return nullptr;
958}
959
960Value *llvm::simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
961 const SimplifyQuery &Q) {
962 return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
963}
964
965/// Given a predicate and two operands, return true if the comparison is true.
966/// This is a helper for div/rem simplification where we return some other value
967/// when we can prove a relationship between the operands.
969 const SimplifyQuery &Q, unsigned MaxRecurse) {
970 Value *V = simplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse);
972 return (C && C->isAllOnesValue());
973}
974
975/// Return true if we can simplify X / Y to 0. Remainder can adapt that answer
976/// to simplify X % Y to X.
977static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q,
978 unsigned MaxRecurse, bool IsSigned) {
979 // Recursion is always used, so bail out at once if we already hit the limit.
980 if (!MaxRecurse--)
981 return false;
982
983 if (IsSigned) {
984 // (X srem Y) sdiv Y --> 0
985 if (match(X, m_SRem(m_Value(), m_Specific(Y))))
986 return true;
987
988 // |X| / |Y| --> 0
989 //
990 // We require that 1 operand is a simple constant. That could be extended to
991 // 2 variables if we computed the sign bit for each.
992 //
993 // Make sure that a constant is not the minimum signed value because taking
994 // the abs() of that is undefined.
995 Type *Ty = X->getType();
996 const APInt *C;
997 if (match(X, m_APInt(C)) && !C->isMinSignedValue()) {
998 // Is the variable divisor magnitude always greater than the constant
999 // dividend magnitude?
1000 // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
1001 Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
1002 Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
1003 if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) ||
1004 isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse))
1005 return true;
1006 }
1007 if (match(Y, m_APInt(C))) {
1008 // Special-case: we can't take the abs() of a minimum signed value. If
1009 // that's the divisor, then all we have to do is prove that the dividend
1010 // is also not the minimum signed value.
1011 if (C->isMinSignedValue())
1012 return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse);
1013
1014 // Is the variable dividend magnitude always less than the constant
1015 // divisor magnitude?
1016 // |X| < |C| --> X > -abs(C) and X < abs(C)
1017 Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
1018 Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
1019 if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) &&
1020 isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse))
1021 return true;
1022 }
1023 return false;
1024 }
1025
1026 // IsSigned == false.
1027
1028 // Is the unsigned dividend known to be less than a constant divisor?
1029 // TODO: Convert this (and above) to range analysis
1030 // ("computeConstantRangeIncludingKnownBits")?
1031 const APInt *C;
1032 if (match(Y, m_APInt(C)) && computeKnownBits(X, Q).getMaxValue().ult(*C))
1033 return true;
1034
1035 // Try again for any divisor:
1036 // Is the dividend unsigned less than the divisor?
1037 return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
1038}
1039
1040/// Check for common or similar folds of integer division or integer remainder.
1041/// This applies to all 4 opcodes (sdiv/udiv/srem/urem).
1043 Value *Op1, const SimplifyQuery &Q,
1044 unsigned MaxRecurse) {
1045 bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
1046 bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
1047
1048 Type *Ty = Op0->getType();
1049
1050 // X / undef -> poison
1051 // X % undef -> poison
1052 if (Q.isUndefValue(Op1) || isa<PoisonValue>(Op1))
1053 return PoisonValue::get(Ty);
1054
1055 // X / 0 -> poison
1056 // X % 0 -> poison
1057 // We don't need to preserve faults!
1058 if (match(Op1, m_Zero()))
1059 return PoisonValue::get(Ty);
1060
1061 // poison / X -> poison
1062 // poison % X -> poison
1063 if (isa<PoisonValue>(Op0))
1064 return Op0;
1065
1066 // undef / X -> 0
1067 // undef % X -> 0
1068 if (Q.isUndefValue(Op0))
1069 return Constant::getNullValue(Ty);
1070
1071 // 0 / X -> 0
1072 // 0 % X -> 0
1073 if (match(Op0, m_Zero()))
1074 return Constant::getNullValue(Op0->getType());
1075
1076 // X / X -> 1
1077 // X % X -> 0
1078 if (Op0 == Op1)
1079 return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty);
1080
1081 KnownBits Known = computeKnownBits(Op1, Q);
1082 // X / 0 -> poison
1083 // X % 0 -> poison
1084 // If the divisor is known to be zero, just return poison. This can happen in
1085 // some cases where its provable indirectly the denominator is zero but it's
1086 // not trivially simplifiable (i.e known zero through a phi node).
1087 if (Known.isZero())
1088 return PoisonValue::get(Ty);
1089
1090 // X / 1 -> X
1091 // X % 1 -> 0
1092 // If the divisor can only be zero or one, we can't have division-by-zero
1093 // or remainder-by-zero, so assume the divisor is 1.
1094 // e.g. 1, zext (i8 X), sdiv X (Y and 1)
1095 if (Known.countMinLeadingZeros() == Known.getBitWidth() - 1)
1096 return IsDiv ? Op0 : Constant::getNullValue(Ty);
1097
1098 // If X * Y does not overflow, then:
1099 // X * Y / Y -> X
1100 // X * Y % Y -> 0
1101 Value *X;
1102 if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
1104 // The multiplication can't overflow if it is defined not to, or if
1105 // X == A / Y for some A.
1106 if ((IsSigned && Q.IIQ.hasNoSignedWrap(Mul)) ||
1107 (!IsSigned && Q.IIQ.hasNoUnsignedWrap(Mul)) ||
1108 (IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) ||
1109 (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1))))) {
1110 return IsDiv ? X : Constant::getNullValue(Op0->getType());
1111 }
1112 }
1113
1114 if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1115 return IsDiv ? Constant::getNullValue(Op0->getType()) : Op0;
1116
1117 if (Value *V = simplifyByDomEq(Opcode, Op0, Op1, Q, MaxRecurse))
1118 return V;
1119
1120 // If the operation is with the result of a select instruction, check whether
1121 // operating on either branch of the select always yields the same value.
1122 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1123 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1124 return V;
1125
1126 // If the operation is with the result of a phi instruction, check whether
1127 // operating on all incoming values of the phi always yields the same value.
1128 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1129 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1130 return V;
1131
1132 return nullptr;
1133}
1134
1135/// These are simplifications common to SDiv and UDiv.
1137 bool IsExact, const SimplifyQuery &Q,
1138 unsigned MaxRecurse) {
1139 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1140 return C;
1141
1142 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1143 return V;
1144
1145 const APInt *DivC;
1146 if (IsExact && match(Op1, m_APInt(DivC))) {
1147 // If this is an exact divide by a constant, then the dividend (Op0) must
1148 // have at least as many trailing zeros as the divisor to divide evenly. If
1149 // it has less trailing zeros, then the result must be poison.
1150 if (DivC->countr_zero()) {
1151 KnownBits KnownOp0 = computeKnownBits(Op0, Q);
1152 if (KnownOp0.countMaxTrailingZeros() < DivC->countr_zero())
1153 return PoisonValue::get(Op0->getType());
1154 }
1155
1156 // udiv exact (mul nsw X, C), C --> X
1157 // sdiv exact (mul nuw X, C), C --> X
1158 // where C is not a power of 2.
1159 Value *X;
1160 if (!DivC->isPowerOf2() &&
1161 (Opcode == Instruction::UDiv
1162 ? match(Op0, m_NSWMul(m_Value(X), m_Specific(Op1)))
1163 : match(Op0, m_NUWMul(m_Value(X), m_Specific(Op1)))))
1164 return X;
1165 }
1166
1167 return nullptr;
1168}
1169
1170/// These are simplifications common to SRem and URem.
1172 const SimplifyQuery &Q, unsigned MaxRecurse) {
1173 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1174 return C;
1175
1176 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1177 return V;
1178
1179 // (X << Y) % X -> 0
1180 if (Q.IIQ.UseInstrInfo) {
1181 if ((Opcode == Instruction::SRem &&
1182 match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))) ||
1183 (Opcode == Instruction::URem &&
1184 match(Op0, m_NUWShl(m_Specific(Op1), m_Value()))))
1185 return Constant::getNullValue(Op0->getType());
1186
1187 const APInt *C0;
1188 if (match(Op1, m_APInt(C0))) {
1189 // (srem (mul nsw X, C1), C0) -> 0 if C1 s% C0 == 0
1190 // (urem (mul nuw X, C1), C0) -> 0 if C1 u% C0 == 0
1191 if (Opcode == Instruction::SRem
1192 ? match(Op0,
1193 m_NSWMul(m_Value(), m_CheckedInt([C0](const APInt &C) {
1194 return C.srem(*C0).isZero();
1195 })))
1196 : match(Op0,
1197 m_NUWMul(m_Value(), m_CheckedInt([C0](const APInt &C) {
1198 return C.urem(*C0).isZero();
1199 }))))
1200 return Constant::getNullValue(Op0->getType());
1201 }
1202 }
1203 return nullptr;
1204}
1205
1206/// Given operands for an SDiv, see if we can fold the result.
1207/// If not, this returns null.
1208static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1209 const SimplifyQuery &Q, unsigned MaxRecurse) {
1210 // If two operands are negated and no signed overflow, return -1.
1211 if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
1212 return Constant::getAllOnesValue(Op0->getType());
1213
1214 return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1215}
1216
1217Value *llvm::simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1218 const SimplifyQuery &Q) {
1219 return ::simplifySDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1220}
1221
1222/// Given operands for a UDiv, see if we can fold the result.
1223/// If not, this returns null.
1224static Value *simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1225 const SimplifyQuery &Q, unsigned MaxRecurse) {
1226 return simplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1227}
1228
1229Value *llvm::simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1230 const SimplifyQuery &Q) {
1231 return ::simplifyUDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1232}
1233
1234/// Given operands for an SRem, see if we can fold the result.
1235/// If not, this returns null.
1236static Value *simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1237 unsigned MaxRecurse) {
1238 // If the divisor is 0, the result is undefined, so assume the divisor is -1.
1239 // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
1240 Value *X;
1241 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
1242 return ConstantInt::getNullValue(Op0->getType());
1243
1244 // If the two operands are negated, return 0.
1245 if (isKnownNegation(Op0, Op1))
1246 return ConstantInt::getNullValue(Op0->getType());
1247
1248 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1249}
1250
1252 return ::simplifySRemInst(Op0, Op1, Q, RecursionLimit);
1253}
1254
1255/// Given operands for a URem, see if we can fold the result.
1256/// If not, this returns null.
1257static Value *simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1258 unsigned MaxRecurse) {
1259 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1260}
1261
1263 return ::simplifyURemInst(Op0, Op1, Q, RecursionLimit);
1264}
1265
1266/// Returns true if a shift by \c Amount always yields poison.
1267static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) {
1268 Constant *C = dyn_cast<Constant>(Amount);
1269 if (!C)
1270 return false;
1271
1272 // X shift by undef -> poison because it may shift by the bitwidth.
1273 if (Q.isUndefValue(C))
1274 return true;
1275
1276 // Shifting by the bitwidth or more is poison. This covers scalars and
1277 // fixed/scalable vectors with splat constants.
1278 const APInt *AmountC;
1279 if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth()))
1280 return true;
1281
1282 // Try harder for fixed-length vectors:
1283 // If all lanes of a vector shift are poison, the whole shift is poison.
1285 for (unsigned I = 0,
1286 E = cast<FixedVectorType>(C->getType())->getNumElements();
1287 I != E; ++I)
1288 if (!isPoisonShift(C->getAggregateElement(I), Q))
1289 return false;
1290 return true;
1291 }
1292
1293 return false;
1294}
1295
1296/// Given operands for an Shl, LShr or AShr, see if we can fold the result.
1297/// If not, this returns null.
1299 Value *Op1, bool IsNSW, const SimplifyQuery &Q,
1300 unsigned MaxRecurse) {
1301 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1302 return C;
1303
1304 // poison shift by X -> poison
1305 if (isa<PoisonValue>(Op0))
1306 return Op0;
1307
1308 // 0 shift by X -> 0
1309 if (match(Op0, m_Zero()))
1310 return Constant::getNullValue(Op0->getType());
1311
1312 // X shift by 0 -> X
1313 // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
1314 // would be poison.
1315 Value *X;
1316 if (match(Op1, m_Zero()) ||
1317 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1318 return Op0;
1319
1320 // Fold undefined shifts.
1321 if (isPoisonShift(Op1, Q))
1322 return PoisonValue::get(Op0->getType());
1323
1324 // If the operation is with the result of a select instruction, check whether
1325 // operating on either branch of the select always yields the same value.
1326 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1327 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1328 return V;
1329
1330 // If the operation is with the result of a phi instruction, check whether
1331 // operating on all incoming values of the phi always yields the same value.
1332 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1333 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1334 return V;
1335
1336 // If any bits in the shift amount make that value greater than or equal to
1337 // the number of bits in the type, the shift is undefined.
1338 KnownBits KnownAmt = computeKnownBits(Op1, Q);
1339 if (KnownAmt.getMinValue().uge(KnownAmt.getBitWidth()))
1340 return PoisonValue::get(Op0->getType());
1341
1342 // If all valid bits in the shift amount are known zero, the first operand is
1343 // unchanged.
1344 unsigned NumValidShiftBits = Log2_32_Ceil(KnownAmt.getBitWidth());
1345 if (KnownAmt.countMinTrailingZeros() >= NumValidShiftBits)
1346 return Op0;
1347
1348 // Check for nsw shl leading to a poison value.
1349 if (IsNSW) {
1350 assert(Opcode == Instruction::Shl && "Expected shl for nsw instruction");
1351 KnownBits KnownVal = computeKnownBits(Op0, Q);
1352 KnownBits KnownShl = KnownBits::shl(KnownVal, KnownAmt);
1353
1354 if (KnownVal.Zero.isSignBitSet())
1355 KnownShl.Zero.setSignBit();
1356 if (KnownVal.One.isSignBitSet())
1357 KnownShl.One.setSignBit();
1358
1359 if (KnownShl.hasConflict())
1360 return PoisonValue::get(Op0->getType());
1361 }
1362
1363 return nullptr;
1364}
1365
1366/// Given operands for an LShr or AShr, see if we can fold the result. If not,
1367/// this returns null.
1369 Value *Op1, bool IsExact,
1370 const SimplifyQuery &Q, unsigned MaxRecurse) {
1371 if (Value *V =
1372 simplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse))
1373 return V;
1374
1375 // X >> X -> 0
1376 if (Op0 == Op1)
1377 return Constant::getNullValue(Op0->getType());
1378
1379 // undef >> X -> 0
1380 // undef >> X -> undef (if it's exact)
1381 if (Q.isUndefValue(Op0))
1382 return IsExact ? Op0 : Constant::getNullValue(Op0->getType());
1383
1384 // The low bit cannot be shifted out of an exact shift if it is set.
1385 // TODO: Generalize by counting trailing zeros (see fold for exact division).
1386 if (IsExact) {
1387 KnownBits Op0Known = computeKnownBits(Op0, Q);
1388 if (Op0Known.One[0])
1389 return Op0;
1390 }
1391
1392 return nullptr;
1393}
1394
1395/// Given operands for an Shl, see if we can fold the result.
1396/// If not, this returns null.
1397static Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1398 const SimplifyQuery &Q, unsigned MaxRecurse) {
1399 if (Value *V =
1400 simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
1401 return V;
1402
1403 Type *Ty = Op0->getType();
1404 // undef << X -> 0
1405 // undef << X -> undef if (if it's NSW/NUW)
1406 if (Q.isUndefValue(Op0))
1407 return IsNSW || IsNUW ? Op0 : Constant::getNullValue(Ty);
1408
1409 // (X >> A) << A -> X
1410 Value *X;
1411 if (Q.IIQ.UseInstrInfo &&
1412 match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
1413 return X;
1414
1415 // shl nuw i8 C, %x -> C iff C has sign bit set.
1416 if (IsNUW && match(Op0, m_Negative()))
1417 return Op0;
1418 // NOTE: could use computeKnownBits() / LazyValueInfo,
1419 // but the cost-benefit analysis suggests it isn't worth it.
1420
1421 // "nuw" guarantees that only zeros are shifted out, and "nsw" guarantees
1422 // that the sign-bit does not change, so the only input that does not
1423 // produce poison is 0, and "0 << (bitwidth-1) --> 0".
1424 if (IsNSW && IsNUW &&
1425 match(Op1, m_SpecificInt(Ty->getScalarSizeInBits() - 1)))
1426 return Constant::getNullValue(Ty);
1427
1428 return nullptr;
1429}
1430
1431Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1432 const SimplifyQuery &Q) {
1433 return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
1434}
1435
1436/// Given operands for an LShr, see if we can fold the result.
1437/// If not, this returns null.
1438static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1439 const SimplifyQuery &Q, unsigned MaxRecurse) {
1440 if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, IsExact, Q,
1441 MaxRecurse))
1442 return V;
1443
1444 // (X << A) >> A -> X
1445 Value *X;
1446 if (Q.IIQ.UseInstrInfo && match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
1447 return X;
1448
1449 // ((X << A) | Y) >> A -> X if effective width of Y is not larger than A.
1450 // We can return X as we do in the above case since OR alters no bits in X.
1451 // SimplifyDemandedBits in InstCombine can do more general optimization for
1452 // bit manipulation. This pattern aims to provide opportunities for other
1453 // optimizers by supporting a simple but common case in InstSimplify.
1454 Value *Y;
1455 const APInt *ShRAmt, *ShLAmt;
1456 if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(ShRAmt)) &&
1457 match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y))) &&
1458 *ShRAmt == *ShLAmt) {
1459 const KnownBits YKnown = computeKnownBits(Y, Q);
1460 const unsigned EffWidthY = YKnown.countMaxActiveBits();
1461 if (ShRAmt->uge(EffWidthY))
1462 return X;
1463 }
1464
1465 return nullptr;
1466}
1467
1468Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1469 const SimplifyQuery &Q) {
1470 return ::simplifyLShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1471}
1472
1473/// Given operands for an AShr, see if we can fold the result.
1474/// If not, this returns null.
1475static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1476 const SimplifyQuery &Q, unsigned MaxRecurse) {
1477 if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, IsExact, Q,
1478 MaxRecurse))
1479 return V;
1480
1481 // -1 >>a X --> -1
1482 // (-1 << X) a>> X --> -1
1483 // We could return the original -1 constant to preserve poison elements.
1484 if (match(Op0, m_AllOnes()) ||
1485 match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1))))
1486 return Constant::getAllOnesValue(Op0->getType());
1487
1488 // (X << A) >> A -> X
1489 Value *X;
1490 if (Q.IIQ.UseInstrInfo && match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))
1491 return X;
1492
1493 // Arithmetic shifting an all-sign-bit value is a no-op.
1494 unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, Q.AC, Q.CxtI, Q.DT);
1495 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1496 return Op0;
1497
1498 return nullptr;
1499}
1500
1501Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1502 const SimplifyQuery &Q) {
1503 return ::simplifyAShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1504}
1505
1506/// Commuted variants are assumed to be handled by calling this function again
1507/// with the parameters swapped.
1509 ICmpInst *UnsignedICmp, bool IsAnd,
1510 const SimplifyQuery &Q) {
1511 Value *X, *Y;
1512
1513 CmpPredicate EqPred;
1514 if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
1515 !ICmpInst::isEquality(EqPred))
1516 return nullptr;
1517
1518 CmpPredicate UnsignedPred;
1519
1520 Value *A, *B;
1521 // Y = (A - B);
1522 if (match(Y, m_Sub(m_Value(A), m_Value(B)))) {
1523 if (match(UnsignedICmp,
1524 m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) &&
1525 ICmpInst::isUnsigned(UnsignedPred)) {
1526 // A >=/<= B || (A - B) != 0 <--> true
1527 if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1528 UnsignedPred == ICmpInst::ICMP_ULE) &&
1529 EqPred == ICmpInst::ICMP_NE && !IsAnd)
1530 return ConstantInt::getTrue(UnsignedICmp->getType());
1531 // A </> B && (A - B) == 0 <--> false
1532 if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1533 UnsignedPred == ICmpInst::ICMP_UGT) &&
1534 EqPred == ICmpInst::ICMP_EQ && IsAnd)
1535 return ConstantInt::getFalse(UnsignedICmp->getType());
1536
1537 // A </> B && (A - B) != 0 <--> A </> B
1538 // A </> B || (A - B) != 0 <--> (A - B) != 0
1539 if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1540 UnsignedPred == ICmpInst::ICMP_UGT))
1541 return IsAnd ? UnsignedICmp : ZeroICmp;
1542
1543 // A <=/>= B && (A - B) == 0 <--> (A - B) == 0
1544 // A <=/>= B || (A - B) == 0 <--> A <=/>= B
1545 if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1546 UnsignedPred == ICmpInst::ICMP_UGE))
1547 return IsAnd ? ZeroICmp : UnsignedICmp;
1548 }
1549
1550 // Given Y = (A - B)
1551 // Y >= A && Y != 0 --> Y >= A iff B != 0
1552 // Y < A || Y == 0 --> Y < A iff B != 0
1553 if (match(UnsignedICmp,
1554 m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) {
1555 if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1556 EqPred == ICmpInst::ICMP_NE && isKnownNonZero(B, Q))
1557 return UnsignedICmp;
1558 if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1559 EqPred == ICmpInst::ICMP_EQ && isKnownNonZero(B, Q))
1560 return UnsignedICmp;
1561 }
1562 }
1563
1564 if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
1565 ICmpInst::isUnsigned(UnsignedPred))
1566 ;
1567 else if (match(UnsignedICmp,
1568 m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
1569 ICmpInst::isUnsigned(UnsignedPred))
1570 UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1571 else
1572 return nullptr;
1573
1574 // X > Y && Y == 0 --> Y == 0 iff X != 0
1575 // X > Y || Y == 0 --> X > Y iff X != 0
1576 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1577 isKnownNonZero(X, Q))
1578 return IsAnd ? ZeroICmp : UnsignedICmp;
1579
1580 // X <= Y && Y != 0 --> X <= Y iff X != 0
1581 // X <= Y || Y != 0 --> Y != 0 iff X != 0
1582 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1583 isKnownNonZero(X, Q))
1584 return IsAnd ? UnsignedICmp : ZeroICmp;
1585
1586 // The transforms below here are expected to be handled more generally with
1587 // simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's
1588 // foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap,
1589 // these are candidates for removal.
1590
1591 // X < Y && Y != 0 --> X < Y
1592 // X < Y || Y != 0 --> Y != 0
1593 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1594 return IsAnd ? UnsignedICmp : ZeroICmp;
1595
1596 // X >= Y && Y == 0 --> Y == 0
1597 // X >= Y || Y == 0 --> X >= Y
1598 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1599 return IsAnd ? ZeroICmp : UnsignedICmp;
1600
1601 // X < Y && Y == 0 --> false
1602 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1603 IsAnd)
1604 return getFalse(UnsignedICmp->getType());
1605
1606 // X >= Y || Y != 0 --> true
1607 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1608 !IsAnd)
1609 return getTrue(UnsignedICmp->getType());
1610
1611 return nullptr;
1612}
1613
1614/// Test if a pair of compares with a shared operand and 2 constants has an
1615/// empty set intersection, full set union, or if one compare is a superset of
1616/// the other.
1618 bool IsAnd) {
1619 // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)).
1620 if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
1621 return nullptr;
1622
1623 const APInt *C0, *C1;
1624 if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
1625 !match(Cmp1->getOperand(1), m_APInt(C1)))
1626 return nullptr;
1627
1628 auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
1629 auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
1630
1631 // For and-of-compares, check if the intersection is empty:
1632 // (icmp X, C0) && (icmp X, C1) --> empty set --> false
1633 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1634 return getFalse(Cmp0->getType());
1635
1636 // For or-of-compares, check if the union is full:
1637 // (icmp X, C0) || (icmp X, C1) --> full set --> true
1638 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1639 return getTrue(Cmp0->getType());
1640
1641 // Is one range a superset of the other?
1642 // If this is and-of-compares, take the smaller set:
1643 // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
1644 // If this is or-of-compares, take the larger set:
1645 // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
1646 if (Range0.contains(Range1))
1647 return IsAnd ? Cmp1 : Cmp0;
1648 if (Range1.contains(Range0))
1649 return IsAnd ? Cmp0 : Cmp1;
1650
1651 return nullptr;
1652}
1653
1655 const InstrInfoQuery &IIQ) {
1656 // (icmp (add V, C0), C1) & (icmp V, C0)
1657 CmpPredicate Pred0, Pred1;
1658 const APInt *C0, *C1;
1659 Value *V;
1660 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1661 return nullptr;
1662
1663 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1664 return nullptr;
1665
1666 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
1667 if (AddInst->getOperand(1) != Op1->getOperand(1))
1668 return nullptr;
1669
1670 Type *ITy = Op0->getType();
1671 bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1672 bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1673
1674 const APInt Delta = *C1 - *C0;
1675 if (C0->isStrictlyPositive()) {
1676 if (Delta == 2) {
1677 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1678 return getFalse(ITy);
1679 if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1680 return getFalse(ITy);
1681 }
1682 if (Delta == 1) {
1683 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1684 return getFalse(ITy);
1685 if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1686 return getFalse(ITy);
1687 }
1688 }
1689 if (C0->getBoolValue() && IsNUW) {
1690 if (Delta == 2)
1691 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1692 return getFalse(ITy);
1693 if (Delta == 1)
1694 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1695 return getFalse(ITy);
1696 }
1697
1698 return nullptr;
1699}
1700
1701/// Try to simplify and/or of icmp with ctpop intrinsic.
1703 bool IsAnd) {
1704 CmpPredicate Pred0, Pred1;
1705 Value *X;
1706 const APInt *C;
1708 m_APInt(C))) ||
1709 !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())) || C->isZero())
1710 return nullptr;
1711
1712 // (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0
1713 if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1714 return Cmp1;
1715 // (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0
1716 if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1717 return Cmp1;
1718
1719 return nullptr;
1720}
1721
1723 const SimplifyQuery &Q) {
1724 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true, Q))
1725 return X;
1726 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true, Q))
1727 return X;
1728
1729 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true))
1730 return X;
1731
1732 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, true))
1733 return X;
1734 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, true))
1735 return X;
1736
1737 if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1738 return X;
1739 if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1740 return X;
1741
1742 return nullptr;
1743}
1744
1746 const InstrInfoQuery &IIQ) {
1747 // (icmp (add V, C0), C1) | (icmp V, C0)
1748 CmpPredicate Pred0, Pred1;
1749 const APInt *C0, *C1;
1750 Value *V;
1751 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1752 return nullptr;
1753
1754 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1755 return nullptr;
1756
1757 auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1758 if (AddInst->getOperand(1) != Op1->getOperand(1))
1759 return nullptr;
1760
1761 Type *ITy = Op0->getType();
1762 bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1763 bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1764
1765 const APInt Delta = *C1 - *C0;
1766 if (C0->isStrictlyPositive()) {
1767 if (Delta == 2) {
1768 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1769 return getTrue(ITy);
1770 if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1771 return getTrue(ITy);
1772 }
1773 if (Delta == 1) {
1774 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1775 return getTrue(ITy);
1776 if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1777 return getTrue(ITy);
1778 }
1779 }
1780 if (C0->getBoolValue() && IsNUW) {
1781 if (Delta == 2)
1782 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1783 return getTrue(ITy);
1784 if (Delta == 1)
1785 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1786 return getTrue(ITy);
1787 }
1788
1789 return nullptr;
1790}
1791
1793 const SimplifyQuery &Q) {
1794 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false, Q))
1795 return X;
1796 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false, Q))
1797 return X;
1798
1799 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false))
1800 return X;
1801
1802 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, false))
1803 return X;
1804 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, false))
1805 return X;
1806
1807 if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1808 return X;
1809 if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1810 return X;
1811
1812 return nullptr;
1813}
1814
1816 FCmpInst *RHS, bool IsAnd) {
1817 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1818 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1819 if (LHS0->getType() != RHS0->getType())
1820 return nullptr;
1821
1822 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1823 auto AbsOrSelfLHS0 = m_CombineOr(m_Specific(LHS0), m_FAbs(m_Specific(LHS0)));
1824 if ((PredL == FCmpInst::FCMP_ORD || PredL == FCmpInst::FCMP_UNO) &&
1825 ((FCmpInst::isOrdered(PredR) && IsAnd) ||
1826 (FCmpInst::isUnordered(PredR) && !IsAnd))) {
1827 // (fcmp ord X, 0) & (fcmp o** X/abs(X), Y) --> fcmp o** X/abs(X), Y
1828 // (fcmp uno X, 0) & (fcmp o** X/abs(X), Y) --> false
1829 // (fcmp uno X, 0) | (fcmp u** X/abs(X), Y) --> fcmp u** X/abs(X), Y
1830 // (fcmp ord X, 0) | (fcmp u** X/abs(X), Y) --> true
1831 if ((match(RHS0, AbsOrSelfLHS0) || match(RHS1, AbsOrSelfLHS0)) &&
1832 match(LHS1, m_PosZeroFP()))
1833 return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1834 ? static_cast<Value *>(RHS)
1835 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1836 }
1837
1838 auto AbsOrSelfRHS0 = m_CombineOr(m_Specific(RHS0), m_FAbs(m_Specific(RHS0)));
1839 if ((PredR == FCmpInst::FCMP_ORD || PredR == FCmpInst::FCMP_UNO) &&
1840 ((FCmpInst::isOrdered(PredL) && IsAnd) ||
1841 (FCmpInst::isUnordered(PredL) && !IsAnd))) {
1842 // (fcmp o** X/abs(X), Y) & (fcmp ord X, 0) --> fcmp o** X/abs(X), Y
1843 // (fcmp o** X/abs(X), Y) & (fcmp uno X, 0) --> false
1844 // (fcmp u** X/abs(X), Y) | (fcmp uno X, 0) --> fcmp u** X/abs(X), Y
1845 // (fcmp u** X/abs(X), Y) | (fcmp ord X, 0) --> true
1846 if ((match(LHS0, AbsOrSelfRHS0) || match(LHS1, AbsOrSelfRHS0)) &&
1847 match(RHS1, m_PosZeroFP()))
1848 return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1849 ? static_cast<Value *>(LHS)
1850 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1851 }
1852
1853 Value *V0;
1854 const APFloat *V0Op1, *V1Op1;
1855 // (fcmp olt V0, V0Op1) || (fcmp olt V0, V1Op1)
1856 // --> fcmp olt V0, max(V0Op1, V1Op1)
1857 // (fcmp ogt V0, V0Op1) || (fcmp ogt V0, V1Op1)
1858 // --> fcmp ogt V0, max(V0Op1, V1Op1)
1859 //
1860 // (fcmp olt V0, V0Op1) && (fcmp olt V0, V1Op1)
1861 // --> fcmp olt V0, min(V0Op1, V1Op1)
1862 // (fcmp ogt V0, V0Op1) && (fcmp ogt V0, V1Op1)
1863 // --> fcmp ogt V0, min(V0Op1, V1Op1)
1865 m_APFloat(V0Op1))) &&
1867 m_APFloat(V1Op1)))) {
1868 if (*V0Op1 > *V1Op1)
1869 return IsAnd ? RHS : LHS;
1870 if (*V1Op1 > *V0Op1)
1871 return IsAnd ? LHS : RHS;
1873 m_APFloat(V0Op1))) &&
1875 m_APFloat(V1Op1)))) {
1876 if (*V0Op1 < *V1Op1)
1877 return IsAnd ? RHS : LHS;
1878 if (*V1Op1 < *V0Op1)
1879 return IsAnd ? LHS : RHS;
1880 }
1881
1882 return nullptr;
1883}
1884
1886 Value *Op1, bool IsAnd) {
1887 // Look through casts of the 'and' operands to find compares.
1888 auto *Cast0 = dyn_cast<CastInst>(Op0);
1889 auto *Cast1 = dyn_cast<CastInst>(Op1);
1890 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1891 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1892 Op0 = Cast0->getOperand(0);
1893 Op1 = Cast1->getOperand(0);
1894 }
1895
1896 Value *V = nullptr;
1897 auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
1898 auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
1899 if (ICmp0 && ICmp1)
1900 V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1, Q)
1901 : simplifyOrOfICmps(ICmp0, ICmp1, Q);
1902
1903 auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
1904 auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
1905 if (FCmp0 && FCmp1)
1906 V = simplifyAndOrOfFCmps(Q, FCmp0, FCmp1, IsAnd);
1907
1908 if (!V)
1909 return nullptr;
1910 if (!Cast0)
1911 return V;
1912
1913 // If we looked through casts, we can only handle a constant simplification
1914 // because we are not allowed to create a cast instruction here.
1915 if (auto *C = dyn_cast<Constant>(V))
1916 return ConstantFoldCastOperand(Cast0->getOpcode(), C, Cast0->getType(),
1917 Q.DL);
1918
1919 return nullptr;
1920}
1921
1922static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
1923 const SimplifyQuery &Q,
1924 bool AllowRefinement,
1926 unsigned MaxRecurse);
1927
1928static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,
1929 const SimplifyQuery &Q,
1930 unsigned MaxRecurse) {
1931 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1932 "Must be and/or");
1933 CmpPredicate Pred;
1934 Value *A, *B;
1935 if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) ||
1936 !ICmpInst::isEquality(Pred))
1937 return nullptr;
1938
1939 auto Simplify = [&](Value *Res) -> Value * {
1940 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, Res->getType());
1941
1942 // and (icmp eq a, b), x implies (a==b) inside x.
1943 // or (icmp ne a, b), x implies (a==b) inside x.
1944 // If x simplifies to true/false, we can simplify the and/or.
1945 if (Pred ==
1946 (Opcode == Instruction::And ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
1947 if (Res == Absorber)
1948 return Absorber;
1949 if (Res == ConstantExpr::getBinOpIdentity(Opcode, Res->getType()))
1950 return Op0;
1951 return nullptr;
1952 }
1953
1954 // If we have and (icmp ne a, b), x and for a==b we can simplify x to false,
1955 // then we can drop the icmp, as x will already be false in the case where
1956 // the icmp is false. Similar for or and true.
1957 if (Res == Absorber)
1958 return Op1;
1959 return nullptr;
1960 };
1961
1962 // In the final case (Res == Absorber with inverted predicate), it is safe to
1963 // refine poison during simplification, but not undef. For simplicity always
1964 // disable undef-based folds here.
1965 if (Value *Res = simplifyWithOpReplaced(Op1, A, B, Q.getWithoutUndef(),
1966 /* AllowRefinement */ true,
1967 /* DropFlags */ nullptr, MaxRecurse))
1968 return Simplify(Res);
1969 if (Value *Res = simplifyWithOpReplaced(Op1, B, A, Q.getWithoutUndef(),
1970 /* AllowRefinement */ true,
1971 /* DropFlags */ nullptr, MaxRecurse))
1972 return Simplify(Res);
1973
1974 return nullptr;
1975}
1976
1977/// Given a bitwise logic op, check if the operands are add/sub with a common
1978/// source value and inverted constant (identity: C - X -> ~(X + ~C)).
1980 Instruction::BinaryOps Opcode) {
1981 assert(Op0->getType() == Op1->getType() && "Mismatched binop types");
1982 assert(BinaryOperator::isBitwiseLogicOp(Opcode) && "Expected logic op");
1983 Value *X;
1984 Constant *C1, *C2;
1985 if ((match(Op0, m_Add(m_Value(X), m_Constant(C1))) &&
1986 match(Op1, m_Sub(m_Constant(C2), m_Specific(X)))) ||
1987 (match(Op1, m_Add(m_Value(X), m_Constant(C1))) &&
1988 match(Op0, m_Sub(m_Constant(C2), m_Specific(X))))) {
1989 if (ConstantExpr::getNot(C1) == C2) {
1990 // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0
1991 // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1
1992 // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1
1993 Type *Ty = Op0->getType();
1994 return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
1996 }
1997 }
1998 return nullptr;
1999}
2000
2001// Commutative patterns for and that will be tried with both operand orders.
2003 const SimplifyQuery &Q,
2004 unsigned MaxRecurse) {
2005 // ~A & A = 0
2006 if (match(Op0, m_Not(m_Specific(Op1))))
2007 return Constant::getNullValue(Op0->getType());
2008
2009 // (A | ?) & A = A
2010 if (match(Op0, m_c_Or(m_Specific(Op1), m_Value())))
2011 return Op1;
2012
2013 // (X | ~Y) & (X | Y) --> X
2014 Value *X, *Y;
2015 if (match(Op0, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
2016 match(Op1, m_c_Or(m_Specific(X), m_Specific(Y))))
2017 return X;
2018
2019 // If we have a multiplication overflow check that is being 'and'ed with a
2020 // check that one of the multipliers is not zero, we can omit the 'and', and
2021 // only keep the overflow check.
2022 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, true))
2023 return Op1;
2024
2025 // -A & A = A if A is a power of two or zero.
2026 if (match(Op0, m_Neg(m_Specific(Op1))) &&
2027 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI, Q.DT))
2028 return Op1;
2029
2030 // This is a similar pattern used for checking if a value is a power-of-2:
2031 // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
2032 if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
2033 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI, Q.DT))
2034 return Constant::getNullValue(Op1->getType());
2035
2036 // (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and
2037 // M <= N.
2038 const APInt *Shift1, *Shift2;
2039 if (match(Op0, m_Shl(m_Value(X), m_APInt(Shift1))) &&
2040 match(Op1, m_Add(m_Shl(m_Specific(X), m_APInt(Shift2)), m_AllOnes())) &&
2041 isKnownToBeAPowerOfTwo(X, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI) &&
2042 Shift1->uge(*Shift2))
2043 return Constant::getNullValue(Op0->getType());
2044
2045 if (Value *V =
2046 simplifyAndOrWithICmpEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2047 return V;
2048
2049 return nullptr;
2050}
2051
2052/// Given operands for an And, see if we can fold the result.
2053/// If not, this returns null.
2054static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2055 unsigned MaxRecurse) {
2056 if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
2057 return C;
2058
2059 // X & poison -> poison
2060 if (isa<PoisonValue>(Op1))
2061 return Op1;
2062
2063 // X & undef -> 0
2064 if (Q.isUndefValue(Op1))
2065 return Constant::getNullValue(Op0->getType());
2066
2067 // X & X = X
2068 if (Op0 == Op1)
2069 return Op0;
2070
2071 // X & 0 = 0
2072 if (match(Op1, m_Zero()))
2073 return Constant::getNullValue(Op0->getType());
2074
2075 // X & -1 = X
2076 if (match(Op1, m_AllOnes()))
2077 return Op0;
2078
2079 if (Value *Res = simplifyAndCommutative(Op0, Op1, Q, MaxRecurse))
2080 return Res;
2081 if (Value *Res = simplifyAndCommutative(Op1, Op0, Q, MaxRecurse))
2082 return Res;
2083
2084 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::And))
2085 return V;
2086
2087 // A mask that only clears known zeros of a shifted value is a no-op.
2088 const APInt *Mask;
2089 const APInt *ShAmt;
2090 Value *X, *Y;
2091 if (match(Op1, m_APInt(Mask))) {
2092 // If all bits in the inverted and shifted mask are clear:
2093 // and (shl X, ShAmt), Mask --> shl X, ShAmt
2094 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) &&
2095 (~(*Mask)).lshr(*ShAmt).isZero())
2096 return Op0;
2097
2098 // If all bits in the inverted and shifted mask are clear:
2099 // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
2100 if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
2101 (~(*Mask)).shl(*ShAmt).isZero())
2102 return Op0;
2103 }
2104
2105 // and 2^x-1, 2^C --> 0 where x <= C.
2106 const APInt *PowerC;
2107 Value *Shift;
2108 if (match(Op1, m_Power2(PowerC)) &&
2109 match(Op0, m_Add(m_Value(Shift), m_AllOnes())) &&
2110 isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ false, Q.AC, Q.CxtI,
2111 Q.DT)) {
2112 KnownBits Known = computeKnownBits(Shift, Q);
2113 // Use getActiveBits() to make use of the additional power of two knowledge
2114 if (PowerC->getActiveBits() >= Known.getMaxValue().getActiveBits())
2115 return ConstantInt::getNullValue(Op1->getType());
2116 }
2117
2118 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
2119 return V;
2120
2121 // Try some generic simplifications for associative operations.
2122 if (Value *V =
2123 simplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse))
2124 return V;
2125
2126 // And distributes over Or. Try some generic simplifications based on this.
2127 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2128 Instruction::Or, Q, MaxRecurse))
2129 return V;
2130
2131 // And distributes over Xor. Try some generic simplifications based on this.
2132 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2133 Instruction::Xor, Q, MaxRecurse))
2134 return V;
2135
2136 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2137 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2138 // A & (A && B) -> A && B
2139 if (match(Op1, m_Select(m_Specific(Op0), m_Value(), m_Zero())))
2140 return Op1;
2141 else if (match(Op0, m_Select(m_Specific(Op1), m_Value(), m_Zero())))
2142 return Op0;
2143 }
2144 // If the operation is with the result of a select instruction, check
2145 // whether operating on either branch of the select always yields the same
2146 // value.
2147 if (Value *V =
2148 threadBinOpOverSelect(Instruction::And, Op0, Op1, Q, MaxRecurse))
2149 return V;
2150 }
2151
2152 // If the operation is with the result of a phi instruction, check whether
2153 // operating on all incoming values of the phi always yields the same value.
2154 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2155 if (Value *V =
2156 threadBinOpOverPHI(Instruction::And, Op0, Op1, Q, MaxRecurse))
2157 return V;
2158
2159 // Assuming the effective width of Y is not larger than A, i.e. all bits
2160 // from X and Y are disjoint in (X << A) | Y,
2161 // if the mask of this AND op covers all bits of X or Y, while it covers
2162 // no bits from the other, we can bypass this AND op. E.g.,
2163 // ((X << A) | Y) & Mask -> Y,
2164 // if Mask = ((1 << effective_width_of(Y)) - 1)
2165 // ((X << A) | Y) & Mask -> X << A,
2166 // if Mask = ((1 << effective_width_of(X)) - 1) << A
2167 // SimplifyDemandedBits in InstCombine can optimize the general case.
2168 // This pattern aims to help other passes for a common case.
2169 Value *XShifted;
2170 if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(Mask)) &&
2172 m_Value(XShifted)),
2173 m_Value(Y)))) {
2174 const unsigned Width = Op0->getType()->getScalarSizeInBits();
2175 const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
2176 const KnownBits YKnown = computeKnownBits(Y, Q);
2177 const unsigned EffWidthY = YKnown.countMaxActiveBits();
2178 if (EffWidthY <= ShftCnt) {
2179 const KnownBits XKnown = computeKnownBits(X, Q);
2180 const unsigned EffWidthX = XKnown.countMaxActiveBits();
2181 const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY);
2182 const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt;
2183 // If the mask is extracting all bits from X or Y as is, we can skip
2184 // this AND op.
2185 if (EffBitsY.isSubsetOf(*Mask) && !EffBitsX.intersects(*Mask))
2186 return Y;
2187 if (EffBitsX.isSubsetOf(*Mask) && !EffBitsY.intersects(*Mask))
2188 return XShifted;
2189 }
2190 }
2191
2192 // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
2193 // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
2195 if (match(Op0, m_c_Xor(m_Value(X),
2197 m_c_Or(m_Deferred(X), m_Value(Y))))) &&
2199 return Constant::getNullValue(Op0->getType());
2200
2201 const APInt *C1;
2202 Value *A;
2203 // (A ^ C) & (A ^ ~C) -> 0
2204 if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2205 match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2206 return Constant::getNullValue(Op0->getType());
2207
2208 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2209 if (std::optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) {
2210 // If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
2211 if (*Implied == true)
2212 return Op0;
2213 // If Op0 is true implies Op1 is false, then they are not true together.
2214 if (*Implied == false)
2215 return ConstantInt::getFalse(Op0->getType());
2216 }
2217 if (std::optional<bool> Implied = isImpliedCondition(Op1, Op0, Q.DL)) {
2218 // If Op1 is true implies Op0 is true, then Op1 is a subset of Op0.
2219 if (*Implied)
2220 return Op1;
2221 // If Op1 is true implies Op0 is false, then they are not true together.
2222 if (!*Implied)
2223 return ConstantInt::getFalse(Op1->getType());
2224 }
2225 }
2226
2227 if (Value *V = simplifyByDomEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2228 return V;
2229
2230 return nullptr;
2231}
2232
2234 return ::simplifyAndInst(Op0, Op1, Q, RecursionLimit);
2235}
2236
2237// TODO: Many of these folds could use LogicalAnd/LogicalOr.
2239 assert(X->getType() == Y->getType() && "Expected same type for 'or' ops");
2240 Type *Ty = X->getType();
2241
2242 // X | ~X --> -1
2243 if (match(Y, m_Not(m_Specific(X))))
2245
2246 // X | ~(X & ?) = -1
2247 if (match(Y, m_Not(m_c_And(m_Specific(X), m_Value()))))
2249
2250 // X | (X & ?) --> X
2251 if (match(Y, m_c_And(m_Specific(X), m_Value())))
2252 return X;
2253
2254 Value *A, *B;
2255
2256 // (A ^ B) | (A | B) --> A | B
2257 // (A ^ B) | (B | A) --> B | A
2258 if (match(X, m_Xor(m_Value(A), m_Value(B))) &&
2260 return Y;
2261
2262 // ~(A ^ B) | (A | B) --> -1
2263 // ~(A ^ B) | (B | A) --> -1
2264 if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) &&
2267
2268 // (A & ~B) | (A ^ B) --> A ^ B
2269 // (~B & A) | (A ^ B) --> A ^ B
2270 // (A & ~B) | (B ^ A) --> B ^ A
2271 // (~B & A) | (B ^ A) --> B ^ A
2272 if (match(X, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2274 return Y;
2275
2276 // (~A ^ B) | (A & B) --> ~A ^ B
2277 // (B ^ ~A) | (A & B) --> B ^ ~A
2278 // (~A ^ B) | (B & A) --> ~A ^ B
2279 // (B ^ ~A) | (B & A) --> B ^ ~A
2280 if (match(X, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2282 return X;
2283
2284 // (~A | B) | (A ^ B) --> -1
2285 // (~A | B) | (B ^ A) --> -1
2286 // (B | ~A) | (A ^ B) --> -1
2287 // (B | ~A) | (B ^ A) --> -1
2288 if (match(X, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2291
2292 // (~A & B) | ~(A | B) --> ~A
2293 // (~A & B) | ~(B | A) --> ~A
2294 // (B & ~A) | ~(A | B) --> ~A
2295 // (B & ~A) | ~(B | A) --> ~A
2296 Value *NotA;
2298 m_Value(B))) &&
2300 return NotA;
2301 // The same is true of Logical And
2302 // TODO: This could share the logic of the version above if there was a
2303 // version of LogicalAnd that allowed more than just i1 types.
2305 m_Value(B))) &&
2307 return NotA;
2308
2309 // ~(A ^ B) | (A & B) --> ~(A ^ B)
2310 // ~(A ^ B) | (B & A) --> ~(A ^ B)
2311 Value *NotAB;
2313 m_Value(NotAB))) &&
2315 return NotAB;
2316
2317 // ~(A & B) | (A ^ B) --> ~(A & B)
2318 // ~(A & B) | (B ^ A) --> ~(A & B)
2320 m_Value(NotAB))) &&
2322 return NotAB;
2323
2324 return nullptr;
2325}
2326
2327/// Given operands for an Or, see if we can fold the result.
2328/// If not, this returns null.
2329static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2330 unsigned MaxRecurse) {
2331 if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q))
2332 return C;
2333
2334 // X | poison -> poison
2335 if (isa<PoisonValue>(Op1))
2336 return Op1;
2337
2338 // X | undef -> -1
2339 // X | -1 = -1
2340 // Do not return Op1 because it may contain undef elements if it's a vector.
2341 if (Q.isUndefValue(Op1) || match(Op1, m_AllOnes()))
2342 return Constant::getAllOnesValue(Op0->getType());
2343
2344 // X | X = X
2345 // X | 0 = X
2346 if (Op0 == Op1 || match(Op1, m_Zero()))
2347 return Op0;
2348
2349 if (Value *R = simplifyOrLogic(Op0, Op1))
2350 return R;
2351 if (Value *R = simplifyOrLogic(Op1, Op0))
2352 return R;
2353
2354 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Or))
2355 return V;
2356
2357 // Rotated -1 is still -1:
2358 // (-1 << X) | (-1 >> (C - X)) --> -1
2359 // (-1 >> X) | (-1 << (C - X)) --> -1
2360 // ...with C <= bitwidth (and commuted variants).
2361 Value *X, *Y;
2362 if ((match(Op0, m_Shl(m_AllOnes(), m_Value(X))) &&
2363 match(Op1, m_LShr(m_AllOnes(), m_Value(Y)))) ||
2364 (match(Op1, m_Shl(m_AllOnes(), m_Value(X))) &&
2365 match(Op0, m_LShr(m_AllOnes(), m_Value(Y))))) {
2366 const APInt *C;
2367 if ((match(X, m_Sub(m_APInt(C), m_Specific(Y))) ||
2368 match(Y, m_Sub(m_APInt(C), m_Specific(X)))) &&
2369 C->ule(X->getType()->getScalarSizeInBits())) {
2370 return ConstantInt::getAllOnesValue(X->getType());
2371 }
2372 }
2373
2374 // A funnel shift (rotate) can be decomposed into simpler shifts. See if we
2375 // are mixing in another shift that is redundant with the funnel shift.
2376
2377 // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y
2378 // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y
2379 if (match(Op0,
2381 match(Op1, m_Shl(m_Specific(X), m_Specific(Y))))
2382 return Op0;
2383 if (match(Op1,
2385 match(Op0, m_Shl(m_Specific(X), m_Specific(Y))))
2386 return Op1;
2387
2388 // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y
2389 // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y
2390 if (match(Op0,
2392 match(Op1, m_LShr(m_Specific(X), m_Specific(Y))))
2393 return Op0;
2394 if (match(Op1,
2396 match(Op0, m_LShr(m_Specific(X), m_Specific(Y))))
2397 return Op1;
2398
2399 if (Value *V =
2400 simplifyAndOrWithICmpEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2401 return V;
2402 if (Value *V =
2403 simplifyAndOrWithICmpEq(Instruction::Or, Op1, Op0, Q, MaxRecurse))
2404 return V;
2405
2406 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false))
2407 return V;
2408
2409 // If we have a multiplication overflow check that is being 'and'ed with a
2410 // check that one of the multipliers is not zero, we can omit the 'and', and
2411 // only keep the overflow check.
2412 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, false))
2413 return Op1;
2414 if (isCheckForZeroAndMulWithOverflow(Op1, Op0, false))
2415 return Op0;
2416
2417 // Try some generic simplifications for associative operations.
2418 if (Value *V =
2419 simplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2420 return V;
2421
2422 // Or distributes over And. Try some generic simplifications based on this.
2423 if (Value *V = expandCommutativeBinOp(Instruction::Or, Op0, Op1,
2424 Instruction::And, Q, MaxRecurse))
2425 return V;
2426
2427 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2428 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2429 // A | (A || B) -> A || B
2430 if (match(Op1, m_Select(m_Specific(Op0), m_One(), m_Value())))
2431 return Op1;
2432 else if (match(Op0, m_Select(m_Specific(Op1), m_One(), m_Value())))
2433 return Op0;
2434 }
2435 // If the operation is with the result of a select instruction, check
2436 // whether operating on either branch of the select always yields the same
2437 // value.
2438 if (Value *V =
2439 threadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2440 return V;
2441 }
2442
2443 // (A & C1)|(B & C2)
2444 Value *A, *B;
2445 const APInt *C1, *C2;
2446 if (match(Op0, m_And(m_Value(A), m_APInt(C1))) &&
2447 match(Op1, m_And(m_Value(B), m_APInt(C2)))) {
2448 if (*C1 == ~*C2) {
2449 // (A & C1)|(B & C2)
2450 // If we have: ((V + N) & C1) | (V & C2)
2451 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
2452 // replace with V+N.
2453 Value *N;
2454 if (C2->isMask() && // C2 == 0+1+
2456 // Add commutes, try both ways.
2457 if (MaskedValueIsZero(N, *C2, Q))
2458 return A;
2459 }
2460 // Or commutes, try both ways.
2461 if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))) {
2462 // Add commutes, try both ways.
2463 if (MaskedValueIsZero(N, *C1, Q))
2464 return B;
2465 }
2466 }
2467 }
2468
2469 // If the operation is with the result of a phi instruction, check whether
2470 // operating on all incoming values of the phi always yields the same value.
2471 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2472 if (Value *V = threadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2473 return V;
2474
2475 // (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one.
2476 if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2477 match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2478 return Constant::getAllOnesValue(Op0->getType());
2479
2480 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2481 if (std::optional<bool> Implied =
2482 isImpliedCondition(Op0, Op1, Q.DL, false)) {
2483 // If Op0 is false implies Op1 is false, then Op1 is a subset of Op0.
2484 if (*Implied == false)
2485 return Op0;
2486 // If Op0 is false implies Op1 is true, then at least one is always true.
2487 if (*Implied == true)
2488 return ConstantInt::getTrue(Op0->getType());
2489 }
2490 if (std::optional<bool> Implied =
2491 isImpliedCondition(Op1, Op0, Q.DL, false)) {
2492 // If Op1 is false implies Op0 is false, then Op0 is a subset of Op1.
2493 if (*Implied == false)
2494 return Op1;
2495 // If Op1 is false implies Op0 is true, then at least one is always true.
2496 if (*Implied == true)
2497 return ConstantInt::getTrue(Op1->getType());
2498 }
2499 }
2500
2501 if (Value *V = simplifyByDomEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2502 return V;
2503
2504 return nullptr;
2505}
2506
2508 return ::simplifyOrInst(Op0, Op1, Q, RecursionLimit);
2509}
2510
2511/// Given operands for a Xor, see if we can fold the result.
2512/// If not, this returns null.
2513static Value *simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2514 unsigned MaxRecurse) {
2515 if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q))
2516 return C;
2517
2518 // X ^ poison -> poison
2519 if (isa<PoisonValue>(Op1))
2520 return Op1;
2521
2522 // A ^ undef -> undef
2523 if (Q.isUndefValue(Op1))
2524 return Op1;
2525
2526 // A ^ 0 = A
2527 if (match(Op1, m_Zero()))
2528 return Op0;
2529
2530 // A ^ A = 0
2531 if (Op0 == Op1)
2532 return Constant::getNullValue(Op0->getType());
2533
2534 // A ^ ~A = ~A ^ A = -1
2535 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
2536 return Constant::getAllOnesValue(Op0->getType());
2537
2538 auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
2539 Value *A, *B;
2540 // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
2541 if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
2543 return A;
2544
2545 // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
2546 // The 'not' op must contain a complete -1 operand (no undef elements for
2547 // vector) for the transform to be safe.
2548 Value *NotA;
2550 m_Value(B))) &&
2552 return NotA;
2553
2554 return nullptr;
2555 };
2556 if (Value *R = foldAndOrNot(Op0, Op1))
2557 return R;
2558 if (Value *R = foldAndOrNot(Op1, Op0))
2559 return R;
2560
2561 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor))
2562 return V;
2563
2564 // Try some generic simplifications for associative operations.
2565 if (Value *V =
2566 simplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2567 return V;
2568
2569 // Threading Xor over selects and phi nodes is pointless, so don't bother.
2570 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2571 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2572 // only if B and C are equal. If B and C are equal then (since we assume
2573 // that operands have already been simplified) "select(cond, B, C)" should
2574 // have been simplified to the common value of B and C already. Analysing
2575 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
2576 // for threading over phi nodes.
2577
2578 if (Value *V = simplifyByDomEq(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2579 return V;
2580
2581 // (xor (sub nuw C_Mask, X), C_Mask) -> X
2582 {
2583 Value *X;
2584 if (match(Op0, m_NUWSub(m_Specific(Op1), m_Value(X))) &&
2585 match(Op1, m_LowBitMask()))
2586 return X;
2587 }
2588
2589 return nullptr;
2590}
2591
2593 return ::simplifyXorInst(Op0, Op1, Q, RecursionLimit);
2594}
2595
2597 return CmpInst::makeCmpResultType(Op->getType());
2598}
2599
2600/// Rummage around inside V looking for something equivalent to the comparison
2601/// "LHS Pred RHS". Return such a value if found, otherwise return null.
2602/// Helper function for analyzing max/min idioms.
2604 Value *LHS, Value *RHS) {
2606 if (!SI)
2607 return nullptr;
2608 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2609 if (!Cmp)
2610 return nullptr;
2611 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2612 if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2613 return Cmp;
2614 if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2615 LHS == CmpRHS && RHS == CmpLHS)
2616 return Cmp;
2617 return nullptr;
2618}
2619
2620/// Return true if the underlying object (storage) must be disjoint from
2621/// storage returned by any noalias return call.
2622static bool isAllocDisjoint(const Value *V) {
2623 // For allocas, we consider only static ones (dynamic
2624 // allocas might be transformed into calls to malloc not simultaneously
2625 // live with the compared-to allocation). For globals, we exclude symbols
2626 // that might be resolve lazily to symbols in another dynamically-loaded
2627 // library (and, thus, could be malloc'ed by the implementation).
2628 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2629 return AI->isStaticAlloca();
2630 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2631 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2632 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2633 !GV->isThreadLocal();
2634 if (const Argument *A = dyn_cast<Argument>(V))
2635 return A->hasByValAttr();
2636 return false;
2637}
2638
2639/// Return true if V1 and V2 are each the base of some distict storage region
2640/// [V, object_size(V)] which do not overlap. Note that zero sized regions
2641/// *are* possible, and that zero sized regions do not overlap with any other.
2642static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
2643 // Global variables always exist, so they always exist during the lifetime
2644 // of each other and all allocas. Global variables themselves usually have
2645 // non-overlapping storage, but since their addresses are constants, the
2646 // case involving two globals does not reach here and is instead handled in
2647 // constant folding.
2648 //
2649 // Two different allocas usually have different addresses...
2650 //
2651 // However, if there's an @llvm.stackrestore dynamically in between two
2652 // allocas, they may have the same address. It's tempting to reduce the
2653 // scope of the problem by only looking at *static* allocas here. That would
2654 // cover the majority of allocas while significantly reducing the likelihood
2655 // of having an @llvm.stackrestore pop up in the middle. However, it's not
2656 // actually impossible for an @llvm.stackrestore to pop up in the middle of
2657 // an entry block. Also, if we have a block that's not attached to a
2658 // function, we can't tell if it's "static" under the current definition.
2659 // Theoretically, this problem could be fixed by creating a new kind of
2660 // instruction kind specifically for static allocas. Such a new instruction
2661 // could be required to be at the top of the entry block, thus preventing it
2662 // from being subject to a @llvm.stackrestore. Instcombine could even
2663 // convert regular allocas into these special allocas. It'd be nifty.
2664 // However, until then, this problem remains open.
2665 //
2666 // So, we'll assume that two non-empty allocas have different addresses
2667 // for now.
2668 auto isByValArg = [](const Value *V) {
2669 const Argument *A = dyn_cast<Argument>(V);
2670 return A && A->hasByValAttr();
2671 };
2672
2673 // Byval args are backed by store which does not overlap with each other,
2674 // allocas, or globals.
2675 if (isByValArg(V1))
2676 return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2677 if (isByValArg(V2))
2678 return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2679
2680 return isa<AllocaInst>(V1) &&
2682}
2683
2684// A significant optimization not implemented here is assuming that alloca
2685// addresses are not equal to incoming argument values. They don't *alias*,
2686// as we say, but that doesn't mean they aren't equal, so we take a
2687// conservative approach.
2688//
2689// This is inspired in part by C++11 5.10p1:
2690// "Two pointers of the same type compare equal if and only if they are both
2691// null, both point to the same function, or both represent the same
2692// address."
2693//
2694// This is pretty permissive.
2695//
2696// It's also partly due to C11 6.5.9p6:
2697// "Two pointers compare equal if and only if both are null pointers, both are
2698// pointers to the same object (including a pointer to an object and a
2699// subobject at its beginning) or function, both are pointers to one past the
2700// last element of the same array object, or one is a pointer to one past the
2701// end of one array object and the other is a pointer to the start of a
2702// different array object that happens to immediately follow the first array
2703// object in the address space.)
2704//
2705// C11's version is more restrictive, however there's no reason why an argument
2706// couldn't be a one-past-the-end value for a stack object in the caller and be
2707// equal to the beginning of a stack object in the callee.
2708//
2709// If the C and C++ standards are ever made sufficiently restrictive in this
2710// area, it may be possible to update LLVM's semantics accordingly and reinstate
2711// this optimization.
2713 const SimplifyQuery &Q) {
2714 assert(LHS->getType() == RHS->getType() && "Must have same types");
2715 const DataLayout &DL = Q.DL;
2716 const TargetLibraryInfo *TLI = Q.TLI;
2717
2718 // We fold equality and unsigned predicates on pointer comparisons, but forbid
2719 // signed predicates since a GEP with inbounds could cross the sign boundary.
2720 if (CmpInst::isSigned(Pred))
2721 return nullptr;
2722
2723 // We have to switch to a signed predicate to handle negative indices from
2724 // the base pointer.
2725 Pred = ICmpInst::getSignedPredicate(Pred);
2726
2727 // Strip off any constant offsets so that we can reason about them.
2728 // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2729 // here and compare base addresses like AliasAnalysis does, however there are
2730 // numerous hazards. AliasAnalysis and its utilities rely on special rules
2731 // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2732 // doesn't need to guarantee pointer inequality when it says NoAlias.
2733
2734 // Even if an non-inbounds GEP occurs along the path we can still optimize
2735 // equality comparisons concerning the result.
2736 bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2737 unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
2738 APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2739 LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
2740 RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);
2741
2742 // If LHS and RHS are related via constant offsets to the same base
2743 // value, we can replace it with an icmp which just compares the offsets.
2744 if (LHS == RHS)
2745 return ConstantInt::get(getCompareTy(LHS),
2746 ICmpInst::compare(LHSOffset, RHSOffset, Pred));
2747
2748 // Various optimizations for (in)equality comparisons.
2749 if (ICmpInst::isEquality(Pred)) {
2750 // Different non-empty allocations that exist at the same time have
2751 // different addresses (if the program can tell). If the offsets are
2752 // within the bounds of their allocations (and not one-past-the-end!
2753 // so we can't use inbounds!), and their allocations aren't the same,
2754 // the pointers are not equal.
2756 uint64_t LHSSize, RHSSize;
2757 ObjectSizeOpts Opts;
2759 auto *F = [](Value *V) -> Function * {
2760 if (auto *I = dyn_cast<Instruction>(V))
2761 return I->getFunction();
2762 if (auto *A = dyn_cast<Argument>(V))
2763 return A->getParent();
2764 return nullptr;
2765 }(LHS);
2766 Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
2767 if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && LHSSize != 0 &&
2768 getObjectSize(RHS, RHSSize, DL, TLI, Opts) && RHSSize != 0) {
2769 APInt Dist = LHSOffset - RHSOffset;
2770 if (Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize))
2771 return ConstantInt::get(getCompareTy(LHS),
2773 }
2774 }
2775
2776 // If one side of the equality comparison must come from a noalias call
2777 // (meaning a system memory allocation function), and the other side must
2778 // come from a pointer that cannot overlap with dynamically-allocated
2779 // memory within the lifetime of the current function (allocas, byval
2780 // arguments, globals), then determine the comparison result here.
2781 SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2782 getUnderlyingObjects(LHS, LHSUObjs);
2783 getUnderlyingObjects(RHS, RHSUObjs);
2784
2785 // Is the set of underlying objects all noalias calls?
2786 auto IsNAC = [](ArrayRef<const Value *> Objects) {
2787 return all_of(Objects, isNoAliasCall);
2788 };
2789
2790 // Is the set of underlying objects all things which must be disjoint from
2791 // noalias calls. We assume that indexing from such disjoint storage
2792 // into the heap is undefined, and thus offsets can be safely ignored.
2793 auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2794 return all_of(Objects, ::isAllocDisjoint);
2795 };
2796
2797 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2798 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2799 return ConstantInt::get(getCompareTy(LHS),
2801
2802 // Fold comparisons for non-escaping pointer even if the allocation call
2803 // cannot be elided. We cannot fold malloc comparison to null. Also, the
2804 // dynamic allocation call could be either of the operands. Note that
2805 // the other operand can not be based on the alloc - if it were, then
2806 // the cmp itself would be a capture.
2807 Value *MI = nullptr;
2808 if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonZero(RHS, Q))
2809 MI = LHS;
2810 else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonZero(LHS, Q))
2811 MI = RHS;
2812 if (MI) {
2813 // FIXME: This is incorrect, see PR54002. While we can assume that the
2814 // allocation is at an address that makes the comparison false, this
2815 // requires that *all* comparisons to that address be false, which
2816 // InstSimplify cannot guarantee.
2817 struct CustomCaptureTracker : public CaptureTracker {
2818 bool Captured = false;
2819 void tooManyUses() override { Captured = true; }
2820 Action captured(const Use *U, UseCaptureInfo CI) override {
2821 // TODO(captures): Use UseCaptureInfo.
2822 if (auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2823 // Comparison against value stored in global variable. Given the
2824 // pointer does not escape, its value cannot be guessed and stored
2825 // separately in a global variable.
2826 unsigned OtherIdx = 1 - U->getOperandNo();
2827 auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2828 if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2829 return Continue;
2830 }
2831
2832 Captured = true;
2833 return Stop;
2834 }
2835 };
2836 CustomCaptureTracker Tracker;
2837 PointerMayBeCaptured(MI, &Tracker);
2838 if (!Tracker.Captured)
2839 return ConstantInt::get(getCompareTy(LHS),
2841 }
2842 }
2843
2844 // Otherwise, fail.
2845 return nullptr;
2846}
2847
2848/// Fold an icmp when its operands have i1 scalar type.
2850 const SimplifyQuery &Q) {
2851 Type *ITy = getCompareTy(LHS); // The return type.
2852 Type *OpTy = LHS->getType(); // The operand type.
2853 if (!OpTy->isIntOrIntVectorTy(1))
2854 return nullptr;
2855
2856 // A boolean compared to true/false can be reduced in 14 out of the 20
2857 // (10 predicates * 2 constants) possible combinations. The other
2858 // 6 cases require a 'not' of the LHS.
2859
2860 auto ExtractNotLHS = [](Value *V) -> Value * {
2861 Value *X;
2862 if (match(V, m_Not(m_Value(X))))
2863 return X;
2864 return nullptr;
2865 };
2866
2867 if (match(RHS, m_Zero())) {
2868 switch (Pred) {
2869 case CmpInst::ICMP_NE: // X != 0 -> X
2870 case CmpInst::ICMP_UGT: // X >u 0 -> X
2871 case CmpInst::ICMP_SLT: // X <s 0 -> X
2872 return LHS;
2873
2874 case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
2875 case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2876 case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2877 if (Value *X = ExtractNotLHS(LHS))
2878 return X;
2879 break;
2880
2881 case CmpInst::ICMP_ULT: // X <u 0 -> false
2882 case CmpInst::ICMP_SGT: // X >s 0 -> false
2883 return getFalse(ITy);
2884
2885 case CmpInst::ICMP_UGE: // X >=u 0 -> true
2886 case CmpInst::ICMP_SLE: // X <=s 0 -> true
2887 return getTrue(ITy);
2888
2889 default:
2890 break;
2891 }
2892 } else if (match(RHS, m_One())) {
2893 switch (Pred) {
2894 case CmpInst::ICMP_EQ: // X == 1 -> X
2895 case CmpInst::ICMP_UGE: // X >=u 1 -> X
2896 case CmpInst::ICMP_SLE: // X <=s -1 -> X
2897 return LHS;
2898
2899 case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
2900 case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
2901 case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
2902 if (Value *X = ExtractNotLHS(LHS))
2903 return X;
2904 break;
2905
2906 case CmpInst::ICMP_UGT: // X >u 1 -> false
2907 case CmpInst::ICMP_SLT: // X <s -1 -> false
2908 return getFalse(ITy);
2909
2910 case CmpInst::ICMP_ULE: // X <=u 1 -> true
2911 case CmpInst::ICMP_SGE: // X >=s -1 -> true
2912 return getTrue(ITy);
2913
2914 default:
2915 break;
2916 }
2917 }
2918
2919 switch (Pred) {
2920 default:
2921 break;
2922 case ICmpInst::ICMP_UGE:
2923 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2924 return getTrue(ITy);
2925 break;
2926 case ICmpInst::ICMP_SGE:
2927 /// For signed comparison, the values for an i1 are 0 and -1
2928 /// respectively. This maps into a truth table of:
2929 /// LHS | RHS | LHS >=s RHS | LHS implies RHS
2930 /// 0 | 0 | 1 (0 >= 0) | 1
2931 /// 0 | 1 | 1 (0 >= -1) | 1
2932 /// 1 | 0 | 0 (-1 >= 0) | 0
2933 /// 1 | 1 | 1 (-1 >= -1) | 1
2934 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2935 return getTrue(ITy);
2936 break;
2937 case ICmpInst::ICMP_ULE:
2938 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2939 return getTrue(ITy);
2940 break;
2941 case ICmpInst::ICMP_SLE:
2942 /// SLE follows the same logic as SGE with the LHS and RHS swapped.
2943 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2944 return getTrue(ITy);
2945 break;
2946 }
2947
2948 return nullptr;
2949}
2950
2951/// Try hard to fold icmp with zero RHS because this is a common case.
2953 const SimplifyQuery &Q) {
2954 if (!match(RHS, m_Zero()))
2955 return nullptr;
2956
2957 Type *ITy = getCompareTy(LHS); // The return type.
2958 switch (Pred) {
2959 default:
2960 llvm_unreachable("Unknown ICmp predicate!");
2961 case ICmpInst::ICMP_ULT:
2962 return getFalse(ITy);
2963 case ICmpInst::ICMP_UGE:
2964 return getTrue(ITy);
2965 case ICmpInst::ICMP_EQ:
2966 case ICmpInst::ICMP_ULE:
2967 if (isKnownNonZero(LHS, Q))
2968 return getFalse(ITy);
2969 break;
2970 case ICmpInst::ICMP_NE:
2971 case ICmpInst::ICMP_UGT:
2972 if (isKnownNonZero(LHS, Q))
2973 return getTrue(ITy);
2974 break;
2975 case ICmpInst::ICMP_SLT: {
2976 KnownBits LHSKnown = computeKnownBits(LHS, Q);
2977 if (LHSKnown.isNegative())
2978 return getTrue(ITy);
2979 if (LHSKnown.isNonNegative())
2980 return getFalse(ITy);
2981 break;
2982 }
2983 case ICmpInst::ICMP_SLE: {
2984 KnownBits LHSKnown = computeKnownBits(LHS, Q);
2985 if (LHSKnown.isNegative())
2986 return getTrue(ITy);
2987 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2988 return getFalse(ITy);
2989 break;
2990 }
2991 case ICmpInst::ICMP_SGE: {
2992 KnownBits LHSKnown = computeKnownBits(LHS, Q);
2993 if (LHSKnown.isNegative())
2994 return getFalse(ITy);
2995 if (LHSKnown.isNonNegative())
2996 return getTrue(ITy);
2997 break;
2998 }
2999 case ICmpInst::ICMP_SGT: {
3000 KnownBits LHSKnown = computeKnownBits(LHS, Q);
3001 if (LHSKnown.isNegative())
3002 return getFalse(ITy);
3003 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
3004 return getTrue(ITy);
3005 break;
3006 }
3007 }
3008
3009 return nullptr;
3010}
3011
3013 Value *RHS, const SimplifyQuery &Q) {
3014 Type *ITy = getCompareTy(RHS); // The return type.
3015
3016 Value *X;
3017 const APInt *C;
3018 if (!match(RHS, m_APIntAllowPoison(C)))
3019 return nullptr;
3020
3021 // Sign-bit checks can be optimized to true/false after unsigned
3022 // floating-point casts:
3023 // icmp slt (bitcast (uitofp X)), 0 --> false
3024 // icmp sgt (bitcast (uitofp X)), -1 --> true
3026 bool TrueIfSigned;
3027 if (isSignBitCheck(Pred, *C, TrueIfSigned))
3028 return ConstantInt::getBool(ITy, !TrueIfSigned);
3029 }
3030
3031 // Rule out tautological comparisons (eg., ult 0 or uge 0).
3033 if (RHS_CR.isEmptySet())
3034 return ConstantInt::getFalse(ITy);
3035 if (RHS_CR.isFullSet())
3036 return ConstantInt::getTrue(ITy);
3037
3038 ConstantRange LHS_CR =
3040 if (!LHS_CR.isFullSet()) {
3041 if (RHS_CR.contains(LHS_CR))
3042 return ConstantInt::getTrue(ITy);
3043 if (RHS_CR.inverse().contains(LHS_CR))
3044 return ConstantInt::getFalse(ITy);
3045 }
3046
3047 // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
3048 // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
3049 const APInt *MulC;
3050 if (Q.IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
3052 *MulC != 0 && C->urem(*MulC) != 0) ||
3054 *MulC != 0 && C->srem(*MulC) != 0)))
3055 return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
3056
3057 if (Pred == ICmpInst::ICMP_UGE && C->isOne() && isKnownNonZero(LHS, Q))
3058 return ConstantInt::getTrue(ITy);
3059
3060 return nullptr;
3061}
3062
3064
3065/// Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq).
3068 const SimplifyQuery &Q,
3069 unsigned Depth = 0) {
3070 if (!Res.insert(V).second)
3071 return;
3072
3073 // Can be increased if useful.
3074 if (++Depth > 1)
3075 return;
3076
3077 auto *I = dyn_cast<Instruction>(V);
3078 if (!I)
3079 return;
3080
3081 Value *X, *Y;
3083 if (match(I, m_Or(m_Value(X), m_Value(Y))) ||
3087 }
3088 // X * Y >= X --> true
3089 if (match(I, m_NUWMul(m_Value(X), m_Value(Y)))) {
3090 if (isKnownNonZero(X, Q))
3092 if (isKnownNonZero(Y, Q))
3094 }
3095 } else {
3097 switch (I->getOpcode()) {
3098 case Instruction::And:
3099 getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Q, Depth);
3100 getUnsignedMonotonicValues(Res, I->getOperand(1), Type, Q, Depth);
3101 break;
3102 case Instruction::URem:
3103 case Instruction::UDiv:
3104 case Instruction::LShr:
3105 getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Q, Depth);
3106 break;
3107 case Instruction::Call:
3110 break;
3111 default:
3112 break;
3113 }
3114 }
3115}
3116
3118 Value *RHS,
3119 const SimplifyQuery &Q) {
3120 if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_ULT)
3121 return nullptr;
3122
3123 // We have LHS uge GreaterValues and LowerValues uge RHS. If any of the
3124 // GreaterValues and LowerValues are the same, it follows that LHS uge RHS.
3125 SmallPtrSet<Value *, 4> GreaterValues;
3126 SmallPtrSet<Value *, 4> LowerValues;
3129 for (Value *GV : GreaterValues)
3130 if (LowerValues.contains(GV))
3132 Pred == ICmpInst::ICMP_UGE);
3133 return nullptr;
3134}
3135
3137 Value *RHS, const SimplifyQuery &Q,
3138 unsigned MaxRecurse) {
3139 Type *ITy = getCompareTy(RHS); // The return type.
3140
3141 Value *Y = nullptr;
3142 // icmp pred (or X, Y), X
3143 if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
3144 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3145 KnownBits RHSKnown = computeKnownBits(RHS, Q);
3146 KnownBits YKnown = computeKnownBits(Y, Q);
3147 if (RHSKnown.isNonNegative() && YKnown.isNegative())
3148 return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
3149 if (RHSKnown.isNegative() || YKnown.isNonNegative())
3150 return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
3151 }
3152 }
3153
3154 // icmp pred (urem X, Y), Y
3155 if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
3156 switch (Pred) {
3157 default:
3158 break;
3159 case ICmpInst::ICMP_SGT:
3160 case ICmpInst::ICMP_SGE: {
3161 KnownBits Known = computeKnownBits(RHS, Q);
3162 if (!Known.isNonNegative())
3163 break;
3164 [[fallthrough]];
3165 }
3166 case ICmpInst::ICMP_EQ:
3167 case ICmpInst::ICMP_UGT:
3168 case ICmpInst::ICMP_UGE:
3169 return getFalse(ITy);
3170 case ICmpInst::ICMP_SLT:
3171 case ICmpInst::ICMP_SLE: {
3172 KnownBits Known = computeKnownBits(RHS, Q);
3173 if (!Known.isNonNegative())
3174 break;
3175 [[fallthrough]];
3176 }
3177 case ICmpInst::ICMP_NE:
3178 case ICmpInst::ICMP_ULT:
3179 case ICmpInst::ICMP_ULE:
3180 return getTrue(ITy);
3181 }
3182 }
3183
3184 // If x is nonzero:
3185 // x >>u C <u x --> true for C != 0.
3186 // x >>u C != x --> true for C != 0.
3187 // x >>u C >=u x --> false for C != 0.
3188 // x >>u C == x --> false for C != 0.
3189 // x udiv C <u x --> true for C != 1.
3190 // x udiv C != x --> true for C != 1.
3191 // x udiv C >=u x --> false for C != 1.
3192 // x udiv C == x --> false for C != 1.
3193 // TODO: allow non-constant shift amount/divisor
3194 const APInt *C;
3195 if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 0) ||
3196 (match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C))) && *C != 1)) {
3197 if (isKnownNonZero(RHS, Q)) {
3198 switch (Pred) {
3199 default:
3200 break;
3201 case ICmpInst::ICMP_EQ:
3202 case ICmpInst::ICMP_UGE:
3203 case ICmpInst::ICMP_UGT:
3204 return getFalse(ITy);
3205 case ICmpInst::ICMP_NE:
3206 case ICmpInst::ICMP_ULT:
3207 case ICmpInst::ICMP_ULE:
3208 return getTrue(ITy);
3209 }
3210 }
3211 }
3212
3213 // (x*C1)/C2 <= x for C1 <= C2.
3214 // This holds even if the multiplication overflows: Assume that x != 0 and
3215 // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
3216 // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3217 //
3218 // Additionally, either the multiplication and division might be represented
3219 // as shifts:
3220 // (x*C1)>>C2 <= x for C1 < 2**C2.
3221 // (x<<C1)/C2 <= x for 2**C1 < C2.
3222 const APInt *C1, *C2;
3223 if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3224 C1->ule(*C2)) ||
3225 (match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3226 C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
3227 (match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3228 (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
3229 if (Pred == ICmpInst::ICMP_UGT)
3230 return getFalse(ITy);
3231 if (Pred == ICmpInst::ICMP_ULE)
3232 return getTrue(ITy);
3233 }
3234
3235 // (sub C, X) == X, C is odd --> false
3236 // (sub C, X) != X, C is odd --> true
3237 if (match(LBO, m_Sub(m_APIntAllowPoison(C), m_Specific(RHS))) &&
3238 (*C & 1) == 1 && ICmpInst::isEquality(Pred))
3239 return (Pred == ICmpInst::ICMP_EQ) ? getFalse(ITy) : getTrue(ITy);
3240
3241 return nullptr;
3242}
3243
3244// If only one of the icmp's operands has NSW flags, try to prove that:
3245//
3246// icmp slt (x + C1), (x +nsw C2)
3247//
3248// is equivalent to:
3249//
3250// icmp slt C1, C2
3251//
3252// which is true if x + C2 has the NSW flags set and:
3253// *) C1 < C2 && C1 >= 0, or
3254// *) C2 < C1 && C1 <= 0.
3255//
3257 const InstrInfoQuery &IIQ) {
3258 // TODO: only support icmp slt for now.
3259 if (Pred != CmpInst::ICMP_SLT || !IIQ.UseInstrInfo)
3260 return false;
3261
3262 // Canonicalize nsw add as RHS.
3263 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3264 std::swap(LHS, RHS);
3265 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3266 return false;
3267
3268 Value *X;
3269 const APInt *C1, *C2;
3270 if (!match(LHS, m_Add(m_Value(X), m_APInt(C1))) ||
3271 !match(RHS, m_Add(m_Specific(X), m_APInt(C2))))
3272 return false;
3273
3274 return (C1->slt(*C2) && C1->isNonNegative()) ||
3275 (C2->slt(*C1) && C1->isNonPositive());
3276}
3277
3278/// TODO: A large part of this logic is duplicated in InstCombine's
3279/// foldICmpBinOp(). We should be able to share that and avoid the code
3280/// duplication.
3282 const SimplifyQuery &Q,
3283 unsigned MaxRecurse) {
3286 if (MaxRecurse && (LBO || RBO)) {
3287 // Analyze the case when either LHS or RHS is an add instruction.
3288 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
3289 // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
3290 bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
3291 if (LBO && LBO->getOpcode() == Instruction::Add) {
3292 A = LBO->getOperand(0);
3293 B = LBO->getOperand(1);
3294 NoLHSWrapProblem =
3295 ICmpInst::isEquality(Pred) ||
3296 (CmpInst::isUnsigned(Pred) &&
3298 (CmpInst::isSigned(Pred) &&
3300 }
3301 if (RBO && RBO->getOpcode() == Instruction::Add) {
3302 C = RBO->getOperand(0);
3303 D = RBO->getOperand(1);
3304 NoRHSWrapProblem =
3305 ICmpInst::isEquality(Pred) ||
3306 (CmpInst::isUnsigned(Pred) &&
3308 (CmpInst::isSigned(Pred) &&
3310 }
3311
3312 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3313 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
3314 if (Value *V = simplifyICmpInst(Pred, A == RHS ? B : A,
3315 Constant::getNullValue(RHS->getType()), Q,
3316 MaxRecurse - 1))
3317 return V;
3318
3319 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3320 if ((C == LHS || D == LHS) && NoRHSWrapProblem)
3321 if (Value *V =
3323 C == LHS ? D : C, Q, MaxRecurse - 1))
3324 return V;
3325
3326 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3327 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3329 if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
3330 // Determine Y and Z in the form icmp (X+Y), (X+Z).
3331 Value *Y, *Z;
3332 if (A == C) {
3333 // C + B == C + D -> B == D
3334 Y = B;
3335 Z = D;
3336 } else if (A == D) {
3337 // D + B == C + D -> B == C
3338 Y = B;
3339 Z = C;
3340 } else if (B == C) {
3341 // A + C == C + D -> A == D
3342 Y = A;
3343 Z = D;
3344 } else {
3345 assert(B == D);
3346 // A + D == C + D -> A == C
3347 Y = A;
3348 Z = C;
3349 }
3350 if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
3351 return V;
3352 }
3353 }
3354
3355 if (LBO)
3356 if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
3357 return V;
3358
3359 if (RBO)
3361 ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse))
3362 return V;
3363
3364 // 0 - (zext X) pred C
3365 if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
3366 const APInt *C;
3367 if (match(RHS, m_APInt(C))) {
3368 if (C->isStrictlyPositive()) {
3369 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3371 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3373 }
3374 if (C->isNonNegative()) {
3375 if (Pred == ICmpInst::ICMP_SLE)
3377 if (Pred == ICmpInst::ICMP_SGT)
3379 }
3380 }
3381 }
3382
3383 // If C2 is a power-of-2 and C is not:
3384 // (C2 << X) == C --> false
3385 // (C2 << X) != C --> true
3386 const APInt *C;
3387 if (match(LHS, m_Shl(m_Power2(), m_Value())) &&
3388 match(RHS, m_APIntAllowPoison(C)) && !C->isPowerOf2()) {
3389 // C2 << X can equal zero in some circumstances.
3390 // This simplification might be unsafe if C is zero.
3391 //
3392 // We know it is safe if:
3393 // - The shift is nsw. We can't shift out the one bit.
3394 // - The shift is nuw. We can't shift out the one bit.
3395 // - C2 is one.
3396 // - C isn't zero.
3399 match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
3400 if (Pred == ICmpInst::ICMP_EQ)
3402 if (Pred == ICmpInst::ICMP_NE)
3404 }
3405 }
3406
3407 // If C is a power-of-2:
3408 // (C << X) >u 0x8000 --> false
3409 // (C << X) <=u 0x8000 --> true
3410 if (match(LHS, m_Shl(m_Power2(), m_Value())) && match(RHS, m_SignMask())) {
3411 if (Pred == ICmpInst::ICMP_UGT)
3413 if (Pred == ICmpInst::ICMP_ULE)
3415 }
3416
3417 if (!MaxRecurse || !LBO || !RBO || LBO->getOpcode() != RBO->getOpcode())
3418 return nullptr;
3419
3420 if (LBO->getOperand(0) == RBO->getOperand(0)) {
3421 switch (LBO->getOpcode()) {
3422 default:
3423 break;
3424 case Instruction::Shl: {
3425 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3426 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3427 if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3428 !isKnownNonZero(LBO->getOperand(0), Q))
3429 break;
3430 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1),
3431 RBO->getOperand(1), Q, MaxRecurse - 1))
3432 return V;
3433 break;
3434 }
3435 // If C1 & C2 == C1, A = X and/or C1, B = X and/or C2:
3436 // icmp ule A, B -> true
3437 // icmp ugt A, B -> false
3438 // icmp sle A, B -> true (C1 and C2 are the same sign)
3439 // icmp sgt A, B -> false (C1 and C2 are the same sign)
3440 case Instruction::And:
3441 case Instruction::Or: {
3442 const APInt *C1, *C2;
3443 if (ICmpInst::isRelational(Pred) &&
3444 match(LBO->getOperand(1), m_APInt(C1)) &&
3445 match(RBO->getOperand(1), m_APInt(C2))) {
3446 if (!C1->isSubsetOf(*C2)) {
3447 std::swap(C1, C2);
3448 Pred = ICmpInst::getSwappedPredicate(Pred);
3449 }
3450 if (C1->isSubsetOf(*C2)) {
3451 if (Pred == ICmpInst::ICMP_ULE)
3453 if (Pred == ICmpInst::ICMP_UGT)
3455 if (C1->isNonNegative() == C2->isNonNegative()) {
3456 if (Pred == ICmpInst::ICMP_SLE)
3458 if (Pred == ICmpInst::ICMP_SGT)
3460 }
3461 }
3462 }
3463 break;
3464 }
3465 }
3466 }
3467
3468 if (LBO->getOperand(1) == RBO->getOperand(1)) {
3469 switch (LBO->getOpcode()) {
3470 default:
3471 break;
3472 case Instruction::UDiv:
3473 case Instruction::LShr:
3474 if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO) ||
3475 !Q.IIQ.isExact(RBO))
3476 break;
3477 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3478 RBO->getOperand(0), Q, MaxRecurse - 1))
3479 return V;
3480 break;
3481 case Instruction::SDiv:
3482 if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) ||
3483 !Q.IIQ.isExact(RBO))
3484 break;
3485 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3486 RBO->getOperand(0), Q, MaxRecurse - 1))
3487 return V;
3488 break;
3489 case Instruction::AShr:
3490 if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO))
3491 break;
3492 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3493 RBO->getOperand(0), Q, MaxRecurse - 1))
3494 return V;
3495 break;
3496 case Instruction::Shl: {
3497 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3498 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3499 if (!NUW && !NSW)
3500 break;
3501 if (!NSW && ICmpInst::isSigned(Pred))
3502 break;
3503 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3504 RBO->getOperand(0), Q, MaxRecurse - 1))
3505 return V;
3506 break;
3507 }
3508 }
3509 }
3510 return nullptr;
3511}
3512
3513/// simplify integer comparisons where at least one operand of the compare
3514/// matches an integer min/max idiom.
3516 const SimplifyQuery &Q,
3517 unsigned MaxRecurse) {
3518 Type *ITy = getCompareTy(LHS); // The return type.
3519 Value *A, *B;
3521 CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
3522
3523 // Signed variants on "max(a,b)>=a -> true".
3524 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3525 if (A != RHS)
3526 std::swap(A, B); // smax(A, B) pred A.
3527 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3528 // We analyze this as smax(A, B) pred A.
3529 P = Pred;
3530 } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
3531 (A == LHS || B == LHS)) {
3532 if (A != LHS)
3533 std::swap(A, B); // A pred smax(A, B).
3534 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3535 // We analyze this as smax(A, B) swapped-pred A.
3537 } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
3538 (A == RHS || B == RHS)) {
3539 if (A != RHS)
3540 std::swap(A, B); // smin(A, B) pred A.
3541 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3542 // We analyze this as smax(-A, -B) swapped-pred -A.
3543 // Note that we do not need to actually form -A or -B thanks to EqP.
3545 } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
3546 (A == LHS || B == LHS)) {
3547 if (A != LHS)
3548 std::swap(A, B); // A pred smin(A, B).
3549 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3550 // We analyze this as smax(-A, -B) pred -A.
3551 // Note that we do not need to actually form -A or -B thanks to EqP.
3552 P = Pred;
3553 }
3555 // Cases correspond to "max(A, B) p A".
3556 switch (P) {
3557 default:
3558 break;
3559 case CmpInst::ICMP_EQ:
3560 case CmpInst::ICMP_SLE:
3561 // Equivalent to "A EqP B". This may be the same as the condition tested
3562 // in the max/min; if so, we can just return that.
3563 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3564 return V;
3565 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3566 return V;
3567 // Otherwise, see if "A EqP B" simplifies.
3568 if (MaxRecurse)
3569 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3570 return V;
3571 break;
3572 case CmpInst::ICMP_NE:
3573 case CmpInst::ICMP_SGT: {
3575 // Equivalent to "A InvEqP B". This may be the same as the condition
3576 // tested in the max/min; if so, we can just return that.
3577 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3578 return V;
3579 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3580 return V;
3581 // Otherwise, see if "A InvEqP B" simplifies.
3582 if (MaxRecurse)
3583 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3584 return V;
3585 break;
3586 }
3587 case CmpInst::ICMP_SGE:
3588 // Always true.
3589 return getTrue(ITy);
3590 case CmpInst::ICMP_SLT:
3591 // Always false.
3592 return getFalse(ITy);
3593 }
3594 }
3595
3596 // Unsigned variants on "max(a,b)>=a -> true".
3598 if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3599 if (A != RHS)
3600 std::swap(A, B); // umax(A, B) pred A.
3601 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3602 // We analyze this as umax(A, B) pred A.
3603 P = Pred;
3604 } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
3605 (A == LHS || B == LHS)) {
3606 if (A != LHS)
3607 std::swap(A, B); // A pred umax(A, B).
3608 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3609 // We analyze this as umax(A, B) swapped-pred A.
3611 } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3612 (A == RHS || B == RHS)) {
3613 if (A != RHS)
3614 std::swap(A, B); // umin(A, B) pred A.
3615 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3616 // We analyze this as umax(-A, -B) swapped-pred -A.
3617 // Note that we do not need to actually form -A or -B thanks to EqP.
3619 } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
3620 (A == LHS || B == LHS)) {
3621 if (A != LHS)
3622 std::swap(A, B); // A pred umin(A, B).
3623 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3624 // We analyze this as umax(-A, -B) pred -A.
3625 // Note that we do not need to actually form -A or -B thanks to EqP.
3626 P = Pred;
3627 }
3629 // Cases correspond to "max(A, B) p A".
3630 switch (P) {
3631 default:
3632 break;
3633 case CmpInst::ICMP_EQ:
3634 case CmpInst::ICMP_ULE:
3635 // Equivalent to "A EqP B". This may be the same as the condition tested
3636 // in the max/min; if so, we can just return that.
3637 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3638 return V;
3639 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3640 return V;
3641 // Otherwise, see if "A EqP B" simplifies.
3642 if (MaxRecurse)
3643 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3644 return V;
3645 break;
3646 case CmpInst::ICMP_NE:
3647 case CmpInst::ICMP_UGT: {
3649 // Equivalent to "A InvEqP B". This may be the same as the condition
3650 // tested in the max/min; if so, we can just return that.
3651 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3652 return V;
3653 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3654 return V;
3655 // Otherwise, see if "A InvEqP B" simplifies.
3656 if (MaxRecurse)
3657 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3658 return V;
3659 break;
3660 }
3661 case CmpInst::ICMP_UGE:
3662 return getTrue(ITy);
3663 case CmpInst::ICMP_ULT:
3664 return getFalse(ITy);
3665 }
3666 }
3667
3668 // Comparing 1 each of min/max with a common operand?
3669 // Canonicalize min operand to RHS.
3670 if (match(LHS, m_UMin(m_Value(), m_Value())) ||
3671 match(LHS, m_SMin(m_Value(), m_Value()))) {
3672 std::swap(LHS, RHS);
3673 Pred = ICmpInst::getSwappedPredicate(Pred);
3674 }
3675
3676 Value *C, *D;
3677 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
3678 match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
3679 (A == C || A == D || B == C || B == D)) {
3680 // smax(A, B) >=s smin(A, D) --> true
3681 if (Pred == CmpInst::ICMP_SGE)
3682 return getTrue(ITy);
3683 // smax(A, B) <s smin(A, D) --> false
3684 if (Pred == CmpInst::ICMP_SLT)
3685 return getFalse(ITy);
3686 } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3687 match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
3688 (A == C || A == D || B == C || B == D)) {
3689 // umax(A, B) >=u umin(A, D) --> true
3690 if (Pred == CmpInst::ICMP_UGE)
3691 return getTrue(ITy);
3692 // umax(A, B) <u umin(A, D) --> false
3693 if (Pred == CmpInst::ICMP_ULT)
3694 return getFalse(ITy);
3695 }
3696
3697 return nullptr;
3698}
3699
3701 Value *LHS, Value *RHS,
3702 const SimplifyQuery &Q) {
3703 // Gracefully handle instructions that have not been inserted yet.
3704 if (!Q.AC || !Q.CxtI)
3705 return nullptr;
3706
3707 for (Value *AssumeBaseOp : {LHS, RHS}) {
3708 for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
3709 if (!AssumeVH)
3710 continue;
3711
3712 CallInst *Assume = cast<CallInst>(AssumeVH);
3713 if (std::optional<bool> Imp = isImpliedCondition(
3714 Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL))
3715 if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT))
3716 return ConstantInt::get(getCompareTy(LHS), *Imp);
3717 }
3718 }
3719
3720 return nullptr;
3721}
3722
3724 Value *RHS) {
3726 if (!II)
3727 return nullptr;
3728
3729 switch (II->getIntrinsicID()) {
3730 case Intrinsic::uadd_sat:
3731 // uadd.sat(X, Y) uge X + Y
3732 if (match(RHS, m_c_Add(m_Specific(II->getArgOperand(0)),
3733 m_Specific(II->getArgOperand(1))))) {
3734 if (Pred == ICmpInst::ICMP_UGE)
3736 if (Pred == ICmpInst::ICMP_ULT)
3738 }
3739 return nullptr;
3740 case Intrinsic::usub_sat:
3741 // usub.sat(X, Y) ule X - Y
3742 if (match(RHS, m_Sub(m_Specific(II->getArgOperand(0)),
3743 m_Specific(II->getArgOperand(1))))) {
3744 if (Pred == ICmpInst::ICMP_ULE)
3746 if (Pred == ICmpInst::ICMP_UGT)
3748 }
3749 return nullptr;
3750 default:
3751 return nullptr;
3752 }
3753}
3754
3755/// Helper method to get range from metadata or attribute.
3756static std::optional<ConstantRange> getRange(Value *V,
3757 const InstrInfoQuery &IIQ) {
3759 if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
3760 return getConstantRangeFromMetadata(*MD);
3761
3762 if (const Argument *A = dyn_cast<Argument>(V))
3763 return A->getRange();
3764 else if (const CallBase *CB = dyn_cast<CallBase>(V))
3765 return CB->getRange();
3766
3767 return std::nullopt;
3768}
3769
3770/// Given operands for an ICmpInst, see if we can fold the result.
3771/// If not, this returns null.
3773 const SimplifyQuery &Q, unsigned MaxRecurse) {
3774 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3775
3776 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3777 if (Constant *CRHS = dyn_cast<Constant>(RHS))
3778 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3779
3780 // If we have a constant, make sure it is on the RHS.
3781 std::swap(LHS, RHS);
3782 Pred = CmpInst::getSwappedPredicate(Pred);
3783 }
3784 assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
3785
3786 Type *ITy = getCompareTy(LHS); // The return type.
3787
3788 // icmp poison, X -> poison
3789 if (isa<PoisonValue>(RHS))
3790 return PoisonValue::get(ITy);
3791
3792 // For EQ and NE, we can always pick a value for the undef to make the
3793 // predicate pass or fail, so we can return undef.
3794 // Matches behavior in llvm::ConstantFoldCompareInstruction.
3795 if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred))
3796 return UndefValue::get(ITy);
3797
3798 // icmp X, X -> true/false
3799 // icmp X, undef -> true/false because undef could be X.
3800 if (LHS == RHS || Q.isUndefValue(RHS))
3801 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3802
3803 if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3804 return V;
3805
3806 // TODO: Sink/common this with other potentially expensive calls that use
3807 // ValueTracking? See comment below for isKnownNonEqual().
3808 if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3809 return V;
3810
3811 if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q))
3812 return V;
3813
3814 // If both operands have range metadata, use the metadata
3815 // to simplify the comparison.
3816 if (std::optional<ConstantRange> RhsCr = getRange(RHS, Q.IIQ))
3817 if (std::optional<ConstantRange> LhsCr = getRange(LHS, Q.IIQ)) {
3818 if (LhsCr->icmp(Pred, *RhsCr))
3819 return ConstantInt::getTrue(ITy);
3820
3821 if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr))
3822 return ConstantInt::getFalse(ITy);
3823 }
3824
3825 // Compare of cast, for example (zext X) != 0 -> X != 0
3828 Value *SrcOp = LI->getOperand(0);
3829 Type *SrcTy = SrcOp->getType();
3830 Type *DstTy = LI->getType();
3831
3832 // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3833 // if the integer type is the same size as the pointer type.
3834 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3835 Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3836 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3837 // Transfer the cast to the constant.
3838 if (Value *V = simplifyICmpInst(Pred, SrcOp,
3839 ConstantExpr::getIntToPtr(RHSC, SrcTy),
3840 Q, MaxRecurse - 1))
3841 return V;
3842 } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3843 if (RI->getOperand(0)->getType() == SrcTy)
3844 // Compare without the cast.
3845 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3846 MaxRecurse - 1))
3847 return V;
3848 }
3849 }
3850
3851 if (isa<ZExtInst>(LHS)) {
3852 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3853 // same type.
3854 if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3855 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3856 // Compare X and Y. Note that signed predicates become unsigned.
3857 if (Value *V =
3859 RI->getOperand(0), Q, MaxRecurse - 1))
3860 return V;
3861 }
3862 // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
3863 else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3864 if (SrcOp == RI->getOperand(0)) {
3865 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3866 return ConstantInt::getTrue(ITy);
3867 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3868 return ConstantInt::getFalse(ITy);
3869 }
3870 }
3871 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3872 // too. If not, then try to deduce the result of the comparison.
3873 else if (match(RHS, m_ImmConstant())) {
3875 assert(C != nullptr);
3876
3877 // Compute the constant that would happen if we truncated to SrcTy then
3878 // reextended to DstTy.
3879 Constant *Trunc =
3880 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3881 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3882 Constant *RExt =
3883 ConstantFoldCastOperand(CastInst::ZExt, Trunc, DstTy, Q.DL);
3884 assert(RExt && "Constant-fold of ImmConstant should not fail");
3885 Constant *AnyEq =
3887 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3888
3889 // If the re-extended constant didn't change any of the elements then
3890 // this is effectively also a case of comparing two zero-extended
3891 // values.
3892 if (AnyEq->isAllOnesValue() && MaxRecurse)
3894 SrcOp, Trunc, Q, MaxRecurse - 1))
3895 return V;
3896
3897 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3898 // there. Use this to work out the result of the comparison.
3899 if (AnyEq->isNullValue()) {
3900 switch (Pred) {
3901 default:
3902 llvm_unreachable("Unknown ICmp predicate!");
3903 // LHS <u RHS.
3904 case ICmpInst::ICMP_EQ:
3905 case ICmpInst::ICMP_UGT:
3906 case ICmpInst::ICMP_UGE:
3907 return Constant::getNullValue(ITy);
3908
3909 case ICmpInst::ICMP_NE:
3910 case ICmpInst::ICMP_ULT:
3911 case ICmpInst::ICMP_ULE:
3912 return Constant::getAllOnesValue(ITy);
3913
3914 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
3915 // is non-negative then LHS <s RHS.
3916 case ICmpInst::ICMP_SGT:
3917 case ICmpInst::ICMP_SGE:
3920 Q.DL);
3921 case ICmpInst::ICMP_SLT:
3922 case ICmpInst::ICMP_SLE:
3925 Q.DL);
3926 }
3927 }
3928 }
3929 }
3930
3931 if (isa<SExtInst>(LHS)) {
3932 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3933 // same type.
3934 if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3935 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3936 // Compare X and Y. Note that the predicate does not change.
3937 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3938 MaxRecurse - 1))
3939 return V;
3940 }
3941 // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
3942 else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3943 if (SrcOp == RI->getOperand(0)) {
3944 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3945 return ConstantInt::getTrue(ITy);
3946 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3947 return ConstantInt::getFalse(ITy);
3948 }
3949 }
3950 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3951 // too. If not, then try to deduce the result of the comparison.
3952 else if (match(RHS, m_ImmConstant())) {
3954
3955 // Compute the constant that would happen if we truncated to SrcTy then
3956 // reextended to DstTy.
3957 Constant *Trunc =
3958 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3959 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3960 Constant *RExt =
3961 ConstantFoldCastOperand(CastInst::SExt, Trunc, DstTy, Q.DL);
3962 assert(RExt && "Constant-fold of ImmConstant should not fail");
3963 Constant *AnyEq =
3965 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3966
3967 // If the re-extended constant didn't change then this is effectively
3968 // also a case of comparing two sign-extended values.
3969 if (AnyEq->isAllOnesValue() && MaxRecurse)
3970 if (Value *V =
3971 simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1))
3972 return V;
3973
3974 // Otherwise the upper bits of LHS are all equal, while RHS has varying
3975 // bits there. Use this to work out the result of the comparison.
3976 if (AnyEq->isNullValue()) {
3977 switch (Pred) {
3978 default:
3979 llvm_unreachable("Unknown ICmp predicate!");
3980 case ICmpInst::ICMP_EQ:
3981 return Constant::getNullValue(ITy);
3982 case ICmpInst::ICMP_NE:
3983 return Constant::getAllOnesValue(ITy);
3984
3985 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
3986 // LHS >s RHS.
3987 case ICmpInst::ICMP_SGT:
3988 case ICmpInst::ICMP_SGE:
3991 Q.DL);
3992 case ICmpInst::ICMP_SLT:
3993 case ICmpInst::ICMP_SLE:
3996 Q.DL);
3997
3998 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
3999 // LHS >u RHS.
4000 case ICmpInst::ICMP_UGT:
4001 case ICmpInst::ICMP_UGE:
4002 // Comparison is true iff the LHS <s 0.
4003 if (MaxRecurse)
4005 Constant::getNullValue(SrcTy), Q,
4006 MaxRecurse - 1))
4007 return V;
4008 break;
4009 case ICmpInst::ICMP_ULT:
4010 case ICmpInst::ICMP_ULE:
4011 // Comparison is true iff the LHS >=s 0.
4012 if (MaxRecurse)
4014 Constant::getNullValue(SrcTy), Q,
4015 MaxRecurse - 1))
4016 return V;
4017 break;
4018 }
4019 }
4020 }
4021 }
4022 }
4023
4024 // icmp eq|ne X, Y -> false|true if X != Y
4025 // This is potentially expensive, and we have already computedKnownBits for
4026 // compares with 0 above here, so only try this for a non-zero compare.
4027 if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero()) &&
4028 isKnownNonEqual(LHS, RHS, Q)) {
4029 return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy);
4030 }
4031
4032 if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
4033 return V;
4034
4035 if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
4036 return V;
4037
4039 return V;
4042 return V;
4043
4044 if (Value *V = simplifyICmpUsingMonotonicValues(Pred, LHS, RHS, Q))
4045 return V;
4048 return V;
4049
4050 if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q))
4051 return V;
4052
4053 if (std::optional<bool> Res =
4054 isImpliedByDomCondition(Pred, LHS, RHS, Q.CxtI, Q.DL))
4055 return ConstantInt::getBool(ITy, *Res);
4056
4057 // Simplify comparisons of related pointers using a powerful, recursive
4058 // GEP-walk when we have target data available..
4059 if (LHS->getType()->isPointerTy())
4060 if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
4061 return C;
4062 if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
4063 if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
4064 if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4065 Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4066 Q.DL.getTypeSizeInBits(CLHS->getType()))
4067 if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
4068 CRHS->getPointerOperand(), Q))
4069 return C;
4070
4071 // If the comparison is with the result of a select instruction, check whether
4072 // comparing with either branch of the select always yields the same value.
4074 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4075 return V;
4076
4077 // If the comparison is with the result of a phi instruction, check whether
4078 // doing the compare with each incoming phi value yields a common result.
4080 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4081 return V;
4082
4083 return nullptr;
4084}
4085
4087 const SimplifyQuery &Q) {
4088 return ::simplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
4089}
4090
4091/// Given operands for an FCmpInst, see if we can fold the result.
4092/// If not, this returns null.
4094 FastMathFlags FMF, const SimplifyQuery &Q,
4095 unsigned MaxRecurse) {
4096 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
4097
4098 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
4099 if (Constant *CRHS = dyn_cast<Constant>(RHS))
4100 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI,
4101 Q.CxtI);
4102
4103 // If we have a constant, make sure it is on the RHS.
4104 std::swap(LHS, RHS);
4105 Pred = CmpInst::getSwappedPredicate(Pred);
4106 }
4107
4108 // Fold trivial predicates.
4109 Type *RetTy = getCompareTy(LHS);
4110 if (Pred == FCmpInst::FCMP_FALSE)
4111 return getFalse(RetTy);
4112 if (Pred == FCmpInst::FCMP_TRUE)
4113 return getTrue(RetTy);
4114
4115 // fcmp pred x, poison and fcmp pred poison, x
4116 // fold to poison
4118 return PoisonValue::get(RetTy);
4119
4120 // fcmp pred x, undef and fcmp pred undef, x
4121 // fold to true if unordered, false if ordered
4122 if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) {
4123 // Choosing NaN for the undef will always make unordered comparison succeed
4124 // and ordered comparison fail.
4125 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4126 }
4127
4128 // fcmp x,x -> true/false. Not all compares are foldable.
4129 if (LHS == RHS) {
4130 if (CmpInst::isTrueWhenEqual(Pred))
4131 return getTrue(RetTy);
4132 if (CmpInst::isFalseWhenEqual(Pred))
4133 return getFalse(RetTy);
4134 }
4135
4136 // Fold (un)ordered comparison if we can determine there are no NaNs.
4137 //
4138 // This catches the 2 variable input case, constants are handled below as a
4139 // class-like compare.
4140 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4143
4144 if (FMF.noNaNs() ||
4145 (RHSClass.isKnownNeverNaN() && LHSClass.isKnownNeverNaN()))
4146 return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
4147
4148 if (RHSClass.isKnownAlwaysNaN() || LHSClass.isKnownAlwaysNaN())
4149 return ConstantInt::get(RetTy, Pred == CmpInst::FCMP_UNO);
4150 }
4151
4152 const APFloat *C = nullptr;
4154 std::optional<KnownFPClass> FullKnownClassLHS;
4155
4156 // Lazily compute the possible classes for LHS. Avoid computing it twice if
4157 // RHS is a 0.
4158 auto computeLHSClass = [=, &FullKnownClassLHS](FPClassTest InterestedFlags =
4159 fcAllFlags) {
4160 if (FullKnownClassLHS)
4161 return *FullKnownClassLHS;
4162 return computeKnownFPClass(LHS, FMF, InterestedFlags, Q);
4163 };
4164
4165 if (C && Q.CxtI) {
4166 // Fold out compares that express a class test.
4167 //
4168 // FIXME: Should be able to perform folds without context
4169 // instruction. Always pass in the context function?
4170
4171 const Function *ParentF = Q.CxtI->getFunction();
4172 auto [ClassVal, ClassTest] = fcmpToClassTest(Pred, *ParentF, LHS, C);
4173 if (ClassVal) {
4174 FullKnownClassLHS = computeLHSClass();
4175 if ((FullKnownClassLHS->KnownFPClasses & ClassTest) == fcNone)
4176 return getFalse(RetTy);
4177 if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) == fcNone)
4178 return getTrue(RetTy);
4179 }
4180 }
4181
4182 // Handle fcmp with constant RHS.
4183 if (C) {
4184 // TODO: If we always required a context function, we wouldn't need to
4185 // special case nans.
4186 if (C->isNaN())
4187 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4188
4189 // TODO: Need version fcmpToClassTest which returns implied class when the
4190 // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but
4191 // isn't implementable as a class call.
4192 if (C->isNegative() && !C->isNegZero()) {
4194
4195 // TODO: We can catch more cases by using a range check rather than
4196 // relying on CannotBeOrderedLessThanZero.
4197 switch (Pred) {
4198 case FCmpInst::FCMP_UGE:
4199 case FCmpInst::FCMP_UGT:
4200 case FCmpInst::FCMP_UNE: {
4201 KnownFPClass KnownClass = computeLHSClass(Interested);
4202
4203 // (X >= 0) implies (X > C) when (C < 0)
4204 if (KnownClass.cannotBeOrderedLessThanZero())
4205 return getTrue(RetTy);
4206 break;
4207 }
4208 case FCmpInst::FCMP_OEQ:
4209 case FCmpInst::FCMP_OLE:
4210 case FCmpInst::FCMP_OLT: {
4211 KnownFPClass KnownClass = computeLHSClass(Interested);
4212
4213 // (X >= 0) implies !(X < C) when (C < 0)
4214 if (KnownClass.cannotBeOrderedLessThanZero())
4215 return getFalse(RetTy);
4216 break;
4217 }
4218 default:
4219 break;
4220 }
4221 }
4222 // Check comparison of [minnum/maxnum with constant] with other constant.
4223 const APFloat *C2;
4225 *C2 < *C) ||
4227 *C2 > *C)) {
4228 bool IsMaxNum =
4229 cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
4230 // The ordered relationship and minnum/maxnum guarantee that we do not
4231 // have NaN constants, so ordered/unordered preds are handled the same.
4232 switch (Pred) {
4233 case FCmpInst::FCMP_OEQ:
4234 case FCmpInst::FCMP_UEQ:
4235 // minnum(X, LesserC) == C --> false
4236 // maxnum(X, GreaterC) == C --> false
4237 return getFalse(RetTy);
4238 case FCmpInst::FCMP_ONE:
4239 case FCmpInst::FCMP_UNE:
4240 // minnum(X, LesserC) != C --> true
4241 // maxnum(X, GreaterC) != C --> true
4242 return getTrue(RetTy);
4243 case FCmpInst::FCMP_OGE:
4244 case FCmpInst::FCMP_UGE:
4245 case FCmpInst::FCMP_OGT:
4246 case FCmpInst::FCMP_UGT:
4247 // minnum(X, LesserC) >= C --> false
4248 // minnum(X, LesserC) > C --> false
4249 // maxnum(X, GreaterC) >= C --> true
4250 // maxnum(X, GreaterC) > C --> true
4251 return ConstantInt::get(RetTy, IsMaxNum);
4252 case FCmpInst::FCMP_OLE:
4253 case FCmpInst::FCMP_ULE:
4254 case FCmpInst::FCMP_OLT:
4255 case FCmpInst::FCMP_ULT:
4256 // minnum(X, LesserC) <= C --> true
4257 // minnum(X, LesserC) < C --> true
4258 // maxnum(X, GreaterC) <= C --> false
4259 // maxnum(X, GreaterC) < C --> false
4260 return ConstantInt::get(RetTy, !IsMaxNum);
4261 default:
4262 // TRUE/FALSE/ORD/UNO should be handled before this.
4263 llvm_unreachable("Unexpected fcmp predicate");
4264 }
4265 }
4266 }
4267
4268 // TODO: Could fold this with above if there were a matcher which returned all
4269 // classes in a non-splat vector.
4270 if (match(RHS, m_AnyZeroFP())) {
4271 switch (Pred) {
4272 case FCmpInst::FCMP_OGE:
4273 case FCmpInst::FCMP_ULT: {
4275 if (!FMF.noNaNs())
4276 Interested |= fcNan;
4277
4278 KnownFPClass Known = computeLHSClass(Interested);
4279
4280 // Positive or zero X >= 0.0 --> true
4281 // Positive or zero X < 0.0 --> false
4282 if ((FMF.noNaNs() || Known.isKnownNeverNaN()) &&
4284 return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy) : getFalse(RetTy);
4285 break;
4286 }
4287 case FCmpInst::FCMP_UGE:
4288 case FCmpInst::FCMP_OLT: {
4290 KnownFPClass Known = computeLHSClass(Interested);
4291
4292 // Positive or zero or nan X >= 0.0 --> true
4293 // Positive or zero or nan X < 0.0 --> false
4294 if (Known.cannotBeOrderedLessThanZero())
4295 return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy) : getFalse(RetTy);
4296 break;
4297 }
4298 default:
4299 break;
4300 }
4301 }
4302
4303 // If the comparison is with the result of a select instruction, check whether
4304 // comparing with either branch of the select always yields the same value.
4306 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4307 return V;
4308
4309 // If the comparison is with the result of a phi instruction, check whether
4310 // doing the compare with each incoming phi value yields a common result.
4312 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4313 return V;
4314
4315 return nullptr;
4316}
4317
4319 FastMathFlags FMF, const SimplifyQuery &Q) {
4320 return ::simplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);
4321}
4322
4324 ArrayRef<std::pair<Value *, Value *>> Ops,
4325 const SimplifyQuery &Q,
4326 bool AllowRefinement,
4328 unsigned MaxRecurse) {
4329 assert((AllowRefinement || !Q.CanUseUndef) &&
4330 "If AllowRefinement=false then CanUseUndef=false");
4331 for (const auto &OpAndRepOp : Ops) {
4332 // We cannot replace a constant, and shouldn't even try.
4333 if (isa<Constant>(OpAndRepOp.first))
4334 return nullptr;
4335
4336 // Trivial replacement.
4337 if (V == OpAndRepOp.first)
4338 return OpAndRepOp.second;
4339 }
4340
4341 if (!MaxRecurse--)
4342 return nullptr;
4343
4344 auto *I = dyn_cast<Instruction>(V);
4345 if (!I)
4346 return nullptr;
4347
4348 // The arguments of a phi node might refer to a value from a previous
4349 // cycle iteration.
4350 if (isa<PHINode>(I))
4351 return nullptr;
4352
4353 // Don't fold away llvm.is.constant checks based on assumptions.
4355 return nullptr;
4356
4357 // Don't simplify freeze.
4358 if (isa<FreezeInst>(I))
4359 return nullptr;
4360
4361 for (const auto &OpAndRepOp : Ops) {
4362 // For vector types, the simplification must hold per-lane, so forbid
4363 // potentially cross-lane operations like shufflevector.
4364 if (OpAndRepOp.first->getType()->isVectorTy() &&
4366 return nullptr;
4367 }
4368
4369 // Replace Op with RepOp in instruction operands.
4371 bool AnyReplaced = false;
4372 for (Value *InstOp : I->operands()) {
4373 if (Value *NewInstOp = simplifyWithOpsReplaced(
4374 InstOp, Ops, Q, AllowRefinement, DropFlags, MaxRecurse)) {
4375 NewOps.push_back(NewInstOp);
4376 AnyReplaced = InstOp != NewInstOp;
4377 } else {
4378 NewOps.push_back(InstOp);
4379 }
4380
4381 // Bail out if any operand is undef and SimplifyQuery disables undef
4382 // simplification. Constant folding currently doesn't respect this option.
4383 if (isa<UndefValue>(NewOps.back()) && !Q.CanUseUndef)
4384 return nullptr;
4385 }
4386
4387 if (!AnyReplaced)
4388 return nullptr;
4389
4390 if (!AllowRefinement) {
4391 // General InstSimplify functions may refine the result, e.g. by returning
4392 // a constant for a potentially poison value. To avoid this, implement only
4393 // a few non-refining but profitable transforms here.
4394
4395 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
4396 unsigned Opcode = BO->getOpcode();
4397 // id op x -> x, x op id -> x
4398 // Exclude floats, because x op id may produce a different NaN value.
4399 if (!BO->getType()->isFPOrFPVectorTy()) {
4400 if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType()))
4401 return NewOps[1];
4402 if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(),
4403 /* RHS */ true))
4404 return NewOps[0];
4405 }
4406
4407 // x & x -> x, x | x -> x
4408 if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
4409 NewOps[0] == NewOps[1]) {
4410 // or disjoint x, x results in poison.
4411 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(BO)) {
4412 if (PDI->isDisjoint()) {
4413 if (!DropFlags)
4414 return nullptr;
4415 DropFlags->push_back(BO);
4416 }
4417 }
4418 return NewOps[0];
4419 }
4420
4421 // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison
4422 // by assumption and this case never wraps, so nowrap flags can be
4423 // ignored.
4424 if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor) &&
4425 NewOps[0] == NewOps[1] &&
4426 any_of(Ops, [=](const auto &Rep) { return NewOps[0] == Rep.second; }))
4427 return Constant::getNullValue(I->getType());
4428
4429 // If we are substituting an absorber constant into a binop and extra
4430 // poison can't leak if we remove the select -- because both operands of
4431 // the binop are based on the same value -- then it may be safe to replace
4432 // the value with the absorber constant. Examples:
4433 // (Op == 0) ? 0 : (Op & -Op) --> Op & -Op
4434 // (Op == 0) ? 0 : (Op * (binop Op, C)) --> Op * (binop Op, C)
4435 // (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op)
4436 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, I->getType());
4437 if ((NewOps[0] == Absorber || NewOps[1] == Absorber) &&
4438 any_of(Ops,
4439 [=](const auto &Rep) { return impliesPoison(BO, Rep.first); }))
4440 return Absorber;
4441 }
4442
4444 // getelementptr x, 0 -> x.
4445 // This never returns poison, even if inbounds is set.
4446 if (NewOps.size() == 2 && match(NewOps[1], m_Zero()))
4447 return NewOps[0];
4448 }
4449 } else {
4450 // The simplification queries below may return the original value. Consider:
4451 // %div = udiv i32 %arg, %arg2
4452 // %mul = mul nsw i32 %div, %arg2
4453 // %cmp = icmp eq i32 %mul, %arg
4454 // %sel = select i1 %cmp, i32 %div, i32 undef
4455 // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which
4456 // simplifies back to %arg. This can only happen because %mul does not
4457 // dominate %div. To ensure a consistent return value contract, we make sure
4458 // that this case returns nullptr as well.
4459 auto PreventSelfSimplify = [V](Value *Simplified) {
4460 return Simplified != V ? Simplified : nullptr;
4461 };
4462
4463 return PreventSelfSimplify(
4464 ::simplifyInstructionWithOperands(I, NewOps, Q, MaxRecurse));
4465 }
4466
4467 // If all operands are constant after substituting Op for RepOp then we can
4468 // constant fold the instruction.
4470 for (Value *NewOp : NewOps) {
4471 if (Constant *ConstOp = dyn_cast<Constant>(NewOp))
4472 ConstOps.push_back(ConstOp);
4473 else
4474 return nullptr;
4475 }
4476
4477 // Consider:
4478 // %cmp = icmp eq i32 %x, 2147483647
4479 // %add = add nsw i32 %x, 1
4480 // %sel = select i1 %cmp, i32 -2147483648, i32 %add
4481 //
4482 // We can't replace %sel with %add unless we strip away the flags (which
4483 // will be done in InstCombine).
4484 // TODO: This may be unsound, because it only catches some forms of
4485 // refinement.
4486 if (!AllowRefinement) {
4487 if (canCreatePoison(cast<Operator>(I), !DropFlags)) {
4488 // abs cannot create poison if the value is known to never be int_min.
4489 if (auto *II = dyn_cast<IntrinsicInst>(I);
4490 II && II->getIntrinsicID() == Intrinsic::abs) {
4491 if (!ConstOps[0]->isNotMinSignedValue())
4492 return nullptr;
4493 } else
4494 return nullptr;
4495 }
4496 Constant *Res = ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4497 /*AllowNonDeterministic=*/false);
4498 if (DropFlags && Res && I->hasPoisonGeneratingAnnotations())
4499 DropFlags->push_back(I);
4500 return Res;
4501 }
4502
4503 return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4504 /*AllowNonDeterministic=*/false);
4505}
4506
4508 const SimplifyQuery &Q,
4509 bool AllowRefinement,
4511 unsigned MaxRecurse) {
4512 return simplifyWithOpsReplaced(V, {{Op, RepOp}}, Q, AllowRefinement,
4513 DropFlags, MaxRecurse);
4514}
4515
4517 const SimplifyQuery &Q,
4518 bool AllowRefinement,
4519 SmallVectorImpl<Instruction *> *DropFlags) {
4520 // If refinement is disabled, also disable undef simplifications (which are
4521 // always refinements) in SimplifyQuery.
4522 if (!AllowRefinement)
4523 return ::simplifyWithOpReplaced(V, Op, RepOp, Q.getWithoutUndef(),
4524 AllowRefinement, DropFlags, RecursionLimit);
4525 return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags,
4527}
4528
4529/// Try to simplify a select instruction when its condition operand is an
4530/// integer comparison where one operand of the compare is a constant.
4531static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
4532 const APInt *Y, bool TrueWhenUnset) {
4533 const APInt *C;
4534
4535 // (X & Y) == 0 ? X & ~Y : X --> X
4536 // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
4537 if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) &&
4538 *Y == ~*C)
4539 return TrueWhenUnset ? FalseVal : TrueVal;
4540
4541 // (X & Y) == 0 ? X : X & ~Y --> X & ~Y
4542 // (X & Y) != 0 ? X : X & ~Y --> X
4543 if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) &&
4544 *Y == ~*C)
4545 return TrueWhenUnset ? FalseVal : TrueVal;
4546
4547 if (Y->isPowerOf2()) {
4548 // (X & Y) == 0 ? X | Y : X --> X | Y
4549 // (X & Y) != 0 ? X | Y : X --> X
4550 if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) &&
4551 *Y == *C) {
4552 // We can't return the or if it has the disjoint flag.
4553 if (TrueWhenUnset && cast<PossiblyDisjointInst>(TrueVal)->isDisjoint())
4554 return nullptr;
4555 return TrueWhenUnset ? TrueVal : FalseVal;
4556 }
4557
4558 // (X & Y) == 0 ? X : X | Y --> X
4559 // (X & Y) != 0 ? X : X | Y --> X | Y
4560 if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) &&
4561 *Y == *C) {
4562 // We can't return the or if it has the disjoint flag.
4563 if (!TrueWhenUnset && cast<PossiblyDisjointInst>(FalseVal)->isDisjoint())
4564 return nullptr;
4565 return TrueWhenUnset ? TrueVal : FalseVal;
4566 }
4567 }
4568
4569 return nullptr;
4570}
4571
4572static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS,
4573 CmpPredicate Pred, Value *TVal,
4574 Value *FVal) {
4575 // Canonicalize common cmp+sel operand as CmpLHS.
4576 if (CmpRHS == TVal || CmpRHS == FVal) {
4577 std::swap(CmpLHS, CmpRHS);
4578 Pred = ICmpInst::getSwappedPredicate(Pred);
4579 }
4580
4581 // Canonicalize common cmp+sel operand as TVal.
4582 if (CmpLHS == FVal) {
4583 std::swap(TVal, FVal);
4584 Pred = ICmpInst::getInversePredicate(Pred);
4585 }
4586
4587 // A vector select may be shuffling together elements that are equivalent
4588 // based on the max/min/select relationship.
4589 Value *X = CmpLHS, *Y = CmpRHS;
4590 bool PeekedThroughSelectShuffle = false;
4591 auto *Shuf = dyn_cast<ShuffleVectorInst>(FVal);
4592 if (Shuf && Shuf->isSelect()) {
4593 if (Shuf->getOperand(0) == Y)
4594 FVal = Shuf->getOperand(1);
4595 else if (Shuf->getOperand(1) == Y)
4596 FVal = Shuf->getOperand(0);
4597 else
4598 return nullptr;
4599 PeekedThroughSelectShuffle = true;
4600 }
4601
4602 // (X pred Y) ? X : max/min(X, Y)
4603 auto *MMI = dyn_cast<MinMaxIntrinsic>(FVal);
4604 if (!MMI || TVal != X ||
4606 return nullptr;
4607
4608 // (X > Y) ? X : max(X, Y) --> max(X, Y)
4609 // (X >= Y) ? X : max(X, Y) --> max(X, Y)
4610 // (X < Y) ? X : min(X, Y) --> min(X, Y)
4611 // (X <= Y) ? X : min(X, Y) --> min(X, Y)
4612 //
4613 // The equivalence allows a vector select (shuffle) of max/min and Y. Ex:
4614 // (X > Y) ? X : (Z ? max(X, Y) : Y)
4615 // If Z is true, this reduces as above, and if Z is false:
4616 // (X > Y) ? X : Y --> max(X, Y)
4617 ICmpInst::Predicate MMPred = MMI->getPredicate();
4618 if (MMPred == CmpInst::getStrictPredicate(Pred))
4619 return MMI;
4620
4621 // Other transforms are not valid with a shuffle.
4622 if (PeekedThroughSelectShuffle)
4623 return nullptr;
4624
4625 // (X == Y) ? X : max/min(X, Y) --> max/min(X, Y)
4626 if (Pred == CmpInst::ICMP_EQ)
4627 return MMI;
4628
4629 // (X != Y) ? X : max/min(X, Y) --> X
4630 if (Pred == CmpInst::ICMP_NE)
4631 return X;
4632
4633 // (X < Y) ? X : max(X, Y) --> X
4634 // (X <= Y) ? X : max(X, Y) --> X
4635 // (X > Y) ? X : min(X, Y) --> X
4636 // (X >= Y) ? X : min(X, Y) --> X
4638 if (MMPred == CmpInst::getStrictPredicate(InvPred))
4639 return X;
4640
4641 return nullptr;
4642}
4643
4644/// An alternative way to test if a bit is set or not.
4645/// uses e.g. sgt/slt or trunc instead of eq/ne.
4646static Value *simplifySelectWithBitTest(Value *CondVal, Value *TrueVal,
4647 Value *FalseVal) {
4648 if (auto Res = decomposeBitTest(CondVal))
4649 return simplifySelectBitTest(TrueVal, FalseVal, Res->X, &Res->Mask,
4650 Res->Pred == ICmpInst::ICMP_EQ);
4651
4652 return nullptr;
4653}
4654
4655/// Try to simplify a select instruction when its condition operand is an
4656/// integer equality or floating-point equivalence comparison.
4658 ArrayRef<std::pair<Value *, Value *>> Replacements, Value *TrueVal,
4659 Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse) {
4660 Value *SimplifiedFalseVal =
4661 simplifyWithOpsReplaced(FalseVal, Replacements, Q.getWithoutUndef(),
4662 /* AllowRefinement */ false,
4663 /* DropFlags */ nullptr, MaxRecurse);
4664 if (!SimplifiedFalseVal)
4665 SimplifiedFalseVal = FalseVal;
4666
4667 Value *SimplifiedTrueVal =
4668 simplifyWithOpsReplaced(TrueVal, Replacements, Q,
4669 /* AllowRefinement */ true,
4670 /* DropFlags */ nullptr, MaxRecurse);
4671 if (!SimplifiedTrueVal)
4672 SimplifiedTrueVal = TrueVal;
4673
4674 if (SimplifiedFalseVal == SimplifiedTrueVal)
4675 return FalseVal;
4676
4677 return nullptr;
4678}
4679
4680/// Try to simplify a select instruction when its condition operand is an
4681/// integer comparison.
4682static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4683 Value *FalseVal,
4684 const SimplifyQuery &Q,
4685 unsigned MaxRecurse) {
4686 CmpPredicate Pred;
4687 Value *CmpLHS, *CmpRHS;
4688 if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4689 return nullptr;
4690
4691 if (Value *V = simplifyCmpSelOfMaxMin(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
4692 return V;
4693
4694 // Canonicalize ne to eq predicate.
4695 if (Pred == ICmpInst::ICMP_NE) {
4696 Pred = ICmpInst::ICMP_EQ;
4697 std::swap(TrueVal, FalseVal);
4698 }
4699
4700 // Check for integer min/max with a limit constant:
4701 // X > MIN_INT ? X : MIN_INT --> X
4702 // X < MAX_INT ? X : MAX_INT --> X
4703 if (TrueVal->getType()->isIntOrIntVectorTy()) {
4704 Value *X, *Y;
4706 matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal,
4707 X, Y)
4708 .Flavor;
4709 if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
4711 X->getType()->getScalarSizeInBits());
4712 if (match(Y, m_SpecificInt(LimitC)))
4713 return X;
4714 }
4715 }
4716
4717 if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())) {
4718 Value *X;
4719 const APInt *Y;
4720 if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
4721 if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
4722 /*TrueWhenUnset=*/true))
4723 return V;
4724
4725 // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
4726 Value *ShAmt;
4727 auto isFsh = m_CombineOr(m_FShl(m_Value(X), m_Value(), m_Value(ShAmt)),
4728 m_FShr(m_Value(), m_Value(X), m_Value(ShAmt)));
4729 // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
4730 // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
4731 if (match(TrueVal, isFsh) && FalseVal == X && CmpLHS == ShAmt)
4732 return X;
4733
4734 // Test for a zero-shift-guard-op around rotates. These are used to
4735 // avoid UB from oversized shifts in raw IR rotate patterns, but the
4736 // intrinsics do not have that problem.
4737 // We do not allow this transform for the general funnel shift case because
4738 // that would not preserve the poison safety of the original code.
4739 auto isRotate =
4741 m_FShr(m_Value(X), m_Deferred(X), m_Value(ShAmt)));
4742 // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4743 // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4744 if (match(FalseVal, isRotate) && TrueVal == X && CmpLHS == ShAmt &&
4745 Pred == ICmpInst::ICMP_EQ)
4746 return FalseVal;
4747
4748 // X == 0 ? abs(X) : -abs(X) --> -abs(X)
4749 // X == 0 ? -abs(X) : abs(X) --> abs(X)
4750 if (match(TrueVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))) &&
4752 return FalseVal;
4753 if (match(TrueVal,
4755 match(FalseVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))))
4756 return FalseVal;
4757 }
4758
4759 // If we have a scalar equality comparison, then we know the value in one of
4760 // the arms of the select. See if substituting this value into the arm and
4761 // simplifying the result yields the same value as the other arm.
4762 if (Pred == ICmpInst::ICMP_EQ) {
4763 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4764 canReplacePointersIfEqual(CmpLHS, CmpRHS, Q.DL))
4765 if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4766 FalseVal, Q, MaxRecurse))
4767 return V;
4768 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4769 canReplacePointersIfEqual(CmpRHS, CmpLHS, Q.DL))
4770 if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4771 FalseVal, Q, MaxRecurse))
4772 return V;
4773
4774 Value *X;
4775 Value *Y;
4776 // select((X | Y) == 0 ? X : 0) --> 0 (commuted 2 ways)
4777 if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) &&
4778 match(CmpRHS, m_Zero())) {
4779 // (X | Y) == 0 implies X == 0 and Y == 0.
4781 {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4782 return V;
4783 }
4784
4785 // select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
4786 if (match(CmpLHS, m_And(m_Value(X), m_Value(Y))) &&
4787 match(CmpRHS, m_AllOnes())) {
4788 // (X & Y) == -1 implies X == -1 and Y == -1.
4790 {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4791 return V;
4792 }
4793 }
4794
4795 return nullptr;
4796}
4797
4798/// Try to simplify a select instruction when its condition operand is a
4799/// floating-point comparison.
4801 const SimplifyQuery &Q,
4802 unsigned MaxRecurse) {
4803 CmpPredicate Pred;
4804 Value *CmpLHS, *CmpRHS;
4805 if (!match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4806 return nullptr;
4808
4809 bool IsEquiv = I->isEquivalence();
4810 if (I->isEquivalence(/*Invert=*/true)) {
4811 std::swap(T, F);
4812 Pred = FCmpInst::getInversePredicate(Pred);
4813 IsEquiv = true;
4814 }
4815
4816 // This transforms is safe if at least one operand is known to not be zero.
4817 // Otherwise, the select can change the sign of a zero operand.
4818 if (IsEquiv) {
4819 if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, T, F, Q,
4820 MaxRecurse))
4821 return V;
4822 if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, T, F, Q,
4823 MaxRecurse))
4824 return V;
4825 }
4826
4827 // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
4828 if (CmpLHS == F && CmpRHS == T)
4829 std::swap(CmpLHS, CmpRHS);
4830
4831 if (CmpLHS != T || CmpRHS != F)
4832 return nullptr;
4833
4834 // This transform is also safe if we do not have (do not care about) -0.0.
4835 if (Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros()) {
4836 // (T == F) ? T : F --> F
4837 if (Pred == FCmpInst::FCMP_OEQ)
4838 return F;
4839
4840 // (T != F) ? T : F --> T
4841 if (Pred == FCmpInst::FCMP_UNE)
4842 return T;
4843 }
4844
4845 return nullptr;
4846}
4847
4848/// Given operands for a SelectInst, see if we can fold the result.
4849/// If not, this returns null.
4850static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4851 const SimplifyQuery &Q, unsigned MaxRecurse) {
4852 if (auto *CondC = dyn_cast<Constant>(Cond)) {
4853 if (auto *TrueC = dyn_cast<Constant>(TrueVal))
4854 if (auto *FalseC = dyn_cast<Constant>(FalseVal))
4855 if (Constant *C = ConstantFoldSelectInstruction(CondC, TrueC, FalseC))
4856 return C;
4857
4858 // select poison, X, Y -> poison
4859 if (isa<PoisonValue>(CondC))
4860 return PoisonValue::get(TrueVal->getType());
4861
4862 // select undef, X, Y -> X or Y
4863 if (Q.isUndefValue(CondC))
4864 return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
4865
4866 // select true, X, Y --> X
4867 // select false, X, Y --> Y
4868 // For vectors, allow undef/poison elements in the condition to match the
4869 // defined elements, so we can eliminate the select.
4870 if (match(CondC, m_One()))
4871 return TrueVal;
4872 if (match(CondC, m_Zero()))
4873 return FalseVal;
4874 }
4875
4876 assert(Cond->getType()->isIntOrIntVectorTy(1) &&
4877 "Select must have bool or bool vector condition");
4878 assert(TrueVal->getType() == FalseVal->getType() &&
4879 "Select must have same types for true/false ops");
4880
4881 if (Cond->getType() == TrueVal->getType()) {
4882 // select i1 Cond, i1 true, i1 false --> i1 Cond
4883 if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
4884 return Cond;
4885
4886 // (X && Y) ? X : Y --> Y (commuted 2 ways)
4887 if (match(Cond, m_c_LogicalAnd(m_Specific(TrueVal), m_Specific(FalseVal))))
4888 return FalseVal;
4889
4890 // (X || Y) ? X : Y --> X (commuted 2 ways)
4891 if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
4892 return TrueVal;
4893
4894 // (X || Y) ? false : X --> false (commuted 2 ways)
4895 if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
4896 match(TrueVal, m_ZeroInt()))
4897 return ConstantInt::getFalse(Cond->getType());
4898
4899 // Match patterns that end in logical-and.
4900 if (match(FalseVal, m_ZeroInt())) {
4901 // !(X || Y) && X --> false (commuted 2 ways)
4902 if (match(Cond, m_Not(m_c_LogicalOr(m_Specific(TrueVal), m_Value()))))
4903 return ConstantInt::getFalse(Cond->getType());
4904 // X && !(X || Y) --> false (commuted 2 ways)
4905 if (match(TrueVal, m_Not(m_c_LogicalOr(m_Specific(Cond), m_Value()))))
4906 return ConstantInt::getFalse(Cond->getType());
4907
4908 // (X || Y) && Y --> Y (commuted 2 ways)
4909 if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Value())))
4910 return TrueVal;
4911 // Y && (X || Y) --> Y (commuted 2 ways)
4912 if (match(TrueVal, m_c_LogicalOr(m_Specific(Cond), m_Value())))
4913 return Cond;
4914
4915 // (X || Y) && (X || !Y) --> X (commuted 8 ways)
4916 Value *X, *Y;
4919 return X;
4920 if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4922 return X;
4923 }
4924
4925 // Match patterns that end in logical-or.
4926 if (match(TrueVal, m_One())) {
4927 // !(X && Y) || X --> true (commuted 2 ways)
4928 if (match(Cond, m_Not(m_c_LogicalAnd(m_Specific(FalseVal), m_Value()))))
4929 return ConstantInt::getTrue(Cond->getType());
4930 // X || !(X && Y) --> true (commuted 2 ways)
4931 if (match(FalseVal, m_Not(m_c_LogicalAnd(m_Specific(Cond), m_Value()))))
4932 return ConstantInt::getTrue(Cond->getType());
4933
4934 // (X && Y) || Y --> Y (commuted 2 ways)
4935 if (match(Cond, m_c_LogicalAnd(m_Specific(FalseVal), m_Value())))
4936 return FalseVal;
4937 // Y || (X && Y) --> Y (commuted 2 ways)
4938 if (match(FalseVal, m_c_LogicalAnd(m_Specific(Cond), m_Value())))
4939 return Cond;
4940 }
4941 }
4942
4943 // select ?, X, X -> X
4944 if (TrueVal == FalseVal)
4945 return TrueVal;
4946
4947 if (Cond == TrueVal) {
4948 // select i1 X, i1 X, i1 false --> X (logical-and)
4949 if (match(FalseVal, m_ZeroInt()))
4950 return Cond;
4951 // select i1 X, i1 X, i1 true --> true
4952 if (match(FalseVal, m_One()))
4953 return ConstantInt::getTrue(Cond->getType());
4954 }
4955 if (Cond == FalseVal) {
4956 // select i1 X, i1 true, i1 X --> X (logical-or)
4957 if (match(TrueVal, m_One()))
4958 return Cond;
4959 // select i1 X, i1 false, i1 X --> false
4960 if (match(TrueVal, m_ZeroInt()))
4961 return ConstantInt::getFalse(Cond->getType());
4962 }
4963
4964 // If the true or false value is poison, we can fold to the other value.
4965 // If the true or false value is undef, we can fold to the other value as
4966 // long as the other value isn't poison.
4967 // select ?, poison, X -> X
4968 // select ?, undef, X -> X
4969 if (isa<PoisonValue>(TrueVal) ||
4970 (Q.isUndefValue(TrueVal) && impliesPoison(FalseVal, Cond)))
4971 return FalseVal;
4972 // select ?, X, poison -> X
4973 // select ?, X, undef -> X
4974 if (isa<PoisonValue>(FalseVal) ||
4975 (Q.isUndefValue(FalseVal) && impliesPoison(TrueVal, Cond)))
4976 return TrueVal;
4977
4978 // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
4979 Constant *TrueC, *FalseC;
4980 if (isa<FixedVectorType>(TrueVal->getType()) &&
4981 match(TrueVal, m_Constant(TrueC)) &&
4982 match(FalseVal, m_Constant(FalseC))) {
4983 unsigned NumElts =
4984 cast<FixedVectorType>(TrueC->getType())->getNumElements();
4986 for (unsigned i = 0; i != NumElts; ++i) {
4987 // Bail out on incomplete vector constants.
4988 Constant *TEltC = TrueC->getAggregateElement(i);
4989 Constant *FEltC = FalseC->getAggregateElement(i);
4990 if (!TEltC || !FEltC)
4991 break;
4992
4993 // If the elements match (undef or not), that value is the result. If only
4994 // one element is undef, choose the defined element as the safe result.
4995 if (TEltC == FEltC)
4996 NewC.push_back(TEltC);
4997 else if (isa<PoisonValue>(TEltC) ||
4998 (Q.isUndefValue(TEltC) && isGuaranteedNotToBePoison(FEltC)))
4999 NewC.push_back(FEltC);
5000 else if (isa<PoisonValue>(FEltC) ||
5001 (Q.isUndefValue(FEltC) && isGuaranteedNotToBePoison(TEltC)))
5002 NewC.push_back(TEltC);
5003 else
5004 break;
5005 }
5006 if (NewC.size() == NumElts)
5007 return ConstantVector::get(NewC);
5008 }
5009
5010 if (Value *V =
5011 simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
5012 return V;
5013
5014 if (Value *V = simplifySelectWithBitTest(Cond, TrueVal, FalseVal))
5015 return V;
5016
5017 if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q, MaxRecurse))
5018 return V;
5019
5020 std::optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
5021 if (Imp)
5022 return *Imp ? TrueVal : FalseVal;
5023
5024 return nullptr;
5025}
5026
5028 const SimplifyQuery &Q) {
5029 return ::simplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit);
5030}
5031
5032/// Given operands for an GetElementPtrInst, see if we can fold the result.
5033/// If not, this returns null.
5036 const SimplifyQuery &Q, unsigned) {
5037 // The type of the GEP pointer operand.
5038 unsigned AS =
5039 cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace();
5040
5041 // getelementptr P -> P.
5042 if (Indices.empty())
5043 return Ptr;
5044
5045 // Compute the (pointer) type returned by the GEP instruction.
5046 Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Indices);
5047 Type *GEPTy = Ptr->getType();
5048 if (!GEPTy->isVectorTy()) {
5049 for (Value *Op : Indices) {
5050 // If one of the operands is a vector, the result type is a vector of
5051 // pointers. All vector operands must have the same number of elements.
5052 if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) {
5053 GEPTy = VectorType::get(GEPTy, VT->getElementCount());
5054 break;
5055 }
5056 }
5057 }
5058
5059 // All-zero GEP is a no-op, unless it performs a vector splat.
5060 if (Ptr->getType() == GEPTy && all_of(Indices, match_fn(m_Zero())))
5061 return Ptr;
5062
5063 // getelementptr poison, idx -> poison
5064 // getelementptr baseptr, poison -> poison
5066 return PoisonValue::get(GEPTy);
5067
5068 // getelementptr undef, idx -> undef
5069 if (Q.isUndefValue(Ptr))
5070 return UndefValue::get(GEPTy);
5071
5072 bool IsScalableVec =
5073 SrcTy->isScalableTy() || any_of(Indices, [](const Value *V) {
5074 return isa<ScalableVectorType>(V->getType());
5075 });
5076
5077 if (Indices.size() == 1) {
5078 Type *Ty = SrcTy;
5079 if (!IsScalableVec && Ty->isSized()) {
5080 Value *P;
5081 uint64_t C;
5082 uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
5083 // getelementptr P, N -> P if P points to a type of zero size.
5084 if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
5085 return Ptr;
5086
5087 // The following transforms are only safe if the ptrtoint cast
5088 // doesn't truncate the pointers.
5089 if (Indices[0]->getType()->getScalarSizeInBits() ==
5090 Q.DL.getPointerSizeInBits(AS)) {
5091 auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
5092 return P->getType() == GEPTy &&
5094 };
5095 // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
5096 if (TyAllocSize == 1 &&
5097 match(Indices[0],
5099 CanSimplify())
5100 return P;
5101
5102 // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of
5103 // size 1 << C.
5104 if (match(Indices[0], m_AShr(m_Sub(m_PtrToInt(m_Value(P)),
5106 m_ConstantInt(C))) &&
5107 TyAllocSize == 1ULL << C && CanSimplify())
5108 return P;
5109
5110 // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of
5111 // size C.
5112 if (match(Indices[0], m_SDiv(m_Sub(m_PtrToInt(m_Value(P)),
5114 m_SpecificInt(TyAllocSize))) &&
5115 CanSimplify())
5116 return P;
5117 }
5118 }
5119 }
5120
5121 if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
5122 all_of(Indices.drop_back(1), match_fn(m_Zero()))) {
5123 unsigned IdxWidth =
5124 Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
5125 if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
5126 APInt BasePtrOffset(IdxWidth, 0);
5127 Value *StrippedBasePtr =
5128 Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset);
5129
5130 // Avoid creating inttoptr of zero here: While LLVMs treatment of
5131 // inttoptr is generally conservative, this particular case is folded to
5132 // a null pointer, which will have incorrect provenance.
5133
5134 // gep (gep V, C), (sub 0, V) -> C
5135 if (match(Indices.back(),
5136 m_Neg(m_PtrToInt(m_Specific(StrippedBasePtr)))) &&
5137 !BasePtrOffset.isZero()) {
5138 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
5139 return ConstantExpr::getIntToPtr(CI, GEPTy);
5140 }
5141 // gep (gep V, C), (xor V, -1) -> C-1
5142 if (match(Indices.back(),
5143 m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes())) &&
5144 !BasePtrOffset.isOne()) {
5145 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
5146 return ConstantExpr::getIntToPtr(CI, GEPTy);
5147 }
5148 }
5149 }
5150
5151 // Check to see if this is constant foldable.
5152 if (!isa<Constant>(Ptr) || !all_of(Indices, IsaPred<Constant>))
5153 return nullptr;
5154
5156 return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr), std::nullopt,
5157 Indices);
5158
5159 auto *CE =
5161 return ConstantFoldConstant(CE, Q.DL);
5162}
5163
5165 GEPNoWrapFlags NW, const SimplifyQuery &Q) {
5166 return ::simplifyGEPInst(SrcTy, Ptr, Indices, NW, Q, RecursionLimit);
5167}
5168
5169/// Given operands for an InsertValueInst, see if we can fold the result.
5170/// If not, this returns null.
5172 ArrayRef<unsigned> Idxs,
5173 const SimplifyQuery &Q, unsigned) {
5174 if (Constant *CAgg = dyn_cast<Constant>(Agg))
5175 if (Constant *CVal = dyn_cast<Constant>(Val))
5176 return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
5177
5178 // insertvalue x, poison, n -> x
5179 // insertvalue x, undef, n -> x if x cannot be poison
5180 if (isa<PoisonValue>(Val) ||
5181 (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Agg)))
5182 return Agg;
5183
5184 // insertvalue x, (extractvalue y, n), n
5186 if (EV->getAggregateOperand()->getType() == Agg->getType() &&
5187 EV->getIndices() == Idxs) {
5188 // insertvalue poison, (extractvalue y, n), n -> y
5189 // insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison
5190 if (isa<PoisonValue>(Agg) ||
5191 (Q.isUndefValue(Agg) &&
5192 isGuaranteedNotToBePoison(EV->getAggregateOperand())))
5193 return EV->getAggregateOperand();
5194
5195 // insertvalue y, (extractvalue y, n), n -> y
5196 if (Agg == EV->getAggregateOperand())
5197 return Agg;
5198 }
5199
5200 return nullptr;
5201}
5202
5204 ArrayRef<unsigned> Idxs,
5205 const SimplifyQuery &Q) {
5206 return ::simplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
5207}
5208
5210 const SimplifyQuery &Q) {
5211 // Try to constant fold.
5212 auto *VecC = dyn_cast<Constant>(Vec);
5213 auto *ValC = dyn_cast<Constant>(Val);
5214 auto *IdxC = dyn_cast<Constant>(Idx);
5215 if (VecC && ValC && IdxC)
5216 return ConstantExpr::getInsertElement(VecC, ValC, IdxC);
5217
5218 // For fixed-length vector, fold into poison if index is out of bounds.
5219 if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
5220 if (isa<FixedVectorType>(Vec->getType()) &&
5221 CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements()))
5222 return PoisonValue::get(Vec->getType());
5223 }
5224
5225 // If index is undef, it might be out of bounds (see above case)
5226 if (Q.isUndefValue(Idx))
5227 return PoisonValue::get(Vec->getType());
5228
5229 // If the scalar is poison, or it is undef and there is no risk of
5230 // propagating poison from the vector value, simplify to the vector value.
5231 if (isa<PoisonValue>(Val) ||
5232 (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)))
5233 return Vec;
5234
5235 // Inserting the splatted value into a constant splat does nothing.
5236 if (VecC && ValC && VecC->getSplatValue() == ValC)
5237 return Vec;
5238
5239 // If we are extracting a value from a vector, then inserting it into the same
5240 // place, that's the input vector:
5241 // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
5242 if (match(Val, m_ExtractElt(m_Specific(Vec), m_Specific(Idx))))
5243 return Vec;
5244
5245 return nullptr;
5246}
5247
5248/// Given operands for an ExtractValueInst, see if we can fold the result.
5249/// If not, this returns null.
5251 const SimplifyQuery &, unsigned) {
5252 if (auto *CAgg = dyn_cast<Constant>(Agg))
5253 return ConstantFoldExtractValueInstruction(CAgg, Idxs);
5254
5255 // extractvalue x, (insertvalue y, elt, n), n -> elt
5256 unsigned NumIdxs = Idxs.size();
5257 for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
5258 IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
5259 ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
5260 unsigned NumInsertValueIdxs = InsertValueIdxs.size();
5261 unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
5262 if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
5263 Idxs.slice(0, NumCommonIdxs)) {
5264 if (NumIdxs == NumInsertValueIdxs)
5265 return IVI->getInsertedValueOperand();
5266 break;
5267 }
5268 }
5269
5270 // Simplify umul_with_overflow where one operand is 1.
5271 Value *V;
5272 if (Idxs.size() == 1 &&
5273 (match(Agg,
5276 m_Value(V))))) {
5277 if (Idxs[0] == 0)
5278 return V;
5279 assert(Idxs[0] == 1 && "invalid index");
5280 return getFalse(CmpInst::makeCmpResultType(V->getType()));
5281 }
5282
5283 return nullptr;
5284}
5285
5287 const SimplifyQuery &Q) {
5288 return ::simplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
5289}
5290
5291/// Given operands for an ExtractElementInst, see if we can fold the result.
5292/// If not, this returns null.
5294 const SimplifyQuery &Q, unsigned) {
5295 auto *VecVTy = cast<VectorType>(Vec->getType());
5296 if (auto *CVec = dyn_cast<Constant>(Vec)) {
5297 if (auto *CIdx = dyn_cast<Constant>(Idx))
5298 return ConstantExpr::getExtractElement(CVec, CIdx);
5299
5300 if (Q.isUndefValue(Vec))
5301 return UndefValue::get(VecVTy->getElementType());
5302 }
5303
5304 // An undef extract index can be arbitrarily chosen to be an out-of-range
5305 // index value, which would result in the instruction being poison.
5306 if (Q.isUndefValue(Idx))
5307 return PoisonValue::get(VecVTy->getElementType());
5308
5309 // If extracting a specified index from the vector, see if we can recursively
5310 // find a previously computed scalar that was inserted into the vector.
5311 if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
5312 // For fixed-length vector, fold into undef if index is out of bounds.
5313 unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
5314 if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
5315 return PoisonValue::get(VecVTy->getElementType());
5316 // Handle case where an element is extracted from a splat.
5317 if (IdxC->getValue().ult(MinNumElts))
5318 if (auto *Splat = getSplatValue(Vec))
5319 return Splat;
5320 if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
5321 return Elt;
5322 } else {
5323 // extractelt x, (insertelt y, elt, n), n -> elt
5324 // If the possibly-variable indices are trivially known to be equal
5325 // (because they are the same operand) then use the value that was
5326 // inserted directly.
5327 auto *IE = dyn_cast<InsertElementInst>(Vec);
5328 if (IE && IE->getOperand(2) == Idx)
5329 return IE->getOperand(1);
5330
5331 // The index is not relevant if our vector is a splat.
5332 if (Value *Splat = getSplatValue(Vec))
5333 return Splat;
5334 }
5335 return nullptr;
5336}
5337
5339 const SimplifyQuery &Q) {
5340 return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
5341}
5342
5343/// See if we can fold the given phi. If not, returns null.
5345 const SimplifyQuery &Q) {
5346 // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE
5347 // here, because the PHI we may succeed simplifying to was not
5348 // def-reachable from the original PHI!
5349
5350 // If all of the PHI's incoming values are the same then replace the PHI node
5351 // with the common value.
5352 Value *CommonValue = nullptr;
5353 bool HasPoisonInput = false;
5354 bool HasUndefInput = false;
5355 for (Value *Incoming : IncomingValues) {
5356 // If the incoming value is the phi node itself, it can safely be skipped.
5357 if (Incoming == PN)
5358 continue;
5360 HasPoisonInput = true;
5361 continue;
5362 }
5363 if (Q.isUndefValue(Incoming)) {
5364 // Remember that we saw an undef value, but otherwise ignore them.
5365 HasUndefInput = true;
5366 continue;
5367 }
5368 if (CommonValue && Incoming != CommonValue)
5369 return nullptr; // Not the same, bail out.
5370 CommonValue = Incoming;
5371 }
5372
5373 // If CommonValue is null then all of the incoming values were either undef,
5374 // poison or equal to the phi node itself.
5375 if (!CommonValue)
5376 return HasUndefInput ? UndefValue::get(PN->getType())
5377 : PoisonValue::get(PN->getType());
5378
5379 if (HasPoisonInput || HasUndefInput) {
5380 // If we have a PHI node like phi(X, undef, X), where X is defined by some
5381 // instruction, we cannot return X as the result of the PHI node unless it
5382 // dominates the PHI block.
5383 if (!valueDominatesPHI(CommonValue, PN, Q.DT))
5384 return nullptr;
5385
5386 // Make sure we do not replace an undef value with poison.
5387 if (HasUndefInput &&
5388 !isGuaranteedNotToBePoison(CommonValue, Q.AC, Q.CxtI, Q.DT))
5389 return nullptr;
5390 return CommonValue;
5391 }
5392
5393 return CommonValue;
5394}
5395
5396static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5397 const SimplifyQuery &Q, unsigned MaxRecurse) {
5398 if (auto *C = dyn_cast<Constant>(Op))
5399 return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
5400
5401 if (auto *CI = dyn_cast<CastInst>(Op)) {
5402 auto *Src = CI->getOperand(0);
5403 Type *SrcTy = Src->getType();
5404 Type *MidTy = CI->getType();
5405 Type *DstTy = Ty;
5406 if (Src->getType() == Ty) {
5407 auto FirstOp = CI->getOpcode();
5408 auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
5409 Type *SrcIntPtrTy =
5410 SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
5411 Type *MidIntPtrTy =
5412 MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
5413 Type *DstIntPtrTy =
5414 DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
5415 if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
5416 SrcIntPtrTy, MidIntPtrTy,
5417 DstIntPtrTy) == Instruction::BitCast)
5418 return Src;
5419 }
5420 }
5421
5422 // bitcast x -> x
5423 if (CastOpc == Instruction::BitCast)
5424 if (Op->getType() == Ty)
5425 return Op;
5426
5427 // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
5428 Value *Ptr, *X;
5429 if (CastOpc == Instruction::PtrToInt &&
5432 X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
5433 return X;
5434
5435 return nullptr;
5436}
5437
5438Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5439 const SimplifyQuery &Q) {
5440 return ::simplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
5441}
5442
5443/// For the given destination element of a shuffle, peek through shuffles to
5444/// match a root vector source operand that contains that element in the same
5445/// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
5446static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
5447 int MaskVal, Value *RootVec,
5448 unsigned MaxRecurse) {
5449 if (!MaxRecurse--)
5450 return nullptr;
5451
5452 // Bail out if any mask value is undefined. That kind of shuffle may be
5453 // simplified further based on demanded bits or other folds.
5454 if (MaskVal == -1)
5455 return nullptr;
5456
5457 // The mask value chooses which source operand we need to look at next.
5458 int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
5459 int RootElt = MaskVal;
5460 Value *SourceOp = Op0;
5461 if (MaskVal >= InVecNumElts) {
5462 RootElt = MaskVal - InVecNumElts;
5463 SourceOp = Op1;
5464 }
5465
5466 // If the source operand is a shuffle itself, look through it to find the
5467 // matching root vector.
5468 if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
5469 return foldIdentityShuffles(
5470 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
5471 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
5472 }
5473
5474 // The source operand is not a shuffle. Initialize the root vector value for
5475 // this shuffle if that has not been done yet.
5476 if (!RootVec)
5477 RootVec = SourceOp;
5478
5479 // Give up as soon as a source operand does not match the existing root value.
5480 if (RootVec != SourceOp)
5481 return nullptr;
5482
5483 // The element must be coming from the same lane in the source vector
5484 // (although it may have crossed lanes in intermediate shuffles).
5485 if (RootElt != DestElt)
5486 return nullptr;
5487
5488 return RootVec;
5489}
5490
5492 ArrayRef<int> Mask, Type *RetTy,
5493 const SimplifyQuery &Q,
5494 unsigned MaxRecurse) {
5495 if (all_of(Mask, [](int Elem) { return Elem == PoisonMaskElem; }))
5496 return PoisonValue::get(RetTy);
5497
5498 auto *InVecTy = cast<VectorType>(Op0->getType());
5499 unsigned MaskNumElts = Mask.size();
5500 ElementCount InVecEltCount = InVecTy->getElementCount();
5501
5502 bool Scalable = InVecEltCount.isScalable();
5503
5504 SmallVector<int, 32> Indices;
5505 Indices.assign(Mask.begin(), Mask.end());
5506
5507 // Canonicalization: If mask does not select elements from an input vector,
5508 // replace that input vector with poison.
5509 if (!Scalable) {
5510 bool MaskSelects0 = false, MaskSelects1 = false;
5511 unsigned InVecNumElts = InVecEltCount.getKnownMinValue();
5512 for (unsigned i = 0; i != MaskNumElts; ++i) {
5513 if (Indices[i] == -1)
5514 continue;
5515 if ((unsigned)Indices[i] < InVecNumElts)
5516 MaskSelects0 = true;
5517 else
5518 MaskSelects1 = true;
5519 }
5520 if (!MaskSelects0)
5521 Op0 = PoisonValue::get(InVecTy);
5522 if (!MaskSelects1)
5523 Op1 = PoisonValue::get(InVecTy);
5524 }
5525
5526 auto *Op0Const = dyn_cast<Constant>(Op0);
5527 auto *Op1Const = dyn_cast<Constant>(Op1);
5528
5529 // If all operands are constant, constant fold the shuffle. This
5530 // transformation depends on the value of the mask which is not known at
5531 // compile time for scalable vectors
5532 if (Op0Const && Op1Const)
5533 return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask);
5534
5535 // Canonicalization: if only one input vector is constant, it shall be the
5536 // second one. This transformation depends on the value of the mask which
5537 // is not known at compile time for scalable vectors
5538 if (!Scalable && Op0Const && !Op1Const) {
5539 std::swap(Op0, Op1);
5541 InVecEltCount.getKnownMinValue());
5542 }
5543
5544 // A splat of an inserted scalar constant becomes a vector constant:
5545 // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
5546 // NOTE: We may have commuted above, so analyze the updated Indices, not the
5547 // original mask constant.
5548 // NOTE: This transformation depends on the value of the mask which is not
5549 // known at compile time for scalable vectors
5550 Constant *C;
5551 ConstantInt *IndexC;
5552 if (!Scalable && match(Op0, m_InsertElt(m_Value(), m_Constant(C),
5553 m_ConstantInt(IndexC)))) {
5554 // Match a splat shuffle mask of the insert index allowing undef elements.
5555 int InsertIndex = IndexC->getZExtValue();
5556 if (all_of(Indices, [InsertIndex](int MaskElt) {
5557 return MaskElt == InsertIndex || MaskElt == -1;
5558 })) {
5559 assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat");
5560
5561 // Shuffle mask poisons become poison constant result elements.
5562 SmallVector<Constant *, 16> VecC(MaskNumElts, C);
5563 for (unsigned i = 0; i != MaskNumElts; ++i)
5564 if (Indices[i] == -1)
5565 VecC[i] = PoisonValue::get(C->getType());
5566 return ConstantVector::get(VecC);
5567 }
5568 }
5569
5570 // A shuffle of a splat is always the splat itself. Legal if the shuffle's
5571 // value type is same as the input vectors' type.
5572 if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
5573 if (Q.isUndefValue(Op1) && RetTy == InVecTy &&
5574 all_equal(OpShuf->getShuffleMask()))
5575 return Op0;
5576
5577 // All remaining transformation depend on the value of the mask, which is
5578 // not known at compile time for scalable vectors.
5579 if (Scalable)
5580 return nullptr;
5581
5582 // Don't fold a shuffle with undef mask elements. This may get folded in a
5583 // better way using demanded bits or other analysis.
5584 // TODO: Should we allow this?
5585 if (is_contained(Indices, -1))
5586 return nullptr;
5587
5588 // Check if every element of this shuffle can be mapped back to the
5589 // corresponding element of a single root vector. If so, we don't need this
5590 // shuffle. This handles simple identity shuffles as well as chains of
5591 // shuffles that may widen/narrow and/or move elements across lanes and back.
5592 Value *RootVec = nullptr;
5593 for (unsigned i = 0; i != MaskNumElts; ++i) {
5594 // Note that recursion is limited for each vector element, so if any element
5595 // exceeds the limit, this will fail to simplify.
5596 RootVec =
5597 foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse);
5598
5599 // We can't replace a widening/narrowing shuffle with one of its operands.
5600 if (!RootVec || RootVec->getType() != RetTy)
5601 return nullptr;
5602 }
5603 return RootVec;
5604}
5605
5606/// Given operands for a ShuffleVectorInst, fold the result or return null.
5608 ArrayRef<int> Mask, Type *RetTy,
5609 const SimplifyQuery &Q) {
5610 return ::simplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit);
5611}
5612
5614 const SimplifyQuery &Q) {
5615 if (auto *C = dyn_cast<Constant>(Op))
5616 return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL);
5617 return nullptr;
5618}
5619
5620/// Given the operand for an FNeg, see if we can fold the result. If not, this
5621/// returns null.
5623 const SimplifyQuery &Q, unsigned MaxRecurse) {
5624 if (Constant *C = foldConstant(Instruction::FNeg, Op, Q))
5625 return C;
5626
5627 Value *X;
5628 // fneg (fneg X) ==> X
5629 if (match(Op, m_FNeg(m_Value(X))))
5630 return X;
5631
5632 return nullptr;
5633}
5634
5636 const SimplifyQuery &Q) {
5637 return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit);
5638}
5639
5640/// Try to propagate existing NaN values when possible. If not, replace the
5641/// constant or elements in the constant with a canonical NaN.
5643 Type *Ty = In->getType();
5644 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
5645 unsigned NumElts = VecTy->getNumElements();
5646 SmallVector<Constant *, 32> NewC(NumElts);
5647 for (unsigned i = 0; i != NumElts; ++i) {
5648 Constant *EltC = In->getAggregateElement(i);
5649 // Poison elements propagate. NaN propagates except signaling is quieted.
5650 // Replace unknown or undef elements with canonical NaN.
5651 if (EltC && isa<PoisonValue>(EltC))
5652 NewC[i] = EltC;
5653 else if (EltC && EltC->isNaN())
5654 NewC[i] = ConstantFP::get(
5655 EltC->getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet());
5656 else
5657 NewC[i] = ConstantFP::getNaN(VecTy->getElementType());
5658 }
5659 return ConstantVector::get(NewC);
5660 }
5661
5662 // If it is not a fixed vector, but not a simple NaN either, return a
5663 // canonical NaN.
5664 if (!In->isNaN())
5665 return ConstantFP::getNaN(Ty);
5666
5667 // If we known this is a NaN, and it's scalable vector, we must have a splat
5668 // on our hands. Grab that before splatting a QNaN constant.
5669 if (isa<ScalableVectorType>(Ty)) {
5670 auto *Splat = In->getSplatValue();
5671 assert(Splat && Splat->isNaN() &&
5672 "Found a scalable-vector NaN but not a splat");
5673 In = Splat;
5674 }
5675
5676 // Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but
5677 // preserve the sign/payload.
5678 return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet());
5679}
5680
5681/// Perform folds that are common to any floating-point operation. This implies
5682/// transforms based on poison/undef/NaN because the operation itself makes no
5683/// difference to the result.
5685 const SimplifyQuery &Q,
5686 fp::ExceptionBehavior ExBehavior,
5687 RoundingMode Rounding) {
5688 // Poison is independent of anything else. It always propagates from an
5689 // operand to a math result.
5691 return PoisonValue::get(Ops[0]->getType());
5692
5693 for (Value *V : Ops) {
5694 bool IsNan = match(V, m_NaN());
5695 bool IsInf = match(V, m_Inf());
5696 bool IsUndef = Q.isUndefValue(V);
5697
5698 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
5699 // (an undef operand can be chosen to be Nan/Inf), then the result of
5700 // this operation is poison.
5701 if (FMF.noNaNs() && (IsNan || IsUndef))
5702 return PoisonValue::get(V->getType());
5703 if (FMF.noInfs() && (IsInf || IsUndef))
5704 return PoisonValue::get(V->getType());
5705
5706 if (isDefaultFPEnvironment(ExBehavior, Rounding)) {
5707 // Undef does not propagate because undef means that all bits can take on
5708 // any value. If this is undef * NaN for example, then the result values
5709 // (at least the exponent bits) are limited. Assume the undef is a
5710 // canonical NaN and propagate that.
5711 if (IsUndef)
5712 return ConstantFP::getNaN(V->getType());
5713 if (IsNan)
5714 return propagateNaN(cast<Constant>(V));
5715 } else if (ExBehavior != fp::ebStrict) {
5716 if (IsNan)
5717 return propagateNaN(cast<Constant>(V));
5718 }
5719 }
5720 return nullptr;
5721}
5722
5723/// Given operands for an FAdd, see if we can fold the result. If not, this
5724/// returns null.
5725static Value *
5727 const SimplifyQuery &Q, unsigned MaxRecurse,
5730 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5731 if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
5732 return C;
5733
5734 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5735 return C;
5736
5737 // fadd X, -0 ==> X
5738 // With strict/constrained FP, we have these possible edge cases that do
5739 // not simplify to Op0:
5740 // fadd SNaN, -0.0 --> QNaN
5741 // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
5742 if (canIgnoreSNaN(ExBehavior, FMF) &&
5744 FMF.noSignedZeros()))
5745 if (match(Op1, m_NegZeroFP()))
5746 return Op0;
5747
5748 // fadd X, 0 ==> X, when we know X is not -0
5749 if (canIgnoreSNaN(ExBehavior, FMF))
5750 if (match(Op1, m_PosZeroFP()) &&
5751 (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, Q)))
5752 return Op0;
5753
5754 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5755 return nullptr;
5756
5757 if (FMF.noNaNs()) {
5758 // With nnan: X + {+/-}Inf --> {+/-}Inf
5759 if (match(Op1, m_Inf()))
5760 return Op1;
5761
5762 // With nnan: -X + X --> 0.0 (and commuted variant)
5763 // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
5764 // Negative zeros are allowed because we always end up with positive zero:
5765 // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5766 // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5767 // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
5768 // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
5769 if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
5770 match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
5771 return ConstantFP::getZero(Op0->getType());
5772
5773 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
5774 match(Op1, m_FNeg(m_Specific(Op0))))
5775 return ConstantFP::getZero(Op0->getType());
5776 }
5777
5778 // (X - Y) + Y --> X
5779 // Y + (X - Y) --> X
5780 Value *X;
5781 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5782 (match(Op0, m_FSub(m_Value(X), m_Specific(Op1))) ||
5783 match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))))
5784 return X;
5785
5786 return nullptr;
5787}
5788
5789/// Given operands for an FSub, see if we can fold the result. If not, this
5790/// returns null.
5791static Value *
5793 const SimplifyQuery &Q, unsigned MaxRecurse,
5796 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5797 if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
5798 return C;
5799
5800 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5801 return C;
5802
5803 // fsub X, +0 ==> X
5804 if (canIgnoreSNaN(ExBehavior, FMF) &&
5806 FMF.noSignedZeros()))
5807 if (match(Op1, m_PosZeroFP()))
5808 return Op0;
5809
5810 // fsub X, -0 ==> X, when we know X is not -0
5811 if (canIgnoreSNaN(ExBehavior, FMF))
5812 if (match(Op1, m_NegZeroFP()) &&
5813 (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, Q)))
5814 return Op0;
5815
5816 // fsub -0.0, (fsub -0.0, X) ==> X
5817 // fsub -0.0, (fneg X) ==> X
5818 Value *X;
5819 if (canIgnoreSNaN(ExBehavior, FMF))
5820 if (match(Op0, m_NegZeroFP()) && match(Op1, m_FNeg(m_Value(X))))
5821 return X;
5822
5823 // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
5824 // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
5825 if (canIgnoreSNaN(ExBehavior, FMF))
5826 if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()) &&
5827 (match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X))) ||
5828 match(Op1, m_FNeg(m_Value(X)))))
5829 return X;
5830
5831 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5832 return nullptr;
5833
5834 if (FMF.noNaNs()) {
5835 // fsub nnan x, x ==> 0.0
5836 if (Op0 == Op1)
5837 return Constant::getNullValue(Op0->getType());
5838
5839 // With nnan: {+/-}Inf - X --> {+/-}Inf
5840 if (match(Op0, m_Inf()))
5841 return Op0;
5842
5843 // With nnan: X - {+/-}Inf --> {-/+}Inf
5844 if (match(Op1, m_Inf()))
5845 return foldConstant(Instruction::FNeg, Op1, Q);
5846 }
5847
5848 // Y - (Y - X) --> X
5849 // (X + Y) - Y --> X
5850 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5851 (match(Op1, m_FSub(m_Specific(Op0), m_Value(X))) ||
5852 match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))))
5853 return X;
5854
5855 return nullptr;
5856}
5857
5859 const SimplifyQuery &Q, unsigned MaxRecurse,
5860 fp::ExceptionBehavior ExBehavior,
5861 RoundingMode Rounding) {
5862 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5863 return C;
5864
5865 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5866 return nullptr;
5867
5868 // Canonicalize special constants as operand 1.
5869 if (match(Op0, m_FPOne()) || match(Op0, m_AnyZeroFP()))
5870 std::swap(Op0, Op1);
5871
5872 // X * 1.0 --> X
5873 if (match(Op1, m_FPOne()))
5874 return Op0;
5875
5876 if (match(Op1, m_AnyZeroFP())) {
5877 // X * 0.0 --> 0.0 (with nnan and nsz)
5878 if (FMF.noNaNs() && FMF.noSignedZeros())
5879 return ConstantFP::getZero(Op0->getType());
5880
5881 KnownFPClass Known = computeKnownFPClass(Op0, FMF, fcInf | fcNan, Q);
5882 if (Known.isKnownNever(fcInf | fcNan)) {
5883 // if nsz is set, return 0.0
5884 if (FMF.noSignedZeros())
5885 return ConstantFP::getZero(Op0->getType());
5886 // +normal number * (-)0.0 --> (-)0.0
5887 if (Known.SignBit == false)
5888 return Op1;
5889 // -normal number * (-)0.0 --> -(-)0.0
5890 if (Known.SignBit == true)
5891 return foldConstant(Instruction::FNeg, Op1, Q);
5892 }
5893 }
5894
5895 // sqrt(X) * sqrt(X) --> X, if we can:
5896 // 1. Remove the intermediate rounding (reassociate).
5897 // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
5898 // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
5899 Value *X;
5900 if (Op0 == Op1 && match(Op0, m_Sqrt(m_Value(X))) && FMF.allowReassoc() &&
5901 FMF.noNaNs() && FMF.noSignedZeros())
5902 return X;
5903
5904 return nullptr;
5905}
5906
5907/// Given the operands for an FMul, see if we can fold the result
5908static Value *
5910 const SimplifyQuery &Q, unsigned MaxRecurse,
5913 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5914 if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
5915 return C;
5916
5917 // Now apply simplifications that do not require rounding.
5918 return simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
5919}
5920
5922 const SimplifyQuery &Q,
5923 fp::ExceptionBehavior ExBehavior,
5924 RoundingMode Rounding) {
5925 return ::simplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5926 Rounding);
5927}
5928
5930 const SimplifyQuery &Q,
5931 fp::ExceptionBehavior ExBehavior,
5932 RoundingMode Rounding) {
5933 return ::simplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5934 Rounding);
5935}
5936
5938 const SimplifyQuery &Q,
5939 fp::ExceptionBehavior ExBehavior,
5940 RoundingMode Rounding) {
5941 return ::simplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5942 Rounding);
5943}
5944
5946 const SimplifyQuery &Q,
5947 fp::ExceptionBehavior ExBehavior,
5948 RoundingMode Rounding) {
5949 return ::simplifyFMAFMul(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5950 Rounding);
5951}
5952
5953static Value *
5955 const SimplifyQuery &Q, unsigned,
5958 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5959 if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
5960 return C;
5961
5962 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5963 return C;
5964
5965 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5966 return nullptr;
5967
5968 // X / 1.0 -> X
5969 if (match(Op1, m_FPOne()))
5970 return Op0;
5971
5972 // 0 / X -> 0
5973 // Requires that NaNs are off (X could be zero) and signed zeroes are
5974 // ignored (X could be positive or negative, so the output sign is unknown).
5975 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
5976 return ConstantFP::getZero(Op0->getType());
5977
5978 if (FMF.noNaNs()) {
5979 // X / X -> 1.0 is legal when NaNs are ignored.
5980 // We can ignore infinities because INF/INF is NaN.
5981 if (Op0 == Op1)
5982 return ConstantFP::get(Op0->getType(), 1.0);
5983
5984 // (X * Y) / Y --> X if we can reassociate to the above form.
5985 Value *X;
5986 if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1))))
5987 return X;
5988
5989 // -X / X -> -1.0 and
5990 // X / -X -> -1.0 are legal when NaNs are ignored.
5991 // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
5992 if (match(Op0, m_FNegNSZ(m_Specific(Op1))) ||
5993 match(Op1, m_FNegNSZ(m_Specific(Op0))))
5994 return ConstantFP::get(Op0->getType(), -1.0);
5995
5996 // nnan ninf X / [-]0.0 -> poison
5997 if (FMF.noInfs() && match(Op1, m_AnyZeroFP()))
5998 return PoisonValue::get(Op1->getType());
5999 }
6000
6001 return nullptr;
6002}
6003
6005 const SimplifyQuery &Q,
6006 fp::ExceptionBehavior ExBehavior,
6007 RoundingMode Rounding) {
6008 return ::simplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
6009 Rounding);
6010}
6011
6012static Value *
6014 const SimplifyQuery &Q, unsigned,
6017 if (isDefaultFPEnvironment(ExBehavior, Rounding))
6018 if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
6019 return C;
6020
6021 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
6022 return C;
6023
6024 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
6025 return nullptr;
6026
6027 // Unlike fdiv, the result of frem always matches the sign of the dividend.
6028 // The constant match may include undef elements in a vector, so return a full
6029 // zero constant as the result.
6030 if (FMF.noNaNs()) {
6031 // +0 % X -> 0
6032 if (match(Op0, m_PosZeroFP()))
6033 return ConstantFP::getZero(Op0->getType());
6034 // -0 % X -> -0
6035 if (match(Op0, m_NegZeroFP()))
6036 return ConstantFP::getNegativeZero(Op0->getType());
6037 }
6038
6039 return nullptr;
6040}
6041
6043 const SimplifyQuery &Q,
6044 fp::ExceptionBehavior ExBehavior,
6045 RoundingMode Rounding) {
6046 return ::simplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
6047 Rounding);
6048}
6049
6050//=== Helper functions for higher up the class hierarchy.
6051
6052/// Given the operand for a UnaryOperator, see if we can fold the result.
6053/// If not, this returns null.
6054static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
6055 unsigned MaxRecurse) {
6056 switch (Opcode) {
6057 case Instruction::FNeg:
6058 return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse);
6059 default:
6060 llvm_unreachable("Unexpected opcode");
6061 }
6062}
6063
6064/// Given the operand for a UnaryOperator, see if we can fold the result.
6065/// If not, this returns null.
6066/// Try to use FastMathFlags when folding the result.
6067static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
6068 const FastMathFlags &FMF, const SimplifyQuery &Q,
6069 unsigned MaxRecurse) {
6070 switch (Opcode) {
6071 case Instruction::FNeg:
6072 return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
6073 default:
6074 return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
6075 }
6076}
6077
6078Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
6079 return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit);
6080}
6081
6083 const SimplifyQuery &Q) {
6084 return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit);
6085}
6086
6087/// Given operands for a BinaryOperator, see if we can fold the result.
6088/// If not, this returns null.
6089static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6090 const SimplifyQuery &Q, unsigned MaxRecurse) {
6091 switch (Opcode) {
6092 case Instruction::Add:
6093 return simplifyAddInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6094 MaxRecurse);
6095 case Instruction::Sub:
6096 return simplifySubInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6097 MaxRecurse);
6098 case Instruction::Mul:
6099 return simplifyMulInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6100 MaxRecurse);
6101 case Instruction::SDiv:
6102 return simplifySDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6103 case Instruction::UDiv:
6104 return simplifyUDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6105 case Instruction::SRem:
6106 return simplifySRemInst(LHS, RHS, Q, MaxRecurse);
6107 case Instruction::URem:
6108 return simplifyURemInst(LHS, RHS, Q, MaxRecurse);
6109 case Instruction::Shl:
6110 return simplifyShlInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6111 MaxRecurse);
6112 case Instruction::LShr:
6113 return simplifyLShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6114 case Instruction::AShr:
6115 return simplifyAShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6116 case Instruction::And:
6117 return simplifyAndInst(LHS, RHS, Q, MaxRecurse);
6118 case Instruction::Or:
6119 return simplifyOrInst(LHS, RHS, Q, MaxRecurse);
6120 case Instruction::Xor:
6121 return simplifyXorInst(LHS, RHS, Q, MaxRecurse);
6122 case Instruction::FAdd:
6123 return simplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6124 case Instruction::FSub:
6125 return simplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6126 case Instruction::FMul:
6127 return simplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6128 case Instruction::FDiv:
6129 return simplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6130 case Instruction::FRem:
6131 return simplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6132 default:
6133 llvm_unreachable("Unexpected opcode");
6134 }
6135}
6136
6137/// Given operands for a BinaryOperator, see if we can fold the result.
6138/// If not, this returns null.
6139/// Try to use FastMathFlags when folding the result.
6140static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6141 const FastMathFlags &FMF, const SimplifyQuery &Q,
6142 unsigned MaxRecurse) {
6143 switch (Opcode) {
6144 case Instruction::FAdd:
6145 return simplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
6146 case Instruction::FSub:
6147 return simplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
6148 case Instruction::FMul:
6149 return simplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
6150 case Instruction::FDiv:
6151 return simplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
6152 default:
6153 return simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
6154 }
6155}
6156
6157Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6158 const SimplifyQuery &Q) {
6159 return ::simplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit);
6160}
6161
6162Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6163 FastMathFlags FMF, const SimplifyQuery &Q) {
6164 return ::simplifyBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit);
6165}
6166
6167/// Given operands for a CmpInst, see if we can fold the result.
6169 const SimplifyQuery &Q, unsigned MaxRecurse) {
6171 return simplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
6172 return simplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6173}
6174
6176 const SimplifyQuery &Q) {
6177 return ::simplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
6178}
6179
6181 switch (ID) {
6182 default:
6183 return false;
6184
6185 // Unary idempotent: f(f(x)) = f(x)
6186 case Intrinsic::fabs:
6187 case Intrinsic::floor:
6188 case Intrinsic::ceil:
6189 case Intrinsic::trunc:
6190 case Intrinsic::rint:
6191 case Intrinsic::nearbyint:
6192 case Intrinsic::round:
6193 case Intrinsic::roundeven:
6194 case Intrinsic::canonicalize:
6195 case Intrinsic::arithmetic_fence:
6196 return true;
6197 }
6198}
6199
6200/// Return true if the intrinsic rounds a floating-point value to an integral
6201/// floating-point value (not an integer type).
6203 switch (ID) {
6204 default:
6205 return false;
6206
6207 case Intrinsic::floor:
6208 case Intrinsic::ceil:
6209 case Intrinsic::trunc:
6210 case Intrinsic::rint:
6211 case Intrinsic::nearbyint:
6212 case Intrinsic::round:
6213 case Intrinsic::roundeven:
6214 return true;
6215 }
6216}
6217
6219 const DataLayout &DL) {
6220 GlobalValue *PtrSym;
6221 APInt PtrOffset;
6222 if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
6223 return nullptr;
6224
6225 Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
6226
6227 auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
6228 if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
6229 return nullptr;
6230
6231 APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc(
6232 DL.getIndexTypeSizeInBits(Ptr->getType()));
6233 if (OffsetInt.srem(4) != 0)
6234 return nullptr;
6235
6236 Constant *Loaded =
6237 ConstantFoldLoadFromConstPtr(Ptr, Int32Ty, std::move(OffsetInt), DL);
6238 if (!Loaded)
6239 return nullptr;
6240
6241 auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
6242 if (!LoadedCE)
6243 return nullptr;
6244
6245 if (LoadedCE->getOpcode() == Instruction::Trunc) {
6246 LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6247 if (!LoadedCE)
6248 return nullptr;
6249 }
6250
6251 if (LoadedCE->getOpcode() != Instruction::Sub)
6252 return nullptr;
6253
6254 auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6255 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
6256 return nullptr;
6257 auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
6258
6259 Constant *LoadedRHS = LoadedCE->getOperand(1);
6260 GlobalValue *LoadedRHSSym;
6261 APInt LoadedRHSOffset;
6262 if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
6263 DL) ||
6264 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
6265 return nullptr;
6266
6267 return LoadedLHSPtr;
6268}
6269
6270// TODO: Need to pass in FastMathFlags
6271static Value *simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q,
6272 bool IsStrict) {
6273 // ldexp(poison, x) -> poison
6274 // ldexp(x, poison) -> poison
6275 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6276 return Op0;
6277
6278 // ldexp(undef, x) -> nan
6279 if (Q.isUndefValue(Op0))
6280 return ConstantFP::getNaN(Op0->getType());
6281
6282 if (!IsStrict) {
6283 // TODO: Could insert a canonicalize for strict
6284
6285 // ldexp(x, undef) -> x
6286 if (Q.isUndefValue(Op1))
6287 return Op0;
6288 }
6289
6290 const APFloat *C = nullptr;
6292
6293 // These cases should be safe, even with strictfp.
6294 // ldexp(0.0, x) -> 0.0
6295 // ldexp(-0.0, x) -> -0.0
6296 // ldexp(inf, x) -> inf
6297 // ldexp(-inf, x) -> -inf
6298 if (C && (C->isZero() || C->isInfinity()))
6299 return Op0;
6300
6301 // These are canonicalization dropping, could do it if we knew how we could
6302 // ignore denormal flushes and target handling of nan payload bits.
6303 if (IsStrict)
6304 return nullptr;
6305
6306 // TODO: Could quiet this with strictfp if the exception mode isn't strict.
6307 if (C && C->isNaN())
6308 return ConstantFP::get(Op0->getType(), C->makeQuiet());
6309
6310 // ldexp(x, 0) -> x
6311
6312 // TODO: Could fold this if we know the exception mode isn't
6313 // strict, we know the denormal mode and other target modes.
6314 if (match(Op1, PatternMatch::m_ZeroInt()))
6315 return Op0;
6316
6317 return nullptr;
6318}
6319
6321 const SimplifyQuery &Q,
6322 const CallBase *Call) {
6323 // Idempotent functions return the same result when called repeatedly.
6324 Intrinsic::ID IID = F->getIntrinsicID();
6325 if (isIdempotent(IID))
6326 if (auto *II = dyn_cast<IntrinsicInst>(Op0))
6327 if (II->getIntrinsicID() == IID)
6328 return II;
6329
6330 if (removesFPFraction(IID)) {
6331 // Converting from int or calling a rounding function always results in a
6332 // finite integral number or infinity. For those inputs, rounding functions
6333 // always return the same value, so the (2nd) rounding is eliminated. Ex:
6334 // floor (sitofp x) -> sitofp x
6335 // round (ceil x) -> ceil x
6336 auto *II = dyn_cast<IntrinsicInst>(Op0);
6337 if ((II && removesFPFraction(II->getIntrinsicID())) ||
6338 match(Op0, m_SIToFP(m_Value())) || match(Op0, m_UIToFP(m_Value())))
6339 return Op0;
6340 }
6341
6342 Value *X;
6343 switch (IID) {
6344 case Intrinsic::fabs:
6345 if (computeKnownFPSignBit(Op0, Q) == false)
6346 return Op0;
6347 break;
6348 case Intrinsic::bswap:
6349 // bswap(bswap(x)) -> x
6350 if (match(Op0, m_BSwap(m_Value(X))))
6351 return X;
6352 break;
6353 case Intrinsic::bitreverse:
6354 // bitreverse(bitreverse(x)) -> x
6355 if (match(Op0, m_BitReverse(m_Value(X))))
6356 return X;
6357 break;
6358 case Intrinsic::ctpop: {
6359 // ctpop(X) -> 1 iff X is non-zero power of 2.
6360 if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ false, Q.AC, Q.CxtI, Q.DT))
6361 return ConstantInt::get(Op0->getType(), 1);
6362 // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
6363 // ctpop(and X, 1) --> and X, 1
6364 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6366 Q))
6367 return Op0;
6368 break;
6369 }
6370 case Intrinsic::exp:
6371 // exp(log(x)) -> x
6372 if (Call->hasAllowReassoc() &&
6374 return X;
6375 break;
6376 case Intrinsic::exp2:
6377 // exp2(log2(x)) -> x
6378 if (Call->hasAllowReassoc() &&
6380 return X;
6381 break;
6382 case Intrinsic::exp10:
6383 // exp10(log10(x)) -> x
6384 if (Call->hasAllowReassoc() &&
6386 return X;
6387 break;
6388 case Intrinsic::log:
6389 // log(exp(x)) -> x
6390 if (Call->hasAllowReassoc() &&
6392 return X;
6393 break;
6394 case Intrinsic::log2:
6395 // log2(exp2(x)) -> x
6396 if (Call->hasAllowReassoc() &&
6398 match(Op0,
6400 return X;
6401 break;
6402 case Intrinsic::log10:
6403 // log10(pow(10.0, x)) -> x
6404 // log10(exp10(x)) -> x
6405 if (Call->hasAllowReassoc() &&
6407 match(Op0,
6409 return X;
6410 break;
6411 case Intrinsic::vector_reverse:
6412 // vector.reverse(vector.reverse(x)) -> x
6413 if (match(Op0, m_VecReverse(m_Value(X))))
6414 return X;
6415 // vector.reverse(splat(X)) -> splat(X)
6416 if (isSplatValue(Op0))
6417 return Op0;
6418 break;
6419 default:
6420 break;
6421 }
6422
6423 return nullptr;
6424}
6425
6426/// Given a min/max intrinsic, see if it can be removed based on having an
6427/// operand that is another min/max intrinsic with shared operand(s). The caller
6428/// is expected to swap the operand arguments to handle commutation.
6430 Value *X, *Y;
6431 if (!match(Op0, m_MaxOrMin(m_Value(X), m_Value(Y))))
6432 return nullptr;
6433
6434 auto *MM0 = dyn_cast<IntrinsicInst>(Op0);
6435 if (!MM0)
6436 return nullptr;
6437 Intrinsic::ID IID0 = MM0->getIntrinsicID();
6438
6439 if (Op1 == X || Op1 == Y ||
6441 // max (max X, Y), X --> max X, Y
6442 if (IID0 == IID)
6443 return MM0;
6444 // max (min X, Y), X --> X
6445 if (IID0 == getInverseMinMaxIntrinsic(IID))
6446 return Op1;
6447 }
6448 return nullptr;
6449}
6450
6451/// Given a min/max intrinsic, see if it can be removed based on having an
6452/// operand that is another min/max intrinsic with shared operand(s). The caller
6453/// is expected to swap the operand arguments to handle commutation.
6455 Value *Op1) {
6456 assert((IID == Intrinsic::maxnum || IID == Intrinsic::minnum ||
6457 IID == Intrinsic::maximum || IID == Intrinsic::minimum) &&
6458 "Unsupported intrinsic");
6459
6460 auto *M0 = dyn_cast<IntrinsicInst>(Op0);
6461 // If Op0 is not the same intrinsic as IID, do not process.
6462 // This is a difference with integer min/max handling. We do not process the
6463 // case like max(min(X,Y),min(X,Y)) => min(X,Y). But it can be handled by GVN.
6464 if (!M0 || M0->getIntrinsicID() != IID)
6465 return nullptr;
6466 Value *X0 = M0->getOperand(0);
6467 Value *Y0 = M0->getOperand(1);
6468 // Simple case, m(m(X,Y), X) => m(X, Y)
6469 // m(m(X,Y), Y) => m(X, Y)
6470 // For minimum/maximum, X is NaN => m(NaN, Y) == NaN and m(NaN, NaN) == NaN.
6471 // For minimum/maximum, Y is NaN => m(X, NaN) == NaN and m(NaN, NaN) == NaN.
6472 // For minnum/maxnum, X is NaN => m(NaN, Y) == Y and m(Y, Y) == Y.
6473 // For minnum/maxnum, Y is NaN => m(X, NaN) == X and m(X, NaN) == X.
6474 if (X0 == Op1 || Y0 == Op1)
6475 return M0;
6476
6477 auto *M1 = dyn_cast<IntrinsicInst>(Op1);
6478 if (!M1)
6479 return nullptr;
6480 Value *X1 = M1->getOperand(0);
6481 Value *Y1 = M1->getOperand(1);
6482 Intrinsic::ID IID1 = M1->getIntrinsicID();
6483 // we have a case m(m(X,Y),m'(X,Y)) taking into account m' is commutative.
6484 // if m' is m or inversion of m => m(m(X,Y),m'(X,Y)) == m(X,Y).
6485 // For minimum/maximum, X is NaN => m(NaN,Y) == m'(NaN, Y) == NaN.
6486 // For minimum/maximum, Y is NaN => m(X,NaN) == m'(X, NaN) == NaN.
6487 // For minnum/maxnum, X is NaN => m(NaN,Y) == m'(NaN, Y) == Y.
6488 // For minnum/maxnum, Y is NaN => m(X,NaN) == m'(X, NaN) == X.
6489 if ((X0 == X1 && Y0 == Y1) || (X0 == Y1 && Y0 == X1))
6490 if (IID1 == IID || getInverseMinMaxIntrinsic(IID1) == IID)
6491 return M0;
6492
6493 return nullptr;
6494}
6495
6497 Value *Op0, Value *Op1,
6498 const SimplifyQuery &Q,
6499 const CallBase *Call) {
6500 unsigned BitWidth = ReturnType->getScalarSizeInBits();
6501 switch (IID) {
6502 case Intrinsic::get_active_lane_mask:
6503 if (match(Op1, m_Zero()))
6504 return ConstantInt::getFalse(ReturnType);
6505 break;
6506 case Intrinsic::abs:
6507 // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
6508 // It is always ok to pick the earlier abs. We'll just lose nsw if its only
6509 // on the outer abs.
6511 return Op0;
6512 break;
6513
6514 case Intrinsic::cttz: {
6515 Value *X;
6516 if (match(Op0, m_Shl(m_One(), m_Value(X))))
6517 return X;
6518 break;
6519 }
6520 case Intrinsic::ctlz: {
6521 Value *X;
6522 if (match(Op0, m_LShr(m_Negative(), m_Value(X))))
6523 return X;
6524 if (match(Op0, m_AShr(m_Negative(), m_Value())))
6525 return Constant::getNullValue(ReturnType);
6526 break;
6527 }
6528 case Intrinsic::ptrmask: {
6529 // NOTE: We can't apply this simplifications based on the value of Op1
6530 // because we need to preserve provenance.
6531 if (Q.isUndefValue(Op0) || match(Op0, m_Zero()))
6532 return Constant::getNullValue(Op0->getType());
6533
6535 Q.DL.getIndexTypeSizeInBits(Op0->getType()) &&
6536 "Invalid mask width");
6537 // If index-width (mask size) is less than pointer-size then mask is
6538 // 1-extended.
6539 if (match(Op1, m_PtrToInt(m_Specific(Op0))))
6540 return Op0;
6541
6542 // NOTE: We may have attributes associated with the return value of the
6543 // llvm.ptrmask intrinsic that will be lost when we just return the
6544 // operand. We should try to preserve them.
6545 if (match(Op1, m_AllOnes()) || Q.isUndefValue(Op1))
6546 return Op0;
6547
6548 Constant *C;
6549 if (match(Op1, m_ImmConstant(C))) {
6550 KnownBits PtrKnown = computeKnownBits(Op0, Q);
6551 // See if we only masking off bits we know are already zero due to
6552 // alignment.
6553 APInt IrrelevantPtrBits =
6554 PtrKnown.Zero.zextOrTrunc(C->getType()->getScalarSizeInBits());
6556 Instruction::Or, C, ConstantInt::get(C->getType(), IrrelevantPtrBits),
6557 Q.DL);
6558 if (C != nullptr && C->isAllOnesValue())
6559 return Op0;
6560 }
6561 break;
6562 }
6563 case Intrinsic::smax:
6564 case Intrinsic::smin:
6565 case Intrinsic::umax:
6566 case Intrinsic::umin: {
6567 // If the arguments are the same, this is a no-op.
6568 if (Op0 == Op1)
6569 return Op0;
6570
6571 // Canonicalize immediate constant operand as Op1.
6572 if (match(Op0, m_ImmConstant()))
6573 std::swap(Op0, Op1);
6574
6575 // Assume undef is the limit value.
6576 if (Q.isUndefValue(Op1))
6577 return ConstantInt::get(
6579
6580 const APInt *C;
6581 if (match(Op1, m_APIntAllowPoison(C))) {
6582 // Clamp to limit value. For example:
6583 // umax(i8 %x, i8 255) --> 255
6585 return ConstantInt::get(ReturnType, *C);
6586
6587 // If the constant op is the opposite of the limit value, the other must
6588 // be larger/smaller or equal. For example:
6589 // umin(i8 %x, i8 255) --> %x
6592 return Op0;
6593
6594 // Remove nested call if constant operands allow it. Example:
6595 // max (max X, 7), 5 -> max X, 7
6596 auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0);
6597 if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
6598 // TODO: loosen undef/splat restrictions for vector constants.
6599 Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
6600 const APInt *InnerC;
6601 if ((match(M00, m_APInt(InnerC)) || match(M01, m_APInt(InnerC))) &&
6602 ICmpInst::compare(*InnerC, *C,
6605 return Op0;
6606 }
6607 }
6608
6609 if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1))
6610 return V;
6611 if (Value *V = foldMinMaxSharedOp(IID, Op1, Op0))
6612 return V;
6613
6614 ICmpInst::Predicate Pred =
6616 if (isICmpTrue(Pred, Op0, Op1, Q.getWithoutUndef(), RecursionLimit))
6617 return Op0;
6618 if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit))
6619 return Op1;
6620
6621 break;
6622 }
6623 case Intrinsic::scmp:
6624 case Intrinsic::ucmp: {
6625 // Fold to a constant if the relationship between operands can be
6626 // established with certainty
6627 if (isICmpTrue(CmpInst::ICMP_EQ, Op0, Op1, Q, RecursionLimit))
6628 return Constant::getNullValue(ReturnType);
6629
6630 ICmpInst::Predicate PredGT =
6631 IID == Intrinsic::scmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
6632 if (isICmpTrue(PredGT, Op0, Op1, Q, RecursionLimit))
6633 return ConstantInt::get(ReturnType, 1);
6634
6635 ICmpInst::Predicate PredLT =
6636 IID == Intrinsic::scmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
6637 if (isICmpTrue(PredLT, Op0, Op1, Q, RecursionLimit))
6638 return ConstantInt::getSigned(ReturnType, -1);
6639
6640 break;
6641 }
6642 case Intrinsic::usub_with_overflow:
6643 case Intrinsic::ssub_with_overflow:
6644 // X - X -> { 0, false }
6645 // X - undef -> { 0, false }
6646 // undef - X -> { 0, false }
6647 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6648 return Constant::getNullValue(ReturnType);
6649 break;
6650 case Intrinsic::uadd_with_overflow:
6651 case Intrinsic::sadd_with_overflow:
6652 // X + undef -> { -1, false }
6653 // undef + x -> { -1, false }
6654 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) {
6655 return ConstantStruct::get(
6656 cast<StructType>(ReturnType),
6657 {Constant::getAllOnesValue(ReturnType->getStructElementType(0)),
6658 Constant::getNullValue(ReturnType->getStructElementType(1))});
6659 }
6660 break;
6661 case Intrinsic::umul_with_overflow:
6662 case Intrinsic::smul_with_overflow:
6663 // 0 * X -> { 0, false }
6664 // X * 0 -> { 0, false }
6665 if (match(Op0, m_Zero()) || match(Op1, m_Zero()))
6666 return Constant::getNullValue(ReturnType);
6667 // undef * X -> { 0, false }
6668 // X * undef -> { 0, false }
6669 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6670 return Constant::getNullValue(ReturnType);
6671 break;
6672 case Intrinsic::uadd_sat:
6673 // sat(MAX + X) -> MAX
6674 // sat(X + MAX) -> MAX
6675 if (match(Op0, m_AllOnes()) || match(Op1, m_AllOnes()))
6676 return Constant::getAllOnesValue(ReturnType);
6677 [[fallthrough]];
6678 case Intrinsic::sadd_sat:
6679 // sat(X + undef) -> -1
6680 // sat(undef + X) -> -1
6681 // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
6682 // For signed: Assume undef is ~X, in which case X + ~X = -1.
6683 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6684 return Constant::getAllOnesValue(ReturnType);
6685
6686 // X + 0 -> X
6687 if (match(Op1, m_Zero()))
6688 return Op0;
6689 // 0 + X -> X
6690 if (match(Op0, m_Zero()))
6691 return Op1;
6692 break;
6693 case Intrinsic::usub_sat:
6694 // sat(0 - X) -> 0, sat(X - MAX) -> 0
6695 if (match(Op0, m_Zero()) || match(Op1, m_AllOnes()))
6696 return Constant::getNullValue(ReturnType);
6697 [[fallthrough]];
6698 case Intrinsic::ssub_sat:
6699 // X - X -> 0, X - undef -> 0, undef - X -> 0
6700 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6701 return Constant::getNullValue(ReturnType);
6702 // X - 0 -> X
6703 if (match(Op1, m_Zero()))
6704 return Op0;
6705 break;
6706 case Intrinsic::load_relative:
6707 if (auto *C0 = dyn_cast<Constant>(Op0))
6708 if (auto *C1 = dyn_cast<Constant>(Op1))
6709 return simplifyRelativeLoad(C0, C1, Q.DL);
6710 break;
6711 case Intrinsic::powi:
6712 if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
6713 // powi(x, 0) -> 1.0
6714 if (Power->isZero())
6715 return ConstantFP::get(Op0->getType(), 1.0);
6716 // powi(x, 1) -> x
6717 if (Power->isOne())
6718 return Op0;
6719 }
6720 break;
6721 case Intrinsic::ldexp:
6722 return simplifyLdexp(Op0, Op1, Q, false);
6723 case Intrinsic::copysign:
6724 // copysign X, X --> X
6725 if (Op0 == Op1)
6726 return Op0;
6727 // copysign -X, X --> X
6728 // copysign X, -X --> -X
6729 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
6730 match(Op1, m_FNeg(m_Specific(Op0))))
6731 return Op1;
6732 break;
6733 case Intrinsic::is_fpclass: {
6734 uint64_t Mask = cast<ConstantInt>(Op1)->getZExtValue();
6735 // If all tests are made, it doesn't matter what the value is.
6736 if ((Mask & fcAllFlags) == fcAllFlags)
6737 return ConstantInt::get(ReturnType, true);
6738 if ((Mask & fcAllFlags) == 0)
6739 return ConstantInt::get(ReturnType, false);
6740 if (Q.isUndefValue(Op0))
6741 return UndefValue::get(ReturnType);
6742 break;
6743 }
6744 case Intrinsic::maxnum:
6745 case Intrinsic::minnum:
6746 case Intrinsic::maximum:
6747 case Intrinsic::minimum: {
6748 // If the arguments are the same, this is a no-op.
6749 if (Op0 == Op1)
6750 return Op0;
6751
6752 // Canonicalize constant operand as Op1.
6753 if (isa<Constant>(Op0))
6754 std::swap(Op0, Op1);
6755
6756 // If an argument is undef, return the other argument.
6757 if (Q.isUndefValue(Op1))
6758 return Op0;
6759
6760 bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
6761 bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum;
6762
6763 // minnum(X, nan) -> X
6764 // maxnum(X, nan) -> X
6765 // minimum(X, nan) -> nan
6766 // maximum(X, nan) -> nan
6767 if (match(Op1, m_NaN()))
6768 return PropagateNaN ? propagateNaN(cast<Constant>(Op1)) : Op0;
6769
6770 // In the following folds, inf can be replaced with the largest finite
6771 // float, if the ninf flag is set.
6772 const APFloat *C;
6773 if (match(Op1, m_APFloat(C)) &&
6774 (C->isInfinity() || (Call && Call->hasNoInfs() && C->isLargest()))) {
6775 // minnum(X, -inf) -> -inf
6776 // maxnum(X, +inf) -> +inf
6777 // minimum(X, -inf) -> -inf if nnan
6778 // maximum(X, +inf) -> +inf if nnan
6779 if (C->isNegative() == IsMin &&
6780 (!PropagateNaN || (Call && Call->hasNoNaNs())))
6781 return ConstantFP::get(ReturnType, *C);
6782
6783 // minnum(X, +inf) -> X if nnan
6784 // maxnum(X, -inf) -> X if nnan
6785 // minimum(X, +inf) -> X
6786 // maximum(X, -inf) -> X
6787 if (C->isNegative() != IsMin &&
6788 (PropagateNaN || (Call && Call->hasNoNaNs())))
6789 return Op0;
6790 }
6791
6792 // Min/max of the same operation with common operand:
6793 // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
6794 if (Value *V = foldMinimumMaximumSharedOp(IID, Op0, Op1))
6795 return V;
6796 if (Value *V = foldMinimumMaximumSharedOp(IID, Op1, Op0))
6797 return V;
6798
6799 break;
6800 }
6801 case Intrinsic::vector_extract: {
6802 // (extract_vector (insert_vector _, X, 0), 0) -> X
6803 unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue();
6804 Value *X = nullptr;
6806 m_Zero())) &&
6807 IdxN == 0 && X->getType() == ReturnType)
6808 return X;
6809
6810 break;
6811 }
6812 default:
6813 break;
6814 }
6815
6816 return nullptr;
6817}
6818
6820 ArrayRef<Value *> Args,
6821 const SimplifyQuery &Q) {
6822 // Operand bundles should not be in Args.
6823 assert(Call->arg_size() == Args.size());
6824 unsigned NumOperands = Args.size();
6825 Function *F = cast<Function>(Callee);
6826 Intrinsic::ID IID = F->getIntrinsicID();
6827
6830 return PoisonValue::get(F->getReturnType());
6831 // Most of the intrinsics with no operands have some kind of side effect.
6832 // Don't simplify.
6833 if (!NumOperands) {
6834 switch (IID) {
6835 case Intrinsic::vscale: {
6836 Type *RetTy = F->getReturnType();
6837 ConstantRange CR = getVScaleRange(Call->getFunction(), 64);
6838 if (const APInt *C = CR.getSingleElement())
6839 return ConstantInt::get(RetTy, C->getZExtValue());
6840 return nullptr;
6841 }
6842 default:
6843 return nullptr;
6844 }
6845 }
6846
6847 if (NumOperands == 1)
6848 return simplifyUnaryIntrinsic(F, Args[0], Q, Call);
6849
6850 if (NumOperands == 2)
6851 return simplifyBinaryIntrinsic(IID, F->getReturnType(), Args[0], Args[1], Q,
6852 Call);
6853
6854 // Handle intrinsics with 3 or more arguments.
6855 switch (IID) {
6856 case Intrinsic::masked_load:
6857 case Intrinsic::masked_gather: {
6858 Value *MaskArg = Args[2];
6859 Value *PassthruArg = Args[3];
6860 // If the mask is all zeros or undef, the "passthru" argument is the result.
6861 if (maskIsAllZeroOrUndef(MaskArg))
6862 return PassthruArg;
6863 return nullptr;
6864 }
6865 case Intrinsic::fshl:
6866 case Intrinsic::fshr: {
6867 Value *Op0 = Args[0], *Op1 = Args[1], *ShAmtArg = Args[2];
6868
6869 // If both operands are undef, the result is undef.
6870 if (Q.isUndefValue(Op0) && Q.isUndefValue(Op1))
6871 return UndefValue::get(F->getReturnType());
6872
6873 // If shift amount is undef, assume it is zero.
6874 if (Q.isUndefValue(ShAmtArg))
6875 return Args[IID == Intrinsic::fshl ? 0 : 1];
6876
6877 const APInt *ShAmtC;
6878 if (match(ShAmtArg, m_APInt(ShAmtC))) {
6879 // If there's effectively no shift, return the 1st arg or 2nd arg.
6880 APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth());
6881 if (ShAmtC->urem(BitWidth).isZero())
6882 return Args[IID == Intrinsic::fshl ? 0 : 1];
6883 }
6884
6885 // Rotating zero by anything is zero.
6886 if (match(Op0, m_Zero()) && match(Op1, m_Zero()))
6887 return ConstantInt::getNullValue(F->getReturnType());
6888
6889 // Rotating -1 by anything is -1.
6890 if (match(Op0, m_AllOnes()) && match(Op1, m_AllOnes()))
6891 return ConstantInt::getAllOnesValue(F->getReturnType());
6892
6893 return nullptr;
6894 }
6895 case Intrinsic::experimental_constrained_fma: {
6897 if (Value *V = simplifyFPOp(Args, {}, Q, *FPI->getExceptionBehavior(),
6898 *FPI->getRoundingMode()))
6899 return V;
6900 return nullptr;
6901 }
6902 case Intrinsic::fma:
6903 case Intrinsic::fmuladd: {
6904 if (Value *V = simplifyFPOp(Args, {}, Q, fp::ebIgnore,
6906 return V;
6907 return nullptr;
6908 }
6909 case Intrinsic::smul_fix:
6910 case Intrinsic::smul_fix_sat: {
6911 Value *Op0 = Args[0];
6912 Value *Op1 = Args[1];
6913 Value *Op2 = Args[2];
6914 Type *ReturnType = F->getReturnType();
6915
6916 // Canonicalize constant operand as Op1 (ConstantFolding handles the case
6917 // when both Op0 and Op1 are constant so we do not care about that special
6918 // case here).
6919 if (isa<Constant>(Op0))
6920 std::swap(Op0, Op1);
6921
6922 // X * 0 -> 0
6923 if (match(Op1, m_Zero()))
6924 return Constant::getNullValue(ReturnType);
6925
6926 // X * undef -> 0
6927 if (Q.isUndefValue(Op1))
6928 return Constant::getNullValue(ReturnType);
6929
6930 // X * (1 << Scale) -> X
6931 APInt ScaledOne =
6932 APInt::getOneBitSet(ReturnType->getScalarSizeInBits(),
6933 cast<ConstantInt>(Op2)->getZExtValue());
6934 if (ScaledOne.isNonNegative() && match(Op1, m_SpecificInt(ScaledOne)))
6935 return Op0;
6936
6937 return nullptr;
6938 }
6939 case Intrinsic::vector_insert: {
6940 Value *Vec = Args[0];
6941 Value *SubVec = Args[1];
6942 Value *Idx = Args[2];
6943 Type *ReturnType = F->getReturnType();
6944
6945 // (insert_vector Y, (extract_vector X, 0), 0) -> X
6946 // where: Y is X, or Y is undef
6947 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
6948 Value *X = nullptr;
6949 if (match(SubVec,
6951 (Q.isUndefValue(Vec) || Vec == X) && IdxN == 0 &&
6952 X->getType() == ReturnType)
6953 return X;
6954
6955 return nullptr;
6956 }
6957 case Intrinsic::experimental_constrained_fadd: {
6959 return simplifyFAddInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6960 *FPI->getExceptionBehavior(),
6961 *FPI->getRoundingMode());
6962 }
6963 case Intrinsic::experimental_constrained_fsub: {
6965 return simplifyFSubInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6966 *FPI->getExceptionBehavior(),
6967 *FPI->getRoundingMode());
6968 }
6969 case Intrinsic::experimental_constrained_fmul: {
6971 return simplifyFMulInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6972 *FPI->getExceptionBehavior(),
6973 *FPI->getRoundingMode());
6974 }
6975 case Intrinsic::experimental_constrained_fdiv: {
6977 return simplifyFDivInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6978 *FPI->getExceptionBehavior(),
6979 *FPI->getRoundingMode());
6980 }
6981 case Intrinsic::experimental_constrained_frem: {
6983 return simplifyFRemInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6984 *FPI->getExceptionBehavior(),
6985 *FPI->getRoundingMode());
6986 }
6987 case Intrinsic::experimental_constrained_ldexp:
6988 return simplifyLdexp(Args[0], Args[1], Q, true);
6989 case Intrinsic::experimental_gc_relocate: {
6991 Value *DerivedPtr = GCR.getDerivedPtr();
6992 Value *BasePtr = GCR.getBasePtr();
6993
6994 // Undef is undef, even after relocation.
6995 if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
6996 return UndefValue::get(GCR.getType());
6997 }
6998
6999 if (auto *PT = dyn_cast<PointerType>(GCR.getType())) {
7000 // For now, the assumption is that the relocation of null will be null
7001 // for most any collector. If this ever changes, a corresponding hook
7002 // should be added to GCStrategy and this code should check it first.
7003 if (isa<ConstantPointerNull>(DerivedPtr)) {
7004 // Use null-pointer of gc_relocate's type to replace it.
7005 return ConstantPointerNull::get(PT);
7006 }
7007 }
7008 return nullptr;
7009 }
7010 case Intrinsic::experimental_vp_reverse: {
7011 Value *Vec = Call->getArgOperand(0);
7012 Value *Mask = Call->getArgOperand(1);
7013 Value *EVL = Call->getArgOperand(2);
7014
7015 Value *X;
7016 // vp.reverse(vp.reverse(X)) == X (with all ones mask and matching EVL)
7017 if (match(Mask, m_AllOnes()) &&
7019 m_Value(X), m_AllOnes(), m_Specific(EVL))))
7020 return X;
7021
7022 // vp.reverse(splat(X)) -> splat(X) (regardless of mask and EVL)
7023 if (isSplatValue(Vec))
7024 return Vec;
7025 return nullptr;
7026 }
7027 default:
7028 return nullptr;
7029 }
7030}
7031
7033 ArrayRef<Value *> Args,
7034 const SimplifyQuery &Q) {
7035 auto *F = dyn_cast<Function>(Callee);
7036 if (!F || !canConstantFoldCallTo(Call, F))
7037 return nullptr;
7038
7039 SmallVector<Constant *, 4> ConstantArgs;
7040 ConstantArgs.reserve(Args.size());
7041 for (Value *Arg : Args) {
7043 if (!C) {
7044 if (isa<MetadataAsValue>(Arg))
7045 continue;
7046 return nullptr;
7047 }
7048 ConstantArgs.push_back(C);
7049 }
7050
7051 return ConstantFoldCall(Call, F, ConstantArgs, Q.TLI);
7052}
7053
7055 const SimplifyQuery &Q) {
7056 // Args should not contain operand bundle operands.
7057 assert(Call->arg_size() == Args.size());
7058
7059 // musttail calls can only be simplified if they are also DCEd.
7060 // As we can't guarantee this here, don't simplify them.
7061 if (Call->isMustTailCall())
7062 return nullptr;
7063
7064 // call undef -> poison
7065 // call null -> poison
7066 if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee))
7067 return PoisonValue::get(Call->getType());
7068
7069 if (Value *V = tryConstantFoldCall(Call, Callee, Args, Q))
7070 return V;
7071
7072 auto *F = dyn_cast<Function>(Callee);
7073 if (F && F->isIntrinsic())
7074 if (Value *Ret = simplifyIntrinsic(Call, Callee, Args, Q))
7075 return Ret;
7076
7077 return nullptr;
7078}
7079
7082 SmallVector<Value *, 4> Args(Call->args());
7083 if (Value *V = tryConstantFoldCall(Call, Call->getCalledOperand(), Args, Q))
7084 return V;
7085 if (Value *Ret = simplifyIntrinsic(Call, Call->getCalledOperand(), Args, Q))
7086 return Ret;
7087 return nullptr;
7088}
7089
7090/// Given operands for a Freeze, see if we can fold the result.
7092 // Use a utility function defined in ValueTracking.
7094 return Op0;
7095 // We have room for improvement.
7096 return nullptr;
7097}
7098
7100 return ::simplifyFreezeInst(Op0, Q);
7101}
7102
7104 const SimplifyQuery &Q) {
7105 if (LI->isVolatile())
7106 return nullptr;
7107
7108 if (auto *PtrOpC = dyn_cast<Constant>(PtrOp))
7109 return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Q.DL);
7110
7111 // We can only fold the load if it is from a constant global with definitive
7112 // initializer. Skip expensive logic if this is not the case.
7114 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7115 return nullptr;
7116
7117 // If GlobalVariable's initializer is uniform, then return the constant
7118 // regardless of its offset.
7119 if (Constant *C = ConstantFoldLoadFromUniformValue(GV->getInitializer(),
7120 LI->getType(), Q.DL))
7121 return C;
7122
7123 // Try to convert operand into a constant by stripping offsets while looking
7124 // through invariant.group intrinsics.
7126 PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
7127 Q.DL, Offset, /* AllowNonInbounts */ true,
7128 /* AllowInvariantGroup */ true);
7129 if (PtrOp == GV) {
7130 // Index size may have changed due to address space casts.
7131 Offset = Offset.sextOrTrunc(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()));
7132 return ConstantFoldLoadFromConstPtr(GV, LI->getType(), std::move(Offset),
7133 Q.DL);
7134 }
7135
7136 return nullptr;
7137}
7138
7139/// See if we can compute a simplified version of this instruction.
7140/// If not, this returns null.
7141
7143 ArrayRef<Value *> NewOps,
7144 const SimplifyQuery &SQ,
7145 unsigned MaxRecurse) {
7146 assert(I->getFunction() && "instruction should be inserted in a function");
7147 assert((!SQ.CxtI || SQ.CxtI->getFunction() == I->getFunction()) &&
7148 "context instruction should be in the same function");
7149
7150 const SimplifyQuery Q = SQ.CxtI ? SQ : SQ.getWithInstruction(I);
7151
7152 switch (I->getOpcode()) {
7153 default:
7154 if (all_of(NewOps, IsaPred<Constant>)) {
7155 SmallVector<Constant *, 8> NewConstOps(NewOps.size());
7156 transform(NewOps, NewConstOps.begin(),
7157 [](Value *V) { return cast<Constant>(V); });
7158 return ConstantFoldInstOperands(I, NewConstOps, Q.DL, Q.TLI);
7159 }
7160 return nullptr;
7161 case Instruction::FNeg:
7162 return simplifyFNegInst(NewOps[0], I->getFastMathFlags(), Q, MaxRecurse);
7163 case Instruction::FAdd:
7164 return simplifyFAddInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7165 MaxRecurse);
7166 case Instruction::Add:
7167 return simplifyAddInst(
7168 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7169 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7170 case Instruction::FSub:
7171 return simplifyFSubInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7172 MaxRecurse);
7173 case Instruction::Sub:
7174 return simplifySubInst(
7175 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7176 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7177 case Instruction::FMul:
7178 return simplifyFMulInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7179 MaxRecurse);
7180 case Instruction::Mul:
7181 return simplifyMulInst(
7182 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7183 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7184 case Instruction::SDiv:
7185 return simplifySDivInst(NewOps[0], NewOps[1],
7187 MaxRecurse);
7188 case Instruction::UDiv:
7189 return simplifyUDivInst(NewOps[0], NewOps[1],
7191 MaxRecurse);
7192 case Instruction::FDiv:
7193 return simplifyFDivInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7194 MaxRecurse);
7195 case Instruction::SRem:
7196 return simplifySRemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7197 case Instruction::URem:
7198 return simplifyURemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7199 case Instruction::FRem:
7200 return simplifyFRemInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7201 MaxRecurse);
7202 case Instruction::Shl:
7203 return simplifyShlInst(
7204 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7205 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7206 case Instruction::LShr:
7207 return simplifyLShrInst(NewOps[0], NewOps[1],
7209 MaxRecurse);
7210 case Instruction::AShr:
7211 return simplifyAShrInst(NewOps[0], NewOps[1],
7213 MaxRecurse);
7214 case Instruction::And:
7215 return simplifyAndInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7216 case Instruction::Or:
7217 return simplifyOrInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7218 case Instruction::Xor:
7219 return simplifyXorInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7220 case Instruction::ICmp:
7221 return simplifyICmpInst(cast<ICmpInst>(I)->getCmpPredicate(), NewOps[0],
7222 NewOps[1], Q, MaxRecurse);
7223 case Instruction::FCmp:
7224 return simplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), NewOps[0],
7225 NewOps[1], I->getFastMathFlags(), Q, MaxRecurse);
7226 case Instruction::Select:
7227 return simplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse);
7228 case Instruction::GetElementPtr: {
7229 auto *GEPI = cast<GetElementPtrInst>(I);
7230 return simplifyGEPInst(GEPI->getSourceElementType(), NewOps[0],
7231 ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q,
7232 MaxRecurse);
7233 }
7234 case Instruction::InsertValue: {
7236 return simplifyInsertValueInst(NewOps[0], NewOps[1], IV->getIndices(), Q,
7237 MaxRecurse);
7238 }
7239 case Instruction::InsertElement:
7240 return simplifyInsertElementInst(NewOps[0], NewOps[1], NewOps[2], Q);
7241 case Instruction::ExtractValue: {
7242 auto *EVI = cast<ExtractValueInst>(I);
7243 return simplifyExtractValueInst(NewOps[0], EVI->getIndices(), Q,
7244 MaxRecurse);
7245 }
7246 case Instruction::ExtractElement:
7247 return simplifyExtractElementInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7248 case Instruction::ShuffleVector: {
7249 auto *SVI = cast<ShuffleVectorInst>(I);
7250 return simplifyShuffleVectorInst(NewOps[0], NewOps[1],
7251 SVI->getShuffleMask(), SVI->getType(), Q,
7252 MaxRecurse);
7253 }
7254 case Instruction::PHI:
7255 return simplifyPHINode(cast<PHINode>(I), NewOps, Q);
7256 case Instruction::Call:
7257 return simplifyCall(
7258 cast<CallInst>(I), NewOps.back(),
7259 NewOps.drop_back(1 + cast<CallInst>(I)->getNumTotalBundleOperands()), Q);
7260 case Instruction::Freeze:
7261 return llvm::simplifyFreezeInst(NewOps[0], Q);
7262#define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
7263#include "llvm/IR/Instruction.def"
7264#undef HANDLE_CAST_INST
7265 return simplifyCastInst(I->getOpcode(), NewOps[0], I->getType(), Q,
7266 MaxRecurse);
7267 case Instruction::Alloca:
7268 // No simplifications for Alloca and it can't be constant folded.
7269 return nullptr;
7270 case Instruction::Load:
7271 return simplifyLoadInst(cast<LoadInst>(I), NewOps[0], Q);
7272 }
7273}
7274
7276 ArrayRef<Value *> NewOps,
7277 const SimplifyQuery &SQ) {
7278 assert(NewOps.size() == I->getNumOperands() &&
7279 "Number of operands should match the instruction!");
7280 return ::simplifyInstructionWithOperands(I, NewOps, SQ, RecursionLimit);
7281}
7282
7284 SmallVector<Value *, 8> Ops(I->operands());
7286
7287 /// If called on unreachable code, the instruction may simplify to itself.
7288 /// Make life easier for users by detecting that case here, and returning a
7289 /// safe value instead.
7290 return Result == I ? PoisonValue::get(I->getType()) : Result;
7291}
7292
7293/// Implementation of recursive simplification through an instruction's
7294/// uses.
7295///
7296/// This is the common implementation of the recursive simplification routines.
7297/// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
7298/// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
7299/// instructions to process and attempt to simplify it using
7300/// InstructionSimplify. Recursively visited users which could not be
7301/// simplified themselves are to the optional UnsimplifiedUsers set for
7302/// further processing by the caller.
7303///
7304/// This routine returns 'true' only when *it* simplifies something. The passed
7305/// in simplified value does not count toward this.
7307 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7308 const DominatorTree *DT, AssumptionCache *AC,
7309 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) {
7310 bool Simplified = false;
7312 const DataLayout &DL = I->getDataLayout();
7313
7314 // If we have an explicit value to collapse to, do that round of the
7315 // simplification loop by hand initially.
7316 if (SimpleV) {
7317 for (User *U : I->users())
7318 if (U != I)
7319 Worklist.insert(cast<Instruction>(U));
7320
7321 // Replace the instruction with its simplified value.
7322 I->replaceAllUsesWith(SimpleV);
7323
7324 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7325 I->eraseFromParent();
7326 } else {
7327 Worklist.insert(I);
7328 }
7329
7330 // Note that we must test the size on each iteration, the worklist can grow.
7331 for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
7332 I = Worklist[Idx];
7333
7334 // See if this instruction simplifies.
7335 SimpleV = simplifyInstruction(I, {DL, TLI, DT, AC});
7336 if (!SimpleV) {
7337 if (UnsimplifiedUsers)
7338 UnsimplifiedUsers->insert(I);
7339 continue;
7340 }
7341
7342 Simplified = true;
7343
7344 // Stash away all the uses of the old instruction so we can check them for
7345 // recursive simplifications after a RAUW. This is cheaper than checking all
7346 // uses of To on the recursive step in most cases.
7347 for (User *U : I->users())
7348 Worklist.insert(cast<Instruction>(U));
7349
7350 // Replace the instruction with its simplified value.
7351 I->replaceAllUsesWith(SimpleV);
7352
7353 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7354 I->eraseFromParent();
7355 }
7356 return Simplified;
7357}
7358
7360 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7361 const DominatorTree *DT, AssumptionCache *AC,
7362 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers) {
7363 assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
7364 assert(SimpleV && "Must provide a simplified value.");
7365 return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC,
7366 UnsimplifiedUsers);
7367}
7368
7369namespace llvm {
7371 auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>();
7372 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
7373 auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
7374 auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
7375 auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>();
7376 auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;
7377 return {F.getDataLayout(), TLI, DT, AC};
7378}
7379
7381 const DataLayout &DL) {
7382 return {DL, &AR.TLI, &AR.DT, &AR.AC};
7383}
7384
7385template <class T, class... TArgs>
7387 Function &F) {
7388 auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F);
7389 auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F);
7390 auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F);
7391 return {F.getDataLayout(), TLI, DT, AC};
7392}
7394 Function &);
7395
7397 if (!CanUseUndef)
7398 return false;
7399
7400 return match(V, m_Undef());
7401}
7402
7403} // namespace llvm
7404
7405void InstSimplifyFolder::anchor() {}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
IRTranslator LLVM IR MI
static Value * simplifyCmpSelFalseCase(CmpPredicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify comparison with false branch of select.
static Value * simplifyCmpSelCase(CmpPredicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse, Constant *TrueOrFalse)
Simplify comparison with true or false branch of select: sel = select i1 cond, i32 tv,...
static Value * foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1)
Given a min/max intrinsic, see if it can be removed based on having an operand that is another min/ma...
static Value * expandCommutativeBinOp(Instruction::BinaryOps Opcode, Value *L, Value *R, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify binops of form "A op (B op' C)" or the commuted variant by distributing op over op'.
static Constant * foldOrCommuteConstant(Instruction::BinaryOps Opcode, Value *&Op0, Value *&Op1, const SimplifyQuery &Q)
static bool haveNonOverlappingStorage(const Value *V1, const Value *V2)
Return true if V1 and V2 are each the base of some distict storage region [V, object_size(V)] which d...
static Constant * foldConstant(Instruction::UnaryOps Opcode, Value *&Op, const SimplifyQuery &Q)
static Value * handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
We know comparison with both branches of select can be simplified, but they are not equal.
static Value * threadCmpOverPHI(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a comparison with a PHI instruction, try to simplify the comparison by seeing whether ...
static Constant * propagateNaN(Constant *In)
Try to propagate existing NaN values when possible.
static Value * simplifyICmpOfBools(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Fold an icmp when its operands have i1 scalar type.
static Value * simplifyICmpWithBinOpOnLHS(CmpPredicate Pred, BinaryOperator *LBO, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
static void getUnsignedMonotonicValues(SmallPtrSetImpl< Value * > &Res, Value *V, MonotonicType Type, const SimplifyQuery &Q, unsigned Depth=0)
Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq).
static Value * simplifyRelativeLoad(Constant *Ptr, Constant *Offset, const DataLayout &DL)
static Value * simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
These are simplifications common to SDiv and UDiv.
static Value * simplifyPHINode(PHINode *PN, ArrayRef< Value * > IncomingValues, const SimplifyQuery &Q)
See if we can fold the given phi. If not, returns null.
@ RecursionLimit
static bool isSameCompare(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS)
isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
static Value * simplifyAndCommutative(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
static bool isIdempotent(Intrinsic::ID ID)
static std::optional< ConstantRange > getRange(Value *V, const InstrInfoQuery &IIQ)
Helper method to get range from metadata or attribute.
static Value * simplifyAndOrOfICmpsWithCtpop(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
Try to simplify and/or of icmp with ctpop intrinsic.
static Value * simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd, const SimplifyQuery &Q)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
static Value * tryConstantFoldCall(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
static Value * simplifyWithOpsReplaced(Value *V, ArrayRef< std::pair< Value *, Value * > > Ops, const SimplifyQuery &Q, bool AllowRefinement, SmallVectorImpl< Instruction * > *DropFlags, unsigned MaxRecurse)
static Value * simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
static Value * simplifyICmpWithMinMax(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
simplify integer comparisons where at least one operand of the compare matches an integer min/max idi...
static Value * simplifyCmpSelTrueCase(CmpPredicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify comparison with true branch of select.
static Value * simplifyIntrinsic(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
static Value * simplifyICmpUsingMonotonicValues(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q)
Returns true if a shift by Amount always yields poison.
static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, bool AllowNonInbounds=false)
Compute the base pointer and cumulative constant offsets for V.
static Value * simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an LShr or AShr, see if we can fold the result.
static Value * simplifyICmpWithIntrinsicOnLHS(CmpPredicate Pred, Value *LHS, Value *RHS)
static Value * simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Test if there is a dominating equivalence condition for the two operands.
static Value * simplifyFPUnOp(unsigned, Value *, const FastMathFlags &, const SimplifyQuery &, unsigned)
Given the operand for a UnaryOperator, see if we can fold the result.
static Value * simplifyICmpWithBinOp(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
TODO: A large part of this logic is duplicated in InstCombine's foldICmpBinOp().
static Value * simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
static Value * expandBinOp(Instruction::BinaryOps Opcode, Value *V, Value *OtherOp, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a binary operator of form "V op OtherOp" where V is "(B0 opex B1)" by distributing 'o...
static Value * simplifyICmpWithZero(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Try hard to fold icmp with zero RHS because this is a common case.
static Value * simplifySelectWithFCmp(Value *Cond, Value *T, Value *F, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a select instruction when its condition operand is a floating-point comparison.
static Constant * getFalse(Type *Ty)
For a boolean type or a vector of boolean type, return false or a vector with every element false.
static Value * simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Check for common or similar folds of integer division or integer remainder.
static bool removesFPFraction(Intrinsic::ID ID)
Return true if the intrinsic rounds a floating-point value to an integral floating-point value (not a...
static Value * simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
static Value * simplifySelectWithEquivalence(ArrayRef< std::pair< Value *, Value * > > Replacements, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a select instruction when its condition operand is an integer equality or floating-po...
static bool trySimplifyICmpWithAdds(CmpPredicate Pred, Value *LHS, Value *RHS, const InstrInfoQuery &IIQ)
static Value * simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, const APInt *Y, bool TrueWhenUnset)
Try to simplify a select instruction when its condition operand is an integer comparison where one op...
static Value * simplifyAssociativeBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Generic simplifications for associative binary operations.
static Value * threadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a binary operation with an operand that is a PHI instruction, try to simplify the bino...
static Value * simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS, CmpPredicate Pred, Value *TVal, Value *FVal)
static Constant * simplifyFPOp(ArrayRef< Value * > Ops, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior, RoundingMode Rounding)
Perform folds that are common to any floating-point operation.
static Value * threadCmpOverSelect(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a comparison with a select instruction, try to simplify the comparison by seeing wheth...
static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Implementation of recursive simplification through an instruction's uses.
static bool isAllocDisjoint(const Value *V)
Return true if the underlying object (storage) must be disjoint from storage returned by any noalias ...
static Constant * getTrue(Type *Ty)
For a boolean type or a vector of boolean type, return true or a vector with every element true.
static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q, unsigned MaxRecurse, bool IsSigned)
Return true if we can simplify X / Y to 0.
static Value * simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q, bool IsStrict)
static Value * simplifyLogicOfAddSub(Value *Op0, Value *Op1, Instruction::BinaryOps Opcode)
Given a bitwise logic op, check if the operands are add/sub with a common source value and inverted c...
static Value * simplifySelectWithBitTest(Value *CondVal, Value *TrueVal, Value *FalseVal)
An alternative way to test if a bit is set or not.
static Value * simplifyOrLogic(Value *X, Value *Y)
static Type * getCompareTy(Value *Op)
static Value * simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
static bool isICmpTrue(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Given a predicate and two operands, return true if the comparison is true.
static Value * foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1, int MaskVal, Value *RootVec, unsigned MaxRecurse)
For the given destination element of a shuffle, peek through shuffles to match a root vector source o...
static Value * simplifyAndOrOfFCmps(const SimplifyQuery &Q, FCmpInst *LHS, FCmpInst *RHS, bool IsAnd)
static Value * simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
static Value * extractEquivalentCondition(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS)
Rummage around inside V looking for something equivalent to the comparison "LHS Pred RHS".
static Value * simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0, Value *Op1, bool IsAnd)
static Value * threadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a binary operation with a select instruction as an operand, try to simplify the binop ...
static Constant * computePointerDifference(const DataLayout &DL, Value *LHS, Value *RHS)
Compute the constant difference between two pointer values.
static Value * simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
Test if a pair of compares with a shared operand and 2 constants has an empty set intersection,...
static Value * simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
static Value * simplifyICmpWithDominatingAssume(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
static Value * simplifyShift(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsNSW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Shl, LShr or AShr, see if we can fold the result.
static Constant * computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
static Value * simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
These are simplifications common to SRem and URem.
static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT)
Does the given value dominate the specified phi node?
static Value * simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a select instruction when its condition operand is an integer comparison.
static Value * foldMinimumMaximumSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1)
Given a min/max intrinsic, see if it can be removed based on having an operand that is another min/ma...
static Value * simplifyUnaryIntrinsic(Function *F, Value *Op0, const SimplifyQuery &Q, const CallBase *Call)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
This header provides classes for managing per-loop analyses.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define T
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
static unsigned getScalarSizeInBits(Type *Ty)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
Value * RHS
Value * LHS
BinaryOperator * Mul
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1033
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1512
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
void setSignBit()
Set the sign bit to 1.
Definition APInt.h:1340
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1111
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1249
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1639
bool isNonPositive() const
Determine if this APInt Value is non-positive (<= 0).
Definition APInt.h:361
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:356
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:475
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:471
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
bool isMask(unsigned numBits) const
Definition APInt.h:488
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1150
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1257
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:440
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:306
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
Definition APInt.h:341
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1130
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:389
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:239
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
an instruction to allocate memory on the stack
A container for analyses that lazily runs them and caches their results.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
const T & back() const
back - Get the last element.
Definition ArrayRef.h:156
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition ArrayRef.h:206
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:191
An immutable pass that tracks lazily created AssumptionCache objects.
AssumptionCache & getAssumptionCache(Function &F)
Get the cached assumptions for a function.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This class represents a function call, abstracting a target machine's calling convention.
static LLVM_ABI unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, Type *DstIntPtrTy)
Determine how a pair of casts can be eliminated, if they can be at all.
This class is the base class for the comparison instructions.
Definition InstrTypes.h:666
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition InstrTypes.h:984
Predicate getStrictPredicate() const
For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
Definition InstrTypes.h:860
bool isFalseWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:950
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:681
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:695
@ ICMP_SLT
signed less than
Definition InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:692
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:686
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:689
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:685
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:687
@ ICMP_NE
not equal
Definition InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:706
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:694
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:691
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:680
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:688
bool isSigned() const
Definition InstrTypes.h:932
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:829
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:944
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:772
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition InstrTypes.h:873
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:791
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:767
static LLVM_ABI bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:778
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:938
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI Constant * getBinOpAbsorber(unsigned Opcode, Type *Ty, bool AllowLHSConstant=false)
Return the absorbing element for the given binary operation, i.e.
static LLVM_ABI Constant * getNot(Constant *C)
static LLVM_ABI Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
Definition Constants.h:1387
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition Constants.h:1274
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
static LLVM_ABI Constant * getZero(Type *Ty, bool Negative=false)
static Constant * getNegativeZero(Type *Ty)
Definition Constants.h:315
static LLVM_ABI Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:131
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
This class represents a range of values.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
LLVM_ABI bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI ConstantRange inverse() const
Return a new range that is the logical not of the current set.
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
LLVM_ABI bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI bool isNaN() const
Return true if this is a floating-point NaN constant or a vector floating-point constant with all NaN...
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
unsigned getPointerSizeInBits(unsigned AS=0) const
The size in bits of the pointer representation in a given address space.
Definition DataLayout.h:388
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
unsigned getIndexSizeInBits(unsigned AS) const
The size in bits of indices used for address calculation in getelementptr and for addresses in the gi...
Definition DataLayout.h:396
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:669
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:322
DominatorTree & getDomTree()
Definition Dominators.h:330
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
bool noNaNs() const
Definition FMF.h:65
Represents calls to the gc.relocate intrinsic.
LLVM_ABI Value * getBasePtr() const
LLVM_ABI Value * getDerivedPtr() const
Represents flags for the getelementptr instruction/expression.
static LLVM_ABI Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
This instruction compares its operands according to the predicate given to the constructor.
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
LLVM_ABI bool hasNoSignedZeros() const LLVM_READONLY
Determine whether the no-signed-zeros flag is set.
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
LLVM_ABI bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
An instruction for reading from memory.
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Metadata node.
Definition Metadata.h:1077
static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits)
Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, so there is a certain thre...
static ICmpInst::Predicate getPredicate(Intrinsic::ID ID)
Returns the comparison predicate underlying the intrinsic.
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class represents a cast from a pointer to an integer.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:104
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:168
static void commuteShuffleMask(MutableArrayRef< int > Mask, unsigned InVecNumElts)
Change values in a shuffle permute mask assuming the two vector operands of length InVecNumElts have ...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:356
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetLibraryInfo & getTLI(const Function &F)
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:198
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
This class represents zero extension of integer types.
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, FCmpInst > m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI Value * simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:355
@ Offset
Definition DWP.cpp:477
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
LLVM_ABI Value * simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FMul, fold the result or return null.
LLVM_ABI Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
LLVM_ABI Value * simplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
LLVM_ABI Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
LLVM_ABI Value * simplifySDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for an SDiv, fold the result or return null.
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
LLVM_ABI Value * simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q)
Given operand for a UnaryOperator, fold the result or return null.
bool isDefaultFPEnvironment(fp::ExceptionBehavior EB, RoundingMode RM)
Returns true if the exception handling behavior and rounding mode match what is used in the default f...
Definition FPEnv.h:68
LLVM_ABI Value * simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Mul, fold the result or return null.
LLVM_ABI bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
LLVM_ABI Value * simplifyInstructionWithOperands(Instruction *I, ArrayRef< Value * > NewOps, const SimplifyQuery &Q)
Like simplifyInstruction but the operands of I are replaced with NewOps.
LLVM_ABI Value * simplifyCall(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
Given a callsite, callee, and arguments, fold the result or return null.
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
bool canRoundingModeBe(RoundingMode RM, RoundingMode QRM)
Returns true if the rounding mode RM may be QRM at compile time or at run time.
Definition FPEnv.h:80
LLVM_ABI bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
LLVM_ABI Value * simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, std::optional< ConstantRange > InRange, ArrayRef< Value * > Idxs)
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
LLVM_ABI Value * simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
LLVM_ABI Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
LLVM_ABI Value * simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
LLVM_ABI Value * simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Xor, fold the result or return null.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
LLVM_ABI Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
LLVM_ABI bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc,...
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
LLVM_ABI Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
unsigned M1(unsigned Val)
Definition VE.h:377
LLVM_ABI Value * simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
LLVM_ABI Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:1948
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1712
LLVM_ABI bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
LLVM_ABI bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
LLVM_ABI Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
LLVM_ABI bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
SelectPatternFlavor
Specific patterns of select instructions we can match.
LLVM_ABI Value * simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
LLVM_ABI Value * simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q)
Given operand for an FNeg, fold the result or return null.
LLVM_ABI Value * simplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FSub, fold the result or return null.
LLVM_ABI bool canReplacePointersIfEqual(const Value *From, const Value *To, const DataLayout &DL)
Returns true if a pointer value From can be replaced with another pointer value \To if they are deeme...
Definition Loads.cpp:853
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI Value * simplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FRem, fold the result or return null.
LLVM_ABI Value * simplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FAdd, fold the result or return null.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI Value * simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI Value * simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI Value * simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an InsertValueInst, fold the result or return null.
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI Value * simplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FDiv, fold the result or return null.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI Value * simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q)
Given a load instruction and its pointer operand, fold the result or return null.
LLVM_ABI Value * simplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for the multiplication of a FMA, fold the result or return null.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI Value * simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q)
Given a constrained FP intrinsic call, tries to compute its simplified version.
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
std::optional< DecomposedBitTest > decomposeBitTest(Value *Cond, bool LookThroughTrunc=true, bool AllowNonZeroC=false, bool DecomposeAnd=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
LLVM_ABI Value * findScalarElement(Value *V, unsigned EltNo)
Given a vector and an element number, see if the scalar value is already around as a register,...
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
LLVM_ABI Value * simplifyUDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for a UDiv, fold the result or return null.
DWARFExpression::Operation Op
LLVM_ABI bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
LLVM_ABI Value * simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType, Value *Op0, Value *Op1, const SimplifyQuery &Q, const CallBase *Call)
Given operands for a BinaryIntrinsic, fold the result or return null.
RoundingMode
Rounding mode.
@ NearestTiesToEven
roundTiesToEven.
@ TowardNegative
roundTowardNegative.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
unsigned M0(unsigned Val)
Definition VE.h:376
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI Value * simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
constexpr unsigned BitWidth
LLVM_ABI Value * simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const SimplifyQuery &Q, bool AllowRefinement, SmallVectorImpl< Instruction * > *DropFlags=nullptr)
See if V simplifies when its operand Op is replaced with RepOp.
LLVM_ABI bool maskIsAllZeroOrUndef(Value *Mask)
Given a mask vector of i1, Return true if all of the elements of this predicate mask are known to be ...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
LLVM_ABI Value * simplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SRem, fold the result or return null.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1877
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2088
LLVM_ABI Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
@ Continue
Definition DWP.h:22
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
std::pair< Value *, FPClassTest > fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Returns a pair of values, which if passed to llvm.is.fpclass, returns the same result as an fcmp with...
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
bool isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1, bool IsAnd, Use *&Y)
Match one of the patterns up to the select/logic op: Op0 = icmp ne i4 X, 0 Agg = call { i4,...
bool canIgnoreSNaN(fp::ExceptionBehavior EB, FastMathFlags FMF)
Returns true if the possibility of a signaling NaN can be safely ignored.
Definition FPEnv.h:86
LLVM_ABI Value * simplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a URem, fold the result or return null.
LLVM_ABI Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
LLVM_ABI Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:836
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
#define N
This callback is used in conjunction with PointerMayBeCaptured.
virtual Action captured(const Use *U, UseCaptureInfo CI)=0
Use U directly captures CI.UseCC and additionally CI.ResultCC through the return value of the user of...
virtual void tooManyUses()=0
tooManyUses - The depth of traversal has breached a limit.
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:101
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:235
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:267
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
unsigned countMaxActiveBits() const
Returns the maximum number of bits needed to represent all possible unsigned values with these known ...
Definition KnownBits.h:289
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:241
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:138
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:122
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:98
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
bool isKnownAlwaysNaN() const
Return true if it's known this must always be a nan.
static constexpr FPClassTest OrderedLessThanZeroMask
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
Various options to control the behavior of getObjectSize.
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
Mode EvalMode
How we want to evaluate this object's size.
@ Min
Evaluate all branches of an unknown condition.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
const Instruction * CxtI
bool CanUseUndef
Controls whether simplifications are allowed to constrain the range of possible values for uses of un...
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
LLVM_ABI bool isUndefValue(Value *V) const
If CanUseUndef is true, returns whether V is undef.
AssumptionCache * AC
const TargetLibraryInfo * TLI
SimplifyQuery getWithoutUndef() const
const InstrInfoQuery IIQ
Capture information for a specific Use.