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