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 can only fold certain predicates on pointer comparisons.
2690 switch (Pred) {
2691 default:
2692 return nullptr;
2693
2694 // Equality comparisons are easy to fold.
2695 case CmpInst::ICMP_EQ:
2696 case CmpInst::ICMP_NE:
2697 break;
2698
2699 // We can only handle unsigned relational comparisons because 'inbounds' on
2700 // a GEP only protects against unsigned wrapping.
2701 case CmpInst::ICMP_UGT:
2702 case CmpInst::ICMP_UGE:
2703 case CmpInst::ICMP_ULT:
2704 case CmpInst::ICMP_ULE:
2705 // However, we have to switch them to their signed variants to handle
2706 // negative indices from the base pointer.
2707 Pred = ICmpInst::getSignedPredicate(Pred);
2708 break;
2709 }
2710
2711 // Strip off any constant offsets so that we can reason about them.
2712 // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2713 // here and compare base addresses like AliasAnalysis does, however there are
2714 // numerous hazards. AliasAnalysis and its utilities rely on special rules
2715 // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2716 // doesn't need to guarantee pointer inequality when it says NoAlias.
2717
2718 // Even if an non-inbounds GEP occurs along the path we can still optimize
2719 // equality comparisons concerning the result.
2720 bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2721 unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
2722 APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2723 LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
2724 RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);
2725
2726 // If LHS and RHS are related via constant offsets to the same base
2727 // value, we can replace it with an icmp which just compares the offsets.
2728 if (LHS == RHS)
2729 return ConstantInt::get(getCompareTy(LHS),
2730 ICmpInst::compare(LHSOffset, RHSOffset, Pred));
2731
2732 // Various optimizations for (in)equality comparisons.
2733 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
2734 // Different non-empty allocations that exist at the same time have
2735 // different addresses (if the program can tell). If the offsets are
2736 // within the bounds of their allocations (and not one-past-the-end!
2737 // so we can't use inbounds!), and their allocations aren't the same,
2738 // the pointers are not equal.
2740 uint64_t LHSSize, RHSSize;
2741 ObjectSizeOpts Opts;
2742 Opts.EvalMode = ObjectSizeOpts::Mode::Min;
2743 auto *F = [](Value *V) -> Function * {
2744 if (auto *I = dyn_cast<Instruction>(V))
2745 return I->getFunction();
2746 if (auto *A = dyn_cast<Argument>(V))
2747 return A->getParent();
2748 return nullptr;
2749 }(LHS);
2750 Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
2751 if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && LHSSize != 0 &&
2752 getObjectSize(RHS, RHSSize, DL, TLI, Opts) && RHSSize != 0) {
2753 APInt Dist = LHSOffset - RHSOffset;
2754 if (Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize))
2755 return ConstantInt::get(getCompareTy(LHS),
2757 }
2758 }
2759
2760 // If one side of the equality comparison must come from a noalias call
2761 // (meaning a system memory allocation function), and the other side must
2762 // come from a pointer that cannot overlap with dynamically-allocated
2763 // memory within the lifetime of the current function (allocas, byval
2764 // arguments, globals), then determine the comparison result here.
2765 SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2766 getUnderlyingObjects(LHS, LHSUObjs);
2767 getUnderlyingObjects(RHS, RHSUObjs);
2768
2769 // Is the set of underlying objects all noalias calls?
2770 auto IsNAC = [](ArrayRef<const Value *> Objects) {
2771 return all_of(Objects, isNoAliasCall);
2772 };
2773
2774 // Is the set of underlying objects all things which must be disjoint from
2775 // noalias calls. We assume that indexing from such disjoint storage
2776 // into the heap is undefined, and thus offsets can be safely ignored.
2777 auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2778 return all_of(Objects, ::isAllocDisjoint);
2779 };
2780
2781 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2782 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2783 return ConstantInt::get(getCompareTy(LHS),
2785
2786 // Fold comparisons for non-escaping pointer even if the allocation call
2787 // cannot be elided. We cannot fold malloc comparison to null. Also, the
2788 // dynamic allocation call could be either of the operands. Note that
2789 // the other operand can not be based on the alloc - if it were, then
2790 // the cmp itself would be a capture.
2791 Value *MI = nullptr;
2792 if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonZero(RHS, Q))
2793 MI = LHS;
2794 else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonZero(LHS, Q))
2795 MI = RHS;
2796 if (MI) {
2797 // FIXME: This is incorrect, see PR54002. While we can assume that the
2798 // allocation is at an address that makes the comparison false, this
2799 // requires that *all* comparisons to that address be false, which
2800 // InstSimplify cannot guarantee.
2801 struct CustomCaptureTracker : public CaptureTracker {
2802 bool Captured = false;
2803 void tooManyUses() override { Captured = true; }
2804 bool captured(const Use *U) override {
2805 if (auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2806 // Comparison against value stored in global variable. Given the
2807 // pointer does not escape, its value cannot be guessed and stored
2808 // separately in a global variable.
2809 unsigned OtherIdx = 1 - U->getOperandNo();
2810 auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2811 if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2812 return false;
2813 }
2814
2815 Captured = true;
2816 return true;
2817 }
2818 };
2819 CustomCaptureTracker Tracker;
2820 PointerMayBeCaptured(MI, &Tracker);
2821 if (!Tracker.Captured)
2822 return ConstantInt::get(getCompareTy(LHS),
2824 }
2825 }
2826
2827 // Otherwise, fail.
2828 return nullptr;
2829}
2830
2831/// Fold an icmp when its operands have i1 scalar type.
2833 const SimplifyQuery &Q) {
2834 Type *ITy = getCompareTy(LHS); // The return type.
2835 Type *OpTy = LHS->getType(); // The operand type.
2836 if (!OpTy->isIntOrIntVectorTy(1))
2837 return nullptr;
2838
2839 // A boolean compared to true/false can be reduced in 14 out of the 20
2840 // (10 predicates * 2 constants) possible combinations. The other
2841 // 6 cases require a 'not' of the LHS.
2842
2843 auto ExtractNotLHS = [](Value *V) -> Value * {
2844 Value *X;
2845 if (match(V, m_Not(m_Value(X))))
2846 return X;
2847 return nullptr;
2848 };
2849
2850 if (match(RHS, m_Zero())) {
2851 switch (Pred) {
2852 case CmpInst::ICMP_NE: // X != 0 -> X
2853 case CmpInst::ICMP_UGT: // X >u 0 -> X
2854 case CmpInst::ICMP_SLT: // X <s 0 -> X
2855 return LHS;
2856
2857 case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
2858 case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2859 case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2860 if (Value *X = ExtractNotLHS(LHS))
2861 return X;
2862 break;
2863
2864 case CmpInst::ICMP_ULT: // X <u 0 -> false
2865 case CmpInst::ICMP_SGT: // X >s 0 -> false
2866 return getFalse(ITy);
2867
2868 case CmpInst::ICMP_UGE: // X >=u 0 -> true
2869 case CmpInst::ICMP_SLE: // X <=s 0 -> true
2870 return getTrue(ITy);
2871
2872 default:
2873 break;
2874 }
2875 } else if (match(RHS, m_One())) {
2876 switch (Pred) {
2877 case CmpInst::ICMP_EQ: // X == 1 -> X
2878 case CmpInst::ICMP_UGE: // X >=u 1 -> X
2879 case CmpInst::ICMP_SLE: // X <=s -1 -> X
2880 return LHS;
2881
2882 case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
2883 case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
2884 case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
2885 if (Value *X = ExtractNotLHS(LHS))
2886 return X;
2887 break;
2888
2889 case CmpInst::ICMP_UGT: // X >u 1 -> false
2890 case CmpInst::ICMP_SLT: // X <s -1 -> false
2891 return getFalse(ITy);
2892
2893 case CmpInst::ICMP_ULE: // X <=u 1 -> true
2894 case CmpInst::ICMP_SGE: // X >=s -1 -> true
2895 return getTrue(ITy);
2896
2897 default:
2898 break;
2899 }
2900 }
2901
2902 switch (Pred) {
2903 default:
2904 break;
2905 case ICmpInst::ICMP_UGE:
2906 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2907 return getTrue(ITy);
2908 break;
2909 case ICmpInst::ICMP_SGE:
2910 /// For signed comparison, the values for an i1 are 0 and -1
2911 /// respectively. This maps into a truth table of:
2912 /// LHS | RHS | LHS >=s RHS | LHS implies RHS
2913 /// 0 | 0 | 1 (0 >= 0) | 1
2914 /// 0 | 1 | 1 (0 >= -1) | 1
2915 /// 1 | 0 | 0 (-1 >= 0) | 0
2916 /// 1 | 1 | 1 (-1 >= -1) | 1
2917 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2918 return getTrue(ITy);
2919 break;
2920 case ICmpInst::ICMP_ULE:
2921 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2922 return getTrue(ITy);
2923 break;
2924 case ICmpInst::ICMP_SLE:
2925 /// SLE follows the same logic as SGE with the LHS and RHS swapped.
2926 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2927 return getTrue(ITy);
2928 break;
2929 }
2930
2931 return nullptr;
2932}
2933
2934/// Try hard to fold icmp with zero RHS because this is a common case.
2936 const SimplifyQuery &Q) {
2937 if (!match(RHS, m_Zero()))
2938 return nullptr;
2939
2940 Type *ITy = getCompareTy(LHS); // The return type.
2941 switch (Pred) {
2942 default:
2943 llvm_unreachable("Unknown ICmp predicate!");
2944 case ICmpInst::ICMP_ULT:
2945 return getFalse(ITy);
2946 case ICmpInst::ICMP_UGE:
2947 return getTrue(ITy);
2948 case ICmpInst::ICMP_EQ:
2949 case ICmpInst::ICMP_ULE:
2950 if (isKnownNonZero(LHS, Q))
2951 return getFalse(ITy);
2952 break;
2953 case ICmpInst::ICMP_NE:
2954 case ICmpInst::ICMP_UGT:
2955 if (isKnownNonZero(LHS, Q))
2956 return getTrue(ITy);
2957 break;
2958 case ICmpInst::ICMP_SLT: {
2959 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2960 if (LHSKnown.isNegative())
2961 return getTrue(ITy);
2962 if (LHSKnown.isNonNegative())
2963 return getFalse(ITy);
2964 break;
2965 }
2966 case ICmpInst::ICMP_SLE: {
2967 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2968 if (LHSKnown.isNegative())
2969 return getTrue(ITy);
2970 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2971 return getFalse(ITy);
2972 break;
2973 }
2974 case ICmpInst::ICMP_SGE: {
2975 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2976 if (LHSKnown.isNegative())
2977 return getFalse(ITy);
2978 if (LHSKnown.isNonNegative())
2979 return getTrue(ITy);
2980 break;
2981 }
2982 case ICmpInst::ICMP_SGT: {
2983 KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2984 if (LHSKnown.isNegative())
2985 return getFalse(ITy);
2986 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2987 return getTrue(ITy);
2988 break;
2989 }
2990 }
2991
2992 return nullptr;
2993}
2994
2996 Value *RHS, const InstrInfoQuery &IIQ) {
2997 Type *ITy = getCompareTy(RHS); // The return type.
2998
2999 Value *X;
3000 const APInt *C;
3001 if (!match(RHS, m_APIntAllowPoison(C)))
3002 return nullptr;
3003
3004 // Sign-bit checks can be optimized to true/false after unsigned
3005 // floating-point casts:
3006 // icmp slt (bitcast (uitofp X)), 0 --> false
3007 // icmp sgt (bitcast (uitofp X)), -1 --> true
3009 bool TrueIfSigned;
3010 if (isSignBitCheck(Pred, *C, TrueIfSigned))
3011 return ConstantInt::getBool(ITy, !TrueIfSigned);
3012 }
3013
3014 // Rule out tautological comparisons (eg., ult 0 or uge 0).
3016 if (RHS_CR.isEmptySet())
3017 return ConstantInt::getFalse(ITy);
3018 if (RHS_CR.isFullSet())
3019 return ConstantInt::getTrue(ITy);
3020
3021 ConstantRange LHS_CR =
3023 if (!LHS_CR.isFullSet()) {
3024 if (RHS_CR.contains(LHS_CR))
3025 return ConstantInt::getTrue(ITy);
3026 if (RHS_CR.inverse().contains(LHS_CR))
3027 return ConstantInt::getFalse(ITy);
3028 }
3029
3030 // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
3031 // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
3032 const APInt *MulC;
3033 if (IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
3035 *MulC != 0 && C->urem(*MulC) != 0) ||
3037 *MulC != 0 && C->srem(*MulC) != 0)))
3038 return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
3039
3040 return nullptr;
3041}
3042
3044
3045/// Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq).
3047 MonotonicType Type, unsigned Depth = 0) {
3048 if (!Res.insert(V).second)
3049 return;
3050
3051 // Can be increased if useful.
3052 if (++Depth > 1)
3053 return;
3054
3055 auto *I = dyn_cast<Instruction>(V);
3056 if (!I)
3057 return;
3058
3059 Value *X, *Y;
3061 if (match(I, m_Or(m_Value(X), m_Value(Y))) ||
3062 match(I, m_Intrinsic<Intrinsic::uadd_sat>(m_Value(X), m_Value(Y)))) {
3065 }
3066 } else {
3068 switch (I->getOpcode()) {
3069 case Instruction::And:
3070 getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Depth);
3071 getUnsignedMonotonicValues(Res, I->getOperand(1), Type, Depth);
3072 break;
3073 case Instruction::URem:
3074 case Instruction::UDiv:
3075 case Instruction::LShr:
3076 getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Depth);
3077 break;
3078 case Instruction::Call:
3079 if (match(I, m_Intrinsic<Intrinsic::usub_sat>(m_Value(X))))
3081 break;
3082 default:
3083 break;
3084 }
3085 }
3086}
3087
3089 Value *RHS) {
3090 if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_ULT)
3091 return nullptr;
3092
3093 // We have LHS uge GreaterValues and LowerValues uge RHS. If any of the
3094 // GreaterValues and LowerValues are the same, it follows that LHS uge RHS.
3095 SmallPtrSet<Value *, 4> GreaterValues;
3096 SmallPtrSet<Value *, 4> LowerValues;
3099 for (Value *GV : GreaterValues)
3100 if (LowerValues.contains(GV))
3102 Pred == ICmpInst::ICMP_UGE);
3103 return nullptr;
3104}
3105
3107 Value *RHS, const SimplifyQuery &Q,
3108 unsigned MaxRecurse) {
3109 Type *ITy = getCompareTy(RHS); // The return type.
3110
3111 Value *Y = nullptr;
3112 // icmp pred (or X, Y), X
3113 if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
3114 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3115 KnownBits RHSKnown = computeKnownBits(RHS, /* Depth */ 0, Q);
3116 KnownBits YKnown = computeKnownBits(Y, /* Depth */ 0, Q);
3117 if (RHSKnown.isNonNegative() && YKnown.isNegative())
3118 return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
3119 if (RHSKnown.isNegative() || YKnown.isNonNegative())
3120 return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
3121 }
3122 }
3123
3124 // icmp pred (urem X, Y), Y
3125 if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
3126 switch (Pred) {
3127 default:
3128 break;
3129 case ICmpInst::ICMP_SGT:
3130 case ICmpInst::ICMP_SGE: {
3131 KnownBits Known = computeKnownBits(RHS, /* Depth */ 0, Q);
3132 if (!Known.isNonNegative())
3133 break;
3134 [[fallthrough]];
3135 }
3136 case ICmpInst::ICMP_EQ:
3137 case ICmpInst::ICMP_UGT:
3138 case ICmpInst::ICMP_UGE:
3139 return getFalse(ITy);
3140 case ICmpInst::ICMP_SLT:
3141 case ICmpInst::ICMP_SLE: {
3142 KnownBits Known = computeKnownBits(RHS, /* Depth */ 0, Q);
3143 if (!Known.isNonNegative())
3144 break;
3145 [[fallthrough]];
3146 }
3147 case ICmpInst::ICMP_NE:
3148 case ICmpInst::ICMP_ULT:
3149 case ICmpInst::ICMP_ULE:
3150 return getTrue(ITy);
3151 }
3152 }
3153
3154 // If x is nonzero:
3155 // x >>u C <u x --> true for C != 0.
3156 // x >>u C != x --> true for C != 0.
3157 // x >>u C >=u x --> false for C != 0.
3158 // x >>u C == x --> false for C != 0.
3159 // x udiv C <u x --> true for C != 1.
3160 // x udiv C != x --> true for C != 1.
3161 // x udiv C >=u x --> false for C != 1.
3162 // x udiv C == x --> false for C != 1.
3163 // TODO: allow non-constant shift amount/divisor
3164 const APInt *C;
3165 if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 0) ||
3166 (match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C))) && *C != 1)) {
3167 if (isKnownNonZero(RHS, Q)) {
3168 switch (Pred) {
3169 default:
3170 break;
3171 case ICmpInst::ICMP_EQ:
3172 case ICmpInst::ICMP_UGE:
3173 case ICmpInst::ICMP_UGT:
3174 return getFalse(ITy);
3175 case ICmpInst::ICMP_NE:
3176 case ICmpInst::ICMP_ULT:
3177 case ICmpInst::ICMP_ULE:
3178 return getTrue(ITy);
3179 }
3180 }
3181 }
3182
3183 // (x*C1)/C2 <= x for C1 <= C2.
3184 // This holds even if the multiplication overflows: Assume that x != 0 and
3185 // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
3186 // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3187 //
3188 // Additionally, either the multiplication and division might be represented
3189 // as shifts:
3190 // (x*C1)>>C2 <= x for C1 < 2**C2.
3191 // (x<<C1)/C2 <= x for 2**C1 < C2.
3192 const APInt *C1, *C2;
3193 if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3194 C1->ule(*C2)) ||
3195 (match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3196 C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
3197 (match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3198 (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
3199 if (Pred == ICmpInst::ICMP_UGT)
3200 return getFalse(ITy);
3201 if (Pred == ICmpInst::ICMP_ULE)
3202 return getTrue(ITy);
3203 }
3204
3205 // (sub C, X) == X, C is odd --> false
3206 // (sub C, X) != X, C is odd --> true
3207 if (match(LBO, m_Sub(m_APIntAllowPoison(C), m_Specific(RHS))) &&
3208 (*C & 1) == 1 && ICmpInst::isEquality(Pred))
3209 return (Pred == ICmpInst::ICMP_EQ) ? getFalse(ITy) : getTrue(ITy);
3210
3211 return nullptr;
3212}
3213
3214// If only one of the icmp's operands has NSW flags, try to prove that:
3215//
3216// icmp slt (x + C1), (x +nsw C2)
3217//
3218// is equivalent to:
3219//
3220// icmp slt C1, C2
3221//
3222// which is true if x + C2 has the NSW flags set and:
3223// *) C1 < C2 && C1 >= 0, or
3224// *) C2 < C1 && C1 <= 0.
3225//
3227 const InstrInfoQuery &IIQ) {
3228 // TODO: only support icmp slt for now.
3229 if (Pred != CmpInst::ICMP_SLT || !IIQ.UseInstrInfo)
3230 return false;
3231
3232 // Canonicalize nsw add as RHS.
3233 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3234 std::swap(LHS, RHS);
3235 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3236 return false;
3237
3238 Value *X;
3239 const APInt *C1, *C2;
3240 if (!match(LHS, m_Add(m_Value(X), m_APInt(C1))) ||
3241 !match(RHS, m_Add(m_Specific(X), m_APInt(C2))))
3242 return false;
3243
3244 return (C1->slt(*C2) && C1->isNonNegative()) ||
3245 (C2->slt(*C1) && C1->isNonPositive());
3246}
3247
3248/// TODO: A large part of this logic is duplicated in InstCombine's
3249/// foldICmpBinOp(). We should be able to share that and avoid the code
3250/// duplication.
3252 const SimplifyQuery &Q,
3253 unsigned MaxRecurse) {
3254 BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
3255 BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
3256 if (MaxRecurse && (LBO || RBO)) {
3257 // Analyze the case when either LHS or RHS is an add instruction.
3258 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
3259 // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
3260 bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
3261 if (LBO && LBO->getOpcode() == Instruction::Add) {
3262 A = LBO->getOperand(0);
3263 B = LBO->getOperand(1);
3264 NoLHSWrapProblem =
3265 ICmpInst::isEquality(Pred) ||
3266 (CmpInst::isUnsigned(Pred) &&
3267 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))) ||
3268 (CmpInst::isSigned(Pred) &&
3269 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)));
3270 }
3271 if (RBO && RBO->getOpcode() == Instruction::Add) {
3272 C = RBO->getOperand(0);
3273 D = RBO->getOperand(1);
3274 NoRHSWrapProblem =
3275 ICmpInst::isEquality(Pred) ||
3276 (CmpInst::isUnsigned(Pred) &&
3277 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))) ||
3278 (CmpInst::isSigned(Pred) &&
3279 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO)));
3280 }
3281
3282 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3283 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
3284 if (Value *V = simplifyICmpInst(Pred, A == RHS ? B : A,
3286 MaxRecurse - 1))
3287 return V;
3288
3289 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3290 if ((C == LHS || D == LHS) && NoRHSWrapProblem)
3291 if (Value *V =
3293 C == LHS ? D : C, Q, MaxRecurse - 1))
3294 return V;
3295
3296 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3297 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3299 if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
3300 // Determine Y and Z in the form icmp (X+Y), (X+Z).
3301 Value *Y, *Z;
3302 if (A == C) {
3303 // C + B == C + D -> B == D
3304 Y = B;
3305 Z = D;
3306 } else if (A == D) {
3307 // D + B == C + D -> B == C
3308 Y = B;
3309 Z = C;
3310 } else if (B == C) {
3311 // A + C == C + D -> A == D
3312 Y = A;
3313 Z = D;
3314 } else {
3315 assert(B == D);
3316 // A + D == C + D -> A == C
3317 Y = A;
3318 Z = C;
3319 }
3320 if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
3321 return V;
3322 }
3323 }
3324
3325 if (LBO)
3326 if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
3327 return V;
3328
3329 if (RBO)
3331 ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse))
3332 return V;
3333
3334 // 0 - (zext X) pred C
3335 if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
3336 const APInt *C;
3337 if (match(RHS, m_APInt(C))) {
3338 if (C->isStrictlyPositive()) {
3339 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3341 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3343 }
3344 if (C->isNonNegative()) {
3345 if (Pred == ICmpInst::ICMP_SLE)
3347 if (Pred == ICmpInst::ICMP_SGT)
3349 }
3350 }
3351 }
3352
3353 // If C2 is a power-of-2 and C is not:
3354 // (C2 << X) == C --> false
3355 // (C2 << X) != C --> true
3356 const APInt *C;
3357 if (match(LHS, m_Shl(m_Power2(), m_Value())) &&
3358 match(RHS, m_APIntAllowPoison(C)) && !C->isPowerOf2()) {
3359 // C2 << X can equal zero in some circumstances.
3360 // This simplification might be unsafe if C is zero.
3361 //
3362 // We know it is safe if:
3363 // - The shift is nsw. We can't shift out the one bit.
3364 // - The shift is nuw. We can't shift out the one bit.
3365 // - C2 is one.
3366 // - C isn't zero.
3367 if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3368 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3369 match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
3370 if (Pred == ICmpInst::ICMP_EQ)
3372 if (Pred == ICmpInst::ICMP_NE)
3374 }
3375 }
3376
3377 // If C is a power-of-2:
3378 // (C << X) >u 0x8000 --> false
3379 // (C << X) <=u 0x8000 --> true
3380 if (match(LHS, m_Shl(m_Power2(), m_Value())) && match(RHS, m_SignMask())) {
3381 if (Pred == ICmpInst::ICMP_UGT)
3383 if (Pred == ICmpInst::ICMP_ULE)
3385 }
3386
3387 if (!MaxRecurse || !LBO || !RBO || LBO->getOpcode() != RBO->getOpcode())
3388 return nullptr;
3389
3390 if (LBO->getOperand(0) == RBO->getOperand(0)) {
3391 switch (LBO->getOpcode()) {
3392 default:
3393 break;
3394 case Instruction::Shl: {
3395 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3396 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3397 if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3398 !isKnownNonZero(LBO->getOperand(0), Q))
3399 break;
3400 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1),
3401 RBO->getOperand(1), Q, MaxRecurse - 1))
3402 return V;
3403 break;
3404 }
3405 // If C1 & C2 == C1, A = X and/or C1, B = X and/or C2:
3406 // icmp ule A, B -> true
3407 // icmp ugt A, B -> false
3408 // icmp sle A, B -> true (C1 and C2 are the same sign)
3409 // icmp sgt A, B -> false (C1 and C2 are the same sign)
3410 case Instruction::And:
3411 case Instruction::Or: {
3412 const APInt *C1, *C2;
3413 if (ICmpInst::isRelational(Pred) &&
3414 match(LBO->getOperand(1), m_APInt(C1)) &&
3415 match(RBO->getOperand(1), m_APInt(C2))) {
3416 if (!C1->isSubsetOf(*C2)) {
3417 std::swap(C1, C2);
3418 Pred = ICmpInst::getSwappedPredicate(Pred);
3419 }
3420 if (C1->isSubsetOf(*C2)) {
3421 if (Pred == ICmpInst::ICMP_ULE)
3423 if (Pred == ICmpInst::ICMP_UGT)
3425 if (C1->isNonNegative() == C2->isNonNegative()) {
3426 if (Pred == ICmpInst::ICMP_SLE)
3428 if (Pred == ICmpInst::ICMP_SGT)
3430 }
3431 }
3432 }
3433 break;
3434 }
3435 }
3436 }
3437
3438 if (LBO->getOperand(1) == RBO->getOperand(1)) {
3439 switch (LBO->getOpcode()) {
3440 default:
3441 break;
3442 case Instruction::UDiv:
3443 case Instruction::LShr:
3444 if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO) ||
3445 !Q.IIQ.isExact(RBO))
3446 break;
3447 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3448 RBO->getOperand(0), Q, MaxRecurse - 1))
3449 return V;
3450 break;
3451 case Instruction::SDiv:
3452 if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) ||
3453 !Q.IIQ.isExact(RBO))
3454 break;
3455 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3456 RBO->getOperand(0), Q, MaxRecurse - 1))
3457 return V;
3458 break;
3459 case Instruction::AShr:
3460 if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO))
3461 break;
3462 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3463 RBO->getOperand(0), Q, MaxRecurse - 1))
3464 return V;
3465 break;
3466 case Instruction::Shl: {
3467 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3468 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3469 if (!NUW && !NSW)
3470 break;
3471 if (!NSW && ICmpInst::isSigned(Pred))
3472 break;
3473 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3474 RBO->getOperand(0), Q, MaxRecurse - 1))
3475 return V;
3476 break;
3477 }
3478 }
3479 }
3480 return nullptr;
3481}
3482
3483/// simplify integer comparisons where at least one operand of the compare
3484/// matches an integer min/max idiom.
3486 const SimplifyQuery &Q,
3487 unsigned MaxRecurse) {
3488 Type *ITy = getCompareTy(LHS); // The return type.
3489 Value *A, *B;
3491 CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
3492
3493 // Signed variants on "max(a,b)>=a -> true".
3494 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3495 if (A != RHS)
3496 std::swap(A, B); // smax(A, B) pred A.
3497 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3498 // We analyze this as smax(A, B) pred A.
3499 P = Pred;
3500 } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
3501 (A == LHS || B == LHS)) {
3502 if (A != LHS)
3503 std::swap(A, B); // A pred smax(A, B).
3504 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3505 // We analyze this as smax(A, B) swapped-pred A.
3507 } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
3508 (A == RHS || B == RHS)) {
3509 if (A != RHS)
3510 std::swap(A, B); // smin(A, B) pred A.
3511 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3512 // We analyze this as smax(-A, -B) swapped-pred -A.
3513 // Note that we do not need to actually form -A or -B thanks to EqP.
3515 } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
3516 (A == LHS || B == LHS)) {
3517 if (A != LHS)
3518 std::swap(A, B); // A pred smin(A, B).
3519 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3520 // We analyze this as smax(-A, -B) pred -A.
3521 // Note that we do not need to actually form -A or -B thanks to EqP.
3522 P = Pred;
3523 }
3525 // Cases correspond to "max(A, B) p A".
3526 switch (P) {
3527 default:
3528 break;
3529 case CmpInst::ICMP_EQ:
3530 case CmpInst::ICMP_SLE:
3531 // Equivalent to "A EqP B". This may be the same as the condition tested
3532 // in the max/min; if so, we can just return that.
3533 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3534 return V;
3535 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3536 return V;
3537 // Otherwise, see if "A EqP B" simplifies.
3538 if (MaxRecurse)
3539 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3540 return V;
3541 break;
3542 case CmpInst::ICMP_NE:
3543 case CmpInst::ICMP_SGT: {
3545 // Equivalent to "A InvEqP B". This may be the same as the condition
3546 // tested in the max/min; if so, we can just return that.
3547 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3548 return V;
3549 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3550 return V;
3551 // Otherwise, see if "A InvEqP B" simplifies.
3552 if (MaxRecurse)
3553 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3554 return V;
3555 break;
3556 }
3557 case CmpInst::ICMP_SGE:
3558 // Always true.
3559 return getTrue(ITy);
3560 case CmpInst::ICMP_SLT:
3561 // Always false.
3562 return getFalse(ITy);
3563 }
3564 }
3565
3566 // Unsigned variants on "max(a,b)>=a -> true".
3568 if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3569 if (A != RHS)
3570 std::swap(A, B); // umax(A, B) pred A.
3571 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3572 // We analyze this as umax(A, B) pred A.
3573 P = Pred;
3574 } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
3575 (A == LHS || B == LHS)) {
3576 if (A != LHS)
3577 std::swap(A, B); // A pred umax(A, B).
3578 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3579 // We analyze this as umax(A, B) swapped-pred A.
3581 } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3582 (A == RHS || B == RHS)) {
3583 if (A != RHS)
3584 std::swap(A, B); // umin(A, B) pred A.
3585 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3586 // We analyze this as umax(-A, -B) swapped-pred -A.
3587 // Note that we do not need to actually form -A or -B thanks to EqP.
3589 } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
3590 (A == LHS || B == LHS)) {
3591 if (A != LHS)
3592 std::swap(A, B); // A pred umin(A, B).
3593 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3594 // We analyze this as umax(-A, -B) pred -A.
3595 // Note that we do not need to actually form -A or -B thanks to EqP.
3596 P = Pred;
3597 }
3599 // Cases correspond to "max(A, B) p A".
3600 switch (P) {
3601 default:
3602 break;
3603 case CmpInst::ICMP_EQ:
3604 case CmpInst::ICMP_ULE:
3605 // Equivalent to "A EqP B". This may be the same as the condition tested
3606 // in the max/min; if so, we can just return that.
3607 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3608 return V;
3609 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3610 return V;
3611 // Otherwise, see if "A EqP B" simplifies.
3612 if (MaxRecurse)
3613 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3614 return V;
3615 break;
3616 case CmpInst::ICMP_NE:
3617 case CmpInst::ICMP_UGT: {
3619 // Equivalent to "A InvEqP B". This may be the same as the condition
3620 // tested in the max/min; if so, we can just return that.
3621 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3622 return V;
3623 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3624 return V;
3625 // Otherwise, see if "A InvEqP B" simplifies.
3626 if (MaxRecurse)
3627 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3628 return V;
3629 break;
3630 }
3631 case CmpInst::ICMP_UGE:
3632 return getTrue(ITy);
3633 case CmpInst::ICMP_ULT:
3634 return getFalse(ITy);
3635 }
3636 }
3637
3638 // Comparing 1 each of min/max with a common operand?
3639 // Canonicalize min operand to RHS.
3640 if (match(LHS, m_UMin(m_Value(), m_Value())) ||
3641 match(LHS, m_SMin(m_Value(), m_Value()))) {
3642 std::swap(LHS, RHS);
3643 Pred = ICmpInst::getSwappedPredicate(Pred);
3644 }
3645
3646 Value *C, *D;
3647 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
3648 match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
3649 (A == C || A == D || B == C || B == D)) {
3650 // smax(A, B) >=s smin(A, D) --> true
3651 if (Pred == CmpInst::ICMP_SGE)
3652 return getTrue(ITy);
3653 // smax(A, B) <s smin(A, D) --> false
3654 if (Pred == CmpInst::ICMP_SLT)
3655 return getFalse(ITy);
3656 } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3657 match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
3658 (A == C || A == D || B == C || B == D)) {
3659 // umax(A, B) >=u umin(A, D) --> true
3660 if (Pred == CmpInst::ICMP_UGE)
3661 return getTrue(ITy);
3662 // umax(A, B) <u umin(A, D) --> false
3663 if (Pred == CmpInst::ICMP_ULT)
3664 return getFalse(ITy);
3665 }
3666
3667 return nullptr;
3668}
3669
3671 Value *LHS, Value *RHS,
3672 const SimplifyQuery &Q) {
3673 // Gracefully handle instructions that have not been inserted yet.
3674 if (!Q.AC || !Q.CxtI)
3675 return nullptr;
3676
3677 for (Value *AssumeBaseOp : {LHS, RHS}) {
3678 for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
3679 if (!AssumeVH)
3680 continue;
3681
3682 CallInst *Assume = cast<CallInst>(AssumeVH);
3683 if (std::optional<bool> Imp = isImpliedCondition(
3684 Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL))
3685 if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT))
3686 return ConstantInt::get(getCompareTy(LHS), *Imp);
3687 }
3688 }
3689
3690 return nullptr;
3691}
3692
3694 Value *RHS) {
3695 auto *II = dyn_cast<IntrinsicInst>(LHS);
3696 if (!II)
3697 return nullptr;
3698
3699 switch (II->getIntrinsicID()) {
3700 case Intrinsic::uadd_sat:
3701 // uadd.sat(X, Y) uge X + Y
3702 if (match(RHS, m_c_Add(m_Specific(II->getArgOperand(0)),
3703 m_Specific(II->getArgOperand(1))))) {
3704 if (Pred == ICmpInst::ICMP_UGE)
3706 if (Pred == ICmpInst::ICMP_ULT)
3708 }
3709 return nullptr;
3710 case Intrinsic::usub_sat:
3711 // usub.sat(X, Y) ule X - Y
3712 if (match(RHS, m_Sub(m_Specific(II->getArgOperand(0)),
3713 m_Specific(II->getArgOperand(1))))) {
3714 if (Pred == ICmpInst::ICMP_ULE)
3716 if (Pred == ICmpInst::ICMP_UGT)
3718 }
3719 return nullptr;
3720 default:
3721 return nullptr;
3722 }
3723}
3724
3725/// Helper method to get range from metadata or attribute.
3726static std::optional<ConstantRange> getRange(Value *V,
3727 const InstrInfoQuery &IIQ) {
3728 if (Instruction *I = dyn_cast<Instruction>(V))
3729 if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
3730 return getConstantRangeFromMetadata(*MD);
3731
3732 if (const Argument *A = dyn_cast<Argument>(V))
3733 return A->getRange();
3734 else if (const CallBase *CB = dyn_cast<CallBase>(V))
3735 return CB->getRange();
3736
3737 return std::nullopt;
3738}
3739
3740/// Given operands for an ICmpInst, see if we can fold the result.
3741/// If not, this returns null.
3743 const SimplifyQuery &Q, unsigned MaxRecurse) {
3744 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3745
3746 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3747 if (Constant *CRHS = dyn_cast<Constant>(RHS))
3748 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3749
3750 // If we have a constant, make sure it is on the RHS.
3751 std::swap(LHS, RHS);
3752 Pred = CmpInst::getSwappedPredicate(Pred);
3753 }
3754 assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
3755
3756 Type *ITy = getCompareTy(LHS); // The return type.
3757
3758 // icmp poison, X -> poison
3759 if (isa<PoisonValue>(RHS))
3760 return PoisonValue::get(ITy);
3761
3762 // For EQ and NE, we can always pick a value for the undef to make the
3763 // predicate pass or fail, so we can return undef.
3764 // Matches behavior in llvm::ConstantFoldCompareInstruction.
3765 if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred))
3766 return UndefValue::get(ITy);
3767
3768 // icmp X, X -> true/false
3769 // icmp X, undef -> true/false because undef could be X.
3770 if (LHS == RHS || Q.isUndefValue(RHS))
3771 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3772
3773 if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3774 return V;
3775
3776 // TODO: Sink/common this with other potentially expensive calls that use
3777 // ValueTracking? See comment below for isKnownNonEqual().
3778 if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3779 return V;
3780
3781 if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
3782 return V;
3783
3784 // If both operands have range metadata, use the metadata
3785 // to simplify the comparison.
3786 if (std::optional<ConstantRange> RhsCr = getRange(RHS, Q.IIQ))
3787 if (std::optional<ConstantRange> LhsCr = getRange(LHS, Q.IIQ)) {
3788 if (LhsCr->icmp(Pred, *RhsCr))
3789 return ConstantInt::getTrue(ITy);
3790
3791 if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr))
3792 return ConstantInt::getFalse(ITy);
3793 }
3794
3795 // Compare of cast, for example (zext X) != 0 -> X != 0
3796 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3797 Instruction *LI = cast<CastInst>(LHS);
3798 Value *SrcOp = LI->getOperand(0);
3799 Type *SrcTy = SrcOp->getType();
3800 Type *DstTy = LI->getType();
3801
3802 // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3803 // if the integer type is the same size as the pointer type.
3804 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3805 Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3806 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3807 // Transfer the cast to the constant.
3808 if (Value *V = simplifyICmpInst(Pred, SrcOp,
3809 ConstantExpr::getIntToPtr(RHSC, SrcTy),
3810 Q, MaxRecurse - 1))
3811 return V;
3812 } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3813 if (RI->getOperand(0)->getType() == SrcTy)
3814 // Compare without the cast.
3815 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3816 MaxRecurse - 1))
3817 return V;
3818 }
3819 }
3820
3821 if (isa<ZExtInst>(LHS)) {
3822 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3823 // same type.
3824 if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3825 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3826 // Compare X and Y. Note that signed predicates become unsigned.
3827 if (Value *V =
3829 RI->getOperand(0), Q, MaxRecurse - 1))
3830 return V;
3831 }
3832 // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
3833 else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3834 if (SrcOp == RI->getOperand(0)) {
3835 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3836 return ConstantInt::getTrue(ITy);
3837 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3838 return ConstantInt::getFalse(ITy);
3839 }
3840 }
3841 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3842 // too. If not, then try to deduce the result of the comparison.
3843 else if (match(RHS, m_ImmConstant())) {
3844 Constant *C = dyn_cast<Constant>(RHS);
3845 assert(C != nullptr);
3846
3847 // Compute the constant that would happen if we truncated to SrcTy then
3848 // reextended to DstTy.
3849 Constant *Trunc =
3850 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3851 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3852 Constant *RExt =
3853 ConstantFoldCastOperand(CastInst::ZExt, Trunc, DstTy, Q.DL);
3854 assert(RExt && "Constant-fold of ImmConstant should not fail");
3855 Constant *AnyEq =
3856 ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3857 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3858
3859 // If the re-extended constant didn't change any of the elements then
3860 // this is effectively also a case of comparing two zero-extended
3861 // values.
3862 if (AnyEq->isAllOnesValue() && MaxRecurse)
3864 SrcOp, Trunc, Q, MaxRecurse - 1))
3865 return V;
3866
3867 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3868 // there. Use this to work out the result of the comparison.
3869 if (AnyEq->isNullValue()) {
3870 switch (Pred) {
3871 default:
3872 llvm_unreachable("Unknown ICmp predicate!");
3873 // LHS <u RHS.
3874 case ICmpInst::ICMP_EQ:
3875 case ICmpInst::ICMP_UGT:
3876 case ICmpInst::ICMP_UGE:
3877 return Constant::getNullValue(ITy);
3878
3879 case ICmpInst::ICMP_NE:
3880 case ICmpInst::ICMP_ULT:
3881 case ICmpInst::ICMP_ULE:
3882 return Constant::getAllOnesValue(ITy);
3883
3884 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
3885 // is non-negative then LHS <s RHS.
3886 case ICmpInst::ICMP_SGT:
3887 case ICmpInst::ICMP_SGE:
3889 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3890 Q.DL);
3891 case ICmpInst::ICMP_SLT:
3892 case ICmpInst::ICMP_SLE:
3894 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3895 Q.DL);
3896 }
3897 }
3898 }
3899 }
3900
3901 if (isa<SExtInst>(LHS)) {
3902 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3903 // same type.
3904 if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3905 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3906 // Compare X and Y. Note that the predicate does not change.
3907 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3908 MaxRecurse - 1))
3909 return V;
3910 }
3911 // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
3912 else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3913 if (SrcOp == RI->getOperand(0)) {
3914 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3915 return ConstantInt::getTrue(ITy);
3916 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3917 return ConstantInt::getFalse(ITy);
3918 }
3919 }
3920 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3921 // too. If not, then try to deduce the result of the comparison.
3922 else if (match(RHS, m_ImmConstant())) {
3923 Constant *C = cast<Constant>(RHS);
3924
3925 // Compute the constant that would happen if we truncated to SrcTy then
3926 // reextended to DstTy.
3927 Constant *Trunc =
3928 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3929 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3930 Constant *RExt =
3931 ConstantFoldCastOperand(CastInst::SExt, Trunc, DstTy, Q.DL);
3932 assert(RExt && "Constant-fold of ImmConstant should not fail");
3933 Constant *AnyEq =
3934 ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3935 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3936
3937 // If the re-extended constant didn't change then this is effectively
3938 // also a case of comparing two sign-extended values.
3939 if (AnyEq->isAllOnesValue() && MaxRecurse)
3940 if (Value *V =
3941 simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1))
3942 return V;
3943
3944 // Otherwise the upper bits of LHS are all equal, while RHS has varying
3945 // bits there. Use this to work out the result of the comparison.
3946 if (AnyEq->isNullValue()) {
3947 switch (Pred) {
3948 default:
3949 llvm_unreachable("Unknown ICmp predicate!");
3950 case ICmpInst::ICMP_EQ:
3951 return Constant::getNullValue(ITy);
3952 case ICmpInst::ICMP_NE:
3953 return Constant::getAllOnesValue(ITy);
3954
3955 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
3956 // LHS >s RHS.
3957 case ICmpInst::ICMP_SGT:
3958 case ICmpInst::ICMP_SGE:
3960 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3961 Q.DL);
3962 case ICmpInst::ICMP_SLT:
3963 case ICmpInst::ICMP_SLE:
3965 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3966 Q.DL);
3967
3968 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
3969 // LHS >u RHS.
3970 case ICmpInst::ICMP_UGT:
3971 case ICmpInst::ICMP_UGE:
3972 // Comparison is true iff the LHS <s 0.
3973 if (MaxRecurse)
3974 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
3975 Constant::getNullValue(SrcTy), Q,
3976 MaxRecurse - 1))
3977 return V;
3978 break;
3979 case ICmpInst::ICMP_ULT:
3980 case ICmpInst::ICMP_ULE:
3981 // Comparison is true iff the LHS >=s 0.
3982 if (MaxRecurse)
3983 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
3984 Constant::getNullValue(SrcTy), Q,
3985 MaxRecurse - 1))
3986 return V;
3987 break;
3988 }
3989 }
3990 }
3991 }
3992 }
3993
3994 // icmp eq|ne X, Y -> false|true if X != Y
3995 // This is potentially expensive, and we have already computedKnownBits for
3996 // compares with 0 above here, so only try this for a non-zero compare.
3997 if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero()) &&
3998 isKnownNonEqual(LHS, RHS, Q)) {
3999 return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy);
4000 }
4001
4002 if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
4003 return V;
4004
4005 if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
4006 return V;
4007
4009 return V;
4011 ICmpInst::getSwappedPredicate(Pred), RHS, LHS))
4012 return V;
4013
4015 return V;
4017 ICmpInst::getSwappedPredicate(Pred), RHS, LHS))
4018 return V;
4019
4020 if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q))
4021 return V;
4022
4023 if (std::optional<bool> Res =
4024 isImpliedByDomCondition(Pred, LHS, RHS, Q.CxtI, Q.DL))
4025 return ConstantInt::getBool(ITy, *Res);
4026
4027 // Simplify comparisons of related pointers using a powerful, recursive
4028 // GEP-walk when we have target data available..
4029 if (LHS->getType()->isPointerTy())
4030 if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
4031 return C;
4032 if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
4033 if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
4034 if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4035 Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4036 Q.DL.getTypeSizeInBits(CLHS->getType()))
4037 if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
4038 CRHS->getPointerOperand(), Q))
4039 return C;
4040
4041 // If the comparison is with the result of a select instruction, check whether
4042 // comparing with either branch of the select always yields the same value.
4043 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4044 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4045 return V;
4046
4047 // If the comparison is with the result of a phi instruction, check whether
4048 // doing the compare with each incoming phi value yields a common result.
4049 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4050 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4051 return V;
4052
4053 return nullptr;
4054}
4055
4057 const SimplifyQuery &Q) {
4058 return ::simplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
4059}
4060
4061/// Given operands for an FCmpInst, see if we can fold the result.
4062/// If not, this returns null.
4064 FastMathFlags FMF, const SimplifyQuery &Q,
4065 unsigned MaxRecurse) {
4066 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
4067
4068 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
4069 if (Constant *CRHS = dyn_cast<Constant>(RHS))
4070 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI,
4071 Q.CxtI);
4072
4073 // If we have a constant, make sure it is on the RHS.
4074 std::swap(LHS, RHS);
4075 Pred = CmpInst::getSwappedPredicate(Pred);
4076 }
4077
4078 // Fold trivial predicates.
4080 if (Pred == FCmpInst::FCMP_FALSE)
4081 return getFalse(RetTy);
4082 if (Pred == FCmpInst::FCMP_TRUE)
4083 return getTrue(RetTy);
4084
4085 // fcmp pred x, poison and fcmp pred poison, x
4086 // fold to poison
4087 if (isa<PoisonValue>(LHS) || isa<PoisonValue>(RHS))
4088 return PoisonValue::get(RetTy);
4089
4090 // fcmp pred x, undef and fcmp pred undef, x
4091 // fold to true if unordered, false if ordered
4092 if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) {
4093 // Choosing NaN for the undef will always make unordered comparison succeed
4094 // and ordered comparison fail.
4095 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4096 }
4097
4098 // fcmp x,x -> true/false. Not all compares are foldable.
4099 if (LHS == RHS) {
4100 if (CmpInst::isTrueWhenEqual(Pred))
4101 return getTrue(RetTy);
4102 if (CmpInst::isFalseWhenEqual(Pred))
4103 return getFalse(RetTy);
4104 }
4105
4106 // Fold (un)ordered comparison if we can determine there are no NaNs.
4107 //
4108 // This catches the 2 variable input case, constants are handled below as a
4109 // class-like compare.
4110 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4111 KnownFPClass RHSClass =
4112 computeKnownFPClass(RHS, fcAllFlags, /*Depth=*/0, Q);
4113 KnownFPClass LHSClass =
4114 computeKnownFPClass(LHS, fcAllFlags, /*Depth=*/0, Q);
4115
4116 if (FMF.noNaNs() ||
4117 (RHSClass.isKnownNeverNaN() && LHSClass.isKnownNeverNaN()))
4118 return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
4119
4120 if (RHSClass.isKnownAlwaysNaN() || LHSClass.isKnownAlwaysNaN())
4121 return ConstantInt::get(RetTy, Pred == CmpInst::FCMP_UNO);
4122 }
4123
4124 const APFloat *C = nullptr;
4126 std::optional<KnownFPClass> FullKnownClassLHS;
4127
4128 // Lazily compute the possible classes for LHS. Avoid computing it twice if
4129 // RHS is a 0.
4130 auto computeLHSClass = [=, &FullKnownClassLHS](FPClassTest InterestedFlags =
4131 fcAllFlags) {
4132 if (FullKnownClassLHS)
4133 return *FullKnownClassLHS;
4134 return computeKnownFPClass(LHS, FMF, InterestedFlags, 0, Q);
4135 };
4136
4137 if (C && Q.CxtI) {
4138 // Fold out compares that express a class test.
4139 //
4140 // FIXME: Should be able to perform folds without context
4141 // instruction. Always pass in the context function?
4142
4143 const Function *ParentF = Q.CxtI->getFunction();
4144 auto [ClassVal, ClassTest] = fcmpToClassTest(Pred, *ParentF, LHS, C);
4145 if (ClassVal) {
4146 FullKnownClassLHS = computeLHSClass();
4147 if ((FullKnownClassLHS->KnownFPClasses & ClassTest) == fcNone)
4148 return getFalse(RetTy);
4149 if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) == fcNone)
4150 return getTrue(RetTy);
4151 }
4152 }
4153
4154 // Handle fcmp with constant RHS.
4155 if (C) {
4156 // TODO: If we always required a context function, we wouldn't need to
4157 // special case nans.
4158 if (C->isNaN())
4159 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4160
4161 // TODO: Need version fcmpToClassTest which returns implied class when the
4162 // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but
4163 // isn't implementable as a class call.
4164 if (C->isNegative() && !C->isNegZero()) {
4166
4167 // TODO: We can catch more cases by using a range check rather than
4168 // relying on CannotBeOrderedLessThanZero.
4169 switch (Pred) {
4170 case FCmpInst::FCMP_UGE:
4171 case FCmpInst::FCMP_UGT:
4172 case FCmpInst::FCMP_UNE: {
4173 KnownFPClass KnownClass = computeLHSClass(Interested);
4174
4175 // (X >= 0) implies (X > C) when (C < 0)
4176 if (KnownClass.cannotBeOrderedLessThanZero())
4177 return getTrue(RetTy);
4178 break;
4179 }
4180 case FCmpInst::FCMP_OEQ:
4181 case FCmpInst::FCMP_OLE:
4182 case FCmpInst::FCMP_OLT: {
4183 KnownFPClass KnownClass = computeLHSClass(Interested);
4184
4185 // (X >= 0) implies !(X < C) when (C < 0)
4186 if (KnownClass.cannotBeOrderedLessThanZero())
4187 return getFalse(RetTy);
4188 break;
4189 }
4190 default:
4191 break;
4192 }
4193 }
4194 // Check comparison of [minnum/maxnum with constant] with other constant.
4195 const APFloat *C2;
4196 if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
4197 *C2 < *C) ||
4198 (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) &&
4199 *C2 > *C)) {
4200 bool IsMaxNum =
4201 cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
4202 // The ordered relationship and minnum/maxnum guarantee that we do not
4203 // have NaN constants, so ordered/unordered preds are handled the same.
4204 switch (Pred) {
4205 case FCmpInst::FCMP_OEQ:
4206 case FCmpInst::FCMP_UEQ:
4207 // minnum(X, LesserC) == C --> false
4208 // maxnum(X, GreaterC) == C --> false
4209 return getFalse(RetTy);
4210 case FCmpInst::FCMP_ONE:
4211 case FCmpInst::FCMP_UNE:
4212 // minnum(X, LesserC) != C --> true
4213 // maxnum(X, GreaterC) != C --> true
4214 return getTrue(RetTy);
4215 case FCmpInst::FCMP_OGE:
4216 case FCmpInst::FCMP_UGE:
4217 case FCmpInst::FCMP_OGT:
4218 case FCmpInst::FCMP_UGT:
4219 // minnum(X, LesserC) >= C --> false
4220 // minnum(X, LesserC) > C --> false
4221 // maxnum(X, GreaterC) >= C --> true
4222 // maxnum(X, GreaterC) > C --> true
4223 return ConstantInt::get(RetTy, IsMaxNum);
4224 case FCmpInst::FCMP_OLE:
4225 case FCmpInst::FCMP_ULE:
4226 case FCmpInst::FCMP_OLT:
4227 case FCmpInst::FCMP_ULT:
4228 // minnum(X, LesserC) <= C --> true
4229 // minnum(X, LesserC) < C --> true
4230 // maxnum(X, GreaterC) <= C --> false
4231 // maxnum(X, GreaterC) < C --> false
4232 return ConstantInt::get(RetTy, !IsMaxNum);
4233 default:
4234 // TRUE/FALSE/ORD/UNO should be handled before this.
4235 llvm_unreachable("Unexpected fcmp predicate");
4236 }
4237 }
4238 }
4239
4240 // TODO: Could fold this with above if there were a matcher which returned all
4241 // classes in a non-splat vector.
4242 if (match(RHS, m_AnyZeroFP())) {
4243 switch (Pred) {
4244 case FCmpInst::FCMP_OGE:
4245 case FCmpInst::FCMP_ULT: {
4247 if (!FMF.noNaNs())
4248 Interested |= fcNan;
4249
4250 KnownFPClass Known = computeLHSClass(Interested);
4251
4252 // Positive or zero X >= 0.0 --> true
4253 // Positive or zero X < 0.0 --> false
4254 if ((FMF.noNaNs() || Known.isKnownNeverNaN()) &&
4256 return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy) : getFalse(RetTy);
4257 break;
4258 }
4259 case FCmpInst::FCMP_UGE:
4260 case FCmpInst::FCMP_OLT: {
4262 KnownFPClass Known = computeLHSClass(Interested);
4263
4264 // Positive or zero or nan X >= 0.0 --> true
4265 // Positive or zero or nan X < 0.0 --> false
4266 if (Known.cannotBeOrderedLessThanZero())
4267 return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy) : getFalse(RetTy);
4268 break;
4269 }
4270 default:
4271 break;
4272 }
4273 }
4274
4275 // If the comparison is with the result of a select instruction, check whether
4276 // comparing with either branch of the select always yields the same value.
4277 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4278 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4279 return V;
4280
4281 // If the comparison is with the result of a phi instruction, check whether
4282 // doing the compare with each incoming phi value yields a common result.
4283 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4284 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4285 return V;
4286
4287 return nullptr;
4288}
4289
4291 FastMathFlags FMF, const SimplifyQuery &Q) {
4292 return ::simplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);
4293}
4294
4296 ArrayRef<std::pair<Value *, Value *>> Ops,
4297 const SimplifyQuery &Q,
4298 bool AllowRefinement,
4300 unsigned MaxRecurse) {
4301 assert((AllowRefinement || !Q.CanUseUndef) &&
4302 "If AllowRefinement=false then CanUseUndef=false");
4303 for (const auto &OpAndRepOp : Ops) {
4304 // We cannot replace a constant, and shouldn't even try.
4305 if (isa<Constant>(OpAndRepOp.first))
4306 return nullptr;
4307
4308 // Trivial replacement.
4309 if (V == OpAndRepOp.first)
4310 return OpAndRepOp.second;
4311 }
4312
4313 if (!MaxRecurse--)
4314 return nullptr;
4315
4316 auto *I = dyn_cast<Instruction>(V);
4317 if (!I)
4318 return nullptr;
4319
4320 // The arguments of a phi node might refer to a value from a previous
4321 // cycle iteration.
4322 if (isa<PHINode>(I))
4323 return nullptr;
4324
4325 // Don't fold away llvm.is.constant checks based on assumptions.
4326 if (match(I, m_Intrinsic<Intrinsic::is_constant>()))
4327 return nullptr;
4328
4329 // Don't simplify freeze.
4330 if (isa<FreezeInst>(I))
4331 return nullptr;
4332
4333 for (const auto &OpAndRepOp : Ops) {
4334 // For vector types, the simplification must hold per-lane, so forbid
4335 // potentially cross-lane operations like shufflevector.
4336 if (OpAndRepOp.first->getType()->isVectorTy() &&
4338 return nullptr;
4339 }
4340
4341 // Replace Op with RepOp in instruction operands.
4343 bool AnyReplaced = false;
4344 for (Value *InstOp : I->operands()) {
4345 if (Value *NewInstOp = simplifyWithOpsReplaced(
4346 InstOp, Ops, Q, AllowRefinement, DropFlags, MaxRecurse)) {
4347 NewOps.push_back(NewInstOp);
4348 AnyReplaced = InstOp != NewInstOp;
4349 } else {
4350 NewOps.push_back(InstOp);
4351 }
4352
4353 // Bail out if any operand is undef and SimplifyQuery disables undef
4354 // simplification. Constant folding currently doesn't respect this option.
4355 if (isa<UndefValue>(NewOps.back()) && !Q.CanUseUndef)
4356 return nullptr;
4357 }
4358
4359 if (!AnyReplaced)
4360 return nullptr;
4361
4362 if (!AllowRefinement) {
4363 // General InstSimplify functions may refine the result, e.g. by returning
4364 // a constant for a potentially poison value. To avoid this, implement only
4365 // a few non-refining but profitable transforms here.
4366
4367 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
4368 unsigned Opcode = BO->getOpcode();
4369 // id op x -> x, x op id -> x
4370 // Exclude floats, because x op id may produce a different NaN value.
4371 if (!BO->getType()->isFPOrFPVectorTy()) {
4372 if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType()))
4373 return NewOps[1];
4374 if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(),
4375 /* RHS */ true))
4376 return NewOps[0];
4377 }
4378
4379 // x & x -> x, x | x -> x
4380 if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
4381 NewOps[0] == NewOps[1]) {
4382 // or disjoint x, x results in poison.
4383 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(BO)) {
4384 if (PDI->isDisjoint()) {
4385 if (!DropFlags)
4386 return nullptr;
4387 DropFlags->push_back(BO);
4388 }
4389 }
4390 return NewOps[0];
4391 }
4392
4393 // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison
4394 // by assumption and this case never wraps, so nowrap flags can be
4395 // ignored.
4396 if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor) &&
4397 NewOps[0] == NewOps[1] &&
4398 any_of(Ops, [=](const auto &Rep) { return NewOps[0] == Rep.second; }))
4399 return Constant::getNullValue(I->getType());
4400
4401 // If we are substituting an absorber constant into a binop and extra
4402 // poison can't leak if we remove the select -- because both operands of
4403 // the binop are based on the same value -- then it may be safe to replace
4404 // the value with the absorber constant. Examples:
4405 // (Op == 0) ? 0 : (Op & -Op) --> Op & -Op
4406 // (Op == 0) ? 0 : (Op * (binop Op, C)) --> Op * (binop Op, C)
4407 // (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op)
4408 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, I->getType());
4409 if ((NewOps[0] == Absorber || NewOps[1] == Absorber) &&
4410 any_of(Ops,
4411 [=](const auto &Rep) { return impliesPoison(BO, Rep.first); }))
4412 return Absorber;
4413 }
4414
4415 if (isa<GetElementPtrInst>(I)) {
4416 // getelementptr x, 0 -> x.
4417 // This never returns poison, even if inbounds is set.
4418 if (NewOps.size() == 2 && match(NewOps[1], m_Zero()))
4419 return NewOps[0];
4420 }
4421 } else {
4422 // The simplification queries below may return the original value. Consider:
4423 // %div = udiv i32 %arg, %arg2
4424 // %mul = mul nsw i32 %div, %arg2
4425 // %cmp = icmp eq i32 %mul, %arg
4426 // %sel = select i1 %cmp, i32 %div, i32 undef
4427 // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which
4428 // simplifies back to %arg. This can only happen because %mul does not
4429 // dominate %div. To ensure a consistent return value contract, we make sure
4430 // that this case returns nullptr as well.
4431 auto PreventSelfSimplify = [V](Value *Simplified) {
4432 return Simplified != V ? Simplified : nullptr;
4433 };
4434
4435 return PreventSelfSimplify(
4436 ::simplifyInstructionWithOperands(I, NewOps, Q, MaxRecurse));
4437 }
4438
4439 // If all operands are constant after substituting Op for RepOp then we can
4440 // constant fold the instruction.
4442 for (Value *NewOp : NewOps) {
4443 if (Constant *ConstOp = dyn_cast<Constant>(NewOp))
4444 ConstOps.push_back(ConstOp);
4445 else
4446 return nullptr;
4447 }
4448
4449 // Consider:
4450 // %cmp = icmp eq i32 %x, 2147483647
4451 // %add = add nsw i32 %x, 1
4452 // %sel = select i1 %cmp, i32 -2147483648, i32 %add
4453 //
4454 // We can't replace %sel with %add unless we strip away the flags (which
4455 // will be done in InstCombine).
4456 // TODO: This may be unsound, because it only catches some forms of
4457 // refinement.
4458 if (!AllowRefinement) {
4459 if (canCreatePoison(cast<Operator>(I), !DropFlags)) {
4460 // abs cannot create poison if the value is known to never be int_min.
4461 if (auto *II = dyn_cast<IntrinsicInst>(I);
4462 II && II->getIntrinsicID() == Intrinsic::abs) {
4463 if (!ConstOps[0]->isNotMinSignedValue())
4464 return nullptr;
4465 } else
4466 return nullptr;
4467 }
4468 Constant *Res = ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4469 /*AllowNonDeterministic=*/false);
4470 if (DropFlags && Res && I->hasPoisonGeneratingAnnotations())
4471 DropFlags->push_back(I);
4472 return Res;
4473 }
4474
4475 return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4476 /*AllowNonDeterministic=*/false);
4477}
4478
4480 const SimplifyQuery &Q,
4481 bool AllowRefinement,
4483 unsigned MaxRecurse) {
4484 return simplifyWithOpsReplaced(V, {{Op, RepOp}}, Q, AllowRefinement,
4485 DropFlags, MaxRecurse);
4486}
4487
4489 const SimplifyQuery &Q,
4490 bool AllowRefinement,
4491 SmallVectorImpl<Instruction *> *DropFlags) {
4492 // If refinement is disabled, also disable undef simplifications (which are
4493 // always refinements) in SimplifyQuery.
4494 if (!AllowRefinement)
4495 return ::simplifyWithOpReplaced(V, Op, RepOp, Q.getWithoutUndef(),
4496 AllowRefinement, DropFlags, RecursionLimit);
4497 return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags,
4499}
4500
4501/// Try to simplify a select instruction when its condition operand is an
4502/// integer comparison where one operand of the compare is a constant.
4503static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
4504 const APInt *Y, bool TrueWhenUnset) {
4505 const APInt *C;
4506
4507 // (X & Y) == 0 ? X & ~Y : X --> X
4508 // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
4509 if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) &&
4510 *Y == ~*C)
4511 return TrueWhenUnset ? FalseVal : TrueVal;
4512
4513 // (X & Y) == 0 ? X : X & ~Y --> X & ~Y
4514 // (X & Y) != 0 ? X : X & ~Y --> X
4515 if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) &&
4516 *Y == ~*C)
4517 return TrueWhenUnset ? FalseVal : TrueVal;
4518
4519 if (Y->isPowerOf2()) {
4520 // (X & Y) == 0 ? X | Y : X --> X | Y
4521 // (X & Y) != 0 ? X | Y : X --> X
4522 if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) &&
4523 *Y == *C) {
4524 // We can't return the or if it has the disjoint flag.
4525 if (TrueWhenUnset && cast<PossiblyDisjointInst>(TrueVal)->isDisjoint())
4526 return nullptr;
4527 return TrueWhenUnset ? TrueVal : FalseVal;
4528 }
4529
4530 // (X & Y) == 0 ? X : X | Y --> X
4531 // (X & Y) != 0 ? X : X | Y --> X | Y
4532 if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) &&
4533 *Y == *C) {
4534 // We can't return the or if it has the disjoint flag.
4535 if (!TrueWhenUnset && cast<PossiblyDisjointInst>(FalseVal)->isDisjoint())
4536 return nullptr;
4537 return TrueWhenUnset ? TrueVal : FalseVal;
4538 }
4539 }
4540
4541 return nullptr;
4542}
4543
4544static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS,
4545 CmpPredicate Pred, Value *TVal,
4546 Value *FVal) {
4547 // Canonicalize common cmp+sel operand as CmpLHS.
4548 if (CmpRHS == TVal || CmpRHS == FVal) {
4549 std::swap(CmpLHS, CmpRHS);
4550 Pred = ICmpInst::getSwappedPredicate(Pred);
4551 }
4552
4553 // Canonicalize common cmp+sel operand as TVal.
4554 if (CmpLHS == FVal) {
4555 std::swap(TVal, FVal);
4556 Pred = ICmpInst::getInversePredicate(Pred);
4557 }
4558
4559 // A vector select may be shuffling together elements that are equivalent
4560 // based on the max/min/select relationship.
4561 Value *X = CmpLHS, *Y = CmpRHS;
4562 bool PeekedThroughSelectShuffle = false;
4563 auto *Shuf = dyn_cast<ShuffleVectorInst>(FVal);
4564 if (Shuf && Shuf->isSelect()) {
4565 if (Shuf->getOperand(0) == Y)
4566 FVal = Shuf->getOperand(1);
4567 else if (Shuf->getOperand(1) == Y)
4568 FVal = Shuf->getOperand(0);
4569 else
4570 return nullptr;
4571 PeekedThroughSelectShuffle = true;
4572 }
4573
4574 // (X pred Y) ? X : max/min(X, Y)
4575 auto *MMI = dyn_cast<MinMaxIntrinsic>(FVal);
4576 if (!MMI || TVal != X ||
4578 return nullptr;
4579
4580 // (X > Y) ? X : max(X, Y) --> max(X, Y)
4581 // (X >= Y) ? X : max(X, Y) --> max(X, Y)
4582 // (X < Y) ? X : min(X, Y) --> min(X, Y)
4583 // (X <= Y) ? X : min(X, Y) --> min(X, Y)
4584 //
4585 // The equivalence allows a vector select (shuffle) of max/min and Y. Ex:
4586 // (X > Y) ? X : (Z ? max(X, Y) : Y)
4587 // If Z is true, this reduces as above, and if Z is false:
4588 // (X > Y) ? X : Y --> max(X, Y)
4589 ICmpInst::Predicate MMPred = MMI->getPredicate();
4590 if (MMPred == CmpInst::getStrictPredicate(Pred))
4591 return MMI;
4592
4593 // Other transforms are not valid with a shuffle.
4594 if (PeekedThroughSelectShuffle)
4595 return nullptr;
4596
4597 // (X == Y) ? X : max/min(X, Y) --> max/min(X, Y)
4598 if (Pred == CmpInst::ICMP_EQ)
4599 return MMI;
4600
4601 // (X != Y) ? X : max/min(X, Y) --> X
4602 if (Pred == CmpInst::ICMP_NE)
4603 return X;
4604
4605 // (X < Y) ? X : max(X, Y) --> X
4606 // (X <= Y) ? X : max(X, Y) --> X
4607 // (X > Y) ? X : min(X, Y) --> X
4608 // (X >= Y) ? X : min(X, Y) --> X
4610 if (MMPred == CmpInst::getStrictPredicate(InvPred))
4611 return X;
4612
4613 return nullptr;
4614}
4615
4616/// An alternative way to test if a bit is set or not.
4617/// uses e.g. sgt/slt or trunc instead of eq/ne.
4618static Value *simplifySelectWithBitTest(Value *CondVal, Value *TrueVal,
4619 Value *FalseVal) {
4620 if (auto Res = decomposeBitTest(CondVal))
4621 return simplifySelectBitTest(TrueVal, FalseVal, Res->X, &Res->Mask,
4622 Res->Pred == ICmpInst::ICMP_EQ);
4623
4624 return nullptr;
4625}
4626
4627/// Try to simplify a select instruction when its condition operand is an
4628/// integer equality or floating-point equivalence comparison.
4630 ArrayRef<std::pair<Value *, Value *>> Replacements, Value *TrueVal,
4631 Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse) {
4632 Value *SimplifiedFalseVal =
4633 simplifyWithOpsReplaced(FalseVal, Replacements, Q.getWithoutUndef(),
4634 /* AllowRefinement */ false,
4635 /* DropFlags */ nullptr, MaxRecurse);
4636 if (!SimplifiedFalseVal)
4637 SimplifiedFalseVal = FalseVal;
4638
4639 Value *SimplifiedTrueVal =
4640 simplifyWithOpsReplaced(TrueVal, Replacements, Q,
4641 /* AllowRefinement */ true,
4642 /* DropFlags */ nullptr, MaxRecurse);
4643 if (!SimplifiedTrueVal)
4644 SimplifiedTrueVal = TrueVal;
4645
4646 if (SimplifiedFalseVal == SimplifiedTrueVal)
4647 return FalseVal;
4648
4649 return nullptr;
4650}
4651
4652/// Try to simplify a select instruction when its condition operand is an
4653/// integer comparison.
4654static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4655 Value *FalseVal,
4656 const SimplifyQuery &Q,
4657 unsigned MaxRecurse) {
4658 CmpPredicate Pred;
4659 Value *CmpLHS, *CmpRHS;
4660 if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4661 return nullptr;
4662
4663 if (Value *V = simplifyCmpSelOfMaxMin(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
4664 return V;
4665
4666 // Canonicalize ne to eq predicate.
4667 if (Pred == ICmpInst::ICMP_NE) {
4668 Pred = ICmpInst::ICMP_EQ;
4669 std::swap(TrueVal, FalseVal);
4670 }
4671
4672 // Check for integer min/max with a limit constant:
4673 // X > MIN_INT ? X : MIN_INT --> X
4674 // X < MAX_INT ? X : MAX_INT --> X
4675 if (TrueVal->getType()->isIntOrIntVectorTy()) {
4676 Value *X, *Y;
4678 matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal,
4679 X, Y)
4680 .Flavor;
4681 if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
4683 X->getType()->getScalarSizeInBits());
4684 if (match(Y, m_SpecificInt(LimitC)))
4685 return X;
4686 }
4687 }
4688
4689 if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())) {
4690 Value *X;
4691 const APInt *Y;
4692 if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
4693 if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
4694 /*TrueWhenUnset=*/true))
4695 return V;
4696
4697 // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
4698 Value *ShAmt;
4699 auto isFsh = m_CombineOr(m_FShl(m_Value(X), m_Value(), m_Value(ShAmt)),
4700 m_FShr(m_Value(), m_Value(X), m_Value(ShAmt)));
4701 // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
4702 // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
4703 if (match(TrueVal, isFsh) && FalseVal == X && CmpLHS == ShAmt)
4704 return X;
4705
4706 // Test for a zero-shift-guard-op around rotates. These are used to
4707 // avoid UB from oversized shifts in raw IR rotate patterns, but the
4708 // intrinsics do not have that problem.
4709 // We do not allow this transform for the general funnel shift case because
4710 // that would not preserve the poison safety of the original code.
4711 auto isRotate =
4713 m_FShr(m_Value(X), m_Deferred(X), m_Value(ShAmt)));
4714 // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4715 // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4716 if (match(FalseVal, isRotate) && TrueVal == X && CmpLHS == ShAmt &&
4717 Pred == ICmpInst::ICMP_EQ)
4718 return FalseVal;
4719
4720 // X == 0 ? abs(X) : -abs(X) --> -abs(X)
4721 // X == 0 ? -abs(X) : abs(X) --> abs(X)
4722 if (match(TrueVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))) &&
4723 match(FalseVal, m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))))
4724 return FalseVal;
4725 if (match(TrueVal,
4726 m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))) &&
4727 match(FalseVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))))
4728 return FalseVal;
4729 }
4730
4731 // If we have a scalar equality comparison, then we know the value in one of
4732 // the arms of the select. See if substituting this value into the arm and
4733 // simplifying the result yields the same value as the other arm.
4734 if (Pred == ICmpInst::ICMP_EQ) {
4735 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4736 canReplacePointersIfEqual(CmpLHS, CmpRHS, Q.DL))
4737 if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4738 FalseVal, Q, MaxRecurse))
4739 return V;
4740 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4741 canReplacePointersIfEqual(CmpRHS, CmpLHS, Q.DL))
4742 if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4743 FalseVal, Q, MaxRecurse))
4744 return V;
4745
4746 Value *X;
4747 Value *Y;
4748 // select((X | Y) == 0 ? X : 0) --> 0 (commuted 2 ways)
4749 if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) &&
4750 match(CmpRHS, m_Zero())) {
4751 // (X | Y) == 0 implies X == 0 and Y == 0.
4753 {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4754 return V;
4755 }
4756
4757 // select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
4758 if (match(CmpLHS, m_And(m_Value(X), m_Value(Y))) &&
4759 match(CmpRHS, m_AllOnes())) {
4760 // (X & Y) == -1 implies X == -1 and Y == -1.
4762 {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4763 return V;
4764 }
4765 }
4766
4767 return nullptr;
4768}
4769
4770/// Try to simplify a select instruction when its condition operand is a
4771/// floating-point comparison.
4773 const SimplifyQuery &Q,
4774 unsigned MaxRecurse) {
4775 CmpPredicate Pred;
4776 Value *CmpLHS, *CmpRHS;
4777 if (!match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4778 return nullptr;
4779 FCmpInst *I = cast<FCmpInst>(Cond);
4780
4781 bool IsEquiv = I->isEquivalence();
4782 if (I->isEquivalence(/*Invert=*/true)) {
4783 std::swap(T, F);
4784 Pred = FCmpInst::getInversePredicate(Pred);
4785 IsEquiv = true;
4786 }
4787
4788 // This transforms is safe if at least one operand is known to not be zero.
4789 // Otherwise, the select can change the sign of a zero operand.
4790 if (IsEquiv) {
4791 if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, T, F, Q,
4792 MaxRecurse))
4793 return V;
4794 if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, T, F, Q,
4795 MaxRecurse))
4796 return V;
4797 }
4798
4799 // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
4800 if (CmpLHS == F && CmpRHS == T)
4801 std::swap(CmpLHS, CmpRHS);
4802
4803 if (CmpLHS != T || CmpRHS != F)
4804 return nullptr;
4805
4806 // This transform is also safe if we do not have (do not care about) -0.0.
4807 if (Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros()) {
4808 // (T == F) ? T : F --> F
4809 if (Pred == FCmpInst::FCMP_OEQ)
4810 return F;
4811
4812 // (T != F) ? T : F --> T
4813 if (Pred == FCmpInst::FCMP_UNE)
4814 return T;
4815 }
4816
4817 return nullptr;
4818}
4819
4820/// Given operands for a SelectInst, see if we can fold the result.
4821/// If not, this returns null.
4822static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4823 const SimplifyQuery &Q, unsigned MaxRecurse) {
4824 if (auto *CondC = dyn_cast<Constant>(Cond)) {
4825 if (auto *TrueC = dyn_cast<Constant>(TrueVal))
4826 if (auto *FalseC = dyn_cast<Constant>(FalseVal))
4827 if (Constant *C = ConstantFoldSelectInstruction(CondC, TrueC, FalseC))
4828 return C;
4829
4830 // select poison, X, Y -> poison
4831 if (isa<PoisonValue>(CondC))
4832 return PoisonValue::get(TrueVal->getType());
4833
4834 // select undef, X, Y -> X or Y
4835 if (Q.isUndefValue(CondC))
4836 return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
4837
4838 // select true, X, Y --> X
4839 // select false, X, Y --> Y
4840 // For vectors, allow undef/poison elements in the condition to match the
4841 // defined elements, so we can eliminate the select.
4842 if (match(CondC, m_One()))
4843 return TrueVal;
4844 if (match(CondC, m_Zero()))
4845 return FalseVal;
4846 }
4847
4848 assert(Cond->getType()->isIntOrIntVectorTy(1) &&
4849 "Select must have bool or bool vector condition");
4850 assert(TrueVal->getType() == FalseVal->getType() &&
4851 "Select must have same types for true/false ops");
4852
4853 if (Cond->getType() == TrueVal->getType()) {
4854 // select i1 Cond, i1 true, i1 false --> i1 Cond
4855 if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
4856 return Cond;
4857
4858 // (X && Y) ? X : Y --> Y (commuted 2 ways)
4859 if (match(Cond, m_c_LogicalAnd(m_Specific(TrueVal), m_Specific(FalseVal))))
4860 return FalseVal;
4861
4862 // (X || Y) ? X : Y --> X (commuted 2 ways)
4863 if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
4864 return TrueVal;
4865
4866 // (X || Y) ? false : X --> false (commuted 2 ways)
4867 if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
4868 match(TrueVal, m_ZeroInt()))
4869 return ConstantInt::getFalse(Cond->getType());
4870
4871 // Match patterns that end in logical-and.
4872 if (match(FalseVal, m_ZeroInt())) {
4873 // !(X || Y) && X --> false (commuted 2 ways)
4874 if (match(Cond, m_Not(m_c_LogicalOr(m_Specific(TrueVal), m_Value()))))
4875 return ConstantInt::getFalse(Cond->getType());
4876 // X && !(X || Y) --> false (commuted 2 ways)
4877 if (match(TrueVal, m_Not(m_c_LogicalOr(m_Specific(Cond), m_Value()))))
4878 return ConstantInt::getFalse(Cond->getType());
4879
4880 // (X || Y) && Y --> Y (commuted 2 ways)
4881 if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Value())))
4882 return TrueVal;
4883 // Y && (X || Y) --> Y (commuted 2 ways)
4884 if (match(TrueVal, m_c_LogicalOr(m_Specific(Cond), m_Value())))
4885 return Cond;
4886
4887 // (X || Y) && (X || !Y) --> X (commuted 8 ways)
4888 Value *X, *Y;
4891 return X;
4892 if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4894 return X;
4895 }
4896
4897 // Match patterns that end in logical-or.
4898 if (match(TrueVal, m_One())) {
4899 // !(X && Y) || X --> true (commuted 2 ways)
4900 if (match(Cond, m_Not(m_c_LogicalAnd(m_Specific(FalseVal), m_Value()))))
4901 return ConstantInt::getTrue(Cond->getType());
4902 // X || !(X && Y) --> true (commuted 2 ways)
4903 if (match(FalseVal, m_Not(m_c_LogicalAnd(m_Specific(Cond), m_Value()))))
4904 return ConstantInt::getTrue(Cond->getType());
4905
4906 // (X && Y) || Y --> Y (commuted 2 ways)
4907 if (match(Cond, m_c_LogicalAnd(m_Specific(FalseVal), m_Value())))
4908 return FalseVal;
4909 // Y || (X && Y) --> Y (commuted 2 ways)
4910 if (match(FalseVal, m_c_LogicalAnd(m_Specific(Cond), m_Value())))
4911 return Cond;
4912 }
4913 }
4914
4915 // select ?, X, X -> X
4916 if (TrueVal == FalseVal)
4917 return TrueVal;
4918
4919 if (Cond == TrueVal) {
4920 // select i1 X, i1 X, i1 false --> X (logical-and)
4921 if (match(FalseVal, m_ZeroInt()))
4922 return Cond;
4923 // select i1 X, i1 X, i1 true --> true
4924 if (match(FalseVal, m_One()))
4925 return ConstantInt::getTrue(Cond->getType());
4926 }
4927 if (Cond == FalseVal) {
4928 // select i1 X, i1 true, i1 X --> X (logical-or)
4929 if (match(TrueVal, m_One()))
4930 return Cond;
4931 // select i1 X, i1 false, i1 X --> false
4932 if (match(TrueVal, m_ZeroInt()))
4933 return ConstantInt::getFalse(Cond->getType());
4934 }
4935
4936 // If the true or false value is poison, we can fold to the other value.
4937 // If the true or false value is undef, we can fold to the other value as
4938 // long as the other value isn't poison.
4939 // select ?, poison, X -> X
4940 // select ?, undef, X -> X
4941 if (isa<PoisonValue>(TrueVal) ||
4942 (Q.isUndefValue(TrueVal) && impliesPoison(FalseVal, Cond)))
4943 return FalseVal;
4944 // select ?, X, poison -> X
4945 // select ?, X, undef -> X
4946 if (isa<PoisonValue>(FalseVal) ||
4947 (Q.isUndefValue(FalseVal) && impliesPoison(TrueVal, Cond)))
4948 return TrueVal;
4949
4950 // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
4951 Constant *TrueC, *FalseC;
4952 if (isa<FixedVectorType>(TrueVal->getType()) &&
4953 match(TrueVal, m_Constant(TrueC)) &&
4954 match(FalseVal, m_Constant(FalseC))) {
4955 unsigned NumElts =
4956 cast<FixedVectorType>(TrueC->getType())->getNumElements();
4958 for (unsigned i = 0; i != NumElts; ++i) {
4959 // Bail out on incomplete vector constants.
4960 Constant *TEltC = TrueC->getAggregateElement(i);
4961 Constant *FEltC = FalseC->getAggregateElement(i);
4962 if (!TEltC || !FEltC)
4963 break;
4964
4965 // If the elements match (undef or not), that value is the result. If only
4966 // one element is undef, choose the defined element as the safe result.
4967 if (TEltC == FEltC)
4968 NewC.push_back(TEltC);
4969 else if (isa<PoisonValue>(TEltC) ||
4970 (Q.isUndefValue(TEltC) && isGuaranteedNotToBePoison(FEltC)))
4971 NewC.push_back(FEltC);
4972 else if (isa<PoisonValue>(FEltC) ||
4973 (Q.isUndefValue(FEltC) && isGuaranteedNotToBePoison(TEltC)))
4974 NewC.push_back(TEltC);
4975 else
4976 break;
4977 }
4978 if (NewC.size() == NumElts)
4979 return ConstantVector::get(NewC);
4980 }
4981
4982 if (Value *V =
4983 simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
4984 return V;
4985
4986 if (Value *V = simplifySelectWithBitTest(Cond, TrueVal, FalseVal))
4987 return V;
4988
4989 if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q, MaxRecurse))
4990 return V;
4991
4992 std::optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
4993 if (Imp)
4994 return *Imp ? TrueVal : FalseVal;
4995
4996 return nullptr;
4997}
4998
5000 const SimplifyQuery &Q) {
5001 return ::simplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit);
5002}
5003
5004/// Given operands for an GetElementPtrInst, see if we can fold the result.
5005/// If not, this returns null.
5008 const SimplifyQuery &Q, unsigned) {
5009 // The type of the GEP pointer operand.
5010 unsigned AS =
5011 cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace();
5012
5013 // getelementptr P -> P.
5014 if (Indices.empty())
5015 return Ptr;
5016
5017 // Compute the (pointer) type returned by the GEP instruction.
5018 Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Indices);
5019 Type *GEPTy = Ptr->getType();
5020 if (!GEPTy->isVectorTy()) {
5021 for (Value *Op : Indices) {
5022 // If one of the operands is a vector, the result type is a vector of
5023 // pointers. All vector operands must have the same number of elements.
5024 if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) {
5025 GEPTy = VectorType::get(GEPTy, VT->getElementCount());
5026 break;
5027 }
5028 }
5029 }
5030
5031 // All-zero GEP is a no-op, unless it performs a vector splat.
5032 if (Ptr->getType() == GEPTy &&
5033 all_of(Indices, [](const auto *V) { return match(V, m_Zero()); }))
5034 return Ptr;
5035
5036 // getelementptr poison, idx -> poison
5037 // getelementptr baseptr, poison -> poison
5038 if (isa<PoisonValue>(Ptr) ||
5039 any_of(Indices, [](const auto *V) { return isa<PoisonValue>(V); }))
5040 return PoisonValue::get(GEPTy);
5041
5042 // getelementptr undef, idx -> undef
5043 if (Q.isUndefValue(Ptr))
5044 return UndefValue::get(GEPTy);
5045
5046 bool IsScalableVec =
5047 SrcTy->isScalableTy() || any_of(Indices, [](const Value *V) {
5048 return isa<ScalableVectorType>(V->getType());
5049 });
5050
5051 if (Indices.size() == 1) {
5052 Type *Ty = SrcTy;
5053 if (!IsScalableVec && Ty->isSized()) {
5054 Value *P;
5055 uint64_t C;
5056 uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
5057 // getelementptr P, N -> P if P points to a type of zero size.
5058 if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
5059 return Ptr;
5060
5061 // The following transforms are only safe if the ptrtoint cast
5062 // doesn't truncate the pointers.
5063 if (Indices[0]->getType()->getScalarSizeInBits() ==
5064 Q.DL.getPointerSizeInBits(AS)) {
5065 auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
5066 return P->getType() == GEPTy &&
5068 };
5069 // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
5070 if (TyAllocSize == 1 &&
5071 match(Indices[0],
5073 CanSimplify())
5074 return P;
5075
5076 // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of
5077 // size 1 << C.
5078 if (match(Indices[0], m_AShr(m_Sub(m_PtrToInt(m_Value(P)),
5080 m_ConstantInt(C))) &&
5081 TyAllocSize == 1ULL << C && CanSimplify())
5082 return P;
5083
5084 // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of
5085 // size C.
5086 if (match(Indices[0], m_SDiv(m_Sub(m_PtrToInt(m_Value(P)),
5088 m_SpecificInt(TyAllocSize))) &&
5089 CanSimplify())
5090 return P;
5091 }
5092 }
5093 }
5094
5095 if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
5096 all_of(Indices.drop_back(1),
5097 [](Value *Idx) { return match(Idx, m_Zero()); })) {
5098 unsigned IdxWidth =
5099 Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
5100 if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
5101 APInt BasePtrOffset(IdxWidth, 0);
5102 Value *StrippedBasePtr =
5103 Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset);
5104
5105 // Avoid creating inttoptr of zero here: While LLVMs treatment of
5106 // inttoptr is generally conservative, this particular case is folded to
5107 // a null pointer, which will have incorrect provenance.
5108
5109 // gep (gep V, C), (sub 0, V) -> C
5110 if (match(Indices.back(),
5111 m_Neg(m_PtrToInt(m_Specific(StrippedBasePtr)))) &&
5112 !BasePtrOffset.isZero()) {
5113 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
5114 return ConstantExpr::getIntToPtr(CI, GEPTy);
5115 }
5116 // gep (gep V, C), (xor V, -1) -> C-1
5117 if (match(Indices.back(),
5118 m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes())) &&
5119 !BasePtrOffset.isOne()) {
5120 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
5121 return ConstantExpr::getIntToPtr(CI, GEPTy);
5122 }
5123 }
5124 }
5125
5126 // Check to see if this is constant foldable.
5127 if (!isa<Constant>(Ptr) ||
5128 !all_of(Indices, [](Value *V) { return isa<Constant>(V); }))
5129 return nullptr;
5130
5132 return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr), std::nullopt,
5133 Indices);
5134
5135 auto *CE =
5136 ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices, NW);
5137 return ConstantFoldConstant(CE, Q.DL);
5138}
5139
5141 GEPNoWrapFlags NW, const SimplifyQuery &Q) {
5142 return ::simplifyGEPInst(SrcTy, Ptr, Indices, NW, Q, RecursionLimit);
5143}
5144
5145/// Given operands for an InsertValueInst, see if we can fold the result.
5146/// If not, this returns null.
5148 ArrayRef<unsigned> Idxs,
5149 const SimplifyQuery &Q, unsigned) {
5150 if (Constant *CAgg = dyn_cast<Constant>(Agg))
5151 if (Constant *CVal = dyn_cast<Constant>(Val))
5152 return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
5153
5154 // insertvalue x, poison, n -> x
5155 // insertvalue x, undef, n -> x if x cannot be poison
5156 if (isa<PoisonValue>(Val) ||
5157 (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Agg)))
5158 return Agg;
5159
5160 // insertvalue x, (extractvalue y, n), n
5161 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
5162 if (EV->getAggregateOperand()->getType() == Agg->getType() &&
5163 EV->getIndices() == Idxs) {
5164 // insertvalue poison, (extractvalue y, n), n -> y
5165 // insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison
5166 if (isa<PoisonValue>(Agg) ||
5167 (Q.isUndefValue(Agg) &&
5168 isGuaranteedNotToBePoison(EV->getAggregateOperand())))
5169 return EV->getAggregateOperand();
5170
5171 // insertvalue y, (extractvalue y, n), n -> y
5172 if (Agg == EV->getAggregateOperand())
5173 return Agg;
5174 }
5175
5176 return nullptr;
5177}
5178
5180 ArrayRef<unsigned> Idxs,
5181 const SimplifyQuery &Q) {
5182 return ::simplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
5183}
5184
5186 const SimplifyQuery &Q) {
5187 // Try to constant fold.
5188 auto *VecC = dyn_cast<Constant>(Vec);
5189 auto *ValC = dyn_cast<Constant>(Val);
5190 auto *IdxC = dyn_cast<Constant>(Idx);
5191 if (VecC && ValC && IdxC)
5192 return ConstantExpr::getInsertElement(VecC, ValC, IdxC);
5193
5194 // For fixed-length vector, fold into poison if index is out of bounds.
5195 if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
5196 if (isa<FixedVectorType>(Vec->getType()) &&
5197 CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements()))
5198 return PoisonValue::get(Vec->getType());
5199 }
5200
5201 // If index is undef, it might be out of bounds (see above case)
5202 if (Q.isUndefValue(Idx))
5203 return PoisonValue::get(Vec->getType());
5204
5205 // If the scalar is poison, or it is undef and there is no risk of
5206 // propagating poison from the vector value, simplify to the vector value.
5207 if (isa<PoisonValue>(Val) ||
5208 (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)))
5209 return Vec;
5210
5211 // Inserting the splatted value into a constant splat does nothing.
5212 if (VecC && ValC && VecC->getSplatValue() == ValC)
5213 return Vec;
5214
5215 // If we are extracting a value from a vector, then inserting it into the same
5216 // place, that's the input vector:
5217 // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
5218 if (match(Val, m_ExtractElt(m_Specific(Vec), m_Specific(Idx))))
5219 return Vec;
5220
5221 return nullptr;
5222}
5223
5224/// Given operands for an ExtractValueInst, see if we can fold the result.
5225/// If not, this returns null.
5227 const SimplifyQuery &, unsigned) {
5228 if (auto *CAgg = dyn_cast<Constant>(Agg))
5229 return ConstantFoldExtractValueInstruction(CAgg, Idxs);
5230
5231 // extractvalue x, (insertvalue y, elt, n), n -> elt
5232 unsigned NumIdxs = Idxs.size();
5233 for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
5234 IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
5235 ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
5236 unsigned NumInsertValueIdxs = InsertValueIdxs.size();
5237 unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
5238 if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
5239 Idxs.slice(0, NumCommonIdxs)) {
5240 if (NumIdxs == NumInsertValueIdxs)
5241 return IVI->getInsertedValueOperand();
5242 break;
5243 }
5244 }
5245
5246 return nullptr;
5247}
5248
5250 const SimplifyQuery &Q) {
5251 return ::simplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
5252}
5253
5254/// Given operands for an ExtractElementInst, see if we can fold the result.
5255/// If not, this returns null.
5257 const SimplifyQuery &Q, unsigned) {
5258 auto *VecVTy = cast<VectorType>(Vec->getType());
5259 if (auto *CVec = dyn_cast<Constant>(Vec)) {
5260 if (auto *CIdx = dyn_cast<Constant>(Idx))
5261 return ConstantExpr::getExtractElement(CVec, CIdx);
5262
5263 if (Q.isUndefValue(Vec))
5264 return UndefValue::get(VecVTy->getElementType());
5265 }
5266
5267 // An undef extract index can be arbitrarily chosen to be an out-of-range
5268 // index value, which would result in the instruction being poison.
5269 if (Q.isUndefValue(Idx))
5270 return PoisonValue::get(VecVTy->getElementType());
5271
5272 // If extracting a specified index from the vector, see if we can recursively
5273 // find a previously computed scalar that was inserted into the vector.
5274 if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
5275 // For fixed-length vector, fold into undef if index is out of bounds.
5276 unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
5277 if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
5278 return PoisonValue::get(VecVTy->getElementType());
5279 // Handle case where an element is extracted from a splat.
5280 if (IdxC->getValue().ult(MinNumElts))
5281 if (auto *Splat = getSplatValue(Vec))
5282 return Splat;
5283 if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
5284 return Elt;
5285 } else {
5286 // extractelt x, (insertelt y, elt, n), n -> elt
5287 // If the possibly-variable indices are trivially known to be equal
5288 // (because they are the same operand) then use the value that was
5289 // inserted directly.
5290 auto *IE = dyn_cast<InsertElementInst>(Vec);
5291 if (IE && IE->getOperand(2) == Idx)
5292 return IE->getOperand(1);
5293
5294 // The index is not relevant if our vector is a splat.
5295 if (Value *Splat = getSplatValue(Vec))
5296 return Splat;
5297 }
5298 return nullptr;
5299}
5300
5302 const SimplifyQuery &Q) {
5303 return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
5304}
5305
5306/// See if we can fold the given phi. If not, returns null.
5308 const SimplifyQuery &Q) {
5309 // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE
5310 // here, because the PHI we may succeed simplifying to was not
5311 // def-reachable from the original PHI!
5312
5313 // If all of the PHI's incoming values are the same then replace the PHI node
5314 // with the common value.
5315 Value *CommonValue = nullptr;
5316 bool HasPoisonInput = false;
5317 bool HasUndefInput = false;
5318 for (Value *Incoming : IncomingValues) {
5319 // If the incoming value is the phi node itself, it can safely be skipped.
5320 if (Incoming == PN)
5321 continue;
5322 if (isa<PoisonValue>(Incoming)) {
5323 HasPoisonInput = true;
5324 continue;
5325 }
5326 if (Q.isUndefValue(Incoming)) {
5327 // Remember that we saw an undef value, but otherwise ignore them.
5328 HasUndefInput = true;
5329 continue;
5330 }
5331 if (CommonValue && Incoming != CommonValue)
5332 return nullptr; // Not the same, bail out.
5333 CommonValue = Incoming;
5334 }
5335
5336 // If CommonValue is null then all of the incoming values were either undef,
5337 // poison or equal to the phi node itself.
5338 if (!CommonValue)
5339 return HasUndefInput ? UndefValue::get(PN->getType())
5340 : PoisonValue::get(PN->getType());
5341
5342 if (HasPoisonInput || HasUndefInput) {
5343 // If we have a PHI node like phi(X, undef, X), where X is defined by some
5344 // instruction, we cannot return X as the result of the PHI node unless it
5345 // dominates the PHI block.
5346 if (!valueDominatesPHI(CommonValue, PN, Q.DT))
5347 return nullptr;
5348
5349 // Make sure we do not replace an undef value with poison.
5350 if (HasUndefInput &&
5351 !isGuaranteedNotToBePoison(CommonValue, Q.AC, Q.CxtI, Q.DT))
5352 return nullptr;
5353 return CommonValue;
5354 }
5355
5356 return CommonValue;
5357}
5358
5359static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5360 const SimplifyQuery &Q, unsigned MaxRecurse) {
5361 if (auto *C = dyn_cast<Constant>(Op))
5362 return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
5363
5364 if (auto *CI = dyn_cast<CastInst>(Op)) {
5365 auto *Src = CI->getOperand(0);
5366 Type *SrcTy = Src->getType();
5367 Type *MidTy = CI->getType();
5368 Type *DstTy = Ty;
5369 if (Src->getType() == Ty) {
5370 auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
5371 auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
5372 Type *SrcIntPtrTy =
5373 SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
5374 Type *MidIntPtrTy =
5375 MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
5376 Type *DstIntPtrTy =
5377 DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
5378 if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
5379 SrcIntPtrTy, MidIntPtrTy,
5380 DstIntPtrTy) == Instruction::BitCast)
5381 return Src;
5382 }
5383 }
5384
5385 // bitcast x -> x
5386 if (CastOpc == Instruction::BitCast)
5387 if (Op->getType() == Ty)
5388 return Op;
5389
5390 // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
5391 Value *Ptr, *X;
5392 if (CastOpc == Instruction::PtrToInt &&
5395 X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
5396 return X;
5397
5398 return nullptr;
5399}
5400
5401Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5402 const SimplifyQuery &Q) {
5403 return ::simplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
5404}
5405
5406/// For the given destination element of a shuffle, peek through shuffles to
5407/// match a root vector source operand that contains that element in the same
5408/// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
5409static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
5410 int MaskVal, Value *RootVec,
5411 unsigned MaxRecurse) {
5412 if (!MaxRecurse--)
5413 return nullptr;
5414
5415 // Bail out if any mask value is undefined. That kind of shuffle may be
5416 // simplified further based on demanded bits or other folds.
5417 if (MaskVal == -1)
5418 return nullptr;
5419
5420 // The mask value chooses which source operand we need to look at next.
5421 int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
5422 int RootElt = MaskVal;
5423 Value *SourceOp = Op0;
5424 if (MaskVal >= InVecNumElts) {
5425 RootElt = MaskVal - InVecNumElts;
5426 SourceOp = Op1;
5427 }
5428
5429 // If the source operand is a shuffle itself, look through it to find the
5430 // matching root vector.
5431 if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
5432 return foldIdentityShuffles(
5433 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
5434 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
5435 }
5436
5437 // The source operand is not a shuffle. Initialize the root vector value for
5438 // this shuffle if that has not been done yet.
5439 if (!RootVec)
5440 RootVec = SourceOp;
5441
5442 // Give up as soon as a source operand does not match the existing root value.
5443 if (RootVec != SourceOp)
5444 return nullptr;
5445
5446 // The element must be coming from the same lane in the source vector
5447 // (although it may have crossed lanes in intermediate shuffles).
5448 if (RootElt != DestElt)
5449 return nullptr;
5450
5451 return RootVec;
5452}
5453
5455 ArrayRef<int> Mask, Type *RetTy,
5456 const SimplifyQuery &Q,
5457 unsigned MaxRecurse) {
5458 if (all_of(Mask, [](int Elem) { return Elem == PoisonMaskElem; }))
5459 return PoisonValue::get(RetTy);
5460
5461 auto *InVecTy = cast<VectorType>(Op0->getType());
5462 unsigned MaskNumElts = Mask.size();
5463 ElementCount InVecEltCount = InVecTy->getElementCount();
5464
5465 bool Scalable = InVecEltCount.isScalable();
5466
5467 SmallVector<int, 32> Indices;
5468 Indices.assign(Mask.begin(), Mask.end());
5469
5470 // Canonicalization: If mask does not select elements from an input vector,
5471 // replace that input vector with poison.
5472 if (!Scalable) {
5473 bool MaskSelects0 = false, MaskSelects1 = false;
5474 unsigned InVecNumElts = InVecEltCount.getKnownMinValue();
5475 for (unsigned i = 0; i != MaskNumElts; ++i) {
5476 if (Indices[i] == -1)
5477 continue;
5478 if ((unsigned)Indices[i] < InVecNumElts)
5479 MaskSelects0 = true;
5480 else
5481 MaskSelects1 = true;
5482 }
5483 if (!MaskSelects0)
5484 Op0 = PoisonValue::get(InVecTy);
5485 if (!MaskSelects1)
5486 Op1 = PoisonValue::get(InVecTy);
5487 }
5488
5489 auto *Op0Const = dyn_cast<Constant>(Op0);
5490 auto *Op1Const = dyn_cast<Constant>(Op1);
5491
5492 // If all operands are constant, constant fold the shuffle. This
5493 // transformation depends on the value of the mask which is not known at
5494 // compile time for scalable vectors
5495 if (Op0Const && Op1Const)
5496 return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask);
5497
5498 // Canonicalization: if only one input vector is constant, it shall be the
5499 // second one. This transformation depends on the value of the mask which
5500 // is not known at compile time for scalable vectors
5501 if (!Scalable && Op0Const && !Op1Const) {
5502 std::swap(Op0, Op1);
5504 InVecEltCount.getKnownMinValue());
5505 }
5506
5507 // A splat of an inserted scalar constant becomes a vector constant:
5508 // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
5509 // NOTE: We may have commuted above, so analyze the updated Indices, not the
5510 // original mask constant.
5511 // NOTE: This transformation depends on the value of the mask which is not
5512 // known at compile time for scalable vectors
5513 Constant *C;
5514 ConstantInt *IndexC;
5515 if (!Scalable && match(Op0, m_InsertElt(m_Value(), m_Constant(C),
5516 m_ConstantInt(IndexC)))) {
5517 // Match a splat shuffle mask of the insert index allowing undef elements.
5518 int InsertIndex = IndexC->getZExtValue();
5519 if (all_of(Indices, [InsertIndex](int MaskElt) {
5520 return MaskElt == InsertIndex || MaskElt == -1;
5521 })) {
5522 assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat");
5523
5524 // Shuffle mask poisons become poison constant result elements.
5525 SmallVector<Constant *, 16> VecC(MaskNumElts, C);
5526 for (unsigned i = 0; i != MaskNumElts; ++i)
5527 if (Indices[i] == -1)
5528 VecC[i] = PoisonValue::get(C->getType());
5529 return ConstantVector::get(VecC);
5530 }
5531 }
5532
5533 // A shuffle of a splat is always the splat itself. Legal if the shuffle's
5534 // value type is same as the input vectors' type.
5535 if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
5536 if (Q.isUndefValue(Op1) && RetTy == InVecTy &&
5537 all_equal(OpShuf->getShuffleMask()))
5538 return Op0;
5539
5540 // All remaining transformation depend on the value of the mask, which is
5541 // not known at compile time for scalable vectors.
5542 if (Scalable)
5543 return nullptr;
5544
5545 // Don't fold a shuffle with undef mask elements. This may get folded in a
5546 // better way using demanded bits or other analysis.
5547 // TODO: Should we allow this?
5548 if (is_contained(Indices, -1))
5549 return nullptr;
5550
5551 // Check if every element of this shuffle can be mapped back to the
5552 // corresponding element of a single root vector. If so, we don't need this
5553 // shuffle. This handles simple identity shuffles as well as chains of
5554 // shuffles that may widen/narrow and/or move elements across lanes and back.
5555 Value *RootVec = nullptr;
5556 for (unsigned i = 0; i != MaskNumElts; ++i) {
5557 // Note that recursion is limited for each vector element, so if any element
5558 // exceeds the limit, this will fail to simplify.
5559 RootVec =
5560 foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse);
5561
5562 // We can't replace a widening/narrowing shuffle with one of its operands.
5563 if (!RootVec || RootVec->getType() != RetTy)
5564 return nullptr;
5565 }
5566 return RootVec;
5567}
5568
5569/// Given operands for a ShuffleVectorInst, fold the result or return null.
5571 ArrayRef<int> Mask, Type *RetTy,
5572 const SimplifyQuery &Q) {
5573 return ::simplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit);
5574}
5575
5577 const SimplifyQuery &Q) {
5578 if (auto *C = dyn_cast<Constant>(Op))
5579 return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL);
5580 return nullptr;
5581}
5582
5583/// Given the operand for an FNeg, see if we can fold the result. If not, this
5584/// returns null.
5586 const SimplifyQuery &Q, unsigned MaxRecurse) {
5587 if (Constant *C = foldConstant(Instruction::FNeg, Op, Q))
5588 return C;
5589
5590 Value *X;
5591 // fneg (fneg X) ==> X
5592 if (match(Op, m_FNeg(m_Value(X))))
5593 return X;
5594
5595 return nullptr;
5596}
5597
5599 const SimplifyQuery &Q) {
5600 return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit);
5601}
5602
5603/// Try to propagate existing NaN values when possible. If not, replace the
5604/// constant or elements in the constant with a canonical NaN.
5606 Type *Ty = In->getType();
5607 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
5608 unsigned NumElts = VecTy->getNumElements();
5609 SmallVector<Constant *, 32> NewC(NumElts);
5610 for (unsigned i = 0; i != NumElts; ++i) {
5611 Constant *EltC = In->getAggregateElement(i);
5612 // Poison elements propagate. NaN propagates except signaling is quieted.
5613 // Replace unknown or undef elements with canonical NaN.
5614 if (EltC && isa<PoisonValue>(EltC))
5615 NewC[i] = EltC;
5616 else if (EltC && EltC->isNaN())
5617 NewC[i] = ConstantFP::get(
5618 EltC->getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet());
5619 else
5620 NewC[i] = ConstantFP::getNaN(VecTy->getElementType());
5621 }
5622 return ConstantVector::get(NewC);
5623 }
5624
5625 // If it is not a fixed vector, but not a simple NaN either, return a
5626 // canonical NaN.
5627 if (!In->isNaN())
5628 return ConstantFP::getNaN(Ty);
5629
5630 // If we known this is a NaN, and it's scalable vector, we must have a splat
5631 // on our hands. Grab that before splatting a QNaN constant.
5632 if (isa<ScalableVectorType>(Ty)) {
5633 auto *Splat = In->getSplatValue();
5634 assert(Splat && Splat->isNaN() &&
5635 "Found a scalable-vector NaN but not a splat");
5636 In = Splat;
5637 }
5638
5639 // Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but
5640 // preserve the sign/payload.
5641 return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet());
5642}
5643
5644/// Perform folds that are common to any floating-point operation. This implies
5645/// transforms based on poison/undef/NaN because the operation itself makes no
5646/// difference to the result.
5648 const SimplifyQuery &Q,
5649 fp::ExceptionBehavior ExBehavior,
5650 RoundingMode Rounding) {
5651 // Poison is independent of anything else. It always propagates from an
5652 // operand to a math result.
5653 if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
5654 return PoisonValue::get(Ops[0]->getType());
5655
5656 for (Value *V : Ops) {
5657 bool IsNan = match(V, m_NaN());
5658 bool IsInf = match(V, m_Inf());
5659 bool IsUndef = Q.isUndefValue(V);
5660
5661 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
5662 // (an undef operand can be chosen to be Nan/Inf), then the result of
5663 // this operation is poison.
5664 if (FMF.noNaNs() && (IsNan || IsUndef))
5665 return PoisonValue::get(V->getType());
5666 if (FMF.noInfs() && (IsInf || IsUndef))
5667 return PoisonValue::get(V->getType());
5668
5669 if (isDefaultFPEnvironment(ExBehavior, Rounding)) {
5670 // Undef does not propagate because undef means that all bits can take on
5671 // any value. If this is undef * NaN for example, then the result values
5672 // (at least the exponent bits) are limited. Assume the undef is a
5673 // canonical NaN and propagate that.
5674 if (IsUndef)
5675 return ConstantFP::getNaN(V->getType());
5676 if (IsNan)
5677 return propagateNaN(cast<Constant>(V));
5678 } else if (ExBehavior != fp::ebStrict) {
5679 if (IsNan)
5680 return propagateNaN(cast<Constant>(V));
5681 }
5682 }
5683 return nullptr;
5684}
5685
5686/// Given operands for an FAdd, see if we can fold the result. If not, this
5687/// returns null.
5688static Value *
5690 const SimplifyQuery &Q, unsigned MaxRecurse,
5692 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5693 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5694 if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
5695 return C;
5696
5697 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5698 return C;
5699
5700 // fadd X, -0 ==> X
5701 // With strict/constrained FP, we have these possible edge cases that do
5702 // not simplify to Op0:
5703 // fadd SNaN, -0.0 --> QNaN
5704 // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
5705 if (canIgnoreSNaN(ExBehavior, FMF) &&
5706 (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5707 FMF.noSignedZeros()))
5708 if (match(Op1, m_NegZeroFP()))
5709 return Op0;
5710
5711 // fadd X, 0 ==> X, when we know X is not -0
5712 if (canIgnoreSNaN(ExBehavior, FMF))
5713 if (match(Op1, m_PosZeroFP()) &&
5714 (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, /*Depth=*/0, Q)))
5715 return Op0;
5716
5717 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5718 return nullptr;
5719
5720 if (FMF.noNaNs()) {
5721 // With nnan: X + {+/-}Inf --> {+/-}Inf
5722 if (match(Op1, m_Inf()))
5723 return Op1;
5724
5725 // With nnan: -X + X --> 0.0 (and commuted variant)
5726 // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
5727 // Negative zeros are allowed because we always end up with positive zero:
5728 // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5729 // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5730 // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
5731 // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
5732 if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
5733 match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
5734 return ConstantFP::getZero(Op0->getType());
5735
5736 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
5737 match(Op1, m_FNeg(m_Specific(Op0))))
5738 return ConstantFP::getZero(Op0->getType());
5739 }
5740
5741 // (X - Y) + Y --> X
5742 // Y + (X - Y) --> X
5743 Value *X;
5744 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5745 (match(Op0, m_FSub(m_Value(X), m_Specific(Op1))) ||
5746 match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))))
5747 return X;
5748
5749 return nullptr;
5750}
5751
5752/// Given operands for an FSub, see if we can fold the result. If not, this
5753/// returns null.
5754static Value *
5756 const SimplifyQuery &Q, unsigned MaxRecurse,
5758 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5759 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5760 if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
5761 return C;
5762
5763 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5764 return C;
5765
5766 // fsub X, +0 ==> X
5767 if (canIgnoreSNaN(ExBehavior, FMF) &&
5768 (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5769 FMF.noSignedZeros()))
5770 if (match(Op1, m_PosZeroFP()))
5771 return Op0;
5772
5773 // fsub X, -0 ==> X, when we know X is not -0
5774 if (canIgnoreSNaN(ExBehavior, FMF))
5775 if (match(Op1, m_NegZeroFP()) &&
5776 (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, /*Depth=*/0, Q)))
5777 return Op0;
5778
5779 // fsub -0.0, (fsub -0.0, X) ==> X
5780 // fsub -0.0, (fneg X) ==> X
5781 Value *X;
5782 if (canIgnoreSNaN(ExBehavior, FMF))
5783 if (match(Op0, m_NegZeroFP()) && match(Op1, m_FNeg(m_Value(X))))
5784 return X;
5785
5786 // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
5787 // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
5788 if (canIgnoreSNaN(ExBehavior, FMF))
5789 if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()) &&
5790 (match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X))) ||
5791 match(Op1, m_FNeg(m_Value(X)))))
5792 return X;
5793
5794 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5795 return nullptr;
5796
5797 if (FMF.noNaNs()) {
5798 // fsub nnan x, x ==> 0.0
5799 if (Op0 == Op1)
5800 return Constant::getNullValue(Op0->getType());
5801
5802 // With nnan: {+/-}Inf - X --> {+/-}Inf
5803 if (match(Op0, m_Inf()))
5804 return Op0;
5805
5806 // With nnan: X - {+/-}Inf --> {-/+}Inf
5807 if (match(Op1, m_Inf()))
5808 return foldConstant(Instruction::FNeg, Op1, Q);
5809 }
5810
5811 // Y - (Y - X) --> X
5812 // (X + Y) - Y --> X
5813 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5814 (match(Op1, m_FSub(m_Specific(Op0), m_Value(X))) ||
5815 match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))))
5816 return X;
5817
5818 return nullptr;
5819}
5820
5822 const SimplifyQuery &Q, unsigned MaxRecurse,
5823 fp::ExceptionBehavior ExBehavior,
5824 RoundingMode Rounding) {
5825 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5826 return C;
5827
5828 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5829 return nullptr;
5830
5831 // Canonicalize special constants as operand 1.
5832 if (match(Op0, m_FPOne()) || match(Op0, m_AnyZeroFP()))
5833 std::swap(Op0, Op1);
5834
5835 // X * 1.0 --> X
5836 if (match(Op1, m_FPOne()))
5837 return Op0;
5838
5839 if (match(Op1, m_AnyZeroFP())) {
5840 // X * 0.0 --> 0.0 (with nnan and nsz)
5841 if (FMF.noNaNs() && FMF.noSignedZeros())
5842 return ConstantFP::getZero(Op0->getType());
5843
5844 KnownFPClass Known =
5845 computeKnownFPClass(Op0, FMF, fcInf | fcNan, /*Depth=*/0, Q);
5846 if (Known.isKnownNever(fcInf | fcNan)) {
5847 // +normal number * (-)0.0 --> (-)0.0
5848 if (Known.SignBit == false)
5849 return Op1;
5850 // -normal number * (-)0.0 --> -(-)0.0
5851 if (Known.SignBit == true)
5852 return foldConstant(Instruction::FNeg, Op1, Q);
5853 }
5854 }
5855
5856 // sqrt(X) * sqrt(X) --> X, if we can:
5857 // 1. Remove the intermediate rounding (reassociate).
5858 // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
5859 // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
5860 Value *X;
5861 if (Op0 == Op1 && match(Op0, m_Sqrt(m_Value(X))) && FMF.allowReassoc() &&
5862 FMF.noNaNs() && FMF.noSignedZeros())
5863 return X;
5864
5865 return nullptr;
5866}
5867
5868/// Given the operands for an FMul, see if we can fold the result
5869static Value *
5871 const SimplifyQuery &Q, unsigned MaxRecurse,
5873 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5874 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5875 if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
5876 return C;
5877
5878 // Now apply simplifications that do not require rounding.
5879 return simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
5880}
5881
5883 const SimplifyQuery &Q,
5884 fp::ExceptionBehavior ExBehavior,
5885 RoundingMode Rounding) {
5886 return ::simplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5887 Rounding);
5888}
5889
5891 const SimplifyQuery &Q,
5892 fp::ExceptionBehavior ExBehavior,
5893 RoundingMode Rounding) {
5894 return ::simplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5895 Rounding);
5896}
5897
5899 const SimplifyQuery &Q,
5900 fp::ExceptionBehavior ExBehavior,
5901 RoundingMode Rounding) {
5902 return ::simplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5903 Rounding);
5904}
5905
5907 const SimplifyQuery &Q,
5908 fp::ExceptionBehavior ExBehavior,
5909 RoundingMode Rounding) {
5910 return ::simplifyFMAFMul(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5911 Rounding);
5912}
5913
5914static Value *
5916 const SimplifyQuery &Q, unsigned,
5918 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5919 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5920 if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
5921 return C;
5922
5923 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5924 return C;
5925
5926 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5927 return nullptr;
5928
5929 // X / 1.0 -> X
5930 if (match(Op1, m_FPOne()))
5931 return Op0;
5932
5933 // 0 / X -> 0
5934 // Requires that NaNs are off (X could be zero) and signed zeroes are
5935 // ignored (X could be positive or negative, so the output sign is unknown).
5936 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
5937 return ConstantFP::getZero(Op0->getType());
5938
5939 if (FMF.noNaNs()) {
5940 // X / X -> 1.0 is legal when NaNs are ignored.
5941 // We can ignore infinities because INF/INF is NaN.
5942 if (Op0 == Op1)
5943 return ConstantFP::get(Op0->getType(), 1.0);
5944
5945 // (X * Y) / Y --> X if we can reassociate to the above form.
5946 Value *X;
5947 if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1))))
5948 return X;
5949
5950 // -X / X -> -1.0 and
5951 // X / -X -> -1.0 are legal when NaNs are ignored.
5952 // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
5953 if (match(Op0, m_FNegNSZ(m_Specific(Op1))) ||
5954 match(Op1, m_FNegNSZ(m_Specific(Op0))))
5955 return ConstantFP::get(Op0->getType(), -1.0);
5956
5957 // nnan ninf X / [-]0.0 -> poison
5958 if (FMF.noInfs() && match(Op1, m_AnyZeroFP()))
5959 return PoisonValue::get(Op1->getType());
5960 }
5961
5962 return nullptr;
5963}
5964
5966 const SimplifyQuery &Q,
5967 fp::ExceptionBehavior ExBehavior,
5968 RoundingMode Rounding) {
5969 return ::simplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5970 Rounding);
5971}
5972
5973static Value *
5975 const SimplifyQuery &Q, unsigned,
5977 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5978 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5979 if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
5980 return C;
5981
5982 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5983 return C;
5984
5985 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5986 return nullptr;
5987
5988 // Unlike fdiv, the result of frem always matches the sign of the dividend.
5989 // The constant match may include undef elements in a vector, so return a full
5990 // zero constant as the result.
5991 if (FMF.noNaNs()) {
5992 // +0 % X -> 0
5993 if (match(Op0, m_PosZeroFP()))
5994 return ConstantFP::getZero(Op0->getType());
5995 // -0 % X -> -0
5996 if (match(Op0, m_NegZeroFP()))
5997 return ConstantFP::getNegativeZero(Op0->getType());
5998 }
5999
6000 return nullptr;
6001}
6002
6004 const SimplifyQuery &Q,
6005 fp::ExceptionBehavior ExBehavior,
6006 RoundingMode Rounding) {
6007 return ::simplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
6008 Rounding);
6009}
6010
6011//=== Helper functions for higher up the class hierarchy.
6012
6013/// Given the operand for a UnaryOperator, see if we can fold the result.
6014/// If not, this returns null.
6015static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
6016 unsigned MaxRecurse) {
6017 switch (Opcode) {
6018 case Instruction::FNeg:
6019 return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse);
6020 default:
6021 llvm_unreachable("Unexpected opcode");
6022 }
6023}
6024
6025/// Given the operand for a UnaryOperator, see if we can fold the result.
6026/// If not, this returns null.
6027/// Try to use FastMathFlags when folding the result.
6028static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
6029 const FastMathFlags &FMF, const SimplifyQuery &Q,
6030 unsigned MaxRecurse) {
6031 switch (Opcode) {
6032 case Instruction::FNeg:
6033 return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
6034 default:
6035 return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
6036 }
6037}
6038
6039Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
6040 return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit);
6041}
6042
6044 const SimplifyQuery &Q) {
6045 return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit);
6046}
6047
6048/// Given operands for a BinaryOperator, see if we can fold the result.
6049/// If not, this returns null.
6050static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6051 const SimplifyQuery &Q, unsigned MaxRecurse) {
6052 switch (Opcode) {
6053 case Instruction::Add:
6054 return simplifyAddInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6055 MaxRecurse);
6056 case Instruction::Sub:
6057 return simplifySubInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6058 MaxRecurse);
6059 case Instruction::Mul:
6060 return simplifyMulInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6061 MaxRecurse);
6062 case Instruction::SDiv:
6063 return simplifySDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6064 case Instruction::UDiv:
6065 return simplifyUDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6066 case Instruction::SRem:
6067 return simplifySRemInst(LHS, RHS, Q, MaxRecurse);
6068 case Instruction::URem:
6069 return simplifyURemInst(LHS, RHS, Q, MaxRecurse);
6070 case Instruction::Shl:
6071 return simplifyShlInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6072 MaxRecurse);
6073 case Instruction::LShr:
6074 return simplifyLShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6075 case Instruction::AShr:
6076 return simplifyAShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6077 case Instruction::And:
6078 return simplifyAndInst(LHS, RHS, Q, MaxRecurse);
6079 case Instruction::Or:
6080 return simplifyOrInst(LHS, RHS, Q, MaxRecurse);
6081 case Instruction::Xor:
6082 return simplifyXorInst(LHS, RHS, Q, MaxRecurse);
6083 case Instruction::FAdd:
6084 return simplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6085 case Instruction::FSub:
6086 return simplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6087 case Instruction::FMul:
6088 return simplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6089 case Instruction::FDiv:
6090 return simplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6091 case Instruction::FRem:
6092 return simplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6093 default:
6094 llvm_unreachable("Unexpected opcode");
6095 }
6096}
6097
6098/// Given operands for a BinaryOperator, see if we can fold the result.
6099/// If not, this returns null.
6100/// Try to use FastMathFlags when folding the result.
6101static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6102 const FastMathFlags &FMF, const SimplifyQuery &Q,
6103 unsigned MaxRecurse) {
6104 switch (Opcode) {
6105 case Instruction::FAdd:
6106 return simplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
6107 case Instruction::FSub:
6108 return simplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
6109 case Instruction::FMul:
6110 return simplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
6111 case Instruction::FDiv:
6112 return simplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
6113 default:
6114 return simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
6115 }
6116}
6117
6118Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6119 const SimplifyQuery &Q) {
6120 return ::simplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit);
6121}
6122
6123Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6124 FastMathFlags FMF, const SimplifyQuery &Q) {
6125 return ::simplifyBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit);
6126}
6127
6128/// Given operands for a CmpInst, see if we can fold the result.
6130 const SimplifyQuery &Q, unsigned MaxRecurse) {
6132 return simplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
6133 return simplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6134}
6135
6137 const SimplifyQuery &Q) {
6138 return ::simplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
6139}
6140
6142 switch (ID) {
6143 default:
6144 return false;
6145
6146 // Unary idempotent: f(f(x)) = f(x)
6147 case Intrinsic::fabs:
6148 case Intrinsic::floor:
6149 case Intrinsic::ceil:
6150 case Intrinsic::trunc:
6151 case Intrinsic::rint:
6152 case Intrinsic::nearbyint:
6153 case Intrinsic::round:
6154 case Intrinsic::roundeven:
6155 case Intrinsic::canonicalize:
6156 case Intrinsic::arithmetic_fence:
6157 return true;
6158 }
6159}
6160
6161/// Return true if the intrinsic rounds a floating-point value to an integral
6162/// floating-point value (not an integer type).
6164 switch (ID) {
6165 default:
6166 return false;
6167
6168 case Intrinsic::floor:
6169 case Intrinsic::ceil:
6170 case Intrinsic::trunc:
6171 case Intrinsic::rint:
6172 case Intrinsic::nearbyint:
6173 case Intrinsic::round:
6174 case Intrinsic::roundeven:
6175 return true;
6176 }
6177}
6178
6180 const DataLayout &DL) {
6181 GlobalValue *PtrSym;
6182 APInt PtrOffset;
6183 if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
6184 return nullptr;
6185
6186 Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
6187
6188 auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
6189 if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
6190 return nullptr;
6191
6192 APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc(
6193 DL.getIndexTypeSizeInBits(Ptr->getType()));
6194 if (OffsetInt.srem(4) != 0)
6195 return nullptr;
6196
6197 Constant *Loaded =
6198 ConstantFoldLoadFromConstPtr(Ptr, Int32Ty, std::move(OffsetInt), DL);
6199 if (!Loaded)
6200 return nullptr;
6201
6202 auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
6203 if (!LoadedCE)
6204 return nullptr;
6205
6206 if (LoadedCE->getOpcode() == Instruction::Trunc) {
6207 LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6208 if (!LoadedCE)
6209 return nullptr;
6210 }
6211
6212 if (LoadedCE->getOpcode() != Instruction::Sub)
6213 return nullptr;
6214
6215 auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6216 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
6217 return nullptr;
6218 auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
6219
6220 Constant *LoadedRHS = LoadedCE->getOperand(1);
6221 GlobalValue *LoadedRHSSym;
6222 APInt LoadedRHSOffset;
6223 if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
6224 DL) ||
6225 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
6226 return nullptr;
6227
6228 return LoadedLHSPtr;
6229}
6230
6231// TODO: Need to pass in FastMathFlags
6232static Value *simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q,
6233 bool IsStrict) {
6234 // ldexp(poison, x) -> poison
6235 // ldexp(x, poison) -> poison
6236 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6237 return Op0;
6238
6239 // ldexp(undef, x) -> nan
6240 if (Q.isUndefValue(Op0))
6241 return ConstantFP::getNaN(Op0->getType());
6242
6243 if (!IsStrict) {
6244 // TODO: Could insert a canonicalize for strict
6245
6246 // ldexp(x, undef) -> x
6247 if (Q.isUndefValue(Op1))
6248 return Op0;
6249 }
6250
6251 const APFloat *C = nullptr;
6253
6254 // These cases should be safe, even with strictfp.
6255 // ldexp(0.0, x) -> 0.0
6256 // ldexp(-0.0, x) -> -0.0
6257 // ldexp(inf, x) -> inf
6258 // ldexp(-inf, x) -> -inf
6259 if (C && (C->isZero() || C->isInfinity()))
6260 return Op0;
6261
6262 // These are canonicalization dropping, could do it if we knew how we could
6263 // ignore denormal flushes and target handling of nan payload bits.
6264 if (IsStrict)
6265 return nullptr;
6266
6267 // TODO: Could quiet this with strictfp if the exception mode isn't strict.
6268 if (C && C->isNaN())
6269 return ConstantFP::get(Op0->getType(), C->makeQuiet());
6270
6271 // ldexp(x, 0) -> x
6272
6273 // TODO: Could fold this if we know the exception mode isn't
6274 // strict, we know the denormal mode and other target modes.
6275 if (match(Op1, PatternMatch::m_ZeroInt()))
6276 return Op0;
6277
6278 return nullptr;
6279}
6280
6282 const SimplifyQuery &Q,
6283 const CallBase *Call) {
6284 // Idempotent functions return the same result when called repeatedly.
6285 Intrinsic::ID IID = F->getIntrinsicID();
6286 if (isIdempotent(IID))
6287 if (auto *II = dyn_cast<IntrinsicInst>(Op0))
6288 if (II->getIntrinsicID() == IID)
6289 return II;
6290
6291 if (removesFPFraction(IID)) {
6292 // Converting from int or calling a rounding function always results in a
6293 // finite integral number or infinity. For those inputs, rounding functions
6294 // always return the same value, so the (2nd) rounding is eliminated. Ex:
6295 // floor (sitofp x) -> sitofp x
6296 // round (ceil x) -> ceil x
6297 auto *II = dyn_cast<IntrinsicInst>(Op0);
6298 if ((II && removesFPFraction(II->getIntrinsicID())) ||
6299 match(Op0, m_SIToFP(m_Value())) || match(Op0, m_UIToFP(m_Value())))
6300 return Op0;
6301 }
6302
6303 Value *X;
6304 switch (IID) {
6305 case Intrinsic::fabs:
6306 if (computeKnownFPSignBit(Op0, /*Depth=*/0, Q) == false)
6307 return Op0;
6308 break;
6309 case Intrinsic::bswap:
6310 // bswap(bswap(x)) -> x
6311 if (match(Op0, m_BSwap(m_Value(X))))
6312 return X;
6313 break;
6314 case Intrinsic::bitreverse:
6315 // bitreverse(bitreverse(x)) -> x
6316 if (match(Op0, m_BitReverse(m_Value(X))))
6317 return X;
6318 break;
6319 case Intrinsic::ctpop: {
6320 // ctpop(X) -> 1 iff X is non-zero power of 2.
6321 if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ false, 0, Q.AC, Q.CxtI,
6322 Q.DT))
6323 return ConstantInt::get(Op0->getType(), 1);
6324 // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
6325 // ctpop(and X, 1) --> and X, 1
6326 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6328 Q))
6329 return Op0;
6330 break;
6331 }
6332 case Intrinsic::exp:
6333 // exp(log(x)) -> x
6334 if (Call->hasAllowReassoc() &&
6335 match(Op0, m_Intrinsic<Intrinsic::log>(m_Value(X))))
6336 return X;
6337 break;
6338 case Intrinsic::exp2:
6339 // exp2(log2(x)) -> x
6340 if (Call->hasAllowReassoc() &&
6341 match(Op0, m_Intrinsic<Intrinsic::log2>(m_Value(X))))
6342 return X;
6343 break;
6344 case Intrinsic::exp10:
6345 // exp10(log10(x)) -> x
6346 if (Call->hasAllowReassoc() &&
6347 match(Op0, m_Intrinsic<Intrinsic::log10>(m_Value(X))))
6348 return X;
6349 break;
6350 case Intrinsic::log:
6351 // log(exp(x)) -> x
6352 if (Call->hasAllowReassoc() &&
6353 match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))))
6354 return X;
6355 break;
6356 case Intrinsic::log2:
6357 // log2(exp2(x)) -> x
6358 if (Call->hasAllowReassoc() &&
6359 (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) ||
6360 match(Op0,
6361 m_Intrinsic<Intrinsic::pow>(m_SpecificFP(2.0), m_Value(X)))))
6362 return X;
6363 break;
6364 case Intrinsic::log10:
6365 // log10(pow(10.0, x)) -> x
6366 // log10(exp10(x)) -> x
6367 if (Call->hasAllowReassoc() &&
6368 (match(Op0, m_Intrinsic<Intrinsic::exp10>(m_Value(X))) ||
6369 match(Op0,
6370 m_Intrinsic<Intrinsic::pow>(m_SpecificFP(10.0), m_Value(X)))))
6371 return X;
6372 break;
6373 case Intrinsic::vector_reverse:
6374 // vector.reverse(vector.reverse(x)) -> x
6375 if (match(Op0, m_VecReverse(m_Value(X))))
6376 return X;
6377 // vector.reverse(splat(X)) -> splat(X)
6378 if (isSplatValue(Op0))
6379 return Op0;
6380 break;
6381 case Intrinsic::frexp: {
6382 // Frexp is idempotent with the added complication of the struct return.
6383 if (match(Op0, m_ExtractValue<0>(m_Value(X)))) {
6384 if (match(X, m_Intrinsic<Intrinsic::frexp>(m_Value())))
6385 return X;
6386 }
6387
6388 break;
6389 }
6390 default:
6391 break;
6392 }
6393
6394 return nullptr;
6395}
6396
6397/// Given a min/max intrinsic, see if it can be removed based on having an
6398/// operand that is another min/max intrinsic with shared operand(s). The caller
6399/// is expected to swap the operand arguments to handle commutation.
6401 Value *X, *Y;
6402 if (!match(Op0, m_MaxOrMin(m_Value(X), m_Value(Y))))
6403 return nullptr;
6404
6405 auto *MM0 = dyn_cast<IntrinsicInst>(Op0);
6406 if (!MM0)
6407 return nullptr;
6408 Intrinsic::ID IID0 = MM0->getIntrinsicID();
6409
6410 if (Op1 == X || Op1 == Y ||
6412 // max (max X, Y), X --> max X, Y
6413 if (IID0 == IID)
6414 return MM0;
6415 // max (min X, Y), X --> X
6416 if (IID0 == getInverseMinMaxIntrinsic(IID))
6417 return Op1;
6418 }
6419 return nullptr;
6420}
6421
6422/// Given a min/max intrinsic, see if it can be removed based on having an
6423/// operand that is another min/max intrinsic with shared operand(s). The caller
6424/// is expected to swap the operand arguments to handle commutation.
6426 Value *Op1) {
6427 assert((IID == Intrinsic::maxnum || IID == Intrinsic::minnum ||
6428 IID == Intrinsic::maximum || IID == Intrinsic::minimum) &&
6429 "Unsupported intrinsic");
6430
6431 auto *M0 = dyn_cast<IntrinsicInst>(Op0);
6432 // If Op0 is not the same intrinsic as IID, do not process.
6433 // This is a difference with integer min/max handling. We do not process the
6434 // case like max(min(X,Y),min(X,Y)) => min(X,Y). But it can be handled by GVN.
6435 if (!M0 || M0->getIntrinsicID() != IID)
6436 return nullptr;
6437 Value *X0 = M0->getOperand(0);
6438 Value *Y0 = M0->getOperand(1);
6439 // Simple case, m(m(X,Y), X) => m(X, Y)
6440 // m(m(X,Y), Y) => m(X, Y)
6441 // For minimum/maximum, X is NaN => m(NaN, Y) == NaN and m(NaN, NaN) == NaN.
6442 // For minimum/maximum, Y is NaN => m(X, NaN) == NaN and m(NaN, NaN) == NaN.
6443 // For minnum/maxnum, X is NaN => m(NaN, Y) == Y and m(Y, Y) == Y.
6444 // For minnum/maxnum, Y is NaN => m(X, NaN) == X and m(X, NaN) == X.
6445 if (X0 == Op1 || Y0 == Op1)
6446 return M0;
6447
6448 auto *M1 = dyn_cast<IntrinsicInst>(Op1);
6449 if (!M1)
6450 return nullptr;
6451 Value *X1 = M1->getOperand(0);
6452 Value *Y1 = M1->getOperand(1);
6453 Intrinsic::ID IID1 = M1->getIntrinsicID();
6454 // we have a case m(m(X,Y),m'(X,Y)) taking into account m' is commutative.
6455 // if m' is m or inversion of m => m(m(X,Y),m'(X,Y)) == m(X,Y).
6456 // For minimum/maximum, X is NaN => m(NaN,Y) == m'(NaN, Y) == NaN.
6457 // For minimum/maximum, Y is NaN => m(X,NaN) == m'(X, NaN) == NaN.
6458 // For minnum/maxnum, X is NaN => m(NaN,Y) == m'(NaN, Y) == Y.
6459 // For minnum/maxnum, Y is NaN => m(X,NaN) == m'(X, NaN) == X.
6460 if ((X0 == X1 && Y0 == Y1) || (X0 == Y1 && Y0 == X1))
6461 if (IID1 == IID || getInverseMinMaxIntrinsic(IID1) == IID)
6462 return M0;
6463
6464 return nullptr;
6465}
6466
6468 Value *Op0, Value *Op1,
6469 const SimplifyQuery &Q,
6470 const CallBase *Call) {
6471 unsigned BitWidth = ReturnType->getScalarSizeInBits();
6472 switch (IID) {
6473 case Intrinsic::abs:
6474 // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
6475 // It is always ok to pick the earlier abs. We'll just lose nsw if its only
6476 // on the outer abs.
6477 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(), m_Value())))
6478 return Op0;
6479 break;
6480
6481 case Intrinsic::cttz: {
6482 Value *X;
6483 if (match(Op0, m_Shl(m_One(), m_Value(X))))
6484 return X;
6485 break;
6486 }
6487 case Intrinsic::ctlz: {
6488 Value *X;
6489 if (match(Op0, m_LShr(m_Negative(), m_Value(X))))
6490 return X;
6491 if (match(Op0, m_AShr(m_Negative(), m_Value())))
6492 return Constant::getNullValue(ReturnType);
6493 break;
6494 }
6495 case Intrinsic::ptrmask: {
6496 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6497 return PoisonValue::get(Op0->getType());
6498
6499 // NOTE: We can't apply this simplifications based on the value of Op1
6500 // because we need to preserve provenance.
6501 if (Q.isUndefValue(Op0) || match(Op0, m_Zero()))
6502 return Constant::getNullValue(Op0->getType());
6503
6505 Q.DL.getIndexTypeSizeInBits(Op0->getType()) &&
6506 "Invalid mask width");
6507 // If index-width (mask size) is less than pointer-size then mask is
6508 // 1-extended.
6509 if (match(Op1, m_PtrToInt(m_Specific(Op0))))
6510 return Op0;
6511
6512 // NOTE: We may have attributes associated with the return value of the
6513 // llvm.ptrmask intrinsic that will be lost when we just return the
6514 // operand. We should try to preserve them.
6515 if (match(Op1, m_AllOnes()) || Q.isUndefValue(Op1))
6516 return Op0;
6517
6518 Constant *C;
6519 if (match(Op1, m_ImmConstant(C))) {
6520 KnownBits PtrKnown = computeKnownBits(Op0, /*Depth=*/0, Q);
6521 // See if we only masking off bits we know are already zero due to
6522 // alignment.
6523 APInt IrrelevantPtrBits =
6524 PtrKnown.Zero.zextOrTrunc(C->getType()->getScalarSizeInBits());
6526 Instruction::Or, C, ConstantInt::get(C->getType(), IrrelevantPtrBits),
6527 Q.DL);
6528 if (C != nullptr && C->isAllOnesValue())
6529 return Op0;
6530 }
6531 break;
6532 }
6533 case Intrinsic::smax:
6534 case Intrinsic::smin:
6535 case Intrinsic::umax:
6536 case Intrinsic::umin: {
6537 // If the arguments are the same, this is a no-op.
6538 if (Op0 == Op1)
6539 return Op0;
6540
6541 // Canonicalize immediate constant operand as Op1.
6542 if (match(Op0, m_ImmConstant()))
6543 std::swap(Op0, Op1);
6544
6545 // Assume undef is the limit value.
6546 if (Q.isUndefValue(Op1))
6547 return ConstantInt::get(
6549
6550 const APInt *C;
6551 if (match(Op1, m_APIntAllowPoison(C))) {
6552 // Clamp to limit value. For example:
6553 // umax(i8 %x, i8 255) --> 255
6555 return ConstantInt::get(ReturnType, *C);
6556
6557 // If the constant op is the opposite of the limit value, the other must
6558 // be larger/smaller or equal. For example:
6559 // umin(i8 %x, i8 255) --> %x
6562 return Op0;
6563
6564 // Remove nested call if constant operands allow it. Example:
6565 // max (max X, 7), 5 -> max X, 7
6566 auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0);
6567 if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
6568 // TODO: loosen undef/splat restrictions for vector constants.
6569 Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
6570 const APInt *InnerC;
6571 if ((match(M00, m_APInt(InnerC)) || match(M01, m_APInt(InnerC))) &&
6572 ICmpInst::compare(*InnerC, *C,
6573 ICmpInst::getNonStrictPredicate(
6575 return Op0;
6576 }
6577 }
6578
6579 if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1))
6580 return V;
6581 if (Value *V = foldMinMaxSharedOp(IID, Op1, Op0))
6582 return V;
6583
6584 ICmpInst::Predicate Pred =
6585 ICmpInst::getNonStrictPredicate(MinMaxIntrinsic::getPredicate(IID));
6586 if (isICmpTrue(Pred, Op0, Op1, Q.getWithoutUndef(), RecursionLimit))
6587 return Op0;
6588 if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit))
6589 return Op1;
6590
6591 break;
6592 }
6593 case Intrinsic::scmp:
6594 case Intrinsic::ucmp: {
6595 // Fold to a constant if the relationship between operands can be
6596 // established with certainty
6597 if (isICmpTrue(CmpInst::ICMP_EQ, Op0, Op1, Q, RecursionLimit))
6598 return Constant::getNullValue(ReturnType);
6599
6600 ICmpInst::Predicate PredGT =
6601 IID == Intrinsic::scmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
6602 if (isICmpTrue(PredGT, Op0, Op1, Q, RecursionLimit))
6603 return ConstantInt::get(ReturnType, 1);
6604
6605 ICmpInst::Predicate PredLT =
6606 IID == Intrinsic::scmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
6607 if (isICmpTrue(PredLT, Op0, Op1, Q, RecursionLimit))
6608 return ConstantInt::getSigned(ReturnType, -1);
6609
6610 break;
6611 }
6612 case Intrinsic::usub_with_overflow:
6613 case Intrinsic::ssub_with_overflow:
6614 // X - X -> { 0, false }
6615 // X - undef -> { 0, false }
6616 // undef - X -> { 0, false }
6617 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6618 return Constant::getNullValue(ReturnType);
6619 break;
6620 case Intrinsic::uadd_with_overflow:
6621 case Intrinsic::sadd_with_overflow:
6622 // X + undef -> { -1, false }
6623 // undef + x -> { -1, false }
6624 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) {
6625 return ConstantStruct::get(
6626 cast<StructType>(ReturnType),
6627 {Constant::getAllOnesValue(ReturnType->getStructElementType(0)),
6628 Constant::getNullValue(ReturnType->getStructElementType(1))});
6629 }
6630 break;
6631 case Intrinsic::umul_with_overflow:
6632 case Intrinsic::smul_with_overflow:
6633 // 0 * X -> { 0, false }
6634 // X * 0 -> { 0, false }
6635 if (match(Op0, m_Zero()) || match(Op1, m_Zero()))
6636 return Constant::getNullValue(ReturnType);
6637 // undef * X -> { 0, false }
6638 // X * undef -> { 0, false }
6639 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6640 return Constant::getNullValue(ReturnType);
6641 break;
6642 case Intrinsic::uadd_sat:
6643 // sat(MAX + X) -> MAX
6644 // sat(X + MAX) -> MAX
6645 if (match(Op0, m_AllOnes()) || match(Op1, m_AllOnes()))
6646 return Constant::getAllOnesValue(ReturnType);
6647 [[fallthrough]];
6648 case Intrinsic::sadd_sat:
6649 // sat(X + undef) -> -1
6650 // sat(undef + X) -> -1
6651 // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
6652 // For signed: Assume undef is ~X, in which case X + ~X = -1.
6653 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6654 return Constant::getAllOnesValue(ReturnType);
6655
6656 // X + 0 -> X
6657 if (match(Op1, m_Zero()))
6658 return Op0;
6659 // 0 + X -> X
6660 if (match(Op0, m_Zero()))
6661 return Op1;
6662 break;
6663 case Intrinsic::usub_sat:
6664 // sat(0 - X) -> 0, sat(X - MAX) -> 0
6665 if (match(Op0, m_Zero()) || match(Op1, m_AllOnes()))
6666 return Constant::getNullValue(ReturnType);
6667 [[fallthrough]];
6668 case Intrinsic::ssub_sat:
6669 // X - X -> 0, X - undef -> 0, undef - X -> 0
6670 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6671 return Constant::getNullValue(ReturnType);
6672 // X - 0 -> X
6673 if (match(Op1, m_Zero()))
6674 return Op0;
6675 break;
6676 case Intrinsic::load_relative:
6677 if (auto *C0 = dyn_cast<Constant>(Op0))
6678 if (auto *C1 = dyn_cast<Constant>(Op1))
6679 return simplifyRelativeLoad(C0, C1, Q.DL);
6680 break;
6681 case Intrinsic::powi:
6682 if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
6683 // powi(x, 0) -> 1.0
6684 if (Power->isZero())
6685 return ConstantFP::get(Op0->getType(), 1.0);
6686 // powi(x, 1) -> x
6687 if (Power->isOne())
6688 return Op0;
6689 }
6690 break;
6691 case Intrinsic::ldexp:
6692 return simplifyLdexp(Op0, Op1, Q, false);
6693 case Intrinsic::copysign:
6694 // copysign X, X --> X
6695 if (Op0 == Op1)
6696 return Op0;
6697 // copysign -X, X --> X
6698 // copysign X, -X --> -X
6699 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
6700 match(Op1, m_FNeg(m_Specific(Op0))))
6701 return Op1;
6702 break;
6703 case Intrinsic::is_fpclass: {
6704 if (isa<PoisonValue>(Op0))
6705 return PoisonValue::get(ReturnType);
6706
6707 uint64_t Mask = cast<ConstantInt>(Op1)->getZExtValue();
6708 // If all tests are made, it doesn't matter what the value is.
6709 if ((Mask & fcAllFlags) == fcAllFlags)
6710 return ConstantInt::get(ReturnType, true);
6711 if ((Mask & fcAllFlags) == 0)
6712 return ConstantInt::get(ReturnType, false);
6713 if (Q.isUndefValue(Op0))
6714 return UndefValue::get(ReturnType);
6715 break;
6716 }
6717 case Intrinsic::maxnum:
6718 case Intrinsic::minnum:
6719 case Intrinsic::maximum:
6720 case Intrinsic::minimum: {
6721 // If the arguments are the same, this is a no-op.
6722 if (Op0 == Op1)
6723 return Op0;
6724
6725 // Canonicalize constant operand as Op1.
6726 if (isa<Constant>(Op0))
6727 std::swap(Op0, Op1);
6728
6729 // If an argument is undef, return the other argument.
6730 if (Q.isUndefValue(Op1))
6731 return Op0;
6732
6733 bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
6734 bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum;
6735
6736 // minnum(X, nan) -> X
6737 // maxnum(X, nan) -> X
6738 // minimum(X, nan) -> nan
6739 // maximum(X, nan) -> nan
6740 if (match(Op1, m_NaN()))
6741 return PropagateNaN ? propagateNaN(cast<Constant>(Op1)) : Op0;
6742
6743 // In the following folds, inf can be replaced with the largest finite
6744 // float, if the ninf flag is set.
6745 const APFloat *C;
6746 if (match(Op1, m_APFloat(C)) &&
6747 (C->isInfinity() || (Call && Call->hasNoInfs() && C->isLargest()))) {
6748 // minnum(X, -inf) -> -inf
6749 // maxnum(X, +inf) -> +inf
6750 // minimum(X, -inf) -> -inf if nnan
6751 // maximum(X, +inf) -> +inf if nnan
6752 if (C->isNegative() == IsMin &&
6753 (!PropagateNaN || (Call && Call->hasNoNaNs())))
6754 return ConstantFP::get(ReturnType, *C);
6755
6756 // minnum(X, +inf) -> X if nnan
6757 // maxnum(X, -inf) -> X if nnan
6758 // minimum(X, +inf) -> X
6759 // maximum(X, -inf) -> X
6760 if (C->isNegative() != IsMin &&
6761 (PropagateNaN || (Call && Call->hasNoNaNs())))
6762 return Op0;
6763 }
6764
6765 // Min/max of the same operation with common operand:
6766 // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
6767 if (Value *V = foldMinimumMaximumSharedOp(IID, Op0, Op1))
6768 return V;
6769 if (Value *V = foldMinimumMaximumSharedOp(IID, Op1, Op0))
6770 return V;
6771
6772 break;
6773 }
6774 case Intrinsic::vector_extract: {
6775 // (extract_vector (insert_vector _, X, 0), 0) -> X
6776 unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue();
6777 Value *X = nullptr;
6778 if (match(Op0, m_Intrinsic<Intrinsic::vector_insert>(m_Value(), m_Value(X),
6779 m_Zero())) &&
6780 IdxN == 0 && X->getType() == ReturnType)
6781 return X;
6782
6783 break;
6784 }
6785 default:
6786 break;
6787 }
6788
6789 return nullptr;
6790}
6791
6792static Value *simplifyIntrinsic(CallBase *Call, Value *Callee,
6793 ArrayRef<Value *> Args,
6794 const SimplifyQuery &Q) {
6795 // Operand bundles should not be in Args.
6796 assert(Call->arg_size() == Args.size());
6797 unsigned NumOperands = Args.size();
6798 Function *F = cast<Function>(Callee);
6799 Intrinsic::ID IID = F->getIntrinsicID();
6800
6801 // Most of the intrinsics with no operands have some kind of side effect.
6802 // Don't simplify.
6803 if (!NumOperands) {
6804 switch (IID) {
6805 case Intrinsic::vscale: {
6806 Type *RetTy = F->getReturnType();
6807 ConstantRange CR = getVScaleRange(Call->getFunction(), 64);
6808 if (const APInt *C = CR.getSingleElement())
6809 return ConstantInt::get(RetTy, C->getZExtValue());
6810 return nullptr;
6811 }
6812 default:
6813 return nullptr;
6814 }
6815 }
6816
6817 if (NumOperands == 1)
6818 return simplifyUnaryIntrinsic(F, Args[0], Q, Call);
6819
6820 if (NumOperands == 2)
6821 return simplifyBinaryIntrinsic(IID, F->getReturnType(), Args[0], Args[1], Q,
6822 Call);
6823
6824 // Handle intrinsics with 3 or more arguments.
6825 switch (IID) {
6826 case Intrinsic::masked_load:
6827 case Intrinsic::masked_gather: {
6828 Value *MaskArg = Args[2];
6829 Value *PassthruArg = Args[3];
6830 // If the mask is all zeros or undef, the "passthru" argument is the result.
6831 if (maskIsAllZeroOrUndef(MaskArg))
6832 return PassthruArg;
6833 return nullptr;
6834 }
6835 case Intrinsic::fshl:
6836 case Intrinsic::fshr: {
6837 Value *Op0 = Args[0], *Op1 = Args[1], *ShAmtArg = Args[2];
6838
6839 // If both operands are undef, the result is undef.
6840 if (Q.isUndefValue(Op0) && Q.isUndefValue(Op1))
6841 return UndefValue::get(F->getReturnType());
6842
6843 // If shift amount is undef, assume it is zero.
6844 if (Q.isUndefValue(ShAmtArg))
6845 return Args[IID == Intrinsic::fshl ? 0 : 1];
6846
6847 const APInt *ShAmtC;
6848 if (match(ShAmtArg, m_APInt(ShAmtC))) {
6849 // If there's effectively no shift, return the 1st arg or 2nd arg.
6850 APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth());
6851 if (ShAmtC->urem(BitWidth).isZero())
6852 return Args[IID == Intrinsic::fshl ? 0 : 1];
6853 }
6854
6855 // Rotating zero by anything is zero.
6856 if (match(Op0, m_Zero()) && match(Op1, m_Zero()))
6857 return ConstantInt::getNullValue(F->getReturnType());
6858
6859 // Rotating -1 by anything is -1.
6860 if (match(Op0, m_AllOnes()) && match(Op1, m_AllOnes()))
6861 return ConstantInt::getAllOnesValue(F->getReturnType());
6862
6863 return nullptr;
6864 }
6865 case Intrinsic::experimental_constrained_fma: {
6866 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6867 if (Value *V = simplifyFPOp(Args, {}, Q, *FPI->getExceptionBehavior(),
6868 *FPI->getRoundingMode()))
6869 return V;
6870 return nullptr;
6871 }
6872 case Intrinsic::fma:
6873 case Intrinsic::fmuladd: {
6874 if (Value *V = simplifyFPOp(Args, {}, Q, fp::ebIgnore,
6875 RoundingMode::NearestTiesToEven))
6876 return V;
6877 return nullptr;
6878 }
6879 case Intrinsic::smul_fix:
6880 case Intrinsic::smul_fix_sat: {
6881 Value *Op0 = Args[0];
6882 Value *Op1 = Args[1];
6883 Value *Op2 = Args[2];
6884 Type *ReturnType = F->getReturnType();
6885
6886 // Canonicalize constant operand as Op1 (ConstantFolding handles the case
6887 // when both Op0 and Op1 are constant so we do not care about that special
6888 // case here).
6889 if (isa<Constant>(Op0))
6890 std::swap(Op0, Op1);
6891
6892 // X * 0 -> 0
6893 if (match(Op1, m_Zero()))
6894 return Constant::getNullValue(ReturnType);
6895
6896 // X * undef -> 0
6897 if (Q.isUndefValue(Op1))
6898 return Constant::getNullValue(ReturnType);
6899
6900 // X * (1 << Scale) -> X
6901 APInt ScaledOne =
6902 APInt::getOneBitSet(ReturnType->getScalarSizeInBits(),
6903 cast<ConstantInt>(Op2)->getZExtValue());
6904 if (ScaledOne.isNonNegative() && match(Op1, m_SpecificInt(ScaledOne)))
6905 return Op0;
6906
6907 return nullptr;
6908 }
6909 case Intrinsic::vector_insert: {
6910 Value *Vec = Args[0];
6911 Value *SubVec = Args[1];
6912 Value *Idx = Args[2];
6913 Type *ReturnType = F->getReturnType();
6914
6915 // (insert_vector Y, (extract_vector X, 0), 0) -> X
6916 // where: Y is X, or Y is undef
6917 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
6918 Value *X = nullptr;
6919 if (match(SubVec,
6920 m_Intrinsic<Intrinsic::vector_extract>(m_Value(X), m_Zero())) &&
6921 (Q.isUndefValue(Vec) || Vec == X) && IdxN == 0 &&
6922 X->getType() == ReturnType)
6923 return X;
6924
6925 return nullptr;
6926 }
6927 case Intrinsic::experimental_constrained_fadd: {
6928 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6929 return simplifyFAddInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6930 *FPI->getExceptionBehavior(),
6931 *FPI->getRoundingMode());
6932 }
6933 case Intrinsic::experimental_constrained_fsub: {
6934 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6935 return simplifyFSubInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6936 *FPI->getExceptionBehavior(),
6937 *FPI->getRoundingMode());
6938 }
6939 case Intrinsic::experimental_constrained_fmul: {
6940 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6941 return simplifyFMulInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6942 *FPI->getExceptionBehavior(),
6943 *FPI->getRoundingMode());
6944 }
6945 case Intrinsic::experimental_constrained_fdiv: {
6946 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6947 return simplifyFDivInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6948 *FPI->getExceptionBehavior(),
6949 *FPI->getRoundingMode());
6950 }
6951 case Intrinsic::experimental_constrained_frem: {
6952 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6953 return simplifyFRemInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6954 *FPI->getExceptionBehavior(),
6955 *FPI->getRoundingMode());
6956 }
6957 case Intrinsic::experimental_constrained_ldexp:
6958 return simplifyLdexp(Args[0], Args[1], Q, true);
6959 case Intrinsic::experimental_gc_relocate: {
6960 GCRelocateInst &GCR = *cast<GCRelocateInst>(Call);
6961 Value *DerivedPtr = GCR.getDerivedPtr();
6962 Value *BasePtr = GCR.getBasePtr();
6963
6964 // Undef is undef, even after relocation.
6965 if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
6966 return UndefValue::get(GCR.getType());
6967 }
6968
6969 if (auto *PT = dyn_cast<PointerType>(GCR.getType())) {
6970 // For now, the assumption is that the relocation of null will be null
6971 // for most any collector. If this ever changes, a corresponding hook
6972 // should be added to GCStrategy and this code should check it first.
6973 if (isa<ConstantPointerNull>(DerivedPtr)) {
6974 // Use null-pointer of gc_relocate's type to replace it.
6975 return ConstantPointerNull::get(PT);
6976 }
6977 }
6978 return nullptr;
6979 }
6980 default:
6981 return nullptr;
6982 }
6983}
6984
6986 ArrayRef<Value *> Args,
6987 const SimplifyQuery &Q) {
6988 auto *F = dyn_cast<Function>(Callee);
6989 if (!F || !canConstantFoldCallTo(Call, F))
6990 return nullptr;
6991
6992 SmallVector<Constant *, 4> ConstantArgs;
6993 ConstantArgs.reserve(Args.size());
6994 for (Value *Arg : Args) {
6995 Constant *C = dyn_cast<Constant>(Arg);
6996 if (!C) {
6997 if (isa<MetadataAsValue>(Arg))
6998 continue;
6999 return nullptr;
7000 }
7001 ConstantArgs.push_back(C);
7002 }
7003
7004 return ConstantFoldCall(Call, F, ConstantArgs, Q.TLI);
7005}
7006
7008 const SimplifyQuery &Q) {
7009 // Args should not contain operand bundle operands.
7010 assert(Call->arg_size() == Args.size());
7011
7012 // musttail calls can only be simplified if they are also DCEd.
7013 // As we can't guarantee this here, don't simplify them.
7014 if (Call->isMustTailCall())
7015 return nullptr;
7016
7017 // call undef -> poison
7018 // call null -> poison
7019 if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee))
7020 return PoisonValue::get(Call->getType());
7021
7022 if (Value *V = tryConstantFoldCall(Call, Callee, Args, Q))
7023 return V;
7024
7025 auto *F = dyn_cast<Function>(Callee);
7026 if (F && F->isIntrinsic())
7027 if (Value *Ret = simplifyIntrinsic(Call, Callee, Args, Q))
7028 return Ret;
7029
7030 return nullptr;
7031}
7032
7034 assert(isa<ConstrainedFPIntrinsic>(Call));
7035 SmallVector<Value *, 4> Args(Call->args());
7036 if (Value *V = tryConstantFoldCall(Call, Call->getCalledOperand(), Args, Q))
7037 return V;
7038 if (Value *Ret = simplifyIntrinsic(Call, Call->getCalledOperand(), Args, Q))
7039 return Ret;
7040 return nullptr;
7041}
7042
7043/// Given operands for a Freeze, see if we can fold the result.
7045 // Use a utility function defined in ValueTracking.
7047 return Op0;
7048 // We have room for improvement.
7049 return nullptr;
7050}
7051
7053 return ::simplifyFreezeInst(Op0, Q);
7054}
7055
7057 const SimplifyQuery &Q) {
7058 if (LI->isVolatile())
7059 return nullptr;
7060
7061 if (auto *PtrOpC = dyn_cast<Constant>(PtrOp))
7062 return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Q.DL);
7063
7064 // We can only fold the load if it is from a constant global with definitive
7065 // initializer. Skip expensive logic if this is not the case.
7066 auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(PtrOp));
7067 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7068 return nullptr;
7069
7070 // If GlobalVariable's initializer is uniform, then return the constant
7071 // regardless of its offset.
7072 if (Constant *C = ConstantFoldLoadFromUniformValue(GV->getInitializer(),
7073 LI->getType(), Q.DL))
7074 return C;
7075
7076 // Try to convert operand into a constant by stripping offsets while looking
7077 // through invariant.group intrinsics.
7079 PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
7080 Q.DL, Offset, /* AllowNonInbounts */ true,
7081 /* AllowInvariantGroup */ true);
7082 if (PtrOp == GV) {
7083 // Index size may have changed due to address space casts.
7084 Offset = Offset.sextOrTrunc(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()));
7085 return ConstantFoldLoadFromConstPtr(GV, LI->getType(), std::move(Offset),
7086 Q.DL);
7087 }
7088
7089 return nullptr;
7090}
7091
7092/// See if we can compute a simplified version of this instruction.
7093/// If not, this returns null.
7094
7096 ArrayRef<Value *> NewOps,
7097 const SimplifyQuery &SQ,
7098 unsigned MaxRecurse) {
7099 assert(I->getFunction() && "instruction should be inserted in a function");
7100 assert((!SQ.CxtI || SQ.CxtI->getFunction() == I->getFunction()) &&
7101 "context instruction should be in the same function");
7102
7103 const SimplifyQuery Q = SQ.CxtI ? SQ : SQ.getWithInstruction(I);
7104
7105 switch (I->getOpcode()) {
7106 default:
7107 if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); })) {
7108 SmallVector<Constant *, 8> NewConstOps(NewOps.size());
7109 transform(NewOps, NewConstOps.begin(),
7110 [](Value *V) { return cast<Constant>(V); });
7111 return ConstantFoldInstOperands(I, NewConstOps, Q.DL, Q.TLI);
7112 }
7113 return nullptr;
7114 case Instruction::FNeg:
7115 return simplifyFNegInst(NewOps[0], I->getFastMathFlags(), Q, MaxRecurse);
7116 case Instruction::FAdd:
7117 return simplifyFAddInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7118 MaxRecurse);
7119 case Instruction::Add:
7120 return simplifyAddInst(
7121 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7122 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7123 case Instruction::FSub:
7124 return simplifyFSubInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7125 MaxRecurse);
7126 case Instruction::Sub:
7127 return simplifySubInst(
7128 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7129 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7130 case Instruction::FMul:
7131 return simplifyFMulInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7132 MaxRecurse);
7133 case Instruction::Mul:
7134 return simplifyMulInst(
7135 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7136 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7137 case Instruction::SDiv:
7138 return simplifySDivInst(NewOps[0], NewOps[1],
7139 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7140 MaxRecurse);
7141 case Instruction::UDiv:
7142 return simplifyUDivInst(NewOps[0], NewOps[1],
7143 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7144 MaxRecurse);
7145 case Instruction::FDiv:
7146 return simplifyFDivInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7147 MaxRecurse);
7148 case Instruction::SRem:
7149 return simplifySRemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7150 case Instruction::URem:
7151 return simplifyURemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7152 case Instruction::FRem:
7153 return simplifyFRemInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7154 MaxRecurse);
7155 case Instruction::Shl:
7156 return simplifyShlInst(
7157 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7158 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7159 case Instruction::LShr:
7160 return simplifyLShrInst(NewOps[0], NewOps[1],
7161 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7162 MaxRecurse);
7163 case Instruction::AShr:
7164 return simplifyAShrInst(NewOps[0], NewOps[1],
7165 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7166 MaxRecurse);
7167 case Instruction::And:
7168 return simplifyAndInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7169 case Instruction::Or:
7170 return simplifyOrInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7171 case Instruction::Xor:
7172 return simplifyXorInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7173 case Instruction::ICmp:
7174 return simplifyICmpInst(cast<ICmpInst>(I)->getCmpPredicate(), NewOps[0],
7175 NewOps[1], Q, MaxRecurse);
7176 case Instruction::FCmp:
7177 return simplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), NewOps[0],
7178 NewOps[1], I->getFastMathFlags(), Q, MaxRecurse);
7179 case Instruction::Select:
7180 return simplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse);
7181 case Instruction::GetElementPtr: {
7182 auto *GEPI = cast<GetElementPtrInst>(I);
7183 return simplifyGEPInst(GEPI->getSourceElementType(), NewOps[0],
7184 ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q,
7185 MaxRecurse);
7186 }
7187 case Instruction::InsertValue: {
7188 InsertValueInst *IV = cast<InsertValueInst>(I);
7189 return simplifyInsertValueInst(NewOps[0], NewOps[1], IV->getIndices(), Q,
7190 MaxRecurse);
7191 }
7192 case Instruction::InsertElement:
7193 return simplifyInsertElementInst(NewOps[0], NewOps[1], NewOps[2], Q);
7194 case Instruction::ExtractValue: {
7195 auto *EVI = cast<ExtractValueInst>(I);
7196 return simplifyExtractValueInst(NewOps[0], EVI->getIndices(), Q,
7197 MaxRecurse);
7198 }
7199 case Instruction::ExtractElement:
7200 return simplifyExtractElementInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7201 case Instruction::ShuffleVector: {
7202 auto *SVI = cast<ShuffleVectorInst>(I);
7203 return simplifyShuffleVectorInst(NewOps[0], NewOps[1],
7204 SVI->getShuffleMask(), SVI->getType(), Q,
7205 MaxRecurse);
7206 }
7207 case Instruction::PHI:
7208 return simplifyPHINode(cast<PHINode>(I), NewOps, Q);
7209 case Instruction::Call:
7210 return simplifyCall(
7211 cast<CallInst>(I), NewOps.back(),
7212 NewOps.drop_back(1 + cast<CallInst>(I)->getNumTotalBundleOperands()), Q);
7213 case Instruction::Freeze:
7214 return llvm::simplifyFreezeInst(NewOps[0], Q);
7215#define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
7216#include "llvm/IR/Instruction.def"
7217#undef HANDLE_CAST_INST
7218 return simplifyCastInst(I->getOpcode(), NewOps[0], I->getType(), Q,
7219 MaxRecurse);
7220 case Instruction::Alloca:
7221 // No simplifications for Alloca and it can't be constant folded.
7222 return nullptr;
7223 case Instruction::Load:
7224 return simplifyLoadInst(cast<LoadInst>(I), NewOps[0], Q);
7225 }
7226}
7227
7229 ArrayRef<Value *> NewOps,
7230 const SimplifyQuery &SQ) {
7231 assert(NewOps.size() == I->getNumOperands() &&
7232 "Number of operands should match the instruction!");
7233 return ::simplifyInstructionWithOperands(I, NewOps, SQ, RecursionLimit);
7234}
7235
7237 SmallVector<Value *, 8> Ops(I->operands());
7239
7240 /// If called on unreachable code, the instruction may simplify to itself.
7241 /// Make life easier for users by detecting that case here, and returning a
7242 /// safe value instead.
7243 return Result == I ? PoisonValue::get(I->getType()) : Result;
7244}
7245
7246/// Implementation of recursive simplification through an instruction's
7247/// uses.
7248///
7249/// This is the common implementation of the recursive simplification routines.
7250/// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
7251/// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
7252/// instructions to process and attempt to simplify it using
7253/// InstructionSimplify. Recursively visited users which could not be
7254/// simplified themselves are to the optional UnsimplifiedUsers set for
7255/// further processing by the caller.
7256///
7257/// This routine returns 'true' only when *it* simplifies something. The passed
7258/// in simplified value does not count toward this.
7260 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7261 const DominatorTree *DT, AssumptionCache *AC,
7262 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) {
7263 bool Simplified = false;
7265 const DataLayout &DL = I->getDataLayout();
7266
7267 // If we have an explicit value to collapse to, do that round of the
7268 // simplification loop by hand initially.
7269 if (SimpleV) {
7270 for (User *U : I->users())
7271 if (U != I)
7272 Worklist.insert(cast<Instruction>(U));
7273
7274 // Replace the instruction with its simplified value.
7275 I->replaceAllUsesWith(SimpleV);
7276
7277 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7278 I->eraseFromParent();
7279 } else {
7280 Worklist.insert(I);
7281 }
7282
7283 // Note that we must test the size on each iteration, the worklist can grow.
7284 for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
7285 I = Worklist[Idx];
7286
7287 // See if this instruction simplifies.
7288 SimpleV = simplifyInstruction(I, {DL, TLI, DT, AC});
7289 if (!SimpleV) {
7290 if (UnsimplifiedUsers)
7291 UnsimplifiedUsers->insert(I);
7292 continue;
7293 }
7294
7295 Simplified = true;
7296
7297 // Stash away all the uses of the old instruction so we can check them for
7298 // recursive simplifications after a RAUW. This is cheaper than checking all
7299 // uses of To on the recursive step in most cases.
7300 for (User *U : I->users())
7301 Worklist.insert(cast<Instruction>(U));
7302
7303 // Replace the instruction with its simplified value.
7304 I->replaceAllUsesWith(SimpleV);
7305
7306 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7307 I->eraseFromParent();
7308 }
7309 return Simplified;
7310}
7311
7313 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7314 const DominatorTree *DT, AssumptionCache *AC,
7315 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers) {
7316 assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
7317 assert(SimpleV && "Must provide a simplified value.");
7318 return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC,
7319 UnsimplifiedUsers);
7320}
7321
7322namespace llvm {
7324 auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>();
7325 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
7326 auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
7327 auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
7328 auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>();
7329 auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;
7330 return {F.getDataLayout(), TLI, DT, AC};
7331}
7332
7334 const DataLayout &DL) {
7335 return {DL, &AR.TLI, &AR.DT, &AR.AC};
7336}
7337
7338template <class T, class... TArgs>
7340 Function &F) {
7341 auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F);
7342 auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F);
7343 auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F);
7344 return {F.getDataLayout(), TLI, DT, AC};
7345}
7347 Function &);
7348
7350 if (!CanUseUndef)
7351 return false;
7352
7353 return match(V, m_Undef());
7354}
7355
7356} // namespace llvm
7357
7358void 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:1187
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