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