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