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