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