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