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